HAL_CAN.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. ******************************************************************************
  3. * @file HAL_Can.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief CAN HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (CAN).
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /*********************************************************************************
  16. * Function : HAL_CAN_OperatingModeRequest
  17. * Description : Select the CAN Operation mode.
  18. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  19. * the configuration information for CAN module
  20. * Input : CAN_OperatingMode:CAN Operating Mode. This parameter can be one of @ref CAN_OperatingMode enumeration.
  21. * Output : HAL status
  22. * Author : CWT Data : 2020
  23. **********************************************************************************/
  24. HAL_StatusTypeDef HAL_CAN_OperatingModeRequest(CAN_HandleTypeDef *hcan, uint8_t CAN_OperatingMode)
  25. {
  26. uint8_t status = HAL_ERROR;
  27. /* Check the parameters */
  28. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR;
  29. if(!IS_CAN_OPERATING_MODE(CAN_OperatingMode)) return HAL_ERROR;
  30. if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
  31. {
  32. hcan->Instance->MOD |= CAN_OperatingMode_Initialization; // enter Initialization
  33. if ((hcan->Instance->MOD & CAN_MOD_RM) != CAN_OperatingMode_Initialization)
  34. {
  35. status = HAL_ERROR;
  36. }
  37. else
  38. {
  39. status = HAL_OK;
  40. }
  41. }
  42. else if(CAN_OperatingMode == CAN_OperatingMode_Normal)
  43. {
  44. hcan->Instance->MOD &=~ CAN_OperatingMode_Initialization; //1-->0 enter Normal
  45. if ((hcan->Instance->MOD & CAN_MOD_RM) != CAN_OperatingMode_Normal)
  46. {
  47. status = HAL_ERROR;
  48. }
  49. else
  50. {
  51. status = HAL_OK;
  52. }
  53. }
  54. else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
  55. {
  56. hcan->Instance->MOD |= CAN_OperatingMode_Sleep; // enter Normal
  57. if ((hcan->Instance->MOD & CAN_MOD_SM) != CAN_OperatingMode_Sleep)
  58. {
  59. status = HAL_ERROR;
  60. }
  61. else
  62. {
  63. status = HAL_OK;
  64. }
  65. }
  66. else if(CAN_OperatingMode == CAN_OperatingMode_Listen)
  67. {
  68. hcan->Instance->MOD |= CAN_OperatingMode_Listen; // enter Normal
  69. if((hcan->Instance->MOD & CAN_MOD_LOM) != CAN_OperatingMode_Listen)
  70. {
  71. status = HAL_ERROR;
  72. }
  73. else
  74. {
  75. status = HAL_OK;
  76. }
  77. }
  78. else
  79. {
  80. status = HAL_ERROR;
  81. }
  82. return (uint8_t) status;
  83. }
  84. /*********************************************************************************
  85. * Function : HAL_CAN_MspInit
  86. * Description : Initialize the CAN MSP.
  87. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  88. * the configuration information for CAN module
  89. * Output :
  90. * Author : CWT Data : 2020
  91. **********************************************************************************/
  92. __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
  93. {
  94. /* NOTE : This function should not be modified, when the callback is needed,
  95. the HAL_UART_MspInit can be implemented in the user file
  96. */
  97. /* For Example */
  98. GPIO_InitTypeDef GPIO_InitStruct;
  99. if(hcan->Instance==CAN1)
  100. {
  101. /* Enable CAN clock */
  102. System_Module_Enable(EN_CAN1);
  103. GPIO_InitTypeDef GPIO_InitStructure;
  104. /* Initialization GPIO */
  105. /* PA11:Rx */ /* PA12:Tx */
  106. GPIO_InitStructure.Pin = GPIO_PIN_11|GPIO_PIN_12;
  107. GPIO_InitStructure.Alternate=GPIO_FUNCTION_5;
  108. GPIO_InitStructure.Pull=GPIO_PULLUP;
  109. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  110. HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  111. }
  112. else if(hcan->Instance==CAN2)
  113. {
  114. /* Enable CAN clock */
  115. System_Module_Enable(EN_CAN2);
  116. GPIO_InitTypeDef GPIO_InitStructure;
  117. /* Initialization GPIO */
  118. /* PB5:Rx */ /* PB6:Tx */
  119. GPIO_InitStructure.Pin = GPIO_PIN_5|GPIO_PIN_6;
  120. GPIO_InitStructure.Alternate=GPIO_FUNCTION_5;
  121. GPIO_InitStructure.Pull=GPIO_PULLUP;
  122. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  123. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  124. /* Enable the CAN Receive interrupt */
  125. hcan->Instance->IER |= CAN_IER_RIE;
  126. NVIC_ClearPendingIRQ(CAN2_IRQn);
  127. NVIC_SetPriority(CAN2_IRQn, 5);
  128. NVIC_EnableIRQ(CAN2_IRQn);
  129. }
  130. }
  131. /*********************************************************************************
  132. * Function : HAL_CAN_MspDeInit
  133. * Description : CAN MSP De-Initialization
  134. * This function frees the hardware resources used in this example:
  135. * - Disable the Peripheral's clock
  136. * - Revert GPIO configuration to their default state
  137. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  138. * the configuration information for CAN module
  139. * Output :
  140. * Author : CWT Data : 2020
  141. **********************************************************************************/
  142. void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
  143. {
  144. if(hcan->Instance==CAN1)
  145. {
  146. /* Reset CAN clock */
  147. System_Module_Disable(EN_CAN1);
  148. GPIO_InitTypeDef GPIO_InitStructure;
  149. /* Initialization GPIO */
  150. /* PA11:Rx */ /* PA12:Tx */
  151. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
  152. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
  153. }
  154. else if(hcan->Instance==CAN2)
  155. {
  156. /* Reset CAN clock */
  157. System_Module_Disable(EN_CAN2);
  158. GPIO_InitTypeDef GPIO_InitStructure;
  159. /* Initialization GPIO */
  160. /* PB5:Rx */ /* PB6:Tx */
  161. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_5);
  162. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
  163. }
  164. }
  165. /*********************************************************************************
  166. * Function : HAL_CAN_Init
  167. * Description : Initializes the CAN peripheral according to the specified parameters in the CAN_HandleTypeDef..
  168. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  169. * the configuration information for CAN module
  170. * Output : HAL status
  171. * Author : CWT Data : 2020
  172. **********************************************************************************/
  173. HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
  174. {
  175. uint8_t InitStatus = HAL_ERROR;
  176. /* Check the parameters */
  177. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR;
  178. if(!IS_CAN_MODE(hcan->Init.CAN_Mode)) return HAL_ERROR;
  179. if(!IS_CAN_SJW(hcan->Init.CAN_SJW)) return HAL_ERROR;
  180. if(!IS_CAN_TSEG1(hcan->Init.CAN_TSEG1)) return HAL_ERROR;
  181. if(!IS_CAN_TSEG2(hcan->Init.CAN_TSEG2)) return HAL_ERROR;
  182. if(!IS_CAN_BRP(hcan->Init.CAN_BRP)) return HAL_ERROR;
  183. if(!IS_CAN_SAM(hcan->Init.CAN_SAM)) return HAL_ERROR;
  184. /* Reset the CANx */
  185. if(hcan->Instance==CAN1)
  186. {
  187. System_Module_Reset(RST_CAN1);
  188. }
  189. else
  190. {
  191. System_Module_Reset(RST_CAN2);
  192. }
  193. HAL_CAN_MspInit(hcan);
  194. HAL_CAN_OperatingModeRequest(hcan,CAN_OperatingMode_Initialization);//enter CAN_OperatingMode_Initialization
  195. hcan->Instance->BTR0=0xff;
  196. hcan->Instance->BTR0=(hcan->Init.CAN_SJW<<6)|(hcan->Init.CAN_BRP);
  197. hcan->Instance->BTR1=(hcan->Init.CAN_SAM<<7)|(hcan->Init.CAN_TSEG2<<4)|(hcan->Init.CAN_TSEG1);
  198. HAL_CAN_OperatingModeRequest(hcan,CAN_OperatingMode_Normal);//enter CAN_OperatingMode_Normal
  199. return HAL_OK;
  200. }
  201. /*********************************************************************************
  202. * Function : HAL_CAN_DeInit
  203. * Description : Deinitializes the CAN peripheral registers to their default
  204. * reset values.
  205. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  206. * the configuration information for CAN module
  207. * Output : HAL status
  208. * Author : CWT Data : 2020
  209. **********************************************************************************/
  210. HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
  211. {
  212. /* Check CAN handle */
  213. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR;
  214. HAL_CAN_MspDeInit(hcan);
  215. /* Reset the CAN peripheral */
  216. SET_BIT(hcan->Instance->MOD, CAN_MOD_RM);
  217. /* Return function status */
  218. return HAL_OK;
  219. }
  220. /*********************************************************************************
  221. * Function : HAL_CAN_Transmit
  222. * Description : Initiates the transmission of a message.
  223. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  224. * the configuration information for CAN module
  225. * Input : TxMessage : ppointer to a structure which contains CAN Id, CAN
  226. * DLC and CAN data.
  227. * Output :
  228. * Author : CWT Data : 2020
  229. **********************************************************************************/
  230. HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef *hcan, CanTxRxMsg* TxMessage)
  231. {
  232. uint8_t i = 0;
  233. uint8_t can_id[4];
  234. uint32_t frame_header;
  235. /* Check the parameters */
  236. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR ;
  237. if(!IS_CAN_IDTYPE(TxMessage->IDE)) return HAL_ERROR;
  238. if(!IS_CAN_RTR(TxMessage->RTR)) return HAL_ERROR;
  239. if(!IS_CAN_DLC(TxMessage->DLC)) return HAL_ERROR;
  240. /* Set up the DLC */
  241. frame_header =TxMessage->DLC & 0x0F; // standard data frame
  242. /* Set up the Id */
  243. if(TxMessage->IDE==CAN_Id_Standard)//Standard ID
  244. {
  245. can_id[0] = TxMessage->StdId >>3;
  246. can_id[1] = (TxMessage->StdId&0x07)<<5;
  247. for(i=0;i<2;i++)
  248. {
  249. hcan->Instance->DF.DATABUF[1+i] = can_id[i];
  250. }
  251. }
  252. else//Id_Extended
  253. {
  254. can_id[0] = TxMessage->ExtId>>21;
  255. can_id[1] = (TxMessage->ExtId&0x1FE000)>>13;
  256. can_id[2] = (TxMessage->ExtId&0x1FE0)>>5;
  257. can_id[3] = (TxMessage->ExtId&0x1F)<<3;
  258. frame_header |= (CAN_Id_Extended<<7); // extended data frame
  259. for(i=0;i<4;i++)
  260. {
  261. hcan->Instance->DF.DATABUF[1+i] = can_id[i];
  262. }
  263. }
  264. if(TxMessage->RTR==CAN_RTR_Data)//CAN_RTR_Data
  265. {
  266. frame_header&=~(CAN_RTR_Remote<<6);
  267. for(i=0; i<TxMessage->DLC; i++)
  268. {
  269. hcan->Instance->DF.DATABUF[3+(TxMessage->IDE*2)+i] = TxMessage->Data[i];
  270. }
  271. }
  272. else//CAN_RTR_Remote
  273. {
  274. frame_header|=(CAN_RTR_Remote<<6);
  275. }
  276. hcan->Instance->DF.DATABUF[0]=frame_header;
  277. hcan->Instance->CMR = CAN_CMR_TR; // transfer request
  278. while((hcan->Instance->SR & CAN_SR_TCS)==0x00); //wait for send ok
  279. return HAL_OK;
  280. }
  281. /*********************************************************************************
  282. * Function : HAL_CAN_CancelTransmit
  283. * Description : Cancels a transmit request.
  284. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  285. * the configuration information for CAN module
  286. * Output :
  287. * Author : CWT Data : 2020
  288. **********************************************************************************/
  289. void HAL_CAN_CancelTransmit(CAN_HandleTypeDef *hcan)
  290. {
  291. /* Check the parameters */
  292. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return ;
  293. /* abort transmission */
  294. hcan->Instance->CMR |= CAN_CMR_AT; //Abort Transmission
  295. }
  296. /*********************************************************************************
  297. * Function : HAL_CAN_Receive
  298. * Description : Receives a message.
  299. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  300. * the configuration information for CAN module
  301. * Input : RxMessage : pointer to a structure receive message which contains
  302. * CAN Id, CAN DLC, CAN datas .
  303. * Output :
  304. * Author : CWT Data : 2020
  305. **********************************************************************************/
  306. HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage)
  307. {
  308. /* Check the parameters */
  309. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR ;
  310. hcan->RxMessage=RxMessage;
  311. /* Enable the CAN Receive interrupt */
  312. hcan->Instance->IER |= CAN_IER_RIE;
  313. NVIC_ClearPendingIRQ(CAN1_IRQn);
  314. NVIC_SetPriority(CAN1_IRQn, 5);
  315. NVIC_EnableIRQ(CAN1_IRQn);
  316. return HAL_OK;
  317. }
  318. /*********************************************************************************
  319. * Function : HAL_CAN_Receive
  320. * Description : Receives a message.
  321. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  322. * the configuration information for CAN module
  323. * Input : RxMessage : pointer to a structure receive message which contains
  324. * CAN Id, CAN DLC, CAN datas .
  325. * Output :
  326. * Author : CWT Data : 2020
  327. **********************************************************************************/
  328. HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage)
  329. {
  330. /* Check the parameters */
  331. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR ;
  332. while(!(hcan->Instance->SR & CAN_SR_RBS));
  333. HAL_CAN_GetRxMessage(hcan, RxMessage);
  334. return HAL_OK;
  335. }
  336. void HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, CanTxRxMsg* RxMessage)
  337. {
  338. uint8_t i=0;
  339. /* Check the parameters */
  340. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return ;
  341. if(0 == (hcan->Instance->SR & CAN_SR_RBS) ) return; // receive fifo not empty
  342. /* Get the IDE */
  343. RxMessage->IDE = (uint8_t)(0x80 & hcan->Instance->DF.DATABUF[0])>>7;
  344. /* Get the RTR */
  345. RxMessage->RTR = (uint8_t)(0x40 & hcan->Instance->DF.DATABUF[0])>>6;
  346. /* Get the DLC */
  347. RxMessage->DLC = (uint8_t)0x0F & hcan->Instance->DF.DATABUF[0];
  348. if (RxMessage->IDE == CAN_Id_Standard)
  349. {
  350. RxMessage->StdId = (uint32_t)(( hcan->Instance->DF.DATABUF[1]<<8) | hcan->Instance->DF.DATABUF[2])>>5;;
  351. for(i=0; i<RxMessage->DLC; i++)
  352. {
  353. RxMessage->Data[i] = hcan->Instance->DF.DATABUF[3+i];
  354. }
  355. }
  356. else
  357. {
  358. RxMessage->ExtId = (uint32_t)(( hcan->Instance->DF.DATABUF[1]<<24) | ( hcan->Instance->DF.DATABUF[2]<<16) | ( hcan->Instance->DF.DATABUF[3]<<8) | (hcan->Instance->DF.DATABUF[4] ))>>3;;
  359. for(i=0; i<RxMessage->DLC; i++)
  360. {
  361. RxMessage->Data[i] = hcan->Instance->DF.DATABUF[5+i];
  362. }
  363. }
  364. /* Release the FIFO */
  365. hcan->Instance->CMR |= CAN_CMR_RRB; //Release Receive Buffer
  366. }
  367. /**
  368. * @brief Initializes the CAN peripheral according to the specified
  369. * parameters in the CAN_FilterInitStruct.
  370. * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
  371. CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef
  372. * structure that contains the configuration
  373. * information.
  374. * @retval None.
  375. */
  376. /*********************************************************************************
  377. * Function : HAL_CAN_ConfigFilter
  378. * Description : Initializes the CAN peripheral according to the specified parameters in the CAN_FilterInitStruct.
  379. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  380. * the configuration information for CAN module
  381. * Input : CAN_FilterInitStruct : pointer to a CAN_FilterInitTypeDef structure that contains the configuration
  382. * information.
  383. * Output :
  384. * Author : CWT Data : 2020
  385. **********************************************************************************/
  386. void HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan,CAN_FilterInitTypeDef* CAN_FilterInitStruct)
  387. {
  388. HAL_CAN_OperatingModeRequest(hcan,CAN_OperatingMode_Initialization);//enter CAN_OperatingMode_Initialization
  389. /* Filter Mode */
  390. if (CAN_FilterInitStruct->CAN_FilterMode ==CAN_FilterMode_Dual) /*Dual mode*/
  391. {
  392. hcan->Instance->MOD &= ~CAN_MOD_AFM;
  393. /*Dual mode ACR set*/
  394. hcan->Instance->DF.FILTER.ACR[0] = (CAN_FilterInitStruct->CAN_FilterId1&0x1FE00000)>>21; /*Dual mode ACR0=ID28...ID21 of ID1*/
  395. hcan->Instance->DF.FILTER.ACR[1] = (CAN_FilterInitStruct->CAN_FilterId1&0x1FE000)>>13; /*Dual mode ACR0=ID20...ID13 of ID1*/
  396. hcan->Instance->DF.FILTER.ACR[2] = (CAN_FilterInitStruct->CAN_FilterId2&0x1FE00000)>>21; /*Dual mode ACR0=ID28...ID21 of ID2*/
  397. hcan->Instance->DF.FILTER.ACR[3] = (CAN_FilterInitStruct->CAN_FilterId2&0x1FE000)>>13; /*Dual mode ACR0=ID20...ID13 of ID2*/
  398. /*Dual mode AMR set*/
  399. hcan->Instance->DF.FILTER.AMR[0] = (CAN_FilterInitStruct->CAN_FilterMaskId1)>>24;
  400. hcan->Instance->DF.FILTER.AMR[1] = (CAN_FilterInitStruct->CAN_FilterMaskId1&0xFF0000)>>16;
  401. hcan->Instance->DF.FILTER.AMR[2] = (CAN_FilterInitStruct->CAN_FilterMaskId2)>>24;
  402. hcan->Instance->DF.FILTER.AMR[3] = (CAN_FilterInitStruct->CAN_FilterMaskId2&0xFF0000)>>16;
  403. }
  404. else /*Single mode*/
  405. {
  406. hcan->Instance->MOD |= CAN_MOD_AFM;
  407. /*Single mode ACR set*/
  408. hcan->Instance->DF.FILTER.ACR[0] = (CAN_FilterInitStruct->CAN_FilterId1&0x1FE00000)>>21; /*Single mode ACR0=ID28...ID21*/
  409. hcan->Instance->DF.FILTER.ACR[1] = (CAN_FilterInitStruct->CAN_FilterId1&0x1FE000)>>13; /*Single mode ACR1=ID20...ID13*/
  410. hcan->Instance->DF.FILTER.ACR[2] = (CAN_FilterInitStruct->CAN_FilterId1&0x1FE0)>>5; /*Single mode ACR2=ID12...ID5*/
  411. hcan->Instance->DF.FILTER.ACR[3] = (CAN_FilterInitStruct->CAN_FilterId1&0x1F)<<3; /*Single mode ACR3=ID4...ID0*/
  412. /*Single mode AMR set*/
  413. hcan->Instance->DF.FILTER.AMR[0] = (CAN_FilterInitStruct->CAN_FilterMaskId1)>>24;
  414. hcan->Instance->DF.FILTER.AMR[1] = (CAN_FilterInitStruct->CAN_FilterMaskId1&0xFF0000)>>16;
  415. hcan->Instance->DF.FILTER.AMR[2] = (CAN_FilterInitStruct->CAN_FilterMaskId1&0xFF00)>>8;
  416. hcan->Instance->DF.FILTER.AMR[3] = (CAN_FilterInitStruct->CAN_FilterMaskId1&0xFF);
  417. }
  418. HAL_CAN_OperatingModeRequest(hcan,CAN_OperatingMode_Normal);//enter CAN_OperatingMode_Initialization
  419. }
  420. /*********************************************************************************
  421. * Function : HAL_CAN_Sleep
  422. * Description : Enters the sleep mode.
  423. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  424. * the configuration information for CAN module
  425. * Output :
  426. * Author : CWT Data : 2020
  427. **********************************************************************************/
  428. HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef *hcan)
  429. {
  430. HAL_StatusTypeDef status;
  431. /* Check the parameters */
  432. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR;
  433. /* Request Sleep mode */
  434. hcan->Instance->MOD |= CAN_MOD_SM; //Enter Sleep Mode
  435. /* Sleep mode status */
  436. if ((hcan->Instance->MOD & CAN_MOD_SM) == CAN_MOD_SM)
  437. {
  438. /* Sleep mode entered */
  439. status= HAL_OK;
  440. }else
  441. {
  442. status=HAL_ERROR;
  443. }
  444. /* return sleep mode status */
  445. return status;
  446. }
  447. /*********************************************************************************
  448. * Function : HAL_CAN_WakeUp
  449. * Description : Wakes the CAN up.
  450. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  451. * the configuration information for CAN module
  452. * Output :
  453. * Author : CWT Data : 2020
  454. **********************************************************************************/
  455. HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
  456. {
  457. HAL_StatusTypeDef status;
  458. /* Check the parameters */
  459. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return HAL_ERROR;
  460. /* sleep wake mode */
  461. hcan->Instance->MOD &=~ CAN_MOD_SM; //Enter Sleep Mode
  462. /* sleep wake status */
  463. if ((hcan->Instance->MOD & CAN_MOD_SM)== CAN_MOD_SM)
  464. {
  465. /* sleep wake not entered */
  466. status= HAL_ERROR;
  467. }else
  468. {
  469. status=HAL_OK;
  470. }
  471. /* return sleep mode status */
  472. return status;
  473. }
  474. /*********************************************************************************
  475. * Function : HAL_CAN_GetTransmitErrorCounter
  476. * Description : Returns the CANx Transmit Error Counter(TXERR).
  477. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  478. * the configuration information for CAN module
  479. * Output :
  480. * Author : CWT Data : 2020
  481. **********************************************************************************/
  482. int8_t HAL_CAN_GetTransmitErrorCounter(CAN_HandleTypeDef *hcan)
  483. {
  484. uint8_t counter=0;
  485. /* Check the parameters */
  486. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  487. /* Get the CANx Transmit Error Counter(TXERR) */
  488. counter = (uint8_t)(hcan->Instance->TXERR);
  489. /* Return the CANx Transmit Error Counter(TXERR) */
  490. return counter;
  491. }
  492. /*********************************************************************************
  493. * Function : HAL_CAN_GetReceiveErrorCounter
  494. * Description : Returns the CANx Receive Error Counter(RXERR).
  495. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  496. * the configuration information for CAN module
  497. * Output :
  498. * Author : CWT Data : 2020
  499. **********************************************************************************/
  500. int8_t HAL_CAN_GetReceiveErrorCounter(CAN_HandleTypeDef *hcan)
  501. {
  502. uint8_t counter=0;
  503. /* Check the parameters */
  504. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  505. /* Get the CANx Receive Error Counter(RXERR) */
  506. counter = (uint8_t)(hcan->Instance->RXERR);
  507. /* Return the CANx Receive Error Counter(RXERR) */
  508. return counter;
  509. }
  510. /*********************************************************************************
  511. * Function : HAL_CAN_GetErrorCode
  512. * Description : Returns the CANx's error code (ECC).
  513. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  514. * the configuration information for CAN module
  515. * Input : Error_Type:This parameter can be one of the following flags:
  516. * CAN_ErrorType_SegCode
  517. * CAN_ErrorType_Direction
  518. * CAN_ErrorType_ErrCode
  519. * Output :
  520. * Author : CWT Data : 2020
  521. **********************************************************************************/
  522. int8_t HAL_CAN_GetErrorCode(CAN_HandleTypeDef *hcan,uint32_t Error_Type)
  523. {
  524. uint8_t ErrorCode=0;
  525. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  526. if(!IS_CAN_ErrorType(Error_Type)) return -1;
  527. /* Get the CANx Error SegCode */
  528. if(Error_Type==CAN_ErrorType_SegCode)
  529. {
  530. ErrorCode= (uint8_t)(hcan->Instance->ECC & CAN_ErrorType_SegCode);
  531. }
  532. /* Get the CANx Error Direction */
  533. else if(Error_Type==CAN_ErrorType_Direction)
  534. {
  535. ErrorCode= (uint8_t)((hcan->Instance->ECC & CAN_ErrorType_Direction)>>5);
  536. }
  537. /* Get the CANx Error ErrCode */
  538. else
  539. {
  540. ErrorCode= (uint8_t)((hcan->Instance->ECC & CAN_ErrorType_ErrCode)>>6);
  541. }
  542. return ErrorCode;
  543. }
  544. /*********************************************************************************
  545. * Function : HAL_CAN_GetErrorAlarmCounter
  546. * Description : Returns the CANx Error Alarm Counter(EWLR).
  547. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  548. * the configuration information for CAN module
  549. * Output :
  550. * Author : CWT Data : 2020
  551. **********************************************************************************/
  552. int8_t HAL_CAN_GetErrorAlarmCounter(CAN_HandleTypeDef *hcan)
  553. {
  554. uint8_t counter=0;
  555. /* Check the parameters */
  556. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  557. /* Get the CANx Error Alarm Counter(EWLR) */
  558. counter = (uint8_t)(hcan->Instance->EWLR);
  559. /* Return the CANx Error Alarm Counter(EWLR) */
  560. return counter;
  561. }
  562. /*********************************************************************************
  563. * Function : HAL_CAN_GetArbitrationErrorPosition
  564. * Description : Returns the CANx Arbitration Error Position(ALC).
  565. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  566. * the configuration information for CAN module
  567. * Output :
  568. * Author : CWT Data : 2020
  569. **********************************************************************************/
  570. int8_t HAL_CAN_GetArbitrationErrorPosition(CAN_HandleTypeDef *hcan)
  571. {
  572. uint8_t position=0;
  573. /* Check the parameters */
  574. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  575. /* Get the CANx Arbitration Error Counter(ALC) */
  576. position = (uint8_t)((hcan->Instance->ALC)+1);
  577. /* Return the CANx Arbitration Error Counter(ALC) */
  578. return position;
  579. }
  580. /*********************************************************************************
  581. * Function : HAL_CAN_GetReceiveFiFoCounter
  582. * Description : Returns the CANx Receive FiFo Counter(RMC).
  583. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  584. * the configuration information for CAN module
  585. * Output :
  586. * Author : CWT Data : 2020
  587. **********************************************************************************/
  588. int8_t HAL_CAN_GetReceiveFiFoCounter(CAN_HandleTypeDef *hcan)
  589. {
  590. uint8_t counter=0;
  591. /* Check the parameters */
  592. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  593. /* Get the CANx Receive FiFo Counter(RMC) */
  594. counter = (uint8_t)(hcan->Instance->RMC);
  595. /* Return the CANx Receive FiFo Counter(RMC) */
  596. return counter;
  597. }
  598. /*********************************************************************************
  599. * Function : HAL_CAN_GetReceiveFiFoAddr
  600. * Description : Returns the CANx Receive FiFo start address(RBSA).
  601. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  602. * the configuration information for CAN module
  603. * Output :
  604. * Author : CWT Data : 2020
  605. **********************************************************************************/
  606. int8_t HAL_CAN_GetReceiveFiFoAddr(CAN_HandleTypeDef *hcan)
  607. {
  608. uint8_t addr=0;
  609. /* Check the parameters */
  610. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return -1;
  611. /* Get the CANx Receive FiFo start address(RBSA) */
  612. addr = (uint8_t)(hcan->Instance->RBSA);
  613. /* Return the CANx Receive FiFo start address(RBSA) */
  614. return addr;
  615. }
  616. /*********************************************************************************
  617. * Function : HAL_CAN_ReleaseReceiveFIFO
  618. * Description : Releases the Receive FIFO.
  619. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  620. * the configuration information for CAN module
  621. * Output :
  622. * Author : CWT Data : 2020
  623. **********************************************************************************/
  624. void HAL_CAN_ReleaseReceiveFIFO(CAN_HandleTypeDef *hcan)
  625. {
  626. /* Check the parameters */
  627. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return;
  628. /* Releases the Receive FIFO. */
  629. hcan->Instance->CMR|=CAN_CMR_RRB;
  630. }
  631. /*********************************************************************************
  632. * Function : HAL_CAN_ClearOverload
  633. * Description : Clear Overload
  634. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  635. * the configuration information for CAN module
  636. * Output :
  637. * Author : CWT Data : 2020
  638. **********************************************************************************/
  639. void HAL_CAN_ClearOverload(CAN_HandleTypeDef *hcan)
  640. {
  641. /* Check the parameters */
  642. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return;
  643. /* Clear Overload. */
  644. hcan->Instance->CMR|=CAN_CMR_CDO;
  645. }
  646. /*********************************************************************************
  647. * Function : HAL_CAN_SlefReceive
  648. * Description : Slef Receive
  649. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  650. * the configuration information for CAN module
  651. * Output :
  652. * Author : CWT Data : 2020
  653. **********************************************************************************/
  654. void HAL_CAN_SelfReceive(CAN_HandleTypeDef *hcan)
  655. {
  656. /* Check the parameters */
  657. if(!IS_CAN_ALL_PERIPH(hcan->Instance)) return;
  658. /* Slef Receive. */
  659. hcan->Instance->CMR|=CAN_CMR_SRR;
  660. while((hcan->Instance->SR & CAN_SR_TCS)==0x00); //wait for send ok
  661. }
  662. /*********************************************************************************
  663. * Function : HAL_CAN_IRQHandler
  664. * Description : This function handles CAN interrupt request.
  665. * Input : hdma : pointer to a CAN_HandleTypeDef structure that contains
  666. * the configuration information for CAN module
  667. * Outpu :
  668. * Author : Chris_Kyle Data : 2020
  669. **********************************************************************************/
  670. void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
  671. {
  672. volatile uint32_t lu32_IR;
  673. lu32_IR = hcan->Instance->IR;//read clear
  674. if(lu32_IR & CAN_IR_RI) //RI
  675. {
  676. /* CAN ReceiveIT complete callback */
  677. HAL_CAN_GetRxMessage(hcan, hcan->RxMessage);
  678. hcan->CAN_ReceiveIT_Callback(hcan);
  679. }
  680. if(lu32_IR & CAN_IR_TI) //TI
  681. {
  682. /* CAN TransmitIT complete callback */
  683. hcan->CAN_TransmitIT_Callback(hcan);
  684. }
  685. }