This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USART Tx, STM32F103VCT6, hy-mini STM32V, Cortex M3

I am using the HY mini STM32V eval. board and are trying to set up a simple USART transmission. The board has a UART-to-USB uC so I need to write to USART1. I use RealTerm terminal. I am new to ARM and this is my first ARM. I can't seem to get it to work. It seems that the break and error indicator in RealTerm are flashing sometimes. I do not wish to use the standard library provided (by STM). I will post my code here and if someone has the time to look at it, it would be much appreciated! Any feedback is welcome!

(I saw the tabs got a little off, but should still be readable).

#include "stm32f10x.h"

//LED
#define LED_1                                                                                                                                           GPIO_BSRR_BS0
#define LED_2                                                                                                                                           GPIO_BSRR_BS1
#define LEDPORT                                                                                                                                         GPIOB
//GPIO
#define GPIO_CR_MODE_RESET                                                                                              0
#define GPIO_CR_MODE_OUTPUT_2MHZ                                                                        2
#define GPIO_CR_MODE_OUTPUT_10MHZ                                                                       1
#define GPIO_CR_MODE_OUTPUT_50MHZ                                                                       3
#define GPIO_CR_CNF_OUTPUT_PUSHPULL                                                             0
#define GPIO_CR_CNF_OUTPUT_OPENDRAIN                                                    1
#define GPIO_CR_CNF_OUTPUT_AFIO_PUSHPULL                                        2
#define GPIO_CR_CNF_OUTPUT_AFIO_OPENDRAIN                                       3
#define GPIO_CR_CNF_INPUT_ANALOG                                                                        0
#define GPIO_CR_CNF_INPUT_FLOATING                                                              1
#define GPIO_CR_CNF_INPUT_PULLUPDOWN                                                    2
#define GPIO_setPinModeL(port, pin, mode)                                       port->CRL |= (mode << 4*pin)
#define GPIO_setPinModeH(port, pin, mode)                                       port->CRH |= (mode << ((4*pin) - 32))
#define GPIO_setPinCnfL(port, pin, cnf)                                         port->CRL |= (cnf << ((4*pin) + 2))
#define GPIO_setPinCnfH(port, pin, cnf)                                         port->CRH |= (cnf << ((4*pin) - 32 + 2))
#define GPIO_initPort(port)                                                                                             port = 0x0000

//USART
#define USARTPORT                                                                                                                                       GPIOA


//Delay fucntion
void DELAY(int delay)
{
        volatile int dly;
        for(dly = 0; dly < delay; dly++);
}

//LED functions
void LED_init(void)
{
        RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;                                                                                                  //Enable port B clock
        GPIO_initPort(LEDPORT->CRL);                                                                                                                         //Have to set CRL to 0x0000 before we can bitwise or to the register.
        GPIO_setPinModeL(LEDPORT, 0, GPIO_CR_MODE_OUTPUT_2MHZ);
        GPIO_setPinModeL(LEDPORT, 1, GPIO_CR_MODE_OUTPUT_2MHZ);
}

void LED1_set(void)
{
        LEDPORT->BSRR = LED_1;
}

void LED2_set(void)
{
        LEDPORT->BSRR = LED_2;
}

void LED2_clear(void)
{
        LEDPORT->BRR = LED_2;
}

//USART funtions
void USART1_send_byte(int byte)
{
        while( !(USART1->SR & USART_SR_TXE) );
        USART1->DR = byte;
}

int USART1_get_byte(void)
{
        while( (USART1->SR & USART_SR_RXNE) != 0 );
        return ( (int) (USART1->DR & 0x000000FF) );
}

void USART1_init(void)
{
        RCC->APB2ENR |= RCC_APB2ENR_USART1EN;                //Enable USART clock
        RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
        RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;

        //NULL registers
        USART1->CR1 = 0x0000;
        USART1->CR2 = 0x0000;
        USART1->CR3 = 0x0000;
        USART1->BRR = 0x0000;

        //Tx - PA9
        GPIO_setPinModeH(USARTPORT, 9, GPIO_CR_MODE_OUTPUT_50MHZ);
        GPIO_setPinCnfH(USARTPORT, 9, GPIO_CR_CNF_OUTPUT_AFIO_PUSHPULL);
        USART1->CR1 |= USART_CR1_UE;
        USART1->BRR = ( (1 << 0) | (0x00000034 << 4) );          // 8MHz system clock and 9600 baud rate.
        USART1->CR1 |= USART_CR1_TE;

        //Rx
        //GPIO_setPinModeH(USARTPORT, 10, GPIO_CR_MODE_OUTPUT_50MHZ);
        //GPIO_setPinCnfH(USARTPORT, 10, GPIO_CR_CNF_INPUT_FLOATING);
        //USART1->CR1 |= USART_CR1_RE;
}

/*********              MAIN            *********/
int main(void)
{
        LED_init();
        USART1_init();
        //USART1_init2();

        USART1->DR = 0x000000AA;
        LED1_set();
        while(1)
        {
                USART1_send_byte(0x000000BB);
                LED2_set();
                DELAY(5000000);
                LED2_clear();
                DELAY(5000000);

        }
}

0