[c] DW1000
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include "nrf_drv_spi.h"
- #include "app_util_platform.h"
- #include "nrf_gpio.h"
- #include "nrf_delay.h"
- #include "boards.h"
- #include "app_error.h"
- #include <string.h>
- #include "nrf_log.h"
- #include "nrf_log_ctrl.h"
- #include "nrf_log_default_backends.h"
- #include "deca_device_api.h"
- #include "deca_regs.h"
- //#include "sleep.h"
- #define SPI_INSTANCE 0 /**< SPI instance index. */
- static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
- static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
- //#define TEST_STRING "0x00"
- //static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
- //static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
- //static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
- static uint8_t m_tx_buf[8] = {0x00}; /**< TX buffer. */
- static uint8_t m_rx_buf[254]; /**< RX buffer. */
- static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
- /**
- * @brief SPI user event handler.
- * @param event
- */
- void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
- void * p_context)
- {
- spi_xfer_done = true;
- NRF_LOG_INFO("Transfer completed.");
- if (m_rx_buf[0] != 0)
- {
- NRF_LOG_INFO(" Received:");
- NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
- }
- }
- static dwt_config_t config = {
- 5, /* Channel number. */
- DWT_PRF_64M, /* Pulse repetition frequency. */
- DWT_PLEN_1024, /* Preamble length. Used in TX only. */
- DWT_PAC32, /* Preamble acquisition chunk size. Used in RX only. */
- 9, /* TX preamble code. Used in TX only. */
- 9, /* RX preamble code. Used in RX only. */
- 1, /* 0 to use standard SFD, 1 to use non-standard SFD. */
- DWT_BR_110K, /* Data rate. */
- DWT_PHRMODE_STD, /* PHY header mode. */
- (1025 + 64 - 32) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
- };
- int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)
- {
- decaIrqStatus_t decamutexon(void)
- {
- // To be implemented if needed
- return 1 ;
- }
- void decamutexoff(decaIrqStatus_t s)
- {
- // To be implemented if needed
- }
- uint8 rxBodyBuff[readlength+headerLength];
- // Drive CS Low
- nrf_gpio_pin_write (23, 0);
- // Send/read data
- nrf_drv_spi_transfer (&spi, headerBuffer, headerLength, rxBodyBuff, readlength+headerLength);
- // Drive CS High
- nrf_gpio_pin_write (23, 1);
- // get "read part" of rxBodyBuff
- for(uint8 i=0;i<readlength;i++) readBuffer[i] = rxBodyBuff[headerLength+i];
- return 0;
- }
- int writetospi(uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer)
- {
- decaIrqStatus_t decamutexon(void)
- {
- // To be implemented if needed
- return 1 ;
- }
- void decamutexoff(decaIrqStatus_t s)
- {
- // To be implemented if needed
- }
- // prepare TX buffer
- uint8_t txBuff[headerLength+bodylength];
- for(uint8_t i=0;i<headerLength;i++) txBuff[i] = headerBuffer[i];
- for(uint8_t i=0;i<bodylength;i++) txBuff[headerLength+i] = bodyBuffer[i];
- // Drive CS Low
- nrf_gpio_pin_write (23, 0);
- // send data
- nrf_drv_spi_transfer (&spi, txBuff, headerLength+bodylength, NULL, 0);
- // Drive CS High
- nrf_gpio_pin_write (23, 1);
- return 0;
- }
- // required by decadrivers
- void sleep_ms(unsigned int time_ms)
- {
- nrf_delay_ms (time_ms);
- }
- void deca_sleep(unsigned int time_ms)
- {
- nrf_delay_ms (time_ms);
- }
- static uint8 tx_msg[] = {0x00, 0x00, 0x00};
- int main(void)
- {
- // error check throws errors given in application manual of ISP1510, page 34
- //if (dwt_initialise(DWT_LOADNONE) == DWT_ERROR)
- //{
- // while (1)
- // { };
- //}
- APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
- NRF_LOG_DEFAULT_BACKENDS_INIT();
- NRF_LOG_INFO("Test");
- nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
- spi_config.sck_pin = 8; // D12 - 0
- spi_config.miso_pin = 24; // D10 - 1
- spi_config.mosi_pin = 17; // D11 - 2
- spi_config.ss_pin = 23; // D9 - 3
- APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
- NRF_LOG_INFO("SPI example started.");
- printf("test");
- while (1)
- {
- // Reset rx buffer and transfer done flag
- memset(m_rx_buf, 0, m_length);
- //dwt_writetxdata(sizeof(m_tx_buf), m_tx_buf, 0); // the TX message data into the DW1000 TX buffer with 0 offset
- //dwt_writetxfctrl(sizeof(m_tx_buf), 0, 0); // used to configure the TX frame control register with 0 offsett
- //dwt_starttx(DWT_START_TX_IMMEDIATE); // start transmission
- //// poll DW1000 until TX frame sent event set.
- //while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) {
- //} ;
- // dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); // Cear TX frame sent event
- //nrf_delay_ms(10);
- //spi_xfer_done = false;
- // start spi transaction
- APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
- while (!spi_xfer_done)
- {
- __WFE(); // wait for event
- }
- NRF_LOG_FLUSH();
- // sleep ms
- sleep_ms(1000);
- }
- }
Editor
You can edit this paste and save as new: