-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_adc.c
More file actions
199 lines (168 loc) · 5.37 KB
/
app_adc.c
File metadata and controls
199 lines (168 loc) · 5.37 KB
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
/*
* Copyright 2025 Atmark Techno
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "fsl_iomuxc.h"
#include "fsl_reset.h"
#include "fsl_lpadc.h"
#include "app_srtm_internal.h"
#include "srtm_adc_service.h"
#include "build_bug.h"
#define ADC_MAX_PORTS 4
#define ADC_MAX 2
/* forward declarations to use in adcAdapter */
static srtm_status_t adc_get(uint8_t idx, uint16_t *value);
static srtm_status_t adc_init(uint8_t idx, struct srtm_adc_init_payload *init);
/* global state */
static struct adc_handle *adcHandles[ADC_MAX_PORTS];
static bool adcInit[ADC_MAX];
static srtm_service_t adcService;
static struct _srtm_adc_adapter adcAdapter = {
.get = adc_get,
.init = adc_init,
};
/**********************************************************
* SRTM callbacks
*********************************************************/
static srtm_status_t adc_get(uint8_t idx, uint16_t *value)
{
lpadc_conv_result_t resultConfig;
if (idx > ARRAY_SIZE(adcHandles))
{
PRINTF("adc_get called with idx %d > %d\r\n", idx, ARRAY_SIZE(adcHandles));
return kStatus_Fail;
}
struct adc_handle *handle = adcHandles[idx];
if (!handle)
{
PRINTF("adc_get called with non-init'd adc %d\r\n", idx);
return kStatus_Fail;
}
LPADC_DoSoftwareTrigger(handle->base, 1 << idx);
LPADC_GetConvResultBlocking(handle->base, &resultConfig);
*value = resultConfig.convValue >> 3U;
return kStatus_Success;
}
int adc_init_device(struct adc_handle *handle, uint8_t idx)
{
int adcIdx;
switch ((uint32_t)handle->base)
{
case ADC0_BASE:
adcIdx = 0;
if (!adcInit[adcIdx])
{
CLOCK_SetIpSrc(kCLOCK_Adc0, kCLOCK_Pcc0BusIpSrcCm33Bus);
RESET_PeripheralReset(kRESET_Adc0);
}
break;
case ADC1_BASE:
adcIdx = 1;
if (!adcInit[adcIdx])
{
CLOCK_SetIpSrc(kCLOCK_Adc1, kCLOCK_Pcc1BusIpSrcCm33Bus);
RESET_PeripheralReset(kRESET_Adc1);
}
break;
default:
return kStatus_InvalidArgument;
}
/* only init ADC once per ADC */
if (!adcInit[adcIdx])
{
lpadc_config_t config;
LPADC_GetDefaultConfig(&config);
config.enableAnalogPreliminary = true;
LPADC_Init(handle->base, &config);
adcInit[adcIdx] = true;
}
lpadc_conv_command_config_t commandConfig;
LPADC_GetDefaultConvCommandConfig(&commandConfig);
commandConfig.channelNumber = handle->chan;
commandConfig.sampleChannelMode = handle->side;
commandConfig.sampleScaleMode = handle->scale;
commandConfig.hardwareAverageMode = handle->average;
LPADC_SetConvCommandConfig(handle->base, idx + 1, &commandConfig);
lpadc_conv_trigger_config_t triggerConfig;
LPADC_GetDefaultConvTriggerConfig(&triggerConfig);
triggerConfig.targetCommandId = idx + 1;
triggerConfig.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(handle->base, idx, &triggerConfig);
return 0;
}
srtm_status_t adc_init(uint8_t idx, struct srtm_adc_init_payload *init)
{
/* We use index as command id / trigger id; this will need rework if we use more than 4:
* - for command id, 1-15 are usable
* - for trigger id, 0-3 are usable
* - (note this is per ADC so a first improvement would be to count per ADC, then
* we'd need to disable/re-enable triggers as appropriate...)
*/
BUILD_BUG_ON(ADC_MAX_PORTS > 4);
if (idx >= ADC_MAX_PORTS)
{
PRINTF("adc %d: index %d too big\r\n", idx, idx);
return kStatus_Fail;
}
if (adcHandles[idx])
{
PRINTF("adc %d: index %d already setup\r\n", idx, idx);
return kStatus_Fail;
}
ADC_Type *adc_base;
switch (init->adc_index)
{
case 0:
adc_base = ADC0;
break;
case 1:
adc_base = ADC1;
break;
default:
BUILD_BUG_ON(ADC_MAX != 2);
PRINTF("adc %d: adc_index %d unsupported\r\n", idx, init->adc_index);
return kStatus_Fail;
}
PRINTF("ADC %d init: ADC %x, chan %d, side %d, scale %d, average %d\r\n", idx, init->adc_index, init->adc_chan,
init->adc_side, init->adc_scale, init->adc_average);
struct adc_handle *handle = pvPortMalloc(sizeof(handle));
if (!handle)
return kStatus_Fail;
handle->base = adc_base;
handle->chan = init->adc_chan;
handle->side = init->adc_side;
handle->scale = init->adc_scale;
handle->average = init->adc_average;
int rc = adc_init_device(handle, idx);
if (rc)
{
vPortFree(handle);
return rc;
}
adcHandles[idx] = handle;
return kStatus_Success;
}
/**********************************************************
* init/PM hooks called from APP_SRTM
*********************************************************/
void APP_ADC_InitService(void)
{
APP_ADC_Resume();
/* Create and register adc service */
adcService = SRTM_AdcService_Create(&adcAdapter);
SRTM_Dispatcher_RegisterService(disp, adcService);
}
void APP_ADC_Resume(void)
{
size_t i;
/* reset and re-init */
memset(&adcInit, 0, sizeof(adcInit));
for (i = 0; i < ADC_MAX_PORTS; i++)
{
if (!adcHandles[i])
continue;
adc_init_device(adcHandles[i], i);
}
}