+/rtos

git clone https://git.julia.blue/+/rtos

Vivek MalneediMerge branch 'master' of github.com:EE445M-Sp20/lab-6-vm22967_jbr2558021d2d4

master
17.4 KiB587 linesraw
1#include "ST7735.h"
2#include "OS.h"
3#include "eDisk.h"
4#include "font.h"
5#include "std.h"
6#include "timer.h"
7#include "tivaware/gpio.h"
8#include "tivaware/rom.h"
9#include <stdint.h>
10
11// 16 rows (0 to 15) and 21 characters (0 to 20)
12// Requires (11 + size*size*6*8) bytes of transmission for each character
13static uint8_t cursor_x = 0; // position along the horizonal axis 0 to 20
14static uint8_t cursor_y = 0; // position along the vertical axis 0 to 15
15uint16_t text_color = LCD_CYAN;
16
17#define ST7735_NOP 0x00
18#define ST7735_SWRESET 0x01
19#define ST7735_RDDID 0x04
20#define ST7735_RDDST 0x09
21
22#define ST7735_SLPIN 0x10
23#define ST7735_SLPOUT 0x11
24#define ST7735_PTLON 0x12
25#define ST7735_NORON 0x13
26
27#define ST7735_INVOFF 0x20
28#define ST7735_INVON 0x21
29#define ST7735_DISPOFF 0x28
30#define ST7735_DISPON 0x29
31#define ST7735_CASET 0x2A
32#define ST7735_RASET 0x2B
33#define ST7735_RAMWR 0x2C
34#define ST7735_RAMRD 0x2E
35
36#define ST7735_PTLAR 0x30
37#define ST7735_COLMOD 0x3A
38#define ST7735_MADCTL 0x36
39
40#define ST7735_FRMCTR1 0xB1
41#define ST7735_FRMCTR2 0xB2
42#define ST7735_FRMCTR3 0xB3
43#define ST7735_INVCTR 0xB4
44#define ST7735_DISSET5 0xB6
45
46#define ST7735_PWCTR1 0xC0
47#define ST7735_PWCTR2 0xC1
48#define ST7735_PWCTR3 0xC2
49#define ST7735_PWCTR4 0xC3
50#define ST7735_PWCTR5 0xC4
51#define ST7735_VMCTR1 0xC5
52
53#define ST7735_RDID1 0xDA
54#define ST7735_RDID2 0xDB
55#define ST7735_RDID3 0xDC
56#define ST7735_RDID4 0xDD
57
58#define ST7735_PWCTR6 0xFC
59
60#define ST7735_GMCTRP1 0xE0
61#define ST7735_GMCTRN1 0xE1
62
63#define ST7735_WIDTH 128
64#define ST7735_HEIGHT 160
65
66static void select(void) {
67    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_PIN_0);
68    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0);
69}
70
71static void deselect(void) {
72    while (ROM_SSIBusy(SSI0_BASE)) {}
73    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
74}
75
76// The Data/Command pin must be valid when the eighth bit is
77// sent.  The SSI module has hardware input and output FIFOs
78// that are 8 locations deep.  Based on the observation that
79// the LCD interface tends to send a few commands and then a
80// lot of data, the FIFOs are not used when writing
81// commands, and they are used when writing data.  This
82// ensures that the Data/Command pin status matches the byte
83// that is actually being transmitted.
84// The write command operation waits until all data has been
85// sent, configures the Data/Command pin for commands, sends
86// the command, and then waits for the transmission to
87// finish.
88// The write data operation waits until there is room in the
89// transmit FIFO, configures the Data/Command pin for data,
90// and then adds the data to the transmit FIFO.
91// NOTE: These functions will crash or stall indefinitely if
92// the SSI0 module is not initialized and enabled.
93static void write_command(uint8_t c) {
94    uint32_t _response;
95    while (ROM_SSIBusy(SSI0_BASE)) {}
96    select();
97    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0); // command mode
98    ROM_SSIDataPut(SSI0_BASE, c);
99    ROM_SSIDataGet(SSI0_BASE, &_response);
100    deselect();
101}
102
103static void write_data(uint8_t c) {
104    uint32_t _response;
105    while (ROM_SSIBusy(SSI0_BASE)) {}
106    select();
107    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6); // data mode
108    ROM_SSIDataPut(SSI0_BASE, c);
109    ROM_SSIDataGet(SSI0_BASE, &_response);
110    deselect();
111}
112
113// Rather than a bazillion write_command() and write_data() calls, screen
114// initialization commands and arguments are organized in these tables
115// stored in ROM.  The table may look bulky, but that's mostly the
116// formatting -- storage-wise this is hundreds of bytes more compact
117// than the equivalent code.  Companion function follows.
118#define DELAY 0x80
119static const uint8_t Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
120    15,                          // 15 commands in list:
121    ST7735_SWRESET,
122    DELAY, //  1: Software reset, 0 args, w/delay
123    150,   //     150 ms delay
124    ST7735_SLPOUT,
125    DELAY, //  2: Out of sleep mode, 0 args, w/delay
126    255,   //     500 ms delay
127    ST7735_FRMCTR1,
128    3, //  3: Frame rate ctrl - normal mode, 3 args:
129    0x01,
130    0x2C,
131    0x2D, //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
132    ST7735_FRMCTR2,
133    3, //  4: Frame rate control - idle mode, 3 args:
134    0x01,
135    0x2C,
136    0x2D, //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
137    ST7735_FRMCTR3,
138    6, //  5: Frame rate ctrl - partial mode, 6 args:
139    0x01,
140    0x2C,
141    0x2D, //     Dot inversion mode
142    0x01,
143    0x2C,
144    0x2D, //     Line inversion mode
145    ST7735_INVCTR,
146    1,    //  6: Display inversion ctrl, 1 arg, no delay:
147    0x07, //     No inversion
148    ST7735_PWCTR1,
149    3, //  7: Power control, 3 args, no delay:
150    0xA2,
151    0x02, //     -4.6V
152    0x84, //     AUTO mode
153    ST7735_PWCTR2,
154    1,    //  8: Power control, 1 arg, no delay:
155    0xC5, //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
156    ST7735_PWCTR3,
157    2,    //  9: Power control, 2 args, no delay:
158    0x0A, //     Opamp current small
159    0x00, //     Boost frequency
160    ST7735_PWCTR4,
161    2,    // 10: Power control, 2 args, no delay:
162    0x8A, //     BCLK/2, Opamp current small & Medium low
163    0x2A,
164    ST7735_PWCTR5,
165    2, // 11: Power control, 2 args, no delay:
166    0x8A,
167    0xEE,
168    ST7735_VMCTR1,
169    1, // 12: Power control, 1 arg, no delay:
170    0x0E,
171    ST7735_INVOFF,
172    0, // 13: Don't invert display, no args, no delay
173    ST7735_MADCTL,
174    1,    // 14: Memory access control (directions), 1 arg:
175    0xC8, //     row addr/col addr, bottom to top refresh
176    ST7735_COLMOD,
177    1,                              // 15: set color mode, 1 arg, no delay:
178    0x05};                          //     16-bit color
179static const uint8_t Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
180    2,                              //  2 commands in list:
181    ST7735_CASET,
182    4, //  1: Column addr set, 4 args, no delay:
183    0x00,
184    0x00, //     XSTART = 0
185    0x00,
186    0x7F, //     XEND = 127
187    ST7735_RASET,
188    4, //  2: Row addr set, 4 args, no delay:
189    0x00,
190    0x00, //     XSTART = 0
191    0x00,
192    0x9F};                       //     XEND = 159
193static const uint8_t Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
194    4,                           //  4 commands in list:
195    ST7735_GMCTRP1,
196    16, //  1: Magical unicorn dust, 16 args, no delay:
197    0x02,
198    0x1c,
199    0x07,
200    0x12,
201    0x37,
202    0x32,
203    0x29,
204    0x2d,
205    0x29,
206    0x25,
207    0x2B,
208    0x39,
209    0x00,
210    0x01,
211    0x03,
212    0x10,
213    ST7735_GMCTRN1,
214    16, //  2: Sparkles and rainbows, 16 args, no delay:
215    0x03,
216    0x1d,
217    0x07,
218    0x06,
219    0x2E,
220    0x2C,
221    0x29,
222    0x2D,
223    0x2E,
224    0x2E,
225    0x37,
226    0x3F,
227    0x00,
228    0x00,
229    0x02,
230    0x10,
231    ST7735_NORON,
232    DELAY, //  3: Normal display on, no args, w/delay
233    10,    //     10 ms delay
234    ST7735_DISPON,
235    DELAY, //  4: Main screen turn on, no args w/delay
236    100};  //     100 ms delay
237
238// Companion code to the above tables.  Reads and issues
239// a series of LCD commands stored in ROM byte array.
240static void command_list(const uint8_t* addr) {
241    uint8_t numCommands, numArgs;
242    uint16_t wait;
243
244    numCommands = *(addr++);       // Number of commands to follow
245    while (numCommands--) {        // For each command...
246        write_command(*(addr++));  //   Read, issue command
247        numArgs = *(addr++);       //   Number of args to follow
248        wait = numArgs & DELAY;    //   If hibit set, delay follows args
249        numArgs &= ~DELAY;         //   Mask out delay bit
250        while (numArgs--) {        //   For each argument...
251            write_data(*(addr++)); //     Read, issue argument
252        }
253
254        if (wait) {
255            wait = *(addr++); // Read post-command delay time (ms)
256            if (wait == 255)
257                wait = 500; // If 255, delay for 500 ms
258            busy_wait(3, ms(wait));
259        }
260    }
261}
262
263Sema4 LCDFree;
264
265void lcd_init() {
266    SSI0_Init(10);
267    select();
268    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_PIN_7); // reset high
269    busy_wait(3, ms(500));
270    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0); // reset low
271    busy_wait(3, ms(500));
272    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_PIN_7); // reset high
273    busy_wait(3, ms(500));
274
275    command_list(Rcmd1);
276    command_list(Rcmd2red);
277    command_list(Rcmd3);
278
279    lcd_set_cursor(0, 0);
280    text_color = LCD_YELLOW;
281    lcd_fill(LCD_BLACK);
282    OS_InitSemaphore(&LCDFree, 0);
283}
284
285// Set the region of the screen RAM to be modified
286// Pixel colors are sent left to right, top to bottom
287// (same as Font table is encoded; different from regular bitmap)
288// Requires 11 bytes of transmission
289static void set_address_window(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
290    write_command(ST7735_CASET); // Column addr set
291    write_data(0x00);
292    write_data(x0); // XSTART
293    write_data(0x00);
294    write_data(x1); // XEND
295
296    write_command(ST7735_RASET); // Row addr set
297    write_data(0x00);
298    write_data(y0); // YSTART
299    write_data(0x00);
300    write_data(y1); // YEND
301
302    write_command(ST7735_RAMWR); // write to RAM
303}
304
305// Send two bytes of data, most significant byte first
306static void push_color(Color color) {
307    write_data((color >> 8));
308    write_data(color);
309}
310
311void lcd_pixel(int16_t x, int16_t y, Color color) {
312    if ((x < 0) || (x >= ST7735_WIDTH) || (y < 0) || (y >= ST7735_HEIGHT))
313        return;
314    set_address_window(x, y, x, y);
315    push_color(color);
316}
317
318void lcd_fast_vline(int16_t x, int16_t y, int16_t h, Color color) {
319    uint8_t hi = color >> 8, lo = color;
320
321    // Rudimentary clipping
322    if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
323        return;
324    if ((y + h - 1) >= ST7735_HEIGHT)
325        h = ST7735_HEIGHT - y;
326    set_address_window(x, y, x, y + h - 1);
327
328    while (h--) {
329        write_data(hi);
330        write_data(lo);
331    }
332}
333
334void lcd_fast_hline(int16_t x, int16_t y, int16_t w, Color color) {
335    uint8_t hi = color >> 8, lo = color;
336
337    // Rudimentary clipping
338    if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
339        return;
340    if ((x + w - 1) >= ST7735_WIDTH)
341        w = ST7735_WIDTH - x;
342    set_address_window(x, y, x + w - 1, y);
343
344    while (w--) {
345        write_data(hi);
346        write_data(lo);
347    }
348}
349
350void lcd_fill(Color color) {
351    lcd_rect(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
352    //  screen is actually 129 by 161 pixels, x 0 to 128, y goes from 0 to 160
353}
354
355void lcd_rect(int16_t x, int16_t y, int16_t w, int16_t h, Color color) {
356    uint8_t hi = color >> 8, lo = color;
357
358    // rudimentary clipping (drawChar w/big text requires this)
359    if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
360        return;
361    if ((x + w - 1) >= ST7735_WIDTH)
362        w = ST7735_WIDTH - x;
363    if ((y + h - 1) >= ST7735_HEIGHT)
364        h = ST7735_HEIGHT - y;
365
366    set_address_window(x, y, x + w - 1, y + h - 1);
367
368    for (y = h; y > 0; y--) {
369        for (x = w; x > 0; x--) {
370            write_data(hi);
371            write_data(lo);
372        }
373    }
374}
375
376Color rgb_packed(uint32_t packed_color) {
377    return ((packed_color << 8) & ~0xF800) | ((packed_color >> 3) & ~0x07E0) |
378           (packed_color >> 19);
379}
380
381Color rgb(uint8_t r, uint8_t g, uint8_t b) {
382    return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
383}
384
385void lcd_bitmap(int16_t x, int16_t y, const uint16_t* image, int16_t w,
386                int16_t h) {
387    int16_t skipC = 0; // non-zero if columns need to be skipped due to clipping
388    int16_t originalWidth =
389        w; // save this value; even if not all columns fit on the screen, the
390           // image is still this width in ROM
391    int i = w * (h - 1);
392
393    if ((x >= ST7735_WIDTH) || ((y - h + 1) >= ST7735_HEIGHT) ||
394        ((x + w) <= 0) || (y < 0)) {
395        return; // image is totally off the screen, do nothing
396    }
397    if ((w > ST7735_WIDTH) ||
398        (h > ST7735_HEIGHT)) { // image is too wide for the screen, do nothing
399        // This isn't necessarily a fatal error, but it makes the
400        // following logic much more complicated, since you can have
401        // an image that exceeds multiple boundaries and needs to be
402        // clipped on more than one side.
403        return;
404    }
405    if ((x + w - 1) >= ST7735_WIDTH) {  // image exceeds right of screen
406        skipC = (x + w) - ST7735_WIDTH; // skip cut off columns
407        w = ST7735_WIDTH - x;
408    }
409    if ((y - h + 1) < 0) {                   // image exceeds top of screen
410        i = i - (h - y - 1) * originalWidth; // skip the last cut off rows
411        h = y + 1;
412    }
413    if (x < 0) { // image exceeds left of screen
414        w = w + x;
415        skipC = -x; // skip cut off columns
416        i = i - x;  // skip the first cut off columns
417        x = 0;
418    }
419    if (y >= ST7735_HEIGHT) { // image exceeds bottom of screen
420        h = h - (y - ST7735_HEIGHT + 1);
421        y = ST7735_HEIGHT - 1;
422    }
423
424    set_address_window(x, y - h + 1, x + w - 1, y);
425
426    for (y = 0; y < h; y = y + 1) {
427        for (x = 0; x < w; x = x + 1) {
428            // send the top 8 bits
429            write_data((uint8_t)(image[i] >> 8));
430            // send the bottom 8 bits
431            write_data((uint8_t)image[i]);
432            i = i + 1; // go to the next pixel
433        }
434        i = i + skipC;
435        i = i - 2 * originalWidth;
436    }
437}
438
439void lcd_char_slow(int16_t x, int16_t y, char c, int16_t textColor,
440                   int16_t bgColor, uint8_t size) {
441    uint8_t line; // vertical column of pixels of character in font
442    int32_t i, j;
443    if ((x >= ST7735_WIDTH) ||      // Clip right
444        (y >= ST7735_HEIGHT) ||     // Clip bottom
445        ((x + 6 * size - 1) < 0) || // Clip left
446        ((y + 8 * size - 1) < 0))   // Clip top
447        return;
448
449    for (i = 0; i < 6; i++) {
450        if (i == 5)
451            line = 0x0;
452        else
453            line = Font[(c * 5) + i];
454        for (j = 0; j < 8; j++) {
455            if (line & 0x1) {
456                if (size == 1) // default size
457                    lcd_pixel(x + i, y + j, textColor);
458                else { // big size
459                    lcd_rect(x + (i * size), y + (j * size), size, size,
460                             textColor);
461                }
462            } else if (bgColor != textColor) {
463                if (size == 1) // default size
464                    lcd_pixel(x + i, y + j, bgColor);
465                else { // big size
466                    lcd_rect(x + i * size, y + j * size, size, size, bgColor);
467                }
468            }
469            line >>= 1;
470        }
471    }
472}
473
474void lcd_char(int16_t x, int16_t y, char c, int16_t textColor, int16_t bgColor,
475              uint8_t size) {
476    uint8_t line;           // horizontal row of pixels of character
477    int32_t col, row, i, j; // loop indices
478    if (((x + 6 * size - 1) >= ST7735_WIDTH) ||  // Clip right
479        ((y + 8 * size - 1) >= ST7735_HEIGHT) || // Clip bottom
480        ((x + 6 * size - 1) < 0) ||              // Clip left
481        ((y + 8 * size - 1) < 0)) {              // Clip top
482        return;
483    }
484
485    set_address_window(x, y, x + 6 * size - 1, y + 8 * size - 1);
486
487    line = 0x01; // print the top row first
488    // print the rows, starting at the top
489    for (row = 0; row < 8; row = row + 1) {
490        for (i = 0; i < size; i = i + 1) {
491            // print the columns, starting on the left
492            for (col = 0; col < 5; col = col + 1) {
493                if (Font[(c * 5) + col] & line) {
494                    // bit is set in Font, print pixel(s) in text color
495                    for (j = 0; j < size; j = j + 1) { push_color(textColor); }
496                } else {
497                    // bit is cleared in Font, print pixel(s) in background
498                    // color
499                    for (j = 0; j < size; j = j + 1) { push_color(bgColor); }
500                }
501            }
502            // print blank column(s) to the right of character
503            for (j = 0; j < size; j = j + 1) { push_color(bgColor); }
504        }
505        line = line << 1; // move up to the next row
506    }
507}
508
509uint32_t lcd_string(uint16_t x, uint16_t y, char* pt, int16_t textColor) {
510    uint32_t count = 0;
511    if (y > 15)
512        return 0;
513    while (*pt) {
514        lcd_char(x * 6, y * 10, *pt, textColor, LCD_BLACK, 1);
515        pt++;
516        x = x + 1;
517        if (x > 20)
518            return count; // number of characters printed
519        count++;
520    }
521    return count; // number of characters printed
522}
523
524void lcd_set_cursor(uint32_t newX, uint32_t newY) {
525    if ((newX > 20) || (newY > 15)) { // bad input
526        return;                       // do nothing
527    }
528    cursor_x = newX;
529    cursor_y = newY;
530}
531
532void lcd_message_num(uint32_t d, uint32_t l, char* pt, int32_t value) {
533    OS_Wait(&LCDFree);
534    lcd_message(d, l, pt);
535    if (value < 0) {
536        lcd_putchar('-');
537        lcd_num(-value);
538    } else {
539        lcd_num(value);
540    }
541    OS_Signal(&LCDFree);
542}
543
544void lcd_num(uint32_t n) {
545    char buf[12];
546    lcd_puts(itoa(n, buf, 10));
547}
548
549void lcd_message(uint32_t d, uint32_t l, char* pt) {
550    lcd_set_cursor(0, 8 * d + l);
551    lcd_puts(pt);
552}
553
554void lcd_invert(bool i) {
555    if (i) {
556        write_command(ST7735_INVON);
557    } else {
558        write_command(ST7735_INVOFF);
559    }
560}
561
562void lcd_putchar(char ch) {
563    if ((ch == 10) || (ch == 13) || (ch == 27)) {
564        cursor_y++;
565        cursor_x = 0;
566        if (cursor_y > 15) {
567            cursor_y = 0;
568        }
569        lcd_string(0, cursor_y, "                     ", text_color);
570        return;
571    }
572    lcd_char(cursor_x * 6, cursor_y * 10, ch, LCD_YELLOW, LCD_BLACK, 1);
573    cursor_x++;
574    if (cursor_x > 20) {
575        cursor_x = 20;
576        lcd_char(cursor_x * 6, cursor_y * 10, '*', LCD_RED, LCD_BLACK, 1);
577    }
578    return;
579}
580
581void lcd_puts(const char* s) {
582    while (*s) { lcd_putchar(*s), ++s; }
583}
584
585void lcd_set_text_color(Color color) {
586    text_color = color;
587}