39
39
#define NO_PIN 0xff
40
40
#define MAX_PULSE 65535
41
41
#define MIN_PULSE 10
42
- volatile bool last_level ;
43
- volatile uint32_t level_count = 0 ;
44
- volatile uint32_t result = 0 ;
45
- volatile uint16_t buf_index = 0 ;
46
42
47
43
uint16_t pulsein_program [] = {
48
44
0x4001 , // 1: in pins, 1
@@ -77,12 +73,15 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
77
73
true, 32 , true, // RX auto-push every 32 bits
78
74
false); // claim pins
79
75
76
+ if (!ok ) {
77
+ mp_raise_RuntimeError (translate ("All state machines in use" ));
78
+ }
79
+
80
80
pio_sm_set_enabled (self -> state_machine .pio ,self -> state_machine .state_machine , false);
81
81
pio_sm_clear_fifos (self -> state_machine .pio ,self -> state_machine .state_machine );
82
- last_level = self -> idle_state ;
83
- level_count = 0 ;
84
- result = 0 ;
85
- buf_index = 0 ;
82
+ self -> last_level = self -> idle_state ;
83
+ self -> level_count = 0 ;
84
+ self -> buf_index = 0 ;
86
85
87
86
pio_sm_set_in_pins (self -> state_machine .pio ,self -> state_machine .state_machine ,pin -> number );
88
87
common_hal_rp2pio_statemachine_set_interrupt_handler (& (self -> state_machine ),& common_hal_pulseio_pulsein_interrupt ,self ,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS );
@@ -116,47 +115,50 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
116
115
void common_hal_pulseio_pulsein_pause (pulseio_pulsein_obj_t * self ) {
117
116
pio_sm_restart (self -> state_machine .pio , self -> state_machine .state_machine );
118
117
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , false);
119
- last_level = self -> idle_state ;
120
- level_count = 0 ;
121
- result = 0 ;
122
- buf_index = 0 ;
118
+ self -> last_level = self -> idle_state ;
119
+ self -> level_count = 0 ;
120
+ self -> buf_index = 0 ;
123
121
}
124
122
void common_hal_pulseio_pulsein_interrupt (pulseio_pulsein_obj_t * self ) {
125
123
126
124
uint32_t rxfifo = 0 ;
127
125
128
126
rxfifo = pio_sm_get_blocking (self -> state_machine .pio , self -> state_machine .state_machine );
129
127
// translate from fifo to buffer
130
- for (uint i = 0 ; i < 32 ; i ++ ) {
131
- bool level = (rxfifo & (1 << i )) >> i ;
132
- if (level == last_level ) {
133
- level_count ++ ;
134
- } else {
135
- result = level_count ;
136
- last_level = level ;
137
- level_count = 0 ;
138
- // Pulses that are longer than MAX_PULSE will return MAX_PULSE
139
- if (result > MAX_PULSE ) {
140
- result = MAX_PULSE ;
141
- }
142
- // return pulses that are not too short
143
- if (result > MIN_PULSE ) {
144
- self -> buffer [buf_index ] = (uint16_t )result ;
145
- if (self -> len < self -> maxlen ) {
146
- self -> len ++ ;
128
+ if ((rxfifo == 0 && self -> last_level == false) || (rxfifo == 0xffffffff && self -> last_level == true)) {
129
+ self -> level_count = self -> level_count + 32 ;
130
+ } else {
131
+ for (uint i = 0 ; i < 32 ; i ++ ) {
132
+ bool level = (rxfifo & (1 << i )) >> i ;
133
+ if (level == self -> last_level ) {
134
+ self -> level_count ++ ;
135
+ } else {
136
+ uint32_t result = self -> level_count ;
137
+ self -> last_level = level ;
138
+ self -> level_count = 0 ;
139
+ // Pulses that are longer than MAX_PULSE will return MAX_PULSE
140
+ if (result > MAX_PULSE ) {
141
+ result = MAX_PULSE ;
147
142
}
148
- if (buf_index < self -> maxlen ) {
149
- buf_index ++ ;
150
- } else {
151
- self -> start = 0 ;
152
- buf_index = 0 ;
143
+ // return pulses that are not too short
144
+ if (result > MIN_PULSE ) {
145
+ self -> buffer [self -> buf_index ] = (uint16_t )result ;
146
+ if (self -> len < self -> maxlen ) {
147
+ self -> len ++ ;
148
+ }
149
+ if (self -> buf_index < self -> maxlen ) {
150
+ self -> buf_index ++ ;
151
+ } else {
152
+ self -> start = 0 ;
153
+ self -> buf_index = 0 ;
154
+ }
153
155
}
154
156
}
155
157
}
156
158
}
157
159
158
160
// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
159
- if ((level_count > MAX_PULSE ) || (buf_index >= self -> maxlen )) {
161
+ if ((self -> level_count > MAX_PULSE ) || (self -> buf_index >= self -> maxlen )) {
160
162
pio_sm_set_enabled (self -> state_machine .pio , self -> state_machine .state_machine , false);
161
163
pio_sm_init (self -> state_machine .pio , self -> state_machine .state_machine , self -> state_machine .offset , & self -> state_machine .sm_config );
162
164
pio_sm_restart (self -> state_machine .pio ,self -> state_machine .state_machine );
@@ -189,7 +191,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
189
191
void common_hal_pulseio_pulsein_clear (pulseio_pulsein_obj_t * self ) {
190
192
self -> start = 0 ;
191
193
self -> len = 0 ;
192
- buf_index = 0 ;
194
+ self -> buf_index = 0 ;
193
195
}
194
196
195
197
uint16_t common_hal_pulseio_pulsein_popleft (pulseio_pulsein_obj_t * self ) {
@@ -202,8 +204,8 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
202
204
// if we are empty reset buffer pointer and counters
203
205
if (self -> len == 0 ) {
204
206
self -> start = 0 ;
205
- buf_index = 0 ;
206
- level_count = 0 ;
207
+ self -> buf_index = 0 ;
208
+ self -> level_count = 0 ;
207
209
}
208
210
return value ;
209
211
}
0 commit comments