@@ -46,13 +46,12 @@ volatile uint16_t result = 0;
46
46
volatile uint16_t buf_index = 0 ;
47
47
48
48
uint16_t pulsein_program [] = {
49
- 0x2020 , // 0: wait 0 pin, 0
50
- 0xe03f , // 1: set x, 31
51
- 0x4001 , // 2: in pins, 1
52
- 0x0042 , // 3: jmp x--, 2
53
- 0x8060 , // 4: push iffull block
54
- 0xc020 , // 5: irq wait 0
55
- 0x0001 , // 6: jmp 1
49
+ 0xe03f , // 0: set x, 31
50
+ 0x4001 , // 1: in pins, 1
51
+ 0x0041 , // 2: jmp x--, 2
52
+ 0x8060 , // 3: push iffull block
53
+ 0xc020 , // 4: irq wait 0
54
+ 0x0000 , // 5: jmp 1
56
55
};
57
56
58
57
void common_hal_pulseio_pulsein_construct (pulseio_pulsein_obj_t * self ,
@@ -68,10 +67,6 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
68
67
self -> start = 0 ;
69
68
self -> len = 0 ;
70
69
save_self = self ;
71
- // change initial state machine wait if idle_state is false
72
- if (idle_state == false) {
73
- pulsein_program [0 ] = 0x20a0 ;
74
- }
75
70
76
71
// Set everything up.
77
72
rp2pio_statemachine_obj_t state_machine ;
@@ -91,6 +86,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
91
86
false,
92
87
true, 32 , true, // RX auto-push every 32 bits
93
88
false); // claim pins
89
+ pio_sm_set_enabled (state_machine .pio ,state_machine .state_machine , false);
94
90
self -> state_machine .pio = state_machine .pio ;
95
91
self -> state_machine .state_machine = state_machine .state_machine ;
96
92
self -> state_machine .sm_config = state_machine .sm_config ;
@@ -107,15 +103,20 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
107
103
buf_index = 0 ;
108
104
109
105
pio_sm_set_in_pins (state_machine .pio ,state_machine .state_machine ,pin -> number );
110
- pio_sm_set_enabled (state_machine .pio ,state_machine .state_machine , false);
111
106
irq_set_exclusive_handler (self -> pio_interrupt , common_hal_pulseio_pulsein_interrupt );
112
107
hw_clear_bits (& state_machine .pio -> inte0 , 1u << state_machine .state_machine );
113
108
hw_set_bits (& state_machine .pio -> inte0 , 1u << (state_machine .state_machine + 8 ));
114
109
115
110
// exec a set pindirs to 0 for input
116
111
pio_sm_exec (state_machine .pio ,state_machine .state_machine ,0xe080 );
117
- irq_set_enabled (self -> pio_interrupt , true);
112
+ //exec the appropriate wait for pin
113
+ if (self -> idle_state == true ) {
114
+ pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
115
+ } else {
116
+ pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
117
+ }
118
118
pio_sm_set_enabled (state_machine .pio , state_machine .state_machine , true);
119
+ irq_set_enabled (self -> pio_interrupt , true);
119
120
}
120
121
121
122
bool common_hal_pulseio_pulsein_deinited (pulseio_pulsein_obj_t * self ) {
@@ -177,9 +178,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
177
178
uint16_t trigger_duration ) {
178
179
// exec a wait for the selected pin to change state
179
180
if (self -> idle_state == true ) {
180
- pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
181
- } else {
182
181
pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x2020 );
182
+ } else {
183
+ pio_sm_exec (self -> state_machine .pio ,self -> state_machine .state_machine ,0x20a0 );
183
184
}
184
185
// Send the trigger pulse.
185
186
if (trigger_duration > 0 ) {
@@ -191,7 +192,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
191
192
}
192
193
193
194
// Reconfigure the pin for PIO
194
- common_hal_mcu_delay_us (100 );
195
+ common_hal_mcu_delay_us (200 );
195
196
gpio_set_function (self -> pin , GPIO_FUNC_PIO0 );
196
197
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , true);
197
198
}
0 commit comments