Sunday 9 April 2017

Typecasting in Embedded C


"In this blog we will study about “Typecasting in Embedded  C”

Type casting in C language is the process of converting one data type variable to another data type. For example converting integer value to long integer value. These are also called short instruction in embedded C. Some time type casting is handled by the compiler and in some IDE’s programmer has to take care of this.   On the base of compiler capability types casting is of two types:
Implicit Type casting: in this Compiler will take care of all type casting functionality. 
Explicit Type casting: In this programmer has to take cares type casting when solving mathematical expressions. 
Syntax of typecasting is as follows:
(data_type)expression;
Here data_type is the new data type in to which we have to convert expression. Type casting also called short instruction in embedded C.

/**********************Example of type Casting************************/
unsigned char var1;     // declaration of unsigned character var1
float var2;                    // declaration of float variable var2
unsigned int var3;       //declaration of unsigned int variable var3

var1=40;                      // save value 40 to var1
var3= var1*10;           // if compiler does not support implicit type casting after this instruction                        //execution  var3 will contain 190. Because compiler consider var1 as char
var3 =  (unsigned int)  var1/10;  // after execution of this instruction var3 will contain 400.
var1= 55;
var2=(float)var1/6;  //in this case first we have type cast var1 to float and then divide by 6. 
                                   //In this case var2 will contain 9.166666
There are two type of type casting first is Upcasting and Downcasting. If we converting data type from lower data type to higher data type i.e. chats to integer or integer to float, then it is called Upcasting.  If we are converting from higher data type to lower data type, it is called Downcasting.
Down casting help when we have to convert 16 bit integer values to two bytes. Lower byte can be easily extracted without shifting operation.  For example:
var3 = 0x1234;
var1 = (unsigned char)var3;
in this case we type cast var3 to unsigned char.  After execution of this instruction var1 will contain 0x34.
Type casting is also used in arithmetic operation to get correct result. this is very much needed in case of division when integer gets divided and the remainder is omitted. In order to get correct value always use typecasting.


Sunday 5 February 2017

The Future of IoT is Embedded Systems and Real-time Operating Systems (RTOS)

Embedded systems are highly customized, developed and programmed as per user requirements.  Conventional embedded systems are electronic components that possess a microprocessor, peripherals, memory card, and software program that run instructions, operating system and tools (debuggers, static and non-programmable), and follows firmware programming embedded in the micro-chip.
Embedded systems will play an important role in Internet of Things (IoT) due to their unique characteristics and features such as real time computing, low power consumption, low maintenance and high availability are becoming the key enabler of IoT.  Major players in embedded system hardware and software developments are aiming to bring these transformations into their products to take advantage of growing IoT market. The areas that are going to transform are Real Time Operating Systems (RTOS) and microprocessors and microcontrollers, followed by memory footprints and networking, open source communities and developers.
Smart embedded systems will require changing contemporary embedded system design and architecture to suit real-time operations, smaller size of the unit and lowered power consumption and become cost efficient. Use of microcontroller and technologies such as Systems on Chip (SoC) and Reduced Instruction set Computing Chips (RISC) will have greater scope in IoT.
Related to embedded systems is Real-time Operating Systems (RTOS), which is an OS that manages hardware resources, hosts applications, and processes data on real-time basis.  RTOS defines the real time task processing time, interrupt latency, and longer period reliability of both hardware and applications, especially for low powered and memory constrained devices and networks.  The key difference between RTOS and a general purpose OS lies within its high degree of reliability and consistency on timing between application's task acceptance and completion.

What is the future of a career in embedded systems engineering?

The field of embedded systems engineering has given me a good steady income since the 1970s, and it still does to this day.  The number of "intelligent" devices continues to multiply rapidly, and all of those devices are "embedded systems" (a device with a processor in it which is not considered a "traditional" computer).  All of those devices run on code, therefore needing software engineers skilled in the technologies of embedded systems to create that code.  As the number of different intelligent devices in the world grows, so does need for embedded engineers - it's that simple.  And since it appears that almost every technological device has an embedded processor in it, and the number and kind of those devices will only continue to increase there is most certainly a very bright future for embedded engineers.  Consider the latest tech "trend", IoT (Internet of Things) - it's all about embedded devices!!! 
A good analogy is cloud providers like AWS, GCE etc. Once upon a time, if you had to develop your application, on top of writing the application logic, you also had to configure DNS routing, load balancing, caching, data-store etc. Now that applications are getting hosted online via popular cloud service providers, most of this routine, non-creative but very-much-needed work is automatically given to you as basic platform services, leaving you with just the creative component of your unique application. This aspect of PaaS/IaaS providers is called as non-differentiated heavy lifting.
This is bound to happen in embedded systems engineering as well. As a matter of fact, its well underway with commodity hardware, off-the-shelf chips coming inbuilt with networking and routing, sensor and drones manufactured inexpensively in China/Taiwan with in-built MPLS, GPS algorithms, plug and play diagnostic routines etc, etc. You name it.
So the key is to have a unique value proposition for yourselves, beyond simply knowing to program in C/C++, experience in BIOS/PROM, SAS, PCI-e, I2C. That kind of job does exist very much in the market now, think IoT, connected cars, VR headsets or even the Pebble smart watch. It can be a niche role.

Monday 12 December 2016

Implementation of Task Scheduler


Implementation of Task Scheduler with Function pointer
“In this blog we will discuss how to implement Task Scheduler in Embedded C programming”
Implementing Task Schedulers mainly require following steps:

  • Define task interval Counter Value
  • Define Task
  • Structure Declaration
  • Function Prototyping
  • Function Definition
  • Array of Structure Declaration
  • Main Routine

Define task interval Counter Value


In this example we have run a timer with ISR with 100 usec interrupt. System_Time_Counter value will be incremented in Timer ISR.

System_Time_Counter value for 1ms Interval = 10

System_Time_Counter value for 5ms Interval =  50 

 For all tasks we define macros for task interval value described below:



#define TICK_5ms      50     // 5ms = 5×10

#define TICK_10ms     100    // 10ms = 10×10

#define TICK_50ms     500    // 50ms = 50×10

#define TICK_100ms    1000   // 100ms= 100×10

#define TICK_200ms    2000   // 200ms= 200×10



After this we define as macros of NO_OF_TASK we want to execute


#define NO_OF_TASKS    5



Function Prototype


Function prototyping should be done before Task table declaration:

void TASK1_FUNC(void);

void TASK2_FUNC(void);

void TASK3_FUNC(void);

void TASK4_FUNC(void);

void TASK5_FUNC(void);


Function Definition


In function definition I have toggling a GPIO pin to check the execution time of each task:



void TASK1_FUNC(void)

{

TOGGLE_PIN1;

}

void TASK2_FUNC(void)

{

TOGGLE_PIN2;

}



void TASK3_FUNC(void)

{

TOGGLE_PIN3;

}





void TASK4_FUNC(void)

{

TOGGLE_PIN4;

}



void TASK5_FUNC(void)

{

TOGGLE_PIN5;

}



Structure Declaration Task Table Declaration


In this we declare a structure TASK_STRUCTURE for task table. In structure declaration defines three parameters i.e. function pointer associated with each task, Task repetitive interval and Task_Last_Execution_Time as shown in below:



typedef struct{

   void (TASK_FUNC)(void);

   unsigned int Repititive_Execution_Times;

   unsigned int Task_Last_Execution_Time;

  }TASK_STRUCTURE;



After this make the table of each task by making an array of structure:

TASK_STRUCTURE TASK_STRUCTURE _ARRAY[]=

       {

           {TASK1_FUNC,  TICK_5ms  , 0 },
           {TASK2_FUNC,  TICK_10ms  , 0 },
           {TASK3_FUNC,  TICK_50ms  , 0 },

           {TASK4_FUNC,  TICK_100ms  , 0 },

           {TASK5_FUNC,  TICK_200ms  , 0 }

      };

Initial value of Task_Last_Execution_Time set to zero.



Main Routine


In main routine we run a for loop using TASK_INDEX to execute all tasks. Here SYSTEM_TIME is the counter that is running in Timer ISR and incremented after every 100 usec. 

unsigned char TASK_INDEX=0;

static TASK_TYPE *TASK_PTR;      
// Pointer Declaration for Task Array

TASK_PTR= TASK_STRUCTURE _ARRAY;      

           

           

for(TASK_INDEX=0;TASK_INDEX<NO_OF_TASKS;TASK_INDEX++)

{

  if((SYSTEM_TIME-TASK_PTR[TASK_INDEX].LAST_EXECTION_TICKS)>=     TASK_PTR[TASK_INDEX]. Repititive_Execution_Times)

    {

       (*TASK_PTR[TASK_INDEX]. TASK_FUNC)();    // function call

TASK_PTR[TASK_INDEX]. Task_Last_Execution_Time =SYSTEM_TIME;

//Copy SYSTEM_TIME in Task_Last_Execution_Time value.

    }

}



So this is the implementation of Task Scheduler using function pointer. If you want to add more task functions these can be implemented easily. If any task requires higher priority of execution it can be map on the External Interrupt event.

Sunday 11 December 2016

Task Scheduling



Task Scheduling In Embedded Software


“In this blog we will discuss about how to do task scheduling in Embedded Software. Discuss the method for task scheduling and Implementation of task Scheduling”

 Today’s embedded system product requires real time execution of different function. So the software architecture should be implemented in such a way that all functions should be executed in real time and should response to external world quickly. We can achieve real time execution using Task Scheduling. In task scheduling we divide the whole system in small tasks and define time for each task when it will be execute. All tasks will be executed in the round robin fashion. System operating system will monitor each task set execution time when it would be executed. In this scenario we can’t define the priority of task. Next task will be executed after complete execution of current task. If any process requires priority over this task scheduler function we can port that task on some external interrupt.

Task Scheduler Working

In this task scheduling method we monitor the system timer (which can be generated by running a counter in Timer ISR routine) and execute tasks when tasks execution time occurs. In this single task will be executed completely without interruption of other tasks. Task Scheduler mainly requires three parameters related to each task, first is function related to task, how often task will be executed i.e. called Interval Of task and time  when last time task will executed.  For this we make an array of structure with these three parameters and  use function pointers to call the task. System time will be generated through running a counter in timer ISR.
Figure below show the flow chart of Task scheduler routine:


As shown in flow chart after system peripheral initialization array of Structure will be initialized with pointer to function, task interval and last time executed time default to 0. After this compare the difference of System time and task last executed time with the task interval. If difference is greater than task repetitive interval than executed the task else check for next task time. After execution of task update the task last executed time with System current time.