• AVR Freaks

Helpful ReplyHot!Explore USB with dsPIC33EP

Author
mpgmike
Super Member
  • Total Posts : 281
  • Reward points : 0
  • Joined: 2014/01/23 17:27:06
  • Location: NJ
  • Status: offline
2019/08/21 10:04:43 (permalink)
0

Explore USB with dsPIC33EP

Specs:
- Explorer 16/32 Development Board
- dsPIC33EP512MU810 PIM
- MPLABX v5.25
- XC16 v1.40
- Windows 10 (64-bit)
 
In another thread I questioned the positioning of various jumpers in relation to the USB functions, so I'm confident they're correct (checked with an ohm meter to verify).
 
My first question is getting the 48 MHz clock for USB.  I'm relying on the Explorer's built in 8 MHz crystal in XT Mode.  It is just not enumerating.  (I have successfully gotten the PIC18FxxK50 series to communicate in HID Mode.  Now I'm trying to conquer the 16-bit world.). I'm trying to set PRI_OSC to 48 MHz and use that with no Pre/Post scaling (as opposed to the APLL).  Here are my settings:

OSCCON = 0x3300; //PRI_OSC, !LPOSCEN
CLKDIV = 0x0000; //OSC = 1:1
PLLFBD = 0x0005; //6X PLL, 6 X 8 MHz = 48 MHz
__builtin_write_OSCCONH(0x01); //Standard XCx Function, Short-Cuts Unlock Sequence (4 Lines)
__builtin_write_OSCCONL(OSCCON | 0x01);
while(OSCCONbits.COSC!= 0b001);
while(OSCCONbits.LOCK!= 1);
ACLKCON3 = 0x04E0; //!ENAPLL,

Will that give the USB Module the requisite 48 MHz?  If so, I'll move on to USB related SFRs.

I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
#1
du00000001
Just Some Member
  • Total Posts : 3056
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Explore USB with dsPIC33EP 2019/08/21 11:45:13 (permalink)
0
You need more than 48 MHz at the PLL as there is a postscaler that's dividing by a minimum of 2.
Check for the frequency band the PLL frequency has to fit within (differs somewhat between the families), and use a postscaler of 2 or 4 (whichever is appropriate)!

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#2
davekw7x
Entropy++
  • Total Posts : 1827
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: Explore USB with dsPIC33EP 2019/08/24 07:49:10 (permalink) ☄ Helpfulby mpgmike 2019/08/24 08:44:33
5 (1)
mpgmike
...

dsPIC33EP512MU810 PIM

...
8 MHz crystal in XT Mode.
...
I'm trying to set PRI_OSC to 48 MHz
...
Will that give the USB Module the requisite 48 MHz?

I always use the APLL for the USB clock and set the main clock to whatever I need --- Usually 80 MIPS--- however it is possible to use the main clock to generate the USB clock.

The 48 MHz USB clock comes out of the APLL block. (See attachment)

To use PRIPLL to generate the USB clock here's a way to do the deed:
 
Set up the main PLL to give 96 MHz output.  Feed that 96 MHz to the APLL output circuit, by seting ACLKCON3bits.SELACLK to 0, and set APLLPOST to divide-by-2 to get the 48 MHz USB clock.
Your 96 MHz main clock Fosc will result in an instruction clock of 48 MIPS.
 
Now, here's a Neat Thing about setting up a number of MIPS using an 8 MHz crystal

With an 8 MHz crystal, if the desired instruction clock is an integer, you can readily set up the clock circuit as follows
Then
  Set N1 to divide-by-2
  Set N2 to divide-by-2
  Set M equal to MIPS

 So Fosc = 8e6 / 2 * MIPS / 2 = 2e6*MIPS  and the instruction clock frequency in Hz is 1e6*MIPs


I always set configuration bits to start with FRC, then in a function, say init_clock() I set up the PLL and switch to PRIPLL mode.

#pragma config FNOSC = FRC              // Initial Oscillator Source Selection Bits (Internal Fast RC (FRC))
#pragma config FCKSM = CSECME           // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are enabled)


Then

#define MIPS 48
// Define FCY and include libpic30.h to be able to use __delay_ms()
// Also, use FCY to set up timer periods, UART Baud settings, etc...
#define FCY (MIPS*1000000UL)
#include <libpic30.h>

// Initialize main clock.  USB setup of APLL is a separate function
void init_clock()
{
    CLKDIVbits.PLLPRE = 0;  // N1 = Divide-by-2
    PLLFBD = MIPS-2;        // FOR 48 MIPS, this is 46
    CLKDIVbits.PLLPOST = 0; // N2 = Divide-by-2

// Note that you need NOSC equal to 0b011 for PRIPLL
    __builtin_write_OSCCONH(0b011); // Other bits of OSCCONH are Read-Only
    __builtin_write_OSCCONL(OSCCON | 0x01); // Enable clock switch
    while (OSCCONbits.COSC != 0b011)
        ;
// TODO: Maybe put timeout here so that it can fall back to FRC in case crystal fails
    while (OSCCONbits.LOCK != 1)
        ;
} // End of init_clock

 
I have assumed you want 48 MIPS, but if you really want your primary clock to be 48 MHz (it will be set up for 24 MIPs), you can set APLLPOST to divide the 48 MHz Fosc by 1.  I haven't actually done this, but it looks like it should work.

Regards,

Dave

Footnote: "Issues" with your code:
  • Initial attempt to write 0x3300 to OSCCON is irrelevant.  Needs the unlock sequence
  • Your PLLFBD value doesn't take into account prescaler divide-by-2 and postscaler divide-by-2
    Also, for future reference, note that PLLFBD value of 5 gives multiplication by 7, not 6 as indicated in your comment
  • Writing 0x01 to OSCCONH sets up FRCPLL.  To use the 8 MHz crystal, you need PRIPLL, as I showed
 
post edited by davekw7x - 2019/08/24 08:42:24

Attached Image(s)


Sometimes I just can't help myself...
#3
mpgmike
Super Member
  • Total Posts : 281
  • Reward points : 0
  • Joined: 2014/01/23 17:27:06
  • Location: NJ
  • Status: offline
Re: Explore USB with dsPIC33EP 2019/08/28 18:04:03 (permalink)
0
I finally got the basics working...by purchasing the dsPIC33E USB Development Board.  Not sure what I did wrong on the Explorer 16/32, but code that works on the dsPIC33E Dev Board didn't on the Explorer.  Thanks again for all your offers of help.

I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5