/
tn_user.c
210 lines (174 loc) · 5.72 KB
/
tn_user.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
/*
Example project for SATNKernel
Two tasks write to the USB devcart FIFO.
FIFO access is protected by a semaphore.
The CPU free-running timer's compare match
interrupt is used for the timer tick.
*/
#include <stdint.h>
#include "tn.h"
#include "tn_port_config.h"
// 10 ms tick
#define TICK_MS_INTERVAL 10
// FRT compare match interrupt vector
// (see System Program User's Manual)
#define FRT_TIMER_VECTOR 101
#define TASK_A_PRIORITY 5
#define TASK_B_PRIORITY 7
// Task stacks, in words
#define TASK_A_STK_SIZE 128
#define TASK_B_STK_SIZE 128
TN_KERN_CTX kernelcontext;
TN_SEM port_lock;
TN_TCB task_A;
TN_TCB task_B;
unsigned int task_A_stack[TASK_A_STK_SIZE];
unsigned int task_B_stack[TASK_B_STK_SIZE];
// ISR wrapper/dispatch
extern void frt_compare_dispatch(void);
// VDP2 video mode registers
#define VDP2_BASE 0x25e00000
#define VDP2_REGISTER_BASE (VDP2_BASE+0x180000)
#define TVMD (*(volatile uint16_t *)(VDP2_REGISTER_BASE+0x00))
#define HRESO0 (1<<0)
#define TVSTAT (*(volatile uint16_t *)(VDP2_REGISTER_BASE+0x04))
#define PAL (1<<0)
// SH7604 hardware registers
#define TIER (*(volatile uint8_t *)0xfffffe10)
#define OCIAE (1<<3)
#define FTCSR (*(volatile uint8_t *)0xfffffe11)
#define OCFA (1<<3)
#define CCLRA (1<<0)
#define FRCH (*(volatile uint8_t *)0xfffffe12)
#define FRCL (*(volatile uint8_t *)0xfffffe13)
#define OCRAH (*(volatile uint8_t *)0xfffffe14)
#define OCRAL (*(volatile uint8_t *)0xfffffe15)
#define TCR (*(volatile uint8_t *)0xfffffe16)
#define CKS1 (1<<1)
#define CKS0 (1<<0)
#define TOCR (*(volatile uint8_t *)0xfffffe17)
#define OCRS (1<<4)
#define VCRC (*(volatile uint16_t*)0xfffffe66)
//----------------------------------------------------------------------------
// SH7604 timer init function
//----------------------------------------------------------------------------
void frt_init(void)
{
// Precalculated timer tick values for /8 prescaler
static const uint16_t tickValues[4] =
{
26874100/(8 * 1000.0f/TICK_MS_INTERVAL), /* NTSC 320/640 */
26687499/(8 * 1000.0f/TICK_MS_INTERVAL), /* PAL 320/640 */
28636400/(8 * 1000.0f/TICK_MS_INTERVAL), /* NTSC 352/704 */
28437500/(8 * 1000.0f/TICK_MS_INTERVAL), /* PAL 352/704 */
};
uint16_t tickValue;
// Disable FRT interrupts
TIER = 0;
// Set interrupt vector - should be done by system init code
VCRC = (VCRC & 0xFF00) | FRT_TIMER_VECTOR;
tickValue = tickValues[((TVMD & HRESO0) << 1) | (TVSTAT & PAL)];
// Clock source is internal clock/8
TCR &= ~(CKS1|CKS0);
// Clear FRC on compare match A
FTCSR |= CCLRA;
// Map OCRA register
TOCR &= ~OCRS;
// Set match A value
OCRAH = tickValue >> 8;
OCRAL = tickValue;
// Clear FRC value
FRCH = 0;
FRCL = 0;
// Enable output compare A interrupt
TIER = OCIAE;
}
//----------------------------------------------------------------------------
// SH7604 timer interrupt handler
//----------------------------------------------------------------------------
void frt_irq_handler(void)
{
unsigned char st = FTCSR;
if (st & OCFA)
{
tn_tick_int_processing();
FTCSR = (~OCFA)|CCLRA;
}
}
void tn_cpu_int_enable(void)
{
tn_cpu_restore_sr(0);
}
#define USB_FLAGS (*(volatile unsigned char*)(0x22200001))
#define USB_RXF (1 << 0)
#define USB_TXE (1 << 1)
#define USB_PWREN (1 << 7)
#define USB_FIFO (*(volatile unsigned char*)(0x22100001))
void printmessage(const char *message)
{
while (*message != '\0')
{
while ((USB_FLAGS & USB_TXE) != 0) ;
USB_FIFO = *message++;
}
return;
}
int main(void)
{
// disable interrupts
tn_cpu_save_sr();
// hook timer interrupt vector
tn_hook_vec(FRT_TIMER_VECTOR, frt_compare_dispatch);
// init timer
frt_init();
// start kernel, does not return
tn_start_system(&kernelcontext);
return 1;
}
void task_A_func(void *par)
{
for(;;)
{
tn_sem_acquire(&port_lock, TN_WAIT_INFINITE);
printmessage("task A\n");
tn_sem_signal(&port_lock);
tn_task_sleep((unsigned long)par);
}
}
void task_B_func(void *par)
{
for(;;)
{
tn_sem_acquire(&port_lock, TN_WAIT_INFINITE);
printmessage("task B\n");
tn_sem_signal(&port_lock);
tn_task_sleep((unsigned long)par);
}
}
void tn_app_init(void)
{
//--- USB FIFO lock
tn_sem_create(&port_lock, 1, 1);
//--- Task A
task_A.id_task = 0;
tn_task_create(&task_A, //-- task TCB
task_A_func, //-- task function
TASK_A_PRIORITY, //-- task priority
&(task_A_stack //-- task stack first addr in memory
[TASK_A_STK_SIZE-1]),
TASK_A_STK_SIZE, //-- task stack size (in int,not bytes)
(void*)100, //-- task function parameter
TN_TASK_START_ON_CREATION //-- Creation option
);
//--- Task B
task_B.id_task = 0;
tn_task_create(&task_B, //-- task TCB
task_B_func, //-- task function
TASK_B_PRIORITY, //-- task priority
&(task_B_stack //-- task stack first addr in memory
[TASK_B_STK_SIZE-1]),
TASK_B_STK_SIZE, //-- task stack size (in int,not bytes)
(void*)150, //-- task function parameter
TN_TASK_START_ON_CREATION //-- Creation option
);
}