Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

lcd_scan.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/ScanLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <kll_defs.h>
  22. #include <led.h>
  23. #include <print.h>
  24. // Local Includes
  25. #include "lcd_scan.h"
  26. // ----- Defines -----
  27. #define LCD_TOTAL_VISIBLE_PAGES 4
  28. #define LCD_TOTAL_PAGES 9
  29. #define LCD_PAGE_LEN 128
  30. // ----- Macros -----
  31. // Number of entries in the SPI0 TxFIFO
  32. #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
  33. // ----- Structs -----
  34. // ----- Function Declarations -----
  35. // CLI Functions
  36. void cliFunc_lcdCmd ( char* args );
  37. void cliFunc_lcdColor( char* args );
  38. void cliFunc_lcdDisp ( char* args );
  39. void cliFunc_lcdInit ( char* args );
  40. void cliFunc_lcdTest ( char* args );
  41. // ----- Variables -----
  42. // Default Image - Displays on startup
  43. const uint8_t STLcdDefaultImage[] = { STLcdDefaultImage_define };
  44. // Full Toggle State
  45. uint8_t cliFullToggleState = 0;
  46. // Normal/Reverse Toggle State
  47. uint8_t cliNormalReverseToggleState = 0;
  48. // Scan Module command dictionary
  49. CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
  50. CLIDict_Entry( lcdColor, "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
  51. CLIDict_Entry( lcdDisp, "Write byte(s) to given page starting at given address. i.e. 0x1 0x5 0xFF 0x00" );
  52. CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
  53. CLIDict_Entry( lcdTest, "Test out the LCD display." );
  54. CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
  55. CLIDict_Item( lcdCmd ),
  56. CLIDict_Item( lcdColor ),
  57. CLIDict_Item( lcdDisp ),
  58. CLIDict_Item( lcdInit ),
  59. CLIDict_Item( lcdTest ),
  60. { 0, 0, 0 } // Null entry for dictionary end
  61. };
  62. // ----- Interrupt Functions -----
  63. // ----- Functions -----
  64. inline void SPI_setup()
  65. {
  66. // Enable SPI internal clock
  67. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  68. // Setup MOSI (SOUT) and SCLK (SCK)
  69. PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  70. PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  71. // Setup SS (PCS)
  72. PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  73. // Master Mode, CS0
  74. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
  75. // DSPI Clock and Transfer Attributes
  76. // Frame Size: 8 bits
  77. // MSB First
  78. // CLK Low by default
  79. SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
  80. | SPI_CTAR_ASC(7)
  81. | SPI_CTAR_DT(7)
  82. | SPI_CTAR_CSSCK(7)
  83. | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
  84. }
  85. // Write buffer to SPI FIFO
  86. void SPI_write( uint8_t *buffer, uint8_t len )
  87. {
  88. for ( uint8_t byte = 0; byte < len; byte++ )
  89. {
  90. // Wait for SPI TxFIFO to have 4 or fewer entries
  91. while ( !( SPI0_SR & SPI_SR_TFFF ) )
  92. delayMicroseconds(10);
  93. // Write byte to TxFIFO
  94. // CS0, CTAR0
  95. SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
  96. // Indicate transfer has completed
  97. while ( !( SPI0_SR & SPI_SR_TCF ) );
  98. SPI0_SR |= SPI_SR_TCF;
  99. }
  100. }
  101. // Write to a control register
  102. void LCD_writeControlReg( uint8_t byte )
  103. {
  104. // Wait for TxFIFO to be empt
  105. while ( SPI0_TxFIFO_CNT != 0 );
  106. // Set A0 low to enter control register mode
  107. GPIOC_PCOR |= (1<<7);
  108. // Write byte to SPI FIFO
  109. SPI_write( &byte, 1 );
  110. // Wait for TxFIFO to be empty
  111. while ( SPI0_TxFIFO_CNT != 0 );
  112. // Make sure data has transferred
  113. delayMicroseconds(10); // XXX Adjust if SPI speed changes
  114. // Set A0 high to go back to display register mode
  115. GPIOC_PSOR |= (1<<7);
  116. }
  117. // Write to display register
  118. // Pages 0-7 normal display
  119. // Page 8 icon buffer
  120. void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
  121. {
  122. // Set the register page
  123. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  124. // Set display start line
  125. LCD_writeControlReg( 0x40 );
  126. // Reset Column Address
  127. LCD_writeControlReg( 0x10 );
  128. LCD_writeControlReg( 0x00 );
  129. // Write buffer to SPI
  130. SPI_write( buffer, len );
  131. }
  132. inline void LCD_clearPage( uint8_t page )
  133. {
  134. // Set the register page
  135. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  136. // Set display start line
  137. LCD_writeControlReg( 0x40 );
  138. // Reset Column Address
  139. LCD_writeControlReg( 0x10 );
  140. LCD_writeControlReg( 0x00 );
  141. for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
  142. {
  143. uint8_t byte = 0;
  144. // Write buffer to SPI
  145. SPI_write( &byte, 1 );
  146. }
  147. // Wait for TxFIFO to be empty
  148. while ( SPI0_TxFIFO_CNT != 0 );
  149. }
  150. // Clear Display
  151. void LCD_clear()
  152. {
  153. // Setup each page
  154. for ( uint8_t page = 0; page < LCD_TOTAL_PAGES; page++ )
  155. {
  156. LCD_clearPage( page );
  157. }
  158. // Reset Page, Start Line, and Column Address
  159. // Page
  160. LCD_writeControlReg( 0xB0 );
  161. // Start Line
  162. LCD_writeControlReg( 0x40 );
  163. // Reset Column Address
  164. LCD_writeControlReg( 0x10 );
  165. LCD_writeControlReg( 0x00 );
  166. }
  167. // Intialize display
  168. void LCD_initialize()
  169. {
  170. // ADC Select (Normal)
  171. LCD_writeControlReg( 0xA0 );
  172. // LCD Off
  173. LCD_writeControlReg( 0xAE );
  174. // COM Scan Output Direction
  175. LCD_writeControlReg( 0xC0 );
  176. // LCD Bias (1/6 bias)
  177. LCD_writeControlReg( 0xA2 );
  178. // Power Supply Operating Mode (Internal Only)
  179. LCD_writeControlReg( 0x2F );
  180. // Internal Rb/Ra Ratio
  181. LCD_writeControlReg( 0x26 );
  182. // Reset
  183. LCD_writeControlReg( 0xE2 );
  184. // Electric volume mode set, and value
  185. LCD_writeControlReg( 0x81 );
  186. LCD_writeControlReg( 0x00 );
  187. // LCD On
  188. LCD_writeControlReg( 0xAF );
  189. // Clear Display RAM
  190. LCD_clear();
  191. }
  192. // Setup
  193. inline void LCD_setup()
  194. {
  195. // Register Scan CLI dictionary
  196. CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
  197. // Initialize SPI
  198. SPI_setup();
  199. // Setup Register Control Signal (A0)
  200. // Start in display register mode (1)
  201. GPIOC_PDDR |= (1<<7);
  202. PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  203. GPIOC_PSOR |= (1<<7);
  204. // Setup LCD Reset pin (RST)
  205. // 0 - Reset, 1 - Normal Operation
  206. // Start in normal mode (1)
  207. GPIOC_PDDR |= (1<<8);
  208. PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  209. GPIOC_PSOR |= (1<<8);
  210. // Run LCD intialization sequence
  211. LCD_initialize();
  212. // Write default image to LCD
  213. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  214. LCD_writeDisplayReg( page, (uint8_t*)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
  215. // Setup Backlight
  216. SIM_SCGC6 |= SIM_SCGC6_FTM0;
  217. FTM0_CNT = 0; // Reset counter
  218. // PWM Period
  219. // 16-bit maximum
  220. FTM0_MOD = 0xFFFF;
  221. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  222. FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
  223. FTM0_C1SC = 0x24;
  224. FTM0_C2SC = 0x24;
  225. // Base FTM clock selection (72 MHz system clock)
  226. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  227. // Higher pre-scalar will use the most power (also look the best)
  228. // Pre-scalar calculations
  229. // 0 - 72 MHz -> 549 Hz
  230. // 1 - 36 MHz -> 275 Hz
  231. // 2 - 18 MHz -> 137 Hz
  232. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  233. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  234. // 5 - 2 250 kHz -> 17 Hz
  235. // 6 - 1 125 kHz -> 9 Hz
  236. // 7 - 562 500 Hz -> 4 Hz
  237. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  238. // Which will reduce the brightness range
  239. // System clock, /w prescalar setting
  240. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
  241. // Red
  242. FTM0_C0V = STLcdBacklightRed_define;
  243. PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  244. // Green
  245. FTM0_C1V = STLcdBacklightGreen_define;
  246. PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  247. // Blue
  248. FTM0_C2V = STLcdBacklightBlue_define;
  249. PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  250. }
  251. // LCD State processing loop
  252. inline uint8_t LCD_scan()
  253. {
  254. return 0;
  255. }
  256. // ----- Capabilities -----
  257. uint16_t LCD_layerStack_prevSize = 0;
  258. void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  259. {
  260. // Display capability name
  261. if ( stateType == 0xFF && state == 0xFF )
  262. {
  263. print("LCD_layerStack_capability");
  264. return;
  265. }
  266. // Parse the layer stack, top to bottom
  267. extern uint16_t macroLayerIndexStack[];
  268. extern uint16_t macroLayerIndexStackSize;
  269. // Only process if the stack size has changed
  270. if ( macroLayerIndexStackSize == LCD_layerStack_prevSize )
  271. {
  272. return;
  273. }
  274. LCD_layerStack_prevSize = macroLayerIndexStackSize;
  275. // Number data for LCD
  276. const uint8_t numbers[10][128] = {
  277. { STLcdNumber0_define },
  278. { STLcdNumber1_define },
  279. { STLcdNumber2_define },
  280. { STLcdNumber3_define },
  281. { STLcdNumber4_define },
  282. { STLcdNumber5_define },
  283. { STLcdNumber6_define },
  284. { STLcdNumber7_define },
  285. { STLcdNumber8_define },
  286. { STLcdNumber9_define },
  287. };
  288. // Only display if there are layers active
  289. if ( macroLayerIndexStackSize > 0 )
  290. {
  291. // Iterate through each of the pages
  292. // XXX Many of the values here are hard-coded
  293. // Eventually a proper font rendering engine should take care of things like this... -HaaTa
  294. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  295. {
  296. // Set the register page
  297. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  298. // Set starting address
  299. LCD_writeControlReg( 0x10 );
  300. LCD_writeControlReg( 0x00 );
  301. // Write data
  302. for ( uint16_t layer = 1; layer <= macroLayerIndexStackSize; layer++ )
  303. {
  304. uint16_t layerIndex = macroLayerIndexStack[ macroLayerIndexStackSize - layer ];
  305. // Default to 0, if over 9
  306. if ( layerIndex > 9 )
  307. {
  308. layerIndex = 0;
  309. }
  310. // Write page of number to display
  311. SPI_write( (uint8_t*)&numbers[ layerIndex ][ page * 32 ], 32 );
  312. }
  313. // Blank out rest of display
  314. uint8_t data = 0;
  315. for ( uint8_t c = 0; c < 4 - macroLayerIndexStackSize; c++ )
  316. {
  317. for ( uint8_t byte = 0; byte < 32; byte++ )
  318. {
  319. SPI_write( &data, 1 );
  320. }
  321. }
  322. }
  323. }
  324. else
  325. {
  326. // Write default image
  327. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  328. LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
  329. }
  330. }
  331. // ----- CLI Command Functions -----
  332. void cliFunc_lcdInit( char* args )
  333. {
  334. LCD_initialize();
  335. }
  336. void cliFunc_lcdTest( char* args )
  337. {
  338. // Write default image
  339. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  340. LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
  341. }
  342. void cliFunc_lcdCmd( char* args )
  343. {
  344. char* curArgs;
  345. char* arg1Ptr;
  346. char* arg2Ptr = args;
  347. print( NL ); // No \r\n by default after the command is entered
  348. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  349. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  350. // No args
  351. if ( *arg1Ptr == '\0' )
  352. return;
  353. // SPI Command
  354. uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
  355. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  356. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  357. // Single Arg
  358. if ( *arg1Ptr == '\0' )
  359. goto cmd;
  360. // TODO Deal with a0
  361. cmd:
  362. info_msg("Sending - ");
  363. printHex( cmd );
  364. print( NL );
  365. LCD_writeControlReg( cmd );
  366. }
  367. void cliFunc_lcdColor( char* args )
  368. {
  369. char* curArgs;
  370. char* arg1Ptr;
  371. char* arg2Ptr = args;
  372. // Colors
  373. uint16_t rgb[3]; // Red, Green, Blue
  374. // Parse integers from 3 arguments
  375. for ( uint8_t color = 0; color < 3; color++ )
  376. {
  377. curArgs = arg2Ptr;
  378. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  379. // Give up if not enough args given
  380. if ( *arg1Ptr == '\0' )
  381. return;
  382. // Convert argument to integer
  383. rgb[ color ] = numToInt( arg1Ptr );
  384. }
  385. // Set PWM channels
  386. FTM0_C0V = rgb[0];
  387. FTM0_C1V = rgb[1];
  388. FTM0_C2V = rgb[2];
  389. }
  390. void cliFunc_lcdDisp( char* args )
  391. {
  392. char* curArgs;
  393. char* arg1Ptr;
  394. char* arg2Ptr = args;
  395. // First process page and starting address
  396. curArgs = arg2Ptr;
  397. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  398. // Stop processing args if no more are found
  399. if ( *arg1Ptr == '\0' )
  400. return;
  401. uint8_t page = numToInt( arg1Ptr );
  402. curArgs = arg2Ptr;
  403. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  404. // Stop processing args if no more are found
  405. if ( *arg1Ptr == '\0' )
  406. return;
  407. uint8_t address = numToInt( arg1Ptr );
  408. // Set the register page
  409. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  410. // Set starting address
  411. LCD_writeControlReg( 0x10 | ( ( 0xF0 & address ) >> 4 ) );
  412. LCD_writeControlReg( 0x00 | ( 0x0F & address ));
  413. // Process all args
  414. for ( ;; )
  415. {
  416. curArgs = arg2Ptr;
  417. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  418. // Stop processing args if no more are found
  419. if ( *arg1Ptr == '\0' )
  420. break;
  421. uint8_t value = numToInt( arg1Ptr );
  422. // Write buffer to SPI
  423. SPI_write( &value, 1 );
  424. }
  425. }