drv_spi.c 5.7 KB

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