too1 / ncs-spi-master-slave-example

17 stars 4 forks source link

Result fo spi_transceive_async() is incorrect #4

Closed snowuyl closed 1 year ago

snowuyl commented 1 year ago

Results of spi_transceive_async() are always 0xff. But it should be 0x33 for reading WHO_AM_I (0Fh) register of lis2dh12. 00> Booting Zephyr OS build v3.3.99-ncs1 00> SPI master/slave example started 00> SPI TX: 0x0f 00> SPI RX: 0xff 00> SPI TX: 0x0f 00> SPI RX: 0xff 00> SPI TX: 0x0f 00> SPI RX: 0xff

snowuyl commented 1 year ago
whoami
snowuyl commented 1 year ago

lis2dh12.pdf

snowuyl commented 1 year ago

I have fixed this issue by changing _lis2dh12RegRead() as follows.

define LIS2DH12_REG_READ_MSK 0xC0

static int _lis2dh12RegRead(const uint8_t regAddr, uint8_t* regVal) { int ret; uint8_t txBuf[1] = { regAddr | LIS2DH12_REG_READ_MSK };

const struct spi_buf txSpiBuf = {
    .buf = txBuf,
    .len = sizeof(txBuf)
};

const struct spi_buf_set txSpiBufSet = {
    .buffers = &txSpiBuf,
    .count   = 1
};

uint8_t rxBuf[2];
struct spi_buf rxSpiBuf = {
    .buf = rxBuf,
    .len = sizeof(rxBuf)
};
const struct spi_buf_set rxSpiBufSet = { .buffers = &rxSpiBuf, .count = 1 };
ret = spi_transceive(_gSpiDev, &_gSpiCfg, &txSpiBufSet, &rxSpiBufSet);
if (ret < 0) {
    printk("Reading LIS2DH12 register failed, regAddr=0x%x, err=%d\n", regAddr, ret);
    return ERROR_GSENSOR_READ_FAIL;
}

*regVal = rxBuf[1];

return 0;

}