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

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