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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
/**************************************************************************//**
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-12-12 Wayne First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_ADC)
#include <rtdevice.h>
#include "NuMicro.h"
#include "drv_sys.h"
#include "nu_bitutil.h"
#include "drv_adc.h"
/* Private define ---------------------------------------------------------------*/
#define DEF_ADC_TOUCH_SMPL_TICK 20
#define TOUCH_MQ_LENGTH 64
/* Private Typedef --------------------------------------------------------------*/
struct nu_adc
{
struct rt_adc_device dev;
char *name;
ADC_T *base;
uint32_t bReset;
IRQn_Type irqn;
uint32_t rstidx;
uint32_t modid;
uint32_t chn_mask;
rt_sem_t m_psSem;
#if defined(BSP_USING_ADC_TOUCH)
rt_touch_t psRtTouch;
rt_timer_t psRtTouchMenuTimer;
rt_mq_t m_pmqTouchXYZ;
#endif
nu_adc_cb m_isr[eAdc_ISR_CNT];
nu_adc_cb m_wkisr[eAdc_WKISR_CNT];
};
typedef struct nu_adc *nu_adc_t;
#if defined(BSP_USING_ADC_TOUCH)
struct nu_adc_touch_data
{
uint32_t u32X;
uint32_t u32Y;
uint32_t u32Z0;
uint32_t u32Z1;
};
typedef struct nu_adc_touch_data *nu_adc_touch_data_t;
#endif
/* Private functions ------------------------------------------------------------*/
static rt_err_t nu_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled);
static rt_err_t nu_adc_convert(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value);
static rt_err_t _nu_adc_control(rt_device_t dev, int cmd, void *args);
/* Public functions ------------------------------------------------------------*/
int rt_hw_adc_init(void);
/* Private variables ------------------------------------------------------------*/
static struct nu_adc g_sNuADC =
{
.name = "adc",
.base = ADC0,
.bReset = 1,
.irqn = ADC0_IRQn,
.rstidx = ADC0_RST,
.modid = ADC_MODULE,
.chn_mask = 0
};
static void nu_adc_isr(int vector, void *param)
{
volatile rt_int32_t isr, wkisr;
nu_adc_t psNuAdc = (nu_adc_t)param;
rt_int32_t irqidx;
ADC_T *adc = psNuAdc->base;
/* rt_kprintf("[%s %d] CTL: %08x CONF:%08x IER:%08x ISR:%08x\n", __func__, __LINE__, adc->CTL, adc->CONF, adc->IER, adc->ISR); */
isr = adc->ISR;
wkisr = adc->WKISR;
adc->ISR = isr;
while ((irqidx = nu_ctz(isr)) < eAdc_ISR_CNT)
{
uint32_t u32IsrBitMask = 1 << irqidx ;
if (psNuAdc->m_isr[irqidx].cbfunc != RT_NULL)
{
/* rt_kprintf("[%s] %d %x\n", __func__, irqidx, psNuAdc->m_isr[irqidx].cbfunc); */
psNuAdc->m_isr[irqidx].cbfunc(isr, psNuAdc->m_isr[irqidx].private_data);
}
/* Clear sent bit */
isr &= ~(u32IsrBitMask);
} /* while */
while ((irqidx = nu_ctz(wkisr)) < eAdc_WKISR_CNT)
{
uint32_t u32IsrBitMask = 1 << irqidx ;
if (psNuAdc->m_wkisr[irqidx].cbfunc != RT_NULL)
{
psNuAdc->m_wkisr[irqidx].cbfunc(wkisr, psNuAdc->m_wkisr[irqidx].private_data);
}
wkisr &= ~(u32IsrBitMask);
} /* while */
}
static rt_err_t _nu_adc_init(rt_device_t dev)
{
nu_adc_t psNuAdc = (nu_adc_t)dev;
/* ADC Engine Clock is set to freq Khz */
CLK_SetModuleClock(psNuAdc->modid, 0, CLK_CLKDIV4_ADC(180)); /* Set ADC clock rate to 9MHz */
/* Install interrupt service routine */
rt_hw_interrupt_install(psNuAdc->irqn, nu_adc_isr, (void *)psNuAdc, psNuAdc->name);
return RT_EOK;
}
#define ADC_TOUCH_Z0_ACTIVE 20
static int32_t AdcMenuStartCallback(uint32_t status, uint32_t userData)
{
nu_adc_t psNuAdc = (nu_adc_t)userData;
#if defined(BSP_USING_ADC_TOUCH)
ADC_T *adc = psNuAdc->base;
static struct nu_adc_touch_data point;
static rt_bool_t bDrop = RT_FALSE;
static uint32_t u32LastZ0 = 0xffffu;
if (psNuAdc->psRtTouch != RT_NULL)
{
point.u32X = ADC_GET_CONVERSION_XDATA(adc);
point.u32Y = ADC_GET_CONVERSION_YDATA(adc);
point.u32Z0 = ADC_GET_CONVERSION_Z1DATA(adc);
point.u32Z1 = ADC_GET_CONVERSION_Z2DATA(adc);
/* rt_kprintf("x=%d y=%d z0=%d z1=%d\n", point.u32X, point.u32Y, point.u32Z0, point.u32Z1); */
/* Trigger next or not. */
if (point.u32Z0 < ADC_TOUCH_Z0_ACTIVE)
{
/* Stop sampling procedure. */
rt_timer_stop(g_sNuADC.psRtTouchMenuTimer);
/* Re-start pendown detection */
nu_adc_touch_detect(RT_TRUE);
psNuAdc->bReset = 1;
point.u32Z0 = point.u32Z1 = 0;
bDrop = RT_TRUE;
}
else
{
bDrop = RT_FALSE;
}
/* Notify upper layer. */
if ((!bDrop || (u32LastZ0 > ADC_TOUCH_Z0_ACTIVE)) && rt_mq_send(psNuAdc->m_pmqTouchXYZ, (const void *)&point, sizeof(struct nu_adc_touch_data)) == RT_EOK)
{
rt_hw_touch_isr(psNuAdc->psRtTouch);
}
u32LastZ0 = point.u32Z0;
}
else
#endif
{
rt_err_t result = rt_sem_release(psNuAdc->m_psSem);
RT_ASSERT(result == RT_EOK);
}
return 0;
}
#if defined(BSP_USING_ADC_TOUCH)
static void nu_adc_touch_antiglitch(ADC_T *adc)
{
int count = 10;
do
{
rt_hw_us_delay(1000); /* 1ms */
ADC_CLR_INT_FLAG(adc, adc->ISR);
if (adc->ISR == 0)
break;
}
while (count-- > 0);
}
void nu_adc_touch_detect(rt_bool_t bStartDetect)
{
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
ADC_T *adc = psNuAdc->base;
/* Disable interrupt */
rt_hw_interrupt_mask(psNuAdc->irqn);
ADC_POWER_DOWN(adc);
/* Disable interrupt */
ADC_DISABLE_INT(adc, ADC_IER_PEDEIEN_Msk | ADC_IER_MIEN_Msk);
nu_adc_touch_antiglitch(adc);
if (bStartDetect == RT_TRUE)
{
/* Switch to PenDown detection mode */
ADC_DETECT_PD_MODE(adc);
nu_adc_touch_antiglitch(adc);
/* Enable interrupt */
ADC_ENABLE_INT(adc, ADC_IER_PEDEIEN_Msk);
}
else
{
/* Switch to XY coordination converting mode */
ADC_CONVERT_XY_MODE(adc);
nu_adc_touch_antiglitch(adc);
/* Enable interrupt */
ADC_ENABLE_INT(adc, ADC_IER_MIEN_Msk);
}
ADC_POWER_ON(adc);
/* Enable interrupt */
rt_hw_interrupt_umask(psNuAdc->irqn);
}
static int32_t PenDownCallback(uint32_t status, uint32_t userData)
{
nu_adc_t psNuAdc = (nu_adc_t)userData;
return rt_timer_start(psNuAdc->psRtTouchMenuTimer);
}
int32_t nu_adc_touch_read_xyz(uint32_t *bufX, uint32_t *bufY, uint32_t *bufZ0, uint32_t *bufZ1, int32_t dataCnt)
{
int i;
struct nu_adc_touch_data value;
for (i = 0 ; i < dataCnt; i++)
{
if (rt_mq_recv(g_sNuADC.m_pmqTouchXYZ, (void *)&value, sizeof(struct nu_adc_touch_data), 0) == -RT_ETIMEOUT)
break;
bufX[i] = value.u32X;
bufY[i] = value.u32Y;
bufZ0[i] = value.u32Z0;
bufZ1[i] = value.u32Z1;
}
return i;
}
rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch)
{
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
nu_adc_cb sNuAdcCb;
ADC_T *adc = psNuAdc->base;
adc->CONF = 0x0;
rt_adc_enable((rt_adc_device_t)psNuAdc, 4); /* Channel number 4 */
rt_adc_enable((rt_adc_device_t)psNuAdc, 5); /* Channel number 5 */
rt_adc_enable((rt_adc_device_t)psNuAdc, 6); /* Channel number 6 */
rt_adc_enable((rt_adc_device_t)psNuAdc, 7); /* Channel number 7 */
/* Register touch device. */
psNuAdc->psRtTouch = psRtTouch;
/* Register PenDown callback. */
sNuAdcCb.cbfunc = PenDownCallback;
sNuAdcCb.private_data = (rt_uint32_t)psNuAdc;
rt_memcpy(&psNuAdc->m_isr[eAdc_PEDEF], &sNuAdcCb, sizeof(nu_adc_cb));
nu_adc_touch_detect(RT_TRUE);
return RT_EOK;
}
rt_err_t nu_adc_touch_disable(void)
{
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
nu_adc_touch_detect(RT_FALSE);
_nu_adc_control((rt_device_t)psNuAdc, T_OFF, RT_NULL);
_nu_adc_control((rt_device_t)psNuAdc, Z_OFF, RT_NULL);
_nu_adc_control((rt_device_t)psNuAdc, PEDEF_OFF, RT_NULL);
rt_adc_disable((rt_adc_device_t)psNuAdc, 4); /* Channel number 4 */
rt_adc_disable((rt_adc_device_t)psNuAdc, 5); /* Channel number 5 */
rt_adc_disable((rt_adc_device_t)psNuAdc, 6); /* Channel number 6 */
rt_adc_disable((rt_adc_device_t)psNuAdc, 7); /* Channel number 7 */
return RT_EOK;
}
static void nu_adc_touch_smpl(void *p)
{
nu_adc_t psNuAdc = (nu_adc_t)p;
if (psNuAdc->bReset)
{
psNuAdc->bReset = 0;
nu_adc_touch_detect(RT_FALSE);
}
/* Start conversion */
ADC_START_CONV(psNuAdc->base);
}
#endif
static rt_err_t _nu_adc_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t ret = -RT_EINVAL ;
nu_adc_t psNuAdc = (nu_adc_t)dev;
ADC_T *adc = psNuAdc->base;
nu_adc_cb_t psAdcCb = (nu_adc_cb_t)args;
switch (cmd)
{
case START_MST: /* Menu Start Conversion */
{
/* Enable interrupt */
ADC_ENABLE_INT(adc, ADC_IER_MIEN_Msk);
/* Start conversion */
ADC_START_CONV(adc);
/* Wait it done */
ret = rt_sem_take(psNuAdc->m_psSem, RT_WAITING_FOREVER);
RT_ASSERT(ret == RT_EOK);
/* Get data: valid data is 12-bit */
if (args != RT_NULL)
*((uint32_t *)args) = ADC_GET_CONVERSION_DATA(adc, 0);
}
break;
case WKT_ON: /* Enable Touch Wake Up */
{
if (psAdcCb)
{
rt_memcpy(&psNuAdc->m_wkisr[eAdc_WPEDEF], psAdcCb, sizeof(nu_adc_cb));
}
adc->CTL |= ADC_CTL_WKTEN_Msk;
adc->IER |= ADC_IER_WKTIEN_Msk;
/* TODO outpw(REG_SYS_WKUPSER, inpw(REG_SYS_WKUPSER) | (1 << 26)); */
}
break;
case WKT_OFF: /* Disable Touch Wake Up */
{
adc->CTL &= ~ADC_CTL_WKTEN_Msk;
adc->IER &= ~ADC_IER_WKTIEN_Msk;
/* TODO outpw(REG_SYS_WKUPSER, inpw(REG_SYS_WKUPSER) & ~(1 << 26)); */
}
break;
case SWITCH_5WIRE_ON: /* Wire Mode Switch to 5-Wire */
{
adc->CTL |= ADC_CTL_WMSWCH_Msk;
}
break;
case SWITCH_5WIRE_OFF: /* Wire Mode Switch to 4-Wire */
{
adc->CTL &= ~ADC_CTL_WMSWCH_Msk;
}
break;
case T_ON: /* Enable Touch detection function */
{
adc->CONF |= ADC_CONF_TEN_Msk;
}
break;
case T_OFF: /* Disable Touch detection function */
{
adc->CONF &= ~ADC_CONF_TEN_Msk;
}
break;
case TAVG_ON: /* Enable Touch Mean average for X and Y function */
{
adc->CONF |= ADC_CONF_TMAVDIS_Msk;
}
break;
case TAVG_OFF: /* Disable Touch Mean average for X and Y function */
{
adc->CONF &= ~ADC_CONF_TMAVDIS_Msk;
}
break;
case Z_ON: /* Enable Press measure function */
{
adc->CONF |= ADC_CONF_ZEN_Msk;
}
break;
case Z_OFF: /* Disable Press measure function */
{
adc->CONF &= ~ADC_CONF_ZEN_Msk;
#if defined(BSP_USING_ADC_TOUCH)
rt_mq_control(psNuAdc->m_pmqTouchXYZ, RT_IPC_CMD_RESET, RT_NULL);
#endif
}
break;
case TZAVG_ON: /* Enable Pressure Mean average for Z1 and Z2 function */
{
adc->CONF |= ADC_CONF_ZMAVDIS_Msk;
}
break;
case TZAVG_OFF: /* Disable Pressure Mean average for Z1 and Z2 function */
{
adc->CONF &= ~ADC_CONF_ZMAVDIS_Msk;
}
break;
case NAC_ON: /* Enable Normal AD Conversion */
{
adc->CONF |= (ADC_CONF_NACEN_Msk | ADC_CONF_REFSEL_AVDD33);
}
break;
case NAC_OFF: /* Disable Normal AD Conversion */
{
adc->CONF &= ~ADC_CONF_NACEN_Msk;
}
break;
case SWITCH_CH:
{
int chn = (int)args;
if (chn >= ADC_CH_NUM)
{
return -ret;
}
adc->CONF &= ~ADC_CONF_CHSEL_Msk;
adc->CONF |= (chn << ADC_CONF_CHSEL_Pos);
}
break;
default:
return -(ret);
}
return RT_EOK;
}
static rt_err_t _nu_adc_open(rt_device_t dev, rt_uint16_t oflag)
{
nu_adc_t psNuAdc = (nu_adc_t)dev;
ADC_T *adc = psNuAdc->base;
/* Enable ADC engine clock */
CLK_EnableModuleClock(psNuAdc->modid);
/* Reset the ADC IP */
SYS_ResetModule(psNuAdc->modid);
/* Enable ADC Power */
ADC_POWER_ON(adc);
/* Enable ADC to high speed mode */
adc->CONF |= ADC_CONF_SPEED_Msk;
/* Enable interrupt */
rt_hw_interrupt_umask(psNuAdc->irqn);
/* Enable Normal AD Conversion */
_nu_adc_control(dev, NAC_ON, RT_NULL);
return RT_EOK;
}
static rt_err_t _nu_adc_close(rt_device_t dev)
{
nu_adc_t psNuAdc = (nu_adc_t)dev;
ADC_T *adc = psNuAdc->base;
/* Disable Normal AD Conversion */
_nu_adc_control(dev, NAC_OFF, RT_NULL);
/* Disable interrupt */
rt_hw_interrupt_mask(psNuAdc->irqn);
/* Disable ADC Power */
ADC_POWER_DOWN(adc);
/* Disable ADC engine clock */
CLK_DisableModuleClock(psNuAdc->modid);
return RT_EOK;
}
static const struct rt_adc_ops nu_adc_ops =
{
nu_adc_enabled,
nu_adc_convert,
};
/* nu_adc_enabled - Enable ADC clock and wait for ready */
static rt_err_t nu_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
{
nu_adc_t psNuADC = (nu_adc_t)device;
RT_ASSERT(device);
if (channel >= ADC_CH_NUM)
return -(RT_EINVAL);
if (enabled)
{
psNuADC->chn_mask |= (1 << channel);
}
else
{
psNuADC->chn_mask &= ~(1 << channel);
}
if (psNuADC->chn_mask > 0 && ((rt_device_t)device)->ref_count == 0)
{
_nu_adc_open((rt_device_t)device, 0);
((rt_device_t)device)->ref_count = 1;
}
else if ((psNuADC->chn_mask == 0) && ((rt_device_t)device)->ref_count == 1)
{
_nu_adc_close((rt_device_t)device);
((rt_device_t)device)->ref_count = 0;
}
return RT_EOK;
}
static rt_err_t nu_adc_convert(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
{
rt_err_t ret = RT_EOK;
RT_ASSERT(device);
RT_ASSERT(value);
if (channel >= ADC_CH_NUM)
{
ret = -RT_EINVAL;
goto exit_nu_adc_convert;
}
else if ((ret = _nu_adc_control((rt_device_t)device, SWITCH_CH, (void *)(intptr_t)channel)) != RT_EOK)
{
goto exit_nu_adc_convert;
}
else if ((ret = _nu_adc_control((rt_device_t)device, START_MST, (void *)value)) != RT_EOK)
{
goto exit_nu_adc_convert;
}
exit_nu_adc_convert:
return (-ret) ;
}
int rt_hw_adc_init(void)
{
rt_err_t result = -RT_ERROR;
rt_device_t psDev = &g_sNuADC.dev.parent;
result = rt_hw_adc_register(&g_sNuADC.dev, g_sNuADC.name, &nu_adc_ops, &g_sNuADC);
RT_ASSERT(result == RT_EOK);
result = _nu_adc_init(psDev);
RT_ASSERT(result == RT_EOK);
g_sNuADC.m_psSem = rt_sem_create("adc_mst_sem", 0, RT_IPC_FLAG_FIFO);
RT_ASSERT(g_sNuADC.m_psSem);
#if defined(BSP_USING_ADC_TOUCH)
g_sNuADC.m_pmqTouchXYZ = rt_mq_create("ADC_TOUCH_XYZ", sizeof(struct nu_adc_touch_data), TOUCH_MQ_LENGTH, RT_IPC_FLAG_FIFO);
RT_ASSERT(g_sNuADC.m_pmqTouchXYZ);
g_sNuADC.psRtTouchMenuTimer = rt_timer_create("TOUCH_SMPL_TIMER", nu_adc_touch_smpl, (void *)&g_sNuADC, DEF_ADC_TOUCH_SMPL_TICK, RT_TIMER_FLAG_PERIODIC);
RT_ASSERT(g_sNuADC.psRtTouchMenuTimer);
#endif
rt_memset(&g_sNuADC.m_isr, 0, sizeof(g_sNuADC.m_isr));
rt_memset(&g_sNuADC.m_wkisr, 0, sizeof(g_sNuADC.m_wkisr));
g_sNuADC.m_isr[eAdc_MF].cbfunc = AdcMenuStartCallback;
g_sNuADC.m_isr[eAdc_MF].private_data = (uint32_t)&g_sNuADC;
return (int)result;
}
INIT_BOARD_EXPORT(rt_hw_adc_init);
#endif /* #if defined(BSP_USING_ADC) */
|