core0.h

Used exclusively by core0

Defines

MESSAGE_3_BYTES

Comparison bitmask for detecting 3 byte long DCC messages/packets.

MESSAGE_MASK_3_BYTES

Bitmask for detecting 3 byte long DCC messages/packets, input_bit_buffer is bitwise ANDed with the mask.

MESSAGE_4_BYTES

Comparison bitmask for detecting 4 byte long DCC messages/packets.

MESSAGE_MASK_4_BYTES

Bitmask for detecting 4 byte long DCC messages/packets, input_bit_buffer is bitwise ANDed with the mask.

MESSAGE_5_BYTES

Comparison bitmask for detecting 5 byte long DCC messages/packets.

MESSAGE_MASK_5_BYTES

Bitmask for detecting 5 byte long DCC messages/packets, input_bit_buffer is bitwise ANDed with the mask.

INVALID_PACKAGE

Return value of detect_dcc_packet() when the packet is invalid, meaning no packet was detected.

RING_BUFFER_PACKETS

First dimension of dcc ring buffer array - meaning RING_BUFFER_PACKETS can be retained inside the buffer.

RING_BUFFER_BYTES

Secoond dimension of dcc ring buffer array - meaning each packet can have a maximum size of RING_BUFFER_BYTES.

FLASH_CMD_READ_JEDEC_ID

Constant value of 0x9F used as a JEDEC ID read command for reading the JEDEC ID of a winbond flash memory chip.

FLASH_TIMEOUT_IN_MS

Timeout value for flash access.

ADC_CALIBRATION_ITERATIONS

Iterations for measuring adc offset.

WATCHDOG_TIMER_IN_MS

Watchdog timer setting, watchdog update needs to be called every WATCHDOG_TIMER_IN_MS milliseconds, otherwise decoder resets itself.

Typedefs

typedef struct dcc_packet_t dcc_packet_t

Structure representing a DCC packet.

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

typedef struct dcc_ring_buffer_t dcc_ring_buffer_t

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.

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>
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

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

The length of the base PWM array.

This macro defines the length of the base PWM ring buffer array.

Typedefs

typedef struct startup_parameters_t startup_parameters_t

Structure for startup controller parameters and variables.

This structure defines the parameters and variables used in the startup controller, including the latest PWM level, base pwm level ring buffer array and the respective ring buffer index.

typedef struct pid_parameters_t pid_parameters_t

Structure for PID controller parameters and variables.

This structure defines the parameters and variables used in the PID controller, including gains, error tracking, integrator limits, …

typedef struct controller_parameter_t controller_parameter_t

Structure for various controller parameters.

This structure defines the highest level general controller parameters, including General settings (setpoint, mode), startup controller settings struct, PID controller struct, and measurement data.

Enums

enum controller_mode_t

Enumeration for controller operating modes.

Defines the two controller operating modes.

Values:

enumerator STARTUP_MODE

Controller startup mode state

enumerator PID_MODE

Controller pid mode state

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

Latest level

uint16_t base_pwm_arr[BASE_PWM_ARR_LEN]

base pwm ring buffer array

uint16_t base_pwm_arr_i

base pwm ring buffer array index

float k_ff

Feed forward factor

struct pid_parameters_t

Public Members

float k_i

Integral gain

float k_d

Derivative gain

float t

Sampling time

float tau

Low-pass-filter time constant

float ci_0

(k_i*t)/2

float cd_0

(k_d*2)/(2*tau+t)

float cd_1

(2*tau-t)/(2*tau+t)

float int_lim_max

max limit for integrator

float int_lim_min

min limit for integrator

float max_output

max possible pwm output value

float k_p

Proportional gain

float e

Error

float e_prev

Previous Error

float i_prev

Previous Integral Value

float d_prev

Previous Derivative Value

float k_p_x_1_shift

Effectively defines where x1 is shifted from 0% of max setpoint to 100% of max setpoint

float k_p_x_1

x1

float k_p_x_2

x2

float k_p_y_0

y0 = KP @ x0

float k_p_y_1

y1 = KP @ x1

float k_p_y_2

y2 = KP @ x2

float k_p_m_1

slope from (x0, y0) to (x1, y1)

float k_p_m_2

slope from (x1, y1) to (x2, y2)

struct controller_parameter_t

Public Members

controller_mode_t mode

Current controller mode

float feed_fwd

Current feed forward value set by startup controller

uint32_t setpoint

Current setpoint

uint16_t speed_table[127]

Array with setpoint values corresponding to every speed step

startup_parameters_t startup

Struct for startup controller specific variables

pid_parameters_t pid

Struct for PID controller specific variables

float measurement

Latest measurement value

float measurement_prev

Previous measurement value

float measurement_corrected

Corrected measurement value (measurement - adc_offset = measurement_corrected)

float adc_offset

ADC offset value

uint8_t msr_delay_in_us

Delay before V_EMF is measured

uint8_t msr_total_iterations

Amount of samples

uint8_t l_side_arr_cutoff

Discarded outlier samples (left side)

uint8_t r_side_arr_cutoff

Discarded outlier samples (right side)

shared.h

Used by both cores

Defines

LOG(level, ...)

Logs a message with a specified log level.

This macro logs a message if the specified log level is less than or equal to the global LOGLEVEL. The log message includes the core number and the time since boot in milliseconds.

Parameters:
  • level – The log level of the message.

  • ... – The format string and arguments for the log message.

_125M

Constant value representing 125 million (125 x 10^6).

Enums

enum error_t

Enumeration of error codes used in the system.

This enumeration defines various error codes that can be encountered during the execution of the program. Each error code is represented as a bit flag, allowing multiple errors to be combined using bitwise operations.

Values:

enumerator FLASH_SAFE_EXECUTE_CORE_INIT_FAILURE

Indicates a failure during the initialization of the flash safe execute mode.

enumerator FLASH_SAFE_EXECUTE_ERASE_FAILURE

Indicates a failure during the erase operation in flash safe execute mode.

enumerator FLASH_SAFE_EXECUTE_PROGRAM_FAILURE

Indicates a failure during the program operation in flash safe execute mode.

enumerator FLASH_SIZE_READBACK_FAILURE

Indicates a failure in reading back the flash size.

enumerator STDIO_INIT_FAILURE

Indicates a failure during the initialization of stdio.

enumerator REBOOT_BY_WATCHDOG

Indicates that the system rebooted due to the watchdog timer.

enumerator INVALID_DIRECTION

Indicates an invalid direction was specified.

enum direction_t

Enumeration for motor direction.

Values:

enumerator DIRECTION_FORWARD

Represents the forward direction

enumerator DIRECTION_REVERSE

Represents the reverse direction.

enum speed_step_t

Enum to define all speed steps.

Values:

enumerator SPEED_STEP_REVERSE_STOP

00000000bin 000dec

enumerator SPEED_STEP_REVERSE_EMERGENCY_STOP

00000001bin 001dec

enumerator SPEED_STEP_REVERSE_001

00000010bin 002dec

enumerator SPEED_STEP_REVERSE_002

00000011bin 003dec

enumerator SPEED_STEP_REVERSE_003

00000100bin 004dec

enumerator SPEED_STEP_REVERSE_004

00000101bin 005dec

enumerator SPEED_STEP_REVERSE_005

00000110bin 006dec

enumerator SPEED_STEP_REVERSE_006

00000111bin 007dec

enumerator SPEED_STEP_REVERSE_007

00001000bin 008dec

enumerator SPEED_STEP_REVERSE_008

00001001bin 009dec

enumerator SPEED_STEP_REVERSE_009

00001010bin 010dec

enumerator SPEED_STEP_REVERSE_010

00001011bin 011dec

enumerator SPEED_STEP_REVERSE_011

00001100bin 012dec

enumerator SPEED_STEP_REVERSE_012

00001101bin 013dec

enumerator SPEED_STEP_REVERSE_013

00001110bin 014dec

enumerator SPEED_STEP_REVERSE_014

00001111bin 015dec

enumerator SPEED_STEP_REVERSE_015

00010000bin 016dec

enumerator SPEED_STEP_REVERSE_016

00010001bin 017dec

enumerator SPEED_STEP_REVERSE_017

00010010bin 018dec

enumerator SPEED_STEP_REVERSE_018

00010011bin 019dec

enumerator SPEED_STEP_REVERSE_019

00010100bin 020dec

enumerator SPEED_STEP_REVERSE_020

00010101bin 021dec

enumerator SPEED_STEP_REVERSE_021

00010110bin 022dec

enumerator SPEED_STEP_REVERSE_022

00010111bin 023dec

enumerator SPEED_STEP_REVERSE_023

00011000bin 024dec

enumerator SPEED_STEP_REVERSE_024

00011001bin 025dec

enumerator SPEED_STEP_REVERSE_025

00011010bin 026dec

enumerator SPEED_STEP_REVERSE_026

00011011bin 027dec

enumerator SPEED_STEP_REVERSE_027

00011100bin 028dec

enumerator SPEED_STEP_REVERSE_028

00011101bin 029dec

enumerator SPEED_STEP_REVERSE_029

00011110bin 030dec

enumerator SPEED_STEP_REVERSE_030

00011111bin 031dec

enumerator SPEED_STEP_REVERSE_031

00100000bin 032dec

enumerator SPEED_STEP_REVERSE_032

00100001bin 033dec

enumerator SPEED_STEP_REVERSE_033

00100010bin 034dec

enumerator SPEED_STEP_REVERSE_034

00100011bin 035dec

enumerator SPEED_STEP_REVERSE_035

00100100bin 036dec

enumerator SPEED_STEP_REVERSE_036

00100101bin 037dec

enumerator SPEED_STEP_REVERSE_037

00100110bin 038dec

enumerator SPEED_STEP_REVERSE_038

00100111bin 039dec

enumerator SPEED_STEP_REVERSE_039

00101000bin 040dec

enumerator SPEED_STEP_REVERSE_040

00101001bin 041dec

enumerator SPEED_STEP_REVERSE_041

00101010bin 042dec

enumerator SPEED_STEP_REVERSE_042

00101011bin 043dec

enumerator SPEED_STEP_REVERSE_043

00101100bin 044dec

enumerator SPEED_STEP_REVERSE_044

00101101bin 045dec

enumerator SPEED_STEP_REVERSE_045

00101110bin 046dec

enumerator SPEED_STEP_REVERSE_046

00101111bin 047dec

enumerator SPEED_STEP_REVERSE_047

00110000bin 048dec

enumerator SPEED_STEP_REVERSE_048

00110001bin 049dec

enumerator SPEED_STEP_REVERSE_049

00110010bin 050dec

enumerator SPEED_STEP_REVERSE_050

00110011bin 051dec

enumerator SPEED_STEP_REVERSE_051

00110100bin 052dec

enumerator SPEED_STEP_REVERSE_052

00110101bin 053dec

enumerator SPEED_STEP_REVERSE_053

00110110bin 054dec

enumerator SPEED_STEP_REVERSE_054

00110111bin 055dec

enumerator SPEED_STEP_REVERSE_055

00111000bin 056dec

enumerator SPEED_STEP_REVERSE_056

00111001bin 057dec

enumerator SPEED_STEP_REVERSE_057

00111010bin 058dec

enumerator SPEED_STEP_REVERSE_058

00111011bin 059dec

enumerator SPEED_STEP_REVERSE_059

00111100bin 060dec

enumerator SPEED_STEP_REVERSE_060

00111101bin 061dec

enumerator SPEED_STEP_REVERSE_061

00111110bin 062dec

enumerator SPEED_STEP_REVERSE_062

00111111bin 063dec

enumerator SPEED_STEP_REVERSE_063

01000000bin 064dec

enumerator SPEED_STEP_REVERSE_064

01000001bin 065dec

enumerator SPEED_STEP_REVERSE_065

01000010bin 066dec

enumerator SPEED_STEP_REVERSE_066

01000011bin 067dec

enumerator SPEED_STEP_REVERSE_067

01000100bin 068dec

enumerator SPEED_STEP_REVERSE_068

01000101bin 069dec

enumerator SPEED_STEP_REVERSE_069

01000110bin 070dec

enumerator SPEED_STEP_REVERSE_070

01000111bin 071dec

enumerator SPEED_STEP_REVERSE_071

01001000bin 072dec

enumerator SPEED_STEP_REVERSE_072

01001001bin 073dec

enumerator SPEED_STEP_REVERSE_073

01001010bin 074dec

enumerator SPEED_STEP_REVERSE_074

01001011bin 075dec

enumerator SPEED_STEP_REVERSE_075

01001100bin 076dec

enumerator SPEED_STEP_REVERSE_076

01001101bin 077dec

enumerator SPEED_STEP_REVERSE_077

01001110bin 078dec

enumerator SPEED_STEP_REVERSE_078

01001111bin 079dec

enumerator SPEED_STEP_REVERSE_079

01010000bin 080dec

enumerator SPEED_STEP_REVERSE_080

01010001bin 081dec

enumerator SPEED_STEP_REVERSE_081

01010010bin 082dec

enumerator SPEED_STEP_REVERSE_082

01010011bin 083dec

enumerator SPEED_STEP_REVERSE_083

01010100bin 084dec

enumerator SPEED_STEP_REVERSE_084

01010101bin 085dec

enumerator SPEED_STEP_REVERSE_085

01010110bin 086dec

enumerator SPEED_STEP_REVERSE_086

01010111bin 087dec

enumerator SPEED_STEP_REVERSE_087

01011000bin 088dec

enumerator SPEED_STEP_REVERSE_088

01011001bin 089dec

enumerator SPEED_STEP_REVERSE_089

01011010bin 090dec

enumerator SPEED_STEP_REVERSE_090

01011011bin 091dec

enumerator SPEED_STEP_REVERSE_091

01011100bin 092dec

enumerator SPEED_STEP_REVERSE_092

01011101bin 093dec

enumerator SPEED_STEP_REVERSE_093

01011110bin 094dec

enumerator SPEED_STEP_REVERSE_094

01011111bin 095dec

enumerator SPEED_STEP_REVERSE_095

01100000bin 096dec

enumerator SPEED_STEP_REVERSE_096

01100001bin 097dec

enumerator SPEED_STEP_REVERSE_097

01100010bin 098dec

enumerator SPEED_STEP_REVERSE_098

01100011bin 099dec

enumerator SPEED_STEP_REVERSE_099

01100100bin 100dec

enumerator SPEED_STEP_REVERSE_100

01100101bin 101dec

enumerator SPEED_STEP_REVERSE_101

01100110bin 102dec

enumerator SPEED_STEP_REVERSE_102

01100111bin 103dec

enumerator SPEED_STEP_REVERSE_103

01101000bin 104dec

enumerator SPEED_STEP_REVERSE_104

01101001bin 105dec

enumerator SPEED_STEP_REVERSE_105

01101010bin 106dec

enumerator SPEED_STEP_REVERSE_106

01101011bin 107dec

enumerator SPEED_STEP_REVERSE_107

01101100bin 108dec

enumerator SPEED_STEP_REVERSE_108

01101101bin 109dec

enumerator SPEED_STEP_REVERSE_109

01101110bin 110dec

enumerator SPEED_STEP_REVERSE_110

01101111bin 111dec

enumerator SPEED_STEP_REVERSE_111

01110000bin 112dec

enumerator SPEED_STEP_REVERSE_112

01110001bin 113dec

enumerator SPEED_STEP_REVERSE_113

01110010bin 114dec

enumerator SPEED_STEP_REVERSE_114

01110011bin 115dec

enumerator SPEED_STEP_REVERSE_115

01110100bin 116dec

enumerator SPEED_STEP_REVERSE_116

01110101bin 117dec

enumerator SPEED_STEP_REVERSE_117

01110110bin 118dec

enumerator SPEED_STEP_REVERSE_118

01110111bin 119dec

enumerator SPEED_STEP_REVERSE_119

01111000bin 120dec

enumerator SPEED_STEP_REVERSE_120

01111001bin 121dec

enumerator SPEED_STEP_REVERSE_121

01111010bin 122dec

enumerator SPEED_STEP_REVERSE_122

01111011bin 123dec

enumerator SPEED_STEP_REVERSE_123

01111100bin 124dec

enumerator SPEED_STEP_REVERSE_124

01111101bin 125dec

enumerator SPEED_STEP_REVERSE_125

01111110bin 126dec

enumerator SPEED_STEP_REVERSE_126

01111111bin 127dec

enumerator SPEED_STEP_FORWARD_STOP

10000000bin 128dec

enumerator SPEED_STEP_FORWARD_EMERGENCY_STOP

10000001bin 129dec

enumerator SPEED_STEP_FORWARD_001

10000010bin 130dec

enumerator SPEED_STEP_FORWARD_002

10000011bin 131dec

enumerator SPEED_STEP_FORWARD_003

10000100bin 132dec

enumerator SPEED_STEP_FORWARD_004

10000101bin 133dec

enumerator SPEED_STEP_FORWARD_005

10000110bin 134dec

enumerator SPEED_STEP_FORWARD_006

10000111bin 135dec

enumerator SPEED_STEP_FORWARD_007

10001000bin 136dec

enumerator SPEED_STEP_FORWARD_008

10001001bin 137dec

enumerator SPEED_STEP_FORWARD_009

10001010bin 138dec

enumerator SPEED_STEP_FORWARD_010

10001011bin 139dec

enumerator SPEED_STEP_FORWARD_011

10001100bin 140dec

enumerator SPEED_STEP_FORWARD_012

10001101bin 141dec

enumerator SPEED_STEP_FORWARD_013

10001110bin 142dec

enumerator SPEED_STEP_FORWARD_014

10001111bin 143dec

enumerator SPEED_STEP_FORWARD_015

10010000bin 144dec

enumerator SPEED_STEP_FORWARD_016

10010001bin 145dec

enumerator SPEED_STEP_FORWARD_017

10010010bin 146dec

enumerator SPEED_STEP_FORWARD_018

10010011bin 147dec

enumerator SPEED_STEP_FORWARD_019

10010100bin 148dec

enumerator SPEED_STEP_FORWARD_020

10010101bin 149dec

enumerator SPEED_STEP_FORWARD_021

10010110bin 150dec

enumerator SPEED_STEP_FORWARD_022

10010111bin 151dec

enumerator SPEED_STEP_FORWARD_023

10011000bin 152dec

enumerator SPEED_STEP_FORWARD_024

10011001bin 153dec

enumerator SPEED_STEP_FORWARD_025

10011010bin 154dec

enumerator SPEED_STEP_FORWARD_026

10011011bin 155dec

enumerator SPEED_STEP_FORWARD_027

10011100bin 156dec

enumerator SPEED_STEP_FORWARD_028

10011101bin 157dec

enumerator SPEED_STEP_FORWARD_029

10011110bin 158dec

enumerator SPEED_STEP_FORWARD_030

10011111bin 159dec

enumerator SPEED_STEP_FORWARD_031

10100000bin 160dec

enumerator SPEED_STEP_FORWARD_032

10100001bin 161dec

enumerator SPEED_STEP_FORWARD_033

10100010bin 162dec

enumerator SPEED_STEP_FORWARD_034

10100011bin 163dec

enumerator SPEED_STEP_FORWARD_035

10100100bin 164dec

enumerator SPEED_STEP_FORWARD_036

10100101bin 165dec

enumerator SPEED_STEP_FORWARD_037

10100110bin 166dec

enumerator SPEED_STEP_FORWARD_038

10100111bin 167dec

enumerator SPEED_STEP_FORWARD_039

10101000bin 168dec

enumerator SPEED_STEP_FORWARD_040

10101001bin 169dec

enumerator SPEED_STEP_FORWARD_041

10101010bin 170dec

enumerator SPEED_STEP_FORWARD_042

10101011bin 171dec

enumerator SPEED_STEP_FORWARD_043

10101100bin 172dec

enumerator SPEED_STEP_FORWARD_044

10101101bin 173dec

enumerator SPEED_STEP_FORWARD_045

10101110bin 174dec

enumerator SPEED_STEP_FORWARD_046

10101111bin 175dec

enumerator SPEED_STEP_FORWARD_047

10110000bin 176dec

enumerator SPEED_STEP_FORWARD_048

10110001bin 177dec

enumerator SPEED_STEP_FORWARD_049

10110010bin 178dec

enumerator SPEED_STEP_FORWARD_050

10110011bin 179dec

enumerator SPEED_STEP_FORWARD_051

10110100bin 180dec

enumerator SPEED_STEP_FORWARD_052

10110101bin 181dec

enumerator SPEED_STEP_FORWARD_053

10110110bin 182dec

enumerator SPEED_STEP_FORWARD_054

10110111bin 183dec

enumerator SPEED_STEP_FORWARD_055

10111000bin 184dec

enumerator SPEED_STEP_FORWARD_056

10111001bin 185dec

enumerator SPEED_STEP_FORWARD_057

10111010bin 186dec

enumerator SPEED_STEP_FORWARD_058

10111011bin 187dec

enumerator SPEED_STEP_FORWARD_059

10111100bin 188dec

enumerator SPEED_STEP_FORWARD_060

10111101bin 189dec

enumerator SPEED_STEP_FORWARD_061

10111110bin 190dec

enumerator SPEED_STEP_FORWARD_062

10111111bin 191dec

enumerator SPEED_STEP_FORWARD_063

11000000bin 192dec

enumerator SPEED_STEP_FORWARD_064

11000001bin 193dec

enumerator SPEED_STEP_FORWARD_065

11000010bin 194dec

enumerator SPEED_STEP_FORWARD_066

11000011bin 195dec

enumerator SPEED_STEP_FORWARD_067

11000100bin 196dec

enumerator SPEED_STEP_FORWARD_068

11000101bin 197dec

enumerator SPEED_STEP_FORWARD_069

11000110bin 198dec

enumerator SPEED_STEP_FORWARD_070

11000111bin 199dec

enumerator SPEED_STEP_FORWARD_071

11001000bin 200dec

enumerator SPEED_STEP_FORWARD_072

11001001bin 201dec

enumerator SPEED_STEP_FORWARD_073

11001010bin 202dec

enumerator SPEED_STEP_FORWARD_074

11001011bin 203dec

enumerator SPEED_STEP_FORWARD_075

11001100bin 204dec

enumerator SPEED_STEP_FORWARD_076

11001101bin 205dec

enumerator SPEED_STEP_FORWARD_077

11001110bin 206dec

enumerator SPEED_STEP_FORWARD_078

11001111bin 207dec

enumerator SPEED_STEP_FORWARD_079

11010000bin 208dec

enumerator SPEED_STEP_FORWARD_080

11010001bin 209dec

enumerator SPEED_STEP_FORWARD_081

11010010bin 210dec

enumerator SPEED_STEP_FORWARD_082

11010011bin 211dec

enumerator SPEED_STEP_FORWARD_083

11010100bin 212dec

enumerator SPEED_STEP_FORWARD_084

11010101bin 213dec

enumerator SPEED_STEP_FORWARD_085

11010110bin 214dec

enumerator SPEED_STEP_FORWARD_086

11010111bin 215dec

enumerator SPEED_STEP_FORWARD_087

11011000bin 216dec

enumerator SPEED_STEP_FORWARD_088

11011001bin 217dec

enumerator SPEED_STEP_FORWARD_089

11011010bin 218dec

enumerator SPEED_STEP_FORWARD_090

11011011bin 219dec

enumerator SPEED_STEP_FORWARD_091

11011100bin 220dec

enumerator SPEED_STEP_FORWARD_092

11011101bin 221dec

enumerator SPEED_STEP_FORWARD_093

11011110bin 222dec

enumerator SPEED_STEP_FORWARD_094

11011111bin 223dec

enumerator SPEED_STEP_FORWARD_095

11100000bin 224dec

enumerator SPEED_STEP_FORWARD_096

11100001bin 225dec

enumerator SPEED_STEP_FORWARD_097

11100010bin 226dec

enumerator SPEED_STEP_FORWARD_098

11100011bin 227dec

enumerator SPEED_STEP_FORWARD_099

11100100bin 228dec

enumerator SPEED_STEP_FORWARD_100

11100101bin 229dec

enumerator SPEED_STEP_FORWARD_101

11100110bin 230dec

enumerator SPEED_STEP_FORWARD_102

11100111bin 231dec

enumerator SPEED_STEP_FORWARD_103

11101000bin 232dec

enumerator SPEED_STEP_FORWARD_104

11101001bin 233dec

enumerator SPEED_STEP_FORWARD_105

11101010bin 234dec

enumerator SPEED_STEP_FORWARD_106

11101011bin 235dec

enumerator SPEED_STEP_FORWARD_107

11101100bin 236dec

enumerator SPEED_STEP_FORWARD_108

11101101bin 237dec

enumerator SPEED_STEP_FORWARD_109

11101110bin 238dec

enumerator SPEED_STEP_FORWARD_110

11101111bin 239dec

enumerator SPEED_STEP_FORWARD_111

11110000bin 240dec

enumerator SPEED_STEP_FORWARD_112

11110001bin 241dec

enumerator SPEED_STEP_FORWARD_113

11110010bin 242dec

enumerator SPEED_STEP_FORWARD_114

11110011bin 243dec

enumerator SPEED_STEP_FORWARD_115

11110100bin 244dec

enumerator SPEED_STEP_FORWARD_116

11110101bin 245dec

enumerator SPEED_STEP_FORWARD_117

11110110bin 246dec

enumerator SPEED_STEP_FORWARD_118

11110111bin 247dec

enumerator SPEED_STEP_FORWARD_119

11111000bin 248dec

enumerator SPEED_STEP_FORWARD_120

11111001bin 249dec

enumerator SPEED_STEP_FORWARD_121

11111010bin 250dec

enumerator SPEED_STEP_FORWARD_122

11111011bin 251dec

enumerator SPEED_STEP_FORWARD_123

11111100bin 252dec

enumerator SPEED_STEP_FORWARD_124

11111101bin 253dec

enumerator SPEED_STEP_FORWARD_125

11111110bin 254dec

enumerator SPEED_STEP_FORWARD_126

11111111bin 255dec

Functions

void core1_entry()

Entry point for the second core (core 1).

This function is the entry point for the second core, basically the main() of core1.

uint32_t get_32bit_CV(uint16_t CV_start_index)

Retrieves a 32-bit Configuration Variable (CV) starting from the specified index.

This function reads and returns a 32-bit CV value from the given starting index.

Parameters:

CV_start_index – The starting index of the 32-bit CV to be retrieved.

Returns:

The 32-bit CV value.

uint16_t get_16bit_CV(uint16_t CV_start_index)

Retrieves a 16-bit Configuration Variable (CV) value starting from the specified index.

This function reads and returns a 16-bit CV value from the given starting index.

Parameters:

CV_start_index – The starting index of the 16-bit CV to be retrieved.

Returns:

The 16-bit CV value starting from the specified 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

also see speed_step_t for available steps.

Parameters:

speed_step – The speed step byte.

Returns:

DIRECTION_FORWARD for forward direction, DIRECTION_REVERSE for reverse direction.

void set_error(error_t err)

Sets a specific error.

This function sets a specific error to the error_state variable.

Parameters:

err – The error code to set.

void clear_error(error_t err)

Clears the specified error.

This function resets the error state for the given error code.

Parameters:

err – The error code to be cleared.

void clear_all_errors()

Clears all error flags or statuses in the system.

Clears error_state variable

error_t get_error_state()

Retrieves the current error state.

This function returns the current error state of the system.

Returns:

error_t The current error state.

Variables

const uint8_t *CV_ARRAY_FLASH

Pointer to the uint8_t array in flash which stores the CVs.

speed_step_t speed_step_target

Target speed step for the decoder.

This external variable represents the desired speed step that the decoder should aim to achieve.

speed_step_t speed_step_target_prev

Previous target speed step.

This variable holds the previous target speed step value for the decoder. It is used to keep track of the last speed step that was set.

bool cv_setup_check_done

Indicates whether the CV setup check has been completed.

This flag is used to track the status of the CV setup check. When the check is done, this variable should is set to true by core 0.

bool flash_safe_execute_core_init_done

Indicates whether the core initialization for safe flash execution is complete.

This flag is used to signal the completion of the flash safe execute core initialization. When the initialization is done this variable is set to true by core 1.

error_t error_state

Global variable to hold the current error state.

This variable is used to track the error state.

CV.h

Defines

CV_ARRAY_SIZE

Variables

uint8_t CV_ARRAY_DEFAULT[CV_ARRAY_SIZE]