The Controller Area Network (CAN, also known as CAN Bus) is a vehicle bus standard designed to allow electronic control units and devices to communicate with each other in applications without a host computer.
STM32F779-Eval board has 3 CAN interfaces, however only the two of them have their own clock (master). For that reason the following library provides some C methods to activate and use those CAN interfaces. Beware that each CAN interface requires its own FIFO as well as its own RX interrupt (RX0/RX1)
/*! @file dev_can.h @brief < brief description here > This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Date: 14 Feb 2017 Version: 1 Design: Ioannis D. Implementation: Ioannis D. */ #ifdef __cplusplus extern "C" { #endif /****************************************************************************** * Header File Guard Symbol Start ******************************************************************************/ #ifndef DEVICES_DEV_CAN_H_ /****************************************************************************** * Definitions ******************************************************************************/ #define DEVICES_DEV_CAN_H_ /****************************************************************************** * Includes ******************************************************************************/ #include "can.h" #include <Libraries/lbr_framework.h> /****************************************************************************** * Structures & Global Variables & Extern ******************************************************************************/ typedef enum { CAN_1 = 0x00, CAN_3 = 0x01, }I_DEV_CAN; extern CanRxMsgTypeDef dev_can1_rxframe,dev_can3_rxframe; extern CanTxMsgTypeDef dev_can1_txframe,dev_can3_txframe; extern void (*CAN1_RXCallback)(), (*CAN3_RXCallback)(); /****************************************************************************** * Functions ******************************************************************************/ I_Status dev_can_init(I_DEV_CAN); I_Status dev_can_deinit(I_DEV_CAN); I_Status dev_can_send_std(I_DEV_CAN, uint32_t, uint8_t, uint8_t *); I_Status dev_can_send_ext(I_DEV_CAN, uint32_t, uint32_t, uint8_t, uint8_t *); I_Status dev_can_send_frame(I_DEV_CAN, CanTxMsgTypeDef *); I_Status dev_can_set_rx_callback(I_DEV_CAN, void(*)()); I_Status dev_can_clear_rx_callback(I_DEV_CAN); CanRxMsgTypeDef * dev_can_get_rx_frame(I_DEV_CAN can); /****************************************************************************** * Header File Guard Symbol End *******************************************************************************/ #endif #ifdef __cplusplus } #endif
/*! @file dev_can.c @brief < brief description here > This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Date: 15 Ìáñ 2017 Version: 1.7 Design: Io. D Implementation: Io. D */ /****************************************************************************** * Source File Start ******************************************************************************/ /****************************************************************************** * Includes ******************************************************************************/ #include <Devices/dev_can.h> /****************************************************************************** * Extern Variables, Global Variables ******************************************************************************/ int dev_can1_active = 0,dev_can3_active = 0; CanRxMsgTypeDef dev_can1_rxframe,dev_can3_rxframe; CanTxMsgTypeDef dev_can1_txframe,dev_can3_txframe; void (*CAN1_RXCallback)() = NULL, (*CAN3_RXCallback)() = NULL; /****************************************************************************** * Functions ******************************************************************************/ void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *canHandle) { if(canHandle->Instance==CAN1) { if(CAN1_RXCallback != NULL) CAN1_RXCallback(); HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0); } else if(canHandle->Instance==CAN3) { if(CAN3_RXCallback != NULL) CAN3_RXCallback(); HAL_CAN_Receive_IT(&hcan3, CAN_FIFO1); } } I_Status dev_can_init(I_DEV_CAN can) { CAN_FilterConfTypeDef CAN_FilterInitStructure; switch(can) { case CAN_1: CAN1_RXCallback = NULL; MX_CAN1_Init(); CAN_FilterInitStructure.FilterNumber = 0; CAN_FilterInitStructure.FilterMode = CAN_FILTERMODE_IDMASK; CAN_FilterInitStructure.FilterScale = CAN_FILTERSCALE_32BIT; CAN_FilterInitStructure.FilterIdHigh = 0x0000; CAN_FilterInitStructure.FilterIdLow = 0x0000; CAN_FilterInitStructure.FilterMaskIdHigh = 0x0000; CAN_FilterInitStructure.FilterMaskIdLow = 0x0000; CAN_FilterInitStructure.FilterFIFOAssignment = 0; CAN_FilterInitStructure.FilterActivation = ENABLE; HAL_CAN_ConfigFilter(&hcan1, &CAN_FilterInitStructure); hcan1.pRxMsg = &dev_can1_rxframe; hcan1.pTxMsg = &dev_can1_txframe; HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0); break; case CAN_3: CAN3_RXCallback = NULL; MX_CAN3_Init(); CAN_FilterInitStructure.FilterNumber = 0; CAN_FilterInitStructure.FilterMode = CAN_FILTERMODE_IDMASK; CAN_FilterInitStructure.FilterScale = CAN_FILTERSCALE_32BIT; CAN_FilterInitStructure.FilterIdHigh = 0x0000; CAN_FilterInitStructure.FilterIdLow = 0x0000; CAN_FilterInitStructure.FilterMaskIdHigh = 0x0000; CAN_FilterInitStructure.FilterMaskIdLow = 0x0000; CAN_FilterInitStructure.FilterFIFOAssignment = 1; CAN_FilterInitStructure.FilterActivation = ENABLE; HAL_CAN_ConfigFilter(&hcan3, &CAN_FilterInitStructure); hcan3.pRx1Msg = &dev_can3_rxframe; hcan3.pTxMsg = &dev_can3_txframe; HAL_CAN_Receive_IT(&hcan3, CAN_FIFO1); break; default: return I_INVALID; break; } return I_OK; } I_Status dev_can_deinit(I_DEV_CAN can) { switch(can) { case CAN_1: CAN1_RXCallback = NULL; break; case CAN_3: CAN3_RXCallback = NULL; break; default: return I_INVALID; break; } return I_OK; } I_Status dev_can_send_std(I_DEV_CAN can, uint32_t stdID, uint8_t dlc, uint8_t * data) { CAN_HandleTypeDef * hcan; switch(can) { case CAN_1: hcan = &hcan1; break; case CAN_3: hcan = &hcan3; break; default: return I_INVALID; break; } hcan->pTxMsg->StdId = stdID; hcan->pTxMsg->ExtId = 0x0; hcan->pTxMsg->RTR = CAN_RTR_DATA; hcan->pTxMsg->IDE = CAN_ID_STD; hcan->pTxMsg->DLC = dlc; memmove(hcan->pTxMsg->Data,data,dlc*sizeof(uint8_t)); return HAL_CAN_Transmit_IT(hcan) == HAL_OK ? I_OK : I_ERROR; } I_Status dev_can_send_ext(I_DEV_CAN can, uint32_t stdID, uint32_t extID, uint8_t dlc, uint8_t * data) { CAN_HandleTypeDef * hcan; switch(can) { case CAN_1: hcan = &hcan1; break; case CAN_3: hcan = &hcan3; break; default: return I_INVALID; break; } hcan->pTxMsg->StdId = stdID; hcan->pTxMsg->ExtId = extID; hcan->pTxMsg->RTR = CAN_RTR_DATA; hcan->pTxMsg->IDE = CAN_ID_EXT; hcan1.pTxMsg->DLC = dlc; memmove(hcan->pTxMsg->Data,data,dlc*sizeof(uint8_t)); return HAL_CAN_Transmit_IT(hcan) == HAL_OK ? I_OK : I_ERROR; } I_Status dev_can_send_frame(I_DEV_CAN can, CanTxMsgTypeDef * frame) { CAN_HandleTypeDef * hcan; switch(can) { case CAN_1: hcan = &hcan1; break; case CAN_3: hcan = &hcan3; break; default: return I_INVALID; break; } memmove(hcan->pTxMsg,frame,sizeof(CanTxMsgTypeDef)); return HAL_CAN_Transmit_IT(hcan) == HAL_OK ? I_OK : I_ERROR; } I_Status dev_can_set_rx_callback(I_DEV_CAN can, void(*function)()) { void (**can_RX_callback)(); switch(can) { case CAN_1: can_RX_callback = &CAN1_RXCallback; break; case CAN_3: can_RX_callback = &CAN3_RXCallback; break; default: return I_INVALID; break; } (*can_RX_callback) = function; return I_OK; } I_Status dev_can_clear_rx_callback(I_DEV_CAN can) { void (**can_RX_callback)(); switch(can) { case CAN_1: can_RX_callback = &CAN1_RXCallback; break; case CAN_3: can_RX_callback = &CAN3_RXCallback; break; default: return I_INVALID; break; } (*can_RX_callback) = NULL; return I_OK; } inline CanRxMsgTypeDef * dev_can_get_rx_frame(I_DEV_CAN can) { switch(can) { case CAN_1: return hcan1.pRxMsg; break; case CAN_3: return hcan3.pRx1Msg; break; default: break; } return NULL; }