spi.c 8.2 KB

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