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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.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_PAGE_LEN 128
  29. // ----- Macros -----
  30. // Number of entries in the SPI0 TxFIFO
  31. #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
  32. // ----- Structs -----
  33. // ----- Function Declarations -----
  34. // CLI Functions
  35. void cliFunc_lcdCmd ( char* args );
  36. void cliFunc_lcdColor( char* args );
  37. void cliFunc_lcdInit ( char* args );
  38. void cliFunc_lcdTest ( char* args );
  39. // ----- Variables -----
  40. // Full Toggle State
  41. uint8_t cliFullToggleState = 0;
  42. // Normal/Reverse Toggle State
  43. uint8_t cliNormalReverseToggleState = 0;
  44. // Scan Module command dictionary
  45. CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
  46. CLIDict_Entry( lcdColor, "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
  47. CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
  48. CLIDict_Entry( lcdTest, "Test out the LCD display." );
  49. CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
  50. CLIDict_Item( lcdCmd ),
  51. CLIDict_Item( lcdColor ),
  52. CLIDict_Item( lcdInit ),
  53. CLIDict_Item( lcdTest ),
  54. { 0, 0, 0 } // Null entry for dictionary end
  55. };
  56. // ----- Interrupt Functions -----
  57. // ----- Functions -----
  58. inline void SPI_setup()
  59. {
  60. // Enable SPI internal clock
  61. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  62. // Setup MOSI (SOUT) and SCLK (SCK)
  63. PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  64. PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  65. // Setup SS (PCS)
  66. PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  67. // Master Mode, CS0
  68. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
  69. // DSPI Clock and Transfer Attributes
  70. // Frame Size: 8 bits
  71. // MSB First
  72. // CLK Low by default
  73. SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
  74. | SPI_CTAR_ASC(7)
  75. | SPI_CTAR_DT(7)
  76. | SPI_CTAR_CSSCK(7)
  77. | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
  78. }
  79. // Write buffer to SPI FIFO
  80. void SPI_write( uint8_t *buffer, uint8_t len )
  81. {
  82. for ( uint8_t byte = 0; byte < len; byte++ )
  83. {
  84. // Wait for SPI TxFIFO to have 4 or fewer entries
  85. while ( !( SPI0_SR & SPI_SR_TFFF ) )
  86. delayMicroseconds(10);
  87. // Write byte to TxFIFO
  88. // CS0, CTAR0
  89. SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
  90. // Indicate transfer has completed
  91. while ( !( SPI0_SR & SPI_SR_TCF ) );
  92. SPI0_SR |= SPI_SR_TCF;
  93. }
  94. }
  95. // Write to a control register
  96. void LCD_writeControlReg( uint8_t byte )
  97. {
  98. // Wait for TxFIFO to be empt
  99. while ( SPI0_TxFIFO_CNT != 0 );
  100. // Set A0 low to enter control register mode
  101. GPIOC_PCOR |= (1<<7);
  102. // Write byte to SPI FIFO
  103. SPI_write( &byte, 1 );
  104. // Wait for TxFIFO to be empty
  105. while ( SPI0_TxFIFO_CNT != 0 );
  106. // Make sure data has transferred
  107. delayMicroseconds(10); // XXX Adjust if SPI speed changes
  108. // Set A0 high to go back to display register mode
  109. GPIOC_PSOR |= (1<<7);
  110. }
  111. // Write to display register
  112. // Pages 0-7 normal display
  113. // Page 8 icon buffer
  114. void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
  115. {
  116. // Set the register page
  117. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  118. // Set display start line
  119. LCD_writeControlReg( 0x40 );
  120. // Reset Column Address
  121. LCD_writeControlReg( 0x10 );
  122. LCD_writeControlReg( 0x00 );
  123. // Write buffer to SPI
  124. SPI_write( buffer, len );
  125. }
  126. inline void LCD_clearPage( uint8_t page )
  127. {
  128. // Set the register page
  129. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  130. // Set display start line
  131. LCD_writeControlReg( 0x40 );
  132. // Reset Column Address
  133. LCD_writeControlReg( 0x10 );
  134. LCD_writeControlReg( 0x00 );
  135. for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
  136. {
  137. uint8_t byte = 0;
  138. // Write buffer to SPI
  139. SPI_write( &byte, 1 );
  140. }
  141. // Wait for TxFIFO to be empty
  142. while ( SPI0_TxFIFO_CNT != 0 );
  143. }
  144. // Clear Display
  145. void LCD_clear()
  146. {
  147. // Setup each page
  148. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  149. {
  150. LCD_clearPage( page );
  151. }
  152. // Reset Page, Start Line, and Column Address
  153. // Page
  154. LCD_writeControlReg( 0xB0 );
  155. // Start Line
  156. LCD_writeControlReg( 0x40 );
  157. // Reset Column Address
  158. LCD_writeControlReg( 0x10 );
  159. LCD_writeControlReg( 0x00 );
  160. }
  161. // Intialize display
  162. void LCD_initialize()
  163. {
  164. // ADC Select (Normal)
  165. LCD_writeControlReg( 0xA0 );
  166. // LCD Off
  167. LCD_writeControlReg( 0xAE );
  168. // COM Scan Output Direction
  169. LCD_writeControlReg( 0xC0 );
  170. // LCD Bias (1/6 bias)
  171. LCD_writeControlReg( 0xA2 );
  172. // Power Supply Operating Mode (Internal Only)
  173. LCD_writeControlReg( 0x2F );
  174. // Internal Rb/Ra Ratio
  175. LCD_writeControlReg( 0x26 );
  176. // Reset
  177. LCD_writeControlReg( 0xE2 );
  178. // Electric volume mode set, and value
  179. LCD_writeControlReg( 0x81 );
  180. LCD_writeControlReg( 0x00 );
  181. // LCD On
  182. LCD_writeControlReg( 0xAF );
  183. // Clear Display RAM
  184. LCD_clear();
  185. }
  186. // Setup
  187. inline void LCD_setup()
  188. {
  189. // Register Scan CLI dictionary
  190. CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
  191. // Initialize SPI
  192. SPI_setup();
  193. // Setup Register Control Signal (A0)
  194. // Start in display register mode (1)
  195. GPIOC_PDDR |= (1<<7);
  196. PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  197. GPIOC_PSOR |= (1<<7);
  198. // Setup LCD Reset pin (RST)
  199. // 0 - Reset, 1 - Normal Operation
  200. // Start in normal mode (1)
  201. GPIOC_PDDR |= (1<<8);
  202. PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  203. GPIOC_PSOR |= (1<<8);
  204. // Run LCD intialization sequence
  205. LCD_initialize();
  206. // Setup Backlight
  207. // TODO Expose default settings
  208. SIM_SCGC6 |= SIM_SCGC6_FTM0;
  209. FTM0_CNT = 0; // Reset counter
  210. // PWM Period
  211. // 16-bit maximum
  212. FTM0_MOD = 0xFFFF;
  213. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  214. FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
  215. FTM0_C1SC = 0x24;
  216. FTM0_C2SC = 0x24;
  217. // Base FTM clock selection (72 MHz system clock)
  218. // @ 0xFFFF period, 72 MHz / 0xFFFF * 2 = Actual period
  219. // Higher pre-scalar will use the most power (also look the best)
  220. // Pre-scalar calculations
  221. // 0 - 72 MHz -> 549 Hz
  222. // 1 - 36 MHz -> 275 Hz
  223. // 2 - 18 MHz -> 137 Hz
  224. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  225. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  226. // 5 - 2 250 kHz -> 17 Hz
  227. // 6 - 1 125 kHz -> 9 Hz
  228. // 7 - 562 500 Hz -> 4 Hz
  229. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  230. // Which will reduce the brightness range
  231. // System clock, /w prescalar setting
  232. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
  233. // Red
  234. FTM0_C0V = STLcdBacklightRed_define;
  235. PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  236. // Green
  237. FTM0_C1V = STLcdBacklightGreen_define;
  238. PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  239. // Blue
  240. FTM0_C2V = STLcdBacklightBlue_define;
  241. PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
  242. }
  243. // LCD State processing loop
  244. inline uint8_t LCD_scan()
  245. {
  246. // NOP - Screen Refresh
  247. //LCD_writeControlReg( 0xE3 );
  248. return 0;
  249. }
  250. // ----- CLI Command Functions -----
  251. void cliFunc_lcdInit( char* args )
  252. {
  253. print( NL ); // No \r\n by default after the command is entered
  254. LCD_initialize();
  255. }
  256. void cliFunc_lcdTest( char* args )
  257. {
  258. print( NL ); // No \r\n by default after the command is entered
  259. //LCD_initialize();
  260. // Test pattern
  261. uint8_t pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  262. uint8_t logo[] = {
  263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  264. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  265. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  266. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  267. };
  268. //uint8_t pattern[] = { 0xFF, 0x00, 0x96, 0xFF, 0x00, 0xFF, 0x00 };
  269. // Write to page D0
  270. //LCD_writeDisplayReg( 0, pattern, sizeof( pattern ) );
  271. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  272. {
  273. LCD_writeDisplayReg( page, &logo[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
  274. }
  275. }
  276. void cliFunc_lcdCmd( char* args )
  277. {
  278. char* curArgs;
  279. char* arg1Ptr;
  280. char* arg2Ptr = args;
  281. print( NL ); // No \r\n by default after the command is entered
  282. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  283. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  284. // No args
  285. if ( *arg1Ptr == '\0' )
  286. return;
  287. // SPI Command
  288. uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
  289. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  290. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  291. // Single Arg
  292. if ( *arg1Ptr == '\0' )
  293. goto cmd;
  294. // TODO Deal with a0
  295. cmd:
  296. info_msg("Sending - ");
  297. printHex( cmd );
  298. print( NL );
  299. LCD_writeControlReg( cmd );
  300. }
  301. void cliFunc_lcdColor( char* args )
  302. {
  303. char* curArgs;
  304. char* arg1Ptr;
  305. char* arg2Ptr = args;
  306. // Colors
  307. uint16_t rgb[3]; // Red, Green, Blue
  308. // Parse integers from 3 arguments
  309. for ( uint8_t color = 0; color < 3; color++ )
  310. {
  311. curArgs = arg2Ptr;
  312. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  313. // Give up if not enough args given
  314. if ( *arg1Ptr == '\0' )
  315. return;
  316. // Convert argument to integer
  317. rgb[ color ] = numToInt( arg1Ptr );
  318. }
  319. // Set PWM channels
  320. FTM0_C0V = rgb[0];
  321. FTM0_C1V = rgb[1];
  322. FTM0_C2V = rgb[2];
  323. print( NL ); // No \r\n by default after the command is entered
  324. }