hal_lcd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /**************************************************************************************************
  2. Filename: hal_lcd.c
  3. Revised: $Date: 2010-08-10 19:12:51 -0700 (Tue, 10 Aug 2010) $
  4. Revision: $Revision: 23375 $
  5. Description: This file contains the interface to the HAL LCD Service.
  6. Copyright 2007-2010 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /**************************************************************************************************
  34. * INCLUDES
  35. **************************************************************************************************/
  36. #include "hal_types.h"
  37. #include "hal_lcd.h"
  38. #include "OSAL.h"
  39. #include "OnBoard.h"
  40. #include "hal_assert.h"
  41. #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  42. #include "DebugTrace.h"
  43. #endif
  44. /**************************************************************************************************
  45. * CONSTANTS
  46. **************************************************************************************************/
  47. /*
  48. LCD pins
  49. //control
  50. P0.0 - LCD_MODE
  51. P1.1 - LCD_FLASH_RESET
  52. P1.2 - LCD_CS
  53. //spi
  54. P1.5 - CLK
  55. P1.6 - MOSI
  56. P1.7 - MISO
  57. */
  58. /* LCD Control lines */
  59. #define HAL_LCD_MODE_PORT 0
  60. #define HAL_LCD_MODE_PIN 0
  61. #define HAL_LCD_RESET_PORT 1
  62. #define HAL_LCD_RESET_PIN 1
  63. #define HAL_LCD_CS_PORT 1
  64. #define HAL_LCD_CS_PIN 2
  65. /* LCD SPI lines */
  66. #define HAL_LCD_CLK_PORT 1
  67. #define HAL_LCD_CLK_PIN 5
  68. #define HAL_LCD_MOSI_PORT 1
  69. #define HAL_LCD_MOSI_PIN 6
  70. #define HAL_LCD_MISO_PORT 1
  71. #define HAL_LCD_MISO_PIN 7
  72. /* SPI settings */
  73. #define HAL_SPI_CLOCK_POL_LO 0x00
  74. #define HAL_SPI_CLOCK_PHA_0 0x00
  75. #define HAL_SPI_TRANSFER_MSB_FIRST 0x20
  76. /* LCD lines */
  77. #define LCD_MAX_LINE_COUNT 3
  78. #define LCD_MAX_LINE_LENGTH 16
  79. #define LCD_MAX_BUF 25
  80. /* Defines for HW LCD */
  81. /* Set power save mode */
  82. #define OSC_OFF 0x00
  83. #define OSC_ON 0x01
  84. #define POWER_SAVE_OFF 0x00
  85. #define POWER_SAVE_ON 0x02
  86. #define SET_POWER_SAVE_MODE(options) HalLcd_HW_Control(0x0C | (options))
  87. /* Function Set */
  88. #define CGROM 0x00
  89. #define CGRAM 0x01
  90. #define COM_FORWARD 0x00
  91. #define COM_BACKWARD 0x02
  92. #define TWO_LINE 0x00
  93. #define THREE_LINE 0x04
  94. #define FUNCTION_SET(options) HalLcd_HW_Control(0x10 | (options))
  95. /* Set Display Start Line */
  96. #define LINE1 0x00
  97. #define LINE2 0x01
  98. #define LINE3 0x02
  99. #define LINE4 0x03
  100. #define SET_DISPLAY_START_LINE(line) HalLcd_HW_Control(0x18 | (line))
  101. /* Bias control */
  102. #define BIAS_1_5 0x00
  103. #define BIAS_1_4 0x01
  104. #define SET_BIAS_CTRL(bias) HalLcd_HW_Control(0x1C | (bias))
  105. /* Power control */
  106. #define VOLTAGE_DIVIDER_OFF 0x00
  107. #define VOLTAGE_DIVIDER_ON 0x01
  108. #define CONVERTER_AND_REG_OFF 0x00
  109. #define CONVERTER_AND_REG_ON 0x04
  110. #define SET_POWER_CTRL(options) HalLcd_HW_Control(0x20 | (options))
  111. // Set display control
  112. #define DISPLAY_CTRL_ON 0x01
  113. #define DISPLAY_CTRL_OFF 0x00
  114. #define DISPLAY_CTRL_BLINK_ON 0x02
  115. #define DISPLAY_CTRL_BLINK_OFF 0x00
  116. #define DISPLAY_CTRL_CURSOR_ON 0x04
  117. #define DISPLAY_CTRL_CURSOR_OFF 0x00
  118. #define SET_DISPLAY_CTRL(options) HalLcd_HW_Control(0x28 | (options))
  119. /* Set DD/ CGRAM address */
  120. #define SET_DDRAM_ADDR(charIndex) HalLcd_HW_Control(0x80 | (charIndex))
  121. #define SET_GCRAM_CHAR(specIndex) HalLcd_HW_Control(0xC0 | (specIndex))
  122. /* Set ICONRAM address */
  123. #define CONTRAST_CTRL_REGISTER 0x10
  124. #define SET_ICONRAM_ADDR(addr) HalLcd_HW_Control(0x40 | (addr))
  125. /* Set double height */
  126. #define LINE_1_AND_2 0x01
  127. #define LINE_2_AND_3 0x02
  128. #define NORMAL_DISPLAY 0x00
  129. #define SET_DOUBLE_HEIGHT(options) HalLcd_HW_Control(0x08 | (options))
  130. /**************************************************************************************************
  131. * MACROS
  132. **************************************************************************************************/
  133. #define HAL_IO_SET(port, pin, val) HAL_IO_SET_PREP(port, pin, val)
  134. #define HAL_IO_SET_PREP(port, pin, val) st( P##port##_##pin## = val; )
  135. #define HAL_CONFIG_IO_OUTPUT(port, pin, val) HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val)
  136. #define HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \
  137. P##port##_##pin## = val; \
  138. P##port##DIR |= BV(pin); )
  139. #define HAL_CONFIG_IO_PERIPHERAL(port, pin) HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin)
  140. #define HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BV(pin); )
  141. /* SPI interface control */
  142. #define LCD_SPI_BEGIN() HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 0); /* chip select */
  143. #define LCD_SPI_END() \
  144. { \
  145. asm("NOP"); \
  146. asm("NOP"); \
  147. asm("NOP"); \
  148. asm("NOP"); \
  149. HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); /* chip select */ \
  150. }
  151. /* clear the received and transmit byte status, write tx data to buffer, wait till transmit done */
  152. #define LCD_SPI_TX(x) { U1CSR &= ~(BV(2) | BV(1)); U1DBUF = x; while( !(U1CSR & BV(1)) ); }
  153. #define LCD_SPI_WAIT_RXRDY() { while(!(U1CSR & BV(1))); }
  154. /* Control macros */
  155. #define LCD_DO_WRITE() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1);
  156. #define LCD_DO_CONTROL() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 0);
  157. #define LCD_ACTIVATE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 0);
  158. #define LCD_RELEASE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1);
  159. #if (HAL_LCD == TRUE)
  160. /**************************************************************************************************
  161. * LOCAL VARIABLES
  162. **************************************************************************************************/
  163. static uint8 *Lcd_Line1;
  164. /**************************************************************************************************
  165. * FUNCTIONS - API
  166. **************************************************************************************************/
  167. void HalLcd_HW_Init(void);
  168. void HalLcd_HW_WaitUs(uint16 i);
  169. void HalLcd_HW_Clear(void);
  170. void HalLcd_HW_ClearAllSpecChars(void);
  171. void HalLcd_HW_Control(uint8 cmd);
  172. void HalLcd_HW_Write(uint8 data);
  173. void HalLcd_HW_SetContrast(uint8 value);
  174. void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text);
  175. void HalLcd_HW_WriteLine(uint8 line, const char *pText);
  176. #endif //LCD
  177. /**************************************************************************************************
  178. * @fn HalLcdInit
  179. *
  180. * @brief Initilize LCD Service
  181. *
  182. * @param init - pointer to void that contains the initialized value
  183. *
  184. * @return None
  185. **************************************************************************************************/
  186. void HalLcdInit(void)
  187. {
  188. #if (HAL_LCD == TRUE)
  189. Lcd_Line1 = NULL;
  190. HalLcd_HW_Init();
  191. #endif
  192. }
  193. /*************************************************************************************************
  194. * LCD EMULATION FUNCTIONS
  195. *
  196. * Some evaluation boards are equipped with Liquid Crystal Displays
  197. * (LCD) which may be used to display diagnostic information. These
  198. * functions provide LCD emulation, sending the diagnostic strings
  199. * to Z-Tool via the RS232 serial port. These functions are enabled
  200. * when the "LCD_SUPPORTED" compiler flag is placed in the makefile.
  201. *
  202. * Most applications update both lines (1 and 2) of the LCD whenever
  203. * text is posted to the device. This emulator assumes that line 1 is
  204. * updated first (saved locally) and the formatting and send operation
  205. * is triggered by receipt of line 2. Nothing will be transmitted if
  206. * only line 1 is updated.
  207. *
  208. *************************************************************************************************/
  209. /**************************************************************************************************
  210. * @fn HalLcdWriteString
  211. *
  212. * @brief Write a string to the LCD
  213. *
  214. * @param str - pointer to the string that will be displayed
  215. * option - display options
  216. *
  217. * @return None
  218. **************************************************************************************************/
  219. void HalLcdWriteString ( char *str, uint8 option)
  220. {
  221. #if (HAL_LCD == TRUE)
  222. uint8 strLen = 0;
  223. uint8 totalLen = 0;
  224. uint8 *buf;
  225. uint8 tmpLen;
  226. if ( Lcd_Line1 == NULL )
  227. {
  228. Lcd_Line1 = osal_mem_alloc( HAL_LCD_MAX_CHARS+1 );
  229. HalLcdWriteString( "TexasInstruments", 1 );
  230. }
  231. strLen = (uint8)osal_strlen( (char*)str );
  232. /* Check boundries */
  233. if ( strLen > HAL_LCD_MAX_CHARS )
  234. strLen = HAL_LCD_MAX_CHARS;
  235. if ( option == HAL_LCD_LINE_1 )
  236. {
  237. /* Line 1 gets saved for later */
  238. osal_memcpy( Lcd_Line1, str, strLen );
  239. Lcd_Line1[strLen] = '\0';
  240. }
  241. else
  242. {
  243. /* Line 2 triggers action */
  244. tmpLen = (uint8)osal_strlen( (char*)Lcd_Line1 );
  245. totalLen = tmpLen + 1 + strLen + 1;
  246. buf = osal_mem_alloc( totalLen );
  247. if ( buf != NULL )
  248. {
  249. /* Concatenate strings */
  250. osal_memcpy( buf, Lcd_Line1, tmpLen );
  251. buf[tmpLen++] = ' ';
  252. osal_memcpy( &buf[tmpLen], str, strLen );
  253. buf[tmpLen+strLen] = '\0';
  254. /* Send it out */
  255. #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  256. #if defined(SERIAL_DEBUG_SUPPORTED)
  257. debug_str( (uint8*)buf );
  258. #endif //LCD_SUPPORTED
  259. #endif //ZTOOL_P1
  260. /* Free mem */
  261. osal_mem_free( buf );
  262. }
  263. }
  264. /* Display the string */
  265. HalLcd_HW_WriteLine (option, str);
  266. #endif //HAL_LCD
  267. }
  268. /**************************************************************************************************
  269. * @fn HalLcdWriteValue
  270. *
  271. * @brief Write a value to the LCD
  272. *
  273. * @param value - value that will be displayed
  274. * radix - 8, 10, 16
  275. * option - display options
  276. *
  277. * @return None
  278. **************************************************************************************************/
  279. void HalLcdWriteValue ( uint32 value, const uint8 radix, uint8 option)
  280. {
  281. #if (HAL_LCD == TRUE)
  282. uint8 buf[LCD_MAX_BUF];
  283. _ltoa( value, &buf[0], radix );
  284. HalLcdWriteString( (char*)buf, option );
  285. #endif
  286. }
  287. /**************************************************************************************************
  288. * @fn HalLcdWriteScreen
  289. *
  290. * @brief Write a value to the LCD
  291. *
  292. * @param line1 - string that will be displayed on line 1
  293. * line2 - string that will be displayed on line 2
  294. *
  295. * @return None
  296. **************************************************************************************************/
  297. void HalLcdWriteScreen( char *line1, char *line2 )
  298. {
  299. #if (HAL_LCD == TRUE)
  300. HalLcdWriteString( line1, 1 );
  301. HalLcdWriteString( line2, 2 );
  302. #endif
  303. }
  304. /**************************************************************************************************
  305. * @fn HalLcdWriteStringValue
  306. *
  307. * @brief Write a string followed by a value to the LCD
  308. *
  309. * @param title - Title that will be displayed before the value
  310. * value - value
  311. * format - redix
  312. * line - line number
  313. *
  314. * @return None
  315. **************************************************************************************************/
  316. void HalLcdWriteStringValue( char *title, uint16 value, uint8 format, uint8 line )
  317. {
  318. #if (HAL_LCD == TRUE)
  319. uint8 tmpLen;
  320. uint8 buf[LCD_MAX_BUF];
  321. uint32 err;
  322. tmpLen = (uint8)osal_strlen( (char*)title );
  323. osal_memcpy( buf, title, tmpLen );
  324. buf[tmpLen] = ' ';
  325. err = (uint32)(value);
  326. _ltoa( err, &buf[tmpLen+1], format );
  327. HalLcdWriteString( (char*)buf, line );
  328. #endif
  329. }
  330. /**************************************************************************************************
  331. * @fn HalLcdWriteStringValue
  332. *
  333. * @brief Write a string followed by a value to the LCD
  334. *
  335. * @param title - Title that will be displayed before the value
  336. * value1 - value #1
  337. * format1 - redix of value #1
  338. * value2 - value #2
  339. * format2 - redix of value #2
  340. * line - line number
  341. *
  342. * @return None
  343. **************************************************************************************************/
  344. void HalLcdWriteStringValueValue( char *title, uint16 value1, uint8 format1,
  345. uint16 value2, uint8 format2, uint8 line )
  346. {
  347. #if (HAL_LCD == TRUE)
  348. uint8 tmpLen;
  349. uint8 buf[LCD_MAX_BUF];
  350. uint32 err;
  351. tmpLen = (uint8)osal_strlen( (char*)title );
  352. if ( tmpLen )
  353. {
  354. osal_memcpy( buf, title, tmpLen );
  355. buf[tmpLen++] = ' ';
  356. }
  357. err = (uint32)(value1);
  358. _ltoa( err, &buf[tmpLen], format1 );
  359. tmpLen = (uint8)osal_strlen( (char*)buf );
  360. buf[tmpLen++] = ',';
  361. buf[tmpLen++] = ' ';
  362. err = (uint32)(value2);
  363. _ltoa( err, &buf[tmpLen], format2 );
  364. HalLcdWriteString( (char *)buf, line );
  365. #endif
  366. }
  367. /**************************************************************************************************
  368. * @fn HalLcdDisplayPercentBar
  369. *
  370. * @brief Display percentage bar on the LCD
  371. *
  372. * @param title -
  373. * value -
  374. *
  375. * @return None
  376. **************************************************************************************************/
  377. void HalLcdDisplayPercentBar( char *title, uint8 value )
  378. {
  379. #if (HAL_LCD == TRUE)
  380. uint8 percent;
  381. uint8 leftOver;
  382. uint8 buf[17];
  383. uint32 err;
  384. uint8 x;
  385. /* Write the title: */
  386. HalLcdWriteString( title, HAL_LCD_LINE_1 );
  387. if ( value > 100 )
  388. value = 100;
  389. /* convert to blocks */
  390. percent = (uint8)(value / 10);
  391. leftOver = (uint8)(value % 10);
  392. /* Make window */
  393. osal_memcpy( buf, "[ ] ", 15 );
  394. for ( x = 0; x < percent; x ++ )
  395. {
  396. buf[1+x] = '>';
  397. }
  398. if ( leftOver >= 5 )
  399. buf[1+x] = '+';
  400. err = (uint32)value;
  401. _ltoa( err, (uint8*)&buf[13], 10 );
  402. HalLcdWriteString( (char*)buf, HAL_LCD_LINE_2 );
  403. #endif
  404. }
  405. #if (HAL_LCD == TRUE)
  406. /**************************************************************************************************
  407. * HARDWARE LCD
  408. **************************************************************************************************/
  409. /**************************************************************************************************
  410. * @fn halLcd_ConfigIO
  411. *
  412. * @brief Configure IO lines needed for LCD control.
  413. *
  414. * @param None
  415. *
  416. * @return None
  417. **************************************************************************************************/
  418. static void halLcd_ConfigIO(void)
  419. {
  420. /* GPIO configuration */
  421. HAL_CONFIG_IO_OUTPUT(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1);
  422. HAL_CONFIG_IO_OUTPUT(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1);
  423. HAL_CONFIG_IO_OUTPUT(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1);
  424. }
  425. /**************************************************************************************************
  426. * @fn halLcd_ConfigSPI
  427. *
  428. * @brief Configure SPI lines needed for talking to LCD.
  429. *
  430. * @param None
  431. *
  432. * @return None
  433. **************************************************************************************************/
  434. static void halLcd_ConfigSPI(void)
  435. {
  436. /* UART/SPI Peripheral configuration */
  437. uint8 baud_exponent;
  438. uint8 baud_mantissa;
  439. /* Set SPI on UART 1 alternative 2 */
  440. PERCFG |= 0x02;
  441. /* Configure clk, master out and master in lines */
  442. HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_CLK_PORT, HAL_LCD_CLK_PIN);
  443. HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MOSI_PORT, HAL_LCD_MOSI_PIN);
  444. HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MISO_PORT, HAL_LCD_MISO_PIN);
  445. /* Set SPI speed to 1 MHz (the values assume system clk of 32MHz)
  446. * Confirm on board that this results in 1MHz spi clk.
  447. */
  448. baud_exponent = 15;
  449. baud_mantissa = 0;
  450. /* Configure SPI */
  451. U1UCR = 0x80; /* Flush and goto IDLE state. 8-N-1. */
  452. U1CSR = 0x00; /* SPI mode, master. */
  453. U1GCR = HAL_SPI_TRANSFER_MSB_FIRST | HAL_SPI_CLOCK_PHA_0 | HAL_SPI_CLOCK_POL_LO | baud_exponent;
  454. U1BAUD = baud_mantissa;
  455. }
  456. /**************************************************************************************************
  457. * @fn HalLcd_HW_Init
  458. *
  459. * @brief Initilize HW LCD Driver.
  460. *
  461. * @param None
  462. *
  463. * @return None
  464. **************************************************************************************************/
  465. void HalLcd_HW_Init(void)
  466. {
  467. /* Initialize LCD IO lines */
  468. halLcd_ConfigIO();
  469. /* Initialize SPI */
  470. halLcd_ConfigSPI();
  471. /* Perform reset */
  472. LCD_ACTIVATE_RESET();
  473. HalLcd_HW_WaitUs(15000); // 15 ms
  474. LCD_RELEASE_RESET();
  475. HalLcd_HW_WaitUs(15); // 15 us
  476. /* Perform the initialization sequence */
  477. FUNCTION_SET(CGRAM | COM_FORWARD | THREE_LINE);
  478. /* Set contrast */
  479. HalLcd_HW_SetContrast(15);
  480. /* Set power */
  481. SET_POWER_SAVE_MODE(OSC_OFF | POWER_SAVE_ON);
  482. SET_POWER_CTRL(VOLTAGE_DIVIDER_ON | CONVERTER_AND_REG_ON);
  483. SET_BIAS_CTRL(BIAS_1_5);
  484. HalLcd_HW_WaitUs(21000);// 21 ms
  485. /* Clear the display */
  486. HalLcd_HW_Clear();
  487. HalLcd_HW_ClearAllSpecChars();
  488. SET_DISPLAY_CTRL(DISPLAY_CTRL_ON | DISPLAY_CTRL_BLINK_OFF | DISPLAY_CTRL_CURSOR_OFF);
  489. }
  490. /**************************************************************************************************
  491. * @fn HalLcd_HW_Control
  492. *
  493. * @brief Write 1 command to the LCD
  494. *
  495. * @param uint8 cmd - command to be written to the LCD
  496. *
  497. * @return None
  498. **************************************************************************************************/
  499. void HalLcd_HW_Control(uint8 cmd)
  500. {
  501. LCD_SPI_BEGIN();
  502. LCD_DO_CONTROL();
  503. LCD_SPI_TX(cmd);
  504. LCD_SPI_WAIT_RXRDY();
  505. LCD_SPI_END();
  506. }
  507. /**************************************************************************************************
  508. * @fn HalLcd_HW_Write
  509. *
  510. * @brief Write 1 byte to the LCD
  511. *
  512. * @param uint8 data - data to be written to the LCD
  513. *
  514. * @return None
  515. **************************************************************************************************/
  516. void HalLcd_HW_Write(uint8 data)
  517. {
  518. LCD_SPI_BEGIN();
  519. LCD_DO_WRITE();
  520. LCD_SPI_TX(data);
  521. LCD_SPI_WAIT_RXRDY();
  522. LCD_SPI_END();
  523. }
  524. /**************************************************************************************************
  525. * @fn HalLcd_HW_SetContrast
  526. *
  527. * @brief Set display contrast
  528. *
  529. * @param uint8 value - contrast value
  530. *
  531. * @return none
  532. **************************************************************************************************/
  533. void HalLcd_HW_SetContrast(uint8 value)
  534. {
  535. SET_ICONRAM_ADDR(CONTRAST_CTRL_REGISTER);
  536. HalLcd_HW_Write(value);
  537. }
  538. /**************************************************************************************************
  539. * @fn HalLcd_HW_Clear
  540. *
  541. * @brief Clear the HW LCD
  542. *
  543. * @param None
  544. *
  545. * @return None
  546. **************************************************************************************************/
  547. void HalLcd_HW_Clear(void)
  548. {
  549. uint8 n;
  550. SET_DDRAM_ADDR(0x00);
  551. for (n = 0; n < (LCD_MAX_LINE_COUNT * LCD_MAX_LINE_LENGTH); n++)
  552. {
  553. HalLcd_HW_Write(' ');
  554. }
  555. }
  556. /**************************************************************************************************
  557. * @fn HalLcd_HW_ClearAllSpecChars
  558. *
  559. * @brief Clear all special chars
  560. *
  561. * @param None
  562. *
  563. * @return None
  564. **************************************************************************************************/
  565. void HalLcd_HW_ClearAllSpecChars(void)
  566. {
  567. uint8 n = 0;
  568. SET_GCRAM_CHAR(0);
  569. for (n = 0; n < (8 * 8); n++)
  570. {
  571. HalLcd_HW_Write(0x00);
  572. }
  573. }
  574. /**************************************************************************************************
  575. * @fn HalLcd_HW_WriteChar
  576. *
  577. * @brief Write one char to the display
  578. *
  579. * @param uint8 line - line number that the char will be displayed
  580. * uint8 col - colum where the char will be displayed
  581. *
  582. * @return None
  583. **************************************************************************************************/
  584. void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text)
  585. {
  586. if (col < LCD_MAX_LINE_LENGTH)
  587. {
  588. SET_DDRAM_ADDR((line - 1) * LCD_MAX_LINE_LENGTH + col);
  589. HalLcd_HW_Write(text);
  590. }
  591. else
  592. {
  593. return;
  594. }
  595. }
  596. /**************************************************************************************************
  597. * @fn halLcdWriteLine
  598. *
  599. * @brief Write one line on display
  600. *
  601. * @param uint8 line - display line
  602. * char *pText - text buffer to write
  603. *
  604. * @return none
  605. **************************************************************************************************/
  606. void HalLcd_HW_WriteLine(uint8 line, const char *pText)
  607. {
  608. uint8 count;
  609. uint8 totalLength = (uint8)osal_strlen( (char *)pText );
  610. /* Write the content first */
  611. for (count=0; count<totalLength; count++)
  612. {
  613. HalLcd_HW_WriteChar(line, count, (*(pText++)));
  614. }
  615. /* Write blank spaces to rest of the line */
  616. for(count=totalLength; count<LCD_MAX_LINE_LENGTH;count++)
  617. {
  618. HalLcd_HW_WriteChar(line, count, ' ');
  619. }
  620. }
  621. /**************************************************************************************************
  622. * @fn HalLcd_HW_WaitUs
  623. *
  624. * @brief wait for x us. @ 32MHz MCU clock it takes 32 "nop"s for 1 us delay.
  625. *
  626. * @param x us. range[0-65536]
  627. *
  628. * @return None
  629. **************************************************************************************************/
  630. void HalLcd_HW_WaitUs(uint16 microSecs)
  631. {
  632. while(microSecs--)
  633. {
  634. /* 32 NOPs == 1 usecs */
  635. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  636. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  637. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  638. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  639. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  640. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  641. asm("nop"); asm("nop");
  642. }
  643. }
  644. #endif
  645. /**************************************************************************************************
  646. **************************************************************************************************/