hpm_sdm_drv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_SDM_DRV_H
  8. #define HPM_SDM_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_sdm_regs.h"
  11. /* defined channel mask macro */
  12. #define SAMPLING_MODE_MASK (0x7U)
  13. #define CHN_SAMPLING_MODE_SHIFT(ch) ((ch) * 3U + SDM_CTRL_CHMD_SHIFT)
  14. #define CHN_SAMPLING_MODE_MASK(ch) (SAMPLING_MODE_MASK << CHN_SAMPLING_MODE_SHIFT(ch))
  15. #define CH0_EN_MASK (0x1U << SDM_CTRL_CH_EN_SHIFT)
  16. #define CHN_EN_MASK(ch) (CH0_EN_MASK << (ch))
  17. #define CHN_ERR_MASK(ch) (SDM_INT_EN_CH0ERR_MASK << (ch))
  18. #define CHN_DRY_MASK(ch) (SDM_INT_EN_CH0DRY_MASK << (ch))
  19. typedef enum {
  20. sdm_sampling_rising_clk_edge = 0,
  21. sdm_sampling_every_clk_edge = 1,
  22. sdm_sampling_manchester_mode = 2,
  23. sdm_sampling_falling_clk_edge = 3,
  24. sdm_sampling_rising_two_clk_edge = 4,
  25. sdm_sampling_falling_two_clk_edge = 5
  26. } sdm_sampling_mode_t;
  27. typedef enum {
  28. sdm_filter_sinc1 = 0,
  29. sdm_filter_sinc2 = 1,
  30. sdm_filter_sinc3 = 2,
  31. sdm_filter_fast_sinc2 = 3
  32. } sdm_filter_type_t;
  33. typedef struct {
  34. bool clk_signal_sync;
  35. bool data_signal_sync;
  36. bool interrupt_en;
  37. } sdm_control_t;
  38. typedef struct {
  39. uint8_t sampling_mode;
  40. bool enable_err_interrupt;
  41. bool enable_data_ready_interrupt;
  42. } sdm_channel_common_config_t;
  43. typedef struct {
  44. uint16_t high_threshold;
  45. uint16_t zero_cross_threshold;
  46. uint16_t low_threshold;
  47. bool en_zero_cross_threshold_int;
  48. bool en_clock_invalid_int;
  49. bool en_high_threshold_int;
  50. bool en_low_threshold_int;
  51. uint8_t filter_type; /**< sdm_filter_type_t */
  52. uint8_t oversampling_rate; /**< 1 - 32 */
  53. uint8_t ignore_invalid_samples;
  54. bool enable;
  55. } sdm_comparator_config_t;
  56. typedef struct {
  57. uint8_t fifo_threshold;
  58. bool en_fifo_threshold_int;
  59. uint8_t manchester_threshold :8;
  60. uint8_t wdg_threshold :8;
  61. uint8_t en_af_int :1;
  62. uint8_t en_data_overflow_int :1;
  63. uint8_t en_cic_data_saturation_int :1;
  64. uint8_t en_data_ready_int :1;
  65. uint8_t sync_source :6;
  66. uint8_t fifo_clean_on_sync :1; /**< fifo clean by hardware when fifo interrupt occurred */
  67. uint8_t wtsynaclr :1;
  68. uint8_t wtsynmclr :1;
  69. uint8_t wtsyncen :1;
  70. uint8_t output_32bit :1;
  71. uint8_t data_ready_flag_by_fifo :1;
  72. uint8_t enable :1;
  73. uint8_t filter_type; /**< sdm_filter_type_t */
  74. bool pwm_signal_sync;
  75. uint8_t output_offset; /**< 16bit mode need configure this */
  76. uint16_t oversampling_rate; /**< 1-256 */
  77. uint8_t ignore_invalid_samples;
  78. } sdm_filter_config_t;
  79. typedef struct {
  80. uint32_t count;
  81. uint8_t *buff;
  82. uint8_t data_len_in_bytes; /* 16bit-2 32bit-4 */
  83. bool using_fifo;
  84. } sdm_output_config_t;
  85. typedef enum {
  86. sdm_comparator_no_event = 0,
  87. sdm_comparator_event_out_high_threshold = SDM_CH_SCST_CMPH_MASK,
  88. sdm_comparator_event_out_low_threshold = SDM_CH_SCST_CMPL_MASK,
  89. sdm_comparator_event_hz = SDM_CH_SCST_HZ_MASK,
  90. sdm_comparator_event_invalid_clk = SDM_CH_SCST_MF_MASK
  91. } sdm_comparator_event_t;
  92. typedef enum {
  93. sdm_chn0_error_mask = SDM_INT_EN_CH0ERR_MASK,
  94. sdm_chn1_error_mask = SDM_INT_EN_CH1ERR_MASK,
  95. sdm_chn2_error_mask = SDM_INT_EN_CH2ERR_MASK,
  96. sdm_chn3_error_mask = SDM_INT_EN_CH3ERR_MASK,
  97. sdm_chn0_data_ready_mask = SDM_INT_EN_CH0DRY_MASK,
  98. sdm_chn1_data_ready_mask = SDM_INT_EN_CH1DRY_MASK,
  99. sdm_chn2_data_ready_mask = SDM_INT_EN_CH2DRY_MASK,
  100. sdm_chn3_data_ready_mask = SDM_INT_EN_CH3DRY_MASK
  101. } sdm_channel_int_status_mask_t;
  102. typedef enum {
  103. sdm_chn0_enable_mask = 1U << SDM_CTRL_CH_EN_SHIFT,
  104. sdm_chn1_enable_mask = 1U << (SDM_CTRL_CH_EN_SHIFT + 1U),
  105. sdm_chn2_enable_mask = 1U << (SDM_CTRL_CH_EN_SHIFT + 2U),
  106. sdm_chn3_enable_mask = 1U << (SDM_CTRL_CH_EN_SHIFT + 3U)
  107. } sdm_channel_enable_mask_t;
  108. #ifdef __cplusplus
  109. extern "C" {
  110. #endif
  111. /**
  112. * @brief sdm enable module interrupt
  113. *
  114. * @param ptr SDM base address
  115. * @param enable true for enable, false for disable
  116. */
  117. static inline void sdm_enable_module_interrupt(SDM_Type *ptr, bool enable)
  118. {
  119. if (enable) {
  120. ptr->CTRL |= SDM_CTRL_IE_MASK;
  121. } else {
  122. ptr->CTRL &= ~SDM_CTRL_IE_MASK;
  123. }
  124. }
  125. /**
  126. * @brief sdm enable channel
  127. *
  128. * @param ptr SDM base address
  129. * @param ch_index channel index
  130. * @param enable true for enable, false for disable
  131. */
  132. static inline void sdm_enable_channel(SDM_Type *ptr, uint8_t ch_index, bool enable)
  133. {
  134. if (enable) {
  135. ptr->CTRL |= CHN_EN_MASK(ch_index);
  136. } else {
  137. ptr->CTRL &= ~CHN_EN_MASK(ch_index);
  138. }
  139. }
  140. /**
  141. * @brief sdm enable channel by mask
  142. *
  143. * @note ch_mask supports bitwise or operation, this API could enable multiple channels at the same time
  144. *
  145. * @param ptr SDM base address
  146. * @param ch_mask sdm_channel_enable_mask_t
  147. */
  148. static inline void sdm_enable_channel_by_mask(SDM_Type *ptr, uint32_t ch_mask)
  149. {
  150. ptr->CTRL = (ptr->CTRL & (~SDM_CTRL_CH_EN_MASK)) | ch_mask;
  151. }
  152. /**
  153. * @brief sdm set channel sampling mode
  154. *
  155. * @param ptr SDM base address
  156. * @param ch_index channel index
  157. * @param mode sdm_sampling_mode_t
  158. */
  159. static inline void sdm_set_channel_sampling_mode(SDM_Type *ptr, uint8_t ch_index, sdm_sampling_mode_t mode)
  160. {
  161. ptr->CTRL &= ~CHN_SAMPLING_MODE_MASK(ch_index);
  162. ptr->CTRL |= mode << (SDM_CTRL_CHMD_SHIFT + ch_index);
  163. }
  164. /**
  165. * @brief sdm enable channel interrupt
  166. *
  167. * @param ptr SDM base address
  168. * @param mask sdm_channel_int_status_mask_t, support bitwise or operation
  169. */
  170. static inline void sdm_enable_channel_interrupt(SDM_Type *ptr, uint32_t mask)
  171. {
  172. ptr->INT_EN |= mask;
  173. }
  174. /**
  175. * @brief sdm get status register value
  176. *
  177. * @param ptr SDM base address
  178. * @return uint32_t sdm status register value
  179. */
  180. static inline uint32_t sdm_get_status(SDM_Type *ptr)
  181. {
  182. return ptr->STATUS;
  183. }
  184. /**
  185. * @brief get channel data ready status
  186. *
  187. * @param ptr SDM base address
  188. * @param ch channel
  189. * @return true data ready
  190. * @return false not ready
  191. */
  192. static inline bool sdm_get_channel_data_ready_status(SDM_Type *ptr, uint8_t ch)
  193. {
  194. return (((ptr->STATUS) & CHN_DRY_MASK(ch)) == CHN_DRY_MASK(ch));
  195. }
  196. /**
  197. * @brief get channel error status
  198. *
  199. * @param ptr SDM base address
  200. * @param ch channel
  201. * @return true error occur
  202. * @return false no error
  203. */
  204. static inline bool sdm_get_channel_data_error_status(SDM_Type *ptr, uint8_t ch)
  205. {
  206. return (((ptr->STATUS) & CHN_ERR_MASK(ch)) == CHN_ERR_MASK(ch));
  207. }
  208. /**
  209. * @brief sdm set channel's fifo threshold
  210. *
  211. * @param ptr SDM base address
  212. * @param ch channel index
  213. * @param threshold threshold value
  214. */
  215. static inline void sdm_set_ch_fifo_threshold(SDM_Type *ptr, uint8_t ch, uint8_t threshold)
  216. {
  217. ptr->CH[ch].SDFIFOCTRL = SDM_CH_SDFIFOCTRL_THRSH_SET(threshold);
  218. }
  219. /**
  220. * @brief sdm get channel fifo threshold
  221. *
  222. * @param ptr SDM base address
  223. * @param ch channel index
  224. * @return uint8_t fifo threshold value
  225. */
  226. static inline uint8_t sdm_get_ch_fifo_threshold(SDM_Type *ptr, uint8_t ch)
  227. {
  228. return (uint8_t)(SDM_CH_SDFIFOCTRL_THRSH_GET(ptr->CH[ch].SDFIFOCTRL));
  229. }
  230. /**
  231. * @brief sdm get channel filter status
  232. *
  233. * @param ptr SDM base address
  234. * @param ch channel index
  235. * @return uint32_t channel filter status register value
  236. */
  237. static inline uint32_t sdm_get_channel_filter_status(SDM_Type *ptr, uint8_t ch)
  238. {
  239. return ptr->CH[ch].SDST;
  240. }
  241. /**
  242. * @brief sdm get channel data count in fifo
  243. *
  244. * @param ptr SDM base address
  245. * @param ch channel index
  246. * @return uint8_t data count
  247. */
  248. static inline uint8_t sdm_get_channel_fifo_data_count(SDM_Type *ptr, uint8_t ch)
  249. {
  250. return (uint8_t)(SDM_CH_SDST_FILL_GET(ptr->CH[ch].SDST));
  251. }
  252. /**
  253. * @brief sdm get channel filter output data in fifo
  254. *
  255. * @param ptr SDM base address
  256. * @param ch channel index
  257. * @return int32_t data
  258. */
  259. static inline int32_t sdm_get_channel_fifo_data(SDM_Type *ptr, uint8_t ch)
  260. {
  261. return ptr->CH[ch].SDFIFO;
  262. }
  263. /**
  264. * @brief sdm get channel input clock cycle count
  265. *
  266. * @param ptr SDM base address
  267. * @param ch channel index
  268. * @return uint8_t clock cycle count
  269. */
  270. static inline uint8_t sdm_get_channel_clock_cycle_count(SDM_Type *ptr, uint8_t ch)
  271. {
  272. return (uint8_t)(SDM_CH_SDST_PERIOD_MCLK_GET(ptr->CH[ch].SDST));
  273. }
  274. /**
  275. * @brief sdm get channel comparator data
  276. *
  277. * @param ptr SDM base address
  278. * @param ch channel index
  279. * @return uint16_t comparator data
  280. */
  281. static inline uint16_t sdm_get_channel_comparator_data(SDM_Type *ptr, uint8_t ch)
  282. {
  283. return (uint16_t)(ptr->CH[ch].SCAMP);
  284. }
  285. /**
  286. * @brief sdm set channel comparator high threshold
  287. *
  288. * @param ptr SDM base address
  289. * @param ch channel index
  290. * @param value high threshold value
  291. */
  292. static inline void sdm_set_channel_comparator_high_threshold(SDM_Type *ptr, uint8_t ch, uint16_t value)
  293. {
  294. ptr->CH[ch].SCHTL = value;
  295. }
  296. /**
  297. * @brief sdm set channel comparator zero crossing threshold
  298. *
  299. * @param ptr SDM base address
  300. * @param ch channel index
  301. * @param value zero crossing threshold value
  302. */
  303. static inline void sdm_set_channel_comparator_zero_crossing_threshold(SDM_Type *ptr, uint8_t ch, uint16_t value)
  304. {
  305. ptr->CH[ch].SCHTLZ = value;
  306. }
  307. /**
  308. * @brief sdm set channel comparator low threshold
  309. *
  310. * @param ptr SDM base address
  311. * @param ch channel index
  312. * @param value low threshold value
  313. */
  314. static inline void sdm_set_channel_comparator_low_threshold(SDM_Type *ptr, uint8_t ch, uint16_t value)
  315. {
  316. ptr->CH[ch].SCLLT = value;
  317. }
  318. /**
  319. * @brief sdm get channel comparator status register value
  320. *
  321. * @param ptr SDM base address
  322. * @param ch channel index
  323. * @return uint32_t channel comparator status register value
  324. */
  325. static inline uint32_t sdm_get_channel_comparator_status(SDM_Type *ptr, uint8_t ch)
  326. {
  327. return ptr->CH[ch].SCST;
  328. }
  329. /**
  330. * @brief sdm get default module control
  331. *
  332. * @param ptr SDM base address
  333. * @param control sdm_control_t
  334. */
  335. void sdm_get_default_module_control(SDM_Type *ptr, sdm_control_t *control);
  336. /**
  337. * @brief sdm init module
  338. *
  339. * @param ptr SDM base address
  340. * @param control sdm_control_t
  341. */
  342. void sdm_init_module(SDM_Type *ptr, sdm_control_t *control);
  343. /**
  344. * @brief sdm get channel common setting
  345. *
  346. * @param ptr SDM base address
  347. * @param config sdm_channel_common_config_t
  348. */
  349. void sdm_get_channel_common_setting(SDM_Type *ptr, sdm_channel_common_config_t *config);
  350. /**
  351. * @brief sdm config channel's common setting
  352. *
  353. * @param ptr SDM base address
  354. * @param ch_index channel index
  355. * @param config sdm_channel_common_config_t
  356. */
  357. void sdm_config_channel_common_setting(SDM_Type *ptr, uint8_t ch_index, sdm_channel_common_config_t *config);
  358. /**
  359. * @brief sdm get channel default filter config
  360. *
  361. * @param ptr SDM base address
  362. * @param filter_config sdm_filter_config_t
  363. */
  364. void sdm_get_channel_default_filter_config(SDM_Type *ptr, sdm_filter_config_t *filter_config);
  365. /**
  366. * @brief sdm config channel filter
  367. *
  368. * @param ptr SDM base address
  369. * @param ch_index channel index
  370. * @param filter_config sdm_filter_config_t
  371. */
  372. void sdm_config_channel_filter(SDM_Type *ptr, uint8_t ch_index, sdm_filter_config_t *filter_config);
  373. /**
  374. * @brief sdm get channel default comparator config
  375. *
  376. * @param ptr SDM base address
  377. * @param cmp_config sdm_comparator_config_t
  378. */
  379. void sdm_get_channel_default_comparator_config(SDM_Type *ptr, sdm_comparator_config_t *cmp_config);
  380. /**
  381. * @brief sdm config channel comparator
  382. *
  383. * @param ptr SDM base address
  384. * @param ch_index channel index
  385. * @param cmp_config sdm_comparator_config_t
  386. */
  387. void sdm_config_channel_comparator(SDM_Type *ptr, uint8_t ch_index, sdm_comparator_config_t *cmp_config);
  388. /**
  389. * @brief sdm receive one filter data
  390. *
  391. * @param ptr SDM base address
  392. * @param ch_index channel index
  393. * @param using_fifo true for getting data from fifo, false for getting data from register
  394. * @param data data buff
  395. * @param data_len_in_bytes output data len in bytes
  396. * @retval hpm_stat_t status_success only if it succeeds
  397. */
  398. hpm_stat_t sdm_receive_one_filter_data(SDM_Type *ptr, uint8_t ch_index, bool using_fifo, int8_t *data, uint8_t data_len_in_bytes);
  399. /**
  400. * @brief sdm receive filter data
  401. *
  402. * @param ptr SDM base address
  403. * @param ch_index channel index
  404. * @param using_fifo true for getting data from fifo, false for getting data from register
  405. * @param data data buff
  406. * @param count data count
  407. * @param data_len_in_bytes output data len in bytes
  408. * @retval hpm_stat_t status_success only if it succeeds
  409. */
  410. hpm_stat_t sdm_receive_filter_data(SDM_Type *ptr, uint8_t ch_index, bool using_fifo, int8_t *data, uint32_t count, uint8_t data_len_in_bytes);
  411. /**
  412. * @}
  413. */
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417. #endif /* HPM_SDM_DRV_H */