diff options
Diffstat (limited to 'atmel-samd/common-hal/analogio')
| -rw-r--r-- | atmel-samd/common-hal/analogio/AnalogIn.c | 116 | ||||
| -rw-r--r-- | atmel-samd/common-hal/analogio/AnalogIn.h | 49 | ||||
| -rw-r--r-- | atmel-samd/common-hal/analogio/AnalogOut.c | 71 | ||||
| -rw-r--r-- | atmel-samd/common-hal/analogio/AnalogOut.h | 41 | ||||
| -rw-r--r-- | atmel-samd/common-hal/analogio/__init__.c | 1 |
5 files changed, 278 insertions, 0 deletions
diff --git a/atmel-samd/common-hal/analogio/AnalogIn.c b/atmel-samd/common-hal/analogio/AnalogIn.c new file mode 100644 index 000000000..59ea3b899 --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogIn.c @@ -0,0 +1,116 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "common-hal/analogio/AnalogIn.h" + +#include <string.h> + +#include "py/gc.h" +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "shared-bindings/analogio/AnalogIn.h" + +#include "asf/sam0/drivers/adc/adc.h" +#include "samd21_pins.h" + +// Number of active ADC channels. +volatile uint8_t active_channel_count; +struct adc_module *adc_instance = NULL; + +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, + const mcu_pin_obj_t *pin) { + if (!pin->has_adc) { + // No ADC function on that pin + mp_raise_ValueError("Pin does not have ADC capabilities"); + } + + self->pin = pin; + + if (adc_instance == NULL) { + struct adc_config config_adc; + adc_get_config_defaults(&config_adc); + + config_adc.reference = ADC_REFERENCE_INTVCC1; + config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2; + config_adc.positive_input = self->pin->adc_input; + config_adc.resolution = ADC_RESOLUTION_16BIT; + config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128; + + // Allocate the instance on the heap so we only use the memory when we + // need it. + adc_instance = gc_alloc(sizeof(struct adc_module), false); + + adc_init(adc_instance, ADC, &config_adc); + } + + self->adc_instance = adc_instance; + active_channel_count++; +} + +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { + active_channel_count--; + if (active_channel_count == 0) { + adc_reset(adc_instance); + gc_free(adc_instance); + // Set our reference to NULL so the GC doesn't mistakenly see the + // pointer in memory. + adc_instance = NULL; + } + reset_pin(self->pin->pin); +} + +void analogin_reset() { + if (adc_instance != NULL) { + adc_reset(adc_instance); + adc_instance = NULL; + } + active_channel_count = 0; +} + +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { + adc_set_positive_input(adc_instance, self->pin->adc_input); + + adc_enable(adc_instance); + adc_start_conversion(adc_instance); + + uint16_t data; + enum status_code status = adc_read(adc_instance, &data); + while (status == STATUS_BUSY) { + status = adc_read(adc_instance, &data); + } + if (status == STATUS_ERR_OVERFLOW) { + // TODO(tannewt): Throw an error. + } + + adc_disable(adc_instance); + return data; +} + +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { + return 3.3f; +} diff --git a/atmel-samd/common-hal/analogio/AnalogIn.h b/atmel-samd/common-hal/analogio/AnalogIn.h new file mode 100644 index 000000000..78c7b82e8 --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogIn.h @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__ + +#include "common-hal/microcontroller/types.h" + +// Don't reorder these includes because they are dependencies of adc_feature.h. +// They should really be included by adc_feature.h. +#include <compiler.h> +#include "asf/sam0/drivers/system/clock/gclk.h" +#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h" +#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + struct adc_module * adc_instance; +} analogio_analogin_obj_t; + +void analogin_reset(void); + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__ diff --git a/atmel-samd/common-hal/analogio/AnalogOut.c b/atmel-samd/common-hal/analogio/AnalogOut.c new file mode 100644 index 000000000..a618a6eeb --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogOut.c @@ -0,0 +1,71 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <stdint.h> +#include <string.h> + +#include "py/mperrno.h" +#include "py/runtime.h" + +#include "shared-bindings/analogio/AnalogOut.h" + +#include "asf/sam0/drivers/dac/dac.h" +#include "samd21_pins.h" + +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, + const mcu_pin_obj_t *pin) { + if (pin->pin != PIN_PA02) { + mp_raise_ValueError("AnalogOut not supported on given pin"); + return; + } + struct dac_config config_dac; + dac_get_config_defaults(&config_dac); + config_dac.reference = DAC_REFERENCE_AVCC; + enum status_code status = dac_init(&self->dac_instance, DAC, &config_dac); + if (status != STATUS_OK) { + mp_raise_OSError(MP_EIO); + return; + } + + struct dac_chan_config config_analogout_chan; + dac_chan_get_config_defaults(&config_analogout_chan); + dac_chan_set_config(&self->dac_instance, DAC_CHANNEL_0, &config_analogout_chan); + dac_chan_enable(&self->dac_instance, DAC_CHANNEL_0); + + dac_enable(&self->dac_instance); +} + +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { + dac_disable(&self->dac_instance); + dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0); + reset_pin(PIN_PA02); +} + +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, + uint16_t value) { + // Input is 16 bit but we only support 10 bit so we shift the input. + dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value >> 6); +} diff --git a/atmel-samd/common-hal/analogio/AnalogOut.h b/atmel-samd/common-hal/analogio/AnalogOut.h new file mode 100644 index 000000000..f74592d6a --- /dev/null +++ b/atmel-samd/common-hal/analogio/AnalogOut.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ + +#include "common-hal/microcontroller/types.h" + +#include "asf/sam0/drivers/dac/dac.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + struct dac_module dac_instance; +} analogio_analogout_obj_t; + +#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ diff --git a/atmel-samd/common-hal/analogio/__init__.c b/atmel-samd/common-hal/analogio/__init__.c new file mode 100644 index 000000000..eea58c77d --- /dev/null +++ b/atmel-samd/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. |
