1
0

drv_spi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * File : drv_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-06-05 tanek first implementation.
  13. */
  14. #include "drv_spi.h"
  15. #include <board.h>
  16. #include <finsh.h>
  17. #ifdef RT_USING_SPI
  18. #if !defined(RT_USING_SPI0) && !defined(RT_USING_SPI1) && \
  19. !defined(RT_USING_SPI2) && !defined(RT_USING_SPI3) && \
  20. !defined(RT_USING_SPI4) && !defined(RT_USING_SPI5)
  21. #error "Please define at least one SPIx"
  22. #endif
  23. //#define DEBUG
  24. #define ARR_LEN(__N) (sizeof(__N) / sizeof(__N[0]))
  25. #ifdef DEBUG
  26. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  27. #else
  28. #define DEBUG_PRINTF(...)
  29. #endif
  30. /* private rt-thread spi ops function */
  31. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  32. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);
  33. static struct rt_spi_ops gd32_spi_ops =
  34. {
  35. configure,
  36. xfer
  37. };
  38. static rt_err_t configure(struct rt_spi_device* device,
  39. struct rt_spi_configuration* configuration)
  40. {
  41. struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
  42. struct gd32f4_spi *f4_spi = (struct gd32f4_spi *)spi_bus->parent.user_data;
  43. spi_parameter_struct spi_init_struct;
  44. uint32_t spi_periph = f4_spi->spi_periph;
  45. RT_ASSERT(device != RT_NULL);
  46. RT_ASSERT(configuration != RT_NULL);
  47. /* data_width */
  48. if(configuration->data_width <= 8)
  49. {
  50. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  51. }
  52. else if(configuration->data_width <= 16)
  53. {
  54. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  55. }
  56. else
  57. {
  58. return RT_EIO;
  59. }
  60. /* baudrate */
  61. {
  62. rcu_clock_freq_enum spi_src;
  63. uint32_t spi_apb_clock;
  64. uint32_t max_hz;
  65. max_hz = configuration->max_hz;
  66. DEBUG_PRINTF("sys freq: %d\n", HAL_RCC_GetSysClockFreq());
  67. DEBUG_PRINTF("pclk2 freq: %d\n", HAL_RCC_GetPCLK2Freq());
  68. DEBUG_PRINTF("max freq: %d\n", max_hz);
  69. if (spi_periph == SPI1 || spi_periph == SPI2)
  70. {
  71. spi_src = CK_APB1;
  72. }
  73. else
  74. {
  75. spi_src = CK_APB2;
  76. }
  77. spi_apb_clock = rcu_clock_freq_get(spi_src);
  78. if(max_hz >= spi_apb_clock/2)
  79. {
  80. spi_init_struct.prescale = SPI_PSC_2;
  81. }
  82. else if (max_hz >= spi_apb_clock/4)
  83. {
  84. spi_init_struct.prescale = SPI_PSC_4;
  85. }
  86. else if (max_hz >= spi_apb_clock/8)
  87. {
  88. spi_init_struct.prescale = SPI_PSC_8;
  89. }
  90. else if (max_hz >= spi_apb_clock/16)
  91. {
  92. spi_init_struct.prescale = SPI_PSC_16;
  93. }
  94. else if (max_hz >= spi_apb_clock/32)
  95. {
  96. spi_init_struct.prescale = SPI_PSC_32;
  97. }
  98. else if (max_hz >= spi_apb_clock/64)
  99. {
  100. spi_init_struct.prescale = SPI_PSC_64;
  101. }
  102. else if (max_hz >= spi_apb_clock/128)
  103. {
  104. spi_init_struct.prescale = SPI_PSC_128;
  105. }
  106. else
  107. {
  108. /* min prescaler 256 */
  109. spi_init_struct.prescale = SPI_PSC_256;
  110. }
  111. } /* baudrate */
  112. switch(configuration->mode)
  113. {
  114. case RT_SPI_MODE_0:
  115. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  116. break;
  117. case RT_SPI_MODE_1:
  118. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  119. break;
  120. case RT_SPI_MODE_2:
  121. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE;
  122. break;
  123. case RT_SPI_MODE_3:
  124. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  125. break;
  126. }
  127. /* MSB or LSB */
  128. if(configuration->mode & RT_SPI_MSB)
  129. {
  130. spi_init_struct.endian = SPI_ENDIAN_MSB;
  131. }
  132. else
  133. {
  134. spi_init_struct.endian = SPI_ENDIAN_LSB;
  135. }
  136. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  137. spi_init_struct.device_mode = SPI_MASTER;
  138. spi_init_struct.nss = SPI_NSS_SOFT;
  139. spi_crc_off(spi_periph);
  140. /* init SPI */
  141. spi_init(spi_periph, &spi_init_struct);
  142. /* Enable SPI_MASTER */
  143. spi_enable(spi_periph);
  144. return RT_EOK;
  145. };
  146. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
  147. {
  148. struct rt_spi_bus * gd32_spi_bus = (struct rt_spi_bus *)device->bus;
  149. struct gd32f4_spi *f4_spi = (struct gd32f4_spi *)gd32_spi_bus->parent.user_data;
  150. struct rt_spi_configuration * config = &device->config;
  151. struct gd32_spi_cs * gd32_spi_cs = device->parent.user_data;
  152. uint32_t spi_periph = f4_spi->spi_periph;
  153. RT_ASSERT(device != NULL);
  154. RT_ASSERT(message != NULL);
  155. /* take CS */
  156. if(message->cs_take)
  157. {
  158. gpio_bit_reset(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin);
  159. DEBUG_PRINTF("spi take cs\n");
  160. }
  161. {
  162. if(config->data_width <= 8)
  163. {
  164. const rt_uint8_t * send_ptr = message->send_buf;
  165. rt_uint8_t * recv_ptr = message->recv_buf;
  166. rt_uint32_t size = message->length;
  167. DEBUG_PRINTF("spi poll transfer start: %d\n", size);
  168. while(size--)
  169. {
  170. rt_uint8_t data = 0xFF;
  171. if(send_ptr != RT_NULL)
  172. {
  173. data = *send_ptr++;
  174. }
  175. // Todo: replace register read/write by gd32f4 lib
  176. //Wait until the transmit buffer is empty
  177. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  178. // Send the byte
  179. spi_i2s_data_transmit(spi_periph, data);
  180. //Wait until a data is received
  181. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  182. // Get the received data
  183. data = spi_i2s_data_receive(spi_periph);
  184. if(recv_ptr != RT_NULL)
  185. {
  186. *recv_ptr++ = data;
  187. }
  188. }
  189. DEBUG_PRINTF("spi poll transfer finsh\n");
  190. }
  191. else if(config->data_width <= 16)
  192. {
  193. const rt_uint16_t * send_ptr = message->send_buf;
  194. rt_uint16_t * recv_ptr = message->recv_buf;
  195. rt_uint32_t size = message->length;
  196. while(size--)
  197. {
  198. rt_uint16_t data = 0xFF;
  199. if(send_ptr != RT_NULL)
  200. {
  201. data = *send_ptr++;
  202. }
  203. //Wait until the transmit buffer is empty
  204. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
  205. // Send the byte
  206. spi_i2s_data_transmit(spi_periph, data);
  207. //Wait until a data is received
  208. while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
  209. // Get the received data
  210. data = spi_i2s_data_receive(spi_periph);
  211. if(recv_ptr != RT_NULL)
  212. {
  213. *recv_ptr++ = data;
  214. }
  215. }
  216. }
  217. }
  218. /* release CS */
  219. if(message->cs_release)
  220. {
  221. gpio_bit_set(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin);
  222. DEBUG_PRINTF("spi release cs\n");
  223. }
  224. return message->length;
  225. };
  226. static struct rt_spi_bus spi_bus[];
  227. static const struct gd32f4_spi spis[] = {
  228. #ifdef RT_USING_SPI0
  229. {SPI0, RCU_SPI0, &spi_bus[0]},
  230. #endif
  231. #ifdef RT_USING_SPI1
  232. {SPI1, RCU_SPI1, &spi_bus[1]},
  233. #endif
  234. #ifdef RT_USING_SPI2
  235. {SPI2, RCU_SPI2, &spi_bus[2]},
  236. #endif
  237. #ifdef RT_USING_SPI3
  238. {SPI3, RCU_SPI3, &spi_bus[3]},
  239. #endif
  240. #ifdef RT_USING_SPI4
  241. {SPI4, RCU_SPI4, &spi_bus[4]},
  242. #endif
  243. #ifdef RT_USING_SPI5
  244. {SPI5, RCU_SPI5, &spi_bus[5]},
  245. #endif
  246. };
  247. static struct rt_spi_bus spi_bus[ARR_LEN(spis)];
  248. /** \brief init and register gd32 spi bus.
  249. *
  250. * \param SPI: gd32 SPI, e.g: SPI1,SPI2,SPI3.
  251. * \param spi_bus_name: spi bus name, e.g: "spi1"
  252. * \return
  253. *
  254. */
  255. rt_err_t gd32_spi_bus_register(uint32_t spi_periph,
  256. //struct gd32_spi_bus * gd32_spi,
  257. const char * spi_bus_name)
  258. {
  259. int i;
  260. RT_ASSERT(spi_bus_name != RT_NULL);
  261. for (i = 0; i < ARR_LEN(spis); i++)
  262. {
  263. if (spi_periph == spis[i].spi_periph)
  264. {
  265. rcu_periph_clock_enable(spis[i].spi_clk);
  266. spis[i].spi_bus->parent.user_data = (void *)&spis[i];
  267. rt_spi_bus_register(spis[i].spi_bus, spi_bus_name, &gd32_spi_ops);
  268. return RT_EOK;
  269. }
  270. }
  271. return RT_ERROR;
  272. }
  273. #endif