Spi master

Thread Starter

Optical_Arrest

Joined Nov 14, 2023
1
I am reading data from sensor through spi in verilog. The sensor stores its conversion result in three registers with the address 0XC, 0XD,OXE. To read these 8 bit registers i have to send the adress of the first register then it autoincrements. How to do that eitj multibyte read in
 

russelmann

Joined Oct 28, 2019
4
I believe a sequence like this should work:
MOSI: [CMD],[ADDR],[DUMMY],[DUMMY],[DUMMY]
MISO: [XXX],[XXX],[RD_DATA_1],[RD_DATA_2],[RD_DATA_3]

Where each "[ ... ]" represents a 8 bit transfer ...
 

drjohsmith

Joined Dec 13, 2021
1,601
I am reading data from sensor through spi in verilog. The sensor stores its conversion result in three registers with the address 0XC, 0XD,OXE. To read these 8 bit registers i have to send the adress of the first register then it autoincrements. How to do that eitj multibyte read in
Many ways to do said, but this might be your easiest,
A) find code for an spi master, try opencores.org etc all
This will probably have a parallel bus and pins for spi,
B) instantiate your code for the spi master in your cide
C) write state machine in your code to drive the spi master,
How long do you have to hand in this homework ?
 

TommasoB

Joined Nov 26, 2023
27
I am reading data from sensor through spi in verilog. The sensor stores its conversion result in three registers with the address 0XC, 0XD,OXE. To read these 8 bit registers i have to send the adress of the first register then it autoincrements. How to do that eitj multibyte read in
Which kind of language are you using? Which is the sensor? Did you read the datasheet? Did you check for similar sensors if is there an already working library?

An example for a pico 2040 reading from an MPU9250 in C++ follows:

//! Read raw acc in counts
void MPU9250::mpu9250_read_raw_acc(int16_t *acc_p)
{
/*Used to get the raw acceleration values from the mpu*/
uint8_t buffer[6];

// Start reading acceleration registers from register 0x3B for 6 bytes
this->read_registers(ACCEL_XOUT_H, buffer, 6); // See read_registers

for (int i = 0; i < 3; i++)
{
*(acc_p + i) = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);
}
}

//! Read mpu9250 register
void MPU9250::read_registers(uint8_t reg, uint8_t *buf, uint16_t len)
{
reg |= READ_BIT; //The first bit of the first byte contains the Read/Write bit and indicates the Read (1) or Write (0) operation.
this->cs_select();
spi_write_blocking(SPI_PORT_MPU, &reg, 1); // Define the register to be read and activate reading
sleep_us(100);
spi_read_blocking(SPI_PORT_MPU, 0, buf, len);
this->cs_deselect();
}

//! Select chip SPI
void MPU9250::cs_select()
{
asm volatile("nop \n nop \n nop"); //Delay to set PIN_CS_MPU
gpio_put(PIN_CS_MPU, 0); // Active low
asm volatile("nop \n nop \n nop");
}

//! Deselect chip SPI
void MPU9250::cs_deselect()
{
asm volatile("nop \n nop \n nop"); //Delay to set PIN_CS_MPU
gpio_put(PIN_CS_MPU, 1); // Active low
asm volatile("nop \n nop \n nop");
sleep_us(100);
}
 
Top