-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_OLED.c
391 lines (296 loc) · 8.31 KB
/
Adafruit_OLED.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Standard includes
#include <string.h>
// Driverlib includes
#include "hw_types.h"
#include "hw_memmap.h"
#include "hw_common_reg.h"
#include "hw_ints.h"
#include "gpio.h"
#include "spi.h"
#include "rom.h"
#include "rom_map.h"
#include "utils.h"
#include "prcm.h"
#include "uart.h"
#include "interrupt.h"
// Common interface includes
#include "uart_if.h"
#include "pinmux.h"
#include "Adafruit_SSD1351.h"
#define TR_BUFF_SIZE 1000
static unsigned char g_ucTxBuff[TR_BUFF_SIZE];
static unsigned char ucTxBuffNdx;
//*****************************************************************************
void writeCommand(unsigned char c) {
//TODO 1
/* Write a function to send a command byte c to the OLED via
* SPI.
*/
//
// Set Tx buffer index
//
ucTxBuffNdx = 0;
volatile unsigned long delay;
GPIOPinWrite(GPIOA1_BASE, 0x1, 0); //to send command, DC pin=0
//
// Send the string to slave. Chip Select(CS) needs to be
// asserted at start of transfer and deasserted at the end.
//
//
// Enable SPI for communication
//
SPIEnable(GSPI_BASE);
SPITransfer(GSPI_BASE,g_ucTxBuff,0,1,0);
//
// Enable Chip select
//
GPIOPinWrite(GPIOA0_BASE, 0x40, CS_ENABLE);
//MAP_SPICSEnable(GSPI_BASE);
//
// Push the character over SPI
//
SPIDataPut(GSPI_BASE,c);
for(delay=0; delay < 100; delay=delay+1);
//delay(1);
//
// Clean up the receive register into a dummy
// variable
//
//MAP_SPIDataGet(GSPI_BASE,&ulDummy);
//
// Disable chip select
//
GPIOPinWrite(GPIOA0_BASE, 0x40, CS_DISABLE);
SPIDisable(GSPI_BASE);
//MAP_SPICSDisable(GSPI_BASE);
}
//*****************************************************************************
void writeData(unsigned char c) {
//TODO 2
/* Write a function to send a data byte c to the OLED via
* SPI.
*/
// Set Tx buffer index
//
ucTxBuffNdx = 0;
volatile unsigned long delay;
GPIOPinWrite(GPIOA1_BASE, 0x1, 0x1); //DC
//
// Send the string to slave. Chip Select(CS) needs to be
// asserted at start of transfer and deasserted at the end.
//
SPIEnable(GSPI_BASE);
SPITransfer(GSPI_BASE,g_ucTxBuff,0,1,0);
//
// Enable Chip select
//
GPIOPinWrite(GPIOA0_BASE, 0x40, CS_ENABLE);
//SPICSEnable(GSPI_BASE);
//
// Push the character over SPI
//
SPIDataPut(GSPI_BASE,c);
//for(i = 0; i < 100; i=i+1);
for(delay=0; delay < 100; delay=delay+1);
//delay(1);
//
// Clean up the receive register into a dummy
// variable
//
// MAP_SPIDataGet(GSPI_BASE,&ulDummy);
//
// Disable chip select
//
GPIOPinWrite(GPIOA0_BASE, 0x40, CS_DISABLE);
//MAP_SPICSDisable(GSPI_BASE);
SPIDisable(GSPI_BASE);
}
//*****************************************************************************
void Adafruit_Init(void){
//TODO 3
/* NOTE: This function assumes that the RESET pin of the
* OLED has been wired to GPIO28, pin 18 (P2.2). If you
* use a different pin for the OLED reset, then you should
* update the GPIOPinWrite commands below that set RESET
* high or low.
*/
volatile unsigned long delay;
GPIOPinWrite(GPIOA3_BASE, 0x10, 0); // RESET = RESET_LOW
for(delay=0; delay<100; delay=delay+1);// delay minimum 100 ns
GPIOPinWrite(GPIOA3_BASE, 0x10, 0x10); // RESET = RESET_HIGH
// Initialization Sequence
writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock
writeData(0x12);
writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock
writeData(0xB1);
writeCommand(SSD1351_CMD_DISPLAYOFF); // 0xAE
writeCommand(SSD1351_CMD_CLOCKDIV); // 0xB3
writeCommand(0xF1); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
writeCommand(SSD1351_CMD_MUXRATIO);
writeData(127);
writeCommand(SSD1351_CMD_SETREMAP);
writeData(0x74);
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(0x00);
writeData(0x7F);
writeCommand(SSD1351_CMD_SETROW);
writeData(0x00);
writeData(0x7F);
writeCommand(SSD1351_CMD_STARTLINE); // 0xA1
if (SSD1351HEIGHT == 96) {
writeData(96);
} else {
writeData(0);
}
writeCommand(SSD1351_CMD_DISPLAYOFFSET); // 0xA2
writeData(0x0);
writeCommand(SSD1351_CMD_SETGPIO);
writeData(0x00);
writeCommand(SSD1351_CMD_FUNCTIONSELECT);
writeData(0x01); // internal (diode drop)
//writeData(0x01); // external bias
// writeCommand(SSSD1351_CMD_SETPHASELENGTH);
// writeData(0x32);
writeCommand(SSD1351_CMD_PRECHARGE); // 0xB1
writeCommand(0x32);
writeCommand(SSD1351_CMD_VCOMH); // 0xBE
writeCommand(0x05);
writeCommand(SSD1351_CMD_NORMALDISPLAY); // 0xA6
writeCommand(SSD1351_CMD_CONTRASTABC);
writeData(0xC8);
writeData(0x80);
writeData(0xC8);
writeCommand(SSD1351_CMD_CONTRASTMASTER);
writeData(0x0F);
writeCommand(SSD1351_CMD_SETVSL );
writeData(0xA0);
writeData(0xB5);
writeData(0x55);
writeCommand(SSD1351_CMD_PRECHARGE2);
writeData(0x01);
writeCommand(SSD1351_CMD_DISPLAYON); //--turn on oled panel
}
/***********************************/
void goTo(int x, int y) {
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
// set x and y coordinate
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(SSD1351WIDTH-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(SSD1351HEIGHT-1);
writeCommand(SSD1351_CMD_WRITERAM);
}
unsigned int Color565(unsigned char r, unsigned char g, unsigned char b) {
unsigned int c;
c = r >> 3;
c <<= 6;
c |= g >> 2;
c <<= 5;
c |= b >> 3;
return c;
}
void fillScreen(unsigned int fillcolor) {
fillRect(0, 0, SSD1351WIDTH, SSD1351HEIGHT, fillcolor);
}
/**************************************************************************/
/*!
@brief Draws a filled rectangle using HW acceleration
*/
/**************************************************************************/
void fillRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int fillcolor)
{
unsigned int i;
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// Y bounds check
if (y+h > SSD1351HEIGHT)
{
h = SSD1351HEIGHT - y - 1;
}
// X bounds check
if (x+w > SSD1351WIDTH)
{
w = SSD1351WIDTH - x - 1;
}
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x+w-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y+h-1);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (i=0; i < w*h; i++) {
writeData(fillcolor >> 8);
writeData(fillcolor);
}
}
void drawFastVLine(int x, int y, int h, unsigned int color) {
unsigned int i;
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// X bounds check
if (y+h > SSD1351HEIGHT)
{
h = SSD1351HEIGHT - y - 1;
}
if (h < 0) return;
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y+h-1);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (i=0; i < h; i++) {
writeData(color >> 8);
writeData(color);
}
}
void drawFastHLine(int x, int y, int w, unsigned int color) {
unsigned int i;
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// X bounds check
if (x+w > SSD1351WIDTH)
{
w = SSD1351WIDTH - x - 1;
}
if (w < 0) return;
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x+w-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (i=0; i < w; i++) {
writeData(color >> 8);
writeData(color);
}
}
void drawPixel(int x, int y, unsigned int color)
{
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
if ((x < 0) || (y < 0)) return;
goTo(x, y);
writeData(color >> 8);
writeData(color);
}
void invert(char v) {
if (v) {
writeCommand(SSD1351_CMD_INVERTDISPLAY);
} else {
writeCommand(SSD1351_CMD_NORMALDISPLAY);
}
}