]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/STLcd/lcd_scan.c
Adding basic remote capabilities + UART Rx DMA buffers
[kiibohd-controller.git] / Scan / STLcd / lcd_scan.c
1 /* Copyright (C) 2015 by Jacob Alexander
2  *
3  * This file is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This file is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/ScanLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <kll_defs.h>
25 #include <led.h>
26 #include <print.h>
27
28 // Interconnect module if compiled in
29 #if defined(ConnectEnabled_define)
30 #include <connect_scan.h>
31 #endif
32
33 // Local Includes
34 #include "lcd_scan.h"
35
36
37
38 // ----- Defines -----
39
40 #define LCD_TOTAL_VISIBLE_PAGES 4
41 #define LCD_TOTAL_PAGES 9
42 #define LCD_PAGE_LEN 128
43
44
45
46 // ----- Macros -----
47
48 // Number of entries in the SPI0 TxFIFO
49 #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
50
51
52
53 // ----- Structs -----
54
55 // ----- Function Declarations -----
56
57 // CLI Functions
58 void cliFunc_lcdCmd  ( char* args );
59 void cliFunc_lcdColor( char* args );
60 void cliFunc_lcdDisp ( char* args );
61 void cliFunc_lcdInit ( char* args );
62 void cliFunc_lcdTest ( char* args );
63
64
65
66 // ----- Variables -----
67
68 // Default Image - Displays on startup
69 const uint8_t STLcdDefaultImage[] = { STLcdDefaultImage_define };
70
71 // Full Toggle State
72 uint8_t cliFullToggleState = 0;
73
74 // Normal/Reverse Toggle State
75 uint8_t cliNormalReverseToggleState = 0;
76
77 // Scan Module command dictionary
78 CLIDict_Entry( lcdCmd,      "Send byte via SPI, second argument enables a0. Defaults to control." );
79 CLIDict_Entry( lcdColor,    "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
80 CLIDict_Entry( lcdDisp,     "Write byte(s) to given page starting at given address. i.e. 0x1 0x5 0xFF 0x00" );
81 CLIDict_Entry( lcdInit,     "Re-initialize the LCD display." );
82 CLIDict_Entry( lcdTest,     "Test out the LCD display." );
83
84 CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
85         CLIDict_Item( lcdCmd ),
86         CLIDict_Item( lcdColor ),
87         CLIDict_Item( lcdDisp ),
88         CLIDict_Item( lcdInit ),
89         CLIDict_Item( lcdTest ),
90         { 0, 0, 0 } // Null entry for dictionary end
91 };
92
93
94
95 // ----- Interrupt Functions -----
96
97
98
99 // ----- Functions -----
100
101 inline void SPI_setup()
102 {
103         // Enable SPI internal clock
104         SIM_SCGC6 |= SIM_SCGC6_SPI0;
105
106         // Setup MOSI (SOUT) and SCLK (SCK)
107         PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
108         PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
109
110         // Setup SS (PCS)
111         PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
112
113         // Master Mode, CS0
114         SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
115
116         // DSPI Clock and Transfer Attributes
117         // Frame Size: 8 bits
118         // MSB First
119         // CLK Low by default
120         SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
121                 | SPI_CTAR_ASC(7)
122                 | SPI_CTAR_DT(7)
123                 | SPI_CTAR_CSSCK(7)
124                 | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
125 }
126
127 // Write buffer to SPI FIFO
128 void SPI_write( uint8_t *buffer, uint8_t len )
129 {
130
131         for ( uint8_t byte = 0; byte < len; byte++ )
132         {
133                 // Wait for SPI TxFIFO to have 4 or fewer entries
134                 while ( !( SPI0_SR & SPI_SR_TFFF ) )
135                         delayMicroseconds(10);
136
137                 // Write byte to TxFIFO
138                 // CS0, CTAR0
139                 SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
140
141                 // Indicate transfer has completed
142                 while ( !( SPI0_SR & SPI_SR_TCF ) );
143                 SPI0_SR |= SPI_SR_TCF;
144         }
145 }
146
147 // Write to a control register
148 void LCD_writeControlReg( uint8_t byte )
149 {
150         // Wait for TxFIFO to be empt
151         while ( SPI0_TxFIFO_CNT != 0 );
152
153         // Set A0 low to enter control register mode
154         GPIOC_PCOR |= (1<<7);
155
156         // Write byte to SPI FIFO
157         SPI_write( &byte, 1 );
158
159         // Wait for TxFIFO to be empty
160         while ( SPI0_TxFIFO_CNT != 0 );
161
162         // Make sure data has transferred
163         delayMicroseconds(10); // XXX Adjust if SPI speed changes
164
165         // Set A0 high to go back to display register mode
166         GPIOC_PSOR |= (1<<7);
167 }
168
169 // Write to display register
170 // Pages 0-7 normal display
171 // Page  8   icon buffer
172 void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
173 {
174         // Set the register page
175         LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
176
177         // Set display start line
178         LCD_writeControlReg( 0x40 );
179
180         // Reset Column Address
181         LCD_writeControlReg( 0x10 );
182         LCD_writeControlReg( 0x00 );
183
184         // Write buffer to SPI
185         SPI_write( buffer, len );
186 }
187
188 inline void LCD_clearPage( uint8_t page )
189 {
190         // Set the register page
191         LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
192
193         // Set display start line
194         LCD_writeControlReg( 0x40 );
195
196         // Reset Column Address
197         LCD_writeControlReg( 0x10 );
198         LCD_writeControlReg( 0x00 );
199
200         for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
201         {
202                 uint8_t byte = 0;
203
204                 // Write buffer to SPI
205                 SPI_write( &byte, 1 );
206         }
207
208         // Wait for TxFIFO to be empty
209         while ( SPI0_TxFIFO_CNT != 0 );
210 }
211
212 // Clear Display
213 void LCD_clear()
214 {
215         // Setup each page
216         for ( uint8_t page = 0; page < LCD_TOTAL_PAGES; page++ )
217         {
218                 LCD_clearPage( page );
219         }
220
221         // Reset Page, Start Line, and Column Address
222         // Page
223         LCD_writeControlReg( 0xB0 );
224
225         // Start Line
226         LCD_writeControlReg( 0x40 );
227
228         // Reset Column Address
229         LCD_writeControlReg( 0x10 );
230         LCD_writeControlReg( 0x00 );
231 }
232
233 // Intialize display
234 void LCD_initialize()
235 {
236         // ADC Select (Normal)
237         LCD_writeControlReg( 0xA0 );
238
239         // LCD Off
240         LCD_writeControlReg( 0xAE );
241
242         // COM Scan Output Direction
243         LCD_writeControlReg( 0xC0 );
244
245         // LCD Bias (1/6 bias)
246         LCD_writeControlReg( 0xA2 );
247
248         // Power Supply Operating Mode (Internal Only)
249         LCD_writeControlReg( 0x2F );
250
251         // Internal Rb/Ra Ratio
252         LCD_writeControlReg( 0x26 );
253
254         // Reset
255         LCD_writeControlReg( 0xE2 );
256
257         // Electric volume mode set, and value
258         LCD_writeControlReg( 0x81 );
259         LCD_writeControlReg( 0x00 );
260
261         // LCD On
262         LCD_writeControlReg( 0xAF );
263
264         // Clear Display RAM
265         LCD_clear();
266 }
267
268 // Setup
269 inline void LCD_setup()
270 {
271         // Register Scan CLI dictionary
272         CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
273
274         // Initialize SPI
275         SPI_setup();
276
277         // Setup Register Control Signal (A0)
278         // Start in display register mode (1)
279         GPIOC_PDDR |= (1<<7);
280         PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
281         GPIOC_PSOR |= (1<<7);
282
283         // Setup LCD Reset pin (RST)
284         // 0 - Reset, 1 - Normal Operation
285         // Start in normal mode (1)
286         GPIOC_PDDR |= (1<<8);
287         PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
288         GPIOC_PSOR |= (1<<8);
289
290         // Run LCD intialization sequence
291         LCD_initialize();
292
293         // Write default image to LCD
294         for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
295                 LCD_writeDisplayReg( page, (uint8_t*)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
296
297         // Setup Backlight
298         SIM_SCGC6 |= SIM_SCGC6_FTM0;
299         FTM0_CNT = 0; // Reset counter
300
301         // PWM Period
302         // 16-bit maximum
303         FTM0_MOD = 0xFFFF;
304
305         // Set FTM to PWM output - Edge Aligned, Low-true pulses
306         FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
307         FTM0_C1SC = 0x24;
308         FTM0_C2SC = 0x24;
309
310         // Base FTM clock selection (72 MHz system clock)
311         // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
312         // Higher pre-scalar will use the most power (also look the best)
313         // Pre-scalar calculations
314         // 0 -      72 MHz -> 549 Hz
315         // 1 -      36 MHz -> 275 Hz
316         // 2 -      18 MHz -> 137 Hz
317         // 3 -       9 MHz ->  69 Hz (Slightly visible flicker)
318         // 4 -   4 500 kHz ->  34 Hz (Visible flickering)
319         // 5 -   2 250 kHz ->  17 Hz
320         // 6 -   1 125 kHz ->   9 Hz
321         // 7 - 562 500  Hz ->   4 Hz
322         // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
323         // Which will reduce the brightness range
324
325         // System clock, /w prescalar setting
326         FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
327
328         // Red
329         FTM0_C0V = STLcdBacklightRed_define;
330         PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
331
332         // Green
333         FTM0_C1V = STLcdBacklightGreen_define;
334         PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
335
336         // Blue
337         FTM0_C2V = STLcdBacklightBlue_define;
338         PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
339 }
340
341
342 // LCD State processing loop
343 inline uint8_t LCD_scan()
344 {
345         return 0;
346 }
347
348
349
350 // ----- Capabilities -----
351
352 // Takes 1 8 bit length and 4 16 bit arguments, each corresponding to a layer index
353 // Ordered from top to bottom
354 // The first argument indicates how many numbers to display (max 4), set to 0 to load default image
355 uint16_t LCD_layerStackExact[4];
356 uint8_t LCD_layerStackExact_size = 0;
357 typedef struct LCD_layerStackExact_args {
358         uint8_t numArgs;
359         uint16_t layers[4];
360 } LCD_layerStackExact_args;
361 void LCD_layerStackExact_capability( uint8_t state, uint8_t stateType, uint8_t *args )
362 {
363         // Display capability name
364         if ( stateType == 0xFF && state == 0xFF )
365         {
366                 print("LCD_layerStackExact_capability(num,layer1,layer2,layer3,layer4)");
367                 return;
368         }
369
370         // Read arguments
371         LCD_layerStackExact_args *stack_args = (LCD_layerStackExact_args*)args;
372
373         // Number data for LCD
374         const uint8_t numbers[10][128] = {
375                 { STLcdNumber0_define },
376                 { STLcdNumber1_define },
377                 { STLcdNumber2_define },
378                 { STLcdNumber3_define },
379                 { STLcdNumber4_define },
380                 { STLcdNumber5_define },
381                 { STLcdNumber6_define },
382                 { STLcdNumber7_define },
383                 { STLcdNumber8_define },
384                 { STLcdNumber9_define },
385         };
386
387         // Color data for numbers
388         const uint16_t colors[10][3] = {
389                 { STLcdNumber0Color_define },
390                 { STLcdNumber1Color_define },
391                 { STLcdNumber2Color_define },
392                 { STLcdNumber3Color_define },
393                 { STLcdNumber4Color_define },
394                 { STLcdNumber5Color_define },
395                 { STLcdNumber6Color_define },
396                 { STLcdNumber7Color_define },
397                 { STLcdNumber8Color_define },
398                 { STLcdNumber9Color_define },
399         };
400
401         // Only display if there are layers active
402         if ( stack_args->numArgs > 0 )
403         {
404                 // Set the color according to the "top-of-stack" layer
405                 uint16_t layerIndex = stack_args->layers[0];
406                 FTM0_C0V = colors[ layerIndex ][0];
407                 FTM0_C1V = colors[ layerIndex ][1];
408                 FTM0_C2V = colors[ layerIndex ][2];
409
410                 // Iterate through each of the pages
411                 // XXX Many of the values here are hard-coded
412                 //     Eventually a proper font rendering engine should take care of things like this... -HaaTa
413                 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
414                 {
415                         // Set the register page
416                         LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
417
418                         // Set starting address
419                         LCD_writeControlReg( 0x10 );
420                         LCD_writeControlReg( 0x00 );
421
422                         // Write data
423                         for ( uint16_t layer = 0; layer < stack_args->numArgs; layer++ )
424                         {
425                                 layerIndex = stack_args->layers[ layer ];
426
427                                 // Default to 0, if over 9
428                                 if ( layerIndex > 9 )
429                                 {
430                                         layerIndex = 0;
431                                 }
432
433                                 // Write page of number to display
434                                 SPI_write( (uint8_t*)&numbers[ layerIndex ][ page * 32 ], 32 );
435                         }
436
437                         // Blank out rest of display
438                         uint8_t data = 0;
439                         for ( uint8_t c = 0; c < 4 - stack_args->numArgs; c++ )
440                         {
441                                 for ( uint8_t byte = 0; byte < 32; byte++ )
442                                 {
443                                         SPI_write( &data, 1 );
444                                 }
445                         }
446                 }
447         }
448         else
449         {
450                 // Set default backlight
451                 FTM0_C0V = STLcdBacklightRed_define;
452                 FTM0_C1V = STLcdBacklightGreen_define;
453                 FTM0_C2V = STLcdBacklightBlue_define;
454
455                 // Write default image
456                 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
457                         LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
458         }
459 }
460
461 // Determines the current layer stack, and sets the LCD output accordingly
462 // Will only work on a master node when using the interconnect (use LCD_layerStackExact_capability instead)
463 uint16_t LCD_layerStack_prevSize = 0;
464 uint16_t LCD_layerStack_prevTop  = 0;
465 void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args )
466 {
467         // Display capability name
468         if ( stateType == 0xFF && state == 0xFF )
469         {
470                 print("LCD_layerStack_capability()");
471                 return;
472         }
473
474         // Parse the layer stack, top to bottom
475         extern uint16_t macroLayerIndexStack[];
476         extern uint16_t macroLayerIndexStackSize;
477
478         // Ignore if the stack size hasn't changed and the top of the stack is the same
479         if ( macroLayerIndexStackSize == LCD_layerStack_prevSize
480                 && macroLayerIndexStack[macroLayerIndexStackSize - 1] == LCD_layerStack_prevTop )
481         {
482                 return;
483         }
484         LCD_layerStack_prevSize = macroLayerIndexStackSize;
485         LCD_layerStack_prevTop  = macroLayerIndexStack[macroLayerIndexStackSize - 1];
486
487         LCD_layerStackExact_args stack_args;
488         memset( stack_args.layers, 0, sizeof( stack_args.layers ) );
489
490         // Use the LCD_layerStackExact_capability to set the LCD using the determined stack
491         // Construct argument set for capability
492         stack_args.numArgs = macroLayerIndexStackSize;
493         for ( uint16_t layer = 1; layer <= macroLayerIndexStackSize; layer++ )
494         {
495                 stack_args.layers[ layer - 1 ] = macroLayerIndexStack[ macroLayerIndexStackSize - layer ];
496         }
497
498         // Only deal with the interconnect if it has been compiled in
499 #if defined(ConnectEnabled_define)
500         if ( Connect_master )
501         {
502                 // generatedKeymap.h
503                 extern const Capability CapabilitiesList[];
504
505                 // Broadcast layerStackExact remote capability (0xFF is the broadcast id)
506                 Connect_send_RemoteCapability(
507                         0xFF,
508                         LCD_layerStackExact_capability_index,
509                         state,
510                         stateType,
511                         CapabilitiesList[ LCD_layerStackExact_capability_index ].argCount,
512                         (uint8_t*)&stack_args
513                 );
514         }
515 #endif
516         // Call LCD_layerStackExact directly
517         LCD_layerStackExact_capability( state, stateType, (uint8_t*)&stack_args );
518 }
519
520
521
522 // ----- CLI Command Functions -----
523
524 void cliFunc_lcdInit( char* args )
525 {
526         LCD_initialize();
527 }
528
529 void cliFunc_lcdTest( char* args )
530 {
531         // Write default image
532         for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
533                 LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
534 }
535
536 void cliFunc_lcdCmd( char* args )
537 {
538         char* curArgs;
539         char* arg1Ptr;
540         char* arg2Ptr = args;
541
542         print( NL ); // No \r\n by default after the command is entered
543
544         curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
545         CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
546
547         // No args
548         if ( *arg1Ptr == '\0' )
549                 return;
550
551         // SPI Command
552         uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
553
554         curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
555         CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
556
557         // Single Arg
558         if ( *arg1Ptr == '\0' )
559                 goto cmd;
560
561         // TODO Deal with a0
562 cmd:
563         info_msg("Sending - ");
564         printHex( cmd );
565         print( NL );
566         LCD_writeControlReg( cmd );
567 }
568
569 void cliFunc_lcdColor( char* args )
570 {
571         char* curArgs;
572         char* arg1Ptr;
573         char* arg2Ptr = args;
574
575         // Colors
576         uint16_t rgb[3]; // Red, Green, Blue
577
578         // Parse integers from 3 arguments
579         for ( uint8_t color = 0; color < 3; color++ )
580         {
581                 curArgs = arg2Ptr;
582                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
583
584                 // Give up if not enough args given
585                 if ( *arg1Ptr == '\0' )
586                         return;
587
588                 // Convert argument to integer
589                 rgb[ color ] = numToInt( arg1Ptr );
590         }
591
592         // Set PWM channels
593         FTM0_C0V = rgb[0];
594         FTM0_C1V = rgb[1];
595         FTM0_C2V = rgb[2];
596 }
597
598 void cliFunc_lcdDisp( char* args )
599 {
600         char* curArgs;
601         char* arg1Ptr;
602         char* arg2Ptr = args;
603
604         // First process page and starting address
605         curArgs = arg2Ptr;
606         CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
607
608         // Stop processing args if no more are found
609         if ( *arg1Ptr == '\0' )
610                 return;
611         uint8_t page = numToInt( arg1Ptr );
612
613         curArgs = arg2Ptr;
614         CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
615
616         // Stop processing args if no more are found
617         if ( *arg1Ptr == '\0' )
618                 return;
619         uint8_t address = numToInt( arg1Ptr );
620
621         // Set the register page
622         LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
623
624         // Set starting address
625         LCD_writeControlReg( 0x10 | ( ( 0xF0 & address ) >> 4 ) );
626         LCD_writeControlReg( 0x00 | ( 0x0F & address ));
627
628         // Process all args
629         for ( ;; )
630         {
631                 curArgs = arg2Ptr;
632                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
633
634                 // Stop processing args if no more are found
635                 if ( *arg1Ptr == '\0' )
636                         break;
637
638                 uint8_t value = numToInt( arg1Ptr );
639
640                 // Write buffer to SPI
641                 SPI_write( &value, 1 );
642         }
643 }
644