/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / usart2 problems

Username:     
Password:     
             

Forum

# 1   2011-05-24 06:14:57 usart2 problems

malke
New member
Registered: 2011-05-18
Posts: 3

usart2 problems

Hi,

I'm making a project on a STM32F103 where I have to put a connectBlue BT module in SPP mode, I configure the BT module through USART2.

But it seems that I've configured the USART2 wrong, or maybe it's me sending functions which are causing me problems?

The USART should run:
- uart
- 115200 baudrate
- 8 bit
- 1 stop bit
- no parity
- no HW flow control

When I debug, I see that the USART2 registers are as followed:
SR = 0xC0
BBR = 0x138
CR1 = 0x202C
CR2 = 0x0
CR3 = 0x0
GRPT = 0x1

Here's my function to send a byte:

void usartSendByte(u8 Data)
{
    USART_SendData(USART2, Data);
    while((USART2->SR & USART_FLAG_TXE ) == 0);
}

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
  /* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_USART_DATA(Data));
   
  /* Transmit Data */
  USARTx->DR = (Data & (uint16_t)0x01FF);
}

I'm a bit confused about how to receive data?? I would like to receive it in a circular buffer, since when I'm sending a configuration packet to the BT module I'll receive a comfirm packet including: start byte, packet type (1 byte), length (1 byte), data parameters (n bytes) and followed by a checksum (1 byte).

I know I have to write something in the USART2_IRQHandler(), some help/hints here would be appreciated smile thanks!

Best regards
Malke

Offline

 

# 2   2011-05-24 08:28:56 usart2 problems

malke
New member
Registered: 2011-05-18
Posts: 3

Re: usart2 problems

I expect to receive more than one byte, but I only reads the first byte? When I debug and watch the USART2->DR i see 0x7F, which is the start byte.

I've enabled RXNEIE, and when I receive data an interrupt occurs and the SR changes to 0xF8!

Which mean IDLE = 1 and ORE = 1.
In the datasheet it says:
$1

How do I read the SR register? I've tried:
u8 dummy;
dummy = USART2->SR;
data = USART2->DR;

But it doesn't go any futher with the data in the packet - still shows 0x7F on the USART2 DR in debug mode sad

My receive function:

Offline

 

# 3   2011-05-24 09:41:29 usart2 problems

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: usart2 problems

Take a look at this example project that uses USART2 in IRQ mode on the extension connector:
http://www.stm32circle.com/projects/project.php?id=160

Offline

 

# 4   2011-05-25 14:14:19 usart2 problems

malke
New member
Registered: 2011-05-18
Posts: 3

Re: usart2 problems

That example doesn't help me much.

I receive data right, but I still have some problems by sending data

Code:

struct bufStruct
    {
    u8 head;
    u8 tail;
    u8 buf[rBuf_size];
    };

static struct bufStruct t = { 0, 0, };
static struct bufStruct r = { 0, 0, };

u8 usartSendByte (u8 val)
{    
    
    struct bufStruct *p = &t;
        
    if (((p->head+1) & (tBuf_size-1)) == p->tail) //(p->tail - p->head) >= tBuf_size) // maybe one should  make a while loop, so it waits until there are space free in tBuf           
        return (1);
    
    p->buf [p->head & (tBuf_size-1)] = val;
    p->head = ((p->head+1) & (tBuf_size-1));
    
    if (tx_restart)
        {
        tx_restart = 0;
        USART2->DR =(p->buf[p->head & (tBuf_size-1)] & (uint16_t)0x01FF);
        USART2->CR1 |= USART_FLAG_TXE;  // enable TX interrupt
        }
    return (0);
}

Code:

void USART2_IRQHandler (void)
{
    volatile u8 IIR;    // should maybe be u16, AMK 25-05-2011
struct bufStruct *p;

    IIR = (u8)USART2->SR;

    if (IIR & USART_FLAG_RXNE)
        {
        USART2->SR &= ~USART_FLAG_RXNE;
        
        p = &r;
        
        if (((p->tail - p->head) & ~ (rBuf_size-1)) == 0)
            {
            p->buf [p->tail & (rBuf_size-1)] = (USART2->DR & 0xFF);   // should maybe be 0x01FF or 0xFF
            p->head = ((p->head + 1) & (tBuf_size-1));                // should maybe be p.head++;
            p->tail = ((p->tail + 1) & (rBuf_size-1));
            }
        }
    
    if (IIR & USART_FLAG_TXE)
        {
        USART2->SR &= ~USART_FLAG_TXE;  // clear interrupt
        
        p = &t;
        
        if (p->head != p->tail)               // if data in buffer to sent
            {
            USART2->DR = (p->buf [p->head & (tBuf_size-1)] & 0xFF);   // should maybe be 0x01FF or 0xFF
            p->tail = ((p->tail + 1) & (rBuf_size-1));                // should maybe be p.tail++;
            tx_restart = 0;
            }
        else
            {                               // if tBuf empty -> nothing to send!
            tx_restart = 1;
            USART2->CR1 &= ~USART_FLAG_TXE; // disable TX interrupt
            }
        }
}

I'm sending data like this:

Code:

usartSendByte(0x7F);
usartSendByte(0x09);
usartSendByte(0x01);
usartSendByte(0x01);
usartSendByte(0x0B);

When I debug, no change in the USART2->SR (0xC0) or in USART2-DR (empty!), but I can see that I enable/disable TXIE in USART2->CR1 :S

Offline

 

# 5   2011-05-26 06:51:26 usart2 problems

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: usart2 problems

Which platform do you use ?
If it is a Primer, and you try to send data on USART2 (pin PA2), you face the bug with the TIM2 configuration.
Take a look at the correspondent posts.

Offline

 

Board footer