]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Add PWM Support for Jetson Nano
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Fri, 22 Mar 2024 19:47:41 +0000 (12:47 -0700)
committerMelissa LeBlanc-Williams <melissa@adafruit.com>
Fri, 22 Mar 2024 19:47:41 +0000 (12:47 -0700)
requirements.txt
setup.py
src/adafruit_blinka/microcontroller/tegra/PWMOut.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/tegra/t210/pin.py
src/pwmio.py

index 0b8adb012c815a1eae50a2e4cc490e99c4505b74..1ea7a341bcefa3351cfdd3b697fe860d8f0a199a 100755 (executable)
@@ -1,4 +1,4 @@
-Adafruit-PlatformDetect>=3.53.0
+Adafruit-PlatformDetect>=3.62.0
 Adafruit-PureIO>=1.1.7
 Jetson.GPIO; platform_machine=='aarch64'
 RPi.GPIO; platform_machine=='armv7l' or platform_machine=='armv6l' or platform_machine=='aarch64'
index e9283892ae737099280e8e68739a9a06a8345407..28a8fc31a6920ddf5ca1589c42c4154e1d80dad8 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -89,7 +89,7 @@ setup(
     },
     include_package_data=True,
     install_requires=[
-        "Adafruit-PlatformDetect>=3.53.0",
+        "Adafruit-PlatformDetect>=3.62.0",
         "Adafruit-PureIO>=1.1.7",
         "pyftdi>=0.40.0",
         "adafruit-circuitpython-typing",
diff --git a/src/adafruit_blinka/microcontroller/tegra/PWMOut.py b/src/adafruit_blinka/microcontroller/tegra/PWMOut.py
new file mode 100644 (file)
index 0000000..a74feb6
--- /dev/null
@@ -0,0 +1,159 @@
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""Custom PWMOut Wrapper for Jetson GPIO PWM Class"""
+from RPi import GPIO
+
+GPIO.setmode(GPIO.TEGRA_SOC)  # Use BCM pins D4 = GPIO #4
+GPIO.setwarnings(True)  # shh!
+
+
+class PWMError(IOError):
+    """Base class for PWM errors."""
+
+
+class PWMOut:
+    """Pulse Width Modulation Output Class"""
+
+    def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
+        self._pwmpin = None
+        self._period = 0
+        self._open(pin, duty_cycle, frequency, variable_frequency)
+
+    def __del__(self):
+        self.deinit()
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, t, value, traceback):
+        self.deinit()
+
+    def _open(self, pin, duty=0, freq=500, variable_frequency=False):
+        self._pin = pin
+        GPIO.setup(pin.id, GPIO.OUT, initial=GPIO.HIGH)
+        self._pwmpin = GPIO.PWM(pin.id, freq)
+
+        if variable_frequency:
+            print("Variable Frequency is not supported, continuing without it...")
+
+        # set frequency
+        self.frequency = freq
+        # set duty
+        self.duty_cycle = duty
+
+        self.enabled = True
+
+    def deinit(self):
+        """Deinit the PWM."""
+        if self._pwmpin is not None:
+            self._pwmpin.stop()
+            GPIO.cleanup(self._pin.id)
+            self._pwmpin = None
+
+    def _is_deinited(self):
+        if self._pwmpin is None:
+            raise ValueError(
+                "Object has been deinitialize and can no longer "
+                "be used. Create a new object."
+            )
+
+    @property
+    def period(self):
+        """Get or set the PWM's output period in seconds.
+
+        Raises:
+            PWMError: if an I/O or OS error occurs.
+            TypeError: if value type is not int or float.
+
+        :type: int, float
+        """
+        return 1.0 / self.frequency
+
+    @period.setter
+    def period(self, period):
+        if not isinstance(period, (int, float)):
+            raise TypeError("Invalid period type, should be int or float.")
+
+        self.frequency = 1.0 / period
+
+    @property
+    def duty_cycle(self):
+        """Get or set the PWM's output duty cycle which is the fraction of
+        each pulse which is high. 16-bit
+
+        Raises:
+            PWMError: if an I/O or OS error occurs.
+            TypeError: if value type is not int or float.
+            ValueError: if value is out of bounds of 0.0 to 1.0.
+
+        :type: int, float
+        """
+        return int(self._duty_cycle * 65535)
+
+    @duty_cycle.setter
+    def duty_cycle(self, duty_cycle):
+        if not isinstance(duty_cycle, (int, float)):
+            raise TypeError("Invalid duty cycle type, should be int or float.")
+
+        if not 0 <= duty_cycle <= 65535:
+            raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
+
+        # convert from 16-bit
+        duty_cycle /= 65535.0
+
+        self._duty_cycle = duty_cycle
+        self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100))
+
+    @property
+    def frequency(self):
+        """Get or set the PWM's output frequency in Hertz.
+
+        Raises:
+            PWMError: if an I/O or OS error occurs.
+            TypeError: if value type is not int or float.
+
+        :type: int, float
+        """
+
+        return self._frequency
+
+    @frequency.setter
+    def frequency(self, frequency):
+        if not isinstance(frequency, (int, float)):
+            raise TypeError("Invalid frequency type, should be int or float.")
+
+        self._pwmpin.ChangeFrequency(round(frequency))
+        self._frequency = frequency
+
+    @property
+    def enabled(self):
+        """Get or set the PWM's output enabled state.
+
+        Raises:
+            PWMError: if an I/O or OS error occurs.
+            TypeError: if value type is not bool.
+
+        :type: bool
+        """
+        return self._enabled
+
+    @enabled.setter
+    def enabled(self, value):
+        if not isinstance(value, bool):
+            raise TypeError("Invalid enabled type, should be string.")
+
+        if value:
+            self._pwmpin.start(round(self._duty_cycle * 100))
+        else:
+            self._pwmpin.stop()
+
+        self._enabled = value
+
+    # String representation
+    def __str__(self):
+        return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
+            self._pin,
+            self.frequency,
+            self.duty_cycle,
+        )
index dafe81007196f645d132a44a83f61ce9364ebbe7..61c7ef61d1e1ac25ee31639c5eb9f915fd618701 100644 (file)
@@ -135,3 +135,9 @@ i2cPorts = (
 
 # ordered as spiId, sckId, mosiId, misoId
 spiPorts = ((0, C02, C00, C01), (1, B06, B04, B05))
+
+# SysFS pwm outputs, pwm channel and pin in first tuple
+pwmOuts = (
+    ((0, 0), V00),
+    ((0, 1), E06),
+)
index 397120bb5fb6fcf36ac301506dc3eeb12259d972..11cb882df8aac2cbc31193712e7c03b8953f3e48 100644 (file)
@@ -42,6 +42,8 @@ elif detector.board.any_lubancat:
     from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut
 elif detector.board.pico_u2if:
     from adafruit_blinka.microcontroller.rp2040_u2if.pwmio import PWMOut
+if detector.board.any_jetson_board:
+    from adafruit_blinka.microcontroller.tegra.PWMOut import PWMOut
 elif (
     detector.board.feather_u2if
     or detector.board.feather_can_u2if