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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
/*-------------------------------------------------------------------------
tinibios.c - startup and serial routines for the DS80C390 (tested on TINI)
Copyright (C) 2001, Johan Knol <johan.knol AT iduna.nl>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/
#include <tinibios.h>
#include <stdio.h>
#define TIMED_ACCESS(sfr,value) { TA=0xaa; TA=0x55; sfr=value; }
unsigned char _sdcc_external_startup(void)
{
IE=0; // disable ALL interrupts
// use A19..16 and !CE3..0, no CAN
TIMED_ACCESS(P4CNT,0x3f);
// use !PCE3..0, serial 1 at P5.2/3
TIMED_ACCESS(P5CNT,0x27);
// disable watchdog
EWT=0;
// watchdog set to 9.1 seconds
// CKCON|=0xc0;
// default stretch cycles for MOVX
//CKCON = (CKCON&0xf8)|(CPU_MOVX_STRETCH&0x07);
CKCON=0xf9;
// use internal 4k RAM as data(stack) memory at 0x400000 and
// move CANx memory access to 0x401000 and upwards
// use !CE* for program and/or data memory access
TIMED_ACCESS(MCON,0xaf);
// select default cpu speed
CpuSpeed(CPU_SPEED);
// Copy the Interrupt Vector Table (128 bytes) from 0x10000 to 0x100000
// This isn't needed for older bootloaders than the 0515, but it won't harm
__asm
push dpx
push dph
push dpl
push dps
push b
push acc
mov dps,#0x00 ; make sure no autoincrement in progress
mov dptr,#0x10000 ; from
inc dps ; switch to alternate dptr
mov dptr,#0x100000 ; to
mov b,#0x80 ; count
_Startup390CopyIVT:
inc dps
movx a,@dptr
inc dptr
inc dps
movx @dptr,a
inc dptr
djnz b,_Startup390CopyIVT
pop acc
pop b
pop dps
pop dpl
pop dph
pop dpx
__endasm;
// global interrupt enable, all masks cleared
// let the Gods be with us :)
IE = 0x80;
Serial0Init(SERIAL_0_BAUD,1);
//Serial1Init(SERIAL_1_BAUD,1);
ClockInit();
//RtcInit();
//WatchDogInit();
// signal _sdcc_gsinit_startup to initialize data (call _sdcc_init_data)
return 0;
}
/* Set the cpu speed in clocks per machine cycle, valid values are:
1024: Power management mode
4: Divide-by-4 mode
2: Use frequency multiplier (2x)
1: Use frequency multiplier (4x) (Don't do this on a TINI at 18.432MHz)
TODO: TINI seems to support only 2 and 4: write only bits in PMR ?
*/
unsigned int cpuSpeed;
void CpuSpeed(unsigned int speed)
{
#if 0
while (0 && (EXIF&0x04))
; // cpu operates from ring buffer
#endif
PMR = 0x80; // div4, CTM off, multiplier 2x
switch (speed)
{
case 1:
PMR=0x88; // div4, CTM off, multiplier 4x
PMR=0x98; // div4, CTM on, multiplier 4x
while ((EXIF&0x08)==0) {
; // wait for the multiplier to be ready
}
PMR = 0x18; // use multiplier
cpuSpeed=speed;
break;
case 2:
PMR=0x90; // div4, CTM on, multilier 2x
while ((EXIF&0x08)==0) {
; // wait for the multiplier to be ready
}
PMR = 0x10; // use multiplier
cpuSpeed=speed;
break;
case 4:
// nothing to do
cpuSpeed=speed;
break;
case 1024:
PMR = 0xc0; // div1024, CTM off
cpuSpeed=speed;
break;
}
}
// now the serial0 stuff
// just to make the code more readable
#define S0RBS SERIAL_0_RECEIVE_BUFFER_SIZE
// this is a ring buffer and can overflow at anytime!
static volatile unsigned char receive0Buffer[S0RBS];
static volatile int receive0BufferHead=0;
static volatile int receive0BufferTail=0;
// no buffering for transmit
static volatile char transmit0IsBusy=0;
static __data unsigned char serial0Buffered;
/* Initialize serial0.
Available baudrates are from 110 upto 115200 (using 16-bit timer 2)
If baud==0, the port is disabled.
If buffered!=0, characters received are buffered using an interrupt
*/
void Serial0Init (unsigned long baud, unsigned char buffered)
{
if (baud==0) {
ES0=0; // disable interrupts
SCON0 &= 0xef; // disable receiver
return;
}
ES0 = 0; // disable serial channel 0 interrupt
TR2 = 0; // stop timer 2
// set 8 bit uart with variable baud from timer 1/2
// enable receiver and clear RI and TI
SCON0 = 0x50;
PCON |= 0x80; // clock is 16x bitrate
CKCON|=0x20; // timer uses xtal/4
T2MOD=0; // no fancy functions
T2CON=0x34; // start timer as a baudrate generator for serial0
// set the baud rate
Serial0Baud(baud);
serial0Buffered=buffered;
if (buffered) {
RI_0=TI_0=0; // clear "pending" interrupts
ES0 = 1; // enable serial channel 0 interrupt
} else {
RI_0=0; // receive buffer empty
TI_0=1; // transmit buffer empty
}
}
void Serial0Baud(unsigned long baud)
{
TR2=0; // stop timer
baud=-((long)OSCILLATOR/(32*baud));
TL2=RCAP2L= baud;
TH2=RCAP2H= baud>>8;
TF2=0; // clear overflow flag
TR2=1; // start timer
}
void Serial0IrqHandler (void) __interrupt 4
{
if (RI_0) {
receive0Buffer[receive0BufferHead]=SBUF0;
receive0BufferHead=(receive0BufferHead+1)&(S0RBS-1);
if (receive0BufferHead==receive0BufferTail) {
/* buffer overrun, sorry :) */
receive0BufferTail=(receive0BufferTail+1)&(S0RBS-1);
}
RI_0=0;
}
if (TI_0) {
TI_0=0;
transmit0IsBusy=0;
}
}
char Serial0CharArrived(void)
{
if (serial0Buffered) {
if (receive0BufferHead!=receive0BufferTail)
return receive0Buffer[receive0BufferTail];
} else {
if (RI_0)
return SBUF0;
}
return 0;
}
void Serial0PutChar (char c)
{
if (serial0Buffered) {
while (transmit0IsBusy)
;
transmit0IsBusy=1;
SBUF0=c;
} else {
while (!TI_0)
;
SBUF0=c;
TI_0=0;
}
}
char Serial0GetChar (void)
{
char c;
if (serial0Buffered) {
while (receive0BufferHead==receive0BufferTail)
;
c=receive0Buffer[receive0BufferTail];
ES0=0; // disable serial interrupts
receive0BufferTail=(receive0BufferTail+1)&(S0RBS-1);
ES0=1; // enable serial interrupts
} else {
while (!RI_0)
;
c=SBUF0;
RI_0=0;
}
return c;
}
void Serial0SendBreak()
{
P3 &= ~0x02;
ClockMilliSecondsDelay(2);
P3 |= 0x02;
}
void Serial0Flush()
{
ES0=0; // disable interrupts
receive0BufferHead=receive0BufferTail=0;
RI_0=0;
if (serial0Buffered) {
TI_0=0;
ES0=1; // enable interrupts
} else {
TI_0=1;
}
}
/* now let's go for the serial1 stuff, basically it's a replicate of
serial0 except it uses timer 1
*/
// just to make the code more readable
#define S1RBS SERIAL_1_RECEIVE_BUFFER_SIZE
// this is a ring buffer and can overflow at anytime!
static volatile unsigned char receive1Buffer[S1RBS];
static volatile int receive1BufferHead=0;
static volatile int receive1BufferTail=0;
// no buffering for transmit
static volatile char transmit1IsBusy=0;
static __data unsigned char serial1Buffered;
/* Initialize serial1.
Available baudrates are from 4800 upto 115200 (using 8-bit timer 1)
If baud==0, the port is disabled.
If buffered!=0, characters received are buffered using an interrupt
*/
void Serial1Init (unsigned long baud, unsigned char buffered)
{
if (baud==0) {
ES1=0; // disable interrupt
SCON1 &= 0xef; // disable receiver
return; // and don't touch it
}
ES1 = 0; // disable channel 1 interrupt
TR1 = 0; // stop timer 1
// set 8 bit uart with variable baud from timer 1
// enable receiver and clear RI and TI
SCON1 = 0x50;
WDCON |= 0x80; // clock is 16x bitrate
CKCON|=0x10; // timer uses xtal/4
TMOD = (TMOD&0x0f) | 0x20; // timer 1 is an 8bit auto-reload counter
// set the baud rate
Serial1Baud(baud);
serial1Buffered=buffered;
if (buffered) {
RI_1=TI_1=0; // clear "pending" interrupts
ES1 = 1; // enable serial channel 1 interrupt
} else {
RI_1=0; // receive buffer empty
TI_1=1; // transmit buffer empty
}
}
void Serial1Baud(unsigned long baud)
{
TR1=0; // stop timer
baud=-((long)OSCILLATOR/(32*baud));
TL1=TH1 = baud;
TF1=0; // clear overflow flag
TR1=1; // start timer
}
void Serial1IrqHandler (void) __interrupt 7
{
if (RI_1) {
receive1Buffer[receive1BufferHead]=SBUF1;
receive1BufferHead=(receive1BufferHead+1)&(S1RBS-1);
if (receive1BufferHead==receive1BufferTail) /* buffer overrun, sorry :) */
receive1BufferTail=(receive1BufferTail+1)&(S1RBS-1);
RI_1=0;
}
if (TI_1) {
TI_1=0;
transmit1IsBusy=0;
}
}
char Serial1CharArrived(void)
{
if (serial1Buffered) {
if (receive1BufferHead!=receive1BufferTail)
return receive1Buffer[receive1BufferTail];
} else {
if (RI_1)
return SBUF1;
}
return 0;
}
void Serial1PutChar (char c)
{
if (serial1Buffered) {
while (transmit1IsBusy)
;
transmit1IsBusy=1;
SBUF1=c;
} else {
while (!TI_1)
;
SBUF1=c;
TI_1=0;
}
}
char Serial1GetChar (void)
{
char c;
if (serial1Buffered) {
while (receive1BufferHead==receive1BufferTail)
;
c=receive1Buffer[receive1BufferTail];
ES1=0; // disable serial interrupts
receive1BufferTail=(receive1BufferTail+1)&(S1RBS-1);
ES1=1; // enable serial interrupts
} else {
while (!RI_1)
;
c=SBUF1;
RI_1=0;
}
return c;
}
void Serial1SendBreak()
{
P5 &= ~0x08;
ClockMilliSecondsDelay(2);
P5 |= 0x08;
}
void Serial1Flush()
{
ES1=0; // disable interrupts
receive1BufferHead=receive1BufferTail=0;
RI_1=0;
if (serial1Buffered) {
TI_1=0;
ES1=1; // enable interrupts
} else {
TI_1=1;
}
}
// now let's go for the clock stuff
// these REALLY need to be in data space for the irq routine!
static __data unsigned long milliSeconds=0;
static __data unsigned int timer0ReloadValue;
void ClockInit()
{
unsigned long timerReloadValue=OSCILLATOR/1000;
switch (cpuSpeed) {
case 4: timerReloadValue/=4; break;
case 1: // not tested yet
case 2: // not tested yet
default: timerReloadValue/=2; break;
}
timer0ReloadValue=~timerReloadValue;
// initialise timer 0
ET0=0; // disable timer interrupts initially
TCON = (TCON&0xcc)|0x00; // stop timer, clear overflow
TMOD = (TMOD&0xf0)|0x01; // 16 bit counter
CKCON|=0x08; // timer uses xtal/4
TL0=timer0ReloadValue&0xff;
TH0=timer0ReloadValue>>8;
ET0=1; // enable timer interrupts
TR0=1; // start timer
}
// This needs to be SUPER fast. What we really want is:
#if 0
void junk_ClockIrqHandler (void) __interrupt 10
{
TL0 = timer0ReloadValue & 0xff;
TH0 = timer0ReloadValue >> 8;
milliSeconds++;
}
#else
// but look at the code, and the pushes and pops, so:
void ClockIrqHandler (void) __interrupt 1 __naked
{
__asm
push acc
push psw
mov _TL0,_timer0ReloadValue
mov _TH0,_timer0ReloadValue+1
clr a
inc _milliSeconds+0
cjne a,_milliSeconds+0,_ClockIrqHandlerDone
inc _milliSeconds+1
cjne a,_milliSeconds+1,_ClockIrqHandlerDone
inc _milliSeconds+2
cjne a,_milliSeconds+2,_ClockIrqHandlerDone
inc _milliSeconds+3
_ClockIrqHandlerDone:
pop psw
pop acc
reti
__endasm;
}
#endif
// we can't just use milliSeconds
unsigned long ClockTicks(void)
{
unsigned long ms;
ET0=0;
ms=milliSeconds;
ET0=1;
return ms;
}
void ClockMilliSecondsDelay(unsigned long delay)
{
long ms=ClockTicks()+delay;
while (ms>ClockTicks())
;
}
// stolen from Kevin Vigor, works only for TINI at default speed
void ClockMicroSecondsDelay(unsigned int delay)
{
delay; /* shut compiler up. */
__asm
; delay is in dpl/dph
mov r0, dpl
mov r1, dph
mov a, r0
orl a, r1 ; quick out for zero case.
jz _usDelayDone
inc r1
cjne r0, #0, _usDelayLoop
dec r1
_usDelayLoop:
nop
nop
nop
nop
nop
nop
nop ; 7 nops
djnz r0, _usDelayLoop ; 3 cycles x 1 = 3 cycles
; 10 cycles per iter
; we want 9.216, but more is better
; than less.
djnz r1, _usDelayLoop
_usDelayDone:
__endasm;
}
|