EE 319 K Introduction to Embedded Systems Lecture
EE 319 K Introduction to Embedded Systems Lecture 13: 2 -D Arrays, Bitmaps, Sprites, Structs, Lab 10 Fall 2014 EE 319 K http: //youtu. be/Rf. BDu. GIxz. CU Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -1
Agenda q Recap v. Lab 9 v. UART, Interrupts v. FIFO Queues v. Race Condition, Critical section q Agenda v. Software design v 2 -D array v. Bitmaps v. Structs v. Lab 10 http: //www. youtube. com/watch? v=-p. IMVZZRb 7 Y Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -2
Software Design q Modular programming v. Make it easier to understand o Each screen is a complete story without scrolling v. Maximize the number of modules v. Minimize the interdependency o Bandwidth of data passed from one to another o Control coupling: actions in one cause effects in another o Shared variables (very bad) Book Section 5. 2 Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -3
Software Design q Design for test v. Consider how it will be tested while designing v. Module has three files o Header: What the module does o Code: How it works o Test: A main program used to test the module q Manage resources v. LCD graphics v. Time (processor cycles) o A fun game requires careful control of time v. Input/Output o Switches, slide pot, DAC, LCD Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -4
Callbacks // ********* Timer 0_Init ******** // Activate Timer 0 interrupts to run user task periodically // Inputs: task is a pointer to a user function // period in units (1/clockfreq) // Outputs: none void Timer 0_Init(void(*task)(void), unsigned long period){long sr; sr = Start. Critical(); SYSCTL_RCGCTimer_R |= 0 x 01; // 0) activate timer 0 Periodic. Task = task; // user function TIMER 0_CTL_R &= ~TIMER_CTL_TAEN; // 1) disable timer 0 A during setup // 2) configure for 32 -bit timer mode TIMER 0_CFG_R = TIMER_CFG_32_BIT_TIMER; // 3) configure for periodic mode, default down-count settings TIMER 0_TAMR_R = TIMER_TAMR_PERIOD; TIMER 0_TAILR_R = period-1; // 4) reload value // 5) clear timer 0 A timeout flag TIMER 0_ICR_R = TIMER_ICR_TATOCINT; TIMER 0_IMR_R |= TIMER_IMR_TATOIM; // 6) arm timeout interrupt NVIC_PRI 4_R = (NVIC_PRI 4_R&0 x 00 FFFFFF)|0 x 40000000; // 7) priority 2 NVIC_EN 0_R = NVIC_EN 0_INT 19; // 8) enable interrupt 19 in NVIC TIMER 0_CTL_R |= TIMER_CTL_TAEN; // 9) enable timer 0 A End. Critical(sr); } void User. Task(void){ static int i = 0; LEDS = COLORWHEEL[i&(WHEELSIZE-1)]; i = i + 1; } … Bard, Gerstlauer, Valvano, Yerraballi Timer 0_Init(&User. Task, 80000000); // initialize timer 0 (1 Hz) 13 -5
2 -D Array or Matrix q What: 2 rows and 3 columns, 8 bits each vunsigned char M[2][3]; q Why: v. Images v. Maps q How: (C uses row major) v. C code to access M[i][j] = 5; v. Write this in assembly (R 0=i, R 1=j) i = row j = column n= # of columns Base+n*i+j Base+2*(n*i+j) Base+4*(n*i+j) Num of bytes/element Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -6
2 -D Array or Matrix q What: 6 rows and 7 columns vint 8_t Connect 4[6][7]; q Why: v. Images v. Maps j i Base+2*(7*i+j) q How: (row major) v. Write C code to set array values to 0 v. Write in assembly Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -7
2 -D Array or Matrix q Assuming C[6][7] // check the rows for(i=0; i<6; i++){ for(j=0; j<4; j++){ if((C[i][j]==1) &&(C[i][j+1]==1) &&(C[i][j+2]==1) &&(C[i][j+3]==1)){ Iwin(); } } } j i 0 means free 1 means me -1 means you Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -8
Column 0 ST 7735 Graphics Format Row 0 Row 159 LCD is 160 rows, 128 columns, 18 bits/pixel Column 127 13 -9
BMP File Format q Sprites as objects moving across screen Small. Enemy sprites are 16 -bit color, 16 pixels wide by 10 pixels high Alien sprites are 16 -bit color, 32 pixels wide by 20 pixels high Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -10
Rendering a Sprite ST 7735_Draw. Bitmap(50, 100, Small. Enemy 30 point. B, 16, 10); 16 wide, 10 high F Placed at x=50, y=100 F FFFFFFFFFF FFF FFFF F F F F The raw data from BMP file to illustrate how the image is stored (0 s replaced with spaces). Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -11
BMP File Format const unsigned short Small. Enemy 30 point. B[]={ 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 07 E 0, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, 0 x 0000, }; Data is upside down Example BMP file pixel data written as C constant • The color is a rgb 565 model: • Red: 0 x 001 F =>red=31; green=0; blue=0 • Green: 0 x 07 E 0 => red=0; green=63; blue=0 • Blue: 0 x. F 800 => ref=0; green=0; blue=31 Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -12
Structure Definition q One object q Collection of data q Data has dissimilar types or meanings typedef enum {dead, alive} status; struct State { unsigned long x; // x coordinate unsigned long y; // y coordinate const unsigned char *image; // ptr->image status life; // dead/alive }; typedef struct State STyp; new data types Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -13
Structure Creation q Put in RAM if data changes q Initialized at run time, before main() the new type STyp Enemy[18]={ {0, 10, Small. Enemy 30 Point. A, alive}, {20, 10, Small. Enemy 30 Point. A, alive}, {40, 10, Small. Enemy 30 Point. A, alive}, {60, 10, Small. Enemy 30 Point. A, alive}, {80, 10, Small. Enemy 30 Point. A, alive}, {100, 10, Small. Enemy 30 Point. A, alive}, … {100, 30, Small. Enemy 10 Point. A, alive} }; Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -14
Structure Creation q Put in RAM if data changes q Run-time initialization inside main() STyp Enemy[18]; void Init(void){ int i; for(i=0; i<6; i++){ Enemy[i]. x = 20*i; Enemy[i]. y = 10; Enemy[i]. image = Small. Enemy 30 Point. A; Enemy[i]. life = alive; } } Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -15
Structure Example q Student database v. Set of student records q Structure definition struct Student { char Initials[2]; short id; struct Student *teammate; }; typedef struct Student SType; Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -16
Arrays of Structures q Pointers to specific elements #define XYpt &class[0] #define ABpt &class[1] #define RSpt &class[2]. . . q Array of structure creation SType class[6] = { {{'X', 'Y'}, 123, RSpt}, {{'A', 'B'}, 23, RYpt}, {{'R', 'S'}, 11, XYpt}, . . . {{'R', 'Y'}, 2457, ABpt}}; // XY // AB // RS // RY Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -17
Arrays of Structures q Traverse array Write code to navigate through the class array and print all student records in the following format: FI-LI : id (team-mate_id) q Add features v. Seating chart SType seat. Chart[5][24]; //2 -D array v. Write a function to place a student into seat Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -18
Timer 2 A Periodic interrupt • Resolution: bus period • Precision: 32 bits • Max period: 53 sec (80 MHz) 0) activate timer 2 clock 1) disable timer 2 A 2) Precision to 32 bits 3) periodic mode 4) TAILR value 5) clock resolution 6) clear timeout flag 7) arm timeout 8) priority 4 9) enable in NVIC 10) enable timer 2 A Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -19
INTERRUPT VECTORS 77 total Vector address 0 x 00000038 0 x 0000003 C 0 x 00000040 0 x 00000044 0 x 00000048 0 x 0000004 C 0 x 00000050 0 x 00000054 0 x 00000058 0 x 0000005 C 0 x 00000060 0 x 00000064 0 x 00000068 0 x 0000006 C 0 x 00000070 0 x 00000074 0 x 00000078 0 x 0000007 C 0 x 00000080 0 x 00000084 0 x 00000088 0 x 0000008 C 0 x 00000090 0 x 00000094 0 x 00000098 0 x 0000009 C 0 x 000000 A 0 0 x 000000 A 4 0 x 000000 A 8 0 x 000000 AC 0 x 000000 B 0 0 x 000000 B 4 0 x 000000 B 8 0 x 000000 BC 0 x 000000 C 0 0 x 000000 C 4 0 x 000000 C 8 0 x 000000 CC 0 x 000000 D 0 0 x 000000 D 4 0 x 000000 D 8 0 x 000000 DC 0 x 000000 E 0 0 x 000000 E 4 0 x 000000 E 8 0 x 000000 EC 0 x 000000 F 0 0 x 000000 F 4 0 x 000000 F 8 0 x 000000 FC Number 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 IRQ -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ISR name in Startup. s Pend. SV_Handler Sys. Tick_Handler GPIOPort. A_Handler GPIOPort. B_Handler GPIOPort. C_Handler GPIOPort. D_Handler GPIOPort. E_Handler UART 0_Handler UART 1_Handler SSI 0_Handler I 2 C 0_Handler PWMFault_Handler PWM 0_Handler PWM 1_Handler PWM 2_Handler Quadrature 0_Handler ADC 1_Handler ADC 2_Handler ADC 3_Handler WDT_Handler Timer 0 A_Handler Timer 0 B_Handler Timer 1 A_Handler Timer 1 B_Handler Timer 2 A_Handler Timer 2 B_Handler Comp 0_Handler Comp 1_Handler Comp 2_Handler Sys. Ctl_Handler Flash. Ctl_Handler GPIOPort. F_Handler GPIOPort. G_Handler GPIOPort. H_Handler UART 2_Handler SSI 1_Handler Timer 3 A_Handler Timer 3 B_Handler I 2 C 1_Handler Quadrature 1_Handler CAN 0_Handler CAN 1_Handler CAN 2_Handler Ethernet_Handler Hibernate_Handler USB 0_Handler PWM 3_Handler u. DMA_Error Lab 7 Lab 8 Lab 9 Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Yerraballi NVIC_SYS_PRI 3_R NVIC_PRI 0_R NVIC_PRI 1_R NVIC_PRI 2_R NVIC_PRI 3_R NVIC_PRI 4_R NVIC_PRI 5_R NVIC_PRI 6_R NVIC_PRI 7_R NVIC_PRI 8_R NVIC_PRI 9_R NVIC_PRI 10_R NVIC_PRI 11_R NVIC_PRI 11_R Priority bits 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 7– 5 15 – 13 23 – 21 31 – 29 13 -20
Timer 2 A Periodic interrupt unsigned long Timer. Count; Max is 53 sec void Timer 2_Init(unsigned long period){ unsigned long volatile delay; SYSCTL_RCGCTIMER_R |= 0 x 04; // 0) activate timer 2 delay = SYSCTL_RCGCTIMER_R; Timer. Count = 0; TIMER 2_CTL_R = 0 x 0000; // 1) disable timer 2 A TIMER 2_CFG_R = 0 x 0000; // 2) 32 -bit mode TIMER 2_TAMR_R = 0 x 00000002; // 3) periodic mode TIMER 2_TAILR_R = period-1; // 4) reload value TIMER 2_TAPR_R = 0; // 5) clock resolution TIMER 2_ICR_R = 0 x 00000001; // 6) clear timeout flag TIMER 2_IMR_R = 0 x 00000001; // 7) arm timeout NVIC_PRI 5_R = (NVIC_PRI 5_R&0 x 00 FFFFFF)|0 x 80000000; // 8) priority 4 NVIC_EN 0_R = 1<<23; // 9) enable IRQ 23 in TIMER 2_CTL_R = 0 x 00000001; // 10) enable timer 2 A } 13 -21 Output sound at 11. 025 k. Hz
Timer 2 A plays sounds // trigger is Timer 2 A Time-Out Interrupt // set periodically TATORIS set on rollover void Timer 2 A_Handler(void){ TIMER 2_ICR_R = 0 x 00000001; // acknowledge Timer. Count++; // run some background stuff here } TATORIS Output sounds here void Timer 2 A_Stop(void){ TIMER 2_CTL_R &= ~0 x 00000001; // disable } Ack Stuff Call to stop sound void Timer 2 A_Start(void){ TIMER 2_CTL_R |= 0 x 00000001; } // enable Call to start sound Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -22
Lab 10 – Connect Four There must be at least one button and one slide pot. The colored pieces must move on the LCD. There must be sounds appropriate for the game. The score should be displayed on the screen (but it could be displayed before or after the game action). q At least two interrupt ISRs must used in an appropriate manner. q The game must have a man versus machine mode. q q Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -23
Lab 10 – Space Invaders, Pipe Dreams … q There must be at least one button and one slide pot. q There must be at least three images on the LCD display that move. q There must be sounds appropriate for the game. q The score should be displayed on the screen (but it could be displayed before or after the game action). q At least two interrupt ISRs must used in an appropriate manner. q The game must have a “time” aspect to it (For e. g. , if you don’t move a sprite within a certain time it could be killed). Contrast with Connect. Four which is a taking “turns” game q The game must be both simple to learn and fun to play. http: //youtu. be/Qx. DQUUDSt. Ow Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -24
Lab 10 – Grading q The TAs will sort into groups and certify requirements v Show game to TA by Tuesday 12/1 by 7 pm v Wonderful group will have max of 100 points v Supreme group will have a max of 120 points q Lab 10 will be graded subjectively by other students v v v During class on Wednesday/Thursday 12 -2/12 -3 One team member demonstrates The other team member scores other games Can’t compete unless both members are present Groups of one are checked out by the TA (Max Score 80) q Games are rank-ordered by peers Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -25
Lab 10 – Grading q Wonderful group v 80 if 0 th to 49 th percentile v 90 if 50 th to 74 th percentile v 100 if 75 th to 100 th percentile q Supreme v 100 if 0 th to 49 th percentile v 110 if 50 th to 74 th percentile v 120 if 75 th to 100 th percentile q TA certification is due 7 pm Tuesday 5/3/2016 q Late checkouts are handled by the TA in the usual way. All late checkouts must be completed by Friday 3 pm. Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -26
Game Engine – Call graph Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -27
Game Engine – Data Flow Bard, Tiwari, Janapa Reddi, Gerstlauer, Valvano, Erez, Telang, Yerraballi 13 -28
Game Engine – Flowchart Could use GPIO interrupts Bard, Gerstlauer, Valvano, Erez, Telang, Yerraballi Do not erase the LCD, it causes flicker 13 -29
- Slides: 29