Add 0x04_pwm_cbm: bare-metal RP2350 PWM driver

- PWM slice 4, channel B on GPIO25 (onboard LED)
- Duty cycle sweep 0-100% in 5% steps for breathing effect
- Wrap 9999 with no clock division (~650 Hz from ROSC)
- UART reports each duty step
- 1390B FLASH, 11 source files, zero warnings
This commit is contained in:
Kevin Thomas
2026-04-05 16:21:47 -04:00
parent d912771a5e
commit 7a42b2e088
33 changed files with 2385 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
/**
******************************************************************************
* @file image_def.c
* @author Kevin Thomas
* @brief RP2350 IMAGE_DEF block for boot ROM image recognition.
*
* Must appear within the first 4 KB of flash for the boot ROM
* to accept the image.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include <stdint.h>
/**
* @brief IMAGE_DEF block structure placed in flash
*/
__attribute__((section(".embedded_block"), used))
const uint8_t picobin_block[] = {
0xD3, 0xDE, 0xFF, 0xFF,
0x42, 0x01, 0x21, 0x10,
0xFF, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x79, 0x35, 0x12, 0xAB
};
+99
View File
@@ -0,0 +1,99 @@
/**
******************************************************************************
* @file main.c
* @author Kevin Thomas
* @brief PWM demonstration: LED breathing effect via duty-cycle sweep.
*
* Demonstrates PWM output using the PWM driver. A signal on
* GPIO 25 (onboard LED) sweeps its duty cycle from 0% to 100%
* and back to produce a smooth breathing effect. The current
* duty is reported over UART.
*
* Wiring:
* GPIO0 -> UART TX (USB-to-UART adapter RX)
* GPIO1 -> UART RX (USB-to-UART adapter TX)
* GPIO25 -> Onboard LED (no external wiring needed)
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_pwm.h"
#include "rp2350_uart.h"
#include "rp2350_delay.h"
#define SWEEP_STEP 5
#define SWEEP_DELAY_MS 50
/**
* @brief Print a duty percentage value as a decimal string over UART.
* @param duty duty cycle value (0-100)
* @retval None
*/
static void _print_duty(uint8_t duty)
{
char buf[4];
uint8_t idx = 0;
if (duty >= 100) {
buf[idx++] = '1';
buf[idx++] = '0';
buf[idx++] = '0';
} else if (duty >= 10) {
buf[idx++] = (char)('0' + duty / 10);
buf[idx++] = (char)('0' + duty % 10);
} else {
buf[idx++] = (char)('0' + duty);
}
buf[idx] = '\0';
uart_puts("Duty: ");
uart_puts(buf);
uart_puts("%\r\n");
}
/**
* @brief Sweep duty cycle upward from 0 to 100 percent.
* @retval None
*/
static void _sweep_up(void)
{
for (uint8_t duty = 0; duty <= 100; duty += SWEEP_STEP) {
pwm_set_duty(duty);
_print_duty(duty);
delay_ms(SWEEP_DELAY_MS);
}
}
/**
* @brief Sweep duty cycle downward from 100 to 0 percent.
* @retval None
*/
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);
delay_ms(SWEEP_DELAY_MS);
}
}
/**
* @brief Application entry point for the PWM LED breathing demo.
* @retval int does not return
*/
int main(void)
{
uart_puts("PWM initialized: GPIO25\r\n");
while (1) {
_sweep_up();
_sweep_down();
}
}
@@ -0,0 +1,34 @@
/**
******************************************************************************
* @file rp2350_coprocessor.c
* @author Kevin Thomas
* @brief Coprocessor access control driver implementation for RP2350.
*
* Grants access to coprocessors 0 and 1 by setting the
* corresponding bits in CPACR with DSB/ISB barriers.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_coprocessor.h"
void coprocessor_enable(void)
{
uint32_t value;
value = *CPACR;
value |= (1U << CPACR_CP1_SHIFT);
value |= (1U << CPACR_CP0_SHIFT);
*CPACR = value;
__asm__ volatile ("dsb");
__asm__ volatile ("isb");
}
+39
View File
@@ -0,0 +1,39 @@
/**
******************************************************************************
* @file rp2350_delay.c
* @author Kevin Thomas
* @brief Delay driver implementation for RP2350.
*
* Busy-wait millisecond delay calibrated for a 14.5 MHz clock
* (3600 loop iterations per millisecond).
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_delay.h"
void delay_ms(uint32_t ms)
{
if (ms == 0)
return;
__asm__ volatile (
"mov r4, #3600\n\t"
"mul r5, %0, r4\n\t"
"1:\n\t"
"subs r5, #1\n\t"
"bne 1b\n\t"
:
: "r" (ms)
: "r4", "r5", "cc"
);
}
+90
View File
@@ -0,0 +1,90 @@
/**
******************************************************************************
* @file rp2350_gpio.c
* @author Kevin Thomas
* @brief GPIO driver implementation for RP2350.
*
* SIO-based GPIO configuration using IO_BANK0 and PADS_BANK0
* register structs defined in rp2350.h. All register offsets
* verified against the RP2350 datasheet (RP-008373-DS-2).
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_gpio.h"
/**
* @brief Configure pad control for a GPIO pin.
* @param gpio_num GPIO pin number (0-29)
* @retval None
*/
static void _gpio_config_pad(uint32_t gpio_num)
{
uint32_t value;
value = PADS_BANK0->GPIO[gpio_num];
value &= ~(1U << PADS_BANK0_OD_SHIFT);
value |= (1U << PADS_BANK0_IE_SHIFT);
value &= ~(1U << PADS_BANK0_ISO_SHIFT);
PADS_BANK0->GPIO[gpio_num] = value;
}
/**
* @brief Set IO_BANK0 FUNCSEL to SIO for a GPIO pin.
* @param gpio_num GPIO pin number (0-29)
* @retval None
*/
static void _gpio_config_funcsel(uint32_t gpio_num)
{
uint32_t value;
value = IO_BANK0->GPIO[gpio_num].CTRL;
value &= ~IO_BANK0_CTRL_FUNCSEL_MASK;
value |= IO_BANK0_CTRL_FUNCSEL_SIO;
IO_BANK0->GPIO[gpio_num].CTRL = value;
}
/**
* @brief Enable the output driver for a GPIO pin via SIO.
* @param gpio_num GPIO pin number (0-29)
* @retval None
*/
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);
}
void gpio_set(uint32_t gpio_num)
{
SIO[SIO_GPIO_OUT_SET_OFFSET] = (1U << gpio_num);
}
void gpio_clear(uint32_t gpio_num)
{
SIO[SIO_GPIO_OUT_CLR_OFFSET] = (1U << gpio_num);
}
void gpio_toggle(uint32_t gpio_num)
{
SIO[SIO_GPIO_OUT_XOR_OFFSET] = (1U << gpio_num);
}
bool gpio_get(uint32_t gpio_num)
{
return (SIO[SIO_GPIO_IN_OFFSET] & (1U << gpio_num)) != 0;
}
+49
View File
@@ -0,0 +1,49 @@
/**
******************************************************************************
* @file rp2350_led.c
* @author Kevin Thomas
* @brief LED driver implementation for RP2350.
*
* High-level wrapper around the GPIO driver for LED control.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_led.h"
#include "rp2350_gpio.h"
void led_init(uint32_t pin)
{
gpio_config(pin);
gpio_clear(pin);
}
void led_on(uint32_t pin)
{
gpio_set(pin);
}
void led_off(uint32_t pin)
{
gpio_clear(pin);
}
void led_toggle(uint32_t pin)
{
gpio_toggle(pin);
}
bool led_get_state(uint32_t pin)
{
return gpio_get(pin);
}
+126
View File
@@ -0,0 +1,126 @@
/**
******************************************************************************
* @file rp2350_pwm.c
* @author Kevin Thomas
* @brief PWM driver implementation for RP2350.
*
* Configures PWM slice 4, channel B on GPIO 25 (onboard LED).
* Uses a wrap value of 9999 with no clock division, yielding
* approximately 650 Hz from the ROSC system clock.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_pwm.h"
#define PWM ((volatile uint32_t *) PWM_BASE)
/**
* @brief PWM slice number for GPIO 25.
*/
#define PWM_SLICE 4U
/**
* @brief Compute the byte offset for a register within the slice.
* @param reg per-slice register offset (e.g. PWM_CH_CSR_OFFSET)
* @retval uint32_t word index from PWM_BASE
*/
#define PWM_REG(reg) (((PWM_SLICE * PWM_CH_STRIDE) + (reg)) / 4U)
/**
* @brief Clear the PWM reset bit in the reset controller.
* @retval None
*/
static void _pwm_clear_reset_bit(void)
{
uint32_t value;
value = RESETS->RESET;
value &= ~(1U << RESETS_RESET_PWM_SHIFT);
RESETS->RESET = value;
}
/**
* @brief Wait until the PWM block is out of reset.
* @retval None
*/
static void _pwm_wait_reset_done(void)
{
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_PWM_SHIFT)) == 0) {
}
}
/**
* @brief Configure GPIO 25 pad and funcsel for PWM output.
* @retval None
*/
static void _pwm_configure_pin(void)
{
uint32_t value;
value = PADS_BANK0->GPIO[LED_PIN];
value &= ~(1U << PADS_BANK0_OD_SHIFT);
value |= (1U << PADS_BANK0_IE_SHIFT);
value &= ~(1U << PADS_BANK0_ISO_SHIFT);
PADS_BANK0->GPIO[LED_PIN] = value;
IO_BANK0->GPIO[LED_PIN].CTRL = IO_BANK0_CTRL_FUNCSEL_PWM;
}
/**
* @brief Set the clock divider to 1 (no division).
* @retval None
*/
static void _pwm_set_divider(void)
{
PWM[PWM_REG(PWM_CH_DIV_OFFSET)] = (1U << PWM_DIV_INT_SHIFT);
}
/**
* @brief Set the wrap (TOP) value and zero the compare register.
* @retval None
*/
static void _pwm_set_wrap(void)
{
PWM[PWM_REG(PWM_CH_TOP_OFFSET)] = PWM_WRAP_DEFAULT;
PWM[PWM_REG(PWM_CH_CC_OFFSET)] = 0;
}
/**
* @brief Enable the PWM slice counter.
* @retval None
*/
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();
}
void pwm_init(void)
{
_pwm_configure_pin();
_pwm_set_divider();
_pwm_set_wrap();
_pwm_enable();
}
void pwm_set_duty(uint8_t percent)
{
uint32_t level;
if (percent > 100)
percent = 100;
level = ((uint32_t)percent * (PWM_WRAP_DEFAULT + 1)) / 100;
PWM[PWM_REG(PWM_CH_CC_OFFSET)] = (level << PWM_CC_B_SHIFT);
}
+33
View File
@@ -0,0 +1,33 @@
/**
******************************************************************************
* @file rp2350_reset.c
* @author Kevin Thomas
* @brief Reset controller driver implementation for RP2350.
*
* Releases IO_BANK0 from reset and waits until the subsystem
* is ready.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_reset.h"
void reset_init_subsystem(void)
{
uint32_t value;
value = RESETS->RESET;
value &= ~(1U << RESETS_RESET_IO_BANK0_SHIFT);
RESETS->RESET = value;
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_IO_BANK0_SHIFT)) == 0) {
}
}
@@ -0,0 +1,48 @@
/**
******************************************************************************
* @file rp2350_reset_handler.c
* @author Kevin Thomas
* @brief Reset handler implementation for RP2350.
*
* Entry point after power-on or system reset. Initializes the
* stack, XOSC, subsystem resets, UART, PWM, coprocessor, then
* branches to main().
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_reset_handler.h"
#include "rp2350_stack.h"
#include "rp2350_xosc.h"
#include "rp2350_reset.h"
#include "rp2350_uart.h"
#include "rp2350_pwm.h"
#include "rp2350_coprocessor.h"
extern int main(void);
void __attribute__((naked, noreturn)) Reset_Handler(void)
{
__asm__ volatile (
"bl stack_init\n\t"
"bl xosc_init\n\t"
"bl xosc_enable_peri_clk\n\t"
"bl reset_init_subsystem\n\t"
"bl uart_release_reset\n\t"
"bl uart_init\n\t"
"bl pwm_release_reset\n\t"
"bl pwm_init\n\t"
"bl coprocessor_enable\n\t"
"b main\n\t"
);
}
+38
View File
@@ -0,0 +1,38 @@
/**
******************************************************************************
* @file rp2350_stack.c
* @author Kevin Thomas
* @brief Stack pointer initialization for RP2350.
*
* Sets MSP, PSP, MSPLIM, and PSPLIM using inline assembly.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_stack.h"
void stack_init(void)
{
__asm__ volatile (
"ldr r0, =%0\n\t"
"msr PSP, r0\n\t"
"ldr r0, =%1\n\t"
"msr MSPLIM, r0\n\t"
"msr PSPLIM, r0\n\t"
"ldr r0, =%0\n\t"
"msr MSP, r0\n\t"
:
: "i" (STACK_TOP), "i" (STACK_LIMIT)
: "r0"
);
}
+126
View File
@@ -0,0 +1,126 @@
/**
******************************************************************************
* @file rp2350_uart.c
* @author Kevin Thomas
* @brief UART0 driver implementation for RP2350.
*
* Configures UART0 on GPIO 0 (TX) and GPIO 1 (RX) at 115200
* baud using the 14.5 MHz XOSC clock.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_uart.h"
#define UART_BASE ((volatile uint32_t *) UART0_BASE)
/**
* @brief Clear the UART0 reset bit in the reset controller.
* @retval None
*/
static void _uart_clear_reset_bit(void)
{
uint32_t value;
value = RESETS->RESET;
value &= ~(1U << RESETS_RESET_UART0_SHIFT);
RESETS->RESET = value;
}
/**
* @brief Wait until the UART0 block is out of reset.
* @retval None
*/
static void _uart_wait_reset_done(void)
{
while ((RESETS->RESET_DONE & (1U << RESETS_RESET_UART0_SHIFT)) == 0) {
}
}
/**
* @brief Configure GPIO pins 0 (TX) and 1 (RX) for UART function.
* @retval None
*/
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;
PADS_BANK0->GPIO[0] = 0x04;
PADS_BANK0->GPIO[1] = 0x40;
}
/**
* @brief Set UART0 baud rate divisors for 115200 at 14.5 MHz.
* @retval None
*/
static void _uart_set_baud(void)
{
UART_BASE[UART_CR_OFFSET] = 0;
UART_BASE[UART_IBRD_OFFSET] = 6;
UART_BASE[UART_FBRD_OFFSET] = 33;
}
/**
* @brief Configure line control and enable UART0.
* @retval None
*/
static void _uart_enable(void)
{
UART_BASE[UART_LCR_H_OFFSET] = UART_LCR_H_8N1_FIFO;
UART_BASE[UART_CR_OFFSET] = UART_CR_ENABLE;
}
void uart_release_reset(void)
{
_uart_clear_reset_bit();
_uart_wait_reset_done();
}
void uart_init(void)
{
_uart_configure_pins();
_uart_set_baud();
_uart_enable();
}
bool uart_is_readable(void)
{
return (UART_BASE[UART_FR_OFFSET] & UART_FR_RXFE_MASK) == 0;
}
char uart_getchar(void)
{
while (UART_BASE[UART_FR_OFFSET] & UART_FR_RXFE_MASK) {
}
return (char)(UART_BASE[UART_DR_OFFSET] & 0xFF);
}
void uart_putchar(char c)
{
while (UART_BASE[UART_FR_OFFSET] & UART_FR_TXFF_MASK) {
}
UART_BASE[UART_DR_OFFSET] = (uint32_t)c;
}
void uart_puts(const char *str)
{
while (*str) {
uart_putchar(*str++);
}
}
char uart_to_upper(char c)
{
if (c >= 'a' && c <= 'z')
return (char)(c - 32);
return c;
}
+40
View File
@@ -0,0 +1,40 @@
/**
******************************************************************************
* @file rp2350_xosc.c
* @author Kevin Thomas
* @brief XOSC driver implementation for RP2350.
*
* Configures the external crystal oscillator and enables the
* peripheral clock sourced from XOSC.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_xosc.h"
void xosc_init(void)
{
XOSC->STARTUP = 0x00C4U;
XOSC->CTRL = 0x00FABAA0U;
while ((XOSC->STATUS & (1U << XOSC_STATUS_STABLE_SHIFT)) == 0) {
}
}
void xosc_enable_peri_clk(void)
{
uint32_t value;
value = CLOCKS->CLK_PERI_CTRL;
value |= (1U << CLK_PERI_CTRL_ENABLE_SHIFT);
value |= (CLK_PERI_CTRL_AUXSRC_XOSC << CLK_PERI_CTRL_AUXSRC_SHIFT);
CLOCKS->CLK_PERI_CTRL = value;
}
+38
View File
@@ -0,0 +1,38 @@
/**
******************************************************************************
* @file vector_table.c
* @author Kevin Thomas
* @brief Vector table with initial stack pointer and reset handler.
*
* Placed in the .vectors section at the start of flash.
* The Thumb bit (bit 0 = 1) is automatically set by the
* linker for function pointers in Thumb mode.
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include <stdint.h>
extern uint32_t _stack_top;
extern void Reset_Handler(void);
typedef void (*vector_func_t)(void);
/**
* @brief Vector table placed in .vectors section
*/
__attribute__((section(".vectors"), used))
const void *_vectors[2] = {
&_stack_top,
Reset_Handler
};