Created
October 12, 2021 00:42
-
-
Save circuit4u-medium/e0353faa03e69d36dda83ee9bf8afca9 to your computer and use it in GitHub Desktop.
rust blue pill access GPIO with stm32f1 PAC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![cfg_attr(not(doc), no_main)] | |
//use panic_rtt_target as _; | |
use panic_halt as _; | |
use cortex_m_rt::entry; | |
//use core::ptr; | |
use stm32f1::stm32f103; | |
#[entry] | |
fn main() -> ! { | |
let mut peripherals = stm32f103::Peripherals::take().unwrap(); | |
// // Set up named variables for the register addresses we'll use. These are defined in the STM32's | |
// // reference manual (RM): rm0008. You can find base addresses under the Memory and bus architecture chapter, and the offsets (Things we | the base addresses with), under the RCC and GPIO chapters, registers sections. | |
// let RCC_AHB2ENR = 0x4002_1000 | 0x18; | |
// let GPIOC_BASE = 0x4001_1000; | |
let gpioc = &peripherals.GPIOC; | |
let rcc = &peripherals.RCC; | |
// // You can find these offsets under each RM register table: `Address Offset` | |
// let GPIOC_CRH = GPIOC_BASE | 0x04; | |
// let GPIOC_BSRR = GPIOC_BASE | 0x10; | |
// let gpioaen_field = 4; | |
// let output_pin_num = 13; // A variable to indicate with GPIO pin number we're using. Ie PC13 | |
// unsafe { | |
// // Set the GPIOCEN field of the RCC register block, AHB2_ENR register. This enables the peripheral clock | |
// // to all pins on the GPIOC port. | |
// ptr::write_volatile(RCC_AHB2ENR as *mut u32, 1 << gpioaen_field); | |
rcc.apb2enr.write(|w| w.iopcen().set_bit()); | |
// // Set the mode of PC13 to Output, The hex values 0b01 is defined in the GPIO registers section of the RM | |
// // We Multiply the pin numbers by 4, since each field of this register is 4 bits wide. Also note that we perform a register read, as not to override previous settings. | |
// let mut existing_val = ptr::read_volatile(GPIOC_CRH as *const u32); | |
// // We use binary arithmetic here to 0 out the fields we’ll write to. Modifying single-bit fields is simpler. | |
// existing_val = existing_val & !(0b1111 << ((output_pin_num -8) * 4) ); //-8 being the offset for CRH register | |
// ptr::write_volatile( | |
// GPIOC_CRH as *mut u32, | |
// existing_val | (0b0001 << ((output_pin_num -8) * 4) ) // output mode @ 10MHz max speed; general purpose push-pull | |
// ); | |
gpioc.crh.write(|w| { | |
w.mode13().bits(0b11); | |
w.cnf13().bits(0b00) | |
}); | |
// // check schematic for this bit inverse | |
// // light up LED; RESET PC13 (0V) | |
// ptr::write_volatile(GPIOC_BSRR as *mut u32, 1 << (output_pin_num + 16)); | |
gpioc.bsrr.write(|w| w.br13().set_bit()); | |
// // turn off LED; SET PC13 (3.3V) | |
// //ptr::write_volatile(GPIOC_BSRR as *mut u32, 1 << (output_pin_num)); | |
// } | |
loop {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment