spi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-04 Haley the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "am_mcu_apollo.h"
  13. #include "spi.h"
  14. /* SPI0 */
  15. #define AM_SPI0_IOM_INST 0
  16. #define SPI0_GPIO_SCK 5
  17. #define SPI0_GPIO_CFG_SCK AM_HAL_PIN_5_M0SCK
  18. #define SPI0_GPIO_MISO 6
  19. #define SPI0_GPIO_CFG_MISO AM_HAL_PIN_6_M0MISO
  20. #define SPI0_GPIO_MOSI 7
  21. #define SPI0_GPIO_CFG_MOSI AM_HAL_PIN_7_M0MOSI
  22. /* SPI1 */
  23. #define AM_SPI1_IOM_INST 1
  24. static am_hal_iom_config_t g_sIOMConfig =
  25. {
  26. AM_HAL_IOM_SPIMODE, // ui32InterfaceMode
  27. AM_HAL_IOM_400KHZ, // ui32ClockFrequency
  28. 0, // bSPHA
  29. 0, // bSPOL
  30. 80, // ui8WriteThreshold
  31. 80, // ui8ReadThreshold
  32. };
  33. /* AM spi driver */
  34. struct am_spi_bus
  35. {
  36. struct rt_spi_bus parent;
  37. rt_uint32_t u32Module;
  38. };
  39. //connect am drv to rt drv.
  40. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
  41. {
  42. struct am_spi_bus * am_spi_bus = (struct am_spi_bus *)device->bus;
  43. rt_uint32_t max_hz = configuration->max_hz;
  44. if(max_hz >= 24000000)
  45. {
  46. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_24MHZ;
  47. }
  48. else if(max_hz >= 16000000)
  49. {
  50. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_16MHZ;
  51. }
  52. else if(max_hz >= 12000000)
  53. {
  54. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_12MHZ;
  55. }
  56. else if(max_hz >= 8000000)
  57. {
  58. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_8MHZ;
  59. }
  60. else if(max_hz >= 6000000)
  61. {
  62. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_6MHZ;
  63. }
  64. else if(max_hz >= 4000000)
  65. {
  66. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_4MHZ;
  67. }
  68. else if(max_hz >= 3000000)
  69. {
  70. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_3MHZ;
  71. }
  72. else if(max_hz >= 2000000)
  73. {
  74. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_2MHZ;
  75. }
  76. else if(max_hz >= 1500000)
  77. {
  78. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_1_5MHZ;
  79. }
  80. else if(max_hz >= 1000000)
  81. {
  82. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_1MHZ;
  83. }
  84. else if(max_hz >= 750000)
  85. {
  86. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_750KHZ;
  87. }
  88. else if(max_hz >= 500000)
  89. {
  90. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_500KHZ;
  91. }
  92. else if(max_hz >= 400000)
  93. {
  94. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
  95. }
  96. else if(max_hz >= 375000)
  97. {
  98. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_375KHZ;
  99. }
  100. else if(max_hz >= 250000)
  101. {
  102. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_250KHZ;
  103. }
  104. else if(max_hz >= 100000)
  105. {
  106. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_100KHZ;
  107. }
  108. else if(max_hz >= 50000)
  109. {
  110. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_50KHZ;
  111. }
  112. else
  113. {
  114. g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_10KHZ;
  115. }
  116. /* CPOL */
  117. if(configuration->mode & RT_SPI_CPOL)
  118. {
  119. g_sIOMConfig.bSPOL = 1;
  120. }
  121. else
  122. {
  123. g_sIOMConfig.bSPOL = 0;
  124. }
  125. /* CPHA */
  126. if(configuration->mode & RT_SPI_CPHA)
  127. {
  128. g_sIOMConfig.bSPHA= 1;
  129. }
  130. else
  131. {
  132. g_sIOMConfig.bSPHA= 0;
  133. }
  134. /* init SPI */
  135. am_hal_iom_disable(am_spi_bus->u32Module);
  136. am_hal_iom_config(am_spi_bus->u32Module, &g_sIOMConfig);
  137. am_hal_iom_enable(am_spi_bus->u32Module);
  138. return RT_EOK;
  139. };
  140. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message* message)
  141. {
  142. struct am_spi_bus * am_spi_bus = (struct am_spi_bus *)device->bus;
  143. //struct rt_spi_configuration * config = &device->config;
  144. struct am_spi_cs * am_spi_cs = device->parent.user_data;
  145. rt_uint32_t * send_ptr = (rt_uint32_t *)message->send_buf;
  146. rt_uint32_t * recv_ptr = message->recv_buf;
  147. rt_uint32_t u32BytesRemaining = message->length;
  148. rt_uint32_t u32TransferSize = 0;
  149. /* take CS */
  150. if (message->cs_take)
  151. {
  152. am_hal_gpio_out_bit_clear(am_spi_cs->chip_select);
  153. }
  154. // 读数据
  155. if (recv_ptr != RT_NULL)
  156. {
  157. while (u32BytesRemaining)
  158. {
  159. /* Set the transfer size to either 64, or the number of remaining
  160. bytes, whichever is smaller */
  161. if (u32BytesRemaining > 64)
  162. {
  163. u32TransferSize = 64;
  164. am_hal_gpio_pin_config(SPI0_GPIO_MOSI, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_PULL6K);
  165. am_hal_gpio_out_bit_set(SPI0_GPIO_MOSI);
  166. am_hal_iom_spi_read(am_spi_bus->u32Module, am_spi_cs->chip_select,
  167. (uint32_t *)recv_ptr, u32TransferSize, AM_HAL_IOM_RAW);
  168. am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);
  169. }
  170. else
  171. {
  172. u32TransferSize = u32BytesRemaining;
  173. {
  174. am_hal_gpio_pin_config(SPI0_GPIO_MOSI, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_PULL6K);
  175. am_hal_gpio_out_bit_set(SPI0_GPIO_MOSI);
  176. am_hal_iom_spi_read(am_spi_bus->u32Module, am_spi_cs->chip_select,
  177. (uint32_t *)recv_ptr, u32TransferSize, AM_HAL_IOM_RAW);
  178. am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);
  179. }
  180. }
  181. u32BytesRemaining -= u32TransferSize;
  182. recv_ptr = (rt_uint32_t *)((rt_uint32_t)recv_ptr + u32TransferSize);
  183. }
  184. }
  185. // 写数据
  186. else
  187. {
  188. while (u32BytesRemaining)
  189. {
  190. /* Set the transfer size to either 32, or the number of remaining
  191. bytes, whichever is smaller */
  192. if (u32BytesRemaining > 64)
  193. {
  194. u32TransferSize = 64;
  195. am_hal_iom_spi_write(am_spi_bus->u32Module, am_spi_cs->chip_select,
  196. (uint32_t *)send_ptr, u32TransferSize, AM_HAL_IOM_RAW);
  197. }
  198. else
  199. {
  200. u32TransferSize = u32BytesRemaining;
  201. {
  202. am_hal_iom_spi_write(am_spi_bus->u32Module, am_spi_cs->chip_select,
  203. (uint32_t *)send_ptr, u32TransferSize, AM_HAL_IOM_RAW);
  204. }
  205. }
  206. u32BytesRemaining -= u32TransferSize;
  207. send_ptr = (rt_uint32_t *)((rt_uint32_t)send_ptr + u32TransferSize);
  208. }
  209. }
  210. /* release CS */
  211. if(message->cs_release)
  212. {
  213. am_hal_gpio_out_bit_set(am_spi_cs->chip_select);
  214. }
  215. return message->length;
  216. }
  217. static const struct rt_spi_ops am_spi_ops =
  218. {
  219. configure,
  220. xfer
  221. };
  222. #ifdef RT_USING_SPI0
  223. static struct am_spi_bus am_spi_bus_0 =
  224. {
  225. {0},
  226. AM_SPI0_IOM_INST
  227. };
  228. #endif /* #ifdef RT_USING_SPI0 */
  229. #ifdef RT_USING_SPI1
  230. static struct am_spi_bus am_spi_bus_1 =
  231. {
  232. {0},
  233. AM_SPI1_IOM_INST
  234. };
  235. #endif /* #ifdef RT_USING_SPI1 */
  236. int yr_hw_spi_init(void)
  237. {
  238. struct am_spi_bus* am_spi;
  239. #ifdef RT_USING_SPI0
  240. /* init spi gpio */
  241. am_hal_gpio_pin_config(SPI0_GPIO_SCK, SPI0_GPIO_CFG_SCK);
  242. am_hal_gpio_pin_config(SPI0_GPIO_MISO, SPI0_GPIO_CFG_MISO | AM_HAL_GPIO_PULL6K);
  243. am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);
  244. /* Initialize IOM 0 in SPI mode at 100KHz */
  245. am_hal_iom_pwrctrl_enable(AM_SPI0_IOM_INST);
  246. am_hal_iom_config(AM_SPI0_IOM_INST, &g_sIOMConfig);
  247. am_hal_iom_enable(AM_SPI0_IOM_INST);
  248. //init spi bus device
  249. am_spi = &am_spi_bus_0;
  250. rt_spi_bus_register(&am_spi->parent, "spi0", &am_spi_ops);
  251. #endif
  252. //rt_kprintf("spi init!\n");
  253. return 0;
  254. }
  255. #ifdef RT_USING_COMPONENTS_INIT
  256. INIT_BOARD_EXPORT(yr_hw_spi_init);
  257. #endif
  258. /*@}*/