drv_spi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * 2020-06-22 bigmagic first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include "raspi4.h"
  14. #include "drv_spi.h"
  15. #ifdef RT_USING_SPI
  16. #define RPI_CORE_CLK_HZ (500 * 1000 * 1000)
  17. #define SPITIMEOUT 0x0FFF
  18. static rt_uint8_t raspi_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. #if defined (BSP_USING_SPI0_BUS)
  54. #define SPI0_BUS_NAME "spi0"
  55. #define SPI0_DEVICE0_NAME "spi0.0"
  56. #define SPI0_DEVICE1_NAME "spi0.1"
  57. struct rt_spi_bus spi0_bus;
  58. #if defined (BSP_USING_SPI0_DEVICE0)
  59. static struct rt_spi_device spi0_device0;
  60. #endif
  61. #if defined (BSP_USING_SPI0_DEVICE1)
  62. static struct rt_spi_device spi0_device1;
  63. #endif
  64. #endif
  65. static rt_err_t raspi_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  66. {
  67. RT_ASSERT(cfg != RT_NULL);
  68. RT_ASSERT(device != RT_NULL);
  69. struct raspi_spi_device* hw_config = (struct raspi_spi_device *)(device->parent.user_data);
  70. struct raspi_spi_hw_config *hwcfg = (struct raspi_spi_hw_config *)hw_config->spi_hw_config;
  71. // spi clear fifo
  72. SPI_REG_CS(hwcfg->hw_base) |= (SPI_CS_CLEAR_TX | SPI_CS_CLEAR_RX);
  73. if(cfg->mode & RT_SPI_CPOL)
  74. {
  75. SPI_REG_CS(hwcfg->hw_base) |= SPI_CS_CPOL;
  76. }
  77. else
  78. {
  79. SPI_REG_CS(hwcfg->hw_base) &= ~SPI_CS_CPOL;
  80. }
  81. if(cfg->mode & RT_SPI_CPHA)
  82. {
  83. SPI_REG_CS(hwcfg->hw_base) |= SPI_CS_CPHA;
  84. }else
  85. {
  86. SPI_REG_CS(hwcfg->hw_base) &= ~SPI_CS_CPHA;
  87. }
  88. if(cfg->mode & RT_SPI_CS_HIGH)
  89. {
  90. SPI_REG_CS(hwcfg->hw_base) |= SPI_CS_CSPOL_HIGH;
  91. }
  92. else
  93. {
  94. SPI_REG_CS(hwcfg->hw_base) &= ~SPI_CS_CSPOL_HIGH;
  95. }
  96. return RT_EOK;
  97. }
  98. rt_uint8_t correct_order(rt_uint8_t b, rt_uint8_t flag)
  99. {
  100. if (flag)
  101. return raspi_byte_reverse_table[b];//reverse
  102. else
  103. return b;
  104. }
  105. static rt_err_t spi_transfernb(struct raspi_spi_hw_config *hwcfg, rt_uint8_t* tbuf, rt_uint8_t* rbuf, rt_uint32_t len, rt_uint8_t flag)
  106. {
  107. rt_uint32_t TXCnt=0;
  108. rt_uint32_t RXCnt=0;
  109. /* Use the FIFO's to reduce the interbyte times */
  110. while ((TXCnt < len) || (RXCnt < len))
  111. {
  112. /* TX fifo not full, so add some more bytes */
  113. while (((SPI_REG_CS(hwcfg->hw_base) & SPI_CS_TX_DATA)) && (TXCnt < len))
  114. {
  115. SPI_REG_FIFO(hwcfg->hw_base) = correct_order(tbuf[TXCnt],flag);
  116. TXCnt++;
  117. }
  118. /* Rx fifo not empty, so get the next received bytes */
  119. while (((SPI_REG_CS(hwcfg->hw_base) & SPI_CS_RX_DATA)) && (RXCnt < len))
  120. {
  121. rbuf[RXCnt] = correct_order(SPI_REG_FIFO(hwcfg->hw_base), flag);
  122. RXCnt++;
  123. }
  124. }
  125. /* Wait for DONE to be set */
  126. while (!(SPI_REG_CS(hwcfg->hw_base) & SPI_CS_DONE));
  127. return RT_EOK;
  128. }
  129. static rt_ssize_t raspi_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  130. {
  131. rt_err_t res;
  132. rt_uint8_t flag;
  133. RT_ASSERT(device != RT_NULL);
  134. RT_ASSERT(device->bus != RT_NULL);
  135. RT_ASSERT(device->parent.user_data != RT_NULL);
  136. RT_ASSERT(message->send_buf != RT_NULL || message->recv_buf != RT_NULL);
  137. struct rt_spi_configuration config = device->config;
  138. struct raspi_spi_device * hw_config = (struct raspi_spi_device *)device->parent.user_data;
  139. GPIO_PIN cs_pin = (GPIO_PIN)hw_config->cs_pin;
  140. struct raspi_spi_hw_config *hwcfg = (struct raspi_spi_hw_config *)hw_config->spi_hw_config;
  141. //mode MSB
  142. if (config.mode & RT_SPI_MSB)
  143. {
  144. flag = 1;
  145. }
  146. else
  147. {
  148. flag = 0;
  149. }
  150. //max_hz
  151. if(config.max_hz == 0)
  152. {
  153. SPI_REG_CLK(hwcfg->hw_base) = 0;
  154. }
  155. else
  156. {
  157. SPI_REG_CLK(hwcfg->hw_base) = (RPI_CORE_CLK_HZ / (config.max_hz));
  158. }
  159. //cs_pin spi0.0
  160. if(cs_pin == GPIO_PIN_8)
  161. {
  162. SPI_REG_CS(hwcfg->hw_base) &= (~(3 << 0));
  163. }
  164. else if(cs_pin == GPIO_PIN_7)//spi0.1
  165. {
  166. SPI_REG_CS(hwcfg->hw_base) |= SPI_CS_CHIP_SELECT_1;
  167. }
  168. //Clear TX and RX fifos
  169. SPI_REG_CS(hwcfg->hw_base) |= (SPI_CS_CLEAR_TX | SPI_CS_CLEAR_RX);
  170. if (message->cs_take)
  171. {
  172. SPI_REG_CS(hwcfg->hw_base) |= SPI_CS_TA;
  173. }
  174. res = spi_transfernb(hwcfg, (rt_uint8_t *)message->send_buf, (rt_uint8_t *)message->recv_buf, (rt_int32_t)message->length, flag);
  175. if (message->cs_release)
  176. {
  177. //Set TA = 0, and also set the barrier
  178. SPI_REG_CS(hwcfg->hw_base) &= (~SPI_CS_TA);
  179. }
  180. if (res != RT_EOK)
  181. return res;
  182. return message->length;
  183. }
  184. rt_err_t raspi_spi_bus_attach_device(const char *bus_name, struct raspi_spi_device *device)
  185. {
  186. rt_err_t ret;
  187. RT_ASSERT(device != RT_NULL);
  188. ret = rt_spi_bus_attach_device(device->spi_device, device->device_name, bus_name, (void *)(device));
  189. return ret;
  190. }
  191. rt_err_t raspi_spi_hw_init(struct raspi_spi_hw_config *hwcfg)
  192. {
  193. prev_raspi_pin_mode(hwcfg->sclk_pin, hwcfg->sclk_mode);
  194. prev_raspi_pin_mode(hwcfg->miso_pin, hwcfg->miso_mode);
  195. prev_raspi_pin_mode(hwcfg->mosi_pin, hwcfg->mosi_mode);
  196. #if defined (BSP_USING_SPI0_DEVICE0)
  197. prev_raspi_pin_mode(hwcfg->ce0_pin, hwcfg->ce0_mode);
  198. #endif
  199. #if defined (BSP_USING_SPI0_DEVICE1)
  200. prev_raspi_pin_mode(hwcfg->ce1_pin, hwcfg->ce1_mode);
  201. #endif
  202. //clear rx and tx
  203. SPI_REG_CS(hwcfg->hw_base) = (SPI_CS_CLEAR_TX | SPI_CS_CLEAR_RX);
  204. return RT_EOK;
  205. }
  206. static struct rt_spi_ops raspi_spi_ops =
  207. {
  208. .configure = raspi_spi_configure,
  209. .xfer = raspi_spi_xfer
  210. };
  211. struct raspi_spi_hw_config raspi_spi0_hw =
  212. {
  213. .spi_num = 0,
  214. .sclk_pin = GPIO_PIN_11,
  215. .sclk_mode = ALT0,
  216. .mosi_pin = GPIO_PIN_10,
  217. .mosi_mode = ALT0,
  218. .miso_pin = GPIO_PIN_9,
  219. .miso_mode = ALT0,
  220. #if defined (BSP_USING_SPI0_DEVICE0)
  221. .ce0_pin = GPIO_PIN_8,
  222. .ce0_mode = ALT0,
  223. #endif
  224. #if defined (BSP_USING_SPI0_DEVICE1)
  225. .ce1_pin = GPIO_PIN_7,
  226. .ce1_mode = ALT0,
  227. #endif
  228. .hw_base = SPI_0_BASE,
  229. };
  230. #endif
  231. #if defined (BSP_USING_SPI0_DEVICE0)
  232. struct raspi_spi_device raspi_spi0_device0 =
  233. {
  234. .device_name = SPI0_DEVICE0_NAME,
  235. .spi_bus = &spi0_bus,
  236. .spi_device = &spi0_device0,
  237. .spi_hw_config = &raspi_spi0_hw,
  238. .cs_pin = GPIO_PIN_8,
  239. };
  240. #endif
  241. #if defined (BSP_USING_SPI0_DEVICE1)
  242. struct raspi_spi_device raspi_spi0_device1 =
  243. {
  244. .device_name = SPI0_DEVICE1_NAME,
  245. .spi_bus = &spi0_bus,
  246. .spi_device = &spi0_device1,
  247. .spi_hw_config = &raspi_spi0_hw,
  248. .cs_pin = GPIO_PIN_7,
  249. };
  250. #endif
  251. int rt_hw_spi_init(void)
  252. {
  253. #if defined (BSP_USING_SPI0_BUS)
  254. raspi_spi_hw_init(&raspi_spi0_hw);
  255. rt_spi_bus_register(&spi0_bus, SPI0_BUS_NAME, &raspi_spi_ops);
  256. #if defined (BSP_USING_SPI0_DEVICE0)
  257. raspi_spi_bus_attach_device(SPI0_BUS_NAME, &raspi_spi0_device0);
  258. #endif
  259. #if defined (BSP_USING_SPI0_DEVICE1)
  260. raspi_spi_bus_attach_device(SPI0_BUS_NAME, &raspi_spi0_device1);
  261. #endif
  262. #endif
  263. return RT_EOK;
  264. }
  265. INIT_DEVICE_EXPORT(rt_hw_spi_init);