+/rtos
git clone https://git.julia.blue/+/rtos
021d2d4
master
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 ) { 67ROM_GPIOPinWrite (GPIO_PORTB_BASE ,GPIO_PIN_0 ,GPIO_PIN_0 ); 68ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_3 ,0 ); 69} 70 71static void deselect (void ) { 72while (ROM_SSIBusy (SSI0_BASE )) {} 73ROM_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 ) { 94uint32_t _response ; 95while (ROM_SSIBusy (SSI0_BASE )) {} 96select (); 97ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_6 ,0 );// command mode 98ROM_SSIDataPut (SSI0_BASE ,c ); 99ROM_SSIDataGet (SSI0_BASE ,& _response ); 100deselect (); 101} 102 103static void write_data (uint8_t c ) { 104uint32_t _response ; 105while (ROM_SSIBusy (SSI0_BASE )) {} 106select (); 107ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_6 ,GPIO_PIN_6 );// data mode 108ROM_SSIDataPut (SSI0_BASE ,c ); 109ROM_SSIDataGet (SSI0_BASE ,& _response ); 110deselect (); 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) 12015 ,// 15 commands in list: 121ST7735_SWRESET , 122DELAY ,// 1: Software reset, 0 args, w/delay 123150 ,// 150 ms delay 124ST7735_SLPOUT , 125DELAY ,// 2: Out of sleep mode, 0 args, w/delay 126255 ,// 500 ms delay 127ST7735_FRMCTR1 , 1283 ,// 3: Frame rate ctrl - normal mode, 3 args: 1290x01 , 1300x2C , 1310x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 132ST7735_FRMCTR2 , 1333 ,// 4: Frame rate control - idle mode, 3 args: 1340x01 , 1350x2C , 1360x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 137ST7735_FRMCTR3 , 1386 ,// 5: Frame rate ctrl - partial mode, 6 args: 1390x01 , 1400x2C , 1410x2D ,// Dot inversion mode 1420x01 , 1430x2C , 1440x2D ,// Line inversion mode 145ST7735_INVCTR , 1461 ,// 6: Display inversion ctrl, 1 arg, no delay: 1470x07 ,// No inversion 148ST7735_PWCTR1 , 1493 ,// 7: Power control, 3 args, no delay: 1500xA2 , 1510x02 ,// -4.6V 1520x84 ,// AUTO mode 153ST7735_PWCTR2 , 1541 ,// 8: Power control, 1 arg, no delay: 1550xC5 ,// VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD 156ST7735_PWCTR3 , 1572 ,// 9: Power control, 2 args, no delay: 1580x0A ,// Opamp current small 1590x00 ,// Boost frequency 160ST7735_PWCTR4 , 1612 ,// 10: Power control, 2 args, no delay: 1620x8A ,// BCLK/2, Opamp current small & Medium low 1630x2A , 164ST7735_PWCTR5 , 1652 ,// 11: Power control, 2 args, no delay: 1660x8A , 1670xEE , 168ST7735_VMCTR1 , 1691 ,// 12: Power control, 1 arg, no delay: 1700x0E , 171ST7735_INVOFF , 1720 ,// 13: Don't invert display, no args, no delay 173ST7735_MADCTL , 1741 ,// 14: Memory access control (directions), 1 arg: 1750xC8 ,// row addr/col addr, bottom to top refresh 176ST7735_COLMOD , 1771 ,// 15: set color mode, 1 arg, no delay: 1780x05 };// 16-bit color 179static const uint8_t Rcmd2red []= {// Init for 7735R, part 2 (red tab only) 1802 ,// 2 commands in list: 181ST7735_CASET , 1824 ,// 1: Column addr set, 4 args, no delay: 1830x00 , 1840x00 ,// XSTART = 0 1850x00 , 1860x7F ,// XEND = 127 187ST7735_RASET , 1884 ,// 2: Row addr set, 4 args, no delay: 1890x00 , 1900x00 ,// XSTART = 0 1910x00 , 1920x9F };// XEND = 159 193static const uint8_t Rcmd3 []= {// Init for 7735R, part 3 (red or green tab) 1944 ,// 4 commands in list: 195ST7735_GMCTRP1 , 19616 ,// 1: Magical unicorn dust, 16 args, no delay: 1970x02 , 1980x1c , 1990x07 , 2000x12 , 2010x37 , 2020x32 , 2030x29 , 2040x2d , 2050x29 , 2060x25 , 2070x2B , 2080x39 , 2090x00 , 2100x01 , 2110x03 , 2120x10 , 213ST7735_GMCTRN1 , 21416 ,// 2: Sparkles and rainbows, 16 args, no delay: 2150x03 , 2160x1d , 2170x07 , 2180x06 , 2190x2E , 2200x2C , 2210x29 , 2220x2D , 2230x2E , 2240x2E , 2250x37 , 2260x3F , 2270x00 , 2280x00 , 2290x02 , 2300x10 , 231ST7735_NORON , 232DELAY ,// 3: Normal display on, no args, w/delay 23310 ,// 10 ms delay 234ST7735_DISPON , 235DELAY ,// 4: Main screen turn on, no args w/delay 236100 };// 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 ) { 241uint8_t numCommands ,numArgs ; 242uint16_t wait ; 243 244numCommands = * (addr ++ );// Number of commands to follow 245while (numCommands -- ) {// For each command... 246write_command (* (addr ++ ));// Read, issue command 247numArgs = * (addr ++ );// Number of args to follow 248wait = numArgs & DELAY ;// If hibit set, delay follows args 249numArgs &= ~DELAY ;// Mask out delay bit 250while (numArgs -- ) {// For each argument... 251write_data (* (addr ++ ));// Read, issue argument 252 } 253 254if (wait ) { 255wait = * (addr ++ );// Read post-command delay time (ms) 256if (wait == 255 ) 257wait = 500 ;// If 255, delay for 500 ms 258busy_wait (3 ,ms (wait )); 259 } 260 } 261} 262 263Sema4 LCDFree ; 264 265void lcd_init () { 266SSI0_Init (10 ); 267select (); 268ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_7 ,GPIO_PIN_7 );// reset high 269busy_wait (3 ,ms (500 )); 270ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_7 ,0 );// reset low 271busy_wait (3 ,ms (500 )); 272ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_7 ,GPIO_PIN_7 );// reset high 273busy_wait (3 ,ms (500 )); 274 275command_list (Rcmd1 ); 276command_list (Rcmd2red ); 277command_list (Rcmd3 ); 278 279lcd_set_cursor (0 ,0 ); 280text_color = LCD_YELLOW ; 281lcd_fill (LCD_BLACK ); 282OS_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 ) { 290write_command (ST7735_CASET );// Column addr set 291write_data (0x00 ); 292write_data (x0 );// XSTART 293write_data (0x00 ); 294write_data (x1 );// XEND 295 296write_command (ST7735_RASET );// Row addr set 297write_data (0x00 ); 298write_data (y0 );// YSTART 299write_data (0x00 ); 300write_data (y1 );// YEND 301 302write_command (ST7735_RAMWR );// write to RAM 303} 304 305// Send two bytes of data, most significant byte first 306static void push_color (Color color ) { 307write_data ((color >>8 )); 308write_data (color ); 309} 310 311void lcd_pixel (int16_t x ,int16_t y ,Color color ) { 312if ((x < 0 )|| (x >=ST7735_WIDTH )|| (y < 0 )|| (y >=ST7735_HEIGHT )) 313return ; 314set_address_window (x ,y ,x ,y ); 315push_color (color ); 316} 317 318void lcd_fast_vline (int16_t x ,int16_t y ,int16_t h ,Color color ) { 319uint8_t hi = color >>8 ,lo = color ; 320 321// Rudimentary clipping 322if ((x >=ST7735_WIDTH )|| (y >=ST7735_HEIGHT )) 323return ; 324if ((y + h - 1 ) >=ST7735_HEIGHT ) 325h = ST7735_HEIGHT - y ; 326set_address_window (x ,y ,x ,y + h - 1 ); 327 328while (h -- ) { 329write_data (hi ); 330write_data (lo ); 331 } 332} 333 334void lcd_fast_hline (int16_t x ,int16_t y ,int16_t w ,Color color ) { 335uint8_t hi = color >>8 ,lo = color ; 336 337// Rudimentary clipping 338if ((x >=ST7735_WIDTH )|| (y >=ST7735_HEIGHT )) 339return ; 340if ((x + w - 1 ) >=ST7735_WIDTH ) 341w = ST7735_WIDTH - x ; 342set_address_window (x ,y ,x + w - 1 ,y ); 343 344while (w -- ) { 345write_data (hi ); 346write_data (lo ); 347 } 348} 349 350void lcd_fill (Color color ) { 351lcd_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 ) { 356uint8_t hi = color >>8 ,lo = color ; 357 358// rudimentary clipping (drawChar w/big text requires this) 359if ((x >=ST7735_WIDTH )|| (y >=ST7735_HEIGHT )) 360return ; 361if ((x + w - 1 ) >=ST7735_WIDTH ) 362w = ST7735_WIDTH - x ; 363if ((y + h - 1 ) >=ST7735_HEIGHT ) 364h = ST7735_HEIGHT - y ; 365 366set_address_window (x ,y ,x + w - 1 ,y + h - 1 ); 367 368for (y = h ;y > 0 ;y -- ) { 369for (x = w ;x > 0 ;x -- ) { 370write_data (hi ); 371write_data (lo ); 372 } 373 } 374} 375 376Color rgb_packed (uint32_t packed_color ) { 377return ((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 ) { 382return ((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 , 386int16_t h ) { 387int16_t skipC = 0 ;// non-zero if columns need to be skipped due to clipping 388int16_t originalWidth = 389w ;// save this value; even if not all columns fit on the screen, the 390// image is still this width in ROM 391int i = w * (h - 1 ); 392 393if ((x >=ST7735_WIDTH )|| ((y - h + 1 ) >=ST7735_HEIGHT )|| 394 ((x + w ) <=0 )|| (y < 0 )) { 395return ;// image is totally off the screen, do nothing 396 } 397if ((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. 403return ; 404 } 405if ((x + w - 1 ) >=ST7735_WIDTH ) {// image exceeds right of screen 406skipC = (x + w )- ST7735_WIDTH ;// skip cut off columns 407w = ST7735_WIDTH - x ; 408 } 409if ((y - h + 1 )< 0 ) {// image exceeds top of screen 410i = i - (h - y - 1 )* originalWidth ;// skip the last cut off rows 411h = y + 1 ; 412 } 413if (x < 0 ) {// image exceeds left of screen 414w = w + x ; 415skipC = - x ;// skip cut off columns 416i = i - x ;// skip the first cut off columns 417x = 0 ; 418 } 419if (y >=ST7735_HEIGHT ) {// image exceeds bottom of screen 420h = h - (y - ST7735_HEIGHT + 1 ); 421y = ST7735_HEIGHT - 1 ; 422 } 423 424set_address_window (x ,y - h + 1 ,x + w - 1 ,y ); 425 426for (y = 0 ;y < h ;y = y + 1 ) { 427for (x = 0 ;x < w ;x = x + 1 ) { 428// send the top 8 bits 429write_data ((uint8_t )(image [i ] >>8 )); 430// send the bottom 8 bits 431write_data ((uint8_t )image [i ]); 432i = i + 1 ;// go to the next pixel 433 } 434i = i + skipC ; 435i = i - 2 * originalWidth ; 436 } 437} 438 439void lcd_char_slow (int16_t x ,int16_t y ,char c ,int16_t textColor , 440int16_t bgColor ,uint8_t size ) { 441uint8_t line ;// vertical column of pixels of character in font 442int32_t i ,j ; 443if ((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 447return ; 448 449for (i = 0 ;i < 6 ;i ++ ) { 450if (i == 5 ) 451line = 0x0 ; 452else 453line = Font [(c * 5 )+ i ]; 454for (j = 0 ;j < 8 ;j ++ ) { 455if (line & 0x1 ) { 456if (size == 1 )// default size 457lcd_pixel (x + i ,y + j ,textColor ); 458else {// big size 459lcd_rect (x + (i * size ),y + (j * size ),size ,size , 460textColor ); 461 } 462 }else if (bgColor != textColor ) { 463if (size == 1 )// default size 464lcd_pixel (x + i ,y + j ,bgColor ); 465else {// big size 466lcd_rect (x + i * size ,y + j * size ,size ,size ,bgColor ); 467 } 468 } 469line >>=1 ; 470 } 471 } 472} 473 474void lcd_char (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor , 475uint8_t size ) { 476uint8_t line ;// horizontal row of pixels of character 477int32_t col ,row ,i ,j ;// loop indices 478if (((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 482return ; 483 } 484 485set_address_window (x ,y ,x + 6 * size - 1 ,y + 8 * size - 1 ); 486 487line = 0x01 ;// print the top row first 488// print the rows, starting at the top 489for (row = 0 ;row < 8 ;row = row + 1 ) { 490for (i = 0 ;i < size ;i = i + 1 ) { 491// print the columns, starting on the left 492for (col = 0 ;col < 5 ;col = col + 1 ) { 493if (Font [(c * 5 )+ col ]& line ) { 494// bit is set in Font, print pixel(s) in text color 495for (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 499for (j = 0 ;j < size ;j = j + 1 ) {push_color (bgColor ); } 500 } 501 } 502// print blank column(s) to the right of character 503for (j = 0 ;j < size ;j = j + 1 ) {push_color (bgColor ); } 504 } 505line = 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 ) { 510uint32_t count = 0 ; 511if (y > 15 ) 512return 0 ; 513while (* pt ) { 514lcd_char (x * 6 ,y * 10 ,* pt ,textColor ,LCD_BLACK ,1 ); 515pt ++ ; 516x = x + 1 ; 517if (x > 20 ) 518return count ;// number of characters printed 519count ++ ; 520 } 521return count ;// number of characters printed 522} 523 524void lcd_set_cursor (uint32_t newX ,uint32_t newY ) { 525if ((newX > 20 )|| (newY > 15 )) {// bad input 526return ;// do nothing 527 } 528cursor_x = newX ; 529cursor_y = newY ; 530} 531 532void lcd_message_num (uint32_t d ,uint32_t l ,char * pt ,int32_t value ) { 533OS_Wait (& LCDFree ); 534lcd_message (d ,l ,pt ); 535if (value < 0 ) { 536lcd_putchar ('-' ); 537lcd_num (- value ); 538 }else { 539lcd_num (value ); 540 } 541OS_Signal (& LCDFree ); 542} 543 544void lcd_num (uint32_t n ) { 545char buf [12 ]; 546lcd_puts (itoa (n ,buf ,10 )); 547} 548 549void lcd_message (uint32_t d ,uint32_t l ,char * pt ) { 550lcd_set_cursor (0 ,8 * d + l ); 551lcd_puts (pt ); 552} 553 554void lcd_invert (bool i ) { 555if (i ) { 556write_command (ST7735_INVON ); 557 }else { 558write_command (ST7735_INVOFF ); 559 } 560} 561 562void lcd_putchar (char ch ) { 563if ((ch == 10 )|| (ch == 13 )|| (ch == 27 )) { 564cursor_y ++ ; 565cursor_x = 0 ; 566if (cursor_y > 15 ) { 567cursor_y = 0 ; 568 } 569lcd_string (0 ,cursor_y ," " ,text_color ); 570return ; 571 } 572lcd_char (cursor_x * 6 ,cursor_y * 10 ,ch ,LCD_YELLOW ,LCD_BLACK ,1 ); 573cursor_x ++ ; 574if (cursor_x > 20 ) { 575cursor_x = 20 ; 576lcd_char (cursor_x * 6 ,cursor_y * 10 ,'*' ,LCD_RED ,LCD_BLACK ,1 ); 577 } 578return ; 579} 580 581void lcd_puts (const char * s ) { 582while (* s ) {lcd_putchar (* s ),++ s ; } 583} 584 585void lcd_set_text_color (Color color ) { 586text_color = color ; 587}