wm_7816.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**************************************************************************//**
  2. * @file wm_i2s.c
  3. * @author
  4. * @version
  5. * @date
  6. * @brief
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd. All rights reserved.
  9. *****************************************************************************/
  10. #include <stdbool.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "wm_regs.h"
  14. #include "wm_7816.h"
  15. #include "wm_gpio.h"
  16. #include "wm_uart.h"
  17. #include "wm_cpu.h"
  18. #include "wm_osal.h"
  19. #define DEBUG_7816 1
  20. #if DEBUG_7816
  21. #define PRINT_DEBUG(fmt, args...) do{(printf("[DEBUG] "), printf(fmt, ##args));}while(0)
  22. #else
  23. #define PRINT_DEBUG(fmt, args...)
  24. #endif
  25. #define SYS_CLK (40000000)
  26. sc_io_map sc_io;
  27. /**
  28. * @brief
  29. * This function is used to config the pin in gpio or 7816 mode for the 7816 power on timing
  30. * @param[in] mode : 1--gpio mode ; 0--7816 mode
  31. * @retval
  32. */
  33. void wm_sc_io_clk_config(uint8_t mode)
  34. {
  35. if (sc_io.initialed == 0)
  36. {
  37. printf("error : 7816 io map must init....\r\n");
  38. return ;
  39. }
  40. if (mode) //gpio mode
  41. {
  42. tls_io_cfg_set(sc_io.clk_pin_num, WM_IO_OPTION5);
  43. tls_io_cfg_set(sc_io.io_pin_num, WM_IO_OPTION5);
  44. tls_gpio_cfg(sc_io.clk_pin_num, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  45. tls_gpio_cfg(sc_io.io_pin_num, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  46. }
  47. else //7816 mode
  48. {
  49. tls_io_cfg_set(sc_io.clk_pin_num, sc_io.clk_opt);
  50. tls_io_cfg_set(sc_io.io_pin_num, sc_io.io_opt);
  51. }
  52. }
  53. /**
  54. * @brief
  55. * This function is used to config the block guard time param in 7816 mode
  56. * @param[in] bgt : the value of blcok guard time will be set
  57. * @retval
  58. */
  59. void wm_sc_set_bgt(uint8_t bgt)
  60. {
  61. uint32_t reg;
  62. bgt = (bgt > 0x1f) ? 0x1F : bgt;
  63. reg = tls_reg_read32(HR_UART2_LINE_CTRL);
  64. reg &= ~(0x1f << 11);
  65. reg |= (bgt << 11);
  66. tls_reg_write32(HR_UART2_LINE_CTRL, reg);
  67. }
  68. /**
  69. * @brief
  70. * This function is used to config the tx retry count when detect err signal
  71. * @param[in] count : the value of retry time will be set 7 for max
  72. * @retval
  73. */
  74. void wm_sc_tx_retry_times(uint8_t count)
  75. {
  76. uint32_t reg;
  77. reg = tls_reg_read32(HR_UART2_LINE_CTRL);
  78. reg &= ~(0x7 << 16);
  79. tls_reg_write32(HR_UART2_LINE_CTRL, reg|(count<<16));
  80. tls_bitband_write(HR_UART2_LINE_CTRL, 23, 1);
  81. }
  82. /**
  83. * @brief
  84. * This function is used to config the rx retry count when detect parity error
  85. * @param[in] count : the value of retry time will be set 7 for max
  86. * @retval
  87. */
  88. void wm_sc_rx_retry_times(uint8_t count)
  89. {
  90. uint32_t reg;
  91. reg = tls_reg_read32(HR_UART2_LINE_CTRL);
  92. reg &= ~(0x7 << 20);
  93. tls_reg_write32(HR_UART2_LINE_CTRL, reg|(count<<20));
  94. tls_bitband_write(HR_UART2_LINE_CTRL, 19, 1);
  95. }
  96. /**
  97. * @brief
  98. * This function is used to config the etu param
  99. * @param[in] etu : the value of etu will be set
  100. * @retval
  101. */
  102. void wm_sc_set_etu(uint16_t etu)
  103. {
  104. uint32_t reg;
  105. reg = tls_reg_read32(HR_UART2_BAUD_RATE_CTRL);
  106. reg &= ~ 0xFFFF;
  107. reg |= etu;
  108. tls_reg_write32(HR_UART2_BAUD_RATE_CTRL, reg);
  109. }
  110. /**
  111. * @brief
  112. * This function config the module clock freq
  113. * @param[in] freq : the value of clock freq
  114. * @retval
  115. */
  116. void wm_sc_set_frequency(uint32_t freq)
  117. {
  118. uint32_t reg;
  119. uint8_t div;
  120. tls_sys_clk clk;
  121. tls_sys_clk_get(&clk);
  122. div = (clk.apbclk * 1000000 + freq)/(2 * freq) - 1;
  123. reg = tls_reg_read32(HR_UART2_BAUD_RATE_CTRL);
  124. reg &= ~ 0x3F0000;
  125. reg |= (div<<16);
  126. tls_reg_write32(HR_UART2_BAUD_RATE_CTRL, reg);
  127. }
  128. /**
  129. * @brief
  130. * close af to use PB23 and PB24(uart2_rx as default if af is on) as gpio
  131. * @retval
  132. */
  133. void wm_sc_powerInit(void)
  134. {
  135. #ifdef WM_SC_PWR_PIN
  136. tls_gpio_cfg(WM_SC_RST_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  137. tls_gpio_cfg(WM_SC_PWR_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  138. #endif
  139. }
  140. /**
  141. * @brief
  142. * power on the 7816 device if power is controled by GPIO
  143. * @retval
  144. */
  145. void wm_sc_poweron(void)
  146. {
  147. #ifdef WM_SC_PWR_PIN
  148. tls_gpio_cfg(WM_SC_PWR_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  149. tls_gpio_write(WM_SC_PWR_PIN, 1);
  150. #endif
  151. }
  152. /**
  153. * @brief
  154. * power off the 7816 device if power is controled by GPIO
  155. * @retval
  156. */
  157. void wm_sc_poweroff(void)
  158. {
  159. #ifdef WM_SC_PWR_PIN
  160. tls_gpio_cfg(WM_SC_PWR_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  161. tls_gpio_write(WM_SC_PWR_PIN, 0);
  162. #endif
  163. }
  164. /**
  165. * @brief
  166. * driver the reset gpio in low level
  167. * @retval
  168. */
  169. void wm_sc_rst_low(void)
  170. {
  171. #ifdef WM_SC_RST_PIN
  172. tls_gpio_cfg(WM_SC_RST_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  173. tls_gpio_write(WM_SC_RST_PIN, 0);
  174. #endif
  175. }
  176. /**
  177. * @brief
  178. * driver the reset gpio in high level
  179. * @retval
  180. */
  181. void wm_sc_rst_high(void)
  182. {
  183. #ifdef WM_SC_RST_PIN
  184. tls_gpio_cfg(WM_SC_RST_PIN, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH);
  185. tls_gpio_write(WM_SC_RST_PIN, 1);
  186. #endif
  187. }
  188. /**
  189. * @brief
  190. * hotrest the 7816 device obey the 7816-3 timing
  191. * @retval
  192. */
  193. void wm_sc_hotreset(void)
  194. {
  195. uint32_t delay = 0xffff;
  196. /* set the rst pin to low */
  197. wm_sc_rst_low();
  198. /* delay */
  199. while(delay--);
  200. /* set f/d to default 372 */
  201. wm_sc_set_etu(WM_SC_DEFAULT_FD);
  202. /* set the rst pin to high */
  203. wm_sc_rst_high();
  204. }
  205. /**
  206. * @brief
  207. * colreset the 7816 device obey the 7816-3 timing
  208. * @retval
  209. */
  210. void wm_sc_colreset(void)
  211. {
  212. /* power down */
  213. wm_sc_poweroff();
  214. /* select the clk io in gpio mode */
  215. wm_sc_io_clk_config(1);
  216. /* reset the clk pin */
  217. tls_gpio_write(sc_io.clk_pin_num, 0);
  218. /* reset the io pin */
  219. tls_gpio_write(sc_io.io_pin_num, 0);
  220. /* set the ret pin to low */
  221. wm_sc_rst_low();
  222. /* power on the card */
  223. wm_sc_poweron();
  224. /* config the model in 7816 mode */
  225. wm_sc_7816_mode(1);
  226. /* select the clk io pin in 7816 mode */
  227. wm_sc_io_clk_config(0);
  228. /* config the output clock freq*/
  229. wm_sc_set_frequency(5000000);
  230. /* set the F/D to default (372)*/
  231. wm_sc_set_etu(WM_SC_DEFAULT_FD);
  232. /* set the rst pin to high */
  233. wm_sc_rst_high();
  234. }
  235. /**
  236. * @brief
  237. * deactive the 7816 device obey the 7816-3 timing
  238. * @retval
  239. */
  240. void wm_sc_deactive(void)
  241. {
  242. /* set the rst pin in low level*/
  243. wm_sc_rst_low();
  244. /* select the clk and io pin to 7816 mode */
  245. wm_sc_io_clk_config(0);
  246. /* disable the output clock */
  247. wm_sc_clk_enable(0);
  248. /* select the clk and io pin to gpio mode */
  249. wm_sc_io_clk_config(1);
  250. /* set the clk pin to low */
  251. tls_gpio_write(sc_io.clk_pin_num, 0);
  252. /* set the io pin to low */
  253. tls_gpio_write(sc_io.io_pin_num, 0);
  254. /* set the power pin to low */
  255. wm_sc_poweroff();
  256. }