Compare commits
5 Commits
7c50101f9e
...
interrupt-
| Author | SHA1 | Date | |
|---|---|---|---|
| c4849935c0 | |||
| bfc0eb2276 | |||
|
28fc2a966b
|
|||
|
6051e4b993
|
|||
|
3a8b90acc8
|
@ -5,6 +5,12 @@
|
||||
//#define SN_FRONT
|
||||
#define SN_REAR
|
||||
|
||||
#ifdef SN_FRONT
|
||||
#ifdef SN_REAR
|
||||
#error "Choose to either #define SN_FRONT or SN_REAR, not both!"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "main.h"
|
||||
|
||||
@ -95,10 +101,10 @@ typedef struct {
|
||||
static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
{
|
||||
.can_id = 0x0D1, .dlc = 3, .num_signals = 7, .period = 100, .signals = {
|
||||
{ .type = DIN, .channel = R5, .start = 0, .length = 1, .factor = 1., .name = "LS L" },
|
||||
{ .type = DIN, .channel = L9, .start = 1, .length = 1, .factor = 1., .name = "LS R" },
|
||||
{ .type = DIN, .channel = LA, .start = 2, .length = 1, .factor = 1., .name = "SDC M pre BOTS" },
|
||||
{ .type = DIN, .channel = LC, .start = 3, .length = 1, .factor = 1., .name = "SDC M post INS" },
|
||||
{ .type = DIN, .channel = L9, .start = 0, .length = 1, .factor = 1., .name = "LS L" },
|
||||
{ .type = DIN, .channel = LA, .start = 1, .length = 1, .factor = 1., .name = "LS R" },
|
||||
{ .type = DIN, .channel = R5, .start = 2, .length = 1, .factor = 1., .name = "SDC M pre BOTS" },
|
||||
{ .type = DIN, .channel = R6, .start = 3, .length = 1, .factor = 1., .name = "SDC M post INS" },
|
||||
{ .type = DIN, .channel = R7, .start = 4, .length = 1, .factor = 1., .name = "SDC M post BSPD" },
|
||||
|
||||
{ .type = AIN, .channel = R0, .start = 8, .length = 8, .factor = ((512./2)/ADQ), .name = "BDTS FL" },
|
||||
@ -109,7 +115,7 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
.can_id = 0x0D3, .dlc = 7, .num_signals = 6, .period = 10, .signals = {
|
||||
{ .type = AIN, .channel = L1, .start = 0, .length = 8, .factor = (256./ADQ), .name = "APPS 1" },
|
||||
{ .type = AIN, .channel = L2, .start = 8, .length = 8, .factor = (256./ADQ), .name = "APPS 2" },
|
||||
{ .type = AIN, .channel = RA, .start = 16, .length = 8, .factor = ((512./2)/ADQ), .name = "BP F" },
|
||||
{ .type = AIN, .channel = L5, .start = 16, .length = 8, .factor = ((512./2)/ADQ), .name = "BP F" },
|
||||
{ .type = AIN, .channel = R9, .start = 24, .length = 12, .factor = 1., .name = "SAS" },
|
||||
{ .type = FIN, .channel = R8, .start = 40, .length = 8, .factor = 1., .name = "WSS FL" },
|
||||
{ .type = FIN, .channel = LB, .start = 48, .length = 8, .factor = 1., .name = "WSS FR" },
|
||||
@ -117,7 +123,7 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
},
|
||||
{
|
||||
.can_id = 0x0D5, .dlc = 3, .num_signals = 2, .period = 10, .signals = {
|
||||
{ .type = AIN, .channel = RC, .start = 0, .length = 12, .factor = 1., .name = "DS FL" },
|
||||
{ .type = AIN, .channel = L3, .start = 0, .length = 12, .factor = 1., .name = "DS FL" },
|
||||
{ .type = AIN, .channel = L4, .start = 12, .length = 12, .factor = 1., .name = "DS FR" },
|
||||
}
|
||||
},
|
||||
@ -129,6 +135,11 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
}
|
||||
};
|
||||
|
||||
#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
|
||||
|
||||
#ifdef SN_REAR
|
||||
@ -143,20 +154,20 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
{ .type = AIN, .channel = R0, .start = 8, .length = 8, .factor = ((512./2)/ADQ), .name = "BDTS RL" },
|
||||
{ .type = AIN, .channel = L8, .start = 16, .length = 8, .factor = ((512./2)/ADQ), .name = "BDTS RR" },
|
||||
|
||||
{ .type = AIN, .channel = R9, .start = 24, .length = 8, .factor = (256./ADQ), .name = "WT BAT" },
|
||||
{ .type = AIN, .channel = RB, .start = 32, .length = 8, .factor = (256./ADQ), .name = "WT DT" },
|
||||
{ .type = AIN, .channel = RD, .start = 40, .length = 8, .factor = (256./ADQ), .name = "WP BAT" },
|
||||
{ .type = AIN, .channel = RF, .start = 48, .length = 8, .factor = (256./ADQ), .name = "WP DT" },
|
||||
{ .type = AIN, .channel = L3, .start = 24, .length = 8, .factor = (256./ADQ), .name = "WT BAT" },
|
||||
{ .type = AIN, .channel = R9, .start = 32, .length = 8, .factor = (256./ADQ), .name = "WT DT" },
|
||||
{ .type = AIN, .channel = L5, .start = 40, .length = 8, .factor = (256./ADQ), .name = "WP BAT" },
|
||||
{ .type = AIN, .channel = RB, .start = 48, .length = 8, .factor = (256./ADQ), .name = "WP DT" },
|
||||
}
|
||||
},
|
||||
{
|
||||
.can_id = 0x0D4, .dlc = 8, .num_signals = 7, .period = 10, .signals = {
|
||||
{ .type = AIN, .channel = L1, .start = 0, .length = 8, .factor = ((25.6/.1)/ADQ), .name = "EBS APS 1" },
|
||||
{ .type = AIN, .channel = L2, .start = 8, .length = 8, .factor = ((25.6/.1)/ADQ), .name = "EBS APS 2" },
|
||||
{ .type = AIN, .channel = RA, .start = 16, .length = 8, .factor = ((512./2)/ADQ), .name = "BP F" },
|
||||
{ .type = AIN, .channel = RA, .start = 16, .length = 8, .factor = ((512./2)/ADQ), .name = "BP R" },
|
||||
{ .type = FIN, .channel = R8, .start = 24, .length = 8, .factor = 1., .name = "WSS RL" },
|
||||
{ .type = FIN, .channel = LB, .start = 32, .length = 8, .factor = 1., .name = "WSS RR" },
|
||||
{ .type = AIN, .channel = RC, .start = 40, .length = 12, .factor = 1., .name = "DS RL" },
|
||||
{ .type = AIN, .channel = L7, .start = 40, .length = 12, .factor = 1., .name = "DS RL" },
|
||||
{ .type = AIN, .channel = L4, .start = 52, .length = 12, .factor = 1., .name = "DS RR" },
|
||||
}
|
||||
},
|
||||
@ -173,6 +184,8 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
|
||||
|
||||
#define CAN_PWM_DC_ID 0x0DC
|
||||
#define CAN_PWM_CONF_ID 0x0DD
|
||||
#define CAN_PWM_BASE_ID CAN_PWM_DC_ID
|
||||
#define CAN_PWM_FILTER_MASK 0x7FE // Match both 0x0DC and 0x0DD
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "mappings.h"
|
||||
/* USER CODE END Includes */
|
||||
@ -50,15 +51,20 @@
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
unsigned int mscounter;
|
||||
unsigned int setup_complete;
|
||||
unsigned int tx_counter;
|
||||
bool adc_conv_cplt;
|
||||
|
||||
FDCAN_HandleTypeDef *hMainCAN, *hPeriCAN;
|
||||
FDCAN_TxHeaderTypeDef txHeader;
|
||||
|
||||
/* Declare buffer in D1 domain SRAM */
|
||||
// Declare buffer in AXI SRAM
|
||||
static uint16_t adc_values[NUM_ADC_PINS];
|
||||
static uint8_t dio_values[NUM_DIO_PINS];
|
||||
#ifdef SN_FRONT
|
||||
static uint8_t ls_values[2];
|
||||
#endif /* SN_FRONT */
|
||||
|
||||
static uint16_t filtered_values[NUM_ADC_PINS];
|
||||
|
||||
// See mappings.h pwm_tim_t
|
||||
TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3};
|
||||
@ -78,19 +84,27 @@ static void MX_NVIC_Init(void);
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
void loop_1kHz() {
|
||||
void send_latest_can() {
|
||||
|
||||
if (!setup_complete)
|
||||
return;
|
||||
|
||||
mscounter++;
|
||||
tx_counter++;
|
||||
|
||||
#if defined(SN_FRONT)
|
||||
memcpy(dio_values, ls_values, 2);
|
||||
memset(ls_values, 0, 2);
|
||||
for (int di = 2; di < NUM_DIO_PINS; di++) { //TODO: find way to make this dependent on mappings
|
||||
dio_values[di] = HAL_GPIO_ReadPin(
|
||||
DIO_PIN_MAP[di].port,
|
||||
DIO_PIN_MAP[di].pin
|
||||
);
|
||||
}
|
||||
#elif defined(SN_REAR)
|
||||
for (int di = 0; di < NUM_DIO_PINS; di++) {
|
||||
dio_values[di] = HAL_GPIO_ReadPin(
|
||||
DIO_PIN_MAP[di].port,
|
||||
DIO_PIN_MAP[di].pin
|
||||
);
|
||||
}
|
||||
#endif /* SN_FRONT */
|
||||
|
||||
for (int pi = 0; pi < NUM_TX_PKT; pi++) {
|
||||
|
||||
@ -99,7 +113,7 @@ void loop_1kHz() {
|
||||
if (pktinfo->num_signals < 0)
|
||||
continue;
|
||||
|
||||
if (mscounter % pktinfo->period == 0) {
|
||||
if (tx_counter % pktinfo->period == 0) {
|
||||
|
||||
txHeader.Identifier = pktinfo->can_id;
|
||||
txHeader.DataLength = pktinfo->dlc;
|
||||
@ -115,7 +129,7 @@ void loop_1kHz() {
|
||||
break;
|
||||
|
||||
case AIN:
|
||||
value = signal->factor * adc_values[signal->channel];
|
||||
value = signal->factor * filtered_values[signal->channel];
|
||||
break;
|
||||
|
||||
case FIN:
|
||||
@ -137,15 +151,37 @@ void loop_1kHz() {
|
||||
|
||||
}
|
||||
|
||||
if (mscounter >= 500) {
|
||||
mscounter = 0;
|
||||
if (tx_counter >= 256) {
|
||||
tx_counter = 0;
|
||||
HAL_GPIO_TogglePin(STATUS_G_GPIO_Port, STATUS_G_Pin);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void filter_adc() {
|
||||
for (size_t i = 0; i < NUM_TX_PKT; i++){
|
||||
float alpha = 1 / CAN_SIGNAL_MAP[i].period; // TODO: check if it smoothes too much
|
||||
|
||||
for (size_t j = 0; i < CAN_SIGNAL_MAP[i].num_signals; j++) {
|
||||
if(CAN_SIGNAL_MAP[i].signals[j].type != AIN)
|
||||
continue;
|
||||
|
||||
uint8_t ch_index = CAN_SIGNAL_MAP[i].signals[j].channel;
|
||||
filtered_values[ch_index] = (alpha * adc_values[ch_index]) + ((1 - alpha) * filtered_values[ch_index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SN_FRONT
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||
if (GPIO_Pin == DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[0].channel].pin) {
|
||||
ls_values[1] = 1;
|
||||
} else if (GPIO_Pin == DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[1].channel].pin) {
|
||||
ls_values[0] = 1;
|
||||
}
|
||||
}
|
||||
#endif /* SN_FRONT */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@ -190,9 +226,39 @@ int main(void)
|
||||
/* Initialize interrupts */
|
||||
MX_NVIC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
// Enable Interrupts for Light Switch Pins
|
||||
#ifdef SN_FRONT
|
||||
// check if pin name is "LS L"
|
||||
if (strcmp(CAN_SIGNAL_MAP[0].signals[0].name, "LS L") != 0
|
||||
|| strcmp(CAN_SIGNAL_MAP[0].signals[1].name, "LS R") != 0
|
||||
) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = CAN_SIGNAL_MAP[0].signals[0].channel;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[0].channel].port, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = CAN_SIGNAL_MAP[0].signals[1].channel;
|
||||
HAL_GPIO_Init(DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[1].channel].port, &GPIO_InitStruct);
|
||||
|
||||
// Did not find a way to make this dependent on mappings,
|
||||
// since the EXTI lines are shared for pins 10-15
|
||||
// so i just error if the pins are not 10, 15
|
||||
if (DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[0].channel].pin != GPIO_PIN_10
|
||||
|| DIO_PIN_MAP[CAN_SIGNAL_MAP[0].signals[1].channel].pin != GPIO_PIN_15
|
||||
) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
|
||||
#endif /* SN_FRONT */
|
||||
|
||||
hMainCAN = &hfdcan2;
|
||||
hPeriCAN = &hfdcan1;
|
||||
hMainCAN = &hfdcan1;
|
||||
hPeriCAN = &hfdcan2;
|
||||
|
||||
if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
|
||||
Error_Handler();
|
||||
@ -226,8 +292,8 @@ int main(void)
|
||||
filter.FilterIndex = 0;
|
||||
filter.FilterType = FDCAN_FILTER_MASK;
|
||||
filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
|
||||
filter.FilterID1 = 0x0DD;
|
||||
filter.FilterID2 = 0x7FE; // Match 0x0DC and 0x0DD
|
||||
filter.FilterID1 = CAN_PWM_BASE_ID;
|
||||
filter.FilterID2 = CAN_PWM_FILTER_MASK;
|
||||
|
||||
if (HAL_FDCAN_ConfigFilter(hMainCAN, &filter) != HAL_OK)
|
||||
Error_Handler();
|
||||
@ -242,8 +308,7 @@ int main(void)
|
||||
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1);
|
||||
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_2);
|
||||
|
||||
mscounter = 0;
|
||||
setup_complete = 1;
|
||||
tx_counter = 0;
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@ -251,11 +316,18 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while(1)
|
||||
{
|
||||
if(adc_conv_cplt) {
|
||||
adc_conv_cplt = false;
|
||||
|
||||
filter_adc();
|
||||
|
||||
send_latest_can();
|
||||
}
|
||||
|
||||
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
//HAL_SuspendTick();
|
||||
//HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
@ -333,13 +405,13 @@ static void MX_NVIC_Init(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
/*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
if (hadc->Instance == ADC1)
|
||||
{
|
||||
__asm volatile ("NOP");
|
||||
adc_conv_cplt = true;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||
|
||||
@ -348,7 +420,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||
if (htim != &htim6)
|
||||
return;
|
||||
|
||||
loop_1kHz();
|
||||
// TODO: Make timer 10kHz and dont trigger a function with it, for exiting WFI in main
|
||||
|
||||
};
|
||||
|
||||
@ -397,9 +469,6 @@ void SetCCR(TIM_TypeDef* Instance, unsigned int ch, uint8_t dc) {
|
||||
|
||||
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, uint32_t RxFifo0ITs)
|
||||
{
|
||||
FDCAN_HandleTypeDef* hMainCAN = &hfdcan2;
|
||||
//FDCAN_HandleTypeDef* hPeriCAN = &hfdcan1;
|
||||
|
||||
if (handle != hMainCAN || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET)
|
||||
return; // TODO: handle Peripheral CAN
|
||||
|
||||
|
||||
3
pre-commit.sh
Normal file
3
pre-commit.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
[ ! find . -type f -iname "*.lck" ]
|
||||
Reference in New Issue
Block a user