Monday, March 7, 2011

ARM exceptions and Debugging techniques

ARM exceptions and Debugging techniques :


1.Reset:
  • Common reasons for Reset
  • Function call using a NULL pointer
  • Asserts / System Aborts
  • Hardware error
Mostly a NULL pointer is the cause for RESET. Check out the R14 and find out which was the last instruction before jumping to PC 0.In case of condition check fails the programs can choose to ASSERT the system. In case of invalid scenarios in RTOS too, the system may abort. Putting Breakpoints at the common abort points like ASSERT will be the first step to debug these problems.Hardware errors are less common reason for NULL pointer exceptions. Usually the ABORT signal from the peripherals is routed through the Interrupt Controller. When a system resets no handling for interrupts are done. So the registers of Interrupt Controller will be preserved. It will be helpful to check out the registers to find out the source hardware that caused the reset.



2.Data Abort:
  • Accessing Uninitialized Pointers
  • Stack overflow, array over indexing(usually local)
  • Invalid Operation on a memory
  • Memory Failure

The memory subsystem will generate a signal in case of any of the above
Accessing uninitialized pointers will cause ARM to fetch data from areas of memory that are not defined.If you try to do invalid operation on a particular memory device, eg: If you try to write to ROM, it will cause Data Abort.In both the above cases we can know which instruction caused the data abort, by checking out R14 value.Stack overflow or array over indexing will cause some data pointers to be corrupted. This might cause a Data Abort. (Check out the last section, dealing with stack and array overflows).In case of memory failure… nothing much can be done. Check if the Interrupt Controller has some registers which will show which memory system Aborted.


3.Pre-fetch Abort
Pre-fetch abort is similar to Data Abort. It is caused when Instructions are fetched from invalid address.
Usual reasons are :
  • Calling a function via an Uninitialized Function Pointer
  • Stack overflow, array over indexing(usually local)
Approaches similar to Data Abort can be followed.


4.Undefined Instructions
This exception occurs when the instruction fetched from memory cannot be decode. This can be caused by:
  • Memory corruption by Stack overflow or Array over indexing
  • Un-initialized Function Pointer
If a memory which was originally supposed to contain code, gets overwritten due to some memory corruption, the instructions will become invalid. This error is less common in systems where code executes from FLASH. In such systems (called XIP) only some small set of code will be in RAM.Calling function using an Un-initialized Function Pointer can cause this issue.Check out R14.


What are CRASHES?
Crashes are unhandled EXCEPTIONS. If you are looking at a crash basically you are looking at an EXCEPTION. Find out what is the exception and start digging for clues depending upon the type of exception.


How does Memory get corrupted???
Memory gets corrupted due to Uninitialized Pointer,Stack Overflow,Local Array over-indexing and Global Array over-indexing.

When a system crashes due to un-initialized pointers, the LR value will directly give the place where the pointer was accessed. This is usually one of the easiest of memory corruptions to be tracked. Usually these are very consistent in nature.Stack overflow usually happens in heavy system loads. If you are regressing something and you got some aborts, then it can be a stack overflow. One easy way of finding out is to double the stack size and see, whether the crash occurs again or not. This will give you more confidence to debug in this direction. [This is not a litmus test].Some OS’s write a FENCE to the lowest address of a Stack. A FENCE is a special sequence of bytes like CDCDCDCDCD or DEADDEADDEADDEAD which are used to mark boundaries or show unused memory area etc. If the FENCE has been breached, then there is a stack overflow.Local array over-indexing can cause local pointer variables to get corrupted. If you are seeing some local variables corrupted, it can be a local array over-indexing issue.If you are seeing some global array corruption, it can be a global array over indexing.


Array Over-Indexing Vs Stack Overflow :There is a basic difference between Array over Indexing and Stack Overflow. Stack grows from a higher memory area to a lower memory area. While during Array over indexing u will corrupt data from lower memory area to higher memory area. In the memory dump, check which area is getting corrupted and you will get to know who the actual culprit is.

Embedded System -- Day 3

Embedded System -- Day 3

8051 supports external memory interface.

ARM
-- M3 [ Only Microcontroller ] , generally used ARM7
-- A8 [ ARM9 ]
-- R [ real time critical applications]

Multimedia applications : TI Da vince Processors.

Shifting any number of Bits in single cycle : Barell shifters for fast calculation in DSP processors.

PWM is used for simple audio feeding the PWM using RC circuit.

8051 - Storage Class Specifier :
-- Code : Program Memory(64Kbytes) ; accessed by MOVC@A+DPTR
-- data : Directly addressible internal memory
-- bdata : bit adrresible
-- idata : internal
-- xdata : external data memory.

Static Address Number is used for Interrupt in 8051 and Vector Table Method is used in ARM.


Syncronous Transfer : clock transmited along with data.
Asyncronous Transfer : No clock is transmitted , in UART , we need to manually synchronize.
Magic Lt - debugger for JTAG

Embedded System Training -- Day 2

Embedded System -- Day 2

// Important Program for Volatile
int main()
{

volatile const int x = 10;
int *p = (int *)&x;
*p=20;
printf("%d",x); //if we do not write volatile we will see this as 10 .
printf("%d",*p);

}


Structure:
The Compiler maintains a Structure Defination Table[SDT] which has got offset
name,type,offet
a,char,0
x,double,1
y,char,9
with help of this table, at runtime memory is allocated for structures.

struct student p1;
p1.x = 10;
The address of p1 is resolved using Symbol table and Offset is taken from SDT.

Embedded System Training - Day1

Embedded System Training - Day 1
A system is a way of working , organizing or doing one or many tasks
e.g.
Time display system.
Automatic Cloth Washing Machine : Wash by spinning a motor .
Computers Vs Embedded System
1)Combination of HW and SW.
2)Operating System manages many tasks at a time.
3)Combination of HW and SW which works towards specific goal is called embedded system.As a manufacturer, we clearly predict what embedded system is used for.
Optimally Designed HW and Dedicated Software and Firmware.
CPU sits on top of firmware , sensors and actuators communicate through them.
ES domains
a)Automotive
b)Aerospace
c)Livestock Management
d)Medical

Embedded Systems are not used for computation these days..

Real time system : A time constrained Embedded system.
Hard and Soft Real time systems.

Types of Developement
Native Developement : Desktop application and normal development.
Cross Development : Usage of two environment, host machine[PC] and Target [ where application gets executed]
Here the program executes in different machine and developement happens in different machines.
This interface between the Host and Target can be anything * [Doubt1]
Device corresponds to Data Sheet.ESD is all about Cross Development.


Components of embedded system :
System-On-Chip
1.Processor
2.Program Memory and Data Memory
[Memory is no more critical due to advancement in Memory technology]
3)Timers
4)Interrupt Controller
5)Parellel Ports
6)Serial Communication Port
7)Clock Generation Unit
8)Power Supply and Management
[Critical in all systems]
9)Output Interface
10)System Application Specific Circuits.
Power Source :
Systems may or may not have own power supply
digital watch
media player

System which do not have power supply
-- Network Interface card
-- Pen Drive
-- External Hard Disk.
Power Requirements :
-- 5.0V +/- 0.25V
-- 3.3V +/- 0.3V
-- 2.0V +/- 0.2V
-- 1.5V +/- 0.2V
Power Management :
-- Optimizing the code.
-- Enabling and Disabling Caches and Cache blocks
[High speed memory,bypassing accessing of Cache can save power]
-- Reduction of the clock rate during specific set of instruction.
Clock Oscillator and Clocking Units
-- of the cpu
-- of the system timers
-- Fetching codes and data,decoding and execution

Why exe needs to be moved from Secondary memory to RAM?
Processor is not capable : Harddisk is block addressible and RAM[primary memory] is byte addresible .Microcontrollers are Bit-addresible.Flash is Byte Addresible*[semi-conductor based-- Doubt2]
Reset Circuit
1)CPU begins the processing the instruction from a starting address.
2)Program which runs after reset on powerup
-- A system program that executes from the beginings
-- A system boot-up program
-- A system initialization program
3)Reset on WDT expiry.
Types of Memory:
--Internal and external RAM.
--Internal Caches
-- Flash EEPROM [ external or internal ]
-- ROM/PROM
-- NVRAM
ROM/EROM/FLASH
-- RTOS & Application program bundled together
-- Codes for System Booting/Initialization/Initial input data and strings.
-- Non-Volatile results for processing.
RAM[ Internal/ External / Buffer RAM]
-- Storing the code segment
-- Stack/Heap memory

NOR Flash : Kernel, Configuration data , hence protection is there.
NAND Flash :FS,Application,User Space.

Caches
-- copies of instruction/data and brance tranfer instruction .
Interrupt Handler
-- external port interrupt
-- IO interrupt
-- timer and RTC interrupt
-- SW interrupt
Design Process in ES
-- Abstraction
-- HW & SW Architecture
-- Extra functional properties
-- System related family of design [ Not valid much as embedded devices are very much different from each other]
-- Modular Design [ reusable ]
Design process contd....
-- UI design
-- Refinements
Developement Process
-- Model Analysis
-- Design SW architecture,interfaces and algorithms
-- Implementation of Design
-- Test Internal Logic and External Functions.
Design Metrics
-- Power dissipation
-- Performance
-- Process deadlines
-- User Interfaces [ IOS ]
-- Size : code and device both
-- Engineering Cost
-- Manufacturing Cost
-- Flexibility
-- Prototype
-- Development time
-- Time to Market
-- System and user safety
-- Maintenence
Challenges in ESD
-- Clock rate reduction
-- Voltage reduction
-- Wait , Stop and Cache disable functions
-- Process deadlines
-- Flexibility and upgrade ability

ROM image : Final stage of Software image
-- Just as an image in a unique sequence and arrangement of pixel
-- Embedded software must be placed in proper address.
-- Placed in Non-Volatile Memory
General Concepts :
Boot Loader is loaded through MBR.
Boot Loader loads the Operating System.
Basic GCC
gcc -e file.c ---> output to monitor
gcc -s file.c ---> .s file
gcc -c file.c ---> .o file
gcc -d INTEL file.c ---> conditional compilation using INTEL which defined externally.

Small Scale ES
-- Designed with a single 8 or 16 bit microcontroller
-- Little HW & SW complexities and involved board design.
Medium Scale ES
-- 16 bit or 32 bit micro controllers
-- DSP and RISC

Sophisticated ES
-- Enormous SW and HW complexities
-- Scalable and Configurable Processors
-- HW and SW co-design and component integrations in final stages.

Find the Max Value for Datatypes

/* This is independent of all Platforms */

int main()
{

printf("Max Char is %d",(char)((unsigned char )~0 >> 1));
printf("\nMax short int is %d",(short int)((unsigned short int )~0 >> 1));
printf("\nMax long int is %d",(long int)((unsigned long int )~0 >> 1));
return 0;
}