Here is my RP2040 code to read from the DW1000 module and write to the DW1000 module.
#include “main.h”
DW1000 inst_DW1000;
TaskHandle_t irq_process_Handle = NULL;
void irq_process(void* args);
TaskHandle_t main_thread_Handle = NULL;
void main_thread(void* args);
int main()
{
stdio_init_all();
sleep_ms(1000);
gpio_init(25);
gpio_set_dir(25 , GPIO_OUT);
/* Configuring ADS1198 */
inst_DW1000.configure = DW1000_configure;
inst_DW1000.configure(&inst_DW1000);
xTaskCreate(irq_process, "IRQ Process", 500, NULL, 2, &irq_process_Handle);
xTaskCreate(main_thread, "main thread", 1024, NULL, 2, &main_thread_Handle);
vTaskStartScheduler();
while (1)
{
tight_loop_contents();
}
return 0;
}
void DW1000_configure(struct DW1000* instance)
{
// printf(“DW_Configure Run\r\n”);
/* SPI : GPIO , HOST and HOST Handler */
instance->spi._gpio.sclk_pin = DW_SCLK_Pin;
instance->spi._gpio.mosi_pin = DW_MOSI_Pin;
instance->spi._gpio.miso_pin = DW_MISO_Pin;
instance->spi._gpio.cs_pin = DW_CS_Pin;
instance->spi._gpio.irq_pin = DW_IRQ_Pin;
instance->spi._gpio.reset_pin = DW_RESET_Pin;
instance->spi.host = spi0;
/* Methods */
instance->IRQ_Callback = DW1000_IRQ_Callback;
instance->readBytes = DW1000_readBytes;
instance->writeBytes = DW1000_writeBytes;
/* GPIO Init */
/* CS as OUTPUT */
gpio_init(instance->spi._gpio.cs_pin);
gpio_set_dir(instance->spi._gpio.cs_pin, GPIO_OUT);
sleep_ms(1);
/* IRQ as Input with Interrupt */
gpio_init(instance->spi._gpio.irq_pin);
gpio_pull_down(instance->spi._gpio.irq_pin);
gpio_set_dir(instance->spi._gpio.irq_pin, GPIO_IN);
gpio_set_irq_enabled_with_callback(instance->spi._gpio.irq_pin, GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)instance->IRQ_Callback);
/*SPI initialization */
spi_init(instance->spi.host, 16*1000*1000);
spi_set_format(instance->spi.host, 8, SPI_CPOL_0, SPI_CPHA_1, SPI_MSB_FIRST);
// Initialize SPI pins
gpio_set_function(instance->spi._gpio.sclk_pin, GPIO_FUNC_SPI);
gpio_set_function(instance->spi._gpio.mosi_pin, GPIO_FUNC_SPI);
gpio_set_function(instance->spi._gpio.miso_pin, GPIO_FUNC_SPI);
sleep_ms(10);
}
void DW1000_IRQ_Callback(uint gpio, uint32_t events)
{
if(gpio == inst_DW1000.spi._gpio.irq_pin)
{
inst_DW1000.irq_flag_status = 1;
}
}
void irq_process(void* args)
{
while (1)
{
if(inst_DW1000.irq_flag_status == 1)
{
inst_DW1000.irq_flag_status = 0;
printf(“Hello\r\n”);
// do some process here
////////////////////////
}
vTaskDelay(10/ portTICK_PERIOD_MS);
}
}
void DW1000_readBytes(uint8_t cmd, uint16_t offset, uint8_t rxData[], uint16_t rxLen)
{
uint8_t header[3];
uint8_t headerLen = 1;
uint16_t i = 0;
// build SPI header
if (offset == 0xFF)
{
header[0] = 0x00 | cmd;
}
else
{
header[0] = 0x40 | cmd;
if (offset < 128)
{
header[1] = (uint8_t)offset;
headerLen++;
}
else
{
header[1] = 0x80 | (uint8_t)offset;
header[2] = (uint8_t)(offset >> 7);
headerLen += 2;
}
}
gpio_put(inst_DW1000.spi._gpio.cs_pin, 0); // Set CS pin low to select the DWM1000 module
spi_write_blocking(inst_DW1000.spi.host, &header[0], headerLen); // Send the read command
spi_read_blocking(inst_DW1000.spi.host, 0x00, &rxData[0], rxLen); // Read the response
sleep_us(5);
gpio_put(inst_DW1000.spi._gpio.cs_pin, 1); // Set CS pin high to select the DWM1000 module
}
void main_thread(void* args)
{
uint8_t rxData[4];
while (1)
{
/* Read DEVICE ID register */
inst_DW1000.readBytes(0x00, 0xFF, rxData, 4);
printf(“DEVICE ID:\r\n”);
for(int i = 0 ; i < 4 ; i++)
{
printf(“rxData[%d] : %X \t”, i, rxData[i]);
}
printf(“\r\n”);
sleep_ms(10);
/* Read SYS_CFG register */
printf("SYS_CFG:\r\n");
inst_DW1000.readBytes(0x04, 0xFF, rxData, 4);
for(int i = 0 ; i < 4 ; i++)
{
printf("rxData[%d] : %X \t", i, rxData[i]);
}
printf("\r\n");
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void DW1000_writeBytes(uint8_t cmd, uint16_t offset, uint8_t data[], uint16_t txLen)
{
uint8_t header[3];
uint8_t headerLen = 1;
uint16_t i = 0;
// TODO proper error handling: address out of bounds
// build SPI header
if (offset == 0xFF)
{
header[0] = 0x80 | cmd;
}
else
{
header[0] = 0xC0 | cmd;
if (offset < 128)
{
header[1] = (uint8_t)offset;
headerLen++;
}
else
{
header[1] = 0x80 | (uint8_t)offset;
header[2] = (uint8_t)(offset >> 7);
headerLen += 2;
}
}
size_t sz = headerLen + txLen;
uint8_t txData[sz];
// Copy elements from header to txData
for (int i = 0; i < headerLen; i++)
{
txData[i] = header[i];
}
// Copy elements from data to txData
for (int i = 0; i < txLen; i++)
{
txData[headerLen + i] = data[i];
}
gpio_put(inst_DW1000.spi._gpio.cs_pin, 0); // Set CS pin low to select the DWM1000 module
spi_write_blocking(inst_DW1000.spi.host, &txData[0], sz); // send header and write values
sleep_us(5);
gpio_put(inst_DW1000.spi._gpio.cs_pin, 1); // Set CS pin high to select the DWM1000 module
}
Please check the result of that: