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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. // ----- CLI Command Functions -----
  257. void cliFunc_lcdInit( char* args )
  258. {
  259. LCD_initialize();
  260. }
  261. void cliFunc_lcdTest( char* args )
  262. {
  263. // Write default image
  264. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  265. LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
  266. }
  267. void cliFunc_lcdCmd( char* args )
  268. {
  269. char* curArgs;
  270. char* arg1Ptr;
  271. char* arg2Ptr = args;
  272. print( NL ); // No \r\n by default after the command is entered
  273. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  274. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  275. // No args
  276. if ( *arg1Ptr == '\0' )
  277. return;
  278. // SPI Command
  279. uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
  280. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  281. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  282. // Single Arg
  283. if ( *arg1Ptr == '\0' )
  284. goto cmd;
  285. // TODO Deal with a0
  286. cmd:
  287. info_msg("Sending - ");
  288. printHex( cmd );
  289. print( NL );
  290. LCD_writeControlReg( cmd );
  291. }
  292. void cliFunc_lcdColor( char* args )
  293. {
  294. char* curArgs;
  295. char* arg1Ptr;
  296. char* arg2Ptr = args;
  297. // Colors
  298. uint16_t rgb[3]; // Red, Green, Blue
  299. // Parse integers from 3 arguments
  300. for ( uint8_t color = 0; color < 3; color++ )
  301. {
  302. curArgs = arg2Ptr;
  303. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  304. // Give up if not enough args given
  305. if ( *arg1Ptr == '\0' )
  306. return;
  307. // Convert argument to integer
  308. rgb[ color ] = numToInt( arg1Ptr );
  309. }
  310. // Set PWM channels
  311. FTM0_C0V = rgb[0];
  312. FTM0_C1V = rgb[1];
  313. FTM0_C2V = rgb[2];
  314. }
  315. void cliFunc_lcdDisp( char* args )
  316. {
  317. char* curArgs;
  318. char* arg1Ptr;
  319. char* arg2Ptr = args;
  320. // First process page and starting address
  321. curArgs = arg2Ptr;
  322. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  323. // Stop processing args if no more are found
  324. if ( *arg1Ptr == '\0' )
  325. return;
  326. uint8_t page = numToInt( arg1Ptr );
  327. curArgs = arg2Ptr;
  328. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  329. // Stop processing args if no more are found
  330. if ( *arg1Ptr == '\0' )
  331. return;
  332. uint8_t address = numToInt( arg1Ptr );
  333. // Set the register page
  334. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  335. // Set starting address
  336. LCD_writeControlReg( 0x10 | ( ( 0xF0 & address ) >> 4 ) );
  337. LCD_writeControlReg( 0x00 | ( 0x0F & address ));
  338. // Process all args
  339. for ( ;; )
  340. {
  341. curArgs = arg2Ptr;
  342. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  343. // Stop processing args if no more are found
  344. if ( *arg1Ptr == '\0' )
  345. break;
  346. uint8_t value = numToInt( arg1Ptr );
  347. // Write buffer to SPI
  348. SPI_write( &value, 1 );
  349. }
  350. }