Motor Driver Evaluation Kit NEVB-MTR1-t01-1.3.1
Firmware for NEVB-MTR1-KIT1 for trapezoidal control of BLDC motors using Hall-effect sensors
Loading...
Searching...
No Matches
scpi.cpp File Reference

SCPI implementation source file. More...

#include "scpi.h"
#include "config.h"
#include "scpi_config.h"
#include "scpi_helper.h"
Include dependency graph for scpi.cpp:

Go to the source code of this file.

Functions

static void ScpiCoreIdnQ (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Implements the *IDN? (Identification Query) command.
 
static void ScpiSystemErrorCountQ (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Implements the SYSTem:ERRor:COUNt? (System Error Count Query) command.
 
static void ScpiSystemErrorNextQ (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Implements the SYSTem:ERRor? (System Error Next Query) command.
 
static void GetMotorEnable (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Retrieves the current motor enable state.
 
static void ConfigureMotorEnable (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Configures the motor's enable state.
 
static void GetConfigureMotorDutyCycleSource (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Retrieves the configured motor's duty cycle source.
 
static void ConfigureMotorDutyCycle (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Configures the motor's speed input by setting the duty cycle.
 
static void ConfigureMotorFrequency (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Configures the motor's operating frequency.
 
static void GetConfigureMotorFrequency (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Retrieves the configured motor frequency.
 
static void ConfigureMotorDirection (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Sets the motor's direction based on the input parameter.
 
static void GetConfigureMotorDirection (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Retrieves the configured direction of the motor.
 
static void MeasureMotorSpeed (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures the motor speed.
 
static void MeasureMotorCurrentVBus (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the motor's VBUS current.
 
static void MeasureMotorCurrentPhaseU (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the motor's phase U current.
 
static void MeasureMotorCurrentPhaseV (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the motor's phase V current.
 
static void MeasureMotorCurrentPhaseW (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the motor's phase W current.
 
static void MeasureMotorDirection (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and reports the current direction of the motor.
 
static void MeasureMotorVoltage (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the VBUS voltage of the motor.
 
static void MeasureGateDutyCycle (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Measures and returns the gate PWM duty cycle as a percentage.
 
static void ConfigureMotorDutyCycleSource (SCPI_C commands, SCPI_P parameters, Stream &interface)
 Configures the motor's speed input source.
 
void ScpiInit (void)
 Initializes the SCPI command parser and registers all supported commands.
 
void ScpiInput (Stream &interface)
 Processes incoming data from a serial interface for SCPI commands.
 

Variables

SCPI_Parser scpiParser
 Speed input source options array.
 
const SCPI_choice_def_t motorDirections [2]
 Array defining the possible motor directions for SCPI commands.
 
const SCPI_choice_def_t inputSources [2]
 Array defining the possible input sources for speed/duty cycle control.
 

Detailed Description

SCPI implementation source file.

This file contains the command handlers for the SCPI parser to recognize and execute.

Most of this code was originally written by Diego González Chávez as part of the Vrekrer SCPI Parser project.

It has been merged into the main codebase for customization and linking limitations of the Arduino IDE.

Author
Nexperia: http://www.nexperia.com
Support Page
For additional support, visit: https://www.nexperia.com/support
Author
Aanas Sayed
Date
2025/04/21


Definition in file scpi.cpp.

Function Documentation

◆ ConfigureMotorDirection()

static void ConfigureMotorDirection ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Sets the motor's direction based on the input parameter.

This function reads a direction parameter ('FORW' or 'REVE') and sets the motor's direction accordingly by manipulating the DIRECTION_COMMAND_PIN on PORTD.

In remote mode, the DIRECTION_COMMAND_PIN is configured as an output. Setting or clearing the pin triggers the relevant software interrupt as it would in normal operation.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters containing the direction choice.
interfaceThe serial interface (not used).

Definition at line 498 of file scpi.cpp.

499{
500 uint8_t param;
501
502 // read first parameter if present
504 {
506 return;
507 }
508
509 if (param)
510 {
511 // Set the direction pin if param is 1 (DIRECTION_REVERSE)
512 PORTD |= (1 << DIRECTION_COMMAND_PIN);
513 }
514 else
515 {
516 // Clear the direction pin if param is 0 (DIRECTION_FORWARD)
517 PORTD &= ~(1 << DIRECTION_COMMAND_PIN);
518 }
519 scpiParser.last_error = ErrorCode::NoError;
520}
#define DIRECTION_COMMAND_PIN
Pin where direction command input is located.
Definition config.h:839
SCPI_Parser scpiParser
Speed input source options array.
Definition scpi.cpp:62
const SCPI_choice_def_t motorDirections[2]
Array defining the possible motor directions for SCPI commands.
Definition scpi.cpp:705
#define MOTOR_DIRECTION_OPTIONS
Motor direction options array.
Definition scpi.h:35
uint8_t ScpiParamChoice(SCPI_P &parameters, const SCPI_choice_def_t *options, size_t optionsSize, uint8_t &param)
Extracts a choice parameter from the SCPI parameter list and maps it to a numerical tag.
@ MissingOrInvalidParameter
A required parameter was missing or an invalid parameter was provided.
Definition scpi_types.h:132
@ NoError
No error occurred.
Definition scpi_types.h:120
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConfigureMotorDutyCycle()

static void ConfigureMotorDutyCycle ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Configures the motor's speed input by setting the duty cycle.

This function reads a double parameter (0.0 to 100.0) from the SCPI command and sets the motor's duty cycle accordingly.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters containing the duty cycle value.
interfaceThe serial interface (not used).

Definition at line 391 of file scpi.cpp.

392{
393 double param;
394 if (!ScpiParamDouble(parameters, param) || param < 0.0 || param > 100.0)
395 {
397 return;
398 }
399 speedInput = param;
400 scpiParser.last_error = ErrorCode::NoError;
401}
volatile uint8_t speedInput
The most recent "speed" input measurement.
Definition main.ino:101
uint8_t ScpiParamDouble(SCPI_P &parameters, double &param)
Extracts a double-precision floating-point parameter from the SCPI parameter list.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConfigureMotorDutyCycleSource()

static void ConfigureMotorDutyCycleSource ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Configures the motor's speed input source.

This function reads a choice parameter ('LOCAL' or 'REMOTE') from the SCPI command and sets the motor's speed input source accordingly, corresponding to SPEED_INPUT_SOURCE_LOCAL and SPEED_INPUT_SOURCE_REMOTE.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters containing the input source choice.
interfaceThe serial interface (not used).

Definition at line 321 of file scpi.cpp.

325{
326 uint8_t param;
327
328 // read first parameter if present
329 if (!ScpiParamChoice(parameters, inputSources, INPUT_SOURCE_OPTIONS, param))
330 {
332 return;
333 }
334
335 if (param == SPEED_INPUT_SOURCE_LOCAL)
336 {
337 motorConfigs.speedInputSource = SPEED_INPUT_SOURCE_LOCAL;
338 return;
339 }
340 else
341 {
342 motorConfigs.speedInputSource = SPEED_INPUT_SOURCE_REMOTE;
343 speedInput = 0;
344 return;
345 }
346 scpiParser.last_error = ErrorCode::NoError;
347}
#define SPEED_INPUT_SOURCE_LOCAL
Speed input source - Local or speed input pin.
Definition config.h:849
#define SPEED_INPUT_SOURCE_REMOTE
Speed input source - Remote input.
Definition config.h:851
volatile motorconfigs_t motorConfigs
Motor Configs.
Definition main.ino:66
const SCPI_choice_def_t inputSources[2]
Array defining the possible input sources for speed/duty cycle control.
Definition scpi.cpp:718
#define INPUT_SOURCE_OPTIONS
Motor direction options array.
Definition scpi.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConfigureMotorEnable()

static void ConfigureMotorEnable ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Configures the motor's enable state.

This function reads a boolean parameter (0 or 1) from the SCPI command and sets the motor's enable state accordingly.

In remote mode, the ENABLE_PIN is configured as an output. The motor is then enabled or disabled by setting or clearing the ENABLE_PIN on the PORTD register, which triggers the relevant software interrupt as it would in normal operation.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters containing the boolean enable value.
interfaceThe serial interface (not used).

Definition at line 287 of file scpi.cpp.

288{
289 bool param;
290 if (!ScpiParamBool(parameters, param))
291 {
293 return;
294 }
295 if (param)
296 {
297 // Set the enable pin
298 PORTD |= (1 << ENABLE_PIN);
299 }
300 else
301 {
302 // Clear the enable pin
303 PORTD &= ~(1 << ENABLE_PIN);
304 }
305
306 scpiParser.last_error = ErrorCode::NoError;
307}
#define ENABLE_PIN
Enable input pin.
Definition config.h:841
uint8_t ScpiParamBool(SCPI_P &parameters, bool &param)
Extracts a boolean parameter ('ON', '1', 'OFF', or '0') from the SCPI parameter list.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConfigureMotorFrequency()

static void ConfigureMotorFrequency ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Configures the motor's operating frequency.

This function sets the motor's operating frequency based on the input parameter. It validates the frequency range (7183 to 100000 Hz), updates the motor configuration, and reinitializes timers after ensuring the motor is stopped.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters containing the frequency value in Hz.
interfaceThe serial interface (not used).

Definition at line 439 of file scpi.cpp.

440{
441 // Clear the enable pin to stop the motor before changing frequency
442 PORTD &= ~(1 << ENABLE_PIN);
443
444 uint32_t param;
445
446 // Read first parameter if present and within range
447 if (!ScpiParamUInt32(parameters, param) || param < F_MOSFET_MIN || param > F_MOSFET_MAX)
448 {
450 return;
451 }
452
453 // Reload the configs
454 motorConfigs.tim4Freq = param;
455 motorConfigs.tim4Top = (uint16_t)TIM4_TOP(motorConfigs.tim4Freq);
456
457 // Wait until motor is stopped
458 while (faultFlags.motorStopped == FALSE)
459 {
460 ;
461 }
462
463 // Re-init timers with the new frequency
464 TimersInit();
465 scpiParser.last_error = ErrorCode::NoError;
466}
#define TIM4_TOP(tim4Freq)
Definition config.h:1302
#define FALSE
FALSE constant value.
Definition config.h:745
#define F_MOSFET_MAX
Maximum allowed gate switching frequency.
Definition config.h:99
void TimersInit(void)
Initializes and synchronizes Timers.
Definition main.ino:455
uint8_t ScpiParamUInt32(SCPI_P &parameters, uint32_t &param)
Extracts an unsigned 32-bit integer parameter from the SCPI parameter list.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConfigureMotorDirection()

static void GetConfigureMotorDirection ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Retrieves the configured direction of the motor.

This function queries the desired direction of the motor and returns a textual representation ('FORW' or 'REVE') based on the motorFlags.desiredDirection setting.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 533 of file scpi.cpp.

534{
535 String name;
536 ScpiChoiceToName(motorDirections, MOTOR_DIRECTION_OPTIONS, motorFlags.desiredDirection, name);
537 interface.println(name);
538}
uint8_t ScpiChoiceToName(const SCPI_choice_def_t *options, size_t optionsSize, int8_t value, String &name)
Converts a numerical choice tag back to its string representation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConfigureMotorDutyCycleSource()

static void GetConfigureMotorDutyCycleSource ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Retrieves the configured motor's duty cycle source.

This function queries the currently configured source for the motor's duty cycle (either 'LOCAL' or 'REMOTE') and returns it as a string.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 360 of file scpi.cpp.

374{
375 String name;
377 interface.println(name);
378}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConfigureMotorFrequency()

static void GetConfigureMotorFrequency ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Retrieves the configured motor frequency.

This function queries the current frequency configuration of the motor and returns it as a double value representing the frequency in Hertz.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 478 of file scpi.cpp.

479{
480 interface.println(motorConfigs.tim4Freq);
481}
Here is the caller graph for this function:

◆ GetMotorEnable()

static void GetMotorEnable ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Retrieves the current motor enable state.

This function queries the current enable state of the motor and returns it as a boolean value (1 for enabled, 0 for disabled).

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 267 of file scpi.cpp.

268{
269 interface.println(motorFlags.enable);
270}
Here is the caller graph for this function:

◆ MeasureGateDutyCycle()

static void MeasureGateDutyCycle ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the gate PWM duty cycle as a percentage.

This function reads the current PWM duty cycle from Timer4's compare register (OCR4A) and calculates the percentage relative to the timer's top value. Interrupts are briefly disabled to ensure atomic reading of the 16-bit duty cycle value.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 679 of file scpi.cpp.

680{
681 if (motorFlags.enable == TRUE)
682 {
683 // Reading 16 bit register so disabling interrupts for atomic operation
684 cli();
685 uint16_t duty = 0xff & OCR4A;
686 duty |= (0x03 & TC4H) << 8;
687 sei();
688
689 interface.println((double)duty / (double)motorConfigs.tim4Top * 100.0);
690 }
691 else
692 {
693 interface.println(0.0);
694 }
695}
#define TRUE
TRUE constant value, defined to be compatible with comparisons.
Definition config.h:747
Here is the caller graph for this function:

◆ MeasureMotorCurrentPhaseU()

static void MeasureMotorCurrentPhaseU ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the motor's phase U current.

This function reads the ADC value for the phase U current sense, converts it to Amperes using the known gain and sense resistor value. It accounts for the ADC offset (typically around 511 for a centered reading).

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 590 of file scpi.cpp.

591{
592 interface.println(((double)(iphaseU - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
593}
#define IPHASE_SENSE_RESISTOR
Hi-side Current (IBUS) Sense Resistor Value.
Definition config.h:294
#define IPHASE_GAIN
In-line Phase Current Gain for Current Measurement.
Definition config.h:276
volatile int16_t iphaseU
In-line Phase U current current measurement (Register Value).
Definition main.ino:167
Here is the caller graph for this function:

◆ MeasureMotorCurrentPhaseV()

static void MeasureMotorCurrentPhaseV ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the motor's phase V current.

This function reads the ADC value for the phase V current sense, converts it to Amperes using the known gain and sense resistor value. It accounts for the ADC offset (typically around 511 for a centered reading).

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 606 of file scpi.cpp.

607{
608 interface.println(((double)(iphaseV - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
609}
volatile int16_t iphaseV
In-line Phase V current current measurement (Register Value).
Definition main.ino:196
Here is the caller graph for this function:

◆ MeasureMotorCurrentPhaseW()

static void MeasureMotorCurrentPhaseW ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the motor's phase W current.

This function reads the ADC value for the phase W current sense, converts it to Amperes using the known gain and sense resistor value. It accounts for the ADC offset (typically around 511 for a centered reading).

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 622 of file scpi.cpp.

623{
624 interface.println(((double)(iphaseW - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
625}
volatile int16_t iphaseW
In-line Phase W current current measurement (Register Value).
Definition main.ino:226
Here is the caller graph for this function:

◆ MeasureMotorCurrentVBus()

static void MeasureMotorCurrentVBus ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the motor's VBUS current.

This function reads the ADC value for the VBUS current sense, converts it to Amperes using the known gain and sense resistor value.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 574 of file scpi.cpp.

575{
576 interface.println(((double)ibus * 5.0 * 1000000.0) / ((double)1023.0 * IBUS_GAIN * IBUS_SENSE_RESISTOR));
577}
#define IBUS_GAIN
Hi-side Current (IBUS) Gain for Current Measurement.
Definition config.h:314
#define IBUS_SENSE_RESISTOR
Hi-side Current (IBUS) Sense Resistor Value.
Definition config.h:332
volatile uint16_t ibus
Hi-side Current (IBUS) measurement (Register Value).
Definition main.ino:137
Here is the caller graph for this function:

◆ MeasureMotorDirection()

static void MeasureMotorDirection ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and reports the current direction of the motor.

This function checks the motorFlags.actualDirection and returns a textual representation ('FORW', 'REVE', or 'UNKN') based on the motor's sensed direction.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 638 of file scpi.cpp.

639{
640 if (motorFlags.actualDirection == DIRECTION_UNKNOWN)
641 {
642 interface.println(F("UNKN"));
643 }
644 else
645 {
646 String name;
647 ScpiChoiceToName(motorDirections, MOTOR_DIRECTION_OPTIONS, motorFlags.actualDirection, name);
648 interface.println(name);
649 }
650}
#define DIRECTION_UNKNOWN
Unknown direction flag value.
Definition config.h:789
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MeasureMotorSpeed()

static void MeasureMotorSpeed ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures the motor speed.

This function calculates and returns the motor speed in revolutions per minute (RPM). It uses the time difference between the last two commutation events and the configured motor frequency and pole count for the calculation.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 551 of file scpi.cpp.

552{
553 if (lastCommutationTicks == 0xffff)
554 {
555 interface.println(0.0);
556 }
557 else
558 {
559 interface.println(
560 (motorConfigs.tim4Freq * 20.0) / (lastCommutationTicks * MOTOR_POLES));
561 }
562}
#define MOTOR_POLES
Number of poles in the motor.
Definition config.h:85
volatile uint16_t lastCommutationTicks
The number of 'ticks' between two hall sensor changes (store).
Definition main.ino:94
Here is the caller graph for this function:

◆ MeasureMotorVoltage()

static void MeasureMotorVoltage ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Measures and returns the VBUS voltage of the motor.

This function reads the ADC value for the VBUS voltage reference and converts it to Volts using the voltage divider resistor values.

Parameters
commandsThe SCPI commands (not used).
parametersThe SCPI parameters (not used).
interfaceThe serial interface to write the response to.

Definition at line 662 of file scpi.cpp.

663{
664 interface.println(((double)vbusVref * 5.0 * (VBUS_RTOP + VBUS_RBOTTOM)) / ((double)1023.0 * VBUS_RBOTTOM));
665}
#define VBUS_RBOTTOM
Bottom resistor value in the VBUS voltage potential divider.
Definition config.h:655
#define VBUS_RTOP
Top resistor value in the VBUS voltage potential divider.
Definition config.h:635
volatile uint16_t vbusVref
VBUS voltage measurement (Register Value)
Definition main.ino:255
Here is the caller graph for this function:

◆ ScpiCoreIdnQ()

static void ScpiCoreIdnQ ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Implements the *IDN? (Identification Query) command.

This function responds to the *IDN? command by sending the manufacturer, model, serial number (if defined), and firmware revision of the device. The identification string is formatted as: <Manufacturer>,<Model>,<SerialNumber>,<FirmwareRevision>.

Parameters
commandsThe parsed SCPI commands (not used).
parametersThe parsed SCPI parameters (not used).
interfaceThe serial stream interface to write the response to.

Definition at line 138 of file scpi.cpp.

139{
140 interface.print(F(SCPI_IDN_MANUFACTURER));
141 interface.print(F(","));
142 interface.print(F(SCPI_IDN_MODEL));
143 interface.print(F(","));
144 interface.print((unsigned long)MOTOR_POLES, HEX);
145 interface.print('-');
146 interface.print((unsigned long)F_MOSFET, HEX);
147 interface.print('-');
148 interface.print((unsigned long)DEAD_TIME, HEX);
149 interface.print('-');
150 interface.print((unsigned long)EMULATE_HALL, HEX);
151 interface.print('-');
152 interface.print((unsigned long)TIM3_FREQ, HEX);
153 interface.print('-');
154 interface.print((unsigned long)COMMUTATION_TICKS_STOPPED, HEX);
155 interface.print('-');
156 interface.print((unsigned long)TURN_OFF_MODE, HEX);
157 interface.print('-');
158 interface.print((unsigned long)IPHASE_GAIN, HEX);
159 interface.print('-');
160 interface.print((unsigned long)IPHASE_SENSE_RESISTOR, HEX);
161 interface.print('-');
162 interface.print((unsigned long)IBUS_GAIN, HEX);
163 interface.print('-');
164 interface.print((unsigned long)IBUS_SENSE_RESISTOR, HEX);
165 interface.print('-');
166 interface.print((unsigned long)IBUS_WARNING_THRESHOLD, HEX);
167 interface.print('-');
168 interface.print((unsigned long)IBUS_ERROR_THRESHOLD, HEX);
169 interface.print('-');
170 interface.print((unsigned long)IBUS_FAULT_ENABLE, HEX);
171 interface.print('-');
172 interface.print((unsigned long)SPEED_CONTROL_METHOD, HEX);
173 interface.print('-');
174 interface.print((unsigned long)SPEED_CONTROLLER_TIME_BASE, HEX);
175 interface.print('-');
176 interface.print((unsigned long)SPEED_CONTROLLER_MAX_DELTA, HEX);
177 interface.print('-');
178 interface.print((unsigned long)SPEED_CONTROLLER_MAX_SPEED, HEX);
179 interface.print('-');
180 interface.print((unsigned long)PID_K_P, HEX);
181 interface.print('-');
182 interface.print((unsigned long)PID_K_I, HEX);
183 interface.print('-');
184 interface.print((unsigned long)PID_K_D_ENABLE, HEX);
185 interface.print('-');
186 interface.print((unsigned long)PID_K_D, HEX);
187 interface.print('-');
188 interface.print((unsigned long)VBUS_RTOP, HEX);
189 interface.print('-');
190 interface.print((unsigned long)VBUS_RBOTTOM, HEX);
191 interface.print('-');
192 interface.print((unsigned long)WAIT_FOR_BOARD, HEX);
193 interface.print('-');
194 interface.print((unsigned long)REMOTE_DEBUG_MODE, HEX);
195 interface.print('-');
196 interface.print((unsigned long)PID_MAX_I_TERM, HEX);
197 interface.print('-');
198 interface.print((unsigned long)PID_OUTPUT_MAX, HEX);
199 interface.print('-');
200 interface.print((unsigned long)VBUS_MIN_THRESHOLD, HEX);
201 interface.print(F(","));
202 interface.println(F(SCPI_IDN_FIRMWARE_VERSION));
203}
#define IBUS_WARNING_THRESHOLD
Hi-side Current (IBUS) Warning Threshold (Register Value)
Definition config.h:367
#define TURN_OFF_MODE
Turn Off Mode.
Definition config.h:259
#define PID_K_P
PID Controller Proportional Gain Constant (Only for Closed Loop)
Definition config.h:509
#define EMULATE_HALL
Emulate Motor Spinning.
Definition config.h:220
#define SPEED_CONTROL_METHOD
Speed Control Method.
Definition config.h:433
#define F_MOSFET
Desired Switching Frequency for MOSFET Gate Signals.
Definition config.h:157
#define IBUS_FAULT_ENABLE
Hi-side Current (IBUS) Fault Enable.
Definition config.h:419
#define IBUS_ERROR_THRESHOLD
Hi-side Current (IBUS) Error Threshold (Register Value)
Definition config.h:406
#define WAIT_FOR_BOARD
Wait for inverter board connection before starting execution.
Definition config.h:719
#define VBUS_MIN_THRESHOLD
Minimum VBUS Threshold for Motor Operation (Register Value)
Definition config.h:692
#define SPEED_CONTROLLER_MAX_DELTA
Speed Controller Maximum Delta (Applicable for Open Loop Control)
Definition config.h:469
#define PID_MAX_I_TERM
Maximum PID Integrator Sum (Anti-Windup Limit)
Definition config.h:594
#define TIM3_FREQ
Desired Electrical Rotational Frequency for "Emulated" Motor.
Definition config.h:236
#define PID_OUTPUT_MAX
Maximum PID Controller Output (Only for Closed Loop)
Definition config.h:615
#define PID_K_D
PID Controller Derivative Gain Constant (Only for Closed Loop)
Definition config.h:566
#define SPEED_CONTROLLER_MAX_SPEED
Speed Controller Maximum Speed.
Definition config.h:490
#define PID_K_D_ENABLE
PID Controller Derivative Control Enable Flag (Only for Closed Loop)
Definition config.h:547
#define SPEED_CONTROLLER_TIME_BASE
Speed Controller Time Base.
Definition config.h:448
#define COMMUTATION_TICKS_STOPPED
Commutation Stopped Limit.
Definition config.h:246
#define PID_K_I
PID Controller Integral Gain Constant (Only for Closed Loop)
Definition config.h:528
#define REMOTE_DEBUG_MODE
Set the remote debug mode.
Definition config.h:730
#define DEAD_TIME
Dead Time Specification.
Definition config.h:186
#define SCPI_IDN_MODEL
Model number identification string for the *IDN? command.
#define SCPI_IDN_MANUFACTURER
Manufacturer identification string for the *IDN? command.
#define SCPI_IDN_FIRMWARE_VERSION
Firmware version identification string for the *IDN? command.
Here is the caller graph for this function:

◆ ScpiInit()

void ScpiInit ( void )

Initializes the SCPI command parser and registers all supported commands.

This function sets up the SCPI parser instance and registers the core IEEE mandated commands, required SCPI commands, and the custom motor control and measurement commands.

Definition at line 71 of file scpi.cpp.

72{
73 /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */
74 // "*CLS" and "*RST" are not supported
75 scpiParser.RegisterCommand(F("*IDN?"), &ScpiCoreIdnQ);
76
77 /* Required SCPI commands (SCPI std V1999.0 4.2.1) */
78 // "SYSTem:VERSion?" and "SYSTem:ERRor:NEXT?" are not supported
79 scpiParser.SetCommandTreeBase(F("SYSTem"));
80 scpiParser.RegisterCommand(F(":ERRor?"), &ScpiSystemErrorNextQ);
81 scpiParser.RegisterCommand(F(":ERRor:COUNt?"), &ScpiSystemErrorCountQ);
82
83 /* Motor Configuration Commands */
84 scpiParser.SetCommandTreeBase(F("CONFigure"));
85 scpiParser.RegisterCommand(F(":ENABle"), &ConfigureMotorEnable);
86 scpiParser.RegisterCommand(F(":ENABle?"), &GetMotorEnable);
87#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_OPEN_LOOP)
88 scpiParser.RegisterCommand(F(":DUTYcycle:SOURce"), &ConfigureMotorDutyCycleSource);
89 scpiParser.RegisterCommand(F(":DUTYcycle:SOURce?"), &GetConfigureMotorDutyCycleSource);
90 scpiParser.RegisterCommand(F(":DUTYcycle"), &ConfigureMotorDutyCycle);
91#else
92 scpiParser.RegisterCommand(F(":SPEED:SOURce"), &ConfigureMotorSpeedSource);
93 scpiParser.RegisterCommand(F(":SPEED:SOURce?"), &GetMotorSpeedSource);
94 scpiParser.RegisterCommand(F(":SPEED"), &ConfigureMotorSpeed);
95#endif
96 scpiParser.RegisterCommand(F(":FREQuency"), &ConfigureMotorFrequency);
97 scpiParser.RegisterCommand(F(":FREQuency?"), &GetConfigureMotorFrequency);
98 scpiParser.RegisterCommand(F(":DIREction"), &ConfigureMotorDirection);
99 scpiParser.RegisterCommand(F(":DIREction?"), &GetConfigureMotorDirection);
100
101 /* Motor Measurement Commands */
102 scpiParser.SetCommandTreeBase(F("MEASure"));
103 scpiParser.RegisterCommand(F(":SPEEd?"), &MeasureMotorSpeed);
104 scpiParser.RegisterCommand(F(":CURRent:IBUS?"), &MeasureMotorCurrentVBus);
105 scpiParser.RegisterCommand(F(":CURRent:IPHU?"), &MeasureMotorCurrentPhaseU);
106 scpiParser.RegisterCommand(F(":CURRent:IPHV?"), &MeasureMotorCurrentPhaseV);
107 scpiParser.RegisterCommand(F(":CURRent:IPHW?"), &MeasureMotorCurrentPhaseW);
108 scpiParser.RegisterCommand(F(":VOLTage?"), &MeasureMotorVoltage);
109 scpiParser.RegisterCommand(F(":DIREction?"), &MeasureMotorDirection);
110 scpiParser.RegisterCommand(F(":DUTYcycle?"), &MeasureGateDutyCycle);
111}
static void GetConfigureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured direction of the motor.
Definition scpi.cpp:533
static void GetConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured motor's duty cycle source.
Definition scpi.cpp:360
static void MeasureMotorCurrentPhaseV(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase V current.
Definition scpi.cpp:606
static void ScpiSystemErrorNextQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor? (System Error Next Query) command.
Definition scpi.cpp:232
static void MeasureMotorSpeed(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures the motor speed.
Definition scpi.cpp:551
static void MeasureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and reports the current direction of the motor.
Definition scpi.cpp:638
static void ScpiSystemErrorCountQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor:COUNt? (System Error Count Query) command.
Definition scpi.cpp:216
static void MeasureMotorCurrentPhaseU(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase U current.
Definition scpi.cpp:590
static void MeasureMotorCurrentVBus(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's VBUS current.
Definition scpi.cpp:574
static void ConfigureMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's enable state.
Definition scpi.cpp:287
static void ScpiCoreIdnQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the *IDN? (Identification Query) command.
Definition scpi.cpp:138
static void GetConfigureMotorFrequency(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured motor frequency.
Definition scpi.cpp:478
static void ConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's speed input source.
Definition scpi.cpp:321
static void GetMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the current motor enable state.
Definition scpi.cpp:267
static void ConfigureMotorDutyCycle(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's speed input by setting the duty cycle.
Definition scpi.cpp:391
static void MeasureMotorCurrentPhaseW(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase W current.
Definition scpi.cpp:622
static void ConfigureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sets the motor's direction based on the input parameter.
Definition scpi.cpp:498
static void MeasureMotorVoltage(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the VBUS voltage of the motor.
Definition scpi.cpp:662
static void MeasureGateDutyCycle(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the gate PWM duty cycle as a percentage.
Definition scpi.cpp:679
static void ConfigureMotorFrequency(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's operating frequency.
Definition scpi.cpp:439
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ScpiInput()

void ScpiInput ( Stream & interface)

Processes incoming data from a serial interface for SCPI commands.

This function takes a Stream object (like Serial) and processes any received data, looking for complete SCPI commands terminated by a newline character. It then passes the command to the SCPI parser for execution.

Parameters
interfaceThe serial stream interface to read commands from.

Definition at line 122 of file scpi.cpp.

123{
124 scpiParser.ProcessInput(interface, SCPI_CMD_TERM);
125}
#define SCPI_CMD_TERM
Command termination character used to identify the end of a SCPI command.
Here is the caller graph for this function:

◆ ScpiSystemErrorCountQ()

static void ScpiSystemErrorCountQ ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Implements the SYSTem:ERRor:COUNt? (System Error Count Query) command.

This function returns the number of errors currently in the error queue. In this simplified implementation, it returns 1 if there was a last error, and 0 otherwise.

Parameters
commandsThe parsed SCPI commands (not used).
parametersThe parsed SCPI parameters (not used).
interfaceThe serial stream interface to write the response to.

Definition at line 216 of file scpi.cpp.

217{
218 interface.println(scpiParser.last_error == ErrorCode::NoError ? 0 : 1);
219}
Here is the caller graph for this function:

◆ ScpiSystemErrorNextQ()

static void ScpiSystemErrorNextQ ( SCPI_C commands,
SCPI_P parameters,
Stream & interface )
static

Implements the SYSTem:ERRor? (System Error Next Query) command.

This function returns the oldest error from the error queue and clears it. In this implementation, it returns a textual description of the last recorded error and then clears the last_error flag.

Parameters
commandsThe parsed SCPI commands (not used).
parametersThe parsed SCPI parameters (not used).
interfaceThe serial stream interface to write the response to.

Definition at line 232 of file scpi.cpp.

233{
234 switch (scpiParser.last_error)
235 {
237 interface.println(F("Buffer overflow error"));
238 break;
240 interface.println(F("Communication timeout error"));
241 break;
243 interface.println(F("Unknown command received"));
244 break;
246 interface.println(F("No Error"));
247 break;
249 interface.println(F("Missing or invalid parameter"));
250 break;
251 default:
252 interface.println(F("Unknown error"));
253 }
254 scpiParser.last_error = ErrorCode::NoError;
255}
@ BufferOverflow
The message buffer was exceeded during message reception.
Definition scpi_types.h:129
@ Timeout
A timeout occurred before receiving the expected termination characters.
Definition scpi_types.h:126
@ UnknownCommand
An unknown command was received that could not be matched to a known handler.
Definition scpi_types.h:123
Here is the caller graph for this function:

Variable Documentation

◆ inputSources

const SCPI_choice_def_t inputSources[2]
Initial value:
= {
{"LOCA", "l", 0 },
{"REMO", "te", 1 },
}

Array defining the possible input sources for speed/duty cycle control.

Number of speed input source options.

This array is used by the SCPI parser to interpret and represent the source of the motor's speed or duty cycle control ('LOCA' for local, 'REMO' for remote). Each entry associates a textual representation with a numerical value (e.g., SPEED_INPUT_SOURCE_LOCAL, SPEED_INPUT_SOURCE_REMOTE).

Definition at line 718 of file scpi.cpp.

718 {
719 {"LOCA", "l", SPEED_INPUT_SOURCE_LOCAL},
720 {"REMO", "te", SPEED_INPUT_SOURCE_REMOTE},
721};

◆ motorDirections

const SCPI_choice_def_t motorDirections[2]
Initial value:
= {
{"FORW", "ard", 0 },
{"REVE", "rse", 1 },
}

Array defining the possible motor directions for SCPI commands.

Number of motor direction options.

This array is used by the SCPI parser to interpret and represent the motor's direction ('FORW' for forward, 'REVE' for reverse). Each entry in the array associates a textual representation with a numerical value defined elsewhere (e.g., DIRECTION_FORWARD, DIRECTION_REVERSE).

Definition at line 705 of file scpi.cpp.

705 {
706 {"FORW", "ard", DIRECTION_FORWARD},
707 {"REVE", "rse", DIRECTION_REVERSE},
708};
#define DIRECTION_FORWARD
Forward direction flag value.
Definition config.h:785
#define DIRECTION_REVERSE
Reverse direction flag value.
Definition config.h:787

◆ scpiParser

SCPI_Parser scpiParser

Speed input source options array.

Definition at line 62 of file scpi.cpp.