feat: add 0x05_servo_rust driver with 13 unit tests

This commit is contained in:
Kevin Thomas
2026-03-25 12:04:01 -04:00
parent d315e76f1e
commit 37c1a0b5a3
18 changed files with 1705 additions and 4 deletions
+8
View File
@@ -0,0 +1,8 @@
//! @file lib.rs
//! @brief Library root for the servo driver crate
//! @author Kevin Thomas
//! @date 2025
#![no_std]
pub mod servo;
+324
View File
@@ -0,0 +1,324 @@
//! @file main.rs
//! @brief SG90 servo motor driver demonstration
//! @author Kevin Thomas
//! @date 2025
//!
//! MIT License
//!
//! Copyright (c) 2025 Kevin Thomas
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in
//! all copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.
//!
//! -----------------------------------------------------------------------------
//!
//! Demonstrates SG90 servo control using the servo driver (servo.rs).
//! PWM at 50 Hz on GPIO 6 sweeps the servo from 0 degrees to 180 degrees
//! and back in 10-degree increments, printing each angle over UART.
//!
//! Wiring:
//! GPIO6 -> Servo signal wire (orange or yellow)
//! 5V -> Servo power wire (red) -- use external 5 V supply for load
//! GND -> Servo ground wire (brown or black)
#![no_std]
#![no_main]
#[allow(dead_code)]
mod servo;
use defmt_rtt as _;
#[cfg(target_arch = "riscv32")]
use panic_halt as _;
#[cfg(target_arch = "arm")]
use panic_probe as _;
use embedded_hal::pwm::SetDutyCycle;
use fugit::RateExtU32;
use hal::entry;
use hal::Clock;
use hal::gpio::{FunctionNull, FunctionUart, Pin, PullDown, PullNone};
use hal::uart::{DataBits, Enabled, StopBits, UartConfig, UartPeripheral};
#[cfg(rp2350)]
use rp235x_hal as hal;
#[cfg(rp2040)]
use rp2040_hal as hal;
#[unsafe(link_section = ".boot2")]
#[used]
#[cfg(rp2040)]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
#[unsafe(link_section = ".start_block")]
#[used]
#[cfg(rp2350)]
pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
const UART_BAUD: u32 = 115_200;
const STEP_DEGREES: i32 = 10;
const STEP_DELAY_MS: u32 = 150;
type TxPin = Pin<hal::gpio::bank0::Gpio0, FunctionUart, PullNone>;
type RxPin = Pin<hal::gpio::bank0::Gpio1, FunctionUart, PullNone>;
type TxPinDefault = Pin<hal::gpio::bank0::Gpio0, FunctionNull, PullDown>;
type RxPinDefault = Pin<hal::gpio::bank0::Gpio1, FunctionNull, PullDown>;
type EnabledUart = UartPeripheral<Enabled, hal::pac::UART0, (TxPin, RxPin)>;
/// Initialise system clocks and PLLs from the external 12 MHz crystal.
///
/// # Arguments
///
/// * `xosc` - XOSC peripheral singleton.
/// * `clocks` - CLOCKS peripheral singleton.
/// * `pll_sys` - PLL_SYS peripheral singleton.
/// * `pll_usb` - PLL_USB peripheral singleton.
/// * `resets` - Mutable reference to the RESETS peripheral.
/// * `watchdog` - Mutable reference to the watchdog timer.
///
/// # Returns
///
/// Configured clocks manager.
///
/// # Panics
///
/// Panics if clock initialisation fails.
fn init_clocks(
xosc: hal::pac::XOSC,
clocks: hal::pac::CLOCKS,
pll_sys: hal::pac::PLL_SYS,
pll_usb: hal::pac::PLL_USB,
resets: &mut hal::pac::RESETS,
watchdog: &mut hal::Watchdog,
) -> hal::clocks::ClocksManager {
hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ, xosc, clocks, pll_sys, pll_usb, resets, watchdog,
)
.unwrap()
}
/// Unlock the GPIO bank and return the pin set.
///
/// # Arguments
///
/// * `io_bank0` - IO_BANK0 peripheral singleton.
/// * `pads_bank0` - PADS_BANK0 peripheral singleton.
/// * `sio` - SIO peripheral singleton.
/// * `resets` - Mutable reference to the RESETS peripheral.
///
/// # Returns
///
/// GPIO pin set for the entire bank.
fn init_pins(
io_bank0: hal::pac::IO_BANK0,
pads_bank0: hal::pac::PADS_BANK0,
sio: hal::pac::SIO,
resets: &mut hal::pac::RESETS,
) -> hal::gpio::Pins {
let sio = hal::Sio::new(sio);
hal::gpio::Pins::new(io_bank0, pads_bank0, sio.gpio_bank0, resets)
}
/// Initialise UART0 for serial output (stdio equivalent).
///
/// # Arguments
///
/// * `uart0` - PAC UART0 peripheral singleton.
/// * `tx_pin` - GPIO pin to use as UART0 TX (GPIO 0).
/// * `rx_pin` - GPIO pin to use as UART0 RX (GPIO 1).
/// * `resets` - Mutable reference to the RESETS peripheral.
/// * `clocks` - Reference to the initialised clock configuration.
///
/// # Returns
///
/// Enabled UART0 peripheral ready for blocking writes.
///
/// # Panics
///
/// Panics if the HAL cannot achieve the requested baud rate.
fn init_uart(
uart0: hal::pac::UART0,
tx_pin: TxPinDefault,
rx_pin: RxPinDefault,
resets: &mut hal::pac::RESETS,
clocks: &hal::clocks::ClocksManager,
) -> EnabledUart {
let pins = (
tx_pin.reconfigure::<FunctionUart, PullNone>(),
rx_pin.reconfigure::<FunctionUart, PullNone>(),
);
let cfg = UartConfig::new(UART_BAUD.Hz(), DataBits::Eight, None, StopBits::One);
UartPeripheral::new(uart0, pins, resets)
.enable(cfg, clocks.peripheral_clock.freq())
.unwrap()
}
/// Create a blocking delay timer from the ARM SysTick peripheral.
///
/// # Arguments
///
/// * `clocks` - Reference to the initialised clock configuration.
///
/// # Returns
///
/// Blocking delay provider.
///
/// # Panics
///
/// Panics if the cortex-m core peripherals have already been taken.
fn init_delay(clocks: &hal::clocks::ClocksManager) -> cortex_m::delay::Delay {
let core = cortex_m::Peripherals::take().unwrap();
cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())
}
/// Format an angle into "Angle: NNN deg\r\n".
///
/// # Arguments
///
/// * `buf` - Mutable byte slice (must be at least 20 bytes).
/// * `angle` - Angle in degrees (0..180).
///
/// # Returns
///
/// Number of bytes written into the buffer.
fn format_angle(buf: &mut [u8], angle: i32) -> usize {
let prefix = b"Angle: ";
buf[..7].copy_from_slice(prefix);
let mut pos = 7;
let a = if angle < 0 { 0 } else { angle as u32 };
if a >= 100 {
buf[pos] = b'0' + (a / 100) as u8; pos += 1;
buf[pos] = b'0' + ((a / 10) % 10) as u8; pos += 1;
buf[pos] = b'0' + (a % 10) as u8; pos += 1;
} else if a >= 10 {
buf[pos] = b' '; pos += 1;
buf[pos] = b'0' + (a / 10) as u8; pos += 1;
buf[pos] = b'0' + (a % 10) as u8; pos += 1;
} else {
buf[pos] = b' '; pos += 1;
buf[pos] = b' '; pos += 1;
buf[pos] = b'0' + a as u8; pos += 1;
}
let suffix = b" deg\r\n";
buf[pos..pos + 6].copy_from_slice(suffix);
pos + 6
}
/// Sweep the servo angle upward from 0 to 180 in STEP_DEGREES increments.
///
/// # Arguments
///
/// * `uart` - UART peripheral for serial output.
/// * `channel` - PWM channel implementing SetDutyCycle.
/// * `delay` - Delay provider for pause between steps.
/// * `buf` - Scratch buffer for formatting output.
fn sweep_angle_up(
uart: &EnabledUart,
channel: &mut impl SetDutyCycle,
delay: &mut cortex_m::delay::Delay,
buf: &mut [u8; 20],
) {
let mut angle: i32 = 0;
while angle <= 180 {
let pulse = servo::angle_to_pulse_us(angle as f32, servo::SERVO_DEFAULT_MIN_US, servo::SERVO_DEFAULT_MAX_US);
let level = servo::pulse_us_to_level(pulse as u32, servo::SERVO_WRAP, servo::SERVO_HZ) as u16;
channel.set_duty_cycle(level).ok();
let n = format_angle(buf, angle);
uart.write_full_blocking(&buf[..n]);
delay.delay_ms(STEP_DELAY_MS);
angle += STEP_DEGREES;
}
}
/// Sweep the servo angle downward from 180 to 0 in STEP_DEGREES decrements.
///
/// # Arguments
///
/// * `uart` - UART peripheral for serial output.
/// * `channel` - PWM channel implementing SetDutyCycle.
/// * `delay` - Delay provider for pause between steps.
/// * `buf` - Scratch buffer for formatting output.
fn sweep_angle_down(
uart: &EnabledUart,
channel: &mut impl SetDutyCycle,
delay: &mut cortex_m::delay::Delay,
buf: &mut [u8; 20],
) {
let mut angle: i32 = 180;
while angle >= 0 {
let pulse = servo::angle_to_pulse_us(angle as f32, servo::SERVO_DEFAULT_MIN_US, servo::SERVO_DEFAULT_MAX_US);
let level = servo::pulse_us_to_level(pulse as u32, servo::SERVO_WRAP, servo::SERVO_HZ) as u16;
channel.set_duty_cycle(level).ok();
let n = format_angle(buf, angle);
uart.write_full_blocking(&buf[..n]);
delay.delay_ms(STEP_DELAY_MS);
angle -= STEP_DEGREES;
}
}
/// Application entry point for the servo sweep demo.
///
/// Initializes the servo on GPIO 6 and continuously sweeps 0-180-0
/// degrees in 10-degree increments, reporting each angle over UART.
///
/// # Returns
///
/// Does not return.
#[entry]
fn main() -> ! {
let mut pac = hal::pac::Peripherals::take().unwrap();
let clocks = init_clocks(
pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS,
&mut hal::Watchdog::new(pac.WATCHDOG),
);
let pins = init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let uart = init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
let mut delay = init_delay(&clocks);
let pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);
let mut pwm = pwm_slices.pwm3;
let sys_hz = clocks.system_clock.freq().to_Hz();
let div = servo::calc_clk_div(sys_hz, servo::SERVO_HZ, servo::SERVO_WRAP);
let div_int = div as u8;
pwm.set_div_int(div_int);
pwm.set_div_frac((((div - div_int as f32) * 16.0) as u8).min(15));
pwm.set_top(servo::SERVO_WRAP as u16);
pwm.enable();
pwm.channel_a.output_to(pins.gpio6);
uart.write_full_blocking(b"Servo driver initialized on GPIO 6\r\n");
uart.write_full_blocking(b"Sweeping 0 -> 180 -> 0 degrees in 10-degree steps\r\n");
let mut buf = [0u8; 20];
loop {
sweep_angle_up(&uart, &mut pwm.channel_a, &mut delay, &mut buf);
sweep_angle_down(&uart, &mut pwm.channel_a, &mut delay, &mut buf);
}
}
#[unsafe(link_section = ".bi_entries")]
#[used]
pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 5] = [
hal::binary_info::rp_cargo_bin_name!(),
hal::binary_info::rp_cargo_version!(),
hal::binary_info::rp_program_description!(c"SG90 Servo Sweep Demo"),
hal::binary_info::rp_cargo_homepage_url!(),
hal::binary_info::rp_program_build_attribute!(),
];
// End of file
+205
View File
@@ -0,0 +1,205 @@
//! @file servo.rs
//! @brief Implementation of a simple SG90 servo driver (pure-logic helpers)
//! @author Kevin Thomas
//! @date 2025
//!
//! MIT License
//!
//! Copyright (c) 2025 Kevin Thomas
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in
//! all copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.
/// Default minimum pulse width in microseconds (0 degrees).
pub const SERVO_DEFAULT_MIN_US: u16 = 1000;
/// Default maximum pulse width in microseconds (180 degrees).
pub const SERVO_DEFAULT_MAX_US: u16 = 2000;
/// Default PWM wrap value for 50 Hz servo (20 000 - 1).
pub const SERVO_WRAP: u32 = 20000 - 1;
/// Default servo frequency in Hz.
pub const SERVO_HZ: f32 = 50.0;
/// Convert a pulse width in microseconds to a PWM counter level.
///
/// Uses the configured PWM wrap and servo frequency to map pulse time
/// into the channel compare value expected by the PWM hardware.
///
/// # Arguments
///
/// * `pulse_us` - Pulse width in microseconds.
/// * `wrap` - PWM counter wrap value.
/// * `hz` - PWM frequency in Hz.
///
/// # Returns
///
/// PWM level suitable for the channel compare register.
pub fn pulse_us_to_level(pulse_us: u32, wrap: u32, hz: f32) -> u32 {
let period_us = 1_000_000.0f32 / hz;
let counts_per_us = (wrap + 1) as f32 / period_us;
(pulse_us as f32 * counts_per_us + 0.5f32) as u32
}
/// Clamp a pulse width to the valid servo range.
///
/// Values below min_us are raised to min_us; values above max_us are
/// lowered to max_us.
///
/// # Arguments
///
/// * `pulse_us` - Raw pulse width in microseconds.
/// * `min_us` - Minimum allowed pulse width.
/// * `max_us` - Maximum allowed pulse width.
///
/// # Returns
///
/// Clamped pulse width.
pub fn clamp_pulse_us(pulse_us: u16, min_us: u16, max_us: u16) -> u16 {
if pulse_us < min_us {
min_us
} else if pulse_us > max_us {
max_us
} else {
pulse_us
}
}
/// Map a servo angle in degrees to a pulse width in microseconds.
///
/// Clamps degrees to [0, 180], then linearly maps to the pulse range.
///
/// # Arguments
///
/// * `degrees` - Angle in degrees (0.0 to 180.0).
/// * `min_us` - Pulse width at 0 degrees.
/// * `max_us` - Pulse width at 180 degrees.
///
/// # Returns
///
/// Pulse width in microseconds corresponding to the given angle.
pub fn angle_to_pulse_us(degrees: f32, min_us: u16, max_us: u16) -> u16 {
let d = if degrees < 0.0f32 {
0.0f32
} else if degrees > 180.0f32 {
180.0f32
} else {
degrees
};
let ratio = d / 180.0f32;
let span = (max_us - min_us) as f32;
(min_us as f32 + ratio * span + 0.5f32) as u16
}
/// Compute the PWM clock divider for the servo frequency.
///
/// # Arguments
///
/// * `sys_hz` - System clock frequency in Hz.
/// * `servo_hz` - Desired servo PWM frequency in Hz.
/// * `wrap` - PWM counter wrap value.
///
/// # Returns
///
/// Clock divider value.
pub fn calc_clk_div(sys_hz: u32, servo_hz: f32, wrap: u32) -> f32 {
sys_hz as f32 / (servo_hz * (wrap + 1) as f32)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pulse_us_to_level_1000us() {
let level = pulse_us_to_level(1000, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 1000);
}
#[test]
fn pulse_us_to_level_2000us() {
let level = pulse_us_to_level(2000, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 2000);
}
#[test]
fn pulse_us_to_level_1500us() {
let level = pulse_us_to_level(1500, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 1500);
}
#[test]
fn pulse_us_to_level_zero() {
let level = pulse_us_to_level(0, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 0);
}
#[test]
fn clamp_pulse_us_below_min() {
assert_eq!(clamp_pulse_us(500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 1000);
}
#[test]
fn clamp_pulse_us_above_max() {
assert_eq!(clamp_pulse_us(3000, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 2000);
}
#[test]
fn clamp_pulse_us_within_range() {
assert_eq!(clamp_pulse_us(1500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 1500);
}
#[test]
fn angle_to_pulse_us_zero() {
let pulse = angle_to_pulse_us(0.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1000);
}
#[test]
fn angle_to_pulse_us_180() {
let pulse = angle_to_pulse_us(180.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 2000);
}
#[test]
fn angle_to_pulse_us_90() {
let pulse = angle_to_pulse_us(90.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1500);
}
#[test]
fn angle_to_pulse_us_clamped_negative() {
let pulse = angle_to_pulse_us(-10.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1000);
}
#[test]
fn angle_to_pulse_us_clamped_above() {
let pulse = angle_to_pulse_us(200.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 2000);
}
#[test]
fn calc_clk_div_150mhz() {
let div = calc_clk_div(150_000_000, SERVO_HZ, SERVO_WRAP);
assert!((div - 150.0).abs() < 0.01);
}
}
// End of file