1
0

HAL_UART_EX.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ******************************************************************************
  3. * @file HAL_UART_EX.c
  4. * @version V1.0.0
  5. * @date 2021
  6. * @brief LIN HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the extensional module: Local Interconnect Network Peripheral (LIN).
  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_UART_LIN_Master_Transmit
  17. * Description: Uart lin master transmit data
  18. * input :none
  19. * UART_HandleTypeDef *huart: Serial port number
  20. * uint8_t Lin_Version: LIN version ,should be UART_LIN_V1D3 or UART_LIN_V2DX.
  21. * uint8_t Lin_Id: LIN id
  22. * uint8_t *pData: point to the transmit data buffer.
  23. * uint8_t Size: Transmit buffer Size.
  24. * return: none
  25. ************************************************************************/
  26. void HAL_UART_LIN_Master_Transmit(UART_HandleTypeDef *huart, uint8_t Lin_Version, uint8_t Lin_Id, uint8_t *pData, uint8_t Size)
  27. {
  28. uint8_t Lin_P0,Lin_P1,ucI;
  29. uint16_t Lin_Check_Sum = 0;
  30. if((Size>8)||(pData == 0))
  31. return;
  32. CLEAR_BIT(huart->Instance->IE, UART_EX_IE_LBDI);
  33. huart->Instance->CR = 0x0101; //disable uart_rx
  34. MODIFY_REG(huart->Instance->BCNT, UART_EX_BCNT_VALUE_MASK, (13)<<UART_EX_BCNT_VALUE_POS);
  35. //Transmit break field.
  36. SET_BIT(huart->Instance->BCNT, UART_EX_BCNT_START);
  37. SET_BIT(huart->Instance->LCRH, UART_LCRH_BRK);
  38. while(!(READ_BIT(huart->Instance->RIS, UART_EX_RIS_BCNTI))){} //Check BCNTI.
  39. CLEAR_BIT(huart->Instance->LCRH, UART_LCRH_BRK);
  40. HAL_UART_Transmit(huart, (uint8_t*)"\x55", 1, 0); //Transmit sync field
  41. Lin_Id &= 0x3F; //Lin address check, 0-63.
  42. Lin_P0 = (Lin_Id^(Lin_Id>>1)^(Lin_Id>>2)^(Lin_Id>>4))&0x01; //P0 = ID0^ID1^ID2^ID4
  43. Lin_P1 = (~((Lin_Id>>1)^(Lin_Id>>3)^(Lin_Id>>4)^(Lin_Id>>5)))&0x01; //P1 = ~(ID1^ID3^ID4^ID5)
  44. Lin_Id = Lin_Id | (Lin_P0<<6) | (Lin_P1<<7);
  45. HAL_UART_Transmit(huart, &Lin_Id, 1, 0); //Transmit pid field
  46. if(Lin_Version==UART_LIN_V2DX)
  47. Lin_Check_Sum = Lin_Id; //LIN 2.X check sum calc with PID.
  48. if(Size)
  49. {
  50. for(ucI=0;ucI<Size;ucI++)
  51. {
  52. Lin_Check_Sum += pData[ucI];
  53. if(Lin_Check_Sum>0xFF)
  54. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  55. }
  56. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  57. HAL_UART_Transmit(huart, pData, Size, 0); //Transmit data field
  58. HAL_UART_Transmit(huart, (uint8_t*)&Lin_Check_Sum, 1, 0); //Transmit Lin_Check_Sum field
  59. }
  60. }
  61. /************************************************************************
  62. * function : HAL_UART_LIN_Slave_Transmit
  63. * Description: Uart lin slave transmit data
  64. * input :none
  65. * UART_HandleTypeDef *huart: Serial port number
  66. * uint8_t Lin_Version: LIN version ,should be UART_LIN_V1D3 or UART_LIN_V2DX.
  67. * uint8_t Lin_Id: LIN id
  68. * uint8_t *pData: point to the transmit data buffer.
  69. * uint8_t Size: Transmit buffer Size.
  70. * return: none
  71. ************************************************************************/
  72. void HAL_UART_LIN_Slave_Transmit(UART_HandleTypeDef *huart, uint8_t Lin_Version, uint8_t Lin_Id, uint8_t *pData, uint8_t Size)
  73. {
  74. uint8_t Lin_P0,Lin_P1,ucI;
  75. uint16_t Lin_Check_Sum = 0;
  76. if((Size>8)||(pData == 0))
  77. return;
  78. CLEAR_BIT(huart->Instance->IE, UART_EX_IE_LBDI);//disable LBDI int
  79. huart->Instance->CR = 0x0101; //disable uart_rx
  80. if(Lin_Version==UART_LIN_V2DX)
  81. Lin_Check_Sum = Lin_Id; //LIN 2.X check sum calc with PID.
  82. for(ucI=0;ucI<Size;ucI++)
  83. {
  84. Lin_Check_Sum += pData[ucI];
  85. if(Lin_Check_Sum>0xFF)
  86. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  87. }
  88. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  89. HAL_UART_Transmit(huart, pData, Size, 0); //Transmit data field
  90. HAL_UART_Transmit(huart, (uint8_t*)&Lin_Check_Sum, 1, 0); //Transmit Lin_Check_Sum field
  91. }
  92. /************************************************************************
  93. * function : HAL_UART_LIN_Master_Receive
  94. * Description: Uart lin master receive data
  95. * input :none
  96. * UART_HandleTypeDef *huart: Serial port number
  97. * uint8_t Lin_Version: LIN version ,should be UART_LIN_V1D3 or UART_LIN_V2DX.
  98. * uint8_t Lin_Id: LIN id
  99. * uint8_t *pData: point to the data buffer.
  100. * return: uint8_t RxSize
  101. ************************************************************************/
  102. uint8_t HAL_UART_LIN_Master_Receive(UART_HandleTypeDef *huart, uint8_t Lin_Version, uint8_t Lin_Id, uint8_t *pData, uint32_t Timeout)
  103. {
  104. uint8_t Lin_P0,Lin_P1,ucI,RxSize;
  105. uint8_t Lin_Rx_Buf[16];
  106. uint8_t ucStatus;
  107. uint16_t Lin_Check_Sum = 0;
  108. uint32_t u32_Timeout;
  109. if(pData == 0)
  110. return 0;
  111. huart->Instance->CR = 0x0201; //disable uart_tx
  112. huart->Instance->ICR = 0xfff; //clear int
  113. huart->Instance->LCRH = 0x70; //8 data bit,1 stop bit,0 verify bit,enable FIFO
  114. huart->Instance->IFLS = 0x12; //FIFO send and receive number is 8
  115. huart->Instance->IE = 0x00; //Disable all interrupt
  116. HAL_UART_Receive(huart, Lin_Rx_Buf, sizeof(Lin_Rx_Buf), Timeout);
  117. if(Lin_Version==UART_LIN_V2DX)
  118. Lin_Check_Sum = Lin_Id; //LIN 2.X check sum calc with PID.
  119. if(huart->lu32_RxCount)
  120. {
  121. for(ucI=0;ucI<(huart->lu32_RxCount-1);ucI++)
  122. {
  123. Lin_Check_Sum += Lin_Rx_Buf[ucI];
  124. if(Lin_Check_Sum>0xFF)
  125. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  126. }
  127. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  128. if((uint8_t)Lin_Check_Sum == Lin_Rx_Buf[ucI])
  129. {
  130. RxSize = huart->lu32_RxCount;
  131. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  132. }
  133. else
  134. RxSize = 0xFF;
  135. }
  136. else
  137. RxSize = 0;
  138. return RxSize;
  139. }
  140. /************************************************************************
  141. * function : HAL_UART_LIN_Slave_Receive
  142. * Description: Uart lin slave receive head
  143. * input :none
  144. * UART_HandleTypeDef *huart: Serial port number
  145. * uint8_t Lin_Version: LIN version ,should be UART_LIN_V1D3 or UART_LIN_V2DX.
  146. * uint8_t *pData: point to the data buffer.
  147. * return: uint8_t RxSize
  148. ************************************************************************/
  149. uint8_t HAL_UART_LIN_Slave_Receive(UART_HandleTypeDef *huart, uint8_t Lin_Version, uint8_t *pData, uint32_t Timeout)
  150. {
  151. uint8_t Lin_P0,Lin_P1,ucI,RxSize;
  152. uint8_t Lin_Rx_Buf[16];
  153. uint8_t ucStatus;
  154. uint16_t Lin_Check_Sum = 0;
  155. uint32_t u32_Timeout;
  156. if(pData == 0)
  157. return 0;
  158. huart->Instance->CR = 0x0201; //disable uart_tx
  159. huart->Instance->ICR = 0xfff; //clear int
  160. CLEAR_BIT(huart->Instance->IE, UART_EX_IE_LBDI); //Disable LBDI int
  161. if (Timeout == 0)
  162. {
  163. while(!READ_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI));
  164. }
  165. else
  166. {
  167. u32_Timeout = Timeout * 0xFF;
  168. while(!READ_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI))
  169. {
  170. if (u32_Timeout-- == 0)
  171. {
  172. return 0;
  173. }
  174. }
  175. }
  176. CLEAR_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI);
  177. huart->Instance->LCRH = 0x70; //8 data bit,1 stop bit,0 verify bit,enable FIFO
  178. huart->Instance->IFLS = 0x12; //FIFO send and receive number is 8
  179. huart->Instance->IE = 0x00; //Disable all interrupt
  180. HAL_UART_Receive(huart, Lin_Rx_Buf, sizeof(Lin_Rx_Buf), Timeout); //waitting rx completed.
  181. if(huart->lu32_RxCount > 3)
  182. {
  183. if(Lin_Version==UART_LIN_V2DX)
  184. Lin_Check_Sum = Lin_Rx_Buf[2]; //LIN 2.X check sum calc with PID.
  185. if(huart->lu32_RxCount)
  186. {
  187. for(ucI=3;ucI<(huart->lu32_RxCount-1);ucI++)
  188. {
  189. Lin_Check_Sum += Lin_Rx_Buf[ucI];
  190. if(Lin_Check_Sum>0xFF)
  191. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  192. }
  193. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  194. if((uint8_t)Lin_Check_Sum == Lin_Rx_Buf[ucI])
  195. {
  196. RxSize = huart->lu32_RxCount;
  197. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  198. }
  199. else
  200. RxSize = 0xFF;
  201. }
  202. }
  203. else if(huart->lu32_RxCount<=3)
  204. {
  205. RxSize = huart->lu32_RxCount;
  206. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  207. }
  208. else
  209. RxSize = 0;
  210. return RxSize;
  211. }