Skip to content

Instantly share code, notes, and snippets.

@svensken
Created March 20, 2011 20:11
Show Gist options
  • Save svensken/878632 to your computer and use it in GitHub Desktop.
Save svensken/878632 to your computer and use it in GitHub Desktop.
Hier geht es nicht um Eleganz ;)
#ifndef HARDWARE_PROFILE_H
#define HARDWARE_PROFILE_H
// Defines required for the USB stack
//#define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER
#define self_power 1
/*#define USE_SELF_POWER_SENSE_IO
#define tris_self_power TRISBbits.TRISB0 // Input
#define self_power RB0*/
#define USE_USB_BUS_SENSE_IO
//#define tris_usb_bus_sense TRISCbits.TRISC6 // Input
#define USB_BUS_SENSE 1//RC6
#define DEMO_BOARD PICDEM_FS_USB
#define PICDEM_FS_USB
#define CLOCK_FREQ 48000000
// Fosc frequency (48 Mhz)
#define _XTAL_FREQ 48000000
// Common useful definitions
#define ON 1
#define OFF 0
#define WRITE 0
#define READ 1
// PIC to hardware pin mapping
#define TEMP RA0
#define HEAT RA1
#define LIGHT RA4
// I/O pin definitions
#define INPUT_PIN 1
#define OUTPUT_PIN 0
#endif
#ifndef MAIN_C
#define MAIN_C
#include <htc.h>
#include "usb.h"
#include "HardwareProfile.h"
#include "usb_function_hid.h"
#include "genericHID.h"
// PIC 18F4550 fuse configuration:
// Config word 1 (Oscillator configuration)
// 20Mhz crystal input scaled to 48Mhz and configured for USB operation
__CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5);
// Config word 2
__CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K);
// Config word 3
__CONFIG(3, PBDIGITAL & LPT1DIS & MCLREN);
// Config word 4
__CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS);
// Config word 5, 6 and 7 (protection configuration)
__CONFIG(5, UNPROTECT);
__CONFIG(6, UNPROTECT);
__CONFIG(7, UNPROTECT);
// local prototypes
static void InitialiseSystem(void);
void ProcessIO(void);
void ShutoffCheck(void);
void Standalone(void);
int cycles;
double tempI;
double tempD;
double tempA;
double tempE;
double tempF;
int timeI;
int timeD;
int timeA;
int timeE;
int timeF;
long Trip;
int licks;
int secs;
void main(void)
{
InitialiseSystem();
while(1)
{
// Check bus status and service USB interrupts.
USBDeviceTasks();
//turn off heating element after a second
//ShutoffCheck();
// Application-specific tasks.
ProcessIO();
}
}
static void InitialiseSystem(void)
{
/* ADC setup *******************************************/
ADCON0 = 0b00000000; // channel 0 (AN0)
ADCON1 = 0b00001110; // VSS = Vref- ; VDD = Vref+ ; AN0 pin is analog
ADCON2 = 0b10001010; // right justified ; 2Tad ; 32Tosc
//20Mhz osc should = 16Tosc but that'd be cutting Tad too close to min
ADON=1; // enable A2D converter
ADIE=0; // not interrupt driven
/* Timer1 setup ****************************************/
GIEH=1;
GIEL=1;
T1CON = 0b00110101; // 2x 8-Bit, 1:8, source: Fosc/4
// I messed with it all day, but I couldnt get the Timer1 osc to work.
// 32 kHz would be simpler than counting 30 'licks'/s with Fosc/4 :(
// - Kakabuku
//TMR1IF = 0; // overflow cleared
//TMR1IE = 1; // enable interrupt
/* Ports setup ******************************************/
TRISA = 0b00000001; // temp input - RA0
TRISB = 0b00000000;
TRISC = 0b01000000; // usb sense input - RC6
//#if defined(USE_USB_BUS_SENSE_IO)
//TRISC6 = INPUT_PIN; // See HardwareProfile.h
//#endif
// Clear all ports
PORTA = 0b00000000;
PORTB = 0b00000000;
PORTC = 0b00000000;
// Initialize the variable holding the handle for the last
// transmission
USBOutHandle = 0;
USBInHandle = 0;
USBDeviceInit();
}
// Process input and output
void ProcessIO(void)
{
// If we are not in the configured state just return
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
//Check if data was received from the host.
if(!HIDRxHandleBusy(USBOutHandle))
{
switch(ReceivedDataBuffer[0])
{
case 0x80: // trigger the nichrome
HEAT ^= 1;
// (heats slightly more than a second with Trip = 100000)
//Trip = ReceivedDataBuffer[1]*100000;
// default one second if buf[2] not defined host-side
//if (ReceivedDataBuffer[1] == 0) Trip = 100000;
break;
case 0x81: // transfer parameters
cycles = ReceivedDataBuffer[1];
// temperatures are passed as double their actual values
tempI = ReceivedDataBuffer[2]/2.0;
tempD = ReceivedDataBuffer[3]/2.0;
tempA = ReceivedDataBuffer[4]/2.0;
tempE = ReceivedDataBuffer[5]/2.0;
tempF = ReceivedDataBuffer[6]/2.0;
timeI = ReceivedDataBuffer[7];
timeD = ReceivedDataBuffer[8];
timeA = ReceivedDataBuffer[9];
timeE = ReceivedDataBuffer[10];
timeF = ReceivedDataBuffer[11];
//Trip = cycles*100000; // make sure it works
Standalone(); // pass parameters? or no need?
break;
case 0x82: // Read the temperature
LIGHT = 0;
HEAT = 0;
GODONE = 1; // start conversion
while(GODONE); // wait till done
ToSendDataBuffer[0] = 0x82;
ToSendDataBuffer[1] = ADRESH; // return High byte
ToSendDataBuffer[2] = ADRESL; // return Low byte
// Transmit the response to the host
if(!HIDTxHandleBusy(USBInHandle))
{
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
}
break;
}
// Re-arm the OUT endpoint for the next packet
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
}
}
void ShutoffCheck(void)
{ // can't hurt to have a safety shutoff for the nichrome
if (Trip > 0)
{
Trip--;
}
else
{
LIGHT = 0;
}
}
// Timer1 Interrupt
/* With Fosc/4 as source, and 1:8 PS, we find that 30 interrupts in
a row equal about 0.983 seconds, which is precise enough for PCR ;) */
void interrupt isr(void){
if((TMR1IF)&&(TMR1IE)) {
if (licks++ >= 30) {
licks = 0;
secs++;
}
TMR1IF = 0;
}
}
void Standalone(void)
{
TMR1IF = 0; // overflow cleared
TMR1IE = 1; // enable interrupt
HEAT = 1;
while(secs < timeD);
HEAT = 0;
}
#endif
#V 0.4
import os
import sys, time
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from ctypes import *
try:
toggle = CDLL("toggle.so") # make toggle.main(seconds) work
read = CDLL("temperature.so")
lauschen = CDLL("lauschen.so")
transfer = CDLL("transfer.so") # no parameter value can exceed 255 [ff]
except:
lauschen = CDLL('lauschen.dll')
toggle = CDLL('toggle.dll')
read = CDLL('temperature.dll')
transfer = CDLL('transfer.dll') # no parameter value can exceed 255 [ff]
class main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(550, 430)
self.setMinimumSize(550, 350)
self.setWindowTitle('Poopie')
self.center()
# no menubar needed
# declare timer here (lauschen function might need it before imGange)
self.timer = QTimer()
self.turtle = 0
# update statusbar every tenth of a second
self.lyssna = QTimer()
self.lyssna.timeout.connect(self.lauschen)
self.lyssna.start(100)
self.pieces()
def pieces(self):
# first, a little humor:
Caveman = QIcon('C:\Kennys bajs\caveman.png')
fraga = "Kenny's program is not set as the default USB thermocycler control software \
on this computer... Would you like to set it now?"
#reply = QMessageBox.question(self, 'Default?',fraga,QMessageBox.Yes | QMessageBox.No)
# enable main window widget placement
poop = QWidget()
self.setCentralWidget(poop)
# lay out tabs
self.Tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget() # add in imGange()
self.Tabs.addTab(self.tab1, 'Settings')
self.Tabs.addTab(self.tab2, 'Info')
self.Tabs.setTabIcon(0, QIcon('C:\Kennys bajs\caveman.png'))
# tab1 ########################################
self.bd = QPushButton('[Defaults]')
self.bd.clicked.connect(self.defaultTemps)
self.bRun = QPushButton('Run')
self.bRun.clicked.connect(self.Standalone)
self.cBox = QCheckBox('Standalone')
tab1L = QVBoxLayout()
h1 = QHBoxLayout()
h2 = QHBoxLayout()
h3 = QHBoxLayout()
h4 = QHBoxLayout()
h5 = QHBoxLayout()
h6 = QHBoxLayout()
h7 = QHBoxLayout()
h8 = QHBoxLayout()
for h in [h1, h2, h3, h4, h5, h6, h7, h8]:
tab1L.addLayout(h)
h.t = QLabel(' ')
h.addWidget(h.t, Qt.AlignLeft)
if h in [h1, h4, h5, h6, h8]:
h.temp = QDoubleSpinBox()
h.temp.setAlignment(Qt.AlignRight)
h.temp.setFrame(False)
h.temp.setDecimals(1)
h.temp.setSingleStep(0.5)
h.temp.setSuffix(' Celcius')
h.time = QSpinBox()
h.time.setAlignment(Qt.AlignRight)
h.time.setFrame(False)
h.time.setPrefix('for ')
h.addWidget(h.temp, Qt.AlignLeft)
h.addWidget(h.time, Qt.AlignLeft)
h.unit = QLabel(' ')
h.addWidget(h.unit, Qt.AlignLeft)
h3.cyc = QSpinBox()
h3.cyc.setRange(1, 40)
h3.cyc.setValue(30)
h3.cyc.setAlignment(Qt.AlignRight)
h3.cyc.setFrame(False)
h3.cyc.setSuffix(' Cycles')
h3.insertWidget(0, h3.cyc, Qt.AlignLeft)
h1.temp.setRange(90, 99)#I
h1.temp.setValue(94)
h4.temp.setRange(90, 99)#D
h4.temp.setValue(93)
h5.temp.setRange(50, 65)#A
h5.temp.setValue(64)
h6.temp.setRange(70, 80)#E
h6.temp.setValue(72)
h8.temp.setRange(70, 74)#F
h8.temp.setValue(72)
h1.time.setRange(0, 10)#I
h1.time.setValue(5)
h4.time.setRange(20, 30)#D
h4.time.setValue(30)
h5.time.setRange(20, 40)#A
h5.time.setValue(30)
h6.time.setRange(20, 40)#E
h6.time.setValue(30)
h8.time.setRange(0, 15)#F
h8.time.setValue(10)
h1.time.setSuffix(' minutes')
h4.time.setSuffix(' seconds')
h5.time.setSuffix(' seconds')
h6.time.setSuffix(' seconds')
h8.time.setSuffix(' minutes')
h1.t.setText('Initialization: ')
h2.t.setText('------------------------------------------------------------------------------------------------')
h2.t.setAlignment(Qt.AlignHCenter)
h3.t.setText(' of:')
h4.t.setText('Denaturation: ')
h5.t.setText('Annealing: ')
h6.t.setText('Elongation: ')
h7.t.setText('------------------------------------------------------------------------------------------------')
h7.t.setAlignment(Qt.AlignHCenter)
h8.t.setText('Final Annealing: ')
tab1L.addWidget(self.bd)
runLevel = QHBoxLayout()
tab1L.addLayout(runLevel)
runLevel.addWidget(self.bRun)
runLevel.addWidget(self.cBox)
self.tab1.setLayout(tab1L)
# 'load/save config' buttons.. settings saved to .dat file?
#globals for later
self.cycles = h3.cyc
self.tempI = h1.temp
self.tempD = h4.temp
self.tempA = h5.temp
self.tempE = h6.temp
self.tempF = h8.temp
self.timeI = h1.time
self.timeD = h4.time
self.timeA = h5.time
self.timeE = h6.time
self.timeF = h8.time
# tab2 ########################################
icon = QLabel(self.tab2)
icon.setPixmap(QPixmap('C:\icons\caveman_rock.png'))
#how to resize a pixmapped label??
lVersion = QLabel('Development Version 0.5')
munch = QLabel('[a product of/by/for DIYbio]')
tab2L = QGridLayout()
tab2L.addWidget(icon, 0, 0)
tab2L.addWidget(lVersion, 1, 0)
tab2L.addWidget(munch, 2, 0)
self.tab2.setLayout(tab2L)
# tab3 ########################################
tab3L = QGridLayout()
self.bHalt = QPushButton('Terminate! (Exit Program)')
self.bHalt.clicked.connect(app.quit) # why cant prompt 'are you sure?'???
self.lC = QLabel('You may now unplug that hunk of awesomeness')
self.lI = QLabel(' ') # ^ this shows if 'standalone' option is selected
self.lD = QLabel(' ')
self.lA = QLabel(' ')
self.lE = QLabel(' ')
self.lF = QLabel(' ')
self.testLabel = QLabel(' ')
tab3L.addWidget(self.testLabel, 5, 1)
self.elapsedLabel = QLabel(' ')
tab3L.addWidget(self.elapsedLabel, 7, 0)
self.leftLabel = QLabel(' ')#('Calculating Remaining Time...')
tab3L.addWidget(self.leftLabel, 8, 0)
self.stageLabel = QLabel(' ')
tab3L.addWidget(self.stageLabel, 7, 1)
self.tempLabel = QLabel(' ')
tab3L.addWidget(self.tempLabel, 6, 1)
tab3L.addWidget(self.lI, 0, 0, 1, 2)
tab3L.addWidget(self.lC, 2, 0)
tab3L.addWidget(self.lD, 1, 1)
tab3L.addWidget(self.lA, 2, 1)
tab3L.addWidget(self.lE, 3, 1)
tab3L.addWidget(self.lF, 4, 0, 1, 2)
tab3L.addWidget(self.bHalt, 6, 0)
self.pbar = QProgressBar(self)
tab3L.addWidget(self.pbar, 5, 0)
#alternative: QProgressDialog, if popup bar wanted
self.tab3.setLayout(tab3L)
# place tabs onto main window
mainLayout = QGridLayout()
mainLayout.addWidget(self.Tabs)
poop.setLayout(mainLayout)
def defaultTemps(self):
self.cycles.setValue(30)
self.tempI.setValue(94)
self.tempD.setValue(93)
self.tempA.setValue(64)
self.tempE.setValue(72)
self.tempF.setValue(72)
self.timeI.setValue(5)
self.timeD.setValue(30)
self.timeA.setValue(30)
self.timeE.setValue(30)
self.timeF.setValue(10)
def closeEvent(self, event):
if self.Tabs.isTabEnabled(2):
reply = QMessageBox.question(self, 'Message', "Don't quit! The PCR is running!!", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
def erledigt(self):
self.setWindowTitle('Complete!')
self.Tabs.setTabEnabled(0, True)
self.Tabs.setTabEnabled(1, True)
self.Tabs.removeTab(2)
self.Tabs.setCurrentIndex(0)
self.timer.stop()
self.fart(' ') # reset stage label
self.stepper = 0 # reset the progress bar
def Standalone(self):
self.setWindowTitle('Running...')
self.Tabs.addTab(self.tab3, 'RUNNING')
self.Tabs.setCurrentIndex(2)
self.Tabs.setTabEnabled(0, False)
self.Tabs.setTabEnabled(1, False)
# some stuff relies on the Run tab being 'current index'
# so if Info tab is enabled, be sure to account for that
if self.cBox.isChecked() == 1:
transfer.main( self.cycles.value(),
c_double(self.tempI.value()),
c_double(self.tempD.value()),
c_double(self.tempA.value()),
c_double(self.tempE.value()),
c_double(self.tempF.value()),
self.timeI.value(),
self.timeD.value(),
self.timeA.value(),
self.timeE.value(),
self.timeF.value() )
else:
self.imGange()
def imGange(self):
#display values
if self.timeI.value() == 0:
self.lI.setText('[Initialization disabled]')
else:
self.lI.setText('Initialization: '+str(self.tempI.value())+' for '+str(self.timeI.value())+' minutes')
if self.timeF.value() == 0:
self.lF.setText('[Final Anneal disabled]')
else:
self.lF.setText('Final Annealing: '+str(self.tempF.value())+' for '+str(self.timeF.value())+' minutes')
self.lC.setText(str(self.cycles.value())+' cycles of :')
self.lD.setText('Denaturation: '+str(self.tempD.value())+' for '+str(self.timeD.value())+' seconds')
self.lA.setText('Annealing: '+str(self.tempA.value())+' for '+str(self.timeA.value())+' seconds')
self.lE.setText('Elongation: '+str(self.tempE.value())+'for '+str(self.timeE.value())+' seconds')
self.seconds = 0
self.temperature = 20 # = read.main()
self.salt = 0
self.timer.timeout.connect(self.runner)
self.timer.start(1000)
# dictionify the values when Run is clicked
self.parameters = {
'cycles': self.cycles.value(),
'tempI': self.tempI.value(),
'tempD': self.tempD.value(),
'tempA': self.tempA.value(),
'tempE': self.tempE.value(),
'tempF': self.tempF.value(),
'timeI': self.timeI.value(),
'timeD': self.timeD.value(),
'timeA': self.timeA.value(),
'timeE': self.timeE.value(),
'timeF': self.timeF.value() }
self.cycleThread = CycleThread(self.parameters)
self.connect(self.cycleThread, SIGNAL('update(QString)'), self.fart)
self.connect(self.cycleThread, SIGNAL('k(QString)'), self.degr)
self.connect(self.cycleThread, SIGNAL('bar(QString)'), self.bar)
self.cycleThread.start()
def runner(self):
self.seconds = self.seconds + 1
minutes = self.seconds / 60
overr = self.seconds-(minutes*60)
self.elapsedLabel.setText('Gone: '+str(minutes)+'m'+str(overr)+'s')
def bar(self, val):
self.salt = (int(val)*1. / self.cycles.value())*100 # *1. for decimals
self.pbar.setValue(self.salt)
def degr(self, degrees):
self.tempLabel.setText('Current Temp: '+degrees+'C')
def fart(self, text):
self.stageLabel.setText(text)
def lauschen(self):
if lauschen.main() < 0:
self.statusBar().showMessage('PCR not connected :/')
self.bRun.setEnabled(False)
if self.Tabs.currentIndex() == 2:
if self.turtle < 50:
self.turtle = self.turtle + 1
self.statusBar().showMessage('Plug it back in! Quick! '+str(5-self.turtle/10))
# ^ depends on lyssna-timer's pace
else:
app.quit()
elif lauschen.main() == 0:
self.turtle = 0
self.statusBar().showMessage('PCR connected :)')
self.bRun.setEnabled(True)
#####################################################
class CycleThread(QThread):
def __init__(self, parameters):
QThread.__init__(self)
self.parameters = parameters
def run(self):
prm = self.parameters
self.derp = 20 # = read.main()
self.tt = 'vaxlar'
for i in range(1, prm['cycles']+1):
while self.derp < prm['tempD']:
time.sleep(.2)
self.derp = self.derp + 1
toggle.main()
self.emit(SIGNAL('k(QString)'), str(self.derp))
self.dots()
# denature:
for s in range(0, prm['timeD']+1):
if self.derp < prm['tempD']: # elaborate (max .5C less?)
toggle.main()
time.sleep(.2)
self.emit(SIGNAL('update(QString)'),
'C.'+str(i)+'; D('+str(s)+'/'+str(prm['timeD'])+')')
while self.derp > prm['tempA']:
time.sleep(.2)
self.derp = self.derp - 1
self.emit(SIGNAL('k(QString)'), str(self.derp))
self.dots()
# anneal:
for s in range(0, prm['timeA']+1):
if self.derp < prm['tempA']: # elaborate (max .5C less?)
toggle.main()
time.sleep(.2)
self.emit(SIGNAL('update(QString)'),
'C.'+str(i)+'; A('+str(s)+'/'+str(prm['timeA'])+')')
while self.derp < prm['tempE']:
time.sleep(.2)
self.derp = self.derp + 1
toggle.main()
self.emit(SIGNAL('k(QString)'), str(self.derp))
self.dots()
# elongate:
for s in range(0, prm['timeE']+1):
if self.derp < prm['tempD']: # elaborate (max .5C less?)
toggle.main()
time.sleep(.2)
self.emit(SIGNAL('update(QString)'),
'C.'+str(i)+'; E('+str(s)+'/'+str(prm['timeE'])+')')
self.emit(SIGNAL('bar(QString)'), str(i))
def dots(self):
self.tt = '%s.' % self.tt
if len(self.tt) > 9:
self.tt = 'vaxlar'
self.emit(SIGNAL('update(QString)'), self.tt)
def __del__(self): #for safety? i guess?
self.wait()
#####################################################
if __name__ == '__main__':
app = QApplication(sys.argv)
main = main()
main.show()
sys.exit(app.exec_())
/********************************************************************
FileName: usb_descriptors.c
Dependencies: See INCLUDES section
Processor: PIC18 or PIC24 USB Microcontrollers
Hardware: The code is natively intended to be used on the following
hardware platforms: PICDEM™ FS USB Demo Board,
PIC18F87J50 FS USB Plug-In Module, or
Explorer 16 + PIC24 USB PIM. The firmware may be
modified for use on other USB platforms by editing the
HardwareProfile.h file.
Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
Company: Microchip Technology, Inc.
Software License Agreement:
The software supplied herewith by Microchip Technology Incorporated
(the “Company”) for its PIC® Microcontroller is intended and
supplied to you, the Company’s customer, for use solely and
exclusively on Microchip PIC Microcontroller products. The
software is owned by the Company and/or its supplier, and is
protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the
user to criminal sanctions under applicable laws, as well as to
civil liability for the breach of the terms and conditions of this
license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*********************************************************************
-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 BYTE 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 BYTE 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
********************************************************************/
#ifndef __USB_DESCRIPTORS_C
#define __USB_DESCRIPTORS_C
/** INCLUDES *******************************************************/
#include "usb.h"
#include "usb_function_hid.h"
/* Device Descriptor */
ROM 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
0xFA25, // Product ID
0x0002, // 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 */
ROM BYTE configDescriptor1[]={
/* Configuration Descriptor */
0x09,//sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
0x29,0x00, // 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
2, // 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
DSC_HID, // HID descriptor type
0x11,0x01, // 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
HID_RPT01_SIZE,0x00,//sizeof(hid_rpt01), // Size of the report descriptor
/* Endpoint Descriptor */
0x07,/*sizeof(USB_EP_DSC)*/
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
HID_EP | _EP_IN, //EndpointAddress
_INTERRUPT, //Attributes
0x40,0x00, //size
0x01, //Interval
/* Endpoint Descriptor */
0x07,/*sizeof(USB_EP_DSC)*/
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
HID_EP | _EP_OUT, //EndpointAddress
_INTERRUPT, //Attributes
0x40,0x00, //size
0x01 //Interval
};
//Language code string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[1];}sd000={
sizeof(sd000),USB_DESCRIPTOR_STRING,{0x0409
}};
//Manufacturer string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[8];}sd001={
sizeof(sd001),USB_DESCRIPTOR_STRING,
{'F','a','r','t','h','e','a','d'}};
//Product string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[10];}sd002={
sizeof(sd002),USB_DESCRIPTOR_STRING,
{'S','t','u','p','i','d','f','a','c','e'}};
//Device serial number string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[4];}sd003={
sizeof(sd002),USB_DESCRIPTOR_STRING,
{'1','1','1','1'}}; // or should it be sizeof(sd003) and add ROM BYTE at bottom?
//Class specific descriptor - HID
ROM struct{BYTE report[HID_RPT01_SIZE];}hid_rpt01={
{
0x06, 0x00, 0xFF, // Usage Page = 0xFF00 (Vendor Defined Page 1)
0x09, 0x01, // Usage (Vendor Usage 1)
0xA1, 0x01, // Collection (Application)
0x19, 0x01, // Usage Minimum
0x29, 0x40, // Usage Maximum //64 input usages total (0x01 to 0x40)
0x15, 0x01, // Logical Minimum (data bytes in the report may have minimum value = 0x00)
0x25, 0x40, // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
0x75, 0x08, // Report Size: 8-bit field size
0x95, 0x40, // Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output", or "Feature" item)
0x81, 0x00, // Input (Data, Array, Abs): Instantiates input packet fields based on the above report size, count, logical min/max, and usage.
0x19, 0x01, // Usage Minimum
0x29, 0x40, // Usage Maximum //64 output usages total (0x01 to 0x40)
0x91, 0x00, // Output (Data, Array, Abs): Instantiates output packet fields. Uses same report size and count as "Input" fields, since nothing new/different was specified to the parser since the "Input" item.
0xC0} // End Collection
};
//Array of configuration descriptors
ROM BYTE *ROM USB_CD_Ptr[]=
{
(ROM BYTE *ROM)&configDescriptor1
};
//Array of string descriptors
ROM BYTE *ROM USB_SD_Ptr[]=
{
(ROM BYTE *ROM)&sd000,
(ROM BYTE *ROM)&sd001,
(ROM BYTE *ROM)&sd002
};
/** EOF usb_descriptors.c ***************************************************/
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment