drv_spi.c 5.7 KB

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