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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/*! @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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
/*! @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; } |
2 Comments
Pingback: CAN frames buffer (received by interrupt) for STM32F779-Eval board | Robotomy
Pingback: CAN frames buffer (received by interrupt) for STM32F779-Eval board | DevCoons