STM32F0 Discovery USART example
After a long trail & error finally all the settings for the USART peripheral set correctly and here is a quick tutorial how to do it. Complete Atollic project will be uploaded on my github repository in a days, anyway following this tutorial you should be able to do it. Firstely open Atollic IDE and crate new blank project for your STM32F0 Discovery boar.
At this point the wizard did the magic and it is time to tweak the main.c the source bellow is the original main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
********************************************************************************************************************** /** ** File : main.c ** Abstract : main function. ** Functions : main ** Environment : Atollic TrueSTUDIO(R) ** Distribution: The file is distributed “as is,” without any warranty ** of any kind. ** (c)Copyright Atollic AB. ** You may use this file as-is or modify it according to the needs of your project. This file may only be ** built (assembled or compiled and linked) using the Atollic TrueSTUDIO(R) product. The use of this file ** together with other tools than Atollic TrueSTUDIO(R) is not permitted. ********************************************************************************************************************** */ /* Includes */ #include #include "stm32f0xx_rcc.h" #include "stm32f0xx_gpio.h" /* Private typedef */ /* Private define */ /* STM32F0_DISCOVERY board definitions */ #define LED_PORT GPIOC #define LED1 GPIO_Pin_9 #define LED2 GPIO_Pin_8 #define KEY_PORT GPIOA #define KEY GPIO_Pin_0 /* Private macro */ /* Private variables */ /* Private function prototypes */ /* Private functions */ /* Global variables */ uint32_t timer=0; uint8_t timerFlag=0; /** **=========================================================================== ** Abstract: SysTick interrupt handler **=========================================================================== */ void SysTick_Handler(void) { timer++; if (timer>2000) { timerFlag = 1; timer = 0; } } /** **=========================================================================== ** Abstract: main program **=========================================================================== */ int main(void) { uint32_t ii = 0; GPIO_InitTypeDef GPIO_InitStructure; /* TODO - Add your application code here */ SysTick_Config(4800); /* 0.1 ms = 100us if clock frequency 12 MHz */ SystemCoreClockUpdate(); ii = SystemCoreClock; /* This is a way to read the System core clock */ ii = 0; /* GPIOA-C Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* Configure * PA0 USER Button input * */ GPIO_InitStructure.GPIO_Pin = KEY; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(KEY_PORT, &GPIO_InitStructure); /* Configure PC8 and PC9 in output pushpull mode * PC8 = LD3 Green LED * PC9 = LD4 Blue LED * */ GPIO_InitStructure.GPIO_Pin = LED1 | LED2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(LED_PORT, &GPIO_InitStructure); GPIO_SetBits(LED_PORT, LED1); GPIO_ResetBits(LED_PORT, LED1); LED_PORT->BSRR = LED2; LED_PORT->BRR = LED2; while (1) { if (timerFlag) { timerFlag = 0; ii++; /* Toggle LED1 */ if (ii == 1) { LED_PORT->BSRR = LED1; } else if (ii == 2) { ii = 0; LED_PORT->BRR = LED1; } } if(GPIO_ReadInputDataBit(KEY_PORT, KEY)) { /* USER key pressed */ if (ii == 1) { GPIO_SetBits(LED_PORT, LED2); } } else { GPIO_ResetBits(LED_PORT, LED2); } } return 0; } |
Add this part to main.c first you need to add usart.h library and add message you will send and add the Send_Byte() function
1 |
#include "stm32f0xx_usart.h" |
1 2 3 4 5 6 7 |
/* Private variables */ char MESSAGE[] = "Send a message via UART \r\n"; /* Private function prototypes */ /* Private functions */ void print(USART_TypeDef* USARTx, char* buffer); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/** **=========================================================================== ** ** Abstract: Send_Byte ** **=========================================================================== */ static uint32_t Send_Byte (uint8_t c) { while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, c); return 0; } /** **=========================================================================== ** ** Abstract: SendPacket ** **=========================================================================== */ void SendPacket(uint8_t *data, uint16_t length) { uint16_t i; i = 0; while (i < length) { Send_Byte(data[i]); i++; } } |
Add USART initialization in the GPIO initialization part (USART2 pins: Rx (PA2) and Tx (PA3))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// USART periferial initialization settings USART_InitTypeDef USART_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); //Configure USART2 pins: Rx (PA2) and Tx (PA3) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); //Configure USART2 setting: ---------------------------- USART_InitStructure.USART_BaudRate = 9600; 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(USART2, &USART_InitStructure); USART_Cmd(USART2,ENABLE); |
Modify the while(1) routine so when you press the User button on the STM32F0 Discovery board you will see text transmitted from the board.
1 2 3 4 5 6 7 |
/* USER key pressed */ if (ii == 1) { GPIO_SetBits(LED_PORT, LED2); while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); SendPacket(MESSAGE, 24); } |
32