Skip to content

Commit d200a62

Browse files
committed
Add PulseOut which can pulse a PWMOut for IR remote transmission.
1 parent 07ea2ab commit d200a62

File tree

11 files changed

+447
-0
lines changed

11 files changed

+447
-0
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ SRC_BINDINGS = \
224224
nativeio/AnalogOut.c \
225225
nativeio/DigitalInOut.c \
226226
nativeio/I2C.c \
227+
nativeio/PulseOut.c \
227228
nativeio/PWMOut.c \
228229
nativeio/SPI.c \
229230
nativeio/UART.c \
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "common-hal/nativeio/PulseOut.h"
28+
29+
#include <stdint.h>
30+
31+
#include "asf/sam0/drivers/tc/tc_interrupt.h"
32+
33+
#include "mpconfigport.h"
34+
#include "py/runtime.h"
35+
#include "shared-bindings/nativeio/PulseOut.h"
36+
37+
#undef ENABLE
38+
39+
// This timer is shared amongst all PulseOut objects under the assumption that
40+
// the code is single threaded.
41+
static struct tc_module tc_instance;
42+
static uint8_t refcount = 0;
43+
44+
static __IO PORT_PINCFG_Type *active_pincfg = NULL;
45+
static uint16_t *pulse_buffer = NULL;
46+
static volatile uint16_t pulse_index = 0;
47+
static uint16_t pulse_length;
48+
static volatile uint32_t current_compare = 0;
49+
50+
static void turn_on(__IO PORT_PINCFG_Type * pincfg) {
51+
pincfg->reg = PORT_PINCFG_PMUXEN;
52+
}
53+
54+
static void turn_off(__IO PORT_PINCFG_Type * pincfg) {
55+
pincfg->reg = PORT_PINCFG_RESETVALUE;
56+
}
57+
58+
void pulse_finish(struct tc_module *const module) {
59+
pulse_index++;
60+
61+
if (active_pincfg == NULL) {
62+
return;
63+
}
64+
// Always turn it off.
65+
turn_off(active_pincfg);
66+
if (pulse_index >= pulse_length) {
67+
return;
68+
}
69+
current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff;
70+
tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
71+
if (pulse_index % 2 == 0) {
72+
turn_on(active_pincfg);
73+
}
74+
}
75+
76+
void pulseout_reset() {
77+
refcount = 0;
78+
active_pincfg = NULL;
79+
}
80+
81+
void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
82+
const nativeio_pwmout_obj_t* carrier) {
83+
if (refcount == 0) {
84+
// Find a spare timer.
85+
Tc *t = NULL;
86+
Tc *tcs[TC_INST_NUM] = TC_INSTS;
87+
for (uint8_t i = TC_INST_NUM; i > 0; i--) {
88+
if (tcs[i - 1]->COUNT16.CTRLA.bit.ENABLE == 0) {
89+
t = tcs[i - 1];
90+
break;
91+
}
92+
}
93+
if (t == NULL) {
94+
mp_raise_RuntimeError("All timers in use");
95+
}
96+
97+
struct tc_config config_tc;
98+
tc_get_config_defaults(&config_tc);
99+
100+
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
101+
config_tc.clock_prescaler = TC_CTRLA_PRESCALER_DIV64;
102+
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ;
103+
104+
tc_init(&tc_instance, t, &config_tc);
105+
tc_register_callback(&tc_instance, pulse_finish, TC_CALLBACK_CC_CHANNEL0);
106+
tc_enable(&tc_instance);
107+
tc_stop_counter(&tc_instance);
108+
}
109+
refcount++;
110+
111+
self->pin = carrier->pin->pin;
112+
113+
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
114+
self->pincfg = &port_base->PINCFG[self->pin % 32];
115+
116+
// Set the port to output a zero.
117+
port_base->OUTCLR.reg = 1 << (self->pin % 32);
118+
port_base->DIRSET.reg = 1 << (self->pin % 32);
119+
120+
// Turn off the pinmux which should connect the port output.
121+
turn_off(self->pincfg);
122+
}
123+
124+
void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
125+
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
126+
port_base->DIRCLR.reg = 1 << (self->pin % 32);
127+
128+
turn_on(self->pincfg);
129+
130+
refcount--;
131+
if (refcount == 0) {
132+
tc_reset(&tc_instance);
133+
}
134+
}
135+
136+
void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
137+
if (active_pincfg != NULL) {
138+
mp_raise_RuntimeError("Another send is already active");
139+
}
140+
active_pincfg = self->pincfg;
141+
pulse_buffer = pulses;
142+
pulse_index = 0;
143+
pulse_length = length;
144+
145+
current_compare = pulses[0] * 3 / 4;
146+
tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
147+
148+
tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
149+
turn_on(active_pincfg);
150+
tc_start_counter(&tc_instance);
151+
152+
while(pulse_index < length) {
153+
// Do other things while we wait. The interrupts will handle sending the
154+
// signal.
155+
#ifdef MICROPY_VM_HOOK_LOOP
156+
MICROPY_VM_HOOK_LOOP
157+
#endif
158+
}
159+
160+
tc_stop_counter(&tc_instance);
161+
tc_disable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0);
162+
active_pincfg = NULL;
163+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
29+
30+
void pulseout_reset(void);
31+
32+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__

atmel-samd/common-hal/nativeio/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ typedef struct {
9999
};
100100
} nativeio_pwmout_obj_t;
101101

102+
typedef struct {
103+
mp_obj_base_t base;
104+
__IO PORT_PINCFG_Type *pincfg;
105+
uint8_t pin;
106+
} nativeio_pulseout_obj_t;
107+
102108
typedef struct {
103109
mp_obj_base_t base;
104110
// Only support TouchIn when external SPI flash is used.

atmel-samd/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <board.h>
2626

2727
#include "common-hal/nativeio/AnalogIn.h"
28+
#include "common-hal/nativeio/PulseOut.h"
2829
#include "common-hal/nativeio/PWMOut.h"
2930
#include "common-hal/usb_hid/__init__.h"
3031

@@ -170,6 +171,8 @@ void reset_samd21(void) {
170171

171172
analogin_reset();
172173

174+
pulseout_reset();
175+
173176
// Wait for the DAC to sync.
174177
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {}
175178
DAC->CTRLA.reg |= DAC_CTRLA_SWRST;

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ SRC_COMMON_HAL = \
114114
nativeio/AnalogOut.c \
115115
nativeio/DigitalInOut.c \
116116
nativeio/I2C.c \
117+
nativeio/PulseOut.c \
117118
nativeio/PWMOut.c \
118119
nativeio/SPI.c \
119120
nativeio/TouchIn.c \
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#include <stdint.h>
29+
30+
#include "py/runtime.h"
31+
#include "shared-bindings/nativeio/PulseOut.h"
32+
33+
void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
34+
const nativeio_pwmout_obj_t* carrier) {
35+
36+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut."));
37+
}
38+
39+
void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
40+
}
41+
42+
void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
43+
}

esp8266/common-hal/nativeio/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ typedef struct {
6161
bool locked;
6262
} nativeio_spi_obj_t;
6363

64+
typedef struct {
65+
mp_obj_base_t base;
66+
} nativeio_pulseout_obj_t;
67+
6468
typedef struct {
6569
mp_obj_base_t base;
6670
int channel;

0 commit comments

Comments
 (0)