clocks.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _HARDWARE_CLOCKS_H
8 #define _HARDWARE_CLOCKS_H
9 
10 #include "pico.h"
11 #include "hardware/structs/clocks.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
89 #define KHZ 1000
90 #define MHZ 1000000
91 
92 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_CLOCKS, Enable/disable assertions in the clocks module, type=bool, default=0, group=hardware_clocks
93 #ifndef PARAM_ASSERTIONS_ENABLED_CLOCKS
94 #define PARAM_ASSERTIONS_ENABLED_CLOCKS 0
95 #endif
96 
102 void clocks_init(void);
103 
115 bool clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);
116 
122 void clock_stop(enum clock_index clk_index);
123 
130 uint32_t clock_get_hz(enum clock_index clk_index);
131 
138 uint32_t frequency_count_khz(uint src);
139 
145 void clock_set_reported_hz(enum clock_index clk_index, uint hz);
146 
148 static inline float frequency_count_mhz(uint src) {
149  return ((float) (frequency_count_khz(src))) / KHZ;
150 }
152 
158 typedef void (*resus_callback_t)(void);
159 
168 void clocks_enable_resus(resus_callback_t resus_callback);
169 
178 void clock_gpio_init_int_frac(uint gpio, uint src, uint32_t div_int, uint8_t div_frac);
179 
187 static inline void clock_gpio_init(uint gpio, uint src, float div)
188 {
189  uint div_int = (uint)div;
190  uint8_t frac = (uint8_t)((div - (float)div_int) * (1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB));
191  clock_gpio_init_int_frac(gpio, src, div_int, frac);
192 }
193 
202 bool clock_configure_gpin(enum clock_index clk_index, uint gpio, uint32_t src_freq, uint32_t freq);
203 
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif
clock_configure_gpin
bool clock_configure_gpin(enum clock_index clk_index, uint gpio, uint32_t src_freq, uint32_t freq)
Configure a clock to come from a gpio input.
Definition: clocks.c:364
clock_get_hz
uint32_t clock_get_hz(enum clock_index clk_index)
Get the current frequency of the specified clock.
Definition: clocks.c:208
clock_gpio_init
static void clock_gpio_init(uint gpio, uint src, float div)
Output an optionally divided clock to the specified gpio pin.
Definition: clocks.h:187
clock_index
clock_index
Enumeration identifying a hardware clock.
Definition: clocks.h:27
clock_configure
bool clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq)
Configure the specified clock.
Definition: clocks.c:42
frequency_count_khz
uint32_t frequency_count_khz(uint src)
Measure a clocks frequency using the Frequency counter.
Definition: clocks.c:218
resus_callback_t
void(* resus_callback_t)(void)
Resus callback function type.
Definition: clocks.h:158
clocks_enable_resus
void clocks_enable_resus(resus_callback_t resus_callback)
Enable the resus function. Restarts clk_sys if it is accidentally stopped.
Definition: clocks.c:293
pico.h
clocks_init
void clocks_init(void)
Initialise the clock hardware.
Definition: clocks.c:121
clock_set_reported_hz
void clock_set_reported_hz(enum clock_index clk_index, uint hz)
Set the "current frequency" of the clock as reported by clock_get_hz without actually changing the cl...
Definition: clocks.c:213
clock_stop
void clock_stop(enum clock_index clk_index)
Stop the specified clock.
Definition: clocks.c:35
clock_gpio_init_int_frac
void clock_gpio_init_int_frac(uint gpio, uint src, uint32_t div_int, uint8_t div_frac)
Output an optionally divided clock to the specified gpio pin.
Definition: clocks.c:317