drv_spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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-10-28 0xcccccccccccc Initial Version
  9. * 2021-01-17 0xcccccccccccc Bug Fixed : clock division cannot been adjusted as expected due to wrong register configuration.
  10. */
  11. /**
  12. * @addtogroup ls2k
  13. */
  14. /*@{*/
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <ctype.h>
  18. #include <stdint.h>
  19. #include <rtthread.h>
  20. #include <drivers/spi.h>
  21. #include "drv_spi.h"
  22. #ifdef RT_USING_SPI
  23. static void spi_init(uint8_t spre_spr, uint8_t copl, uint8_t cpha)
  24. {
  25. SET_SPI(SPSR, 0xc0);
  26. SET_SPI(PARAM, 0x40);
  27. SET_SPI(PARAM2, 0x01);
  28. SET_SPI(SPER, (spre_spr & 0b00001100) >> 2);
  29. SET_SPI(SPCR, 0x50 | copl << 3 | cpha << 2 | (spre_spr & 0b00000011));
  30. SET_SPI(SOFTCS, 0xff);
  31. }
  32. static void spi_set_csn(uint8_t val)
  33. {
  34. SET_SPI(SOFTCS, val);
  35. }
  36. #ifdef RT_USING_SPI_GPIOCS
  37. #include <drivers/pin.h>
  38. #endif
  39. static void spi_set_cs(unsigned char cs, int new_status)
  40. {
  41. if (cs < 4)
  42. {
  43. unsigned char val = 0;
  44. val = GET_SPI(SOFTCS);
  45. val |= 0x01 << cs ; // csen=1
  46. if (new_status) // cs = 1
  47. {
  48. val |= (0x10 << cs); // csn=1
  49. }
  50. else // cs = 0
  51. {
  52. val &= ~(0x10 << cs); // csn=0
  53. }
  54. SET_SPI(SOFTCS, val);
  55. return ;
  56. }
  57. #ifdef RT_USING_SPI_GPIOCS
  58. else
  59. {
  60. rt_pin_mode(cs, PIN_MODE_OUTPUT); // with RT_USING_SPI_GPIOCS feature enabled, gpio will be used as csn pin.
  61. rt_pin_write(cs, new_status);
  62. }
  63. #endif
  64. }
  65. static uint8_t spi_write_for_response(uint8_t data)
  66. {
  67. uint8_t val;
  68. SET_SPI(TXFIFO, data);
  69. while ((GET_SPI(SPSR))&RFEMPTY); //wait for echo
  70. val = GET_SPI(RXFIFO);
  71. return val;
  72. }
  73. static int cmd_spi_init(int argc, char *argv[])
  74. {
  75. uint8_t spre_spr, cpol, cpha;
  76. switch (argc)
  77. {
  78. case 2:
  79. spre_spr = strtoul(argv[1], NULL, 0);
  80. spi_init(spre_spr, 0, 0);
  81. break;
  82. case 4:
  83. spre_spr = strtoul(argv[1], NULL, 0);
  84. cpol = strtoul(argv[2], NULL, 0);
  85. cpha = strtoul(argv[3], NULL, 0);
  86. spi_init(spre_spr, 0, 0);
  87. break;
  88. default:
  89. printf("\nusage : cmd_spi_init spre_spr <cpol> <cpha>\n(cmd_spi_init 0x4 0x0 0x0)\n0x4:div8 0xb:div4096\n");
  90. break;
  91. }
  92. }
  93. MSH_CMD_EXPORT(cmd_spi_init, cmd_spi_init);
  94. static int cmd_spi_set_csn(int argc, char *argv[])
  95. {
  96. uint8_t val, csn;
  97. switch (argc)
  98. {
  99. case 3:
  100. csn = strtoul(argv[1], NULL, 0);
  101. val = strtoul(argv[2], NULL, 0);
  102. spi_set_cs(csn, val);
  103. break;
  104. default:
  105. printf("usage:cmd_spi_set_csn csn val\n(0xbf for csn1 enable,0xff for csn1 disable)\n");
  106. break;
  107. }
  108. }
  109. MSH_CMD_EXPORT(cmd_spi_set_csn, cmd_spi_set_csn);
  110. static int cmd_spi_write(int argc, char *argv[])
  111. {
  112. uint8_t data, resp;
  113. switch (argc)
  114. {
  115. case 2:
  116. data = strtoul(argv[1], NULL, 0);
  117. resp = spi_write_for_response(data);
  118. printf("resp:%2X\n", resp);
  119. break;
  120. default:
  121. printf("usage:cmd_spi_write data\n");
  122. break;
  123. }
  124. }
  125. MSH_CMD_EXPORT(cmd_spi_write, cmd_spi_write);
  126. static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  127. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  128. const static unsigned char SPI_DIV_TABLE[] = {0b0000, 0b0001, 0b0100, 0b0010, 0b0011, 0b0101, 0b0110, 0b0111, 0b1000, 0b1001, 0b1010, 0b1011};
  129. // 2 4 8 16 32 64 128 256 512 1024 2048 4096
  130. static rt_err_t configure(struct rt_spi_device *device,
  131. struct rt_spi_configuration *configuration)
  132. {
  133. unsigned char cpol = 0;
  134. unsigned char cpha = 0;
  135. RT_ASSERT(NULL != device);
  136. RT_ASSERT(NULL != configuration);
  137. // baudrate
  138. if (configuration->mode & RT_SPI_CPOL) // cpol
  139. {
  140. cpol = 1;
  141. }
  142. else
  143. {
  144. cpol = 0;
  145. }
  146. if (configuration->mode & RT_SPI_CPHA) // cpha
  147. {
  148. cpha = 1;
  149. }
  150. else
  151. {
  152. cpha = 0;
  153. }
  154. float spi_max_speed = ((float)APB_MAX_SPEED) / (8.0 / (float)APB_FREQSCALE);
  155. uint64_t div = (uint64_t)(spi_max_speed / (float)configuration->max_hz);
  156. int ctr = 0;
  157. while (div != 1 && ctr < 12)
  158. {
  159. ctr++;
  160. div = div >> 1;
  161. }
  162. spi_init(SPI_DIV_TABLE[ctr], cpol, cpha);
  163. return RT_EOK;
  164. }
  165. static rt_uint32_t xfer(struct rt_spi_device *device,
  166. struct rt_spi_message *message)
  167. {
  168. unsigned char cs = 0;
  169. rt_uint32_t size = 0;
  170. const rt_uint8_t *send_ptr = NULL;
  171. rt_uint8_t *recv_ptr = NULL;
  172. rt_uint8_t data = 0;
  173. RT_ASSERT(NULL != device);
  174. RT_ASSERT(NULL != message);
  175. cs = (unsigned char)(device->parent.user_data);
  176. size = message->length;
  177. if (message->cs_take)
  178. {
  179. spi_set_cs(cs, 0);
  180. }
  181. // send data
  182. send_ptr = message->send_buf;
  183. recv_ptr = message->recv_buf;
  184. while (size--)
  185. {
  186. data = 0xFF;
  187. if (NULL != send_ptr)
  188. {
  189. data = *send_ptr++;
  190. }
  191. if (NULL != recv_ptr)
  192. {
  193. *recv_ptr++ = spi_write_for_response(data);
  194. }
  195. else
  196. {
  197. spi_write_for_response(data);
  198. }
  199. }
  200. // release cs
  201. if (message->cs_release)
  202. {
  203. spi_set_cs(cs, 1);
  204. }
  205. return message->length;
  206. }
  207. static struct rt_spi_ops loongson_spi_ops =
  208. {
  209. .configure = configure,
  210. .xfer = xfer
  211. };
  212. static struct rt_spi_bus loongson_spi;
  213. static int loongson_spi_init()
  214. {
  215. //rt_kprintf("spi_init\n");
  216. return rt_spi_bus_register(&loongson_spi, "spi", &loongson_spi_ops);
  217. }
  218. INIT_BOARD_EXPORT(loongson_spi_init);
  219. #endif
  220. /*@}*/