Skip to content

Commit

Permalink
can fixes and add error queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Feb 28, 2024
1 parent 3d9fed2 commit 0853182
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
7 changes: 6 additions & 1 deletion Core/Inc/can.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#ifndef CAN_H
#define CAN_H

#include <stdint.h>
#include "stm32g4xx_hal.h"
#include "fdcan.h"

HAL_StatusTypeDef receive_msg(can_t *hcan);
void init_iroh_can(FDCAN_HandleTypeDef *hcan);

#endif
9 changes: 9 additions & 0 deletions Core/Inc/queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ typedef struct {

extern osMessageQueueId_t bms_data_queue;

#define ERROR_QUEUE_SIZE 8

typedef struct {
uint8_t code;
char msg[20];
} iroh_error_t;

extern osMessageQueueId_t error_queue;

#endif
73 changes: 41 additions & 32 deletions Core/Src/can.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
#include "can.h"
#include "queues.h"
#include "string.h"

//Arbitrary CAN IDs
#define CAN_ID 0x69

bms_data_t bms_data;
#define BMS_DATA_CAN_ID 0x80

void receive_msg(FDCAN_HandleTypeDef *hfdcan)
{

void init_iroh_can(FDCAN_HandleTypeDef *hcan) {
can_t fdcan;
fdcan.callback = receive_msg;
fdcan.id_list = 0;
fdcan.id_list_len = 0;
fdcan.hcan = hcan;
bms_data_t bms_data;
iroh_error_t e;

can_init(&fdcan);
}
FDCAN_RxHeaderTypeDef *rx_header;
uint8_t *rx_data;

HAL_StatusTypeDef receive_msg(can_t *can)
{
if (!HAL_FDCAN_GetRxFifoFillLevel(hfdcan, FDCAN_RX_FIFO0)) {
e.code = 1;
strcpy(e.msg, "No msg Recieved");
osMessageQueuePut(error_queue, &e, 0, 2000);
return;
}

FDCAN_RxHeaderTypeDef rx_header;
uint8_t rx_data[8]; //array of 8 bytes

if (HAL_FDCAN_GetRxFifoFillLevel(can->hcan, FDCAN_RX_FIFO0) > 0) { //Check if messages are available
if (HAL_FDCAN_GetRxMessage(can->hcan,FDCAN_RX_FIFO0,&rx_header,rx_data) == HAL_OK) {
uint32_t message_id = rx_header.Identifier;
if (message_id == CAN_ID) {
bms_data.SoC = (uint8_t) rx_data[0];
bms_data.current = (uint8_t) rx_data[1];
if (osStatus_t osMessageQueuePut(bms_data_queue *bms_data, 0, 2000) == osOK) //timeout is 2000 ms
return HAL_OK;
else
return HAL_ERROR
}
if (!HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, rx_header, rx_data)) {
e.code = 2;
strcpy(e.msg, "Cannot get msg");
osMessageQueuePut(error_queue, &e, 0, 2000);
return;
}

uint32_t message_id = rx_header->Identifier;

if (message_id == BMS_DATA_CAN_ID) {
bms_data.SoC = rx_data[0];
bms_data.current = rx_data[1];
if (!osMessageQueuePut(bms_data_queue, &bms_data, 0, 2000)) {
e.code = 3;
strcpy(e.msg, "BMS data MQ err");
osMessageQueuePut(error_queue, &e, 0, 2000);
return;
}
return HAL_ERROR;
}
return HAL_BUSY;
}
}


void init_iroh_can(FDCAN_HandleTypeDef *hcan) {
can_t *fdcan;
fdcan->callback = receive_msg;
fdcan->id_list = 0;
fdcan->id_list_len = 0;
fdcan->hcan = hcan;

can_init(fdcan);
}
5 changes: 4 additions & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/* USER CODE BEGIN Includes */
#include "queues.h"
#include "iroh_tasks.h"
#include "can.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -59,6 +60,7 @@ const osThreadAttr_t defaultTask_attributes = {
};
/* USER CODE BEGIN PV */
osMessageQueueId_t bms_data_queue;
osMessageQueueId_t error_queue;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand Down Expand Up @@ -110,7 +112,7 @@ int main(void)
MX_USART2_UART_Init();
MX_FDCAN1_Init();
/* USER CODE BEGIN 2 */

init_iroh_can(&hfdcan1);
/* USER CODE END 2 */

/* Init scheduler */
Expand All @@ -130,6 +132,7 @@ int main(void)

/* USER CODE BEGIN RTOS_QUEUES */
bms_data_queue = osMessageQueueNew(BMS_DATA_QUEUE_SIZE, sizeof(bms_data_t), NULL);
error_queue = osMessageQueueNew(ERROR_QUEUE_SIZE, sizeof(iroh_error_t), NULL);
/* USER CODE END RTOS_QUEUES */

/* Create the thread(s) */
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Core/Src/stm32g4xx_hal_msp.c \
Core/Src/seven_segment.c \
Core/Src/switch.c \
Core/Src/iroh_tasks.c \
Core/Src/can.c \
Drivers/Embedded-Base/platforms/stm32g431/src/fdcan.c \
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c \
Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_rcc.c \
Expand Down

0 comments on commit 0853182

Please sign in to comment.