rtthread_driver_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #ifndef APPLICATIONS_HCI_INTERFACE_C_
  2. #define APPLICATIONS_HCI_INTERFACE_C_
  3. #include <rtthread.h>
  4. #include <rtdevice.h>
  5. #include "rtthread_driver_spi.h"
  6. #include "drivers/hci_driver.h"
  7. #include "drivers/hci_h4.h"
  8. #include "logging/bt_log_impl.h"
  9. #include "common/bt_buf.h"
  10. struct hci_trans_spi_config {
  11. const char *device_name; /* spi device name, i.e. "spi1" */
  12. const char *bus_name; /* spi BUS name, i.e. "spi10" */
  13. int cs_pin_num; /* RT-Thread drv_gpio number of SPI CS PIN */
  14. int irq_pin_num; /* RT-Thread drv_gpio number of SPI IRQ PIN */
  15. uint32_t rate;
  16. int data_width; /* Data width: 8bits, 16bits, 32bits */
  17. int LSB_MSB; /* Data transmission order: LSB:0 MSB:1 */
  18. int Master_Slave; /* Set the master-slave mode of SPI: Master:0 Slave:1 */
  19. int CPOL; /* Set clock polarity(CPOL): 0 1 */
  20. int CPHA; /* Set clock phase(CPHA): 0 1 */
  21. };
  22. static struct hci_trans_spi_config hci_config;
  23. struct rt_spi_device *ble_spi = RT_NULL;
  24. int32_t IsDataAvailable(void);
  25. void hci_driver_init_loop(void);
  26. int32_t HCI_TL_SPI_Init(void* pConf)
  27. {
  28. /******PIN******/
  29. rt_pin_mode(hci_config.irq_pin_num, PIN_MODE_INPUT); // IRQ input
  30. rt_pin_mode(hci_config.cs_pin_num, PIN_MODE_OUTPUT); // CS output
  31. /*******SPI******/
  32. RT_ASSERT(hci_config.device_name);
  33. RT_ASSERT(hci_config.bus_name);
  34. ble_spi = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  35. if(RT_NULL == ble_spi)
  36. {
  37. printk("Failed to malloc the spi device.\n");
  38. return -RT_ENOMEM;
  39. }
  40. if (RT_EOK != rt_spi_bus_attach_device(ble_spi, hci_config.device_name, hci_config.bus_name, RT_NULL))
  41. {
  42. printk("Failed to attach the spi device.\n");
  43. return -RT_ERROR;
  44. }
  45. /*******SPI Device Config*******/
  46. struct rt_spi_configuration cfg;
  47. cfg.data_width = hci_config.data_width;
  48. cfg.mode = (hci_config.Master_Slave << 3) |
  49. ((hci_config.CPOL << 1) | (hci_config.CPHA << 0)) |
  50. (hci_config.LSB_MSB << 2);
  51. cfg.max_hz = hci_config.rate; /* 1M */
  52. if (RT_EOK != rt_spi_configure(ble_spi, &cfg)) {
  53. printk("Failed to config the spi device.\n");
  54. return -RT_ERROR;
  55. }
  56. return RT_EOK;
  57. }
  58. /**
  59. * @brief Reads from BlueNRG SPI buffer and store data into local buffer.
  60. *
  61. * @param buffer : Buffer where data from SPI are stored
  62. * @param size : Buffer size
  63. * @retval int32_t: Number of read bytes
  64. */
  65. int32_t HCI_TL_SPI_Receive(uint8_t* buffer, uint16_t size)
  66. {
  67. uint16_t byte_count;
  68. uint8_t len = 0;
  69. uint8_t char_00 = 0x00;
  70. volatile uint8_t read_char;
  71. uint8_t header_master[HEADER_SIZE] = {0x0b, 0x00, 0x00, 0x00, 0x00};
  72. uint8_t header_slave[HEADER_SIZE];
  73. uint32_t tickstart_data_available = rt_tick_get();
  74. /* CS reset */
  75. rt_pin_write(hci_config.cs_pin_num, PIN_LOW);
  76. /*check transfer Available*/
  77. while(!IsDataAvailable())
  78. {
  79. if((rt_tick_get() - tickstart_data_available) > TIMEOUT_DURATION)
  80. {
  81. printk(" SPI Recv timeout : %d\r\n", rt_tick_get() - tickstart_data_available);
  82. return -3;
  83. }
  84. }
  85. /* Read the header */
  86. rt_spi_transfer(ble_spi, &header_master, &header_slave, HEADER_SIZE); //RTAPI
  87. /* device is ready */
  88. byte_count = (header_slave[4] << 8)| header_slave[3];
  89. if(byte_count > 0)
  90. {
  91. /* avoid to read more data than the size of the buffer */
  92. if (byte_count > size)
  93. {
  94. byte_count = size;
  95. }
  96. for(len = 0; len < byte_count; len++)
  97. {
  98. rt_spi_transfer(ble_spi, &char_00, (uint8_t*)&read_char, 1);
  99. buffer[len] = read_char;
  100. }
  101. }
  102. /**
  103. * To be aligned to the SPI protocol.
  104. * Can bring to a delay inside the frame, due to the BlueNRG-2 that needs
  105. * to check if the header is received or not.
  106. */
  107. uint32_t tickstart = rt_tick_get();
  108. while ((rt_tick_get() - tickstart) < TIMEOUT_IRQ_HIGH) {
  109. if (rt_pin_read(hci_config.irq_pin_num) == PIN_LOW) {
  110. break;
  111. }
  112. }
  113. /* Release CS line */
  114. rt_pin_write(hci_config.cs_pin_num, PIN_HIGH);
  115. tickstart = rt_tick_get();
  116. while ((rt_tick_get() - tickstart) < TIMEOUT_HIGH) {
  117. if (rt_pin_read(hci_config.irq_pin_num) == PIN_HIGH) {
  118. break;
  119. }
  120. }
  121. return len;
  122. }
  123. /**
  124. * @brief Writes data from local buffer to SPI.
  125. *
  126. * @param buffer : data buffer to be written
  127. * @param size : size of first data buffer to be written
  128. * @retval int32_t: Number of read bytes
  129. */
  130. int32_t HCI_TL_SPI_Send(uint8_t* buffer, uint16_t size)
  131. {
  132. int32_t result;
  133. uint16_t rx_bytes;
  134. uint8_t char_00 = 0x00;
  135. volatile uint8_t read_char;
  136. uint8_t header_master[HEADER_SIZE] = {0x0a, 0x00, 0x00, 0x00, 0x00};
  137. uint8_t header_slave[HEADER_SIZE];
  138. static uint8_t read_char_buf[MAX_BUFFER_SIZE];
  139. uint32_t tickstart = rt_tick_get();
  140. do
  141. {
  142. uint32_t tickstart_data_available = rt_tick_get();
  143. result = 0;
  144. /* CS reset */
  145. rt_pin_write(hci_config.cs_pin_num, PIN_LOW);
  146. /*
  147. * Wait until BlueNRG-2 is ready.
  148. * When ready it will raise the IRQ pin.
  149. */
  150. while(!IsDataAvailable())
  151. {
  152. if((rt_tick_get() - tickstart_data_available) > TIMEOUT_DURATION)
  153. {
  154. printk("SPI Send timeout %d\r\n", rt_tick_get() - tickstart_data_available);
  155. result = -3;
  156. break;
  157. }
  158. }
  159. if(result == -3)
  160. {
  161. /* The break causes the exiting from the "while", so the CS line must be released */
  162. rt_pin_write(hci_config.cs_pin_num, PIN_HIGH);
  163. break;
  164. }
  165. /* Read header */
  166. rt_spi_transfer(ble_spi, &header_master, &header_slave, HEADER_SIZE);
  167. rx_bytes = (((uint16_t)header_slave[2])<<8) | ((uint16_t)header_slave[1]);
  168. uint16_t byte_count = (header_slave[4] << 8)| header_slave[3];
  169. if (byte_count > 0)
  170. {
  171. /* Release CS line */
  172. rt_pin_write(hci_config.cs_pin_num, PIN_HIGH);
  173. /* to end the send, we need a delay */
  174. rt_thread_delay(1);
  175. hci_driver_init_loop();
  176. result = -2;
  177. }
  178. else
  179. {
  180. if(rx_bytes >= size)
  181. {
  182. /* Buffer is big enough */
  183. rt_spi_transfer(ble_spi, buffer, &read_char_buf, size);
  184. }
  185. else
  186. {
  187. /* Buffer is too small */
  188. result = -2;
  189. }
  190. /* Release CS line */
  191. rt_pin_write(hci_config.cs_pin_num, PIN_HIGH);
  192. }
  193. } while(result < 0);
  194. /**
  195. * To be aligned to the SPI protocol.
  196. * Can bring to a delay inside the frame, due to the BlueNRG-2 that needs
  197. * to check if the header is received or not.
  198. */
  199. tickstart = rt_tick_get();
  200. while ((rt_tick_get() - tickstart) < TIMEOUT_IRQ_HIGH) {
  201. if (rt_pin_read(hci_config.irq_pin_num) == PIN_LOW) {
  202. break;
  203. }
  204. }
  205. return result;
  206. }
  207. /**
  208. * @brief Reports if the BlueNRG has data for the host micro.
  209. *
  210. * @param None
  211. * @retval int32_t: 1 if data are present, 0 otherwise
  212. */
  213. int32_t IsDataAvailable(void)
  214. {
  215. return (rt_pin_read(hci_config.irq_pin_num) == PIN_HIGH);
  216. }
  217. static int hci_driver_open(void)
  218. {
  219. HCI_TL_SPI_Init(NULL); // RT SPI init
  220. printk("hci_driver_open, SPI_config_finish\n");
  221. return 0;
  222. }
  223. int switch_net_buf_type(uint8_t type)
  224. {
  225. switch (type)
  226. {
  227. case BT_BUF_ACL_OUT:
  228. return HCI_ACLDATA_PKT;
  229. case BT_BUF_CMD:
  230. return HCI_COMMAND_PKT;
  231. default:
  232. printk("Unknown buffer type");
  233. }
  234. return 0;
  235. }
  236. static int hci_driver_send(struct net_buf *buf)
  237. {
  238. uint8_t type = bt_buf_get_type(buf);
  239. // net_buf_push_u8(buf, type);
  240. // uint8_t* data = buf->data;
  241. uint8_t len = buf->len;
  242. if(len >= MAX_BUFFER_SIZE)
  243. {
  244. return -1;
  245. }
  246. uint8_t data[MAX_BUFFER_SIZE];
  247. data[0] = switch_net_buf_type(type);
  248. memcpy(data + 1, buf->data, len); //data[0] is the type copy to data[1]
  249. // printk("hci_driver_send, type: %d, len: %d, data: %02x:%02x:%02x:%02x:%02x:%02x\n", type, len, data[0], data[1], data[2], data[3], data[4], data[5]);
  250. if (HCI_TL_SPI_Send(data, len + 1) < 0) { // type +1
  251. return -1;
  252. }
  253. net_buf_unref(buf);
  254. return 0;
  255. }
  256. static int hci_driver_recv(uint8_t *buf, uint16_t len)
  257. {
  258. return HCI_TL_SPI_Receive(buf, len);
  259. }
  260. void hci_driver_init_loop(void)
  261. {
  262. uint8_t data[MAX_BUFFER_SIZE];
  263. uint8_t len = MAX_BUFFER_SIZE;
  264. int ret = HCI_TL_SPI_Receive(data, len); //ret: bytes num Recv
  265. if(ret > 0 && (data[0] != 0)) // type cant be 0
  266. {
  267. // printk("hci_driver_init_loop, ret: %d, data: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  268. // ret, data[0], data[1], data[2], data[3], data[4], data[5],data[6],data[7]);
  269. struct net_buf *buf;
  270. switch(data[0])
  271. {
  272. case HCI_EVENT_PKT:
  273. buf = bt_buf_get_controller_tx_evt();
  274. break;
  275. case HCI_ACLDATA_PKT:
  276. buf = bt_buf_get_controller_tx_acl();
  277. break;
  278. default:
  279. return;
  280. }
  281. if (buf)
  282. {
  283. net_buf_add_mem(buf, data + 1, ret - 1);
  284. bt_recv(buf);
  285. }
  286. }
  287. }
  288. static const struct bt_hci_driver drv = {
  289. .open = hci_driver_open,
  290. .send = hci_driver_send,
  291. };
  292. static char device_name[NAME_SIZE];
  293. static char bus_name[NAME_SIZE];
  294. int hci_driver_init(bt_BlueNRG_SPI_interface_t *hci_cfg, int device_idx, int spi_index)
  295. {
  296. rt_sprintf(bus_name, "spi%d", spi_index);
  297. hci_config.bus_name = bus_name;
  298. rt_sprintf(device_name, "spi%d%d", spi_index, device_idx);
  299. hci_config.device_name = device_name;
  300. hci_config.rate = hci_cfg->rate;
  301. hci_config.data_width = hci_cfg->data_width;
  302. hci_config.LSB_MSB = hci_cfg->LSB_MSB;
  303. hci_config.Master_Slave = hci_cfg->Master_Slave;
  304. hci_config.CPOL = hci_cfg->CPOL;
  305. hci_config.CPHA = hci_cfg->CPHA;
  306. hci_config.cs_pin_num = hci_cfg->cs_pin_num;
  307. hci_config.irq_pin_num = hci_cfg->irq_pin_num;
  308. printk("SPI_init_process device_name: %s, spi_name: %s, rate: %d, databits: %d, LSB_MSB: %d, Master_Slave: %d, CPOL: %d, CPHA: %d\n",
  309. hci_config.device_name, hci_config.bus_name, hci_config.rate, hci_config.data_width,
  310. hci_config.LSB_MSB, hci_config.Master_Slave, hci_config.CPOL, hci_config.CPHA);
  311. printk("SPI_init_process cs_pin_num: %d, irq_pin_num: %d\n",
  312. hci_config.cs_pin_num, hci_config.irq_pin_num);
  313. if (bt_hci_driver_register(&drv) != 0)
  314. {
  315. return -1;
  316. }
  317. return 0;
  318. }
  319. #endif /* APPLICATIONS_HCI_INTERFACE_C_ */