recreated repo with new structure to accomodate the bootloader project

This commit is contained in:
2019-08-09 15:23:06 -04:00
commit 868a64305a
25 changed files with 10613 additions and 0 deletions

101
firmware/src/dancepad.c Normal file
View File

@@ -0,0 +1,101 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file dancepad.c
* @author Justin Byers
* @date 6 Aug 2019
* @brief Processes pad inputs and communicates with USB host.
*/
#include "dancepad.h"
#include "usb.h"
#include "usb_device_hid.h"
#include "padhal.h"
typedef union _INTPUT_CONTROLS_TYPEDEF
{
struct
{
struct
{
uint8_t square:1;
uint8_t x:1;
uint8_t o:1;
uint8_t triangle:1;
uint8_t L1:1;
uint8_t R1:1;
uint8_t L2:1;
uint8_t R2:1;//
uint8_t select:1;
uint8_t start:1;
uint8_t left_stick:1;
uint8_t right_stick:1;
uint8_t home:1;
uint8_t :3; //filler
} buttons;
struct
{
uint8_t hat_switch:4;
uint8_t :4;//filler
} hat_switch;
struct
{
uint8_t X;
uint8_t Y;
uint8_t Z;
uint8_t Rz;
} analog_stick;
} members;
uint8_t val[7];
} INPUT_CONTROLS;
// USB data must exist within the USB RAM memory space
INPUT_CONTROLS joystick_input __at(0x500);
// handle to the last data transmission - allows us to check if it completed
USB_VOLATILE USB_HANDLE lastTransmission = 0;
void DANCEPAD_Initialize()
{
lastTransmission = 0;
//enable the HID endpoint
USBEnableEndpoint(JOYSTICK_EP, USB_IN_ENABLED | USB_HANDSHAKE_ENABLED | USB_DISALLOW_SETUP);
}
void DANCEPAD_Tasks()
{
// do not start another transmission if the previous one isn't completed
if(HIDTxHandleBusy(lastTransmission))
return;
/*
* populate & send a HID report to the host
*/
//Buttons
joystick_input.val[0] = PADHAL_GetButtons();
joystick_input.val[1] = 0x00;
//Hat switch
joystick_input.val[2] = 0x08;
//Analog sticks
joystick_input.val[3] = 0x80;
joystick_input.val[4] = 0x80;
joystick_input.val[5] = 0x80;
joystick_input.val[6] = 0x80;
//Send the 8 byte packet over USB to the host.
lastTransmission = HIDTxPacket(JOYSTICK_EP, (uint8_t*)&joystick_input, sizeof(joystick_input));
}

39
firmware/src/dancepad.h Normal file
View File

@@ -0,0 +1,39 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file dancepad.h
* @author Justin Byers
* @date 6 Aug 2019
* @brief Processes pad inputs and communicates with USB host.
*/
#ifndef DANCEPAD_H
#define DANCEPAD_H
/**
* Initialize the driver.
*/
void DANCEPAD_Initialize();
/**
* Perform any driver-related tasks. Must be called once every loop.
*/
void DANCEPAD_Tasks();
#endif /* DANCEPAD_H */

30
firmware/src/interrupts.c Normal file
View File

@@ -0,0 +1,30 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file interrupts.c
* @author Justin Byers
* @date 6 Aug 2019
* @brief Contains handlers for any used hardware interrupts.
*/
#include "usb.h"
void __interrupt() SYS_InterruptHigh(void)
{
USBDeviceTasks();
}

50
firmware/src/main.c Normal file
View File

@@ -0,0 +1,50 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file main.c
* @author Justin Byers
* @date 6 Aug 2019
* @brief USB interface to 6-sensor DDR pads.
*/
#include "usb.h"
#include "padhal.h"
#include "dancepad.h"
const unsigned int VersionWord __at(0x1416) = 0x0100;
void main(void)
{
// initialize sensor HAL & the dancepad driver
PADHAL_Initialize();
DANCEPAD_Initialize();
// initialize the USB framework
USBDeviceInit();
USBDeviceAttach();
while(1)
{
// do nothing if: not connected to USB host, or the host put us in suspend state
if((USBGetDeviceState() < CONFIGURED_STATE) | USBIsDeviceSuspended())
continue;
// run application specific tasks
DANCEPAD_Tasks();
}
}

49
firmware/src/padhal.c Normal file
View File

@@ -0,0 +1,49 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file padhal.c
* @author Justin Byers
* @date 6 Aug 2019
* @brief Simple abstraction layer to read the physical pad sensors.
*/
#include <xc.h>
#include <stdint.h>
#include "padhal.h"
/**
* RB[0:5] are used as the pad inputs.
* NOTE: RB5 is multiplexed with PGM. It is not usable as digital IO unless the LVP (low voltage programming)
* configuration bit is set to OFF.
*/
#define INPUT_PIN_MASK 0b0111111
void PADHAL_Initialize()
{
// disable analog functionality on AN8-AN12 (RB0-RB4) (see PIC18F2550 datasheet, p266)
ADCON1bits.PCFG = 0b0111;
// set the pins as inputs & enable internal pull-ups
TRISB |= INPUT_PIN_MASK;
INTCON2bits.RBPU = 0;
}
uint8_t PADHAL_GetButtons()
{
// pad inputs are active low, so invert them
return (~PORTB) & INPUT_PIN_MASK;
}

40
firmware/src/padhal.h Normal file
View File

@@ -0,0 +1,40 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file padhal.h
* @author Justin Byers
* @date 6 Aug 2019
* @brief Simple abstraction layer to read the physical pad sensors.
*/
#ifndef PADHAL_H
#define PADHAL_H
/**
* Initialize IO for reading the pad sensors.
*/
void PADHAL_Initialize();
/**
* Get the current state of all pad sensors.
* @return the pad states, as the first 6 bits in a uint8_t.
*/
uint8_t PADHAL_GetButtons();
#endif /* PADHAL_H */

93
firmware/src/system.c Normal file
View File

@@ -0,0 +1,93 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file system.c
* @author Justin Byers
* @date 6 Aug 2019
* @brief Low-level hardware configuration bits.
*/
//#include "usb.h"
// PIC18F2550 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config PLLDIV = 3 // PLL Prescaler Selection bits (Divide by 3 (12 MHz oscillator input))
#pragma config CPUDIV = OSC3_PLL4// System Clock Postscaler Selection bits ([Primary Oscillator Src: /3][96 MHz PLL Src: /4])
#pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
// CONFIG1H
#pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = ON // USB Voltage Regulator Enable bit (USB voltage regulator enabled)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

177
firmware/src/usb_config.h Normal file
View File

@@ -0,0 +1,177 @@
/*************************************************************************
* Copyright (C) 2019 by Justin Byers
*
* This file is part of clubdance_v2.
*
* clubdance_v2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* clubdance_v2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with clubdance_v2. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
/**
* @file usb_config.h
* @author Justin Byers
* @date 6 Aug 2019
* @brief Configuration of the MLA USB framework.
*
* Descriptor specific type definitions are defined in: usbd.h
*/
#ifndef USBCFG_H
#define USBCFG_H
#include <usb_ch9.h>
/** DEFINITIONS ****************************************************/
#define USB_EP0_BUFF_SIZE 8 // Valid Options: 8, 16, 32, or 64 bytes.
// Using larger options take more SRAM, but
// does not provide much advantage in most types
// of applications. Exceptions to this, are applications
// that use EP0 IN or OUT for sending large amounts of
// application related data.
#define USB_MAX_NUM_INT 1 //Set this number to match the maximum interface number used in the descriptors for this firmware project
#define USB_MAX_EP_NUMBER 1 //Set this number to match the maximum endpoint number used in the descriptors for this firmware project
//Device descriptor - if these two definitions are not defined then
// a const USB_DEVICE_DESCRIPTOR variable by the exact name of device_dsc
// must exist.
#define USB_USER_DEVICE_DESCRIPTOR &device_dsc
#define USB_USER_DEVICE_DESCRIPTOR_INCLUDE extern const USB_DEVICE_DESCRIPTOR device_dsc
//Configuration descriptors - if these two definitions do not exist then
// a const BYTE *const variable named exactly USB_CD_Ptr[] must exist.
#define USB_USER_CONFIG_DESCRIPTOR USB_CD_Ptr
#define USB_USER_CONFIG_DESCRIPTOR_INCLUDE extern const uint8_t *const USB_CD_Ptr[]
//------------------------------------------------------------------------------
//Select an endpoint ping-pong bufferring mode. Some microcontrollers only
//support certain modes. For most applications, it is recommended to use either
//the USB_PING_PONG__FULL_PING_PONG or USB_PING_PONG__EP0_OUT_ONLY options.
//The other settings are supported on some devices, but they are not
//recommended, as they offer inferior control transfer timing performance.
//See inline code comments in usb_device.c for additional details.
//Enabling ping pong bufferring on an endpoint generally increases firmware
//overhead somewhat, but when both buffers are used simultaneously in the
//firmware, can offer better sustained bandwidth, especially for OUT endpoints.
//------------------------------------------------------
//#define USB_PING_PONG_MODE USB_PING_PONG__NO_PING_PONG //Not recommended
#define USB_PING_PONG_MODE USB_PING_PONG__FULL_PING_PONG //A good all around setting
//#define USB_PING_PONG_MODE USB_PING_PONG__EP0_OUT_ONLY //Another good setting
//#define USB_PING_PONG_MODE USB_PING_PONG__ALL_BUT_EP0 //Not recommended
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//Select a USB stack operating mode. In the USB_INTERRUPT mode, the USB stack
//main task handler gets called only when necessary as an interrupt handler.
//This can potentially minimize CPU utilization, but adds context saving
//and restoring overhead associated with interrupts, which can potentially
//decrease performance.
//When the USB_POLLING mode is selected, the USB stack main task handler
//(ex: USBDeviceTasks()) must be called periodically by the application firmware
//at a minimum rate as described in the inline code comments in usb_device.c.
//------------------------------------------------------
//#define USB_POLLING
#define USB_INTERRUPT
//------------------------------------------------------------------------------
/* Parameter definitions are defined in usb_device.h */
#define USB_PULLUP_OPTION USB_PULLUP_ENABLE
//#define USB_PULLUP_OPTION USB_PULLUP_DISABLED
#define USB_TRANSCEIVER_OPTION USB_INTERNAL_TRANSCEIVER
//External Transceiver support is not available on all product families. Please
// refer to the product family datasheet for more information if this feature
// is available on the target processor.
//#define USB_TRANSCEIVER_OPTION USB_EXTERNAL_TRANSCEIVER
#define USB_SPEED_OPTION USB_FULL_SPEED
//#define USB_SPEED_OPTION USB_LOW_SPEED //(this mode is only supported on some microcontrollers)
//------------------------------------------------------------------------------------------------------------------
//Option to enable auto-arming of the status stage of control transfers, if no
//"progress" has been made for the USB_STATUS_STAGE_TIMEOUT value.
//If progress is made (any successful transactions completing on EP0 IN or OUT)
//the timeout counter gets reset to the USB_STATUS_STAGE_TIMEOUT value.
//
//During normal control transfer processing, the USB stack or the application
//firmware will call USBCtrlEPAllowStatusStage() as soon as the firmware is finished
//processing the control transfer. Therefore, the status stage completes as
//quickly as is physically possible. The USB_ENABLE_STATUS_STAGE_TIMEOUTS
//feature, and the USB_STATUS_STAGE_TIMEOUT value are only relevant, when:
//1. The application uses the USBDeferStatusStage() API function, but never calls
// USBCtrlEPAllowStatusStage(). Or:
//2. The application uses host to device (OUT) control transfers with data stage,
// and some abnormal error occurs, where the host might try to abort the control
// transfer, before it has sent all of the data it claimed it was going to send.
//
//If the application firmware never uses the USBDeferStatusStage() API function,
//and it never uses host to device control transfers with data stage, then
//it is not required to enable the USB_ENABLE_STATUS_STAGE_TIMEOUTS feature.
#define USB_ENABLE_STATUS_STAGE_TIMEOUTS //Comment this out to disable this feature.
//Section 9.2.6 of the USB 2.0 specifications indicate that:
//1. Control transfers with no data stage: Status stage must complete within
// 50ms of the start of the control transfer.
//2. Control transfers with (IN) data stage: Status stage must complete within
// 50ms of sending the last IN data packet in fullfilment of the data stage.
//3. Control transfers with (OUT) data stage: No specific status stage timing
// requirement. However, the total time of the entire control transfer (ex:
// including the OUT data stage and IN status stage) must not exceed 5 seconds.
//
//Therefore, if the USB_ENABLE_STATUS_STAGE_TIMEOUTS feature is used, it is suggested
//to set the USB_STATUS_STAGE_TIMEOUT value to timeout in less than 50ms. If the
//USB_ENABLE_STATUS_STAGE_TIMEOUTS feature is not enabled, then the USB_STATUS_STAGE_TIMEOUT
//parameter is not relevant.
#define USB_STATUS_STAGE_TIMEOUT (uint8_t)45 //Approximate timeout in milliseconds, except when
//USB_POLLING mode is used, and USBDeviceTasks() is called at < 1kHz
//In this special case, the timeout becomes approximately:
//Timeout(in milliseconds) = ((1000 * (USB_STATUS_STAGE_TIMEOUT - 1)) / (USBDeviceTasks() polling frequency in Hz))
//------------------------------------------------------------------------------------------------------------------
#define USB_SUPPORT_DEVICE
#define USB_NUM_STRING_DESCRIPTORS 3 //Set this number to match the total number of string descriptors that are implemented in the usb_descriptors.c file
/*******************************************************************
* Event disable options
* Enable a definition to suppress a specific event. By default
* all events are sent.
*******************************************************************/
//#define USB_DISABLE_SUSPEND_HANDLER
//#define USB_DISABLE_WAKEUP_FROM_SUSPEND_HANDLER
//#define USB_DISABLE_SOF_HANDLER
//#define USB_DISABLE_TRANSFER_TERMINATED_HANDLER
//#define USB_DISABLE_ERROR_HANDLER
//#define USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER
//#define USB_DISABLE_SET_DESCRIPTOR_HANDLER
//#define USB_DISABLE_SET_CONFIGURATION_HANDLER
//#define USB_DISABLE_TRANSFER_COMPLETE_HANDLER
/** DEVICE CLASS USAGE *********************************************/
#define USB_USE_HID
/** ENDPOINTS ALLOCATION *******************************************/
/* HID */
#define HID_INTF_ID 0x00
#define JOYSTICK_EP 1
#define HID_INT_OUT_EP_SIZE 64
#define HID_INT_IN_EP_SIZE 64
#define HID_NUM_OF_DSC 1
#define HID_RPT01_SIZE 74
/** DEFINITIONS ****************************************************/
#endif //USBCFG_H

View File

@@ -0,0 +1,282 @@
/*******************************************************************************
Copyright 2016 Microchip Technology Inc. (www.microchip.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To request to license the code under the MLA license (www.microchip.com/mla_license),
please contact mla_licensing@microchip.com
*******************************************************************************/
/********************************************************************
-usb_descriptors.c-
-------------------------------------------------------------------
Filling in the descriptor values in the usb_descriptors.c file:
-------------------------------------------------------------------
[Device Descriptors]
The device descriptor is defined as a USB_DEVICE_DESCRIPTOR type.
This type is defined in usb_ch9.h Each entry into this structure
needs to be the correct length for the data type of the entry.
[Configuration Descriptors]
The configuration descriptor was changed in v2.x from a structure
to a uint8_t array. Given that the configuration is now a byte array
each byte of multi-byte fields must be listed individually. This
means that for fields like the total size of the configuration where
the field is a 16-bit value "64,0," is the correct entry for a
configuration that is only 64 bytes long and not "64," which is one
too few bytes.
The configuration attribute must always have the _DEFAULT
definition at the minimum. Additional options can be ORed
to the _DEFAULT attribute. Available options are _SELF and _RWU.
These definitions are defined in the usb_device.h file. The
_SELF tells the USB host that this device is self-powered. The
_RWU tells the USB host that this device supports Remote Wakeup.
[Endpoint Descriptors]
Like the configuration descriptor, the endpoint descriptors were
changed in v2.x of the stack from a structure to a uint8_t array. As
endpoint descriptors also has a field that are multi-byte entities,
please be sure to specify both bytes of the field. For example, for
the endpoint size an endpoint that is 64 bytes needs to have the size
defined as "64,0," instead of "64,"
Take the following example:
// Endpoint Descriptor //
0x07, //the size of this descriptor //
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
_EP02_IN, //EndpointAddress
_INT, //Attributes
0x08,0x00, //size (note: 2 bytes)
0x02, //Interval
The first two parameters are self-explanatory. They specify the
length of this endpoint descriptor (7) and the descriptor type.
The next parameter identifies the endpoint, the definitions are
defined in usb_device.h and has the following naming
convention:
_EP<##>_<dir>
where ## is the endpoint number and dir is the direction of
transfer. The dir has the value of either 'OUT' or 'IN'.
The next parameter identifies the type of the endpoint. Available
options are _BULK, _INT, _ISO, and _CTRL. The _CTRL is not
typically used because the default control transfer endpoint is
not defined in the USB descriptors. When _ISO option is used,
addition options can be ORed to _ISO. Example:
_ISO|_AD|_FE
This describes the endpoint as an isochronous pipe with adaptive
and feedback attributes. See usb_device.h and the USB
specification for details. The next parameter defines the size of
the endpoint. The last parameter in the polling interval.
-------------------------------------------------------------------
Adding a USB String
-------------------------------------------------------------------
A string descriptor array should have the following format:
rom struct{byte bLength;byte bDscType;word string[size];}sdxxx={
sizeof(sdxxx),DSC_STR,<text>};
The above structure provides a means for the C compiler to
calculate the length of string descriptor sdxxx, where xxx is the
index number. The first two bytes of the descriptor are descriptor
length and type. The rest <text> are string texts which must be
in the unicode format. The unicode format is achieved by declaring
each character as a word type. The whole text string is declared
as a word array with the number of characters equals to <size>.
<size> has to be manually counted and entered into the array
declaration. Let's study this through an example:
if the string is "USB" , then the string descriptor should be:
(Using index 02)
rom struct{byte bLength;byte bDscType;word string[3];}sd002={
sizeof(sd002),DSC_STR,'U','S','B'};
A USB project may have multiple strings and the firmware supports
the management of multiple strings through a look-up table.
The look-up table is defined as:
rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
The above declaration has 3 strings, sd000, sd001, and sd002.
Strings can be removed or added. sd000 is a specialized string
descriptor. It defines the language code, usually this is
US English (0x0409). The index of the string must match the index
position of the USB_SD_Ptr array, &sd000 must be in position
USB_SD_Ptr[0], &sd001 must be in position USB_SD_Ptr[1] and so on.
The look-up table USB_SD_Ptr is used by the get string handler
function.
-------------------------------------------------------------------
The look-up table scheme also applies to the configuration
descriptor. A USB device may have multiple configuration
descriptors, i.e. CFG01, CFG02, etc. To add a configuration
descriptor, user must implement a structure similar to CFG01.
The next step is to add the configuration descriptor name, i.e.
cfg01, cfg02,.., to the look-up table USB_CD_Ptr. USB_CD_Ptr[0]
is a dummy place holder since configuration 0 is the un-configured
state according to the definition in the USB specification.
********************************************************************/
/*********************************************************************
* Descriptor specific type definitions are defined in:
* usb_device.h
*
* Configuration options are defined in:
* usb_config.h
********************************************************************/
/** INCLUDES *******************************************************/
#include "usb.h"
#include "usb_device_hid.h"
/** CONSTANTS ******************************************************/
#if defined(COMPILER_MPLAB_C18)
#pragma romdata
#endif
/* Device Descriptor */
const USB_DEVICE_DESCRIPTOR device_dsc=
{
0x12, // Size of this descriptor in bytes
USB_DESCRIPTOR_DEVICE, // DEVICE descriptor type
0x0200, // USB Spec Release Number in BCD format
0x00, // Class Code
0x00, // Subclass code
0x00, // Protocol code
USB_EP0_BUFF_SIZE, // Max packet size for EP0, see usb_config.h
0x04D8, // Vendor ID, see usb_config.h
0x005E, // Product ID, see usb_config.h
0x0001, // Device release number in BCD format
0x01, // Manufacturer string index
0x02, // Product string index
0x00, // Device serial number string index
0x01 // Number of possible configurations
};
/* Configuration 1 Descriptor */
const uint8_t configDescriptor1[]={
/* Configuration Descriptor */
0x09,//sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
DESC_CONFIG_WORD(0x0022), // Total length of data for this cfg
1, // Number of interfaces in this cfg
1, // Index value of this configuration
0, // Configuration string index
_DEFAULT | _SELF, // Attributes, see usb_device.h
50, // Max power consumption (2X mA)
/* Interface Descriptor */
0x09,//sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type
0, // Interface Number
0, // Alternate Setting Number
1, // Number of endpoints in this intf
HID_INTF, // Class code
0, // Subclass code
0, // Protocol code
0, // Interface string index
/* HID Class-Specific Descriptor */
0x09,//sizeof(USB_HID_DSC)+3, // Size of this descriptor in bytes RRoj hack
DSC_HID, // HID descriptor type
DESC_CONFIG_WORD(0x0111), // HID Spec Release Number in BCD format (1.11)
0x00, // Country Code (0x00 for Not supported)
HID_NUM_OF_DSC, // Number of class descriptors, see usbcfg.h
DSC_RPT, // Report descriptor type
DESC_CONFIG_WORD(HID_RPT01_SIZE), //sizeof(hid_rpt01), // Size of the report descriptor
/* Endpoint Descriptor */
0x07,/*sizeof(USB_EP_DSC)*/
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
JOYSTICK_EP | _EP_IN, //EndpointAddress
_INTERRUPT, //Attributes
DESC_CONFIG_WORD(64), //size
0x01, //Interval
};
//Language code string descriptor
const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[1];}sd000={
sizeof(sd000),USB_DESCRIPTOR_STRING,{0x0409
}};
//Manufacturer string descriptor
const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[25];}sd001={
sizeof(sd001),USB_DESCRIPTOR_STRING,
{'M','i','c','r','o','c','h','i','p',' ',
'T','e','c','h','n','o','l','o','g','y',' ','I','n','c','.'
}};
//Product string descriptor
const struct{uint8_t bLength;uint8_t bDscType;uint16_t string[13];}sd002={
sizeof(sd002),USB_DESCRIPTOR_STRING,
{'J','o','y','s','t','i','c','k',' ','D','e','m','o'
}};
//Array of configuration descriptors
const uint8_t *const USB_CD_Ptr[]=
{
(const uint8_t *const)&configDescriptor1
};
//Array of string descriptors
const uint8_t *const USB_SD_Ptr[]=
{
(const uint8_t *const)&sd000,
(const uint8_t *const)&sd001,
(const uint8_t *const)&sd002
};
const struct{uint8_t report[HID_RPT01_SIZE];}hid_rpt01={{
0x05,0x01, //USAGE_PAGE (Generic Desktop)
0x09,0x05, //USAGE (Game Pad)
0xA1,0x01, //COLLECTION (Application)
0x15,0x00, // LOGICAL_MINIMUM(0)
0x25,0x01, // LOGICAL_MAXIMUM(1)
0x35,0x00, // PHYSICAL_MINIMUM(0)
0x45,0x01, // PHYSICAL_MAXIMUM(1)
0x75,0x01, // REPORT_SIZE(1)
0x95,0x0D, // REPORT_COUNT(13)
0x05,0x09, // USAGE_PAGE(Button)
0x19,0x01, // USAGE_MINIMUM(Button 1)
0x29,0x0D, // USAGE_MAXIMUM(Button 13)
0x81,0x02, // INPUT(Data,Var,Abs)
0x95,0x03, // REPORT_COUNT(3)
0x81,0x01, // INPUT(Cnst,Ary,Abs)
0x05,0x01, // USAGE_PAGE(Generic Desktop)
0x25,0x07, // LOGICAL_MAXIMUM(7)
0x46,0x3B,0x01, // PHYSICAL_MAXIMUM(315)
0x75,0x04, // REPORT_SIZE(4)
0x95,0x01, // REPORT_COUNT(1)
0x65,0x14, // UNIT(Eng Rot:Angular Pos)
0x09,0x39, // USAGE(Hat Switch)
0x81,0x42, // INPUT(Data,Var,Abs,Null)
0x65,0x00, // UNIT(None)
0x95,0x01, // REPORT_COUNT(1)
0x81,0x01, // INPUT(Cnst,Ary,Abs)
0x26,0xFF,0x00, // LOGICAL_MAXIMUM(255)
0x46,0xFF,0x00, // PHYSICAL_MAXIMUM(255)
0x09,0x30, // USAGE(X)
0x09,0x31, // USAGE(Y)
0x09,0x32, // USAGE(Z)
0x09,0x35, // USAGE(Rz)
0x75,0x08, // REPORT_SIZE(8)
0x95,0x04, // REPORT_COUNT(4)
0x81,0x02, // INPUT(Data,Var,Abs)
0xC0 //END_COLLECTION
}
};
/** EOF usb_descriptors.c ***************************************************/

86
firmware/src/usb_events.c Normal file
View File

@@ -0,0 +1,86 @@
#include "usb.h"
#include "usb_device_hid.h"
#include "dancepad.h"
/*******************************************************************
* Function: bool USER_USB_CALLBACK_EVENT_HANDLER(
* USB_EVENT event, void *pdata, uint16_t size)
*
* PreCondition: None
*
* Input: USB_EVENT event - the type of event
* void *pdata - pointer to the event data
* uint16_t 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, uint16_t size)
{
switch( (int) event )
{
case EVENT_TRANSFER:
break;
case EVENT_SOF:
break;
case EVENT_SUSPEND:
//Call the hardware platform specific handler for suspend events for
//possible further action (like optionally going reconfiguring the application
//for lower power states and going to sleep during the suspend event). This
//would normally be done in USB compliant bus powered applications, although
//no further processing is needed for purely self powered applications that
//don't consume power from the host.
//SYSTEM_Initialize(SYSTEM_STATE_USB_SUSPEND);
break;
case EVENT_RESUME:
//Call the hardware platform specific resume from suspend handler (ex: to
//restore I/O pins to higher power states if they were changed during the
//preceding SYSTEM_Initialize(SYSTEM_STATE_USB_SUSPEND) call at the start
//of the suspend condition.
//SYSTEM_Initialize(SYSTEM_STATE_USB_RESUME);
break;
case EVENT_CONFIGURED:
/* When the device is configured, we can (re)initialize the dancepad driver */
DANCEPAD_Initialize();
break;
case EVENT_SET_DESCRIPTOR:
break;
case EVENT_EP0_REQUEST:
/* We have received a non-standard USB request. The HID driver
* needs to check to see if the request was for it. */
USBCheckHIDRequest();
break;
case EVENT_BUS_ERROR:
break;
case EVENT_TRANSFER_TERMINATED:
break;
default:
break;
}
return true;
}
/*******************************************************************************
End of File
*/