Skip to content

Commit

Permalink
Use gpiozero
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-gorecki committed Jan 30, 2024
1 parent 1036cd9 commit 16caf9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
8 changes: 4 additions & 4 deletions rosbot_utils/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<url type="repository">https://github.com/husarion/rosbot_ros</url>
<url type="bugtracker">https://github.com/husarion/rosbot_ros/issues</url>

<exec_depend>python3-sh</exec_depend>
<exec_depend>python3-libgpiod</exec_depend>
<exec_depend>python3-gpiozero</exec_depend>
<exec_depend>python3-pyftdi-pip</exec_depend>
<exec_depend>usbutils</exec_depend>
<exec_depend>python3-serial</exec_depend>
<exec_depend>python3-requests</exec_depend>
<exec_depend>python3-serial</exec_depend>
<exec_depend>python3-sh</exec_depend>
<exec_depend>usbutils</exec_depend>

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
Expand Down
40 changes: 18 additions & 22 deletions rosbot_utils/rosbot_utils/flash-firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import time
import sys
import argparse
import gpiod
from gpiod.line import Direction, Value
from gpiozero import OutputDevice


class FirmwareFlasher:
Expand All @@ -39,44 +38,41 @@ def __init__(self, sys_arch, binary_file):
# Setups ThinkerBoard pins
print("Device: ThinkerBoard\n")
self.serial_port = "/dev/ttyS1"
self.boot0_pin = 164
self.reset_pin = 184
boot0_pin_no = 164
reset_pin_no = 184

elif self.sys_arch == "x86_64":
# Setups UpBoard pins
print("Device: UpBoard\n")
self.serial_port = "/dev/ttyS4"
self.boot0_pin = 17
self.reset_pin = 18
boot0_pin_no = 17
reset_pin_no = 18

elif self.sys_arch == "aarch64":
# Setups RPi pins
print("Device: RPi\n")
self.serial_port = "/dev/ttyAMA0"
self.boot0_pin = 17
self.reset_pin = 18
boot0_pin_no = 17
reset_pin_no = 18

else:
print("Unknown device...")

chip = gpiod.Chip("/dev/gpiochip0")
self.gpio_port = chip.request_lines(
{self.boot0_pin: gpiod.LineSettings(Direction.OUTPUT),
self.reset_pin: gpiod.LineSettings(Direction.OUTPUT)}
)
self.boot0_pin = OutputDevice(boot0_pin_no)
self.reset_pin = OutputDevice(reset_pin_no)

def enter_bootloader_mode(self):
self.gpio_port.set_value(self.boot0_pin, Value.ACTIVE)
self.gpio_port.set_value(self.reset_pin, Value.ACTIVE)
self.boot0_pin.on()
self.reset_pin.on()
time.sleep(0.2)
self.gpio_port.set_value(self.reset_pin, Value.INACTIVE)
self.reset_pin.off()
time.sleep(0.2)

def exit_bootloader_mode(self):
self.gpio_port.set_value(self.boot0_pin, Value.INACTIVE)
self.gpio_port.set_value(self.reset_pin, Value.ACTIVE)
self.boot0_pin.off()
self.reset_pin.on()
time.sleep(0.2)
self.gpio_port.set_value(self.reset_pin, Value.INACTIVE)
self.reset_pin.off()
time.sleep(0.2)

def try_flash_operation(self, operation_name, flash_command, flash_args):
Expand All @@ -95,12 +91,12 @@ def try_flash_operation(self, operation_name, flash_command, flash_args):
def flash_firmware(self):
self.enter_bootloader_mode()

# Disable the flash write-protection
self.try_flash_operation("Write-UnProtection", sh.stm32flash, ["-u"])

# Disable the flash read-protection
self.try_flash_operation("Read-UnProtection", sh.stm32flash, ["-k"])

# Disable the flash write-protection
self.try_flash_operation("Write-UnProtection", sh.stm32flash, ["-u"])

# Flashing the firmware
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
self.try_flash_operation("Flashing", sh.stm32flash, flash_args)
Expand Down

0 comments on commit 16caf9c

Please sign in to comment.