Problem using a timer on WeAct Blackpill (STM32F411) #15969
-
I can run this program successfully on a WeAct Blackpill (STM32F411) using the build at import pyb
timer = pyb.Timer(2, freq=36000)
ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin('A1'), pulse_width_percent=10)
while True:
print("Running")
pyb.delay(2000) However, trying to modify a stepper control example running on a PICO to from machine import Pin
import time
import pyb
dir_pin = Pin('A4', Pin.OUT)
step_pin = Pin('A5', Pin.OUT)
steps_per_revolution = 200
tim = pyb.Timer(2)
print('hit CTRL-C to exit')
time.sleep(5) Before I clobber another board is there something wrong with: tim = pyb.Timer(2) According to: But there must be something required before you exit the program or else disaster ensues. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 23 replies
-
I haven't worked with the WeAct Blackpill (STM32F411) for a long time, so I can't test this myself. But have you tried |
Beta Was this translation helpful? Give feedback.
-
Your timer and pin initialization looks okay, but it's important to ensure the timer and pins are fully configured before running any logic. Also, you should specify the freq parameter for the timer to ensure it operates at the desired frequency. For example, |
Beta Was this translation helpful? Give feedback.
-
@shariltumin and @bidrohini Thank you for your comments. The script I was trying to modify is here: from machine import Pin, Timer
import utime
dir_pin = Pin(16, Pin.OUT)
step_pin = Pin(17, Pin.OUT)
steps_per_revolution = 200
# Initialize timer
tim = Timer()
def step(t):
global step_pin
step_pin.value(not step_pin.value())
def rotate_motor(delay):
# Set up timer for stepping
tim.init(freq=1000000//delay, mode=Timer.PERIODIC, callback=step)
def loop():
while True:
# Set motor direction clockwise
dir_pin.value(1)
# Spin motor slowly
rotate_motor(2000)
utime.sleep_ms(steps_per_revolution)
tim.deinit() # stop the timer
utime.sleep(1)
# Set motor direction counterclockwise
dir_pin.value(0)
# Spin motor quickly
rotate_motor(1000)
utime.sleep_ms(steps_per_revolution)
tim.deinit() # stop the timer
utime.sleep(1)
if __name__ == '__main__':
loop() I did In my "problem snippet" a CTRL-C (using rshell) happens before any logic, timer.init or timer.deinit. However, it seems to really clobber something. Even if I reload the firmware and the script, I can navigate to Maybe, I should try I can run the script on a ESP32, but I like to use the WeAct boards for projects that don't need WiFi. |
Beta Was this translation helpful? Give feedback.
-
I just checked it again: The 8MB external flash of the Blackpill board is formatted using LFS2 And there are no issues with large files. The same applies for the internal flash. If I format it with LFS, it works fine. Not issues in copying files larger than 1k to it. And it provides 17k more space than FAT (64k instead of 47k). For formatting the internal flash I used the code snippet from the documentation:
|
Beta Was this translation helpful? Give feedback.
Ah, that code you had in the opening message
Pins A4 and A5 are used by the spiflash chip:
micropython/ports/stm32/boards/WEACT_F411_BLACKPILL/pins.csv
Line 40 in aecdfc1
So yeah if you've got spiflash and you run that code, I expect the flash filesystem would definitely be corrupted by that.