Skip to content

Commit f7ba146

Browse files
committed
Add sdk_dist.py and ADC touching calibration function.
1 parent 0b99eee commit f7ba146

File tree

26 files changed

+2446
-394
lines changed

26 files changed

+2446
-394
lines changed

bsp/nuvoton/libraries/n9h30/rtt_port/drv_adc.c

Lines changed: 104 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,27 @@ struct nu_adc
3535
uint32_t chn_mask;
3636
rt_sem_t m_psSem;
3737

38+
#if defined(BSP_USING_ADC_TOUCH)
3839
rt_touch_t psRtTouch;
3940
rt_timer_t psRtTouchMenuTimer;
41+
rt_mq_t m_pmqTouchXYZ;
42+
#endif
4043

4144
nu_adc_cb m_isr[eAdc_ISR_CNT];
4245
nu_adc_cb m_wkisr[eAdc_WKISR_CNT];
43-
44-
rt_mq_t m_pmqTouchXYZ;
4546
};
4647
typedef struct nu_adc *nu_adc_t;
4748

49+
#if defined(BSP_USING_ADC_TOUCH)
4850
struct nu_adc_touch_data
4951
{
50-
uint16_t u16X;
51-
uint16_t u16Y;
52-
uint16_t u16Z0;
53-
uint16_t u16Z1;
52+
uint32_t u32X;
53+
uint32_t u32Y;
54+
uint32_t u32Z0;
55+
uint32_t u32Z1;
5456
};
5557
typedef struct nu_adc_touch_data *nu_adc_touch_data_t;
58+
#endif
5659

5760
/* Private functions ------------------------------------------------------------*/
5861
static rt_err_t nu_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled);
@@ -134,44 +137,30 @@ static rt_err_t _nu_adc_init(rt_device_t dev)
134137
return RT_EOK;
135138
}
136139

137-
void nu_adc_touch_detect(rt_bool_t bStartDetect)
140+
static int32_t AdcMenuStartCallback(uint32_t status, uint32_t userData)
138141
{
139-
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
142+
nu_adc_t psNuAdc = (nu_adc_t)userData;
140143

141-
if (bStartDetect)
142-
{
143-
/* Start detect PenDown */
144-
_nu_adc_control((rt_device_t)psNuAdc, PEPOWER_ON, RT_NULL);
145-
}
146-
else
147-
{
148-
/* Stop detect PenDown */
149-
_nu_adc_control((rt_device_t)psNuAdc, PEPOWER_OFF, RT_NULL);
150-
}
151-
}
144+
#if defined(BSP_USING_ADC_TOUCH)
152145

153-
static int32_t AdcMenuStartCallback(uint32_t status, uint32_t userData)
154-
{
155146
static struct nu_adc_touch_data point;
156147
static rt_bool_t bDrop = RT_FALSE;
157-
static uint16_t u16LastZ0 = 0xfffful;
158-
159-
nu_adc_t psNuAdc = (nu_adc_t)userData;
148+
static uint32_t u32LastZ0 = 0xffffu;
160149

161150
if (psNuAdc->psRtTouch != RT_NULL)
162151
{
163152
uint32_t value;
164153

165154
value = inpw(REG_ADC_XYDATA);
166-
point.u16X = (uint16_t)(value & 0x0ffful);
167-
point.u16Y = (uint16_t)((value >> 16) & 0x0ffful);
155+
point.u32X = (value & 0x0ffful);
156+
point.u32Y = ((value >> 16) & 0x0ffful);
168157

169158
value = inpw(REG_ADC_ZDATA);
170-
point.u16Z0 = (uint16_t)(value & 0x0ffful);
171-
point.u16Z1 = (uint16_t)((value >> 16) & 0x0ffful);
159+
point.u32Z0 = (value & 0x0ffful);
160+
point.u32Z1 = ((value >> 16) & 0x0ffful);
172161

173162
/* Trigger next or not. */
174-
if (point.u16Z0 == 0)
163+
if (point.u32Z0 == 0)
175164
{
176165
/* Stop sampling procedure. */
177166
rt_timer_stop(g_sNuADC.psRtTouchMenuTimer);
@@ -187,14 +176,15 @@ static int32_t AdcMenuStartCallback(uint32_t status, uint32_t userData)
187176
}
188177

189178
/* Notify upper layer. */
190-
if ((!bDrop || (u16LastZ0 != 0)) && rt_mq_send(psNuAdc->m_pmqTouchXYZ, (const void *)&point, sizeof(struct nu_adc_touch_data)) == RT_EOK)
179+
if ((!bDrop || (u32LastZ0 != 0)) && rt_mq_send(psNuAdc->m_pmqTouchXYZ, (const void *)&point, sizeof(struct nu_adc_touch_data)) == RT_EOK)
191180
{
192181
rt_hw_touch_isr(psNuAdc->psRtTouch);
193182
}
194183

195-
u16LastZ0 = point.u16Z0;
184+
u32LastZ0 = point.u32Z0;
196185
}
197186
else
187+
#endif
198188
{
199189
rt_err_t result = rt_sem_release(psNuAdc->m_psSem);
200190
RT_ASSERT(result == RT_EOK);
@@ -203,6 +193,23 @@ static int32_t AdcMenuStartCallback(uint32_t status, uint32_t userData)
203193
return 0;
204194
}
205195

196+
#if defined(BSP_USING_ADC_TOUCH)
197+
198+
void nu_adc_touch_detect(rt_bool_t bStartDetect)
199+
{
200+
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
201+
202+
if (bStartDetect)
203+
{
204+
/* Start detect PenDown */
205+
_nu_adc_control((rt_device_t)psNuAdc, PEPOWER_ON, RT_NULL);
206+
}
207+
else
208+
{
209+
/* Stop detect PenDown */
210+
_nu_adc_control((rt_device_t)psNuAdc, PEPOWER_OFF, RT_NULL);
211+
}
212+
}
206213

207214
static int32_t PenDownCallback(uint32_t status, uint32_t userData)
208215
{
@@ -213,7 +220,7 @@ static int32_t PenDownCallback(uint32_t status, uint32_t userData)
213220
return 0;
214221
}
215222

216-
int32_t nu_adc_read_touch_xyz(uint16_t *bufX, uint16_t *bufY, uint16_t *bufZ0, uint16_t *bufZ1, int32_t dataCnt)
223+
int32_t nu_adc_touch_read_xyz(uint32_t *bufX, uint32_t *bufY, uint32_t *bufZ0, uint32_t *bufZ1, int32_t dataCnt)
217224
{
218225
int i;
219226
struct nu_adc_touch_data value;
@@ -223,14 +230,71 @@ int32_t nu_adc_read_touch_xyz(uint16_t *bufX, uint16_t *bufY, uint16_t *bufZ0, u
223230
if (rt_mq_recv(g_sNuADC.m_pmqTouchXYZ, (void *)&value, sizeof(struct nu_adc_touch_data), 0) == -RT_ETIMEOUT)
224231
break;
225232

226-
bufX[i] = value.u16X;
227-
bufY[i] = value.u16Y;
228-
bufZ0[i] = value.u16Z0;
229-
bufZ1[i] = value.u16Z1;
233+
bufX[i] = value.u32X;
234+
bufY[i] = value.u32Y;
235+
bufZ0[i] = value.u32Z0;
236+
bufZ1[i] = value.u32Z1;
230237
}
231238
return i;
232239
}
233240

241+
void nu_adc_touch_start_conv(void)
242+
{
243+
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
244+
_nu_adc_control((rt_device_t)psNuAdc, START_MST, RT_NULL);
245+
}
246+
247+
rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch)
248+
{
249+
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
250+
nu_adc_cb sNuAdcCb;
251+
252+
rt_adc_enable((rt_adc_device_t)psNuAdc, 4);
253+
rt_adc_enable((rt_adc_device_t)psNuAdc, 5);
254+
rt_adc_enable((rt_adc_device_t)psNuAdc, 6);
255+
rt_adc_enable((rt_adc_device_t)psNuAdc, 7);
256+
257+
outpw(REG_ADC_CONF, (inpw(REG_ADC_CONF) & ~(0xfful << 24)) | 0xfful << 24);
258+
259+
/* Register touch device. */
260+
psNuAdc->psRtTouch = psRtTouch;
261+
262+
/* Enable TouchXY. */
263+
_nu_adc_control((rt_device_t)psNuAdc, T_ON, RT_NULL);
264+
265+
/* Enable TouchZZ. */
266+
_nu_adc_control((rt_device_t)psNuAdc, Z_ON, RT_NULL);
267+
268+
/* Register PenDown callback. */
269+
sNuAdcCb.cbfunc = PenDownCallback;
270+
sNuAdcCb.private_data = (rt_uint32_t)psRtTouch;
271+
_nu_adc_control((rt_device_t)psNuAdc, PEDEF_ON, (void *)&sNuAdcCb);
272+
273+
nu_adc_touch_detect(RT_TRUE);
274+
275+
return RT_EOK;
276+
}
277+
278+
rt_err_t nu_adc_touch_disable(void)
279+
{
280+
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
281+
282+
nu_adc_touch_detect(RT_FALSE);
283+
284+
_nu_adc_control((rt_device_t)psNuAdc, T_OFF, RT_NULL);
285+
_nu_adc_control((rt_device_t)psNuAdc, Z_OFF, RT_NULL);
286+
_nu_adc_control((rt_device_t)psNuAdc, PEDEF_OFF, RT_NULL);
287+
288+
rt_adc_disable((rt_adc_device_t)psNuAdc, 4);
289+
rt_adc_disable((rt_adc_device_t)psNuAdc, 5);
290+
rt_adc_disable((rt_adc_device_t)psNuAdc, 6);
291+
rt_adc_disable((rt_adc_device_t)psNuAdc, 7);
292+
293+
return RT_EOK;
294+
}
295+
296+
#endif
297+
234298
static rt_err_t _nu_adc_control(rt_device_t dev, int cmd, void *args)
235299
{
236300
rt_err_t ret = RT_EINVAL ;
@@ -443,7 +507,9 @@ static rt_err_t _nu_adc_control(rt_device_t dev, int cmd, void *args)
443507
case Z_OFF: /* Disable Press measure function */
444508
{
445509
outpw(REG_ADC_CONF, inpw(REG_ADC_CONF) & ~ADC_CONF_ZEN);
510+
#if defined(BSP_USING_ADC_TOUCH)
446511
rt_mq_control(psNuAdc->m_pmqTouchXYZ, RT_IPC_CMD_RESET, RT_NULL);
512+
#endif
447513
}
448514
break;
449515

@@ -522,61 +588,6 @@ static rt_err_t _nu_adc_control(rt_device_t dev, int cmd, void *args)
522588
return RT_EOK;
523589
}
524590

525-
void nu_adc_touch_start_conv(void)
526-
{
527-
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
528-
_nu_adc_control((rt_device_t)psNuAdc, START_MST, RT_NULL);
529-
}
530-
531-
rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch)
532-
{
533-
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
534-
nu_adc_cb sNuAdcCb;
535-
536-
rt_adc_enable((rt_adc_device_t)psNuAdc, 4);
537-
rt_adc_enable((rt_adc_device_t)psNuAdc, 5);
538-
rt_adc_enable((rt_adc_device_t)psNuAdc, 6);
539-
rt_adc_enable((rt_adc_device_t)psNuAdc, 7);
540-
541-
outpw(REG_ADC_CONF, (inpw(REG_ADC_CONF) & ~(0xfful << 24)) | 0xfful << 24);
542-
543-
/* Register touch device. */
544-
psNuAdc->psRtTouch = psRtTouch;
545-
546-
/* Enable TouchXY. */
547-
_nu_adc_control((rt_device_t)psNuAdc, T_ON, RT_NULL);
548-
549-
/* Enable TouchZZ. */
550-
_nu_adc_control((rt_device_t)psNuAdc, Z_ON, RT_NULL);
551-
552-
/* Register PenDown callback. */
553-
sNuAdcCb.cbfunc = PenDownCallback;
554-
sNuAdcCb.private_data = (rt_uint32_t)psRtTouch;
555-
_nu_adc_control((rt_device_t)psNuAdc, PEDEF_ON, (void *)&sNuAdcCb);
556-
557-
nu_adc_touch_detect(RT_TRUE);
558-
559-
return RT_EOK;
560-
}
561-
562-
rt_err_t nu_adc_touch_disable(void)
563-
{
564-
nu_adc_t psNuAdc = (nu_adc_t)&g_sNuADC;
565-
566-
nu_adc_touch_detect(RT_FALSE);
567-
568-
_nu_adc_control((rt_device_t)psNuAdc, T_OFF, RT_NULL);
569-
_nu_adc_control((rt_device_t)psNuAdc, Z_OFF, RT_NULL);
570-
_nu_adc_control((rt_device_t)psNuAdc, PEDEF_OFF, RT_NULL);
571-
572-
rt_adc_disable((rt_adc_device_t)psNuAdc, 4);
573-
rt_adc_disable((rt_adc_device_t)psNuAdc, 5);
574-
rt_adc_disable((rt_adc_device_t)psNuAdc, 6);
575-
rt_adc_disable((rt_adc_device_t)psNuAdc, 7);
576-
577-
return RT_EOK;
578-
}
579-
580591
static rt_err_t _nu_adc_open(rt_device_t dev, rt_uint16_t oflag)
581592
{
582593
nu_adc_t psNuAdc = (nu_adc_t)dev;
@@ -709,11 +720,13 @@ int rt_hw_adc_init(void)
709720
g_sNuADC.m_psSem = rt_sem_create("adc_mst_sem", 0, RT_IPC_FLAG_FIFO);
710721
RT_ASSERT(g_sNuADC.m_psSem != RT_NULL);
711722

723+
#if defined(BSP_USING_ADC_TOUCH)
712724
g_sNuADC.m_pmqTouchXYZ = rt_mq_create("ADC_TOUCH_XYZ", sizeof(struct nu_adc_touch_data), TOUCH_MQ_LENGTH, RT_IPC_FLAG_FIFO);
713725
RT_ASSERT(g_sNuADC.m_pmqTouchXYZ != RT_NULL);
714726

715727
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);
716728
RT_ASSERT(g_sNuADC.psRtTouchMenuTimer != RT_NULL);
729+
#endif
717730

718731
rt_memset(&g_sNuADC.m_isr, 0, sizeof(g_sNuADC.m_isr));
719732
rt_memset(&g_sNuADC.m_wkisr, 0, sizeof(g_sNuADC.m_wkisr));

bsp/nuvoton/libraries/n9h30/rtt_port/drv_adc.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515

1616
#include <rtthread.h>
1717
#include "nu_adc.h"
18-
#include "touch.h"
18+
#if defined(BSP_USING_ADC_TOUCH)
19+
#include "touch.h"
20+
#endif
1921

20-
#define TOUCH_MQ_LENGTH 128
22+
#define TOUCH_MQ_LENGTH 64
23+
24+
#define DEF_CAL_POINT_NUM 5
2125

2226
typedef enum
2327
{
@@ -52,13 +56,29 @@ typedef struct
5256

5357
typedef nu_adc_cb *nu_adc_cb_t;
5458

55-
int32_t nu_adc_read_touch_xyz(uint16_t *bufX, uint16_t *bufY, uint16_t *bufZ0, uint16_t *bufZ1, int32_t dataCnt);
59+
#if defined(BSP_USING_ADC_TOUCH)
60+
typedef struct
61+
{
62+
int32_t x;
63+
int32_t y;
64+
} S_COORDINATE_POINT;
65+
66+
typedef struct
67+
{
68+
int32_t a;
69+
int32_t b;
70+
int32_t c;
71+
int32_t d;
72+
int32_t e;
73+
int32_t f;
74+
int32_t div;
75+
} S_CALIBRATION_MATRIX;
76+
77+
int32_t nu_adc_touch_read_xyz(uint32_t *bufX, uint32_t *bufY, uint32_t *bufZ0, uint32_t *bufZ1, int32_t dataCnt);
5678
rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch);
5779
rt_err_t nu_adc_touch_disable(void);
5880
void nu_adc_touch_detect(rt_bool_t bStartDetect);
5981
void nu_adc_touch_start_conv(void);
60-
61-
void nu_adc_touch_update_caldata(int *psi32NewValue);
62-
void nu_adc_touch_reset_caldata(int *psi32NewValue);
82+
#endif
6383

6484
#endif /* __DRV_ADC_H__ */

0 commit comments

Comments
 (0)