cancel
Showing results for 
Search instead for 
Did you mean: 

Why am i not able to Configure same UART on two different channels alternatively and communicate?

Amal Babu
Associate III

Hai,

i tried configuring UART1 on PA9 and PB6 designed to communicate with two different devices.I tried transmitting data by configuring each pins individually and disabling (configuring the pin as low) the pins after transmission.My Transmission doesn't seem to work.Please go through my code and share suggestions for the same.

#include "stm32f0xx.h"                // Device header

#include "RTE_Components.h"            // Component selection

#include "config.h"

#include <stdio.h>  

#include <string.h>

#define baudDivisor 0X1388

void UARTinit();

void send_string(char *msg);

void UARTPutChar(char ch);

void enable_PA9();

void enable_PB6();

int main()

{

Start_HSE();

PORTB_CLK_EN;

PORTA_CLK_EN;

UARTinit();

while(1)

{

enable_PA9();  //dissable PB6 and enable PA9

while(!(USART1->ISR & 0x40));                                USART1 -> TDR = 'a' ;

enable_PB6(); //dissable PA9 and enable PB6

while(!(USART1->ISR & 0x40));                  USART1 -> TDR = 'b' ;

                               

}

}

void UARTinit()

{

 RCC->APB2ENR |= (RCC_APB2ENR_USART1EN);    // Enable USART1 clock

               USART1->CR1 &= ~(BIT(0));                                                

               USART1->CR1 = USART_CR1_TE | USART_CR1_RE |USART_CR1_RXNEIE;

USART1->BRR = baudDivisor;                                     

USART1->CR2 = USART_CR2_MSBFIRST;                                

USART1->CR3 = 0;        

               USART1->CR1 |= USART_CR1_UE ;

                               NVIC_EnableIRQ(USART1_IRQn);

               

}

void enable_PB6()

{

//configuring PA9 as Output low

               PORTA_IO = BIT_OUT(9);

               PORTA = BIT_CLR(9);

 //configuring PB6 as TX pin

               GPIOB->AFR[0] = 0x0000;                    

               GPIOB->MODER = BIT(15) | BIT(13);

}

void enable_PA9();

{

//configuring PB6 as Output low

               PORTB_IO = BIT_OUT(6);

               PORTB |= BIT_CLR(6);

 //configuring PA9 as TX pin

               GPIOA->MODER |= BIT(21) | BIT(19);

               GPIOA->AFR[1] = BIT(8) | BIT(4);

}

6 REPLIES 6

You should make sure TXE asserts before sending data, and then wait for TC to assert indicating the data has crossed the wire before switching between pins.

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

One more note - wait for TXE before sending every byte and then wait for TC after all bytes has been sent.

Amal Babu
Associate III

Thanks for your valuable suggestions.

I have tried as your suggestions ,but my Transmission doesn't seem to work.Please go through my new code and share suggestions for the same.

#include "stm32f0xx.h"         // Device header

#include "RTE_Components.h"       // Component selection

#include "config.h" 

#include <stdio.h>  

#include <string.h>  

#define baudDivisor 0X1388

void UART_init();

void enable_PA9();

void enable_PB6();

int main()

{

 Start_HSE();

PORTC_CLK_EN;

PORTC_IO = BIT_OUT(7);

PORTC = BIT_SET(7);

 UART_pic_secondary_init();

// enable_primary();

while(1)

{

enable_PA9( );

while(!(USART1->ISR & 0x80)); //Wait for TXE flag

USART1 -> TDR = 48 ;

while(!(USART1->ISR & 0x40)); //Wait for TC flag

enable_PB6();

while(!(USART1->ISR & 0x80)); //Wait for empty flag

USART1 -> TDR = 50 ;

while(!(USART1->ISR & 0x40)); //Wait for empty flag

}

}

void UART_init()

{

RCC->APB2ENR |= (RCC_APB2ENR_USART1EN);   // Enable USART1 clock

RCC->AHBENR |= (RCC_AHBENR_GPIOAEN);    // enable clock for used IO pins

 RCC->AHBENR |= (RCC_AHBENR_GPIOBEN);

USART1->CR1 = 0x00;                          

USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE|USART_CR1_RXNEIE;

 USART1->BRR = baudDivisor;                     

 USART1->CR2 = USART_CR2_MSBFIRST;                 

 USART1->CR3 = 0;                         // Disable interrupts and DMA

// GPIOA->MODER |= BIT(21) | BIT(19); 

// GPIOA->AFR[1] = BIT(8) | BIT(4);      // Alternate Func PA 9-10 to USART1

}

void enable_PB6()

{

//configuring PA9 as Output low

        PORTA_IO = BIT_OUT(9);

        PORTA |= BIT_CLR(9);

//PB6 as TX pin

GPIOB->AFR[0] = 0x0000;          

GPIOB->MODER = BIT(15) | BIT(13);  

}

void enable_PA9( )

{

//configuring PB6 as Output low

        PORTB_IO = BIT_OUT(6);

               PORTB |= BIT_CLR(6);

//PA9 as TX pin

GPIOA->MODER |= BIT(21) | BIT(19); 

GPIOA->AFR[1] = BIT(8) | BIT(4);  

}

Do the outputs work if you test independently?

If not focus​ on initialization code.

Sorry not going to comb through some ugly register level stuff.​

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

each channels are found working independently

I think you need to do a better job of masking out the register content, not blind writing registers, and not OR in a way that just accumulates high bits. Only one device should be in AF mode on a pin at any point. I would park the unused pins as GPIO OUT HIGH

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