aboutsummaryrefslogtreecommitdiff
path: root/atmel-samd
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-03-10 19:17:54 +0100
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-03-10 19:17:54 +0100
commitd200a621647aa9fc05aae29ad7bc38363025438b (patch)
tree1216f83d9f4263f012fb063f44975ee77b58f078 /atmel-samd
parent07ea2abcb09aef3b933386669be4802f8c0aba1d (diff)
Add PulseOut which can pulse a PWMOut for IR remote transmission.
Diffstat (limited to 'atmel-samd')
-rw-r--r--atmel-samd/Makefile1
-rw-r--r--atmel-samd/common-hal/nativeio/PulseOut.c163
-rw-r--r--atmel-samd/common-hal/nativeio/PulseOut.h32
-rw-r--r--atmel-samd/common-hal/nativeio/types.h6
-rw-r--r--atmel-samd/main.c3
5 files changed, 205 insertions, 0 deletions
diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile
index 35db9b69d..5db6734f0 100644
--- a/atmel-samd/Makefile
+++ b/atmel-samd/Makefile
@@ -224,6 +224,7 @@ SRC_BINDINGS = \
nativeio/AnalogOut.c \
nativeio/DigitalInOut.c \
nativeio/I2C.c \
+ nativeio/PulseOut.c \
nativeio/PWMOut.c \
nativeio/SPI.c \
nativeio/UART.c \
diff --git a/atmel-samd/common-hal/nativeio/PulseOut.c b/atmel-samd/common-hal/nativeio/PulseOut.c
new file mode 100644
index 000000000..76ef849a1
--- /dev/null
+++ b/atmel-samd/common-hal/nativeio/PulseOut.c
@@ -0,0 +1,163 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 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 "common-hal/nativeio/PulseOut.h"
+
+#include <stdint.h>
+
+#include "asf/sam0/drivers/tc/tc_interrupt.h"
+
+#include "mpconfigport.h"
+#include "py/runtime.h"
+#include "shared-bindings/nativeio/PulseOut.h"
+
+#undef ENABLE
+
+// This timer is shared amongst all PulseOut objects under the assumption that
+// the code is single threaded.
+static struct tc_module tc_instance;
+static uint8_t refcount = 0;
+
+static __IO PORT_PINCFG_Type *active_pincfg = NULL;
+static uint16_t *pulse_buffer = NULL;
+static volatile uint16_t pulse_index = 0;
+static uint16_t pulse_length;
+static volatile uint32_t current_compare = 0;
+
+static void turn_on(__IO PORT_PINCFG_Type * pincfg) {
+ pincfg->reg = PORT_PINCFG_PMUXEN;
+}
+
+static void turn_off(__IO PORT_PINCFG_Type * pincfg) {
+ pincfg->reg = PORT_PINCFG_RESETVALUE;
+}
+
+void pulse_finish(struct tc_module *const module) {
+ pulse_index++;
+
+ if (active_pincfg == NULL) {
+ return;
+ }
+ // Always turn it off.
+ turn_off(active_pincfg);
+ if (pulse_index >= pulse_length) {
+ return;
+ }
+ current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff;
+ tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
+ if (pulse_index % 2 == 0) {
+ turn_on(active_pincfg);
+ }
+}
+
+void pulseout_reset() {
+ refcount = 0;
+ active_pincfg = NULL;
+}
+
+void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
+ const nativeio_pwmout_obj_t* carrier) {
+ if (refcount == 0) {
+ // Find a spare timer.
+ Tc *t = NULL;
+ Tc *tcs[TC_INST_NUM] = TC_INSTS;
+ for (uint8_t i = TC_INST_NUM; i > 0; i--) {
+ if (tcs[i - 1]->COUNT16.CTRLA.bit.ENABLE == 0) {
+ t = tcs[i - 1];
+ break;
+ }
+ }
+ if (t == NULL) {
+ mp_raise_RuntimeError("All timers in use");
+ }
+
+ struct tc_config config_tc;
+ tc_get_config_defaults(&config_tc);
+
+ config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
+ config_tc.clock_prescaler = TC_CTRLA_PRESCALER_DIV64;
+ config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ;
+
+ tc_init(&tc_instance, t, &config_tc);
+ tc_register_callback(&tc_instance, pulse_finish, TC_CALLBACK_CC_CHANNEL0);
+ tc_enable(&tc_instance);
+ tc_stop_counter(&tc_instance);
+ }
+ refcount++;
+
+ self->pin = carrier->pin->pin;
+
+ PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
+ self->pincfg = &port_base->PINCFG[self->pin % 32];
+
+ // Set the port to output a zero.
+ port_base->OUTCLR.reg = 1 << (self->pin % 32);
+ port_base->DIRSET.reg = 1 << (self->pin % 32);
+
+ // Turn off the pinmux which should connect the port output.
+ turn_off(self->pincfg);
+}
+
+void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
+ PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
+ port_base->DIRCLR.reg = 1 << (self->pin % 32);
+
+ turn_on(self->pincfg);
+
+ refcount--;
+ if (refcount == 0) {
+ tc_reset(&tc_instance);
+ }
+}
+
+void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
+ if (active_pincfg != NULL) {
+ mp_raise_RuntimeError("Another send is already active");
+ }
+ active_pincfg = self->pincfg;
+ pulse_buffer = pulses;
+ pulse_index = 0;
+ pulse_length = length;
+
+ current_compare = pulses[0] * 3 / 4;
+ tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
+
+ tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
+ turn_on(active_pincfg);
+ tc_start_counter(&tc_instance);
+
+ while(pulse_index < length) {
+ // Do other things while we wait. The interrupts will handle sending the
+ // signal.
+ #ifdef MICROPY_VM_HOOK_LOOP
+ MICROPY_VM_HOOK_LOOP
+ #endif
+ }
+
+ tc_stop_counter(&tc_instance);
+ tc_disable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
+ active_pincfg = NULL;
+}
diff --git a/atmel-samd/common-hal/nativeio/PulseOut.h b/atmel-samd/common-hal/nativeio/PulseOut.h
new file mode 100644
index 000000000..721670bef
--- /dev/null
+++ b/atmel-samd/common-hal/nativeio/PulseOut.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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.
+ */
+
+#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
+#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
+
+void pulseout_reset(void);
+
+#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
diff --git a/atmel-samd/common-hal/nativeio/types.h b/atmel-samd/common-hal/nativeio/types.h
index c039ed293..ab999cfc6 100644
--- a/atmel-samd/common-hal/nativeio/types.h
+++ b/atmel-samd/common-hal/nativeio/types.h
@@ -101,6 +101,12 @@ typedef struct {
typedef struct {
mp_obj_base_t base;
+ __IO PORT_PINCFG_Type *pincfg;
+ uint8_t pin;
+} nativeio_pulseout_obj_t;
+
+typedef struct {
+ mp_obj_base_t base;
// Only support TouchIn when external SPI flash is used.
#ifdef SPI_FLASH_SECTOR_SIZE
const mcu_pin_obj_t * pin;
diff --git a/atmel-samd/main.c b/atmel-samd/main.c
index ad124b58a..47edc2537 100644
--- a/atmel-samd/main.c
+++ b/atmel-samd/main.c
@@ -25,6 +25,7 @@
#include <board.h>
#include "common-hal/nativeio/AnalogIn.h"
+#include "common-hal/nativeio/PulseOut.h"
#include "common-hal/nativeio/PWMOut.h"
#include "common-hal/usb_hid/__init__.h"
@@ -170,6 +171,8 @@ void reset_samd21(void) {
analogin_reset();
+ pulseout_reset();
+
// Wait for the DAC to sync.
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {}
DAC->CTRLA.reg |= DAC_CTRLA_SWRST;