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

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