summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libnds/source/arm9/ndsmotion.c
blob: 5b3a3cc4d96e410e976a73eb10585dd475b83d7c (plain) (blame)
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
/*---------------------------------------------------------------------------------
	DS Motion Card/DS Motion Pak functionality

	Copyright (C) 2007
		Michael Noland (joat)
		Jason Rogers (dovoto)
		Dave Murphy (WinterMute)
		Keith Epstein (KeithE)

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any
	damages arising from the use of this software.

	Permission is granted to anyone to use this software for any
	purpose, including commercial applications, and to alter it and
	redistribute it freely, subject to the following restrictions:

	1.	The origin of this software must not be misrepresented; you
		must not claim that you wrote the original software. If you use
		this software in a product, an acknowledgment in the product
		documentation would be appreciated but is not required.
	2.	Altered source versions must be plainly marked as such, and
		must not be misrepresented as being the original software.
	3.	This notice may not be removed or altered from any source
		distribution.

---------------------------------------------------------------------------------*/

#include <nds/card.h>
#include <nds/system.h>
#include <nds/memory.h>
#include <nds/bios.h>
#include <nds/arm9/ndsmotion.h>

#define WAIT_CYCLES 185

#define CARD_WaitBusy()   while (CARD_CR1 & /*BUSY*/0x80);

// enables SPI bus at 4.19 MHz
#define SPI_On() CARD_CR1 = /*E*/0x8000 | /*SEL*/0x2000 | /*MODE*/0x40 | 0;

// disables SPI bus
#define SPI_Off() CARD_CR1 = 0;

// Volatile GBA bus SRAM for reading from DS Motion Pak
#define V_SRAM ((volatile unsigned char*)0x0A000000)


int card_type = -1;

//these are the default calibration values for sensitivity and offset
MotionCalibration calibration = {2048, 2048, 2048, 1680, 819, 819, 819, 825};

// sends and receives 1 byte on the SPI bus
unsigned char motion_spi(unsigned char in_byte){

	unsigned char out_byte;
	CARD_EEPDATA = in_byte; // send the output byte to the SPI bus
	CARD_WaitBusy(); // wait for transmission to complete
	out_byte=CARD_EEPDATA; // read the input byte from the SPI bus
	return out_byte;
}


void motion_MK6_sensor_mode(void) {
	// send some commands on the SPI bus
	SPI_On()
	motion_spi(0xFE);
	SPI_Off()
	SPI_On()
	motion_spi(0xFD);
	SPI_Off()
	SPI_On()
	motion_spi(0xFB);
	SPI_Off()
	SPI_On()
	motion_spi(0xF8);
	SPI_Off()
}

void motion_MK6_EEPROM_mode(void) {
	// send some commands on the SPI bus
	SPI_On()
	motion_spi(0xFE);
	SPI_Off()
	SPI_On()
	motion_spi(0xFD);
	SPI_Off()
	SPI_On()
	motion_spi(0xFB);
	SPI_Off()
	SPI_On()
	motion_spi(0xF9);
	SPI_Off()
}

// checks whether a DS Motion Pak is plugged in
int motion_pak_is_inserted(void){
    int motion_pak = 0;
	unsigned char return_byte = V_SRAM[10]; // read first byte of DS Motion Pak check
	swiDelay(WAIT_CYCLES);
	return_byte = V_SRAM[0];
	swiDelay(WAIT_CYCLES);
	if (return_byte==0xF0) { // DS Motion Pak returns 0xF0
		return_byte = V_SRAM[0]; // read second byte of DS Motion Pak check
		swiDelay(WAIT_CYCLES);
		if(return_byte==0x0F) { // DS Motion Pak returns 0x0F
			motion_pak = 1;
		}
	}
    return motion_pak;
}

// checks whether a DS Motion Card is plugged in
// this only works after motion_init()
// it will return false if it is run before motion_init()
int motion_card_is_inserted(void){
	// send 0x03 to read from DS Motion Card control register
	SPI_On()
	motion_spi(0x03); // command to read from control register
	// if the control register is 0x04 then the enable was successful
	if( motion_spi(0x00) == 0x04)
	{
		SPI_Off()
		return 1;
	}
	SPI_Off();
	return 0;
}

// turn on the DS Motion Sensor (DS Motion Pak or DS Motion Card)
// Requires knowing which type is present (can be found by using motion_init)
int motion_enable(int card_type) {
	switch (card_type)
	{
		case 1: // DS Motion Pak - automatically enabled on powerup
			// check to see whether Motion Pak is alive
			return motion_pak_is_inserted();
			break;
		case 2: // DS Motion Card
			// send 0x04, 0x04 to enable
			SPI_On()
			motion_spi(0x04); // command to write to control register
			motion_spi(0x04); // enable
			SPI_Off()
			// check to see whether Motion Card is alive
			return motion_card_is_inserted();
			break;
		case 3: // MK6 - same command as DS Motion Card
			// send 0x04, 0x04 to enable
			SPI_On()
			motion_spi(0x04); // command to write to control register
			motion_spi(0x04); // enable
			SPI_Off()
			// check to see whether Motion Card is alive
			return motion_card_is_inserted();
			break;
		default: // if input parameter is not recognized, return 0
			return 0;
			break;
	}
}

// Initialize the DS Motion Sensor
// Determines which DS Motion Sensor is present
// Turns it on
// Does not require knowing which type is present
int motion_init(void) {
	sysSetBusOwners(true, true);
	// first, check for the DS Motion Pak - type 1
	if( motion_pak_is_inserted() == 1 )
    {
        card_type = 1;
        return 1;
	}// next, check for DS Motion Card - type 2
	if( motion_enable(2) == 1 )
    {
         card_type = 2;
         return 2;
	}

    motion_MK6_sensor_mode(); // send command to switch MK6 to sensor mode

    if( motion_enable(3) == 1 )
    {
        card_type = 3;
        return 3;
	}// if neither cases are true, then return 0 to indicate no DS Motion Sensor
	return 0;
}

// Deinitialize the DS Motion Sensor
// In the case of a DS Motion Pak, do nothing - there is nothing to de-init
// In the case of a DS Motion Card, turns off the accelerometer
// In the case of an MK6, turns off accelerometer and switches out of sensor mode into EEPROM mode
void motion_deinit(void) {
	// DS Motion Card - turn off accelerometer
	SPI_On()
	motion_spi(0x04); // command to write to control register
	motion_spi(0x00); // turn it off
	SPI_Off()
	// MK6 - switch to EEPROM mode
	motion_MK6_EEPROM_mode(); // switch MK6 to EEPROM mode
}

// read the X acceleration
signed int motion_read_x(void) {
	unsigned char High_byte = 0;
	unsigned char Low_byte = 0;
	signed int output = 0;
	switch(card_type)
	{
		case 1: // DS Motion Pak
			High_byte = V_SRAM[2]; // Command to load X High onto bus
			swiDelay(WAIT_CYCLES); // wait for data ready
			High_byte = V_SRAM[0]; // get the high byte
			swiDelay(WAIT_CYCLES); // wait for data ready
			Low_byte = V_SRAM[0]; // get the low byte
			swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
			output = (signed int)( (High_byte<<8 | Low_byte)>>4);
			return output;
			break;
		case 2: // DS Motion Card
			SPI_On()
			motion_spi(0x00); // command to convert X axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		case 3: // MK6 - same command as DS Motion Card
			SPI_On()
			motion_spi(0x00); // command to convert X axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		default:
			return 0;
			break;
	}
}

// read the Y acceleration
signed int motion_read_y(void) {
	unsigned char High_byte = 0;
	unsigned char Low_byte = 0;
	signed int output = 0;
	switch (card_type)
	{
		case 1: // DS Motion Pak
			High_byte = V_SRAM[4]; // Command to load Y High onto bus
			swiDelay(WAIT_CYCLES); // wait for data ready
			High_byte = V_SRAM[0]; // get the high byte
			swiDelay(WAIT_CYCLES); // wait for data ready
			Low_byte = V_SRAM[0]; // get the low byte
			swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
			output = (signed int)( (High_byte<<8 | Low_byte)>>4);
			return output;
			break;
		case 2: // DS Motion Card
			SPI_On()
			motion_spi(0x02); // command to convert Y axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		case 3: // MK6 - same command as DS Motion Card
			SPI_On()
			motion_spi(0x02); // command to convert Y axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		default:
			return 0;
			break;
	}
}

// read the Z acceleration
signed int motion_read_z(void) {
	unsigned char High_byte = 0;
	unsigned char Low_byte = 0;
	signed int output = 0;
	switch (card_type)
	{
		case 1: // DS Motion Pak
			High_byte = V_SRAM[6]; // Command to load Z High onto bus
			swiDelay(WAIT_CYCLES); // wait for data ready
			High_byte = V_SRAM[0]; // get the high byte
			swiDelay(WAIT_CYCLES); // wait for data ready
			Low_byte = V_SRAM[0]; // get the low byte
			swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
			output = (signed int)( (High_byte<<8 | Low_byte)>>4);
			return output;
			break;
		case 2: // DS Motion Card
			SPI_On()
			motion_spi(0x01); // command to convert Z axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		case 3: // MK6 - same command as DS Motion Card
			SPI_On()
			motion_spi(0x01); // command to convert Z axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		default:
			return 0;
			break;
	}
}

// read the Z rotation (gyro)
signed int motion_read_gyro(void) {
	unsigned char High_byte = 0;
	unsigned char Low_byte = 0;
	signed int output = 0;
	switch (card_type)
	{
		case 1: // DS Motion Pak
			High_byte = V_SRAM[8]; // Command to load Gyro High onto bus
			swiDelay(WAIT_CYCLES); // wait for data ready
			High_byte = V_SRAM[0]; // get the high byte
			swiDelay(WAIT_CYCLES); // wait for data ready
			Low_byte = V_SRAM[0]; // get the low byte
			swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
			output = (signed int)( (High_byte<<8 | Low_byte)>>4);
			return output;
			break;
		case 2: // DS Motion Card
			SPI_On()
			motion_spi(0x07); // command to convert Gyro axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		case 3: // MK6 - same command as DS Motion Card
			SPI_On()
			motion_spi(0x07); // command to convert Gyro axis
			swiDelay(625); // wait at least 40 microseconds for the A-D conversion
			output = ( (motion_spi(0x00)<<8)|motion_spi(0x00) )>>4; // read 16 bits and store as a 12 bit number
			SPI_Off()
			return output;
			break;
		default:
			return 0;
			break;
	}
}

//gets acceleration value in mili G (where g is 9.8 m/s*s)
int motion_acceleration_x(void){
	int accel = motion_read_x();
	return (accel - calibration.xoff) * 1000 / calibration.xsens;
}

//gets acceleration value in mili G (where g is 9.8 m/s*s)
int motion_acceleration_y(void){
	int accel = motion_read_y();
	return (accel - calibration.yoff) * 1000 / calibration.ysens;
}
//gets acceleration value in mili G (where g is 9.8 m/s*s)
int motion_acceleration_z(void){
	int accel = motion_read_z();
	return (accel - calibration.zoff) * 1000 / calibration.zsens;
}

//converts raw rotation value to degrees per second
int motion_rotation(void){
	int rotation = motion_read_gyro();
	return (rotation - calibration.goff) * 1000 / calibration.gsens;
}

//this should be passed the raw reading at 1g for accurate
//acceleration calculations.  Default is 819
void motion_set_sens_x(int sens){
	calibration.xsens = sens - calibration.xoff;
}

//this should be passed the raw reading at 1g for accurate
//acceleration calculations.  Default is 819
void motion_set_sens_y(int sens){
	calibration.ysens = sens - calibration.yoff;
}

//this should be passed the raw reading at 1g for accurate
//acceleration calculations.  Default is 819
void motion_set_sens_z(int sens){
	calibration.zsens = sens - calibration.zoff;
}

//this should be passed the raw reading at 1g for accurate
//acceleration calculations.  Default is 825
void motion_set_sens_gyro(int sens){
	calibration.gsens = sens;
}

//this should be called when the axis is under no acceleration
//default is 2048
void motion_set_offs_x(void){
	calibration.xoff = motion_read_x();
}

//this should be called when the axis is under no acceleration
//default is 2048
void motion_set_offs_y(void){
	calibration.yoff = motion_read_y();
}

//this should be called when the axis is under no acceleration
//default is 2048
void motion_set_offs_z(void){
	calibration.zoff = motion_read_z();
}

//this should be called when the axis is under no acceleration
//default is 1680
void motion_set_offs_gyro(void){
	calibration.goff = motion_read_gyro();
}

MotionCalibration* motion_get_calibration(void){
	return &calibration;
}

void motion_set_calibration(MotionCalibration* cal){
	calibration.xsens = cal->xsens;
	calibration.ysens = cal->ysens;
	calibration.zsens = cal->zsens;
	calibration.gsens = cal->gsens;
	calibration.xoff = cal->xoff;
	calibration.yoff = cal->yoff;
	calibration.zoff = cal->zoff;
	calibration.goff = cal->goff;
}

// enable analog input number 1 (ain_1)
void motion_enable_ain_1(void){
	unsigned char return_byte;
    return_byte = V_SRAM[16];
	swiDelay(WAIT_CYCLES);
}

// enable analog input number 2 (ain_2)
void motion_enable_ain_2(void){
	unsigned char return_byte;
    return_byte = V_SRAM[18];
	swiDelay(WAIT_CYCLES);
}

// read from the analog input number 1 - requires enabling ain_1 first
int motion_read_ain_1(void){
	unsigned char High_byte = V_SRAM[12]; // Command to load AIN_1 High onto bus
	swiDelay(WAIT_CYCLES); // wait for data ready
	High_byte = V_SRAM[0]; // get the high byte
	swiDelay(WAIT_CYCLES); // wait for data ready
	unsigned char Low_byte = V_SRAM[0]; // get the low byte
	swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
	signed int output = (signed int)( (High_byte<<8 | Low_byte)>>4);
	return output;
}

// read from the analog input number 2 - requires enabling ain_2 first
int motion_read_ain_2(void){
	unsigned char High_byte = V_SRAM[14]; // Command to load AIN_1 High onto bus
	swiDelay(WAIT_CYCLES); // wait for data ready
	High_byte = V_SRAM[0]; // get the high byte
	swiDelay(WAIT_CYCLES); // wait for data ready
	unsigned char Low_byte = V_SRAM[0]; // get the low byte
	swiDelay(WAIT_CYCLES); // wait after for Motion Pak to be ready for next command
	signed int output = (signed int)( (High_byte<<8 | Low_byte)>>4);
	return output;
}