Topic : STM8L101xx USART_BUFFER_PROBLEM

Forum : ST7/STM8

Original Post
Post Information Post
January 20, 2012 - 2:37pm
Guest

Hi i'm working about STM8L101xx and i try to receive data from RFIinTerm. I checked everything data receiving right(i checked with USBee) but my code is not work correct. I try to use this buffer... What can i do about this subject am i missing any configuration options for use buffer?

example: this code not work correctly :-/

if(USART_DataInRxBuffer())
{
if(USART_Rx()==0x50)
{
GPIO_SetBits(LEDS_PORT,LED1); // Everything right about GPIO options....
Delay((uint32_t)60000);
GPIO_ResetBits(LEDS_PORT,LED1);
Delay((uint32_t)100000);
}
}
////////////////////////////// CONFIGURATION OPTIONS ////////////////////////////////////
enableInterrupts();

CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);//High speed internal clock prescaler: 1

GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_2|GPIO_Pin_3, ENABLE);//Set the USART RX and USART TX at high level

CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);// Enable USART clock

USART_DeInit();

USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1, USART_Parity_No,

USART_Mode_Rx|USART_Mode_Tx);
// Enable the USART Transmit interrupt: this interrupt is generated when the USART transmit data register is empty
//USART_ITConfig(USART_IT_TXE, ENABLE);
// Enable the USART Receive interrupt: this interrupt is generated when the USART receive data register is not empty

USART_ITConfig(USART_IT_RXNE, ENABLE);

USART_Cmd(ENABLE);
////////////////////////////////////////////////////// DEFINES ////////////////////////////////////////
unsigned char USART_Rx( void );
uint8_t USART_DataInRxBuffer( void );
void USART_RX_IRQHandler(void);
static volatile uint8_t USART_TxTail;
typedef unsigned char uint8_t ;
// Rx Buffer
static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE];
static volatile unsigned char USART_RxHead;
static volatile uint8_t USART_RxTail;
static volatile uint8_t USART_RxOverflow;
#define USART_RX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
#define USART_TX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
#if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
#error TX buffer size is not a power of 2
#endifstatic volatile uint8_t USART_TxHead;

//////////////////////////////////////// ABOUT BUFFER CODE /////////////////////////////////////////

void USART_RX_IRQHandler(void)

{

unsigned char RxData;
uint8_t tmphead;
/* Read one byte from the receive data register */
RxData = (uint8_t) (USART_ReceiveData8() & 0x7F); //Read the received data

tmphead = (USART_RxHead + 1) & USART_RX_BUFFER_MASK; //Calculate buffer index
USART_RxHead = tmphead; // Store new index

if (tmphead == USART_RxTail)
{
/* ERROR! Receive buffer overflow */
/* data will get lost !!! */
USART_RxOverflow = 1;
}
else
{
USART_RxBuf[tmphead] = RxData; // Store received data in buffer
USART_RxOverflow = 0;
}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////
uint8_t USART_DataInRxBuffer( void )
{
return ( USART_RxHead != USART_RxTail); /* Return 0 (FALSE) if the receive buffer is empty */
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char USART_Rx( void )
{
uint8_t tmptail;

//see if we have any data
while ( USART_RxHead == USART_RxTail );
tmptail = (USART_RxTail + 1) & USART_RX_BUFFER_MASK;

USART_RxTail = tmptail;

return USART_RxBuf[tmptail];
}

NOT: I hope this codes will helpfull for a lot of people...

Replies
Post Information Post
+1
0
-1
January 25, 2012 - 11:48am
Raisonance Support Team

Hi,

The best is to try with a simple code without interrupts, and perform a :

while(1)
    USART_SendData8('U');   /* U is 01010101b, generates a near-square signal */

Once the project is running, plug a scope on the Tx line and check that the baud rate is correct.
You will then be able to play with your interrupt and try to understand the issue.
The problem is that very often, the "other side" of the USART connection does not receive bytes properly. And Windows terminal emulation programs are of little help.

Regards,