@@ -100,3 +100,58 @@ nrf5340_clock_hfclk192m_release(void)
100100
101101 return stopped ;
102102}
103+
104+ int
105+ nrf5340_set_lf_clock_source (uint32_t clksrc )
106+ {
107+ uint32_t regmsk ;
108+ uint32_t regval ;
109+
110+ regmsk = CLOCK_LFCLKSTAT_STATE_Msk | CLOCK_LFCLKSTAT_SRC_Msk ;
111+ regval = CLOCK_LFCLKSTAT_STATE_Running << CLOCK_LFCLKSTAT_STATE_Pos ;
112+
113+ regval |= clksrc << CLOCK_LFCLKSTAT_SRC_Pos ;
114+
115+ /* Check if this clock source isn't already running */
116+ if ((NRF_CLOCK -> LFCLKSTAT & regmsk ) == regval ) {
117+ return 0 ;
118+ }
119+
120+ /*
121+ * Request HFXO if LFSYNTH is going to be set as source. If LFSYNTH is going to be
122+ * replaced with other source, release HFXO.
123+ */
124+ if (clksrc == CLOCK_LFCLKSTAT_SRC_LFSYNT ) {
125+ if ((NRF_CLOCK -> HFCLKSTAT & CLOCK_HFCLKSTAT_STATE_Msk ) !=
126+ (CLOCK_HFCLKSTAT_STATE_Running << CLOCK_HFCLKSTAT_STATE_Pos )) {
127+ NRF_CLOCK -> EVENTS_HFCLKSTARTED = 0 ;
128+ nrf5340_clock_hfxo_request ();
129+ while (1 ) {
130+ if ((NRF_CLOCK -> EVENTS_HFCLKSTARTED ) != 0 ) {
131+ break ;
132+ }
133+ }
134+ } else {
135+ nrf5340_clock_hfxo_request ();
136+ }
137+ } else if ((NRF_CLOCK -> LFCLKSTAT & CLOCK_LFCLKSTAT_SRC_Msk ) ==
138+ CLOCK_LFCLKSTAT_SRC_LFSYNT ) {
139+ nrf5340_clock_hfxo_release ();
140+ }
141+
142+ NRF_CLOCK -> TASKS_LFCLKSTOP = 1 ;
143+ NRF_CLOCK -> EVENTS_LFCLKSTARTED = 0 ;
144+ NRF_CLOCK -> LFCLKSRC = clksrc ;
145+ NRF_CLOCK -> TASKS_LFCLKSTART = 1 ;
146+
147+ /* Wait here till started! */
148+ while (1 ) {
149+ if (NRF_CLOCK -> EVENTS_LFCLKSTARTED ) {
150+ if ((NRF_CLOCK -> LFCLKSTAT & regmsk ) == regval ) {
151+ break ;
152+ }
153+ }
154+ }
155+
156+ return 1 ;
157+ }
0 commit comments