spi interface stm-32

Discussion in 'Embedded Systems and Microcontrollers' started by Bharath.R, Feb 25, 2015.

  1. Bharath.R

    Thread Starter New Member

    Feb 17, 2015
    2
    0
    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_...p___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 (Text):
    1. float gp22_read_n_bytes(void *bus_type, uint8_t n_bytes, uint8_t read_opcode,
    2.                    uint8_t read_addr, uint8_t fractional_bits)
    3. {
    4.   uint32_t    Result_read = 0;
    5.   float       Result = 0;
    6.   uint8_t     read_opcode_addr = read_opcode | read_addr;
    7.  
    8.   //.............. Result = n Byte = n x 8 bits......................
    9.   if (bus_type==SPI1 | bus_type==SPI2)
    10.   {
    11.        // Deactivating Reset SPIx
    12.        if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
    13.        if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
    14.      
    15.        SPI_I2S_SendData(bus_type, read_opcode_addr);  // READ OPCODE + Address
    16.      
    17.        while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==RESET) {};
    18.   Simple_delay_750ns((void*)10); // important delay (16) at SPI freq.=750kHz
    19.      
    20.        //Compulsory reads to DR and SR to clear OVR,
    21.        //so that next incoming data is saved
    22.        SPI_I2S_ReceiveData(bus_type);                     // To clear OVR
    23.        SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE); // To clear OVR
    24.  
    25.          //Reading byte1
    26.          SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
    27.          // Wait until RX buffer is not empty, then read the received data
    28.          while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
    29.          Result_read = SPI_I2S_ReceiveData(bus_type); //  Read
    30.  
    31.        for (int n = 1; n < n_bytes; n++)
    32.        {      
    33.          //Reading byte2 .. byte.n
    34.          SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
    35.          // Wait until RX buffer is not empty, then read the received data
    36.          while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
    37.  
    38.          Result_read = Result_read<<8;
    39.          Result_read |= SPI_I2S_ReceiveData(bus_type); //  Read
    40.        }
    41.  
    42.        // Reset to device SPIx
    43.        if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
    44.        if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
    45.   }
    46.  
    47.   Result = Result_read / pow(2, fractional_bits);
    48.  
    49.   return Result;
    50. }
     
Loading...