Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs

Using Direct Memory Access (DMA) in STM32 projects

In many microcontroller projects, you need to read and write data. It can be reading data from the peripheral unit like ADC and writing values to RAM. In another case maybe you need to send chunks of data using SPI. Again you need to read it from RAM and continuously write to SPI data register and so on. When you do this using processor – you lose a significant amount of processing time. To avoid occupying CPU most advanced microcontrollers have a Direct Memory Access (DMA) controller. As its name says – DMA does data transfers between memory locations without the need of CPU. Low and medium density ST32 microcontrollers have a single 7 channel DMA unit while high-density devices have two DMA controllers with 12 independent channels. In STM32VLDiscovery… Continue reading

Multichannel ADC using DMA on STM32

Previously we have tried to do a single conversion of one ADC channel. We were waiting for the ADC result in a loop which isn’t an effective way of using processor resources. It is better to trigger conversion and wait for conversion complete interrupt. This way a processor can do other tasks rather than wait for ADC conversion complete. This time we will go through another example where we will set up more than one channel and read ADC values using interrupt service routine. How does multichannel ADC conversion works? If we need to convert several channels continuously, we need to set up Sequence registers (ADC_SQRx). There are three sequence registers: ADC_SQR1, ADC_SQR2, and ADC_SQR3 where we can set up a maximum of 16 channels in any order. Conversion sequence… Continue reading

Use fixed integer types to enhance portability

If you have programmed anything with C, you should be familiar with common data types like char, unsigned char, int, unsigned int, long int, long long int, etc. It is tough to tell by the looks of type how many bytes this variable takes on memory and how it looks in a different system. For instance in 8-bit AVR-GCC compiler int is a 16-bit type, while in ARM-GCC int is 32-bit. So int size is dependent on platform, compiler and runtime libraries. And switching between systems may trick you if you are not careful enough. You can always check the size of the variable by using sizeof() function. What to do if you need your code to be portable between different systems. Some great libraries could work on any system… Continue reading

Introducing to STM32 ADC programming. Part2

After we had a quick overview of STM32 ADC peripheral, we can start digging deeper into specifics. To understand simple things lets go with the simplest case – single conversion mode. In this mode, ADC does one conversion and then stops. After ADC conversion result is stored into 16-bit ADC_DR data register (remember that conversion result is 12-bit), then End of Conversion (EOC) flag is set an interrupt is generated if EOCIE flag is set. The same situation is if the injected channel is converted. The difference is that the result is stored into corresponding ADC_DRJx register, JEOC flag is set and interrupt generated if JEOCIE flag is set. In our example, we are going to measure the internal temperature sensor value and send it using USART. A temperature sensor… Continue reading

Introducing to STM32 ADC programming. Part1

STM32 ADC is pretty complex peripheral. It is designed to be flexible enough to accomplish complex tasks. We are going to dedicate a few posts where we will try to cover the main features and give working examples of code. The block schematic may look scary at the first time, but if you look closer, it can be split into several pieces that are responsible for different functions. Will will go through them step by step to make it look brighter. Continue reading

Programming STM32 USART using GCC tools. Part 2

In the previous 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 especially when RTOS is used or when we perform crucial real-time data processing. And having USART routines with while 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 different purpose. To use one or another interrupt first it has to be enabled in… Continue reading