I have al little problem, i am working with a Panda II. I need to reduce the clock speed op de cpu to 64mhz to get a baudrate of 205 kbits for a can bus.
The i did that well but the problem is now that i forgot to change the clock divider for the usb so i cannot connect by usb anymore for debugging and programming. :
Hi there
I am working with a Panda II
Now is have stil the problem de usb debugger and program port is not working anymore
I have to reduce de cpu clock to 64Mhz to get a proper baud rate for the can bus. The system we have to connect to has a baudrate of 205 kbs.
I made this code:
using System;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Hardware.LowLevel;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.NetworkInformation;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.FEZ;
public class Program
{
// Messages list
static CAN.Message[] msgList;
public static void Main()
{
// Set the system time. CAN messages will have a time stamp
Utility.SetLocalTime(new DateTime(2011, 2, 14, 0, 0, 0));
int T1, T2, BRP;
//CPU clock
Register CLKSRCSEL = new Register(0xE01fc10c);
Register CCLKCFG = new Register(0xE01fc104);
Register USBCLKCFG = new Register(0xE01fc108);
Register PLLSTAT = new Register(0xE01fc088);
Register PLLCON = new Register(0xE01fc080);
Register PLLFEED = new Register(0xE01fc08C);
Register PLLCFG = new Register(0xE01fc084);
bool PLLC;
bool PLLE;
bool Clockset = false;
int StepperClock =0;
int timerwait = 0;
uint ClockSel = CLKSRCSEL.Read();
uint ClockCfg = CCLKCFG.Read();
uint UsbClockCfg = USBCLKCFG.Read();
uint PllStatus = PLLSTAT.Read();
uint Pllcfg = PLLCFG.Read();
uint Pllcin = PLLCON.Read();
uint PllFeed;
PLLC = (PllStatus << 24) !=0 ;
PLLE = (PllStatus << 23) != 0;
while (Clockset == false)
{
while (timerwait > 0) { timerwait--; }
switch (StepperClock)
{
//Disconnect PLL
case 0:
PllStatus = PLLSTAT.Read();
PLLC = (PllStatus << 24) !=0 ;
PLLE = (PllStatus << 23) != 0;
PLLCON.Write(0x1);
PLLFEED.Write(0xAA);
PLLFEED.Write(0x55);
timerwait = 100;
StepperClock++;
break;
//Disable PLL
case 1:
PllStatus = PLLSTAT.Read();
PLLC = (PllStatus << 24) !=0 ;
PLLE = (PllStatus << 23) != 0;
if ((PLLSTAT.Read() << 24) != 1)
{
PLLCON.Write(0x0);
PLLFEED.Write(0xAA);
PLLFEED.Write(0x55);
timerwait = 100;
StepperClock++;
}
break;
//Write PLLCFG Msel 15 is equal to 32 * 12mhz = 384Mhz
case 2:
if ((PLLSTAT.Read() << 23) != 1 & ((PLLSTAT.Read() << 24) != 1))
{
PLLCFG.Write(0xF);
PLLFEED.Write(0xAA);
PLLFEED.Write(0x55);
timerwait = 100;
StepperClock++;
}
break;
//Enable PLL
case 3:
if (PLLCFG.Read() == 0xF)
{
PLLCON.Write(0x1);
PLLFEED.Write(0xAA);
PLLFEED.Write(0x55);
timerwait = 100;
StepperClock++;
}
break;
case 4:
if ((PLLSTAT.Read() << 23) != 0)
{
CCLKCFG.Write(0x5); //384Mhz / 12 == 64Mhz cpu speed
USBCLKCFG.Write(0x7); //384Mhz / 8 == 48Mhz usb speed.
timerwait = 10000;
StepperClock++;
}
break;
case 5: // Look for PLLOCK
if ((PLLSTAT.Read() << 25) != 0)
{
PLLCON.Write(0x3); //Enebale en connect pll
PLLFEED.Write(0xAA);
PLLFEED.Write(0x55);
Clockset = true;
}
break;
}
}
// PllStatus = PLLSTAT.Read();
// Debug.Print(">>> Clock Set 64Mhz <<<");
// PLLC = (PllStatus << 24) != 0;
// PLLE = (PllStatus << 23) != 0;
// These numbers were calculated using the calculator on this link:
// http://www.kvaser.com/can/protocol/index.htm
// We used the very first value from the calculator output
/////////////////////////////////////////////////////////////////////////////////////////////
// Bitrate 250Kbps
// CLK = 72 Mhz, with BRP = 12 -> 6Mhz CAN clock
// 6Mhz/250Kbps = 24 TQ
// T1 = 16 minus 1 for sync = 15
// T2 = 8
// 15 + 1 + 8 = 24 TQs which is what we need
/////////////////////////////////////////////////////////////////////////////////////////////
// uncomment to use this bit timing
//BRP = 12;
//T1 = 15;
//T2 = 8;
/////////////////////////////////////////////////////////////////////////////////////////////
// Bitrate 125Kbps
// CLK = 72 Mhz, with BRP = 24 -> 3Mhz CAN clock
// 3Mhz/125Kbps = 24 TQ
// T1 = 16 minus 1 for sync = 15
// T2 = 8
// 15 + 1 + 8 = 24 TQs which is what we need
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// Bitrate 205Kbps
// CLK = 64 Mhz, with BRP = 4 -> 16Mhz CAN clock
// 16Mhz/205Kbps = 13 TQ
// T1 = 10 minus 1 for sync = 9
// T2 = 2
// 13 + 1 + 2 = 13 TQs which is what we need
/////////////////////////////////////////////////////////////////////////////////////////////
// uncomment to use this bit timing
BRP = 24;
T1 = 9;
T2 = 3;
// For 500Kbps you can use BRP=6 and for 1Mbps you can use BRP=3 ...and so on
// Use channel 1
CAN can = new CAN(CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));
// create a message list of 100 messages
msgList = new CAN.Message[100];
for (int i = 0; i < msgList.Length; i++)
msgList[i] = new CAN.Message();
// example for sending one message
// msg ID
msgList[0].ArbID = 0x55;
msgList[0].Data[0] = 1;
msgList[0].Data[1] = 2;
msgList[0].Data[2] = 3;
msgList[0].Data[3] = 4;
msgList[0].Data[4] = 5;
msgList[0].Data[5] = 6;
msgList[0].Data[6] = 7;
msgList[0].Data[7] = 8;
// Send the 8 bytes for example
msgList[0].DLC = 8;
msgList[0].IsEID = false;
msgList[0].IsRTR = false;
// Send one messages
int numberOfMessagesPosted = can.PostMessages(msgList, 0, 1);
// subscribe to events
can.DataReceivedEvent += new CANDataReceivedEventHandler(can_DataReceivedEvent);
can.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can_ErrorReceivedEvent);
// sleep forever
//Thread.Sleep(Timeout.Infinite);
while (true)
{
numberOfMessagesPosted = can.PostMessages(msgList, 0, 1);
}
}
static void can_DataReceivedEvent(CAN sender, CANDataReceivedEventArgs args)
{
Debug.Print(">>> can_DataReceivedEvent <<<");
// read as many messages as possible
int count = sender.GetMessages(msgList, 0, msgList.Length);
for (int i = 0; i < count; i++)
{
Debug.Print("MSG: ID = " + msgList[i].ArbID + " at time = " + msgList[i].TimeStamp);
}
}
static void can_ErrorReceivedEvent(CAN sender, CANErrorReceivedEventArgs args)
{
Debug.Print(">>> can_ErrorReceivedEvent <<<");
switch (args.Error)
{
case CAN.Error.Overrun:
Debug.Print("Overrun error. Message lost");
break;
case CAN.Error.RXOver:
Debug.Print("RXOver error. Internal buffer is full. Message lost");
break;
case CAN.Error.BusOff:
Debug.Print("BusOff error. Reset CAN controller.");
sender.Reset();
break;
}
}
}
The can bus is working at 205kbs but the usb not anymore.
I still divide USBCLOCFG register but i do not understand why it is not working anymore.
It is hard to read your code. We are too spoiled with formatting and syntax highlighting these days. Please edit your post and enclose entire code with the code tags. It is the button with "1"s and "0"s in the toolbar. Thank you.
The problem is that usb connection for debugging and programming is not available anymore afther running the code above.
The canbus is working fine at 205Kbs so the board is running.