Skip to content

Commit b9a7fcb

Browse files
committed
Implement LED peripheral control
- Added init_led() to configure LED GPIO as output - Added led_on(), led_off(), and toggle_led() functions - Uses direct register access with volatile reads/writes - Provides basic LED control for M2 Bootloader - Ready for adaptation to specific MCU hardware
1 parent 7c41394 commit b9a7fcb

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

app/src/peripherals.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
1-
//! M2 Bootloader RUST
2-
//! ------------------
1+
//! M2 Bootloader RUST App Peripherals Module
2+
//! -----------------------------------------
33
//! License : Dual License
44
//! - Apache 2.0 for open-source / personal use
55
//! - Commercial license required for closed-source use
66
//! Author : Md Mahbubur Rahman
77
//! URL : <https://m-a-h-b-u-b.github.io>
8-
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-RUST>
8+
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-Rust>
99
10-
pub fn init_led() {}
11-
pub fn toggle_led() {}
10+
#![no_std]
11+
12+
use core::ptr;
13+
14+
const GPIO_PORT_BASE: u32 = 0x5000_0000; // Replace with your MCU GPIO base
15+
const LED_PIN: u32 = 5; // Example: Pin 5
16+
17+
/// Initialize LED GPIO pin as output
18+
pub fn init_led() {
19+
unsafe {
20+
// Configure LED_PIN as output
21+
let moder = (GPIO_PORT_BASE + 0x00) as *mut u32; // MODER register
22+
let current = ptr::read_volatile(moder);
23+
// Clear previous mode bits and set as output (01)
24+
ptr::write_volatile(
25+
moder,
26+
(current & !(0b11 << (LED_PIN * 2))) | (0b01 << (LED_PIN * 2)),
27+
);
28+
}
29+
}
30+
31+
/// Turn LED on
32+
pub fn led_on() {
33+
unsafe {
34+
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32; // Output Data Register
35+
ptr::write_volatile(odr, ptr::read_volatile(odr) | (1 << LED_PIN));
36+
}
37+
}
38+
39+
/// Turn LED off
40+
pub fn led_off() {
41+
unsafe {
42+
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32;
43+
ptr::write_volatile(odr, ptr::read_volatile(odr) & !(1 << LED_PIN));
44+
}
45+
}
46+
47+
/// Toggle LED state
48+
pub fn toggle_led() {
49+
unsafe {
50+
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32;
51+
ptr::write_volatile(odr, ptr::read_volatile(odr) ^ (1 << LED_PIN));
52+
}
53+
}

0 commit comments

Comments
 (0)