Improved servocluster teardown

servo-pio
ZodiusInfuser 2022-03-24 13:05:01 +00:00
rodzic 41d577f458
commit 00408d3b2b
5 zmienionych plików z 29 dodań i 4 usunięć

Wyświetl plik

@ -150,9 +150,22 @@ PWMCluster::~PWMCluster() {
if(initialised) { if(initialised) {
pio_sm_set_enabled(pio, sm, false); pio_sm_set_enabled(pio, sm, false);
dma_channel_abort(dma_channel); // Tear down the DMA channel.
dma_channel_set_irq0_enabled(dma_channel, false); // This is copied from: https://github.com/raspberrypi/pico-sdk/pull/744/commits/5e0e8004dd790f0155426e6689a66e08a83cd9fc
//dma_channel_unclaim(dma_channel); // This does not seem to work uint32_t irq0_save = dma_hw->inte0 & (1u << dma_channel);
hw_clear_bits(&dma_hw->inte0, irq0_save);
dma_hw->abort = 1u << dma_channel;
// To fence off on in-flight transfers, the BUSY bit should be polled
// rather than the ABORT bit, because the ABORT bit can clear prematurely.
while (dma_hw->ch[dma_channel].ctrl_trig & DMA_CH0_CTRL_TRIG_BUSY_BITS) tight_loop_contents();
// Clear the interrupt (if any) and restore the interrupt masks.
dma_hw->ints0 = 1u << dma_channel;
hw_set_bits(&dma_hw->inte0, irq0_save);
dma_channel_unclaim(dma_channel); // This works now the teardown behaves correctly
clusters[dma_channel] = nullptr; clusters[dma_channel] = nullptr;
pio_sm_unclaim(pio, sm); pio_sm_unclaim(pio, sm);

Wyświetl plik

@ -1,3 +1,4 @@
import gc
import time import time
from machine import Pin from machine import Pin
from pimoroni import Analog, AnalogMux, Button from pimoroni import Analog, AnalogMux, Button
@ -21,6 +22,9 @@ MAX_CURRENT = 3.0 # The maximum current, in amps, to show on the meter
SAMPLES = 50 # The number of current measurements to take per reading SAMPLES = 50 # The number of current measurements to take per reading
TIME_BETWEEN = 0.001 # The time between each current measurement TIME_BETWEEN = 0.001 # The time between each current measurement
# Free up hardware resources ahead of creating a new ServoCluster
gc.collect()
# Create the user button # Create the user button
user_sw = Button(servo2040.USER_SW) user_sw = Button(servo2040.USER_SW)

Wyświetl plik

@ -1,3 +1,4 @@
import gc
import time import time
import math import math
from servo import ServoCluster, servo2040 from servo import ServoCluster, servo2040
@ -10,6 +11,9 @@ such may have problems when running code multiple times.
If you encounter issues, try resetting your board. If you encounter issues, try resetting your board.
""" """
# Free up hardware resources ahead of creating a new ServoCluster
gc.collect()
# Create a servo cluster for pins 0 to 3, using PIO 0 and State Machine 0 # Create a servo cluster for pins 0 to 3, using PIO 0 and State Machine 0
START_PIN = servo2040.SERVO_1 START_PIN = servo2040.SERVO_1
END_PIN = servo2040.SERVO_4 END_PIN = servo2040.SERVO_4

Wyświetl plik

@ -1,3 +1,4 @@
import gc
import time import time
import math import math
from pimoroni import Button from pimoroni import Button
@ -19,6 +20,9 @@ BRIGHTNESS = 0.4 # The brightness of the LEDs
UPDATES = 50 # How many times to update LEDs and Servos per second UPDATES = 50 # How many times to update LEDs and Servos per second
SERVO_EXTENT = 80.0 # How far from zero to move the servos SERVO_EXTENT = 80.0 # How far from zero to move the servos
# Free up hardware resources ahead of creating a new ServoCluster
gc.collect()
# Create a servo cluster for pins 0 to 7, using PIO 0 and State Machine 0 # Create a servo cluster for pins 0 to 7, using PIO 0 and State Machine 0
START_PIN = servo2040.SERVO_1 START_PIN = servo2040.SERVO_1
END_PIN = servo2040.SERVO_8 END_PIN = servo2040.SERVO_8

Wyświetl plik

@ -1365,7 +1365,7 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t
delete cluster; delete cluster;
m_del(PWMCluster::Sequence, seq_buffer, PWMCluster::NUM_BUFFERS * 2); m_del(PWMCluster::Sequence, seq_buffer, PWMCluster::NUM_BUFFERS * 2);
m_del(PWMCluster::TransitionData, dat_buffer, PWMCluster::BUFFER_SIZE * 2); m_del(PWMCluster::TransitionData, dat_buffer, PWMCluster::BUFFER_SIZE * 2);
mp_raise_msg(&mp_type_RuntimeError, "unable to allocate the hardware resources needed to initialise this ServoCluster");//. Try running `import gc` followed by `gc.collect()`, then create this ServoCluster"); mp_raise_msg(&mp_type_RuntimeError, "unable to allocate the hardware resources needed to initialise this ServoCluster. Try running `import gc` followed by `gc.collect()` before creating it");
} }
self = m_new_obj_with_finaliser(_ServoCluster_obj_t); self = m_new_obj_with_finaliser(_ServoCluster_obj_t);