hpm_lin_drv.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_LIN_DRV_H
  8. #define HPM_LIN_DRV_H
  9. #include <math.h>
  10. #include "hpm_common.h"
  11. #include "hpm_lin_regs.h"
  12. #include "hpm_soc_feature.h"
  13. /** bit4 and bit5 encode data length in ID */
  14. #define LIN_ID_DATA_LEN_SHIFT 4U
  15. #define LIN_ID_DATA_LEN_MASK 0x30U
  16. #define LIN_ID_DATA_LEN_GET(x) (((uint8_t)(x) & LIN_ID_DATA_LEN_MASK) >> LIN_ID_DATA_LEN_SHIFT)
  17. /**
  18. * @brief LIN driver APIs
  19. * @defgroup lin_interface LIN driver APIs
  20. * @ingroup io_interfaces
  21. * @{
  22. */
  23. /**
  24. * @brief data length in ID bit4 and bit5
  25. */
  26. typedef enum {
  27. id_data_length_2bytes,
  28. id_data_length_2bytes_2, /**< both 0 and 1 represent 2 bytes */
  29. id_data_length_4bytes,
  30. id_data_length_8bytes
  31. } lin_id_data_length_t;
  32. /**
  33. * @brief bus inactivity tome to go to sleep
  34. */
  35. typedef enum {
  36. bus_inactivity_time_4s,
  37. bus_inactivity_time_6s,
  38. bus_inactivity_time_8s,
  39. bus_inactivity_time_10s
  40. } lin_bus_inactivity_time_t;
  41. /**
  42. * @brief wakeup repeat time
  43. */
  44. typedef enum {
  45. wakeup_repeat_time_180ms,
  46. wakeup_repeat_time_200ms,
  47. wakeup_repeat_time_220ms,
  48. wakeup_repeat_time_240ms
  49. } lin_wakeup_repeat_time_t;
  50. typedef struct {
  51. uint32_t src_freq_in_hz; /**< Source clock frequency in Hz */
  52. uint32_t baudrate; /**< Baudrate */
  53. } lin_timing_t;
  54. /**
  55. * @brief LIN config
  56. */
  57. typedef struct {
  58. uint8_t id; /**< ID */
  59. uint8_t *data_buff; /**< data buff */
  60. bool data_length_from_id; /**< data length should be decoded from the identifier or not) */
  61. uint8_t data_length; /**< used when data_length_from_id is false */
  62. bool enhanced_checksum; /**< true for enhanced checksum; false for classic checksum */
  63. bool transmit; /**< true for transmit operation; false for receive operation */
  64. /* bool start; */ /**< true for start operation; false for only configuration */
  65. } lin_trans_config_t;
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. /**
  70. * @brief lin get STATE register value
  71. *
  72. * @param [in] ptr lin base address
  73. * @return uint8_t STATE register value
  74. */
  75. static inline uint8_t lin_get_status(LIN_Type *ptr)
  76. {
  77. return ptr->STATE;
  78. }
  79. /**
  80. * @brief lin reset interrupt
  81. *
  82. * @param ptr lin base address
  83. */
  84. static inline void lin_reset_interrupt(LIN_Type *ptr)
  85. {
  86. ptr->CONTROL |= LIN_CONTROL_RESET_INT_MASK;
  87. }
  88. /**
  89. * @brief lin reset error
  90. *
  91. * @param ptr lin base address
  92. */
  93. static inline void lin_reset_error(LIN_Type *ptr)
  94. {
  95. ptr->CONTROL |= LIN_CONTROL_RESET_ERROR_MASK;
  96. }
  97. /**
  98. * @brief lin wakeup
  99. *
  100. * @param ptr lin base address
  101. */
  102. static inline void lin_wakeup(LIN_Type *ptr)
  103. {
  104. ptr->CONTROL |= LIN_CONTROL_WAKEUP_REQ_MASK;
  105. }
  106. /**
  107. * @brief lin sleep
  108. *
  109. * @param ptr lin base address
  110. */
  111. static inline void lin_sleep(LIN_Type *ptr)
  112. {
  113. ptr->CONTROL |= LIN_CONTROL_SLEEP_MASK;
  114. }
  115. /**
  116. * @brief lin slave stop
  117. *
  118. * @param ptr lin base address
  119. */
  120. static inline void lin_slave_stop(LIN_Type *ptr)
  121. {
  122. ptr->CONTROL |= LIN_CONTROL_STOP_MASK;
  123. }
  124. /**
  125. * @brief lin slave ack
  126. *
  127. * @param ptr lin base address
  128. */
  129. static inline void lin_slave_ack(LIN_Type *ptr)
  130. {
  131. ptr->CONTROL |= LIN_CONTROL_DATA_ACK_MASK;
  132. }
  133. /**
  134. * @brief lin slave set bus inactivity time
  135. *
  136. * @param ptr lin base address
  137. * @param time lin_bus_inactivity_time_t
  138. */
  139. static inline void lin_slave_set_bus_inactivity_time(LIN_Type *ptr, lin_bus_inactivity_time_t time)
  140. {
  141. ptr->TV |= LIN_TV_BUS_INACTIVITY_TIME_SET(time);
  142. }
  143. /**
  144. * @brief lin slave set wakeup repeat time
  145. *
  146. * @param ptr lin base address
  147. * @param time lin_wakeup_repeat_time_t
  148. */
  149. static inline void lin_slave_set_wakeup_repeat_time(LIN_Type *ptr, lin_wakeup_repeat_time_t time)
  150. {
  151. ptr->TV |= LIN_TV_WUP_REPEAT_TIME_SET(time);
  152. }
  153. /**
  154. * @brief lin set mode
  155. *
  156. * @param ptr lin base address
  157. * @param master true for master mode, false for slave mode
  158. */
  159. static inline void lin_set_mode(LIN_Type *ptr, bool master)
  160. {
  161. if (master) {
  162. ptr->TV |= LIN_TV_MASTER_MODE_MASK;
  163. } else {
  164. ptr->TV &= ~LIN_TV_MASTER_MODE_MASK;
  165. }
  166. }
  167. /**
  168. * @brief lin get data value in byte
  169. *
  170. * @param ptr lin base address
  171. * @param index byte index
  172. * @return uint8_t byte value
  173. */
  174. static inline uint8_t lin_get_data_byte(LIN_Type *ptr, uint8_t index)
  175. {
  176. return ptr->DATABYTE[index];
  177. }
  178. /**
  179. * @brief lin write data value in byte
  180. *
  181. * @param ptr lin base address
  182. * @param index byte index
  183. * @param data byte value
  184. */
  185. static inline void lin_write_data_byte(LIN_Type *ptr, uint8_t index, uint8_t data)
  186. {
  187. ptr->DATABYTE[index] = data;
  188. }
  189. /**
  190. * @brief lin active status
  191. *
  192. * @param ptr lin base address
  193. * @return bool true for active, false for inactive
  194. */
  195. static inline bool lin_is_active(LIN_Type *ptr)
  196. {
  197. return ((ptr->STATE & LIN_STATE_LIN_ACTIVE_MASK) == LIN_STATE_LIN_ACTIVE_MASK) ? true : false;
  198. }
  199. /**
  200. * @brief lin complete status
  201. *
  202. * @param ptr lin base address
  203. * @return bool true for complete, false for incomplete
  204. */
  205. static inline bool lin_is_complete(LIN_Type *ptr)
  206. {
  207. return ((ptr->STATE & LIN_STATE_COMPLETE_MASK) == LIN_STATE_COMPLETE_MASK) ? true : false;
  208. }
  209. /**
  210. * @brief lin get ID
  211. *
  212. * @param ptr lin base address
  213. * @return uint8_t ID value
  214. */
  215. static inline uint8_t lin_get_id(LIN_Type *ptr)
  216. {
  217. return ptr->ID;
  218. }
  219. /**
  220. * @brief lin configure timing on master mode
  221. *
  222. * @param ptr lin base address
  223. * @param timing lin_timing_t
  224. * @return hpm_stat_t
  225. */
  226. hpm_stat_t lin_master_configure_timing(LIN_Type *ptr, lin_timing_t *timing);
  227. /**
  228. * @brief lin config timing on slave mode
  229. *
  230. * @param ptr lin base address
  231. * @param src_freq_in_hz source frequency
  232. * @return hpm_stat_t
  233. */
  234. hpm_stat_t lin_slave_configure_timing(LIN_Type *ptr, uint32_t src_freq_in_hz);
  235. /**
  236. * @brief lin transfer on master mode
  237. *
  238. * @param ptr lin base address
  239. * @param config lin_trans_config_t
  240. */
  241. void lin_master_transfer(LIN_Type *ptr, lin_trans_config_t *config);
  242. /**
  243. * @brief lin transfer on slave mode
  244. *
  245. * @note call this function after lin generate data request interrupt
  246. *
  247. * @param ptr lin base address
  248. * @param config lin_trans_config_t
  249. */
  250. void lin_slave_transfer(LIN_Type *ptr, lin_trans_config_t *config);
  251. /**
  252. * @brief get data length
  253. *
  254. * @note data length is determined by DATA_LEN register and ID
  255. *
  256. * @param ptr lin base address
  257. * @return uint8_t data length
  258. */
  259. uint8_t lin_get_data_length(LIN_Type *ptr);
  260. /**
  261. * @brief lin send data on master mode
  262. *
  263. * @param ptr lin base address
  264. * @param config lin_trans_config_t
  265. * @return status_timeout
  266. * @return status_success
  267. */
  268. hpm_stat_t lin_master_sent(LIN_Type *ptr, lin_trans_config_t *config);
  269. /**
  270. * @brief lin receive data on master mode
  271. *
  272. * @param ptr lin base address
  273. * @param config lin_trans_config_t
  274. * @return status_timeout
  275. * @return status_success
  276. */
  277. hpm_stat_t lin_master_receive(LIN_Type *ptr, lin_trans_config_t *config);
  278. /**
  279. * @brief lin send data on slave mode
  280. *
  281. * @param ptr lin base address
  282. * @param config lin_trans_config_t
  283. * @return status_timeout
  284. * @return status_success
  285. */
  286. hpm_stat_t lin_slave_sent(LIN_Type *ptr, lin_trans_config_t *config);
  287. /**
  288. * @brief lin receive data on slave mode
  289. *
  290. * @param ptr lin base address
  291. * @param config lin_trans_config_t
  292. * @return status_timeout
  293. * @return status_success
  294. */
  295. hpm_stat_t lin_slave_receive(LIN_Type *ptr, lin_trans_config_t *config);
  296. /**
  297. * @}
  298. */
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* HPM_LIN_DRV_H */