2021-07-28 05:28 PM
Hi!
I just have written a SAE J1939 library for CAN-bus. Yes it works.
But I got a request from someboy and he asking me if I could make this so it not only fit STM32F3 series. It need to fit all STM32 series.
https://github.com/DanielMartensson/Open-SAE-J1939
So my question is:
The HAL libraries in this code, is for STM32F3 series. But does it work with other STM32 series as well?
And one more question:
How large is the can bus buffer for reading a message?
I mean, the message will be stored inside the STM32 until I read it? Right?
How many CAN messages can an STM32 hold? 1 at the time? Or multiple messages in a queue?
/*
* CAN.c
*
* Created on: Jun 14, 2021
* Author: Daniel Mårtensson
*/
#include "Functions.h"
static CAN_HandleTypeDef *can_handler;
static uint32_t CAN_ID = 0;
static uint8_t RxData[8] = {0};
static bool new_message = false;
static void Create_CAN_Filter(CAN_HandleTypeDef *hcan);
static void Create_CAN_Interrupt(CAN_HandleTypeDef *hcan);
void STM32_PLC_Start_CAN(CAN_HandleTypeDef *hcan) {
can_handler = hcan;
Create_CAN_Filter(hcan);
if (HAL_CAN_Start(hcan) != HAL_OK)
Error_Handler();
Create_CAN_Interrupt(hcan);
}
HAL_StatusTypeDef STM32_PLC_CAN_Transmit(uint8_t TxData[], CAN_TxHeaderTypeDef *TxHeader) {
uint32_t TxMailbox;
return HAL_CAN_AddTxMessage(can_handler, TxHeader, TxData, &TxMailbox);
}
/* Returns true if the message data is new */
void STM32_PLC_CAN_Get_ID_Data(uint32_t* ID, uint8_t data[], bool* is_new_message) {
*ID = CAN_ID;
memcpy(data, RxData, 8);
*is_new_message = new_message;
new_message = false;
}
/* Interrupt handler that read message */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
CAN_RxHeaderTypeDef RxHeader;
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
Error_Handler();
/* Read ID */
if(RxHeader.IDE == CAN_ID_STD){
CAN_ID = RxHeader.StdId;
}else{
CAN_ID = RxHeader.ExtId;
}
new_message = true;
}
static void Create_CAN_Interrupt(CAN_HandleTypeDef *hcan) {
if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
Error_Handler();
}
static void Create_CAN_Filter(CAN_HandleTypeDef *hcan) {
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK)
Error_Handler();
}