27
27
#include "common-hal/pulseio/PulseOut.h"
28
28
29
29
#include <stdint.h>
30
-
31
- #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
32
-
33
30
#include "mpconfigport.h"
34
- #include "py/gc.h"
35
31
#include "py/runtime.h"
36
32
#include "shared-bindings/pulseio/PulseOut.h"
33
+ #include "shared-bindings/pwmio/PWMOut.h"
34
+ #include "common-hal/pwmio/PWMOut.h"
37
35
#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"
38
38
39
39
static uint8_t refcount = 0 ;
40
-
41
-
42
40
static uint16_t * pulse_buffer = NULL ;
43
41
static volatile uint16_t pulse_index = 0 ;
44
42
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 ;
46
45
47
46
void pulse_finish (void ) {
48
47
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 );
51
50
if (pulse_index >= pulse_length ) {
52
51
return ;
53
52
}
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 ;
55
62
}
56
63
57
64
void pulseout_reset () {
@@ -63,12 +70,13 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
63
70
const mcu_pin_obj_t * pin ,
64
71
uint32_t frequency ,
65
72
uint16_t duty_cycle ) {
66
- mp_raise_NotImplementedError (translate ("Unsupported operation" ));
67
73
68
74
refcount ++ ;
69
-
75
+ pwmout_obj = (pwmio_pwmout_obj_t * )carrier ;
76
+ current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle (pwmout_obj );
70
77
self -> pin = carrier -> pin -> number ;
71
-
78
+ self -> slice = carrier -> slice ;
79
+ pwm_set_enabled (pwmout_obj -> slice ,false);
72
80
}
73
81
74
82
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) {
79
87
if (common_hal_pulseio_pulseout_deinited (self )) {
80
88
return ;
81
89
}
82
-
83
-
84
90
refcount -- ;
85
91
self -> pin = NO_PIN ;
86
92
}
@@ -90,6 +96,14 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
90
96
pulse_index = 0 ;
91
97
pulse_length = length ;
92
98
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);
94
102
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);
95
109
}
0 commit comments