/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / USART1 REMAP!

Username:     
Password:     
             

Forum

# 1   2011-04-06 18:14:49 USART1 REMAP!

ernif
New member
Registered: 2011-03-23
Posts: 2

USART1 REMAP!

I'm trying to remap the USART1 to pins PB6 and PB7 as written in the manual since I need to use 2 USART for my current project (one for xbee and one for a GPS). However, all I've been trying doesnt work and I can't find any doc that tells me how to do it. USART2 have been working great so far though... Anyone has any exemple that remaps USART1 to PB6 AND PB7?

Here is my actual code:

/********************** (C) COPYRIGHT 2007-2009 RAISONANCE ********************
* File Name          :  usart.c
* Author             :
* Date First Issued  :
* Description        :  Contains USART functions and defines
* Revision           :
*******************************************************************************/


/* BEGIN Custom Code ---------------------------------------------------------*/

bool refreshDisplayProps;
int32_t  screen_width, screen_height;
uint8_t lines_to_clear;
uint8_t OldPLLFrequency;

tHandler OldIRQHandler;

#define  RX_DATA_BUFFER_SIZE 128
static uint8_t rx_data_ptr=0;
char rx_data[RX_DATA_BUFFER_SIZE] = {""};

/* -------------------------------------------------------------------------------------------------- */
/* BEGIN - LED definition */
/* -------------------------------------------------------------------------------------------------- */
#define LEDS_GPIO           GPIOE
#define LEDS_RCC_GPIO       RCC_APB2Periph_GPIOE
#define GPIO_Pin_LED0       GPIO_Pin_0
#define GPIO_Pin_LED1       GPIO_Pin_1
#define BIT_LED_SET         Bit_RESET
#define BIT_LED_RESET       Bit_SET


/* -------------------------------------------------------------------------------------------------- */
/* BEGIN - USART ring buffer implementation */
/* -------------------------------------------------------------------------------------------------- */

//hardware definition - USART of the extension connector is USART1
#define MyUSART                   USART1
#define MyUSART_GPIO              GPIOB
#define MyUSART_CLK               RCC_APB2Periph_USART1
#define MyUSART_GPIO_CLK          RCC_APB2Periph_GPIOB
#define MyUSART_RxPin             GPIO_Pin_7
#define MyUSART_TxPin             GPIO_Pin_6
#define MyUSART_IRQn              USART1_IRQn
#define MyUSART_IRQHandler        USART1_IRQHandler
#define MyUSART_IRQChannel        0xD8   

/* UART Buffer Defines */
#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
#endif

// Rx Buffer
static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE];
static volatile uint8_t USART_RxHead;
static volatile uint8_t USART_RxTail;
static volatile uint8_t USART_RxOverflow;

// Tx Buffer
static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
static volatile uint8_t USART_TxHead;
static volatile uint8_t USART_TxTail;

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


void USART_SendChar(unsigned char data_char )
{
    uint8_t tmphead;
    tmphead = (USART_TxHead + 1) & USART_TX_BUFFER_MASK;

    //wait for the transmitter to catch up
    while (tmphead == USART_TxTail);

    USART_TxBuf[tmphead] = data_char;   
    USART_TxHead = tmphead;         

    //enable the transmit interrupt again so the data can get send out
    USART_ITConfig(MyUSART, USART_IT_TXE, ENABLE);
}

void USART_SendString(unsigned char *data_string)
{
    //while not at the end of the input string
    unsigned int i = 0;
    while (data_string[i])
    {
        //put the next char into the buffer
        USART_SendChar((unsigned char)data_string[i]);
        i++;
    }
}


unsigned char USART_ReceiveChar( 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];         
}

void USART_GetStringBuffer()
{

    while(USART_DataInRxBuffer())
    {
        // first see if we have still space in the buffer
        if (rx_data_ptr >= RX_DATA_BUFFER_SIZE-1) // reached the end of the buffer, start over
            rx_data_ptr = 0;
        // get the character from the receive buffer
        rx_data[rx_data_ptr] = USART_ReceiveChar();
        rx_data_ptr++;
    }
    rx_data[rx_data_ptr] = '\0';
}

//USART Interrupt handler
void MyUSART_IRQHandler(void)
{
    // interrupt was for receive (RXNE)
    if(USART_GetITStatus(MyUSART, USART_IT_RXNE) != RESET)
    {

        unsigned char RxData;
        uint8_t tmphead;

        /* Read one byte from the receive data register */
        RxData = USART_ReceiveData(MyUSART);              //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;
        }
    }
 
    // interrupt was for send (TXE)
    if(USART_GetITStatus(MyUSART, USART_IT_TXE) != RESET)
    {   
   
        uint8_t tmptail;
        // see if we have any data to send
        if (USART_TxHead != USART_TxTail)
        {
            tmptail = (USART_TxTail + 1) & USART_TX_BUFFER_MASK;
            USART_TxTail = tmptail;   

            /* Write one byte to the transmit data register */
            USART_SendData(MyUSART, USART_TxBuf[tmptail]);

        }
        else
        {
            /* Disable the USARTz Transmit interrupt */
            USART_ITConfig(MyUSART, USART_IT_TXE, DISABLE);
        }     
    }
}

/* -------------------------------------------------------------------------------------------------- */
/* END - USART ring buffer implementation */
/* -------------------------------------------------------------------------------------------------- */


void GPIO_Config()
{
    // USART GPIO
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Configure USART1 Tx (PA.2) as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = MyUSART_TxPin ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Configure USART1 Rx as input floating */
    GPIO_InitStructure.GPIO_Pin = MyUSART_RxPin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(MyUSART_GPIO, &GPIO_InitStructure);

    // LED GPIO
    GPIO_InitTypeDef GPIO_InitStructureLED;
    /* Configure LED pins as output push-pull */
    GPIO_InitStructureLED.GPIO_Pin   = GPIO_Pin_LED0 | GPIO_Pin_LED1 ;
    GPIO_InitStructureLED.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitStructureLED.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( LEDS_GPIO, &GPIO_InitStructureLED );

}

void NVIC_Config()
{
    // Init NVIC
    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
 
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = MyUSART_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 4;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}

void USART_Config()
{

    USART_RxTail = 0;
    USART_RxHead = 0;
    USART_TxTail = 0;
    USART_TxHead = 0;

   
    /* USART1 configuration ------------------------------------------------------*/
    /* USART1 configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
    */

    USART_InitTypeDef USART_InitStructure;

    USART_InitStructure.USART_BaudRate = 6400; //this leads to 9600 baud, not sure why this needs to be divided by 1.5;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No ;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(MyUSART, &USART_InitStructure);

    /* Enable USART1 Receive and send interrupts */
    USART_ITConfig(MyUSART, USART_IT_RXNE, ENABLE);     
    USART_ITConfig(MyUSART, USART_IT_TXE, ENABLE);

    /* Enable the USART1 */
    USART_Cmd(MyUSART, ENABLE);
}


void assert_failed( uint8_t* file, uint32_t line )
{
    // User can add his own implementation to report the file name and line number,
    //ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)

    // Infinite loop
    while ( 1 )
    {
    }
}

void AppCleanup()
{

    /* Disable the USART interrupts */
    USART_ITConfig(MyUSART, USART_IT_RXNE, DISABLE);
    USART_ITConfig(MyUSART, USART_IT_TXE, DISABLE);
    USART_DeInit(MyUSART);

    // set the old IRQ handler back
    UTIL_SetIrqHandler(MyUSART_IRQChannel, (tHandler)OldIRQHandler);

   // turn off the LEDs
    GPIO_WriteBit( LEDS_GPIO, GPIO_Pin_LED0, BIT_LED_RESET );
    GPIO_WriteBit( LEDS_GPIO, GPIO_Pin_LED1, BIT_LED_RESET );

    // set TIM2 functionality back to "normal"
    /* Enable TIM2 */
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
    /* TIM2 enable counter */
    TIM_Cmd( TIM2, ENABLE );
    /* Enable TIM2 Update interrupt */
    TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE );

    // set the old PLL frequency back
    UTIL_SetPll(OldPLLFrequency);
}


/* END Custom Code ---------------------------------------------------------*/

Offline

 

# 2   2011-04-07 06:14:03 USART1 REMAP!

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

Re: USART1 REMAP!

Hi,

First, think to start the clock for your GPIO and your USART, before configuring it : RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

Second thing : you forgot to remap the GPIO for USART1 :
1 : enable the clock : RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
2 : remap the USART : GPIO_PinRemapConfig(AFIO_MAPR_USART1_REMAP, ENABLE );

Third thing, which platform do you use ? Because on STM32F10x, the I2C1_SDA and USART1_RX pin (PB7) are shared with "FSMC_NADV". We have already talk about this problem at this topic :

http://www.stm32circle.com/forum/viewto … 3543#p3543


Yves

Last edited by yrt (2011-04-07 06:28:08)

Offline

 

# 3   2011-04-11 18:26:06 USART1 REMAP!

ernif
New member
Registered: 2011-03-23
Posts: 2

Re: USART1 REMAP!

GPIO_PinRemapConfig -> It worked like a charm. That's all I need thanks!

Offline

 

Board footer