spi.c 8.4 KB

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