hpm_linv2_drv.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_LINV2_DRV_H
  8. #define HPM_LINV2_DRV_H
  9. #include <math.h>
  10. #include "hpm_common.h"
  11. #include "hpm_linv2_regs.h"
  12. #include "hpm_soc_feature.h"
  13. /**
  14. * @brief LINV2 driver APIs
  15. * @defgroup linv2_interface LINV2 driver APIs
  16. * @ingroup linv2_interfaces
  17. * @{
  18. */
  19. /** bit4 and bit5 encode data length in ID */
  20. #define LIN_ID_DATA_LEN_SHIFT 4U
  21. #define LIN_ID_DATA_LEN_MASK 0x30U
  22. #define LIN_ID_DATA_LEN_GET(x) (((uint8_t)(x) & LIN_ID_DATA_LEN_MASK) >> LIN_ID_DATA_LEN_SHIFT)
  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, dma mode not use this config */
  61. uint8_t data_length; /**< used when data_length_from_id is false or dma mode */
  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 control and status register value
  71. *
  72. * @param [in] ptr lin base address
  73. * @return uint32_t control and status register value
  74. */
  75. static inline uint32_t lin_get_control_and_status(LINV2_Type *ptr)
  76. {
  77. return ptr->CONTROL_STATUS;
  78. }
  79. /**
  80. * @brief lin reset interrupt
  81. *
  82. * @param ptr lin base address
  83. */
  84. static inline void lin_reset_interrupt(LINV2_Type *ptr)
  85. {
  86. ptr->CONTROL_STATUS = ((ptr->CONTROL_STATUS) & ~LINV2_CONTROL_STATUS_SLEEP_MASK) | LINV2_CONTROL_STATUS_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(LINV2_Type *ptr)
  94. {
  95. ptr->CONTROL_STATUS = ((ptr->CONTROL_STATUS) & ~LINV2_CONTROL_STATUS_SLEEP_MASK) | LINV2_CONTROL_STATUS_RESET_ERROR_MASK;
  96. }
  97. /**
  98. * @brief lin wakeup
  99. *
  100. * @param ptr lin base address
  101. */
  102. static inline void lin_wakeup(LINV2_Type *ptr)
  103. {
  104. ptr->CONTROL_STATUS = ((ptr->CONTROL_STATUS) & ~LINV2_CONTROL_STATUS_SLEEP_MASK) | LINV2_CONTROL_STATUS_WAKEUP_REQ_MASK;
  105. }
  106. /**
  107. * @brief lin sleep
  108. *
  109. * @param ptr lin base address
  110. */
  111. static inline void lin_sleep(LINV2_Type *ptr)
  112. {
  113. ptr->CONTROL_STATUS |= LINV2_CONTROL_STATUS_SLEEP_MASK;
  114. }
  115. /**
  116. * @brief lin slave stop
  117. *
  118. * @param ptr lin base address
  119. */
  120. static inline void lin_slave_stop(LINV2_Type *ptr)
  121. {
  122. ptr->CONTROL_STATUS = ((ptr->CONTROL_STATUS) & ~LINV2_CONTROL_STATUS_SLEEP_MASK) | LINV2_CONTROL_STATUS_STOP_MASK;
  123. }
  124. /**
  125. * @brief lin slave ack
  126. *
  127. * @param ptr lin base address
  128. */
  129. static inline void lin_slave_ack(LINV2_Type *ptr)
  130. {
  131. ptr->CONTROL_STATUS = ((ptr->CONTROL_STATUS) & ~LINV2_CONTROL_STATUS_SLEEP_MASK) | LINV2_CONTROL_STATUS_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(LINV2_Type *ptr, lin_bus_inactivity_time_t time)
  140. {
  141. ptr->TIMING_CONTROL = (ptr->TIMING_CONTROL & (~LINV2_TIMING_CONTROL_BUS_INACTIVE_TIME_MASK))
  142. | LINV2_TIMING_CONTROL_BUS_INACTIVE_TIME_SET(time);
  143. }
  144. /**
  145. * @brief lin slave set wakeup repeat time
  146. *
  147. * @param ptr lin base address
  148. * @param time lin_wakeup_repeat_time_t
  149. */
  150. static inline void lin_slave_set_wakeup_repeat_time(LINV2_Type *ptr, lin_wakeup_repeat_time_t time)
  151. {
  152. ptr->TIMING_CONTROL = (ptr->TIMING_CONTROL & (~LINV2_TIMING_CONTROL_WUP_REPEAT_TIME_MASK))
  153. | LINV2_TIMING_CONTROL_WUP_REPEAT_TIME_SET(time);
  154. }
  155. /**
  156. * @brief lin set mode
  157. *
  158. * @param ptr lin base address
  159. * @param master true for master mode, false for slave mode
  160. */
  161. static inline void lin_set_mode(LINV2_Type *ptr, bool master)
  162. {
  163. if (master) {
  164. ptr->TIMING_CONTROL |= LINV2_TIMING_CONTROL_MASTER_MODE_MASK;
  165. } else {
  166. ptr->TIMING_CONTROL &= ~LINV2_TIMING_CONTROL_MASTER_MODE_MASK;
  167. }
  168. }
  169. /**
  170. * @brief lin set checksum mode
  171. *
  172. * @param ptr lin base address
  173. * @param enhance_checksum true for enhance checksum mode, false for normal checksum mode
  174. */
  175. static inline void lin_set_checksum_mode(LINV2_Type *ptr, bool enhance_checksum)
  176. {
  177. if (enhance_checksum) {
  178. ptr->DATA_LEN_ID |= LINV2_DATA_LEN_ID_ENH_CHECK_MASK;
  179. } else {
  180. ptr->DATA_LEN_ID &= ~LINV2_DATA_LEN_ID_ENH_CHECK_MASK;
  181. }
  182. }
  183. /**
  184. * @brief lin get data value in byte
  185. *
  186. * @param ptr lin base address
  187. * @param index byte index
  188. * @return uint8_t byte value
  189. */
  190. static inline uint8_t lin_get_data_byte(LINV2_Type *ptr, uint8_t index)
  191. {
  192. return ptr->DATA_BYTE[index];
  193. }
  194. /**
  195. * @brief lin write data value in byte
  196. *
  197. * @param ptr lin base address
  198. * @param index byte index
  199. * @param data byte value
  200. */
  201. static inline void lin_write_data_byte(LINV2_Type *ptr, uint8_t index, uint8_t data)
  202. {
  203. ptr->DATA_BYTE[index] = data;
  204. }
  205. /**
  206. * @brief lin get ID
  207. *
  208. * @param ptr lin base address
  209. * @return uint8_t ID value
  210. */
  211. static inline uint8_t lin_get_id(LINV2_Type *ptr)
  212. {
  213. return (uint8_t)LINV2_DATA_LEN_ID_ID_GET(ptr->DATA_LEN_ID);
  214. }
  215. /**
  216. * @brief lin get checksum value
  217. *
  218. * @param ptr lin base address
  219. * @return uint8_t checksum value
  220. */
  221. static inline uint8_t lin_get_checksum(LINV2_Type *ptr)
  222. {
  223. return (uint8_t)LINV2_DATA_LEN_ID_CHECKSUM_GET(ptr->DATA_LEN_ID);
  224. }
  225. /**
  226. * @brief lin active status
  227. *
  228. * @param ptr lin base address
  229. * @return bool true for active, false for inactive
  230. */
  231. static inline uint8_t lin_is_active(LINV2_Type *ptr)
  232. {
  233. return ((ptr->CONTROL_STATUS & LINV2_CONTROL_STATUS_LIN_ACTIVE_MASK) == LINV2_CONTROL_STATUS_LIN_ACTIVE_MASK) ? true : false;
  234. }
  235. /**
  236. * @brief lin complete status
  237. *
  238. * @param ptr lin base address
  239. * @return bool true for complete, false for incomplete
  240. */
  241. static inline uint8_t lin_is_complete(LINV2_Type *ptr)
  242. {
  243. return ((ptr->CONTROL_STATUS & LINV2_CONTROL_STATUS_COMPLETE_MASK) == LINV2_CONTROL_STATUS_COMPLETE_MASK) ? true : false;
  244. }
  245. /**
  246. * @brief lin configure timing on master mode
  247. *
  248. * @param ptr lin base address
  249. * @param timing lin_timing_t
  250. * @return hpm_stat_t
  251. */
  252. hpm_stat_t lin_master_configure_timing(LINV2_Type *ptr, lin_timing_t *timing);
  253. /**
  254. * @brief lin config timing on slave mode
  255. *
  256. * @param ptr lin base address
  257. * @param src_freq_in_hz source frequency
  258. * @return hpm_stat_t
  259. */
  260. hpm_stat_t lin_slave_configure_timing(LINV2_Type *ptr, uint32_t src_freq_in_hz);
  261. /**
  262. * @brief lin transfer on master mode
  263. *
  264. * @param ptr lin base address
  265. * @param config lin_trans_config_t
  266. */
  267. void lin_master_transfer(LINV2_Type *ptr, lin_trans_config_t *config);
  268. /**
  269. * @brief lin transfer on slave mode
  270. *
  271. * @note call this function after lin generate data request interrupt
  272. *
  273. * @param ptr lin base address
  274. * @param config lin_trans_config_t
  275. */
  276. void lin_slave_transfer(LINV2_Type *ptr, lin_trans_config_t *config);
  277. /**
  278. * @brief get data length
  279. *
  280. * @note data length is determined by DATA_LEN register and ID
  281. *
  282. * @param ptr lin base address
  283. * @return uint8_t data length
  284. */
  285. uint8_t lin_get_data_length(LINV2_Type *ptr);
  286. /**
  287. * @brief lin send data on master mode
  288. *
  289. * @param ptr lin base address
  290. * @param config lin_trans_config_t
  291. * @return status_timeout
  292. * @return status_success
  293. */
  294. hpm_stat_t lin_master_sent(LINV2_Type *ptr, lin_trans_config_t *config);
  295. /**
  296. * @brief lin receive data on master mode
  297. *
  298. * @param ptr lin base address
  299. * @param config lin_trans_config_t
  300. * @return status_timeout
  301. * @return status_success
  302. */
  303. hpm_stat_t lin_master_receive(LINV2_Type *ptr, lin_trans_config_t *config);
  304. /**
  305. * @brief lin send data on slave mode
  306. *
  307. * @param ptr lin base address
  308. * @param config lin_trans_config_t
  309. * @return status_timeout
  310. * @return status_success
  311. */
  312. hpm_stat_t lin_slave_sent(LINV2_Type *ptr, lin_trans_config_t *config);
  313. /**
  314. * @brief lin receive data on slave mode
  315. *
  316. * @param ptr lin base address
  317. * @param config lin_trans_config_t
  318. * @return status_timeout
  319. * @return status_success
  320. */
  321. hpm_stat_t lin_slave_receive(LINV2_Type *ptr, lin_trans_config_t *config);
  322. /**
  323. * @brief lin slave dma transfer
  324. *
  325. * @param ptr lin base address
  326. * @param config lin_trans_config_t
  327. */
  328. void lin_slave_dma_transfer(LINV2_Type *ptr, lin_trans_config_t *config);
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. /**
  333. * @}
  334. */
  335. #endif /* HPM_LINV2_DRV_H */