CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C slave problem/USB HID

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Borodin



Joined: 28 Jul 2006
Posts: 4

View user's profile Send private message

I2C slave problem/USB HID
PostPosted: Sun Aug 06, 2006 2:56 pm     Reply with quote

Hello,

I'm using a i2c master who's sending data to slave address 0x46. I'm using a PIC18F4550 as slave. I put the data and the address I received in hex-format on the screen (hyperTerminal) via RS232. But I also want to put the address and data on the screen with my device configured as a USB HID device.

I tested the USB-module (HID) and it works fine. I changed the report counter in a .h-file from 2 to 4 (for sending 4 bytes to the host). Then I tested the i2c interrupt and it also works fine. But when I put them together in one project, my USB never gets enumerated.

Can anyone tell me if there is a fault, any suggestions ... ?
All comments are welcome.
Thanks.

Code:
/*******************************************************************************
                      Configuratie, includes en defines
*******************************************************************************/
#include <18F4550.h>
#device adc=8

#define __USB_PIC_PERIF__ 1 //interne USB peripheral gebruiken

//instellingen en configuraties
#FUSES NOWDT                    //Geen Watch Dog Timer
#FUSES WDT32768               //Watch Dog Timer Postscale = 1:32768
#FUSES HSPLL                    //High Speed Crystal met PLL ge-enabled
#FUSES NOPROTECT                //Code niet beschermd van lezen
#FUSES NOBROWNOUT               //Geen brownout reset
#FUSES BORV46                   //Brownout reset op 4.6V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //Geen EE bescherming
#FUSES STVREN                   //Stack full/underflow zal reset veroorzaken
#FUSES NODEBUG                  //Geen Debug mode voor ICD
#FUSES NOLVP                  //Geen Low Voltage Programmeren op pin B5(PIC18)
#FUSES NOWRT                    //Programma geheugen niet schrijf-beveiligd
#FUSES NOWRTD                   //Data EEPROM niet schrijf-beveiligd
#FUSES NOIESO                 //Internal External Switch Over mode disabled
#FUSES NOFCMEN                //Fail-safe clock monitor disabled
#FUSES NOPBADEN               //PORTB pins zijn geconfigureerd als digitale
                              //ingangskanalen bij RESET
#FUSES NOWRTC                   //registers niet schrijf-beveiligd
#FUSES NOWRTB                   //Boot block niet schrijf-beveiligd
#FUSES NOEBTR                   //Geheugen niet lees-beveiligd
#FUSES NOEBTRB                  //Boot block niet lees-beveiligd
#FUSES NOCPB                    //Geen Boot Block code bescherming
#FUSES NOMCLR                 //Master Clear pin disabled
#FUSES LPT1OSC                  //Timer1 geconfigureerd voor low-power werking
#FUSES NOXINST                  //Extended set extensie en ge-indexeerde Addressing
                              //mode disabled (Legacy mode)
#FUSES PLL6                     //24 MHz externe OSC
#FUSES VREGEN                 //USB Voltage Regulator Enable
#FUSES CPUDIV1                //96MHz PLL, divided by 2 for CPU clock
#FUSES USBDIV                 //Clock source from 96 MHz PLL/2
#FUSES NOLPT1OSC              //Timer1 oscillator geconfigureerd voor high power
                              //(low power timer1 osc enable)
#FUSES CCP2B3                 //CCP2 input/output is multiplexed met RB3
#FUSES NOICPRT                //Dedicated in Circuit Debug/Programming enable
#FUSES NOXINST                //Extended Instruction Set disabled

#use delay(clock=48000000)

#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#use i2c(Slave,sda=PIN_B0,scl=PIN_B1,address=0x46 ,FORCE_HW)

//include HID handling code.
#DEFINE USB_HID_DEVICE  TRUE

/*de volgede defines zijn nodig om de TX endpoint1 (EP1) te enablen en om buffer-
  ruimte te alloceren.                                                         */
#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT //IN interrupt transfers voor EP1
#define USB_EP1_TX_SIZE    8  //alloceer 8 bytes in de hardware voor het verzenden

/*de volgede defines zijn nodig om de RX endpoint1 (EP1) te enablen en om buffer-
  ruimte te alloceren.                                                         */
#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT //OUT interrupt transfers voor EP1
#define USB_EP1_RX_SIZE    8  //alloceer 8 bytes in de hardware voor het ontvangen

//Microchip 18Fxx5x hardware layer voor usb.c
#include <pic18_usb.h>

//USB configuratie en device descriptors voor dit USB device
#include <usb_desc_hid.h>

//handlet usb setup tokens en get descriptor reports
#include <usb.c>

#DEFINE LED_RUNNING PIN_A3
#DEFINE LED_ISR PIN_A4
#DEFINE LED1 PIN_A2
#define LED_ON output_high
#define LED_OFF output_low

//adresbyte and buffer array definiëren
BYTE address, buffer[10]; //zie interrupt en while-lus
//BYTE incoming, state;

int r = 0;

/*******************************************************************************
                                 i²c_interrupt
*******************************************************************************/
//enkel adres 0x46 wordt ge-acknoledged, de instelling van meerdere adressen
//geeft uitvoeringsfouten en while lus wordt niet uitgevoerd.
#INT_SSP
void ssp_interupt ()
{
   BYTE incoming,state;

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                   //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }

   r=1;//zie while-lus

   OUTPUT_HIGH(LED_ISR);
}

void usb_debug_task(void) {
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();

   if (new_enumerated)
      LED_ON(LED1);
   else
      LED_OFF(LED1);

   if (new_connected && !last_connected) //USB geconnecteerd, maar nog niet ge-
                                         //enumereerd door de host.
      printf("\r\n\nUSB connected, waiting for enumeration...");
   if (!new_connected && last_connected) //USB nog niet geconnecteerd
      printf("\r\n\nUSB disconnected, waiting for connection...");
   if (new_enumerated && !last_enumerated) //USB werd geënumereerd door de host
      printf("\r\n\nUSB enumerated by PC/HOST");
   if (!new_enumerated && last_enumerated) //nog niet tegengekomen, maar heb het
                                           //wel gedefinieerd.
      printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");

   last_connected=new_connected;
   last_enumerated=new_enumerated;
}


/*******************************************************************************
                             main-programma
*******************************************************************************/
void main()
{
   int8 out_data[20];   //out: PIC => HOST
   int8 send_timer=0;
   int i = 0;
   BYTE testwaarde = 0x00;
   BYTE testwaarde2 = 0x00;

   LED_ON(LED1);

   usb_init_cs();

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(GLOBAL);    //Globale interrupt Call
   enable_interrupts(INT_SSP);   //Interrupt i2c
   setup_low_volt_detect(FALSE);
   setup_oscillator(False);

   printf("\r\n\Running...\r\n");
   OUTPUT_HIGH(LED_RUNNING);

   while (TRUE) //while-lus
   {
      usb_task();
      usb_debug_task();
      if (usb_enumerated())
      {
      OUTPUT_HIGH(PIN_A5);//LED indicates that system is enumerated
         if (r==1) //er is een interrupt geweest
         {
            if (!send_timer)
            {
               send_timer=250;

               //gegevens in de databuffer steken
               out_data[0]=address;
               out_data[1]=buffer[address];
               out_data[2]=testwaarde;
               out_data[3]=testwaarde2;

               //naar buiten brengen via USB en RS232
               if (usb_put_packet(1, out_data, 4 ,USB_DTS_TOGGLE))//4 bytes zenden naar de host
               {
                  printf("\r\n\Address = 0x%X",out_data[0]);
                  printf("\r\n\Buffer[address] = 0x%X",out_data[1]);
                  printf("\r\n\------------------->testwaarde = 0x%X",out_data[2]);
                  printf("\r\n\------------------->testwaarde2 = 0x%X",out_data[3]);
               }
            }
            send_timer--;
            delay_ms(1);
            //OUTPUT_LOW(LED_ISR);
            r=0;
            testwaarde++;
         }

         if (testwaarde == 0xFF)
         {
            testwaarde = 0x00;
            testwaarde2++;
            if (testwaarde2 == 0xFF)
               testwaarde2 = 0x00;
         }
      }
   }//einde van de while lus
}//einde van de main funtie


Last edited by Borodin on Mon Aug 07, 2006 1:38 am; edited 2 times in total
Borodin



Joined: 28 Jul 2006
Posts: 4

View user's profile Send private message

PostPosted: Sun Aug 06, 2006 2:57 pm     Reply with quote

The code isn't coming thru.
Borodin



Joined: 28 Jul 2006
Posts: 4

View user's profile Send private message

PostPosted: Sun Aug 06, 2006 3:02 pm     Reply with quote

Thank you for that, PCM programmer.

Last edited by Borodin on Mon Aug 07, 2006 1:33 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 06, 2006 3:05 pm     Reply with quote

Quote:
The code isn't coming thru.

Edit your post, fix any problems, and then select the checkbox below the
posting window that says "Disable HTML in this post". Then click Submit.
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

PostPosted: Fri Apr 27, 2007 2:17 am     Reply with quote

Quote:
Then I tested the i2c interrupt and it also works fine.


Hello Borodin,

Could you plz tell me under which conditions (FUSES, compiler vesion etc...) it worked fine (I2C Slave) ?

Thank you!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group