Skip to content

Commit

Permalink
Merge pull request #106 from TheZoq2/master
Browse files Browse the repository at this point in the history
Rewrite SWJ config when modifying MAPR (fixes #105)
  • Loading branch information
therealprof authored Aug 31, 2019
2 parents 964374d + 1ee1ebb commit 53288d6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Remove `set_low` and `set_high` for pins in Alternate output mode
- Renames `set_seconds` and `seconds` methods on RTC to `set_time` and `current_time`, respectively
- Starting the timer does not generate interrupt requests anymore
- Make MAPR::mapr() private

### Fixed

- Fix some F1 variants crashing when modifying MAPR if JTAG is disabled

### Changed

Expand Down
17 changes: 13 additions & 4 deletions src/afio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl AfioExt for AFIO {

Parts {
evcr: EVCR { _0: () },
mapr: MAPR { _0: () },
mapr: MAPR { _0: (), jtag_enabled: true },
exticr1: EXTICR1 { _0: () },
exticr2: EXTICR2 { _0: () },
exticr3: EXTICR3 { _0: () },
Expand Down Expand Up @@ -54,13 +54,21 @@ impl EVCR {

pub struct MAPR {
_0: (),
jtag_enabled: bool,
}

impl MAPR {
pub fn mapr(&mut self) -> &afio::MAPR {
fn mapr(&mut self) -> &afio::MAPR {
unsafe { &(*AFIO::ptr()).mapr }
}

pub fn modify_mapr<F>(&mut self, mod_fn: F)
where F: for<'w> FnOnce(&afio::mapr::R, &'w mut afio::mapr::W) -> &'w mut afio::mapr::W
{
let debug_bits = if self.jtag_enabled {0b000} else {0b010};
self.mapr().modify(unsafe{ |r, w| mod_fn(r, w).swj_cfg().bits(debug_bits) });
}

/// Disables the JTAG to free up pa15, pb3 and pb4 for normal use
pub fn disable_jtag(
&mut self,
Expand All @@ -72,8 +80,9 @@ impl MAPR {
PB3<Input<Floating>>,
PB4<Input<Floating>>,
) {
self.mapr()
.modify(|_, w| unsafe { w.swj_cfg().bits(0b010) });
self.jtag_enabled = false;
// Avoid duplicating swj_cfg write code
self.modify_mapr(|_, w| w);

// NOTE(unsafe) The pins are now in the good state.
unsafe { (pa15.activate(), pb3.activate(), pb4.activate()) }
Expand Down
4 changes: 2 additions & 2 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<PINS> I2c<I2C1, PINS> {
where
PINS: Pins<I2C1>,
{
mapr.mapr().modify(|_, w| w.i2c1_remap().bit(PINS::REMAP));
mapr.modify_mapr(|_, w| w.i2c1_remap().bit(PINS::REMAP));
I2c::_i2c1(i2c, pins, mode, clocks, apb)
}
}
Expand All @@ -118,7 +118,7 @@ impl<PINS> BlockingI2c<I2C1, PINS> {
where
PINS: Pins<I2C1>,
{
mapr.mapr().modify(|_, w| w.i2c1_remap().bit(PINS::REMAP));
mapr.modify_mapr(|_, w| w.i2c1_remap().bit(PINS::REMAP));
BlockingI2c::_i2c1(
i2c,
pins,
Expand Down
9 changes: 3 additions & 6 deletions src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ impl Timer<TIM2> {
PINS: Pins<TIM2>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });

let Self { tim, clk } = self;
tim2(tim, _pins, freq.into(), clk)
Expand All @@ -231,8 +230,7 @@ impl Timer<TIM3> {
PINS: Pins<TIM3>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });

let Self { tim, clk } = self;
tim3(tim, _pins, freq.into(), clk)
Expand All @@ -250,8 +248,7 @@ impl Timer<TIM4> {
PINS: Pins<TIM4>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
mapr.modify_mapr(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));

let Self { tim, clk } = self;
tim4(tim, _pins, freq.into(), clk)
Expand Down
9 changes: 3 additions & 6 deletions src/pwm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ impl Timer<TIM2> {
PINS: Pins<TIM2>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
self.stop_in_debug(dbg, false);
let Self { tim, clk } = self;
tim2(tim, pins, clk, mode)
Expand All @@ -126,8 +125,7 @@ impl Timer<TIM3> {
PINS: Pins<TIM3>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
self.stop_in_debug(dbg, false);
let Self { tim, clk } = self;
tim3(tim, pins, clk, mode)
Expand All @@ -146,8 +144,7 @@ impl Timer<TIM4> {
PINS: Pins<TIM4>,
T: Into<Hertz>,
{
mapr.mapr()
.modify(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
mapr.modify_mapr(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
self.stop_in_debug(dbg, false);
let Self { tim, clk } = self;
tim4(tim, pins, clk, mode)
Expand Down
9 changes: 3 additions & 6 deletions src/qei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ impl Timer<TIM2> {
where
PINS: Pins<TIM2>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });

let Self { tim, clk: _ } = self;
Qei::_tim2(tim, pins)
Expand All @@ -50,8 +49,7 @@ impl Timer<TIM3> {
where
PINS: Pins<TIM3>,
{
mapr.mapr()
.modify(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
mapr.modify_mapr(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });

let Self { tim, clk: _ } = self;
Qei::_tim3(tim, pins)
Expand All @@ -63,8 +61,7 @@ impl Timer<TIM4> {
where
PINS: Pins<TIM4>,
{
mapr.mapr()
.modify(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
mapr.modify_mapr(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));

let Self { tim, clk: _ } = self;
Qei::_tim4(tim, pins)
Expand Down
3 changes: 1 addition & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ macro_rules! hal {
$USARTX::reset(apb);

#[allow(unused_unsafe)]
mapr.mapr()
.modify(|_, w| unsafe{
mapr.modify_mapr(|_, w| unsafe{
w.$usartX_remap().$bit(($closure)(PINS::REMAP))
});

Expand Down
2 changes: 1 addition & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<PINS> Spi<SPI1, PINS> {
F: Into<Hertz>,
PINS: Pins<SPI1>,
{
mapr.mapr().modify(|_, w| w.spi1_remap().bit(PINS::REMAP));
mapr.modify_mapr(|_, w| w.spi1_remap().bit(PINS::REMAP));
Spi::_spi1(spi, pins, mode, freq.into(), clocks, apb)
}
}
Expand Down

0 comments on commit 53288d6

Please sign in to comment.