@@ -16,7 +16,7 @@ fn main() -> ! {
16
16
hrtim:: {
17
17
capture:: HrCapture , compare_register:: HrCompareRegister , control:: HrControltExt ,
18
18
external_event, external_event:: ToExternalEventSource , output:: HrOutput ,
19
- timer:: HrTimer , HrPwmAdvExt , Pscl4 ,
19
+ timer:: HrTimer , HrPwmAdvExt , Pscl128 ,
20
20
} ,
21
21
pwm,
22
22
pwr:: PwrExt ,
@@ -47,30 +47,30 @@ fn main() -> ! {
47
47
info ! ( "Setup Gpio" ) ;
48
48
let gpioa = dp. GPIOA . split ( & mut rcc) ;
49
49
let gpiob = dp. GPIOB . split ( & mut rcc) ;
50
+
51
+ // PA8 (D7 on Nucleo G474RE)
50
52
let pin_a: PA8 < Alternate < AF13 > > = gpioa. pa8 . into_alternate ( ) ;
51
53
52
- // ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
53
- // With max the max period set, this would be 960MHz /2^16 ~= 15kHz ...
54
- let prescaler = Pscl4 ;
54
+ // ...with a prescaler of 128 this gives us a HrTimer with a tick rate of 30MHz
55
+ // With max the max period set, this would be 30MHz /2^16 ~= 458Hz ...
56
+ let prescaler = Pscl128 ;
55
57
56
58
// . .
57
59
// . 50% .
58
60
// ------ ------
59
61
//out1 | | | |
60
62
// | | | |
61
63
// -------- ---------- --------
62
- // . ^ ^
63
- // . | |
64
- //AD samlp pa0 temp
65
64
let period = 0xFFFF ;
66
65
let ( mut hr_control, _flt_inputs, eev_inputs) =
67
66
dp. HRTIM_COMMON . hr_control ( & mut rcc) . wait_for_calibration ( ) ;
68
67
69
- let eev_input3 = eev_inputs
70
- . eev_input3
71
- . bind ( gpiob. pb7 . into_pull_down_input ( ) )
72
- . edge_or_polarity ( external_event:: EdgeOrPolarity :: Polarity (
73
- pwm:: Polarity :: ActiveHigh ,
68
+ // PB5 (D4 on Nucleo G474RE)
69
+ let eev_input6 = eev_inputs
70
+ . eev_input6
71
+ . bind ( gpiob. pb5 . into_pull_down_input ( ) )
72
+ . edge_or_polarity ( external_event:: EdgeOrPolarity :: Edge (
73
+ external_event:: Edge :: Falling ,
74
74
) )
75
75
. finalize ( & mut hr_control) ;
76
76
@@ -87,17 +87,23 @@ fn main() -> ! {
87
87
88
88
cr1. set_duty ( period / 2 ) ;
89
89
timer. start ( & mut hr_control) ;
90
+ out1. enable ( ) ;
90
91
91
92
let capture = timer. capture_ch1 ( ) ;
92
93
capture. enable_interrupt ( true , & mut hr_control) ;
93
- capture. add_event ( & eev_input3 ) ;
94
+ capture. add_event ( & eev_input6 ) ;
94
95
96
+ let mut old_duty = 0 ;
95
97
loop {
96
- if !capture. is_pending ( ) {
97
- continue ;
98
+ for duty in ( u32:: from ( period) / 10 ) ..( 9 * u32:: from ( period) / 10 ) {
99
+ if !capture. is_pending ( ) {
100
+ continue ;
101
+ }
102
+ let value = capture. get_signed ( ) ;
103
+ cr1. set_duty ( duty as u16 ) ;
104
+ capture. clear_interrupt ( ) ;
105
+ info ! ( "Capture: {:?}, duty: {}, diff: {}" , value, old_duty, value - old_duty as i32 ) ;
106
+ old_duty = duty;
98
107
}
99
- capture. clear_interrupt ( ) ;
100
-
101
- info ! ( "Capture: {:?}" , capture. get( ) ) ;
102
108
}
103
109
}
0 commit comments