cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L-Discovery serial (RS232) communication

leone1386
Associate II
Posted on October 01, 2011 at 15:56

Hi,

I'm triyng to transmit data from an accelerometer to STM32L-Discovery and send this data from STM32L-Discovery to PC using SPI and RS232

This is my initialization code

 

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  

  GPIO_InitStruct1.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStruct1.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_40MHz;

  

  GPIO_Init( GPIOB, &GPIO_InitStruct1);

  

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);

  SPI_InitTypeDef SPI_InitStruct1;

      

  SPI_InitStruct1.SPI_Direction = SPI_Direction_2Lines_FullDuplex ;

  SPI_InitStruct1.SPI_Mode =  SPI_Mode_Master;

  SPI_InitStruct1.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStruct1.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStruct1.SPI_CPHA = SPI_CPHA_1Edge;

  SPI_InitStruct1.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStruct1.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

  SPI_InitStruct1.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStruct1.SPI_CRCPolynomial = 7;

  

  SPI_Init(SPI2, &SPI_InitStruct1);

     

  SPI_Cmd(SPI2, ENABLE);

  

and this is the code for trasmit from accelerometer to STM32L

while(1)

{

  uint16_t Data;

  Data = SPI_I2S_ReceiveData(SPI2);

}

Now, could someone tell me if there are examples, libraries or other that I can use to receive data and show it onto the PC?

Thanks
4 REPLIES 4
Andrew Neil
Evangelist III
Posted on October 01, 2011 at 20:36

''could someone tell me if there are examples, libraries or other that I can use''

 

 

Yes: There is the STM32 Standard Peripheral Library, and it includes a load of examples.

At the top of the forum (the list of topics) it says,

 

 

''More product information, tools and resources can be found on ST website: see

http://www.st.com/internet/evalboard/product/250990.jsp

,

http://www.st.com/stonline/stappl/st/mcu/class/1734.jsp

and

http://www.st.com/stonline/stappl/st/evalboard/family/116.jsp

''

.

 

 

Have you visited any of those links?

 

 

Posted on October 03, 2011 at 18:10

Pretty sure that reading SPI data just reads an internal register. You need to send something (pad/junk data) out to generate clocks to fill that register, and you also need to clear and check that it actually received the data into the register.

Spinning in a loop with SPI_I2S_ReceiveData(SPI2) is most assuredly not going to work.

Wouldn't something like this be more appropriate?

if (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == SET) // Clear potentially pending Rx bit

  SPI_I2S_ReceiveData(SPI2); // Clear Rx bit, discarding data

while(1)

{

    uint16_t Data;

    /* Wait for SPI2 to be ready to send new data */

    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

    /* Send SPI2 data */

    SPI_I2S_SendData(SPI2, 0x00); // Junk/Pad

    /* Wait for SPI2 data reception */

    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

    /* Read SPI2 received data */

    Data = SPI_I2S_ReceiveData(SPI2);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
franek
Associate
Posted on November 24, 2011 at 22:28

And now it works?

Posted on November 25, 2011 at 15:06

And now it works?

You must be new here, this is a typical forum, 99% ask questions and leave.

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