HAL_CAN.c 28 KB

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