| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include "nrf_drv_spis.h"
- #include "ble_gap.h"
- #include "type.h"
- #include "queue.h"
- #include "spi.h"
- #include "flash.h"
- #include "nrf_delay.h"
- #define SPIS_CLK_PIN 1
- #define SPIS_MOSI_PIN 2
- #define SPIS_MISO_PIN 3
- #define SPIS_CS_PIN 4
- #define SPIS_INT_PIN 5
- #define SPIS_INSTANCE 1
- static const nrf_drv_spis_t spis = NRF_DRV_SPIS_INSTANCE(SPIS_INSTANCE);/**< SPIS insta nce. */
- volatile bool m_spi_recv_done = false; /**< Flag used to indicate that SPIS instance completed the transfer. */
- volatile bool spis_eint_on = false;
- volatile bool m_spi_send_done = true;
- static uint8_t m_tx_buf[MAX_BUF]; /**< TX buffer. */
- static uint8_t m_rx_buf[MAX_BUF]; /**< RX buffer. */
- extern queue_list_t g_s_spi_msg_recv_list;
- static int _time_out_count = 0;
- void _time_out_init(int time)
- {
- _time_out_count = time;
- }
- bool _time_out_check(void)
- {
- nrf_delay_us(1000);
- _time_out_count --;
- return (_time_out_count == 0) ? true:false;
- }
- #define TIME_OUT_MS_RESET(T) _time_out_init(T)
- #define TIME_OUT_CHECK() _time_out_check()
- static void spis_event_handler(nrf_drv_spis_event_t event)
- {
- // SPI transaction has been completed
- if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
- {
- if(spis_eint_on)
- {
- spis_eint_on = false;
- nrf_gpio_pin_clear(SPIS_INT_PIN);
- }
- if(event.rx_amount > 0)
- {
- if(m_rx_buf[0] == 0 && m_rx_buf[1] == 0) // this package is send to ST, and done, can send next
- {
- m_spi_send_done = true;
- }
- else
- {
- m_spi_recv_done = true;
- queue_in(&g_s_spi_msg_recv_list, m_rx_buf, event.rx_amount);
- }
- memset(m_rx_buf,0,MAX_BUF);
- }
- }
- }
- void spis_send_data(uint8_t* data, uint16_t length)
- {
- m_spi_send_done = false;
- TIME_OUT_MS_RESET(1000);
- memset(m_tx_buf, 0, MAX_BUF);
- memcpy(m_tx_buf, data, length);
- spis_eint_on = true;
- nrf_gpio_pin_set(SPIS_INT_PIN);
- while(!m_spi_send_done && !TIME_OUT_CHECK());
- }
- static void spis_inter_pin_init(void)
- {
- nrf_gpio_cfg_output(SPIS_INT_PIN);
- nrf_gpio_pin_clear(SPIS_INT_PIN);
- }
- void spis_init(void)
- {
- spis_inter_pin_init();
-
- nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE);
-
- spis_config.sck_pin = SPIS_CLK_PIN;
- spis_config.mosi_pin = SPIS_MOSI_PIN;
- spis_config.miso_pin = SPIS_MISO_PIN;
- spis_config.csn_pin = SPIS_CS_PIN;
- spis_config.csn_pullup = NRF_GPIO_PIN_PULLUP;
- spis_config.miso_drive = NRF_GPIO_PIN_H0H1;
- spis_config.mode = NRF_DRV_SPIS_MODE_3;
-
- APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));
- APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, m_tx_buf, MAX_BUF, m_rx_buf, MAX_BUF));
- }
- void spis_pin_reset(void)
- {
- if(spis_eint_on)
- {
- spis_eint_on = false;
- nrf_gpio_pin_clear(SPIS_INT_PIN);
- }
- }
|