Statistics
| Branch: | Tag: | Revision:

amiro-os / modules / DiWheelDrive_1-1 / module.c @ 933df08e

History | View | Annotate | Download (24.8 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
#include "module.h"
20

    
21
/*===========================================================================*/
22
/**
23
 * @name Module specific functions
24
 * @{
25
 */
26
/*===========================================================================*/
27
#include <amiroos.h>
28

    
29
/**
30
 * @brief   Interrupt service routine callback for I/O interrupt signals.
31
 *
32
 * @param   extp      EXT driver to handle the ISR.
33
 * @param   channel   Channel on which the interrupt was encountered.
34
 */
35
static void _moduleIsrCallback(EXTDriver* extp, expchannel_t channel) {
36
  (void)extp;
37

    
38
  chSysLockFromISR();
39
  chEvtBroadcastFlagsI(&aos.events.io, (1 << channel));
40
  chSysUnlockFromISR();
41

    
42
  return;
43
}
44

    
45
/** @} */
46

    
47
/*===========================================================================*/
48
/**
49
 * @name ChibiOS/HAL configuration
50
 * @{
51
 */
52
/*===========================================================================*/
53

    
54
CANConfig moduleHalCanConfig = {
55
  /* mcr  */ CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
56
  /* btr  */ CAN_BTR_SJW(1) | CAN_BTR_TS2(2) | CAN_BTR_TS1(13) | CAN_BTR_BRP(1),
57
};
58

    
59
EXTConfig moduleHalExtConfig = {
60
  /* channel configrations */ {
61
    /* channel  0 */ {
62
      /* mode     */ EXT_CH_MODE_DISABLED,
63
      /* callback */ NULL,
64
    },
65
    /* channel  1 */ { // SYS_INT_N/SYS_SYNC_N: automatic interrupt on event
66
      /* mode     */ EXT_MODE_GPIOC | EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART,
67
      /* callback */ _moduleIsrCallback,
68
    },
69
    /* channel  2 */ { // SYS_WARMRST_N: automatic interrupt when activated
70
      /* mode     */ EXT_MODE_GPIOD | EXT_CH_MODE_FALLING_EDGE | EXT_CH_MODE_AUTOSTART,
71
      /* callback */ _moduleIsrCallback,
72
    },
73
    /* channel  3 */ { // PATH_DCSTAT: must be enabled explicitely when charging is in progress to detect unexpected voltage drop
74
      /* mode     */ EXT_MODE_GPIOC | EXT_CH_MODE_FALLING_EDGE,
75
      /* callback */ _moduleIsrCallback,
76
    },
77
    /* channel  4 */ {
78
      /* mode     */ EXT_CH_MODE_DISABLED,
79
      /* callback */ NULL,
80
    },
81
    /* channel  5 */ { // COMPASS_DRDY: must be enabled explicitely
82
      /* mode     */ EXT_MODE_GPIOB | APAL2CH_EDGE(HMC5883L_LLD_INT_EDGE),
83
      /* callback */ _moduleIsrCallback,
84
    },
85
    /* channel  6 */ {
86
      /* mode     */ EXT_CH_MODE_DISABLED,
87
      /* callback */ NULL,
88
    },
89
    /* channel  7 */ {
90
      /* mode     */ EXT_CH_MODE_DISABLED,
91
      /* callback */ NULL,
92
    },
93
    /* channel  8 */ { // SYS_PD_N: automatic interrupt when activated
94
      /* mode     */ EXT_MODE_GPIOC | EXT_CH_MODE_FALLING_EDGE | EXT_CH_MODE_AUTOSTART,
95
      /* callback */ _moduleIsrCallback,
96
    },
97
    /* channel  9 */ { // SYS_REG_EN: automatic interrupt when activated
98
      /* mode     */ EXT_MODE_GPIOC | EXT_CH_MODE_FALLING_EDGE | EXT_CH_MODE_AUTOSTART,
99
      /* callback */ _moduleIsrCallback,
100
    },
101
    /* channel 10 */ {
102
      /* mode     */ EXT_CH_MODE_DISABLED,
103
      /* callback */ NULL,
104
    },
105
    /* channel 11 */ {
106
      /* mode     */ EXT_CH_MODE_DISABLED,
107
      /* callback */ NULL,
108
    },
109
    /* channel 12 */ { // IR_INT: must be enabled explicitely
110
      /* mode     */ EXT_MODE_GPIOB | APAL2CH_EDGE(VCNL4020_LLD_INT_EDGE),
111
      /* callback */ _moduleIsrCallback,
112
    },
113
    /* channel 13 */ { // GYRO_DRDY: must be enabled explicitely
114
      /* mode     */ EXT_MODE_GPIOB | APAL2CH_EDGE(L3G4200D_LLD_INT_EDGE),
115
      /* callback */ _moduleIsrCallback,
116
    },
117
    /* channel 14 */ { // SYS_UART_UP: automatic interrupt on event
118
      /* mode     */ EXT_MODE_GPIOB | EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART,
119
      /* callback */ _moduleIsrCallback,
120
    },
121
    /* channel 15 */ { // ACCEL_INT_N: must be enabled explicitely
122
      /* mode     */ EXT_MODE_GPIOB | APAL2CH_EDGE(LIS331DLH_LLD_INT_EDGE),
123
      /* callback */ _moduleIsrCallback,
124
    },
125
    /* channel 16 */ {
126
      /* mode     */ EXT_CH_MODE_DISABLED,
127
      /* callback */ NULL,
128
    },
129
    /* channel 17 */ {
130
      /* mode     */ EXT_CH_MODE_DISABLED,
131
      /* callback */ NULL,
132
    },
133
    /* channel 18 */ {
134
      /* mode     */ EXT_CH_MODE_DISABLED,
135
      /* callback */ NULL,
136
    },
137
  },
138
};
139

    
140
I2CConfig moduleHalI2cCompassConfig = {
141
  /* I²C mode   */ OPMODE_I2C,
142
  /* frequency  */ 400000,
143
  /* duty cycle */ FAST_DUTY_CYCLE_2,
144
};
145

    
146
I2CConfig moduleHalI2cProxEepromPwrmtrConfig = {
147
  /* I²C mode   */ OPMODE_I2C,
148
  /* frequency  */ 400000,
149
  /* duty cycle */ FAST_DUTY_CYCLE_2,
150
};
151

    
152
PWMConfig moduleHalPwmDriveConfig = {
153
  /* frequency              */ 7200000,
154
  /* period                 */ 360,
155
  /* callback               */ NULL,
156
  /* channel configurations */ {
157
    /* channel 0              */ {
158
      /* mode                   */ PWM_OUTPUT_ACTIVE_HIGH,
159
      /* callback               */ NULL
160
    },
161
    /* channel 1              */ {
162
      /* mode                   */ PWM_OUTPUT_ACTIVE_HIGH,
163
      /* callback               */ NULL
164
    },
165
    /* channel 2              */ {
166
      /* mode                   */ PWM_OUTPUT_ACTIVE_HIGH,
167
      /* callback               */ NULL
168
    },
169
    /* channel 3              */ {
170
      /* mode                   */ PWM_OUTPUT_ACTIVE_HIGH,
171
      /* callback               */ NULL
172
    },
173
  },
174
  /* TIM CR2 register       */ 0,
175
#if STM32_PWM_USE_ADVANCED
176
  /* TIM BDTR register      */ 0,
177
#endif
178
  /* TIM DIER register      */ 0
179
};
180

    
181
QEIConfig moduleHalQeiConfig = {
182
  /* mode           */ QEI_COUNT_BOTH,
183
  /* channel config */ {
184
    /* channel 0 */ {
185
      /* input mode */ QEI_INPUT_NONINVERTED,
186
    },
187
    /* channel 1 */ {
188
      /* input mode */ QEI_INPUT_NONINVERTED,
189
    },
190
  },
191
  /* encoder range  */  0x10000u,
192
};
193

    
194
SerialConfig moduleHalProgIfConfig = {
195
  /* bit rate */ 115200,
196
  /* CR1      */ 0,
197
  /* CR1      */ 0,
198
  /* CR1      */ 0,
199
};
200

    
201
SPIConfig moduleHalSpiAccelerometerConfig = {
202
  /* callback function pointer    */ NULL,
203
  /* chip select line port        */ GPIOC,
204
  /* chip select line pad number  */ GPIOC_ACCEL_SS_N,
205
  /* CR1                          */ SPI_CR1_BR_0,
206
  /* CR2                          */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
207
};
208

    
209
SPIConfig moduleHalSpiGyroscopeConfig = {
210
  /* callback function pointer    */ NULL,
211
  /* chip select line port        */ GPIOC,
212
  /* chip select line pad number  */ GPIOC_GYRO_SS_N,
213
  /* CR1                          */ SPI_CR1_BR_0,
214
  /* CR2                          */ SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN,
215
};
216

    
217
/** @} */
218

    
219
/*===========================================================================*/
220
/**
221
 * @name GPIO definitions
222
 * @{
223
 */
224
/*===========================================================================*/
225

    
226
apalGpio_t moduleGpioLed = {
227
  /* port */ GPIOA,
228
  /* pad  */ GPIOA_LED,
229
};
230

    
231
apalGpio_t moduleGpioPowerEn = {
232
  /* port */GPIOB,
233
  /* pad  */  GPIOB_POWER_EN,
234
};
235

    
236
apalGpio_t moduleGpioCompassDrdy = {
237
  /* port */ GPIOB,
238
  /* pad  */ GPIOB_COMPASS_DRDY,
239
};
240

    
241
 apalGpio_t moduleGpioIrInt = {
242
  /* port */ GPIOB,
243
  /* pad  */ GPIOB_IR_INT,
244
};
245

    
246
apalGpio_t moduleGpioGyroDrdy = {
247
  /* port */ GPIOB,
248
  /* pad  */ GPIOB_GYRO_DRDY,
249
};
250

    
251
apalGpio_t moduleGpioSysUartUp = {
252
  /* port */ GPIOB,
253
  /* pad  */ GPIOB_SYS_UART_UP,
254
};
255

    
256
apalGpio_t moduleGpioAccelInt = {
257
  /* port */ GPIOB,
258
  /* pad  */ GPIOB_ACCEL_INT_N,
259
};
260

    
261
apalGpio_t moduleGpioSysSync = {
262
  /* port */ GPIOC,
263
  /* pad  */ GPIOC_SYS_INT_N,
264
};
265

    
266
apalGpio_t moduleGpioPathDcStat = {
267
  /* port */ GPIOC,
268
  /* pad  */ GPIOC_PATH_DCSTAT,
269
};
270

    
271
apalGpio_t moduleGpioPathDcEn = {
272
  /* port */ GPIOC,
273
  /* pad  */ GPIOC_PATH_DCEN,
274
};
275

    
276
apalGpio_t moduleGpioSysPd = {
277
  /* port */ GPIOC,
278
  /* pad  */ GPIOC_SYS_PD_N,
279
};
280

    
281
apalGpio_t moduleGpioSysRegEn = {
282
  /* port */ GPIOC,
283
  /* pad  */ GPIOC_SYS_REG_EN,
284
};
285

    
286
apalGpio_t moduleGpioSysWarmrst = {
287
  /* port */ GPIOD,
288
  /* pad  */ GPIOD_SYS_WARMRST_N,
289
};
290

    
291
/** @} */
292

    
293
/*===========================================================================*/
294
/**
295
 * @name AMiRo-OS core configurations
296
 * @{
297
 */
298
/*===========================================================================*/
299

    
300
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
301
const char* moduleShellPrompt = "DiWheelDrive";
302
#endif
303

    
304
/** @} */
305

    
306
/*===========================================================================*/
307
/**
308
 * @name Startup Shutdown Synchronization Protocol (SSSP)
309
 * @{
310
 */
311
/*===========================================================================*/
312

    
313
apalControlGpio_t moduleSsspGpioPd = {
314
  /* GPIO */ &moduleGpioSysPd,
315
  /* meta */ {
316
    /* active state */ APAL_GPIO_ACTIVE_LOW,
317
    /* edge         */ APAL_GPIO_EDGE_FALLING,
318
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
319
  },
320
};
321

    
322
apalControlGpio_t moduleSsspGpioSync = {
323
  /* GPIO */ &moduleGpioSysSync,
324
  /* meta */ {
325
    /* active state */ APAL_GPIO_ACTIVE_LOW,
326
    /* edge         */ APAL_GPIO_EDGE_FALLING,
327
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
328
  },
329
};
330

    
331
apalControlGpio_t moduleSsspGpioUp = {
332
  /* GPIO */ &moduleGpioSysUartUp,
333
  /* meta */ {
334
    /* active state */ APAL_GPIO_ACTIVE_LOW,
335
    /* edge         */ APAL_GPIO_EDGE_FALLING,
336
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
337
  },
338
};
339

    
340
/** @} */
341

    
342
/*===========================================================================*/
343
/**
344
 * @name Low-level drivers
345
 * @{
346
 */
347
/*===========================================================================*/
348

    
349
A3906Driver moduleLldMotors = {
350
  /* power enable GPIO  */ {
351
    /* GPIO */& moduleGpioPowerEn,
352
    /* meta */ {
353
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
354
      /* edge         */ APAL_GPIO_EDGE_NONE,
355
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
356
    },
357
  },
358
};
359

    
360
AT24C01BNDriver moduleLldEeprom = {
361
  /* I2C driver   */ &MODULE_HAL_I2C_PROX_EEPROM_PWRMTR,
362
  /* I²C address  */ AT24C01BN_LLD_I2C_ADDR_FIXED,
363
};
364

    
365
HMC5883LDriver moduleLldCompass = {
366
  /* I²C Driver */ &MODULE_HAL_I2C_COMPASS,
367
};
368

    
369
INA219Driver moduleLldPowerMonitorVdd = {
370
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_EEPROM_PWRMTR,
371
  /* I²C address      */ INA219_LLD_I2C_ADDR_FIXED,
372
  /* current LSB (uA) */ 0x00u,
373
  /* configuration    */ NULL,
374
};
375

    
376
L3G4200DDriver moduleLldGyroscope = {
377
  /* SPI Driver */ &MODULE_HAL_SPI_MOTION,
378
};
379

    
380
LEDDriver moduleLldStatusLed = {
381
  /* LED enable Gpio */ {
382
    /* GPIO       */ &moduleGpioLed,
383
    /* GPIO meta  */ {
384
      /* active state */ APAL_GPIO_ACTIVE_LOW,
385
      /* edge         */ APAL_GPIO_EDGE_NONE,
386
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
387
    },
388
  },
389
};
390

    
391
LIS331DLHDriver moduleLldAccelerometer = {
392
  /* SPI Driver */ &MODULE_HAL_SPI_MOTION,
393
};
394

    
395
LTC4412Driver moduleLldPowerPathController = {
396
  /* Control GPIO */ {
397
    /* GPIO       */ &moduleGpioPathDcEn,
398
    /* GPIO meta  */ {
399
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
400
      /* edge         */ APAL_GPIO_EDGE_NONE,
401
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
402
    },
403
  },
404
  /* Status GPIO */ {
405
    /* GPIO       */ &moduleGpioPathDcStat,
406
    /* GPIO meta  */ {
407
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
408
      /* edge         */ APAL_GPIO_EDGE_NONE,
409
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
410
    },
411
  },
412
};
413

    
414
PCA9544ADriver moduleLldI2cMultiplexer = {
415
  /* I²C driver   */ &MODULE_HAL_I2C_PROX_EEPROM_PWRMTR,
416
  /* I²C address  */ PCA9544A_LLD_I2C_ADDR_FIXED | PCA9544A_LLD_I2C_ADDR_A0 | PCA9544A_LLD_I2C_ADDR_A1 | PCA9544A_LLD_I2C_ADDR_A2,
417
};
418

    
419
TPS62113Driver moduleLldStepDownConverterVdrive = {
420
  /* Power enable Gpio */ {
421
    /* GPIO       */ &moduleGpioPowerEn,
422
    /* GPIO meta  */ {
423
      /* 'power on' state */ APAL_GPIO_ACTIVE_HIGH,
424
      /* edge             */ APAL_GPIO_EDGE_NONE,
425
      /* direction        */ APAL_GPIO_DIRECTION_OUTPUT,
426
    },
427
  },
428
};
429

    
430
VCNL4020Driver moduleLldProximity = {
431
  /* I²C Driver */ &MODULE_HAL_I2C_PROX_EEPROM_PWRMTR,
432
};
433

    
434
/** @} */
435

    
436
/*===========================================================================*/
437
/**
438
 * @name Unit tests (UT)
439
 * @{
440
 */
441
/*===========================================================================*/
442
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
443
#include <string.h>
444
#include <chprintf.h>
445

    
446
/* A3906 (motor driver) */
447
static int _utShellCmdCb_AlldA3906(BaseSequentialStream* stream, int argc, char* argv[])
448
{
449
  (void)argc;
450
  (void)argv;
451
  aosUtRun(stream, &moduleUtAlldA3906, NULL);
452
  return AOS_OK;
453
}
454
static ut_a3906data_t _utA3906Data = {
455
  /* driver           */ &moduleLldMotors,
456
  /* PWM information  */ {
457
    /* driver   */ &MODULE_HAL_PWM_DRIVE,
458
    /* channels */ {
459
      /* left wheel forward   */ MODULE_HAL_PWM_DRIVE_CHANNEL_LEFT_FORWARD,
460
      /* left wheel backward  */ MODULE_HAL_PWM_DRIVE_CHANNEL_LEFT_BACKWARD,
461
      /* right wheel forward  */ MODULE_HAL_PWM_DRIVE_CHANNEL_RIGHT_FORWARD,
462
      /* right wheel backward */ MODULE_HAL_PWM_DRIVE_CHANNEL_RIGHT_BACKWARD,
463
    },
464
  },
465
  /* QEI information  */ {
466
    /* left wheel               */ &MODULE_HAL_QEI_LEFT_WHEEL,
467
    /* right wheel              */ &MODULE_HAL_QEI_RIGHT_WHEEL,
468
    /* increment per revolution */ MODULE_HAL_QEI_INCREMENTS_PER_REVOLUTION,
469
  },
470
  /* Wheel diameter   */ 0.05571f,
471
  /* timeout          */ 10* MICROSECONDS_PER_SECOND,
472
};
473
aos_unittest_t moduleUtAlldA3906  = {
474
  /* name           */ "A3906",
475
  /* info           */ "motor driver",
476
  /* test function  */ utAlldA3906Func,
477
  /* shell command  */ {
478
    /* name     */ "unittest:MotorDriver",
479
    /* callback */ _utShellCmdCb_AlldA3906,
480
    /* next     */ NULL,
481
  },
482
  /* data           */ &_utA3906Data,
483
};
484

    
485
/* AT24C01BN (EEPROM) */
486
static int _utShellCmdCb_AlldAt24c01bn(BaseSequentialStream* stream, int argc, char* argv[])
487
{
488
  (void)argc;
489
  (void)argv;
490
  aosUtRun(stream, &moduleUtAlldAt24c01bn, NULL);
491
  return AOS_OK;
492
}
493
static ut_at24c01bndata_t _utAt24c01bnData = {
494
  /* driver   */ &moduleLldEeprom,
495
  /* timeout  */ MICROSECONDS_PER_SECOND,
496
};
497
aos_unittest_t moduleUtAlldAt24c01bn = {
498
  /* name           */ "AT24C01BN-SH-B",
499
  /* info           */ "1kbit EEPROM",
500
  /* test function  */ utAlldAt24c01bnFunc,
501
  /* shell command  */ {
502
    /* name     */ "unittest:EEPROM",
503
    /* callback */ _utShellCmdCb_AlldAt24c01bn,
504
    /* next     */ NULL,
505
  },
506
  /* data           */ &_utAt24c01bnData,
507
};
508

    
509
/* HMC5883L (compass) */
510
static int _utShellCmdCb_AlldHmc5883l(BaseSequentialStream* stream, int argc, char* argv[])
511
{
512
  (void)argc;
513
  (void)argv;
514
  extChannelEnable(&MODULE_HAL_EXT, MODULE_GPIO_EXTCHANNEL_COMPASSDRDY);
515
  aosUtRun(stream, &moduleUtAlldHmc5883l, NULL);
516
  return AOS_OK;
517
}
518
static ut_hmc5883ldata_t _utHmc5883lData = {
519
  /* HMC driver   */ &moduleLldCompass,
520
  /* event source */ &aos.events.io,
521
  /* event flags  */ (1 << MODULE_GPIO_EXTCHANNEL_COMPASSDRDY),
522
  /* timeout      */ MICROSECONDS_PER_SECOND,
523
};
524
aos_unittest_t moduleUtAlldHmc5883l = {
525
  /* name           */ "HMC5883L",
526
  /* info           */ "compass",
527
  /* test function  */ utAlldHmc5883lFunc,
528
  /* shell command  */ {
529
    /* name     */ "unittest:Compass",
530
    /* callback */ _utShellCmdCb_AlldHmc5883l,
531
    /* next     */ NULL,
532
  },
533
  /* data           */ &_utHmc5883lData,
534
};
535

    
536
/* INA219 (power monitor) */
537
static int _utShellCmdCb_AlldIna219(BaseSequentialStream* stream, int argc, char* argv[])
538
{
539
  (void)argc;
540
  (void)argv;
541
  aosUtRun(stream, &moduleUtAlldIna219, "VDD (3.3V)");
542
  return AOS_OK;
543
}
544
static ut_ina219data_t _utIna219Data = {
545
  /* driver           */ &moduleLldPowerMonitorVdd,
546
  /* expected voltage */ 3.3f,
547
  /* tolerance        */ 0.05f,
548
  /* timeout */ MICROSECONDS_PER_SECOND,
549
};
550
aos_unittest_t moduleUtAlldIna219 = {
551
  /* name           */ "INA219",
552
  /* info           */ "power monitor",
553
  /* test function  */ utAlldIna219Func,
554
  /* shell command  */ {
555
    /* name     */ "unittest:PowerMonitor",
556
    /* callback */ _utShellCmdCb_AlldIna219,
557
    /* next     */ NULL,
558
  },
559
  /* data           */ &_utIna219Data,
560
};
561

    
562
/* L3G4200D (gyroscope) */
563
static int _utShellCmdCb_AlldL3g4200d(BaseSequentialStream* stream, int argc, char* argv[])
564
{
565
  (void)argc;
566
  (void)argv;
567
  extChannelEnable(&MODULE_HAL_EXT, MODULE_GPIO_EXTCHANNEL_GYRODRDY);
568
  spiStart(((ut_l3g4200ddata_t*)moduleUtAlldL3g4200d.data)->l3gd->spid, ((ut_l3g4200ddata_t*)moduleUtAlldL3g4200d.data)->spiconf);
569
  aosUtRun(stream, &moduleUtAlldL3g4200d, NULL);
570
  spiStop(((ut_l3g4200ddata_t*)moduleUtAlldL3g4200d.data)->l3gd->spid);
571
  return AOS_OK;
572
}
573
static ut_l3g4200ddata_t _utL3g4200dData = {
574
  /* driver            */ &moduleLldGyroscope,
575
  /* SPI configuration */ &moduleHalSpiGyroscopeConfig,
576
  /* event source */ &aos.events.io,
577
  /* event flags  */ (1 << MODULE_GPIO_EXTCHANNEL_GYRODRDY),
578
};
579
aos_unittest_t moduleUtAlldL3g4200d = {
580
  /* name           */ "L3G4200D",
581
  /* info           */ "Gyroscope",
582
  /* test function  */ utAlldL3g4200dFunc,
583
  /* shell command  */ {
584
    /* name     */ "unittest:Gyroscope",
585
    /* callback */ _utShellCmdCb_AlldL3g4200d,
586
    /* next     */ NULL,
587
  },
588
  /* data           */ &_utL3g4200dData,
589
};
590

    
591
/* Status LED */
592
static int _utShellCmdCb_AlldLed(BaseSequentialStream* stream, int argc, char* argv[])
593
{
594
  (void)argc;
595
  (void)argv;
596
  aosUtRun(stream, &moduleUtAlldLed, NULL);
597
  return AOS_OK;
598
}
599
aos_unittest_t moduleUtAlldLed = {
600
  /* name           */ "LED",
601
  /* info           */ NULL,
602
  /* test function  */ utAlldLedFunc,
603
  /* shell command  */ {
604
    /* name     */ "unittest:StatusLED",
605
    /* callback */ _utShellCmdCb_AlldLed,
606
    /* next     */ NULL,
607
  },
608
  /* data           */ &moduleLldStatusLed,
609
};
610

    
611
/* LIS331DLH (accelerometer) */
612
static int _utShellCmdCb_AlldLis331dlh(BaseSequentialStream* stream, int argc, char* argv[])
613
{
614
  (void)argc;
615
  (void)argv;
616
  extChannelEnable(&MODULE_HAL_EXT, MODULE_GPIO_EXTCHANNEL_ACCELINT);
617
  spiStart(((ut_lis331dlhdata_t*)moduleUtAlldLis331dlh.data)->lisd->spid, ((ut_lis331dlhdata_t*)moduleUtAlldLis331dlh.data)->spiconf);
618
  aosUtRun(stream, &moduleUtAlldLis331dlh, NULL);
619
  spiStop(((ut_lis331dlhdata_t*)moduleUtAlldLis331dlh.data)->lisd->spid);
620
  return AOS_OK;
621
}
622
static ut_lis331dlhdata_t _utLis331dlhData = {
623
  /* driver            */ &moduleLldAccelerometer,
624
  /* SPI configuration */ &moduleHalSpiAccelerometerConfig,
625
  /* event source */ &aos.events.io,
626
  /* event flags  */ (1 << MODULE_GPIO_EXTCHANNEL_ACCELINT),
627
};
628
aos_unittest_t moduleUtAlldLis331dlh = {
629
  /* name           */ "LIS331DLH",
630
  /* info           */ "Accelerometer",
631
  /* test function  */ utAlldLis331dlhFunc,
632
  /* shell command  */ {
633
    /* name     */ "unittest:Accelerometer",
634
    /* callback */ _utShellCmdCb_AlldLis331dlh,
635
    /* next     */ NULL,
636
  },
637
  /* data           */ &_utLis331dlhData,
638
};
639

    
640
/* LTC4412 (power path controller) */
641
static int _utShellCmdCb_AlldLtc4412(BaseSequentialStream* stream, int argc, char* argv[])
642
{
643
  (void)argc;
644
  (void)argv;
645
  aosUtRun(stream, &moduleUtAlldLtc4412, NULL);
646
  return AOS_OK;
647
}
648
aos_unittest_t moduleUtAlldLtc4412 = {
649
  /* name           */ "LTC4412",
650
  /* info           */ "Power path controller",
651
  /* test function  */ utAlldLtc4412Func,
652
  /* shell command  */ {
653
    /* name     */ "unittest:PowerPathController",
654
    /* callback */ _utShellCmdCb_AlldLtc4412,
655
    /* next     */ NULL,
656
  },
657
  /* data           */ &moduleLldPowerPathController,
658
};
659

    
660
/* PCA9544A (I2C multiplexer) */
661
static int _utShellCmdCb_AlldPca9544a(BaseSequentialStream* stream, int argc, char* argv[])
662
{
663
  (void)argc;
664
  (void)argv;
665
  aosUtRun(stream, &moduleUtAlldPca9544a, NULL);
666
  return AOS_OK;
667
}
668
static ut_pca9544adata_t _utPca9544aData = {
669
  /* driver  */ &moduleLldI2cMultiplexer,
670
  /* timeout */ MICROSECONDS_PER_SECOND,
671
};
672
aos_unittest_t moduleUtAlldPca9544a = {
673
  /* name           */ "PCA9544A",
674
  /* info           */ "I2C multiplexer",
675
  /* test function  */ utAlldPca9544aFunc,
676
  /* shell command  */ {
677
    /* name     */ "unittest:I2CMultiplexer",
678
    /* callback */ _utShellCmdCb_AlldPca9544a,
679
    /* next     */ NULL,
680
  },
681
  /* data           */ &_utPca9544aData,
682
};
683

    
684
/* TPS62113 (step-down converter) */
685
static int _utShellCmdCb_AlldTps62113(BaseSequentialStream* stream, int argc, char* argv[])
686
{
687
  (void)argc;
688
  (void)argv;
689
  aosUtRun(stream, &moduleUtAlldTps62113, NULL);
690
  return AOS_OK;
691
}
692
aos_unittest_t moduleUtAlldTps62113 = {
693
  /* name           */ "TPS62113",
694
  /* info           */ "Step down converter",
695
  /* test function  */ utAlldTps62113Func,
696
  /* shell command  */ {
697
    /* name     */ "unittest:StepDownConverter",
698
    /* callback */ _utShellCmdCb_AlldTps62113,
699
    /* next     */ NULL,
700
  },
701
  /* data           */ &moduleLldStepDownConverterVdrive,
702
};
703

    
704
/* VCNL4020 (proximity sensor) */
705
static void _utAlldVcnl4020_disableInterrupt(VCNL4020Driver* vcnl)
706
{
707
  uint8_t intstatus;
708
  vcnl4020_lld_writereg(vcnl, VCNL4020_LLD_REGADDR_INTCTRL, 0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
709
  vcnl4020_lld_readreg(vcnl, VCNL4020_LLD_REGADDR_INTSTATUS, &intstatus, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
710
  if (intstatus) {
711
    vcnl4020_lld_writereg(vcnl, VCNL4020_LLD_REGADDR_INTSTATUS, intstatus, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
712
  }
713
  return;
714
}
715
static int _utShellCmdCb_AlldVcnl4020(BaseSequentialStream* stream, int argc, char* argv[])
716
{
717
  enum {
718
    UNKNOWN,
719
    FL, FR, WL, WR,
720
  } sensor = UNKNOWN;
721
  // evaluate arguments
722
  if (argc == 2) {
723
    if (strcmp(argv[1], "--frontleft") == 0 || strcmp(argv[1], "-fl") == 0) {
724
      sensor = FL;
725
    } else if (strcmp(argv[1], "--frontright") == 0 || strcmp(argv[1], "-fr") == 0) {
726
      sensor = FR;
727
    } else if (strcmp(argv[1], "--wheelleft") == 0 || strcmp(argv[1], "-wl") == 0) {
728
      sensor = WL;
729
    } else if (strcmp(argv[1], "--wheelright") == 0 || strcmp(argv[1], "-wr") == 0) {
730
      sensor = WR;
731
    }
732
  }
733
  if (sensor != UNKNOWN) {
734
    pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
735
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
736
    pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH1, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
737
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
738
    pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH2, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
739
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
740
    pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH3, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
741
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
742
    extChannelEnable(&MODULE_HAL_EXT, MODULE_GPIO_EXTCHANNEL_IRINT);
743
    switch (sensor) {
744
      case FL:
745
        pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH3, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
746
        aosUtRun(stream, &moduleUtAlldVcnl4020, "front left snesor");
747
        break;
748
      case FR:
749
        pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
750
        aosUtRun(stream, &moduleUtAlldVcnl4020, "front right sensor");
751
        break;
752
      case WL:
753
        pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH2, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
754
        aosUtRun(stream, &moduleUtAlldVcnl4020, "left wheel sensor");
755
        break;
756
      case WR:
757
        pca9544a_lld_setchannel(&moduleLldI2cMultiplexer, PCA9544A_LLD_CH1, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
758
        aosUtRun(stream, &moduleUtAlldVcnl4020, "right wheel sensor");
759
        break;
760
      default:
761
        break;
762
    }
763
    return AOS_OK;
764
  }
765
  // print help
766
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
767
  chprintf(stream, "Options:\n");
768
  chprintf(stream, "  --frontleft, -fl\n");
769
  chprintf(stream, "    Test front left proximity sensor.\n");
770
  chprintf(stream, "  --frontrigt, -fr\n");
771
  chprintf(stream, "    Test front right proximity sensor.\n");
772
  chprintf(stream, "  --wheelleft, -wl\n");
773
  chprintf(stream, "    Test left wheel proximity sensor.\n");
774
  chprintf(stream, "  --wheelright, -wr\n");
775
  chprintf(stream, "    Test right wheel proximity sensor.\n");
776
  return AOS_INVALID_ARGUMENTS;
777
}
778
static ut_vcnl4020data_t _utVcnl4020Data = {
779
  /* driver       */ &moduleLldProximity,
780
  /* timeout      */ MICROSECONDS_PER_SECOND,
781
  /* event source */ &aos.events.io,
782
  /* event flags  */ (1 << MODULE_GPIO_EXTCHANNEL_IRINT),
783
};
784
aos_unittest_t moduleUtAlldVcnl4020 = {
785
  /* name           */ "VCNL4020",
786
  /* info           */ "proximity sensor",
787
  /* test function  */ utAlldVcnl4020Func,
788
  /* shell command  */ {
789
    /* name     */ "unittest:Proximity",
790
    /* callback */ _utShellCmdCb_AlldVcnl4020,
791
    /* next     */ NULL,
792
  },
793
  /* data           */ &_utVcnl4020Data,
794
};
795

    
796
#endif /* AMIROOS_CFG_TESTS_ENABLE == true */
797

    
798
/** @} */