Other Parts Discussed in Thread: CC1310
Greetings
I am attempting to write functions to condense SPI communication between a CC1310 and a LPS22HB pressure sensor. Instead of continuously initialising parameters etc., I would like to have a function for r/w functions using SPI.
For my SPI_Write function:
bool SPI_Write(SPI_Handle spi_handle, SPI_Params spi_params, SPI_Transaction spi_transaction, uint8_t size, uint8_t *txBuffer) { SPI_Params_init(&spi_params); spi_params.transferMode = SPI_MODE_BLOCKING; spi_params.bitRate = 10000000; spi_params.dataSize = 8; spi_handle = SPI_open(0, &spi_params); if (!spi_handle) { // Error opening SPI return false; } spi_transaction.count = size; spi_transaction.txBuf = txBuffer; spi_transaction.rxBuf = NULL; if(!SPI_transfer(spi_handle, &spi_transaction)) { return false; } SPI_close(spi_handle); return true; }
For my SPI_Read function:
bool SPI_Read(SPI_Handle spi_handle, SPI_Params spi_params, SPI_Transaction spi_transaction, uint8_t size, uint8_t *rxBuffer) { SPI_Params_init(&spi_params); spi_params.transferMode = SPI_MODE_BLOCKING; spi_params.bitRate = 10000000; spi_params.dataSize = 8; spi_handle = SPI_open(0, &spi_params); if (!spi_handle) { // Error opening SPI return false; } spi_transaction.count = size; spi_transaction.txBuf = NULL; spi_transaction.rxBuf = rxBuffer; if(!SPI_transfer(spi_handle, &spi_transaction)) { false; } SPI_close(spi_handle); return true; }
In turn, my driver for the LPS22HB consists of:
SPI_Transaction LPS22HB_SpiTransactionHandle; SPI_Handle LPS22HB2_SpiHandle; SPI_Params LPS22HB_SpiParams; #define TOGGLE_CS GPIO_write(Board_GPIO_TEST_DIO25, 0) #define RELEASE_CS GPIO_write(Board_GPIO_TEST_DIO25, 1) static void LPS22HB_Mem_Write(uint8_t reg, uint8_t *dataW, uint8_t size) { uint8_t spiReg = reg; TOGGLE_CS; //sets the CS low SPI_Write(LPS22HB_SpiHandle, LPS22HB_SpiParams, LPS22HB_SpiTransactionHandle, 1, &spiReg); SPI_Write(LPS22HB_SpiHandle, LPS22HB_SpiParams, LPS22HB_SpiTransactionHandle, size, dataW); RELEASE_CS; //sets the CS high } static void LPS22HB_Mem_Read(uint8_t reg, uint8_t *dataR, uint8_t size) { uint8_t spiBuf[1] = {0}; spiBuf[0] = reg | 0x80; uint8_t i; TOGGLE_CS; //sets the CS low SPI_Write(LPS22HB_SpiHandle, LPS22HB_SpiParams, LPS22HB_SpiTransactionHandle, 1, spiBuf); SPI_Read(LPS22HB_SpiHandle, LPS22HB_SpiParams, LPS22HB_SpiTransactionHandle, size, dataR); RELEASE_CS; //sets the CS high }
I am then trying to read the chip_id ("Who am I" of the sensor), which should be 0xB1, using the following code segment:
LPS22HB_Mem_Read(0x0f, ui8dummy, 1); uint8_t ui8dummy[2] if(strcmp(ui8dummy, "D") != 0) { return false; }
When I read the chip-ID, I get zero, when I expect a value of 0xb1, given by the datasheet. I have debugged the SPI R/W code extensively. I have added breakpoints to the SPI_W/R functions and can guarantee that no errors are raised (the functions do not return false). I am using a software chip-select, given by the "toggle" and "release" macros. I can confirm my CS and clock works, as I've checked this on a scope. I am using the uartEcho example from the CC1310 SDK of version 20.02.07.
Are there any glaring errors in my driver for it to not work?