123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- #include "hal_types.h"
- #include "hal_lcd.h"
- #include "OSAL.h"
- #include "OnBoard.h"
- #include "hal_assert.h"
- #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
- #include "DebugTrace.h"
- #endif
- #define HAL_LCD_MODE_PORT 0
- #define HAL_LCD_MODE_PIN 0
- #define HAL_LCD_RESET_PORT 1
- #define HAL_LCD_RESET_PIN 1
- #define HAL_LCD_CS_PORT 1
- #define HAL_LCD_CS_PIN 2
- #define HAL_LCD_CLK_PORT 1
- #define HAL_LCD_CLK_PIN 5
- #define HAL_LCD_MOSI_PORT 1
- #define HAL_LCD_MOSI_PIN 6
- #define HAL_LCD_MISO_PORT 1
- #define HAL_LCD_MISO_PIN 7
- #define HAL_SPI_CLOCK_POL_LO 0x00
- #define HAL_SPI_CLOCK_PHA_0 0x00
- #define HAL_SPI_TRANSFER_MSB_FIRST 0x20
- #define LCD_MAX_LINE_COUNT 3
- #define LCD_MAX_LINE_LENGTH 16
- #define LCD_MAX_BUF 25
- #define OSC_OFF 0x00
- #define OSC_ON 0x01
- #define POWER_SAVE_OFF 0x00
- #define POWER_SAVE_ON 0x02
- #define SET_POWER_SAVE_MODE(options) HalLcd_HW_Control(0x0C | (options))
- #define CGROM 0x00
- #define CGRAM 0x01
- #define COM_FORWARD 0x00
- #define COM_BACKWARD 0x02
- #define TWO_LINE 0x00
- #define THREE_LINE 0x04
- #define FUNCTION_SET(options) HalLcd_HW_Control(0x10 | (options))
- #define LINE1 0x00
- #define LINE2 0x01
- #define LINE3 0x02
- #define LINE4 0x03
- #define SET_DISPLAY_START_LINE(line) HalLcd_HW_Control(0x18 | (line))
- #define BIAS_1_5 0x00
- #define BIAS_1_4 0x01
- #define SET_BIAS_CTRL(bias) HalLcd_HW_Control(0x1C | (bias))
- #define VOLTAGE_DIVIDER_OFF 0x00
- #define VOLTAGE_DIVIDER_ON 0x01
- #define CONVERTER_AND_REG_OFF 0x00
- #define CONVERTER_AND_REG_ON 0x04
- #define SET_POWER_CTRL(options) HalLcd_HW_Control(0x20 | (options))
- #define DISPLAY_CTRL_ON 0x01
- #define DISPLAY_CTRL_OFF 0x00
- #define DISPLAY_CTRL_BLINK_ON 0x02
- #define DISPLAY_CTRL_BLINK_OFF 0x00
- #define DISPLAY_CTRL_CURSOR_ON 0x04
- #define DISPLAY_CTRL_CURSOR_OFF 0x00
- #define SET_DISPLAY_CTRL(options) HalLcd_HW_Control(0x28 | (options))
- #define SET_DDRAM_ADDR(charIndex) HalLcd_HW_Control(0x80 | (charIndex))
- #define SET_GCRAM_CHAR(specIndex) HalLcd_HW_Control(0xC0 | (specIndex))
- #define CONTRAST_CTRL_REGISTER 0x10
- #define SET_ICONRAM_ADDR(addr) HalLcd_HW_Control(0x40 | (addr))
- #define LINE_1_AND_2 0x01
- #define LINE_2_AND_3 0x02
- #define NORMAL_DISPLAY 0x00
- #define SET_DOUBLE_HEIGHT(options) HalLcd_HW_Control(0x08 | (options))
- #define HAL_IO_SET(port, pin, val) HAL_IO_SET_PREP(port, pin, val)
- #define HAL_IO_SET_PREP(port, pin, val) st( P##port##_##pin## = val; )
- #define HAL_CONFIG_IO_OUTPUT(port, pin, val) HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val)
- #define HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \
- P##port##_##pin## = val; \
- P##port##DIR |= BV(pin); )
- #define HAL_CONFIG_IO_PERIPHERAL(port, pin) HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin)
- #define HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BV(pin); )
- #define LCD_SPI_BEGIN() HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 0);
- #define LCD_SPI_END() \
- { \
- asm("NOP"); \
- asm("NOP"); \
- asm("NOP"); \
- asm("NOP"); \
- HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); \
- }
- #define LCD_SPI_TX(x) { U1CSR &= ~(BV(2) | BV(1)); U1DBUF = x; while( !(U1CSR & BV(1)) ); }
- #define LCD_SPI_WAIT_RXRDY() { while(!(U1CSR & BV(1))); }
- #define LCD_DO_WRITE() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1);
- #define LCD_DO_CONTROL() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 0);
- #define LCD_ACTIVATE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 0);
- #define LCD_RELEASE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1);
- #if (HAL_LCD == TRUE)
- static uint8 *Lcd_Line1;
- void HalLcd_HW_Init(void);
- void HalLcd_HW_WaitUs(uint16 i);
- void HalLcd_HW_Clear(void);
- void HalLcd_HW_ClearAllSpecChars(void);
- void HalLcd_HW_Control(uint8 cmd);
- void HalLcd_HW_Write(uint8 data);
- void HalLcd_HW_SetContrast(uint8 value);
- void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text);
- void HalLcd_HW_WriteLine(uint8 line, const char *pText);
- #endif
- void HalLcdInit(void)
- {
- #if (HAL_LCD == TRUE)
- Lcd_Line1 = NULL;
- HalLcd_HW_Init();
- #endif
- }
- void HalLcdWriteString ( char *str, uint8 option)
- {
- #if (HAL_LCD == TRUE)
- uint8 strLen = 0;
- uint8 totalLen = 0;
- uint8 *buf;
- uint8 tmpLen;
- if ( Lcd_Line1 == NULL )
- {
- Lcd_Line1 = osal_mem_alloc( HAL_LCD_MAX_CHARS+1 );
- HalLcdWriteString( "TexasInstruments", 1 );
- }
- strLen = (uint8)osal_strlen( (char*)str );
-
- if ( strLen > HAL_LCD_MAX_CHARS )
- strLen = HAL_LCD_MAX_CHARS;
- if ( option == HAL_LCD_LINE_1 )
- {
-
- osal_memcpy( Lcd_Line1, str, strLen );
- Lcd_Line1[strLen] = '\0';
- }
- else
- {
-
- tmpLen = (uint8)osal_strlen( (char*)Lcd_Line1 );
- totalLen = tmpLen + 1 + strLen + 1;
- buf = osal_mem_alloc( totalLen );
- if ( buf != NULL )
- {
-
- osal_memcpy( buf, Lcd_Line1, tmpLen );
- buf[tmpLen++] = ' ';
- osal_memcpy( &buf[tmpLen], str, strLen );
- buf[tmpLen+strLen] = '\0';
-
- #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
- #if defined(SERIAL_DEBUG_SUPPORTED)
- debug_str( (uint8*)buf );
- #endif
- #endif
-
- osal_mem_free( buf );
- }
- }
-
- HalLcd_HW_WriteLine (option, str);
- #endif
- }
- void HalLcdWriteValue ( uint32 value, const uint8 radix, uint8 option)
- {
- #if (HAL_LCD == TRUE)
- uint8 buf[LCD_MAX_BUF];
- _ltoa( value, &buf[0], radix );
- HalLcdWriteString( (char*)buf, option );
- #endif
- }
- void HalLcdWriteScreen( char *line1, char *line2 )
- {
- #if (HAL_LCD == TRUE)
- HalLcdWriteString( line1, 1 );
- HalLcdWriteString( line2, 2 );
- #endif
- }
- void HalLcdWriteStringValue( char *title, uint16 value, uint8 format, uint8 line )
- {
- #if (HAL_LCD == TRUE)
- uint8 tmpLen;
- uint8 buf[LCD_MAX_BUF];
- uint32 err;
- tmpLen = (uint8)osal_strlen( (char*)title );
- osal_memcpy( buf, title, tmpLen );
- buf[tmpLen] = ' ';
- err = (uint32)(value);
- _ltoa( err, &buf[tmpLen+1], format );
- HalLcdWriteString( (char*)buf, line );
- #endif
- }
- void HalLcdWriteStringValueValue( char *title, uint16 value1, uint8 format1,
- uint16 value2, uint8 format2, uint8 line )
- {
- #if (HAL_LCD == TRUE)
- uint8 tmpLen;
- uint8 buf[LCD_MAX_BUF];
- uint32 err;
- tmpLen = (uint8)osal_strlen( (char*)title );
- if ( tmpLen )
- {
- osal_memcpy( buf, title, tmpLen );
- buf[tmpLen++] = ' ';
- }
- err = (uint32)(value1);
- _ltoa( err, &buf[tmpLen], format1 );
- tmpLen = (uint8)osal_strlen( (char*)buf );
- buf[tmpLen++] = ',';
- buf[tmpLen++] = ' ';
- err = (uint32)(value2);
- _ltoa( err, &buf[tmpLen], format2 );
- HalLcdWriteString( (char *)buf, line );
- #endif
- }
- void HalLcdDisplayPercentBar( char *title, uint8 value )
- {
- #if (HAL_LCD == TRUE)
- uint8 percent;
- uint8 leftOver;
- uint8 buf[17];
- uint32 err;
- uint8 x;
-
- HalLcdWriteString( title, HAL_LCD_LINE_1 );
- if ( value > 100 )
- value = 100;
-
- percent = (uint8)(value / 10);
- leftOver = (uint8)(value % 10);
-
- osal_memcpy( buf, "[ ] ", 15 );
- for ( x = 0; x < percent; x ++ )
- {
- buf[1+x] = '>';
- }
- if ( leftOver >= 5 )
- buf[1+x] = '+';
- err = (uint32)value;
- _ltoa( err, (uint8*)&buf[13], 10 );
- HalLcdWriteString( (char*)buf, HAL_LCD_LINE_2 );
- #endif
- }
- #if (HAL_LCD == TRUE)
- static void halLcd_ConfigIO(void)
- {
-
- HAL_CONFIG_IO_OUTPUT(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1);
- HAL_CONFIG_IO_OUTPUT(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1);
- HAL_CONFIG_IO_OUTPUT(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1);
- }
- static void halLcd_ConfigSPI(void)
- {
-
- uint8 baud_exponent;
- uint8 baud_mantissa;
-
- PERCFG |= 0x02;
-
- HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_CLK_PORT, HAL_LCD_CLK_PIN);
- HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MOSI_PORT, HAL_LCD_MOSI_PIN);
- HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MISO_PORT, HAL_LCD_MISO_PIN);
-
- baud_exponent = 15;
- baud_mantissa = 0;
-
- U1UCR = 0x80;
- U1CSR = 0x00;
- U1GCR = HAL_SPI_TRANSFER_MSB_FIRST | HAL_SPI_CLOCK_PHA_0 | HAL_SPI_CLOCK_POL_LO | baud_exponent;
- U1BAUD = baud_mantissa;
- }
- void HalLcd_HW_Init(void)
- {
-
- halLcd_ConfigIO();
-
- halLcd_ConfigSPI();
-
- LCD_ACTIVATE_RESET();
- HalLcd_HW_WaitUs(15000);
- LCD_RELEASE_RESET();
- HalLcd_HW_WaitUs(15);
-
- FUNCTION_SET(CGRAM | COM_FORWARD | THREE_LINE);
-
- HalLcd_HW_SetContrast(15);
-
- SET_POWER_SAVE_MODE(OSC_OFF | POWER_SAVE_ON);
- SET_POWER_CTRL(VOLTAGE_DIVIDER_ON | CONVERTER_AND_REG_ON);
- SET_BIAS_CTRL(BIAS_1_5);
- HalLcd_HW_WaitUs(21000);
-
- HalLcd_HW_Clear();
- HalLcd_HW_ClearAllSpecChars();
- SET_DISPLAY_CTRL(DISPLAY_CTRL_ON | DISPLAY_CTRL_BLINK_OFF | DISPLAY_CTRL_CURSOR_OFF);
- }
- void HalLcd_HW_Control(uint8 cmd)
- {
- LCD_SPI_BEGIN();
- LCD_DO_CONTROL();
- LCD_SPI_TX(cmd);
- LCD_SPI_WAIT_RXRDY();
- LCD_SPI_END();
- }
- void HalLcd_HW_Write(uint8 data)
- {
- LCD_SPI_BEGIN();
- LCD_DO_WRITE();
- LCD_SPI_TX(data);
- LCD_SPI_WAIT_RXRDY();
- LCD_SPI_END();
- }
- void HalLcd_HW_SetContrast(uint8 value)
- {
- SET_ICONRAM_ADDR(CONTRAST_CTRL_REGISTER);
- HalLcd_HW_Write(value);
- }
- void HalLcd_HW_Clear(void)
- {
- uint8 n;
- SET_DDRAM_ADDR(0x00);
- for (n = 0; n < (LCD_MAX_LINE_COUNT * LCD_MAX_LINE_LENGTH); n++)
- {
- HalLcd_HW_Write(' ');
- }
- }
- void HalLcd_HW_ClearAllSpecChars(void)
- {
- uint8 n = 0;
- SET_GCRAM_CHAR(0);
- for (n = 0; n < (8 * 8); n++)
- {
- HalLcd_HW_Write(0x00);
- }
- }
- void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text)
- {
- if (col < LCD_MAX_LINE_LENGTH)
- {
- SET_DDRAM_ADDR((line - 1) * LCD_MAX_LINE_LENGTH + col);
- HalLcd_HW_Write(text);
- }
- else
- {
- return;
- }
- }
- void HalLcd_HW_WriteLine(uint8 line, const char *pText)
- {
- uint8 count;
- uint8 totalLength = (uint8)osal_strlen( (char *)pText );
-
- for (count=0; count<totalLength; count++)
- {
- HalLcd_HW_WriteChar(line, count, (*(pText++)));
- }
-
- for(count=totalLength; count<LCD_MAX_LINE_LENGTH;count++)
- {
- HalLcd_HW_WriteChar(line, count, ' ');
- }
- }
- void HalLcd_HW_WaitUs(uint16 microSecs)
- {
- while(microSecs--)
- {
-
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
- asm("nop"); asm("nop");
- }
- }
- #endif
|