Skip to content

Commit b667736

Browse files
committed
Merge branch 'adafruit:main' into main
2 parents d8b895f + 8c0f358 commit b667736

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

ports/raspberrypi/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ INC += -I. \
106106
-I$(BUILD)
107107

108108
# Pico specific configuration
109-
CFLAGS += -DRASPBERRYPI -DPICO_ON_DEVICE=1 -DPICO_NO_BINARY_INFO=0 -DPICO_TIME_DEFAULT_ALARM_POOL_DISABLED=1 -DPICO_DIVIDER_CALL_IDIV0=0 -DPICO_DIVIDER_CALL_LDIV0=0 -DPICO_DIVIDER_HARDWARE=1 -DPICO_DOUBLE_ROM=1 -DPICO_FLOAT_ROM=1 -DPICO_MULTICORE=1 -DPICO_BITS_IN_RAM=0 -DPICO_DIVIDER_IN_RAM=0 -DPICO_DOUBLE_PROPAGATE_NANS=0 -DPICO_DOUBLE_IN_RAM=0 -DPICO_MEM_IN_RAM=0 -DPICO_FLOAT_IN_RAM=0 -DPICO_FLOAT_PROPAGATE_NANS=1 -DPICO_NO_FLASH=0 -DPICO_COPY_TO_RAM=0 -DPICO_DISABLE_SHARED_IRQ_HANDLERS=0 -DPICO_NO_BI_BOOTSEL_VIA_DOUBLE_RESET=0
109+
CFLAGS += -DRASPBERRYPI -DPICO_ON_DEVICE=1 -DPICO_NO_BINARY_INFO=0 -DPICO_TIME_DEFAULT_ALARM_POOL_DISABLED=0 -DPICO_DIVIDER_CALL_IDIV0=0 -DPICO_DIVIDER_CALL_LDIV0=0 -DPICO_DIVIDER_HARDWARE=1 -DPICO_DOUBLE_ROM=1 -DPICO_FLOAT_ROM=1 -DPICO_MULTICORE=1 -DPICO_BITS_IN_RAM=0 -DPICO_DIVIDER_IN_RAM=0 -DPICO_DOUBLE_PROPAGATE_NANS=0 -DPICO_DOUBLE_IN_RAM=0 -DPICO_MEM_IN_RAM=0 -DPICO_FLOAT_IN_RAM=0 -DPICO_FLOAT_PROPAGATE_NANS=1 -DPICO_NO_FLASH=0 -DPICO_COPY_TO_RAM=0 -DPICO_DISABLE_SHARED_IRQ_HANDLERS=0 -DPICO_NO_BI_BOOTSEL_VIA_DOUBLE_RESET=0
110110
OPTIMIZATION_FLAGS ?= -O3
111111
# TinyUSB defines
112112
CFLAGS += -DTUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX=1 -DCFG_TUSB_MCU=OPT_MCU_RP2040 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=1024

ports/raspberrypi/common-hal/pulseio/PulseOut.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,38 @@
2727
#include "common-hal/pulseio/PulseOut.h"
2828

2929
#include <stdint.h>
30-
31-
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
32-
3330
#include "mpconfigport.h"
34-
#include "py/gc.h"
3531
#include "py/runtime.h"
3632
#include "shared-bindings/pulseio/PulseOut.h"
33+
#include "shared-bindings/pwmio/PWMOut.h"
34+
#include "common-hal/pwmio/PWMOut.h"
3735
#include "supervisor/shared/translate.h"
36+
#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h"
37+
#include "src/common/pico_time/include/pico/time.h"
3838

3939
static uint8_t refcount = 0;
40-
41-
4240
static uint16_t *pulse_buffer = NULL;
4341
static volatile uint16_t pulse_index = 0;
4442
static uint16_t pulse_length;
45-
static volatile uint32_t current_compare = 0;
43+
pwmio_pwmout_obj_t *pwmout_obj;
44+
volatile uint16_t current_duty_cycle;
4645

4746
void pulse_finish(void) {
4847
pulse_index++;
49-
50-
// Always turn it off.
48+
// Turn pwm pin off by setting duty cyle to 1.
49+
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,1);
5150
if (pulse_index >= pulse_length) {
5251
return;
5352
}
54-
current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff;
53+
add_alarm_in_us(pulse_buffer[pulse_index], pulseout_interrupt_handler, NULL, false);
54+
if (pulse_index % 2 == 0) {
55+
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,current_duty_cycle);
56+
}
57+
}
58+
59+
int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data) {
60+
pulse_finish();
61+
return 0;
5562
}
5663

5764
void pulseout_reset() {
@@ -63,12 +70,13 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
6370
const mcu_pin_obj_t *pin,
6471
uint32_t frequency,
6572
uint16_t duty_cycle) {
66-
mp_raise_NotImplementedError(translate("Unsupported operation"));
6773

6874
refcount++;
69-
75+
pwmout_obj = (pwmio_pwmout_obj_t *)carrier;
76+
current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle(pwmout_obj);
7077
self->pin = carrier->pin->number;
71-
78+
self->slice = carrier->slice;
79+
pwm_set_enabled(pwmout_obj->slice,false);
7280
}
7381

7482
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
@@ -79,8 +87,6 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
7987
if (common_hal_pulseio_pulseout_deinited(self)) {
8088
return;
8189
}
82-
83-
8490
refcount--;
8591
self->pin = NO_PIN;
8692
}
@@ -90,6 +96,14 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
9096
pulse_index = 0;
9197
pulse_length = length;
9298

93-
current_compare = pulses[0] * 3 / 4;
99+
add_alarm_in_us(pulses[0], pulseout_interrupt_handler, NULL, false);
100+
common_hal_pwmio_pwmout_set_duty_cycle(pwmout_obj,current_duty_cycle);
101+
pwm_set_enabled(pwmout_obj->slice,true);
94102

103+
while (pulse_index < length) {
104+
// Do other things while we wait. The interrupts will handle sending the
105+
// signal.
106+
RUN_BACKGROUND_TASKS;
107+
}
108+
pwm_set_enabled(pwmout_obj->slice,false);
95109
}

ports/raspberrypi/common-hal/pulseio/PulseOut.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "src/common/pico_time/include/pico/time.h"
3132

3233
#include "py/obj.h"
3334

@@ -36,9 +37,10 @@
3637
typedef struct {
3738
mp_obj_base_t base;
3839
uint8_t pin;
40+
uint8_t slice;
3941
} pulseio_pulseout_obj_t;
4042

4143
void pulseout_reset(void);
42-
void pulseout_interrupt_handler(uint8_t index);
44+
int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data);
4345

4446
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H

0 commit comments

Comments
 (0)