Skip to content

Commit

Permalink
Manually control USB PLL
Browse files Browse the repository at this point in the history
The 646 and 1286 have different required USB PLL values when you have a
16 MHz crystal. Detect the chip at runtime to set up the PLL correctly.
This requires taking over control of the PLL from LUFA.
  • Loading branch information
dougg3 committed May 29, 2023
1 parent 88e8f47 commit 3a8e006
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/>
<listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/>
<listOptionValue builtIn="false" value="USE_FLASH_DESCRIPTORS"/>
<listOptionValue builtIn="false" value="USE_STATIC_OPTIONS=&quot;(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)&quot;"/>
<listOptionValue builtIn="false" value="USE_STATIC_OPTIONS=&quot;(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_MANUAL_PLL)&quot;"/>
</option>
<option id="de.innot.avreclipse.compiler.option.otherflags.1513967861" name="Other flags" superClass="de.innot.avreclipse.compiler.option.otherflags" value="-ffunction-sections -fdata-sections" valueType="string"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="de.innot.avreclipse.compiler.option.incpath.173462079" superClass="de.innot.avreclipse.compiler.option.incpath" valueType="includePath">
Expand Down Expand Up @@ -113,7 +113,7 @@
<listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/>
<listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/>
<listOptionValue builtIn="false" value="USE_FLASH_DESCRIPTORS"/>
<listOptionValue builtIn="false" value="USE_STATIC_OPTIONS=&quot;(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)&quot;"/>
<listOptionValue builtIn="false" value="USE_STATIC_OPTIONS=&quot;(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_MANUAL_PLL)&quot;"/>
</option>
<option id="de.innot.avreclipse.compiler.option.otherflags.1566165436" name="Other flags" superClass="de.innot.avreclipse.compiler.option.otherflags" value="-ffunction-sections -fdata-sections" valueType="string"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="de.innot.avreclipse.compiler.option.incpath.2119807530" superClass="de.innot.avreclipse.compiler.option.incpath" valueType="includePath">
Expand Down
19 changes: 18 additions & 1 deletion hal/at90usb646/usbcdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,29 @@
#include "../usbcdc.h"
#include "LUFA/Drivers/USB/USB.h"
#include "cdc_device_definition.h"
#include "hardware.h"

/** Initializes the USB CDC device
*
*/
void USBCDC_Init(void)
{
// Initialize LUFA
// Initialize LUFA.
// We have to manually start the USB PLL rather than allow LUFA to do it,
// because we might be on an AT90USB128x instead of AT90USB64x, and LUFA's
// automatic PLL control decides on the PLL init value at compile time.
// It differs between the two chips when there's a 16 MHz crystal.
if (IsAT90USB128x())
{
PLLCSR = (1 << PLLP2) | (1 << PLLP0);
PLLCSR = ((1 << PLLP2) | (1 << PLLP0) | (1 << PLLE));
}
else
{
PLLCSR = (1 << PLLP2) | (1 << PLLP1);
PLLCSR = ((1 << PLLP2) | (1 << PLLP1) | (1 << PLLE));
}
while (!USB_PLL_IsReady());
USB_Init();
}

Expand All @@ -42,6 +58,7 @@ void USBCDC_Disable(void)
{
// Disable LUFA, this will cause us to no longer identify as a USB device
USB_Disable();
USB_PLL_Off();
}

/** Main loop handler for the USB CDC device. Call from the main loop.
Expand Down

0 comments on commit 3a8e006

Please sign in to comment.