-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
270 lines (242 loc) · 5.79 KB
/
device.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
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
#include "device.h"
#include "devices/bma150.h"
#include "devices/buttons.h"
#include "devices/eeprom.h"
#include "devices/factory_control.h"
#include "devices/generic.h"
#include "devices/lcd.h"
#include "devices/led.h"
#include "system.h"
#include "types.h"
#define DEVICES_END H8_DEVICE_INVALID, H8_HOOKUP_PORT_INVALID, { NULL }, { NULL }
static const h8_system_preset_t h8_systems[] =
{
{
"NTR-027",
H8_SYSTEM_NTR_027,
{ 0x82341b9f, 0 },
{
{
H8_DEVICE_FACTORY_CONTROL,
H8_HOOKUP_PORT_1,
{ h8_factory_control_test_in, NULL },
{ NULL, NULL, h8_factory_control_unknown_out, NULL }
},
{
H8_DEVICE_LED,
H8_HOOKUP_PORT_8,
{ NULL },
{ h8_led_on_out, h8_led_color_out, NULL }
},
{
H8_DEVICE_EEPROM_8K,
H8_HOOKUP_PORT_9,
{ NULL },
{ h8_eeprom_select_out, NULL }
},
{
H8_DEVICE_1BUTTON,
H8_HOOKUP_PORT_B,
{ h8_buttons_in_0, NULL },
{ NULL }
},
{ DEVICES_END }
}
},
{
"NTR-031",
H8_SYSTEM_NTR_031,
{ 0x64b40d8d /* earlier */, 0x9321792f /* later */, 0 },
{
{
H8_DEVICE_SPI_BUS,
H8_HOOKUP_PORT_8,
{ NULL, /** @todo Savedata chip select */ NULL, NULL },
{ NULL }
},
/* Not actually used in normal operation */
{
H8_DEVICE_1BUTTON,
H8_HOOKUP_PORT_B,
{ h8_buttons_in_0, NULL },
{ NULL }
},
/**
* There is also code in the earlier ROM for interfacing with an LED on
* port 8, bits 2/3, which is unused and conflicts with SPI chip select.
*/
{ DEVICES_END }
}
},
{
"NTR-032",
H8_SYSTEM_NTR_032,
{ 0xd4a05446, 0 },
{
{
H8_DEVICE_LCD,
H8_HOOKUP_PORT_1,
{ NULL },
{ h8_lcd_select_out, h8_lcd_mode_out, NULL }
},
{
H8_DEVICE_EEPROM_64K,
H8_HOOKUP_PORT_1,
{ NULL },
{ NULL, NULL, h8_eeprom_select_out, NULL }
},
{
H8_DEVICE_BMA150,
H8_HOOKUP_PORT_9,
{ NULL },
{ h8_bma150_select_out, NULL }
},
{
H8_DEVICE_3BUTTON,
H8_HOOKUP_PORT_B,
{ h8_buttons_in_0, h8_buttons_in_1, h8_buttons_in_2, NULL },
{ NULL }
},
{ DEVICES_END }
}
},
{ NULL, H8_SYSTEM_INVALID, { 0 }, { { DEVICES_END } } }
};
h8_bool h8_device_init(h8_device_t *device, const h8_device_id type)
{
if (device)
{
switch (type)
{
case H8_DEVICE_GENERIC:
device->init = h8_generic_init;
break;
case H8_DEVICE_1BUTTON:
device->init = h8_buttons_init_1b;
break;
case H8_DEVICE_3BUTTON:
device->init = h8_buttons_init_3b;
break;
case H8_DEVICE_BMA150:
device->init = h8_bma150_init;
break;
case H8_DEVICE_BUZZER:
device->init = NULL; /** @todo */
break;
case H8_DEVICE_EEPROM_64K:
device->init = h8_eeprom_init_64k;
break;
case H8_DEVICE_EEPROM_8K:
device->init = h8_eeprom_init_8k;
break;
case H8_DEVICE_FACTORY_CONTROL:
device->init = h8_factory_control_init;
break;
case H8_DEVICE_LCD:
device->init = h8_lcd_init;
break;
case H8_DEVICE_LED:
device->init = h8_led_init;
break;
default:
return 0;
}
if (device->init)
device->init(device);
else
device->type = type;
return 1;
}
return 0;
}
h8_bool h8_system_init(h8_system_t *system, const h8_system_id id)
{
if (system)
{
const h8_system_preset_t *preset;
unsigned i, j = 0;
switch (id)
{
case H8_SYSTEM_NTR_027:
preset = &h8_systems[0];
break;
case H8_SYSTEM_NTR_031:
preset = &h8_systems[1];
break;
case H8_SYSTEM_NTR_032:
preset = &h8_systems[2];
break;
default:
return FALSE;
}
for (i = 0; i < H8_HOOKUP_MAX; i++)
{
const software_hookup_t *hookup = &preset->hookups[i];
h8_device_t *device = &system->devices[j];
if (preset->hookups[i].type == H8_DEVICE_INVALID)
break;
else
{
h8_system_pin_in_t *pins_in;
h8_system_pin_out_t *pins_out;
unsigned pin_count;
unsigned k;
/* Create the device if it does not already exist */
if (device->type == H8_DEVICE_INVALID)
{
h8_device_init(device, hookup->type);
j++;
}
/* Setup IO functions */
device->port = hookup->port;
/* Figure out which port we're on */
switch (device->port)
{
case H8_HOOKUP_PORT_1:
pins_in = system->pdr1_in;
pins_out = system->pdr1_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_3:
pins_in = system->pdr3_in;
pins_out = system->pdr3_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_8:
pins_in = system->pdr8_in;
pins_out = system->pdr8_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_9:
pins_in = system->pdr9_in;
pins_out = system->pdr9_out;
pin_count = 4;
break;
case H8_HOOKUP_PORT_B:
pins_in = system->pdrb_in;
pins_out = system->pdrb_out;
pin_count = 6;
break;
default:
continue;
}
/* Hookup any used pins */
for (k = 0; k < pin_count; k++)
{
if (hookup->pdr_ins[k])
{
pins_in[k].device = device;
pins_in[k].func = hookup->pdr_ins[k];
}
if (hookup->pdr_outs[k])
{
pins_out[k].device = device;
pins_out[k].func = hookup->pdr_outs[k];
}
}
}
}
system->device_count = j;
}
return FALSE;
}