I'm having an issue with SPI communication. The problem is quite simple: if I use the Zephyr driver, everything works fine, but if I use the nrfx driver, it doesn't.
The function I call to read a register, Spi_Write_And_Read, using the Zephyr driver is:
&spi2 {
compatible = "nordic,nrf-spim";
status = "okay";
pinctrl-0 = <&spi2_default>;
pinctrl-1 = <&spi2_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 24 GPIO_ACTIVE_LOW>;
ad7779: ad7779@0 {
compatible = "adi,ad7779";
// compatible = "spi-device";
reg = <0>;
spi-max-frequency = <8000000>;
};
};
static struct spi_dt_spec ad779SpiSpec = SPI_DT_SPEC_GET( DT_NODELABEL( ad7779 ), SPI_WORD_SET( 8 ) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA, 0 );
spi_is_ready_dt( &ad779SpiSpec );
static int Spi_Write_And_Read( uint8_t *data, uint16_t len )
{
const struct spi_buf spiTxBuff = { .buf = data, .len = len };
const struct spi_buf spiRxBuff = { .buf = data, .len = len };
const struct spi_buf_set tx = { .buffers = &spiTxBuff, .count = 1 };
const struct spi_buf_set rx = { .buffers = &spiRxBuff, .count = 1 };
return spi_transceive_dt( &ad779SpiSpec, &tx, &rx );
}
where *data is a pointer to a 2-byte buffer, with the first byte set to the value of the register I want to read, OR'ed with 0x80. This function works well and returns the buffer with the register value in the second byte.
However, if I modify the function to use:
&spi2 {
compatible = "nordic,nrf-spim";
status = "okay";
pinctrl-0 = <&spi2_default>;
pinctrl-1 = <&spi2_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 24 GPIO_ACTIVE_LOW>;
ad7779: ad7779@0 {
compatible = "adi,ad7779";
// compatible = "spi-device";
reg = <0>;
spi-max-frequency = <8000000>;
};
};
static const nrfx_spim_t ad779SpiInstance = NRFX_SPIM_INSTANCE( 2 );
static nrfx_spim_config_t spiConfig = NRFX_SPIM_DEFAULT_CONFIG( AD7779_SCK_PIN, AD7779_MOSI_PIN, AD7779_MISO_PIN, AD7779_CS_PIN );
nrfx_spim_init( &ad779SpiInstance, &spiConfig, NULL, NULL );
static int Spi_Write_And_Read( uint8_t *data, uint16_t len )
{
nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX( data, len, data, len );
nrfx_err_t err_code = nrfx_spim_xfer( &ad779SpiInstance, &xfer_desc, 0 );
return ( err_code == NRFX_SUCCESS ) ? 0 : -1;
}
I only read 0xFF.
I checked with an oscilloscope and the SCK, MOSI, and MISO signals are identical in both cases. I'm starting to think it's a configuration issue, but I believe I've tried everything.
Do you have any suggestions?