1 of 16

EE 319K�Introduction to Embedded Systems

Lecture 3: C programming

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-1

2 of 16

Agenda

  • Recap
    • Variables
    • Functions
      • Parameters
      • Prototypes, Implementation, Invocation
  • Outline
    • Overflow (add/sub, mult, divide)
    • Application of arrays
    • Arrays
      • Index
      • Pointer
    • Debugging

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

n+n => n+1

n-n => n+1

n*n => 2n

n/n => n

Review prime pi examples from EE312H

  • Header file
  • Code file
  • Testmain file

3-2

3 of 16

IR distance sensor

#define K1 1195172

#define K2 -1058

int32_t IRconvert(int32_t n){

return K1/(n + K2);

}

int32_t IRconvert2(int32_t n){

int32_t d;

if(n<3000) return 800;

if(n>13000) return 100;

d = K1/(n + K2);

return d;

}

d = 1195172/(n – 1058 )

3-3

4 of 16

Arrays

  • Definition (type, size, allocation)

#define LEN 10

int16_t Data[10]; // global RAM

const int16_t Prime[5]={2,3,5,7,11}; // global ROM

void fun(void){ char name[8];

}

  • Index access

Data[0] = 55;

Data[1] = 72;

  • Zero index address calculation Buf[i]

32 bit: Buf+4*i

16 bit: Buf+2*i

8 bit: Buf+i

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-4

5 of 16

Array example

  • Definition (type, size, allocation)

#define LEN 10

int32_t aa[LEN];

int32_t bb[LEN];

  • Access

int32_t s=0;

for(int32_t i=0;i<LEN;i++){

s += aa[i]*bb[i];

}

  • Review: what is?

aa[0]

&aa[0]

aa

aa[i]

*aa

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-5

6 of 16

Array parameters

  • Parameter is pass by reference

int32_t dot(int32_t a[], int32_t b[], int32_t l){

int32_t s=0;

for(int32_t i=0;i<l;i++){

s += a[i]*b[i];

}

return s;

}

  • Invocation pass by reference

int main(void){

int32_t result;

result = dot(aa,bb,5);

while(1){

}

}

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

s RN 4

i RN 5

dot

MOV s,#0 ;s=0

MOV i,#0 ;i=0

forloop3

CMP i,R2 ;is i<l

BGE forDone3

ASL R6,i,#2 ;i*4

LDR R7,[R0,R6]

LDR R8,[R1,R6]

MUL R7,R7,R8

ADD s,s,R7

ADD i,i,#1

B forloop3

forDone3

MOV R0,s

BX LR

3-6

7 of 16

Thermodilution

uint32_t T[1000];

// temperature in C

// 100 Hz, 10 sec

#define K3 43660

uint32_t CardiacOutput(void){

uint32_t sum,i,CO;

sum = 0;

for(i=0; i<1000; i++){

sum = sum+T[i];

}

CO = K3/sum;

return CO;

}

3-7

8 of 16

Maximum of an array

int32_t max(int32_t data[],

uint32_t size){

int32_t ans,i;

ans = data[0]; // assume first is max

for(i=1; i < size; i++){

if(data[i] > ans){

ans = data[i];

}

}

return ans;

}

#define SIZE 10

int32_t MyData[SIZE];

int main(){int32_t myMax;

while(1){

// fill up MyData

myMax = max(MyData,SIZE);

}

}

3-8

9 of 16

Dot product

int32_t dot(int32_t a[], int32_t b[],

int32_t length){ int32_t s=0;

for(int32_t i=0;i< length;i++){

s += a[i]*b[i];

}

return s;

}

#define LEN 5

int32_t aa[LEN];

int32_t bb[LEN];

int main(void){

int32_t result;

for(int i=0; i< LEN;i++){

aa[i] = i;

bb[i] = 5;

}

result = dot(aa,bb,LEN);

while(1){ }

}

3-9

10 of 16

Capitalize letters in string

char name[10] = "Jonathan";

// uncapitalize every letter

void uncap(char str[]){int i=0;

while(str[i]){

str[i] |= 0x20;

i++;

}

}

void uncap(char *p){

while(*p){

*p |= 0x20;

p++;

}

}

3-10

11 of 16

Debugging

  • Black box testing
    • Inputs
    • Outputs
  • White box testing
    • Debug individual into pieces
    • Control/observe internal

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-11

12 of 16

Debugging

  • Testing, Diagnostics, Verification, Validation
  • Debugging Actions
    • Functional debugging, input/output values
    • Performance debugging, input/output values with time (how fast does it execute)
    • Resource debugging, I/O values w/ time and resources (how much memory, power, …)
  • Regression Testing

test recent change to existing system

full or partial selection of already executed test cases

    • Tracing, measure sequence of operations
    • Profiling,
      • measure percentage for tasks,
      • time relationship between tasks
    • Optimization, make tradeoffs for overall good
      • improve speed,
      • improve accuracy,
      • reduce memory,
      • reduce power,
      • reduce size,
      • reduce cost

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-12

13 of 16

Debugging

  • Control
    • Breakpoints
    • Step
      • Single Step, StepOver, StepOut
    • Run, Run to Cursor
    • Test one piece at a time
      • Stabilization
  • Observability
    • Debugger windows
      • Registers including xPSR
      • Memory
      • Watch Windows
      • Logic Analyzer,
      • GPIO Panel
    • Output (LEDs, LCD, printf)

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-13

14 of 16

Debugging Intrusiveness

  • Intrusive Debugging
    • degree of perturbation caused by the debugging itself
    • how much the debugging slows down execution
  • Non-intrusive Debugging
    • characteristic or quality of a debugger
    • allows system to operate as if debugger did not exist
    • e.g., logic analyzer, ICE, JTAG

  • Minimally intrusive
    • negligible effect on the system being debugged
    • e.g., dumps(ScanPoint) and monitors
  • Highly intrusive
    • print statements, breakpoints and single-stepping

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Tiwari

3-14

15 of 16

Lab 2 demo

  • Advice for debugging

Observe locals and globals

Single step

Breakpoints

  • Example of testing

How does the grader work?

Is it exhaustive?

Bard, Erez, Gerstlauer, Holt, Valvano, Yerraballi, Telang, Cuevas, Tiwari

3-15

16 of 16

Summary

  • Variables
            • Type
            • Size
            • Scope
            • Allocation
  • Functions
      • Declaration
    • Implementation
    • Innovation
  • Arrays
  • Debugging

  • Header file
  • Code file
  • Testmain file

3-16