drv_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-29 zdzn first version
  9. */
  10. #include "drv_spi.h"
  11. #if !defined(BSP_USING_SPI0) && !defined(BSP_USING_SPI1)
  12. #ifdef RT_USING_SPI
  13. #undef RT_USING_SPI
  14. #endif
  15. #endif
  16. #ifdef RT_USING_SPI
  17. static rt_uint8_t bcm2835_spi_bit_order = BCM283X_SPI_BIT_ORDER_MSBFIRST;
  18. static rt_uint8_t bcm2835_byte_reverse_table[] =
  19. {
  20. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  21. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  22. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  23. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  24. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  25. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  26. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  27. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  28. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  29. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  30. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  31. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  32. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  33. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  34. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  35. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  36. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  37. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  38. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  39. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  40. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  41. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  42. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  43. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  44. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  45. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  46. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  47. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  48. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  49. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  50. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  51. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  52. };
  53. #define BSP_SPI_MAX_HZ (30* 1000 *1000)
  54. #define SPITIMEOUT 0x0FFF
  55. struct rt_spi_hw_config
  56. {
  57. rt_int8_t sclk_pin;
  58. rt_int8_t sclk_mode;
  59. rt_int8_t mosi_pin;
  60. rt_int8_t mosi_mode;
  61. rt_int8_t miso_pin;
  62. rt_int8_t miso_mode;
  63. rt_int8_t cs_pin;
  64. rt_int8_t cs_mode;
  65. rt_uint32_t spi_base;
  66. rt_uint32_t clk_div;
  67. };
  68. struct rt_sw_spi_cs
  69. {
  70. rt_uint32_t pin;
  71. };
  72. struct rt_spi
  73. {
  74. char *device_name;
  75. struct rt_spi_bus *spi_bus;
  76. struct rt_spi_hw_config *hwcfg;
  77. struct rt_spi_configuration *cfg;
  78. };
  79. static rt_err_t raspi_hostspi_init(struct rt_spi_configuration *cfg)
  80. {
  81. volatile rt_uint32_t addr = (PER_BASE + BCM283X_SPI0_BASE) + BCM283X_SPI0_CS;
  82. //volatile rt_uint32_t fifo = (PER_BASE + BCM283X_SPI0_BASE) + BCM283X_SPI0_FIFO;
  83. // spi clear fifo
  84. bcm283x_peri_set_bits(addr, BCM283X_SPI0_CS_CLEAR, BCM283X_SPI0_CS_CLEAR);
  85. // /* Set TA = 1 */
  86. // bcm2835_peri_set_bits(addr, BCM283X_SPI0_CS_TA, BCM283X_SPI0_CS_TA);
  87. /* Mask in the CPO and CPHA bits of CS */
  88. bcm283x_peri_set_bits(addr, (rt_uint32_t)(cfg->mode << 2), BCM283X_SPI0_CS_CPOL | BCM283X_SPI0_CS_CPHA);
  89. //chipSelect
  90. bcm283x_peri_set_bits(addr, BCM283X_SPI_CS0, BCM283X_SPI0_CS_CS);
  91. rt_uint8_t shift = 21 + BCM283X_SPI_CS0;
  92. /* Mask in the appropriate CSPOLn bit */
  93. bcm283x_peri_set_bits(addr, LOW << shift, 1 << shift);
  94. if(cfg->max_hz > BSP_SPI_MAX_HZ)
  95. {
  96. cfg->max_hz = BSP_SPI_MAX_HZ;
  97. }
  98. volatile rt_uint32_t clk_addr = (PER_BASE + BCM283X_SPI0_BASE) + BCM283X_SPI0_CLK;
  99. bcm283x_peri_write(clk_addr, cfg->max_hz);
  100. return RT_EOK;
  101. }
  102. static rt_err_t raspi_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  103. {
  104. RT_ASSERT(cfg != RT_NULL);
  105. RT_ASSERT(device != RT_NULL);
  106. struct rt_spi *hspi = (struct rt_spi *)&device->bus->parent;
  107. hspi->cfg = cfg;
  108. raspi_hostspi_init(cfg);
  109. return RT_EOK;
  110. }
  111. rt_uint8_t correct_order(rt_uint8_t b)
  112. {
  113. if (bcm2835_spi_bit_order == BCM283X_SPI_BIT_ORDER_LSBFIRST)
  114. return bcm2835_byte_reverse_table[b];
  115. else
  116. return b;
  117. }
  118. static rt_err_t spi_transfernb(rt_uint8_t* tbuf, rt_uint8_t* rbuf, rt_uint32_t len)
  119. {
  120. volatile rt_uint32_t paddr = SPI0_BASE_ADDR + BCM283X_SPI0_CS;
  121. volatile rt_uint32_t fifo = SPI0_BASE_ADDR + BCM283X_SPI0_FIFO;
  122. rt_uint32_t TXCnt=0;
  123. rt_uint32_t RXCnt=0;
  124. /* Clear TX and RX fifos */
  125. bcm283x_peri_set_bits(paddr, BCM283X_SPI0_CS_CLEAR, BCM283X_SPI0_CS_CLEAR);
  126. /* Set TA = 1 */
  127. bcm283x_peri_set_bits(paddr, BCM283X_SPI0_CS_TA, BCM283X_SPI0_CS_TA);
  128. /* Use the FIFO's to reduce the interbyte times */
  129. while ((TXCnt < len) || (RXCnt < len))
  130. {
  131. /* TX fifo not full, so add some more bytes */
  132. while (((bcm283x_peri_read(paddr) & BCM283X_SPI0_CS_TXD)) && (TXCnt < len))
  133. {
  134. bcm283x_peri_write_nb(fifo, correct_order(tbuf[TXCnt]));
  135. TXCnt++;
  136. }
  137. /* Rx fifo not empty, so get the next received bytes */
  138. while (((bcm283x_peri_read(paddr) & BCM283X_SPI0_CS_RXD)) && (RXCnt < len))
  139. {
  140. rbuf[RXCnt] = correct_order(bcm283x_peri_read_nb(fifo));
  141. RXCnt++;
  142. }
  143. }
  144. /* Wait for DONE to be set */
  145. while (!(bcm283x_peri_read_nb(paddr) & BCM283X_SPI0_CS_DONE));
  146. /* Set TA = 0, and also set the barrier */
  147. bcm283x_peri_set_bits(paddr, 0, BCM283X_SPI0_CS_TA);
  148. return RT_EOK;
  149. }
  150. static rt_uint32_t raspi_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  151. {
  152. rt_err_t res;
  153. RT_ASSERT(device != RT_NULL);
  154. RT_ASSERT(device->bus != RT_NULL);
  155. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  156. RT_ASSERT(message->send_buf != RT_NULL || message->recv_buf != RT_NULL);
  157. struct rt_spi *hspi = (struct rt_spi *)&device->bus->parent;
  158. /* only send data */
  159. if (message->recv_buf == RT_NULL)
  160. {
  161. if (message->cs_take)
  162. {
  163. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 0);
  164. }
  165. res = spi_transfernb((rt_uint8_t *)message->send_buf, RT_NULL,(rt_int32_t)message->length);
  166. if (message->cs_release)
  167. {
  168. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 1);
  169. }
  170. if (res != RT_EOK)
  171. return RT_ERROR;
  172. }
  173. /* only receive data */
  174. if (message->send_buf == RT_NULL)
  175. {
  176. if (message->cs_take)
  177. {
  178. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 0);
  179. }
  180. res = spi_transfernb(RT_NULL,(rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length);
  181. if (message->cs_release)
  182. {
  183. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 1);
  184. }
  185. if (res != RT_EOK)
  186. return RT_ERROR;
  187. }
  188. /* send & receive */
  189. else
  190. {
  191. if (message->cs_take)
  192. {
  193. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 0);
  194. }
  195. res = spi_transfernb((rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf,
  196. (rt_int32_t)message->length);
  197. if (message->cs_release)
  198. {
  199. bcm283x_gpio_write(hspi->hwcfg->cs_pin, 1);
  200. }
  201. if (res != RT_EOK)
  202. return RT_ERROR;
  203. }
  204. return message->length;
  205. }
  206. rt_err_t raspi_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin)
  207. {
  208. rt_err_t ret;
  209. rt_int16_t gpio_pin;
  210. struct rt_spi_device *spi_device;
  211. struct rt_sw_spi_cs *cs_pin;
  212. gpio_pin = phypin_index[pin].bcm_id;
  213. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  214. RT_ASSERT(spi_device != RT_NULL);
  215. cs_pin = (struct rt_sw_spi_cs *)rt_malloc(sizeof(struct rt_sw_spi_cs));
  216. RT_ASSERT(cs_pin != RT_NULL);
  217. cs_pin->pin = gpio_pin;
  218. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  219. return ret;
  220. }
  221. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  222. rt_uint16_t spi_clockdivider(rt_uint32_t speed_hz)
  223. {
  224. rt_uint16_t divider;
  225. if (speed_hz < (rt_uint32_t) BCM283X_AUX_SPI_CLOCK_MIN)
  226. {
  227. speed_hz = (rt_uint32_t) BCM283X_AUX_SPI_CLOCK_MIN;
  228. }
  229. else if (speed_hz > (rt_uint32_t) BCM283X_AUX_SPI_CLOCK_MAX)
  230. {
  231. speed_hz = (rt_uint32_t) BCM283X_AUX_SPI_CLOCK_MAX;
  232. }
  233. divider = (rt_uint16_t) DIV_ROUND_UP(BCM283X_CORE_CLK_HZ, 2 * speed_hz) - 1;
  234. if (divider > (rt_uint16_t) BCM283X_AUX_SPI_CNTL0_SPEED_MAX)
  235. {
  236. return (rt_uint16_t) BCM283X_AUX_SPI_CNTL0_SPEED_MAX;
  237. }
  238. return divider;
  239. }
  240. rt_err_t raspi_spi_hw_init(struct rt_spi_hw_config *hwcfg)
  241. {
  242. volatile rt_uint32_t enable = PER_BASE + BCM283X_AUX_BASE + BCM283X_AUX_ENABLE;
  243. volatile rt_uint32_t cntl0 = PER_BASE + BCM283X_SPI1_BASE + BCM283X_AUX_SPI_CNTL0;
  244. volatile rt_uint32_t cntl1 = PER_BASE + BCM283X_SPI1_BASE + BCM283X_AUX_SPI_CNTL1;
  245. bcm283x_gpio_fsel(hwcfg->sclk_pin, hwcfg->sclk_mode);
  246. bcm283x_gpio_fsel(hwcfg->miso_pin, hwcfg->miso_mode);
  247. bcm283x_gpio_fsel(hwcfg->mosi_pin, hwcfg->mosi_mode);
  248. bcm283x_gpio_fsel(hwcfg->cs_pin, hwcfg->cs_mode);
  249. hwcfg->clk_div = spi_clockdivider(1000000); // Default 1MHz SPI
  250. bcm283x_peri_write(enable, BCM283X_AUX_ENABLE_SPI0);
  251. bcm283x_peri_write(cntl1, 0);
  252. bcm283x_peri_write(cntl0, BCM283X_AUX_SPI_CNTL0_CLEARFIFO);
  253. return RT_EOK;
  254. }
  255. const static struct rt_spi_ops raspi_spi_ops =
  256. {
  257. .configure = raspi_spi_configure,
  258. .xfer = raspi_spi_xfer
  259. };
  260. #if defined (BSP_USING_SPI0)
  261. #define SPI0BUS_NAME "spi0.0"
  262. struct rt_spi_bus raspi_spi0_bus =
  263. {
  264. .ops = &raspi_spi_ops,
  265. .parent.user_data = spi0
  266. };
  267. struct rt_spi_hw_config raspi_spi0_hw =
  268. {
  269. .sclk_pin = BCM_GPIO_PIN_11,
  270. .sclk_mode = BCM283X_GPIO_FSEL_ALT0,
  271. .mosi_pin = BCM_GPIO_PIN_10,
  272. .mosi_mode = BCM283X_GPIO_FSEL_ALT0,
  273. .miso_pin = BCM_GPIO_PIN_9,
  274. .miso_mode = BCM283X_GPIO_FSEL_ALT0,
  275. .cs_pin = BCM_GPIO_PIN_8,
  276. .cs_mode = BCM283X_GPIO_FSEL_ALT0,
  277. .spi_base = (PER_BASE + BCM283X_SPI0_BASE),
  278. .clk_div = 0,
  279. };
  280. struct rt_spi spi0 =
  281. {
  282. .device_name = SPI0BUS_NAME,
  283. .spi_bus = &raspi_spi0_bus,
  284. .hwcfg = &raspi_spi0_hw,
  285. };
  286. #endif
  287. #if defined (BSP_USING_SPI1)
  288. #define SPI1BUS_NAME "spi0.1"
  289. struct rt_spi_bus raspi_spi1_bus =
  290. {
  291. .ops = &raspi_spi_ops,
  292. .parent.user_data = spi1
  293. };
  294. struct rt_spi_hw_config raspi_spi1_hw =
  295. {
  296. .sclk_pin = BCM_GPIO_PIN_11,
  297. .sclk_mode = BCM283X_GPIO_FSEL_ALT0,
  298. .mosi_pin = BCM_GPIO_PIN_10,
  299. .mosi_mode = BCM283X_GPIO_FSEL_ALT0,
  300. .miso_pin = BCM_GPIO_PIN_9,
  301. .miso_mode = BCM283X_GPIO_FSEL_ALT0,
  302. .cs_pin = BCM_GPIO_PIN_7,
  303. .cs_mode = BCM283X_GPIO_FSEL_ALT0,
  304. .spi_base = (PER_BASE + BCM283X_SPI0_BASE),
  305. .clk_div = 0,
  306. };
  307. struct rt_spi spi1 =
  308. {
  309. .device_name = SPI1BUS_NAME,
  310. .spi_bus = &raspi_spi1_bus,
  311. .hwcfg = &raspi_spi1_hw,
  312. };
  313. #endif
  314. int rt_hw_spi_bus_init(void)
  315. {
  316. #if defined (BSP_USING_SPI0)
  317. raspi_spi_hw_init(spi0.hwcfg);
  318. rt_spi_bus_register(spi0.spi_bus, spi0.device_name, spi0.spi_bus->ops);
  319. #endif
  320. #if defined (BSP_USING_SPI1)
  321. raspi_spi_hw_init(spi1.hwcfg);
  322. rt_spi_bus_register(spi1.spi_bus, spi1.device_name, spi1.spi_bus->ops);
  323. #endif
  324. return RT_EOK;
  325. }
  326. INIT_PREV_EXPORT(rt_hw_spi_bus_init);
  327. #endif