Motor Driver Evaluation Kit NEVB-MTR1-t01-1.0.0
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 447 of file scpi.cpp.

448{
449 uint8_t param;
450
451 // read first parameter if present
453 {
455 return;
456 }
457
458 if (param)
459 {
460 // Set the direction pin if param is 1 (DIRECTION_REVERSE)
461 PORTD |= (1 << DIRECTION_COMMAND_PIN);
462 }
463 else
464 {
465 // Clear the direction pin if param is 0 (DIRECTION_FORWARD)
466 PORTD &= ~(1 << DIRECTION_COMMAND_PIN);
467 }
468 scpiParser.last_error = ErrorCode::NoError;
469}
#define DIRECTION_COMMAND_PIN
Pin where direction command input is located.
Definition config.h:690
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:654
#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 340 of file scpi.cpp.

341{
342 double param;
343 if (!ScpiParamDouble(parameters, param) || param < 0.0 || param > 100.0)
344 {
346 return;
347 }
348 speedInput = param;
349 scpiParser.last_error = ErrorCode::NoError;
350}
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 270 of file scpi.cpp.

274{
275 uint8_t param;
276
277 // read first parameter if present
278 if (!ScpiParamChoice(parameters, inputSources, INPUT_SOURCE_OPTIONS, param))
279 {
281 return;
282 }
283
284 if (param == SPEED_INPUT_SOURCE_LOCAL)
285 {
286 motorConfigs.speedInputSource = SPEED_INPUT_SOURCE_LOCAL;
287 return;
288 }
289 else
290 {
291 motorConfigs.speedInputSource = SPEED_INPUT_SOURCE_REMOTE;
292 speedInput = 0;
293 return;
294 }
295 scpiParser.last_error = ErrorCode::NoError;
296}
#define SPEED_INPUT_SOURCE_LOCAL
Speed input source - Local or speed input pin.
Definition config.h:698
#define SPEED_INPUT_SOURCE_REMOTE
Speed input source - Remote input.
Definition config.h:700
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:667
#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 236 of file scpi.cpp.

237{
238 bool param;
239 if (!ScpiParamBool(parameters, param))
240 {
242 return;
243 }
244 if (param)
245 {
246 // Set the enable pin
247 PORTD |= (1 << ENABLE_PIN);
248 }
249 else
250 {
251 // Clear the enable pin
252 PORTD &= ~(1 << ENABLE_PIN);
253 }
254
255 scpiParser.last_error = ErrorCode::NoError;
256}
#define ENABLE_PIN
Enable input pin.
Definition config.h:692
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 388 of file scpi.cpp.

389{
390 // Clear the enable pin to stop the motor before changing frequency
391 PORTD &= ~(1 << ENABLE_PIN);
392
393 uint32_t param;
394
395 // Read first parameter if present and within range
396 if (!ScpiParamUInt32(parameters, param) || param < 7183 || param > 100000)
397 {
399 return;
400 }
401
402 // Reload the configs
403 motorConfigs.tim4Freq = param;
404 motorConfigs.tim4Top = (uint16_t)TIM4_TOP(motorConfigs.tim4Freq);
405
406 // Wait until motor is stopped
407 while (faultFlags.motorStopped == FALSE)
408 {
409 ;
410 }
411
412 // Re-init timers with the new frequency
413 TimersInit();
414 scpiParser.last_error = ErrorCode::NoError;
415}
#define TIM4_TOP(tim4Freq)
Definition config.h:1151
#define FALSE
FALSE constant value.
Definition config.h:596
void TimersInit(void)
Initializes and synchronizes Timers.
Definition main.ino:457
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 482 of file scpi.cpp.

483{
484 String name;
485 ScpiChoiceToName(motorDirections, MOTOR_DIRECTION_OPTIONS, motorFlags.desiredDirection, name);
486 interface.println(name);
487}
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 309 of file scpi.cpp.

323{
324 String name;
326 interface.println(name);
327}
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 427 of file scpi.cpp.

428{
429 interface.println(motorConfigs.tim4Freq);
430}
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 216 of file scpi.cpp.

217{
218 interface.println(motorFlags.enable);
219}
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 628 of file scpi.cpp.

629{
630 if (motorFlags.enable == TRUE)
631 {
632 // Reading 16 bit register so disabling interrupts for atomic operation
633 cli();
634 uint16_t duty = 0xff & OCR4A;
635 duty |= (0x03 & TC4H) << 8;
636 sei();
637
638 interface.println((double)duty / (double)motorConfigs.tim4Top * 100.0);
639 }
640 else
641 {
642 interface.println(0.0);
643 }
644}
#define TRUE
TRUE constant value, defined to be compatible with comparisons.
Definition config.h:598
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 539 of file scpi.cpp.

540{
541 interface.println(((double)(iphaseU - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
542}
#define IPHASE_SENSE_RESISTOR
Hi-side Current (IBUS) Sense Resistor Value.
Definition config.h:261
#define IPHASE_GAIN
In-line Phase Current Gain for Current Measurement.
Definition config.h:243
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 555 of file scpi.cpp.

556{
557 interface.println(((double)(iphaseV - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
558}
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 571 of file scpi.cpp.

572{
573 interface.println(((double)(iphaseW - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
574}
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 523 of file scpi.cpp.

524{
525 interface.println(((double)ibus * 5.0 * 1000000.0) / ((double)1023.0 * IBUS_GAIN * IBUS_SENSE_RESISTOR));
526}
#define IBUS_GAIN
Hi-side Current (IBUS) Gain for Current Measurement.
Definition config.h:281
#define IBUS_SENSE_RESISTOR
Hi-side Current (IBUS) Sense Resistor Value.
Definition config.h:299
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 587 of file scpi.cpp.

588{
589 if (motorFlags.actualDirection == DIRECTION_UNKNOWN)
590 {
591 interface.println(F("UNKN"));
592 }
593 else
594 {
595 String name;
596 ScpiChoiceToName(motorDirections, MOTOR_DIRECTION_OPTIONS, motorFlags.actualDirection, name);
597 interface.println(name);
598 }
599}
#define DIRECTION_UNKNOWN
Unknown direction flag value.
Definition config.h:640
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 500 of file scpi.cpp.

501{
502 if (lastCommutationTicks == 0xffff)
503 {
504 interface.println(0.0);
505 }
506 else
507 {
508 interface.println(
509 (motorConfigs.tim4Freq * 20.0) / (lastCommutationTicks * MOTOR_POLES));
510 }
511}
#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 611 of file scpi.cpp.

612{
613 interface.println(((double)vbusVref * 5.0 * (VBUS_RTOP + VBUS_RBOTTOM)) / ((double)1023.0 * VBUS_RBOTTOM));
614}
#define VBUS_RBOTTOM
Bottom resistor value in the VBUS voltage potential divider.
Definition config.h:573
#define VBUS_RTOP
Top resistor value in the VBUS voltage potential divider.
Definition config.h:553
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 140 of file scpi.cpp.

141{
142 interface.print(F(SCPI_IDN1));
143 interface.print(F(","));
144 interface.print(F(SCPI_IDN2));
145 interface.print(F(","));
146 if (SCPI_IDN3 != NULL && strlen(SCPI_IDN3) > 0)
147 {
148 interface.print(F(SCPI_IDN3));
149 interface.print(F(","));
150 }
151 interface.println(F(SCPI_IDN4));
152}
#define SCPI_IDN4
Firmware version identification string for the *IDN? command.
#define SCPI_IDN1
Manufacturer identification string for the *IDN? command.
#define SCPI_IDN2
Model number identification string for the *IDN? command.
#define SCPI_IDN3
Revision level identification string for the *IDN? command (optional).
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("DEADtime"), &ConfigureMotorDeadTime);
99 // scpiParser.RegisterCommand(F("DEADtime?"), &GetConfigureMotorDeadTime);
100 scpiParser.RegisterCommand(F(":DIREction"), &ConfigureMotorDirection);
101 scpiParser.RegisterCommand(F(":DIREction?"), &GetConfigureMotorDirection);
102
103 /* Motor Measurement Commands */
104 scpiParser.SetCommandTreeBase(F("MEASure"));
105 scpiParser.RegisterCommand(F(":SPEEd?"), &MeasureMotorSpeed);
106 scpiParser.RegisterCommand(F(":CURRent:IBUS?"), &MeasureMotorCurrentVBus);
107 scpiParser.RegisterCommand(F(":CURRent:IPHU?"), &MeasureMotorCurrentPhaseU);
108 scpiParser.RegisterCommand(F(":CURRent:IPHV?"), &MeasureMotorCurrentPhaseV);
109 scpiParser.RegisterCommand(F(":CURRent:IPHW?"), &MeasureMotorCurrentPhaseW);
110 scpiParser.RegisterCommand(F(":VOLTage?"), &MeasureMotorVoltage);
111 scpiParser.RegisterCommand(F(":DIREction?"), &MeasureMotorDirection);
112 scpiParser.RegisterCommand(F(":DUTYcycle?"), &MeasureGateDutyCycle);
113}
static void GetConfigureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured direction of the motor.
Definition scpi.cpp:482
static void GetConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured motor's duty cycle source.
Definition scpi.cpp:309
static void MeasureMotorCurrentPhaseV(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase V current.
Definition scpi.cpp:555
static void ScpiSystemErrorNextQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor? (System Error Next Query) command.
Definition scpi.cpp:181
static void MeasureMotorSpeed(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures the motor speed.
Definition scpi.cpp:500
static void MeasureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and reports the current direction of the motor.
Definition scpi.cpp:587
static void ScpiSystemErrorCountQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor:COUNt? (System Error Count Query) command.
Definition scpi.cpp:165
static void MeasureMotorCurrentPhaseU(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase U current.
Definition scpi.cpp:539
static void MeasureMotorCurrentVBus(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's VBUS current.
Definition scpi.cpp:523
static void ConfigureMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's enable state.
Definition scpi.cpp:236
static void ScpiCoreIdnQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the *IDN? (Identification Query) command.
Definition scpi.cpp:140
static void GetConfigureMotorFrequency(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured motor frequency.
Definition scpi.cpp:427
static void ConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's speed input source.
Definition scpi.cpp:270
static void GetMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the current motor enable state.
Definition scpi.cpp:216
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:340
static void MeasureMotorCurrentPhaseW(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase W current.
Definition scpi.cpp:571
static void ConfigureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sets the motor's direction based on the input parameter.
Definition scpi.cpp:447
static void MeasureMotorVoltage(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the VBUS voltage of the motor.
Definition scpi.cpp:611
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:628
static void ConfigureMotorFrequency(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's operating frequency.
Definition scpi.cpp:388
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 124 of file scpi.cpp.

125{
126 scpiParser.ProcessInput(interface, "\n");
127}
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 165 of file scpi.cpp.

166{
167 interface.println(scpiParser.last_error == ErrorCode::NoError ? 0 : 1);
168}
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 181 of file scpi.cpp.

182{
183 switch (scpiParser.last_error)
184 {
186 interface.println(F("Buffer overflow error"));
187 break;
189 interface.println(F("Communication timeout error"));
190 break;
192 interface.println(F("Unknown command received"));
193 break;
195 interface.println(F("No Error"));
196 break;
198 interface.println(F("Missing or invalid parameter"));
199 break;
200 default:
201 interface.println(F("Unknown error"));
202 }
203 scpiParser.last_error = ErrorCode::NoError;
204}
@ 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 667 of file scpi.cpp.

667 {
668 {"LOCA", "l", SPEED_INPUT_SOURCE_LOCAL},
669 {"REMO", "te", SPEED_INPUT_SOURCE_REMOTE},
670};

◆ 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 654 of file scpi.cpp.

654 {
655 {"FORW", "ard", DIRECTION_FORWARD},
656 {"REVE", "rse", DIRECTION_REVERSE},
657};
#define DIRECTION_FORWARD
Forward direction flag value.
Definition config.h:636
#define DIRECTION_REVERSE
Reverse direction flag value.
Definition config.h:638

◆ scpiParser

SCPI_Parser scpiParser

Speed input source options array.

Definition at line 62 of file scpi.cpp.