Skip to content

Commit 98c21be

Browse files
committed
improve comments for clarity
1 parent 91ad231 commit 98c21be

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

app/src/peripherals.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,73 @@
77
//! URL : <https://m-a-h-b-u-b.github.io>
88
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-Rust>
99
10-
#![no_std]
10+
// Build without the Rust standard library—important for embedded targets.
11+
#![no_std]
1112

12-
use core::ptr;
13+
// Provides volatile read/write functions for direct memory access.
14+
use core::ptr;
1315

14-
const GPIO_PORT_BASE: u32 = 0x5000_0000; // Replace with your MCU GPIO base
15-
const LED_PIN: u32 = 5; // Example: Pin 5
16+
// Hardware-Specific Constants
17+
const GPIO_PORT_BASE: u32 = 0x5000_0000; // Base address of the MCU's GPIO peripheral.
18+
// NOTE: Replace this address with the actual GPIO base of your target MCU.
19+
const LED_PIN: u32 = 5; // LED connected to GPIO pin number 5 (example).
1620

17-
/// Initialize LED GPIO pin as output
21+
// LED GPIO Initialization
22+
// Initialize LED GPIO pin as output
23+
//
24+
// This function configures the selected LED pin to act as an output pin
25+
// by writing to the GPIO port's MODER (Mode Register).
1826
pub fn init_led() {
1927
unsafe {
20-
// Configure LED_PIN as output
21-
let moder = (GPIO_PORT_BASE + 0x00) as *mut u32; // MODER register
28+
// Calculate the address of the MODER register.
29+
let moder = (GPIO_PORT_BASE + 0x00) as *mut u32;
30+
31+
// Read the current MODER value without compiler optimizations
32+
// to ensure hardware register consistency.
2233
let current = ptr::read_volatile(moder);
23-
// Clear previous mode bits and set as output (01)
34+
35+
// Clear the existing 2-bit mode field for LED_PIN,
36+
// then set it to `01` (binary) for "General Purpose Output Mode".
2437
ptr::write_volatile(
2538
moder,
2639
(current & !(0b11 << (LED_PIN * 2))) | (0b01 << (LED_PIN * 2)),
2740
);
2841
}
2942
}
3043

44+
// -----------------------------------------------------------------------------
45+
// LED Control Functions
46+
// -----------------------------------------------------------------------------
47+
3148
/// Turn LED on
49+
///
50+
/// Sets the LED pin's bit in the Output Data Register (ODR) to logic high.
3251
pub fn led_on() {
3352
unsafe {
34-
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32; // Output Data Register
53+
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32; // ODR register offset is 0x14.
54+
// OR the current ODR value with the LED bit to turn it on.
3555
ptr::write_volatile(odr, ptr::read_volatile(odr) | (1 << LED_PIN));
3656
}
3757
}
3858

3959
/// Turn LED off
60+
///
61+
/// Clears the LED pin's bit in the Output Data Register (ODR) to logic low.
4062
pub fn led_off() {
4163
unsafe {
4264
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32;
65+
// AND the current ODR value with the inverse of LED bit to turn it off.
4366
ptr::write_volatile(odr, ptr::read_volatile(odr) & !(1 << LED_PIN));
4467
}
4568
}
4669

4770
/// Toggle LED state
71+
///
72+
/// Flips the current logic level of the LED pin by XORing the ODR value.
4873
pub fn toggle_led() {
4974
unsafe {
5075
let odr = (GPIO_PORT_BASE + 0x14) as *mut u32;
76+
// XOR toggles the LED bit: if on → off, if off → on.
5177
ptr::write_volatile(odr, ptr::read_volatile(odr) ^ (1 << LED_PIN));
5278
}
5379
}

0 commit comments

Comments
 (0)