drv_spi.c 13 KB

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