Skip to content

Commit 2d941b0

Browse files
authored
Changed interrupt to per-word basis; cleaned up other small items
1 parent 88353f2 commit 2d941b0

File tree

2 files changed

+68
-60
lines changed

2 files changed

+68
-60
lines changed

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

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2021 Dave Putz for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -29,9 +29,6 @@
2929

3030
#include <stdint.h>
3131

32-
#include "background.h"
33-
#include "mpconfigport.h"
34-
#include "py/gc.h"
3532
#include "py/runtime.h"
3633
#include "shared-bindings/microcontroller/__init__.h"
3734
#include "shared-bindings/pulseio/PulseIn.h"
@@ -43,17 +40,19 @@
4340
pulseio_pulsein_obj_t* save_self;
4441

4542
#define NO_PIN 0xff
46-
47-
const uint16_t pulsein_program[] = {
48-
// wait 0 pin, 0 ; Wait for first low to start
49-
0x2020,
50-
// irq wait 0 ; set IRQ 0 and wait
51-
0xc020,
52-
// .bitloop
53-
// in pins, 1 [1] ; sample every 3 cycles (2 instructions, 1 delay)
54-
0x4101,
55-
// jmp bitloop
56-
0x0002,
43+
volatile bool last_level;
44+
volatile uint16_t level_count = 0;
45+
volatile uint16_t result = 0;
46+
volatile uint16_t buf_index = 0;
47+
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
5756
};
5857

5958
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
@@ -69,19 +68,23 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
6968
self->start = 0;
7069
self->len = 0;
7170
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+
}
7275

7376
// Set everything up.
7477
rp2pio_statemachine_obj_t state_machine;
7578

7679
bool ok = rp2pio_statemachine_construct(&state_machine,
7780
pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]),
78-
125000 * 4,
81+
1000000 * 3,
7982
NULL, 0,
8083
NULL, 0,
8184
pin, 1,
8285
NULL, 0,
8386
NULL, 0,
84-
1, self->pin,
87+
1, 0,
8588
1 << self->pin, false, true,
8689
false, 8, false, // TX, unused
8790
false,
@@ -96,17 +99,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
9699
} else {
97100
self->pio_interrupt = PIO1_IRQ_0;
98101
}
102+
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
103+
last_level = self->idle_state;
104+
level_count = 0;
105+
result = 0;
106+
buf_index = 0;
99107

100-
pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number);
101-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
108+
pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number);
109+
pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false);
102110
irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt);
103-
irq_set_enabled(self->pio_interrupt, true);
104-
hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine);
105-
hw_set_bits(&self->state_machine.pio->inte0, 1u << (self->state_machine.state_machine+8));
106-
// exec a set pindirs to 0 for input
107-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080);
108-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
111+
hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine);
112+
hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8));
109113

114+
// exec a set pindirs to 0 for input
115+
pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080);
116+
irq_set_enabled(self->pio_interrupt, true);
117+
pio_sm_set_enabled(state_machine.pio, state_machine.state_machine, true);
110118
}
111119

112120
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
@@ -131,48 +139,48 @@ void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
131139
void common_hal_pulseio_pulsein_interrupt() {
132140

133141
pulseio_pulsein_obj_t* self = save_self;
134-
// clear interrupt
135-
hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine);
136-
self->state_machine.pio->irq = 1u << self->state_machine.state_machine;
137-
irq_clear(self->pio_interrupt);
138-
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
139-
bool last_level = true;
140-
uint level_count = 0;
141-
uint16_t result = 0;
142-
uint16_t buf_index = 0;
143-
while ( buf_index < self->maxlen ) {
144-
uint32_t rxfifo = 0;
145-
rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine);
146-
// translate from fifo to buffer
147-
for (uint i = 0; i < 32; i++) {
148-
bool level = (rxfifo & (1 << i)) >> i;
149-
if (level == last_level ) {
150-
level_count ++;
151-
} else {
152-
result = level_count * 6;
153-
last_level = level;
154-
level_count = 1;
142+
uint32_t rxfifo = 0;
143+
144+
rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine);
145+
// translate from fifo to buffer
146+
for (uint i = 0; i < 32; i++) {
147+
bool level = (rxfifo & (1 << i)) >> i;
148+
if (level == last_level ) {
149+
level_count ++;
150+
} else {
151+
result = level_count;
152+
last_level = level;
153+
level_count = 1;
155154
// ignore pulses that are too long and too short
156-
if (result < 10000 && result > 10) {
155+
if (result < 2000 && result > 10) {
157156
self->buffer[buf_index] = result;
158157
buf_index++;
159158
self->len++;
160159
}
161160
}
162161
}
163-
// check for a pulse thats too long (20ms)
164-
if ( level_count > 3000 ) {
165-
break;
166-
}
162+
gpio_put(pin_GPIO15.number, true);
163+
// clear interrupt
164+
irq_clear(self->pio_interrupt);
165+
hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine);
166+
self->state_machine.pio->irq = 1u << self->state_machine.state_machine;
167+
// check for a pulse thats too long (2000 us) and reset
168+
if ( level_count > 2000 ) {
169+
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
170+
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
171+
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
172+
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
173+
irq_set_enabled(self->pio_interrupt, true);
167174
}
168-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
169-
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
170-
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
171-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
172-
irq_set_enabled(self->pio_interrupt, true);
173175
}
174176
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
175177
uint16_t trigger_duration) {
178+
// exec a wait for the selected pin to change state
179+
if (self->idle_state == true ) {
180+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
181+
} else {
182+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
183+
}
176184
// Send the trigger pulse.
177185
if (trigger_duration > 0) {
178186
gpio_set_function(self->pin ,GPIO_FUNC_SIO);
@@ -186,8 +194,6 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
186194
common_hal_mcu_delay_us(100);
187195
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
188196
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
189-
// exec a wait for the selected pin to go high
190-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
191197
}
192198

193199
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
@@ -202,9 +208,11 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
202208
uint16_t value = self->buffer[self->start];
203209
self->start = (self->start + 1) % self->maxlen;
204210
self->len--;
211+
// if we are empty reset buffer pointer and counters
205212
if (self->len == 0 ) {
206-
// reset buffer pointer
207213
self->start = 0;
214+
buf_index = 0;
215+
level_count = 0;
208216
}
209217
return value;
210218
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017-2021 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2021 Dave Putz for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)