Add PulseOut which can pulse a PWMOut for IR remote transmission.

This commit is contained in:
Scott Shawcroft 2017-03-10 19:17:54 +01:00
parent 07ea2abcb0
commit d200a62164
11 changed files with 447 additions and 0 deletions

View File

@ -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 \

View File

@ -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;
}

View File

@ -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__

View File

@ -99,6 +99,12 @@ typedef struct {
};
} nativeio_pwmout_obj_t;
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.

View File

@ -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;

View File

@ -114,6 +114,7 @@ SRC_COMMON_HAL = \
nativeio/AnalogOut.c \
nativeio/DigitalInOut.c \
nativeio/I2C.c \
nativeio/PulseOut.c \
nativeio/PWMOut.c \
nativeio/SPI.c \
nativeio/TouchIn.c \

View File

@ -0,0 +1,43 @@
/*
* This file is part of the Micro Python 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.
*/
#include <stdint.h>
#include "py/runtime.h"
#include "shared-bindings/nativeio/PulseOut.h"
void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
const nativeio_pwmout_obj_t* carrier) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut."));
}
void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
}
void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
}

View File

@ -61,6 +61,10 @@ typedef struct {
bool locked;
} nativeio_spi_obj_t;
typedef struct {
mp_obj_base_t base;
} nativeio_pulseout_obj_t;
typedef struct {
mp_obj_base_t base;
int channel;

View File

@ -0,0 +1,150 @@
/*
* 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 <stdint.h>
#include "lib/utils/context_manager_helpers.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/PulseOut.h"
#include "shared-bindings/nativeio/PWMOut.h"
//| .. currentmodule:: nativeio
//|
//| :class:`PulseOut` -- Output a pulse train
//| ========================================================
//|
//| PulseOut is used to pulse PWM "carrier" output on and off. This is commonly
//| used in infrared remotes. The pulsed signal consists of timed on and off
//| periods. Unlike PWM, there is no set duration for on and off pairs.
//|
//| .. class:: PulseOut(carrier)
//|
//| Create a PulseOut object associated with the given PWM out experience.
//|
//| :param ~nativeio.PWMOut carrier: PWMOut that is set to output on the desired pin.
//|
//| Send a short series of pulses::
//|
//| import array
//| import nativeio
//| import board
//|
//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm:
//| pulse = nativeio.PulseOut(pwm)
//| # on off on off on
//| pulses = array.array('h', [65000, 1000, 65000, 65000, 1000])
//| pulse.send(pulses)
//|
//| # Modify the array of pulses.
//| pulses[0] = 200
//| pulse.send(pulses)
//|
STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, true);
mp_obj_t carrier_obj = args[0];
if (!MP_OBJ_IS_TYPE(carrier_obj, &nativeio_pwmout_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_pwmout_type.name));
}
// create Pulse object from the given pin
nativeio_pulseout_obj_t *self = m_new_obj(nativeio_pulseout_obj_t);
self->base.type = &nativeio_pulseout_type;
common_hal_nativeio_pulseout_construct(self, (nativeio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj));
return MP_OBJ_FROM_PTR(self);
}
//| .. method:: deinit()
//|
//| Deinitialises the PulseOut and releases any hardware resources for reuse.
//|
STATIC mp_obj_t nativeio_pulseout_deinit(mp_obj_t self_in) {
nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_pulseout_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout_deinit);
//| .. method:: __enter__()
//|
//| No-op used by Context Managers.
//|
// Provided by context manager helper.
//| .. method:: __exit__()
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_pulseout_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, nativeio_pulseout_obj___exit__);
//| .. method:: send(pulses)
//|
//| Pulse alternating on and off durations in microseconds starting with on.
//| ``pulses`` must be an `array.array` with data type 'H' for unsigned
//| halfword (two bytes).
//|
//| This method waits until the whole array of pulses has been sent and
//| ensures the signal is off afterwards.
//|
//| :param array.array pulses: pulse durations in microseconds
//|
STATIC mp_obj_t nativeio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) {
nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ);
if (bufinfo.typecode != 'H') {
mp_raise_TypeError("Array must contain halfwords (type 'H')");
}
common_hal_nativeio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulseout_send_obj, nativeio_pulseout_obj_send);
STATIC const mp_rom_map_elem_t nativeio_pulseout_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulseout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulseout___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&nativeio_pulseout_send_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_pulseout_locals_dict, nativeio_pulseout_locals_dict_table);
const mp_obj_type_t nativeio_pulseout_type = {
{ &mp_type_type },
.name = MP_QSTR_PulseOut,
.make_new = nativeio_pulseout_make_new,
.locals_dict = (mp_obj_dict_t*)&nativeio_pulseout_locals_dict,
};

View File

@ -0,0 +1,41 @@
/*
* 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.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
extern const mp_obj_type_t nativeio_pulseout_type;
extern void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
const nativeio_pwmout_obj_t* carrier);
extern void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self);
extern void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self,
uint16_t* pulses, uint16_t len);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__

View File

@ -39,6 +39,7 @@
#include "shared-bindings/nativeio/AnalogOut.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/nativeio/I2C.h"
#include "shared-bindings/nativeio/PulseOut.h"
#include "shared-bindings/nativeio/PWMOut.h"
#include "shared-bindings/nativeio/SPI.h"
#include "shared-bindings/nativeio/TouchIn.h"
@ -74,6 +75,7 @@
//| AnalogOut
//| DigitalInOut
//| I2C
//| PulseOut
//| PWMOut
//| SPI
//| TouchIn
@ -116,6 +118,7 @@ STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) },
{ MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) },
{ MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) },