Commit 22b4c28f authored by Daniel Campora's avatar Daniel Campora

cc3200: New ADC API.

parent 0e52d986
......@@ -35,6 +35,8 @@ class AF:
def __init__(self, name, idx, fn, unit, type):
self.name = name
self.idx = idx
if self.idx > 15:
self.idx = -1
self.fn = fn
self.unit = unit
self.type = type
......@@ -57,12 +59,16 @@ class Pin:
def print(self):
print('// {}'.format(self.name))
print('const pin_af_t pin_{}_af[] = {{'.format(self.name))
for af in self.afs:
af.print()
print('};')
print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n'.format(
self.name, self.name, self.port, self.gpio_bit, self.pin_num, self.name, len(self.afs)))
if len(self.afs):
print('const pin_af_t pin_{}_af[] = {{'.format(self.name))
for af in self.afs:
af.print()
print('};')
print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n'.format(
self.name, self.name, self.port, self.gpio_bit, self.pin_num, self.name, len(self.afs)))
else:
print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, NULL, 0);\n'.format(
self.name, self.name, self.port, self.gpio_bit, self.pin_num))
def print_header(self, hdr_file):
hdr_file.write('extern pin_obj_t pin_{:s};\n'.format(self.name))
......
This diff is collapsed.
......@@ -184,6 +184,16 @@ uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type)
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
uint8_t pin_find_peripheral_type (const mp_obj_t pin, uint8_t fn, uint8_t unit) {
pin_obj_t *pin_o = pin_find(pin);
for (int i = 0; i < pin_o->num_afs; i++) {
if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].unit == unit) {
return pin_o->af_list[i].type;
}
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
/******************************************************************************
DEFINE PRIVATE FUNCTIONS
******************************************************************************/
......
......@@ -85,15 +85,15 @@ enum {
};
enum {
PIN_TYPE_ADC_CH0 = -1,
PIN_TYPE_ADC_CH1 = -1,
PIN_TYPE_ADC_CH2 = -1,
PIN_TYPE_ADC_CH3 = -1,
PIN_TYPE_ADC_CH0 = 0,
PIN_TYPE_ADC_CH1,
PIN_TYPE_ADC_CH2,
PIN_TYPE_ADC_CH3,
};
typedef struct {
qstr name;
uint8_t idx;
int8_t idx;
uint8_t fn;
uint8_t unit;
uint8_t type;
......@@ -136,5 +136,6 @@ void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint s
pin_obj_t *pin_find(mp_obj_t user_obj);
void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit);
uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type);
uint8_t pin_find_peripheral_type (const mp_obj_t pin, uint8_t fn, uint8_t unit);
#endif // PYBPIN_H_
......@@ -177,9 +177,13 @@ Q(MASTER)
// for ADC class
Q(ADC)
Q(read)
Q(ADCChannel)
Q(value)
Q(init)
Q(deinit)
Q(channel)
Q(id)
Q(pin)
#if MICROPY_HW_HAS_SDCARD
// for SD class
......
'''
ADC test for the CC3200 based boards.
'''
from pyb import ADC
from pyb import Pin
import os
machine = os.uname().machine
if 'LaunchPad' in machine:
adc_pin = 'GP5'
adc_channel = 3
elif 'WiPy' in machine:
adc_pin = 'GP3'
adc_channel = 1
else:
raise Exception('Board not supported!')
adc = ADC(0)
print(adc)
adc = ADC()
print(adc)
adc = ADC(0, bits=12)
print(adc)
apin = adc.channel(adc_channel)
print(apin)
apin = adc.channel(id=adc_channel)
print(apin)
apin = adc.channel(adc_channel, pin=adc_pin)
print(apin)
apin = adc.channel(id=adc_channel, pin=adc_pin)
print(apin)
print(apin.value() > 3000)
print(apin() > 3000)
# de-init must work
apin.deinit()
print(apin)
adc.deinit()
print(adc)
print(apin)
adc.init()
print(adc)
print(apin)
apin.init()
print(apin)
print(apin() > 3000)
# check for memory leaks...
for i in range (0, 1000):
adc = ADC()
apin = adc.channel(adc_channel)
# next ones should raise
try:
adc = ADC(bits=17)
except:
print('Exception')
try:
adc = ADC(id=1)
except:
print('Exception')
try:
adc = ADC(0, 16)
except:
print('Exception')
adc = ADC()
try:
apin = adc.channel(4)
except:
print('Exception')
try:
apin = adc.channel(-1)
except:
print('Exception')
try:
apin = adc.channel(0, pin='GP3')
except:
print('Exception')
apin = adc.channel(1)
apin.deinit()
try:
apin()
except:
print('Exception')
try:
apin.value()
except:
print('Exception')
adc.deinit()
try:
apin.value()
except:
print('Exception')
try:
apin = adc.channel(1)
except:
print('Exception')
# re-init must work
adc.init()
apin.init()
print(apin)
print(apin() > 3000)
ADC(0, bits=12)
ADC(0, bits=12)
ADC(0, bits=12)
ADCChannel(1, pin=GP3)
ADCChannel(1, pin=GP3)
ADCChannel(1, pin=GP3)
ADCChannel(1, pin=GP3)
True
True
ADCChannel(1)
ADC(0)
ADCChannel(1)
ADC(0, bits=12)
ADCChannel(1)
ADCChannel(1, pin=GP3)
True
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
ADCChannel(1, pin=GP3)
True
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment