cancel
Showing results for 
Search instead for 
Did you mean: 

spi interface stm-32

bharath993
Associate
Posted on February 25, 2015 at 06:19

hi

I have been trying to interface cc3200 and tdc gp-22,by referring to example code which is written for stm-32 microcontroller. i cant understand whats happening in this function which i have given below.How the spi interface actually works below,the use of transmission buffer empty check and reciever buffer not empty check.Whats actualy the process goes ,when i compile these code.Pls help!! i have attached the stm-32 module for reference

http://www.disca.upv.es/aperles/arm_cortex_m3/curset/STM32F4xx_DSP_StdPeriph_Lib_V1.0.1/html/group___s_p_i.html

The specified code is described as Function Name: gp22_read_n_bytes * Parameters: bus_type = (SPI1, SPI2) * n_bytes = how many bytes should be read * read_opcode = read opcode of the device * read_addr = read address of the device * fractional_bits = number of fractional bits of read data * Return: n bytes from the specified read address * Description: Reads n bytes from an address in GP22

float gp22_read_n_bytes(void *bus_type, uint8_t n_bytes, uint8_t read_opcode,
uint8_t read_addr, uint8_t fractional_bits)
{
uint32_t Result_read = 0;
float Result = 0;
uint8_t read_opcode_addr = read_opcode | read_addr;
//.............. Result = n Byte = n x 8 bits......................
if (bus_type==SPI1 | bus_type==SPI2) 
{
// Deactivating Reset SPIx
if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
SPI_I2S_SendData(bus_type, read_opcode_addr); // READ OPCODE + Address
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==RESET) {};
Simple_delay_750ns((void*)10); // important delay (16) at SPI freq.=750kHz
//Compulsory reads to DR and SR to clear OVR,
//so that next incoming data is saved
SPI_I2S_ReceiveData(bus_type); // To clear OVR
SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE); // To clear OVR
//Reading byte1
SPI_I2S_SendData(bus_type, 0x00FF); // DUMMY WRITE
// Wait until RX buffer is not empty, then read the received data
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
Result_read = SPI_I2S_ReceiveData(bus_type); // Read
for (int n = 1; n < n_bytes; n++)
{ 
//Reading byte2 .. byte.n
SPI_I2S_SendData(bus_type, 0x00FF); // DUMMY WRITE
// Wait until RX buffer is not empty, then read the received data
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
Result_read = Result_read<<8;
Result_read |= SPI_I2S_ReceiveData(bus_type); // Read
}
// Reset to device SPIx
if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
}
Result = Result_read / pow(2, fractional_bits);
return Result;
}

#spi #stm32
3 REPLIES 3
Posted on February 25, 2015 at 17:32

Understand that SPI is a symmetrical bus, for each bit clocked out, one bit is clocked back in.

TXE asserts as the first bit shifts out, RXNE asserts as the last bit shifts in. If you wait on RXNE for the data to leave the SPI peripheral, TXE should implicitly be asserted unless you've done something with the data register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bharath993
Associate
Posted on March 02, 2015 at 08:13 Hi clive1, Thank you for replying.I still got doubt,after we have send the read_opcode address

SPI_I2S_SendData(bus_type, read_opcode_addr);

, we use this function

SPI_I2S_ReceiveData(bus_type);

what does we recieve actually by using this function?? and after which why are we checking again the transmission buffer is empty ?? the comments is stated as to clear OVR . Can you pls explain to me??
Posted on March 02, 2015 at 17:37

In the context of the code it reads SPI->DR clearing any pending data, so that when it next spins on RXNE it's waiting for the *current* transfer to complete and it can then read valid data, instead of whatever other junk was in the register from *prior* transfers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..