avr-game

view dotmatrix.c @ 0:9db99968b55e

initial commit, screen test working
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 09 Sep 2014 04:40:47 +0300
parents
children 363acf3e61d4
line source
1 #include <util/delay.h>
2 #include "dotmatrix.h"
4 enum { CMD, DATA };
6 #define CMD_DISPCTL(x) (((x) & 7) | (1 << 3))
7 #define CMD_FUNCSET(x) (((x) & 7) | (1 << 5))
8 #define CMD_SETY(x) (((x) & 7) | (1 << 6))
9 #define CMD_SETX(x) (((x) & 0x7f) | (1 << 7))
11 #define CMD_EXT_BIAS(x) (((x) & 7) | (1 << 4))
12 #define CMD_EXT_VOP(x) (((x) & 0x7f) | (1 << 7))
14 #define FSET_BASIC 0
15 #define FSET_EXTENDED 1
16 #define FSET_VERTICAL 2
17 #define FSET_POWERDOWN 3
19 #define DCTL_BLANK 0
20 #define DCTL_NORMAL 4
21 #define DCTL_ALL 1
22 #define DCTL_INVERSE 5
24 #define FRAME_BYTES ((DM_WIDTH / 8) * DM_HEIGHT)
26 #define PINMASK \
27 ((1 << DM_PIN_CLK) | \
28 (1 << DM_PIN_DIN) | \
29 (1 << DM_PIN_DC) | \
30 (1 << DM_PIN_CE) | \
31 (1 << DM_PIN_RST))
33 #define SET(var, bit) ((var) |= 1 << (bit))
34 #define CLR(var, bit) ((var) &= ~(1 << (bit)))
36 #define NOP() asm volatile ("nop")
38 static void start_send(void);
39 static void send(int type, unsigned char c);
40 static void stop_send(void);
41 static void send_byte(int type, unsigned char c);
43 static int mode;
45 int dm_init(void)
46 {
47 int vop;
49 DM_DDR |= PINMASK; /* set used pins as outputs */
50 SET(DM_PORT, DM_PIN_CE);
51 SET(DM_PORT, DM_PIN_RST);
53 /* reset the lcd controller */
54 CLR(DM_PORT, DM_PIN_RST);
55 NOP();
56 SET(DM_PORT, DM_PIN_RST);
58 /* set Vop (???) */
59 start_send();
60 send(CMD, CMD_FUNCSET(FSET_EXTENDED));
61 send(CMD, CMD_EXT_VOP(80));
63 /* select function set (vertical addressing, basic set) */
64 send(CMD, CMD_FUNCSET(FSET_VERTICAL | FSET_BASIC));
66 /* enable display (normal mode) */
67 mode = DCTL_NORMAL;
68 send(CMD, CMD_DISPCTL(mode));
69 stop_send();
71 dm_clear(0);
73 return 0;
74 }
76 /* fill the screen with a test pattern */
77 void dm_test(void)
78 {
79 static const unsigned char pattern[] = {
80 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
81 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
82 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
83 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
84 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
85 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
86 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
87 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f
88 };
89 int i;
91 for(i=0; i<DM_HEIGHT / 8; i++) {
92 dm_copy(0, i * 8, pattern, sizeof pattern);
93 }
94 dm_copy(0, DM_HEIGHT - 4, pattern, 6 * 4);
95 }
97 void dm_invert(void)
98 {
99 mode = (mode & 0xfe) | ((~mode) & 1);
100 send_byte(CMD, CMD_DISPCTL(mode));
101 }
103 void dm_clear(unsigned char val)
104 {
105 int i;
107 start_send();
108 send(CMD, CMD_SETY(0));
109 send(CMD, CMD_SETX(0));
111 for(i=0; i<FRAME_BYTES; i++) {
112 send(DATA, val);
113 }
114 stop_send();
115 }
117 void dm_copy(int x, int y, const unsigned char *fb, int bytes)
118 {
119 int i;
121 if(bytes <= 0 || bytes > FRAME_BYTES) {
122 bytes = FRAME_BYTES;
123 }
125 start_send();
126 send(CMD, CMD_SETY(x / 8));
127 send(CMD, CMD_SETX(y));
129 for(i=0; i<bytes; i++) {
130 send(DATA, *fb++);
131 }
132 stop_send();
133 }
135 static void start_send(void)
136 {
137 /* start by pulling CE low */
138 CLR(DM_PORT, DM_PIN_CE);
140 /* enable SPI, master, clock/4, setup-falling, sample-rising, MSB-first */
141 SPCR = (1 << SPE) | (1 << MSTR);
142 }
144 static void stop_send(void)
145 {
146 SPCR = 0;
148 /* end transfer by pulling CE high again */
149 SET(DM_PORT, DM_PIN_CE);
150 }
152 static void send(int type, unsigned char c)
153 {
154 int i;
155 unsigned char bmask;
157 if(type == CMD) {
158 CLR(DM_PORT, DM_PIN_DC);
159 } else {
160 SET(DM_PORT, DM_PIN_DC);
161 }
163 SPDR = c;
164 while(!(SPSR & (1 << SPIF)));
166 /* bitbang */
167 /*
168 bmask = 1 << 7;
169 for(i=0; i<8; i++) {
170 CLR(DM_PORT, DM_PIN_CLK);
172 if(c & bmask) {
173 SET(DM_PORT, DM_PIN_DIN);
174 } else {
175 CLR(DM_PORT, DM_PIN_DIN);
176 }
177 bmask >>= 1;
179 SET(DM_PORT, DM_PIN_CLK);
180 NOP();
181 }
182 */
183 }
185 static void send_byte(int type, unsigned char c)
186 {
187 start_send();
188 send(type, c);
189 stop_send();
190 }