Skip to content

Commit 3ba66b6

Browse files
thalesfragosoTheZoq2
authored andcommitted
Fixes PWM on TIM1 implementation
1 parent 0cc2262 commit 3ba66b6

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
- Use `Deref` for SPI generic implementations instead of macros
1212
- Make traits `rcc::Enable` and `rcc::Reset` public, but `RccBus` sealed
1313
- Add `QeiOptions` struct to configure slave mode and auto reload value of QEI interface
14+
- Fix PWM on `TIM1`
1415

1516
## [v0.5.3] - 2020-01-20
1617

src/pwm.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@
5656
use core::marker::PhantomData;
5757
use core::mem;
5858

59-
use cast::{u16, u32};
6059
use crate::hal;
61-
#[cfg(any(
62-
feature = "stm32f100",
63-
feature = "stm32f103",
64-
feature = "stm32f105",
65-
))]
60+
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "stm32f105",))]
6661
use crate::pac::TIM1;
67-
use crate::pac::{TIM2, TIM3};
6862
#[cfg(feature = "medium")]
6963
use crate::pac::TIM4;
64+
use crate::pac::{TIM2, TIM3};
65+
use cast::{u16, u32};
7066

7167
use crate::afio::MAPR;
7268
use crate::bb;
@@ -82,7 +78,7 @@ pub trait Pins<REMAP, P> {
8278
type Channels;
8379
}
8480

85-
use crate::timer::sealed::{Remap, Ch1, Ch2, Ch3, Ch4};
81+
use crate::timer::sealed::{Ch1, Ch2, Ch3, Ch4, Remap};
8682
macro_rules! pins_impl {
8783
( $( ( $($PINX:ident),+ ), ( $($TRAIT:ident),+ ), ( $($ENCHX:ident),* ); )+ ) => {
8884
$(
@@ -117,37 +113,27 @@ pins_impl!(
117113
(P4), (Ch4), (C4);
118114
);
119115

120-
#[cfg(any(
121-
feature = "stm32f100",
122-
feature = "stm32f103",
123-
feature = "stm32f105",
124-
))]
116+
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "stm32f105",))]
125117
impl Timer<TIM1> {
126-
pub fn pwm<REMAP, P, PINS, T>(
127-
self,
128-
_pins: PINS,
129-
mapr: &mut MAPR,
130-
freq: T,
131-
) -> PINS::Channels
118+
pub fn pwm<REMAP, P, PINS, T>(self, _pins: PINS, mapr: &mut MAPR, freq: T) -> PINS::Channels
132119
where
133120
REMAP: Remap<Periph = TIM1>,
134121
PINS: Pins<REMAP, P>,
135122
T: Into<Hertz>,
136123
{
137124
mapr.modify_mapr(|_, w| unsafe { w.tim1_remap().bits(REMAP::REMAP) });
138125

126+
// TIM1 has a break function that deactivates the outputs, this bit automatically activates
127+
// the output when no break input is present
128+
self.tim.bdtr.modify(|_, w| w.aoe().set_bit());
129+
139130
let Self { tim, clk } = self;
140131
tim1(tim, _pins, freq.into(), clk)
141132
}
142133
}
143134

144135
impl Timer<TIM2> {
145-
pub fn pwm<REMAP, P, PINS, T>(
146-
self,
147-
_pins: PINS,
148-
mapr: &mut MAPR,
149-
freq: T,
150-
) -> PINS::Channels
136+
pub fn pwm<REMAP, P, PINS, T>(self, _pins: PINS, mapr: &mut MAPR, freq: T) -> PINS::Channels
151137
where
152138
REMAP: Remap<Periph = TIM2>,
153139
PINS: Pins<REMAP, P>,
@@ -161,12 +147,7 @@ impl Timer<TIM2> {
161147
}
162148

163149
impl Timer<TIM3> {
164-
pub fn pwm<REMAP, P, PINS, T>(
165-
self,
166-
_pins: PINS,
167-
mapr: &mut MAPR,
168-
freq: T,
169-
) -> PINS::Channels
150+
pub fn pwm<REMAP, P, PINS, T>(self, _pins: PINS, mapr: &mut MAPR, freq: T) -> PINS::Channels
170151
where
171152
REMAP: Remap<Periph = TIM3>,
172153
PINS: Pins<REMAP, P>,
@@ -181,12 +162,7 @@ impl Timer<TIM3> {
181162

182163
#[cfg(feature = "medium")]
183164
impl Timer<TIM4> {
184-
pub fn pwm<REMAP, P, PINS, T>(
185-
self,
186-
_pins: PINS,
187-
mapr: &mut MAPR,
188-
freq: T,
189-
) -> PINS::Channels
165+
pub fn pwm<REMAP, P, PINS, T>(self, _pins: PINS, mapr: &mut MAPR, freq: T) -> PINS::Channels
190166
where
191167
REMAP: Remap<Periph = TIM4>,
192168
PINS: Pins<REMAP, P>,
@@ -247,6 +223,12 @@ macro_rules! hal {
247223
let arr = u16(ticks / u32(psc + 1)).unwrap();
248224
tim.arr.write(|w| w.arr().bits(arr));
249225

226+
// The psc register is buffered, so we trigger an update event to update it
227+
// Sets the URS bit to prevent an interrupt from being triggered by the UG bit
228+
tim.cr1.modify(|_, w| w.urs().set_bit());
229+
tim.egr.write(|w| w.ug().set_bit());
230+
tim.cr1.modify(|_, w| w.urs().clear_bit());
231+
250232
tim.cr1.write(|w|
251233
w.cms()
252234
.bits(0b00)
@@ -360,11 +342,7 @@ macro_rules! hal {
360342
}
361343
}
362344

363-
#[cfg(any(
364-
feature = "stm32f100",
365-
feature = "stm32f103",
366-
feature = "stm32f105",
367-
))]
345+
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "stm32f105",))]
368346
hal! {
369347
TIM1: (tim1),
370348
}

0 commit comments

Comments
 (0)