Saturday, September 3, 2016

Blink LED Using Cortex-M3


The Cortex-M3 and Cortex-M4 are processors designed by ARM. The Cortex-M3 processor was the first of the Cortex generation of processors, released by ARM in 2005 (silicon products released in 2006). The Cortex-M4 processor was released in 2010 (released products also in 2010). The Cortex-M3 and Cortex-M4 processors use a 32-bit architecture. Internal registers in the register bank, the data path, and the bus interfaces are all 32 bits wide. The Instruction Set Architecture (ISA) in the Cortex-M processors is called the Thumb. ISA and is based on Thumb-2 Technology which supports a mixture of 16-bit and 32-bit instructions.
The Cortex-M3 and Cortex-M4 processors have:

  • Three-stage pipeline design
  • Harvard bus architecture with unified memory space: instructions and data use the same address space.
  • 32-bit addressing, supporting 4GB of memory space
  • On-chip bus interfaces based on ARM AMBA. (Advanced Microcontroller Bus Architecture)Technology,which allowpipelined bus operations for higher throughput
  • An interrupt controller called NVIC (Nested Vectored Interrupt Controller) supporting up to 240 interrupt requests and from 8 to 256 interrupt priority levels (dependent on the actual device implementation)
  • Support for various features for OS (Operating System) implementation such as a system tick timer, shadowed stack pointer
  • Sleep mode support and various low power features
  • Support for an optional MPU (Memory Protection Unit) to provide memory protection features like programmable memory, or access permission control
  • Support for bit-data accesses in two specific memory regions using a feature called Bit Band
  • The option of being used in single processor or multi-processor designs
The ISA used in Cortex-M3 and Cortex-M4 processors provides a wide range of instructions:
  • General data processing, including hardware divide instructions
  • Memory access instructions supporting 8-bit, 16-bit, 32-bit, and 64-bit data, as well as instructions for transferring multiple 32-bit data
  • Instructions for bit field processing
  • Multiply Accumulate (MAC) and saturate instructions
  • Instructions for branches, conditional branches and function calls
  • Instructions for system control, OS support, etc.
  • Both the Cortex-M3 and Cortex-M4 processors are widely used in modern microcontroller products, as well as other specialized silicon designs such as System on Chips (SoC) and Application Specific Standard Products (ASSP).
In general, the ARM Cortex-M processors are regarded as RISC (Reduced Instruction Set Computing) processors. Some might argue that certain characteristics of the Cortex-M3 and Cortex-M4 processors, such as the rich instruction set and mixed instruction sizes, are closer to CISC (Complex Instruction Set Computing) processors. But as processor technologies advance, the instruction sets of most RISC processors are also getting more complex, so much so that this traditional boundary between RISC and CISC processor definition can no longer be applied.
There are a lot of similarities between the Cortex-M3 and Cortex-M4 processors. Most of the instructions are available on both processors, and the processors have the same programmer’s model for NVIC, MPU, etc. However, there are some differences in their internal designs, which allow the Cortex-M4 processor to deliver higher performance in DSP applications, and to support floating point operations. As a result, some of the instructions available on both processors can be executed in fewer clock cycles on the Cortex-M4.

Without wasting much time in learning theory, lets start with practical, in this set of tutorials we are going to use LPC1343 micro-controller from NXP family, which comes under the category of Cortex-M3.
BlueBoard LPC1343
With more than 10 different vendors selling C compiler suites for Cortex-M micro-controllers, deciding which one to use can be a difficult choice. The development suites range from open-source free tools, to budget low-cost tools, to high-end commercial packages.
The current available choices included various products from the following vendors:
  • Keil. Microcontroller Development Kit (MDK-ARM)
  • ARM. DS-5. (Development Studio 5)
  • IAR Systems (Embedded Workbench for ARM Cortex-M)
  • Red Suite from Code Red Technologies (acquired by NXP in 2013)
  • Mentor Graphics Sourcery CodeBench (formerly CodeSourcery Sourcery)
  • mbed.org
  • Altium Tasking VX-toolset for ARM Cortex-M
  • Rowley Associates (CrossWorks)
  • Coocox
  • Texas Instruments Code Composer Studio (CCS)
  • Raisonance RIDE
  • Atollic TrueStudio
  • GNU Compiler Collection (GCC)
  • ImageCraft ICCV8
  • Cosmic Software C Cross Compiler for Cortex-M
  • mikroElektronika mikroC
  • Arduino
Keil MDK-ARM and IAR Systems Embedded Workbench are two most popular and costly commercial compilers, in our tutorials we will also provide tutorials for these two, but initially we will start with CooCox CoIDE, which is free and very good to use, for debugging we will use CoLink-Ex which is from CooCox community, this debugger is very good and its hardware is open source, you can buy this cheap debugger for Cortex-M micro-controllers.

CoIDE can be downloaded from http://www.coocox.org/software.html
For CoLinkEx, have a look at this link ttp://www.coocox.org/hardware/colinkex.php

The following video shows how to create a project in CooCox CoIDE and a technique known as "Semi-hosting", which is very useful for debugging and printing messages without requiring any other interface, just the CoLink-Ex debugger will do the work.

Schematic Diagram for Blink LED's
Schematic Diagram
The LPC1343 development board which we are using in this tutorial has two Led's, one green and another is Blue, the following program will toggle the Led's after 1 second.
 #include "config.h"  
 #include "lpc13xx_gpio.h"  
 int main(void)  
 {  
   u32_t count = 0, time = 0;  
   boolean led_state = TRUE;  
   InitializeSystem();      // Initialize System Clock and SysTick Interrupt  
   // Power On the GPIO Clock  
   SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_GPIO, ENABLE);  
   // Set Direction as Output  
   GPIO_SetDir(PORT1, GreenLED, 1);  
   GPIO_SetDir(PORT1, BlueLED, 1);  
   time = millis();  
   while(1)  
   {  
     if(millis() - time > 1000)  
     {  
       time = millis();  
       if(led_state == TRUE)  
       {  
         led_state = FALSE;  
         // Set GPIO pins State High  
         GPIO_SetBits(PORT1, GreenLED);  
         GPIO_SetBits(PORT1, BlueLED);  
       }  
       else  
       {  
         led_state = TRUE;  
         // Set GPIO pins State Low  
         GPIO_ResetBits(PORT1, GreenLED);  
         GPIO_ResetBits(PORT1, BlueLED);  
       }  
     }  
 #ifdef SEMIHOSTING  
     printf("Count Value is %d\n", count);  
 #endif  
     count++;  
   }  
 }  
There is a file included as #include "config.h", this file is not a part of CooCox community, and is written for the LPC1343 Development Board, the content of this file is shown below.
 #ifndef CONFIG_H_  
 #define CONFIG_H_  
 // Un-Comment this to view debug data on SemiHosting window  
 // But this will hamper program speed.  
 // To run in run time Un-Comment this MACRO  
 #define SEMIHOSTING  
 #include <stdio.h>  
 #include "lpc13xx_syscon.h"  
 #include "micro.h"  
 #ifdef SEMIHOSTING  
 #include "semihosting.h"  
 #endif  
 #define  GreenLED   GPIO_Pin_10  
 #define  BlueLED    GPIO_Pin_11  
 #define  Buzzer     GPIO_Pin_5  
 // Function Prototype  
 void InitializeSystem( void );  
 u32_t millis( void );  
 #endif // CONFIG_H_  

There are two important functions in this file:
  1. InitializeSystem() 
  2. millis()
InitializeSystem will initialize Cortex-M3 clock and some system settings such as SysTick Interrupt, and the another functions which is millis() returns the number of milliseconds since the LPC1343 board began running the current program. This number will overflow (go back to zero), after approximately 50 days, if you had worked with Arduino, then you must be aware of this function and how important it is.

Led's status when this program runs in development board can be seen below:
LED On
LED Off

No comments:

Post a Comment