Skip to content

Commit

Permalink
Handle debugger pins
Browse files Browse the repository at this point in the history
At startup, pins used by the debugger are in `Debugger` state, prohibiting their
use. `disable_jtag` allow to get PA15, PB3 and PB4 usable as GPIO or alternate
functions at the cost of disabling JTAG.
  • Loading branch information
TeXitoi committed Jul 8, 2019
1 parent 0c8cce5 commit 730678e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Breaking changes

- ADC now requires the clock configuration for intialisation
- `disable_jtag` now transforms PA15, PB3 and PB4 to forbid their use without desactivating JTAG

### Changed

Expand Down
14 changes: 11 additions & 3 deletions examples/nojtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ fn main() -> ! {
let p = pac::Peripherals::take().unwrap();

let mut rcc = p.RCC.constrain();
let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
let mut afio = p.AFIO.constrain(&mut rcc.apb2);

afio.mapr.disable_jtag();
let (pa15, pb3, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);

gpiob.pb4.into_push_pull_output(&mut gpiob.crl).set_low();
let mut pa15 = pa15.into_push_pull_output(&mut gpioa.crh);
let mut pb3 = pb3.into_push_pull_output(&mut gpiob.crl);
let mut pb4 = pb4.into_push_pull_output(&mut gpiob.crl);

loop {}
loop {
pa15.toggle();
pb3.toggle();
pb4.toggle();
cortex_m::asm::delay(8_000_000);
}
}
5 changes: 3 additions & 2 deletions examples/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ fn main() -> ! {
let mut afio = p.AFIO.constrain(&mut rcc.apb2);
let mut dbg = p.DBGMCU;

let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
let gpioa = p.GPIOA.split(&mut rcc.apb2);
let gpiob = p.GPIOB.split(&mut rcc.apb2);

let pb4 = gpiob.pb4;
let (_pa15, _pb3, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);
let pb5 = gpiob.pb5;

let pwm_input = p.TIM3.pwm_input(
Expand Down
26 changes: 23 additions & 3 deletions src/afio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ use crate::pac::{afio, AFIO};

use crate::rcc::APB2;

use crate::gpio::{
Debugger,
Input,
Floating,
gpioa::PA15,
gpiob::{PB3, PB4},
};

pub trait AfioExt {
fn constrain(self, apb2: &mut APB2) -> Parts;
}
Expand Down Expand Up @@ -54,10 +62,22 @@ impl MAPR {
unsafe { &(*AFIO::ptr()).mapr }
}

/// Disables the JTAG to free up pb3, pb4 and pa15 for normal use
pub fn disable_jtag(&mut self) {
/// Disables the JTAG to free up pa15, pb3 and pb4 for normal use
pub fn disable_jtag(
&mut self,
_pa15: PA15<Debugger>,
_pb3: PB3<Debugger>,
_pb4: PB4<Debugger>
) -> (
PA15<Input<Floating>>,
PB3<Input<Floating>>,
PB4<Input<Floating>>,
) {
self.mapr()
.modify(|_, w| unsafe { w.swj_cfg().bits(0b010) })
.modify(|_, w| unsafe { w.swj_cfg().bits(0b010) });

// NOTE(unsafe) zero sized type, and recreated in its initial state
unsafe { core::mem::MaybeUninit::uninit().assume_init() }
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Input<MODE> {
_mode: PhantomData<MODE>,
}

/// Used by the debugger (type state)
pub struct Debugger;
/// Floating input (type state)
pub struct Floating;
/// Pulled down input (type state)
Expand Down Expand Up @@ -503,17 +505,17 @@ gpio!(GPIOA, gpioa, gpioa, iopaen, ioparst, PAx, [
PA10: (pa10, 10, Input<Floating>, CRH),
PA11: (pa11, 11, Input<Floating>, CRH),
PA12: (pa12, 12, Input<Floating>, CRH),
PA13: (pa13, 13, Input<Floating>, CRH),
PA14: (pa14, 14, Input<Floating>, CRH),
PA15: (pa15, 15, Input<Floating>, CRH),
PA13: (pa13, 13, super::Debugger, CRH),
PA14: (pa14, 14, super::Debugger, CRH),
PA15: (pa15, 15, super::Debugger, CRH),
]);

gpio!(GPIOB, gpiob, gpioa, iopben, iopbrst, PBx, [
PB0: (pb0, 0, Input<Floating>, CRL),
PB1: (pb1, 1, Input<Floating>, CRL),
PB2: (pb2, 2, Input<Floating>, CRL),
PB3: (pb3, 3, Input<Floating>, CRL),
PB4: (pb4, 4, Input<Floating>, CRL),
PB3: (pb3, 3, super::Debugger, CRL),
PB4: (pb4, 4, super::Debugger, CRL),
PB5: (pb5, 5, Input<Floating>, CRL),
PB6: (pb6, 6, Input<Floating>, CRL),
PB7: (pb7, 7, Input<Floating>, CRL),
Expand Down

0 comments on commit 730678e

Please sign in to comment.