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

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

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

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

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

387{
388 // Clear the enable pin to stop the motor before changing frequency
389 PORTD &= ~(1 << ENABLE_PIN);
390
391 uint32_t param;
392
393 // Read first parameter if present and within range
394 if (!ScpiParamUInt32(parameters, param) || param < F_MOSFET_MIN || param > F_MOSFET_MAX)
395 {
397 return;
398 }
399
400 // Reload the configs
401 motorConfigs.tim4Freq = param;
402 motorConfigs.tim4Top = (uint16_t)TIM4_TOP(motorConfigs.tim4Freq);
403
404 // Wait until motor is stopped
405 while (faultFlags.motorStopped == FALSE)
406 {
407 ;
408 }
409
410 // Re-init timers with the new frequency
411 TimersInit();
412 scpiParser.last_error = ErrorCode::NoError;
413}
#define TIM4_TOP(tim4Freq)
Definition config.h:1216
#define FALSE
FALSE constant value.
Definition config.h:659
#define F_MOSFET_MAX
Maximum allowed gate switching frequency.
Definition config.h:99
volatile faultflags_t faultFlags
Fault flags placed in I/O space for fast access.
Definition main.ino:60
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 480 of file scpi.cpp.

481{
482 String name;
484 interface.println(name);
485}
volatile motorflags_t motorFlags
Motor control flags placed in I/O space for fast access.
Definition main.ino:50
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 307 of file scpi.cpp.

321{
322 String name;
324 interface.println(name);
325}
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 425 of file scpi.cpp.

426{
427 interface.println(motorConfigs.tim4Freq);
428}
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 214 of file scpi.cpp.

215{
216 interface.println(motorFlags.enable);
217}
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 626 of file scpi.cpp.

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

538{
539 interface.println(((double)(iphaseU - 511) * 5.0 * 1000000.0) / ((double)1023.0 * IPHASE_GAIN * IPHASE_SENSE_RESISTOR));
540}
#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 553 of file scpi.cpp.

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

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

522{
523 interface.println(((double)ibus * 5.0 * 1000000.0) / ((double)1023.0 * IBUS_GAIN * IBUS_SENSE_RESISTOR));
524}
#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 585 of file scpi.cpp.

586{
587 if (motorFlags.actualDirection == DIRECTION_UNKNOWN)
588 {
589 interface.println(F("UNKN"));
590 }
591 else
592 {
593 String name;
595 interface.println(name);
596 }
597}
#define DIRECTION_UNKNOWN
Unknown direction flag value.
Definition config.h:703
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 498 of file scpi.cpp.

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

610{
611 interface.println(((double)vbusVref * 5.0 * (VBUS_RTOP + VBUS_RBOTTOM)) / ((double)1023.0 * VBUS_RBOTTOM));
612}
#define VBUS_RBOTTOM
Bottom resistor value in the VBUS voltage potential divider.
Definition config.h:606
#define VBUS_RTOP
Top resistor value in the VBUS voltage potential divider.
Definition config.h:586
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 if (SCPI_IDN_DEFAULT_SERIAL != NULL && strlen(SCPI_IDN_DEFAULT_SERIAL) > 0)
145 {
146 interface.print(F(SCPI_IDN_DEFAULT_SERIAL));
147 }
148 interface.print(F(","));
149 interface.println(F(SCPI_IDN_FIRMWARE_VERSION));
150}
#define SCPI_IDN_MODEL
Model number identification string for the *IDN? command.
#define SCPI_IDN_DEFAULT_SERIAL
Revision level identification string for the *IDN? command (optional).
#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:480
static void GetConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the configured motor's duty cycle source.
Definition scpi.cpp:307
static void MeasureMotorCurrentPhaseV(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase V current.
Definition scpi.cpp:553
static void ScpiSystemErrorNextQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor? (System Error Next Query) command.
Definition scpi.cpp:179
static void MeasureMotorSpeed(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures the motor speed.
Definition scpi.cpp:498
static void MeasureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and reports the current direction of the motor.
Definition scpi.cpp:585
static void ScpiSystemErrorCountQ(SCPI_C commands, SCPI_P parameters, Stream &interface)
Implements the SYSTem:ERRor:COUNt? (System Error Count Query) command.
Definition scpi.cpp:163
static void MeasureMotorCurrentPhaseU(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase U current.
Definition scpi.cpp:537
static void MeasureMotorCurrentVBus(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's VBUS current.
Definition scpi.cpp:521
static void ConfigureMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's enable state.
Definition scpi.cpp:234
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:425
static void ConfigureMotorDutyCycleSource(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's speed input source.
Definition scpi.cpp:268
static void GetMotorEnable(SCPI_C commands, SCPI_P parameters, Stream &interface)
Retrieves the current motor enable state.
Definition scpi.cpp:214
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:338
static void MeasureMotorCurrentPhaseW(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the motor's phase W current.
Definition scpi.cpp:569
static void ConfigureMotorDirection(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sets the motor's direction based on the input parameter.
Definition scpi.cpp:445
static void MeasureMotorVoltage(SCPI_C commands, SCPI_P parameters, Stream &interface)
Measures and returns the VBUS voltage of the motor.
Definition scpi.cpp:609
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:626
static void ConfigureMotorFrequency(SCPI_C commands, SCPI_P parameters, Stream &interface)
Configures the motor's operating frequency.
Definition scpi.cpp:386
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 163 of file scpi.cpp.

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

180{
181 switch (scpiParser.last_error)
182 {
184 interface.println(F("Buffer overflow error"));
185 break;
187 interface.println(F("Communication timeout error"));
188 break;
190 interface.println(F("Unknown command received"));
191 break;
193 interface.println(F("No Error"));
194 break;
196 interface.println(F("Missing or invalid parameter"));
197 break;
198 default:
199 interface.println(F("Unknown error"));
200 }
201 scpiParser.last_error = ErrorCode::NoError;
202}
@ 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 665 of file scpi.cpp.

665 {
666 {"LOCA", "l", SPEED_INPUT_SOURCE_LOCAL},
667 {"REMO", "te", SPEED_INPUT_SOURCE_REMOTE},
668};

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

652 {
653 {"FORW", "ard", DIRECTION_FORWARD},
654 {"REVE", "rse", DIRECTION_REVERSE},
655};
#define DIRECTION_FORWARD
Forward direction flag value.
Definition config.h:699
#define DIRECTION_REVERSE
Reverse direction flag value.
Definition config.h:701

◆ scpiParser

SCPI_Parser scpiParser

Speed input source options array.

Definition at line 62 of file scpi.cpp.