Problem with USB CDC and a PIC18F4550 as a slave

Status
Not open for further replies.

grindylow

Newbie level 6
hello, I have been playing around with 18F4550 as a USB device using a modified CDC firmware. I recently needed to have another 18F4550 to be controlled as a slave device. The problem is whenever I send data to the slave, the master hangs up(the blinkusb status freezes). I interfaced it using USART then changed it to software UART then I2C and the same scenario goes on. Prior to this, I successfully managed to interface a sensor that outputs serial data, I did that using software UART(just Rx is used). Now I interfaced it using SPI and it doesn't freezes anymore but the slave still doesn't get any data. Can anyone give me a hand on this? I'm really stuck on this..

here is my master code, and it's within the ProcessIO() function:
Code:
if (getsUSBUSART(USB_Out_Buffer, 4))
{

{
case 'T' :
USB_In_Buffer[0] = 'X';
{
putUSBUSART(USB_In_Buffer,1);
}
break;
case 'q' :
mLED_3_Off();
break;
case 'Q' :
mLED_3_On();
break;
case 'w' :
mLED_4_Off();
break;
case 'W' :
mLED_4_On();
break;
case '*' :
servo='a';
break;
default  : break;
}
}

if(servo=='a')
{
OpenSPI(SPI_FOSC_4, MODE_11, SMPMID);
PORTCbits.RC2=0;
Delay10TCYx(5);
Delay10TCYx(5);
PORTCbits.RC2=1;
CloseSPI();
servo=0x00;
}

here is my slave code:

Code:
void main(void)
{
char master;

TRISDbits.TRISD0=0;
LED=0;
for(i=0;i<18;i++) {
Delay10KTCYx(0);
}
for(i=0;i<7;i++) {
Delay1KTCYx(0);
}
for(i=0;i<5;i++) {
Delay100TCYx(0);
}

while(1)
{
LED=1;

OpenSPI(SLV_SSON, MODE_11, SMPMID);
if(DataRdySPI())
CloseSPI();

switch(master)
{
case '1':
LED=0;
for(i=0;i<18;i++) {
Delay10KTCYx(0);
}
for(i=0;i<7;i++) {
Delay1KTCYx(0);
}
for(i=0;i<5;i++) {
Delay100TCYx(0);
}
break;
case '2':
LED=1;
for(i=0;i<18;i++) {
Delay10KTCYx(0);
}
for(i=0;i<7;i++) {
Delay1KTCYx(0);
}
for(i=0;i<5;i++) {
Delay100TCYx(0);
}
break;
default: break;
}
}
}

both pics are 18F4550 running at 48MHz

blueroomelectronics

getsusbusart+processio

Not sure what you want. Are you trying to use a 18F4550 as a USB master?

grindylow

Newbie level 6
putusbusart

Not sure what you want. Are you trying to use a 18F4550 as a USB master?

basically yes. I have 18f4550 as master connected via USB, and a slave 18f4550 connected to the master

Kurenai_ryu

putusbusart function makes the usb hang

mmmm why do you open and close your SPI all the time?, you must open your SPI once during initialization, and U can change during execution... but openning and closing? even then... WHY don't you wait to finnish SPI communication????(in SPI master..) does WriteSPI function waits until all data is sended??? I don't think so, please check that...

grindylow

Points: 2

blueroomelectronics

usb cdc

I'm not sure I understand what your doing, the 18F4550 cannot be a USB host only a slave.

grindylow

Newbie level 6
openspi 1

Kurenai_ryu said:
mmmm why do you open and close your SPI all the time?, you must open your SPI once during initialization, and U can change during execution... but openning and closing? even then... WHY don't you wait to finnish SPI communication????(in SPI master..) does WriteSPI function waits until all data is sended??? I don't think so, please check that...
There is no function to check if the SPI has finished sending that is why I used some delay but I'll consider your suggestions.

blueroomelectronics said:
I'm not sure I understand what your doing, the 18F4550 cannot be a USB host only a slave.
I don't think you really understand it. I have successfully used 18F4550 as USB device and now I want to add a second 18F4550 as a slave to the first.

grindylow

Newbie level 6
Re: USB CDC and a slave

I have worked it out! Thanks Kurenai_ryu! Your suggestion definitely made it. I have stopped opening and closing the SPI all the time, and increased the delay. I should not forget those simple stuffs from now on. Here's the modified code:
Master:
Code:
void UserInit(void)
{
mInitAllLEDs();
mInitSensor();
TRISCbits.TRISC2=0;
PORTCbits.RC2=1;
OpenSPI(SPI_FOSC_4, MODE_11, SMPMID);
}

if(servo=='a')
{
PORTCbits.RC2=0;
Delay10TCYx(5);
Delay10TCYx(50);
PORTCbits.RC2=1;
servo=0x00;
}
Slave:
Code:
void main(void)
{
char master;

TRISDbits.TRISD0=0;
LED=0;
OpenSPI(SLV_SSON, MODE_11, SMPMID);

for(i=0;i<18;i++) {
Delay10KTCYx(0);
}
for(i=0;i<7;i++) {
Delay1KTCYx(0);
}
for(i=0;i<5;i++) {
Delay100TCYx(0);
}

while(1)
{
LED=1;

if(DataRdySPI())

switch(master)

Status
Not open for further replies.