- Updated CMakeLists.txt and board config .h files
- Refactored core0.c to most functions are now static functions. - Introduced new structures (`dcc_packet_t` and `dcc_ring_buffer_t`) for DCC packet data handling and packet detection. - DCC pin interrupt now only does packet detection and no evaluation, evaluation is now done inside the main loop. - Refactor message evaluation logic. - Implemented hard fault handler, now rebooting when hardfault is encountered. - Optimized initialization sequence in main(). - Controller and speed helper functions now run outside the timer interrupts, instead flags are set, which are checked inside the core1 main loop. - Added enumeration for speed steps to improve readability. (However they are yet to be used) - Added various documentation comments. - Added various LOG() messages. - Added python script for generating speed_step typedef definition.
This commit is contained in:
1 parent
acdbf3cffe
commit
d970170795
10 files changed
+847
-300
No files matched your search
+167
-58
@@ -9,7 +9,6 @@
|
||||
#include "shared.h"
|
||||
#include "CV.h"
|
||||
|
||||
#define SIZE_BYTE_ARRAY 5
|
||||
#define MESSAGE_3_BYTES 0b11111111110000000000000000000000000001
|
||||
#define MESSAGE_MASK_3_BYTES 0b11111111111000000001000000001000000001
|
||||
#define MESSAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
|
||||
@@ -17,9 +16,16 @@
|
||||
#define MESSAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
|
||||
#define MESSAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
||||
|
||||
#define INVALID_PACKAGE SIZE_MAX
|
||||
|
||||
#define RING_BUFFER_PACKETS 8
|
||||
#define RING_BUFFER_BYTES 5
|
||||
|
||||
#define FLASH_CMD_READ_JEDEC_ID 0x9F
|
||||
#define FLASH_TIMEOUT_IN_MS 500
|
||||
|
||||
#define ADC_CALIBRATION_ITERATIONS 8192
|
||||
#define WATCHDOG_TIMER_IN_MS 2000
|
||||
#define WATCHDOG_TIMER_IN_MS 5000
|
||||
|
||||
/*!
|
||||
* \brief Bitmasks used for clearing bits corresponding to function blocks
|
||||
@@ -41,29 +47,126 @@ const uint32_t clr_bit_arr[6] = {
|
||||
0b00011111111111111111111111111111, // Clear Bits 29-31 (F29-F31)
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Function returns average of values deviating less than twice the standard deviation from original average.
|
||||
*
|
||||
* \param arr input array
|
||||
* \param length amount of elements in input array
|
||||
* \return average value of values deviating less than 2*standard_dev.
|
||||
|
||||
/**
|
||||
* @brief Structure representing a DCC packet.
|
||||
*
|
||||
* This structure is used to store the data and length of a DCC packet.
|
||||
*
|
||||
* @typedef dcc_packet_t
|
||||
*
|
||||
* @field data An array of bytes representing the DCC packet data.
|
||||
* @field length The length of the DCC packet data.
|
||||
*/
|
||||
uint16_t two_std_dev(const uint16_t arr[], uint32_t length);
|
||||
typedef struct dcc_packet_t {
|
||||
uint8_t data[RING_BUFFER_BYTES];
|
||||
size_t length;
|
||||
}dcc_packet_t;
|
||||
|
||||
/**
|
||||
* @brief 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.
|
||||
*
|
||||
* @typedef dcc_ring_buffer_t
|
||||
*
|
||||
* @field packets Array of DCC packets stored in the ring buffer.
|
||||
* @field wr_idx Index for the next write operation in the ring buffer.
|
||||
* @field rd_idx Index for the next read operation in the ring buffer.
|
||||
*/
|
||||
typedef struct dcc_ring_buffer_t {
|
||||
dcc_packet_t packets[RING_BUFFER_PACKETS];
|
||||
size_t wr_idx;
|
||||
size_t rd_idx;
|
||||
} dcc_ring_buffer_t;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 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.
|
||||
*
|
||||
* @param idx Pointer to the index to be incremented.
|
||||
* @param ring_buffer_length The length of the ring buffer.
|
||||
*/
|
||||
static void increment_ring_buffer_idx(size_t * idx, uint8_t ring_buffer_length);
|
||||
|
||||
/*!
|
||||
* \brief Function for erasing a sector of flash memory.
|
||||
*
|
||||
* This function erases a specified range of flash memory sectors.
|
||||
*
|
||||
* \param 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_erase(void *param);
|
||||
|
||||
/*!
|
||||
* \brief Function for programming a range of flash memory
|
||||
*
|
||||
* This function programs a specified range of flash memory with the provided data.
|
||||
*
|
||||
* \param 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_range_program(void *param);
|
||||
|
||||
/*!
|
||||
* \brief Function for executing a flash command via QSPI
|
||||
*
|
||||
* \param 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 void call_flash_do_cmd(void *param);
|
||||
|
||||
/*!
|
||||
* \brief Function for calculating the absolute value of an integer.
|
||||
*
|
||||
* This function takes an integer as input and returns its absolute value.
|
||||
*
|
||||
* \param x The input integer whose absolute value is to be calculated.
|
||||
* \return The absolute value of the input integer.
|
||||
*/
|
||||
static unsigned int absolute_val(int x);
|
||||
|
||||
/*!
|
||||
* \brief 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.
|
||||
*
|
||||
* \param arr The input array of values.
|
||||
* \param length The number of elements in the input array.
|
||||
* \return The average value of the elements that deviate less than twice the standard deviation from the original average.
|
||||
*/
|
||||
static uint16_t two_std_dev(const uint16_t arr[], uint32_t length);
|
||||
|
||||
/*!
|
||||
* \brief Measures ADC offset and saves the offset value to flash memory.
|
||||
* \param n Amount of measurements to be done
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* \param n The number of measurements to be performed to calculate the offset.
|
||||
*/
|
||||
void adc_offset_adjustment(uint32_t n);
|
||||
static void adc_offset_adjustment(uint32_t n);
|
||||
|
||||
/*!
|
||||
* \brief 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.
|
||||
* Refers to NMRA S-9.2.3 Section D and RCN-216 Section 3
|
||||
*
|
||||
* \note Refers to NMRA S-9.2.3 Section D and RCN-216 Section 3
|
||||
*/
|
||||
void acknowledge();
|
||||
static void acknowledge();
|
||||
|
||||
/*!
|
||||
* \brief Function for verifying a single bit of a CV
|
||||
@@ -71,39 +174,48 @@ void acknowledge();
|
||||
* \param bit_val Value of bit to be verified
|
||||
* \param bit_pos Position of bit to be verified
|
||||
*/
|
||||
void verify_cv_bit(uint16_t cv_address, bool bit_val, uint8_t bit_pos);
|
||||
static void verify_cv_bit(uint16_t cv_address, bool bit_val, uint8_t bit_pos);
|
||||
|
||||
/*!
|
||||
* \brief Function for verifying a CV byte
|
||||
* \param cv_address CV Address
|
||||
* \param cv_data Byte to be verified
|
||||
*
|
||||
* This function verifies the byte of a Configuration Variable (CV) at the specified address.
|
||||
*
|
||||
* \param cv_address The address of the CV to be verified.
|
||||
* \param cv_data The byte value to be verified against the CV.
|
||||
*/
|
||||
void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
static void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
|
||||
/*!
|
||||
* \brief Function for writing a CV byte
|
||||
* \param cv_address
|
||||
* \param cv_data
|
||||
* \brief Writes a byte to a Configuration Variable (CV).
|
||||
*
|
||||
* This function writes a byte of data to a specified CV address.
|
||||
*
|
||||
* \param cv_address The address of the CV to write to.
|
||||
* \param cv_data The data byte to write to the CV.
|
||||
*/
|
||||
void write_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
static void write_cv_byte(uint16_t cv_address, uint8_t cv_data);
|
||||
|
||||
/*!
|
||||
* \brief This function sets all the CV values stored in flash back to their default settings (CV_ARRAY_DEFAULT).
|
||||
*/
|
||||
void reset_cv_array_to_default();
|
||||
static void reset_cv_array_to_default();
|
||||
|
||||
/*!
|
||||
* \brief CV Programming mode function
|
||||
* Used on entering programming mode in order to write CV values to persistent flash memory or verify/read bits/bytes from flash. <br>
|
||||
* Refers to NMRA Standard 9.2.3 ("Service Mode Programming") or RCN-216<br>
|
||||
*
|
||||
* 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 valid programming command
|
||||
* 2. Evaluates the type of programming instruction Write byte / Verify byte / Verify bit and runs corresponding function
|
||||
* 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.
|
||||
*
|
||||
* \param number_of_bytes Number of bytes in the array.
|
||||
* \param byte_array Pointer to the byte array.
|
||||
* @param number_of_bytes The number of bytes in the array.
|
||||
* @param byte_array A pointer to the byte array.
|
||||
*
|
||||
* @note Refer to NMRA Standard 9.2.3 ("Service Mode Programming") or RCN-216 for more details.
|
||||
*/
|
||||
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
static void program_mode(size_t number_of_bytes, const uint8_t *const byte_array);
|
||||
|
||||
/*!
|
||||
* \brief Set outputs according to function mapping in CV257 to CV512
|
||||
@@ -113,16 +225,19 @@ void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
*
|
||||
* \param functions_to_set_bitmask Function bitmask F0 is bit0, F1 is bit1, ...
|
||||
*/
|
||||
void set_outputs(uint32_t functions_to_set_bitmask);
|
||||
static void set_outputs(uint32_t functions_to_set_bitmask);
|
||||
|
||||
/**
|
||||
* @brief Update functions F0 - F31 when function command is received or in case of direction change.
|
||||
*
|
||||
* @param new_function_bitmask New function bitmask.
|
||||
* @param clr_bit_ind Clear bit index.
|
||||
* @param direction_change Direction change flag.
|
||||
* This function updates the active functions based on the provided bitmask. It can also clear a specific bit
|
||||
* and handle changes in direction.
|
||||
*
|
||||
* @param new_function_bitmask New function bitmask representing the state of functions F0 - F31.
|
||||
* @param clr_bit_ind Index of the bit to be cleared in the bitmask.
|
||||
* @param direction_change Flag indicating if there is a change in direction.
|
||||
*/
|
||||
void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change);
|
||||
static void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change);
|
||||
|
||||
/**
|
||||
* @brief Detect errors in the byte array using exor of all bytes.
|
||||
@@ -133,7 +248,7 @@ void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind,
|
||||
* @param byte_array Pointer to the byte array.
|
||||
* @return true if no error is detected, false otherwise.
|
||||
*/
|
||||
bool error_detection(int8_t number_of_bytes, const uint8_t *byte_array);
|
||||
static bool error_detection(size_t number_of_bytes, const uint8_t *byte_array);
|
||||
|
||||
/**
|
||||
* @brief Check if the address is a long address.
|
||||
@@ -142,7 +257,7 @@ bool error_detection(int8_t number_of_bytes, const uint8_t *byte_array);
|
||||
* @param byte_array Pointer to the byte array.
|
||||
* @return true if it is a long address, false otherwise.
|
||||
*/
|
||||
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
static bool is_long_address(size_t number_of_bytes, const uint8_t byte_array[]);
|
||||
|
||||
/**
|
||||
* @brief Evaluate the address of the message.
|
||||
@@ -151,7 +266,7 @@ bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
* @param byte_array Pointer to the byte array.
|
||||
* @return true if the address is correct, false otherwise.
|
||||
*/
|
||||
bool address_evaluation(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
static bool address_evaluation(size_t number_of_bytes, const uint8_t byte_array[]);
|
||||
|
||||
/**
|
||||
* @brief Evaluate the instruction of the message.
|
||||
@@ -159,14 +274,14 @@ bool address_evaluation(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
* @param number_of_bytes Number of bytes in the array.
|
||||
* @param byte_array Pointer to the byte array.
|
||||
*/
|
||||
void instruction_evaluation(uint8_t number_of_bytes, const uint8_t byte_array[]);
|
||||
static void instruction_evaluation(size_t number_of_bytes, const uint8_t byte_array[]);
|
||||
|
||||
/**
|
||||
* @brief Check for reset message - When reset message is found, stop the motor and disable all functions.
|
||||
*
|
||||
* @return Number of bytes if valid reset message bit-pattern is found. Otherwise -1 is returned.
|
||||
*/
|
||||
bool reset_message_check(uint8_t number_of_bytes, const uint8_t *const byte_array);
|
||||
static bool reset_message_check(size_t number_of_bytes, const uint8_t *const byte_array);
|
||||
|
||||
/**
|
||||
* @brief Verify DCC message
|
||||
@@ -177,7 +292,7 @@ bool reset_message_check(uint8_t number_of_bytes, const uint8_t *const byte_arra
|
||||
*
|
||||
* \return Number of bytes if valid bit-pattern is found. Otherwise return -1.
|
||||
*/
|
||||
int8_t verify_dcc_message();
|
||||
static size_t detect_dcc_packet();
|
||||
|
||||
/**
|
||||
* @brief Convert sequence of bits to byte array
|
||||
@@ -186,10 +301,9 @@ int8_t verify_dcc_message();
|
||||
* Bytes are ordered as follows:
|
||||
* Start of transmission -> byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission
|
||||
*
|
||||
* \param number_of_bytes Number of bytes in the array.
|
||||
* \param byte_array Pointer to the byte array.
|
||||
* \param packet Pointer to dcc packet struct which contains data and length of packet.
|
||||
*/
|
||||
void bits_to_byte_array(int8_t number_of_bytes, uint8_t byte_array[]);
|
||||
static void bits_to_dcc_packet_data(dcc_packet_t * packet);
|
||||
|
||||
/**
|
||||
* @brief Main message evaluation function
|
||||
@@ -202,32 +316,27 @@ void bits_to_byte_array(int8_t number_of_bytes, uint8_t byte_array[]);
|
||||
* 5. Check for reset message and set flag when reset message is received
|
||||
*
|
||||
*/
|
||||
void evaluate_message();
|
||||
static void evaluate_packet();
|
||||
|
||||
/*!
|
||||
* \brief 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.
|
||||
* Parameters are unused.
|
||||
*
|
||||
* \param gpio Unused parameter
|
||||
* \param events Unused parameter
|
||||
*/
|
||||
void track_signal_rise(unsigned int gpio, long unsigned int events);
|
||||
static void track_signal_rise();
|
||||
|
||||
/*!
|
||||
* \brief 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.
|
||||
* 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 buffer, then evaluates complete buffer using evaluate_message().
|
||||
* Parameters are unused.
|
||||
* 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.
|
||||
*
|
||||
* \param gpio Unused parameter
|
||||
* \param events Unused parameter
|
||||
*/
|
||||
void track_signal_fall(unsigned int gpio, long unsigned int events);
|
||||
static void track_signal_fall();
|
||||
|
||||
/*!
|
||||
* \brief Output initialization function
|
||||
@@ -236,7 +345,7 @@ void track_signal_fall(unsigned int gpio, long unsigned int events);
|
||||
* Sets PWM functionality for GPIOs configured as PWM pins CV_112 - CV_115 (see CV.h for reference) <br>
|
||||
* PWM frequency and clock divider are configured according to CV_116 - CV_171 (see CV.h for reference) <br>
|
||||
*/
|
||||
void init_outputs();
|
||||
static void init_outputs();
|
||||
|
||||
/*!
|
||||
* \brief Checks various CV values in order to detect factory condition or pending calibrations
|
||||
@@ -246,17 +355,17 @@ void init_outputs();
|
||||
* 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
|
||||
*/
|
||||
void cv_setup_check();
|
||||
static void cv_setup_check();
|
||||
|
||||
/*!
|
||||
* \brief Function initializes motor PWM for pin specified in CMakeLists.txt
|
||||
* \param gpio GPIO pin
|
||||
*/
|
||||
void init_motor_pwm(uint8_t gpio);
|
||||
static void init_motor_pwm(uint8_t gpio);
|
||||
|
||||
/*!
|
||||
* \brief Initializes ADC
|
||||
*
|
||||
* Function initializes ADC, corresponding GPIO pins specified in CMakeLists.txt, also configures ADC FIFO.
|
||||
*/
|
||||
void init_adc();
|
||||
static void init_adc();
|
||||
Reference in new issue
Block a user