Programming STM32 USART using GCC tools. Part 2

In the last part of the tutorial, we have covered simple USART routines that send data directly to USART peripheral. This is OK to use such an approach when a project isn’t time-critical and processing resources are far from limits. But most often, we stuck with these limiting factors, mainly when RTOS is used or when we perform necessary real-time data processing. And having USART routines with while the loop-based wait isn’t a good idea – it steals processing power only to send a data. As you may guess – next step is to employ interrupts. As you can see, there are many sources to trigger interrupts, and each of them is used for a different purpose. To use one or another interrupt, first, it has to be enabled in USART control register (USART_CR1, USART_CR2, or USART_CR3). Then NVIC USART_IRQn channel has to be enabled to map interrupt to its service routine. Because NVIC has only one vector for all USART interrupt triggers, service routine has to figure out which of interrupts has triggered an event. This is done by…

Continue reading

Programming STM32 USART using GCC tools. Part 1

When we need some feedback from the microcontroller, usually we use USART. It allows to output messages and debug information to the terminal screen. Also, data can be sent to MCU same way. For this purpose, STM32 microcontrollers have more than one USART interface allowing to have multiple streams of data output and input. USART interface is designed to be very versatile, allowing to have lots of modes including LIN, IrDA, Smart card emulation, DMA based transmissions. But for now, let’s focus on standard USART communications we could send and receive messages from the terminal window.

Continue reading

STM32 interrupts and programming with GCC

Probably one of the key features of any microcontroller is the interrupt system. ARM Cortex-M3 microcontrollers may have up to 256 interrupted sources. The first 15 interrupt sources are called system exceptions. These exceptions arise within Cortex core like reset, NMI, hard fault and error, debug, and SystTick timer interrupt. In the exception table, they start from address 0x00000004 and are numbered from 1 to 15. There is no 0 number exception (FYI – the very top of exception table address is used to store the starting point of stack pointer): Each exception vector holds the four-byte address of the service routine that is called when an exception occurs. Exception table usually is located in startup code like this:

Continue reading

Programming STM32F10x I/O port pins

Previously we learned how to compile STM32VL Discovery projects that were included in the package. But to understand how to write our own programs, we need to get to some basics. I think the best place to start is the input and output system (I/O). Before we begin to write some code, let’s go through what’s inside STM32 ports. If you look into the STM32 reference manual, you’ll find that the I/O system is pretty flexible. Port pins can work in several modes: Input floating; Input pull-up; Input pull-down; Analog; Output open drain; Output push-pull; Alternate function push-pull; Alternate function open drain. Pins are organized as 16-bit ports that have their names like PORTA, PORTB, PORC, PORTD… Ports are 16-bit wide; they are controlled with 32-bit words. Individually each port pin can be configured to one of these functions. Additionally, each pin’s maximum speed can be set to one of the values: 2MHz, 10MHz, and 50MHz. STM32 I/Os are 5V tolerant. Anyway, the proper design should use 5 to 3.3V level converters.

Continue reading