apostila de microcontroladores
TRANSCRIPT
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
1/25
APOSTILA DEMICROCONTROLA
DORES PIC EPEFIFRICOS264
}float get_float_usb(){char s[20];float
f;get_string_usb(s, 20);f
=atof(s);return(f);}#endif
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
2/25
BIBLIOTECA
usb_san_desc.h:////////////////////////////////////////
///////////////////////////////////////
usb_desc_cdc.h ////////////////
////////////////////////////////////////
///////////////////////#IFNDEF
__USB_DESCRIPTORS__#DEFINE
__USB_DESCRIPTORS__
#include////////////////////////////
////////////////////////////////////////
//// start config descriptor //
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
3/25
/ right now we only support
one configurationdescriptor./// the config,
interface, class,
and endpoint goes into
this array./////////////////////////
////////////////////////////////////////
////#DEFINE USB_TOTAL
_CONFIG_LEN 67//config+interface+class+en
dpoint+endpoint (2 endpoin
ts)const charUSB_CONFIG_DESC[] =
{//IN ORDER TO
COMPLY WITH
WINDOWS HOSTS, THE
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
4/25
ORDER OF THIS ARRAY
MUST BE:// config(s)//interface(s)// class(es)//
endpoint(s)//config_descrip
tor for config index
1USB_DESC_CONFIG_L
EN, //length of descriptor si
ze ==0USB_DESC_CONF
IG_TYPE, //constant CONFIGURATION (CONFIGU
RATION 0x02) ==1USB_
TOTAL_CONFIG_LEN,0,//size of all data returned f
or this config ==2,32, //nu
mber of interfaces this devi
ce supports ==40x01, //iden
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
5/25
tifier for this
configuration. (IF we hadmore than one configuratio
ns) ==50x00, //index of stri
ng descriptor for this config
uration ==60xC0, //bit
6=1 if self powered, bit
5=1 if supports remote wak
eup (we don't), bits 0-4 unused and bit7=1 ==70x
32, //maximum bus power
required (maximummilliamperes/2) (0x32
= 100mA) ==8//interface
descriptor 0 (comm class
interface)USB_DESC_INT
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
6/25
ERFACE_LEN, //length of
descriptor =9USB_DESC_INTERFACE_TYPE, //cons
tant INTERFACE (INTER
FACE 0x04) =100x00,
//number defining this
interface (IF we had
more than one
interface) ==110x00, //alternate setting ==121, //numb
er of endpoints ==130x02, /
/classcode, 02 = Comm Interface
Class ==140x02, //subclass
code, 2 = Abstract ==150x
01, //protocol code, 1 = v.2
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
7/25
5ter ==160x00, //index of s
tring descriptor for interface ==17//class descriptor
[functional
header]5, //length of descri
ptor ==180x24, //dscriptor t
ype (0x24 == ) ==190,
//sub type (0=functional
header)==200x10,0x01, // ==21,22
//cdc version
APOSTILA DE
MICROCONTROLA
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
8/25
DORES PIC E
PEFIFRICOS265
//class descriptor [acm
header]4, //length of descriptor ==230x24, //dscriptor t
ype (0x24 == ) ==242, //su
b type (2=ACM) ==252,//capabilities ==26 //we
support Set_Line_Coding,
Set_Control_Line_State,
Get_Line_Coding, and
the notification
Serial_State.//class
descriptor [union
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
9/25
header]5, //length of descri
ptor ==270x24, //dscriptor type (0x24 == ) ==286, //su
b type (6=union) ==290,
//master intf ==30 //The
interface number of the
Communication or Dat a Cl
ass interface, designated
as the masterorcontrollinginterface for the
union.1,
//save intf0 ==31 //Interface number of first slave
or associated interface in
the union. *//class
descriptor [call mgmt
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
10/25
header]5, //length of descri
ptor ==320x24, //dscriptor type (0x24 == ) ==331,
//sub type (1=call
mgmt) ==340, //capabilities
==35
//device does not handle cal
l management itself 1, //dat
a interface ==36//interface number of data c
lass interface//endpoint
descriptor USB_DESC_ENDPOINT_LEN, //length of
descriptor ==37USB_DES
C_ENDPOINT_TYPE, //co
nstant ENDPOINT (ENDP
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
11/25
OINT 0x05) ==38USB_CD
C_COMM_IN_ENDPOINT | 0x80, //endpoint number
and
direction0x03, //transfer ty
pe supported (0x03 is interr
upt) ==40USB_CDC_CO
MM_IN_SIZE,0x00, //max
imum packet size supported ==41,42250, //polling
interval, in ms.
(cant be smaller than 10) ==43//interface descriptor 1
(data class
interface)USB_DESC_INT
ERFACE_LEN, //length of
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
12/25
descriptor =44USB_DESC
_INTERFACE_TYPE, //constant INTERFACE (INTE
RFACE 0x04) =450x01,
//number defining this
interface (IF we had
more than one
interface) ==460x00, //alter
nate setting ==472, //number of endpoints ==480x0A,
//class code, 0A = Data
Interface Class ==490x00, //subclass code ==500x00, //
protocol code ==510x00, //i
ndex of string descriptor fo
r interface ==52//endpoint
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
13/25
descriptor USB_DESC_EN
DPOINT_LEN, //length ofdescriptor ==60USB_DES
C_ENDPOINT_TYPE, //co
nstant ENDPOINT (ENDP
OINT 0x05) ==61USB_CD
C_DATA_OUT_ENDPOI
NT, //endpoint number and
direction (0x02 = EP2 OUT) ==620x02, //transfer type
supported (0x02 is bulk) ==
63// make8(USB_CDC_DATA_OUT_SIZE,0),make
8(USB_CDC_DATA_OUT
_SIZE,1), //maximum pack
et size supported ==64,65U
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
14/25
SB_CDC_DATA_OUT_SI
ZE & 0xFF,(USB_CDC_DATA_OUT_
SIZE >> 8) & 0xFF,
//maximum packet size
supported==64, 65250,
//polling interval, in ms.
(cant be smaller than 10) =
=66//endpointdescriptor USB_DESC_EN
DPOINT_LEN, //length of
descriptor ==53USB_DESC_ENDPOINT_TYPE, //co
nstant ENDPOINT (ENDP
OINT 0x05) ==54USB_CD
C_DATA_IN_ENDPOINT
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
15/25
| 0x80, //endpoint number
and direction (0x82 =EP2 IN) ==550x02, //transf
er type supported (0x02 is
bulk) ==56// make8(USB_
CDC_DATA_IN_SIZE,0),
make8(USB_CDC_DATA
_IN_SIZE,1), //maximum p
acket size supported ==57,58USB_CDC_DATA_IN_
SIZE & 0xFF, (USB_CDC
_DATA_IN_SIZE >> 8) &0xFF, //maximum packet si
ze supported ==64, 65250,
//polling interval, in ms.
(cant be smaller than 10) =
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
16/25
=59};//****** BEGIN
CONFIG DESCRIPTORLOOKUP
TABLES ********//since
we can't make pointers to
constants in certain pic16s,
this is an offset table to
find// a specific descriptor
in the above table.//themaximum number of
interfaces seen on any
config//for example, ifconfig 1 has 1 interface and
config 2 has 2 interfaces
you must define this as
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
17/25
2#define USB_MAX_NU
M_INTERFACES 2
APOSTILA DEMICROCONTROLADORES PIC E
PEFIFRICOS266//define how many
interfaces there are perconfig. [0] is the first
config, etc.const
char USB_NUM_INTERF
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
18/25
ACES[USB_NUM_CONFI
GURATIONS]={2};//define where to find class
descriptors//first dimension
is the config
number //second dimension
specifies which
interface//last dimension
specifies which class in thisinterface to get, but most
will only have 1 class per
interface//if a classdescriptor is not valid, set
the value to
0xFFFFconst int16 USB_C
LASS_DESCRIPTORS[U
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
19/25
SB_NUM_CONFIGURAT
IONS][USB_MAX_NUM_INTERFACES][4]={//conf
ig 1//interface 0//class 1-
418,23,27,32,//interface
1//no classes for this
interface0xFFFF,0xFFFF,0
xFFFF,0xFFFF};#if
(sizeof(USB_CONFIG_DESC)
!= USB_TOTAL_CONFIG
_LEN)#errorUSB_TOTAL_CONFIG_L
EN not defined
correctly#endif /////////////////
////////////////////////////////////////
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
20/25
/////////////// start device desc
riptors/////////////////////////////////////////////////////////////////////
const char
USB_DEVICE_DESC[US
B_DESC_DEVICE_LEN]
={//starts of with
device configuration. only
onepossibleUSB_DESC_DEVI
CE_LEN, //the length of
this report ==00x01, //theconstant DEVICE
(DEVICE
0x01) ==10x10,0x01,
//usb version in
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
21/25
bcd ==2,30x02, //class
code.0x02=Communication
Device Class ==40x00,
//subclass code ==50x00,
//protocol code
==6USB_MAX_EP0_PAC
KET_LENGTH, //max
packet size for endpoint 0.(SLOW SPEED
SPECIFIES 8)
==70xD8,0x04, //vendor id(0x04D8 is Microchip)0x0
A,0x00, //product id//
RR2 cambiado para 0x61,0
x04, //vendor id
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
22/25
(0x04D8 is Microchip, or is
it 0x0461 ??) ==8,9//compatibilidad con .inf
0x33,0x00, //product
id ==10,11// de
Microchip0x00,0x01, //dev
ice release
number ==12,130x01,
//index of string descriptionof manufacturer. therefore
we point to string_1 array
(see below) ==140x02,//index of string
descriptor of the
product ==150x00, //index
of string descriptor of serial
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
23/25
number ==16USB_NUM_
CONFIGURATIONS //number of
possible configurations ==1
7};//the offset of the
starting location of each
string. offset[0] is the start
of string 0, offset[1] is the
start of string 1,etc.char USB_STRING_D
ESC_OFFSET[]={0,4,12};
char constUSB_STRING_DESC[]={/
/string 04, //length of string
indexUSB_DESC_STRIN
G_TYPE, //descriptor type
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
24/25
0x03
(STRING)0x09,0x04, //Microsoft Defined for US-
English//string
18,USB_DESC_STRING_
TYPE,112,0,105,0,99,0,//st
ring 232,
APOSTILA DEMICROCONTROLA
DORES PIC EPEFIFRICOS
267
-
8/2/2019 APOSTILA DE MICROCONTROLADORES
25/25
USB_DESC_STRING_TY
PE,115,0,97,0,110,0,100,0,114,0,111,0,' ',0,106,0,117,
0,99,0,97,0,' ',0,117,0,115,0
,98,0};#ENDIF