Sunday, April 19, 2015

Get started with ARM development on STM32F411RET6


For a pretty long time I have been wanting to lay my hands on some sort of ARM based microcontrollers. There were too many options out there and never knew how to get started with it. Recently I came across ST Microelectronics. They have an awesome collection of low cost ARM based evaluation boards. Then I came across their Nucleo series and I decided to get one at the top of the series, the NUCLEO F411RE. What excited me is that it is cheaper than an Arduino Uno. ST has a number of low cost ARM evaluation boards.

Now coming to the point, the NUCLEO F411RE is based on the STM32F411RET6 32 bit microcontroller which has an ARM Cortex M4 architecture at its heart with a maximum clock speed of 100 MHz. That is roughly 6.25X the speed of your Arduino Uno. The best part is that the evaluation board is supported by mbed.

Mbed is an venture from ARM holdings and its numerous technology partners including Atmel, FreeScale, IBM. It is basically a free online compiler platform for ARM-M series microcontrollers where you can type in your code, compile and download the bin file for flashing onto your device. I would call mbed as an Arduino equivalent when it comes to the compiler part. There are numerous libraries available to take care of your digital I/Os, timers, PWM etc. So this is a good starting point even if you haven’t gone beyond the 8bit devices. Importing of libraries is pretty easy and there is an awesome forum to resolve all your issues. It also relieves you of finding a good compiler and IDE for ARM development. It also offers good code portability since the library functions are the same for different microcontrollers. But making use of the pre-built libraries robs you of the knowledge of what’s really happening inside and often keeps you away from exploring the full potential of your device. So this post is going to be about register level programming of NUCLEOF411RE.

First logon to developer.mbed.org. Instructions to get everything up and running is provided there. Now for the hardware part, you can follow this link to configure any NUCLEO board:
http://www.emcu.it/NUCLEOevaBoards/HowToUse-NUCLEO-F401RE.pdf
Once you have setup your Nucleo board and mbed account, it’s time to get some code running on that 32 bit device.

I won’t be walking you through the code. But I’ll be telling you the basics needed to get an led to blink. I chose to connect my led to the first bit(LSB) of GPIOA of STM32F411RET6.

At the start of the code you’ll have to include mbed.h. This will take care of a few memory related setup you’ll have to do otherwise.

First of all you need to turn on the clock supply to whatever peripheral you are planning to use. In this case it is GPIOA. If you refer the datasheet, the  clock supply to GPIOA can be turned on by setting the LSB of RCC_AHB1ENR.

Next register is the GPIOx_MODER. This is the register where you decide whether a GPIO pin is going to be an input or an output. X is the GPIO port eg. A,B,H etc.
The next one is GPIOx_OTYPER. The microcontroller offers different types of output configuration. You can chose either push-pull, open drain configuration using this register. GPIOx_ODR is the register whose contents will be reflected at the GPIO pin i.e a set bit will set the corresponding pin to a logic high and a reset bit will set it to logic low(0 V, almost).



For the time being you’ll need to use only the above mentioned registers. But there are four other registers worth mentioning. GPIOX_BSRR which can be used to set or reset individual bits. GPIOx_LCKR which can be used to lock other registers associated with GPIO to ensure that they are not modified accidentally. GPIO_AFRH and GPIO_AFRL are used to configure the IOs having dual functionalities.

The code for blinking an LED on PA_0 has been uploaded on GitHub.
https://github.com/arduinoguy/STM32F411RET6-led-blink


All the best with your foray into the ARM world.

Sunday, November 9, 2014

Detect the port to which Arduino is connected

 When interfacing an embedded system with a computer, it often becomes necessary to find the COM port to which the device is connected. This will enable the application running on the computer to home in on the target device.

The methodology is to let the target system to continuously send messages over the COM port to the computer system. The application on the computer, checks each COM port for the particular message and flags when the message is received from a COM port. The application can send back a keyword to the embedded system to stop transmitting the message since it is no longer needed.

I wrote a c++ application just out of curiosity to identify the port to which my Arduino was connected. In the setup I used, the Arduino continuously transmitted  the word "hello". The app checked for this message and identifies the port from which the message was received. I thought it might might be useful to put up the code so that someone else might find it useful. A screenshot of the application is shown below:


 I am adding the source code and the library files required. The Serial library files needed for the purpose was found on the Arduino website. With fair amount of tinkering, I was able to get things running. The app was coded using Visual Studio 2013 Ultimate Edition.

The project can be downloaded from GitHub

Sunday, August 24, 2014

Exciting world of FPGA

I have been in the embedded domain for a while and has a fair share of experience with those tiny 8-bit devices. FPGA (Field Programmable Gate Array) caught my attention recently and is one of the hottest things in the electronics industry at the moment. FPGA is probably the closest you can get near to achieving real time processing.
So what all the hype around FPGA? Anyone who has fiddled with embedded systems will know that processors are never real time. They execute lines of codes one after another. They appear to work in real-time for a wide variety of applications because they execute the code pretty fast. To put things into simple words, suppose that you put up a simple microcontroller circuit to turn an LED ON when a switch is pressed and turn it OFF when the switch has been released. Just a few lines of code will achieve this and the microcontroller will execute the code over and over again so many times in a single second. Think that you are a super fast human being capable of clicking the switch on and off REALLY fast such that you can push the switch ON and turn it OFF before the microcontroller executes the line of code which check if the switch has been pressed or not, you’ll never see the LED turn ON just because you microcontroller never detected the press of the switch. This makes the software or code based embedded systems non-real time.
When it comes to FPGAs things are totally different, there is no line by line execution of code. If you do the same LED operation as mentioned above, what happens is that a direct connection is established between the switch and LED, just like you would do on a breadboard. So you are going to see the LED light up every time you turn the switch ON.
What’s more exciting is that you can implement a number of such units on the same chip and all of those units will run in parallel i.e. you can have a single FPGA to so some serious image processing, blink some LEDs, drive motors all at the same time. Really cool!!!!

A FPGA from XILINX


Altera and Xilinx are the forerunners in FPGA manufacturing. To implement a logic on FPGA, the configuration files are creates in a suitable Hardware Description Language (HDL) like Verilog or VHDL and are downloaded to the FPGA. FPGAs are wiped clean i.e. goes blank when they are powered off. So you always need an external memory to hold the configuration files from which the device can be reconfigured every time it boots up. Most FPGA development boards come with an on-board storage facility to store the config files. The data is not stored exactly as a file. It will be a stream of bits stored in a PROM chip.

Monday, January 20, 2014

A deep introduction to MOSFETs and its working

I have recently fallen for MOSFETs. These incredible power semi-conductor devices simply amazes me. MOSFET is the acronym of Metal Oxide Semi-Conductor Field Effect Transistor.

It is basically a 3-terminal device(even though you might find a 4th one in a few)-the drain, source and gate. It is often an important element in power control applications They are used in numerous devices-inverters, motor drives, SMPS to name a few.

It is basically a switching device in which the conduction between the drain and source can be controlled using the voltage between the source and gate terminals. You have to apply a voltage above the threshold voltage of the FET to turn it on. This value can be found in the datasheet.. At first the conduction increases( drain current) linearly and then it attains saturation values( maximum value).

The name Metal Oxide Semiconductor comes from the fact that the gate is made up of three layers-metal, an oxide layer and a semi conductor. A channel exists between the drain and source terminal whose character differs depending on the type of MOSFET. The channel lies below the insulator at the gate. The conduction between gate and source happens when the channel is filled with sufficient number of free charges.

There are two different types of MOSFETs: enhancement type and depletion type. Enhancement type requires you to enhance its conductivity by applying suitable gate to source voltage or the transistor is OFF by default (application of voltage creates free charges in the channel). On the other hand, depletion ones needs suitable gate to source voltage to stop its conduction or they are ON by default( consists of free charges without the application of voltage). In addition to this a further sub division NMOS and PMOS exists. The former one conducts using free negative charge and the latter one using free positive charge or holes. Enhancement type NMOSFET is the most widely used one due to a number of factors.







If you look at the figure, the topmost layer of the gate terminal is the metal, the white one is the insulating oxide layer usually made of Silicon dioxide. Below that is the semiconductor. When a voltage is applied between gate and source, the insulator does not allow the voltage to go directly to the semiconductor. Instead, a capacitive effect occurs where the plates of the capacitor are the metal and the semiconductor layers and the oxide layer serves the purpose of the dielectric medium. The B terminal is the body terminal and serves to control the threshold voltage. This is not often provided.

Take the case of enhancement type NMOSFET, a positive gate to source voltage induces positive charges on the metal and an equal and opposite charges develop in the semiconductor layer which is actually the channel. Now a path which consists of sufficient number of free charges exist and conduction can happen between drain and source. But for the conduction to occur, the gate voltage has to be above the threshold levels. Removal of the voltage diminishes the conducting channel and the conductivity comes to a halt. 

A typical MOSFET offers an OFF resistance in the order of Mega ohms whereas the ON resistance is within a few ohms. 

This figure shows how the channel is formed and the drain current increases and saturates with increase in gate voltage.  

The various symbols of MOSFET are:





The schematic shows the use of a MOSFET for driving a load, here it is an LED:




Here the transistor is acting as a acting as a a low side driver i.e the MOSFET connects and disconnects the lower voltage, in this case ground, from the load. The resistance between gate and source is to prevent any false triggering and also for discharging the stored charges in the gate during turn off.

A lot of things have to be considered when using MOSFETs and other power control devices. I'll be posting more about MOSFETs. The upcoming posts will be on low side, high side driving and gate driver ICs like IR2111. I simply love them, they amaze me.

Saturday, January 11, 2014

Tri state- The third logic state

All of us are quite familiar with the so called binary system of 0's and 1's which is the essence of digital electronics. In digital electronics, all voltage levels are measured in terms of a logic 0 or logic 1. Taking 5V to be the reference(Vref), logic 0 lies in between 0V and 1/3Vreg whereas logic 1 state is 2/3Vref to Vref. The above said voltage levels is in case of CMOS technology. These levels are 0 to 0.8V in 2 to Vref in case TTL. It has to be noted that the voltage levels in between 0.8V and 2V should be avoided in TTL circuits as it maybe treated as a logic 1 or 0 depending on the circuit characteristics.

Now there exists a lesser known state other than the o and 1 called the tri state or the high impedance state. This third state can be totally isolated state and is similar to an open switch. Usually the output of any digital circuit is a 0 or a 1. But if the output is a tristate, you don't get any voltage readings simply because no voltage exists there. The logic 1 state can be considered to be a direct connection to Vcc( supply voltage) and 0 can be considered as a direct connection to ground. However, tri state is a state when there is no connect to any voltage source.
Suppose you have an LED whose anode is connected to the output of a digital circuit and the cathode connected to Vcc through a resistor and another one whose cathode is connected to the same point in a similar fashion. When the output is 1, the first led glows and when the output is 0, the second one glows. However, when the output is tristated none of the LEDs glow.


The above shown is a figure of a tri state buffer. What it does is that it conveys the input logic level to the output, but the output can be switched to high impedance state using proper signal at enable/inhibit pin.

If you still need clarifications or note any discrepancies, feel free to post them in comments.