Skip to content

Commit

Permalink
Fix CAN
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Knight committed Oct 17, 2024
1 parent 5b890ba commit 9189313
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
53 changes: 2 additions & 51 deletions include/CANLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@


extern CAN_HandleTypeDef hcan;
extern void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length);

namespace CANLib
{
void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length);


EasyPinD can_rs(GPIOA, {GPIO_PIN_15, GPIO_MODE_OUTPUT_OD, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW}, GPIO_PIN_SET);

Expand Down Expand Up @@ -187,57 +188,7 @@ namespace CANLib
CANObject<int8_t, 1> obj_battery_state(0x0058);


void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxHeader = {0};
uint8_t RxData[8] = {0};

if( HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK )
{
CANLib::can_manager.IncomingCANFrame(RxHeader.StdId, RxData, RxHeader.DLC);
}

return;
}

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "RX error event, code: 0x%08lX\n", HAL_CAN_GetError(hcan));

return;
}

void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length)
{
CAN_TxHeaderTypeDef TxHeader = {0};
uint8_t TxData[8] = {0};
uint32_t TxMailbox = 0;

TxHeader.StdId = id;
TxHeader.ExtId = 0;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = length;
TxHeader.TransmitGlobalTime = DISABLE;
memcpy(TxData, data, length);

while( HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0 )
{
Leds::obj.SetOn(Leds::LED_RED);
}
Leds::obj.SetOff(Leds::LED_RED);

if( HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK )
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "TX error event, code: 0x%08lX\n", HAL_CAN_GetError(&hcan));
}

return;
}



Expand Down
50 changes: 50 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,57 @@ static void MX_GPIO_Init(void);



void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CAN_RxHeaderTypeDef RxHeader = {0};
uint8_t RxData[8] = {0};

if( HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK )
{
CANLib::can_manager.IncomingCANFrame(RxHeader.StdId, RxData, RxHeader.DLC);
}

return;
}

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "RX error event, code: 0x%08lX\n", HAL_CAN_GetError(hcan));

return;
}

void HAL_CAN_Send(can_object_id_t id, uint8_t *data, uint8_t length)
{
CAN_TxHeaderTypeDef TxHeader = {0};
uint8_t TxData[8] = {0};
uint32_t TxMailbox = 0;

TxHeader.StdId = id;
TxHeader.ExtId = 0;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = length;
TxHeader.TransmitGlobalTime = DISABLE;
memcpy(TxData, data, length);

while( HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0 )
{
Leds::obj.SetOn(Leds::LED_RED);
}
Leds::obj.SetOff(Leds::LED_RED);

if( HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK )
{
Leds::obj.SetOn(Leds::LED_RED, 100);

DEBUG_LOG_TOPIC("CAN", "TX error event, code: 0x%08lX\n", HAL_CAN_GetError(&hcan));
}

return;
}



Expand Down

0 comments on commit 9189313

Please sign in to comment.