[c] DW1000

Viewer

  1. #include "nrf_drv_spi.h"
  2. #include "app_util_platform.h"
  3. #include "nrf_gpio.h"
  4. #include "nrf_delay.h"
  5. #include "boards.h"
  6. #include "app_error.h"
  7. #include <string.h>
  8. #include "nrf_log.h"
  9. #include "nrf_log_ctrl.h"
  10. #include "nrf_log_default_backends.h"
  11. #include "deca_device_api.h"
  12. #include "deca_regs.h"
  13. //#include "sleep.h"
  14.  
  15. #define SPI_INSTANCE  0 /**< SPI instance index. */
  16. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
  17. static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
  18.  
  19.  
  20. //#define TEST_STRING "0x00"
  21. //static uint8_t       m_tx_buf[] = TEST_STRING;           /**< TX buffer. */
  22. //static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**< RX buffer. */
  23. //static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
  24.  
  25. static uint8_t       m_tx_buf[8] = {0x00};           /**< TX buffer. */
  26. static uint8_t       m_rx_buf[254];    /**< RX buffer. */
  27. static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
  28.  
  29. /**
  30.  * @brief SPI user event handler.
  31.  * @param event
  32.  */
  33. void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
  34.                        void *                    p_context)
  35. {
  36.     spi_xfer_done = true;
  37.     NRF_LOG_INFO("Transfer completed.");
  38.     if (m_rx_buf[0] != 0)
  39.     {
  40.         NRF_LOG_INFO(" Received:");
  41.         NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
  42.     }
  43. }
  44.  
  45.  
  46. static dwt_config_t config = {
  47.     5,               /* Channel number. */
  48.     DWT_PRF_64M,     /* Pulse repetition frequency. */
  49.     DWT_PLEN_1024,   /* Preamble length. Used in TX only. */
  50.     DWT_PAC32,       /* Preamble acquisition chunk size. Used in RX only. */
  51.     9,               /* TX preamble code. Used in TX only. */
  52.     9,               /* RX preamble code. Used in RX only. */
  53.     1,               /* 0 to use standard SFD, 1 to use non-standard SFD. */
  54.     DWT_BR_110K,     /* Data rate. */
  55.     DWT_PHRMODE_STD, /* PHY header mode. */
  56.     (1025 + 64 - 32) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
  57. };
  58.  
  59. int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)
  60. {
  61.  
  62.   decaIrqStatus_t decamutexon(void)
  63.   {
  64.     // To be implemented if needed
  65.     return 1 ;
  66.   }
  67.   void decamutexoff(decaIrqStatus_t s)
  68.   {
  69.     // To be implemented if needed
  70.   }
  71.  
  72.   uint8 rxBodyBuff[readlength+headerLength];
  73.   // Drive CS Low
  74.   nrf_gpio_pin_write (23, 0);
  75.   // Send/read data
  76.   nrf_drv_spi_transfer (&spi, headerBuffer, headerLength, rxBodyBuff, readlength+headerLength);
  77.   // Drive CS High
  78.   nrf_gpio_pin_write (23, 1);
  79.   // get "read part" of rxBodyBuff
  80.   for(uint8 i=0;i<readlength;i++) readBuffer[i] = rxBodyBuff[headerLength+i];
  81.   return 0;
  82. }
  83.  
  84. int writetospi(uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer)
  85. {
  86.  
  87.   decaIrqStatus_t decamutexon(void)
  88.   {
  89.     // To be implemented if needed
  90.     return 1 ;
  91.   }
  92.   void decamutexoff(decaIrqStatus_t s)
  93.   {
  94.     // To be implemented if needed
  95.   }
  96.   // prepare TX buffer
  97.   uint8_t txBuff[headerLength+bodylength];
  98.   for(uint8_t i=0;i<headerLength;i++) txBuff[i] = headerBuffer[i];
  99.   for(uint8_t i=0;i<bodylength;i++) txBuff[headerLength+i] = bodyBuffer[i];
  100.   // Drive CS Low
  101.   nrf_gpio_pin_write (23, 0);
  102.   // send data
  103.   nrf_drv_spi_transfer (&spi, txBuff, headerLength+bodylength, NULL, 0);
  104.   // Drive CS High
  105.   nrf_gpio_pin_write (23, 1);
  106.   return 0;
  107. }
  108.  
  109.  
  110. // required by decadrivers
  111.  void sleep_ms(unsigned int time_ms)
  112. {
  113.   nrf_delay_ms (time_ms);
  114. }
  115.  
  116. void deca_sleep(unsigned int time_ms)
  117. {
  118.   nrf_delay_ms (time_ms);
  119. }
  120.  
  121.  
  122.  
  123. static uint8 tx_msg[] = {0x00, 0x00, 0x00};
  124.  
  125. int main(void)
  126. {
  127.     // error check throws errors given in application manual of ISP1510, page 34
  128.     //if (dwt_initialise(DWT_LOADNONE) == DWT_ERROR)
  129.     //{
  130.     //    while (1)
  131.     //    { };
  132.     //}
  133.  
  134.     APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  135.     NRF_LOG_DEFAULT_BACKENDS_INIT();
  136.     NRF_LOG_INFO("Test");
  137.  
  138.     nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  139.     spi_config.sck_pin  = 8;  // D12 - 0
  140.     spi_config.miso_pin = 24; // D10 - 1
  141.     spi_config.mosi_pin = 17; // D11 - 2
  142.     spi_config.ss_pin   = 23; //  D9 - 3
  143.  
  144.     APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
  145.  
  146.     NRF_LOG_INFO("SPI example started.");
  147.  
  148.     printf("test");
  149.  
  150.     while (1)
  151.     {
  152.         // Reset rx buffer and transfer done flag
  153.         memset(m_rx_buf, 0, m_length);
  154.  
  155.         //dwt_writetxdata(sizeof(m_tx_buf), m_tx_buf, 0); //  the TX message data into the DW1000 TX buffer with 0 offset
  156.         //dwt_writetxfctrl(sizeof(m_tx_buf), 0, 0);         // used to configure the TX frame control register with 0 offsett
  157.         //dwt_starttx(DWT_START_TX_IMMEDIATE);              // start transmission
  158.  
  159.         
  160.         //// poll DW1000 until TX frame sent event set. 
  161.         //while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) {
  162.         //} ;
  163.  
  164.         // dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); // Cear TX frame sent event
  165.  
  166.         //nrf_delay_ms(10);
  167.         //spi_xfer_done = false;
  168.  
  169.         // start spi transaction
  170.         APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
  171.  
  172.         while (!spi_xfer_done)
  173.         {
  174.             __WFE(); // wait for event
  175.         }
  176.  
  177.         NRF_LOG_FLUSH();
  178.  
  179.         // sleep ms
  180.         sleep_ms(1000);
  181.     }
  182. }
  183.  

Editor

You can edit this paste and save as new:


File Description
  • DW1000
  • Paste Code
  • 20 May-2021
  • 5.47 Kb
You can Share it: