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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [zipos/] [doorbell.c] - Blame information for rev 27

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

Line No. Rev Author Line
1 22 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    doorbell.c
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
15
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
#include "zipsys.h"
39
#include "board.h"
40
#include "ksched.h"
41
#include "kfildes.h"
42
#include "taskp.h"
43
#include "syspipe.h"
44
#include "ktraps.h"
45
#include "errno.h"
46
#include "swint.h"
47
 
48
#include "../dev/display.h"
49
#include "../dev/rtcsim.h"
50
 
51
/* Our system will need some pipes to handle ... life.  How about these:
52
 *
53
 *      rxpipe  - read()s from this pipe read from the UART
54
 *                      Interrupt fed
55
 *      txpipe  - write()s to this pipe write to the UART
56
 *                      Interrupt consumed
57
 *      keypipe - read()s from this pipe return values read by the keypad
58
 *      lcdpipe - write()s to this pipe write to the LCD display SPI port
59
 *      pwmpipe - write()s to this pipe will send values to the audio port
60
 *                      Interrupt consumed
61
 *      cmdpipe - written to by the user command task, read by the display task
62
 *              used to communicate menu status
63
 *
64
 */
65
 
66
/* We'll need some tasks as well:
67
 *      User command task
68
 *              Handles user interaction
69
 *                      Reads from pipe--either the keypad or the UARTRX pipe
70
 *                      (Might be two such tasks in the system, one for each.)
71
 *              Sets clock upon request
72
 *              Reads from a pipe (rxpipe or keypipe), Writes to the txpipe pipe
73
 *      Doorbell task
74
 *              Maintains system time on the clock      : TIME: HH:MM:SS
75
 *              Maintains system status on display      : Light is (dis/en)abled
76
 *              Transitions when the doorbell is rung to: (fixed time line)
77
 *                                                      : DOORBELL!!
78
 *              When the doorbell is clear, returns to the original task.
79
 *              ---
80
 *              Waits on events, writes to the lcdpipe and pwmpipe.
81
 *              Reads from a command pipe, so that it can handle any user menu's
82
 *                      Command pipe.  This, though, is tricky.  It requires
83
 *                      a task that can be interrupted by either an event or a
84
 *                      pipe.  Blocking is going to be more tricky ...
85
 *      Keypad task
86
 *              Normally, you might think this should be an interrupt task.
87
 *              But, it needs state in order to have timeouts and to debounce
88
 *              the input pin.  So ... let's leave this as a task.
89
 *              ---
90
 *              Waits on events(keypad/timer), writes to the keypipe
91
 *      Display task
92
 *              The display does *not* need to be written to at an interrupt
93
 *              level.  It really needs to be written to at a task level, so
94
 *              let's make a display task.
95
 *              ---
96
 *              Reads from the lcdpipe
97
 *      Real-time Clock Task
98
 *              Gets called once per second to update the real-time clock
99
 *              and to post those updates as an event to other tasks that might
100
 *              be interested in it.
101
 *              ---
102
 *              Waits on system tasks, uses two semaphores
103
 */
104
 
105
 
106
/*
107
 * Read the keypad, write the results to an output pipe
108
 */
109
// #define      KEYPAD_TASK     keypad_task_id
110
/*
111
 * Maintain a realtime clock
112
 */
113
#define RTCCLOCK_TASK   rtccclock_task_id
114
/*
115
 * Read from an incoming pipe, write results to the SPI port controlling the
116
 * display.
117
 */
118
#define DISPLAY_TASK    display_task_id
119
 
120
/*
121
 * Wait for a button press, and then based upon the clock set a light
122
 */
123
#define DOORBELL_TASK   doorbell_task_id
124
 
125
/*
126
 * Interract with any user commands, such as setting the clock, setting
127
 * nighttime (when the lights turn on) or setting daytime when only the
128
 * doorbell rings.
129
 */
130
// #define      COMMAND_TASK    command_task_id
131
#define LAST_TASK       last_task_id
132
 
133
typedef enum    {
134
#ifdef  RTCCLOCK_TASK
135
        RTCCLOCK_TASK,
136
#endif
137
#ifdef  DOORBELL_TASK
138
#ifdef  DISPLAY_TASK
139
        DOORBELL_TASK, DISPLAY_TASK,
140
#endif
141
#endif
142
#ifdef  KEYPAD_TASK
143
        KEYPAD_TASK,
144
#endif
145
#ifdef  COMMAND_TASK
146
        COMMAND_TASK,
147
#endif
148
        LAST_TASK
149
} TASKNAME;
150
 
151
 
152
void    rtctask(void),
153
        doorbell_task(void),
154
        display_task(void),
155
        keypad_task(void),
156
        command_task(void);
157
        // idle_task ... is accomplished within the kernel
158
extern  void    restore_context(int *), save_context(int *);
159
extern  SYSPIPE *rxpipe, *txpipe, *pwmpipe, *lcdpipe;
160
SYSPIPE *midpipe;
161
extern  KDEVICE *pipedev;
162
 
163
int     kntasks(void) {
164
        return LAST_TASK;
165
} void  kinit(TASKP *tasklist) {
166
#ifdef  RTCCLOCK_TASK
167
        //
168
        tasklist[RTCCLOCK_TASK]    = new_task(16, rtctask);
169
#endif
170
 
171
#ifdef  DOORBELL_TASK
172
#ifdef  DISPLAY_TASK
173 27 dgisselq
        // 13 + 10 +9(uwrite)+4(uarthex)+2(uartstr)+2(uartchr)
174 22 dgisselq
        tasklist[DOORBELL_TASK]    = new_task(64, doorbell_task);
175
        tasklist[DOORBELL_TASK]->fd[FILENO_STDOUT] = sys_malloc(sizeof(KFILDES));
176
                tasklist[DOORBELL_TASK]->fd[FILENO_STDOUT]->id = (int)lcdpipe;
177
                tasklist[DOORBELL_TASK]->fd[FILENO_STDOUT]->dev= pipedev;
178 27 dgisselq
        tasklist[DOORBELL_TASK]->fd[FILENO_STDERR] = sys_malloc(sizeof(KFILDES));
179
                tasklist[DOORBELL_TASK]->fd[FILENO_STDERR]->id = (int)txpipe;
180
                tasklist[DOORBELL_TASK]->fd[FILENO_STDERR]->dev= pipedev;
181 22 dgisselq
        tasklist[DOORBELL_TASK]->fd[FILENO_AUX] = sys_malloc(sizeof(KFILDES));
182
                tasklist[DOORBELL_TASK]->fd[FILENO_AUX]->id = (int)pwmpipe;
183
                tasklist[DOORBELL_TASK]->fd[FILENO_AUX]->dev= pipedev;
184
 
185
        //
186
        tasklist[DISPLAY_TASK] = new_task(32, display_task);
187
        tasklist[DISPLAY_TASK]->fd[FILENO_STDIN] = sys_malloc(sizeof(KFILDES));
188
                tasklist[DISPLAY_TASK]->fd[FILENO_STDIN]->id = (int)lcdpipe;
189
                tasklist[DISPLAY_TASK]->fd[FILENO_STDIN]->dev= pipedev;
190
#endif
191
#endif
192
 
193
 
194
#ifdef  KEYPAD_TASK
195
        tasklist[KEYPAD_TASK]    = new_task(16, keypad_task);
196
        tasklist[KEYPAD_TASK]->fd[FILENO_STDOUT] = sys_malloc(sizeof(KFILDES));
197
                tasklist[NMEA_TASK]->fd[FILENO_STDOUT]->id = (int)keypipe;
198
                tasklist[NMEA_TASK]->fd[FILENO_STDOUT]->dev= pipedev;
199
#endif
200
}
201
 
202
#ifdef DOORBELL_TASK
203
// #define      HALF_HOUR_S     1800    // Seconds per half hour
204
// #define      HALF_HOUR_S     180     // Seconds per three minutes--for test
205
#define HALF_HOUR_S     30      // 3 Mins is to long, here's 3 seconds
206
 
207
#include "../dev/samples.c"
208
 
209
const unsigned  dawn = 0x060000, dusk = 0x180000;
210 27 dgisselq
int     nwritten = 0, nread = 0, nstarts = 0;
211 22 dgisselq
 
212
void    shownow(unsigned now) { // Uses 10 stack slots + 8 for write()
213
        char    dmsg[9];
214
        dmsg[0] = PACK(0x1b,'[','j','T');
215
        dmsg[1] = PACK('i','m','e',':');
216
        dmsg[2] = PACK(' ',((now>>20)&0x3)+'0',
217
                        ((now>>16)&0xf)+'0',':');
218
        dmsg[3] = PACK( ((now>>12)&0xf)+'0',
219
                        ((now>> 8)&0xf)+'0',
220
                        ':',
221
                        ((now>> 4)&0xf)+'0');
222
        dmsg[4] = PACK( ((now    )&0xf)+'0',
223
                        0x1b, '[', '1');
224
        dmsg[5] = PACK(';','0','H',' ');
225
        if ((now < dawn)||(now > dusk)) {
226
                dmsg[6] = PACK('N','i','g','h');
227
                dmsg[7] = PACK('t',' ','t','i');
228
                dmsg[8] = PACK('m','e',0,0);
229
        } else {
230
                dmsg[6] = PACK('D','a','y','l');
231
                dmsg[7] = PACK('i','g','h','t');
232
                dmsg[8] = PACK('!',' ',0,0);
233
        } write(FILENO_STDOUT, dmsg, 9);
234
}
235
 
236
void    showbell(unsigned now) {        // Uses 10 stack slots + 8 for write()
237
        char    dmsg[9];
238
        dmsg[0] = PACK(0x1b,'[','j','T');
239
        dmsg[1] = PACK('i','m','e',':');
240
        dmsg[2] = PACK(' ',((now>>20)&0x3)+'0',
241
                        ((now>>16)&0xf)+'0',':');
242
        dmsg[3] = PACK( ((now>>12)&0xf)+'0',
243
                        ((now>> 8)&0xf)+'0',
244
                        ':',
245
                        ((now>> 4)&0xf)+'0');
246
        dmsg[4] = PACK( ((now    )&0xf)+'0',
247
                        0x1b, '[', '1');
248
        dmsg[5] = PACK(';','0','H',' ');
249
        dmsg[6] = PACK('D','o','o','r');
250
        dmsg[7] = PACK('b','e','l','l');
251
        dmsg[8] = PACK('!',' ',0,0);
252
        write(FILENO_STDOUT, dmsg, 9);
253
}
254
 
255
void    belllight(unsigned now) {
256
        IOSPACE *sys = (IOSPACE *)IOADDR;
257
        if ((now < dawn)||(now > dusk))
258
                sys->io_spio = 0x088; // Turn our light on
259
        else
260
                sys->io_spio = 0x80; // Turn light off
261
}
262
 
263 27 dgisselq
void    uartchr(char v) {
264
        if (write(FILENO_STDERR, &v, 1) != 1)
265
                write(FILENO_STDERR, "APPLE-PANIC", 11);
266
}
267
 
268
void    uartstr(const char *str) {
269
        int     cnt=0;
270
        while(str[cnt])
271
                cnt++;
272
        if (cnt != write(FILENO_STDERR, str, cnt))
273
                write(FILENO_STDERR, "PIPE-PANIC", 10);
274
}
275
 
276
void    uarthex(int num) {
277
        for(int ds=28; ds>=0; ds-=4) {
278
                int ch;
279
                ch = (num>>ds)&0x0f;
280
                if (ch >= 10)
281
                        ch = 'A'+ch-10;
282
                else
283
                        ch += '0';
284
                uartchr(ch);
285
        } uartstr("\r\n\0");
286
}
287
 
288 22 dgisselq
void    doorbell_task(void) {
289
        // Controls LED 0x08
290
 
291
        // Start by initializing the display to GT Gisselquist\nTechnology
292
        // write(KFD_STDOUT, disp_build_backslash,sizeof(disp_build_backslash));
293
        // write(KFD_STDOUT, disp_build_gtlogo, sizeof(disp_build_gtlogo));
294
        // write(KFD_STDOUT, disp_reset_data, sizeof(disp_reset_data));
295
        // write(KFD_STDOUT, disp_gtech_data, sizeof(disp_gtech_data));
296
 
297
        IOSPACE *sys = (IOSPACE *)IOADDR;
298
 
299
        while(1) {
300 27 dgisselq
                nread = nwritten = 0;
301 22 dgisselq
                int     event;
302
                // Initial state: doorbell is not ringing.  In this state, we
303
                // can wait forever for an event
304
                sys->io_spio = 0x080; // Turn our light off
305
                event = wait(INT_BUTTON|SWINT_PPS,-1);
306
                unsigned when = rtcclock;
307
                if (event & INT_BUTTON)
308
                        showbell(when);
309
                else if (event & SWINT_PPS)
310
                        shownow(when);
311
 
312
                while(event & INT_BUTTON) {
313
                        // Next state, the button has been pressed, the
314
                        // doorbell is ringing
315
 
316
                        // Seconds records the number of seconds since the
317
                        // button was last pressed.
318
                        int     seconds = 0;
319
 
320
                        // Check time: should we turn our light on or not?
321
                        belllight(rtcclock);
322
                        const int *sptr = sound_data;
323 27 dgisselq
                        // uartchr('N');
324 22 dgisselq
                        while(sptr < &sound_data[NSAMPLE_WORDS]) {
325
                                int     len = &sound_data[NSAMPLE_WORDS]-sptr;
326
                                if (len > 256)
327
                                        len = 256;
328
 
329 27 dgisselq
                                /*
330
                                while(len > 64) {
331
                                        write(FILENO_AUX, sptr, 64);
332
                                        sptr += 64;
333
                                        len -= 64;
334
                                }*/
335
 
336
                                // We will stall here, if the audio FIFO is full
337 22 dgisselq
                                write(FILENO_AUX, sptr, len);
338
                                sptr += len;
339 27 dgisselq
                                nwritten += len;
340
 
341 22 dgisselq
                                // If the user presses the button more than
342
                                // once, we start the sound over as well as
343
                                // our light counter.
344
                                event = wait(INT_BUTTON|SWINT_PPS, 0);
345
                                if (event&INT_BUTTON) {
346
                                        if (sptr > &sound_data[2048]) {
347
                                                sptr = sound_data;
348
                                                seconds = 0;
349
                                                when = (volatile unsigned)rtcclock;
350
                                                showbell(when);
351
                                        }
352
                                } else if (event&SWINT_PPS) {
353
                                        seconds++;
354
                                        belllight(rtcclock);
355
                                        showbell(when);
356
                                }
357
                        }
358
 
359 27 dgisselq
                        uartchr('D');
360 22 dgisselq
 
361
                        // Next state: the doorbell is no longer ringing, but
362
                        // we have yet to return to normal--the light is still
363
                        // on.
364
                        while((seconds < HALF_HOUR_S)&&
365
                                (((event=wait(INT_BUTTON|SWINT_PPS,-1))&INT_BUTTON)==0)) {
366
                                seconds++;
367
                                belllight(rtcclock);
368
                                showbell(when);
369
                        }
370
                        if (event&INT_BUTTON) {
371
                                when = (volatile unsigned)rtcclock;
372
                                showbell(when);
373 27 dgisselq
                                uartchr('B');
374 22 dgisselq
                        }
375
                }
376 27 dgisselq
 
377
                // uartstr("\r\n");
378
                uartstr("\r\nNWritten: "); uarthex(nwritten);
379
                uartstr("NRead   : "); uarthex(nread);
380
                uartstr("NStarts : "); uarthex(nstarts);
381
                nwritten = nread = nstarts = 0;
382 22 dgisselq
        }
383
}
384
#endif
385
 
386 27 dgisselq
 
387
 
388
/*
389
 
390
 
391
NWritten: 000018E7
392
NRead   : 000018E7
393
NStarts : 00000001
394
 
395
 
396
*/

powered by: WebSVN 2.1.0

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