Skip to content

Commit 7027fd5

Browse files
author
Daniel Campora
committed
cc3200: Make ADC API compatible with the pyboard.
1 parent 7da2fdc commit 7027fd5

3 files changed

Lines changed: 47 additions & 70 deletions

File tree

cc3200/mods/pybadc.c

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@
5757
///
5858
/// Usage:
5959
///
60-
/// adc = pyb.ADC(channel) # create an adc object on the given channel (1 to 4)
61-
/// this automatically configures the pin associated to
62-
/// that analog channel.
63-
/// adc.read() # read channel value
60+
/// adc = pyb.ADC('GP5') # create an adc object on the given pin (GP2, GP3, GP4 o GP5)
61+
/// adc.read() # read channel value
6462
///
6563
/// The sample rate is fixed to 62.5KHz and the resolution to 12 bits.
6664

@@ -75,14 +73,23 @@
7573
******************************************************************************/
7674
typedef struct {
7775
mp_obj_base_t base;
76+
pin_obj_t *pin;
7877
byte channel;
79-
byte idx;
78+
byte id;
8079
} pyb_adc_obj_t;
8180

81+
/******************************************************************************
82+
DECLARE PRIVATE DATA
83+
******************************************************************************/
84+
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS] = { {.pin = &pin_GP2, .channel = ADC_CH_0, .id = 1}, {.pin = &pin_GP3, .channel = ADC_CH_1, .id = 2},
85+
{.pin = &pin_GP4, .channel = ADC_CH_2, .id = 2}, {.pin = &pin_GP5, .channel = ADC_CH_3, .id = 4} };
86+
8287
/******************************************************************************
8388
DEFINE PUBLIC FUNCTIONS
8489
******************************************************************************/
8590
STATIC void pybadc_init (pyb_adc_obj_t *self) {
91+
// configure the pin in analog mode
92+
pin_config (self->pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);
8693
// enable the ADC channel
8794
MAP_ADCChannelEnable(ADC_BASE, self->channel);
8895
// enable and configure the timer
@@ -92,68 +99,34 @@ STATIC void pybadc_init (pyb_adc_obj_t *self) {
9299
MAP_ADCEnable(ADC_BASE);
93100
}
94101

95-
/******************************************************************************
96-
DECLARE PRIVATE DATA
97-
******************************************************************************/
98-
STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
99-
100102
/******************************************************************************/
101103
/* Micro Python bindings : adc object */
102104

103105
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
104106
pyb_adc_obj_t *self = self_in;
105-
mp_printf(print, "<ADC, channel=%u>", (self->idx + 1));
107+
mp_printf(print, "<ADC1 channel=%u on %q>", self->id, self->pin->name);
106108
}
107109

108-
/// \classmethod \constructor(channel)
109-
/// Create an ADC object associated with the given channel.
110+
/// \classmethod \constructor(pin)
111+
/// Create an ADC object associated with the given pin.
110112
/// This allows you to then read analog values on that pin.
111113
STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
112114
// check number of arguments
113115
mp_arg_check_num(n_args, n_kw, 1, 1, false);
114116

115-
// the first argument is the channel number
116-
int32_t idx = mp_obj_get_int(args[0]) - 1;
117-
const pin_obj_t *pin;
118-
uint channel;
119-
switch (idx) {
120-
case 0:
121-
channel = ADC_CH_0;
122-
pin = &pin_GP2;
123-
break;
124-
case 1:
125-
channel = ADC_CH_1;
126-
pin = &pin_GP3;
127-
break;
128-
case 2:
129-
channel = ADC_CH_2;
130-
pin = &pin_GP4;
131-
break;
132-
case 3:
133-
channel = ADC_CH_3;
134-
pin = &pin_GP5;
135-
break;
136-
default:
137-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
138-
break;
117+
// the argument passed is the pin
118+
const pin_obj_t *pin = (pin_obj_t *)pin_find(args[0]);
119+
for (int32_t idx = 0; idx < PYB_ADC_NUM_CHANNELS; idx++) {
120+
if (pin == pyb_adc_obj[idx].pin) {
121+
pyb_adc_obj_t *self = &pyb_adc_obj[idx];
122+
self->base.type = &pyb_adc_type;
123+
pybadc_init (self);
124+
// register it with the sleep module
125+
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);
126+
return self;
127+
}
139128
}
140-
141-
// disable the callback before re-configuring
142-
pyb_adc_obj_t *self = &pyb_adc_obj[idx];
143-
self->base.type = &pyb_adc_type;
144-
self->channel = channel;
145-
self->idx = idx;
146-
147-
// configure the pin in analog mode
148-
pin_config ((pin_obj_t *)pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);
149-
150-
// initialize it
151-
pybadc_init (self);
152-
153-
// register it with the sleep module
154-
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);
155-
156-
return self;
129+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
157130
}
158131

159132
/// \method read()
@@ -172,32 +145,32 @@ STATIC mp_obj_t adc_read(mp_obj_t self_in) {
172145
}
173146
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_read_obj, adc_read);
174147

175-
/// \method enable()
148+
/// \method init()
176149
/// Enable the adc channel
177-
STATIC mp_obj_t adc_enable(mp_obj_t self_in) {
150+
STATIC mp_obj_t adc_init(mp_obj_t self_in) {
178151
pyb_adc_obj_t *self = self_in;
179152

180153
pybadc_init(self);
181154
return mp_const_none;
182155
}
183-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_enable_obj, adc_enable);
156+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_init_obj, adc_init);
184157

185-
/// \method disable()
158+
/// \method deinit()
186159
/// Disable the adc channel
187-
STATIC mp_obj_t adc_disable(mp_obj_t self_in) {
160+
STATIC mp_obj_t adc_deinit(mp_obj_t self_in) {
188161
pyb_adc_obj_t *self = self_in;
189162

190163
MAP_ADCChannelDisable(ADC_BASE, self->channel);
191164
// unregister it with the sleep module
192165
pybsleep_remove ((const mp_obj_t)self);
193166
return mp_const_none;
194167
}
195-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_disable_obj, adc_disable);
168+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adc_deinit_obj, adc_deinit);
196169

197170
STATIC const mp_map_elem_t adc_locals_dict_table[] = {
198171
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&adc_read_obj },
199-
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&adc_enable_obj },
200-
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&adc_disable_obj },
172+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&adc_init_obj },
173+
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&adc_deinit_obj },
201174
};
202175

203176
STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table);

cc3200/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Q(MASTER)
182182
// for ADC class
183183
Q(ADC)
184184
Q(read)
185+
Q(init)
186+
Q(deinit)
185187

186188
#if MICROPY_HW_HAS_SDCARD
187189
// for SD class

docs/library/pyb.ADC.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class ADC -- analog to digital conversion: read analog values on a pin
2424
2525
import pyb
2626

27-
adc = pyb.ADC(channel) # create an analog object on one of the 4 ADC channels
27+
adc = pyb.ADC(pin) # create an analog object on one of the 4 ADC channels
2828
val = adc.read() # read an analog value
29+
adc.deinit() # disable the adc channel
30+
adc.init() # enable the adc channel
2931

3032
Constructors
3133
------------
@@ -39,13 +41,13 @@ Constructors
3941

4042
.. only:: port_wipy
4143

42-
.. class:: pyb.ADC(channel)
44+
.. class:: pyb.ADC(pin)
4345

44-
Create an ADC object on the given channel. Each channel is associated
45-
to a specific pin. For more info check the `pinout and alternate functions
46-
table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
46+
Create an ADC object associated with the given pin.
4747
This allows you to then read analog values on that pin.
48-
48+
For more info check the `pinout and alternate functions
49+
table. <https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png>`_
50+
4951
.. warning::
5052

5153
ADC pin input range is 0-1.4V (being 1.8V the absolute maximum that it
@@ -100,10 +102,10 @@ Methods
100102

101103
.. only:: port_wipy
102104

103-
.. method:: adc.enable()
105+
.. method:: adc.init()
104106

105107
Enable the ADC channel.
106108

107-
.. method:: adc.disable()
109+
.. method:: adc.deinit()
108110

109111
Disable the ADC channel.

0 commit comments

Comments
 (0)