Statistics
| Branch: | Tag: | Revision:

amiro-os / modules / PowerManagement_1-1 / module.c @ 57d411d6

History | View | Annotate | Download (39.133 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   args      Channel on which the interrupt was encountered.
33
 */
34
static void _modulePalIsrCallback(void *args) {
35
  chSysLockFromISR();
36
  chEvtBroadcastFlagsI(&aos.events.io, (1 << (*(uint16_t*)args)));
37
  chSysUnlockFromISR();
38

    
39
  return;
40
}
41

    
42
/** @} */
43

    
44
/*===========================================================================*/
45
/**
46
 * @name ChibiOS/HAL configuration
47
 * @{
48
 */
49
/*===========================================================================*/
50

    
51
ADCConversionGroup moduleHalAdcVsysConversionGroup = {
52
  /* buffer type        */ true,
53
  /* number of channels */ 1,
54
  /* callback function  */ NULL,
55
  /* error callback     */ NULL,
56
  /* CR1                */ ADC_CR1_AWDEN | ADC_CR1_AWDIE,
57
  /* CR2                */ ADC_CR2_SWSTART | ADC_CR2_CONT,
58
  /* SMPR1              */ 0,
59
  /* SMPR2              */ ADC_SMPR2_SMP_AN9(ADC_SAMPLE_480),
60
  /* HTR                */ ADC_HTR_HT,
61
  /* LTR                */ 0,
62
  /* SQR1               */ ADC_SQR1_NUM_CH(1),
63
  /* SQR2               */ 0,
64
  /* SQR3               */ ADC_SQR3_SQ1_N(ADC_CHANNEL_IN9),
65
};
66

    
67
CANConfig moduleHalCanConfig = {
68
  /* mcr  */ CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
69
  /* btr  */ CAN_BTR_SJW(1) | CAN_BTR_TS2(3) | CAN_BTR_TS1(15) | CAN_BTR_BRP(1),
70
};
71

    
72
aos_interrupt_cfg_t moduleIntConfig[14] = {
73
    /* channel  1 */ { // IR_INT1_N: must be enabled explicitely
74
      /* port     */ GPIOB,
75
      /* pad      */ GPIOB_IR_INT1_N,
76
      /* flags    */ 0,
77
      /* mode     */ APAL2CH_EDGE(VCNL4020_LLD_INT_EDGE),
78
      /* callback */ _modulePalIsrCallback,
79
      /* cb arg   */ 1,
80
    },
81
    /* channel  2 */ { // GAUGE_BATLOW1: must be enabled explicitely
82
      /* port     */ GPIOC,
83
      /* pad      */ GPIOC_GAUGE_BATLOW1,
84
      /* flags    */ 0,
85
      /* mode     */ PAL_EVENT_MODE_RISING_EDGE,
86
      /* callback */ _modulePalIsrCallback,
87
      /* cb arg   */ 2,
88
    },
89
    /* channel  3 */ { // GAUGE_BATGD1_N: must be enabled explicitely
90
      /* port     */ GPIOC,
91
      /* pad      */ GPIOC_GAUGE_BATGD1_N,
92
      /* flags    */ 0,
93
      /* mode     */ PAL_EVENT_MODE_RISING_EDGE,
94
      /* callback */ _modulePalIsrCallback,
95
      /* cb arg   */ 3,
96
    },
97
    /* channel  4 */ { // SYS_UART_DN: automatic interrupt on event
98
      /* port     */ GPIOB,
99
      /* pad      */ GPIOB_SYS_UART_DN,
100
      /* flags    */ AOS_INTERRUPT_AUTOSTART,
101
      /* mode     */ PAL_EVENT_MODE_BOTH_EDGES,
102
      /* callback */ _modulePalIsrCallback,
103
      /* cb arg   */ 4,
104
    },
105
    /* channel  5 */ { // IR_INT2_N: must be enabled explicitely
106
      /* port     */ GPIOC,
107
      /* pad      */ GPIOC_IR_INT2_N,
108
      /* flags    */ 0,
109
      /* mode     */ APAL2CH_EDGE(VCNL4020_LLD_INT_EDGE),
110
      /* callback */ _modulePalIsrCallback,
111
      /* cb arg   */ 5,
112
    },
113
    /* channel  6 */ { // TOUCH_INT: must be enabled explicitely
114
      /* port     */ GPIOC,
115
      /* pad      */ GPIOC_TOUCH_INT_N,
116
      /* flags    */ 0,
117
      /* mode     */ APAL2CH_EDGE(MPR121_LLD_INT_EDGE),
118
      /* callback */ _modulePalIsrCallback,
119
      /* cb arg   */ 6,
120
    },
121
    /* channel  7 */ { // GAUGE_BATLOW2: must be enabled explicitely
122
      /* port     */ GPIOB,
123
      /* pad      */ GPIOB_GAUGE_BATLOW2,
124
      /* flags    */ 0,
125
      /* mode     */ PAL_EVENT_MODE_RISING_EDGE,
126
      /* callback */ _modulePalIsrCallback,
127
      /* cb arg   */ 7,
128
    },
129
    /* channel  8 */ { // GAUGE_BATGD2_N: must be enabled explicitely
130
      /* port     */ GPIOB,
131
      /* pad      */ GPIOB_GAUGE_BATGD2_N,
132
      /* flags    */ 0,
133
      /* mode     */ PAL_EVENT_MODE_RISING_EDGE,
134
      /* callback */ _modulePalIsrCallback,
135
      /* cb arg   */ 8,
136
    },
137
    /* channel  9 */ { // PATH_DC: must be enabled explicitely
138
      /* port     */ GPIOC,
139
      /* pad      */ GPIOC_PATH_DC,
140
      /* flags    */ 0,
141
      /* mode     */ PAL_EVENT_MODE_BOTH_EDGES,
142
      /* callback */ _modulePalIsrCallback,
143
      /* cb arg   */ 9,
144
    },
145
    /* channel 10 */ { // SYS_SPI_DIR: must be enabled explicitely
146
      /* port     */ GPIOC,
147
      /* pad      */ GPIOC_SYS_SPI_DIR,
148
      /* flags    */ 0,
149
      /* mode     */ PAL_EVENT_MODE_BOTH_EDGES,
150
      /* callback */ _modulePalIsrCallback,
151
      /* cb arg   */ 10,
152
    },
153
    /* channel 11 */ { // SYS_SYNC_N: must be enabled explicitely
154
      /* port     */ GPIOC,
155
      /* pad      */ GPIOC_SYS_INT_N, // TODO: check this
156
      /* flags    */ AOS_INTERRUPT_AUTOSTART,
157
      /* mode     */ PAL_EVENT_MODE_BOTH_EDGES,
158
      /* callback */ _modulePalIsrCallback,
159
      /* cb arg   */ 11,
160
    },
161
    /* channel 12 */ { // SYS_PD_N: must be enabled explicitely
162
      /* port     */ GPIOC,
163
      /* pad      */ GPIOC_SYS_PD_N,
164
      /* flags    */ AOS_INTERRUPT_AUTOSTART,
165
      /* mode     */ PAL_EVENT_MODE_FALLING_EDGE,
166
      /* callback */ _modulePalIsrCallback,
167
      /* cb arg   */ 12,
168
    },
169
    /* channel 13 */ { // SYS_WARMRST_N: must be enabled explicitely
170
      /* port     */ GPIOC,
171
      /* pad      */ GPIOC_SYS_WARMRST_N,
172
      /* flags    */ AOS_INTERRUPT_AUTOSTART,
173
      /* mode     */ PAL_EVENT_MODE_FALLING_EDGE,
174
      /* callback */ _modulePalIsrCallback,
175
      /* cb arg   */ 13,
176
    },
177
    /* channel 14 */ { // SYS_UART_UP: must be enabled explicitely
178
      /* port     */ GPIOB,
179
      /* pad      */ GPIOB_SYS_UART_UP,
180
      /* flags    */ AOS_INTERRUPT_AUTOSTART,
181
      /* mode     */ PAL_EVENT_MODE_BOTH_EDGES,
182
      /* callback */ _modulePalIsrCallback,
183
      /* cb arg   */ 14,
184
    },
185
};
186

    
187
aos_interrupt_driver_t moduleIntDriver = {
188
  /* config     */ NULL,
189
  /* interrupts */ 14,
190
};
191

    
192
I2CConfig moduleHalI2cProxPm18Pm33GaugeRearConfig = {
193
  /* I²C mode   */ OPMODE_I2C,
194
  /* frequency  */ 400000, // TODO: replace with some macro (-> ChibiOS/HAL)
195
  /* duty cycle */ FAST_DUTY_CYCLE_2,
196
};
197

    
198
I2CConfig moduleHalI2cProxPm42Pm50PmVddEepromTouchGaugeFrontConfig = {
199
  /* I²C mode   */ OPMODE_I2C,
200
  /* frequency  */ 400000, // TODO: replace with some macro (-> ChibiOS/HAL)
201
  /* duty cycle */ FAST_DUTY_CYCLE_2,
202
};
203

    
204
PWMConfig moduleHalPwmBuzzerConfig = {
205
  /* frequency              */ 1000000,
206
  /* period                 */ 0,
207
  /* callback               */ NULL,
208
  /* channel configurations */ {
209
    /* channel 0              */ {
210
      /* mode                   */ PWM_OUTPUT_DISABLED,
211
      /* callback               */ NULL
212
    },
213
    /* channel 1              */ {
214
      /* mode                   */ PWM_OUTPUT_ACTIVE_HIGH,
215
      /* callback               */ NULL
216
    },
217
    /* channel 2              */ {
218
      /* mode                   */ PWM_OUTPUT_DISABLED,
219
      /* callback               */ NULL
220
    },
221
    /* channel 3              */ {
222
      /* mode                   */ PWM_OUTPUT_DISABLED,
223
      /* callback               */ NULL
224
    },
225
  },
226
  /* TIM CR2 register       */ 0,
227
#if STM32_PWM_USE_ADVANCED
228
  /* TIM BDTR register      */ 0,
229
#endif
230
  /* TIM DIER register      */ 0,
231
};
232

    
233
SerialConfig moduleHalProgIfConfig = {
234
  /* bit rate */ 115200,
235
  /* CR1      */ 0,
236
  /* CR1      */ 0,
237
  /* CR1      */ 0,
238
};
239

    
240
/** @} */
241

    
242
/*===========================================================================*/
243
/**
244
 * @name GPIO definitions
245
 * @{
246
 */
247
/*===========================================================================*/
248

    
249
apalGpio_t moduleGpioSysRegEn = {
250
  /* port */ GPIOA,
251
  /* pad  */ GPIOA_SYS_REG_EN,
252
};
253

    
254
apalGpio_t moduleGpioIrInt1 = {
255
  /* port */ GPIOB,
256
  /* pad  */ GPIOB_IR_INT1_N,
257
};
258

    
259
apalGpio_t moduleGpioPowerEn = {
260
  /* port */ GPIOB,
261
  /* pad  */ GPIOB_POWER_EN,
262
};
263

    
264
apalGpio_t moduleGpioSysUartDn = {
265
  /* port */ GPIOB,
266
  /* pad  */ GPIOB_SYS_UART_DN,
267
};
268

    
269
apalGpio_t moduleGpioChargeStat2A = {
270
  /* port */ GPIOB,
271
  /* pad  */ GPIOB_CHARGE_STAT2A,
272
};
273

    
274
apalGpio_t moduleGpioGaugeBatLow2 = {
275
  /* port */ GPIOB,
276
  /* pad  */ GPIOB_GAUGE_BATLOW2,
277
};
278

    
279
apalGpio_t moduleGpioGaugeBatGd2 = {
280
  /* port */ GPIOB,
281
  /* pad  */ GPIOB_GAUGE_BATGD2_N,
282
};
283

    
284
apalGpio_t moduleGpioLed = {
285
  /* port */ GPIOB,
286
  /* pad  */ GPIOB_LED,
287
};
288

    
289
apalGpio_t moduleGpioSysUartUp = {
290
  /* port */ GPIOB,
291
  /* pad  */ GPIOB_SYS_UART_UP,
292
};
293

    
294
apalGpio_t moduleGpioChargeStat1A = {
295
  /* port */ GPIOC,
296
  /* pad  */ GPIOC_CHARGE_STAT1A,
297
};
298

    
299
apalGpio_t moduleGpioGaugeBatLow1 = {
300
  /* port */ GPIOC,
301
  /* pad  */ GPIOC_GAUGE_BATLOW1,
302
};
303

    
304
apalGpio_t moduleGpioGaugeBatGd1 = {
305
  /* port */ GPIOC,
306
  /* pad  */ GPIOC_GAUGE_BATGD1_N,
307
};
308

    
309
apalGpio_t moduleGpioChargeEn1 = {
310
  /* port */ GPIOC,
311
  /* pad  */ GPIOC_CHARGE_EN1_N,
312
};
313

    
314
apalGpio_t moduleGpioIrInt2 = {
315
  /* port */ GPIOC,
316
  /* pad  */ GPIOC_IR_INT2_N,
317
};
318

    
319
apalGpio_t moduleGpioTouchInt = {
320
  /* port */ GPIOC,
321
  /* pad  */ GPIOC_TOUCH_INT_N,
322
};
323

    
324
apalGpio_t moduleGpioSysDone = {
325
  /* port */ GPIOC,
326
  /* pad  */ GPIOC_SYS_DONE,
327
};
328

    
329
apalGpio_t moduleGpioSysProg = {
330
  /* port */ GPIOC,
331
  /* pad  */ GPIOC_SYS_PROG_N,
332
};
333

    
334
apalGpio_t moduleGpioPathDc = {
335
  /* port */ GPIOC,
336
  /* pad  */ GPIOC_PATH_DC,
337
};
338

    
339
apalGpio_t moduleGpioSysSpiDir = {
340
  /* port */ GPIOC,
341
  /* pad  */ GPIOC_SYS_SPI_DIR,
342
};
343

    
344
apalGpio_t moduleGpioSysSync = {
345
  /* port */ GPIOC,
346
  /* pad  */ GPIOC_SYS_INT_N,
347
};
348

    
349
apalGpio_t moduleGpioSysPd = {
350
  /* port */ GPIOC,
351
  /* pad  */ GPIOC_SYS_PD_N,
352
};
353

    
354
apalGpio_t moduleGpioSysWarmrst = {
355
  /* port */ GPIOC,
356
  /* pad  */ GPIOC_SYS_WARMRST_N,
357
};
358

    
359
apalGpio_t moduleGpioBtRst = {
360
  /* port */ GPIOC,
361
  /* pad  */ GPIOC_BT_RST,
362
};
363

    
364
apalGpio_t moduleGpioChargeEn2 = {
365
  /* port */ GPIOD,
366
  /* pad  */ GPIOD_CHARGE_EN2_N,
367
};
368

    
369
/** @} */
370

    
371
/*===========================================================================*/
372
/**
373
 * @name AMiRo-OS core configurations
374
 * @{
375
 */
376
/*===========================================================================*/
377

    
378
#if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
379
const char* moduleShellPrompt = "PowerManagement";
380
#endif
381

    
382
/** @} */
383

    
384
/*===========================================================================*/
385
/**
386
 * @name Startup Shutdown Synchronization Protocol (SSSP)
387
 * @{
388
 */
389
/*===========================================================================*/
390

    
391
apalControlGpio_t moduleSsspGpioPd = {
392
  /* GPIO */ &moduleGpioSysPd,
393
  /* meta */ {
394
    /* active state */ APAL_GPIO_ACTIVE_LOW,
395
    /* edge         */ APAL_GPIO_EDGE_FALLING,
396
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
397
  },
398
};
399

    
400
apalControlGpio_t moduleSsspGpioSync = {
401
  /* GPIO */ &moduleGpioSysSync,
402
  /* meta */ {
403
    /* active state */ APAL_GPIO_ACTIVE_LOW,
404
    /* edge         */ APAL_GPIO_EDGE_FALLING,
405
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
406
  },
407
};
408

    
409
apalControlGpio_t moduleSsspGpioDn = {
410
  /* GPIO */ &moduleGpioSysUartDn,
411
  /* meta */ {
412
    /* active state */ APAL_GPIO_ACTIVE_LOW,
413
    /* edge         */ APAL_GPIO_EDGE_FALLING,
414
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
415
  },
416
};
417

    
418
apalControlGpio_t moduleSsspGpioUp = {
419
  /* GPIO */ &moduleGpioSysUartUp,
420
  /* meta */ {
421
    /* active state */ APAL_GPIO_ACTIVE_LOW,
422
    /* edge         */ APAL_GPIO_EDGE_FALLING,
423
    /* direction    */ APAL_GPIO_DIRECTION_BIDIRECTIONAL,
424
  },
425
};
426

    
427
/** @} */
428

    
429
/*===========================================================================*/
430
/**
431
 * @name Low-level drivers
432
 * @{
433
 */
434
/*===========================================================================*/
435

    
436
AT24C01BNDriver moduleLldEeprom = {
437
  /* I2C driver   */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
438
  /* I2C address  */ AT24C01BN_LLD_I2C_ADDR_FIXED,
439
};
440

    
441
BQ24103ADriver moduleLldBatteryChargerFront = {
442
  /* charge enable GPIO */ {
443
    /* GPIO */ &moduleGpioChargeEn1,
444
    /* meta */ {
445
      /* active state */ APAL_GPIO_ACTIVE_LOW,
446
      /* edge         */ APAL_GPIO_EDGE_NONE,
447
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
448
    },
449
  },
450
  /* charge status GPIO */ {
451
    /* GPIO */ &moduleGpioChargeStat1A,
452
    /* meta */ {
453
      /* active state */ APAL_GPIO_ACTIVE_LOW,
454
      /* edge         */ APAL_GPIO_EDGE_NONE,
455
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
456
    },
457
  },
458
};
459

    
460
BQ24103ADriver moduleLldBatteryChargerRear = {
461
  /* charge enable GPIO */ {
462
    /* GPIO */ &moduleGpioChargeEn2,
463
    /* meta */ {
464
      /* active state */ APAL_GPIO_ACTIVE_LOW,
465
      /* edge         */ APAL_GPIO_EDGE_NONE,
466
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
467
    },
468
  },
469
  /* charge status GPIO */ {
470
    /* GPIO */ &moduleGpioChargeStat2A,
471
    /* meta */ {
472
      /* active state */ APAL_GPIO_ACTIVE_LOW,
473
      /* edge         */ APAL_GPIO_EDGE_NONE,
474
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
475
    },
476
  },
477
};
478

    
479
BQ27500Driver moduleLldFuelGaugeFront = {
480
  /* I2C driver       */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
481
  /* battery low GPIO */ {
482
    /* GPIO */ &moduleGpioGaugeBatLow1,
483
    /* meta */ {
484
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
485
      /* edge         */ APAL_GPIO_EDGE_NONE,
486
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
487
    },
488
  },
489
  /* battery good GPIO */ {
490
    /* GPIO */ &moduleGpioGaugeBatGd1,
491
    /* meta */ {
492
      /* active state */ APAL_GPIO_ACTIVE_LOW,
493
      /* edge         */ APAL_GPIO_EDGE_NONE,
494
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
495
    },
496
  },
497
};
498

    
499
BQ27500Driver moduleLldFuelGaugeRear = {
500
  /* I2C driver       */ &MODULE_HAL_I2C_PROX_PM18_PM33_GAUGEREAR,
501
  /* battery low GPIO */ {
502
    /* GPIO */ &moduleGpioGaugeBatLow2,
503
    /* meta */ {
504
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
505
      /* edge         */ APAL_GPIO_EDGE_NONE,
506
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
507
    },
508
  },
509
  /* battery good GPIO */ {
510
    /* GPIO */ &moduleGpioGaugeBatGd2,
511
    /* meta */ {
512
      /* active state */ APAL_GPIO_ACTIVE_LOW,
513
      /* edge         */ APAL_GPIO_EDGE_NONE,
514
      /* direction    */ APAL_GPIO_DIRECTION_INPUT,
515
    },
516
  },
517
};
518

    
519
INA219Driver moduleLldPowerMonitorVdd = {
520
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
521
  /* I²C address      */ INA219_LLD_I2C_ADDR_A0 | INA219_LLD_I2C_ADDR_A1,
522
  /* current LSB (uA) */ 0x00u,
523
  /* configuration    */ NULL,
524
};
525

    
526
INA219Driver moduleLldPowerMonitorVio18 = {
527
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_PM18_PM33_GAUGEREAR,
528
  /* I²C address      */ INA219_LLD_I2C_ADDR_A1,
529
  /* current LSB (uA) */ 0x00u,
530
  /* configuration    */ NULL,
531
};
532

    
533
INA219Driver moduleLldPowerMonitorVio33 = {
534
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_PM18_PM33_GAUGEREAR,
535
  /* I²C address      */ INA219_LLD_I2C_ADDR_FIXED,
536
  /* current LSB (uA) */ 0x00u,
537
  /* configuration    */ NULL,
538
};
539

    
540
INA219Driver moduleLldPowerMonitorVsys42 = {
541
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
542
  /* I²C address      */ INA219_LLD_I2C_ADDR_FIXED,
543
  /* current LSB (uA) */ 0x00u,
544
  /* configuration    */ NULL,
545
};
546

    
547
INA219Driver moduleLldPowerMonitorVio50 = {
548
  /* I2C Driver       */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
549
  /* I²C address      */ INA219_LLD_I2C_ADDR_A1,
550
  /* current LSB (uA) */ 0x00u,
551
  /* configuration    */ NULL,
552
};
553

    
554
LEDDriver moduleLldStatusLed = {
555
  /* LED GPIO */ {
556
    /* GPIO */ &moduleGpioLed,
557
    /* meta */ {
558
      /* active state */ APAL_GPIO_ACTIVE_LOW,
559
      /* edge         */ APAL_GPIO_EDGE_NONE,
560
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
561
    },
562
  },
563
};
564

    
565
MPR121Driver moduleLldTouch = {
566
  /* I²C Driver */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
567
};
568

    
569
PCA9544ADriver moduleLldI2cMultiplexer1 = {
570
  /* I²C driver   */ &MODULE_HAL_I2C_PROX_PM18_PM33_GAUGEREAR,
571
  /* I²C address  */ PCA9544A_LLD_I2C_ADDR_A0 | PCA9544A_LLD_I2C_ADDR_A1 | PCA9544A_LLD_I2C_ADDR_A2,
572
};
573

    
574
PCA9544ADriver moduleLldI2cMultiplexer2 = {
575
  /* I²C driver   */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
576
  /* I²C address  */ PCA9544A_LLD_I2C_ADDR_A0 | PCA9544A_LLD_I2C_ADDR_A1 | PCA9544A_LLD_I2C_ADDR_A2,
577
};
578

    
579
TPS62113Driver moduleLldStepDownConverter = {
580
  /* Power enable GPIO */ {
581
    /* GPIO       */ &moduleGpioPowerEn,
582
    /* GPIO meta  */ {
583
      /* active state */ APAL_GPIO_ACTIVE_HIGH,
584
      /* egde         */ APAL_GPIO_EDGE_NONE,
585
      /* direction    */ APAL_GPIO_DIRECTION_OUTPUT,
586
    },
587
  },
588
};
589

    
590
VCNL4020Driver moduleLldProximity1 = {
591
  /* I²C Driver */ &MODULE_HAL_I2C_PROX_PM18_PM33_GAUGEREAR,
592
};
593

    
594
VCNL4020Driver moduleLldProximity2 = {
595
  /* I²C Driver */ &MODULE_HAL_I2C_PROX_PM42_PM50_PMVDD_EEPROM_TOUCH_GAUGEFRONT,
596
};
597

    
598
/** @} */
599

    
600
/*===========================================================================*/
601
/**
602
 * @name Unit tests (UT)
603
 * @{
604
 */
605
/*===========================================================================*/
606
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
607
#include <string.h>
608

    
609
/* ADC  */
610
static int _utShellCmdCb_Adc(BaseSequentialStream* stream, int argc, char* argv[])
611
{
612
  (void)argc;
613
  (void)argv;
614
  aosUtRun(stream, &moduleUtAdcVsys, NULL);
615
  return AOS_OK;
616
}
617
static ut_adcdata_t _utAdcVsysData = {
618
  /* driver               */ &MODULE_HAL_ADC_VSYS,
619
  /* ADC conversion group */ &moduleHalAdcVsysConversionGroup,
620
};
621
aos_unittest_t moduleUtAdcVsys = {
622
  /* name           */ "ADC",
623
  /* info           */ "VSYS",
624
  /* test function  */ utAdcFunc,
625
  /* shell command  */ {
626
    /* name     */ "unittest:ADC",
627
    /* callback */ _utShellCmdCb_Adc,
628
    /* next     */ NULL,
629
  },
630
  /* data           */ &_utAdcVsysData,
631
};
632

    
633
/* AT24C01BN-SH-B (EEPROM) */
634
static int _utShellCmdCb_AlldAt24c01bn(BaseSequentialStream* stream, int argc, char* argv[])
635
{
636
  (void)argc;
637
  (void)argv;
638
  aosUtRun(stream, &moduleUtAlldAt24c01bn, NULL);
639
  return AOS_OK;
640
}
641
static ut_at24c01bndata_t _utAlldAt24c01bnData = {
642
  /* driver   */ &moduleLldEeprom,
643
  /* timeout  */ MICROSECONDS_PER_SECOND,
644
};
645
aos_unittest_t moduleUtAlldAt24c01bn = {
646
  /* name           */ "AT24C01BN-SH-B",
647
  /* info           */ "1kbit EEPROM",
648
  /* test function  */ utAlldAt24c01bnFunc,
649
  /* shell command  */ {
650
    /* name     */ "unittest:EEPROM",
651
    /* callback */ _utShellCmdCb_AlldAt24c01bn,
652
    /* next     */ NULL,
653
  },
654
  /* data           */ &_utAlldAt24c01bnData,
655
};
656

    
657
/* BQ24103A (battery charger) */
658
static int _utShellCmdCb_AlldBq24103a(BaseSequentialStream* stream, int argc, char* argv[])
659
{
660
  // local variables
661
  bool print_help = false;
662

    
663
  // evaluate argument
664
  if (argc == 2) {
665
    if (strcmp(argv[1], "-f") == 0 || strcmp(argv[1], "--front") == 0) {
666
      moduleUtAlldBq24103a.data = &moduleLldBatteryChargerFront;
667
      aosUtRun(stream, &moduleUtAlldBq24103a, "front battery");
668
      moduleUtAlldBq24103a.data = NULL;
669
    }
670
    else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--rear") == 0) {
671
      moduleUtAlldBq24103a.data = &moduleLldBatteryChargerRear;
672
      aosUtRun(stream, &moduleUtAlldBq24103a, "rear battery");
673
      moduleUtAlldBq24103a.data = NULL;
674
    }
675
    else {
676
      print_help = true;
677
    }
678
  } else {
679
    print_help = true;
680
  }
681

    
682
  // print help or just return
683
  if (print_help) {
684
    chprintf(stream, "Usage: %s OPTION\n", argv[0]);
685
    chprintf(stream, "Options:\n");
686
    chprintf(stream, "  --front, -f\n");
687
    chprintf(stream, "    Test the front battery charger.\n");
688
    chprintf(stream, "  --rear, -r\n");
689
    chprintf(stream, "    Test the rear battery charger.\n");
690
    return AOS_INVALID_ARGUMENTS;
691
  } else {
692
    return AOS_OK;
693
  }
694
}
695
aos_unittest_t moduleUtAlldBq24103a = {
696
  /* name           */ "BQ24103A",
697
  /* info           */ "battery charger",
698
  /* test function  */ utAlldBq24103aFunc,
699
  /* shell command  */ {
700
    /* name     */ "unittest:BatteryCharger",
701
    /* callback */ _utShellCmdCb_AlldBq24103a,
702
    /* next     */ NULL,
703
  },
704
  /* data           */ NULL,
705
};
706

    
707
/* BQ27500 (fuel gauge) */
708
static int _utShellCmdCb_AlldBq27500(BaseSequentialStream* stream, int argc, char* argv[])
709
{
710
  // evaluate arguments
711
  if (argc == 2) {
712
    if (strcmp(argv[1], "-f") == 0 || strcmp(argv[1], "--front") == 0) {
713
      ((ut_bq27500data_t*)moduleUtAlldBq27500.data)->driver = &moduleLldFuelGaugeFront;
714
      aosUtRun(stream, &moduleUtAlldBq27500, "front battery");
715
      ((ut_bq27500data_t*)moduleUtAlldBq27500.data)->driver = NULL;
716
      return AOS_OK;
717
    }
718
    else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--rear") == 0) {
719
      ((ut_bq27500data_t*)moduleUtAlldBq27500.data)->driver = &moduleLldFuelGaugeRear;
720
      aosUtRun(stream, &moduleUtAlldBq27500, "rear battery");
721
      ((ut_bq27500data_t*)moduleUtAlldBq27500.data)->driver = NULL;
722
      return AOS_OK;
723
    }
724
  }
725
  // print help
726
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
727
  chprintf(stream, "Options:\n");
728
  chprintf(stream, "  --front, -f\n");
729
  chprintf(stream, "    Test the front battery fuel gauge.\n");
730
  chprintf(stream, "  --rear, -r\n");
731
  chprintf(stream, "    Test the rear battery fuel gauge.\n");
732
  return AOS_INVALID_ARGUMENTS;
733
}
734
static ut_bq27500data_t _utAlldBq27500Data = {
735
  /* driver   */ NULL,
736
  /* timeout  */ MICROSECONDS_PER_SECOND,
737
};
738
aos_unittest_t moduleUtAlldBq27500 = {
739
  /* name           */ "BQ27500",
740
  /* info           */ "fuel gauge",
741
  /* test function  */ utAlldBq27500Func,
742
  /* shell command  */ {
743
    /* name     */ "unittest:FuelGauge",
744
    /* callback */ _utShellCmdCb_AlldBq27500,
745
    /* next     */ NULL,
746
  },
747
  /* data           */ &_utAlldBq27500Data,
748
};
749

    
750
/* BQ27500 (fuel gauge) in combination with BQ24103A (battery charger) */
751
static int _utShellCmdCb_AlldBq27500Bq24103a(BaseSequentialStream* stream, int argc, char* argv[])
752
{
753
  // evaluate arguments
754
  if (argc == 2) {
755
    if (strcmp(argv[1], "-f") == 0 || strcmp(argv[1], "--front") == 0) {
756
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq27500 = &moduleLldFuelGaugeFront;
757
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq24103a = &moduleLldBatteryChargerFront;
758
      aosUtRun(stream, &moduleUtAlldBq27500Bq24103a, "front battery");
759
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq27500 = NULL;
760
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq24103a = NULL;
761
      return AOS_OK;
762
    }
763
    else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--rear") == 0) {
764
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq27500 = &moduleLldFuelGaugeRear;
765
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq24103a = &moduleLldBatteryChargerRear;
766
      aosUtRun(stream, &moduleUtAlldBq27500Bq24103a, "rear battery");
767
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq27500 = NULL;
768
      ((ut_bq27500bq24103adata_t*)moduleUtAlldBq27500Bq24103a.data)->bq24103a = NULL;
769
      return AOS_OK;
770
    }
771
  }
772
  // print help
773
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
774
  chprintf(stream, "Options:\n");
775
  chprintf(stream, "  --front, -f\n");
776
  chprintf(stream, "    Test the front battery fuel gauge and charger.\n");
777
  chprintf(stream, "  --rear, -r\n");
778
  chprintf(stream, "    Test the rear battery fuel gauge and charger.\n");
779
  return AOS_INVALID_ARGUMENTS;
780
}
781
static ut_bq27500bq24103adata_t _utAlldBq27500Bq24103aData= {
782
  /* BQ27500 driver   */ NULL,
783
  /* BQ23203A driver  */ NULL,
784
  /* timeout          */ MICROSECONDS_PER_SECOND,
785
};
786
aos_unittest_t moduleUtAlldBq27500Bq24103a = {
787
  /* name           */ "BQ27500 & BQ24103A",
788
  /* info           */ "fuel gauge & battery charger",
789
  /* test function  */ utAlldBq27500Bq24103aFunc,
790
  /* shell command  */ {
791
    /* name     */ "unittest:FuelGauge&BatteryCharger",
792
    /* callback */ _utShellCmdCb_AlldBq27500Bq24103a,
793
    /* next     */ NULL,
794
  },
795
  /* data           */ &_utAlldBq27500Bq24103aData,
796
};
797

    
798
/* INA219 (power monitor) */
799
static int _utShellCmdCb_AlldIna219(BaseSequentialStream* stream, int argc, char* argv[])
800
{
801
  // evaluate arguments
802
  if (argc == 2) {
803
    if (strcmp(argv[1], "VDD") == 0) {
804
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = &moduleLldPowerMonitorVdd;
805
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 3.3f;
806
      aosUtRun(stream, &moduleUtAlldIna219, "VDD (3.3V)");
807
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = NULL;
808
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 0.0f;
809
      return AOS_OK;
810
    }
811
    else if (strcmp(argv[1], "VIO1.8") == 0) {
812
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = &moduleLldPowerMonitorVio18;
813
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 1.8f;
814
      aosUtRun(stream, &moduleUtAlldIna219, "VIO (1.8V)");
815
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = NULL;
816
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 0.0f;
817
      return AOS_OK;
818
    }
819
    else if (strcmp(argv[1], "VIO3.3") == 0) {
820
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = &moduleLldPowerMonitorVio33;
821
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 3.3f;
822
      aosUtRun(stream, &moduleUtAlldIna219, "VIO (3.3V)");
823
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = NULL;
824
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 0.0f;
825
      return AOS_OK;
826
    }
827
    else if (strcmp(argv[1], "VSYS4.2") == 0) {
828
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = &moduleLldPowerMonitorVsys42;
829
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 4.2f;
830
      aosUtRun(stream, &moduleUtAlldIna219, "VSYS (4.2V)");
831
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = NULL;
832
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 0.0f;
833
      return AOS_OK;
834
    }
835
    else if (strcmp(argv[1], "VIO5.0") == 0) {
836
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = &moduleLldPowerMonitorVio50;
837
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 5.0f;
838
      aosUtRun(stream, &moduleUtAlldIna219, "VIO (5.0V)");
839
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->inad = NULL;
840
      ((ut_ina219data_t*)moduleUtAlldIna219.data)->v_expected = 0.0f;
841
      return AOS_OK;
842
    }
843
  }
844
  // print help
845
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
846
  chprintf(stream, "Options:\n");
847
  chprintf(stream, "  VDD\n");
848
  chprintf(stream, "    Test VDD (3.3V) power monitor.\n");
849
  chprintf(stream, "  VIO1.8\n");
850
  chprintf(stream, "    Test VIO 1.8V power monitor.\n");
851
  chprintf(stream, "  VIO3.3\n");
852
  chprintf(stream, "    Test VIO 3.3V power monitor.\n");
853
  chprintf(stream, "  VSYS4.2\n");
854
  chprintf(stream, "    Test VSYS 4.2V power monitor.\n");
855
  chprintf(stream, "  VIO5.0\n");
856
  chprintf(stream, "    Test VIO 5.0V power monitor.\n");
857
  return AOS_INVALID_ARGUMENTS;
858
}
859
static ut_ina219data_t _utAlldIna219Data = {
860
  /* driver           */ NULL,
861
  /* expected voltage */ 0.0f,
862
  /* tolerance        */ 0.05f,
863
  /* timeout          */ MICROSECONDS_PER_SECOND,
864
};
865
aos_unittest_t moduleUtAlldIna219 = {
866
  /* name           */ "INA219",
867
  /* info           */ "power monitor",
868
  /* test function  */ utAlldIna219Func,
869
  /* shell command  */ {
870
    /* name     */ "unittest:PowerMonitor",
871
    /* callback */ _utShellCmdCb_AlldIna219,
872
    /* next     */ NULL,
873
  },
874
  /* data           */ &_utAlldIna219Data,
875
};
876

    
877
/* Status LED */
878
static int _utShellCmdCb_AlldLed(BaseSequentialStream* stream, int argc, char* argv[])
879
{
880
  (void)argc;
881
  (void)argv;
882
  aosUtRun(stream, &moduleUtAlldLed, NULL);
883
  return AOS_OK;
884
}
885
aos_unittest_t moduleUtAlldLed = {
886
  /* name           */ "LED",
887
  /* info           */ NULL,
888
  /* test function  */ utAlldLedFunc,
889
  /* shell command  */ {
890
    /* name     */ "unittest:StatusLED",
891
    /* callback */ _utShellCmdCb_AlldLed,
892
    /* next     */ NULL,
893
  },
894
  /* data           */ &moduleLldStatusLed,
895
};
896

    
897
/* MPR121 (touch sensor) */
898
static int _utShellCmdCb_AlldMpr121(BaseSequentialStream* stream, int argc, char* argv[])
899
{
900
  (void)argc;
901
  (void)argv;
902
  aosIntEnable(&moduleIntDriver, MODULE_GPIO_INT_TOUCHINT);
903
  aosUtRun(stream, &moduleUtAlldMpr121, NULL);
904
  aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_TOUCHINT);
905
  return AOS_OK;
906
}
907
static ut_mpr121data_t _utAlldMpr121Data= {
908
  /* MPR121 driver  */ &moduleLldTouch,
909
  /* timeout        */ MICROSECONDS_PER_SECOND,
910
  /* event source   */ &aos.events.io,
911
  /* event flags    */ (1 << MODULE_GPIO_INT_TOUCHINT),
912
};
913
aos_unittest_t moduleUtAlldMpr121 = {
914
  /* name           */ "MPR121",
915
  /* info           */ "touch sensor",
916
  /* test function  */ utAlldMpr121Func,
917
  /* shell command  */ {
918
    /* name     */ "unittest:Touch",
919
    /* callback */ _utShellCmdCb_AlldMpr121,
920
    /* next     */ NULL,
921
  },
922
  /* data           */ &_utAlldMpr121Data,
923
};
924

    
925
/* PCA9544A (I2C multiplexer) */
926
static int _utShellCmdCb_AlldPca5944a(BaseSequentialStream* stream, int argc, char* argv[])
927
{
928
  // evaluate arguments
929
  if (argc == 2) {
930
    if (strcmp(argv[1], "#1") == 0) {
931
      ((ut_pca9544adata_t*)moduleUtAlldPca9544a.data)->driver = &moduleLldI2cMultiplexer1;
932
      aosUtRun(stream, &moduleUtAlldPca9544a, "I2C bus #1");
933
      ((ut_pca9544adata_t*)moduleUtAlldPca9544a.data)->driver = NULL;
934
      return AOS_OK;
935
    }
936
    else if (strcmp(argv[1], "#2") == 0) {
937
      ((ut_pca9544adata_t*)moduleUtAlldPca9544a.data)->driver = &moduleLldI2cMultiplexer2;
938
      aosUtRun(stream, &moduleUtAlldPca9544a, "I2C bus #2");
939
      ((ut_pca9544adata_t*)moduleUtAlldPca9544a.data)->driver = NULL;
940
      return AOS_OK;
941
    }
942
  }
943
  // print help
944
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
945
  chprintf(stream, "Options:\n");
946
  chprintf(stream, "  #1\n");
947
  chprintf(stream, "    Test the multiplexer on the I2C bus #1.\n");
948
  chprintf(stream, "  #2\n");
949
  chprintf(stream, "    Test the multiplexer on the I2C bus #2.\n");
950
  return AOS_INVALID_ARGUMENTS;
951
}
952
static ut_pca9544adata_t _utAlldPca9544aData = {
953
  /* driver   */ NULL,
954
  /* timeout  */ MICROSECONDS_PER_SECOND,
955
};
956
aos_unittest_t moduleUtAlldPca9544a = {
957
  /* name           */ "PCA9544A",
958
  /* info           */ "I2C multiplexer",
959
  /* test function  */ utAlldPca9544aFunc,
960
  /* shell command  */ {
961
    /* name     */ "unittest:I2CMultiplexer",
962
    /* callback */ _utShellCmdCb_AlldPca5944a,
963
    /* next     */ NULL,
964
  },
965
  /* data           */ &_utAlldPca9544aData,
966
};
967

    
968
/* PKLCS1212E4001 (buzzer) */
969
static int _utShellCmdCb_AlldPklcs1212e4001(BaseSequentialStream* stream, int argc, char* argv[])
970
{
971
  (void)argc;
972
  (void)argv;
973
  aosUtRun(stream, &moduleUtAlldPklcs1212e4001, NULL);
974
  return AOS_OK;
975
}
976
static ut_pklcs1212e4001_t _utAlldPklcs1212e4001Data = {
977
  /* PWM driver   */ &MODULE_HAL_PWM_BUZZER,
978
  /* PWM channel  */ MODULE_HAL_PWM_BUZZER_CHANNEL
979
};
980
aos_unittest_t moduleUtAlldPklcs1212e4001 = {
981
  /* name           */ "PKLCS1212E4001",
982
  /* info           */ "buzzer",
983
  /* test function  */ utAlldPklcs1212e4001Func,
984
  /* shell command  */ {
985
    /* name     */ "unittest:Buzzer",
986
    /* callback */ _utShellCmdCb_AlldPklcs1212e4001,
987
    /* next     */ NULL,
988
  },
989
  /* data           */ &_utAlldPklcs1212e4001Data,
990
};
991

    
992
/* TPS62113 (stop-down converter) */
993
static int _utShellCmdCb_AlldTps62113(BaseSequentialStream* stream, int argc, char* argv[])
994
{
995
  // Although there are four TPS62113 on the PCB, they all share the same input signal.
996
  // A sa result, no additional shell arguments need to be evaluated.
997
  (void)argc;
998
  (void)argv;
999
  aosUtRun(stream, &moduleUtAlldTps62113, NULL);
1000
  return AOS_OK;
1001
}
1002
aos_unittest_t moduleUtAlldTps62113 = {
1003
  /* name           */ "TPS62113",
1004
  /* info           */ "step-down converter",
1005
  /* test function  */ utAlldTps62113Func,
1006
  /* shell command  */ {
1007
    /* name     */ "unittest:StepDownConverter",
1008
    /* callback */ _utShellCmdCb_AlldTps62113,
1009
    /* next     */ NULL,
1010
  },
1011
  /* data           */ &moduleLldStepDownConverter,
1012
};
1013

    
1014
/* TPS62113 (step-donw converter) in combination with INA219 (power monitor) */
1015
static int _utShellCmdCb_AlldTps62113Ina219(BaseSequentialStream* stream, int argc, char* argv[])
1016
{
1017
  (void)argc;
1018
  (void)argv;
1019
  aosUtRun(stream, &moduleUtAlldTps62113Ina219, "VSYS (4.2V)");
1020
  return AOS_OK;
1021
}
1022
ut_tps62113ina219data_t _utAlldTps62113Ina219Data = {
1023
  /* TPS62113 */ &moduleLldStepDownConverter,
1024
  /* INA219   */ &moduleLldPowerMonitorVsys42,
1025
  /* timeout  */ MICROSECONDS_PER_SECOND,
1026
};
1027
aos_unittest_t moduleUtAlldTps62113Ina219 = {
1028
  /* name           */ "TPS62113 & INA219",
1029
  /* info           */ "step-down converter & power monitor",
1030
  /* test function  */ utAlldTps62113Ina219Func,
1031
  /* shell command  */ {
1032
    /* name     */ "unittest:StepDownConverter&PowerMonitor",
1033
    /* callback */ _utShellCmdCb_AlldTps62113Ina219,
1034
    /* next     */ NULL,
1035
  },
1036
  /* data           */ &_utAlldTps62113Ina219Data,
1037
};
1038

    
1039
/* VCNL4020 (proximity sensor) */
1040
static void _utAlldVcnl4020_disableInterrupt(VCNL4020Driver* vcnl)
1041
{
1042
  uint8_t intstatus;
1043
  vcnl4020_lld_writereg(vcnl, VCNL4020_LLD_REGADDR_INTCTRL, 0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1044
  vcnl4020_lld_readreg(vcnl, VCNL4020_LLD_REGADDR_INTSTATUS, &intstatus, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1045
  if (intstatus) {
1046
    vcnl4020_lld_writereg(vcnl, VCNL4020_LLD_REGADDR_INTSTATUS, intstatus, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1047
  }
1048
  return;
1049
}
1050
static int _utShellCmdCb_AlldVcnl4020(BaseSequentialStream* stream, int argc, char* argv[])
1051
{
1052
  enum {
1053
    UNKNOWN,
1054
    NNE, ENE, ESE, SSE, SSW,WSW,WNW, NNW,
1055
  } sensor = UNKNOWN;
1056
  // evaluate arguments
1057
  if (argc == 2) {
1058
    if (strcmp(argv[1], "-nne") == 0) {
1059
      sensor = NNE;
1060
    } else if (strcmp(argv[1], "-ene") == 0) {
1061
      sensor = ENE;
1062
    } else if (strcmp(argv[1], "-ese") == 0) {
1063
      sensor = ESE;
1064
    } else if (strcmp(argv[1], "-sse") == 0) {
1065
      sensor = SSE;
1066
    } else if (strcmp(argv[1], "-ssw") == 0) {
1067
      sensor = SSW;
1068
    } else if (strcmp(argv[1], "-wsw") == 0) {
1069
      sensor = WSW;
1070
    } else if (strcmp(argv[1], "-wnw") == 0) {
1071
      sensor = WNW;
1072
    } else if (strcmp(argv[1], "-nnw") == 0) {
1073
      sensor = NNW;
1074
    }
1075
  }
1076
  if (sensor != UNKNOWN) {
1077
    PCA9544ADriver* mux = NULL;
1078
    switch (sensor) {
1079
      case SSE:
1080
      case SSW:
1081
      case WSW:
1082
      case WNW:
1083
        mux = &moduleLldI2cMultiplexer1;
1084
        ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld = &moduleLldProximity1;
1085
        ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->evtflags = (1 << MODULE_GPIO_INT_IRINT2);
1086
        aosIntEnable(&moduleIntDriver, MODULE_GPIO_INT_IRINT2);
1087
        break;
1088
      case NNW:
1089
      case NNE:
1090
      case ENE:
1091
      case ESE:
1092
        mux = &moduleLldI2cMultiplexer2;
1093
        ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld = &moduleLldProximity2;
1094
        ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->evtflags = (1 << MODULE_GPIO_INT_IRINT1);
1095
        aosIntEnable(&moduleIntDriver, MODULE_GPIO_INT_IRINT1);
1096
        break;
1097
      default:
1098
        break;
1099
    }
1100
    pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1101
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
1102
    pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH1, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1103
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
1104
    pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH2, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1105
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
1106
    pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH3, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1107
    _utAlldVcnl4020_disableInterrupt(((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld);
1108
    switch (sensor) {
1109
      case NNE:
1110
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH1, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1111
        aosUtRun(stream, &moduleUtAlldVcnl4020, "north-northeast sensor");
1112
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT1);
1113
        break;
1114
      case ENE:
1115
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH3, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1116
        aosUtRun(stream, &moduleUtAlldVcnl4020, "east-northeast sensor");
1117
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT1);
1118
        break;
1119
      case ESE:
1120
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH2, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1121
        aosUtRun(stream, &moduleUtAlldVcnl4020, "north-southeast sensor");
1122
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT1);
1123
        break;
1124
      case SSE:
1125
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1126
        aosUtRun(stream, &moduleUtAlldVcnl4020, "south-southeast sensor");
1127
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT2);
1128
        break;
1129
      case SSW:
1130
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH1, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1131
        aosUtRun(stream, &moduleUtAlldVcnl4020, "south-southwest sensor");
1132
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT2);
1133
        break;
1134
      case WSW:
1135
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH3, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1136
        aosUtRun(stream, &moduleUtAlldVcnl4020, "west-southwest sensor");
1137
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT2);
1138
        break;
1139
      case WNW:
1140
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH2, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1141
        aosUtRun(stream, &moduleUtAlldVcnl4020, "west-northwest sensor");
1142
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT2);
1143
        break;
1144
      case NNW:
1145
        pca9544a_lld_setchannel(mux, PCA9544A_LLD_CH0, ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->timeout);
1146
        aosUtRun(stream, &moduleUtAlldVcnl4020, "north-northwest sensor");
1147
        aosIntDisable(&moduleIntDriver, MODULE_GPIO_INT_IRINT1);
1148
        break;
1149
      default:
1150
        break;
1151
    }
1152
    ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->vcnld = NULL;
1153
    ((ut_vcnl4020data_t*)moduleUtAlldVcnl4020.data)->evtflags = 0;
1154
    return AOS_OK;
1155
  }
1156
  // print help
1157
  chprintf(stream, "Usage: %s OPTION\n", argv[0]);
1158
  chprintf(stream, "Options:\n");
1159
  chprintf(stream, "  -nne\n");
1160
  chprintf(stream, "    Test north-northeast sensor.\n");
1161
  chprintf(stream, "  -ene\n");
1162
  chprintf(stream, "    Test east-northeast sensor.\n");
1163
  chprintf(stream, "  -ese\n");
1164
  chprintf(stream, "    Test east-southeast sensor.\n");
1165
  chprintf(stream, "  -sse\n");
1166
  chprintf(stream, "    Test south-southeast sensor.\n");
1167
  chprintf(stream, "  -ssw\n");
1168
  chprintf(stream, "    Test south-southwest sensor.\n");
1169
  chprintf(stream, "  -wsw\n");
1170
  chprintf(stream, "    Test west-southwest sensor.\n");
1171
  chprintf(stream, "  -wnw\n");
1172
  chprintf(stream, "    Test west-northwest sensor.\n");
1173
  chprintf(stream, "  -nnw\n");
1174
  chprintf(stream, "    Test north-northwest sensor.\n");
1175
  return AOS_INVALID_ARGUMENTS;
1176
}
1177
static ut_vcnl4020data_t _utAlldVcnl4020Data = {
1178
  /* driver       */ NULL,
1179
  /* timeout      */ MICROSECONDS_PER_SECOND,
1180
  /* event source */ &aos.events.io,
1181
  /* event flags  */ 0,
1182
};
1183
aos_unittest_t moduleUtAlldVcnl4020 = {
1184
  /* name           */ "VCNL4020",
1185
  /* info           */ "proximity sensor",
1186
  /* test function  */ utAlldVcnl4020Func,
1187
  /* shell command  */ {
1188
    /* name     */ "unittest:Proximity",
1189
    /* callback */ _utShellCmdCb_AlldVcnl4020,
1190
    /* next     */ NULL,
1191
  },
1192
  /* data           */ &_utAlldVcnl4020Data,
1193
};
1194

    
1195
#endif /* AMIROOS_CFG_TESTS_ENABLE == true */
1196

    
1197
/** @} */