1
0

HAL_UART_EX.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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)&&(Lin_Id !=0x3C && Lin_Id!=0x3D))
  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 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)&&(Lin_Id !=0x3C && Lin_Id!=0x3D))
  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 ucI,RxSize;
  105. uint8_t Lin_Rx_Buf[16];
  106. uint16_t Lin_Check_Sum = 0;
  107. if(pData == 0)
  108. return 0;
  109. huart->Instance->CR = 0x0201; //disable uart_tx
  110. huart->Instance->ICR = 0xfff; //clear int
  111. huart->Instance->LCRH = 0x70; //8 data bit,1 stop bit,0 verify bit,enable FIFO
  112. huart->Instance->IFLS = 0x12; //FIFO send and receive number is 8
  113. huart->Instance->IE = 0x00; //Disable all interrupt
  114. HAL_UART_Receive(huart, Lin_Rx_Buf, sizeof(Lin_Rx_Buf), Timeout);
  115. if((Lin_Version==UART_LIN_V2DX)&&(Lin_Id !=0x3C && Lin_Id!=0x3D))
  116. Lin_Check_Sum = Lin_Id; //LIN 2.X check sum calc with PID.
  117. if(huart->lu32_RxCount)
  118. {
  119. for(ucI=0;ucI<(huart->lu32_RxCount-1);ucI++)
  120. {
  121. Lin_Check_Sum += Lin_Rx_Buf[ucI];
  122. if(Lin_Check_Sum>0xFF)
  123. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  124. }
  125. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  126. if((uint8_t)Lin_Check_Sum == Lin_Rx_Buf[ucI])
  127. {
  128. RxSize = huart->lu32_RxCount;
  129. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  130. }
  131. else
  132. RxSize = 0xFF;
  133. }
  134. else
  135. RxSize = 0;
  136. return RxSize;
  137. }
  138. /************************************************************************
  139. * function : HAL_UART_LIN_Slave_Receive
  140. * Description: Uart lin slave receive head
  141. * input :none
  142. * UART_HandleTypeDef *huart: Serial port number
  143. * uint8_t Lin_Version: LIN version ,should be UART_LIN_V1D3 or UART_LIN_V2DX.
  144. * uint8_t *pData: point to the data buffer.
  145. * return: uint8_t RxSize
  146. ************************************************************************/
  147. uint8_t HAL_UART_LIN_Slave_Receive(UART_HandleTypeDef *huart, uint8_t Lin_Version, uint8_t *pData, uint32_t Timeout)
  148. {
  149. uint8_t ucI,RxSize;
  150. uint8_t Lin_Rx_Buf[16];
  151. uint16_t Lin_Check_Sum = 0;
  152. uint32_t u32_Timeout;
  153. if(pData == 0)
  154. return 0;
  155. huart->Instance->CR = 0x0201; //disable uart_tx
  156. huart->Instance->ICR = 0xfff; //clear int
  157. CLEAR_BIT(huart->Instance->IE, UART_EX_IE_LBDI); //Disable LBDI int
  158. if (Timeout == 0)
  159. {
  160. while(!READ_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI));
  161. }
  162. else
  163. {
  164. u32_Timeout = Timeout * 0xFF;
  165. while(!READ_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI))
  166. {
  167. if (u32_Timeout-- == 0)
  168. {
  169. return 0;
  170. }
  171. }
  172. }
  173. CLEAR_BIT(huart->Instance->RIS, UART_EX_RIS_LBDI);
  174. huart->Instance->LCRH = 0x70; //8 data bit,1 stop bit,0 verify bit,enable FIFO
  175. huart->Instance->IFLS = 0x12; //FIFO send and receive number is 8
  176. huart->Instance->IE = 0x00; //Disable all interrupt
  177. HAL_UART_Receive(huart, Lin_Rx_Buf, sizeof(Lin_Rx_Buf), Timeout); //waitting rx completed.
  178. if(huart->lu32_RxCount > 3)
  179. {
  180. if((Lin_Version==UART_LIN_V2DX)&&(Lin_Rx_Buf[2] !=0x3C && Lin_Rx_Buf[2]!=0x3D))
  181. Lin_Check_Sum = Lin_Rx_Buf[2]; //LIN 2.X check sum calc with PID.
  182. if(huart->lu32_RxCount)
  183. {
  184. for(ucI=3;ucI<(huart->lu32_RxCount-1);ucI++)
  185. {
  186. Lin_Check_Sum += Lin_Rx_Buf[ucI];
  187. if(Lin_Check_Sum>0xFF)
  188. Lin_Check_Sum = ((Lin_Check_Sum>>8)+Lin_Check_Sum)&0xFF;
  189. }
  190. Lin_Check_Sum = (~Lin_Check_Sum) & 0xFF;
  191. if((uint8_t)Lin_Check_Sum == Lin_Rx_Buf[ucI])
  192. {
  193. RxSize = huart->lu32_RxCount;
  194. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  195. }
  196. else
  197. RxSize = 0xFF;
  198. }
  199. }
  200. else if(huart->lu32_RxCount<=3)
  201. {
  202. RxSize = huart->lu32_RxCount;
  203. memcpy(pData, (uint8_t*)Lin_Rx_Buf, RxSize);
  204. }
  205. else
  206. RxSize = 0;
  207. return RxSize;
  208. }