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.


40 comments:

  1. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.
    Selenium training in Chennai

    Selenium training in Bangalore

    ReplyDelete
  2. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks For Sharing The Information The information shared Is Very Valuable Please Keep Updating Us Time just went On reading The article Python Online Training Aws Online Training Hadoop Online Training Data Science Online Training

    ReplyDelete
  5. Thanks for sharing, nice post! Post really provice useful information!

    An Thái Sơn với website anthaison.vn chuyên sản phẩm máy đưa võng hay máy đưa võng tự động tốt cho bé là địa chỉ bán máy đưa võng giá rẻ tại TP.HCM và giúp bạn tìm máy đưa võng loại nào tốt hiện nay.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery

    ReplyDelete
  8. Roulette - The Roulette Software & Software Review - Betting
    Roulette. 아이벳25 The roulette software provides players with 저녁 메뉴 룰렛 a wide selection of different table games. The 888스포츠 roulette table features 실시간 스포츠 배팅 a lot of 꽁 머니 토토 사이트 popular games,

    ReplyDelete
  9. Casino 2021: Get a 50% deposit bonus up to €100
    Play the 익산 출장샵 latest casino games from leading software providers 대전광역 출장안마 and offer a top casino bonus. Discover 동두천 출장안마 hundreds of casino games and 광양 출장안마 get 보령 출장마사지 up to 50%

    ReplyDelete