rp2040/test/
pwm.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Integration tests for PWM peripheral
6//!
7//! This module provides four integration tests:
8//!
9//! ## hello_pwm
10//!
11//! This test sets up GPIOs 14 and 15 as PWM pins. GPIO 15 should be much brighter than 14.
12//!
13//! ## Running the test
14//!
15//! First step is including the test module:
16//!
17//! ```rust,ignore
18//! #[allow(dead_code)]
19//! use rp2040::test;
20//! ```
21//!
22//! Then create a test instance:
23//!
24//! ```rust,ignore
25//! let pwm_test = test::pwm::new(peripherals);
26//! ```
27//!
28//! Then run the test:
29//!
30//! ```rust,ignore
31//! pwm_test.hello_pwm();
32//! ```
33
34use kernel::debug;
35use kernel::hil::pwm::Pwm;
36use kernel::hil::pwm::PwmPin;
37
38use crate::chip::Rp2040DefaultPeripherals;
39use crate::gpio::{GpioFunction, RPGpio};
40
41/// Struct used to run integration tests
42pub struct PwmTest {
43    peripherals: &'static Rp2040DefaultPeripherals<'static>,
44}
45
46/// Create a PwmTest to run tests
47pub fn new(peripherals: &'static Rp2040DefaultPeripherals<'static>) -> PwmTest {
48    PwmTest { peripherals }
49}
50
51impl PwmTest {
52    /// Run hello_pwm test
53    pub fn hello_pwm(&self) {
54        self.peripherals
55            .pins
56            .get_pin(RPGpio::GPIO14)
57            .set_function(GpioFunction::PWM);
58        self.peripherals
59            .pins
60            .get_pin(RPGpio::GPIO15)
61            .set_function(GpioFunction::PWM);
62        let pwm_pin_14 = self.peripherals.pwm.gpio_to_pwm_pin(RPGpio::GPIO14);
63        let max_freq = pwm_pin_14.get_maximum_frequency_hz();
64        let max_duty_cycle = pwm_pin_14.get_maximum_duty_cycle();
65        assert_eq!(pwm_pin_14.start(max_freq / 8, max_duty_cycle / 2), Ok(()));
66        let pwm = &self.peripherals.pwm;
67        debug!("PWM pin 14 started");
68        let max_freq = pwm.get_maximum_frequency_hz();
69        let max_duty_cycle = pwm.get_maximum_duty_cycle();
70        assert_eq!(
71            pwm.start(&RPGpio::GPIO15, max_freq / 8, max_duty_cycle / 8 * 7),
72            Ok(())
73        );
74        debug!("PWM pin 15 started");
75    }
76}