Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Problem with USB CDC and a PIC18F4550 as a slave

Status
Not open for further replies.

grindylow

Newbie level 6
Joined
Sep 4, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
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))
   {
      received_data0 = USB_Out_Buffer[0];
      received_data1 = USB_Out_Buffer[1];
      
      switch(received_data0)
      {
       case 'T' :
         USB_In_Buffer[0] = 'X';
         if (mUSBUSARTIsTxTrfReady())
            {
               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);
    WriteSPI(received_data1);
    Delay10TCYx(5);
    PORTCbits.RC2=1;
    CloseSPI();
    servo=0x00;
   }
here is my slave code:

Code:
void main(void)
{
char master;
int addr, i;

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())
	master = ReadSPI();
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
 

grindylow

Newbie level 6
Joined
Sep 4, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
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

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
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
    Helpful Answer Positive Rating

grindylow

Newbie level 6
Joined
Sep 4, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
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
Joined
Sep 4, 2009
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,367
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);
	 WriteSPI(received_data1);
	 Delay10TCYx(50);
	 PORTCbits.RC2=1;
	 servo=0x00;
}
Slave:
Code:
void main(void)
{
char master;
int addr, i;

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())
	master = ReadSPI();

switch(master)
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top