-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathwiring_analog_nRF51.c
397 lines (323 loc) · 11.1 KB
/
wiring_analog_nRF51.c
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
/*
Copyright (c) 2014 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef NRF51
#include "nrf.h"
#include "Arduino.h"
#include "wiring_private.h"
#ifdef __cplusplus
extern "C" {
#endif
static uint32_t adcReference = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling;
static uint32_t adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling;
NRF_TIMER_Type* pwms[PWM_MODULE_COUNT] = {
NRF_TIMER1,
NRF_TIMER2
};
struct PWMContext pwmContext[PWM_COUNT] = {
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE1_Msk, 1, 1, 0 },
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE2_Msk, 2, 2, 0 },
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE3_Msk, 3, 3, 0 },
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE1_Msk, 1, 1, 1 },
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE2_Msk, 2, 2, 1 },
{ PIN_FREE, 0, TIMER_INTENSET_COMPARE3_Msk, 3, 3, 1 }
};
struct PWMStatus pwmStatus[PWM_MODULE_COUNT] = {{0, TIMER1_IRQn},{0, TIMER2_IRQn}};
static uint32_t readResolution = 10;
static uint32_t writeResolution = 8;
static uint32_t halfAnalogWriteMax = 128; // default for 8b
static uint32_t NRF_TIMER_BITMODE = TIMER_BITMODE_BITMODE_08Bit;
void analogReadResolution( int res )
{
readResolution = res;
}
void analogWriteResolution( int res )
{
writeResolution = res;
// TIMER1 has either 16b or 8b PWM resolution
if ((res > 1) && (res < 17))
{
if (res < 9)
{
halfAnalogWriteMax = 128;
NRF_TIMER_BITMODE = TIMER_BITMODE_BITMODE_08Bit;
}
else
{
halfAnalogWriteMax = 32768; // (2^16) >> 1
NRF_TIMER_BITMODE = TIMER_BITMODE_BITMODE_16Bit;
}
}
else //default to 8b for any invalid res values
{
writeResolution = 8;
halfAnalogWriteMax = 128;
NRF_TIMER_BITMODE = TIMER_BITMODE_BITMODE_08Bit;
}
}
static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to )
{
if ( from == to )
{
return value ;
}
if ( from > to )
{
return value >> (from-to) ;
}
else
{
return value << (to-from) ;
}
}
/*
* Internal VBG Reference is 1.2 V.
* External References AREF0 and AREF1 should be between 0.83 V - 1.3 V.
*
* Warning : ADC should not be exposed to > 2.4 V, calculated after prescaling.
* GPIO pins must not be exposed to higher voltage than VDD + 0.3 V.
*/
void analogReference( eAnalogReference ulMode )
{
switch ( ulMode ) {
case AR_VBG:
// 1.2 Reference, 1/3 prescaler = 0 V - 3.6 V range
// Minimum VDD for full range in safe operation = 3.3V
adcReference = ADC_CONFIG_REFSEL_VBG;
adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling;
break;
case AR_SUPPLY_ONE_HALF:
// 1/2 VDD Reference, 2/3 prescaler = 0 V - 0.75VDD range
adcReference = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling;
adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling;
break;
case AR_EXT0:
// ARF0 reference, 2/3 prescaler = 0 V - 1.5 ARF0
adcReference = ADC_CONFIG_REFSEL_External | (ADC_CONFIG_EXTREFSEL_AnalogReference0 << ADC_CONFIG_EXTREFSEL_Pos);
adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling;
break;
case AR_EXT1:
// ARF1 reference, 2/3 prescaler = 0 V - 1.5 ARF1
adcReference = (ADC_CONFIG_REFSEL_External | ADC_CONFIG_EXTREFSEL_AnalogReference1 << ADC_CONFIG_EXTREFSEL_Pos);
adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling;
break;
case AR_SUPPLY_ONE_THIRD:
case AR_DEFAULT:
default:
// 1/3 VDD Reference, 1/3 prescaler = 0 V - VDD range
adcReference = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling;
adcPrescaling = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling;
break;
}
}
uint32_t analogRead( uint32_t ulPin )
{
uint32_t pin = ADC_CONFIG_PSEL_Disabled;
uint32_t adcResolution;
uint32_t resolution;
int16_t value;
if (ulPin >= PINS_COUNT) {
return 0;
}
ulPin = g_ADigitalPinMap[ulPin];
switch ( ulPin ) {
case 26:
pin = ADC_CONFIG_PSEL_AnalogInput0;
break;
case 27:
pin = ADC_CONFIG_PSEL_AnalogInput1;
break;
case 1:
pin = ADC_CONFIG_PSEL_AnalogInput2;
break;
case 2:
pin = ADC_CONFIG_PSEL_AnalogInput3;
break;
case 3:
pin = ADC_CONFIG_PSEL_AnalogInput4;
break;
case 4:
pin = ADC_CONFIG_PSEL_AnalogInput5;
break;
case 5:
pin = ADC_CONFIG_PSEL_AnalogInput6;
break;
case 6:
pin = ADC_CONFIG_PSEL_AnalogInput7;
break;
default:
return 0;
}
if (readResolution <= 8) {
resolution = 8;
adcResolution = ADC_CONFIG_RES_8bit;
} else if (readResolution <= 9) {
resolution = 9;
adcResolution = ADC_CONFIG_RES_9bit;
} else {
resolution = 10;
adcResolution = ADC_CONFIG_RES_10bit;
}
NRF_ADC->ENABLE = 1;
uint32_t config_reg = 0;
config_reg |= ((uint32_t)adcResolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk;
config_reg |= ((uint32_t)adcPrescaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk;
config_reg |= ((uint32_t)adcReference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk;
if (adcReference & ADC_CONFIG_EXTREFSEL_Msk)
{
config_reg |= adcReference & ADC_CONFIG_EXTREFSEL_Msk;
}
NRF_ADC->CONFIG = ((uint32_t)pin << ADC_CONFIG_PSEL_Pos) | (NRF_ADC->CONFIG & ~ADC_CONFIG_PSEL_Msk);
NRF_ADC->CONFIG = config_reg | (NRF_ADC->CONFIG & ADC_CONFIG_PSEL_Msk);
NRF_ADC->TASKS_START = 1;
while(!NRF_ADC->EVENTS_END);
NRF_ADC->EVENTS_END = 0;
value = (int32_t)NRF_ADC->RESULT;
NRF_ADC->TASKS_STOP = 1;
NRF_ADC->ENABLE = 0;
return mapResolution(value, resolution, readResolution);
}
// Right now, PWM output only works on the pins with
// hardware support. These are defined in the appropriate
// pins_*.c file. For the rest of the pins, we default
// to digital output.
void analogWrite( uint32_t ulPin, uint32_t ulValue )
{
if (ulPin >= PINS_COUNT) {
return;
}
uint32_t ulPin_ = g_ADigitalPinMap[ulPin];
// Turn off PWM if duty cycle == 0
if (ulValue == 0)
{
for (uint8_t i = 0; i < PWM_COUNT; i++)
{
if (pwmContext[i].pin == ulPin_)
{
pwmContext[i].pin = PIN_FREE;
pwmStatus[pwmContext[i].module].numActive--;
// allocate the pwm channel
NRF_TIMER_Type* pwm = pwms[pwmContext[i].module];
// Turn off the PWM module if no pwm channels are allocated
if (pwmStatus[pwmContext[i].module].numActive == 0)
{
NVIC_ClearPendingIRQ(pwmStatus[pwmContext[i].module].irqNumber);
NVIC_DisableIRQ(pwmStatus[pwmContext[i].module].irqNumber);
pwm->TASKS_STOP = 1;
pwm->TASKS_CLEAR = 1;
}
digitalWrite(ulPin, 0);
}
}
return;
}
for (uint8_t i = 0; i < PWM_COUNT; i++)
{
if (pwmContext[i].pin == PIN_FREE || pwmContext[i].pin == ulPin_)
{
pwmContext[i].pin = ulPin_;
NRF_GPIO->PIN_CNF[ulPin_] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
// rescale from an arbitrary resolution to either 8b or 16b (transparent to the user)
// This assumes that the user wont specify an 8b res then pass a 16b res value..
pwmContext[i].value = mapResolution( ulValue, writeResolution, (writeResolution < 9)?8:16);
// allocate the pwm channel
NRF_TIMER_Type* pwm = pwms[pwmContext[i].module];
// if this is the first channel allocated to the module, turn on pwm
if (pwmStatus[pwmContext[i].module].numActive == 0)
{
NVIC_SetPriority(pwmStatus[pwmContext[i].module].irqNumber, 3);
NVIC_ClearPendingIRQ(pwmStatus[pwmContext[i].module].irqNumber);
NVIC_EnableIRQ(pwmStatus[pwmContext[i].module].irqNumber);
pwm->MODE = (pwm->MODE & ~TIMER_MODE_MODE_Msk) | ((TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk);
pwm->BITMODE = (pwm->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) | ((NRF_TIMER_BITMODE << TIMER_BITMODE_BITMODE_Pos) & TIMER_BITMODE_BITMODE_Msk);
pwm->PRESCALER = (pwm->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) | ((7 << TIMER_PRESCALER_PRESCALER_Pos) & TIMER_PRESCALER_PRESCALER_Msk);
pwm->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
pwm->CC[0] = 0;
pwm->TASKS_START = 0x1UL;
}
pwm->CC[pwmContext[i].channel] = ulValue;
pwm->INTENSET |= pwmContext[i].mask;
pwmStatus[pwmContext[i].module].numActive++;
return;
}
}
// fallback to digitalWrite if no available PWM channel
if (ulValue < halfAnalogWriteMax)
{
digitalWrite(ulPin, LOW);
}
else
{
digitalWrite(ulPin, HIGH);
}
}
void TIMER1_IRQHandler(void)
{
if (NRF_TIMER1->EVENTS_COMPARE[0]) // channel 0 sets all PWM signals HIGH
{
for (uint8_t i = 0; i < PWM_CHANNEL_COUNT; i++)
{
if (pwmContext[i].pin != PIN_FREE && pwmContext[i].value != 0)
{
NRF_GPIO->OUTSET = (1UL << pwmContext[i].pin);
}
}
NRF_TIMER1->EVENTS_COMPARE[0] = 0;
}
for (uint8_t i = 0; i < PWM_CHANNEL_COUNT; i++) // compare to CC sets the individual PWM signal LOW
{
if (NRF_TIMER1->EVENTS_COMPARE[pwmContext[i].event])
{
if (pwmContext[i].pin != PIN_FREE && pwmContext[i].value != 2*halfAnalogWriteMax - 1)
{
NRF_GPIO->OUTCLR = (1UL << pwmContext[i].pin);
}
NRF_TIMER1->EVENTS_COMPARE[pwmContext[i].event] = 0;
}
}
}
void TIMER2_IRQHandler(void)
{
if (NRF_TIMER2->EVENTS_COMPARE[0]) // channel 0 sets all PWM signals HIGH
{
for (uint8_t i = PWM_CHANNEL_COUNT; i < 2*PWM_CHANNEL_COUNT; i++)
{
if (pwmContext[i].pin != PIN_FREE && pwmContext[i].value != 0)
{
NRF_GPIO->OUTSET = (1UL << pwmContext[i].pin);
}
}
NRF_TIMER2->EVENTS_COMPARE[0] = 0;
}
for (uint8_t i = PWM_CHANNEL_COUNT; i < 2*PWM_CHANNEL_COUNT; i++) // compare to CC sets the individual PWM signal LOW
{
if (NRF_TIMER2->EVENTS_COMPARE[pwmContext[i].event])
{
if (pwmContext[i].pin != PIN_FREE && pwmContext[i].value != 2*halfAnalogWriteMax - 1)
{
NRF_GPIO->OUTCLR = (1UL << pwmContext[i].pin);
}
NRF_TIMER2->EVENTS_COMPARE[pwmContext[i].event] = 0;
}
}
}
#ifdef __cplusplus
}
#endif
#endif