From 2afdfa829358d78d82cf552b606422ea135c7f45 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Fri, 20 Jun 2025 00:01:30 +0200 Subject: [PATCH 1/7] add more error led states and ignore CAN send FIFO full error --- Software/Core/Inc/main.h | 1 + Software/Core/Src/main.c | 67 +++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Software/Core/Inc/main.h b/Software/Core/Inc/main.h index f529088..f473388 100644 --- a/Software/Core/Inc/main.h +++ b/Software/Core/Inc/main.h @@ -47,6 +47,7 @@ extern "C" { /* Exported macro ------------------------------------------------------------*/ /* USER CODE BEGIN EM */ +void Error_Handler_Led(uint8_t err); /* USER CODE END EM */ /* Exported functions prototypes ---------------------------------------------*/ diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index a76b745..e882e71 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -132,9 +132,11 @@ void loop_1kHz() { } - if (HAL_FDCAN_AddMessageToTxFifoQ(hMainCAN, &txHeader, (uint8_t*) &txData) != HAL_OK) - Error_Handler(); - + if (HAL_FDCAN_AddMessageToTxFifoQ(hMainCAN, &txHeader, (uint8_t*) &txData) != HAL_OK) { + if(HAL_FDCAN_GetError(hMainCAN) != HAL_FDCAN_ERROR_FIFO_FULL) { + Error_Handler_Led(1); + } + } } if (mscounter >= 500) { @@ -195,10 +197,10 @@ int main(void) hPeriCAN = &hfdcan2; if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK) - Error_Handler(); + Error_Handler_Led(2); if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_values, NUM_ADC_PINS) != HAL_OK) - Error_Handler(); + Error_Handler_Led(2); HAL_TIM_Base_Start_IT(&htim6); @@ -215,11 +217,11 @@ int main(void) txHeader.DataLength = 8; if (HAL_FDCAN_ActivateNotification(hMainCAN, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) - Error_Handler(); + Error_Handler_Led(3); if (HAL_FDCAN_ConfigGlobalFilter(hMainCAN, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE) != HAL_OK) - Error_Handler(); + Error_Handler_Led(3); FDCAN_FilterTypeDef filter; filter.IdType = FDCAN_STANDARD_ID; @@ -230,10 +232,10 @@ int main(void) filter.FilterID2 = CAN_PWM_FILTER_MASK; if (HAL_FDCAN_ConfigFilter(hMainCAN, &filter) != HAL_OK) - Error_Handler(); + Error_Handler_Led(5); if (HAL_FDCAN_Start(hMainCAN) != HAL_OK) - Error_Handler(); + Error_Handler_Led(5); // Init all channels as stopped memset(pwm_ch_active, 0, 8); @@ -299,7 +301,7 @@ void SystemClock_Config(void) RCC_OscInitStruct.PLL.PLLFRACN = 0; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); + Error_Handler_Led(4); } /** Initializes the CPU, AHB and APB buses clocks @@ -317,7 +319,7 @@ void SystemClock_Config(void) if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { - Error_Handler(); + Error_Handler_Led(4); } } @@ -458,6 +460,49 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) HAL_ResumeTick(); } +void Error_Handler_Led(uint8_t err) { + HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(STATUS_G_GPIO_Port, STATUS_G_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, GPIO_PIN_RESET); + switch (err) + { + case 0: // red: general error + HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_SET); + break; + + case 1: // red blue: in 1 kHz loop + HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, GPIO_PIN_SET); + break; + + case 2: // blue: ADC calibration or DMA start + HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, GPIO_PIN_SET); + break; + + case 3: // green blue: CAN notify and config gloabl filter + HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(STATUS_G_GPIO_Port, STATUS_G_Pin, GPIO_PIN_SET); + break; + + case 4: //red green: clock + HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(STATUS_G_GPIO_Port, STATUS_G_Pin, GPIO_PIN_SET); + break; + + case 5: // red green blue: CAN config filter and start + HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(STATUS_G_GPIO_Port, STATUS_G_Pin, GPIO_PIN_SET); + break; + + default: + break; + } + + __disable_irq(); + while (1); +} + /* USER CODE END 4 */ /** From 75950a226d80588b2e52a62b78835fdd8fec7b86 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Fri, 20 Jun 2025 00:04:48 +0200 Subject: [PATCH 2/7] change wss to moving sum --- Software/Core/Src/main.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index e882e71..eac2132 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -40,6 +40,7 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define TIM_BASE_FREQ 96000000UL +#define WSS_HISTORY_SIZE 10 /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -66,6 +67,9 @@ TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3}; static uint8_t pwm_ch_active[8]; static uint16_t wss_flanks[2]; +static uint16_t wss_flanks_avg[2]; +static uint8_t wss_flanks_history[2][WSS_HISTORY_SIZE]; +static uint8_t wss_flanks_history_idx[2]; // index of the oldest entry (next to be overwritten) /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -119,7 +123,11 @@ void loop_1kHz() { break; case FIN: - value = wss_flanks[signal->channel]; + uint8_t oldest_hist_idx = wss_flanks_history_idx[signal->channel]; + // subtract oldest entry from avg + wss_flanks_avg[signal->channel] -= wss_flanks_history[signal->channel][oldest_hist_idx]; + // overwrite oldest history entry with new value and reset counter + wss_flanks_history[signal->channel][oldest_hist_idx] = wss_flanks[signal->channel]; wss_flanks[signal->channel] = 0; break; @@ -247,6 +255,10 @@ int main(void) mscounter = 0; setup_complete = 1; + memset(wss_flanks, 0, sizeof(wss_flanks)); + memset(wss_flanks_avg, 0, sizeof(wss_flanks)); + memset(wss_flanks_history, 0, sizeof(wss_flanks_history)); + memset(wss_flanks_history_idx, 0, sizeof(wss_flanks_history_idx)); /* USER CODE END 2 */ /* Infinite loop */ From dc8e0c806aa406c348cdba247df6046fbb0e0cbd Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Fri, 20 Jun 2025 00:14:40 +0200 Subject: [PATCH 3/7] fix digital inputs pin mapping --- Software/Core/Inc/mappings.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Software/Core/Inc/mappings.h b/Software/Core/Inc/mappings.h index 7a5a872..89a8fac 100644 --- a/Software/Core/Inc/mappings.h +++ b/Software/Core/Inc/mappings.h @@ -20,12 +20,12 @@ static struct { GPIO_TypeDef* port; uint16_t pin; } DIO_PIN_MAP[NUM_DIO_PINS] = { -/* 0 */ { .port = D1_IC_GPIO_Port, .pin = D1_IC_Pin}, -/* 1 */ { .port = D2_GPIO_Port, .pin = D2_Pin}, -/* 2 */ { .port = D3_GPIO_Port, .pin = D3_Pin}, -/* 3 */ { .port = D4_IC_GPIO_Port, .pin = D4_IC_Pin}, -/* 4 */ { .port = D5_GPIO_Port, .pin = D5_Pin}, -/* 5 */ { .port = D6_GPIO_Port, .pin = D6_Pin}, +/* 0 */ { .port = D5_GPIO_Port, .pin = D5_Pin}, +/* 1 */ { .port = D6_GPIO_Port, .pin = D6_Pin}, +/* 2 */ { .port = D4_IC_GPIO_Port, .pin = D4_IC_Pin}, +/* 3 */ { .port = D3_GPIO_Port, .pin = D3_Pin}, +/* 4 */ { .port = D2_GPIO_Port, .pin = D2_Pin}, +/* 5 */ { .port = D1_IC_GPIO_Port, .pin = D1_IC_Pin}, }; typedef enum { From 63e33332609592dc6689448baa96358b6e786382 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Fri, 20 Jun 2025 00:23:42 +0200 Subject: [PATCH 4/7] add debug CAN mapping --- Software/Core/Inc/mappings.h | 61 +++++++++++++++++++++++++++++++--- Software/debug_dbc_entries.txt | 29 ++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 Software/debug_dbc_entries.txt diff --git a/Software/Core/Inc/mappings.h b/Software/Core/Inc/mappings.h index 89a8fac..6ce96fe 100644 --- a/Software/Core/Inc/mappings.h +++ b/Software/Core/Inc/mappings.h @@ -4,11 +4,12 @@ // CHOOSE ONE, comment the other or comment both and use -D SN_FRONT //#define SN_FRONT #define SN_REAR +// #define DEBUG -#ifdef SN_FRONT -#ifdef SN_REAR -#error "Choose to either #define SN_FRONT or SN_REAR, not both!" -#endif +#if (defined(SN_FRONT) && defined(SN_REAR)) || \ + (defined(SN_FRONT) && defined(DEBUG)) || \ + (defined(SN_REAR) && defined(DEBUG)) +#error "Choose only one of #define SN_FRONT, #define SN_REAR, or #define DEBUG!" #endif #include @@ -189,6 +190,58 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = { #endif +#ifdef DEBUG + +static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = { + { + .can_id = 0x044, .dlc = 6, .num_signals = 9, .period = 100, .signals = { + { .type = DIN, .channel = L9, .start = 0, .length = 1, .factor = 1., .name = "" }, + { .type = DIN, .channel = LA, .start = 1, .length = 1, .factor = 1., .name = "" }, + { .type = DIN, .channel = LC, .start = 2, .length = 1, .factor = 1., .name = "" }, + { .type = DIN, .channel = R5, .start = 3, .length = 1, .factor = 1., .name = "" }, + { .type = DIN, .channel = R6, .start = 4, .length = 1, .factor = 1., .name = "" }, + { .type = DIN, .channel = R7, .start = 5, .length = 1, .factor = 1., .name = "" }, + + { .type = AIN, .channel = L1, .start = 6+12*0, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L2, .start = 6+12*1, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L3, .start = 6+12*2, .length = 12, .factor = 1., .name = "" }, + } + }, + { + .can_id = 0x045, .dlc = 8, .num_signals = 5, .period = 100, .signals = { + { .type = AIN, .channel = L4, .start = 12*0, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L5, .start = 12*1, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L6, .start = 12*2, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L7, .start = 12*3, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = L8, .start = 12*4, .length = 12, .factor = 1., .name = "" }, + } + }, + { + .can_id = 0x046, .dlc = 8, .num_signals = 5, .period = 100, .signals = { + { .type = AIN, .channel = R9, .start = 12*0, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = RA, .start = 12*1, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = RB, .start = 12*2, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = RC, .start = 12*3, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = RD, .start = 12*4, .length = 12, .factor = 1., .name = "" }, + } + }, + { + .can_id = 0x047, .dlc = 3, .num_signals = 2, .period = 100, .signals = { + { .type = AIN, .channel = RE, .start = 12*0, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = RF, .start = 12*1, .length = 12, .factor = 1., .name = "" }, + { .type = AIN, .channel = R0, .start = 12*2, .length = 12, .factor = 1., .name = "" }, + + } + }, +}; + +#define CAN_PWM_DC_ID 0x0DA // UNUSED +#define CAN_PWM_CONF_ID 0x0DB // UNUSED +#define CAN_PWM_BASE_ID CAN_PWM_DC_ID +#define CAN_PWM_FILTER_MASK 0x7FE // Match both + +#endif + /* user needs TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3}; */ diff --git a/Software/debug_dbc_entries.txt b/Software/debug_dbc_entries.txt new file mode 100644 index 0000000..b11a074 --- /dev/null +++ b/Software/debug_dbc_entries.txt @@ -0,0 +1,29 @@ +BO_ 68 Sensornode_Debug_1: 6 Sensornode_F + SG_ Sensornode_Debug_L9 : 0|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_LA : 1|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_LC : 2|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_R5 : 3|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_R6 : 4|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_R7 : 5|1@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L1 : 6|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L2 : 18|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L3 : 30|12@1+ (1,0) [0|1] "" Vector__XXX + +BO_ 69 Sensornode_Debug_2: 8 Sensornode_F + SG_ Sensornode_Debug_L4 : 0|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L5 : 12|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L6 : 24|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L7 : 36|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_L8 : 48|12@1+ (1,0) [0|1] "" Vector__XXX + +BO_ 70 Sensornode_Debug_3: 8 Sensornode_F + SG_ Sensornode_Debug_R9 : 0|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_RA : 12|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_RB : 24|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_RC : 36|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_RD : 48|12@1+ (1,0) [0|1] "" Vector__XXX + +BO_ 71 Sensornode_Debug_4: 3 Sensornode_F + SG_ Sensornode_Debug_RE : 0|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_RF : 12|12@1+ (1,0) [0|1] "" Vector__XXX + SG_ Sensornode_Debug_R0 : 24|12@1+ (1,0) [0|1] "" Vector__XXX \ No newline at end of file From 22e24b38b590a73de26ce28121f8728269135fb1 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Mon, 23 Jun 2025 17:23:41 +0200 Subject: [PATCH 5/7] fix debug CAN mapping --- Software/Core/Inc/mappings.h | 2 +- Software/debug_dbc_entries.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Software/Core/Inc/mappings.h b/Software/Core/Inc/mappings.h index 6ce96fe..d72f84d 100644 --- a/Software/Core/Inc/mappings.h +++ b/Software/Core/Inc/mappings.h @@ -226,7 +226,7 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = { } }, { - .can_id = 0x047, .dlc = 3, .num_signals = 2, .period = 100, .signals = { + .can_id = 0x047, .dlc = 5, .num_signals = 3, .period = 100, .signals = { { .type = AIN, .channel = RE, .start = 12*0, .length = 12, .factor = 1., .name = "" }, { .type = AIN, .channel = RF, .start = 12*1, .length = 12, .factor = 1., .name = "" }, { .type = AIN, .channel = R0, .start = 12*2, .length = 12, .factor = 1., .name = "" }, diff --git a/Software/debug_dbc_entries.txt b/Software/debug_dbc_entries.txt index b11a074..85995e1 100644 --- a/Software/debug_dbc_entries.txt +++ b/Software/debug_dbc_entries.txt @@ -23,7 +23,7 @@ BO_ 70 Sensornode_Debug_3: 8 Sensornode_F SG_ Sensornode_Debug_RC : 36|12@1+ (1,0) [0|1] "" Vector__XXX SG_ Sensornode_Debug_RD : 48|12@1+ (1,0) [0|1] "" Vector__XXX -BO_ 71 Sensornode_Debug_4: 3 Sensornode_F +BO_ 71 Sensornode_Debug_4: 5 Sensornode_F SG_ Sensornode_Debug_RE : 0|12@1+ (1,0) [0|1] "" Vector__XXX SG_ Sensornode_Debug_RF : 12|12@1+ (1,0) [0|1] "" Vector__XXX SG_ Sensornode_Debug_R0 : 24|12@1+ (1,0) [0|1] "" Vector__XXX \ No newline at end of file From 866d0642fbd3095e08f946b44f8b13decc510e81 Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Tue, 8 Jul 2025 17:59:53 +0200 Subject: [PATCH 6/7] temporarily implement light gate input latching --- Software/Core/Inc/mappings.h | 8 ++++++++ Software/Core/Src/main.c | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Software/Core/Inc/mappings.h b/Software/Core/Inc/mappings.h index d72f84d..10da724 100644 --- a/Software/Core/Inc/mappings.h +++ b/Software/Core/Inc/mappings.h @@ -136,6 +136,10 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = { } }; +static uint8_t DIO_LATCHING[NUM_DIO_PINS] = { + 1, 1, 0, 0, 0, 0, +}; + #define CAN_PWM_DC_ID 0x0DA // UNUSED #define CAN_PWM_CONF_ID 0x0DB // UNUSED #define CAN_PWM_BASE_ID CAN_PWM_DC_ID @@ -183,6 +187,10 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = { } }; +static uint8_t DIO_LATCHING[NUM_DIO_PINS] = { + 0, 0, 0, 0, 0, 0, +}; + #define CAN_PWM_DC_ID 0x0DC #define CAN_PWM_CONF_ID 0x0DD #define CAN_PWM_BASE_ID CAN_PWM_DC_ID diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index eac2132..35a5f1e 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -90,10 +90,16 @@ void loop_1kHz() { mscounter++; for (int di = 0; di < NUM_DIO_PINS; di++) { - dio_values[di] = HAL_GPIO_ReadPin( + uint8_t value = HAL_GPIO_ReadPin( DIO_PIN_MAP[di].port, DIO_PIN_MAP[di].pin ); + + if (DIO_LATCHING[di]) { + dio_values[di] |= value; + } else { + dio_values[di] = value; + } } for (int pi = 0; pi < NUM_TX_PKT; pi++) { @@ -116,6 +122,7 @@ void loop_1kHz() { switch (signal->type) { case DIN: value = dio_values[signal->channel]; + dio_values[signal->channel] = 0; // will be overwritten with real value at start of 1kHz loop break; case AIN: From c8b5a060d4f6458c9fcff7436a2fdfc4fcfcd59e Mon Sep 17 00:00:00 2001 From: Leonard Gies Date: Wed, 9 Jul 2025 00:08:46 +0200 Subject: [PATCH 7/7] add missing wss code --- Software/Core/Src/main.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index 35a5f1e..3cbfac3 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -136,6 +136,15 @@ void loop_1kHz() { // overwrite oldest history entry with new value and reset counter wss_flanks_history[signal->channel][oldest_hist_idx] = wss_flanks[signal->channel]; wss_flanks[signal->channel] = 0; + // add new counter to avg + wss_flanks_avg[signal->channel] += wss_flanks_history[signal->channel][oldest_hist_idx]; + // increase / wrap around index + wss_flanks_history_idx[signal->channel]++; + if(wss_flanks_history_idx[signal->channel] >= WSS_HISTORY_SIZE) { + wss_flanks_history_idx[signal->channel] = 0; + } + // value = wss_flanks_avg[signal->channel] * (1000 / pktinfo->period / WSS_HISTORY_SIZE); + value = wss_flanks_avg[signal->channel]; break; default: