Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

spi interface stm-32

Status
Not open for further replies.

bharockz

Newbie level 2
Newbie level 2
Joined
Feb 19, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
26
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
https://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

Code:
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;
}
 

GPIOA, GPIO_Pin_4 - CS pin for SP1 and GPIOB, GPIO_Pin_12 - CS pin for SPI2
Buffer for TX and RX are common, like in UART, I2c and e.t.c.
Used SPL-based pooling method as simplest. For some reason SPI defined by * void pointer. There are some special type available in SPL spi header file.
 

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!!

You get the answer by reviewing the STM32 reference manual and the spi library source code.

You can try not to be bothered with STM32 low level details and use SPI library functions according to the documentation and modifying the existing examples. This "high level" approach will probably work, at least for standard tasks. Or try to understand every detail so that you are pepared to debug SPI operation on the register level if it's not working as expected. In the second case, you should budget the time needed for digging through the manual and library code.
 

hi thx for replying.As far as i understand the codes

if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
is used for lowering the SPI chip select.

and SPI_I2S_SendData(bus_type, read_opcode_addr); is used for sending the read opcode to gp-22.

in this 1. what i dont understand is,whats the purpose of
while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==RESET) {};
is this to check whether tranmission buffer is empty?

2.what is the use of
//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

3. SPI_I2S_SendData(bus_type, 0x00FF); is used to send a dummy value
whats the use of
// 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

what exactly be read in SPI_I2S_ReceiveData(bus_type); ??

Could pls explain to me this ??!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top