Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions include/can_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ void can_handle_data();
void can_init(can_t *obj, CANName peripheral, CanNominalBitTimingResult const can_bit_timing);
int can_frequency(can_t *obj, uint32_t const can_bitrate);

int can_write(can_t *obj, union x8h7_can_message const * msg);
void can_write(can_t *obj, union x8h7_can_message const * msg);
int can_read(can_t *obj, union x8h7_can_message *msg);
int can_mode(can_t *obj, CanMode mode);
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle);
void can_reset(can_t *obj);
unsigned char can_rderror(can_t *obj);
unsigned char can_tderror(can_t *obj);

Expand Down
61 changes: 3 additions & 58 deletions src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {

dbg_printf("fdcan1_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);

if (!can_write(&fdcan_1, &msg))
can_reset(&fdcan_1);
can_write(&fdcan_1, &msg);
}
else {
dbg_printf("fdcan1_handler: error invalid opcode (:%d)\n", opcode);
Expand Down Expand Up @@ -221,8 +220,7 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {

dbg_printf("fdcan2_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);

if (!can_write(&fdcan_2, &msg))
can_reset(&fdcan_2);
can_write(&fdcan_2, &msg);
}
else {
dbg_printf("fdcan2_handler: error invalid opcode (:%d)\n", opcode);
Expand Down Expand Up @@ -354,18 +352,6 @@ void can_init(can_t *obj, CANName peripheral, CanNominalBitTimingResult const ca
can_internal_init(obj);
}

/** Reset CAN interface.
*
* To use after error overflow.
*/
void can_reset(can_t *obj)
{
can_mode(obj, MODE_RESET);
HAL_FDCAN_ResetTimeoutCounter(&obj->CanHandle);
HAL_FDCAN_ResetTimestampCounter(&obj->CanHandle);
}


int can_frequency(can_t *obj, uint32_t const can_bitrate)
{
if (HAL_FDCAN_Stop(&obj->CanHandle) != HAL_OK) {
Expand Down Expand Up @@ -443,7 +429,7 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t
}


int can_write(can_t *obj, union x8h7_can_message const * msg)
void can_write(can_t *obj, union x8h7_can_message const * msg)
{
FDCAN_TxHeaderTypeDef TxHeader = {0};

Expand Down Expand Up @@ -494,13 +480,11 @@ int can_write(can_t *obj, union x8h7_can_message const * msg)
if (err_code == HAL_FDCAN_ERROR_FIFO_FULL) msg[1] = X8H7_CAN_STS_FLG_TX_OVR;

enqueue_packet(obj == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
return 0;
}
else
{
uint8_t msg[2] = {X8H7_CAN_STS_INT_TX, 0};
enqueue_packet(obj == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
return 1;
}
}

Expand Down Expand Up @@ -554,42 +538,3 @@ unsigned char can_tderror(can_t *obj)

return (unsigned char)ErrorCounters.TxErrorCnt;
}

/** Change CAN operation to the specified mode
*
* @param mode The new operation mode (MODE_RESET, MODE_NORMAL, MODE_SILENT, MODE_TEST_LOCAL, MODE_TEST_GLOBAL, MODE_TEST_SILENT)
*
* @returns
* 0 if mode change failed or unsupported,
* 1 if mode change was successful
*/
int can_mode(can_t *obj, CanMode mode)
{
if (HAL_FDCAN_Stop(&obj->CanHandle) != HAL_OK) {
error("HAL_FDCAN_Stop error\n");
}

switch (mode) {
case MODE_RESET:
break;
case MODE_NORMAL:
obj->CanHandle.Init.Mode = FDCAN_MODE_NORMAL;
// obj->CanHandle.Init.NominalPrescaler = 100; // Prescaler
break;
case MODE_SILENT: // Bus Monitoring
obj->CanHandle.Init.Mode = FDCAN_MODE_BUS_MONITORING;
break;
case MODE_TEST_GLOBAL: // External LoopBack
case MODE_TEST_LOCAL:
obj->CanHandle.Init.Mode = FDCAN_MODE_EXTERNAL_LOOPBACK;
break;
case MODE_TEST_SILENT: // Internal LoopBack
obj->CanHandle.Init.Mode = FDCAN_MODE_INTERNAL_LOOPBACK;
// obj->CanHandle.Init.NominalPrescaler = 1; // Prescaler
break;
default:
return 0;
}

return can_internal_init(obj);
}