Hello,
I am attempting to use the function spi_write_dt to write data (configuration files and lots of data) to a specific register. is there a better function for SPI that would do what I need it to?
This is my read function and I think it is working...
BMA4_INTF_RET_TYPE bma4_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) { tx.buf = ®_addr; tx.len = sizeof(reg_addr); printf("tx.buf read = %x\n", tx.buf); rx.buf = (uint8_t *)reg_data; rx.len = len; rslt= spi_transceive_dt(&dev_spi, &tx_bufs, &rx_bufs); reg_data = rx.buf; //<-Does this do what I think? return rslt; }
I want the read to something similar to the I2C. the reg_data needs to hold the read info from the register for the program to work properly.
BMA4_INTF_RET_TYPE bma4_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr) { uint8_t buffer[] = {reg_addr, *reg_data}; rslt = i2c_write_read_dt(&dev_i2c, &buffer[0], 1, reg_data, len); return rslt; }
Here is my attempted write function but I am stuck on how to put the register address in front of the data being sent.
The device tree:
&spi1 { // compatible = "nordic,nrf-spim"; status = "okay"; pinctrl-0 = <&spi1_default>; pinctrl-1 = <&spi1_sleep>; // pinctrl-names = "default", "sleep"; cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>; a: bma456-a@0 { compatible = "revel,spi-device"; status = "okay"; reg = <0>; spi-max-frequency = <7000000>; //spi-max-frequency = <900000>; }; };
Here are some of the definitions:
#define SPI1_NODE DT_NODELABEL(a) #define SPI_OP SPI_OP_MODE_MASTER | SPI_WORD_SET(8) | SPI_TRANSFER_MSB static const struct spi_dt_spec dev_spi = SPI_DT_SPEC_GET(SPI1_NODE, SPI_OP, 0); static struct spi_buf rx; const static struct spi_buf_set rx_bufs = { .buffers = &rx, .count = 1, }; static struct spi_buf tx; const static struct spi_buf_set tx_bufs = { .buffers = &tx, .count = 1, }; BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { tx.buf = ®_addr; printf("tx.buf = %x\n", tx.buf); tx.len = sizeof(reg_addr); rslt= spi_write_dt(&dev_spi, &tx_bufs); tx.buf = (uint8_t *)reg_data; printf("tx.buf 2= %x\n", tx.buf); tx.len = len; rslt= spi_write_dt(&dev_spi, &tx_bufs); return rslt; }
The biggest problem is trying to figure out how to write data to a specific register address. I want it to do what the I2c version does.
BMA4_INTF_RET_TYPE bma4_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { uint8_t buffer[] = {reg_addr, *reg_data}; rslt= i2c_burst_write_dt(&dev_i2c, buffer[0], (uint8_t *)reg_data, len); return rslt; }
BMA4_INTF_RET_TYPE bma4_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr) { uint8_t txbuffer[2] = {reg_addr, *reg_data}; struct spi_buf tx = { .buf = txbuffer, .len = sizeof(txbuffer),}; struct spi_buf_set tx_bufs = { .buffers = &tx, .count = 1,}; rslt= spi_write_dt(&dev_spi, &tx_bufs); return rslt; }
Any help would be much appreciated!!
Best regards,
Jared