#ifndef HARDWARE_PROFILE_H
#define HARDWARE_PROFILE_H
#define DEMO_BOARD USER_DEFINED_BOARD
#define self_power 1
#define USB_BUS_SENSE 1
#endif //HARDWARE_PROFILE_H * usb_decriptors.c: Este archivo contiene el nombre con el que identificará nuestro dispositivo en el sistema y lo más cómodo es copiarlo directamente del ejemplo C:\Microchip Solutions v2010-10-19\USB Device - CDC - Basic Demo\CDC - Basic Demo - Firmware. #define USB_POLLING
//#define USB_INTERRUPT3. Ejecutar MPLAB IDE./** CONFIGURATION **************************************************/
//Descomente estos pragmas para usar el oscilador interno. Probado en un pic18f27j53
#pragma config WDTEN = OFF
#pragma config CPUDIV = OSC1
#pragma config CFGPLLEN = ON
#pragma config OSC = INTOSCPLL
#pragma config PLLDIV = 2
#pragma config XINST = OFF
//Descomente estos pragmas para usar un cristal externo de 20MHz. Probado en un pic18f2455
//#pragma config WDT = OFF
//#pragma config CPUDIV = OSC1_PLL2
//#pragma config FOSC = HSPLL_HS
//#pragma config PLLDIV = 5
//#pragma config XINST = OFF
//#pragma config USBDIV = 2
//#pragma config VREGEN = ON
/** I N C L U D E S **********************************************************/
#include "USB/usb.h"
#include "USB/usb_function_cdc.h"
/** V A R I A B L E S ********************************************************/
char USB_Out_Buffer[64];
char buffer[64];
/** P R I V A T E P R O T O T Y P E S ***************************************/
void ProcessIO(void);
/** DECLARATIONS ***************************************************/
void main(void)
{
USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware
//variables to known states.
while(1)
{
USBDeviceTasks();
ProcessIO();
}//end while
}//end main
/********************************************************************
* Function: void ProcessIO(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is a place holder for other user
* routines. It is a mixture of both USB and
* non-USB tasks.
*
* Note: None
*******************************************************************/
void ProcessIO(void)
{
BYTE numBytesRead;
// User Application USB tasks
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
if(USBUSARTIsTxTrfReady())
{
numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
if(numBytesRead != 0)
{
strcpypgm2ram(buffer,(const rom far char *)"Ha pulsado: ");
strncat(buffer,USB_Out_Buffer,numBytesRead);
strcatpgm2ram(buffer,(const rom far char *)"\r\n");
putsUSBUSART(buffer);
}
}
CDCTxService();
} //end ProcessIO
// ******************************************************************************************************
// ************** USB Callback Functions ****************************************************************
// ******************************************************************************************************
// The USB firmware stack will call the callback functions USBCBxxx() in response to certain USB related
// events. For example, if the host PC is powering down, it will stop sending out Start of Frame (SOF)
// packets to your device. In response to this, all USB devices are supposed to decrease their power
// consumption from the USB Vbus to <2.5mA each. The USB module detects this condition (which according
// to the USB specifications is 3+ms of no bus activity/SOF packets) and then calls the USBCBSuspend()
// function. You should modify these callback functions to take appropriate actions for each of these
// conditions. For example, in the USBCBSuspend(), you may wish to add code that will decrease power
// consumption from Vbus to <2.5mA (such as by clock switching, turning off LEDs, putting the
// microcontroller to sleep, etc.). Then, in the USBCBWakeFromSuspend() function, you may then wish to
// add code that undoes the power saving things done in the USBCBSuspend() function.
// The USBCBSendResume() function is special, in that the USB stack will not automatically call this
// function. This function is meant to be called from the application firmware instead. See the
// additional comments near the function.
/*******************************************************************
* Function: void USBCBCheckOtherReq(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: When SETUP packets arrive from the host, some
* firmware must process the request and respond
* appropriately to fulfill the request. Some of
* the SETUP packets will be for standard
* USB "chapter 9" (as in, fulfilling chapter 9 of
* the official USB specifications) requests, while
* others may be specific to the USB device class
* that is being implemented. For example, a HID
* class device needs to be able to respond to
* "GET REPORT" type of requests. This
* is not a standard USB chapter 9 request, and
* therefore not handled by usb_device.c. Instead
* this request should be handled by class specific
* firmware, such as that contained in usb_function_hid.c.
*
* Note: None
*******************************************************************/
void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}//end
/*******************************************************************
* Function: void USBCBInitEP(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is called when the device becomes
* initialized, which occurs after the host sends a
* SET_CONFIGURATION (wValue not = 0) request. This
* callback function should initialize the endpoints
* for the device's usage according to the current
* configuration.
*
* Note: None
*******************************************************************/
void USBCBInitEP(void)
{
CDCInitEP();
}
/*******************************************************************
* Function: BOOL USER_USB_CALLBACK_EVENT_HANDLER(
* USB_EVENT event, void *pdata, WORD size)
*
* PreCondition: None
*
* Input: USB_EVENT event - the type of event
* void *pdata - pointer to the event data
* WORD size - size of the event data
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is called from the USB stack to
* notify a user application that a USB event
* occured. This callback is in interrupt context
* when the USB_INTERRUPT option is selected.
*
* Note: None
*******************************************************************/
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event)
{
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
default:
break;
}
return TRUE;
}
/** EOF main.c *************************************************/El resultado se lo dejo a continuación, y me ha permitido establecer una comunicación CDC muy básica tanto con un 19f27j53 usando el oscilador interno como en un 18f2455 usando un cristal de 20MHz.
Tengo una duda acerca de como funciona ese stack por interrupciones, osea no se si se puede que sea así como el uart que cuando llega el dato se genera la interrupción y el va y la atiende.