Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
exclusively use PLL_USB for clocking
Browse files Browse the repository at this point in the history
  • Loading branch information
majbthrd committed Feb 9, 2021
1 parent 05f9c15 commit c200baf
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 96 deletions.
92 changes: 0 additions & 92 deletions clock_setup.c

This file was deleted.

78 changes: 76 additions & 2 deletions myboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,90 @@
#include "tusb.h"
#include <rp2040.h>

#include "hardware/pll.h"

/*
This is a more streamlined alternative to the current pico-sdk based TinyUSB board support package.
Sticking to C and avoiding all that C++ yields a much smaller executable.
*/

void _usb_clock_setup(void); /* clock initialization routine borrowed from pico-bootrom */
/* overhaul of clock_configure() from pico-sdk to use much less memory */
bool simple_clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, bool glitchless)
{
const uint32_t div = 0x100; /* always 1:1 ratio */

clock_hw_t *clock = &clocks_hw->clk[clk_index];

// If increasing divisor, set divisor before source. Otherwise set source
// before divisor. This avoids a momentary overspeed when e.g. switching
// to a faster source and increasing divisor to compensate.
if (div > clock->div)
clock->div = div;

// If switching a glitchless slice (ref or sys) to an aux source, switch
// away from aux *first* to avoid passing glitches when changing aux mux.
// Assume (!!!) glitchless source 0 is no faster than the aux source.
if (glitchless)
{
hw_clear_bits(&clock->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
while (!(clock->selected & 1u));
}
// If no glitchless mux, cleanly stop the clock to avoid glitches
// propagating when changing aux mux. Note it would be a really bad idea
// to do this on one of the glitchless clocks (clk_sys, clk_ref).
else
{
hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
}

// Set aux mux first, and then glitchless mux if this clock has one
hw_write_masked(&clock->ctrl,
(auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB),
CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS
);

if (glitchless)
{
hw_write_masked(&clock->ctrl,
src << CLOCKS_CLK_REF_CTRL_SRC_LSB,
CLOCKS_CLK_REF_CTRL_SRC_BITS
);
while (!(clock->selected & (1u << src)));
}

hw_set_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);

// Now that the source is configured, we can trust that the user-supplied
// divisor is a safe value.
clock->div = div;

return true;
}

static void usb_clock_init(void)
{
hw_set_bits(&resets_hw->reset, RESETS_RESET_PLL_USB_BITS);
hw_clear_bits(&resets_hw->reset, RESETS_RESET_PLL_USB_BITS);
while (~resets_hw->reset_done & RESETS_RESET_PLL_USB_BITS);

pll_init(pll_usb_hw, 1, 480 * 1000000, 5, 2);

// CLK SYS = PLL USB (48MHz) / 1 = 48MHz
simple_clock_configure(clk_sys,
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
true);

// CLK USB = PLL USB (48MHz) / 1 = 48MHz
simple_clock_configure(clk_usb,
0, // No GLMUX
CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
false);
}

void board_init(void)
{
_usb_clock_setup();
usb_clock_init();
}

void irq_set_exclusive_handler(unsigned int num, void *handler) {}
Expand Down
2 changes: 1 addition & 1 deletion pico-debug.hzp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
<file file_name="./main.c" />
<file file_name="./usb_descriptors.c" />
<file file_name="myboard.c" />
<file file_name="clock_setup.c" />
<file file_name="spawn.c" />
</folder>
<folder Name="hal">
<file file_name="$(TOP)/hw/bsp/board.c" />
<file file_name="$(TOP)/src/portable/raspberrypi/rp2040/rp2040_usb.c" />
<file file_name="$(TOP)/hw/mcu/raspberrypi/pico-sdk/src/rp2_common/hardware_pll/pll.c" />
</folder>
<folder Name="CMSIS-DAP">
<file file_name="./CMSIS_5/CMSIS/DAP/Firmware/Source/DAP.c" />
Expand Down
2 changes: 1 addition & 1 deletion usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tusb_desc_device_t const desc_device =
/* using Dapper Miser CMSIS-DAP VID:PID */
.idVendor = 0x1209,
.idProduct = 0x2488,
.bcdDevice = 0x1001,
.bcdDevice = 0x1002,

.iManufacturer = 0,
.iProduct = STRID_PRODUCT,
Expand Down

0 comments on commit c200baf

Please sign in to comment.