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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <led.h>
  22. #include <print.h>
  23. // Local Includes
  24. #include "lcd_scan.h"
  25. // ----- Defines -----
  26. #define LCD_TOTAL_VISIBLE_PAGES 4
  27. #define LCD_PAGE_LEN 132
  28. // ----- Macros -----
  29. // Number of entries in the SPI0 TxFIFO
  30. #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
  31. // ----- Structs -----
  32. // ----- Function Declarations -----
  33. // CLI Functions
  34. void cliFunc_lcdCmd( char* args );
  35. void cliFunc_lcdInit( char* args );
  36. void cliFunc_lcdTest( char* args );
  37. // ----- Variables -----
  38. // Full Toggle State
  39. uint8_t cliFullToggleState = 0;
  40. // Normal/Reverse Toggle State
  41. uint8_t cliNormalReverseToggleState = 0;
  42. // Scan Module command dictionary
  43. CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
  44. CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
  45. CLIDict_Entry( lcdTest, "Test out the LCD display." );
  46. CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
  47. CLIDict_Item( lcdCmd ),
  48. CLIDict_Item( lcdInit ),
  49. CLIDict_Item( lcdTest ),
  50. { 0, 0, 0 } // Null entry for dictionary end
  51. };
  52. // ----- Interrupt Functions -----
  53. // ----- Functions -----
  54. inline void SPI_setup()
  55. {
  56. // Enable SPI internal clock
  57. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  58. // Setup MOSI (SOUT) and SCLK (SCK)
  59. PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  60. PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  61. // Setup SS (PCS)
  62. PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
  63. // Master Mode, CS0
  64. SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
  65. // DSPI Clock and Transfer Attributes
  66. // Frame Size: 8 bits
  67. // MSB First
  68. // CLK Low by default
  69. SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
  70. | SPI_CTAR_ASC(7)
  71. | SPI_CTAR_DT(7)
  72. | SPI_CTAR_CSSCK(7)
  73. | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
  74. }
  75. // Write buffer to SPI FIFO
  76. void SPI_write( uint8_t *buffer, uint8_t len )
  77. {
  78. for ( uint8_t byte = 0; byte < len; byte++ )
  79. {
  80. // Wait for SPI TxFIFO to have 4 or fewer entries
  81. while ( !( SPI0_SR & SPI_SR_TFFF ) )
  82. delayMicroseconds(10);
  83. // Write byte to TxFIFO
  84. // CS0, CTAR0
  85. SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
  86. // Indicate transfer has completed
  87. while ( !( SPI0_SR & SPI_SR_TCF ) );
  88. SPI0_SR |= SPI_SR_TCF;
  89. }
  90. }
  91. // Write to a control register
  92. void LCD_writeControlReg( uint8_t byte )
  93. {
  94. // Wait for TxFIFO to be empt
  95. while ( SPI0_TxFIFO_CNT != 0 );
  96. // Set A0 low to enter control register mode
  97. GPIOC_PCOR |= (1<<7);
  98. // Write byte to SPI FIFO
  99. SPI_write( &byte, 1 );
  100. // Wait for TxFIFO to be empty
  101. while ( SPI0_TxFIFO_CNT != 0 );
  102. // Make sure data has transferred
  103. delayMicroseconds(10); // XXX Adjust if SPI speed changes
  104. // Set A0 high to go back to display register mode
  105. GPIOC_PSOR |= (1<<7);
  106. }
  107. // Write to display register
  108. // Pages 0-7 normal display
  109. // Page 8 icon buffer
  110. void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
  111. {
  112. // Set the register page
  113. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  114. // Write buffer to SPI
  115. SPI_write( buffer, len );
  116. }
  117. inline void LCD_clearPage( uint8_t page )
  118. {
  119. // Set the register page
  120. LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
  121. // Set display start line
  122. LCD_writeControlReg( 0x40 );
  123. // Reset Column Address
  124. LCD_writeControlReg( 0x10 );
  125. LCD_writeControlReg( 0x00 );
  126. for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
  127. {
  128. uint8_t byte = 0;
  129. // Write buffer to SPI
  130. SPI_write( &byte, 1 );
  131. }
  132. // Wait for TxFIFO to be empty
  133. while ( SPI0_TxFIFO_CNT != 0 );
  134. }
  135. // Clear Display
  136. void LCD_clear()
  137. {
  138. // Setup each page
  139. for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
  140. {
  141. LCD_clearPage( page );
  142. }
  143. // Reset Page, Start Line, and Column Address
  144. // Page
  145. LCD_writeControlReg( 0xB0 );
  146. // Start Line
  147. LCD_writeControlReg( 0x40 );
  148. // Reset Column Address
  149. LCD_writeControlReg( 0x10 );
  150. LCD_writeControlReg( 0x00 );
  151. }
  152. // Intialize display
  153. void LCD_initialize()
  154. {
  155. // ADC Select (Normal)
  156. LCD_writeControlReg( 0xA0 );
  157. // LCD Off
  158. LCD_writeControlReg( 0xAE );
  159. // COM Scan Output Direction
  160. LCD_writeControlReg( 0xC0 );
  161. // LCD Bias (1/6 bias)
  162. LCD_writeControlReg( 0xA2 );
  163. // Power Supply Operating Mode (Internal Only)
  164. LCD_writeControlReg( 0x2F );
  165. // Internal Rb/Ra Ratio
  166. LCD_writeControlReg( 0x26 );
  167. // Reset
  168. LCD_writeControlReg( 0xE2 );
  169. // Electric volume mode set, and value
  170. LCD_writeControlReg( 0x81 );
  171. LCD_writeControlReg( 0x00 );
  172. // LCD On
  173. LCD_writeControlReg( 0xAF );
  174. // Clear Display RAM
  175. LCD_clear();
  176. }
  177. // Setup
  178. inline void LCD_setup()
  179. {
  180. // Register Scan CLI dictionary
  181. CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
  182. // Initialize SPI
  183. SPI_setup();
  184. // Setup Register Control Signal (A0)
  185. // Start in display register mode (1)
  186. GPIOC_PDDR |= (1<<7);
  187. PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  188. GPIOC_PSOR |= (1<<7);
  189. // Setup LCD Reset pin (RST)
  190. // 0 - Reset, 1 - Normal Operation
  191. // Start in normal mode (1)
  192. GPIOC_PDDR |= (1<<8);
  193. PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  194. GPIOC_PSOR |= (1<<8);
  195. // Run LCD intialization sequence
  196. LCD_initialize();
  197. }
  198. // LCD State processing loop
  199. inline uint8_t LCD_scan()
  200. {
  201. // NOP - Screen Refresh
  202. //LCD_writeControlReg( 0xE3 );
  203. return 0;
  204. }
  205. // ----- CLI Command Functions -----
  206. void cliFunc_lcdInit( char* args )
  207. {
  208. print( NL ); // No \r\n by default after the command is entered
  209. LCD_initialize();
  210. }
  211. void cliFunc_lcdTest( char* args )
  212. {
  213. print( NL ); // No \r\n by default after the command is entered
  214. //LCD_initialize();
  215. // Test pattern
  216. uint8_t pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  217. //uint8_t pattern[] = { 0xFF, 0x00, 0x96, 0xFF, 0x00, 0xFF, 0x00 };
  218. // Write to page D0
  219. LCD_writeDisplayReg( 0, pattern, sizeof( pattern ) );
  220. }
  221. void cliFunc_lcdCmd( char* args )
  222. {
  223. char* curArgs;
  224. char* arg1Ptr;
  225. char* arg2Ptr = args;
  226. print( NL ); // No \r\n by default after the command is entered
  227. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  228. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  229. // No args
  230. if ( *arg1Ptr == '\0' )
  231. return;
  232. // SPI Command
  233. uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
  234. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  235. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  236. // Single Arg
  237. if ( *arg1Ptr == '\0' )
  238. goto cmd;
  239. // TODO Deal with a0
  240. cmd:
  241. info_msg("Sending - ");
  242. printHex( cmd );
  243. print( NL );
  244. LCD_writeControlReg( cmd );
  245. }