psoc-drivers 0.1.0

Hardware driver implementations for psoc-rs
//! System Resources Subsystem (SRSS) drivers
// Copyright (c) 2026, Infineon Technologies AG or an affiliate of Infineon Technologies AG.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

use core::arch::asm;

pub mod clock;
pub mod watchdog;

#[cfg(feature = "embassy-time")]
pub mod embassy_time;

/// Delays for at least the specified number of cycles.
pub fn delay_cycles(cycles: u32) {
    let i = cycles / 4 + 1;
    unsafe {
        asm!(
            ".p2align 3",
            "1:",
            "  adds {i}, {i}, 1",    // 1 cycle
            "  subs {i}, {i}, 2",    // 1 cycle
            "  bne  1b",             // 2 cycles (if taken)
            i = inout(reg) i => _,
            options(nomem, nostack)
        );
    }
}

/// Delays for at least the specified number of microseconds.
pub fn delay_microseconds(microseconds: u64) {
    let mut cycles = (clock::current_cpu_frequency() / 1_000_000) as u64 * microseconds;
    while cycles > u32::MAX as u64 {
        delay_cycles(u32::MAX);
        cycles -= 1 << 32;
    }
    delay_cycles(cycles as u32);
}