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

Subversion Repositories openarty

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

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

powered by: WebSVN 2.1.0

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