37 #include "contiki-conf.h"
39 #include "hal_MSP-EXP430F5438.h"
40 #include "hal_lcd_fonts.h"
42 unsigned char LcdInitMacro[] = {
43 0x74, 0x00, 0x00, 0x76, 0x00, 0x01,
44 0x74, 0x00, 0x01, 0x76, 0x00, 0x0D,
45 0x74, 0x00, 0x02, 0x76, 0x00, 0x4C,
46 0x74, 0x00, 0x03, 0x76, 0x12, 0x14,
47 0x74, 0x00, 0x04, 0x76, 0x04, 0x66,
48 0x74, 0x00, 0x05, 0x76, 0x00, 0x10,
49 0x74, 0x00, 0x06, 0x76, 0x00, 0x00,
50 0x74, 0x00, 0x07, 0x76, 0x00, 0x15,
51 0x74, 0x00, 0x08, 0x76, 0x00, 0x03,
52 0x74, 0x00, 0x09, 0x76, 0x00, 0x00,
53 0x74, 0x00, 0x0A, 0x76, 0x00, 0x15,
54 0x74, 0x00, 0x0B, 0x76, 0x00, 0x03,
55 0x74, 0x00, 0x0C, 0x76, 0x00, 0x03,
56 0x74, 0x00, 0x0D, 0x76, 0x00, 0x00,
57 0x74, 0x00, 0x0E, 0x76, 0x00, 0x15,
58 0x74, 0x00, 0x0F, 0x76, 0x00, 0x03,
59 0x74, 0x00, 0x10, 0x76, 0x00, 0x15,
60 0x74, 0x00, 0x11, 0x76, 0x00, 0x03,
63 unsigned char Read_Block_Address_Macro[] = {0x74, 0x00, 0x12, 0x77, 0x00, 0x00};
64 unsigned char Draw_Block_Value_Macro[] = {0x74, 0x00, 0x12, 0x76, 0xFF, 0xFF};
65 unsigned char Draw_Block_Address_Macro[] = {0x74, 0x00, 0x11, 0x76, 0x00, 0x00};
67 unsigned int LcdAddress = 0, LcdTableAddress = 0;
68 unsigned char contrast = 0x66;
69 unsigned char backlight = 8;
70 int LCD_MEM[110 * 17];
85 void halLcdSendCommand(
unsigned char Data[])
89 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
90 for (i = 0; i < 6; i++)
92 while (!(UCB2IFG & UCTXIFG)) ;
97 while (UCB2STAT & UCBUSY) ;
98 LCD_CS_RST_OUT |= LCD_CS_PIN;
99 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
102 while (UCB2STAT & UCBUSY) ;
103 LCD_CS_RST_OUT |= LCD_CS_PIN;
117 void halLcdInit(
void)
119 volatile unsigned int i = 0;
121 LCD_CS_RST_OUT |= LCD_CS_PIN | LCD_RESET_PIN;
122 LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN;
124 LCD_BACKLT_SEL |= LCD_BACKLIGHT_PIN;
126 LCD_CS_RST_OUT &= ~LCD_RESET_PIN;
128 LCD_CS_RST_OUT |= LCD_RESET_PIN;
131 LCD_SPI_SEL |= LCD_MOSI_PIN + LCD_CLK_PIN;
132 LCD_SPI_SEL &= ~LCD_MISO_PIN;
133 LCD_SPI_DIR &= ~(LCD_MISO_PIN + LCD_MOSI_PIN);
139 UCB2CTL0 = UCMST + UCSYNC + UCCKPL + UCMSB;
140 UCB2CTL1 |= UCSSEL_2;
143 UCB2CTL1 &= ~UCSWRST;
150 halLcdSendCommand(&LcdInitMacro[1 * 6]);
151 halLcdSendCommand(&LcdInitMacro[2 * 6]);
152 halLcdSendCommand(&LcdInitMacro[4 * 6]);
153 halLcdSendCommand(&LcdInitMacro[5 * 6]);
154 halLcdSendCommand(&LcdInitMacro[6 * 6]);
155 halLcdSendCommand(&LcdInitMacro[7 * 6]);
167 void halLcdShutDown(
void)
171 LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN;
172 LCD_CS_RST_OUT &= ~(LCD_CS_PIN | LCD_RESET_PIN);
173 LCD_CS_RST_OUT &= ~LCD_RESET_PIN;
175 LCD_SPI_SEL &= ~(LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN);
176 LCD_CS_RST_DIR |= LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN;
177 LCD_CS_RST_OUT &= ~(LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN);
191 void halLcdBackLightInit(
void)
193 LCD_BACKLT_DIR |= LCD_BACKLIGHT_PIN;
194 LCD_BACKLT_OUT |= LCD_BACKLIGHT_PIN;
195 LCD_BACKLT_SEL |= LCD_BACKLIGHT_PIN;
198 TA0CCR3 = TA0CCR0 >> 1;
202 TA0CTL = TASSEL_2 + MC_1;
214 unsigned int halLcdGetBackLight(
void)
227 void halLcdSetBackLight(
unsigned char BackLightLevel)
229 unsigned int dutyCycle = 0, i, dummy;
231 if (BackLightLevel > 0)
234 dummy = (TA0CCR0 >> 4);
236 for (i = 0; i < BackLightLevel; i++)
250 backlight = BackLightLevel;
263 void halLcdShutDownBackLight(
void)
265 LCD_BACKLT_DIR |= LCD_BACKLIGHT_PIN;
266 LCD_BACKLT_OUT &= ~(LCD_BACKLIGHT_PIN);
267 LCD_BACKLT_SEL &= ~LCD_BACKLIGHT_PIN;
283 void halLcdSetContrast(
unsigned char ContrastLevel)
285 if (ContrastLevel > 127) ContrastLevel = 127;
286 if (ContrastLevel < 70) ContrastLevel = 70;
287 LcdInitMacro[0x04 * 6 + 5] = ContrastLevel;
288 halLcdSendCommand(&LcdInitMacro[0x04 * 6]);
299 unsigned char halLcdGetContrast(
void)
301 return LcdInitMacro[0x04 * 6 + 5];
312 void halLcdCursor(
void)
314 LcdInitMacro[8 * 6 + 5] ^= BIT2;
315 halLcdSendCommand(&LcdInitMacro[8 * 6]);
317 LcdInitMacro[0x0B * 6 + 5] = ((LcdAddress & 0x1F) << 3);
318 LcdInitMacro[0x0B * 6 + 4] = ((LcdAddress & 0x1F) << 3) + 3;
319 LcdInitMacro[0x0C * 6 + 5] = (LcdAddress >> 5);
320 LcdInitMacro[0x0C * 6 + 4] = (LcdAddress >> 5) + 7;
321 halLcdSendCommand(&LcdInitMacro[0x0B * 6]);
322 halLcdSendCommand(&LcdInitMacro[0x0C * 6]);
324 halLcdSetAddress(LcdAddress);
335 void halLcdCursorOff(
void)
337 LcdInitMacro[8 * 6 + 5] &= ~BIT2;
338 halLcdSendCommand(&LcdInitMacro[8 * 6]);
349 void halLcdReverse(
void)
351 LcdInitMacro[7 * 6 + 5] ^= BIT1;
352 halLcdSendCommand(&LcdInitMacro[7 * 6]);
363 void halLcdStandby(
void)
365 LcdInitMacro[3 * 6 + 5] &= (~BIT3) & (~BIT2);
366 LcdInitMacro[3 * 6 + 5] |= BIT0;
367 halLcdSendCommand(&LcdInitMacro[3 * 6]);
378 void halLcdActive(
void)
380 halLcdSendCommand(LcdInitMacro);
386 for(i = 0; i < 5; ++i) {
391 LcdInitMacro[3 * 6 + 5] |= BIT3;
392 LcdInitMacro[3 * 6 + 5] &= ~BIT0;
393 halLcdSendCommand(&LcdInitMacro[3 * 6]);
408 void halLcdSetAddress(
int Address)
412 Draw_Block_Address_Macro[4] = Address >> 8;
413 Draw_Block_Address_Macro[5] = Address & 0xFF;
414 halLcdSendCommand(Draw_Block_Address_Macro);
415 LcdAddress = Address;
417 temp = temp + (temp << 4);
419 LcdTableAddress = temp + (Address & 0x1F);
436 void halLcdDrawBlock(
unsigned int Address,
unsigned int Value)
438 halLcdSetAddress(Address);
439 halLcdDrawCurrentBlock(Value);
452 void halLcdDrawCurrentBlock(
unsigned int Value)
456 Draw_Block_Value_Macro[4] = Value >> 8;
457 Draw_Block_Value_Macro[5] = Value & 0xFF;
458 LCD_MEM[LcdTableAddress] = Value;
460 halLcdSendCommand(Draw_Block_Value_Macro);
463 temp = LcdAddress >> 5;
464 temp = temp + (temp << 4);
466 LcdTableAddress = temp + (LcdAddress & 0x1F);
469 if ((LcdAddress & 0x1F) > 0x11)
470 halLcdSetAddress((LcdAddress & 0xFFE0) + 0x20);
471 if (LcdAddress == LCD_Size)
483 int halLcdReadBlock(
unsigned int Address)
485 int i = 0, Value = 0, ReadData[7];
487 halLcdSetAddress(Address);
488 halLcdSendCommand(Read_Block_Address_Macro);
490 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
493 while (!(UCB2IFG & UCTXIFG)) ;
494 while (UCB2STAT & UCBUSY) ;
497 LCD_SPI_SEL &= ~LCD_MOSI_PIN;
498 LCD_SPI_SEL |= LCD_MISO_PIN;
500 for (i = 0; i < 7; i++)
504 while (!(UCB2IFG & UCRXIFG)) ;
505 ReadData[i] = UCB2RXBUF;
507 LCD_CS_RST_OUT |= LCD_CS_PIN;
509 LCD_SPI_SEL |= LCD_MOSI_PIN;
510 LCD_SPI_SEL &= ~LCD_MISO_PIN;
511 LCD_CS_RST_DIR |= LCD_MOSI_PIN + LCD_CLK_PIN;
512 LCD_CS_RST_DIR &= ~LCD_MISO_PIN;
514 Value = (ReadData[5] << 8) + ReadData[6];
531 void halLcdPixel(
int x,
int y,
unsigned char GrayScale)
534 unsigned char offset;
537 if ((x >= 0) && (x < LCD_COL) && (y >= 0) && (y < LCD_ROW))
539 Address = (y << 5) + (x >> 3);
541 Value = LCD_MEM[(y << 4) + y + (x >> 3)];
543 offset = (x & 0x07) << 1;
544 Value &= ~(3 << offset);
545 Value |= GrayScale << offset;
547 halLcdDrawBlock(Address, Value);
559 void halLcdClearScreen(
void)
561 int i, j, k, Current_Location = 0;
565 for (i = 0; i < 110; i++)
568 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
569 for (k = 0; k < 3; k++)
571 while (!(UCB2IFG & UCTXIFG)) ;
572 UCB2TXBUF = Draw_Block_Value_Macro[k];
574 while (UCB2STAT & UCBUSY) ;
575 LCD_CS_RST_OUT |= LCD_CS_PIN;
576 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
577 while (!(UCB2IFG & UCTXIFG)) ;
578 UCB2TXBUF = Draw_Block_Value_Macro[3];
581 for (j = 0; j < 17; j++)
583 LCD_MEM[LcdTableAddress++] = 0x00;
584 while (!(UCB2IFG & UCTXIFG)) ;
586 while (!(UCB2IFG & UCTXIFG)) ;
590 while (!(UCB2IFG & UCTXIFG)) ;
592 while (!(UCB2IFG & UCTXIFG)) ;
594 while (UCB2STAT & UCBUSY) ;
595 LCD_CS_RST_OUT |= LCD_CS_PIN;
597 Current_Location += 0x20;
598 halLcdSetAddress(Current_Location);
621 void halLcdImage(
const unsigned int Image[],
int Columns,
int Rows,
int x,
int y)
623 int i, CurrentLocation;
625 CurrentLocation = (y << 5) + (x >> 3);
626 halLcdSetAddress(CurrentLocation);
627 for (i = 0; i < Rows; i++)
629 halLcdDrawCurrentLine(Image, Columns);
631 CurrentLocation += 0x20;
632 halLcdSetAddress(CurrentLocation);
646 void halLcdDrawCurrentLine(
const unsigned int *value,
int Columns)
651 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
652 for (i = 0; i < 3; i++)
654 while (!(UCB2IFG & UCTXIFG)) ;
655 UCB2TXBUF = Draw_Block_Value_Macro[i];
657 while (UCB2STAT & UCBUSY) ;
658 LCD_CS_RST_OUT |= LCD_CS_PIN;
659 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
660 while (!(UCB2IFG & UCTXIFG)) ;
661 UCB2TXBUF = Draw_Block_Value_Macro[3];
664 for (i = 0; i < Columns; i++)
667 if (LcdTableAddress >=
sizeof(LCD_MEM)){
670 LCD_MEM[LcdTableAddress++] = *value;
671 while (!(UCB2IFG & UCTXIFG)) ;
672 UCB2TXBUF = (*value) >> 8;
673 while (!(UCB2IFG & UCTXIFG)) ;
674 UCB2TXBUF = (*value++) & 0xFF;
677 while (UCB2STAT & UCBUSY) ;
678 LCD_CS_RST_OUT |= LCD_CS_PIN;
695 void halLcdClearImage(
int Columns,
int Rows,
int x,
int y)
697 int i, j, k, Current_Location;
699 Current_Location = (y << 5) + (x >> 3);
700 halLcdSetAddress(Current_Location);
702 for (i = 0; i < Rows; i++)
705 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
706 for (k = 0; k < 3; k++)
708 while (!(UCB2IFG & UCTXIFG)) ;
709 UCB2TXBUF = Draw_Block_Value_Macro[k];
711 while (UCB2STAT & UCBUSY) ;
712 LCD_CS_RST_OUT |= LCD_CS_PIN;
713 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
714 while (!(UCB2IFG & UCTXIFG)) ;
715 UCB2TXBUF = Draw_Block_Value_Macro[3];
718 for (j = 0; j < Columns; j++)
720 LCD_MEM[LcdTableAddress++] = 0x00;
721 while (!(UCB2IFG & UCTXIFG)) ;
723 while (!(UCB2IFG & UCTXIFG)) ;
726 while (UCB2STAT & UCBUSY) ;
727 LCD_CS_RST_OUT |= LCD_CS_PIN;
729 Current_Location += 0x20;
730 halLcdSetAddress(Current_Location);
743 void halLcdDrawTextBlock(
unsigned int Value)
747 Draw_Block_Value_Macro[4] = Value >> 8;
748 Draw_Block_Value_Macro[5] = Value & 0xFF;
749 LCD_MEM[LcdTableAddress] = Value;
751 halLcdSendCommand(Draw_Block_Value_Macro);
754 temp = LcdAddress >> 5;
755 temp = temp + (temp << 4);
757 LcdTableAddress = temp + (LcdAddress & 0x1F);
760 if ((LcdAddress & 0x1F) > 0x10)
761 halLcdSetAddress((LcdAddress & 0xFFE0) + 0x20);
763 if (LcdAddress >= LCD_Size)
783 void halLcdPrint(
char String[],
unsigned char TextStyle)
785 int i, j, Counter = 0, BlockValue;
786 int Address, LCD_MEM_Add, ActualAddress;
790 ActualAddress = LcdAddress;
791 Counter = LcdAddress & 0x1F;
794 while (String[i] != 0)
796 LookUpChar = fonts_lookup[String[i]];
798 for (j = 0; j < FONT_HEIGHT; j++)
800 Address = ActualAddress + j * 0x20;
804 LCD_MEM_Add = temp + (Address & 0x1F);
806 BlockValue = LCD_MEM[LCD_MEM_Add];
808 if (TextStyle & GRAYSCALE_TEXT)
810 if (TextStyle & INVERT_TEXT)
811 if (TextStyle & OVERWRITE_TEXT)
812 BlockValue = 0xAAAA - GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
814 BlockValue |= 0xAAAA - GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
816 if (TextStyle & OVERWRITE_TEXT)
817 BlockValue = GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
819 BlockValue |= GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
823 if (TextStyle & INVERT_TEXT)
824 if (TextStyle & OVERWRITE_TEXT)
825 BlockValue = 0xFFFF - fonts[LookUpChar * 13 + j];
827 BlockValue |= 0xFFFF - fonts[LookUpChar * 13 + j];
830 if (TextStyle & OVERWRITE_TEXT)
831 BlockValue = fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
833 BlockValue |= fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
835 halLcdDrawBlock(Address, BlockValue);
842 ActualAddress += 0x20 * FONT_HEIGHT - 16;
843 if (ActualAddress > LCD_Last_Pixel - 0x20 * FONT_HEIGHT)
850 halLcdSetAddress(ActualAddress);
872 void halLcdPrintXY(
char String[],
int x,
int y,
unsigned char TextStyle)
875 halLcdSetAddress((y << 5) + (x >> 3));
876 halLcdPrint(String, TextStyle);
894 void halLcdPrintLine(
char String[],
unsigned char Line,
unsigned char TextStyle)
898 temp = Line * FONT_HEIGHT;
899 halLcdSetAddress(temp << 5);
900 halLcdPrint(String, TextStyle);
920 void halLcdPrintLineCol(
char String[],
unsigned char Line,
unsigned char Col,
921 unsigned char TextStyle)
925 temp = Line * FONT_HEIGHT;
929 halLcdSetAddress(temp);
930 halLcdPrint(String, TextStyle);
947 void halLcdHLine(
int x1,
int x2,
int y,
unsigned char GrayScale)
958 halLcdPixel(x, y, GrayScale);
977 void halLcdVLine(
int x,
int y1,
int y2,
unsigned char GrayScale)
988 halLcdPixel(x, y, GrayScale);
1011 void halLcdLine(
int x1,
int y1,
int x2,
int y2,
unsigned char GrayScale)
1013 int x, y, deltay, deltax, d;
1017 halLcdVLine(x1, y1, y2, GrayScale);
1021 halLcdHLine(x1, x2, y1, GrayScale);
1033 deltay = ABS(y2 - y1);
1034 deltax = ABS(x2 - x1);
1036 if (deltax >= deltay)
1038 d = (deltay << 1) - deltax;
1041 halLcdPixel(x, y, GrayScale);
1046 d += ((deltay - deltax) << 1);
1054 d = (deltax << 1) - deltay;
1057 halLcdPixel(x, y, GrayScale);
1062 d += ((deltax - deltay) << 1);
1086 void halLcdCircle(
int x,
int y,
int Radius,
int GrayScale)
1088 int xx, yy, ddF_x, ddF_y, f;
1091 ddF_y = -(2 * Radius);
1096 halLcdPixel(x + xx, y + yy, GrayScale);
1097 halLcdPixel(x + xx, y - yy, GrayScale);
1098 halLcdPixel(x - xx, y + yy, GrayScale);
1099 halLcdPixel(x - xx, y - yy, GrayScale);
1100 halLcdPixel(x + yy, y + xx, GrayScale);
1101 halLcdPixel(x + yy, y - xx, GrayScale);
1102 halLcdPixel(x - yy, y + xx, GrayScale);
1103 halLcdPixel(x - yy, y - xx, GrayScale);
1115 halLcdPixel(x + xx, y + yy, GrayScale);
1116 halLcdPixel(x + xx, y - yy, GrayScale);
1117 halLcdPixel(x - xx, y + yy, GrayScale);
1118 halLcdPixel(x - xx, y - yy, GrayScale);
1119 halLcdPixel(x + yy, y + xx, GrayScale);
1120 halLcdPixel(x + yy, y - xx, GrayScale);
1121 halLcdPixel(x - yy, y + xx, GrayScale);
1122 halLcdPixel(x - yy, y - xx, GrayScale);
1138 void halLcdScrollRow(
int y)
1140 int i, Address, LcdTableAddressTemp;
1145 halLcdSetAddress(Address);
1148 LcdTableAddressTemp = y + (y << 4);
1149 temp = ((LCD_MEM[LcdTableAddressTemp] & 0x0003) << 14);
1151 for (i = 0; i < 0x10; i++)
1152 halLcdDrawCurrentBlock(((LCD_MEM[LcdTableAddressTemp + i] & 0xFFFC) >> 2) \
1153 + ((LCD_MEM[LcdTableAddressTemp + i + 1] & 0x0003) << 14));
1155 halLcdDrawCurrentBlock(((LCD_MEM[LcdTableAddressTemp + 0x10] & 0xFFFC) >> 2) + temp);
1173 void halLcdHScroll(
int yStart,
int yEnd)
1177 for (i = yStart; i < yEnd + 1; i++)
1189 void halLcdScrollLine(
int Line)
1193 Row = Line * FONT_HEIGHT;
1195 for (i = Row; i < Row + FONT_HEIGHT; i++)
#define __delay_cycles(x)
__delay_cycles() is an intrinsic IAR call; however, we have explicity disallowed it since it is too s...