Files
RP2040-Decoder/Software/shared.c
T
gab-k d970170795 - 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.
2024-12-29 17:06:50 +01:00

63 lines
1.8 KiB
C

//////////////////////////
// RP2040-Decoder //
// Gabriel Koppenstein //
// shared.c //
//////////////////////////
#include "shared.h"
// TODO: Implement enum typedef for speed steps, also getter and setter functions checking validity of speed step values.
uint8_t speed_step_target = 0;
uint8_t speed_step_target_prev = 0;
static error_t error_state = 0;
// Functions in shared.c are accessed by both cores
// retrieve 4Bytes from CV_ARRAY_FLASH at once and return 32-Bit value
uint32_t get_32bit_CV (uint16_t CV_start_index){
uint8_t byte_3 = CV_ARRAY_FLASH[CV_start_index];
uint8_t byte_2 = CV_ARRAY_FLASH[CV_start_index+1];
uint8_t byte_1 = CV_ARRAY_FLASH[CV_start_index+2];
uint8_t byte_0 = CV_ARRAY_FLASH[CV_start_index+3];
return (byte_0) + (byte_1<<8) + (byte_2<<16) + (byte_3<<24);
}
// retrieve 2Bytes from CV_ARRAY_FLASH at once and return 16-Bit value
uint16_t get_16bit_CV (uint16_t CV_start_index){
uint8_t byte_1 = CV_ARRAY_FLASH[CV_start_index];
uint8_t byte_0 = CV_ARRAY_FLASH[CV_start_index+1];
return (byte_0) + (byte_1<<8);
}
direction_t get_direction_of_speed_step(uint8_t speed_step){
// Shift by 7 Bytes to move bit7 into bit0 position and return
return speed_step >> 7;
}
void set_error(error_t err){
error_state |= err;
#ifdef RP2040_DECODER_DEFAULT_LED_PIN
gpio_put(RP2040_DECODER_DEFAULT_LED_PIN, true);
#endif
}
void clear_error(error_t err){
error_state &= (~err);
if (error_state == 0){
#ifdef RP2040_DECODER_DEFAULT_LED_PIN
gpio_put(RP2040_DECODER_DEFAULT_LED_PIN, false);
#endif
}
}
void clear_all_errors(){
error_state = 0;
#ifdef RP2040_DECODER_DEFAULT_LED_PIN
gpio_put(RP2040_DECODER_DEFAULT_LED_PIN, false);
#endif
}
error_t get_error_state(){
return error_state;
}