Hackio-Credits.
[hackover2013-badge-firmware.git] / badge / ui / display.c
1 #include "display.h"
2
3 #include <core/ssp/ssp.h>
4 #include <core/gpio/gpio.h>
5 #include <core/systick/systick.h>
6 #include <sysdefs.h>
7 #include <lpc134x.h>
8
9 #ifdef R0KET
10
11 #include <r0ketports.h>
12
13 #define CS_LOW() gpioSetValue(RB_LCD_CS , 0)
14 #define CS_HIGH() gpioSetValue(RB_LCD_CS , 1)
15 #define RST_LOW() gpioSetValue(RB_LCD_RST, 0)
16 #define RST_HIGH() gpioSetValue(RB_LCD_RST, 1)
17
18 #else
19
20 #include <badge/pinconfig.h>
21
22 #define CS_LOW() gpioSetValue(HOB_PORT(HOB_LCD_CS ), HOB_PIN(HOB_LCD_CS ), 0)
23 #define CS_HIGH() gpioSetValue(HOB_PORT(HOB_LCD_CS ), HOB_PIN(HOB_LCD_CS ), 1)
24 #define RST_LOW() gpioSetValue(HOB_PORT(HOB_LCD_RST), HOB_PIN(HOB_LCD_RST), 0)
25 #define RST_HIGH() gpioSetValue(HOB_PORT(HOB_LCD_RST), HOB_PIN(HOB_LCD_RST), 1)
26
27 #endif
28
29 #ifdef CFG_USBMSC
30 #include <core/usbhid-rom/usbmsc.h>
31
32 static uint32_t usbintstatus;
33 #endif
34
35 static void lcd_select() {
36 #ifdef CFG_USBMSC
37 if(usbMSCenabled) {
38 usbintstatus = USB_DEVINTEN;
39 USB_DEVINTEN = 0;
40 }
41 #endif
42
43 /* the LCD requires 9-Bit frames */
44 uint32_t configReg = ( SSP_SSP0CR0_DSS_9BIT // Data size = 9-bit
45 | SSP_SSP0CR0_FRF_SPI // Frame format = SPI
46 | SSP_SSP0CR0_SCR_8); // Serial clock rate = 8
47 SSP_SSP0CR0 = configReg;
48
49 CS_LOW();
50 }
51
52 static void lcd_deselect() {
53 CS_HIGH();
54 /* reset the bus to 8-Bit frames that everyone else uses */
55 uint32_t configReg = ( SSP_SSP0CR0_DSS_8BIT // Data size = 8-bit
56 | SSP_SSP0CR0_FRF_SPI // Frame format = SPI
57 | SSP_SSP0CR0_SCR_8); // Serial clock rate = 8
58 SSP_SSP0CR0 = configReg;
59
60 #ifdef CFG_USBMSC
61 if(usbMSCenabled) {
62 USB_DEVINTEN = usbintstatus;
63 }
64 #endif
65 }
66
67 static inline void lcd_write(uint16_t frame) {
68 while ((SSP_SSP0SR & (SSP_SSP0SR_TNF_NOTFULL | SSP_SSP0SR_BSY_BUSY)) != SSP_SSP0SR_TNF_NOTFULL);
69 SSP_SSP0DR = frame;
70 while ((SSP_SSP0SR & (SSP_SSP0SR_BSY_BUSY|SSP_SSP0SR_RNE_NOTEMPTY)) != SSP_SSP0SR_RNE_NOTEMPTY);
71 /* clear the FIFO */
72 frame = SSP_SSP0DR;
73 }
74
75 static void lcd_write_command(uint8_t data) { lcd_write( data); }
76 static void lcd_write_data (uint8_t data) { lcd_write(0x0100 | data); }
77
78 void badge_display_init(void) {
79 sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);
80
81 CS_HIGH();
82 RST_HIGH();
83
84 systickDelay(100);
85 RST_LOW();
86 systickDelay(100);
87 RST_HIGH();
88 systickDelay(100);
89 /*
90 int id = lcdRead(220);
91
92 if(id == 14) {
93 gpioSetDir(1, 7, gpioDirection_Output);
94 gpioSetValue(1, 7, 1);
95 }
96 */
97
98 /* Small Nokia 1200 LCD docs:
99 * clear/ set
100 * on 0xae / 0xaf
101 * invert 0xa6 / 0xa7
102 * mirror-x 0xA0 / 0xA1
103 * mirror-y 0xc7 / 0xc8
104 *
105 * 0x20+x contrast (0=black - 0x2e)
106 * 0x40+x offset in rows from top (-0x7f)
107 * 0x80+x contrast? (0=black -0x9f?)
108 * 0xd0+x black lines from top? (-0xdf?)
109 *
110 */
111 lcd_select();
112
113 /* Decoded:
114 * E2: Internal reset
115 * AF: Display on/off: DON = 1
116 * A1: undefined?
117 * A4: all on/normal: DAL = 0
118 * 2F: charge pump on/off: PC = 1
119 * B0: set y address: Y[0-3] = 0
120 * 10: set x address (upper bits): X[6-4] = 0
121 */
122 static uint8_t const initseq[]= { 0xE2, 0xAF, // Display ON
123 0xA1, // Mirror-X
124 //0xc8, // mirror-y
125 0xa7, // invert (1 = black)
126 0xA4, 0x2F,
127 // 0x9f, 0x24
128 0xB0, 0x10,
129 };
130 for(uint8_t i = 0; i < sizeof(initseq); ++i){
131 lcd_write_command(initseq[i]);
132 systickDelay(5);
133 }
134
135 lcd_deselect();
136 }
137
138 void badge_framebuffer_flush(badge_framebuffer const *fb) {
139 lcd_select();
140
141 lcd_write_command(0xb0);
142 lcd_write_command(0x10);
143 lcd_write_command(0x00);
144
145
146 for(int i = 0; i < BADGE_DISPLAY_STRIPE_COUNT * BADGE_DISPLAY_WIDTH; ++i) {
147 lcd_write_data(fb->data[0][i]);
148 }
149
150 lcd_deselect();
151 }
This page took 0.0567 seconds and 5 git commands to generate.