OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [board/] [oledtest.c] - Blame information for rev 35

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 35 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    oledtest.c
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     To see whether or not we can display an image onto the OLEDrgb
8
//              PMod.  This program runs on the ZipCPU internal to the FPGA,
9
//      and commands the OLEDrgb to power on, reset, initialize, and then to
10
//      display an alternating pair of images onto the display.
11
//
12
//
13
// Creator:     Dan Gisselquist, Ph.D.
14
//              Gisselquist Technology, LLC
15
//
16
////////////////////////////////////////////////////////////////////////////////
17
//
18
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
19
//
20
// This program is free software (firmware): you can redistribute it and/or
21
// modify it under the terms of  the GNU General Public License as published
22
// by the Free Software Foundation, either version 3 of the License, or (at
23
// your option) any later version.
24
//
25
// This program is distributed in the hope that it will be useful, but WITHOUT
26
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
27
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28
// for more details.
29
//
30
// You should have received a copy of the GNU General Public License along
31
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
32
// target there if the PDF file isn't present.)  If not, see
33
// <http://www.gnu.org/licenses/> for a copy.
34
//
35
// License:     GPL, v3, as defined and found on www.gnu.org,
36
//              http://www.gnu.org/licenses/gpl.html
37
//
38
//
39
////////////////////////////////////////////////////////////////////////////////
40
//
41
//
42
 
43 34 dgisselq
#include "artyboard.h"
44
#include "zipsys.h"
45
 
46
void    idle_task(void) {
47
        while(1)
48
                zip_idle();
49
}
50
 
51
extern int      splash[], mug[];
52
 
53
#define OLED_PMODEN             0x0010001
54
#define OLED_PMODEN_OFF         0x0010000
55
#define OLED_IOPWR              OLED_PMODEN
56
#define OLED_VCCEN              0x0020002
57
#define OLED_VCC_DISABLE        0x0020000
58
#define OLED_RESET              0x0040000
59
#define OLED_RESET_CLR          0x0040004
60 35 dgisselq
#define OLED_FULLPOWER          (OLED_PMODEN|OLED_VCCEN|OLED_RESET_CLR)
61 34 dgisselq
#define OLED_POWER_DOWN         (OLED_PMODEN_OFF|OLED_VCC_DISABLE)
62
#define OLED_BUSY               1
63
#define OLED_DISPLAYON          0x0af
64
 
65
 
66
#define MICROSECOND             (CLOCKFREQ_HZ/1000000)
67
 
68
#define OLED_DISPLAY_OFF
69
 
70
 
71 35 dgisselq
 
72
/*
73
 * timer_delay()
74
 *
75
 * Using the timer peripheral, delay by a given number of counts.  We'll sleep
76
 * during this delayed time, and wait on an interrupt to wake us.  As a result,
77
 * this will only work from supervisor mode.
78
 */
79 34 dgisselq
void    timer_delay(int counts) {
80
        // Clear the PIC.  We want to exit from here on timer counts alone
81
        zip->pic = CLEARPIC;
82
 
83
        if (counts > 10) {
84
                // Set our timer to count down the given number of counts
85
                zip->tma = counts;
86
                zip->pic = EINT(SYSINT_TMA);
87
                zip_rtu();
88
                zip->pic = CLEARPIC;
89
        } // else anything less has likely already passed
90
}
91
 
92 35 dgisselq
 
93 34 dgisselq
void oled_clear(void);
94
 
95 35 dgisselq
/*
96
 * The following outlines a series of commands to send to the OLEDrgb as part
97
 * of an initialization sequence.  The sequence itself was taken from the
98
 * MPIDE demo.  Single byte numbers in the sequence are just that: commands
99
 * to send 8-bit values across the port.  17-bit values with the 16th bit
100
 * set send two bytes (bits 15-0) to the port.  The OLEDrgb treats these as
101
 * commands (first byte) with an argument (the second byte).
102
 */
103
const int       init_sequence[] = {
104
        //  Unlock commands
105
        0x01fd12,
106
        //  Display off
107
        0x0ae,
108
        //  Set remap and data format
109
        0x01a072,
110
        //  Set the start line
111
        0x01a100,
112
        //  Set the display offset
113
        0x01a200,
114
        //  Normal display mode
115
        0x0000a4,
116
        //  Set multiplex ratio
117
        0x01a83f,
118
        //  Set master configuration:
119
        //      Use External VCC
120
        0x01ad8e,
121
        //  Disable power save mode
122
        0x01b00b,
123
        //  Set phase length
124
        0x01b131,
125
        //  Set clock divide
126
        0x01b3f0,
127
        //  Set Second Pre-change Speed For ColorA
128
        0x018a64,
129
        //  5l) Set Set Second Pre-charge Speed of Color B
130
        0x018b78,
131
        //  5m) Set Second Pre-charge Speed of Color C
132
        0x018c64,
133
        //  5n) Set Pre-Charge Voltage
134
        0x01bb3a,
135
        //  50) Set VCOMH Deselect Level
136
        0x01be3e,
137
        //  5p) Set Master Current
138
        0x018706,
139
        //  5q) Set Contrast for Color A
140
        0x018191,
141
        //  5r) Set Contrast for Color B
142
        0x018250,
143
        //  5s) Set Contrast for Color C
144
        0x01837D,
145
        //  disable scrolling
146
        0x02e
147
};
148
 
149
/*
150
 * oled_init()
151
 *
152
 * This initializes and starts up the OLED.  While it sounds important, really
153
 * the majority of the work necessary to do this is really captured in the
154
 * init_sequence[] above.  This just primarily works to send that sequence to
155
 * the PMod.
156
 *
157
 * We should be able to do all of this with the DMA: wait for an OLEDrgb not
158
 * busy interrupt, send one value, repeat until done.  For now ... we'll just
159
 * leave that as an advanced exercise.
160
 *
161
 */
162 34 dgisselq
void    oled_init(void) {
163
        int     i;
164
 
165
        for(i=0; i<sizeof(init_sequence); i++) {
166
                while(sys->io_oled.o_ctrl & OLED_BUSY)
167
                        ;
168
                sys->io_oled.o_ctrl = init_sequence[i];
169
        }
170
 
171
        oled_clear();
172
 
173 35 dgisselq
        // Wait 5ms
174
        timer_delay(CLOCKFREQ_HZ/200);
175 34 dgisselq
 
176
        // Turn on VCC and wait 100ms
177
        sys->io_oled.o_data = OLED_VCCEN;
178
        // Wait 100 ms
179
        timer_delay(CLOCKFREQ_HZ/10);
180
 
181
        // Send Display On command
182 35 dgisselq
        sys->io_oled.o_ctrl = OLED_DISPLAYON;
183
}
184 34 dgisselq
 
185 35 dgisselq
/*
186
 * oled_clear()
187
 *
188
 * This should be fairly self-explanatory: it clears (sets to black) all of the
189
 * graphics memory on the OLED.
190
 *
191
 * What may not be self-explanatory is that to send any more than three bytes
192
 * using our interface you need to send the first three bytes in o_ctrl,
193
 * and set the next bytes (up to four) in o_a.  (Another four can be placed in
194
 * o_b.)  When the word is written to o_ctrl, the command goes over the wire
195
 * and o_a and o_b are reset.  Hence we set o_a first, then o_ctrl.  Further,
196
 * the '4' in the top nibble of o_ctrl indicates that we are sending 5-bytes
197
 * (4+5), so the OLEDrgb should see: 0x25,0x00,0x00,0x5f,0x3f.
198
 */
199 34 dgisselq
void    oled_clear(void) {
200
        while(sys->io_oled.o_ctrl & OLED_BUSY)
201
                ;
202
        sys->io_oled.o_a = 0x5f3f0000;
203
        sys->io_oled.o_ctrl = 0x40250000;
204
        while(sys->io_oled.o_ctrl & OLED_BUSY)
205
                ;
206
}
207
 
208 35 dgisselq
/*
209
 * oled_fill
210
 *
211
 * Similar to oled_clear, this fills a rectangle with a given pixel value.
212
 */
213 34 dgisselq
void    oled_fill(int c, int r, int w, int h, int pix) {
214
        int     ctrl; // We'll send this value out the control/command port
215
 
216
        if (c > 95) c = 95;
217
        if (c <  0) c =  0;
218
        if (r > 63) r = 63;
219
        if (r <  0) r =  0;
220
        if (w <  0) w = 0;
221
        if (h <  0) h = 0;
222
        if (c+w > 95) w = 95-c;
223
        if (r+h > 63) h = 63-r;
224
 
225
        // Enable a rectangle to fill
226
        while(sys->io_oled.o_ctrl & OLED_BUSY)
227
                ;
228
        sys->io_oled.o_ctrl = 0x12601;
229
 
230 35 dgisselq
        //
231 34 dgisselq
        // Now, let's build the actual copy command
232 35 dgisselq
        //
233
        // This is an 11 byte command, consisting of the 0x22, followed by
234
        // the top left column and row of our rectangle, and then the bottom
235
        // right column and row.  That's the first five bytes.  The next six
236
        // bytes are the color of the border and the color of the fill.
237
        // Here, we set both colors to be identical.
238
        // 
239 34 dgisselq
        ctrl = 0xa0220000 | ((c&0x07f)<<8) | (r&0x03f);
240
        sys->io_oled.o_a = (((c+w)&0x07f)<<24) | (((r+h)&0x03f)<<16);
241
        sys->io_oled.o_a|= ((pix >> 11) & 0x01f)<< 9;
242
        sys->io_oled.o_a|= ((pix >>  5) & 0x03f)    ;
243 35 dgisselq
        sys->io_oled.o_b = ((pix      ) & 0x01f)<<25;
244
        sys->io_oled.o_b|= ((pix >> 11) & 0x01f)<<17;
245 34 dgisselq
        sys->io_oled.o_b|= ((pix >>  5) & 0x03f)<< 8;
246
        sys->io_oled.o_b|= ((pix      ) & 0x01f)<< 1;
247
 
248 35 dgisselq
        // Make certain we had finished with the port (we should've by now)
249 34 dgisselq
        while(sys->io_oled.o_ctrl & OLED_BUSY)
250
                ;
251
 
252 35 dgisselq
        // and send our new command.  Note that o_a and o_b were already set
253
        // ahead of time, and are only now being sent together with this
254
        // command.
255 34 dgisselq
        sys->io_oled.o_ctrl = ctrl;
256
 
257
        // To be nice to whatever routine follows, we'll wait 'til the port
258
        // is clear again.
259
        while(sys->io_oled.o_ctrl & OLED_BUSY)
260
                ;
261
}
262
 
263 35 dgisselq
 
264
/*
265
 * entry()
266
 *
267
 * In all (current) ZipCPU programs, the programs start with an entry()
268
 * function that takes no arguments.  The actual bootup entry can be found
269
 * in the bootstram directory, but that calls us here.
270
 *
271
 */
272 34 dgisselq
void    entry(void) {
273 35 dgisselq
 
274
        // Since we'll be returning to userspace via zip_rtu() in order to
275
        // wait for an interrupt, let's at least place a valid program into
276
        // userspace to run: the idle_task.
277 34 dgisselq
        unsigned        user_regs[16];
278
        for(int i=0; i<15; i++)
279
                user_regs[i] = 0;
280
        user_regs[15] = (unsigned int)idle_task;
281
        zip_restore_context(user_regs);
282
 
283 35 dgisselq
        // Clear the PIC.  We'll come back and use it later.  We clear it here
284
        // partly in order to avoid a race condition later.
285 34 dgisselq
        zip->pic = CLEARPIC;
286
 
287 35 dgisselq
        // Wait till we've had power for at least a quarter second
288
        if (0) {
289
                // While this appears to do the task quite nicely, it leaves
290
                // the master_ce line high within the CPU, and so it generates
291
                // a whole lot of debug information in our Verilator simulation,
292
                // busmaster_tb.
293 34 dgisselq
                int pwrcount = sys->io_pwrcount;
294
                do {
295
                        pwrcount = sys->io_pwrcount;
296
                } while((pwrcount>0)&&(pwrcount < CLOCKFREQ_HZ/4));
297
        } else {
298 35 dgisselq
                // By using the timer and sleeping instead, the simulator can
299
                // be made to run a *lot* faster, with a *lot* less debugging
300
                // ... junk.
301 34 dgisselq
                int pwrcount = sys->io_pwrcount;
302
                if ((pwrcount > 0)&&(pwrcount < CLOCKFREQ_HZ/4)) {
303
                        pwrcount = CLOCKFREQ_HZ/4 - pwrcount;
304
                        timer_delay(pwrcount);
305
                }
306
        }
307
 
308
 
309
        // If the OLED is already powered, such as might be the case if
310
        // we rebooted but the board was still hot, shut it down
311
        if (sys->io_oled.o_data & 0x07) {
312
                sys->io_oled.o_data = OLED_VCC_DISABLE;
313
                // Wait 100 ms
314
                timer_delay(CLOCKFREQ_HZ/10);
315
                // Shutdown the entire devices power
316
                sys->io_oled.o_data = OLED_POWER_DOWN;
317
                // Wait 100 ms
318
                timer_delay(CLOCKFREQ_HZ/10);
319
 
320
                // Now let's try to restart it
321
        }
322
 
323
        // 1. Power up the OLED by applying power to VCC
324
        //      This means we need to apply power to both the VCCEN line as well
325
        //      as the PMODEN line.  We'll also set the reset line low, so the
326
        //      device starts in a reset condition.
327
        sys->io_oled.o_data = OLED_PMODEN|OLED_RESET_CLR;
328
        timer_delay(4*MICROSECOND);
329
        sys->io_oled.o_data = OLED_RESET;
330
        timer_delay(4*MICROSECOND);
331
 
332
        // 2. Send the Display OFF command
333
        //      This isn't necessary, since we already pulled RESET low.
334
        //
335
        // sys->io_oled.o_ctrl = OLED_DISPLAY_OFF;
336
        //
337
 
338
        // However, we must hold the reset line low for at least 3us, as per
339
        // the spec.  We may also need to wait another 2us after that.  Let's
340
        // hold reset low for 4us here.
341
        timer_delay(4*MICROSECOND);
342
 
343
        // Clear the reset condition.
344
        sys->io_oled.o_data = OLED_RESET_CLR;
345
        // Wait another 4us.
346
        timer_delay(4*MICROSECOND);
347
 
348
        // 3. Initialize the display to the default settings
349
        //      This just took place during the reset cycle we just completed.
350
        //
351
        oled_init();
352
 
353
        // 4. Clear screen
354 35 dgisselq
        // 5. Apply voltage
355
        // 6. Turn on display
356
        // 7. Wait 100ms
357
        //      We already stuffed this command sequence into the oled_init,
358
        //      so we're good here.
359 34 dgisselq
 
360
        while(1) {
361 35 dgisselq
                sys->io_ledctrl = 0x0f0;
362 34 dgisselq
 
363
                sys->io_oled.o_ctrl = OLED_DISPLAYON;
364
 
365
                oled_clear();
366
 
367 35 dgisselq
                // Let's start our writes at the top left of the GDDRAM
368
                // (screen memory)
369 34 dgisselq
                while(sys->io_oled.o_ctrl & OLED_BUSY)
370
                        ;
371 35 dgisselq
                sys->io_oled.o_ctrl = 0x2015005f; // Sets column min/max address
372 34 dgisselq
 
373
                while(sys->io_oled.o_ctrl & OLED_BUSY)
374
                        ;
375 35 dgisselq
                sys->io_oled.o_ctrl = 0x2075003f; // Sets row min/max address
376 34 dgisselq
 
377
                // Now ... finally ... we can send our image.
378
                for(int i=0; i<6144; i++) {
379
                        while(sys->io_oled.o_ctrl & OLED_BUSY)
380
                                ;
381
                        sys->io_oled.o_data = splash[i];
382
                }
383 35 dgisselq
 
384
                // Wait 25 seconds.  The LEDs are for a fun effect.
385
                sys->io_ledctrl = 0x0f1;
386 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
387 35 dgisselq
                sys->io_ledctrl = 0x0f3;
388 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
389 35 dgisselq
                sys->io_ledctrl = 0x0f7;
390 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
391 35 dgisselq
                sys->io_ledctrl = 0x0ff;
392 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
393 35 dgisselq
                sys->io_ledctrl = 0x0fe;
394 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
395
 
396
 
397 35 dgisselq
                // Display a second image.
398
                sys->io_ledctrl = 0x0fc;
399 34 dgisselq
                for(int i=0; i<6144; i++) {
400
                        while(sys->io_oled.o_ctrl & OLED_BUSY)
401
                                ;
402
                        sys->io_oled.o_data = mug[i];
403
                }
404
 
405 35 dgisselq
                // Leave this one in effect for 5 seconds only.
406
                sys->io_ledctrl = 0x0f8;
407 34 dgisselq
                timer_delay(CLOCKFREQ_HZ*5);
408
        }
409
 
410 35 dgisselq
        // We'll never get here, so this line is really just for form.
411 34 dgisselq
        zip_halt();
412
}
413
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.