Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Systems Programming Pattern. Generation of Code in Embedded System main() { while (1) { … } main() { while (1) { … } xxx.c , xxx.h main() { while.

Similar presentations


Presentation on theme: "Embedded Systems Programming Pattern. Generation of Code in Embedded System main() { while (1) { … } main() { while (1) { … } xxx.c , xxx.h main() { while."— Presentation transcript:

1 Embedded Systems Programming Pattern

2 Generation of Code in Embedded System main() { while (1) { … } main() { while (1) { … } xxx.c , xxx.h main() { while (1) { … } main() { while (1) { … } main() { while (1) { … } main() { while (1) { … } main() { while (1) { } main() { while (1) { } xxx.s Iniitial codes xxx.lds memory address Compile gcc –c xxx.s –o xxx.o xxx.o // never return Compile xxx.o gcc –c xxx.c –o xxx.o Link ld –T xxx.lds xxx.o –o xxx.elf xxx.elf Generate ROM Image Objcopy –O ihex xxx.elf xxx.hex FlashROM programmer

3 Memory Image of Embedded Processor Interrupt Vectors ISR Executive binary Variable data area xxx.lds memory address is given by xxx.lds ROM RAM FlashROM RAM CPU

4 Startup method (Run from ROM) First instruction executed is in address 0 Jump to initialization codes Jump to entry of C code——main() ROM

5 First instruction executed is in address 0 Function of Initiation Codes Set SP registers ( Stack ) Copy ROM data to RAM Jump main() Startup method (Run from ROM) ROM RAM

6 Startup method (run from ROM) ROM sp RAM RAM allocation SP pointer to stack space Ensure stack pointer won’t invade heap area use LDS file to define RAM allocation scheme 堆栈空间 数据空间 First instruction executed is in address 0

7 Startup method (Run from RAM) 1.Copy itself and ROM data to RAM 2.Jump to entry point in RAM ROM RAM First instruction executed is in address 0

8 Startup method (Load Executable image to RAM and execute) 1.Copy external data into RAM 2.Jump to the entry of executable binary ROM RAM External Storage ( SD Card 、 USB Disk 、 Network IF ) First instruction executed is in address 0

9 Why run from RAM Faster Dynamically load executable codes multi-task/thread/process Run from ROM need less RAM space reliable low cost simple, high reliable. control oriented code often run from ROMlow cost simple, high reliable. control oriented code often run from ROM program with complex GUI and complex application often run from RAMprogram with complex GUI and complex application often run from RAM

10 Memory Management Static allocation User defined management methods

11 Static memory allocation pre-defined global variables Static variable within a function u8 buffer[8192]; // a 8KByte Buffer Space void func() { static u8 str[128]; … }

12 Dynamically allocate memory Using malloc/free (require C run-time library support) Write simple memory management code

13 Manage memory in fixed block size Free Node List Block User Data List 1 Block User Data List 2 Block

14 Support different block size Free Node List 1 Block Free Node List 2 Block …

15 Memory management function format() Construct link list for all memory block get_mem_block() Obtain an idle memory block from the line free_mem_block() release a memory block,return it to the memory link list

16 Implementation of Malloc function 初始状态 valid size

17 Initial state Allocated a block

18 Initial state Allocated a block Allocated another

19 Return one block Initial state Allocated a block Allocated another

20 Flowchart of malloc function Find the memory block large enough for allocation found ? fragment combination find memory block large enough for allocation found ? return memory pointer return error yes No yes

21 Data exchange between device driver and Application program INT signal Interrupt Applications

22 Data exchange methods Shared buffer Ring buffer Link list

23 Data exchange between device driver and Application program INT signal ISR Application Shared Buffer

24 Data exchange by shared buffer Data Data length INT signal ISR Application

25 Data exchange between device driver and Application program INT signal ISR Application Ring Buffer

26 Data exchange between device driver and Application program INT signal ISR Application Ring Buffer

27 Data exchange between device driver and Application program length of data block data block read pointer write pointer

28 Data exchange between device driver and Application program INT Signal ISR Application Linked List

29 Data exchange between device driver and Application program data pointer data pointer data pointer remove from the linked list add to the linked list pointer

30 Requirement to the embedded program Periodical Multi-task Real-time response

31 Example: Embedded data recorder Tasks Sample analog in fixed (high) frequency Output Control Signal Accept User inputs Update display time tasks

32 Cyclic Execution void main() { while (1) { measure(); check_user_input(); measure(); control_output(); measure(); display_and_store(); measure(); control_output(); } }

33 Cyclic Execution Frequency of each task (function) is fixed difficult to adjust period Response time is given by the slowest task(function) void main() { while (1) { measure(); check_user_input(); measure(); control_output(); measure(); display_and_store(); measure(); control_output(); } }

34 Adjust sample rate int meaure_data() { static last_sample_time; while (get_time () % sample_period) ; return read_data(); } // Ensure sample at time grid

35 INT controlled analog signal sampling void main() { while (1) { check_user_input(); control_output(); display_and_store(); control_output(); } } timer_ISR() { measure(); }

36 Mix mode Realtime operation is implemented by ISR un-realtime operation is implemented in main loop Example : Measure and analysis instrument with GUI GUI measurement Sample clock INT Shared Memory

37 split slow operation for realtime execution slow_operation() {static current_phase = SPHASE_1; switch (current_phase) { case PHASE_1: operation_1 (); current_phase = PHASE_2; break; case PHASE_2: operation_2(); current_phase = PHASE_3; break; case PHASE_3: operation_3(); current_phase = PHASE_1; break; } return; } PHASE_1 PHASE_2 PHASE_3 slow_operation() { operation_1(); operation_2(); operation_3(); }

38 Structure of event driven program ISR (Detect user inputs) ISR (Measure) ISR (Update display) INT of high frequency timer Button INT INT of low frequency timer // main loop void main {while(1);}

39 Event list main( ) { while (1) { if (there is pending event in list) { Get_event(); Process_event(); } ISR_1() { Add_event_to_list(); } ISR_2() { Add_event_to_list(); } ISR_3() { Add_event_to_list(); } Event List

40 Event list with priority main( ) { while (1) { if (there is pending event in lists) { Get_event(); Process_event(); } ISR_1() { Add_event_to_list(); } ISR_2() { Add_event_to_list(); } ISR_3() { Add_event_to_list(); } Event Lists high priority ( get event from no-empty list with highest priority ) low priority

41 Event list with time information main( ) { while (1) { if (first clock time out) { Execute event list on 1 st clock; take off 1 st clock; } } Time_ISR () { update 1 st clock; }

42 Programming requirement of ISR Fast and high efficiency If not fast enough? Consider put data processing into mainloop, while ISR focus on data receive and send

43 ISR and data processing main() { while (1) if (Buffer is full) { Data processing; Clear buffer full flag; } ISR() { if (Buffer is empty) { Fill Buffer with data; Set buffer full flag; } data data length (flag) Buffer

44 Structure of event driven program (polling mode) While (1) { switch (Read_IO_Event()) { case EVEN_A: Event_A_Handler( ); break; case EVEN_B: Event_B_Handler( ); break; case EVEN_C: Event_C_Handler( ); break ; } System IOA IOB IOC Event_A_Handler() { … } Event_B_Handler() { … } Event_C_Handler() { … } IO priority is determined by Read_IO_Event function

45 FSM programming State 0 Output 0 State 1 Output 1 State 2 Output 2 State 3 Output 3 State 4 Output 4 State 5 Output 5 Input A Input B Input C Input B Input C Input A Input B Input C Input B

46 while (TRUE) { Input = Get_IO_Data(); Switch (State) { case STATE_A: State = Data_Processing_for_State_A( Input ); break; case STATE_B: State = Data_Processing_for_State_B( Input ); break; case STATE_C: State = Data_Processing_for_State_C( Input ); break; } Data Proc. outputInput FSM programming

47 Device Driver Function Initialize device Data receive and transmit Device driver without ISR –may lost data Device driver with ISR –Need data receiving buffer

48 Device Driver without ISR ( Pool Mode ) Char get_data_block_mode() { while (*IO_port_status==EMPTY); return *IO_port; } char get_data_non_block_mode() { if (*IO_port_status==EMPTY) return INVALID_DATA; else return *IO_port; }

49 Device Driver without ISR ( Pool Mode ) void send_data_block_mode(data) { while (*IO_port_status==BUSY); *IO_port=data; } int send_data_non_block_mode(data) { if (*IO_port_status==BUSY) return FALSE; *IO_port=data; return TRUE; }

50 Device Driver with ISR ( Receive data ) Rx Controller Hardware FIFO CPU IRQ ISR Applciation Buffer API Device Driver

51 Device Driver with ISR ( Receive data ) ISR() { data=IO_port_read(); put_to_buffer(data); } read_data() { if (buffer_empty) return INVALID_DATA; return get_from_buffer(); }

52 Device Driver with ISR ( Sending data ) Tx Controler Hardware FIFO CPU IRQ ISR Application Buffer API Device Driver

53 Device Driver with ISR ( Send data ) ISR() { if (buffer_empty()) return; data=get_from_buffer(); IO_port_send(data); } write_data() { if (buffer_full()) return FALSE; return put_to_buffer(data); } Flush_transmitter_buffer() { while(!buffer_empty()); }

54 Start transmitting of 1 st data Data transmitting is continued by ISR triggled by previous “data sent” INT The first data transmission should be manually started write_data(data) { if (buffer_full()) return FALSE; fill_data(); if (device_idle()) transmit_first_data(data); return; }

55 FIFO Half Full/Empty ISR_FIFO_Half_Empty() { feed data to Tx FIFO; } ISR_FIFO_Half_Empty() { fetch data from Rx FIFO; } Tx FIFORx FIFO

56 ISR design requirement Return——No return value Speed——as fast as possible Can we reenter ISR (allow INT nest?)

57 Data Tx/Rx by timer ISR Tx Controller FIFO CPU ISR Application Buffer API Device Driver

58 Methods to increase efficiency of ISR Batch data transfer——DMA Simple memory management scheme Implement least work, leave remaining's to the mainloop Allow INT nest for higher priority INT


Download ppt "Embedded Systems Programming Pattern. Generation of Code in Embedded System main() { while (1) { … } main() { while (1) { … } xxx.c , xxx.h main() { while."

Similar presentations


Ads by Google