Wednesday, October 1, 2014

Embedded C Programming for ARM Cortex (Introduction)

If you are not familiar with embedded systems, when you look at a C code, you will notice some differences.

Before starting a programming tutorial i wanted to build a basic knowledge about embedded programming.


   
If you are designing windows applications in C, C++, C# ; data types are standard, your code can run on any machine running windows (if .net and other dependencies are supplied of course).

You don't have to know memory and peripheral addresses mostly. Bitwise operations are not common, you don't have to worry about cpu type, pin configuration, enabling clocks.

Probably you are not familiar with infinite while loop and RTOS (Real Time Operating System). Limited code and data memory restricts using big arrays.

Preprocessor directives and preprocessor macros are a little tricky usually. Portability is a big problem. If you write a program for STM32F1 even porting this program to STM32F4 can be pain in the ***.

1) Data Types

    Don't get surprized when you see uint8_t, uint16_t etc. All type definitions are in stdint.h header file. Some of the definitions are below :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    /* exact-width signed integer types */
typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;
typedef   signed       __int64 int64_t;

    /* exact-width unsigned integer types */
typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;
typedef unsigned       __int64 uint64_t

2) Preprocessor Directives and Macros

   Preprocessor directives and macros start with "#", sometimes you are going to encounter "\" character, it is used for line continuation. Some of the preprocessor directive keywords are :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#define
#if 
#else
#elif
#endif
#ifdef
#ifndef
#include
#line
#message
#pragma
#undef
#warning

Take a look at these ebook :
https://gcc.gnu.org/onlinedocs/gcc-4.0.4/cpp.pdf


3) Microcontroller Dependent Register Names

Even if you are goint to use higher level libraries, it would be handy to know basic register names. In some tutorials and examples register based operations are used, for example below code does not mean anything to you if you don't know the register names :

1
GPIOC->IDR & 0x0020


IDR is Input Data Register, this code reads Port C, Pin5. Below code is equivalent but easy to read.

1
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)

4) Pin Configuration

Using GPIO pins is a little tricky for beginners at first. I am going to write a detailed gpio tutorial later but i want to make a brief explanation. Before using your MCU's gpio pins you have to make some effort, all pins are usually at floating input state initially.
  • Enable clock for related port. (Port A etc.)
  • Select related pins (Pin 0 etc.)
  • Select Mode (Input/Output/Alternate Function/Analog)
  • Select Speed (2-100Mhz)
  • Select Output Type (Push Pull/Open Drain)
  • Select Pull Property (Pull Up/Pull Down/No Pull)
Here is the code fragment of the algorithm above :


1
2
3
4
5
6
7
8
 GPIO_InitTypeDef GPIO_InitStruct;
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;   
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;  
 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;  
 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOC, &GPIO_InitStruct); 

Startup code is automatically generated by Keil and SystemInit function is called before main function. SystemInit function configures PLL parameters and sets the mcu at it's maximum frequency if possible.


Monday, September 29, 2014

ARM and STM32 Abbreviations


  If you are new to STM32 microcontroller or programming, while reading datasheets you are going to encounter a lot of terms or abbreviations that you don't know.

  I wanted to make a list for most of the terms and their abbreviations/descriptions. I'm going to update the list, feel free to offer additions.



A/D Converter: Analog Digital Converter
ADC : Analog Digital Converter
AHB : Advanced High Performance Bus
APB : Advanced Peripheral Bus
ART Accelator : Adaptive Real Time Accelator
B: Dedicated to BOOT0 Pin (Pin Abbreviation)
BCD : Binary Coded Decimal 
BGA : Ball Grid Array 
BJT : Bipolar Junction Transistor
BOR : Brownout Reset
BQFP : Bumpered Quad Flat Package
CAN : Controller Area Network
CF : Compact Flash
CMOS : Complementary Metal Oxide Semiconductor
CQFP : Ceramic Quad Flat Package   
CRC : Cyclic Redundancy Check 
CTS : Clear to Send
D/A Converter : Digital Analog Converter
DAC : Digital Analog Converter
DCE : Data Communication Equipment
DCMI : Digital Camera Interface 
DFU : Device Firmware Upgrade
DMA : Direct Memory Access
DMIPS : Dhrystone Million Instructions Per Second
DSP : Digital Signal Processing
DTE : Data Terminal Equipment
EMI : Electromagnetic Interference
EMS : Electromagnetic Susceptibility
ESD : Electrostatic Discharge
ESR : Equivalent Series Resistance
ETM : Embedded Trace Macrocell
EXTI : External Interrupt
FET : Field Effect Transistor
FIFO : First In, First Out 
FM+ : Fast Mode Plus (Pin Abbreviation)
FPGA : Field Programmable Gate Array
FPU : Floating Point Unit
FPU : Floating Point Unit 
FSMC : Flexible Static Memory Controller
FT : 5V Tolerant Input Output Pin (Pin Abbreviation)
FTB : Fast Transient Burst (Voltage)
FTf : 5V Tolerant Input Output Pin with FM+ capable (Pin Abbreviation)
GPIO : General Purpose Input Output
HSE : High Speed External (Oscillator/Clock)
HSI : High Speed Internal (Oscillator/Clock)
HVAC : Heating Ventilating and Air Conditioning
I : Input Only Pin (Pin Abbreviation)
I/O : Input Output Pin (Pin Abbreviation)
I/O : Input/Output 
I2C : Inter Integrated Circuit, aka I squared C
I2S : Inter-IC Sound, Integrated Interchip Sound
IC : Input Capture
IC : Integrated Circuit
IRDA : Infrared Data Association
IWDG : Independent Watch Dog
JTAG : Joint Test Action Group
LAN : Local Area Network
LIN : Local Interconnect Network 
LPR : Low Power Regulator 
LQFP : Low Profile Quad Flat Package
LSE : Low Speed External (Oscillator/Clock)
LSI : Low Speed Internal (Oscillator/Clock)
MAC : Media Access Control (Address)
MCU : Micro Controller Unit
MII : Media Independent Interface
MIPS : Microprocessor without Interlocked Pipeline Stages
MIPS : Million Instructions Per Second 
MISO : Master Input Slave Output (Serial Peripheral Interface Bus Abbreviation)
MMC : Multi Media Card
MOSI : Master Output Slave In (Serial Peripheral Interface Bus Abbreviation)
MPU : Memory Protection Unit
MR : Main Regulator
MSPS : Mega Sample Per Second
NC : Normally Closed 
NC : Not Connected
NO : Normally Open
NRST : nRESET (Pin)
NVIC : Nested Vectored Interrupt Controller
OC : Output Compare
PDR : Power Down Reset
PHY : Physical (Layer)
PLC : Programmable Logic Controller
PLL : Phase Locked Loop
PM Bus : Power Management Bus
POR : Power On Reset
PPB : Private Peripheral Bus
PVD : Programmable Voltage Detector
PWM : Pulse Width Modulation 
QFP :  Quad Flat Package
RAM : Random Access Memory 
RC : Resistor Capacitor
RMII : Reduced Media Independent Interface
RNG : Random Number Generator
RST : Bidirectional Reset Pin With Embedded Weak Pull Up Resistor 
RTC : Real Time Clock
RTS : Request to Send
RTR : Ready to Receive
S : Supply Pin (Pin Abbreviation)
SCL : Serial Clock Line
SCLK : Serial Clock (Serial Peripheral Interface Bus Abbreviation)
SDA : Serial Data Line
SDIO : Secure Digital Input Output
SM Bus : System Management Bus
SMI : Serial Management Interface
SPI : Serial Peripheral Interface 
SRAM : Static Random Access Memory
SS : Slave Select (Serial Peripheral Interface Bus Abbreviation)
SSCG : Spread Spectrum Clock Generation
SWD : Serial Wire Debug
TC: Standartd 3.3V Input Output Pin (Pin Abbreviation)
TIM : Timer
TPA : Trace Port Analyzer
TPIU : Trace Port Interface Unit 
TQFP : Thin Quad Flat Package
TTa : 3.3V Tolerant Input Output Pin Directly Connected to ADC (Pin Abbreviation)
UART : Universal Asynchronous Receiver Transmitter
ULPI : Utmi Low Pin Interface
USART : Universal Synchronous Asynchronous Receiver Transmitter
USB OTG : USB On The Go
UTMI : USB 2.0 Transceiver Macrocell Interface
VBAT : Battery Voltage Supply (Pin)
VCC :  Positive Supply Voltage (BJT)
VDD : Positive Supply Voltage (FET)
VEE : Negative Supply (Ground) (BJT)
VSS : Negative Supply (Ground) (FET)
WWDG : Windows Watch Dog


Saturday, September 27, 2014

STM32 Software Development Tools

   After deciding which microcontroller should i choose,  next question is what should I use to develop on STM32. You should select a development platform for your MCU (Microcontroller Unit). There are multiple choices :

1) KEIL MKD-ARM

   In my blog, i am going to use KEIL uVision and ST Standart Peripheral Library. KEIL MDK-ARM Microcontroller Devolopment Kit is not free unfortunately. But luckily it is fully functional up to 32KB projects.

32KB is fair enough for test applications. µVision (i will say uVision later) is a great IDE for development and debugging. It supoorts intellisense like feature called autocomplete.

Multiproject workspace feature allows simultaneous projects in the same environment. Advanced debug feature allows easy and detailed debugging sessions. 

KEIL supports latest ARM Cortex M7 processor. You can download from the below link :

http://www.keil.com/arm/mdk.asp




2) IAR EMBEDDED WORKBENCH FOR ARM



   IAR is an another successful compiler/debugger for ARM architecture. It also supports latest ARM Cortex M7 processors.

   It also integrates C-Run. C-Run is a runtime analysis add on. Buffer overflow detection, detection of value changes while casting operations are some of the features.


Unfortunately IAR is not free also. But you have two options :

a) Time Limited Version : You can use IAR for 30Days with full functionality, you can not use for commercial applications.

b) Size Limited Version : Your code is limited to 32KB,  no MISRA C support, no power debug support, runtime libraries are not included.

Yes i know a third option also ;)

You can download IAR from below :

http://www.iar.com/Service-Center/Downloads/




3) HITEX (HiTOP FOR CORTEX)

    HiTOP is another development platform for Cortex M processors. This one has also code size and time limitation.



Personally i haven't tried yet, you can check from the below link :

http://www.hitex.com/index.php?id=download-hitop540&L=%255C%255C%255C%2527241

Link changes according to version, if the above link is broken then go to hitex web site's download section :

http://www.hitex.com




4) ATOLLIC (TRUESTUDIO FOR ARM)

    Atollic TrueStudio is a compiler and debugger for ARM. Importing Eclipse IDE and GNU Toolchain projects are supported.


You have to pay for good ones, 30 day evaluation version is avaliable, check the link below :

http://atollic.com/index.php/truestudio





5) RAINSONANCE (RKit-ARM)

    RKit-Arm is a compiler, debugger and editor package for ARM. It has an IDE (Integrated Development Environment) called Ride7.

   As always this one is not free also. But you can use for 30 Day in evaluation mode. Check the links below for details :

http://www.raisonance.com/
http://www.raisonance.com/ride7.html




6) MIKROELEKTRONIKA (mikroC, mikroBASIC, mikroPASCAL for ARM)

    MikroElektronika has it's own libraries for popular devices like touch control chips, lcd libraries, sd/mmc card libraries etc and wide example projects.

   You can use three different products : mikroC, mikroBASIC or mikroPASCAL. Costs about $250 to $300, free versions are fully functional but has a funny 8KB code limit.

meh, 8KB code limit is funny for arm. You can check mikroelektronika web page :

http://www.mikroe.com/arm/compilers/




7) CODE::BLOCKS

    Code::Blocks is a free IDE for C and C++. For using STM32 development you have to install an external debugger plugin called Embedded Plugin Suite (EPS)


    Code::Blocks IDE is free but EPS costs about €120 per series, ie if you want STM32F1 and STM32F4 support you have to pay €240.

Code::Blocks web site :

http://www.codeblocks.org/

Embedded Plugin Suite web site : 

http://www.comsytec.eu/

 

 8) CooCox CoIDE

    You are not going to believe me but it is free ! CooCox has integrated CoBuilder and CoDebugger features. 


Another good side is you can use CoOS, a free open source RTOS (Real Time Operating System). Give it a try, here is the link :

http://www.coocox.org



 9) ECLIPSE IDE

   Eclipse IDE is not enough for embedded programming alone but with a few tools, you can develop for free. What you need is a compiler, debugger and maybe a RTOS.




   You can use GCC (GNU Compiler Collection) for free. For debugging purposes you can use Open OCD (Open On Chip Debugger), and it is free also. If you need a RTOS then freeRTOS is a good choice.

So Eclipse IDE + GCC + Open OCD + FreeRTOS is a killer combo.

And here is the GNU ARM Eclipse Plugins :

http://sourceforge.net/projects/gnuarmeclipse/




 10) MIOSIX
 
  For using Miosix you will need a computer running on Linux. I hope they release Windows version also because it has mxgui, you can get real time 3D renders really fast.
Miosix uses Netbeans IDE. But Netbeans is not supporting Miosix, you have to configure it for yourself.

For instructions an downloads here is the Miosix web page link :

http://miosix.org