spi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "nrf_drv_spis.h"
  2. #include "ble_gap.h"
  3. #include "type.h"
  4. #include "queue.h"
  5. #include "spi.h"
  6. #include "flash.h"
  7. #include "nrf_delay.h"
  8. #define SPIS_CLK_PIN 1
  9. #define SPIS_MOSI_PIN 2
  10. #define SPIS_MISO_PIN 3
  11. #define SPIS_CS_PIN 4
  12. #define SPIS_INT_PIN 5
  13. #define SPIS_INSTANCE 1
  14. static const nrf_drv_spis_t spis = NRF_DRV_SPIS_INSTANCE(SPIS_INSTANCE);/**< SPIS insta nce. */
  15. volatile bool m_spi_recv_done = false; /**< Flag used to indicate that SPIS instance completed the transfer. */
  16. volatile bool spis_eint_on = false;
  17. volatile bool m_spi_send_done = true;
  18. static uint8_t m_tx_buf[MAX_BUF]; /**< TX buffer. */
  19. static uint8_t m_rx_buf[MAX_BUF]; /**< RX buffer. */
  20. extern queue_list_t g_s_spi_msg_recv_list;
  21. static int _time_out_count = 0;
  22. void _time_out_init(int time)
  23. {
  24. _time_out_count = time;
  25. }
  26. bool _time_out_check(void)
  27. {
  28. nrf_delay_us(1000);
  29. _time_out_count --;
  30. return (_time_out_count == 0) ? true:false;
  31. }
  32. #define TIME_OUT_MS_RESET(T) _time_out_init(T)
  33. #define TIME_OUT_CHECK() _time_out_check()
  34. static void spis_event_handler(nrf_drv_spis_event_t event)
  35. {
  36. // SPI transaction has been completed
  37. if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
  38. {
  39. if(spis_eint_on)
  40. {
  41. spis_eint_on = false;
  42. nrf_gpio_pin_clear(SPIS_INT_PIN);
  43. }
  44. if(event.rx_amount > 0)
  45. {
  46. if(m_rx_buf[0] == 0 && m_rx_buf[1] == 0) // this package is send to ST, and done, can send next
  47. {
  48. m_spi_send_done = true;
  49. }
  50. else
  51. {
  52. m_spi_recv_done = true;
  53. queue_in(&g_s_spi_msg_recv_list, m_rx_buf, event.rx_amount);
  54. }
  55. memset(m_rx_buf,0,MAX_BUF);
  56. }
  57. }
  58. }
  59. void spis_send_data(uint8_t* data, uint16_t length)
  60. {
  61. m_spi_send_done = false;
  62. TIME_OUT_MS_RESET(1000);
  63. memset(m_tx_buf, 0, MAX_BUF);
  64. memcpy(m_tx_buf, data, length);
  65. spis_eint_on = true;
  66. nrf_gpio_pin_set(SPIS_INT_PIN);
  67. while(!m_spi_send_done && !TIME_OUT_CHECK());
  68. }
  69. static void spis_inter_pin_init(void)
  70. {
  71. nrf_gpio_cfg_output(SPIS_INT_PIN);
  72. nrf_gpio_pin_clear(SPIS_INT_PIN);
  73. }
  74. void spis_init(void)
  75. {
  76. spis_inter_pin_init();
  77. nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE);
  78. spis_config.sck_pin = SPIS_CLK_PIN;
  79. spis_config.mosi_pin = SPIS_MOSI_PIN;
  80. spis_config.miso_pin = SPIS_MISO_PIN;
  81. spis_config.csn_pin = SPIS_CS_PIN;
  82. spis_config.csn_pullup = NRF_GPIO_PIN_PULLUP;
  83. spis_config.miso_drive = NRF_GPIO_PIN_H0H1;
  84. spis_config.mode = NRF_DRV_SPIS_MODE_3;
  85. APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));
  86. APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, m_tx_buf, MAX_BUF, m_rx_buf, MAX_BUF));
  87. }
  88. void spis_pin_reset(void)
  89. {
  90. if(spis_eint_on)
  91. {
  92. spis_eint_on = false;
  93. nrf_gpio_pin_clear(SPIS_INT_PIN);
  94. }
  95. }