Manually control USB PLL

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.
This commit is contained in:
Doug Brown 2023-05-28 11:24:21 -07:00 committed by Doug Brown
parent 88e8f47bde
commit 3a8e006925
2 changed files with 20 additions and 3 deletions

View File

@ -38,7 +38,7 @@
<listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/> <listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/>
<listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/> <listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/>
<listOptionValue builtIn="false" value="USE_FLASH_DESCRIPTORS"/> <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>
<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 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"> <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">
@ -113,7 +113,7 @@
<listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/> <listOptionValue builtIn="false" value="FIXED_CONTROL_ENDPOINT_SIZE=8"/>
<listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/> <listOptionValue builtIn="false" value="FIXED_NUM_CONFIGURATIONS=1"/>
<listOptionValue builtIn="false" value="USE_FLASH_DESCRIPTORS"/> <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>
<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 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"> <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">

View File

@ -25,13 +25,29 @@
#include "../usbcdc.h" #include "../usbcdc.h"
#include "LUFA/Drivers/USB/USB.h" #include "LUFA/Drivers/USB/USB.h"
#include "cdc_device_definition.h" #include "cdc_device_definition.h"
#include "hardware.h"
/** Initializes the USB CDC device /** Initializes the USB CDC device
* *
*/ */
void USBCDC_Init(void) 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(); USB_Init();
} }
@ -42,6 +58,7 @@ void USBCDC_Disable(void)
{ {
// Disable LUFA, this will cause us to no longer identify as a USB device // Disable LUFA, this will cause us to no longer identify as a USB device
USB_Disable(); USB_Disable();
USB_PLL_Off();
} }
/** Main loop handler for the USB CDC device. Call from the main loop. /** Main loop handler for the USB CDC device. Call from the main loop.