Control GPIO
Using registers, we can control the GPIO 16 to be in output mode and with output high or low, or in input mode with the possibility to read those values.
Search for those registers on the datasheet and read their description to learn about how it works.
#![no_std]
#![no_main]
use cortex_m::asm;
pub use cortex_m_rt::entry;
use defmt::info;
use {defmt_rtt as _, panic_probe as _};
#[unsafe(link_section = ".start_block")]
#[unsafe(no_mangle)]
static BOOT_ROM_INFO: [u8; 44] = [
0xd3, 0xde, 0xff, 0xff, 0x42, 0x01, 0x21, 0x10, 0xff, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x79, 0x35, 0x12, 0xab, 0xf2, 0xeb, 0x88, 0x71,
0x6c, 0x93, 0x02, 0x10, 0x7c, 0x93, 0x02, 0x10, 0x70, 0xf6, 0x02, 0x10,
0x90, 0xa3, 0x1a, 0xe7, 0x1f, 0x01, 0x02, 0x03,
];
#[entry]
fn main() -> ! {
unsafe {
// IO_BANK0: GPIO16_CTRL Register
// Set to use SIO
core::ptr::write_volatile(0x40028084 as *mut u32, 0x5);
// Now put in Output mode!
// PADS_BANK0: GPIO16 Register
// 0x40: set bit 6: IE: Input enable
core::ptr::write_volatile(0x40038044 as *mut u32, 0x40);
// GPIO_OE_SET
// 0x10000: enable output for bit 16 (pin 16)
core::ptr::write_volatile(0xd0000038 as *mut u32, 1 << 16);
// 0x018 GPIO_OUT_SET
// High
core::ptr::write_volatile(0xd0000018 as *mut u32, 1 << 16);
status();
// 0x020 GPIO_OUT_CLEAR
// Low
core::ptr::write_volatile(0xd0000020 as *mut u32, 1 << 16);
status();
// Now put in Input mode!
// GPIO_OE_CLEAR
// 0x10000: disable output for bit 16 (pin 16)
core::ptr::write_volatile(0xd0000040 as *mut u32, 1 << 16);
// PADS_BANK0: GPIO16 Register
// 0x48: set bit 6 (IE: Input enable() and bit 3 (PUE: Pull up enable)
core::ptr::write_volatile(0x40038044 as *mut u32, 1 << 6 | 1 << 3);
};
status();
loop {
unsafe {
info!(
"{:#x} GPIO_IN",
core::ptr::read_volatile(0xd0000004 as *const u32)
);
}
for _ in 1..100000 {
asm::nop();
}
}
}
fn status() {
unsafe {
info!(
"{:#x} IO_BANK0: GPIO16_STATUS Register",
core::ptr::read_volatile(0x40028080 as *const u32)
);
info!(
"{:#x} IO_BANK0: GPIO16_CTRL Register",
core::ptr::read_volatile(0x40028084 as *const u32)
);
info!(
"{:#x} GPIO_OUT",
core::ptr::read_volatile(0xd0000010 as *const u32)
);
info!(
"{:#x} GPIO_OE",
core::ptr::read_volatile(0xd0000030 as *const u32)
);
info!(
"{:#x} PADS_BANK0: GPIO16 Register",
core::ptr::read_volatile(0x40038044 as *const u32)
);
}
}