core0.h

Used exclusively by core0

Defines

MESSAGE_3_BYTES
MESSAGE_MASK_3_BYTES
MESSAGE_4_BYTES
MESSAGE_MASK_4_BYTES
MESSAGE_5_BYTES
MESSAGE_MASK_5_BYTES
INVALID_PACKAGE
RING_BUFFER_PACKETS
RING_BUFFER_BYTES
FLASH_CMD_READ_JEDEC_ID
FLASH_TIMEOUT_IN_MS
ADC_CALIBRATION_ITERATIONS
WATCHDOG_TIMER_IN_MS

Typedefs

typedef struct dcc_packet_t dcc_packet_t
typedef struct dcc_ring_buffer_t dcc_ring_buffer_t

Functions

static void increment_ring_buffer_idx(size_t *idx, uint8_t ring_buffer_length)

Increment the index of a ring buffer.

This function increments the provided index of a ring buffer, wrapping around to zero if the end of the buffer is reached.

Parameters:
  • idx – Pointer to the index to be incremented.

  • ring_buffer_length – The length of the ring buffer.

static void call_flash_range_erase(void *param)

Function for erasing a sector of flash memory.

This function erases a specified range of flash memory sectors.

Parameters:

param – Pointer to parameters.

  • First parameter: Offset into flash, in bytes. Must be aligned to a 4096-byte flash sector.

  • Second parameter: Number of bytes to be erased. Must be a multiple of 4096 bytes (one sector).

static void call_flash_range_program(void *param)

Function for programming a range of flash memory.

This function programs a specified range of flash memory with the provided data.

Parameters:

param – Pointer to parameters

  • First parameter: The Flash address of the first byte to be programmed. Must be aligned to a 256-byte flash page.

  • Second parameter: The number of bytes to program. Must be a multiple of 256 bytes (one page).

  • Third parameter: Pointer to the data to program into flash.

static void call_flash_do_cmd(void *param)

Function for executing a flash command via QSPI.

Parameters:

param – Pointer to parameters First parameter is the pointer to a byte buffer which will be transmitted to the flash Second parameter is the pointer to a byte buffer where data received from the flash will be written. Third parameter is the length in bytes of any of the two buffers (same size).

static unsigned int absolute_val(int x)

Function for calculating the absolute value of an integer.

This function takes an integer as input and returns its absolute value.

Parameters:

x – The input integer whose absolute value is to be calculated.

Returns:

The absolute value of the input integer.

static uint16_t two_std_dev(const uint16_t arr[], uint32_t length)

Computes the average of values in an array that deviate less than twice the standard deviation from the original average.

This function calculates the average of the elements in the input array that are within two standard deviations of the mean of the array.

Parameters:
  • arr – The input array of values.

  • length – The number of elements in the input array.

Returns:

The average value of the elements that deviate less than twice the standard deviation from the original average.

static void adc_offset_adjustment(uint32_t n)

Measures ADC offset and saves the offset value to flash memory.

This function performs a series of ADC measurements to determine the offset and then stores this offset value in the flash memory for future use.

Parameters:

n – The number of measurements to be performed to calculate the offset.

static void acknowledge()

Function for acknowledging a CV write or CV verify instruction.

Acknowledgement works by creating a spike in current consumption, normally realized by running the motor in alternating directions. This current increase is detected by the command station.

Note

Refers to NMRA S-9.2.3 Section D and RCN-216 Section 3

static void verify_cv_bit(uint16_t cv_address, bool bit_val, uint8_t bit_pos)

Function for verifying a single bit of a CV.

Parameters:
  • cv_address – CV Address

  • bit_val – Value of bit to be verified

  • bit_pos – Position of bit to be verified

static void verify_cv_byte(uint16_t cv_address, uint8_t cv_data)

Function for verifying a CV byte.

This function verifies the byte of a Configuration Variable (CV) at the specified address.

Parameters:
  • cv_address – The address of the CV to be verified.

  • cv_data – The byte value to be verified against the CV.

static void write_cv_byte(uint16_t cv_address, uint8_t cv_data)

Writes a byte to a Configuration Variable (CV).

This function writes a byte of data to a specified CV address.

Parameters:
  • cv_address – The address of the CV to write to.

  • cv_data – The data byte to write to the CV.

static void reset_cv_array_to_default()

This function sets all the CV values stored in flash back to their default settings (CV_ARRAY_DEFAULT).

static void program_mode(size_t number_of_bytes, const uint8_t *const byte_array)

CV Programming mode function.

This function is used to enter programming mode in order to write CV values to persistent flash memory or verify/read bits/bytes from flash memory.

Procedure:

  1. Checks for a valid programming command.

  2. Evaluates the type of programming instruction (Write byte / Verify byte / Verify bit) and runs the corresponding function. See verify_cv_byte(), verify_cv_bit() and write_cv_byte().

Note

Refer to NMRA Standard 9.2.3 (“Service Mode Programming”) or RCN-216 for more details.

Parameters:
  • number_of_bytes – The number of bytes in the array.

  • byte_array – A pointer to the byte array.

static void set_outputs(uint32_t functions_to_set_bitmask)

Set outputs according to function mapping in CV257 to CV512.

This function maps the functions and direction to the mapped outputs configured in CV257 to CV512. Also enables PWM when enabled according to CV112 to CV115.

Parameters:

functions_to_set_bitmask – Function bitmask F0 is bit0, F1 is bit1, …

static void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change)

Update functions F0 - F31 when function command is received or in case of direction change.

This function updates the active functions based on the provided bitmask. It can also clear a specific bit and handle changes in direction.

Parameters:
  • new_function_bitmask – New function bitmask representing the state of functions F0 - F31.

  • clr_bit_ind – Index of the bit to be cleared in the bitmask.

  • direction_change – Flag indicating if there is a change in direction.

static bool error_detection(size_t number_of_bytes, const uint8_t *byte_array)

Detect errors in the byte array using exor of all bytes.

Exor of all bytes is expected to be “0b00000000”.

Parameters:
  • number_of_bytes – Number of bytes in the array.

  • byte_array – Pointer to the byte array.

Returns:

true if no error is detected, false otherwise.

static bool is_long_address(size_t number_of_bytes, const uint8_t byte_array[])

Check if the address is a long address.

Parameters:
  • number_of_bytes – Number of bytes in the array.

  • byte_array – Pointer to the byte array.

Returns:

true if it is a long address, false otherwise.

static bool address_evaluation(size_t number_of_bytes, const uint8_t byte_array[])

Evaluate the address of the message.

Parameters:
  • number_of_bytes – Number of bytes in the array.

  • byte_array – Pointer to the byte array.

Returns:

true if the address is correct, false otherwise.

static void instruction_evaluation(size_t number_of_bytes, const uint8_t byte_array[])

Evaluate the instruction of the message.

Parameters:
  • number_of_bytes – Number of bytes in the array.

  • byte_array – Pointer to the byte array.

static bool reset_message_check(size_t number_of_bytes, const uint8_t *const byte_array)

Check for reset message - When reset message is found, stop the motor and disable all functions.

Returns:

Number of bytes if valid reset message bit-pattern is found. Otherwise -1 is returned.

static size_t detect_dcc_packet()

Verify DCC message.

Procedure:

  1. Check for valid preamble using bitmasks

  2. Return number of bytes if valid bit-pattern is found. Otherwise -1 is returned.

Returns:

Number of bytes if valid bit-pattern is found. Otherwise return -1.

static void bits_to_dcc_packet_data(dcc_packet_t *packet)

Convert sequence of bits to byte array.

For each byte in the byte array, shift the input bit buffer by 9 bits and save the result in the byte array. Bytes are ordered as follows: Start of transmission -> byte_n(address byte) -> … -> byte_0(error detection byte) -> end of transmission

Parameters:

packet – Pointer to dcc packet struct which contains data and length of packet.

static void evaluate_packet()

Main message evaluation function.

Procedure:

  1. Check if input buffer contains a valid DCC message

  2. Split data into array of bytes

  3. Check for errors

  4. Check for matching address

  5. Check for reset message and set flag when reset message is received

static void track_signal_rise()

Track signal rising edge interrupt callback function.

Saves timer counter value using get_absolute_time(), then disables rising edge IRQ and enables falling edge IRQ.

static void track_signal_fall()

Track signal falling edge interrupt callback function.

Saves timer counter value using get_absolute_time(), then calculates time difference between rising and falling edge. When the time difference is greater than 87us, this bit is interpreted as a logical 0. Otherwise, it is interpreted as a logical 1. Refers to NMRA S-9.1 and RCN-210 standards Puts the latest bit value into 64 bit unsigned integer “buffer”, then checks if the buffer contains a valid DCC packet. When a valid packet is found, it is written into the ring buffer, and the write index gets incremented.

static void init_outputs()

Output initialization function.

Initializes all allowed GPIO outputs as outputs

Sets PWM functionality for GPIOs configured as PWM pins CV_112 - CV_115 (see

CV.h

for reference)

PWM frequency and clock divider are configured according to CV_116 - CV_171 (see

CV.h for reference)

static void cv_setup_check()

Checks various CV values in order to detect factory condition or pending calibrations.

Procedure:

  1. Check for flash memory factory condition when found write default values specified in CV.h to flash memory

  2. Check for existing ADC offset setup when not found write 7 to read-only CV_7 in order to trigger a ADC offset measurement/calibration

  3. Check for base PWM configuration/calibration, run calibration when none is found

static void init_motor_pwm(uint8_t gpio)

Function initializes motor PWM for pin specified in CMakeLists.txt.

Parameters:

gpio – GPIO pin

static void init_adc()

Initializes ADC.

Function initializes ADC, corresponding GPIO pins specified in CMakeLists.txt, also configures ADC FIFO.

Variables

const uint32_t clr_bit_arr[6] = {0b11111111111111111111111111100000, 0b11111111111111111111111000011111, 0b11111111111111111110000111111111, 0b11111111111000000001111111111111, 0b11100000000111111111111111111111, 0b00011111111111111111111111111111,}

Bitmasks used for clearing bits corresponding to function blocks.

[0] = F0-F4 [1] = F5-F8 [2] = F9-F12 [3] = F13-F20 [4] = F21-F28 [5] = F29-F31

struct dcc_packet_t
#include <core0.h>

Structure representing a DCC packet.

This structure is used to store the data and length of a DCC packet.

Param data:

An array of bytes representing the DCC packet data.

Param length:

The length of the DCC packet data.

Public Members

uint8_t data[RING_BUFFER_BYTES]

An array of bytes representing the DCC packet data.

size_t length

The length of the DCC packet data.

struct dcc_ring_buffer_t
#include <core0.h>

Structure representing a ring buffer for DCC packets.

This structure is used to manage a ring buffer that stores DCC packets. It contains an array of packets and indices for writing and reading.

Public Members

dcc_packet_t packets[RING_BUFFER_PACKETS]

Array of DCC packets stored in the ring buffer.

size_t wr_idx

Index for the next write operation in the ring buffer.

size_t rd_idx

Index for the next read operation in the ring buffer.

core1.h

Used exclusively by core1

Defines

BASE_PWM_ARR_LEN

Typedefs

typedef struct startup_parameters_t startup_parameters_t
typedef struct pid_parameters_t pid_parameter_t
typedef struct controller_parameter_t controller_parameter_t

Enums

enum controller_mode_t

Values:

enumerator STARTUP_MODE
enumerator PID_MODE

Functions

float measure(uint8_t total_iterations, uint8_t measurement_delay_us, uint8_t l_side_arr_cutoff, uint8_t r_side_arr_cutoff, direction_t direction)

Measures Back-EMF voltage (proportional to the rotational speed of the motor) on GPIO 28 and GPIO 29 respectively (depending on direction).

This function performs multiple ADC measurements, sorts the results using insertion sort, and calculates the average of the middle values, discarding a specified number of the lowest and highest values.

Parameters:
  • total_iterations – The total number of ADC measurements to perform.

  • measurement_delay_us – The delay in microseconds between the switch-off of the motor PWM and the measurement.

  • l_side_arr_cutoff – The number of lowest ADC values to discard.

  • r_side_arr_cutoff – The number of highest ADC values to discard.

  • direction – The direction of the motor.

Returns:

The average of the middle ADC values after discarding the specified number of lowest and highest values.

uint8_t get_speed_step_table_index_of_speed_step(uint8_t speed_step)

Get the speed step table index based on the speed step value.

Parameters:

speed_step – The speed step value.

Returns:

The index in the speed step table.

void speed_helper(controller_parameter_t *ctrl_par)

Repeating timer callback function called every x milliseconds to implement a time delay in acceleration or deceleration.

x is CV_175

Parameters:

ctrl_par – Pointer to the controller parameter structure.

void adjust_pwm_level(uint16_t level)

Function to adjust the PWM level/duty cycle.

Parameters:

level – The new PWM level.

float get_kp(const controller_parameter_t *ctrl_par)

Function to calculate and return the proportional gain based on the speed step.

This is done in order to make kp dependent on the speed step (gain scheduling).

Parameters:

ctrl_par – Pointer to the controller parameter structure.

Returns:

The proportional gain.

uint16_t get_initial_level(controller_parameter_t *ctrl_par)

Calculate and return the initial PWM level.

Calculation is done by taking the last 16 initial levels (when available) and calculating the average. The average is the multiplied by a factor of 2/3.

Parameters:

ctrl_par – Pointer to the controller parameter structure.

Returns:

The initial PWM level.

void controller_startup_mode(controller_parameter_t *ctrl_par)

Controller function for startup mode.

This is called inside the controller_general() function when the controller mode is set to STARTUP_MODE.

Parameters:

ctrl_par – Pointer to the controller parameter structure.

void controller_pid_mode(controller_parameter_t *const ctrl_par)

Controller function for PID mode.

This is called inside the controller_general() function when the controller mode is set to PID_MODE.

Parameters:

ctrl_par – Pointer to the controller parameter structure.

void controller_general(controller_parameter_t *ctrl_par)

General controller function called every x milliseconds.

Parameters:

ctrl_par – Pointer to the controller parameter structure.

void init_controller(controller_parameter_t *ctrl_par)

Initialize controller variables, measurement parameters, and speed table.

Parameters:

ctrl_par – Pointer to the controller parameter structure.

struct startup_parameters_t

Public Members

uint16_t level
uint16_t base_pwm_arr[BASE_PWM_ARR_LEN]
uint16_t base_pwm_arr_i
struct pid_parameters_t

Public Members

float k_i
float k_d
float t
float tau
float ci_0
float cd_0
float cd_1
float int_lim_max
float int_lim_min
float max_output
float k_p
float e
float e_prev
float i_prev
float d_prev
float k_p_x_1_shift
float k_p_x_1
float k_p_x_2
float k_p_y_0
float k_p_y_1
float k_p_y_2
float k_p_m_1
float k_p_m_2
struct controller_parameter_t

Public Members

controller_mode_t mode

General Variables

float feed_fwd
uint32_t setpoint
uint16_t speed_table[127]
startup_parameters_t startup

Startup controller mode specific variables

pid_parameter_t pid

PID Controller variables

float measurement

Measurement Variables

float measurement_prev
float measurement_corrected
float adc_offset
uint8_t msr_delay_in_us
uint8_t msr_total_iterations
uint8_t l_side_arr_cutoff
uint8_t r_side_arr_cutoff

shared.h

Used by both cores

Defines

LOG(level, ...)
_125M

Enums

enum error_t

Values:

enumerator FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE
enumerator FLASH_SAFE_EXECUTE_ERASE_FAILURE
enumerator FLASH_SAFE_EXECUTE_PROGRAM_FAILURE
enumerator FLASH_SIZE_READBACK_FAILURE
enumerator STDIO_INIT_FAILURE
enumerator REBOOT_BY_WATCHDOG
enumerator INVALID_DIRECTION
enum direction_t

Values:

enumerator DIRECTION_FORWARD
enumerator DIRECTION_REVERSE
enum speed_step_t

Values:

enumerator SPEED_STEP_REVERSE_STOP
enumerator SPEED_STEP_REVERSE_EMERGENCY_STOP
enumerator SPEED_STEP_REVERSE_001
enumerator SPEED_STEP_REVERSE_002
enumerator SPEED_STEP_REVERSE_003
enumerator SPEED_STEP_REVERSE_004
enumerator SPEED_STEP_REVERSE_005
enumerator SPEED_STEP_REVERSE_006
enumerator SPEED_STEP_REVERSE_007
enumerator SPEED_STEP_REVERSE_008
enumerator SPEED_STEP_REVERSE_009
enumerator SPEED_STEP_REVERSE_010
enumerator SPEED_STEP_REVERSE_011
enumerator SPEED_STEP_REVERSE_012
enumerator SPEED_STEP_REVERSE_013
enumerator SPEED_STEP_REVERSE_014
enumerator SPEED_STEP_REVERSE_015
enumerator SPEED_STEP_REVERSE_016
enumerator SPEED_STEP_REVERSE_017
enumerator SPEED_STEP_REVERSE_018
enumerator SPEED_STEP_REVERSE_019
enumerator SPEED_STEP_REVERSE_020
enumerator SPEED_STEP_REVERSE_021
enumerator SPEED_STEP_REVERSE_022
enumerator SPEED_STEP_REVERSE_023
enumerator SPEED_STEP_REVERSE_024
enumerator SPEED_STEP_REVERSE_025
enumerator SPEED_STEP_REVERSE_026
enumerator SPEED_STEP_REVERSE_027
enumerator SPEED_STEP_REVERSE_028
enumerator SPEED_STEP_REVERSE_029
enumerator SPEED_STEP_REVERSE_030
enumerator SPEED_STEP_REVERSE_031
enumerator SPEED_STEP_REVERSE_032
enumerator SPEED_STEP_REVERSE_033
enumerator SPEED_STEP_REVERSE_034
enumerator SPEED_STEP_REVERSE_035
enumerator SPEED_STEP_REVERSE_036
enumerator SPEED_STEP_REVERSE_037
enumerator SPEED_STEP_REVERSE_038
enumerator SPEED_STEP_REVERSE_039
enumerator SPEED_STEP_REVERSE_040
enumerator SPEED_STEP_REVERSE_041
enumerator SPEED_STEP_REVERSE_042
enumerator SPEED_STEP_REVERSE_043
enumerator SPEED_STEP_REVERSE_044
enumerator SPEED_STEP_REVERSE_045
enumerator SPEED_STEP_REVERSE_046
enumerator SPEED_STEP_REVERSE_047
enumerator SPEED_STEP_REVERSE_048
enumerator SPEED_STEP_REVERSE_049
enumerator SPEED_STEP_REVERSE_050
enumerator SPEED_STEP_REVERSE_051
enumerator SPEED_STEP_REVERSE_052
enumerator SPEED_STEP_REVERSE_053
enumerator SPEED_STEP_REVERSE_054
enumerator SPEED_STEP_REVERSE_055
enumerator SPEED_STEP_REVERSE_056
enumerator SPEED_STEP_REVERSE_057
enumerator SPEED_STEP_REVERSE_058
enumerator SPEED_STEP_REVERSE_059
enumerator SPEED_STEP_REVERSE_060
enumerator SPEED_STEP_REVERSE_061
enumerator SPEED_STEP_REVERSE_062
enumerator SPEED_STEP_REVERSE_063
enumerator SPEED_STEP_REVERSE_064
enumerator SPEED_STEP_REVERSE_065
enumerator SPEED_STEP_REVERSE_066
enumerator SPEED_STEP_REVERSE_067
enumerator SPEED_STEP_REVERSE_068
enumerator SPEED_STEP_REVERSE_069
enumerator SPEED_STEP_REVERSE_070
enumerator SPEED_STEP_REVERSE_071
enumerator SPEED_STEP_REVERSE_072
enumerator SPEED_STEP_REVERSE_073
enumerator SPEED_STEP_REVERSE_074
enumerator SPEED_STEP_REVERSE_075
enumerator SPEED_STEP_REVERSE_076
enumerator SPEED_STEP_REVERSE_077
enumerator SPEED_STEP_REVERSE_078
enumerator SPEED_STEP_REVERSE_079
enumerator SPEED_STEP_REVERSE_080
enumerator SPEED_STEP_REVERSE_081
enumerator SPEED_STEP_REVERSE_082
enumerator SPEED_STEP_REVERSE_083
enumerator SPEED_STEP_REVERSE_084
enumerator SPEED_STEP_REVERSE_085
enumerator SPEED_STEP_REVERSE_086
enumerator SPEED_STEP_REVERSE_087
enumerator SPEED_STEP_REVERSE_088
enumerator SPEED_STEP_REVERSE_089
enumerator SPEED_STEP_REVERSE_090
enumerator SPEED_STEP_REVERSE_091
enumerator SPEED_STEP_REVERSE_092
enumerator SPEED_STEP_REVERSE_093
enumerator SPEED_STEP_REVERSE_094
enumerator SPEED_STEP_REVERSE_095
enumerator SPEED_STEP_REVERSE_096
enumerator SPEED_STEP_REVERSE_097
enumerator SPEED_STEP_REVERSE_098
enumerator SPEED_STEP_REVERSE_099
enumerator SPEED_STEP_REVERSE_100
enumerator SPEED_STEP_REVERSE_101
enumerator SPEED_STEP_REVERSE_102
enumerator SPEED_STEP_REVERSE_103
enumerator SPEED_STEP_REVERSE_104
enumerator SPEED_STEP_REVERSE_105
enumerator SPEED_STEP_REVERSE_106
enumerator SPEED_STEP_REVERSE_107
enumerator SPEED_STEP_REVERSE_108
enumerator SPEED_STEP_REVERSE_109
enumerator SPEED_STEP_REVERSE_110
enumerator SPEED_STEP_REVERSE_111
enumerator SPEED_STEP_REVERSE_112
enumerator SPEED_STEP_REVERSE_113
enumerator SPEED_STEP_REVERSE_114
enumerator SPEED_STEP_REVERSE_115
enumerator SPEED_STEP_REVERSE_116
enumerator SPEED_STEP_REVERSE_117
enumerator SPEED_STEP_REVERSE_118
enumerator SPEED_STEP_REVERSE_119
enumerator SPEED_STEP_REVERSE_120
enumerator SPEED_STEP_REVERSE_121
enumerator SPEED_STEP_REVERSE_122
enumerator SPEED_STEP_REVERSE_123
enumerator SPEED_STEP_REVERSE_124
enumerator SPEED_STEP_REVERSE_125
enumerator SPEED_STEP_REVERSE_126
enumerator SPEED_STEP_FORWARD_STOP
enumerator SPEED_STEP_FORWARD_EMERGENCY_STOP
enumerator SPEED_STEP_FORWARD_001
enumerator SPEED_STEP_FORWARD_002
enumerator SPEED_STEP_FORWARD_003
enumerator SPEED_STEP_FORWARD_004
enumerator SPEED_STEP_FORWARD_005
enumerator SPEED_STEP_FORWARD_006
enumerator SPEED_STEP_FORWARD_007
enumerator SPEED_STEP_FORWARD_008
enumerator SPEED_STEP_FORWARD_009
enumerator SPEED_STEP_FORWARD_010
enumerator SPEED_STEP_FORWARD_011
enumerator SPEED_STEP_FORWARD_012
enumerator SPEED_STEP_FORWARD_013
enumerator SPEED_STEP_FORWARD_014
enumerator SPEED_STEP_FORWARD_015
enumerator SPEED_STEP_FORWARD_016
enumerator SPEED_STEP_FORWARD_017
enumerator SPEED_STEP_FORWARD_018
enumerator SPEED_STEP_FORWARD_019
enumerator SPEED_STEP_FORWARD_020
enumerator SPEED_STEP_FORWARD_021
enumerator SPEED_STEP_FORWARD_022
enumerator SPEED_STEP_FORWARD_023
enumerator SPEED_STEP_FORWARD_024
enumerator SPEED_STEP_FORWARD_025
enumerator SPEED_STEP_FORWARD_026
enumerator SPEED_STEP_FORWARD_027
enumerator SPEED_STEP_FORWARD_028
enumerator SPEED_STEP_FORWARD_029
enumerator SPEED_STEP_FORWARD_030
enumerator SPEED_STEP_FORWARD_031
enumerator SPEED_STEP_FORWARD_032
enumerator SPEED_STEP_FORWARD_033
enumerator SPEED_STEP_FORWARD_034
enumerator SPEED_STEP_FORWARD_035
enumerator SPEED_STEP_FORWARD_036
enumerator SPEED_STEP_FORWARD_037
enumerator SPEED_STEP_FORWARD_038
enumerator SPEED_STEP_FORWARD_039
enumerator SPEED_STEP_FORWARD_040
enumerator SPEED_STEP_FORWARD_041
enumerator SPEED_STEP_FORWARD_042
enumerator SPEED_STEP_FORWARD_043
enumerator SPEED_STEP_FORWARD_044
enumerator SPEED_STEP_FORWARD_045
enumerator SPEED_STEP_FORWARD_046
enumerator SPEED_STEP_FORWARD_047
enumerator SPEED_STEP_FORWARD_048
enumerator SPEED_STEP_FORWARD_049
enumerator SPEED_STEP_FORWARD_050
enumerator SPEED_STEP_FORWARD_051
enumerator SPEED_STEP_FORWARD_052
enumerator SPEED_STEP_FORWARD_053
enumerator SPEED_STEP_FORWARD_054
enumerator SPEED_STEP_FORWARD_055
enumerator SPEED_STEP_FORWARD_056
enumerator SPEED_STEP_FORWARD_057
enumerator SPEED_STEP_FORWARD_058
enumerator SPEED_STEP_FORWARD_059
enumerator SPEED_STEP_FORWARD_060
enumerator SPEED_STEP_FORWARD_061
enumerator SPEED_STEP_FORWARD_062
enumerator SPEED_STEP_FORWARD_063
enumerator SPEED_STEP_FORWARD_064
enumerator SPEED_STEP_FORWARD_065
enumerator SPEED_STEP_FORWARD_066
enumerator SPEED_STEP_FORWARD_067
enumerator SPEED_STEP_FORWARD_068
enumerator SPEED_STEP_FORWARD_069
enumerator SPEED_STEP_FORWARD_070
enumerator SPEED_STEP_FORWARD_071
enumerator SPEED_STEP_FORWARD_072
enumerator SPEED_STEP_FORWARD_073
enumerator SPEED_STEP_FORWARD_074
enumerator SPEED_STEP_FORWARD_075
enumerator SPEED_STEP_FORWARD_076
enumerator SPEED_STEP_FORWARD_077
enumerator SPEED_STEP_FORWARD_078
enumerator SPEED_STEP_FORWARD_079
enumerator SPEED_STEP_FORWARD_080
enumerator SPEED_STEP_FORWARD_081
enumerator SPEED_STEP_FORWARD_082
enumerator SPEED_STEP_FORWARD_083
enumerator SPEED_STEP_FORWARD_084
enumerator SPEED_STEP_FORWARD_085
enumerator SPEED_STEP_FORWARD_086
enumerator SPEED_STEP_FORWARD_087
enumerator SPEED_STEP_FORWARD_088
enumerator SPEED_STEP_FORWARD_089
enumerator SPEED_STEP_FORWARD_090
enumerator SPEED_STEP_FORWARD_091
enumerator SPEED_STEP_FORWARD_092
enumerator SPEED_STEP_FORWARD_093
enumerator SPEED_STEP_FORWARD_094
enumerator SPEED_STEP_FORWARD_095
enumerator SPEED_STEP_FORWARD_096
enumerator SPEED_STEP_FORWARD_097
enumerator SPEED_STEP_FORWARD_098
enumerator SPEED_STEP_FORWARD_099
enumerator SPEED_STEP_FORWARD_100
enumerator SPEED_STEP_FORWARD_101
enumerator SPEED_STEP_FORWARD_102
enumerator SPEED_STEP_FORWARD_103
enumerator SPEED_STEP_FORWARD_104
enumerator SPEED_STEP_FORWARD_105
enumerator SPEED_STEP_FORWARD_106
enumerator SPEED_STEP_FORWARD_107
enumerator SPEED_STEP_FORWARD_108
enumerator SPEED_STEP_FORWARD_109
enumerator SPEED_STEP_FORWARD_110
enumerator SPEED_STEP_FORWARD_111
enumerator SPEED_STEP_FORWARD_112
enumerator SPEED_STEP_FORWARD_113
enumerator SPEED_STEP_FORWARD_114
enumerator SPEED_STEP_FORWARD_115
enumerator SPEED_STEP_FORWARD_116
enumerator SPEED_STEP_FORWARD_117
enumerator SPEED_STEP_FORWARD_118
enumerator SPEED_STEP_FORWARD_119
enumerator SPEED_STEP_FORWARD_120
enumerator SPEED_STEP_FORWARD_121
enumerator SPEED_STEP_FORWARD_122
enumerator SPEED_STEP_FORWARD_123
enumerator SPEED_STEP_FORWARD_124
enumerator SPEED_STEP_FORWARD_125
enumerator SPEED_STEP_FORWARD_126

Functions

void core1_entry()
uint32_t get_32bit_CV(uint16_t CV_start_index)
uint16_t get_16bit_CV(uint16_t CV_start_index)
direction_t get_direction_of_speed_step(speed_step_t speed_step)

Calculates the direction based on a speed step byte.

This function calculates the direction of movement based on the value of the speed_step byte. It refers to the “128 Speed Control” standard as outlined in NMRA Standard 9.2.1 section 2.3.2.1 or RCN-212 section 2.2.2.

The following speed steps are defined:

DEC

BIN

DIRECTION

DEFINITION

000

0000 0000

Reverse

STOP

001

0000 0001

Reverse

EMERGENCY STOP

002

0000 0010

Reverse

Speed step 001

003

0000 0011

Reverse

Speed step 002

004

0000 0100

Reverse

Speed step 003

…

…. ….

Reverse

…

126

0111 1110

Reverse

Speed step 125

127

0111 1111

Reverse

Speed step 126

128

1000 0000

Forward

STOP

129

1000 0001

Forward

EMERGENCY STOP

130

1000 0010

Forward

Speed step 001

131

1000 0011

Forward

Speed step 002

132

1000 0100

Forward

Speed step 003

…

…. ….

Forward

…

254

1111 1110

Forward

Speed step 125

255

1111 1111

Forward

Speed step 126

Parameters:

speed_step – The speed step byte.

Returns:

DIRECTION_FORWARD for forward direction, DIRECTION_REVERSE for reverse direction.

void set_error(error_t err)
void clear_error(error_t err)
void clear_all_errors()
error_t get_error_state()

Variables

repeating_timer_t pid_control_timer
repeating_timer_t speed_helper_timer
const uint8_t *CV_ARRAY_FLASH
speed_step_t speed_step_target
speed_step_t speed_step_target_prev
bool cv_setup_check_done
bool flash_safe_execute_core_init_done

CV.h

Defines

CV_ARRAY_SIZE

Variables

uint8_t CV_ARRAY_DEFAULT[CV_ARRAY_SIZE]