Author Archives: Shawon Shahryiar

STM32 Serial Communication

STM32 micros just like any other micro provide hardware for serial communication. As we all know serial communication is a very important tool for debugging, connecting with external hardware like RFID, GPS, GSM modems, etc. and for performing other communication-related tasks. STM32s have several hardware serial (USART) ports. The number of ports available in a STM32 micro is dependent on device family type and the device itself. Typically there are at least 5 serial ports. Apart from the basic serial communication needs, STM32’s USART hardware also have support for LIN, one-wire, smart card protocols, SPI and IrDA. We can even utilize STM32’s DMA controller with USART hardware to achieve high speed automated data communication. Thus these hardware are truly universal synchronous-asynchronous receiver-transmitters.

In any standard serial communication, we need three wires – TX, RX and GND. TX pin is an output pin and transmits data serially to another device’s RX pin. RX pin is an input pin and is responsible for receiving data from another device’s TX pin. The two devices connected in this way must have same ground (GND). There are other pins like CTS and RTS which are used for hardware flow control. Additionally there’s also another pin called CK. It is transmitter’s clock output and used usually in SPI and other modes.

Depending on package, USART pins are arranged in the following pattern:

BGA100 LQFP100 LQFP48 LQFP64 Pin Name USART/UART
D9 67 29 41 PA8 USART1_CK
C10 70 32 44 PA11 USART1_CTS
B10 71 33 45 PA12 USART1_RTS
C9 68 30 42 PA9 USART1_TX
D10 69 31 43 PA10 USART1_RX
G3 29 14 20 PA4 USART2_CK
G2 23 10 14 PA0 USART2_CTS
H2 24 11 15 PA1 USART2_RTS
J2 25 12 16 PA2 USART2_TX
K2 26 13 17 PA3 USART2_RX
K8 51 25 33 PB12 USART3_CK
J8 52 26 34 PB13 USART3_CTS
H8 53 27 35 PB14 USART3_RTS
J7 47 21 29 PB10 USART3_TX
K7 48 22 30 PB11 USART3_RX
B8 79 52 PC11 UART4_RX
B9 78 51 PC10 UART4_TX
B7 83 54 PD2 UART5_RX
C8 80 53 PC12 UART5_TX

Personally I’m interested in LQFP packages, particularly 48 and 64 packages as they are mostly used in the most common STM32 development boards. I suggest locating USART/UART pins before working with them.

Read more

Porting ST Standard Peripheral Library (SPL) with MikroC PRO for ARM

What is this Standard Peripheral Library (SPL)?

It’s just a collection of hardware libraries that provide an easy approach to any STM32 ARM programmer. It has support for every peripheral a STM32 micro has like CAN, USB, ADC, Timers, etc. In short it’s a hardware abstraction layer fully covering the STM32.

Why use the ST SPL?

  1. Reduce coding time by spending less time figuring out 32 bit register values and going through a near thousand page reference manual.
  2. Take the advantages of the built-in code library of MikroC which other compilers don’t offer.
  3. SPL is used by most STM32 ARM programmers and so it is a widely used tool with a large community.
  4. Most books and documents on STM32 ARMs are based on SPL.
  5. A compiler like MikroC doesn’t give full focus towards STM32. It has other ARM concerns like TI’s TIVA, NXP, etc and so MikroC’s built-in libraries are not 100% compatible with all ST hardware.
  6. Following the previous point MikroC (at least at the time of writing this article) doesn’t give library support for hardware like the DMA and so there’s incompleteness to some extent. It is also worth noting that depending on MikroC’s library also has the disadvantage of code size and efficiency. It is in general true for every compiler. However MikroC’s IDE is cool enough to do things with ease and that’s why I like it. Unless you are a Maple or Arduino fanboy, you’ll get my point.

Software Needed

  1. Modified ST Standard Peripheral Library (SPL) as supplied here: STM32 Standard Peripheral Library for MikroC. It works for STM32F10x micros only.
  2. MikroC PRO for ARM compiler.
  3. Flash Loader Demo or ST-Link Programmer Software.

Hardware Needed

  1. A STM32F10x Development Board.
  2. A USB-Serial Converter or ST-Link Programmer.
  3. Some basic components like a mini bread board, connecting wires/jumpers, a LED, a 220Ω 0.25W resistor and a push button.
  4. Optional debugger hardware.

How to use the Standard Peripheral Library?

First we need to prepare our MikroC PRO for ARM compiler for linking with SPL. I’m assuming that the compiler is preinstalled. First go to the compiler’s installation folder and look for the include folder. In my case, it is:
C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for ARM\Include.

In this folder there’s a file named stdint.h. It’s basically a definition of variable types and other related stuffs. Rename it to stdint (backup).h and copy the new stdint.h file from the supplied folder to this location. We are done here.

stdint modification

Read more

STM32 – Prior to Start

STM32 ARM-based micros from STMicroelectronics pack high density resources than any other conventional microcontroller. They are also high speed devices, operating typically at 72MHz and beyond. Despite several advanced features and heavy resources, they turn out to be misfortunes for beginners who wish to play with them. Available in market are several cool STM32 boards but most of them are not well documented. The aim of this document is to address some common FAQs.

Typically most people ask the following question:

  • How to program the STM32 micro embedded in my development board?
  • What tools do I need to get started?
  • What compiler support do I have for STM32?
  • What resources are available for STM32 on the internet?

In this doc, I’ll be addressing these FAQs along with some basics as I had similar questions in my early expeditions with STM32 micros. When I started with STM32 these questions were there as they are now. Literally there was nothing to address them till now. Most experts take it for granted that those who are beginning with ARMs must have heavy knowledge on the topic and thus skip answering these simple or silly questions.

Well I decided to break this stalemate for any starter no matter what’s his/her experience. Firstly most people nowadays believe ARM is only for mobile phones, PDAs, tablets, etc. and can’t be used like other micros like AVRs and PICs. This is completely wrong. Firstly because no one has set such a rule and secondly because there’s no limit to requirements in an embedded design. However it would be wasteful to make simple LED blinking stuffs with such chips. When it comes to size and resources ARM varies in all aspects. You can have an ARM chip with similar resources available in a typical 8 bit or 16 bit micro and yet you can also have an ARM chip with high resources. For instance if you think of flash memory, an ARM chip can have a few kilobytes to several megabytes of flash memory. Thus not just for real-time operating systems, phones, DSP, graphical processing and so on but also for other kinds of embedded systems the door for ARM is always open. It’ll completely depend on its user. STM32 portfolio has wide ranges of 32 bit ARM micros just as I described. A quick look on ST’s page will give you a brief idea: http://www.st.com/web/en/catalog/mmc/FM141/SC1169 .

Answering from the last, let’s see what resources are available for STM32s on the internet. Going back to ST’s site one can clearly see that there are several app notes for STM32s. At the time of writing this doc there 83 app notes on ST’s page. Obviously then there are reference manuals, datasheet and other docs too. Of these the important ones are the datasheet of a particular micro and its family reference manual. The rest of the stuffs are various software and tools but here confusion arises. We’ll see what other things are needed later but for now let’s see the purpose of reference manuals and datasheet. The reference manual is needed for understanding the internal hardware of a particular STM32 family, the registers associated with these hardware, programming techniques and what “not”s and what “are”s. These make it the core document of all. Datasheet basically give overviews of the devices. Books available for STM32-based ARMs are hard to find but still found a book called Discovering STM32 on Google search’s first page.

Regarding compiler selection, there are several available for STM32 micros. The most common ones are Keil, CooCox and MikroC PRO for ARM. My personal favourite is MikroC (http://www.mikroe.com/mikroc/arm/) as I’m already familiar with MikroC for AVR and 8051. MikroC comes with lot of easy-to-use built-in and examples and so it’s a good compiler to work with. Anyone familiar with MikroC will not have any trouble using it for ARM. With MikroC one can go to raw levels and he/she can also go for easy library-based coding.

Now let’s see what tools we’ll need for STM32 micros. The first tool we’ll need is called Flash Loader Demonstrator (http://www.st.com/web/en/catalog/tools/PF257525).  Basically it’s a programmer GUI based on factory loaded bootloader that allows a user to load, erase, verify, read or set configuration bits for his/her STM32 micro. The second tool is called Microxplorer (http://www.st.com/web/en/catalog/tools/PF251717). This tool allows a user to graphically set configurations for IO ports, internal hardware and many more. Basically it’ll help you reduce your time debugging registers. Both of these software are free and easy to use.  The last but not the least tool is called Timer Calculator (http://www.libstock.com/projects/view/398/timer-calculator). It’s a handy tool for those who’ll use MikroC for ARM. It can be used to generate codes for timer interrupts and timer-based stuffs. There are other tools too.

The final topic is about loading code to a STM32 micro. There are two ways either by a JTAG programmer like ST-Link or by bootloader and UART. The former is the best choice as it is a programmer and a debugger but in terms of expense this is unnecessary as the chips come with preprogramed bootloader. Thus the latter method of using bootloader and UART is simplest and also the most popular solution. This is also my personal preference as I didn’t want to spend for another programmer just for the purpose studying. This is the method I’ll describe here. Let’s see the board:

Untitled

Read more

Recent Entries »