Motor Evaluation Kit NEVC-MCTRL-100-t01-1.0.0
Firmware for NEVB-MCTRL-100-01 for trapezoidal control of BLDC motors using Hall-effect sensors
Loading...
Searching...
No Matches
scpi.h File Reference

SCPI implementation header file. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "src/SCPI_Parser/SCPI_Parser.h"
#include <Arduino.h>
#include <avr/io.h>
#include "main.h"
Include dependency graph for scpi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __AVR_ATmega32U4__   1
 Define macro for ATmega32U4 micro controller.
 
#define SCPI_INPUT_BUFFER_LENGTH   64
 
#define SCPI_ERROR_QUEUE_SIZE   4
 The SCPI error queue size or the maximum number of error retained in memory.
 
#define SCPI_IDN1   "NEXPERIA"
 The manufacturer's name for the identification SCPI query.
 
#define SCPI_IDN2   "NEVB-MCTRL-100-xx"
 The device's model number for the identification SCPI query.
 
#define SCPI_IDN3   NULL
 The device's serial number for the identification SCPI query.
 
#define SCPI_IDN4   "NEVC-MCTRL-100-t01-1.0.0"
 The software verion for the identification SCPI query.
 

Functions

void TimersInit (void)
 Initializes and synchronizes Timers.
 
void ConfigsInit (void)
 Initializes motorConfigs.
 
size_t SCPI_Write (scpi_t *context, const char *data, size_t len)
 Writes data to the SCPI interface.
 
int SCPI_Error (scpi_t *context, int_fast16_t err)
 Handles SCPI errors and outputs them to the Serial interface.
 
scpi_result_t SCPI_Control (scpi_t *context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val)
 Handles control messages for the SCPI interface (dummy)
 
scpi_result_t SCPI_Reset (scpi_t *context)
 Resets the SCPI context.
 
scpi_result_t SCPI_Flush (scpi_t *context)
 Flushes the Serial interface buffer.
 

Variables

const scpi_command_t scpi_commands []
 Array of SCPI commands.
 
scpi_interface_t scpi_interface
 SCPI interface structure.
 
char scpi_input_buffer []
 SCPI input buffer.
 
scpi_error_t scpi_error_queue_data []
 SCPI error queue data array.
 
scpi_t scpi_context
 SCPI context structure.
 
volatile motorconfigs_t motorConfigs
 Motor Configs.
 
volatile motorflags_t motorFlags
 Motor control flags placed in I/O space for fast access.
 
volatile faultflags_t faultFlags
 Fault flags placed in I/O space for fast access.
 
volatile uint16_t lastCommutationTicks
 The number of 'ticks' between two hall sensor changes (store).
 
volatile uint16_t current
 Current measurement (Register Value).
 
volatile uint16_t gateVref
 Gate voltage measurement (Register Value)
 
volatile uint8_t speedInput
 The most recent "speed" input measurement.
 

Detailed Description

SCPI implementation header file.

This file contains all the defines for configurations and function prototypes related to the SCPI implementation.

This file depends on the base SCPI parser library v2 (commit #4e87990) by Jan Breuer which was then ported by Scott Feister to the Arduino IDE, SCPI Parser Arduino Library.

Further modifications were made to the base library to allow for memory optimisation and support for the avr-gcc compiler.

Author
Nexperia: http://www.nexperia.com
Support Page
For additional support, visit: https://www.nexperia.com/support
Author
Aanas Sayed
Date
2024/03/08


Definition in file scpi.h.

Macro Definition Documentation

◆ __AVR_ATmega32U4__

#define __AVR_ATmega32U4__   1

Define macro for ATmega32U4 micro controller.

Definition at line 34 of file scpi.h.

◆ SCPI_ERROR_QUEUE_SIZE

#define SCPI_ERROR_QUEUE_SIZE   4

The SCPI error queue size or the maximum number of error retained in memory.

Definition at line 50 of file scpi.h.

◆ SCPI_IDN1

#define SCPI_IDN1   "NEXPERIA"

The manufacturer's name for the identification SCPI query.

Definition at line 53 of file scpi.h.

◆ SCPI_IDN2

#define SCPI_IDN2   "NEVB-MCTRL-100-xx"

The device's model number for the identification SCPI query.

Definition at line 55 of file scpi.h.

◆ SCPI_IDN3

#define SCPI_IDN3   NULL

The device's serial number for the identification SCPI query.

Definition at line 57 of file scpi.h.

◆ SCPI_IDN4

#define SCPI_IDN4   "NEVC-MCTRL-100-t01-1.0.0"

The software verion for the identification SCPI query.

Definition at line 59 of file scpi.h.

◆ SCPI_INPUT_BUFFER_LENGTH

#define SCPI_INPUT_BUFFER_LENGTH   64

The SCPI input buffer length or the maximum characters allowed at for a single command.

Definition at line 47 of file scpi.h.

Function Documentation

◆ SCPI_Control()

scpi_result_t SCPI_Control ( scpi_t * context,
scpi_ctrl_name_t ctrl,
scpi_reg_val_t val )

Handles control messages for the SCPI interface (dummy)

This function processes control messages such as SRQ (Service Request) or other control signals. It outputs relevant information to the Serial interface. Used as a callback in the SCPI interface structure.

Note
Dummy function implemented here.
Parameters
contextThe SCPI context
ctrlThe control name/type
valThe control value
Returns
SCPI result code indicating successful operation

Definition at line 605 of file scpi.cpp.

606{
607 (void)context;
608
609 return SCPI_RES_OK;
610}

◆ SCPI_Error()

int SCPI_Error ( scpi_t * context,
int_fast16_t err )

Handles SCPI errors and outputs them to the Serial interface.

This function processes and displays SCPI error messages. It is used as a callback in the SCPI interface structure for error handling.

Parameters
contextThe SCPI context
errThe error code
Returns
Always returns 0

Definition at line 581 of file scpi.cpp.

582{
583 (void)context;
584#if (REMOTE_DEBUG_MODE == TRUE)
585 Serial.print(err);
586 Serial.print(", \"");
587 Serial.print(SCPI_ErrorTranslate(err));
588 Serial.println("\"");
589#endif
590 return 0;
591}

◆ SCPI_Flush()

scpi_result_t SCPI_Flush ( scpi_t * context)

Flushes the Serial interface buffer.

This function ensures that all pending Serial data is transmitted. It acts as a callback for the SCPI interface structure.

Parameters
contextThe SCPI context
Returns
SCPI result code indicating successful operation

Definition at line 565 of file scpi.cpp.

566{
567 (void)context;
568 Serial.flush();
569 return SCPI_RES_OK;
570}

◆ SCPI_Reset()

scpi_result_t SCPI_Reset ( scpi_t * context)

Resets the SCPI context.

This function is called to reset the SCPI environment. It outputs a reset message to the Serial interface and is used as a callback in the SCPI interface structure.

Parameters
contextThe SCPI context
Returns
SCPI result code indicating successful operation

Definition at line 622 of file scpi.cpp.

623{
624 (void)context;
625
626 // Clear the enable pin
627 PORTD &= ~(1 << ENABLE_PIN);
628
629 // Wait until motor is stopped
630 while (faultFlags.motorStopped == FALSE)
631 {
632 ;
633 }
634
635 // Reset configurations
636 ConfigsInit();
637
638 // Re-init timers
639 TimersInit();
640
641 return SCPI_RES_OK;
642}
#define ENABLE_PIN
Enable input pin.
Definition main.h:623
#define FALSE
FALSE constant value.
Definition main.h:535
volatile faultflags_t faultFlags
Fault flags placed in I/O space for fast access.
Definition main.ino:84
void TimersInit(void)
Initializes and synchronizes Timers.
Definition main.ino:405
static void ConfigsInit(void)
Initializes motorConfigs.
Definition main.ino:320
uint8_t motorStopped
Is motor stopped?
Definition main.h:1005
Here is the call graph for this function:

◆ SCPI_Write()

size_t SCPI_Write ( scpi_t * context,
const char * data,
size_t len )

Writes data to the SCPI interface.

This function is responsible for writing data to the Serial interface. It is used as a callback in the SCPI interface structure.

Parameters
contextThe SCPI context
dataPointer to the data to be written
lenLength of the data to be written
Returns
The number of bytes written

Definition at line 549 of file scpi.cpp.

550{
551 (void)context;
552 Serial.write(data, len);
553 return len;
554}

◆ TimersInit()

void TimersInit ( void )
extern

Initializes and synchronizes Timers.

This function sets the correct pre-scaler and starts all required timers.

Timer 1 is used to trigger the fault multiplexing on overflow. Timer 3 is used, if EMULATE_HALL is set, to trigger the change in hall output on overflow. Timer 4 is used to generate PWM outputs for the gates and trigger the commutation tick counter and check if the motor is spinning on overflow. The overflow event interrupt for Timer 4 is set outside this function at the end of the main initialization function.

See also
EMULATE_HALL, TIM3_FREQ, TimersSetModeBlockCommutation(), TimersSetModeBrake()

Definition at line 405 of file main.ino.

406{
407 // Set Timer1 accordingly.
408 TCCR1A = (1 << WGM11) | (0 << WGM10);
409 TCCR1B = (0 << WGM13) | (1 << WGM12);
410 TIMSK1 = (1 << TOIE1);
411
412 // Start Timer1.
413 TCCR1B |= TIM1_CLOCK_DIV_64;
414
415#if (EMULATE_HALL == TRUE)
416 // Set Timer3 accordingly.
417 TCCR3A = (1 << WGM31) | (1 << WGM30);
418 TCCR3B = (1 << WGM33) | (1 << WGM32);
419 TIMSK3 = (1 << TOIE3);
420
421 // Set top value of Timer/counter3.
422 OCR3AH = (uint8_t)(TIM3_TOP >> 8);
423 OCR3AL = (uint8_t)(0xff & TIM3_TOP);
424
425 // Start Timer3.
426 TCCR3B |= TIM1_CLOCK_DIV_8;
427#endif
428 // Set Timer4 in "PWM6 / Dual-slope" mode. Does not enable outputs yet.
429 TCCR4A = (0 << COM4A1) | (1 << COM4A0) | (0 << COM4B1) | (1 << COM4B0) | (1 << PWM4A) | (1 << PWM4B);
431 TCCR4C |= (0 << COM4D1) | (1 << COM4D0) | (1 << PWM4D);
432 TCCR4E = (1 << ENHC4);
433
434 // Set top value of Timer/counter4.
435 TC4H = (uint8_t)(motorConfigs.tim4Top >> 8);
436 OCR4C = (uint8_t)(0xff & motorConfigs.tim4Top);
437
438 // Set the dead time.
440
441 // Start Timer4.
443}
#define DEAD_TIME_HALF(deadTime)
This value specifies half the dead time in number of clock cycles. Divide by frequency to get duratio...
Definition main.h:1080
#define DT_PRESCALER_DIV_PATTERN(dtPrescaler)
Deadtime generator pre-scaler selection bits based on pre-scaler value.
Definition main.h:1070
#define TIM3_TOP
Calculated top value for Timer 3.
Definition main.h:1088
#define TIM4_PRESCALER_DIV_PATTERN(tim4Prescaler)
Timer 4 clock select bits based on pre-scaler value.
Definition main.h:1052
#define CHOOSE_DT_PRESCALER(deadTime)
Macro to choose Timer4 dead time pre-scaler based on the dead time.
Definition main.h:695
#define CHOOSE_TIM4_PRESCALER(tim4Freq)
Macro to choose Timer4 pre-scaler.
Definition main.h:677
#define TIM1_CLOCK_DIV_64
Timer1 clock - i/o clk with division factor 64.
Definition main.h:805
#define TIM1_CLOCK_DIV_8
Timer1 clock - i/o clk with division factor 8.
Definition main.h:803
#define TIM4_FREQ
Desired Switching Frequency for MOSFET Gate Signals.
Definition main.h:99
volatile motorconfigs_t motorConfigs
Motor Configs.
Definition main.ino:90
uint16_t tim4Top
Corresponding TIM4 top value for TIM4 frequency.
Definition main.h:1028
uint16_t tim4DeadTime
Corresponding dead time for TIM4 output.
Definition main.h:1030
Here is the caller graph for this function:

Variable Documentation

◆ current

volatile uint16_t current
extern

Current measurement (Register Value).

The most recent current measurement is stored in this variable.

The range is 0-1023.

This value is not scaled and represents the raw ADC register value. To obtain the scaled current value in amperes, you can use the formula:

\[ \text{Current (A)} = \frac{\text{REGISTER_VALUE} \times 0.004887586 \times 1000000}{\text{CURRENT_GAIN} \times \text{CURRENT_SENSE_RESISTOR}} \]

Where:

  • REGISTER_VALUE : The raw ADC register value stored in this variable.
  • 0.004887586 : The conversion factor for a 10-bit ADC with a Vref of 5V.
  • CURRENT_GAIN : The gain of the current sense operational amplifier.
  • CURRENT_SENSE_RESISTOR : The value of the shunt resistor in micro-ohms (μΩ).

The NEVB-3INV-001-01 comes with a current op-amp with a gain factor of 50 and a current sense resistor of value 2 mΩ. This corresponds to approximately 0.049 amperes (A) per register value.

See also
CURRENT_WARNING_THRESHOLD, CURRENT_ERROR_THRESHOLD

Definition at line 161 of file main.ino.

◆ faultFlags

volatile faultflags_t faultFlags
extern

Fault flags placed in I/O space for fast access.

This variable contains all the flags used for faults. It is placed in GPIOR1 register, which allows usage of several fast bit manipulation/branch instructions.

Definition at line 84 of file main.ino.

◆ gateVref

volatile uint16_t gateVref
extern

Gate voltage measurement (Register Value)

The most recent gate voltage measurement is stored in this variable.

The range is 0-1023.

This value is not scaled and represents the raw ADC register value. To obtain the scaled gate voltage in volts, you can use the formula:

\[ \text{Voltage (V)} = \frac{\text{REGISTER_VALUE} \times 0.004887586 \times (\text{GATE_RTOP} + \text{GATE_RBOTTOM})}{\text{GATE_RBOTTOM}} \]

Where:

  • REGISTER_VALUE : The raw ADC register value stored in this variable.
  • 0.004887586 : The conversion factor for a 10-bit ADC with a Vref of 5V.
  • GATE_RTOP : The top resistor value in ohms (Ω) in the potential divider.
  • GATE_RBOTTOM : The bottom resistor value in ohms (Ω) in the potential divider.

NEVB-3INV-001-01 has a resistor divider with RTOP of 1 MΩ and RBOTTOM of 71.5 kΩ, so it corresponds to approximately 0.0732 volts (V) per register value.

Note
It is not used for any significant purpose in this implementation, but the measurement is updated.

Definition at line 190 of file main.ino.

◆ lastCommutationTicks

volatile uint16_t lastCommutationTicks
extern

The number of 'ticks' between two hall sensor changes (store).

This variable is used to store the number of 'ticks' between each hall sensor change. It is set to the value in commutationTicks before it is cleared when the hall sensor change occurs.

One 'tick' is one PWM period. The speed of the motor is inversely proportional to this value.

See also
commutationTicks

Definition at line 118 of file main.ino.

◆ motorConfigs

volatile motorconfigs_t motorConfigs
extern

Motor Configs.

This variable contains some of the motor configurations.

Definition at line 90 of file main.ino.

◆ motorFlags

volatile motorflags_t motorFlags
extern

Motor control flags placed in I/O space for fast access.

This variable contains all the flags used for motor control. It is placed in GPIOR0 register, which allows usage of several fast bit manipulation/branch instructions.

Definition at line 76 of file main.ino.

◆ scpi_commands

const scpi_command_t scpi_commands[]
extern

Array of SCPI commands.

This array contains the definitions of all SCPI commands that the system recognizes. Each command is defined with its pattern, callback function, and associated context. The array is stored in program memory (PROGMEM) to optimize memory usage.

Definition at line 504 of file scpi.cpp.

504 {
505 /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */
506 {"*CLS", SCPI_CoreCls, 0},
507 {"*IDN?", SCPI_CoreIdnQ, 0},
508 {"*RST", SCPI_CoreRst, 0},
509
510 /* Required SCPI commands (SCPI std V1999.0 4.2.1) */
511 {"SYSTem:ERRor[:NEXT]?", SCPI_SystemErrorNextQ, 0},
512 {"SYSTem:ERRor:COUNt?", SCPI_SystemErrorCountQ, 0},
513 {"SYSTem:VERSion?", SCPI_SystemVersionQ, 0},
514
515 /* Motor */
516 {"CONFigure:MOTOr:ENABle", ConfigureMotorEnable, 0},
517 {"CONFigure:MOTOr:ENABle?", GetConfigureMotorEnable, 0},
518#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_OPEN_LOOP)
519 {"CONFigure:MOTOr:GATE:DUTYcycle:SOURce", ConfigureMotorDutyCycleSource, 0},
520 {"CONFigure:MOTOr:GATE:DUTYcycle", ConfigureMotorDutyCycle, 0},
521#else
522 {"CONFigure:MOTOr:SPEED:SOURce", ConfigureMotorSpeedSource, 0},
523 {"CONFigure:MOTOr:SPEED", ConfigureMotorSpeed, 0},
524#endif
525 {"CONFigure:MOTOr:GATE:FREQuency", ConfigureMotorFrequency, 0},
526 {"CONFigure:MOTOr:GATE:FREQuency?", GetConfigureMotorFrequency, 0},
527 {"CONFigure:MOTOr:GATE:DEADtime", ConfigureMotorDeadTime, 0},
528 {"CONFigure:MOTOr:GATE:DEADtime?", GetConfigureMotorDeadTime, 0},
529 {"CONFigure:MOTOr:DIREction", ConfigureMotorDirection, 0},
530 {"CONFigure:MOTOr:DIREction?", GetConfigureMotorDirection, 0},
531 {"MEASure:MOTOr:SPEEd?", MeasureMotorSpeed, 0},
532 {"MEASure:MOTOr:CURRent?", MeasureMotorCurrent, 0},
533 {"MEASure:MOTOr:DIREction?", MeasureMotorDirection, 0},
534 {"MEASure:MOTOr:GATE:VOLTage?", MeasureGateVoltage, 0},
535 {"MEASure:MOTOr:GATE:DUTYcycle?", MeasureGateDutyCycle, 0},
536
537 SCPI_CMD_LIST_END};
static scpi_result_t MeasureGateVoltage(scpi_t *context)
Measures and returns the gate voltage of the motor.
Definition scpi.cpp:122
static scpi_result_t MeasureMotorSpeed(scpi_t *context)
Measures the motor speed.
Definition scpi.cpp:55
static scpi_result_t GetConfigureMotorDeadTime(scpi_t *context)
Retrieves the configured motor dead time.
Definition scpi.cpp:489
static scpi_result_t GetConfigureMotorFrequency(scpi_t *context)
Retrieves the configured motor frequency.
Definition scpi.cpp:427
static scpi_result_t GetConfigureMotorDirection(scpi_t *context)
Retrieves the configured direction of the motor.
Definition scpi.cpp:360
static scpi_result_t MeasureMotorCurrent(scpi_t *context)
Measures and returns the motor's current.
Definition scpi.cpp:78
static scpi_result_t ConfigureMotorDeadTime(scpi_t *context)
Configures the motor's dead time.
Definition scpi.cpp:444
static scpi_result_t ConfigureMotorDirection(scpi_t *context)
Sets the motor's direction based on the input parameter.
Definition scpi.cpp:324
static scpi_result_t ConfigureMotorEnable(scpi_t *context)
Configures the motor's enable state.
Definition scpi.cpp:268
static scpi_result_t GetConfigureMotorEnable(scpi_t *context)
Retrieves the current motor enable state.
Definition scpi.cpp:303
static scpi_result_t ConfigureMotorDutyCycleSource(scpi_t *context)
Configures the motor's speed input source.
Definition scpi.cpp:140
static scpi_result_t ConfigureMotorFrequency(scpi_t *context)
Configures the motor's operating frequency.
Definition scpi.cpp:381
static scpi_result_t MeasureGateDutyCycle(scpi_t *context)
Measures and returns the gate PWM duty cycle.
Definition scpi.cpp:234
static scpi_result_t ConfigureMotorDutyCycle(scpi_t *context)
Configures the motor's speed input by changing the duty cycle.
Definition scpi.cpp:175
static scpi_result_t MeasureMotorDirection(scpi_t *context)
Measures and reports the current direction of the motor.
Definition scpi.cpp:95

◆ scpi_context

scpi_t scpi_context
extern

SCPI context structure.

This structure holds the context for SCPI operations, encapsulating state and configuration information.

Definition at line 681 of file scpi.cpp.

◆ scpi_error_queue_data

scpi_error_t scpi_error_queue_data[]
extern

SCPI error queue data array.

Array for storing SCPI errors. Its size is defined by SCPI_ERROR_QUEUE_SIZE.

Definition at line 673 of file scpi.cpp.

◆ scpi_input_buffer

char scpi_input_buffer[]
extern

SCPI input buffer.

Array for storing incoming SCPI commands. Its size is defined by SCPI_INPUT_BUFFER_LENGTH.

Definition at line 665 of file scpi.cpp.

◆ scpi_interface

scpi_interface_t scpi_interface
extern

SCPI interface structure.

This structure defines the callbacks for various SCPI interface operations like error handling, writing to an output, control functions, flushing the output, and resetting the interface.

Definition at line 651 of file scpi.cpp.

651 {
652 /*.error = */ SCPI_Error,
653 /*.write = */ SCPI_Write,
654 /*.control = */ SCPI_Control,
655 /*.flush = */ SCPI_Flush,
656 /*.reset = */ SCPI_Reset,
657};
scpi_result_t SCPI_Reset(scpi_t *context)
Resets the SCPI context.
Definition scpi.cpp:622
scpi_result_t SCPI_Control(scpi_t *context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val)
Handles control messages for the SCPI interface (dummy)
Definition scpi.cpp:605
size_t SCPI_Write(scpi_t *context, const char *data, size_t len)
Writes data to the SCPI interface.
Definition scpi.cpp:549
int SCPI_Error(scpi_t *context, int_fast16_t err)
Handles SCPI errors and outputs them to the Serial interface.
Definition scpi.cpp:581
scpi_result_t SCPI_Flush(scpi_t *context)
Flushes the Serial interface buffer.
Definition scpi.cpp:565

◆ speedInput

volatile uint8_t speedInput
extern

The most recent "speed" input measurement.

This variable is set by the ADC from the speed input reference pin. The range is 0-255.

Definition at line 125 of file main.ino.