feat: init commit
This commit is contained in:
68
Software/Core/Inc/can.h
Normal file
68
Software/Core/Inc/can.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* can.h
|
||||
*
|
||||
* Created on: Jun 12, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#ifndef INC_CAN_H_
|
||||
#define INC_CAN_H_
|
||||
|
||||
#include "can_halal.h"
|
||||
|
||||
#define CAN_ID_TX 0x420
|
||||
#define CAN_ID_RX_R2D 0x410
|
||||
#define CAN_ID_RX_AMS 0x00A
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t ts_inactive : 1;
|
||||
uint8_t ts_active : 1;
|
||||
uint8_t ts_precharge : 1;
|
||||
uint8_t ts_discharge : 1;
|
||||
uint8_t ts_error : 1;
|
||||
uint8_t NC1 : 1;
|
||||
uint8_t NC2 : 1;
|
||||
uint8_t sdc_closed : 1;
|
||||
};
|
||||
uint8_t ams_rx;
|
||||
} ams_info;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t ts_green : 1;
|
||||
uint8_t imd_error : 1;
|
||||
uint8_t ams_error : 1;
|
||||
uint8_t NC1 : 1;
|
||||
uint8_t NC2 : 1;
|
||||
uint8_t NC3 : 1;
|
||||
uint8_t NC4 : 1;
|
||||
uint8_t NC5 : 1;
|
||||
};
|
||||
uint8_t ams_led;
|
||||
}led_info;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
ams_info ams_status;
|
||||
led_info led_status;
|
||||
};
|
||||
uint16_t ams_bitmask;
|
||||
} rx_acc;
|
||||
|
||||
typedef union{
|
||||
struct{
|
||||
uint8_t r2d : 1;
|
||||
uint8_t tson : 1;
|
||||
uint8_t racemode : 1;
|
||||
uint8_t sdc_in : 1;
|
||||
uint8_t sdc_out : 1;
|
||||
};
|
||||
uint8_t dash_send;
|
||||
}dash_tx_t;
|
||||
|
||||
void can_init(CAN_HandleTypeDef* hcan);
|
||||
void can_send();
|
||||
void can_rxupdateFrame();
|
||||
|
||||
#endif /* INC_CAN_H_ */
|
||||
72
Software/Core/Inc/can_halal.h
Normal file
72
Software/Core/Inc/can_halal.h
Normal file
@ -0,0 +1,72 @@
|
||||
#ifndef CAN_HALAL_H
|
||||
#define CAN_HALAL_H
|
||||
|
||||
// Define family macros if none are defined and we recognize a chip macro
|
||||
#if !defined(STM32F3) && !defined(STM32H7) && !defined(STM32F0)
|
||||
#if defined(STM32F302x6) || defined(STM32F302x8) || defined(STM32F302xB) || \
|
||||
defined(STM32F302xC)
|
||||
#define STM32F3
|
||||
#endif
|
||||
#if defined(STM32H7A3xx) || defined(STM32H723xx)
|
||||
#define STM32H7
|
||||
#endif
|
||||
#if defined(STM32F042x6)
|
||||
#define STM32F0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(STM32F3)
|
||||
#include "stm32f3xx_hal.h"
|
||||
#define FTCAN_IS_BXCAN
|
||||
#define FTCAN_NUM_FILTERS 13
|
||||
#elif defined(STM32H7)
|
||||
#include "stm32h7xx_hal.h"
|
||||
#define FTCAN_IS_FDCAN
|
||||
#ifndef FTCAN_NUM_FILTERS
|
||||
#error "Please configure the number of filters in CubeMX, and then add a compiler define for FTCAN_NUM_FILTERS"
|
||||
#endif
|
||||
#elif defined(STM32F0)
|
||||
#include "stm32f0xx_hal.h"
|
||||
#define FTCAN_IS_BXCAN
|
||||
#define FTCAN_NUM_FILTERS 13
|
||||
#else
|
||||
#error "Couldn't detect STM family"
|
||||
#endif
|
||||
|
||||
#if defined(FTCAN_IS_BXCAN)
|
||||
HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle);
|
||||
#elif defined(FTCAN_IS_FDCAN)
|
||||
HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle);
|
||||
#else
|
||||
#error "Unknown CAN peripheral"
|
||||
#endif
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen);
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask);
|
||||
|
||||
/**
|
||||
* Define this function to be notified of incoming CAN messages
|
||||
*/
|
||||
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data);
|
||||
|
||||
/**
|
||||
* Read num_bytes bytes from a message (unmarshalled network byte order). The
|
||||
* msg pointer is advanced by the corresponding number of bytes.
|
||||
*
|
||||
* Both methods return a 64-bit integer, but you can safely cast it to a smaller
|
||||
* integer type.
|
||||
*/
|
||||
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data, size_t num_bytes);
|
||||
int64_t ftcan_unmarshal_signed(const uint8_t **data, size_t num_bytes);
|
||||
|
||||
/**
|
||||
* Write num_bytes to a message (marshalled in network byte order). The pointer
|
||||
* is advanced by the corresponding number of bytes and returned.
|
||||
*/
|
||||
uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes);
|
||||
uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes);
|
||||
|
||||
#endif // CAN_HALAL_H
|
||||
|
||||
@ -46,7 +46,15 @@ extern "C" {
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
#define R2D_NONE 0x00
|
||||
#define R2D_TSMS 0x01
|
||||
#define R2D_TS_ACTIVE 0x02
|
||||
#define R2D_RESETTING_NODES 0x03
|
||||
#define R2D_RESETTING_COMMS 0x04
|
||||
#define R2D_WAITING_INIT 0x05
|
||||
#define R2D_INIT_STAGE1 0x06
|
||||
#define R2D_INIT_STAGE2 0x07
|
||||
#define R2D_INIT_SUCCESS 0x0F
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
|
||||
@ -81,7 +81,7 @@
|
||||
* (when HSE is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
|
||||
#define HSE_VALUE ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
/**
|
||||
|
||||
36
Software/Core/Src/can.c
Normal file
36
Software/Core/Src/can.c
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* can.c
|
||||
*
|
||||
* Created on: Jun 12, 2025
|
||||
* Author: janek
|
||||
*/
|
||||
|
||||
#include "can.h"
|
||||
|
||||
rx_acc acc_status = {};
|
||||
extern int ts_on;
|
||||
extern int ams_last_tick;
|
||||
extern int r2d_progress;
|
||||
dash_tx_t dash_tx;
|
||||
|
||||
void can_init(CAN_HandleTypeDef* hcan){
|
||||
ftcan_init(hcan);
|
||||
ftcan_add_filter(0x00, 0x00); // no filter
|
||||
}
|
||||
|
||||
void can_send(){
|
||||
uint8_t data[1];
|
||||
data[0] = dash_tx.dash_send;
|
||||
ftcan_transmit(CAN_ID_TX, data, 1);
|
||||
}
|
||||
|
||||
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t* data){
|
||||
if(id == CAN_ID_RX_AMS){
|
||||
ams_last_tick = HAL_GetTick();
|
||||
acc_status.ams_status.ams_rx = data[0];
|
||||
acc_status.led_status.ams_led = data[7];
|
||||
}
|
||||
if(id == CAN_ID_RX_R2D){
|
||||
r2d_progress = data[1] & 0x0F;
|
||||
}
|
||||
}
|
||||
273
Software/Core/Src/can_halal.c
Normal file
273
Software/Core/Src/can_halal.c
Normal file
@ -0,0 +1,273 @@
|
||||
#include "can_halal.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(FTCAN_IS_BXCAN)
|
||||
static CAN_HandleTypeDef *hcan;
|
||||
|
||||
HAL_StatusTypeDef ftcan_init(CAN_HandleTypeDef *handle) {
|
||||
hcan = handle;
|
||||
|
||||
HAL_StatusTypeDef status =
|
||||
HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return HAL_CAN_Start(hcan);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen) {
|
||||
static CAN_TxHeaderTypeDef header;
|
||||
header.StdId = id;
|
||||
header.IDE = CAN_ID_STD;
|
||||
header.RTR = CAN_RTR_DATA;
|
||||
header.DLC = datalen;
|
||||
uint32_t mailbox;
|
||||
return HAL_CAN_AddTxMessage(hcan, &header, data, &mailbox);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
|
||||
static uint32_t next_filter_no = 0;
|
||||
static CAN_FilterTypeDef filter;
|
||||
if (next_filter_no % 2 == 0) {
|
||||
filter.FilterIdHigh = id << 5;
|
||||
filter.FilterMaskIdHigh = mask << 5;
|
||||
filter.FilterIdLow = id << 5;
|
||||
filter.FilterMaskIdLow = mask << 5;
|
||||
} else {
|
||||
// Leave high filter untouched from the last configuration
|
||||
filter.FilterIdLow = id << 5;
|
||||
filter.FilterMaskIdLow = mask << 5;
|
||||
}
|
||||
filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
|
||||
filter.FilterBank = next_filter_no / 2;
|
||||
if (filter.FilterBank > FTCAN_NUM_FILTERS + 1) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
filter.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
filter.FilterScale = CAN_FILTERSCALE_16BIT;
|
||||
filter.FilterActivation = CAN_FILTER_ENABLE;
|
||||
|
||||
// Disable slave filters
|
||||
// TODO: Some STM32 have multiple CAN peripherals, and one uses the slave
|
||||
// filter bank
|
||||
filter.SlaveStartFilterBank = FTCAN_NUM_FILTERS;
|
||||
|
||||
HAL_StatusTypeDef status = HAL_CAN_ConfigFilter(hcan, &filter);
|
||||
if (status == HAL_OK) {
|
||||
next_filter_no++;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *handle) {
|
||||
if (handle != hcan) {
|
||||
return;
|
||||
}
|
||||
CAN_RxHeaderTypeDef header;
|
||||
uint8_t data[8];
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &header, data) != HAL_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header.IDE != CAN_ID_STD) {
|
||||
return;
|
||||
}
|
||||
|
||||
ftcan_msg_received_cb(header.StdId, header.DLC, data);
|
||||
}
|
||||
#elif defined(FTCAN_IS_FDCAN)
|
||||
static FDCAN_HandleTypeDef *hcan;
|
||||
|
||||
HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) {
|
||||
hcan = handle;
|
||||
|
||||
HAL_StatusTypeDef status =
|
||||
HAL_FDCAN_ActivateNotification(hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
// Reject non-matching messages
|
||||
status =
|
||||
HAL_FDCAN_ConfigGlobalFilter(hcan, FDCAN_REJECT, FDCAN_REJECT,
|
||||
FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE);
|
||||
if (status != HAL_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return HAL_FDCAN_Start(hcan);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
|
||||
size_t datalen) {
|
||||
static FDCAN_TxHeaderTypeDef header;
|
||||
header.Identifier = id;
|
||||
header.IdType = FDCAN_STANDARD_ID;
|
||||
header.TxFrameType = FDCAN_DATA_FRAME;
|
||||
switch (datalen) {
|
||||
case 0:
|
||||
header.DataLength = FDCAN_DLC_BYTES_0;
|
||||
break;
|
||||
case 1:
|
||||
header.DataLength = FDCAN_DLC_BYTES_1;
|
||||
break;
|
||||
case 2:
|
||||
header.DataLength = FDCAN_DLC_BYTES_2;
|
||||
break;
|
||||
case 3:
|
||||
header.DataLength = FDCAN_DLC_BYTES_3;
|
||||
break;
|
||||
case 4:
|
||||
header.DataLength = FDCAN_DLC_BYTES_4;
|
||||
break;
|
||||
case 5:
|
||||
header.DataLength = FDCAN_DLC_BYTES_5;
|
||||
break;
|
||||
case 6:
|
||||
header.DataLength = FDCAN_DLC_BYTES_6;
|
||||
break;
|
||||
case 7:
|
||||
header.DataLength = FDCAN_DLC_BYTES_7;
|
||||
break;
|
||||
case 8:
|
||||
default:
|
||||
header.DataLength = FDCAN_DLC_BYTES_8;
|
||||
break;
|
||||
}
|
||||
header.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
|
||||
header.BitRateSwitch = FDCAN_BRS_OFF;
|
||||
header.FDFormat = FDCAN_CLASSIC_CAN;
|
||||
header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
|
||||
|
||||
// HAL_FDCAN_AddMessageToTxFifoQ doesn't modify the data, but it's not marked
|
||||
// as const for some reason.
|
||||
uint8_t *data_nonconst = (uint8_t *)data;
|
||||
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data_nonconst);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
|
||||
static uint32_t next_filter_no = 0;
|
||||
static FDCAN_FilterTypeDef filter;
|
||||
filter.IdType = FDCAN_STANDARD_ID;
|
||||
filter.FilterIndex = next_filter_no;
|
||||
if (filter.FilterIndex > FTCAN_NUM_FILTERS + 1) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
filter.FilterType = FDCAN_FILTER_MASK;
|
||||
filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
|
||||
filter.FilterID1 = id;
|
||||
filter.FilterID2 = mask;
|
||||
|
||||
HAL_StatusTypeDef status = HAL_FDCAN_ConfigFilter(hcan, &filter);
|
||||
if (status == HAL_OK) {
|
||||
next_filter_no++;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
|
||||
uint32_t RxFifo0ITs) {
|
||||
if (handle != hcan || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET) {
|
||||
return;
|
||||
}
|
||||
|
||||
static FDCAN_RxHeaderTypeDef header;
|
||||
static uint8_t data[8];
|
||||
if (HAL_FDCAN_GetRxMessage(hcan, FDCAN_RX_FIFO0, &header, data) != HAL_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header.FDFormat != FDCAN_CLASSIC_CAN ||
|
||||
header.RxFrameType != FDCAN_DATA_FRAME ||
|
||||
header.IdType != FDCAN_STANDARD_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t datalen;
|
||||
switch (header.DataLength) {
|
||||
case FDCAN_DLC_BYTES_0:
|
||||
datalen = 0;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_1:
|
||||
datalen = 1;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_2:
|
||||
datalen = 2;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_3:
|
||||
datalen = 3;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_4:
|
||||
datalen = 4;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_5:
|
||||
datalen = 5;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_6:
|
||||
datalen = 6;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_7:
|
||||
datalen = 7;
|
||||
break;
|
||||
case FDCAN_DLC_BYTES_8:
|
||||
datalen = 8;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
ftcan_msg_received_cb(header.Identifier, datalen, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
|
||||
const uint8_t *data) {}
|
||||
|
||||
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data_ptr, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
const uint8_t *data = *data_ptr;
|
||||
uint64_t result = 0;
|
||||
for (size_t i = 0; i < num_bytes; i++) {
|
||||
result <<= 8;
|
||||
result |= data[i];
|
||||
}
|
||||
*data_ptr += num_bytes;
|
||||
return result;
|
||||
}
|
||||
|
||||
int64_t ftcan_unmarshal_signed(const uint8_t **data_ptr, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
uint64_t result_unsigned = ftcan_unmarshal_unsigned(data_ptr, num_bytes);
|
||||
// Sign extend by shifting left, then copying to a signed int and shifting
|
||||
// back to the right
|
||||
size_t diff_to_64 = 64 - num_bytes * 8;
|
||||
result_unsigned <<= diff_to_64;
|
||||
int64_t result;
|
||||
memcpy(&result, &result_unsigned, 8);
|
||||
return result >> diff_to_64;
|
||||
}
|
||||
|
||||
uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes) {
|
||||
if (num_bytes > 8) {
|
||||
num_bytes = 8;
|
||||
}
|
||||
|
||||
for (int i = num_bytes - 1; i >= 0; i--) {
|
||||
data[i] = val & 0xFF;
|
||||
val >>= 8;
|
||||
}
|
||||
|
||||
return data + num_bytes;
|
||||
}
|
||||
|
||||
uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes) {
|
||||
return ftcan_marshal_unsigned(data, val, num_bytes);
|
||||
}
|
||||
@ -21,54 +21,21 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "can.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PTD */
|
||||
|
||||
typedef struct {
|
||||
int r2d : 1;
|
||||
int tson : 1;
|
||||
int racemode : 1;
|
||||
int sdc_in : 1;
|
||||
int sdc_out : 1;
|
||||
} dash_tx_t;
|
||||
|
||||
typedef enum {
|
||||
TS_INACTIVE = 0,
|
||||
TS_ACTIVE = 1,
|
||||
TS_PRECHARGE = 2,
|
||||
TS_DISCHARGE = 3,
|
||||
TS_ERROR = 4,
|
||||
} ams_state_t;
|
||||
|
||||
typedef enum {
|
||||
R2D_NONE = 0,
|
||||
R2D_TSMS = 1,
|
||||
R2D_TSActive = 2,
|
||||
R2D_Resetting_Nodes = 3,
|
||||
R2D_Resetting_Comms = 4,
|
||||
R2D_Waiting_Init = 5,
|
||||
R2D_Init_Stage1 = 6,
|
||||
R2D_Init_Stage2 = 7,
|
||||
R2D_Success = 0xF,
|
||||
} r2d_progress_t;
|
||||
|
||||
typedef struct {
|
||||
ams_state_t ams_state;
|
||||
int imd_ok;
|
||||
int sdc_closed;
|
||||
r2d_progress_t r2d_progress;
|
||||
} dash_rx_t;
|
||||
|
||||
/* USER CODE END PTD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
#define CAN_ID_TX 0x420
|
||||
#define CAN_ID_RX_R2D 0x410
|
||||
#define CAN_ID_RX_AMS 0x00A
|
||||
/* USER CODE END PTD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@ -80,15 +47,6 @@ typedef struct {
|
||||
CAN_HandleTypeDef hcan;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
dash_tx_t dash_tx;
|
||||
CAN_TxHeaderTypeDef txHeader;
|
||||
uint32_t txMailbox;
|
||||
|
||||
dash_rx_t dash_rx;
|
||||
|
||||
uint32_t ams_last_tick = 0;
|
||||
uint32_t last_send_can_tick = 0;
|
||||
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
@ -102,7 +60,15 @@ static void MX_CAN_Init(void);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
extern dash_tx_t dash_tx;
|
||||
extern rx_acc acc_status;
|
||||
uint8_t sdc_closed;
|
||||
uint8_t blink_state;
|
||||
uint8_t r2d_progress;
|
||||
uint32_t blink_tick_tson;
|
||||
uint32_t blink_tick_r2d;
|
||||
uint32_t ams_last_tick;
|
||||
uint32_t can_send_tick;
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@ -136,38 +102,13 @@ int main(void)
|
||||
MX_GPIO_Init();
|
||||
MX_CAN_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
can_init(&hcan);
|
||||
|
||||
txHeader.IDE = CAN_ID_STD;
|
||||
txHeader.StdId = CAN_ID_TX;
|
||||
txHeader.RTR = CAN_RTR_DATA;
|
||||
txHeader.DLC = 1;
|
||||
|
||||
|
||||
if (HAL_CAN_Start(&hcan) != HAL_OK)
|
||||
Error_Handler();
|
||||
|
||||
CAN_FilterTypeDef canfilterconfig;
|
||||
|
||||
canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
|
||||
canfilterconfig.FilterBank = 0;
|
||||
canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
|
||||
canfilterconfig.FilterIdHigh = CAN_ID_RX_AMS << (16 - 11);
|
||||
canfilterconfig.FilterIdLow = CAN_ID_RX_R2D << (16 - 11);
|
||||
canfilterconfig.FilterMaskIdHigh = 0x7FF << (16 - 11);
|
||||
canfilterconfig.FilterMaskIdLow = 0x7FF << (16 - 11);
|
||||
canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
|
||||
canfilterconfig.SlaveStartFilterBank = 14;
|
||||
|
||||
if (HAL_CAN_ConfigFilter(&hcan, &canfilterconfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
|
||||
Error_Handler();
|
||||
|
||||
// blink flags
|
||||
int blink_state = 0;
|
||||
blink_state = 0;
|
||||
sdc_closed = 0;
|
||||
blink_tick_tson = HAL_GetTick();
|
||||
blink_tick_r2d = HAL_GetTick();
|
||||
can_send_tick = HAL_GetTick();
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@ -184,82 +125,65 @@ int main(void)
|
||||
dash_tx.sdc_out = HAL_GPIO_ReadPin(SDC_Out_3V3_GPIO_Port, SDC_Out_3V3_Pin);
|
||||
dash_tx.racemode = HAL_GPIO_ReadPin(RMode_Out_3V3_GPIO_Port, RMode_Out_3V3_Pin);
|
||||
|
||||
if ((HAL_GetTick() - last_send_can_tick ) > 200) {
|
||||
if (HAL_CAN_AddTxMessage(&hcan, &txHeader, (uint8_t*) &dash_tx, &txMailbox) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
last_send_can_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
// Inverted in hardware
|
||||
if ((HAL_GetTick() - ams_last_tick) < 150) { //master sendet aller 100ms, fürs testen erstmal auf 150ms gesetzt -> kann später wieder runter
|
||||
HAL_GPIO_WritePin(IMD_LED_GPIO_Port, IMD_LED_Pin, dash_rx.imd_ok);
|
||||
HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, dash_rx.ams_state != TS_ERROR);
|
||||
HAL_GPIO_WritePin(TSOFF_LED_GPIO_Port, TSOFF_LED_Pin, dash_rx.ams_state == TS_INACTIVE);
|
||||
if ((HAL_GetTick() - ams_last_tick) < 350) { //master sendet aller 100ms, fürs testen erstmal auf 150ms gesetzt -> kann später wieder runter
|
||||
HAL_GPIO_WritePin(IMD_LED_GPIO_Port, IMD_LED_Pin, acc_status.led_status.imd_error);
|
||||
HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, acc_status.led_status.ams_error);
|
||||
HAL_GPIO_WritePin(TSOFF_LED_GPIO_Port, TSOFF_LED_Pin,acc_status.led_status.ts_green);
|
||||
} else {
|
||||
// Safe state: Error LEDs on, TSOFF off
|
||||
HAL_GPIO_WritePin(IMD_LED_GPIO_Port, IMD_LED_Pin, 0);
|
||||
HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, 0);
|
||||
HAL_GPIO_WritePin(TSOFF_LED_GPIO_Port, TSOFF_LED_Pin, 0);
|
||||
// Safe state: Error LEDs on, TSOFF off
|
||||
HAL_GPIO_WritePin(IMD_LED_GPIO_Port, IMD_LED_Pin, 0);
|
||||
HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, 0);
|
||||
HAL_GPIO_WritePin(TSOFF_LED_GPIO_Port, TSOFF_LED_Pin, 0);
|
||||
}
|
||||
|
||||
int r = 0, g = 0, b = 0;
|
||||
int br = 0, bg = 0, bb = 0;
|
||||
|
||||
if (dash_rx.sdc_closed) {
|
||||
switch (dash_rx.ams_state) {
|
||||
case TS_INACTIVE:
|
||||
r = g = 1;
|
||||
break;
|
||||
case TS_PRECHARGE:
|
||||
// Gelb blink
|
||||
br = bg = 1;
|
||||
break;
|
||||
case TS_ACTIVE:
|
||||
g = 1;
|
||||
break;
|
||||
case TS_DISCHARGE:
|
||||
// Blau blink
|
||||
bb = 1;
|
||||
break;
|
||||
default:
|
||||
r = 1;
|
||||
break;
|
||||
if(acc_status.ams_status.sdc_closed == 1){
|
||||
if(acc_status.ams_status.ts_inactive){
|
||||
r = g = 1;
|
||||
}
|
||||
} else {
|
||||
b = 1;
|
||||
if(acc_status.ams_status.ts_precharge){
|
||||
br = bg = 1;
|
||||
}
|
||||
if(acc_status.ams_status.ts_active){
|
||||
g = 1;
|
||||
}
|
||||
if(acc_status.ams_status.ts_discharge){
|
||||
br = bb = 1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
b = r = 1;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(TSON_R_GPIO_Port, TSON_R_Pin, r);
|
||||
HAL_GPIO_WritePin(TSON_G_GPIO_Port, TSON_G_Pin, g);
|
||||
HAL_GPIO_WritePin(TSON_B_GPIO_Port, TSON_B_Pin, b);
|
||||
|
||||
if (br || bg || bb) {
|
||||
HAL_GPIO_WritePin(TSON_R_GPIO_Port, TSON_R_Pin, br && blink_state);
|
||||
HAL_GPIO_WritePin(TSON_G_GPIO_Port, TSON_G_Pin, bg && blink_state);
|
||||
HAL_GPIO_WritePin(TSON_B_GPIO_Port, TSON_B_Pin, bb && blink_state);
|
||||
if ((br || bg || bb) && ((HAL_GetTick() - blink_tick_tson) > 100u)) {
|
||||
HAL_GPIO_WritePin(TSON_R_GPIO_Port, TSON_R_Pin, br && blink_state);
|
||||
HAL_GPIO_WritePin(TSON_G_GPIO_Port, TSON_G_Pin, bg && blink_state);
|
||||
HAL_GPIO_WritePin(TSON_B_GPIO_Port, TSON_B_Pin, bb && blink_state);
|
||||
blink_tick_tson = HAL_GetTick();
|
||||
}
|
||||
|
||||
r = g = b = 0;
|
||||
br = bg = bb = 0;
|
||||
|
||||
if (dash_rx.ams_state == TS_ACTIVE) {
|
||||
switch (dash_rx.r2d_progress) {
|
||||
case R2D_NONE:
|
||||
case R2D_TSMS:
|
||||
case R2D_TSActive:
|
||||
r = g = 1;
|
||||
break;
|
||||
case R2D_Success:
|
||||
g = 1;
|
||||
break;
|
||||
default:
|
||||
// Gelb blink
|
||||
bg = br = 1;
|
||||
break;
|
||||
if (acc_status.ams_status.ts_active) {
|
||||
if((r2d_progress == R2D_TS_ACTIVE) || (r2d_progress == R2D_TSMS) || (r2d_progress == R2D_NONE)){
|
||||
r = g = 1;
|
||||
}
|
||||
if((r2d_progress == R2D_RESETTING_NODES) || (r2d_progress == R2D_RESETTING_COMMS) || (r2d_progress == R2D_WAITING_INIT) || (r2d_progress == R2D_INIT_STAGE1) || (r2d_progress == R2D_INIT_STAGE2)){
|
||||
br = bg = 1;
|
||||
}
|
||||
if(r2d_progress == R2D_INIT_SUCCESS){
|
||||
g = 1;
|
||||
}
|
||||
} else {
|
||||
b = 1;
|
||||
b = r = 1;
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(R2D_R_GPIO_Port, R2D_R_Pin, r);
|
||||
@ -267,20 +191,19 @@ int main(void)
|
||||
HAL_GPIO_WritePin(R2D_B_GPIO_Port, R2D_B_Pin, b);
|
||||
|
||||
|
||||
if (br || bg || bb) {
|
||||
HAL_GPIO_WritePin(R2D_R_GPIO_Port, R2D_R_Pin, br && blink_state);
|
||||
HAL_GPIO_WritePin(R2D_G_GPIO_Port, R2D_G_Pin, bg && blink_state);
|
||||
HAL_GPIO_WritePin(R2D_B_GPIO_Port, R2D_B_Pin, bb && blink_state);
|
||||
if ((br || bg || bb) && ((HAL_GetTick() - blink_tick_r2d) > 100u)) {
|
||||
HAL_GPIO_WritePin(R2D_R_GPIO_Port, R2D_R_Pin, br && blink_state);
|
||||
HAL_GPIO_WritePin(R2D_G_GPIO_Port, R2D_G_Pin, bg && blink_state);
|
||||
HAL_GPIO_WritePin(R2D_B_GPIO_Port, R2D_B_Pin, bb && blink_state);
|
||||
blink_tick_r2d = HAL_GetTick();
|
||||
}
|
||||
|
||||
blink_state = !blink_state;
|
||||
|
||||
HAL_Delay(50);
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Farbveläufe
|
||||
**/
|
||||
if ((HAL_GetTick() - can_send_tick) > 50u){
|
||||
can_send();
|
||||
can_send_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
@ -298,12 +221,10 @@ void SystemClock_Config(void)
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||||
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
@ -313,7 +234,7 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
||||
@ -373,6 +294,7 @@ static void MX_GPIO_Init(void)
|
||||
/* USER CODE END MX_GPIO_Init_1 */
|
||||
|
||||
/* GPIO Ports Clock Enable */
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
@ -412,36 +334,7 @@ static void MX_GPIO_Init(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
// CAN RX interrupt handler
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
|
||||
|
||||
CAN_RxHeaderTypeDef rxHeader;
|
||||
uint8_t rxData[8];
|
||||
|
||||
// Read frame from HW into buffer
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, rxData) != HAL_OK)
|
||||
Error_Handler();
|
||||
|
||||
// Discard if it's not for us (shouldn't happen thanks to filter, but just to be sure)
|
||||
if (rxHeader.StdId == CAN_ID_RX_AMS) {
|
||||
uint8_t ams_info = rxData[0];
|
||||
uint8_t imd_info = rxData[6];
|
||||
|
||||
dash_rx.ams_state = ams_info & 0b01111111;
|
||||
dash_rx.sdc_closed = ams_info >> 7;
|
||||
dash_rx.imd_ok = imd_info >> 7;
|
||||
ams_last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
|
||||
if (rxHeader.StdId == CAN_ID_RX_R2D) {
|
||||
uint8_t r2d_info = rxData[1];
|
||||
|
||||
dash_rx.r2d_progress = r2d_info & 0b00001111;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user