View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
dsPic & USB |
Posted: Wed Feb 05, 2025 11:03 am |
|
|
Hello,
I have new trouble with dsPic33EP512MU810 and usb as cdc device.
This is sample test code:
Code: |
#include <33EP512MU810.h>
#device ICSP=1
//#device PASS_STRINGS=IN_RAM
#use delay(clock=32MHz,crystal=8MHz,USB_FULL,AUX:clock=48MHz)
#FUSES NOWDT //No Watch Dog Timer
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOJTAG //JTAG disabled
#define S_LED PIN_A6
#define ETH_EN PIN_G14
#define PER_EN PIN_D5
#define PER_ON() output_high(PER_EN)
#define PER_OFF() output_low(PER_EN)
/* HW UART1 */
#pin_select U1RX=PIN_E1 // Rx Com1.
#pin_select U1TX=PIN_E0 // Tx Com1.
#define EN1_485 PIN_D1 // RE/DE Com1.
//=============== Debug UART1 =================
#use rs232(UART1,baud=115200,errors,enable=EN1_485,stream=DEBUG)
// Tick Timer3
#use TIMER(TIMER=3,TICK=1ms,BITS=16,NOISR)
#define TickDiff(a, b) (a-b)
void putc_debug(char c){
fputc(c, DEBUG);
}
#include <usb_cdc.h>
// USB-CDC Vars.
#define _CR 0x0D
#define _LF 0x0A
#define U_STX 0x02
#define U_ETX 0x03
#define U_ACK 0x06
#define U_NAK 0x15
#define U_SP1 0x26 //'&'
#define U_SP2 0x3A //':'
#define URX_BUFF_SIZE 128
static int8 urx_buff[URX_BUFF_SIZE];
static int8 urx_index = 0;
static int1 FL_NEW_USB_PACKET = FALSE;
//#define UTX_BUFF_SIZE 64
//static int8 utx_buff[UTX_BUFF_SIZE];
unsigned int8 UsbSeqEvnt = 0;
// ***** USB-CDC Task(). *****
void MyUsbTask(void){
char c;
if(usb_cdc_kbhit()){
c=usb_cdc_getc();
if(c == U_STX)
urx_index = 0;
else if(c == U_ETX)
FL_NEW_USB_PACKET = TRUE;
urx_buff[urx_index]= c;
if(++urx_index > URX_BUFF_SIZE-1) // usb_rx_buff overflow check**!
urx_index = URX_BUFF_SIZE-1;
printf(putc_debug,"\r\n***Get New char from Usb: '%c''\r\n", c);
}
// Get New Packet from USB-CDC Port ?
if(FL_NEW_USB_PACKET){
printf(putc_debug,"\r\n***Get new Packet!\r\n\n");
FL_NEW_USB_PACKET= FALSE;
urx_index = 0;
}
}
void main(void){
unsigned int16 c_tick, p_tick; // timer Tick vars.
delay_ms(100); // Some startUp Delay..
PER_ON();
delay_ms(100);
usb_init_cs();
while(TRUE){
usb_task();
MyUsbTask();
c_tick = get_ticks();
if(usb_enumerated()){
if(TickDiff(c_tick, p_tick) > 1000) {
output_toggle(S_LED);
p_tick = c_tick = get_ticks();
}
}
else{
if(TickDiff(c_tick, p_tick) > 300) {
output_toggle(S_LED);
p_tick = c_tick = get_ticks();
}
}
}
}
|
After send few bytes usb cdc port stop reciving data, but S_LED still blinking.
Best Regards. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Wed Feb 05, 2025 12:45 pm |
|
|
Reduce the size of your debug message. That will take nearly 30mSec to
execute. You must call usb_task at leats every 10mSec....... |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Thu Feb 06, 2025 4:05 am |
|
|
Hi mr.Tt.
It could be the problem, but with reducing debug mess. not solve the problem.
Also the same code running on 18F26J50 with no problems
Best Regards, |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Thu Feb 06, 2025 6:33 am |
|
|
OK. Critical thing.
Increase your stack size.
After the processor include, have:
#build (stack=0x400)
Key difference. The PIC18, does not use the stack, except for return
addresses. The PIC24 uses it for data as well.
Now the printf calls are heavy stack users. Combine with a lot of USB
calls as well, and you are almost certainly running out of stack space.
Anything remotely larger than just a basic few lines needs the stack
expanded.
A few versions ago, everybody had to do this. CCS then expanded the
default stack size, and now it is a less common problem, but the symptoms
fit. |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Thu Feb 06, 2025 10:52 am |
|
|
Hi,
Yes, I have also tried with "#build(stack=1024)" because "CCS Pcd Wizzard" generated example code, but still not solved problem.
Best Regards, |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Fri Feb 07, 2025 5:28 am |
|
|
The stack was one I expected to go wrong.
I have to query the hardware?. Hiccups on the USB, can be caused by
problems with decoupling on Vusb, and how the USB signals themselves
are terminated.
What compiler version are you using?.
Is the unit powered off USB?. If not, USB technically requires connection
sense to be used. You don't show this. |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Fri Feb 07, 2025 10:18 am |
|
|
Another strange thing is wen sending just one byte no problem, when send packet with 10-20 bytes after few transmit usb stop to work! |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Fri Feb 07, 2025 12:35 pm |
|
|
I don't see an enable_interrupts(GLOBAL) in your code. This is needed. |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Mon Feb 10, 2025 1:23 am |
|
|
Hi mr.Tt,
Yes, In this example forgot to enable global ISR, but now add it and still not working. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Mon Feb 10, 2025 7:36 am |
|
|
Use unb_init, not usb_init_cs.
Using usb_init_cs, mean _you_ must service the usb_task at least as
often ad data arrives. usb_init, instead enables the usb interrupt to
do this handling. |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Mon Feb 10, 2025 8:53 am |
|
|
Hi,
I forgot to tell that test with "usb_init()" instead "usb_init_cs()" and problem not solved, but work better.
Another strange things are when sending packet with about 16-20 bytes and usb dev. stop to work send me back some echo or received packet data. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Tue Feb 11, 2025 2:35 am |
|
|
The circuit tells us nothing about how the system is implemented.
How long the USB connections are?.
How closely matched the signal lines are for the two signals?.
What the impedance of the traces are to ground?.
How near the decoupling is to the chips?.
That all the supply pins are connected, and decoupling is close to each.
Is there a ground plane?.
etc. etc. etc......
The two USB lines need to be matched to within 100pSec. This is normally
a maximum length difference of a couple of cm. However much more
important is the impedance between the lines and to ground. 90ohm
differential impedance. An impedance mismatch _will_ lead to USB
failing. The termination resistors help alleviate this, but only if these
are close to the processor.
You show a USB-DET signal being generated, but not the connection of this
to the CPU, or the setting in the code to use this. You show this having
a 100R resistor, but this is not the recommended connection for the
3.3v DsPIC (This is OK for a 5v PIC18). This could damage the PIC if
connected. |
|
 |
kmp84
Joined: 02 Feb 2010 Posts: 381
|
|
Posted: Wed Feb 12, 2025 2:46 am |
|
|
Hi mr.'Tt',
I'm using USB A to USB Mini cable about 1meter. As you can see on schematics posted before, 22ohm res series on D+, D- line. Connector away from chip about 3-4cm. On datasheet saw "Voltage on VBUS with respect to VSS -0.3V to +5.5V"(Which res. to use?). How to use "USB-DET" signal?
This is microchip dev.board and use same schematic.
https://www.microchip.com/en-us/development-tool/dm240018#Documentation
Best Regards, |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19700
|
|
Posted: Wed Feb 12, 2025 3:25 am |
|
|
The point is I am asking how the connections are physically made?.
How are the PCB traces spaced from the other signals nearby?.
USB is a very high speed bus. It requires proper signal layout. You
can't get away with things that are fine on things like simple serial.
Are you running through a hub?. Is there any possibility this is a
low speed hub only?. If so, there is an erratum for this PIC when the
bus goes to the idle state (it will quite often). This could be your problem.
The VBUS signal uses a charge pump to
generate this when the chip is being used for USB OTG. Now you
can use this to detect the power good as you show, but you must have
a pull down resistor on this pin, otherwise the signal will be seen high
even when there is no connection, and you need to add the code to
test this. Pull down should be something like 47K to 100KR. Microchip
also recommend the input resistor is a lot larger to prevent the possibility
of damage to the chip 2K2 or 4K7.
Then with that attached, you need the following line in front of
loading the usb driver:
Code: |
#bit U1OTGSTAT_SESVD=getenv("BIT:SESVD")
#define USB_CABLE_IS_ATTACHED() (U1OTGSTAT_SESVD)
//cable attached for DsPIC
#include <usb_cdc.h> //Usb
|
This then uses the SESVD bit from the USB comparator to give
'cable_is_attached' detection.
Now, the USB cdc driver already has a 256 byte receive buffer, so
get rid of your added buffer code. |
|
 |
|