mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-04-20 18:16:49 +02:00
Fixed improper _
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
* @param pin GPIO pin number to query
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_led_state(uint32_t pin)
|
||||
static void print_led_state(uint32_t pin)
|
||||
{
|
||||
if (led_get_state(pin))
|
||||
uart_puts("LED: ON\r\n");
|
||||
@@ -47,7 +47,7 @@ int main(void)
|
||||
while (1)
|
||||
{
|
||||
led_toggle(LED_PIN);
|
||||
_print_led_state(LED_PIN);
|
||||
print_led_state(LED_PIN);
|
||||
delay_ms(BLINK_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_pad(uint32_t gpio_num)
|
||||
static void gpio_config_pad(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[gpio_num];
|
||||
@@ -43,7 +43,7 @@ static void _gpio_config_pad(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
static void gpio_config_funcsel(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = IO_BANK0->GPIO[gpio_num].CTRL;
|
||||
@@ -57,16 +57,16 @@ static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_enable_output(uint32_t gpio_num)
|
||||
static void gpio_enable_output(uint32_t gpio_num)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = (1U << gpio_num);
|
||||
}
|
||||
|
||||
void gpio_config(uint32_t gpio_num)
|
||||
{
|
||||
_gpio_config_pad(gpio_num);
|
||||
_gpio_config_funcsel(gpio_num);
|
||||
_gpio_enable_output(gpio_num);
|
||||
gpio_config_pad(gpio_num);
|
||||
gpio_config_funcsel(gpio_num);
|
||||
gpio_enable_output(gpio_num);
|
||||
}
|
||||
|
||||
void gpio_set(uint32_t gpio_num)
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
*
|
||||
* @param last_state Pointer to the stored previous button state
|
||||
*/
|
||||
static void _poll_button(bool *last_state) {
|
||||
static void poll_button(bool *last_state) {
|
||||
bool pressed = button_is_pressed(BUTTON_PIN);
|
||||
button_led_set(LED_PIN, pressed);
|
||||
if (pressed != *last_state) {
|
||||
@@ -73,7 +73,7 @@ int main(void) {
|
||||
printf("Button driver initialized: button=GPIO%d led=GPIO%d\r\n", BUTTON_PIN, LED_PIN);
|
||||
bool last_state = false;
|
||||
while (true) {
|
||||
_poll_button(&last_state);
|
||||
poll_button(&last_state);
|
||||
sleep_ms(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ static uint32_t debounce_delay_ms = 20;
|
||||
* @param pin GPIO pin number to re-sample
|
||||
* @return bool true if the pin is still low after the debounce delay
|
||||
*/
|
||||
static bool _debounce_confirm(uint32_t pin) {
|
||||
static bool debounce_confirm(uint32_t pin) {
|
||||
sleep_ms(debounce_delay_ms);
|
||||
return !gpio_get(pin);
|
||||
}
|
||||
@@ -58,7 +58,7 @@ void button_init(uint32_t pin, uint32_t debounce_ms) {
|
||||
|
||||
bool button_is_pressed(uint32_t pin) {
|
||||
if (!gpio_get(pin)) {
|
||||
return _debounce_confirm(pin);
|
||||
return debounce_confirm(pin);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
* @param pressed true if button is pressed, false if released
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_led_state(bool pressed)
|
||||
static void set_led_state(bool pressed)
|
||||
{
|
||||
if (pressed)
|
||||
led_on(LED_PIN);
|
||||
@@ -53,7 +53,7 @@ static void _set_led_state(bool pressed)
|
||||
* @param last_state pointer to the stored previous button state
|
||||
* @retval None
|
||||
*/
|
||||
static void _report_edge(bool pressed, bool *last_state)
|
||||
static void report_edge(bool pressed, bool *last_state)
|
||||
{
|
||||
if (pressed != *last_state)
|
||||
{
|
||||
@@ -70,12 +70,12 @@ static void _report_edge(bool pressed, bool *last_state)
|
||||
* @param last_state pointer to the stored previous button state
|
||||
* @retval None
|
||||
*/
|
||||
static void _poll_button(bool *last_state)
|
||||
static void poll_button(bool *last_state)
|
||||
{
|
||||
bool pressed;
|
||||
pressed = button_is_pressed(BUTTON_PIN);
|
||||
_set_led_state(pressed);
|
||||
_report_edge(pressed, last_state);
|
||||
set_led_state(pressed);
|
||||
report_edge(pressed, last_state);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -86,7 +86,7 @@ int main(void)
|
||||
bool last_state = false;
|
||||
while (1)
|
||||
{
|
||||
_poll_button(&last_state);
|
||||
poll_button(&last_state);
|
||||
delay_ms(POLL_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ static uint32_t debounce_delay_ms = 20;
|
||||
* @param pin GPIO pin number to re-sample
|
||||
* @retval bool true if the pin is still low after the debounce delay
|
||||
*/
|
||||
static bool _debounce_confirm(uint32_t pin)
|
||||
static bool debounce_confirm(uint32_t pin)
|
||||
{
|
||||
delay_ms(debounce_delay_ms);
|
||||
return !gpio_get(pin);
|
||||
@@ -47,6 +47,6 @@ void button_init(uint32_t pin, uint32_t debounce_ms)
|
||||
bool button_is_pressed(uint32_t pin)
|
||||
{
|
||||
if (!gpio_get(pin))
|
||||
return _debounce_confirm(pin);
|
||||
return debounce_confirm(pin);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_pad(uint32_t gpio_num)
|
||||
static void gpio_config_pad(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[gpio_num];
|
||||
@@ -43,7 +43,7 @@ static void _gpio_config_pad(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
static void gpio_config_funcsel(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = IO_BANK0->GPIO[gpio_num].CTRL;
|
||||
@@ -57,7 +57,7 @@ static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_pad_input_pullup(uint32_t gpio_num)
|
||||
static void gpio_config_pad_input_pullup(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[gpio_num];
|
||||
@@ -73,16 +73,16 @@ static void _gpio_config_pad_input_pullup(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_enable_output(uint32_t gpio_num)
|
||||
static void gpio_enable_output(uint32_t gpio_num)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = (1U << gpio_num);
|
||||
}
|
||||
|
||||
void gpio_config(uint32_t gpio_num)
|
||||
{
|
||||
_gpio_config_pad(gpio_num);
|
||||
_gpio_config_funcsel(gpio_num);
|
||||
_gpio_enable_output(gpio_num);
|
||||
gpio_config_pad(gpio_num);
|
||||
gpio_config_funcsel(gpio_num);
|
||||
gpio_enable_output(gpio_num);
|
||||
}
|
||||
|
||||
void gpio_set(uint32_t gpio_num)
|
||||
@@ -107,6 +107,6 @@ bool gpio_get(uint32_t gpio_num)
|
||||
|
||||
void gpio_config_input_pullup(uint32_t gpio_num)
|
||||
{
|
||||
_gpio_config_pad_input_pullup(gpio_num);
|
||||
_gpio_config_funcsel(gpio_num);
|
||||
gpio_config_pad_input_pullup(gpio_num);
|
||||
gpio_config_funcsel(gpio_num);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
* @param end Ending duty percentage
|
||||
* @param step Increment per iteration (negative for descending)
|
||||
*/
|
||||
static void _sweep_duty(int start, int end, int step) {
|
||||
static void sweep_duty(int start, int end, int step) {
|
||||
for (int duty = start; (step > 0) ? duty <= end : duty >= end; duty += step) {
|
||||
pwm_driver_set_duty_percent((uint8_t)duty);
|
||||
printf("Duty: %3d%%\r\n", duty);
|
||||
@@ -71,7 +71,7 @@ int main(void) {
|
||||
pwm_driver_init(PWM_PIN, PWM_FREQ_HZ);
|
||||
printf("PWM initialized: GPIO%d @ %d Hz\r\n", PWM_PIN, PWM_FREQ_HZ);
|
||||
while (true) {
|
||||
_sweep_duty(0, 100, 5);
|
||||
_sweep_duty(100, 0, -5);
|
||||
sweep_duty(0, 100, 5);
|
||||
sweep_duty(100, 0, -5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ static uint32_t pwm_wrap;
|
||||
* @param wrap_val Chosen PWM counter wrap value (period - 1)
|
||||
* @return float Clock divider to program into the PWM slice
|
||||
*/
|
||||
static float _calc_clk_div(uint32_t freq_hz, uint32_t wrap_val) {
|
||||
static float calc_clk_div(uint32_t freq_hz, uint32_t wrap_val) {
|
||||
uint32_t sys_hz = clock_get_hz(clk_sys);
|
||||
return (float)sys_hz / ((float)freq_hz * (float)(wrap_val + 1));
|
||||
}
|
||||
@@ -63,9 +63,9 @@ static float _calc_clk_div(uint32_t freq_hz, uint32_t wrap_val) {
|
||||
*
|
||||
* @param freq_hz Desired PWM output frequency in Hz
|
||||
*/
|
||||
static void _apply_pwm_config(uint32_t freq_hz) {
|
||||
static void apply_pwm_config(uint32_t freq_hz) {
|
||||
pwm_config cfg = pwm_get_default_config();
|
||||
pwm_config_set_clkdiv(&cfg, _calc_clk_div(freq_hz, pwm_wrap));
|
||||
pwm_config_set_clkdiv(&cfg, calc_clk_div(freq_hz, pwm_wrap));
|
||||
pwm_config_set_wrap(&cfg, pwm_wrap);
|
||||
pwm_init(pwm_slice, &cfg, true);
|
||||
pwm_set_chan_level(pwm_slice, pwm_chan, 0);
|
||||
@@ -76,7 +76,7 @@ void pwm_driver_init(uint32_t pin, uint32_t freq_hz) {
|
||||
pwm_slice = pwm_gpio_to_slice_num(pin);
|
||||
pwm_chan = pwm_gpio_to_channel(pin);
|
||||
pwm_wrap = 10000 - 1;
|
||||
_apply_pwm_config(freq_hz);
|
||||
apply_pwm_config(freq_hz);
|
||||
}
|
||||
|
||||
void pwm_driver_set_duty_percent(uint8_t percent) {
|
||||
|
||||
@@ -56,7 +56,7 @@ static void _uint8_to_str(uint8_t value, char *buf)
|
||||
* @param duty duty cycle value (0-100)
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_duty(uint8_t duty)
|
||||
static void print_duty(uint8_t duty)
|
||||
{
|
||||
char buf[4];
|
||||
_uint8_to_str(duty, buf);
|
||||
@@ -69,12 +69,12 @@ static void _print_duty(uint8_t duty)
|
||||
* @brief Sweep duty cycle upward from 0 to 100 percent.
|
||||
* @retval None
|
||||
*/
|
||||
static void _sweep_up(void)
|
||||
static void sweep_up(void)
|
||||
{
|
||||
for (uint8_t duty = 0; duty <= 100; duty += SWEEP_STEP)
|
||||
{
|
||||
pwm_set_duty(duty);
|
||||
_print_duty(duty);
|
||||
print_duty(duty);
|
||||
delay_ms(SWEEP_DELAY_MS);
|
||||
}
|
||||
}
|
||||
@@ -83,12 +83,12 @@ static void _sweep_up(void)
|
||||
* @brief Sweep duty cycle downward from 100 to 0 percent.
|
||||
* @retval None
|
||||
*/
|
||||
static void _sweep_down(void)
|
||||
static void sweep_down(void)
|
||||
{
|
||||
for (int8_t duty = 100; duty >= 0; duty -= SWEEP_STEP)
|
||||
{
|
||||
pwm_set_duty((uint8_t)duty);
|
||||
_print_duty((uint8_t)duty);
|
||||
print_duty((uint8_t)duty);
|
||||
delay_ms(SWEEP_DELAY_MS);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ int main(void)
|
||||
uart_puts("PWM initialized: GPIO25\r\n");
|
||||
while (1)
|
||||
{
|
||||
_sweep_up();
|
||||
_sweep_down();
|
||||
sweep_up();
|
||||
sweep_down();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_pad(uint32_t gpio_num)
|
||||
static void gpio_config_pad(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[gpio_num];
|
||||
@@ -43,7 +43,7 @@ static void _gpio_config_pad(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
static void gpio_config_funcsel(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = IO_BANK0->GPIO[gpio_num].CTRL;
|
||||
@@ -57,16 +57,16 @@ static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_enable_output(uint32_t gpio_num)
|
||||
static void gpio_enable_output(uint32_t gpio_num)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = (1U << gpio_num);
|
||||
}
|
||||
|
||||
void gpio_config(uint32_t gpio_num)
|
||||
{
|
||||
_gpio_config_pad(gpio_num);
|
||||
_gpio_config_funcsel(gpio_num);
|
||||
_gpio_enable_output(gpio_num);
|
||||
gpio_config_pad(gpio_num);
|
||||
gpio_config_funcsel(gpio_num);
|
||||
gpio_enable_output(gpio_num);
|
||||
}
|
||||
|
||||
void gpio_set(uint32_t gpio_num)
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
* @brief Clear the PWM reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_clear_reset_bit(void)
|
||||
static void pwm_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -53,7 +53,7 @@ static void _pwm_clear_reset_bit(void)
|
||||
* @brief Wait until the PWM block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_wait_reset_done(void)
|
||||
static void pwm_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_PWM_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ static void _pwm_wait_reset_done(void)
|
||||
* @brief Configure GPIO 25 pad and funcsel for PWM output.
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_configure_pin(void)
|
||||
static void pwm_configure_pin(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[LED_PIN];
|
||||
@@ -77,7 +77,7 @@ static void _pwm_configure_pin(void)
|
||||
* @brief Set the clock divider to 1 (no division).
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_set_divider(void)
|
||||
static void pwm_set_divider(void)
|
||||
{
|
||||
PWM[PWM_REG(PWM_CH_DIV_OFFSET)] = (1U << PWM_DIV_INT_SHIFT);
|
||||
}
|
||||
@@ -86,7 +86,7 @@ static void _pwm_set_divider(void)
|
||||
* @brief Set the wrap (TOP) value and zero the compare register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_set_wrap(void)
|
||||
static void pwm_set_wrap(void)
|
||||
{
|
||||
PWM[PWM_REG(PWM_CH_TOP_OFFSET)] = PWM_WRAP_DEFAULT;
|
||||
PWM[PWM_REG(PWM_CH_CC_OFFSET)] = 0;
|
||||
@@ -96,23 +96,23 @@ static void _pwm_set_wrap(void)
|
||||
* @brief Enable the PWM slice counter.
|
||||
* @retval None
|
||||
*/
|
||||
static void _pwm_enable(void)
|
||||
static void pwm_enable(void)
|
||||
{
|
||||
PWM[PWM_REG(PWM_CH_CSR_OFFSET)] = (1U << PWM_CSR_EN_SHIFT);
|
||||
}
|
||||
|
||||
void pwm_release_reset(void)
|
||||
{
|
||||
_pwm_clear_reset_bit();
|
||||
_pwm_wait_reset_done();
|
||||
pwm_clear_reset_bit();
|
||||
pwm_wait_reset_done();
|
||||
}
|
||||
|
||||
void pwm_init(void)
|
||||
{
|
||||
_pwm_configure_pin();
|
||||
_pwm_set_divider();
|
||||
_pwm_set_wrap();
|
||||
_pwm_enable();
|
||||
pwm_configure_pin();
|
||||
pwm_set_divider();
|
||||
pwm_set_wrap();
|
||||
pwm_enable();
|
||||
}
|
||||
|
||||
void pwm_set_duty(uint8_t percent)
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -60,7 +60,7 @@
|
||||
* @param end Ending angle in degrees
|
||||
* @param step Increment per iteration (negative for descending)
|
||||
*/
|
||||
static void _sweep_angle(int start, int end, int step) {
|
||||
static void sweep_angle(int start, int end, int step) {
|
||||
for (int angle = start; (step > 0) ? angle <= end : angle >= end; angle += step) {
|
||||
servo_set_angle((float)angle);
|
||||
printf("Angle: %3d deg\r\n", angle);
|
||||
@@ -74,7 +74,7 @@ int main(void) {
|
||||
printf("Servo driver initialized on GPIO %d\r\n", SERVO_GPIO);
|
||||
printf("Sweeping 0 -> 180 -> 0 degrees in %d-degree steps\r\n", STEP_DEGREES);
|
||||
while (true) {
|
||||
_sweep_angle(0, 180, STEP_DEGREES);
|
||||
_sweep_angle(180, 0, -STEP_DEGREES);
|
||||
sweep_angle(0, 180, STEP_DEGREES);
|
||||
sweep_angle(180, 0, -STEP_DEGREES);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ static bool servo_initialized = false;
|
||||
* @param pulse_us Pulse width in microseconds
|
||||
* @return uint32_t PWM level suitable for pwm_set_chan_level()
|
||||
*/
|
||||
static uint32_t _pulse_us_to_level(uint32_t pulse_us) {
|
||||
static uint32_t pulse_us_to_level(uint32_t pulse_us) {
|
||||
const float period_us = 1000000.0f / servo_hz;
|
||||
float counts_per_us = (servo_wrap + 1) / period_us;
|
||||
return (uint32_t)(pulse_us * counts_per_us + 0.5f);
|
||||
@@ -72,7 +72,7 @@ static uint32_t _pulse_us_to_level(uint32_t pulse_us) {
|
||||
* target servo frequency with the chosen wrap value, then starts
|
||||
* the PWM slice.
|
||||
*/
|
||||
static void _apply_servo_config(void) {
|
||||
static void apply_servo_config(void) {
|
||||
pwm_config config = pwm_get_default_config();
|
||||
const uint32_t sys_clock_hz = clock_get_hz(clk_sys);
|
||||
float clock_div = (float)sys_clock_hz / (servo_hz * (servo_wrap + 1));
|
||||
@@ -86,7 +86,7 @@ void servo_init(uint8_t pin) {
|
||||
gpio_set_function(servo_pin, GPIO_FUNC_PWM);
|
||||
servo_slice = pwm_gpio_to_slice_num(servo_pin);
|
||||
servo_chan = pwm_gpio_to_channel(servo_pin);
|
||||
_apply_servo_config();
|
||||
apply_servo_config();
|
||||
servo_initialized = true;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ void servo_set_pulse_us(uint16_t pulse_us) {
|
||||
if (!servo_initialized) return;
|
||||
if (pulse_us < SERVO_DEFAULT_MIN_US) pulse_us = SERVO_DEFAULT_MIN_US;
|
||||
if (pulse_us > SERVO_DEFAULT_MAX_US) pulse_us = SERVO_DEFAULT_MAX_US;
|
||||
uint32_t level = _pulse_us_to_level(pulse_us);
|
||||
uint32_t level = pulse_us_to_level(pulse_us);
|
||||
pwm_set_chan_level(servo_slice, servo_chan, level);
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ static void _uint8_to_str(uint8_t value, char *buf)
|
||||
* @param angle angle in degrees (0-180)
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_angle(uint8_t angle)
|
||||
static void print_angle(uint8_t angle)
|
||||
{
|
||||
char buf[4];
|
||||
_uint8_to_str(angle, buf);
|
||||
@@ -70,12 +70,12 @@ static void _print_angle(uint8_t angle)
|
||||
* @brief Sweep servo angle upward from 0 to 180 degrees.
|
||||
* @retval None
|
||||
*/
|
||||
static void _sweep_up(void)
|
||||
static void sweep_up(void)
|
||||
{
|
||||
for (uint8_t angle = 0; angle <= 180; angle += STEP_DEGREES)
|
||||
{
|
||||
servo_set_angle(angle);
|
||||
_print_angle(angle);
|
||||
print_angle(angle);
|
||||
delay_ms(STEP_DELAY_MS);
|
||||
}
|
||||
}
|
||||
@@ -84,12 +84,12 @@ static void _sweep_up(void)
|
||||
* @brief Sweep servo angle downward from 180 to 0 degrees.
|
||||
* @retval None
|
||||
*/
|
||||
static void _sweep_down(void)
|
||||
static void sweep_down(void)
|
||||
{
|
||||
for (int16_t angle = 180; angle >= 0; angle -= STEP_DEGREES)
|
||||
{
|
||||
servo_set_angle((uint8_t)angle);
|
||||
_print_angle((uint8_t)angle);
|
||||
print_angle((uint8_t)angle);
|
||||
delay_ms(STEP_DELAY_MS);
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ int main(void)
|
||||
uart_puts("Sweeping 0 -> 180 -> 0 degrees\r\n");
|
||||
while (1)
|
||||
{
|
||||
_sweep_up();
|
||||
_sweep_down();
|
||||
sweep_up();
|
||||
sweep_down();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_pad(uint32_t gpio_num)
|
||||
static void gpio_config_pad(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[gpio_num];
|
||||
@@ -43,7 +43,7 @@ static void _gpio_config_pad(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
static void gpio_config_funcsel(uint32_t gpio_num)
|
||||
{
|
||||
uint32_t value;
|
||||
value = IO_BANK0->GPIO[gpio_num].CTRL;
|
||||
@@ -57,16 +57,16 @@ static void _gpio_config_funcsel(uint32_t gpio_num)
|
||||
* @param gpio_num GPIO pin number (0-29)
|
||||
* @retval None
|
||||
*/
|
||||
static void _gpio_enable_output(uint32_t gpio_num)
|
||||
static void gpio_enable_output(uint32_t gpio_num)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = (1U << gpio_num);
|
||||
}
|
||||
|
||||
void gpio_config(uint32_t gpio_num)
|
||||
{
|
||||
_gpio_config_pad(gpio_num);
|
||||
_gpio_config_funcsel(gpio_num);
|
||||
_gpio_enable_output(gpio_num);
|
||||
gpio_config_pad(gpio_num);
|
||||
gpio_config_funcsel(gpio_num);
|
||||
gpio_enable_output(gpio_num);
|
||||
}
|
||||
|
||||
void gpio_set(uint32_t gpio_num)
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
* @brief Clear the PWM reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_clear_reset_bit(void)
|
||||
static void servo_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -73,7 +73,7 @@ static void _servo_clear_reset_bit(void)
|
||||
* @brief Wait until the PWM block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_wait_reset_done(void)
|
||||
static void servo_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_PWM_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ static void _servo_wait_reset_done(void)
|
||||
* @brief Configure GPIO 6 pad and funcsel for PWM output.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_configure_pin(void)
|
||||
static void servo_configure_pin(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[SERVO_PIN];
|
||||
@@ -97,7 +97,7 @@ static void _servo_configure_pin(void)
|
||||
* @brief Set the fractional clock divider for 50 Hz servo frequency.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_set_divider(void)
|
||||
static void servo_set_divider(void)
|
||||
{
|
||||
PWM[SERVO_REG(PWM_CH_DIV_OFFSET)] = SERVO_DIV_VAL;
|
||||
}
|
||||
@@ -106,7 +106,7 @@ static void _servo_set_divider(void)
|
||||
* @brief Set the wrap value and zero the compare register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_set_wrap(void)
|
||||
static void servo_set_wrap(void)
|
||||
{
|
||||
PWM[SERVO_REG(PWM_CH_TOP_OFFSET)] = SERVO_WRAP;
|
||||
PWM[SERVO_REG(PWM_CH_CC_OFFSET)] = 0;
|
||||
@@ -116,7 +116,7 @@ static void _servo_set_wrap(void)
|
||||
* @brief Enable the PWM slice counter.
|
||||
* @retval None
|
||||
*/
|
||||
static void _servo_enable(void)
|
||||
static void servo_enable(void)
|
||||
{
|
||||
PWM[SERVO_REG(PWM_CH_CSR_OFFSET)] = (1U << PWM_CSR_EN_SHIFT);
|
||||
}
|
||||
@@ -126,23 +126,23 @@ static void _servo_enable(void)
|
||||
* @param pulse_us pulse width in microseconds
|
||||
* @retval uint32_t PWM level for the CC register
|
||||
*/
|
||||
static uint32_t _pulse_us_to_level(uint16_t pulse_us)
|
||||
static uint32_t pulse_us_to_level(uint16_t pulse_us)
|
||||
{
|
||||
return ((uint32_t)pulse_us * (SERVO_WRAP + 1)) / 20000U;
|
||||
}
|
||||
|
||||
void servo_release_reset(void)
|
||||
{
|
||||
_servo_clear_reset_bit();
|
||||
_servo_wait_reset_done();
|
||||
servo_clear_reset_bit();
|
||||
servo_wait_reset_done();
|
||||
}
|
||||
|
||||
void servo_init(void)
|
||||
{
|
||||
_servo_configure_pin();
|
||||
_servo_set_divider();
|
||||
_servo_set_wrap();
|
||||
_servo_enable();
|
||||
servo_configure_pin();
|
||||
servo_set_divider();
|
||||
servo_set_wrap();
|
||||
servo_enable();
|
||||
}
|
||||
|
||||
void servo_set_pulse_us(uint16_t pulse_us)
|
||||
@@ -152,7 +152,7 @@ void servo_set_pulse_us(uint16_t pulse_us)
|
||||
pulse_us = SERVO_MIN_US;
|
||||
if (pulse_us > SERVO_MAX_US)
|
||||
pulse_us = SERVO_MAX_US;
|
||||
level = _pulse_us_to_level(pulse_us);
|
||||
level = pulse_us_to_level(pulse_us);
|
||||
PWM[SERVO_REG(PWM_CH_CC_OFFSET)] = level;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
* Samples the ADC channel for voltage in millivolts and reads the
|
||||
* on-chip temperature sensor, then prints both values.
|
||||
*/
|
||||
static void _print_adc_readings(void) {
|
||||
static void print_adc_readings(void) {
|
||||
uint32_t voltage_mv = adc_driver_read_mv();
|
||||
float temp_c = adc_driver_read_temp_celsius();
|
||||
printf("ADC0: %4lu mV | Chip temp: %.1f C\r\n", voltage_mv, temp_c);
|
||||
@@ -64,7 +64,7 @@ int main(void) {
|
||||
adc_driver_init(ADC_GPIO, ADC_CHANNEL);
|
||||
printf("ADC driver initialized: GPIO%d (channel %d)\r\n", ADC_GPIO, ADC_CHANNEL);
|
||||
while (true) {
|
||||
_print_adc_readings();
|
||||
print_adc_readings();
|
||||
sleep_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ static uint8_t active_channel = 0;
|
||||
* @param raw 12-bit ADC conversion result (0 - 4095)
|
||||
* @return uint32_t Equivalent voltage in millivolts (0 - 3300)
|
||||
*/
|
||||
static uint32_t _raw_to_mv(uint16_t raw) {
|
||||
static uint32_t raw_to_mv(uint16_t raw) {
|
||||
return (uint32_t)raw * ADC_VREF_MV / ADC_FULL_SCALE;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ static uint32_t _raw_to_mv(uint16_t raw) {
|
||||
* @param raw 12-bit ADC result from the internal temperature sensor (channel 4)
|
||||
* @return float Die temperature in degrees Celsius
|
||||
*/
|
||||
static float _raw_to_celsius(uint16_t raw) {
|
||||
static float raw_to_celsius(uint16_t raw) {
|
||||
float voltage = (float)raw * 3.3f / (float)ADC_FULL_SCALE;
|
||||
return 27.0f - (voltage - 0.706f) / 0.001721f;
|
||||
}
|
||||
@@ -74,12 +74,12 @@ void adc_driver_init(uint32_t gpio, uint8_t channel) {
|
||||
}
|
||||
|
||||
uint32_t adc_driver_read_mv(void) {
|
||||
return _raw_to_mv(adc_read());
|
||||
return raw_to_mv(adc_read());
|
||||
}
|
||||
|
||||
float adc_driver_read_temp_celsius(void) {
|
||||
adc_select_input(4);
|
||||
float result = _raw_to_celsius(adc_read());
|
||||
float result = raw_to_celsius(adc_read());
|
||||
adc_select_input(active_channel);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
* @param len number of characters to reverse
|
||||
* @retval None
|
||||
*/
|
||||
static void _reverse(char *buf, uint8_t len)
|
||||
static void reverse(char *buf, uint8_t len)
|
||||
{
|
||||
for (uint8_t i = 0; i < len / 2; i++)
|
||||
{
|
||||
@@ -62,7 +62,7 @@ static void _print_uint32(uint32_t val)
|
||||
char buf[11];
|
||||
uint8_t len = 0;
|
||||
do { buf[len++] = (char)('0' + val % 10); val /= 10; } while (val > 0);
|
||||
_reverse(buf, len);
|
||||
reverse(buf, len);
|
||||
buf[len] = '\0';
|
||||
uart_puts(buf);
|
||||
}
|
||||
@@ -72,7 +72,7 @@ static void _print_uint32(uint32_t val)
|
||||
* @param tenths temperature in tenths of degrees Celsius
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_temp(int32_t tenths)
|
||||
static void print_temp(int32_t tenths)
|
||||
{
|
||||
if (tenths < 0) { uart_puts("-"); tenths = -tenths; }
|
||||
_print_uint32((uint32_t)(tenths / 10));
|
||||
@@ -85,14 +85,14 @@ static void _print_temp(int32_t tenths)
|
||||
* @brief Print ADC voltage and chip temperature readings over UART.
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_readings(void)
|
||||
static void print_readings(void)
|
||||
{
|
||||
uint32_t mv = adc_read_mv();
|
||||
int32_t temp = adc_read_temp_tenths();
|
||||
uart_puts("ADC0: ");
|
||||
_print_uint32(mv);
|
||||
uart_puts(" mV | Chip temp: ");
|
||||
_print_temp(temp);
|
||||
print_temp(temp);
|
||||
uart_puts(" C\r\n");
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ int main(void)
|
||||
adc_init();
|
||||
while (1)
|
||||
{
|
||||
_print_readings();
|
||||
print_readings();
|
||||
delay_ms(SAMPLE_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ static uint8_t active_channel = 0;
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void _adc_config_pad(void)
|
||||
static void adc_config_pad(void)
|
||||
{
|
||||
PADS_BANK0->GPIO[ADC_PIN] = PADS_BANK0_DRIVE_4MA;
|
||||
}
|
||||
@@ -48,7 +48,7 @@ static void _adc_config_pad(void)
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
static void _adc_config_gpio(void)
|
||||
static void adc_config_gpio(void)
|
||||
{
|
||||
IO_BANK0->GPIO[ADC_PIN].CTRL = IO_BANK0_CTRL_FUNCSEL_NULL;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ static void _adc_config_gpio(void)
|
||||
* @param ch channel number (0-4)
|
||||
* @retval None
|
||||
*/
|
||||
static void _adc_select_input(uint8_t ch)
|
||||
static void adc_select_input(uint8_t ch)
|
||||
{
|
||||
uint32_t cs = ADC->CS;
|
||||
cs &= ~ADC_CS_AINSEL_MASK;
|
||||
@@ -78,7 +78,7 @@ static void _adc_select_input(uint8_t ch)
|
||||
*
|
||||
* @retval uint16_t raw ADC conversion result (0-4095)
|
||||
*/
|
||||
static uint16_t _adc_read_raw(void)
|
||||
static uint16_t adc_read_raw(void)
|
||||
{
|
||||
uint32_t timeout = ADC_READY_TIMEOUT;
|
||||
ADC->CS |= (1U << ADC_CS_START_ONCE_SHIFT);
|
||||
@@ -102,7 +102,7 @@ static uint16_t _adc_read_raw(void)
|
||||
* @param raw 12-bit ADC conversion result (0-4095)
|
||||
* @retval uint32_t equivalent voltage in millivolts (0-3300)
|
||||
*/
|
||||
static uint32_t _raw_to_mv(uint16_t raw)
|
||||
static uint32_t raw_to_mv(uint16_t raw)
|
||||
{
|
||||
return (uint32_t)raw * ADC_VREF_MV / ADC_FULL_SCALE;
|
||||
}
|
||||
@@ -117,7 +117,7 @@ static uint32_t _raw_to_mv(uint16_t raw)
|
||||
* @param raw 12-bit ADC result from the internal temperature sensor
|
||||
* @retval int32_t die temperature in tenths of degrees Celsius
|
||||
*/
|
||||
static int32_t _raw_to_temp_tenths(uint16_t raw)
|
||||
static int32_t raw_to_temp_tenths(uint16_t raw)
|
||||
{
|
||||
int32_t v_mv10 = (int32_t)((uint32_t)raw * 33000U / ADC_FULL_SCALE);
|
||||
return 270 - (v_mv10 - 7060) * 1000 / 1721;
|
||||
@@ -138,7 +138,7 @@ void adc_release_reset(void)
|
||||
* @brief Enable the ADC block and wait until it is ready.
|
||||
* @retval None
|
||||
*/
|
||||
static void _adc_enable(void)
|
||||
static void adc_enable(void)
|
||||
{
|
||||
ADC->CS = (1U << ADC_CS_EN_SHIFT);
|
||||
uint32_t timeout = ADC_READY_TIMEOUT;
|
||||
@@ -149,22 +149,22 @@ static void _adc_enable(void)
|
||||
|
||||
void adc_init(void)
|
||||
{
|
||||
_adc_config_pad();
|
||||
_adc_config_gpio();
|
||||
_adc_enable();
|
||||
adc_config_pad();
|
||||
adc_config_gpio();
|
||||
adc_enable();
|
||||
active_channel = ADC_CHANNEL;
|
||||
_adc_select_input(active_channel);
|
||||
adc_select_input(active_channel);
|
||||
}
|
||||
|
||||
uint32_t adc_read_mv(void)
|
||||
{
|
||||
return _raw_to_mv(_adc_read_raw());
|
||||
return raw_to_mv(adc_read_raw());
|
||||
}
|
||||
|
||||
int32_t adc_read_temp_tenths(void)
|
||||
{
|
||||
_adc_select_input(ADC_TEMP_CHANNEL);
|
||||
int32_t result = _raw_to_temp_tenths(_adc_read_raw());
|
||||
_adc_select_input(active_channel);
|
||||
adc_select_input(ADC_TEMP_CHANNEL);
|
||||
int32_t result = raw_to_temp_tenths(adc_read_raw());
|
||||
adc_select_input(active_channel);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -62,7 +62,7 @@ bool i2c_driver_probe(uint8_t port, uint8_t addr) {
|
||||
/**
|
||||
* @brief Print the I2C scan table header over UART
|
||||
*/
|
||||
static void _print_scan_header(void) {
|
||||
static void print_scan_header(void) {
|
||||
printf("\r\nI2C bus scan:\r\n");
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\r\n");
|
||||
}
|
||||
@@ -77,7 +77,7 @@ static void _print_scan_header(void) {
|
||||
* @param port I2C port number (0 for i2c0, 1 for i2c1)
|
||||
* @param addr 7-bit I2C address being probed
|
||||
*/
|
||||
static void _print_scan_entry(uint8_t port, uint8_t addr) {
|
||||
static void print_scan_entry(uint8_t port, uint8_t addr) {
|
||||
if (addr % 16 == 0) printf("%02X: ", addr);
|
||||
if (addr < 0x08 || addr > 0x77) printf(" ");
|
||||
else if (i2c_driver_probe(port, addr)) printf("%02X ", addr);
|
||||
@@ -86,7 +86,7 @@ static void _print_scan_entry(uint8_t port, uint8_t addr) {
|
||||
}
|
||||
|
||||
void i2c_driver_scan(uint8_t port) {
|
||||
_print_scan_header();
|
||||
print_scan_header();
|
||||
for (uint8_t addr = 0; addr < 128; addr++)
|
||||
_print_scan_entry(port, addr);
|
||||
print_scan_entry(port, addr);
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ static void _print_hex8(uint8_t val)
|
||||
* @param addr 7-bit I2C address
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_probe_result(uint8_t addr)
|
||||
static void print_probe_result(uint8_t addr)
|
||||
{
|
||||
if (addr < 0x08 || addr > 0x77)
|
||||
{
|
||||
@@ -125,10 +125,10 @@ static void _print_probe_result(uint8_t addr)
|
||||
* @param addr 7-bit I2C address (0x00-0x7F)
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_scan_entry(uint8_t addr)
|
||||
static void print_scan_entry(uint8_t addr)
|
||||
{
|
||||
if (addr % 16 == 0) { _print_hex8(addr); uart_puts(": "); }
|
||||
_print_probe_result(addr);
|
||||
print_probe_result(addr);
|
||||
if (addr % 16 == 15)
|
||||
uart_puts("\r\n");
|
||||
}
|
||||
@@ -157,7 +157,7 @@ void i2c_init(void)
|
||||
* @param addr 7-bit target address
|
||||
* @retval None
|
||||
*/
|
||||
static void _probe_set_target(uint8_t addr)
|
||||
static void probe_set_target(uint8_t addr)
|
||||
{
|
||||
I2C1->ENABLE = 0U;
|
||||
I2C1->TAR = addr;
|
||||
@@ -168,7 +168,7 @@ static void _probe_set_target(uint8_t addr)
|
||||
* @brief Issue a single read command with stop to probe the target.
|
||||
* @retval None
|
||||
*/
|
||||
static void _probe_send_read(void)
|
||||
static void probe_send_read(void)
|
||||
{
|
||||
(void)I2C1->CLR_TX_ABRT;
|
||||
I2C1->DATA_CMD = (1U << I2C_DATA_CMD_CMD_SHIFT) | (1U << I2C_DATA_CMD_STOP_SHIFT);
|
||||
@@ -178,7 +178,7 @@ static void _probe_send_read(void)
|
||||
* @brief Wait for probe response, checking for abort or RX data.
|
||||
* @retval bool true if transaction was aborted
|
||||
*/
|
||||
static bool _probe_wait_response(void)
|
||||
static bool probe_wait_response(void)
|
||||
{
|
||||
uint32_t timeout = I2C_TIMEOUT;
|
||||
while (timeout > 0U)
|
||||
@@ -197,7 +197,7 @@ static bool _probe_wait_response(void)
|
||||
* @param aborted true if the probe was aborted
|
||||
* @retval None
|
||||
*/
|
||||
static void _probe_cleanup(bool aborted)
|
||||
static void probe_cleanup(bool aborted)
|
||||
{
|
||||
if (aborted)
|
||||
(void)I2C1->CLR_TX_ABRT;
|
||||
@@ -208,10 +208,10 @@ static void _probe_cleanup(bool aborted)
|
||||
|
||||
bool i2c_probe(uint8_t addr)
|
||||
{
|
||||
_probe_set_target(addr);
|
||||
_probe_send_read();
|
||||
bool aborted = _probe_wait_response();
|
||||
_probe_cleanup(aborted);
|
||||
probe_set_target(addr);
|
||||
probe_send_read();
|
||||
bool aborted = probe_wait_response();
|
||||
probe_cleanup(aborted);
|
||||
return !aborted;
|
||||
}
|
||||
|
||||
@@ -221,6 +221,6 @@ void i2c_scan(void)
|
||||
uart_puts(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\r\n");
|
||||
for (uint8_t addr = 0; addr < 128; addr++)
|
||||
{
|
||||
_print_scan_entry(addr);
|
||||
print_scan_entry(addr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
/**
|
||||
* @brief Initialize the LCD, display the title, and log over UART
|
||||
*/
|
||||
static void _setup_display(void) {
|
||||
static void setup_display(void) {
|
||||
lcd_init(I2C_PORT, I2C_SDA_PIN, I2C_SCL_PIN, I2C_BAUD_HZ, LCD_I2C_ADDR, 4, 0x08);
|
||||
lcd_set_cursor(0, 0);
|
||||
lcd_puts("Reverse Eng.");
|
||||
@@ -71,7 +71,7 @@ static void _setup_display(void) {
|
||||
*
|
||||
* @param count Pointer to the running counter (post-incremented)
|
||||
*/
|
||||
static void _update_counter(uint32_t *count) {
|
||||
static void update_counter(uint32_t *count) {
|
||||
char buf[17];
|
||||
snprintf(buf, sizeof(buf), "Count: %6lu", (*count)++);
|
||||
lcd_set_cursor(1, 0);
|
||||
@@ -82,8 +82,8 @@ static void _update_counter(uint32_t *count) {
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
_setup_display();
|
||||
setup_display();
|
||||
uint32_t count = 0;
|
||||
while (true)
|
||||
_update_counter(&count);
|
||||
update_counter(&count);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ static uint8_t lcd_backlight_mask = 0x08;
|
||||
*
|
||||
* @param data Output byte to send to the expander
|
||||
*/
|
||||
static void _pcf_write_byte(uint8_t data) {
|
||||
static void pcf_write_byte(uint8_t data) {
|
||||
if (!lcd_i2c) return;
|
||||
i2c_write_blocking(lcd_i2c, lcd_addr, &data, 1, false);
|
||||
}
|
||||
@@ -75,10 +75,10 @@ static void _pcf_write_byte(uint8_t data) {
|
||||
*
|
||||
* @param data Current control/data bus byte (with RS and backlight already set)
|
||||
*/
|
||||
static void _pcf_pulse_enable(uint8_t data) {
|
||||
_pcf_write_byte(data | PIN_EN);
|
||||
static void pcf_pulse_enable(uint8_t data) {
|
||||
pcf_write_byte(data | PIN_EN);
|
||||
sleep_us(1);
|
||||
_pcf_write_byte(data & ~PIN_EN);
|
||||
pcf_write_byte(data & ~PIN_EN);
|
||||
sleep_us(50);
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ static void _lcd_write4(uint8_t nibble, uint8_t mode) {
|
||||
uint8_t data = (nibble & 0x0F) << lcd_nibble_shift;
|
||||
data |= mode ? PIN_RS : 0;
|
||||
data |= lcd_backlight_mask;
|
||||
_pcf_pulse_enable(data);
|
||||
pcf_pulse_enable(data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +101,7 @@ static void _lcd_write4(uint8_t nibble, uint8_t mode) {
|
||||
* @param value Byte to send to the LCD
|
||||
* @param mode 0 for command, non-zero for character data
|
||||
*/
|
||||
static void _lcd_send(uint8_t value, uint8_t mode) {
|
||||
static void lcd_send(uint8_t value, uint8_t mode) {
|
||||
_lcd_write4((value >> 4) & 0x0F, mode);
|
||||
_lcd_write4(value & 0x0F, mode);
|
||||
}
|
||||
@@ -114,7 +114,7 @@ static void _lcd_send(uint8_t value, uint8_t mode) {
|
||||
* @param nibble_shift Bit shift for 4-bit nibbles
|
||||
* @param backlight_mask Backlight control bit mask
|
||||
*/
|
||||
static void _lcd_store_config(uint8_t i2c_port, uint8_t pcf_addr,
|
||||
static void lcd_store_config(uint8_t i2c_port, uint8_t pcf_addr,
|
||||
int nibble_shift, uint8_t backlight_mask) {
|
||||
lcd_i2c = _get_i2c_inst(i2c_port);
|
||||
lcd_addr = pcf_addr;
|
||||
@@ -143,16 +143,16 @@ static void _lcd_hd44780_reset(void) {
|
||||
* cursor hidden, clears the screen, and selects left-to-right entry mode.
|
||||
*/
|
||||
static void _lcd_hd44780_configure(void) {
|
||||
_lcd_send(0x28, 0);
|
||||
_lcd_send(0x0C, 0);
|
||||
_lcd_send(0x01, 0);
|
||||
lcd_send(0x28, 0);
|
||||
lcd_send(0x0C, 0);
|
||||
lcd_send(0x01, 0);
|
||||
sleep_ms(2);
|
||||
_lcd_send(0x06, 0);
|
||||
lcd_send(0x06, 0);
|
||||
}
|
||||
|
||||
void lcd_i2c_init(uint8_t i2c_port, uint8_t pcf_addr, int nibble_shift,
|
||||
uint8_t backlight_mask) {
|
||||
_lcd_store_config(i2c_port, pcf_addr, nibble_shift, backlight_mask);
|
||||
lcd_store_config(i2c_port, pcf_addr, nibble_shift, backlight_mask);
|
||||
_lcd_hd44780_reset();
|
||||
_lcd_hd44780_configure();
|
||||
}
|
||||
@@ -186,17 +186,17 @@ void lcd_init(uint8_t i2c_port, uint32_t sda_pin, uint32_t scl_pin,
|
||||
}
|
||||
|
||||
void lcd_clear(void) {
|
||||
_lcd_send(0x01, 0);
|
||||
lcd_send(0x01, 0);
|
||||
sleep_ms(2);
|
||||
}
|
||||
|
||||
void lcd_set_cursor(int line, int position) {
|
||||
const uint8_t row_offsets[] = {0x00, 0x40};
|
||||
if (line > 1) line = 1;
|
||||
_lcd_send(0x80 | (position + row_offsets[line]), 0);
|
||||
lcd_send(0x80 | (position + row_offsets[line]), 0);
|
||||
}
|
||||
|
||||
void lcd_puts(const char *s) {
|
||||
while (*s)
|
||||
_lcd_send((uint8_t)*s++, 1);
|
||||
lcd_send((uint8_t)*s++, 1);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
* @param buf output buffer
|
||||
* @retval None
|
||||
*/
|
||||
static void _reverse_copy(const char *tmp, int len, char *buf)
|
||||
static void reverse_copy(const char *tmp, int len, char *buf)
|
||||
{
|
||||
for (int j = 0; j < len; j++)
|
||||
buf[j] = tmp[len - 1 - j];
|
||||
@@ -63,7 +63,7 @@ static void _reverse_copy(const char *tmp, int len, char *buf)
|
||||
* @param buf destination buffer (minimum 11 bytes)
|
||||
* @retval None
|
||||
*/
|
||||
static void _uint_to_str(uint32_t val, char *buf)
|
||||
static void uint_to_str(uint32_t val, char *buf)
|
||||
{
|
||||
char tmp[11];
|
||||
int i = 0;
|
||||
@@ -73,7 +73,7 @@ static void _uint_to_str(uint32_t val, char *buf)
|
||||
tmp[i++] = (char)('0' + (val % 10));
|
||||
val /= 10;
|
||||
}
|
||||
_reverse_copy(tmp, i, buf);
|
||||
reverse_copy(tmp, i, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +83,7 @@ static void _uint_to_str(uint32_t val, char *buf)
|
||||
* @param src source string
|
||||
* @retval int new offset past the copied characters
|
||||
*/
|
||||
static int _copy_str(char *dst, int off, const char *src)
|
||||
static int copy_str(char *dst, int off, const char *src)
|
||||
{
|
||||
while (*src)
|
||||
dst[off++] = *src++;
|
||||
@@ -100,12 +100,12 @@ static int _copy_str(char *dst, int off, const char *src)
|
||||
* @param buf destination buffer (minimum 17 bytes)
|
||||
* @retval None
|
||||
*/
|
||||
static void _format_counter(uint32_t count, char *buf)
|
||||
static void format_counter(uint32_t count, char *buf)
|
||||
{
|
||||
char num[11];
|
||||
_uint_to_str(count, num);
|
||||
int i = _copy_str(buf, 0, "Count: ");
|
||||
i = _copy_str(buf, i, num);
|
||||
uint_to_str(count, num);
|
||||
int i = copy_str(buf, 0, "Count: ");
|
||||
i = copy_str(buf, i, num);
|
||||
while (i < 16) buf[i++] = ' ';
|
||||
buf[i] = '\0';
|
||||
}
|
||||
@@ -114,7 +114,7 @@ static void _format_counter(uint32_t count, char *buf)
|
||||
* @brief Initialize clocks, I2C, LCD, UART, and display the static title.
|
||||
* @retval None
|
||||
*/
|
||||
static void _lcd_setup(void)
|
||||
static void lcd_setup(void)
|
||||
{
|
||||
xosc_set_clk_ref();
|
||||
i2c_release_reset();
|
||||
@@ -130,10 +130,10 @@ static void _lcd_setup(void)
|
||||
* @param count current counter value
|
||||
* @retval None
|
||||
*/
|
||||
static void _display_count(uint32_t count)
|
||||
static void display_count(uint32_t count)
|
||||
{
|
||||
char buf[17];
|
||||
_format_counter(count, buf);
|
||||
format_counter(count, buf);
|
||||
lcd_set_cursor(1, 0);
|
||||
lcd_puts(buf);
|
||||
uart_puts(buf);
|
||||
@@ -143,10 +143,10 @@ static void _display_count(uint32_t count)
|
||||
int main(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
_lcd_setup();
|
||||
lcd_setup();
|
||||
while (1)
|
||||
{
|
||||
_display_count(count);
|
||||
display_count(count);
|
||||
count++;
|
||||
delay_ms(COUNT_DELAY_MS);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ static void _i2c_config_timing(void)
|
||||
* @brief Check for TX abort and clear if detected.
|
||||
* @retval bool true if TX abort occurred
|
||||
*/
|
||||
static bool _check_abort(void)
|
||||
static bool check_abort(void)
|
||||
{
|
||||
if (I2C1->RAW_INTR_STAT & I2C_RAW_INTR_TX_ABRT)
|
||||
{
|
||||
@@ -101,7 +101,7 @@ static bool _check_abort(void)
|
||||
* @brief Check for stop condition detected and clear if so.
|
||||
* @retval bool true if stop detected
|
||||
*/
|
||||
static bool _check_stop(void)
|
||||
static bool check_stop(void)
|
||||
{
|
||||
if (I2C1->RAW_INTR_STAT & I2C_RAW_INTR_STOP_DET)
|
||||
{
|
||||
@@ -120,7 +120,7 @@ static void _i2c_wait_done(void)
|
||||
uint32_t timeout = I2C_TIMEOUT;
|
||||
while (timeout > 0U)
|
||||
{
|
||||
if (_check_abort() || _check_stop())
|
||||
if (check_abort() || check_stop())
|
||||
break;
|
||||
timeout--;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
* @param data PCF8574 output byte (RS, backlight, and nibble already set)
|
||||
* @retval None
|
||||
*/
|
||||
static void _lcd_pulse_enable(uint8_t data)
|
||||
static void lcd_pulse_enable(uint8_t data)
|
||||
{
|
||||
i2c_write_byte(data | LCD_PIN_EN);
|
||||
delay_us(1);
|
||||
@@ -54,7 +54,7 @@ static void _lcd_write4(uint8_t nibble, uint8_t mode)
|
||||
uint8_t data = (nibble & 0x0FU) << LCD_NIBBLE_SHIFT;
|
||||
data |= mode ? LCD_PIN_RS : 0U;
|
||||
data |= LCD_BACKLIGHT;
|
||||
_lcd_pulse_enable(data);
|
||||
lcd_pulse_enable(data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ static void _lcd_write4(uint8_t nibble, uint8_t mode)
|
||||
* @param mode 0 for command, non-zero for character data
|
||||
* @retval None
|
||||
*/
|
||||
static void _lcd_send(uint8_t value, uint8_t mode)
|
||||
static void lcd_send(uint8_t value, uint8_t mode)
|
||||
{
|
||||
_lcd_write4((value >> 4) & 0x0FU, mode);
|
||||
_lcd_write4(value & 0x0FU, mode);
|
||||
@@ -100,11 +100,11 @@ static void _lcd_hd44780_reset(void)
|
||||
*/
|
||||
static void _lcd_hd44780_configure(void)
|
||||
{
|
||||
_lcd_send(LCD_CMD_FUNCTION_SET_4BIT, 0);
|
||||
_lcd_send(LCD_CMD_DISPLAY_ON, 0);
|
||||
_lcd_send(LCD_CMD_CLEAR, 0);
|
||||
lcd_send(LCD_CMD_FUNCTION_SET_4BIT, 0);
|
||||
lcd_send(LCD_CMD_DISPLAY_ON, 0);
|
||||
lcd_send(LCD_CMD_CLEAR, 0);
|
||||
delay_ms(2);
|
||||
_lcd_send(LCD_CMD_ENTRY_MODE, 0);
|
||||
lcd_send(LCD_CMD_ENTRY_MODE, 0);
|
||||
}
|
||||
|
||||
void lcd_init(void)
|
||||
@@ -116,18 +116,18 @@ void lcd_init(void)
|
||||
|
||||
void lcd_clear(void)
|
||||
{
|
||||
_lcd_send(LCD_CMD_CLEAR, 0);
|
||||
lcd_send(LCD_CMD_CLEAR, 0);
|
||||
delay_ms(2);
|
||||
}
|
||||
|
||||
void lcd_set_cursor(uint8_t line, uint8_t position)
|
||||
{
|
||||
uint8_t offset = (line == 0U) ? LCD_ROW0_OFFSET : LCD_ROW1_OFFSET;
|
||||
_lcd_send(LCD_CMD_SET_DDRAM | (position + offset), 0);
|
||||
lcd_send(LCD_CMD_SET_DDRAM | (position + offset), 0);
|
||||
}
|
||||
|
||||
void lcd_puts(const char *str)
|
||||
{
|
||||
while (*str)
|
||||
_lcd_send((uint8_t)*str++, 1);
|
||||
lcd_send((uint8_t)*str++, 1);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
* on failure prints a wiring-check message. Waits 2 s before returning
|
||||
* to respect the DHT11 minimum polling interval.
|
||||
*/
|
||||
static void _print_reading(void) {
|
||||
static void print_reading(void) {
|
||||
float humidity = 0.0f;
|
||||
float temperature = 0.0f;
|
||||
if (dht11_read(&humidity, &temperature))
|
||||
@@ -68,5 +68,5 @@ int main(void) {
|
||||
dht11_init(DHT11_GPIO);
|
||||
printf("DHT11 driver initialized on GPIO %d\r\n", DHT11_GPIO);
|
||||
while (true)
|
||||
_print_reading();
|
||||
print_reading();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ static uint dht_pin;
|
||||
* Drives the pin LOW for 18 ms then HIGH for 40 us before switching
|
||||
* the pin to input mode to listen for the sensor response.
|
||||
*/
|
||||
static void _send_start_signal(void) {
|
||||
static void send_start_signal(void) {
|
||||
gpio_set_dir(dht_pin, GPIO_OUT);
|
||||
gpio_put(dht_pin, 0);
|
||||
sleep_ms(18);
|
||||
@@ -58,7 +58,7 @@ static void _send_start_signal(void) {
|
||||
* @param level Logic level to wait through (0 or 1)
|
||||
* @return bool true once the level changed, false on timeout
|
||||
*/
|
||||
static bool _wait_for_level(int level) {
|
||||
static bool wait_for_level(int level) {
|
||||
uint32_t timeout = 10000;
|
||||
while (gpio_get(dht_pin) == level)
|
||||
if (--timeout == 0) return false;
|
||||
@@ -73,10 +73,10 @@ static bool _wait_for_level(int level) {
|
||||
*
|
||||
* @return bool true if the full response was received, false on timeout
|
||||
*/
|
||||
static bool _wait_response(void) {
|
||||
if (!_wait_for_level(1)) return false;
|
||||
if (!_wait_for_level(0)) return false;
|
||||
if (!_wait_for_level(1)) return false;
|
||||
static bool wait_response(void) {
|
||||
if (!wait_for_level(1)) return false;
|
||||
if (!wait_for_level(0)) return false;
|
||||
if (!wait_for_level(1)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -90,10 +90,10 @@ static bool _wait_response(void) {
|
||||
* @param i Bit index (0-39)
|
||||
* @return bool true on success, false on timeout
|
||||
*/
|
||||
static bool _read_bit(uint8_t *data, int i) {
|
||||
if (!_wait_for_level(0)) return false;
|
||||
static bool read_bit(uint8_t *data, int i) {
|
||||
if (!wait_for_level(0)) return false;
|
||||
uint32_t start = time_us_32();
|
||||
if (!_wait_for_level(1)) return false;
|
||||
if (!wait_for_level(1)) return false;
|
||||
uint32_t duration = time_us_32() - start;
|
||||
data[i / 8] <<= 1;
|
||||
if (duration > 40) data[i / 8] |= 1;
|
||||
@@ -108,7 +108,7 @@ static bool _read_bit(uint8_t *data, int i) {
|
||||
*/
|
||||
static bool _read_40_bits(uint8_t *data) {
|
||||
for (int i = 0; i < 40; i++)
|
||||
if (!_read_bit(data, i)) return false;
|
||||
if (!read_bit(data, i)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ static bool _read_40_bits(uint8_t *data) {
|
||||
* @param data 5-byte received data (bytes 0-3 plus checksum in byte 4)
|
||||
* @return bool true if the checksum matches, false otherwise
|
||||
*/
|
||||
static bool _validate_checksum(const uint8_t *data) {
|
||||
static bool validate_checksum(const uint8_t *data) {
|
||||
return data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF);
|
||||
}
|
||||
|
||||
@@ -130,10 +130,10 @@ void dht11_init(uint8_t pin) {
|
||||
|
||||
bool dht11_read(float *humidity, float *temperature) {
|
||||
uint8_t data[5] = {0};
|
||||
_send_start_signal();
|
||||
if (!_wait_response()) return false;
|
||||
send_start_signal();
|
||||
if (!wait_response()) return false;
|
||||
if (!_read_40_bits(data)) return false;
|
||||
if (!_validate_checksum(data)) return false;
|
||||
if (!validate_checksum(data)) return false;
|
||||
*humidity = data[0] + data[1] * 0.1f;
|
||||
*temperature = data[2] + data[3] * 0.1f;
|
||||
return true;
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
* @param buf output buffer (at least 4 bytes)
|
||||
* @retval None
|
||||
*/
|
||||
static void _uint_to_str(uint8_t value, char *buf)
|
||||
static void uint_to_str(uint8_t value, char *buf)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
if (value >= 100)
|
||||
@@ -61,14 +61,14 @@ static void _uint_to_str(uint8_t value, char *buf)
|
||||
* @param temperature temperature in Celsius (integer)
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_reading(uint8_t humidity, uint8_t temperature)
|
||||
static void print_reading(uint8_t humidity, uint8_t temperature)
|
||||
{
|
||||
char buf[4];
|
||||
uart_puts("Humidity: ");
|
||||
_uint_to_str(humidity, buf);
|
||||
uint_to_str(humidity, buf);
|
||||
uart_puts(buf);
|
||||
uart_puts("% Temperature: ");
|
||||
_uint_to_str(temperature, buf);
|
||||
uint_to_str(temperature, buf);
|
||||
uart_puts(buf);
|
||||
uart_puts(" C\r\n");
|
||||
}
|
||||
@@ -77,7 +77,7 @@ static void _print_reading(uint8_t humidity, uint8_t temperature)
|
||||
* @brief Print a read failure message over UART.
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_failure(void)
|
||||
static void print_failure(void)
|
||||
{
|
||||
uart_puts("DHT11 read failed - check wiring on GPIO4\r\n");
|
||||
}
|
||||
@@ -104,14 +104,14 @@ static void _dht11_setup(void)
|
||||
* @brief Read the sensor once and print results or failure.
|
||||
* @retval None
|
||||
*/
|
||||
static void _poll_sensor(void)
|
||||
static void poll_sensor(void)
|
||||
{
|
||||
uint8_t humidity;
|
||||
uint8_t temperature;
|
||||
if (dht11_read(&humidity, &temperature))
|
||||
_print_reading(humidity, temperature);
|
||||
print_reading(humidity, temperature);
|
||||
else
|
||||
_print_failure();
|
||||
print_failure();
|
||||
delay_ms(DHT11_POLL_MS);
|
||||
}
|
||||
|
||||
@@ -119,5 +119,5 @@ int main(void)
|
||||
{
|
||||
_dht11_setup();
|
||||
while (1)
|
||||
_poll_sensor();
|
||||
poll_sensor();
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
* @brief Read the TIMER0 raw low register for a microsecond timestamp.
|
||||
* @retval uint32_t current microsecond count
|
||||
*/
|
||||
static uint32_t _time_us(void)
|
||||
static uint32_t time_us(void)
|
||||
{
|
||||
return TIMER0[TIMER_TIMERAWL_OFFSET];
|
||||
}
|
||||
@@ -43,7 +43,7 @@ static uint32_t _time_us(void)
|
||||
* @brief Set GPIO4 as output via SIO output-enable set register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_output(void)
|
||||
static void set_output(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = DHT11_PIN_MASK;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ static void _set_output(void)
|
||||
* @brief Set GPIO4 as input via SIO output-enable clear register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_input(void)
|
||||
static void set_input(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_CLR_OFFSET] = DHT11_PIN_MASK;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ static void _set_input(void)
|
||||
* @brief Drive GPIO4 low via SIO output clear register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _drive_low(void)
|
||||
static void drive_low(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OUT_CLR_OFFSET] = DHT11_PIN_MASK;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ static void _drive_low(void)
|
||||
* @brief Drive GPIO4 high via SIO output set register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _drive_high(void)
|
||||
static void drive_high(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OUT_SET_OFFSET] = DHT11_PIN_MASK;
|
||||
}
|
||||
@@ -79,7 +79,7 @@ static void _drive_high(void)
|
||||
* @brief Read the current level of GPIO4 from SIO input register.
|
||||
* @retval bool true if pin is high, false if low
|
||||
*/
|
||||
static bool _read_pin(void)
|
||||
static bool read_pin(void)
|
||||
{
|
||||
return (SIO[SIO_GPIO_IN_OFFSET] & DHT11_PIN_MASK) != 0;
|
||||
}
|
||||
@@ -89,10 +89,10 @@ static bool _read_pin(void)
|
||||
* @param level logic level to wait through (true=high, false=low)
|
||||
* @retval bool true once the level changed, false on timeout
|
||||
*/
|
||||
static bool _wait_for_level(bool level)
|
||||
static bool wait_for_level(bool level)
|
||||
{
|
||||
uint32_t count = DHT11_TIMEOUT;
|
||||
while (_read_pin() == level)
|
||||
while (read_pin() == level)
|
||||
if (--count == 0)
|
||||
return false;
|
||||
return true;
|
||||
@@ -102,27 +102,27 @@ static bool _wait_for_level(bool level)
|
||||
* @brief Send the 18 ms low / 40 us high start signal to the DHT11.
|
||||
* @retval None
|
||||
*/
|
||||
static void _send_start_signal(void)
|
||||
static void send_start_signal(void)
|
||||
{
|
||||
_set_output();
|
||||
_drive_low();
|
||||
set_output();
|
||||
drive_low();
|
||||
delay_ms(DHT11_START_LOW_MS);
|
||||
_drive_high();
|
||||
drive_high();
|
||||
delay_us(DHT11_START_HIGH_US);
|
||||
_set_input();
|
||||
set_input();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for the DHT11 response (low-high-low handshake).
|
||||
* @retval bool true if response received, false on timeout
|
||||
*/
|
||||
static bool _wait_response(void)
|
||||
static bool wait_response(void)
|
||||
{
|
||||
if (!_wait_for_level(true))
|
||||
if (!wait_for_level(true))
|
||||
return false;
|
||||
if (!_wait_for_level(false))
|
||||
if (!wait_for_level(false))
|
||||
return false;
|
||||
if (!_wait_for_level(true))
|
||||
if (!wait_for_level(true))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -132,15 +132,15 @@ static bool _wait_response(void)
|
||||
* @param duration_out pointer to store the pulse duration in microseconds
|
||||
* @retval bool true on success, false on timeout
|
||||
*/
|
||||
static bool _measure_high_pulse(uint32_t *duration_out)
|
||||
static bool measure_high_pulse(uint32_t *duration_out)
|
||||
{
|
||||
uint32_t start;
|
||||
if (!_wait_for_level(false))
|
||||
if (!wait_for_level(false))
|
||||
return false;
|
||||
start = _time_us();
|
||||
if (!_wait_for_level(true))
|
||||
start = time_us();
|
||||
if (!wait_for_level(true))
|
||||
return false;
|
||||
*duration_out = _time_us() - start;
|
||||
*duration_out = time_us() - start;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -150,10 +150,10 @@ static bool _measure_high_pulse(uint32_t *duration_out)
|
||||
* @param bit bit index (0-39)
|
||||
* @retval bool true on success, false on timeout
|
||||
*/
|
||||
static bool _read_bit(uint8_t *data, uint8_t bit)
|
||||
static bool read_bit(uint8_t *data, uint8_t bit)
|
||||
{
|
||||
uint32_t duration;
|
||||
if (!_measure_high_pulse(&duration))
|
||||
if (!measure_high_pulse(&duration))
|
||||
return false;
|
||||
data[bit / 8U] <<= 1;
|
||||
if (duration > DHT11_BIT_THRESHOLD_US)
|
||||
@@ -169,7 +169,7 @@ static bool _read_bit(uint8_t *data, uint8_t bit)
|
||||
static bool _read_40_bits(uint8_t *data)
|
||||
{
|
||||
for (uint8_t i = 0; i < DHT11_DATA_BITS; i++)
|
||||
if (!_read_bit(data, i))
|
||||
if (!read_bit(data, i))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -179,7 +179,7 @@ static bool _read_40_bits(uint8_t *data)
|
||||
* @param data 5 bytes of received sensor data
|
||||
* @retval bool true if checksum matches, false otherwise
|
||||
*/
|
||||
static bool _validate_checksum(const uint8_t *data)
|
||||
static bool validate_checksum(const uint8_t *data)
|
||||
{
|
||||
uint8_t sum;
|
||||
sum = data[0] + data[1] + data[2] + data[3];
|
||||
@@ -190,7 +190,7 @@ static bool _validate_checksum(const uint8_t *data)
|
||||
* @brief Clear the TIMER0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_clear_reset(void)
|
||||
static void timer_clear_reset(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -202,7 +202,7 @@ static void _timer_clear_reset(void)
|
||||
* @brief Wait until TIMER0 is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_wait_reset_done(void)
|
||||
static void timer_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_TIMER0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -211,7 +211,7 @@ static void _timer_wait_reset_done(void)
|
||||
* @brief Configure GPIO4 pad: enable input, pull-up, clear isolation.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_pad(void)
|
||||
static void configure_pad(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[DHT11_PIN];
|
||||
@@ -226,15 +226,15 @@ static void _configure_pad(void)
|
||||
* @brief Set GPIO4 funcsel to SIO for software-controlled IO.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_funcsel(void)
|
||||
static void configure_funcsel(void)
|
||||
{
|
||||
IO_BANK0->GPIO[DHT11_PIN].CTRL = IO_BANK0_CTRL_FUNCSEL_SIO;
|
||||
}
|
||||
|
||||
void dht11_timer_release_reset(void)
|
||||
{
|
||||
_timer_clear_reset();
|
||||
_timer_wait_reset_done();
|
||||
timer_clear_reset();
|
||||
timer_wait_reset_done();
|
||||
}
|
||||
|
||||
void dht11_timer_start_tick(void)
|
||||
@@ -245,9 +245,9 @@ void dht11_timer_start_tick(void)
|
||||
|
||||
void dht11_init(void)
|
||||
{
|
||||
_configure_pad();
|
||||
_configure_funcsel();
|
||||
_set_input();
|
||||
configure_pad();
|
||||
configure_funcsel();
|
||||
set_input();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,20 +255,20 @@ void dht11_init(void)
|
||||
* @param data 5-byte output array for sensor data
|
||||
* @retval bool true on success, false on timeout or checksum error
|
||||
*/
|
||||
static bool _acquire_data(uint8_t *data)
|
||||
static bool acquire_data(uint8_t *data)
|
||||
{
|
||||
_send_start_signal();
|
||||
if (!_wait_response())
|
||||
send_start_signal();
|
||||
if (!wait_response())
|
||||
return false;
|
||||
if (!_read_40_bits(data))
|
||||
return false;
|
||||
return _validate_checksum(data);
|
||||
return validate_checksum(data);
|
||||
}
|
||||
|
||||
bool dht11_read(uint8_t *humidity, uint8_t *temperature)
|
||||
{
|
||||
uint8_t data[DHT11_DATA_BYTES] = {0};
|
||||
if (!_acquire_data(data))
|
||||
if (!acquire_data(data))
|
||||
return false;
|
||||
*humidity = data[0];
|
||||
*temperature = data[2];
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
/**
|
||||
* @brief Poll for an NEC frame and print the command if received
|
||||
*/
|
||||
static void _poll_and_print(void) {
|
||||
static void poll_and_print(void) {
|
||||
int command = ir_getkey();
|
||||
if (command >= 0)
|
||||
printf("NEC command: 0x%02X (%d)\r\n", command, command);
|
||||
@@ -61,5 +61,5 @@ int main(void) {
|
||||
printf("NEC IR driver initialized on GPIO %d\r\n", IR_GPIO);
|
||||
printf("Press a button on your NEC remote...\r\n");
|
||||
while (true)
|
||||
_poll_and_print();
|
||||
poll_and_print();
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ static unsigned int ir_pin = 0;
|
||||
* @param timeout_us Maximum wait in microseconds
|
||||
* @return int64_t Elapsed microseconds, or -1 on timeout
|
||||
*/
|
||||
static int64_t _wait_for_level(unsigned int gpio, bool level, uint32_t timeout_us) {
|
||||
static int64_t wait_for_level(unsigned int gpio, bool level, uint32_t timeout_us) {
|
||||
absolute_time_t start = get_absolute_time();
|
||||
while (gpio_get(gpio) != level) {
|
||||
if (absolute_time_diff_us(start, get_absolute_time()) > (int64_t)timeout_us)
|
||||
@@ -60,11 +60,11 @@ static int64_t _wait_for_level(unsigned int gpio, bool level, uint32_t timeout_u
|
||||
*
|
||||
* @return bool true if a valid leader was detected, false on timeout
|
||||
*/
|
||||
static bool _wait_leader(void) {
|
||||
if (_wait_for_level(ir_pin, 0, 150000) < 0) return false;
|
||||
int64_t t = _wait_for_level(ir_pin, 1, 12000);
|
||||
static bool wait_leader(void) {
|
||||
if (wait_for_level(ir_pin, 0, 150000) < 0) return false;
|
||||
int64_t t = wait_for_level(ir_pin, 1, 12000);
|
||||
if (t < 8000 || t > 10000) return false;
|
||||
t = _wait_for_level(ir_pin, 0, 7000);
|
||||
t = wait_for_level(ir_pin, 0, 7000);
|
||||
if (t < 3500 || t > 5000) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -79,9 +79,9 @@ static bool _wait_leader(void) {
|
||||
* @param i Bit index (0-31)
|
||||
* @return bool true on success, false on timeout or protocol error
|
||||
*/
|
||||
static bool _read_nec_bit(uint8_t *data, int i) {
|
||||
if (_wait_for_level(ir_pin, 1, 1000) < 0) return false;
|
||||
int64_t t = _wait_for_level(ir_pin, 0, 2500);
|
||||
static bool read_nec_bit(uint8_t *data, int i) {
|
||||
if (wait_for_level(ir_pin, 1, 1000) < 0) return false;
|
||||
int64_t t = wait_for_level(ir_pin, 0, 2500);
|
||||
if (t < 200) return false;
|
||||
int byte_idx = i / 8;
|
||||
int bit_idx = i % 8;
|
||||
@@ -97,7 +97,7 @@ static bool _read_nec_bit(uint8_t *data, int i) {
|
||||
*/
|
||||
static bool _read_32_bits(uint8_t *data) {
|
||||
for (int i = 0; i < 32; ++i)
|
||||
if (!_read_nec_bit(data, i)) return false;
|
||||
if (!read_nec_bit(data, i)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ static bool _read_32_bits(uint8_t *data) {
|
||||
* @param data 4-byte NEC frame (addr, ~addr, cmd, ~cmd)
|
||||
* @return int Command byte (0-255) on success, -1 on validation failure
|
||||
*/
|
||||
static int _validate_nec_frame(const uint8_t *data) {
|
||||
static int validate_nec_frame(const uint8_t *data) {
|
||||
if ((uint8_t)(data[0] + data[1]) == 0xFF && (uint8_t)(data[2] + data[3]) == 0xFF)
|
||||
return data[2];
|
||||
return -1;
|
||||
@@ -123,8 +123,8 @@ void ir_init(uint8_t pin) {
|
||||
}
|
||||
|
||||
int ir_getkey(void) {
|
||||
if (!_wait_leader()) return -1;
|
||||
if (!wait_leader()) return -1;
|
||||
uint8_t data[4] = {0, 0, 0, 0};
|
||||
if (!_read_32_bits(data)) return -1;
|
||||
return _validate_nec_frame(data);
|
||||
return validate_nec_frame(data);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ static const char _hex_lut[16] = "0123456789ABCDEF";
|
||||
* @param value byte to print
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_hex(uint8_t value)
|
||||
static void print_hex(uint8_t value)
|
||||
{
|
||||
char buf[3];
|
||||
buf[0] = _hex_lut[value >> 4];
|
||||
@@ -72,7 +72,7 @@ static void _uint8_to_str(uint8_t value, char *buf)
|
||||
* @param value byte to print (0-255)
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_dec(uint8_t value)
|
||||
static void print_dec(uint8_t value)
|
||||
{
|
||||
char buf[4];
|
||||
_uint8_to_str(value, buf);
|
||||
@@ -84,12 +84,12 @@ static void _print_dec(uint8_t value)
|
||||
* @param command decoded command byte
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_command(uint8_t command)
|
||||
static void print_command(uint8_t command)
|
||||
{
|
||||
uart_puts("NEC command: 0x");
|
||||
_print_hex(command);
|
||||
print_hex(command);
|
||||
uart_puts(" (");
|
||||
_print_dec(command);
|
||||
print_dec(command);
|
||||
uart_puts(")\r\n");
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ static void _print_command(uint8_t command)
|
||||
* @brief Initialize clocks, timer, IR receiver, and announce over UART.
|
||||
* @retval None
|
||||
*/
|
||||
static void _ir_setup(void)
|
||||
static void ir_setup(void)
|
||||
{
|
||||
xosc_set_clk_ref();
|
||||
ir_timer_release_reset();
|
||||
@@ -110,11 +110,11 @@ static void _ir_setup(void)
|
||||
int main(void)
|
||||
{
|
||||
int command;
|
||||
_ir_setup();
|
||||
ir_setup();
|
||||
while (1)
|
||||
{
|
||||
command = ir_getkey();
|
||||
if (command >= 0)
|
||||
_print_command((uint8_t)command);
|
||||
print_command((uint8_t)command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
* @brief Read the TIMER0 raw low register for a microsecond timestamp.
|
||||
* @retval uint32_t current microsecond count
|
||||
*/
|
||||
static uint32_t _time_us(void)
|
||||
static uint32_t time_us(void)
|
||||
{
|
||||
return TIMER0[TIMER_TIMERAWL_OFFSET];
|
||||
}
|
||||
@@ -42,7 +42,7 @@ static uint32_t _time_us(void)
|
||||
* @brief Read the current level of GPIO5 from SIO input register.
|
||||
* @retval bool true if pin is high, false if low
|
||||
*/
|
||||
static bool _read_pin(void)
|
||||
static bool read_pin(void)
|
||||
{
|
||||
return (SIO[SIO_GPIO_IN_OFFSET] & IR_PIN_MASK) != 0;
|
||||
}
|
||||
@@ -53,24 +53,24 @@ static bool _read_pin(void)
|
||||
* @param timeout_us maximum wait in microseconds
|
||||
* @retval int32_t elapsed microseconds, or -1 on timeout
|
||||
*/
|
||||
static int32_t _wait_for_level(bool level, uint32_t timeout_us)
|
||||
static int32_t wait_for_level(bool level, uint32_t timeout_us)
|
||||
{
|
||||
uint32_t start = _time_us();
|
||||
while (_read_pin() != level)
|
||||
uint32_t start = time_us();
|
||||
while (read_pin() != level)
|
||||
{
|
||||
if ((_time_us() - start) > timeout_us)
|
||||
if ((time_us() - start) > timeout_us)
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)(_time_us() - start);
|
||||
return (int32_t)(time_us() - start);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate the 9 ms leader mark pulse duration.
|
||||
* @retval bool true if within expected range, false on timeout or invalid
|
||||
*/
|
||||
static bool _validate_leader_mark(void)
|
||||
static bool validate_leader_mark(void)
|
||||
{
|
||||
int32_t t = _wait_for_level(true, NEC_LEADER_MARK_TIMEOUT_US);
|
||||
int32_t t = wait_for_level(true, NEC_LEADER_MARK_TIMEOUT_US);
|
||||
if (t < (int32_t)NEC_LEADER_MARK_MIN_US)
|
||||
return false;
|
||||
return t <= (int32_t)NEC_LEADER_MARK_MAX_US;
|
||||
@@ -80,9 +80,9 @@ static bool _validate_leader_mark(void)
|
||||
* @brief Validate the 4.5 ms leader space duration.
|
||||
* @retval bool true if within expected range, false on timeout or invalid
|
||||
*/
|
||||
static bool _validate_leader_space(void)
|
||||
static bool validate_leader_space(void)
|
||||
{
|
||||
int32_t t = _wait_for_level(false, NEC_LEADER_SPACE_TIMEOUT_US);
|
||||
int32_t t = wait_for_level(false, NEC_LEADER_SPACE_TIMEOUT_US);
|
||||
if (t < (int32_t)NEC_LEADER_SPACE_MIN_US)
|
||||
return false;
|
||||
return t <= (int32_t)NEC_LEADER_SPACE_MAX_US;
|
||||
@@ -92,13 +92,13 @@ static bool _validate_leader_space(void)
|
||||
* @brief Wait for the NEC 9 ms leader pulse and 4.5 ms space.
|
||||
* @retval bool true if valid leader detected, false on timeout
|
||||
*/
|
||||
static bool _wait_leader(void)
|
||||
static bool wait_leader(void)
|
||||
{
|
||||
if (_wait_for_level(false, NEC_LEADER_WAIT_US) < 0)
|
||||
if (wait_for_level(false, NEC_LEADER_WAIT_US) < 0)
|
||||
return false;
|
||||
if (!_validate_leader_mark())
|
||||
if (!validate_leader_mark())
|
||||
return false;
|
||||
return _validate_leader_space();
|
||||
return validate_leader_space();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,11 +106,11 @@ static bool _wait_leader(void)
|
||||
* @param duration_out pointer to store the space duration
|
||||
* @retval bool true on success, false on timeout or invalid
|
||||
*/
|
||||
static bool _measure_bit_space(int32_t *duration_out)
|
||||
static bool measure_bit_space(int32_t *duration_out)
|
||||
{
|
||||
if (_wait_for_level(true, NEC_BIT_MARK_TIMEOUT_US) < 0)
|
||||
if (wait_for_level(true, NEC_BIT_MARK_TIMEOUT_US) < 0)
|
||||
return false;
|
||||
*duration_out = _wait_for_level(false, NEC_BIT_SPACE_TIMEOUT_US);
|
||||
*duration_out = wait_for_level(false, NEC_BIT_SPACE_TIMEOUT_US);
|
||||
return *duration_out >= (int32_t)NEC_BIT_SPACE_MIN_US;
|
||||
}
|
||||
|
||||
@@ -120,10 +120,10 @@ static bool _measure_bit_space(int32_t *duration_out)
|
||||
* @param bit bit index (0-31)
|
||||
* @retval bool true on success, false on timeout
|
||||
*/
|
||||
static bool _read_nec_bit(uint8_t *data, uint8_t bit)
|
||||
static bool read_nec_bit(uint8_t *data, uint8_t bit)
|
||||
{
|
||||
int32_t t;
|
||||
if (!_measure_bit_space(&t))
|
||||
if (!measure_bit_space(&t))
|
||||
return false;
|
||||
if (t > (int32_t)NEC_BIT_ONE_THRESHOLD_US)
|
||||
data[bit / 8U] |= (1U << (bit % 8U));
|
||||
@@ -138,7 +138,7 @@ static bool _read_nec_bit(uint8_t *data, uint8_t bit)
|
||||
static bool _read_32_bits(uint8_t *data)
|
||||
{
|
||||
for (uint8_t i = 0; i < NEC_DATA_BITS; i++)
|
||||
if (!_read_nec_bit(data, i))
|
||||
if (!read_nec_bit(data, i))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -148,7 +148,7 @@ static bool _read_32_bits(uint8_t *data)
|
||||
* @param data 4-byte NEC frame (addr, ~addr, cmd, ~cmd)
|
||||
* @retval int command byte (0-255) on success, -1 on failure
|
||||
*/
|
||||
static int _validate_nec_frame(const uint8_t *data)
|
||||
static int validate_nec_frame(const uint8_t *data)
|
||||
{
|
||||
uint8_t addr_check = (uint8_t)(data[0] + data[1]);
|
||||
uint8_t cmd_check = (uint8_t)(data[2] + data[3]);
|
||||
@@ -161,7 +161,7 @@ static int _validate_nec_frame(const uint8_t *data)
|
||||
* @brief Clear the TIMER0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_clear_reset(void)
|
||||
static void timer_clear_reset(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -173,7 +173,7 @@ static void _timer_clear_reset(void)
|
||||
* @brief Wait until TIMER0 is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_wait_reset_done(void)
|
||||
static void timer_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_TIMER0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -182,7 +182,7 @@ static void _timer_wait_reset_done(void)
|
||||
* @brief Configure GPIO5 pad: enable input, pull-up, clear isolation.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_pad(void)
|
||||
static void configure_pad(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[IR_PIN];
|
||||
@@ -198,7 +198,7 @@ static void _configure_pad(void)
|
||||
* @brief Set GPIO5 funcsel to SIO for software-controlled IO.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_funcsel(void)
|
||||
static void configure_funcsel(void)
|
||||
{
|
||||
IO_BANK0->GPIO[IR_PIN].CTRL = IO_BANK0_CTRL_FUNCSEL_SIO;
|
||||
}
|
||||
@@ -207,15 +207,15 @@ static void _configure_funcsel(void)
|
||||
* @brief Set GPIO5 as input via SIO output-enable clear register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_input(void)
|
||||
static void set_input(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OE_CLR_OFFSET] = IR_PIN_MASK;
|
||||
}
|
||||
|
||||
void ir_timer_release_reset(void)
|
||||
{
|
||||
_timer_clear_reset();
|
||||
_timer_wait_reset_done();
|
||||
timer_clear_reset();
|
||||
timer_wait_reset_done();
|
||||
}
|
||||
|
||||
void ir_timer_start_tick(void)
|
||||
@@ -226,19 +226,19 @@ void ir_timer_start_tick(void)
|
||||
|
||||
void ir_init(void)
|
||||
{
|
||||
_configure_pad();
|
||||
_configure_funcsel();
|
||||
_set_input();
|
||||
configure_pad();
|
||||
configure_funcsel();
|
||||
set_input();
|
||||
}
|
||||
|
||||
int ir_getkey(void)
|
||||
{
|
||||
uint8_t data[NEC_DATA_BYTES] = {0};
|
||||
if (!_wait_leader())
|
||||
if (!wait_leader())
|
||||
return -1;
|
||||
if (!_read_32_bits(data))
|
||||
return -1;
|
||||
int result = _validate_nec_frame(data);
|
||||
int result = validate_nec_frame(data);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
return result;
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
* @param rx_buf Receive buffer (cleared after printing)
|
||||
* @param len Number of bytes to transfer
|
||||
*/
|
||||
static void _loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, size_t len) {
|
||||
static void loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, size_t len) {
|
||||
spi_driver_cs_select(PIN_CS);
|
||||
spi_driver_transfer(SPI_PORT, tx_buf, rx_buf, len);
|
||||
spi_driver_cs_deselect(PIN_CS);
|
||||
@@ -82,5 +82,5 @@ int main(void) {
|
||||
const uint8_t tx_buf[] = "SPI loopback OK";
|
||||
uint8_t rx_buf[sizeof(tx_buf)] = {0};
|
||||
while (true)
|
||||
_loopback_transfer(tx_buf, rx_buf, sizeof(tx_buf));
|
||||
loopback_transfer(tx_buf, rx_buf, sizeof(tx_buf));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
* @param port SPI port number (0 for spi0, 1 for spi1)
|
||||
* @return spi_inst_t* Pointer to the corresponding SPI hardware instance
|
||||
*/
|
||||
static spi_inst_t *_get_spi_inst(uint8_t port) {
|
||||
static spi_inst_t *get_spi_inst(uint8_t port) {
|
||||
return port == 0 ? spi0 : spi1;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ static spi_inst_t *_get_spi_inst(uint8_t port) {
|
||||
* @param miso GPIO pin for MISO
|
||||
* @param sck GPIO pin for SCK
|
||||
*/
|
||||
static void _setup_spi_pins(uint32_t mosi, uint32_t miso, uint32_t sck) {
|
||||
static void setup_spi_pins(uint32_t mosi, uint32_t miso, uint32_t sck) {
|
||||
gpio_set_function(mosi, GPIO_FUNC_SPI);
|
||||
gpio_set_function(miso, GPIO_FUNC_SPI);
|
||||
gpio_set_function(sck, GPIO_FUNC_SPI);
|
||||
@@ -56,9 +56,9 @@ static void _setup_spi_pins(uint32_t mosi, uint32_t miso, uint32_t sck) {
|
||||
|
||||
void spi_driver_init(uint8_t port, uint32_t mosi, uint32_t miso,
|
||||
uint32_t sck, uint32_t cs, uint32_t baud_hz) {
|
||||
spi_inst_t *spi = _get_spi_inst(port);
|
||||
spi_inst_t *spi = get_spi_inst(port);
|
||||
spi_init(spi, baud_hz);
|
||||
_setup_spi_pins(mosi, miso, sck);
|
||||
setup_spi_pins(mosi, miso, sck);
|
||||
gpio_init(cs);
|
||||
gpio_set_dir(cs, GPIO_OUT);
|
||||
gpio_put(cs, 1);
|
||||
@@ -74,6 +74,6 @@ void spi_driver_cs_deselect(uint32_t cs) {
|
||||
|
||||
void spi_driver_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx,
|
||||
uint32_t len) {
|
||||
spi_inst_t *spi = _get_spi_inst(port);
|
||||
spi_inst_t *spi = get_spi_inst(port);
|
||||
spi_write_read_blocking(spi, tx, rx, (size_t)len);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ static const char _hex_lut[16] = "0123456789ABCDEF";
|
||||
* @param value byte to print
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_hex(uint8_t value)
|
||||
static void print_hex(uint8_t value)
|
||||
{
|
||||
char buf[3];
|
||||
buf[0] = _hex_lut[value >> 4];
|
||||
@@ -59,12 +59,12 @@ static void _print_hex(uint8_t value)
|
||||
* @param len number of bytes in buffer
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_buffer(const char *label, const uint8_t *buf, uint32_t len)
|
||||
static void print_buffer(const char *label, const uint8_t *buf, uint32_t len)
|
||||
{
|
||||
uart_puts(label);
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
{
|
||||
_print_hex(buf[i]);
|
||||
print_hex(buf[i]);
|
||||
if (i + 1 < len)
|
||||
uart_putchar(' ');
|
||||
}
|
||||
@@ -78,13 +78,13 @@ static void _print_buffer(const char *label, const uint8_t *buf, uint32_t len)
|
||||
* @param len number of bytes to transfer
|
||||
* @retval None
|
||||
*/
|
||||
static void _loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t len)
|
||||
static void loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t len)
|
||||
{
|
||||
spi_cs_select();
|
||||
spi_transfer(tx_buf, rx_buf, len);
|
||||
spi_cs_deselect();
|
||||
_print_buffer("TX: ", tx_buf, len);
|
||||
_print_buffer("RX: ", rx_buf, len);
|
||||
print_buffer("TX: ", tx_buf, len);
|
||||
print_buffer("RX: ", rx_buf, len);
|
||||
uart_puts("\r\n");
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ static void _loopback_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t
|
||||
* @param len number of bytes to clear
|
||||
* @retval None
|
||||
*/
|
||||
static void _clear_buffer(uint8_t *buf, uint32_t len)
|
||||
static void clear_buffer(uint8_t *buf, uint32_t len)
|
||||
{
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
buf[i] = 0;
|
||||
@@ -104,7 +104,7 @@ static void _clear_buffer(uint8_t *buf, uint32_t len)
|
||||
* @brief Initialize clocks, SPI peripheral, and announce over UART.
|
||||
* @retval None
|
||||
*/
|
||||
static void _spi_setup(void)
|
||||
static void spi_setup(void)
|
||||
{
|
||||
xosc_set_clk_ref();
|
||||
spi_release_reset();
|
||||
@@ -119,11 +119,11 @@ int main(void)
|
||||
/** @brief Buffer length for SPI loopback transfer. */
|
||||
static const uint32_t len = sizeof(tx);
|
||||
uint8_t rx[sizeof(tx)] = {0};
|
||||
_spi_setup();
|
||||
spi_setup();
|
||||
while (1)
|
||||
{
|
||||
_loopback_transfer(tx, rx, len);
|
||||
_clear_buffer(rx, len);
|
||||
loopback_transfer(tx, rx, len);
|
||||
clear_buffer(rx, len);
|
||||
delay_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
* @brief Clear the SPI0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _spi_clear_reset(void)
|
||||
static void spi_clear_reset(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -45,7 +45,7 @@ static void _spi_clear_reset(void)
|
||||
* @brief Wait until SPI0 is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _spi_wait_reset_done(void)
|
||||
static void spi_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_SPI0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ static void _spi_wait_reset_done(void)
|
||||
* @param pin GPIO pin number to configure
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_spi_pad(uint8_t pin)
|
||||
static void configure_spi_pad(uint8_t pin)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[pin];
|
||||
@@ -70,7 +70,7 @@ static void _configure_spi_pad(uint8_t pin)
|
||||
* @param pin GPIO pin number to configure
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_cs_pad(uint8_t pin)
|
||||
static void configure_cs_pad(uint8_t pin)
|
||||
{
|
||||
uint32_t value;
|
||||
value = PADS_BANK0->GPIO[pin];
|
||||
@@ -86,7 +86,7 @@ static void _configure_cs_pad(uint8_t pin)
|
||||
* @param pin GPIO pin number
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_funcsel_spi(uint8_t pin)
|
||||
static void set_funcsel_spi(uint8_t pin)
|
||||
{
|
||||
IO_BANK0->GPIO[pin].CTRL = IO_BANK0_CTRL_FUNCSEL_SPI;
|
||||
}
|
||||
@@ -96,7 +96,7 @@ static void _set_funcsel_spi(uint8_t pin)
|
||||
* @param pin GPIO pin number
|
||||
* @retval None
|
||||
*/
|
||||
static void _set_funcsel_sio(uint8_t pin)
|
||||
static void set_funcsel_sio(uint8_t pin)
|
||||
{
|
||||
IO_BANK0->GPIO[pin].CTRL = IO_BANK0_CTRL_FUNCSEL_SIO;
|
||||
}
|
||||
@@ -105,7 +105,7 @@ static void _set_funcsel_sio(uint8_t pin)
|
||||
* @brief Configure CS pin as output, initially deasserted (high).
|
||||
* @retval None
|
||||
*/
|
||||
static void _cs_init(void)
|
||||
static void cs_init(void)
|
||||
{
|
||||
SIO[SIO_GPIO_OUT_SET_OFFSET] = CS_PIN_MASK;
|
||||
SIO[SIO_GPIO_OE_SET_OFFSET] = CS_PIN_MASK;
|
||||
@@ -127,7 +127,7 @@ static void _configure_cr0(void)
|
||||
* @brief Configure SSPCPSR clock prescaler for 1 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_prescaler(void)
|
||||
static void configure_prescaler(void)
|
||||
{
|
||||
SPI0->SSPCPSR = SPI_CPSDVSR_1MHZ;
|
||||
}
|
||||
@@ -136,7 +136,7 @@ static void _configure_prescaler(void)
|
||||
* @brief Enable the SPI0 peripheral (SSE=1 in SSPCR1).
|
||||
* @retval None
|
||||
*/
|
||||
static void _enable_spi(void)
|
||||
static void enable_spi(void)
|
||||
{
|
||||
SPI0->SSPCR1 = (1U << SPI_SSPCR1_SSE_SHIFT);
|
||||
}
|
||||
@@ -145,7 +145,7 @@ static void _enable_spi(void)
|
||||
* @brief Wait until the SPI transmit FIFO has space.
|
||||
* @retval None
|
||||
*/
|
||||
static void _wait_tx_not_full(void)
|
||||
static void wait_tx_not_full(void)
|
||||
{
|
||||
while ((SPI0->SSPSR & SPI_SSPSR_TNF_MASK) == 0) {}
|
||||
}
|
||||
@@ -154,49 +154,49 @@ static void _wait_tx_not_full(void)
|
||||
* @brief Wait until the SPI receive FIFO has data.
|
||||
* @retval None
|
||||
*/
|
||||
static void _wait_rx_not_empty(void)
|
||||
static void wait_rx_not_empty(void)
|
||||
{
|
||||
while ((SPI0->SSPSR & SPI_SSPSR_RNE_MASK) == 0) {}
|
||||
}
|
||||
|
||||
void spi_release_reset(void)
|
||||
{
|
||||
_spi_clear_reset();
|
||||
_spi_wait_reset_done();
|
||||
spi_clear_reset();
|
||||
spi_wait_reset_done();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure all SPI and CS GPIO pads.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_all_pads(void)
|
||||
static void configure_all_pads(void)
|
||||
{
|
||||
_configure_spi_pad(SPI_MOSI_PIN);
|
||||
_configure_spi_pad(SPI_MISO_PIN);
|
||||
_configure_spi_pad(SPI_SCK_PIN);
|
||||
_configure_cs_pad(SPI_CS_PIN);
|
||||
configure_spi_pad(SPI_MOSI_PIN);
|
||||
configure_spi_pad(SPI_MISO_PIN);
|
||||
configure_spi_pad(SPI_SCK_PIN);
|
||||
configure_cs_pad(SPI_CS_PIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assign SPI and SIO alternate functions to all GPIO pins.
|
||||
* @retval None
|
||||
*/
|
||||
static void _configure_all_funcsel(void)
|
||||
static void configure_all_funcsel(void)
|
||||
{
|
||||
_set_funcsel_spi(SPI_MOSI_PIN);
|
||||
_set_funcsel_spi(SPI_MISO_PIN);
|
||||
_set_funcsel_spi(SPI_SCK_PIN);
|
||||
_set_funcsel_sio(SPI_CS_PIN);
|
||||
set_funcsel_spi(SPI_MOSI_PIN);
|
||||
set_funcsel_spi(SPI_MISO_PIN);
|
||||
set_funcsel_spi(SPI_SCK_PIN);
|
||||
set_funcsel_sio(SPI_CS_PIN);
|
||||
}
|
||||
|
||||
void spi_init(void)
|
||||
{
|
||||
_configure_all_pads();
|
||||
_configure_all_funcsel();
|
||||
_cs_init();
|
||||
configure_all_pads();
|
||||
configure_all_funcsel();
|
||||
cs_init();
|
||||
_configure_cr0();
|
||||
_configure_prescaler();
|
||||
_enable_spi();
|
||||
configure_prescaler();
|
||||
enable_spi();
|
||||
}
|
||||
|
||||
void spi_cs_select(void)
|
||||
@@ -213,9 +213,9 @@ void spi_transfer(const uint8_t *tx, uint8_t *rx, uint32_t len)
|
||||
{
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
{
|
||||
_wait_tx_not_full();
|
||||
wait_tx_not_full();
|
||||
SPI0->SSPDR = tx[i];
|
||||
_wait_rx_not_empty();
|
||||
wait_rx_not_empty();
|
||||
rx[i] = (uint8_t)SPI0->SSPDR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -59,7 +59,7 @@ static void _core1_main(void) {
|
||||
*
|
||||
* @param counter Pointer to the running counter (post-incremented)
|
||||
*/
|
||||
static void _send_and_print(uint32_t *counter) {
|
||||
static void send_and_print(uint32_t *counter) {
|
||||
multicore_driver_push(*counter);
|
||||
uint32_t response = multicore_driver_pop();
|
||||
printf("core0 sent: %lu, core1 returned: %lu\r\n",
|
||||
@@ -73,5 +73,5 @@ int main(void) {
|
||||
multicore_driver_launch(_core1_main);
|
||||
uint32_t counter = 0;
|
||||
while (true)
|
||||
_send_and_print(&counter);
|
||||
send_and_print(&counter);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
* @param tmp output buffer for reversed digits (min 11 bytes)
|
||||
* @retval int number of digits written
|
||||
*/
|
||||
static int _extract_digits(uint32_t value, char *tmp)
|
||||
static int extract_digits(uint32_t value, char *tmp)
|
||||
{
|
||||
int i = 0;
|
||||
if (value == 0)
|
||||
@@ -57,7 +57,7 @@ static int _extract_digits(uint32_t value, char *tmp)
|
||||
* @param buf destination buffer (must hold len + 1 bytes)
|
||||
* @retval None
|
||||
*/
|
||||
static void _reverse_copy(const char *tmp, int len, char *buf)
|
||||
static void reverse_copy(const char *tmp, int len, char *buf)
|
||||
{
|
||||
int j = 0;
|
||||
while (len > 0)
|
||||
@@ -71,11 +71,11 @@ static void _reverse_copy(const char *tmp, int len, char *buf)
|
||||
* @param buf output buffer (min 12 bytes)
|
||||
* @retval None
|
||||
*/
|
||||
static void _uint_to_str(uint32_t value, char *buf)
|
||||
static void uint_to_str(uint32_t value, char *buf)
|
||||
{
|
||||
char tmp[11];
|
||||
int len = _extract_digits(value, tmp);
|
||||
_reverse_copy(tmp, len, buf);
|
||||
int len = extract_digits(value, tmp);
|
||||
reverse_copy(tmp, len, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,11 +84,11 @@ static void _uint_to_str(uint32_t value, char *buf)
|
||||
* @param value unsigned integer to print
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_labeled_value(const char *label, uint32_t value)
|
||||
static void print_labeled_value(const char *label, uint32_t value)
|
||||
{
|
||||
char buf[12];
|
||||
uart_puts(label);
|
||||
_uint_to_str(value, buf);
|
||||
uint_to_str(value, buf);
|
||||
uart_puts(buf);
|
||||
}
|
||||
|
||||
@@ -98,10 +98,10 @@ static void _print_labeled_value(const char *label, uint32_t value)
|
||||
* @param received value returned by core 1
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_counter_line(uint32_t sent, uint32_t received)
|
||||
static void print_counter_line(uint32_t sent, uint32_t received)
|
||||
{
|
||||
_print_labeled_value("core0 sent: ", sent);
|
||||
_print_labeled_value(", core1 returned: ", received);
|
||||
print_labeled_value("core0 sent: ", sent);
|
||||
print_labeled_value(", core1 returned: ", received);
|
||||
uart_puts("\r\n");
|
||||
}
|
||||
|
||||
@@ -123,11 +123,11 @@ static void _core1_main(void)
|
||||
* @param counter pointer to the running counter (post-incremented)
|
||||
* @retval None
|
||||
*/
|
||||
static void _send_and_print(uint32_t *counter)
|
||||
static void send_and_print(uint32_t *counter)
|
||||
{
|
||||
multicore_fifo_push(*counter);
|
||||
uint32_t response = multicore_fifo_pop();
|
||||
_print_counter_line(*counter, response);
|
||||
print_counter_line(*counter, response);
|
||||
(*counter)++;
|
||||
delay_ms(1000);
|
||||
}
|
||||
@@ -138,5 +138,5 @@ int main(void)
|
||||
multicore_launch(_core1_main);
|
||||
uart_puts("Multicore FIFO demo initialized\r\n");
|
||||
while (1)
|
||||
_send_and_print(&counter);
|
||||
send_and_print(&counter);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ static uint32_t _core1_stack[CORE1_STACK_WORDS];
|
||||
* @brief Drain all pending values from the RX FIFO.
|
||||
* @retval None
|
||||
*/
|
||||
static void _fifo_drain(void)
|
||||
static void fifo_drain(void)
|
||||
{
|
||||
while (SIO[SIO_FIFO_ST_OFFSET] & SIO_FIFO_ST_VLD_MASK)
|
||||
(void)SIO[SIO_FIFO_RD_OFFSET];
|
||||
@@ -48,7 +48,7 @@ static void _fifo_drain(void)
|
||||
* @param data value to write
|
||||
* @retval None
|
||||
*/
|
||||
static void _fifo_push_blocking(uint32_t data)
|
||||
static void fifo_push_blocking(uint32_t data)
|
||||
{
|
||||
while (!(SIO[SIO_FIFO_ST_OFFSET] & SIO_FIFO_ST_RDY_MASK)) {}
|
||||
SIO[SIO_FIFO_WR_OFFSET] = data;
|
||||
@@ -59,7 +59,7 @@ static void _fifo_push_blocking(uint32_t data)
|
||||
* @brief Pop one 32-bit word from the RX FIFO, blocking until valid.
|
||||
* @retval uint32_t value read from the FIFO
|
||||
*/
|
||||
static uint32_t _fifo_pop_blocking(void)
|
||||
static uint32_t fifo_pop_blocking(void)
|
||||
{
|
||||
while (!(SIO[SIO_FIFO_ST_OFFSET] & SIO_FIFO_ST_VLD_MASK)) {}
|
||||
return SIO[SIO_FIFO_RD_OFFSET];
|
||||
@@ -94,7 +94,7 @@ static void _reset_core1(void)
|
||||
{
|
||||
_set_frce_off_proc1();
|
||||
_clr_frce_off_proc1();
|
||||
(void)_fifo_pop_blocking();
|
||||
(void)fifo_pop_blocking();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,14 +102,14 @@ static void _reset_core1(void)
|
||||
* @param cmd the command word to send
|
||||
* @retval None
|
||||
*/
|
||||
static void _send_handshake_word(uint32_t cmd)
|
||||
static void send_handshake_word(uint32_t cmd)
|
||||
{
|
||||
if (!cmd)
|
||||
{
|
||||
_fifo_drain();
|
||||
fifo_drain();
|
||||
__asm__ volatile ("sev");
|
||||
}
|
||||
_fifo_push_blocking(cmd);
|
||||
fifo_push_blocking(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,30 +117,30 @@ static void _send_handshake_word(uint32_t cmd)
|
||||
* @param entry pointer to the core 1 entry function
|
||||
* @retval None
|
||||
*/
|
||||
static void _launch_handshake(void (*entry)(void))
|
||||
static void launch_handshake(void (*entry)(void))
|
||||
{
|
||||
extern uint32_t __Vectors;
|
||||
uint32_t *sp = &_core1_stack[CORE1_STACK_WORDS];
|
||||
const uint32_t seq[] = {0, 0, 1, (uintptr_t)&__Vectors, (uintptr_t)sp, (uintptr_t)entry};
|
||||
uint32_t idx = 0;
|
||||
do {
|
||||
_send_handshake_word(seq[idx]);
|
||||
idx = (_fifo_pop_blocking() == seq[idx]) ? idx + 1 : 0;
|
||||
send_handshake_word(seq[idx]);
|
||||
idx = (fifo_pop_blocking() == seq[idx]) ? idx + 1 : 0;
|
||||
} while (idx < 6);
|
||||
}
|
||||
|
||||
void multicore_launch(void (*entry)(void))
|
||||
{
|
||||
_reset_core1();
|
||||
_launch_handshake(entry);
|
||||
launch_handshake(entry);
|
||||
}
|
||||
|
||||
void multicore_fifo_push(uint32_t data)
|
||||
{
|
||||
_fifo_push_blocking(data);
|
||||
fifo_push_blocking(data);
|
||||
}
|
||||
|
||||
uint32_t multicore_fifo_pop(void)
|
||||
{
|
||||
return _fifo_pop_blocking();
|
||||
return fifo_pop_blocking();
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -45,14 +45,14 @@
|
||||
*
|
||||
* @return bool true to keep the repeating timer active
|
||||
*/
|
||||
static bool _heartbeat_callback(void) {
|
||||
static bool heartbeat_callback(void) {
|
||||
printf("Timer heartbeat\r\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
timer_driver_start(1000, _heartbeat_callback);
|
||||
timer_driver_start(1000, heartbeat_callback);
|
||||
while (true)
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ static timer_driver_callback_t g_user_callback = NULL;
|
||||
* @param rt Unused repeating-timer handle provided by the SDK
|
||||
* @return bool true to keep the timer running, false to cancel
|
||||
*/
|
||||
static bool _timer_shim(repeating_timer_t *rt) {
|
||||
static bool timer_shim(repeating_timer_t *rt) {
|
||||
(void)rt;
|
||||
if (g_user_callback)
|
||||
return g_user_callback();
|
||||
@@ -56,7 +56,7 @@ void timer_driver_start(int32_t period_ms, timer_driver_callback_t callback) {
|
||||
g_timer_active = false;
|
||||
}
|
||||
g_user_callback = callback;
|
||||
g_timer_active = add_repeating_timer_ms(period_ms, _timer_shim, NULL, &g_timer);
|
||||
g_timer_active = add_repeating_timer_ms(period_ms, timer_shim, NULL, &g_timer);
|
||||
}
|
||||
|
||||
void timer_driver_cancel(void) {
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
* @brief Heartbeat callback invoked by TIMER0 alarm 0 IRQ.
|
||||
* @retval None
|
||||
*/
|
||||
static void _heartbeat(void)
|
||||
static void heartbeat(void)
|
||||
{
|
||||
uart_puts("Timer heartbeat\r\n");
|
||||
}
|
||||
@@ -41,7 +41,7 @@ static void _heartbeat(void)
|
||||
int main(void)
|
||||
{
|
||||
uart_puts("Timer alarm demo initialized\r\n");
|
||||
timer_alarm_start(1000, _heartbeat);
|
||||
timer_alarm_start(1000, heartbeat);
|
||||
while (1)
|
||||
__asm__ volatile ("wfi");
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ static uint32_t _alarm_period_us;
|
||||
* @brief Clear the TIMER0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_clear_reset_bit(void)
|
||||
static void timer_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -49,7 +49,7 @@ static void _timer_clear_reset_bit(void)
|
||||
* @brief Wait until the TIMER0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_wait_reset_done(void)
|
||||
static void timer_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_TIMER0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ static void _timer_wait_reset_done(void)
|
||||
* @brief Set the TIMER0 tick generator cycle count to 12.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_set_tick_cycles(void)
|
||||
static void timer_set_tick_cycles(void)
|
||||
{
|
||||
TICKS_TIMER0->CYCLES = TICKS_TIMER0_CYCLES_12MHZ;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ static void _timer_set_tick_cycles(void)
|
||||
* @brief Enable the TIMER0 tick generator.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_enable_tick(void)
|
||||
static void timer_enable_tick(void)
|
||||
{
|
||||
TICKS_TIMER0->CTRL = (1U << TICKS_CTRL_ENABLE_SHIFT);
|
||||
}
|
||||
@@ -76,7 +76,7 @@ static void _timer_enable_tick(void)
|
||||
* @brief Enable the alarm 0 interrupt in TIMER0 INTE register.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_enable_alarm_irq(void)
|
||||
static void timer_enable_alarm_irq(void)
|
||||
{
|
||||
TIMER0->INTE = (1U << TIMER_INTE_ALARM0_SHIFT);
|
||||
}
|
||||
@@ -85,7 +85,7 @@ static void _timer_enable_alarm_irq(void)
|
||||
* @brief Enable TIMER0_IRQ_0 in the NVIC.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_enable_nvic(void)
|
||||
static void timer_enable_nvic(void)
|
||||
{
|
||||
*NVIC_ISER0 = (1U << TIMER0_ALARM0_IRQ);
|
||||
}
|
||||
@@ -94,7 +94,7 @@ static void _timer_enable_nvic(void)
|
||||
* @brief Arm alarm 0 with the next target time.
|
||||
* @retval None
|
||||
*/
|
||||
static void _timer_arm_alarm(void)
|
||||
static void timer_arm_alarm(void)
|
||||
{
|
||||
uint32_t target;
|
||||
target = TIMER0->TIMERAWL + _alarm_period_us;
|
||||
@@ -103,29 +103,29 @@ static void _timer_arm_alarm(void)
|
||||
|
||||
void timer_release_reset(void)
|
||||
{
|
||||
_timer_clear_reset_bit();
|
||||
_timer_wait_reset_done();
|
||||
timer_clear_reset_bit();
|
||||
timer_wait_reset_done();
|
||||
}
|
||||
|
||||
void timer_tick_init(void)
|
||||
{
|
||||
_timer_set_tick_cycles();
|
||||
_timer_enable_tick();
|
||||
timer_set_tick_cycles();
|
||||
timer_enable_tick();
|
||||
}
|
||||
|
||||
void timer_alarm_start(uint32_t period_ms, timer_callback_t cb)
|
||||
{
|
||||
_user_callback = cb;
|
||||
_alarm_period_us = period_ms * 1000U;
|
||||
_timer_enable_alarm_irq();
|
||||
_timer_enable_nvic();
|
||||
_timer_arm_alarm();
|
||||
timer_enable_alarm_irq();
|
||||
timer_enable_nvic();
|
||||
timer_arm_alarm();
|
||||
}
|
||||
|
||||
void TIMER0_IRQ_0_Handler(void)
|
||||
{
|
||||
TIMER0->INTR = TIMER_INTR_ALARM0_MASK;
|
||||
_timer_arm_alarm();
|
||||
timer_arm_alarm();
|
||||
if (_user_callback)
|
||||
_user_callback();
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -32,7 +32,7 @@ extern void TIMER0_IRQ_0_Handler(void);
|
||||
* @brief Default handler for unused exceptions (infinite loop).
|
||||
* @retval None
|
||||
*/
|
||||
static void _default_handler(void)
|
||||
static void default_handler(void)
|
||||
{
|
||||
while (1) {}
|
||||
}
|
||||
@@ -43,19 +43,19 @@ __attribute__((section(".vectors"), used))
|
||||
const void *_vectors[17] = {
|
||||
&_stack_top, // 0: Initial stack pointer
|
||||
Reset_Handler, // 1: Reset
|
||||
_default_handler, // 2: NMI
|
||||
_default_handler, // 3: HardFault
|
||||
_default_handler, // 4: MemManage
|
||||
_default_handler, // 5: BusFault
|
||||
_default_handler, // 6: UsageFault
|
||||
_default_handler, // 7: SecureFault
|
||||
default_handler, // 2: NMI
|
||||
default_handler, // 3: HardFault
|
||||
default_handler, // 4: MemManage
|
||||
default_handler, // 5: BusFault
|
||||
default_handler, // 6: UsageFault
|
||||
default_handler, // 7: SecureFault
|
||||
0, // 8: Reserved
|
||||
0, // 9: Reserved
|
||||
0, // 10: Reserved
|
||||
_default_handler, // 11: SVCall
|
||||
_default_handler, // 12: DebugMon
|
||||
default_handler, // 11: SVCall
|
||||
default_handler, // 12: DebugMon
|
||||
0, // 13: Reserved
|
||||
_default_handler, // 14: PendSV
|
||||
_default_handler, // 15: SysTick
|
||||
default_handler, // 14: PendSV
|
||||
default_handler, // 15: SysTick
|
||||
TIMER0_IRQ_0_Handler, // 16: IRQ 0 — TIMER0_IRQ_0
|
||||
};
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
/**
|
||||
* @brief Print whether the system booted normally or from a watchdog reset
|
||||
*/
|
||||
static void _print_reset_reason(void) {
|
||||
static void print_reset_reason(void) {
|
||||
if (watchdog_driver_caused_reboot())
|
||||
printf("System rebooted by watchdog timeout\r\n");
|
||||
else
|
||||
@@ -54,7 +54,7 @@ static void _print_reset_reason(void) {
|
||||
/**
|
||||
* @brief Feed the watchdog and log over UART, then wait 1 second
|
||||
*/
|
||||
static void _feed_and_report(void) {
|
||||
static void feed_and_report(void) {
|
||||
watchdog_driver_feed();
|
||||
printf("Watchdog fed\r\n");
|
||||
sleep_ms(1000);
|
||||
@@ -62,9 +62,9 @@ static void _feed_and_report(void) {
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
_print_reset_reason();
|
||||
print_reset_reason();
|
||||
watchdog_driver_enable(3000);
|
||||
printf("Watchdog enabled (3s timeout). Feeding every 1s...\r\n");
|
||||
while (true)
|
||||
_feed_and_report();
|
||||
feed_and_report();
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
* @brief Print the reset reason over UART.
|
||||
* @retval None
|
||||
*/
|
||||
static void _print_reset_reason(void)
|
||||
static void print_reset_reason(void)
|
||||
{
|
||||
if (watchdog_caused_reboot())
|
||||
uart_puts("System rebooted by watchdog timeout\r\n");
|
||||
@@ -46,7 +46,7 @@ static void _print_reset_reason(void)
|
||||
* @brief Feed the watchdog, report over UART, and delay 1 second.
|
||||
* @retval None
|
||||
*/
|
||||
static void _feed_and_report(void)
|
||||
static void feed_and_report(void)
|
||||
{
|
||||
watchdog_feed();
|
||||
uart_puts("Watchdog fed\r\n");
|
||||
@@ -55,9 +55,9 @@ static void _feed_and_report(void)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
_print_reset_reason();
|
||||
print_reset_reason();
|
||||
watchdog_enable(3000);
|
||||
uart_puts("Watchdog enabled (3s timeout). Feeding every 1s...\r\n");
|
||||
while (1)
|
||||
_feed_and_report();
|
||||
feed_and_report();
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
@@ -32,7 +32,7 @@ static uint32_t _load_value;
|
||||
* @brief Set the watchdog tick generator cycle count to 12.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_set_tick_cycles(void)
|
||||
static void watchdog_set_tick_cycles(void)
|
||||
{
|
||||
TICKS_WATCHDOG->CYCLES = TICKS_CYCLES_12MHZ;
|
||||
}
|
||||
@@ -41,7 +41,7 @@ static void _watchdog_set_tick_cycles(void)
|
||||
* @brief Enable the watchdog tick generator.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_enable_tick(void)
|
||||
static void watchdog_enable_tick(void)
|
||||
{
|
||||
TICKS_WATCHDOG->CTRL = (1U << TICKS_CTRL_ENABLE_SHIFT);
|
||||
}
|
||||
@@ -50,7 +50,7 @@ static void _watchdog_enable_tick(void)
|
||||
* @brief Disable the watchdog before reconfiguring.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_disable(void)
|
||||
static void watchdog_disable(void)
|
||||
{
|
||||
WATCHDOG->CTRL &= ~(1U << WATCHDOG_CTRL_ENABLE_SHIFT);
|
||||
}
|
||||
@@ -59,7 +59,7 @@ static void _watchdog_disable(void)
|
||||
* @brief Configure PSM WDSEL to reset everything except oscillators.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_set_psm_wdsel(void)
|
||||
static void watchdog_set_psm_wdsel(void)
|
||||
{
|
||||
uint32_t mask;
|
||||
mask = PSM_WDSEL_ALL_MASK;
|
||||
@@ -72,7 +72,7 @@ static void _watchdog_set_psm_wdsel(void)
|
||||
* @brief Set pause-on-debug bits in CTRL so debugger halts the timer.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_set_pause_debug(void)
|
||||
static void watchdog_set_pause_debug(void)
|
||||
{
|
||||
WATCHDOG->CTRL |= (1U << WATCHDOG_CTRL_PAUSE_DBG0_SHIFT);
|
||||
WATCHDOG->CTRL |= (1U << WATCHDOG_CTRL_PAUSE_DBG1_SHIFT);
|
||||
@@ -83,7 +83,7 @@ static void _watchdog_set_pause_debug(void)
|
||||
* @brief Load and enable the watchdog countdown.
|
||||
* @retval None
|
||||
*/
|
||||
static void _watchdog_load_and_enable(void)
|
||||
static void watchdog_load_and_enable(void)
|
||||
{
|
||||
WATCHDOG->LOAD = _load_value;
|
||||
WATCHDOG->CTRL |= (1U << WATCHDOG_CTRL_ENABLE_SHIFT);
|
||||
@@ -91,8 +91,8 @@ static void _watchdog_load_and_enable(void)
|
||||
|
||||
void watchdog_tick_init(void)
|
||||
{
|
||||
_watchdog_set_tick_cycles();
|
||||
_watchdog_enable_tick();
|
||||
watchdog_set_tick_cycles();
|
||||
watchdog_enable_tick();
|
||||
}
|
||||
|
||||
void watchdog_enable(uint32_t timeout_ms)
|
||||
@@ -100,10 +100,10 @@ void watchdog_enable(uint32_t timeout_ms)
|
||||
_load_value = timeout_ms * 1000U;
|
||||
if (_load_value > WATCHDOG_LOAD_MAX)
|
||||
_load_value = WATCHDOG_LOAD_MAX;
|
||||
_watchdog_disable();
|
||||
_watchdog_set_psm_wdsel();
|
||||
_watchdog_set_pause_debug();
|
||||
_watchdog_load_and_enable();
|
||||
watchdog_disable();
|
||||
watchdog_set_psm_wdsel();
|
||||
watchdog_set_pause_debug();
|
||||
watchdog_load_and_enable();
|
||||
}
|
||||
|
||||
void watchdog_feed(void)
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
* @param buf Destination buffer
|
||||
* @param buf_size Size of the buffer in bytes
|
||||
*/
|
||||
static void _prepare_write_buf(uint8_t *buf, size_t buf_size) {
|
||||
static void prepare_write_buf(uint8_t *buf, size_t buf_size) {
|
||||
memset(buf, 0xFF, buf_size);
|
||||
const char *msg = "Embedded Hacking flash driver demo";
|
||||
memcpy(buf, msg, strlen(msg) + 1);
|
||||
@@ -61,10 +61,10 @@ static void _prepare_write_buf(uint8_t *buf, size_t buf_size) {
|
||||
/**
|
||||
* @brief Write the demo string to flash and print the read-back result
|
||||
*/
|
||||
static void _write_and_verify(void) {
|
||||
static void write_and_verify(void) {
|
||||
static uint8_t write_buf[FLASH_WRITE_LEN];
|
||||
static uint8_t read_buf[FLASH_WRITE_LEN];
|
||||
_prepare_write_buf(write_buf, sizeof(write_buf));
|
||||
prepare_write_buf(write_buf, sizeof(write_buf));
|
||||
flash_driver_write(FLASH_TARGET_OFFSET, write_buf, FLASH_WRITE_LEN);
|
||||
flash_driver_read(FLASH_TARGET_OFFSET, read_buf, FLASH_WRITE_LEN);
|
||||
printf("Flash readback: %s\r\n", read_buf);
|
||||
@@ -72,7 +72,7 @@ static void _write_and_verify(void) {
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
_write_and_verify();
|
||||
write_and_verify();
|
||||
while (true)
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
* @param buf destination buffer (FLASH_PAGE_SIZE bytes in RAM)
|
||||
* @retval None
|
||||
*/
|
||||
static void _prepare_write_buf(uint8_t *buf)
|
||||
static void prepare_write_buf(uint8_t *buf)
|
||||
{
|
||||
uint32_t i;
|
||||
const char *msg = "Embedded Hacking flash driver demo";
|
||||
@@ -54,11 +54,11 @@ static void _prepare_write_buf(uint8_t *buf)
|
||||
* @brief Write the demo string to flash and print the read-back.
|
||||
* @retval None
|
||||
*/
|
||||
static void _write_and_verify(void)
|
||||
static void write_and_verify(void)
|
||||
{
|
||||
uint8_t write_buf[FLASH_PAGE_SIZE];
|
||||
uint8_t read_buf[FLASH_PAGE_SIZE];
|
||||
_prepare_write_buf(write_buf);
|
||||
prepare_write_buf(write_buf);
|
||||
flash_write(FLASH_TARGET_OFFSET, write_buf, FLASH_PAGE_SIZE);
|
||||
flash_read(FLASH_TARGET_OFFSET, read_buf, FLASH_PAGE_SIZE);
|
||||
uart_puts("Flash readback: ");
|
||||
@@ -68,7 +68,7 @@ static void _write_and_verify(void)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
_write_and_verify();
|
||||
write_and_verify();
|
||||
while (1)
|
||||
__asm volatile ("wfi");
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ typedef struct
|
||||
* @param code ROM_FUNC_* code from rp2350.h
|
||||
* @retval void* pointer to the ROM function
|
||||
*/
|
||||
static void *_rom_func_lookup(uint32_t code)
|
||||
static void *rom_func_lookup(uint32_t code)
|
||||
{
|
||||
rom_table_lookup_fn fn =
|
||||
(rom_table_lookup_fn)(uintptr_t)(*(uint16_t *)BOOTROM_TABLE_LOOKUP_OFFSET);
|
||||
@@ -76,14 +76,14 @@ static void *_rom_func_lookup(uint32_t code)
|
||||
* @param fns pointer to the struct to fill
|
||||
* @retval None
|
||||
*/
|
||||
static void _lookup_rom_fns(FlashRomFns *fns)
|
||||
static void lookup_rom_fns(FlashRomFns *fns)
|
||||
{
|
||||
fns->connect = (rom_void_fn)_rom_func_lookup(ROM_FUNC_CONNECT_INTERNAL_FLASH);
|
||||
fns->exit_xip = (rom_void_fn)_rom_func_lookup(ROM_FUNC_FLASH_EXIT_XIP);
|
||||
fns->erase = (rom_flash_erase_fn)_rom_func_lookup(ROM_FUNC_FLASH_RANGE_ERASE);
|
||||
fns->program = (rom_flash_program_fn)_rom_func_lookup(ROM_FUNC_FLASH_RANGE_PROGRAM);
|
||||
fns->flush_cache = (rom_void_fn)_rom_func_lookup(ROM_FUNC_FLASH_FLUSH_CACHE);
|
||||
fns->enter_xip = (rom_void_fn)_rom_func_lookup(ROM_FUNC_FLASH_ENTER_CMD_XIP);
|
||||
fns->connect = (rom_void_fn)rom_func_lookup(ROM_FUNC_CONNECT_INTERNAL_FLASH);
|
||||
fns->exit_xip = (rom_void_fn)rom_func_lookup(ROM_FUNC_FLASH_EXIT_XIP);
|
||||
fns->erase = (rom_flash_erase_fn)rom_func_lookup(ROM_FUNC_FLASH_RANGE_ERASE);
|
||||
fns->program = (rom_flash_program_fn)rom_func_lookup(ROM_FUNC_FLASH_RANGE_PROGRAM);
|
||||
fns->flush_cache = (rom_void_fn)rom_func_lookup(ROM_FUNC_FLASH_FLUSH_CACHE);
|
||||
fns->enter_xip = (rom_void_fn)rom_func_lookup(ROM_FUNC_FLASH_ENTER_CMD_XIP);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +99,7 @@ static void _lookup_rom_fns(FlashRomFns *fns)
|
||||
* @retval None
|
||||
*/
|
||||
__attribute__((section(".ram_func"), noinline))
|
||||
static void _flash_erase_program_ram(const FlashRomFns *fns, uint32_t offset,
|
||||
static void flash_erase_program_ram(const FlashRomFns *fns, uint32_t offset,
|
||||
const uint8_t *data, uint32_t len)
|
||||
{
|
||||
fns->connect();
|
||||
@@ -115,11 +115,11 @@ static void _flash_erase_program_ram(const FlashRomFns *fns, uint32_t offset,
|
||||
void flash_write(uint32_t offset, const uint8_t *data, uint32_t len)
|
||||
{
|
||||
FlashRomFns fns;
|
||||
_lookup_rom_fns(&fns);
|
||||
lookup_rom_fns(&fns);
|
||||
uint32_t primask;
|
||||
__asm volatile ("mrs %0, primask" : "=r" (primask));
|
||||
__asm volatile ("cpsid i");
|
||||
_flash_erase_program_ram(&fns, offset, data, len);
|
||||
flash_erase_program_ram(&fns, offset, data, len);
|
||||
__asm volatile ("msr primask, %0" :: "r" (primask));
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ extern uint32_t __data_end;
|
||||
* @brief Copy initialized data and RAM-resident code from flash to RAM.
|
||||
* @retval None
|
||||
*/
|
||||
static void _data_copy_init(void)
|
||||
static void data_copy_init(void)
|
||||
{
|
||||
uint32_t *src = &__data_lma;
|
||||
uint32_t *dst = &__data_start;
|
||||
@@ -59,7 +59,7 @@ static void _data_copy_init(void)
|
||||
void ram_init(void)
|
||||
{
|
||||
stack_init();
|
||||
_data_copy_init();
|
||||
data_copy_init();
|
||||
}
|
||||
|
||||
void __attribute__((naked, noreturn)) Reset_Handler(void)
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
* @brief Clear the UART0 reset bit in the reset controller.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_clear_reset_bit(void)
|
||||
static void uart_clear_reset_bit(void)
|
||||
{
|
||||
uint32_t value;
|
||||
value = RESETS->RESET;
|
||||
@@ -40,7 +40,7 @@ static void _uart_clear_reset_bit(void)
|
||||
* @brief Wait until the UART0 block is out of reset.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_wait_reset_done(void)
|
||||
static void uart_wait_reset_done(void)
|
||||
{
|
||||
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void _uart_wait_reset_done(void)
|
||||
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_configure_pins(void)
|
||||
static void uart_configure_pins(void)
|
||||
{
|
||||
IO_BANK0->GPIO[0].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
IO_BANK0->GPIO[1].CTRL = IO_BANK0_CTRL_FUNCSEL_UART;
|
||||
@@ -61,7 +61,7 @@ static void _uart_configure_pins(void)
|
||||
* @brief Set UART0 baud rate divisors for 115200 at 12 MHz.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_set_baud(void)
|
||||
static void uart_set_baud(void)
|
||||
{
|
||||
UART_BASE[UART_CR_OFFSET] = 0;
|
||||
UART_BASE[UART_IBRD_OFFSET] = 6;
|
||||
@@ -72,7 +72,7 @@ static void _uart_set_baud(void)
|
||||
* @brief Configure line control and enable UART0.
|
||||
* @retval None
|
||||
*/
|
||||
static void _uart_enable(void)
|
||||
static void uart_enable(void)
|
||||
{
|
||||
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
|
||||
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
|
||||
@@ -80,15 +80,15 @@ static void _uart_enable(void)
|
||||
|
||||
void uart_release_reset(void)
|
||||
{
|
||||
_uart_clear_reset_bit();
|
||||
_uart_wait_reset_done();
|
||||
uart_clear_reset_bit();
|
||||
uart_wait_reset_done();
|
||||
}
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
_uart_configure_pins();
|
||||
_uart_set_baud();
|
||||
_uart_enable();
|
||||
uart_configure_pins();
|
||||
uart_set_baud();
|
||||
uart_enable();
|
||||
}
|
||||
|
||||
bool uart_is_readable(void)
|
||||
|
||||
Reference in New Issue
Block a user