File tree Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 33use crate :: clock:: Clocks ;
44use crate :: pac:: spi0:: ctrlr0:: TMOD_A as transfer_mode;
55use crate :: pac:: SPI0 ;
6- use crate :: sysctl:: { self , APB0 } ;
6+ use crate :: sysctl:: { self , APB2 } ;
77use core:: convert:: Infallible ;
88pub use embedded_hal:: spi:: { Mode , Phase , Polarity } ;
99
@@ -23,7 +23,7 @@ impl Spi<SPI0> {
2323 frame_format : FrameFormat ,
2424 endian : Endian ,
2525 clock : & Clocks ,
26- apb0 : & mut APB0 ,
26+ apb2 : & mut APB2 ,
2727 ) -> Self {
2828 let work_mode = hal_mode_to_pac ( mode) ;
2929 let frame_format = frame_format_to_pac ( frame_format) ;
@@ -54,7 +54,7 @@ impl Spi<SPI0> {
5454 spi. endian . write ( |w| w. bits ( endian) ) ;
5555 }
5656 // enable APB0 bus
57- apb0 . enable ( ) ;
57+ apb2 . enable ( ) ;
5858 // enable peripheral via sysctl
5959 sysctl:: clk_en_peri ( ) . modify ( |_r, w| w. spi0_clk_en ( ) . set_bit ( ) ) ;
6060 Spi { spi, cs_id }
Original file line number Diff line number Diff line change @@ -135,9 +135,37 @@ impl APB0 {
135135// _ownership: ()
136136// }
137137
138- // pub struct APB2 {
139- // _ownership: ()
140- // }
138+ pub struct APB2 {
139+ _ownership : ( ) ,
140+ }
141+
142+ impl APB2 {
143+ pub ( crate ) fn enable ( & mut self ) {
144+ clk_en_cent ( ) . modify ( |_r, w| w. apb2_clk_en ( ) . set_bit ( ) ) ;
145+ }
146+
147+ pub fn set_frequency ( & mut self , expected_freq : impl Into < Hertz > ) -> Hertz {
148+ let aclk = ACLK :: steal ( ) ;
149+ let aclk_frequency = aclk. get_frequency ( ) . 0 as i64 ;
150+ // apb2_frequency = aclk_frequency / (apb2_clk_sel + 1)
151+ let apb2_clk_sel = ( aclk_frequency / expected_freq. into ( ) . 0 as i64 - 1 )
152+ . max ( 0 )
153+ . min ( 0b111 ) as u8 ;
154+ unsafe {
155+ sysctl ( )
156+ . clk_sel0
157+ . modify ( |_, w| w. apb2_clk_sel ( ) . bits ( apb2_clk_sel) ) ;
158+ }
159+ Hertz ( aclk_frequency as u32 / ( apb2_clk_sel as u32 + 1 ) )
160+ }
161+
162+ pub fn get_frequency ( & self ) -> Hertz {
163+ let aclk = ACLK :: steal ( ) ;
164+ let aclk_frequency = aclk. get_frequency ( ) . 0 as i64 ;
165+ let apb2_clk_sel = sysctl ( ) . clk_sel2 . read ( ) . apb2_clk_sel ( ) . bits ( ) ;
166+ Hertz ( aclk_frequency as u32 / ( apb2_clk_sel as u32 + 1 ) )
167+ }
168+ }
141169
142170/// PLL0, which source is CLOCK_FREQ_IN0,
143171/// and the output can be used on ACLK(CPU), SPIs, etc.
You can’t perform that action at this time.
0 commit comments