We need the new USB and HID core. We'll rev the bootloader when production hardware is closerpull/18/head
parent
e39eb807c9
commit
24865f8507
@ -1 +0,0 @@
|
|||||||
This code is derived from the Arduino core. Keyboardio makes no claims as to the license of the original software
|
|
@ -1,28 +0,0 @@
|
|||||||
model01.name=Keyboardio model01
|
|
||||||
model01.vid.0=0x1209
|
|
||||||
model01.pid.0=0x2301
|
|
||||||
model01.upload.tool=avrdude
|
|
||||||
model01.upload.protocol=avr109
|
|
||||||
model01.upload.maximum_size=28672
|
|
||||||
model01.upload.maximum_data_size=2560
|
|
||||||
model01.upload.speed=57600
|
|
||||||
model01.upload.disable_flushing=true
|
|
||||||
model01.upload.use_1200bps_touch=true
|
|
||||||
model01.upload.wait_for_upload_port=false
|
|
||||||
|
|
||||||
model01.bootloader.tool=avrdude
|
|
||||||
model01.bootloader.low_fuses=0xff
|
|
||||||
model01.bootloader.high_fuses=0xd8
|
|
||||||
model01.bootloader.extended_fuses=0xcb
|
|
||||||
model01.bootloader.file=caterina/Caterina-model01.hex
|
|
||||||
model01.bootloader.unlock_bits=0x3F
|
|
||||||
model01.bootloader.lock_bits=0x2F
|
|
||||||
|
|
||||||
model01.build.mcu=atmega32u4
|
|
||||||
model01.build.f_cpu=16000000L
|
|
||||||
model01.build.vid=0x1209
|
|
||||||
model01.build.pid=0x2301
|
|
||||||
model01.build.usb_product="Keyboardio model01"
|
|
||||||
model01.build.core=keyboardio
|
|
||||||
model01.build.variant=model01
|
|
||||||
model01.build.extra_flags={build.usb_flags}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +0,0 @@
|
|||||||
LUFA: 111009
|
|
||||||
make: 3.81
|
|
||||||
avrdude: 5.11.1
|
|
||||||
avr-libc: 1.6.7
|
|
||||||
binutils-avr: 2.19
|
|
||||||
gcc-avr 4.3.3
|
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
@ -1,237 +0,0 @@
|
|||||||
:107000005FC0000078C0000076C0000074C00000BF
|
|
||||||
:1070100072C0000070C000006EC000006CC00000B4
|
|
||||||
:107020006AC0000068C000007EC4000064C00000A8
|
|
||||||
:1070300062C0000060C000005EC000005CC00000D4
|
|
||||||
:107040005AC00000BFC0000056C0000054C000007D
|
|
||||||
:1070500052C0000050C000004EC000004CC00000F4
|
|
||||||
:107060004AC0000048C0000046C0000044C0000004
|
|
||||||
:1070700042C0000040C000003EC000003CC0000014
|
|
||||||
:107080003AC0000038C0000036C0000034C0000024
|
|
||||||
:1070900032C0000030C000002EC000002CC0000034
|
|
||||||
:1070A0002AC0000028C0000026C00000FC3D153E9C
|
|
||||||
:1070B000B93E153EB93E4A3E6C3EB93E8C3E9E3EC0
|
|
||||||
:1070C00011241FBECFEFDAE0DEBFCDBF11E0A0E09C
|
|
||||||
:1070D000B1E0ECEFFDE702C005900D92A63AB107D2
|
|
||||||
:1070E000D9F721E0A6EAB1E001C01D92A63BB207A4
|
|
||||||
:1070F000E1F716D381C684CF84E08093E9008091C4
|
|
||||||
:10710000E80085FD0EC08091E8008B778093E80051
|
|
||||||
:107110008091E80082FDF3CF8EB38111F9CF80E03A
|
|
||||||
:1071200008958091F100089593E09093E900909183
|
|
||||||
:10713000E80095FF03C08093F10008959091E80066
|
|
||||||
:107140009E779093E8009091E80090FDF4CF9EB375
|
|
||||||
:107150009111F9CF0895F89410926F001092810068
|
|
||||||
:10716000109285001092840081E085BF15BE0C94BA
|
|
||||||
:107170000000089584B7877F84BF88E10FB6F89434
|
|
||||||
:1071800080936000109260000FBE90E080E80FB620
|
|
||||||
:10719000F89480936100909361000FBE81E085BFF9
|
|
||||||
:1071A00082E085BFE1E6F0E090E8908310821092E3
|
|
||||||
:1071B00089009AEF9093880080936F0083E080931A
|
|
||||||
:1071C000810099C31F920F920FB60F9211242F9333
|
|
||||||
:1071D0008F939F93EF93FF931092850010928400FA
|
|
||||||
:1071E000E0E0F0E085919491019649F08091A6014C
|
|
||||||
:1071F0009091A70101969093A7018093A601FF911A
|
|
||||||
:10720000EF919F918F912F910F900FBE0F901F9034
|
|
||||||
:10721000189542E061EC82E001D342E161E883E04D
|
|
||||||
:10722000FDD242E160E884E0F9C28091AE01982F7E
|
|
||||||
:107230009F77913239F59091AF01903291F0913270
|
|
||||||
:1072400009F5813AF9F48091E800877F8093E8009E
|
|
||||||
:1072500067E070E083E091E017D48091E8008B77DD
|
|
||||||
:107260000FC0813279F48091E800877F8093E80035
|
|
||||||
:1072700067E070E083E091E060D48091E8008E7771
|
|
||||||
:107280008093E80008954F925F926F927F928F9261
|
|
||||||
:107290009F92AF92BF92CF92DF92EF92FF92CF93E5
|
|
||||||
:1072A000DF9384E08093E9008091E80082FF29C2A7
|
|
||||||
:1072B00023DFD82F853481F48CE49DE19093A701DE
|
|
||||||
:1072C0008093A60107B600FCFDCFF999FECF81E1BE
|
|
||||||
:1072D00080935700E89503C0843521F40DDF8DE0DD
|
|
||||||
:1072E00023DFE6C18C34D9F38035C9F3843721F428
|
|
||||||
:1072F00084E41ADF80E0F4CF813611F489E5F0CF21
|
|
||||||
:10730000813491F4F9DEC82FF7DE90E0880F991FE1
|
|
||||||
:107310002C2F30E0322F2227330F822B932BAA27DA
|
|
||||||
:1073200097FDA095BA2F98C1803711F483E5D8CF87
|
|
||||||
:10733000833549F4CCE9D1E08991F6DE21E0C33A06
|
|
||||||
:10734000D207D1F7B5C1863521F481E3EDDE80E3C4
|
|
||||||
:10735000C7CF833731F487E8E7DE85E9E5DE8EE1E4
|
|
||||||
:10736000BFCF8536B9F4E0E0F0E083E095E08093AC
|
|
||||||
:107370005700E89507B600FCFDCF90935700E895BD
|
|
||||||
:1073800007B600FCFDCFE058FF4FE11520E7F207FC
|
|
||||||
:1073900071F7A5CF823719F4E1E0F0E00EC0863432
|
|
||||||
:1073A00019F4E0E0F0E009C08E3419F4E3E0F0E015
|
|
||||||
:1073B00004C0813539F4E2E0F0E089E080935700C1
|
|
||||||
:1073C00084918ECF823631F489E5AEDE80E0ACDE8A
|
|
||||||
:1073D00080E886CF823419F0873609F0D5C0109244
|
|
||||||
:1073E000A7011092A60188DED82E86DEE82E84DE64
|
|
||||||
:1073F000C82F8BEB8C0F823010F08FE371CFF12C04
|
|
||||||
:10740000FD2810926F00D73609F045C081E18093C6
|
|
||||||
:107410005700E8955701912CD1E0A114B10409F46B
|
|
||||||
:10742000AFC0C090A801D090A901E090AA01F0904F
|
|
||||||
:10743000AB01C634B9F4E92DF0E0EC29FD298491C3
|
|
||||||
:1074400073DE992069F082E0C80ED11CE11CF11CAA
|
|
||||||
:10745000C092A801D092A901E092AA01F092AB01DA
|
|
||||||
:107460009D2615C0D701C601B695A7959795879516
|
|
||||||
:10747000ADD45ADEE2E0CE0ED11CE11CF11CC0926C
|
|
||||||
:10748000A801D092A901E092AA01F092AB01F1E02B
|
|
||||||
:10749000AF1AB108C2CF8090A8019090A901A09026
|
|
||||||
:1074A000AA01B090AB01C63421F0C12CD12CD1E09F
|
|
||||||
:1074B00030C083E0F40180935700E89507B600FCE4
|
|
||||||
:1074C000FDCFF3CFC63461F5DD2039F14090A8013E
|
|
||||||
:1074D0005090A9016090AA017090AB010DDE2C2D97
|
|
||||||
:1074E00030E0382BF2010901D0935700E8951124C0
|
|
||||||
:1074F000F2E04F0E511C611C711C4092A801509289
|
|
||||||
:10750000A9016092AA017092AB01DD2621E0E21A86
|
|
||||||
:10751000F108E114F104B1F627C0EEDDC82EF5CF75
|
|
||||||
:10752000EBDD682F8091A8019091A901A091AA019B
|
|
||||||
:10753000B091AB01B695A795979587954FD440919B
|
|
||||||
:10754000A8015091A9016091AA017091AB014E5F11
|
|
||||||
:107550005F4F6F4F7F4F4093A8015093A9016093F5
|
|
||||||
:10756000AA017093AB01D2CFC63441F485E0F40197
|
|
||||||
:1075700080935700E89507B600FCFDCF8DE0D4DD81
|
|
||||||
:1075800082E080936F0094C0833471F4C091A801AD
|
|
||||||
:10759000D091A901B1DD90E021E0FE010C01209322
|
|
||||||
:1075A0005700E89511249BCE833601F5C090A801C1
|
|
||||||
:1075B000D090A901E090AA01F090AB019DDDF60109
|
|
||||||
:1075C000E16090E021E00C0120935700E895112440
|
|
||||||
:1075D000F2E0CF0ED11CE11CF11CC092A801D092A8
|
|
||||||
:1075E000A901E092AA01F092AB0179CE8D3661F447
|
|
||||||
:1075F000E091A801F091A90185E080935700E895FA
|
|
||||||
:1076000007B600FCFDCF6BCE823551F4E091A801A6
|
|
||||||
:10761000F091A901C591D4918D2F86DD8C2F60CE7C
|
|
||||||
:10762000843419F569DD682F8091A8019091A90132
|
|
||||||
:10763000A091AA01B091AB01B695A79597958795B2
|
|
||||||
:10764000CDD38091A8019091A901A091AA01B091F8
|
|
||||||
:10765000AB010296A11DB11D8093A8019093A901D1
|
|
||||||
:10766000A093AA01B093AB013ACE8436F1F4C09056
|
|
||||||
:10767000A801D090A901E090AA01F090AB01D70138
|
|
||||||
:10768000C601B695A79597958795A0D34DDD22E0C5
|
|
||||||
:10769000C20ED11CE11CF11CC092A801D092A9011C
|
|
||||||
:1076A000E092AA01F092AB0103C08B3109F0A5CEA4
|
|
||||||
:1076B00083E08093E9009091E8008091E8008E7764
|
|
||||||
:1076C0008093E80095FD11C08091E80080FD04C022
|
|
||||||
:1076D0008EB38111F9CF15C08091E8008E77809329
|
|
||||||
:1076E000E80003C08EB3882361F08091E80080FF3A
|
|
||||||
:1076F000F9CF84E08093E9008091E8008B77809354
|
|
||||||
:10770000E800DF91CF91FF90EF90DF90CF90BF9096
|
|
||||||
:10771000AF909F908F907F906F905F904F90089563
|
|
||||||
:1077200080910008909101081092010810920008C1
|
|
||||||
:1077300044B714BE28E10FB6F8942093600010926D
|
|
||||||
:1077400060000FBE41FD18C040FF09C0E0E0F0E05E
|
|
||||||
:10775000259134912F3F3F4F11F0FDDC0DC043FFC9
|
|
||||||
:107760000BC020910001309101018217930721F095
|
|
||||||
:10777000E0E0F0E085919491FDDC78941092A7010F
|
|
||||||
:107780001092A60180910201882361F07CDD07D36D
|
|
||||||
:107790008091A6019091A70181349F4198F31092A6
|
|
||||||
:1077A0000201F0CF8091E00081608093E000D3DCA3
|
|
||||||
:1077B00080E090E00895292F30E02230310539F043
|
|
||||||
:1077C0002330310549F02130310561F410C08EE3DA
|
|
||||||
:1077D00090E02CE431E01EC0882371F0813089F004
|
|
||||||
:1077E0008230A1F080E090E020E030E013C082E140
|
|
||||||
:1077F00090E02AE831E00EC084E090E028E431E037
|
|
||||||
:1078000009C082E290E024E231E004C088E190E027
|
|
||||||
:107810002AE031E0FA013183208308958093E90062
|
|
||||||
:10782000EBEEF0E0808181608083EDEEF0E010828D
|
|
||||||
:107830006093EC0040838091EE00881F8827881FAA
|
|
||||||
:1078400008958091AE0187FD05C08091E80080FF1A
|
|
||||||
:107850000DC010C08091E80082FD04C08EB381117C
|
|
||||||
:10786000F9CF08958091E8008B7707C08EB381111E
|
|
||||||
:10787000ECCF08958091E8008E778093E80008951A
|
|
||||||
:107880000F931F93CF93DF9341D048D0C8EDD0E042
|
|
||||||
:1078900088818F77888388818068888388818F7DBD
|
|
||||||
:1078A000888319BC1EBA1092AC0100EE10E0F801FA
|
|
||||||
:1078B00080818B7F808388818160888342E060E063
|
|
||||||
:1078C00080E0ACDFE1EEF0E080818E7F8083E2EE4D
|
|
||||||
:1078D000F0E0808181608083808188608083F8010E
|
|
||||||
:1078E00080818E7F8083888180618883DF91CF91C2
|
|
||||||
:1078F0001F910F910895E7EDF0E080818160808312
|
|
||||||
:107900008AE482BF81E08093AD01BACFE8EDF0E078
|
|
||||||
:1079100080818E7F80831092E20008951092DA00B9
|
|
||||||
:107920001092E10008951F920F920FB60F9211244A
|
|
||||||
:107930002F933F934F935F936F937F938F939F9377
|
|
||||||
:10794000AF93BF93EF93FF938091DA0080FF1BC04A
|
|
||||||
:107950008091D80080FF17C08091DA008E7F8093DD
|
|
||||||
:10796000DA008091D90080FF0BC080E189BD82E1FF
|
|
||||||
:1079700089BD09B400FEFDCF81E08EBB0FD203C0EC
|
|
||||||
:1079800019BC1EBA0BD28091E10080FF17C0809114
|
|
||||||
:10799000E20080FF13C08091E2008E7F8093E200BE
|
|
||||||
:1079A0008091E20080618093E2008091D800806243
|
|
||||||
:1079B0008093D80019BC85E08EBBF0D18091E100A6
|
|
||||||
:1079C00084FF2CC08091E20084FF28C080E189BD43
|
|
||||||
:1079D00082E189BD09B400FEFDCF8091D8008F7D82
|
|
||||||
:1079E0008093D8008091E1008F7E8093E1008091A8
|
|
||||||
:1079F000E2008F7E8093E2008091E20081608093BC
|
|
||||||
:107A0000E2008091AC01811104C08091E30087FF06
|
|
||||||
:107A100002C084E001C081E08EBBC0D18091E10052
|
|
||||||
:107A200083FF21C08091E20083FF1DC08091E100AF
|
|
||||||
:107A3000877F8093E10082E08EBB1092AC01809141
|
|
||||||
:107A4000E1008E7F8093E1008091E2008E7F809341
|
|
||||||
:107A5000E2008091E20080618093E20042E060E019
|
|
||||||
:107A600080E0DCDE9BD1FF91EF91BF91AF919F91C0
|
|
||||||
:107A70008F917F916F915F914F913F912F910F90D7
|
|
||||||
:107A80000FBE0F901F9018952091B4013091B50151
|
|
||||||
:107A90002617370748F06115710539F42091E80081
|
|
||||||
:107AA0002E772093E80001C0B901FC0120E06115A8
|
|
||||||
:107AB0007105A1F18EB38823E1F18530E1F1809168
|
|
||||||
:107AC000E80083FD34C08091E80082FF06C0809109
|
|
||||||
:107AD000E80082FF26C080E008958091E80080FFE2
|
|
||||||
:107AE000E6CF2091F3008091F20090E0922B611597
|
|
||||||
:107AF000710551F08830910538F421912093F100FF
|
|
||||||
:107B0000615071090196F3CF21E0089709F020E058
|
|
||||||
:107B10008091E8008E778093E800C9CF2111CACF09
|
|
||||||
:107B2000D6CF8EB3882329F0853089F604C081E052
|
|
||||||
:107B3000089582E0089583E008956115710529F4A0
|
|
||||||
:107B40002091E8002B772093E8006115710521F161
|
|
||||||
:107B50002EB3222351F1253051F12091E80023FD6D
|
|
||||||
:107B600020C02091E80022FFF3CFFC01CF0140911B
|
|
||||||
:107B7000F3002091F20030E0342B232B09F38091A5
|
|
||||||
:107B8000F1008193CF016150710981F7D9CF8EB394
|
|
||||||
:107B9000882359F0853059F08091E80080FFF7CFB5
|
|
||||||
:107BA00002C081E0089580E0089582E0089583E0B6
|
|
||||||
:107BB00008950F931F93CF93DF9300D0CDB7DEB717
|
|
||||||
:107BC000EEEAF1E08091F100819381E0E63BF80775
|
|
||||||
:107BD000C9F72BDB8091E80083FFCBC02091AE0179
|
|
||||||
:107BE0003091AF01832F90E08A30910508F0C1C039
|
|
||||||
:107BF000FC01EA5AF74CE4C0223809F0BAC080917F
|
|
||||||
:107C0000B20187708093E9009091EB001092E90037
|
|
||||||
:107C10008091E800877F8093E80095FB882780F9B2
|
|
||||||
:107C20008093F1001092F10082C0822F8D7F09F0C5
|
|
||||||
:107C3000A0C0223009F09DC08091B001811124C004
|
|
||||||
:107C40002091B201277009F494C02093E90080913B
|
|
||||||
:107C5000EB0080FF19C08091EB00333011F480629B
|
|
||||||
:107C600011C080618093EB0081E090E0022E01C0A2
|
|
||||||
:107C7000880F0A94EAF78093EA001092EA00809154
|
|
||||||
:107C8000EB0088608093EB001092E9008091E8009F
|
|
||||||
:107C9000877F50C021116DC01091B0011F770FB7C1
|
|
||||||
:107CA000F8948091E800877F8093E800CADD809196
|
|
||||||
:107CB000E80080FFFCCF8091E3008078812B8093E7
|
|
||||||
:107CC000E30080688093E300112311F083E001C09A
|
|
||||||
:107CD00082E08EBB0FBF4DC02058223008F049C053
|
|
||||||
:107CE000AE014F5F5F4F6091B2018091B001909102
|
|
||||||
:107CF000B10161DD009709F43CC02091E800277FC5
|
|
||||||
:107D00002093E800BC0189819A81BEDE8091E80061
|
|
||||||
:107D10008B778093E8002DC0203859F58091E800DA
|
|
||||||
:107D2000877F8093E8008091AC018093F10080917F
|
|
||||||
:107D3000E8008E778093E80084DD1BC0211119C014
|
|
||||||
:107D40009091B0019230A8F48091E800877F8093F1
|
|
||||||
:107D5000E8009093AC0175DD8091AC01811104C005
|
|
||||||
:107D60008091E30087FF02C084E001C081E08EBB08
|
|
||||||
:107D700050DA8091E80083FF0AC08091EB008062B6
|
|
||||||
:107D80008093EB008091E800877F8093E8000F905C
|
|
||||||
:107D90000F90DF91CF911F910F9108950895CF9388
|
|
||||||
:107DA0008EB3882359F0C091E900C7701092E900A2
|
|
||||||
:107DB0008091E80083FDFDDEC093E900CF91089536
|
|
||||||
:107DC000EE0FFF1F0590F491E02D0994F999FECF75
|
|
||||||
:107DD00092BD81BDF89A992780B50895262FF9990B
|
|
||||||
:107DE000FECF1FBA92BD81BD20BD0FB6F894FA9A9E
|
|
||||||
:0C7DF000F99A0FBE01960895F894FFCF99
|
|
||||||
:107DFC007777010000000000000818034B006500B5
|
|
||||||
:107E0C00790062006F0061007200640069006F000D
|
|
||||||
:107E1C002000000022034B006500790062006F0017
|
|
||||||
:107E2C0061007200640069006F00200042006F0066
|
|
||||||
:107E3C006F007400200000000403090409023E00D6
|
|
||||||
:107E4C000201008032090400000102020100052435
|
|
||||||
:107E5C000010010424020405240600010705820316
|
|
||||||
:107E6C000800FF09040100020A00000007050402D3
|
|
||||||
:107E7C00100001070583021000011201100102001D
|
|
||||||
:107E8C00000809120023010002010001434154457E
|
|
||||||
:067E9C0052494E410000B6
|
|
||||||
:040000030000700089
|
|
||||||
:00000001FF
|
|
File diff suppressed because it is too large
Load Diff
@ -1,679 +0,0 @@
|
|||||||
/*
|
|
||||||
LUFA Library
|
|
||||||
Copyright (C) Dean Camera, 2011.
|
|
||||||
|
|
||||||
dean [at] fourwalledcubicle [dot] com
|
|
||||||
www.lufa-lib.org
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
|
||||||
|
|
||||||
Permission to use, copy, modify, distribute, and sell this
|
|
||||||
software and its documentation for any purpose is hereby granted
|
|
||||||
without fee, provided that the above copyright notice appear in
|
|
||||||
all copies and that both that the copyright notice and this
|
|
||||||
permission notice and warranty disclaimer appear in supporting
|
|
||||||
documentation, and that the name of the author not be used in
|
|
||||||
advertising or publicity pertaining to distribution of the
|
|
||||||
software without specific, written prior permission.
|
|
||||||
|
|
||||||
The author disclaim all warranties with regard to this
|
|
||||||
software, including all implied warranties of merchantability
|
|
||||||
and fitness. In no event shall the author be liable for any
|
|
||||||
special, indirect or consequential damages or any damages
|
|
||||||
whatsoever resulting from loss of use, data or profits, whether
|
|
||||||
in an action of contract, negligence or other tortious action,
|
|
||||||
arising out of or in connection with the use or performance of
|
|
||||||
this software.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** \file
|
|
||||||
*
|
|
||||||
* Main source file for the CDC class bootloader. This file contains the complete bootloader logic.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define INCLUDE_FROM_CATERINA_C
|
|
||||||
#include "Caterina.h"
|
|
||||||
|
|
||||||
/** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some
|
|
||||||
* operating systems will not open the port unless the settings can be set successfully.
|
|
||||||
*/
|
|
||||||
static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
|
|
||||||
.CharFormat = CDC_LINEENCODING_OneStopBit,
|
|
||||||
.ParityType = CDC_PARITY_None,
|
|
||||||
.DataBits = 8 };
|
|
||||||
|
|
||||||
/** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,
|
|
||||||
* and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued
|
|
||||||
* command.)
|
|
||||||
*/
|
|
||||||
static uint32_t CurrAddress;
|
|
||||||
|
|
||||||
/** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
|
|
||||||
* via a watchdog reset. When cleared the bootloader will exit, starting the watchdog and entering an infinite
|
|
||||||
* loop until the AVR restarts and the application runs.
|
|
||||||
*/
|
|
||||||
static bool RunBootloader = true;
|
|
||||||
|
|
||||||
|
|
||||||
/* Bootloader timeout timer */
|
|
||||||
#define TIMEOUT_PERIOD 8000
|
|
||||||
uint16_t Timeout = 0;
|
|
||||||
|
|
||||||
uint16_t bootKey = 0x7777;
|
|
||||||
volatile uint16_t *const bootKeyPtr = (volatile uint16_t *)0x0800;
|
|
||||||
|
|
||||||
void StartSketch(void)
|
|
||||||
{
|
|
||||||
cli();
|
|
||||||
|
|
||||||
/* Undo TIMER1 setup and clear the count before running the sketch */
|
|
||||||
TIMSK1 = 0;
|
|
||||||
TCCR1B = 0;
|
|
||||||
TCNT1H = 0; // 16-bit write to TCNT1 requires high byte be written first
|
|
||||||
TCNT1L = 0;
|
|
||||||
|
|
||||||
/* Relocate the interrupt vector table to the application section */
|
|
||||||
MCUCR = (1 << IVCE);
|
|
||||||
MCUCR = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/* jump to beginning of application space */
|
|
||||||
__asm__ volatile("jmp 0x0000");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
|
|
||||||
* runs the bootloader processing routine until it times out or is instructed to exit.
|
|
||||||
*/
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
/* Save the value of the boot key memory before it is overwritten */
|
|
||||||
uint16_t bootKeyPtrVal = *bootKeyPtr;
|
|
||||||
*bootKeyPtr = 0;
|
|
||||||
|
|
||||||
/* Check the reason for the reset so we can act accordingly */
|
|
||||||
uint8_t mcusr_state = MCUSR; // store the initial state of the Status register
|
|
||||||
MCUSR = 0; // clear all reset flags
|
|
||||||
|
|
||||||
/* Watchdog may be configured with a 15 ms period so must disable it before going any further */
|
|
||||||
wdt_disable();
|
|
||||||
|
|
||||||
if (mcusr_state & (1<<EXTRF)) {
|
|
||||||
// External reset - we should continue to self-programming mode.
|
|
||||||
} else if ((mcusr_state & (1<<PORF)) && (pgm_read_word(0) != 0xFFFF)) {
|
|
||||||
// After a power-on reset skip the bootloader and jump straight to sketch
|
|
||||||
// if one exists.
|
|
||||||
StartSketch();
|
|
||||||
} else if ((mcusr_state & (1<<WDRF)) && (bootKeyPtrVal != bootKey) && (pgm_read_word(0) != 0xFFFF)) {
|
|
||||||
// If it looks like an "accidental" watchdog reset then start the sketch.
|
|
||||||
//StartSketch();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Setup hardware required for the bootloader */
|
|
||||||
SetupHardware();
|
|
||||||
|
|
||||||
/* Enable global interrupts so that the USB stack can function */
|
|
||||||
sei();
|
|
||||||
|
|
||||||
Timeout = 0;
|
|
||||||
|
|
||||||
while (RunBootloader)
|
|
||||||
{
|
|
||||||
CDC_Task();
|
|
||||||
USB_USBTask();
|
|
||||||
/* Time out and start the sketch if one is present */
|
|
||||||
if (Timeout > TIMEOUT_PERIOD)
|
|
||||||
RunBootloader = false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Disconnect from the host - USB interface will be reset later along with the AVR */
|
|
||||||
USB_Detach();
|
|
||||||
|
|
||||||
/* Jump to beginning of application space to run the sketch - do not reset */
|
|
||||||
StartSketch();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Configures all hardware required for the bootloader. */
|
|
||||||
void SetupHardware(void)
|
|
||||||
{
|
|
||||||
/* Disable watchdog if enabled by bootloader/fuses */
|
|
||||||
MCUSR &= ~(1 << WDRF);
|
|
||||||
wdt_disable();
|
|
||||||
|
|
||||||
/* Disable clock division */
|
|
||||||
clock_prescale_set(clock_div_1);
|
|
||||||
|
|
||||||
/* Relocate the interrupt vector table to the bootloader section */
|
|
||||||
MCUCR = (1 << IVCE);
|
|
||||||
MCUCR = (1 << IVSEL);
|
|
||||||
|
|
||||||
CPU_PRESCALE(0);
|
|
||||||
|
|
||||||
/* Initialize TIMER1 to handle bootloader timeout and LED tasks.
|
|
||||||
* With 16 MHz clock and 1/64 prescaler, timer 1 is clocked at 250 kHz
|
|
||||||
* Our chosen compare match generates an interrupt every 1 ms.
|
|
||||||
* This interrupt is disabled selectively when doing memory reading, erasing,
|
|
||||||
* or writing since SPM has tight timing requirements.
|
|
||||||
*/
|
|
||||||
OCR1AH = 0;
|
|
||||||
OCR1AL = 250;
|
|
||||||
TIMSK1 = (1 << OCIE1A); // enable timer 1 output compare A match interrupt
|
|
||||||
TCCR1B = ((1 << CS11) | (1 << CS10)); // 1/64 prescaler on timer 1 input
|
|
||||||
|
|
||||||
/* Initialize USB Subsystem */
|
|
||||||
USB_Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
//uint16_t ctr = 0;
|
|
||||||
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
|
|
||||||
{
|
|
||||||
/* Reset counter */
|
|
||||||
TCNT1H = 0;
|
|
||||||
TCNT1L = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if (pgm_read_word(0) != 0xFFFF)
|
|
||||||
Timeout++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
|
|
||||||
* to relay data to and from the attached USB host.
|
|
||||||
*/
|
|
||||||
void EVENT_USB_Device_ConfigurationChanged(void)
|
|
||||||
{
|
|
||||||
/* Setup CDC Notification, Rx and Tx Endpoints */
|
|
||||||
Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
|
|
||||||
ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
|
|
||||||
ENDPOINT_BANK_SINGLE);
|
|
||||||
|
|
||||||
Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
|
|
||||||
ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
|
|
||||||
ENDPOINT_BANK_SINGLE);
|
|
||||||
|
|
||||||
Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
|
|
||||||
ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
|
|
||||||
ENDPOINT_BANK_SINGLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
|
|
||||||
* the device from the USB host before passing along unhandled control requests to the library for processing
|
|
||||||
* internally.
|
|
||||||
*/
|
|
||||||
void EVENT_USB_Device_ControlRequest(void)
|
|
||||||
{
|
|
||||||
/* Ignore any requests that aren't directed to the CDC interface */
|
|
||||||
if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
|
|
||||||
(REQTYPE_CLASS | REQREC_INTERFACE))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Process CDC specific control requests */
|
|
||||||
switch (USB_ControlRequest.bRequest)
|
|
||||||
{
|
|
||||||
case CDC_REQ_GetLineEncoding:
|
|
||||||
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
|
|
||||||
{
|
|
||||||
Endpoint_ClearSETUP();
|
|
||||||
|
|
||||||
/* Write the line coding data to the control endpoint */
|
|
||||||
Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
|
|
||||||
Endpoint_ClearOUT();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case CDC_REQ_SetLineEncoding:
|
|
||||||
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
|
|
||||||
{
|
|
||||||
Endpoint_ClearSETUP();
|
|
||||||
|
|
||||||
/* Read the line coding data in from the host into the global struct */
|
|
||||||
Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
|
|
||||||
Endpoint_ClearIN();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if !defined(NO_BLOCK_SUPPORT)
|
|
||||||
/** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending
|
|
||||||
* on the AVR910 protocol command issued.
|
|
||||||
*
|
|
||||||
* \param[in] Command Single character AVR910 protocol command indicating what memory operation to perform
|
|
||||||
*/
|
|
||||||
static void ReadWriteMemoryBlock(const uint8_t Command)
|
|
||||||
{
|
|
||||||
uint16_t BlockSize;
|
|
||||||
char MemoryType;
|
|
||||||
|
|
||||||
bool HighByte = false;
|
|
||||||
uint8_t LowByte = 0;
|
|
||||||
|
|
||||||
BlockSize = (FetchNextCommandByte() << 8);
|
|
||||||
BlockSize |= FetchNextCommandByte();
|
|
||||||
|
|
||||||
MemoryType = FetchNextCommandByte();
|
|
||||||
|
|
||||||
if ((MemoryType != 'E') && (MemoryType != 'F'))
|
|
||||||
{
|
|
||||||
/* Send error byte back to the host */
|
|
||||||
WriteNextResponseByte('?');
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Disable timer 1 interrupt - can't afford to process nonessential interrupts
|
|
||||||
* while doing SPM tasks */
|
|
||||||
TIMSK1 = 0;
|
|
||||||
|
|
||||||
/* Check if command is to read memory */
|
|
||||||
if (Command == 'g')
|
|
||||||
{
|
|
||||||
/* Re-enable RWW section */
|
|
||||||
boot_rww_enable();
|
|
||||||
|
|
||||||
while (BlockSize--)
|
|
||||||
{
|
|
||||||
if (MemoryType == 'F')
|
|
||||||
{
|
|
||||||
/* Read the next FLASH byte from the current FLASH page */
|
|
||||||
#if (FLASHEND > 0xFFFF)
|
|
||||||
WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));
|
|
||||||
#else
|
|
||||||
WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* If both bytes in current word have been read, increment the address counter */
|
|
||||||
if (HighByte)
|
|
||||||
CurrAddress += 2;
|
|
||||||
|
|
||||||
HighByte = !HighByte;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Read the next EEPROM byte into the endpoint */
|
|
||||||
WriteNextResponseByte(eeprom_read_byte((uint8_t*)(intptr_t)(CurrAddress >> 1)));
|
|
||||||
|
|
||||||
/* Increment the address counter after use */
|
|
||||||
CurrAddress += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
uint32_t PageStartAddress = CurrAddress;
|
|
||||||
|
|
||||||
if (MemoryType == 'F')
|
|
||||||
{
|
|
||||||
boot_page_erase(PageStartAddress);
|
|
||||||
boot_spm_busy_wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
while (BlockSize--)
|
|
||||||
{
|
|
||||||
if (MemoryType == 'F')
|
|
||||||
{
|
|
||||||
/* If both bytes in current word have been written, increment the address counter */
|
|
||||||
if (HighByte)
|
|
||||||
{
|
|
||||||
/* Write the next FLASH word to the current FLASH page */
|
|
||||||
boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));
|
|
||||||
|
|
||||||
/* Increment the address counter after use */
|
|
||||||
CurrAddress += 2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LowByte = FetchNextCommandByte();
|
|
||||||
}
|
|
||||||
|
|
||||||
HighByte = !HighByte;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Write the next EEPROM byte from the endpoint */
|
|
||||||
eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
|
|
||||||
|
|
||||||
/* Increment the address counter after use */
|
|
||||||
CurrAddress += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If in FLASH programming mode, commit the page after writing */
|
|
||||||
if (MemoryType == 'F')
|
|
||||||
{
|
|
||||||
/* Commit the flash page to memory */
|
|
||||||
boot_page_write(PageStartAddress);
|
|
||||||
|
|
||||||
/* Wait until write operation has completed */
|
|
||||||
boot_spm_busy_wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send response byte back to the host */
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Re-enable timer 1 interrupt disabled earlier in this routine */
|
|
||||||
TIMSK1 = (1 << OCIE1A);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Retrieves the next byte from the host in the CDC data OUT endpoint, and clears the endpoint bank if needed
|
|
||||||
* to allow reception of the next data packet from the host.
|
|
||||||
*
|
|
||||||
* \return Next received byte from the host in the CDC data OUT endpoint
|
|
||||||
*/
|
|
||||||
static uint8_t FetchNextCommandByte(void)
|
|
||||||
{
|
|
||||||
/* Select the OUT endpoint so that the next data byte can be read */
|
|
||||||
Endpoint_SelectEndpoint(CDC_RX_EPNUM);
|
|
||||||
|
|
||||||
/* If OUT endpoint empty, clear it and wait for the next packet from the host */
|
|
||||||
while (!(Endpoint_IsReadWriteAllowed()))
|
|
||||||
{
|
|
||||||
Endpoint_ClearOUT();
|
|
||||||
|
|
||||||
while (!(Endpoint_IsOUTReceived()))
|
|
||||||
{
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Unattached)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fetch the next byte from the OUT endpoint */
|
|
||||||
return Endpoint_Read_8();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the
|
|
||||||
* bank when full ready for the next byte in the packet to the host.
|
|
||||||
*
|
|
||||||
* \param[in] Response Next response byte to send to the host
|
|
||||||
*/
|
|
||||||
static void WriteNextResponseByte(const uint8_t Response)
|
|
||||||
{
|
|
||||||
/* Select the IN endpoint so that the next data byte can be written */
|
|
||||||
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
|
|
||||||
|
|
||||||
/* If IN endpoint full, clear it and wait until ready for the next packet to the host */
|
|
||||||
if (!(Endpoint_IsReadWriteAllowed()))
|
|
||||||
{
|
|
||||||
Endpoint_ClearIN();
|
|
||||||
|
|
||||||
while (!(Endpoint_IsINReady()))
|
|
||||||
{
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Unattached)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write the next byte to the IN endpoint */
|
|
||||||
Endpoint_Write_8(Response);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#define STK_OK 0x10
|
|
||||||
#define STK_INSYNC 0x14 // ' '
|
|
||||||
#define CRC_EOP 0x20 // 'SPACE'
|
|
||||||
#define STK_GET_SYNC 0x30 // '0'
|
|
||||||
|
|
||||||
#define STK_GET_PARAMETER 0x41 // 'A'
|
|
||||||
#define STK_SET_DEVICE 0x42 // 'B'
|
|
||||||
#define STK_SET_DEVICE_EXT 0x45 // 'E'
|
|
||||||
#define STK_LOAD_ADDRESS 0x55 // 'U'
|
|
||||||
#define STK_UNIVERSAL 0x56 // 'V'
|
|
||||||
#define STK_PROG_PAGE 0x64 // 'd'
|
|
||||||
#define STK_READ_PAGE 0x74 // 't'
|
|
||||||
#define STK_READ_SIGN 0x75 // 'u'
|
|
||||||
|
|
||||||
/** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions
|
|
||||||
* and send the appropriate response back to the host.
|
|
||||||
*/
|
|
||||||
void CDC_Task(void)
|
|
||||||
{
|
|
||||||
/* Select the OUT endpoint */
|
|
||||||
Endpoint_SelectEndpoint(CDC_RX_EPNUM);
|
|
||||||
|
|
||||||
/* Check if endpoint has a command in it sent from the host */
|
|
||||||
if (!(Endpoint_IsOUTReceived()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
|
||||||
/* Read in the bootloader command (first byte sent from host) */
|
|
||||||
uint8_t Command = FetchNextCommandByte();
|
|
||||||
|
|
||||||
if (Command == 'E')
|
|
||||||
{
|
|
||||||
/* We nearly run out the bootloader timeout clock,
|
|
||||||
* leaving just a few hundred milliseconds so the
|
|
||||||
* bootloder has time to respond and service any
|
|
||||||
* subsequent requests */
|
|
||||||
Timeout = TIMEOUT_PERIOD - 500;
|
|
||||||
|
|
||||||
/* Re-enable RWW section - must be done here in case
|
|
||||||
* user has disabled verification on upload. */
|
|
||||||
boot_rww_enable_safe();
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'T')
|
|
||||||
{
|
|
||||||
FetchNextCommandByte();
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if ((Command == 'L') || (Command == 'P'))
|
|
||||||
{
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 't')
|
|
||||||
{
|
|
||||||
// Return ATMEGA128 part code - this is only to allow AVRProg to use the bootloader
|
|
||||||
WriteNextResponseByte(0x44);
|
|
||||||
WriteNextResponseByte(0x00);
|
|
||||||
}
|
|
||||||
else if (Command == 'a')
|
|
||||||
{
|
|
||||||
// Indicate auto-address increment is supported
|
|
||||||
WriteNextResponseByte('Y');
|
|
||||||
}
|
|
||||||
else if (Command == 'A')
|
|
||||||
{
|
|
||||||
// Set the current address to that given by the host
|
|
||||||
CurrAddress = (FetchNextCommandByte() << 9);
|
|
||||||
CurrAddress |= (FetchNextCommandByte() << 1);
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'p')
|
|
||||||
{
|
|
||||||
// Indicate serial programmer back to the host
|
|
||||||
WriteNextResponseByte('S');
|
|
||||||
}
|
|
||||||
else if (Command == 'S')
|
|
||||||
{
|
|
||||||
// Write the 7-byte software identifier to the endpoint
|
|
||||||
for (uint8_t CurrByte = 0; CurrByte < 7; CurrByte++)
|
|
||||||
WriteNextResponseByte(SOFTWARE_IDENTIFIER[CurrByte]);
|
|
||||||
}
|
|
||||||
else if (Command == 'V')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte('0' + BOOTLOADER_VERSION_MAJOR);
|
|
||||||
WriteNextResponseByte('0' + BOOTLOADER_VERSION_MINOR);
|
|
||||||
}
|
|
||||||
else if (Command == 's')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte(AVR_SIGNATURE_3);
|
|
||||||
WriteNextResponseByte(AVR_SIGNATURE_2);
|
|
||||||
WriteNextResponseByte(AVR_SIGNATURE_1);
|
|
||||||
}
|
|
||||||
else if (Command == 'e')
|
|
||||||
{
|
|
||||||
// Clear the application section of flash
|
|
||||||
for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE)
|
|
||||||
{
|
|
||||||
boot_page_erase(CurrFlashAddress);
|
|
||||||
boot_spm_busy_wait();
|
|
||||||
boot_page_write(CurrFlashAddress);
|
|
||||||
boot_spm_busy_wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
#if !defined(NO_LOCK_BYTE_WRITE_SUPPORT)
|
|
||||||
else if (Command == 'l')
|
|
||||||
{
|
|
||||||
// Set the lock bits to those given by the host
|
|
||||||
boot_lock_bits_set(FetchNextCommandByte());
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else if (Command == 'r')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOCK_BITS));
|
|
||||||
}
|
|
||||||
else if (Command == 'F')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS));
|
|
||||||
}
|
|
||||||
else if (Command == 'N')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS));
|
|
||||||
}
|
|
||||||
else if (Command == 'Q')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));
|
|
||||||
}
|
|
||||||
#if !defined(NO_BLOCK_SUPPORT)
|
|
||||||
else if (Command == 'b')
|
|
||||||
{
|
|
||||||
WriteNextResponseByte('Y');
|
|
||||||
|
|
||||||
// Send block size to the host
|
|
||||||
WriteNextResponseByte(SPM_PAGESIZE >> 8);
|
|
||||||
WriteNextResponseByte(SPM_PAGESIZE & 0xFF);
|
|
||||||
}
|
|
||||||
else if ((Command == 'B') || (Command == 'g'))
|
|
||||||
{
|
|
||||||
// Keep resetting the timeout counter if we're receiving self-programming instructions
|
|
||||||
Timeout = 0;
|
|
||||||
// Delegate the block write/read to a separate function for clarity
|
|
||||||
ReadWriteMemoryBlock(Command);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#if !defined(NO_FLASH_BYTE_SUPPORT)
|
|
||||||
else if (Command == 'C')
|
|
||||||
{
|
|
||||||
// Write the high byte to the current flash page
|
|
||||||
boot_page_fill(CurrAddress, FetchNextCommandByte());
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'c')
|
|
||||||
{
|
|
||||||
// Write the low byte to the current flash page
|
|
||||||
boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte());
|
|
||||||
|
|
||||||
// Increment the address
|
|
||||||
CurrAddress += 2;
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'm')
|
|
||||||
{
|
|
||||||
// Commit the flash page to memory
|
|
||||||
boot_page_write(CurrAddress);
|
|
||||||
|
|
||||||
// Wait until write operation has completed
|
|
||||||
boot_spm_busy_wait();
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'R')
|
|
||||||
{
|
|
||||||
#if (FLASHEND > 0xFFFF)
|
|
||||||
uint16_t ProgramWord = pgm_read_word_far(CurrAddress);
|
|
||||||
#else
|
|
||||||
uint16_t ProgramWord = pgm_read_word(CurrAddress);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
WriteNextResponseByte(ProgramWord >> 8);
|
|
||||||
WriteNextResponseByte(ProgramWord & 0xFF);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#if !defined(NO_EEPROM_BYTE_SUPPORT)
|
|
||||||
else if (Command == 'D')
|
|
||||||
{
|
|
||||||
// Read the byte from the endpoint and write it to the EEPROM
|
|
||||||
eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
|
|
||||||
|
|
||||||
// Increment the address after use
|
|
||||||
CurrAddress += 2;
|
|
||||||
|
|
||||||
// Send confirmation byte back to the host
|
|
||||||
WriteNextResponseByte('\r');
|
|
||||||
}
|
|
||||||
else if (Command == 'd')
|
|
||||||
{
|
|
||||||
// Read the EEPROM byte and write it to the endpoint
|
|
||||||
WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1))));
|
|
||||||
|
|
||||||
// Increment the address after use
|
|
||||||
CurrAddress += 2;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else if (Command != 27)
|
|
||||||
{
|
|
||||||
// Unknown (non-sync) command, return fail code
|
|
||||||
WriteNextResponseByte('?');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Select the IN endpoint */
|
|
||||||
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
|
|
||||||
|
|
||||||
/* Remember if the endpoint is completely full before clearing it */
|
|
||||||
bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
|
|
||||||
|
|
||||||
/* Send the endpoint data to the host */
|
|
||||||
Endpoint_ClearIN();
|
|
||||||
|
|
||||||
/* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */
|
|
||||||
if (IsEndpointFull)
|
|
||||||
{
|
|
||||||
while (!(Endpoint_IsINReady()))
|
|
||||||
{
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Unattached)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Endpoint_ClearIN();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Wait until the data has been sent to the host */
|
|
||||||
while (!(Endpoint_IsINReady()))
|
|
||||||
{
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Unattached)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Select the OUT endpoint */
|
|
||||||
Endpoint_SelectEndpoint(CDC_RX_EPNUM);
|
|
||||||
|
|
||||||
/* Acknowledge the command from the host */
|
|
||||||
Endpoint_ClearOUT();
|
|
||||||
}
|
|
||||||
|
|
@ -1,99 +0,0 @@
|
|||||||
/*
|
|
||||||
LUFA Library
|
|
||||||
Copyright (C) Dean Camera, 2011.
|
|
||||||
|
|
||||||
dean [at] fourwalledcubicle [dot] com
|
|
||||||
www.lufa-lib.org
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
|
||||||
|
|
||||||
Permission to use, copy, modify, distribute, and sell this
|
|
||||||
software and its documentation for any purpose is hereby granted
|
|
||||||
without fee, provided that the above copyright notice appear in
|
|
||||||
all copies and that both that the copyright notice and this
|
|
||||||
permission notice and warranty disclaimer appear in supporting
|
|
||||||
documentation, and that the name of the author not be used in
|
|
||||||
advertising or publicity pertaining to distribution of the
|
|
||||||
software without specific, written prior permission.
|
|
||||||
|
|
||||||
The author disclaim all warranties with regard to this
|
|
||||||
software, including all implied warranties of merchantability
|
|
||||||
and fitness. In no event shall the author be liable for any
|
|
||||||
special, indirect or consequential damages or any damages
|
|
||||||
whatsoever resulting from loss of use, data or profits, whether
|
|
||||||
in an action of contract, negligence or other tortious action,
|
|
||||||
arising out of or in connection with the use or performance of
|
|
||||||
this software.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** \file
|
|
||||||
*
|
|
||||||
* Header file for BootloaderCDC.c.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _CDC_H_
|
|
||||||
#define _CDC_H_
|
|
||||||
|
|
||||||
/* Includes: */
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/wdt.h>
|
|
||||||
#include <avr/boot.h>
|
|
||||||
#include <avr/eeprom.h>
|
|
||||||
#include <avr/power.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#include "Descriptors.h"
|
|
||||||
|
|
||||||
#include <LUFA/Drivers/USB/USB.h>
|
|
||||||
/* Macros: */
|
|
||||||
/** Version major of the CDC bootloader. */
|
|
||||||
#define BOOTLOADER_VERSION_MAJOR 0x01
|
|
||||||
|
|
||||||
/** Version minor of the CDC bootloader. */
|
|
||||||
#define BOOTLOADER_VERSION_MINOR 0x00
|
|
||||||
|
|
||||||
/** Hardware version major of the CDC bootloader. */
|
|
||||||
#define BOOTLOADER_HWVERSION_MAJOR 0x01
|
|
||||||
|
|
||||||
/** Hardware version minor of the CDC bootloader. */
|
|
||||||
#define BOOTLOADER_HWVERSION_MINOR 0x00
|
|
||||||
|
|
||||||
/** Eight character bootloader firmware identifier reported to the host when requested */
|
|
||||||
#define SOFTWARE_IDENTIFIER "CATERINA"
|
|
||||||
|
|
||||||
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
|
|
||||||
#define LED_SETUP() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5);
|
|
||||||
#define TX_LED_OFF() PORTC &= ~(1<<7)
|
|
||||||
#define TX_LED_ON() PORTC |= (1<<7)
|
|
||||||
#define L_LED_TOGGLE() PORTD ^= (1<<5)
|
|
||||||
#define L_LED_OFF() PORTD |= (1<<5)
|
|
||||||
#define L_LED_ON() PORTD &= ~(1<<5)
|
|
||||||
#define RX_LED_OFF() PORTB |= (1<<0)
|
|
||||||
#define RX_LED_ON() PORTB &= ~(1<<0)
|
|
||||||
|
|
||||||
/* Type Defines: */
|
|
||||||
/** Type define for a non-returning pointer to the start of the loaded application in flash memory. */
|
|
||||||
typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;
|
|
||||||
|
|
||||||
/* Function Prototypes: */
|
|
||||||
void StartSketch(void);
|
|
||||||
void LEDPulse(void);
|
|
||||||
|
|
||||||
void CDC_Task(void);
|
|
||||||
void SetupHardware(void);
|
|
||||||
|
|
||||||
void EVENT_USB_Device_ConfigurationChanged(void);
|
|
||||||
|
|
||||||
#if defined(INCLUDE_FROM_CATERINA_C) || defined(__DOXYGEN__)
|
|
||||||
#if !defined(NO_BLOCK_SUPPORT)
|
|
||||||
static void ReadWriteMemoryBlock(const uint8_t Command);
|
|
||||||
#endif
|
|
||||||
static uint8_t FetchNextCommandByte(void);
|
|
||||||
static void WriteNextResponseByte(const uint8_t Response);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,262 +0,0 @@
|
|||||||
/*
|
|
||||||
LUFA Library
|
|
||||||
Copyright (C) Dean Camera, 2011.
|
|
||||||
|
|
||||||
dean [at] fourwalledcubicle [dot] com
|
|
||||||
www.lufa-lib.org
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
|
||||||
|
|
||||||
Permission to use, copy, modify, distribute, and sell this
|
|
||||||
software and its documentation for any purpose is hereby granted
|
|
||||||
without fee, provided that the above copyright notice appear in
|
|
||||||
all copies and that both that the copyright notice and this
|
|
||||||
permission notice and warranty disclaimer appear in supporting
|
|
||||||
documentation, and that the name of the author not be used in
|
|
||||||
advertising or publicity pertaining to distribution of the
|
|
||||||
software without specific, written prior permission.
|
|
||||||
|
|
||||||
The author disclaim all warranties with regard to this
|
|
||||||
software, including all implied warranties of merchantability
|
|
||||||
and fitness. In no event shall the author be liable for any
|
|
||||||
special, indirect or consequential damages or any damages
|
|
||||||
whatsoever resulting from loss of use, data or profits, whether
|
|
||||||
in an action of contract, negligence or other tortious action,
|
|
||||||
arising out of or in connection with the use or performance of
|
|
||||||
this software.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** \file
|
|
||||||
*
|
|
||||||
* USB Device Descriptors, for library use when in USB device mode. Descriptors are special
|
|
||||||
* computer-readable structures which the host requests upon device enumeration, to determine
|
|
||||||
* the device's capabilities and functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Descriptors.h"
|
|
||||||
|
|
||||||
/** Device descriptor structure. This descriptor, located in SRAM memory, describes the overall
|
|
||||||
* device characteristics, including the supported USB version, control endpoint size and the
|
|
||||||
* number of device configurations. The descriptor is read out by the USB host when the enumeration
|
|
||||||
* process begins.
|
|
||||||
*/
|
|
||||||
const USB_Descriptor_Device_t DeviceDescriptor =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
|
|
||||||
|
|
||||||
.USBSpecification = VERSION_BCD(01.10),
|
|
||||||
.Class = CDC_CSCP_CDCClass,
|
|
||||||
.SubClass = CDC_CSCP_NoSpecificSubclass,
|
|
||||||
.Protocol = CDC_CSCP_NoSpecificProtocol,
|
|
||||||
|
|
||||||
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
|
|
||||||
|
|
||||||
.VendorID = DEVICE_VID,
|
|
||||||
.ProductID = DEVICE_PID,
|
|
||||||
.ReleaseNumber = VERSION_BCD(00.01),
|
|
||||||
|
|
||||||
.ManufacturerStrIndex = 0x02,
|
|
||||||
.ProductStrIndex = 0x01,
|
|
||||||
.SerialNumStrIndex = NO_DESCRIPTOR,
|
|
||||||
|
|
||||||
.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Configuration descriptor structure. This descriptor, located in SRAM memory, describes the usage
|
|
||||||
* of the device in one of its supported configurations, including information about any device interfaces
|
|
||||||
* and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
|
|
||||||
* a configuration so that the host may correctly communicate with the USB device.
|
|
||||||
*/
|
|
||||||
const USB_Descriptor_Configuration_t ConfigurationDescriptor =
|
|
||||||
{
|
|
||||||
.Config =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
|
|
||||||
|
|
||||||
.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
|
|
||||||
.TotalInterfaces = 2,
|
|
||||||
|
|
||||||
.ConfigurationNumber = 1,
|
|
||||||
.ConfigurationStrIndex = NO_DESCRIPTOR,
|
|
||||||
|
|
||||||
.ConfigAttributes = USB_CONFIG_ATTR_BUSPOWERED,
|
|
||||||
|
|
||||||
.MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_CCI_Interface =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
|
|
||||||
|
|
||||||
.InterfaceNumber = 0,
|
|
||||||
.AlternateSetting = 0,
|
|
||||||
|
|
||||||
.TotalEndpoints = 1,
|
|
||||||
|
|
||||||
.Class = CDC_CSCP_CDCClass,
|
|
||||||
.SubClass = CDC_CSCP_ACMSubclass,
|
|
||||||
.Protocol = CDC_CSCP_ATCommandProtocol,
|
|
||||||
|
|
||||||
.InterfaceStrIndex = NO_DESCRIPTOR
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_Functional_Header =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
|
|
||||||
.Subtype = 0x00,
|
|
||||||
|
|
||||||
.CDCSpecification = VERSION_BCD(01.10),
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_Functional_ACM =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
|
|
||||||
.Subtype = 0x02,
|
|
||||||
|
|
||||||
.Capabilities = 0x04,
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_Functional_Union =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
|
|
||||||
.Subtype = 0x06,
|
|
||||||
|
|
||||||
.MasterInterfaceNumber = 0,
|
|
||||||
.SlaveInterfaceNumber = 1,
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_NotificationEndpoint =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
|
|
||||||
|
|
||||||
.EndpointAddress = (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM),
|
|
||||||
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
|
|
||||||
.EndpointSize = CDC_NOTIFICATION_EPSIZE,
|
|
||||||
.PollingIntervalMS = 0xFF
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_DCI_Interface =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
|
|
||||||
|
|
||||||
.InterfaceNumber = 1,
|
|
||||||
.AlternateSetting = 0,
|
|
||||||
|
|
||||||
.TotalEndpoints = 2,
|
|
||||||
|
|
||||||
.Class = CDC_CSCP_CDCDataClass,
|
|
||||||
.SubClass = CDC_CSCP_NoDataSubclass,
|
|
||||||
.Protocol = CDC_CSCP_NoDataProtocol,
|
|
||||||
|
|
||||||
.InterfaceStrIndex = NO_DESCRIPTOR
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_DataOutEndpoint =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
|
|
||||||
|
|
||||||
.EndpointAddress = (ENDPOINT_DIR_OUT | CDC_RX_EPNUM),
|
|
||||||
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
|
|
||||||
.EndpointSize = CDC_TXRX_EPSIZE,
|
|
||||||
.PollingIntervalMS = 0x01
|
|
||||||
},
|
|
||||||
|
|
||||||
.CDC_DataInEndpoint =
|
|
||||||
{
|
|
||||||
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
|
|
||||||
|
|
||||||
.EndpointAddress = (ENDPOINT_DIR_IN | CDC_TX_EPNUM),
|
|
||||||
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
|
|
||||||
.EndpointSize = CDC_TXRX_EPSIZE,
|
|
||||||
.PollingIntervalMS = 0x01
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Language descriptor structure. This descriptor, located in SRAM memory, is returned when the host requests
|
|
||||||
* the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
|
|
||||||
* via the language ID table available at USB.org what languages the device supports for its string descriptors.
|
|
||||||
*/
|
|
||||||
const USB_Descriptor_String_t LanguageString =
|
|
||||||
{
|
|
||||||
.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
|
|
||||||
|
|
||||||
.UnicodeString = {LANGUAGE_ID_ENG}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
|
|
||||||
* and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
|
|
||||||
* Descriptor.
|
|
||||||
*/
|
|
||||||
const USB_Descriptor_String_t ProductString =
|
|
||||||
{
|
|
||||||
.Header = {.Size = USB_STRING_LEN(16), .Type = DTYPE_String},
|
|
||||||
|
|
||||||
#if DEVICE_PID == 0x2300
|
|
||||||
.UnicodeString = L"Keyboardio Boot "
|
|
||||||
#else
|
|
||||||
.UnicodeString = L"USB IO board "
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
const USB_Descriptor_String_t ManufNameString =
|
|
||||||
{
|
|
||||||
.Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
|
|
||||||
|
|
||||||
#if DEVICE_VID == 0x1209
|
|
||||||
.UnicodeString = L"Keyboardio "
|
|
||||||
#else
|
|
||||||
.UnicodeString = L"Unknown "
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
/** This function is called by the library when in device mode, and must be overridden (see LUFA library "USB Descriptors"
|
|
||||||
* documentation) by the application code so that the address and size of a requested descriptor can be given
|
|
||||||
* to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
|
|
||||||
* is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
|
|
||||||
* USB host.
|
|
||||||
*/
|
|
||||||
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
|
|
||||||
const uint8_t wIndex,
|
|
||||||
const void** const DescriptorAddress)
|
|
||||||
{
|
|
||||||
const uint8_t DescriptorType = (wValue >> 8);
|
|
||||||
const uint8_t DescriptorNumber = (wValue & 0xFF);
|
|
||||||
|
|
||||||
const void* Address = NULL;
|
|
||||||
uint16_t Size = NO_DESCRIPTOR;
|
|
||||||
|
|
||||||
switch (DescriptorType)
|
|
||||||
{
|
|
||||||
case DTYPE_Device:
|
|
||||||
Address = &DeviceDescriptor;
|
|
||||||
Size = sizeof(USB_Descriptor_Device_t);
|
|
||||||
break;
|
|
||||||
case DTYPE_Configuration:
|
|
||||||
Address = &ConfigurationDescriptor;
|
|
||||||
Size = sizeof(USB_Descriptor_Configuration_t);
|
|
||||||
break;
|
|
||||||
case DTYPE_String:
|
|
||||||
if (!(DescriptorNumber))
|
|
||||||
{
|
|
||||||
Address = &LanguageString;
|
|
||||||
Size = LanguageString.Header.Size;
|
|
||||||
}
|
|
||||||
else if (DescriptorNumber == DeviceDescriptor.ProductStrIndex)
|
|
||||||
{
|
|
||||||
Address = &ProductString;
|
|
||||||
Size = ProductString.Header.Size;
|
|
||||||
} else if (DescriptorNumber == DeviceDescriptor.ManufacturerStrIndex)
|
|
||||||
{
|
|
||||||
Address = &ManufNameString;
|
|
||||||
Size = ManufNameString.Header.Size;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
*DescriptorAddress = Address;
|
|
||||||
return Size;
|
|
||||||
}
|
|
||||||
|
|
@ -1,139 +0,0 @@
|
|||||||
/*
|
|
||||||
LUFA Library
|
|
||||||
Copyright (C) Dean Camera, 2011.
|
|
||||||
|
|
||||||
dean [at] fourwalledcubicle [dot] com
|
|
||||||
www.lufa-lib.org
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
|
||||||
|
|
||||||
Permission to use, copy, modify, distribute, and sell this
|
|
||||||
software and its documentation for any purpose is hereby granted
|
|
||||||
without fee, provided that the above copyright notice appear in
|
|
||||||
all copies and that both that the copyright notice and this
|
|
||||||
permission notice and warranty disclaimer appear in supporting
|
|
||||||
documentation, and that the name of the author not be used in
|
|
||||||
advertising or publicity pertaining to distribution of the
|
|
||||||
software without specific, written prior permission.
|
|
||||||
|
|
||||||
The author disclaim all warranties with regard to this
|
|
||||||
software, including all implied warranties of merchantability
|
|
||||||
and fitness. In no event shall the author be liable for any
|
|
||||||
special, indirect or consequential damages or any damages
|
|
||||||
whatsoever resulting from loss of use, data or profits, whether
|
|
||||||
in an action of contract, negligence or other tortious action,
|
|
||||||
arising out of or in connection with the use or performance of
|
|
||||||
this software.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** \file
|
|
||||||
*
|
|
||||||
* Header file for Descriptors.c.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _DESCRIPTORS_H_
|
|
||||||
#define _DESCRIPTORS_H_
|
|
||||||
|
|
||||||
/* Includes: */
|
|
||||||
#include <LUFA/Drivers/USB/USB.h>
|
|
||||||
|
|
||||||
/* Macros: */
|
|
||||||
#if defined(__AVR_AT90USB1287__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x97
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#elif defined(__AVR_AT90USB647__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x96
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#elif defined(__AVR_AT90USB1286__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x97
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#elif defined(__AVR_AT90USB646__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x96
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#elif defined(__AVR_ATmega32U6__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x95
|
|
||||||
#define AVR_SIGNATURE_3 0x88
|
|
||||||
#elif defined(__AVR_ATmega32U4__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x95
|
|
||||||
#define AVR_SIGNATURE_3 0x87
|
|
||||||
#elif defined(__AVR_ATmega16U4__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x94
|
|
||||||
#define AVR_SIGNATURE_3 0x88
|
|
||||||
#elif defined(__AVR_ATmega32U2__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x95
|
|
||||||
#define AVR_SIGNATURE_3 0x8A
|
|
||||||
#elif defined(__AVR_ATmega16U2__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x94
|
|
||||||
#define AVR_SIGNATURE_3 0x89
|
|
||||||
#elif defined(__AVR_AT90USB162__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x94
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#elif defined(__AVR_ATmega8U2__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x93
|
|
||||||
#define AVR_SIGNATURE_3 0x89
|
|
||||||
#elif defined(__AVR_AT90USB82__)
|
|
||||||
#define AVR_SIGNATURE_1 0x1E
|
|
||||||
#define AVR_SIGNATURE_2 0x94
|
|
||||||
#define AVR_SIGNATURE_3 0x82
|
|
||||||
#else
|
|
||||||
#error The selected AVR part is not currently supported by this bootloader.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Endpoint number for the CDC control interface event notification endpoint. */
|
|
||||||
#define CDC_NOTIFICATION_EPNUM 2
|
|
||||||
|
|
||||||
/** Endpoint number for the CDC data interface TX (data IN) endpoint. */
|
|
||||||
#define CDC_TX_EPNUM 3
|
|
||||||
|
|
||||||
/** Endpoint number for the CDC data interface RX (data OUT) endpoint. */
|
|
||||||
#define CDC_RX_EPNUM 4
|
|
||||||
|
|
||||||
/** Size of the CDC data interface TX and RX data endpoint banks, in bytes. */
|
|
||||||
#define CDC_TXRX_EPSIZE 16
|
|
||||||
|
|
||||||
/** Size of the CDC control interface notification endpoint bank, in bytes. */
|
|
||||||
#define CDC_NOTIFICATION_EPSIZE 8
|
|
||||||
|
|
||||||
/* Type Defines: */
|
|
||||||
/** Type define for the device configuration descriptor structure. This must be defined in the
|
|
||||||
* application code, as the configuration descriptor contains several sub-descriptors which
|
|
||||||
* vary between devices, and which describe the device's usage to the host.
|
|
||||||
*/
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
USB_Descriptor_Configuration_Header_t Config;
|
|
||||||
|
|
||||||
// CDC Control Interface
|
|
||||||
USB_Descriptor_Interface_t CDC_CCI_Interface;
|
|
||||||
USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
|
|
||||||
USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
|
|
||||||
USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
|
|
||||||
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
|
|
||||||
|
|
||||||
// CDC Data Interface
|
|
||||||
USB_Descriptor_Interface_t CDC_DCI_Interface;
|
|
||||||
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
|
|
||||||
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
|
|
||||||
} USB_Descriptor_Configuration_t;
|
|
||||||
|
|
||||||
/* Function Prototypes: */
|
|
||||||
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
|
|
||||||
const uint8_t wIndex,
|
|
||||||
const void** const DescriptorAddress)
|
|
||||||
ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +0,0 @@
|
|||||||
LUFA: 111009
|
|
||||||
make: 3.81
|
|
||||||
avrdude: 5.11.1
|
|
||||||
avr-libc: 1.6.7
|
|
||||||
binutils-avr: 2.19
|
|
||||||
gcc-avr 4.3.3
|
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
@ -1,728 +0,0 @@
|
|||||||
# Hey Emacs, this is a -*- makefile -*-
|
|
||||||
#----------------------------------------------------------------------------
|
|
||||||
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
|
|
||||||
# >> Modified for use with the LUFA project. <<
|
|
||||||
#
|
|
||||||
# Released to the Public Domain
|
|
||||||
#
|
|
||||||
# Additional material for this makefile was written by:
|
|
||||||
# Peter Fleury
|
|
||||||
# Tim Henigan
|
|
||||||
# Colin O'Flynn
|
|
||||||
# Reiner Patommel
|
|
||||||
# Markus Pfaff
|
|
||||||
# Sander Pool
|
|
||||||
# Frederik Rouleau
|
|
||||||
# Carlos Lamas
|
|
||||||
# Dean Camera
|
|
||||||
# Opendous Inc.
|
|
||||||
# Denver Gingerich
|
|
||||||
#
|
|
||||||
#----------------------------------------------------------------------------
|
|
||||||
# On command line:
|
|
||||||
#
|
|
||||||
# make all = Make software.
|
|
||||||
#
|
|
||||||
# make clean = Clean out built project files.
|
|
||||||
#
|
|
||||||
# make coff = Convert ELF to AVR COFF.
|
|
||||||
#
|
|
||||||
# make extcoff = Convert ELF to AVR Extended COFF.
|
|
||||||
#
|
|
||||||
# make program = Download the hex file to the device, using avrdude.
|
|
||||||
# Please customize the avrdude settings below first!
|
|
||||||
#
|
|
||||||
# make doxygen = Generate DoxyGen documentation for the project (must have
|
|
||||||
# DoxyGen installed)
|
|
||||||
#
|
|
||||||
# make debug = Start either simulavr or avarice as specified for debugging,
|
|
||||||
# with avr-gdb or avr-insight as the front end for debugging.
|
|
||||||
#
|
|
||||||
# make filename.s = Just compile filename.c into the assembler code only.
|
|
||||||
#
|
|
||||||
# make filename.i = Create a preprocessed source file for use in submitting
|
|
||||||
# bug reports to the GCC project.
|
|
||||||
#
|
|
||||||
# To rebuild project do "make clean" then "make all".
|
|
||||||
#----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
# USB vendor ID (VID)
|
|
||||||
# http://pid.codes/1209/
|
|
||||||
VID = 0x1209
|
|
||||||
|
|
||||||
|
|
||||||
# USB product ID (PID)
|
|
||||||
# http://pid.codes/1209/2300/
|
|
||||||
# Keyboardio Model 01 PID
|
|
||||||
PID = 0x2300
|
|
||||||
|
|
||||||
# MCU name
|
|
||||||
MCU = atmega32u4
|
|
||||||
|
|
||||||
|
|
||||||
# Target architecture (see library "Board Types" documentation).
|
|
||||||
ARCH = AVR8
|
|
||||||
|
|
||||||
|
|
||||||
# Target board (see library "Board Types" documentation, NONE for projects not requiring
|
|
||||||
# LUFA board drivers). If USER is selected, put custom board drivers in a directory called
|
|
||||||
# "Board" inside the application directory.
|
|
||||||
BOARD = USER
|
|
||||||
|
|
||||||
|
|
||||||
# Processor frequency.
|
|
||||||
# This will define a symbol, F_CPU, in all source code files equal to the
|
|
||||||
# processor frequency in Hz. You can then use this symbol in your source code to
|
|
||||||
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
|
|
||||||
# automatically to create a 32-bit value in your source code.
|
|
||||||
#
|
|
||||||
# This will be an integer division of F_USB below, as it is sourced by
|
|
||||||
# F_USB after it has run through any CPU prescalers. Note that this value
|
|
||||||
# does not *change* the processor frequency - it should merely be updated to
|
|
||||||
# reflect the processor speed set externally so that the code can use accurate
|
|
||||||
# software delays.
|
|
||||||
F_CPU = 16000000
|
|
||||||
|
|
||||||
|
|
||||||
# Input clock frequency.
|
|
||||||
# This will define a symbol, F_USB, in all source code files equal to the
|
|
||||||
# input clock frequency (before any prescaling is performed) in Hz. This value may
|
|
||||||
# differ from F_CPU if prescaling is used on the latter, and is required as the
|
|
||||||
# raw input clock is fed directly to the PLL sections of the AVR for high speed
|
|
||||||
# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
|
|
||||||
# at the end, this will be done automatically to create a 32-bit value in your
|
|
||||||
# source code.
|
|
||||||
#
|
|
||||||
# If no clock division is performed on the input clock inside the AVR (via the
|
|
||||||
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
|
|
||||||
F_USB = $(F_CPU)
|
|
||||||
|
|
||||||
|
|
||||||
# Starting byte address of the bootloader, as a byte address - computed via the formula
|
|
||||||
# BOOT_START = ((FLASH_SIZE_KB - BOOT_SECTION_SIZE_KB) * 1024)
|
|
||||||
#
|
|
||||||
# Note that the bootloader size and start address given in AVRStudio is in words and not
|
|
||||||
# bytes, and so will need to be doubled to obtain the byte address needed by AVR-GCC.
|
|
||||||
FLASH_SIZE_KB = 32
|
|
||||||
BOOT_SECTION_SIZE_KB = 4
|
|
||||||
BOOT_START = 0x$(shell echo "obase=16; ($(FLASH_SIZE_KB) - $(BOOT_SECTION_SIZE_KB)) * 1024" | bc)
|
|
||||||
|
|
||||||
|
|
||||||
# Output format. (can be srec, ihex, binary)
|
|
||||||
FORMAT = ihex
|
|
||||||
|
|
||||||
|
|
||||||
# Target file name (without extension).
|
|
||||||
TARGET = Caterina
|
|
||||||
|
|
||||||
|
|
||||||
# Object files directory
|
|
||||||
# To put object files in current directory, use a dot (.), do NOT make
|
|
||||||
# this an empty or blank macro!
|
|
||||||
OBJDIR = .
|
|
||||||
|
|
||||||
|
|
||||||
# Path to the LUFA library
|
|
||||||
LUFA_PATH = ../../../../../../../../git/LUFA/LUFA-111009
|
|
||||||
|
|
||||||
|
|
||||||
# LUFA library compile-time options and predefined tokens
|
|
||||||
LUFA_OPTS = -D USB_DEVICE_ONLY
|
|
||||||
LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
|
|
||||||
LUFA_OPTS += -D ORDERED_EP_CONFIG
|
|
||||||
LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
|
|
||||||
LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
|
|
||||||
LUFA_OPTS += -D USE_RAM_DESCRIPTORS
|
|
||||||
LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
|
|
||||||
LUFA_OPTS += -D NO_INTERNAL_SERIAL
|
|
||||||
LUFA_OPTS += -D NO_DEVICE_SELF_POWER
|
|
||||||
LUFA_OPTS += -D NO_DEVICE_REMOTE_WAKEUP
|
|
||||||
LUFA_OPTS += -D NO_SOF_EVENTS
|
|
||||||
|
|
||||||
#LUFA_OPTS += -D NO_BLOCK_SUPPORT
|
|
||||||
#LUFA_OPTS += -D NO_EEPROM_BYTE_SUPPORT
|
|
||||||
#LUFA_OPTS += -D NO_FLASH_BYTE_SUPPORT
|
|
||||||
LUFA_OPTS += -D NO_LOCK_BYTE_WRITE_SUPPORT
|
|
||||||
|
|
||||||
|
|
||||||
# Create the LUFA source path variables by including the LUFA root makefile
|
|
||||||
include $(LUFA_PATH)/LUFA/makefile
|
|
||||||
|
|
||||||
|
|
||||||
# List C source files here. (C dependencies are automatically generated.)
|
|
||||||
SRC = $(TARGET).c \
|
|
||||||
Descriptors.c \
|
|
||||||
$(LUFA_SRC_USB) \
|
|
||||||
|
|
||||||
|
|
||||||
# List C++ source files here. (C dependencies are automatically generated.)
|
|
||||||
CPPSRC =
|
|
||||||
|
|
||||||
|
|
||||||
# List Assembler source files here.
|
|
||||||
# Make them always end in a capital .S. Files ending in a lowercase .s
|
|
||||||
# will not be considered source files but generated files (assembler
|
|
||||||
# output from the compiler), and will be deleted upon "make clean"!
|
|
||||||
# Even though the DOS/Win* filesystem matches both .s and .S the same,
|
|
||||||
# it will preserve the spelling of the filenames, and gcc itself does
|
|
||||||
# care about how the name is spelled on its command-line.
|
|
||||||
ASRC =
|
|
||||||
|
|
||||||
|
|
||||||
# Optimization level, can be [0, 1, 2, 3, s].
|
|
||||||
# 0 = turn off optimization. s = optimize for size.
|
|
||||||
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
|
|
||||||
OPT = s
|
|
||||||
|
|
||||||
|
|
||||||
# Debugging format.
|
|
||||||
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
|
|
||||||
# AVR Studio 4.10 requires dwarf-2.
|
|
||||||
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
|
|
||||||
DEBUG = dwarf-2
|
|
||||||
|
|
||||||
|
|
||||||
# List any extra directories to look for include files here.
|
|
||||||
# Each directory must be seperated by a space.
|
|
||||||
# Use forward slashes for directory separators.
|
|
||||||
# For a directory that has spaces, enclose it in quotes.
|
|
||||||
EXTRAINCDIRS = $(LUFA_PATH)/
|
|
||||||
|
|
||||||
|
|
||||||
# Compiler flag to set the C Standard level.
|
|
||||||
# c89 = "ANSI" C
|
|
||||||
# gnu89 = c89 plus GCC extensions
|
|
||||||
# c99 = ISO C99 standard (not yet fully implemented)
|
|
||||||
# gnu99 = c99 plus GCC extensions
|
|
||||||
CSTANDARD = -std=c99
|
|
||||||
|
|
||||||
|
|
||||||
# Place -D or -U options here for C sources
|
|
||||||
CDEFS = -DF_CPU=$(F_CPU)UL
|
|
||||||
CDEFS += -DF_USB=$(F_USB)UL
|
|
||||||
CDEFS += -DBOARD=BOARD_$(BOARD) -DARCH=ARCH_$(ARCH)
|
|
||||||
CDEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
|
|
||||||
CDEFS += -DDEVICE_VID=$(VID)UL
|
|
||||||
CDEFS += -DDEVICE_PID=$(PID)UL
|
|
||||||
CDEFS += $(LUFA_OPTS)
|
|
||||||
|
|
||||||
|
|
||||||
# Place -D or -U options here for ASM sources
|
|
||||||
ADEFS = -DF_CPU=$(F_CPU)
|
|
||||||
ADEFS += -DF_USB=$(F_USB)UL
|
|
||||||
ADEFS += -DBOARD=BOARD_$(BOARD)
|
|
||||||
ADEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
|
|
||||||
ADEFS += $(LUFA_OPTS)
|
|
||||||
|
|
||||||
|
|
||||||
# Place -D or -U options here for C++ sources
|
|
||||||
CPPDEFS = -DF_CPU=$(F_CPU)UL
|
|
||||||
CPPDEFS += -DF_USB=$(F_USB)UL
|
|
||||||
CPPDEFS += -DBOARD=BOARD_$(BOARD)
|
|
||||||
CPPDEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
|
|
||||||
CPPDEFS += $(LUFA_OPTS)
|
|
||||||
#CPPDEFS += -D__STDC_LIMIT_MACROS
|
|
||||||
#CPPDEFS += -D__STDC_CONSTANT_MACROS
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Compiler Options C ----------------
|
|
||||||
# -g*: generate debugging information
|
|
||||||
# -O*: optimization level
|
|
||||||
# -f...: tuning, see GCC manual and avr-libc documentation
|
|
||||||
# -Wall...: warning level
|
|
||||||
# -Wa,...: tell GCC to pass this to the assembler.
|
|
||||||
# -adhlns...: create assembler listing
|
|
||||||
CFLAGS = -g$(DEBUG)
|
|
||||||
CFLAGS += $(CDEFS)
|
|
||||||
CFLAGS += -O$(OPT)
|
|
||||||
CFLAGS += -funsigned-char
|
|
||||||
CFLAGS += -funsigned-bitfields
|
|
||||||
CFLAGS += -ffunction-sections
|
|
||||||
CFLAGS += -fno-inline-small-functions
|
|
||||||
CFLAGS += -fpack-struct
|
|
||||||
CFLAGS += -fshort-enums
|
|
||||||
CFLAGS += -fno-strict-aliasing
|
|
||||||
CFLAGS += -Wall
|
|
||||||
CFLAGS += -Wstrict-prototypes
|
|
||||||
#CFLAGS += -mshort-calls
|
|
||||||
#CFLAGS += -fno-unit-at-a-time
|
|
||||||
#CFLAGS += -Wundef
|
|
||||||
#CFLAGS += -Wunreachable-code
|
|
||||||
#CFLAGS += -Wsign-compare
|
|
||||||
CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
|
|
||||||
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
|
|
||||||
CFLAGS += $(CSTANDARD)
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Compiler Options C++ ----------------
|
|
||||||
# -g*: generate debugging information
|
|
||||||
# -O*: optimization level
|
|
||||||
# -f...: tuning, see GCC manual and avr-libc documentation
|
|
||||||
# -Wall...: warning level
|
|
||||||
# -Wa,...: tell GCC to pass this to the assembler.
|
|
||||||
# -adhlns...: create assembler listing
|
|
||||||
CPPFLAGS = -g$(DEBUG)
|
|
||||||
CPPFLAGS += $(CPPDEFS)
|
|
||||||
CPPFLAGS += -O$(OPT)
|
|
||||||
CPPFLAGS += -funsigned-char
|
|
||||||
CPPFLAGS += -funsigned-bitfields
|
|
||||||
CPPFLAGS += -fpack-struct
|
|
||||||
CPPFLAGS += -fshort-enums
|
|
||||||
CPPFLAGS += -fno-exceptions
|
|
||||||
CPPFLAGS += -Wall
|
|
||||||
CPPFLAGS += -Wundef
|
|
||||||
#CPPFLAGS += -mshort-calls
|
|
||||||
#CPPFLAGS += -fno-unit-at-a-time
|
|
||||||
#CPPFLAGS += -Wstrict-prototypes
|
|
||||||
#CPPFLAGS += -Wunreachable-code
|
|
||||||
#CPPFLAGS += -Wsign-compare
|
|
||||||
CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
|
|
||||||
CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
|
|
||||||
#CPPFLAGS += $(CSTANDARD)
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Assembler Options ----------------
|
|
||||||
# -Wa,...: tell GCC to pass this to the assembler.
|
|
||||||
# -adhlns: create listing
|
|
||||||
# -gstabs: have the assembler create line number information; note that
|
|
||||||
# for use in COFF files, additional information about filenames
|
|
||||||
# and function names needs to be present in the assembler source
|
|
||||||
# files -- see avr-libc docs [FIXME: not yet described there]
|
|
||||||
# -listing-cont-lines: Sets the maximum number of continuation lines of hex
|
|
||||||
# dump that will be displayed for a given single line of source input.
|
|
||||||
ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Library Options ----------------
|
|
||||||
# Minimalistic printf version
|
|
||||||
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
|
|
||||||
|
|
||||||
# Floating point printf version (requires MATH_LIB = -lm below)
|
|
||||||
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
|
|
||||||
|
|
||||||
# If this is left blank, then it will use the Standard printf version.
|
|
||||||
PRINTF_LIB =
|
|
||||||
#PRINTF_LIB = $(PRINTF_LIB_MIN)
|
|
||||||
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
|
|
||||||
|
|
||||||
|
|
||||||
# Minimalistic scanf version
|
|
||||||
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
|
|
||||||
|
|
||||||
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
|
|
||||||
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
|
|
||||||
|
|
||||||
# If this is left blank, then it will use the Standard scanf version.
|
|
||||||
SCANF_LIB =
|
|
||||||
#SCANF_LIB = $(SCANF_LIB_MIN)
|
|
||||||
#SCANF_LIB = $(SCANF_LIB_FLOAT)
|
|
||||||
|
|
||||||
|
|
||||||
MATH_LIB = -lm
|
|
||||||
|
|
||||||
|
|
||||||
# List any extra directories to look for libraries here.
|
|
||||||
# Each directory must be seperated by a space.
|
|
||||||
# Use forward slashes for directory separators.
|
|
||||||
# For a directory that has spaces, enclose it in quotes.
|
|
||||||
EXTRALIBDIRS =
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- External Memory Options ----------------
|
|
||||||
|
|
||||||
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
|
|
||||||
# used for variables (.data/.bss) and heap (malloc()).
|
|
||||||
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
|
|
||||||
|
|
||||||
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
|
|
||||||
# only used for heap (malloc()).
|
|
||||||
#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
|
|
||||||
|
|
||||||
EXTMEMOPTS =
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Linker Options ----------------
|
|
||||||
# -Wl,...: tell GCC to pass this to linker.
|
|
||||||
# -Map: create map file
|
|
||||||
# --cref: add cross reference to map file
|
|
||||||
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
|
|
||||||
LDFLAGS += -Wl,--section-start=.text=$(BOOT_START)
|
|
||||||
LDFLAGS += -Wl,--relax
|
|
||||||
LDFLAGS += -Wl,--gc-sections
|
|
||||||
LDFLAGS += $(EXTMEMOPTS)
|
|
||||||
LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
|
|
||||||
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
|
|
||||||
#LDFLAGS += -T linker_script.x
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Programming Options (avrdude) ----------------
|
|
||||||
|
|
||||||
# Programming hardware
|
|
||||||
# Type: avrdude -c ?
|
|
||||||
# to get a full listing.
|
|
||||||
#
|
|
||||||
AVRDUDE_PROGRAMMER = avrispmkII
|
|
||||||
|
|
||||||
# com1 = serial port. Use lpt1 to connect to parallel port.
|
|
||||||
AVRDUDE_PORT = usb
|
|
||||||
|
|
||||||
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
|
|
||||||
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
|
|
||||||
|
|
||||||
|
|
||||||
# Uncomment the following if you want avrdude's erase cycle counter.
|
|
||||||
# Note that this counter needs to be initialized first using -Yn,
|
|
||||||
# see avrdude manual.
|
|
||||||
#AVRDUDE_ERASE_COUNTER = -y
|
|
||||||
|
|
||||||
# Uncomment the following if you do /not/ wish a verification to be
|
|
||||||
# performed after programming the device.
|
|
||||||
#AVRDUDE_NO_VERIFY = -V
|
|
||||||
|
|
||||||
# Increase verbosity level. Please use this when submitting bug
|
|
||||||
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
|
|
||||||
# to submit bug reports.
|
|
||||||
#AVRDUDE_VERBOSE = -v -v
|
|
||||||
|
|
||||||
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
|
|
||||||
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
|
|
||||||
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
|
|
||||||
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------- Debugging Options ----------------
|
|
||||||
|
|
||||||
# For simulavr only - target MCU frequency.
|
|
||||||
DEBUG_MFREQ = $(F_CPU)
|
|
||||||
|
|
||||||
# Set the DEBUG_UI to either gdb or insight.
|
|
||||||
# DEBUG_UI = gdb
|
|
||||||
DEBUG_UI = insight
|
|
||||||
|
|
||||||
# Set the debugging back-end to either avarice, simulavr.
|
|
||||||
DEBUG_BACKEND = avarice
|
|
||||||
#DEBUG_BACKEND = simulavr
|
|
||||||
|
|
||||||
# GDB Init Filename.
|
|
||||||
GDBINIT_FILE = __avr_gdbinit
|
|
||||||
|
|
||||||
# When using avarice settings for the JTAG
|
|
||||||
JTAG_DEV = /dev/com1
|
|
||||||
|
|
||||||
# Debugging port used to communicate between GDB / avarice / simulavr.
|
|
||||||
DEBUG_PORT = 4242
|
|
||||||
|
|
||||||
# Debugging host used to communicate between GDB / avarice / simulavr, normally
|
|
||||||
# just set to localhost unless doing some sort of crazy debugging when
|
|
||||||
# avarice is running on a different computer.
|
|
||||||
DEBUG_HOST = localhost
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#============================================================================
|
|
||||||
|
|
||||||
|
|
||||||
# Define programs and commands.
|
|
||||||
SHELL = sh
|
|
||||||
CC = avr-gcc
|
|
||||||
OBJCOPY = avr-objcopy
|
|
||||||
OBJDUMP = avr-objdump
|
|
||||||
SIZE = avr-size
|
|
||||||
AR = avr-ar rcs
|
|
||||||
NM = avr-nm
|
|
||||||
AVRDUDE = /Applications/avrdude -C /Applications/avrdude.conf -B 1
|
|
||||||
REMOVE = rm -f
|
|
||||||
REMOVEDIR = rm -rf
|
|
||||||
COPY = cp
|
|
||||||
WINSHELL = cmd
|
|
||||||
|
|
||||||
|
|
||||||
# Define Messages
|
|
||||||
# English
|
|
||||||
MSG_ERRORS_NONE = Errors: none
|
|
||||||
MSG_BEGIN = -------- begin --------
|
|
||||||
MSG_END = -------- end --------
|
|
||||||
MSG_SIZE_BEFORE = Size before:
|
|
||||||
MSG_SIZE_AFTER = Size after:
|
|
||||||
MSG_COFF = Converting to AVR COFF:
|
|
||||||
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
|
|
||||||
MSG_FLASH = Creating load file for Flash:
|
|
||||||
MSG_EEPROM = Creating load file for EEPROM:
|
|
||||||
MSG_EXTENDED_LISTING = Creating Extended Listing:
|
|
||||||
MSG_SYMBOL_TABLE = Creating Symbol Table:
|
|
||||||
MSG_LINKING = Linking:
|
|
||||||
MSG_COMPILING = Compiling C:
|
|
||||||
MSG_COMPILING_CPP = Compiling C++:
|
|
||||||
MSG_ASSEMBLING = Assembling:
|
|
||||||
MSG_CLEANING = Cleaning project:
|
|
||||||
MSG_CREATING_LIBRARY = Creating library:
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Define all object files.
|
|
||||||
OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
|
|
||||||
|
|
||||||
# Define all listing files.
|
|
||||||
LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
|
|
||||||
|
|
||||||
|
|
||||||
# Compiler flags to generate dependency files.
|
|
||||||
GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
|
|
||||||
|
|
||||||
|
|
||||||
# Combine all necessary flags and optional flags.
|
|
||||||
# Add target processor to flags.
|
|
||||||
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
|
|
||||||
ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
|
|
||||||
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Default target.
|
|
||||||
all: begin gccversion sizebefore build sizeafter end
|
|
||||||
|
|
||||||
# Change the build target to build a HEX file or a library.
|
|
||||||
build: elf hex eep lss sym
|
|
||||||
#build: lib
|
|
||||||
|
|
||||||
|
|
||||||
elf: $(TARGET).elf
|
|
||||||
hex: $(TARGET).hex
|
|
||||||
eep: $(TARGET).eep
|
|
||||||
lss: $(TARGET).lss
|
|
||||||
sym: $(TARGET).sym
|
|
||||||
LIBNAME=lib$(TARGET).a
|
|
||||||
lib: $(LIBNAME)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Eye candy.
|
|
||||||
# AVR Studio 3.x does not check make's exit code but relies on
|
|
||||||
# the following magic strings to be generated by the compile job.
|
|
||||||
begin:
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_BEGIN)
|
|
||||||
|
|
||||||
end:
|
|
||||||
@echo $(MSG_END)
|
|
||||||
@echo
|
|
||||||
|
|
||||||
|
|
||||||
# Display size of file.
|
|
||||||
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
|
|
||||||
ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
|
|
||||||
MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
|
|
||||||
FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
|
|
||||||
|
|
||||||
|
|
||||||
sizebefore:
|
|
||||||
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
|
|
||||||
2>/dev/null; echo; fi
|
|
||||||
|
|
||||||
sizeafter:
|
|
||||||
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
|
|
||||||
2>/dev/null; echo; fi
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Display compiler version information.
|
|
||||||
gccversion :
|
|
||||||
@$(CC) --version
|
|
||||||
|
|
||||||
|
|
||||||
# Program the device.
|
|
||||||
program: $(TARGET).hex $(TARGET).eep
|
|
||||||
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
|
|
||||||
|
|
||||||
|
|
||||||
# Generate avr-gdb config/init file which does the following:
|
|
||||||
# define the reset signal, load the target file, connect to target, and set
|
|
||||||
# a breakpoint at main().
|
|
||||||
gdb-config:
|
|
||||||
@$(REMOVE) $(GDBINIT_FILE)
|
|
||||||
@echo define reset >> $(GDBINIT_FILE)
|
|
||||||
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
|
|
||||||
@echo end >> $(GDBINIT_FILE)
|
|
||||||
@echo file $(TARGET).elf >> $(GDBINIT_FILE)
|
|
||||||
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
|
|
||||||
ifeq ($(DEBUG_BACKEND),simulavr)
|
|
||||||
@echo load >> $(GDBINIT_FILE)
|
|
||||||
endif
|
|
||||||
@echo break main >> $(GDBINIT_FILE)
|
|
||||||
|
|
||||||
debug: gdb-config $(TARGET).elf
|
|
||||||
ifeq ($(DEBUG_BACKEND), avarice)
|
|
||||||
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
|
|
||||||
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
|
|
||||||
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
|
|
||||||
@$(WINSHELL) /c pause
|
|
||||||
|
|
||||||
else
|
|
||||||
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
|
|
||||||
$(DEBUG_MFREQ) --port $(DEBUG_PORT)
|
|
||||||
endif
|
|
||||||
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
|
|
||||||
COFFCONVERT = $(OBJCOPY) --debugging
|
|
||||||
COFFCONVERT += --change-section-address .data-0x800000
|
|
||||||
COFFCONVERT += --change-section-address .bss-0x800000
|
|
||||||
COFFCONVERT += --change-section-address .noinit-0x800000
|
|
||||||
COFFCONVERT += --change-section-address .eeprom-0x810000
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
coff: $(TARGET).elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_COFF) $(TARGET).cof
|
|
||||||
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
|
|
||||||
|
|
||||||
|
|
||||||
extcoff: $(TARGET).elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
|
|
||||||
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create final output files (.hex, .eep) from ELF output file.
|
|
||||||
%.hex: %.elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_FLASH) $@
|
|
||||||
$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock $< $@
|
|
||||||
|
|
||||||
%.eep: %.elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_EEPROM) $@
|
|
||||||
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
|
|
||||||
--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
|
|
||||||
|
|
||||||
# Create extended listing file from ELF output file.
|
|
||||||
%.lss: %.elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_EXTENDED_LISTING) $@
|
|
||||||
$(OBJDUMP) -h -S -z $< > $@
|
|
||||||
|
|
||||||
# Create a symbol table from ELF output file.
|
|
||||||
%.sym: %.elf
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_SYMBOL_TABLE) $@
|
|
||||||
$(NM) -n $< > $@
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create library from object files.
|
|
||||||
.SECONDARY : $(TARGET).a
|
|
||||||
.PRECIOUS : $(OBJ)
|
|
||||||
%.a: $(OBJ)
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_CREATING_LIBRARY) $@
|
|
||||||
$(AR) $@ $(OBJ)
|
|
||||||
|
|
||||||
|
|
||||||
# Link: create ELF output file from object files.
|
|
||||||
.SECONDARY : $(TARGET).elf
|
|
||||||
.PRECIOUS : $(OBJ)
|
|
||||||
%.elf: $(OBJ)
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_LINKING) $@
|
|
||||||
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
|
|
||||||
|
|
||||||
|
|
||||||
# Compile: create object files from C source files.
|
|
||||||
$(OBJDIR)/%.o : %.c
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_COMPILING) $<
|
|
||||||
$(CC) -c $(ALL_CFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Compile: create object files from C++ source files.
|
|
||||||
$(OBJDIR)/%.o : %.cpp
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_COMPILING_CPP) $<
|
|
||||||
$(CC) -c $(ALL_CPPFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Compile: create assembler files from C source files.
|
|
||||||
%.s : %.c
|
|
||||||
$(CC) -S $(ALL_CFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Compile: create assembler files from C++ source files.
|
|
||||||
%.s : %.cpp
|
|
||||||
$(CC) -S $(ALL_CPPFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Assemble: create object files from assembler source files.
|
|
||||||
$(OBJDIR)/%.o : %.S
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_ASSEMBLING) $<
|
|
||||||
$(CC) -c $(ALL_ASFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Create preprocessed source for use in sending a bug report.
|
|
||||||
%.i : %.c
|
|
||||||
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# Target: clean project.
|
|
||||||
clean: begin clean_list end
|
|
||||||
|
|
||||||
clean_list :
|
|
||||||
@echo
|
|
||||||
@echo $(MSG_CLEANING)
|
|
||||||
$(REMOVE) $(TARGET).hex
|
|
||||||
$(REMOVE) $(TARGET).eep
|
|
||||||
$(REMOVE) $(TARGET).cof
|
|
||||||
$(REMOVE) $(TARGET).elf
|
|
||||||
$(REMOVE) $(TARGET).map
|
|
||||||
$(REMOVE) $(TARGET).sym
|
|
||||||
$(REMOVE) $(TARGET).lss
|
|
||||||
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
|
|
||||||
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
|
|
||||||
$(REMOVE) $(SRC:.c=.s)
|
|
||||||
$(REMOVE) $(SRC:.c=.d)
|
|
||||||
$(REMOVE) $(SRC:.c=.i)
|
|
||||||
$(REMOVEDIR) .dep
|
|
||||||
|
|
||||||
doxygen:
|
|
||||||
@echo Generating Project Documentation \($(TARGET)\)...
|
|
||||||
@doxygen Doxygen.conf
|
|
||||||
@echo Documentation Generation Complete.
|
|
||||||
|
|
||||||
clean_doxygen:
|
|
||||||
rm -rf Documentation
|
|
||||||
|
|
||||||
checksource:
|
|
||||||
@for f in $(SRC) $(CPPSRC) $(ASRC); do \
|
|
||||||
if [ -f $$f ]; then \
|
|
||||||
echo "Found Source File: $$f" ; \
|
|
||||||
else \
|
|
||||||
echo "Source File Not Found: $$f" ; \
|
|
||||||
fi; done
|
|
||||||
|
|
||||||
|
|
||||||
# Create object files directory
|
|
||||||
$(shell mkdir $(OBJDIR) 2>/dev/null)
|
|
||||||
|
|
||||||
|
|
||||||
# Include the dependency files.
|
|
||||||
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
|
|
||||||
|
|
||||||
|
|
||||||
# Listing of phony targets.
|
|
||||||
.PHONY : all begin finish end sizebefore sizeafter gccversion \
|
|
||||||
build elf hex eep lss sym coff extcoff doxygen clean \
|
|
||||||
clean_list clean_doxygen program debug gdb-config checksource
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
@ -1,11 +0,0 @@
|
|||||||
Builds against LUFA version 111009
|
|
||||||
make version 3.81
|
|
||||||
avrdude version 5.11
|
|
||||||
|
|
||||||
All AVR tools except avrdude were installed by CrossPack 20100115:
|
|
||||||
avr-gcc version 4.3.3 (GCC)
|
|
||||||
Thread model: single
|
|
||||||
Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
|
|
||||||
avr-libc version 1.6.7
|
|
||||||
binutils version 2.19
|
|
||||||
|
|
@ -1,249 +0,0 @@
|
|||||||
/*
|
|
||||||
Arduino.h - Main include file for the Arduino SDK
|
|
||||||
Copyright (c) 2005-2013 Arduino Team. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Arduino_h
|
|
||||||
#define Arduino_h
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
|
|
||||||
#include "binary.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C"{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void yield(void);
|
|
||||||
|
|
||||||
#define HIGH 0x1
|
|
||||||
#define LOW 0x0
|
|
||||||
|
|
||||||
#define INPUT 0x0
|
|
||||||
#define OUTPUT 0x1
|
|
||||||
#define INPUT_PULLUP 0x2
|
|
||||||
|
|
||||||
#define PI 3.1415926535897932384626433832795
|
|
||||||
#define HALF_PI 1.5707963267948966192313216916398
|
|
||||||
#define TWO_PI 6.283185307179586476925286766559
|
|
||||||
#define DEG_TO_RAD 0.017453292519943295769236907684886
|
|
||||||
#define RAD_TO_DEG 57.295779513082320876798154814105
|
|
||||||
#define EULER 2.718281828459045235360287471352
|
|
||||||
|
|
||||||
#define SERIAL 0x0
|
|
||||||
#define DISPLAY 0x1
|
|
||||||
|
|
||||||
#define LSBFIRST 0
|
|
||||||
#define MSBFIRST 1
|
|
||||||
|
|
||||||
#define CHANGE 1
|
|
||||||
#define FALLING 2
|
|
||||||
#define RISING 3
|
|
||||||
|
|
||||||
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
|
||||||
#define DEFAULT 0
|
|
||||||
#define EXTERNAL 1
|
|
||||||
#define INTERNAL 2
|
|
||||||
#else
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
|
|
||||||
#define INTERNAL1V1 2
|
|
||||||
#define INTERNAL2V56 3
|
|
||||||
#else
|
|
||||||
#define INTERNAL 3
|
|
||||||
#endif
|
|
||||||
#define DEFAULT 1
|
|
||||||
#define EXTERNAL 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// undefine stdlib's abs if encountered
|
|
||||||
#ifdef abs
|
|
||||||
#undef abs
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define min(a,b) ((a)<(b)?(a):(b))
|
|
||||||
#define max(a,b) ((a)>(b)?(a):(b))
|
|
||||||
#define abs(x) ((x)>0?(x):-(x))
|
|
||||||
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
|
|
||||||
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
|
|
||||||
#define radians(deg) ((deg)*DEG_TO_RAD)
|
|
||||||
#define degrees(rad) ((rad)*RAD_TO_DEG)
|
|
||||||
#define sq(x) ((x)*(x))
|
|
||||||
|
|
||||||
#define interrupts() sei()
|
|
||||||
#define noInterrupts() cli()
|
|
||||||
|
|
||||||
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
|
||||||
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
|
||||||
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
|
||||||
|
|
||||||
#define lowByte(w) ((uint8_t) ((w) & 0xff))
|
|
||||||
#define highByte(w) ((uint8_t) ((w) >> 8))
|
|
||||||
|
|
||||||
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
|
|
||||||
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
|
|
||||||
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
|
|
||||||
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
|
|
||||||
|
|
||||||
// avr-libc defines _NOP() since 1.6.2
|
|
||||||
#ifndef _NOP
|
|
||||||
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef unsigned int word;
|
|
||||||
|
|
||||||
#define bit(b) (1UL << (b))
|
|
||||||
|
|
||||||
typedef uint8_t boolean;
|
|
||||||
typedef uint8_t byte;
|
|
||||||
|
|
||||||
void init(void);
|
|
||||||
void initVariant(void);
|
|
||||||
|
|
||||||
int atexit(void (*func)()) __attribute__((weak));
|
|
||||||
|
|
||||||
void pinMode(uint8_t, uint8_t);
|
|
||||||
void digitalWrite(uint8_t, uint8_t);
|
|
||||||
int digitalRead(uint8_t);
|
|
||||||
int analogRead(uint8_t);
|
|
||||||
void analogReference(uint8_t mode);
|
|
||||||
void analogWrite(uint8_t, int);
|
|
||||||
|
|
||||||
unsigned long millis(void);
|
|
||||||
unsigned long micros(void);
|
|
||||||
void delay(unsigned long);
|
|
||||||
void delayMicroseconds(unsigned int us);
|
|
||||||
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
|
|
||||||
|
|
||||||
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
|
|
||||||
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
|
|
||||||
|
|
||||||
void attachInterrupt(uint8_t, void (*)(void), int mode);
|
|
||||||
void detachInterrupt(uint8_t);
|
|
||||||
|
|
||||||
void setup(void);
|
|
||||||
void loop(void);
|
|
||||||
|
|
||||||
// Get the bit location within the hardware port of the given virtual pin.
|
|
||||||
// This comes from the pins_*.c file for the active board configuration.
|
|
||||||
|
|
||||||
#define analogInPinToBit(P) (P)
|
|
||||||
|
|
||||||
// On the ATmega1280, the addresses of some of the port registers are
|
|
||||||
// greater than 255, so we can't store them in uint8_t's.
|
|
||||||
extern const uint16_t PROGMEM port_to_mode_PGM[];
|
|
||||||
extern const uint16_t PROGMEM port_to_input_PGM[];
|
|
||||||
extern const uint16_t PROGMEM port_to_output_PGM[];
|
|
||||||
|
|
||||||
extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
|
|
||||||
// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
|
|
||||||
extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
|
|
||||||
extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
|
|
||||||
|
|
||||||
// Get the bit location within the hardware port of the given virtual pin.
|
|
||||||
// This comes from the pins_*.c file for the active board configuration.
|
|
||||||
//
|
|
||||||
// These perform slightly better as macros compared to inline functions
|
|
||||||
//
|
|
||||||
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
|
|
||||||
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
|
|
||||||
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
|
|
||||||
#define analogInPinToBit(P) (P)
|
|
||||||
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
|
|
||||||
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
|
|
||||||
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
|
|
||||||
|
|
||||||
#define NOT_A_PIN 0
|
|
||||||
#define NOT_A_PORT 0
|
|
||||||
|
|
||||||
#define NOT_AN_INTERRUPT -1
|
|
||||||
|
|
||||||
#ifdef ARDUINO_MAIN
|
|
||||||
#define PA 1
|
|
||||||
#define PB 2
|
|
||||||
#define PC 3
|
|
||||||
#define PD 4
|
|
||||||
#define PE 5
|
|
||||||
#define PF 6
|
|
||||||
#define PG 7
|
|
||||||
#define PH 8
|
|
||||||
#define PJ 10
|
|
||||||
#define PK 11
|
|
||||||
#define PL 12
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define NOT_ON_TIMER 0
|
|
||||||
#define TIMER0A 1
|
|
||||||
#define TIMER0B 2
|
|
||||||
#define TIMER1A 3
|
|
||||||
#define TIMER1B 4
|
|
||||||
#define TIMER1C 5
|
|
||||||
#define TIMER2 6
|
|
||||||
#define TIMER2A 7
|
|
||||||
#define TIMER2B 8
|
|
||||||
|
|
||||||
#define TIMER3A 9
|
|
||||||
#define TIMER3B 10
|
|
||||||
#define TIMER3C 11
|
|
||||||
#define TIMER4A 12
|
|
||||||
#define TIMER4B 13
|
|
||||||
#define TIMER4C 14
|
|
||||||
#define TIMER4D 15
|
|
||||||
#define TIMER5A 16
|
|
||||||
#define TIMER5B 17
|
|
||||||
#define TIMER5C 18
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
} // extern "C"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
#include "WCharacter.h"
|
|
||||||
#include "WString.h"
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "USBAPI.h"
|
|
||||||
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
|
|
||||||
#error "Targets with both UART0 and CDC serial not supported"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint16_t makeWord(uint16_t w);
|
|
||||||
uint16_t makeWord(byte h, byte l);
|
|
||||||
|
|
||||||
#define word(...) makeWord(__VA_ARGS__)
|
|
||||||
|
|
||||||
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
|
|
||||||
|
|
||||||
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
|
|
||||||
void noTone(uint8_t _pin);
|
|
||||||
|
|
||||||
// WMath prototypes
|
|
||||||
long random(long);
|
|
||||||
long random(long, long);
|
|
||||||
void randomSeed(unsigned int);
|
|
||||||
long map(long, long, long, long, long);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,211 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
/* Copyright (c) 2011, Peter Barrett
|
|
||||||
**
|
|
||||||
** Permission to use, copy, modify, and/or distribute this software for
|
|
||||||
** any purpose with or without fee is hereby granted, provided that the
|
|
||||||
** above copyright notice and this permission notice appear in all copies.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
||||||
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
||||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
||||||
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
||||||
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
||||||
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
||||||
** SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "USBAPI.h"
|
|
||||||
#include <avr/wdt.h>
|
|
||||||
|
|
||||||
#if defined(USBCON)
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u32 dwDTERate;
|
|
||||||
u8 bCharFormat;
|
|
||||||
u8 bParityType;
|
|
||||||
u8 bDataBits;
|
|
||||||
u8 lineState;
|
|
||||||
} LineInfo;
|
|
||||||
|
|
||||||
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
|
|
||||||
|
|
||||||
#define WEAK __attribute__ ((weak))
|
|
||||||
|
|
||||||
extern const CDCDescriptor _cdcInterface PROGMEM;
|
|
||||||
const CDCDescriptor _cdcInterface =
|
|
||||||
{
|
|
||||||
D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
|
|
||||||
|
|
||||||
// CDC communication interface
|
|
||||||
D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
|
|
||||||
D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
|
|
||||||
D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
|
|
||||||
D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
|
|
||||||
D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
|
|
||||||
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
|
|
||||||
|
|
||||||
// CDC data interface
|
|
||||||
D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
|
|
||||||
D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
|
|
||||||
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
|
|
||||||
};
|
|
||||||
|
|
||||||
int WEAK CDC_GetInterface(u8* interfaceNum)
|
|
||||||
{
|
|
||||||
interfaceNum[0] += 2; // uses 2
|
|
||||||
return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WEAK CDC_Setup(Setup& setup)
|
|
||||||
{
|
|
||||||
u8 r = setup.bRequest;
|
|
||||||
u8 requestType = setup.bmRequestType;
|
|
||||||
|
|
||||||
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
|
|
||||||
{
|
|
||||||
if (CDC_GET_LINE_CODING == r)
|
|
||||||
{
|
|
||||||
USB_SendControl(0,(void*)&_usbLineInfo,7);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
|
|
||||||
{
|
|
||||||
if (CDC_SET_LINE_CODING == r)
|
|
||||||
{
|
|
||||||
USB_RecvControl((void*)&_usbLineInfo,7);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CDC_SET_CONTROL_LINE_STATE == r)
|
|
||||||
{
|
|
||||||
_usbLineInfo.lineState = setup.wValueL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r)
|
|
||||||
{
|
|
||||||
// auto-reset into the bootloader is triggered when the port, already
|
|
||||||
// open at 1200 bps, is closed. this is the signal to start the watchdog
|
|
||||||
// with a relatively long period so it can finish housekeeping tasks
|
|
||||||
// like servicing endpoints before the sketch ends
|
|
||||||
|
|
||||||
// We check DTR state to determine if host port is open (bit 0 of lineState).
|
|
||||||
if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0)
|
|
||||||
{
|
|
||||||
*(uint16_t *)0x0800 = 0x7777;
|
|
||||||
wdt_enable(WDTO_120MS);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Most OSs do some intermediate steps when configuring ports and DTR can
|
|
||||||
// twiggle more than once before stabilizing.
|
|
||||||
// To avoid spurious resets we set the watchdog to 250ms and eventually
|
|
||||||
// cancel if DTR goes back high.
|
|
||||||
|
|
||||||
wdt_disable();
|
|
||||||
wdt_reset();
|
|
||||||
*(uint16_t *)0x0800 = 0x0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Serial_::begin(unsigned long /* baud_count */)
|
|
||||||
{
|
|
||||||
peek_buffer = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Serial_::begin(unsigned long /* baud_count */, byte /* config */)
|
|
||||||
{
|
|
||||||
peek_buffer = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Serial_::end(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
int Serial_::available(void)
|
|
||||||
{
|
|
||||||
if (peek_buffer >= 0) {
|
|
||||||
return 1 + USB_Available(CDC_RX);
|
|
||||||
}
|
|
||||||
return USB_Available(CDC_RX);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Serial_::peek(void)
|
|
||||||
{
|
|
||||||
if (peek_buffer < 0)
|
|
||||||
peek_buffer = USB_Recv(CDC_RX);
|
|
||||||
return peek_buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Serial_::read(void)
|
|
||||||
{
|
|
||||||
if (peek_buffer >= 0) {
|
|
||||||
int c = peek_buffer;
|
|
||||||
peek_buffer = -1;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
return USB_Recv(CDC_RX);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Serial_::flush(void)
|
|
||||||
{
|
|
||||||
USB_Flush(CDC_TX);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Serial_::write(uint8_t c)
|
|
||||||
{
|
|
||||||
return write(&c, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Serial_::write(const uint8_t *buffer, size_t size)
|
|
||||||
{
|
|
||||||
/* only try to send bytes if the high-level CDC connection itself
|
|
||||||
is open (not just the pipe) - the OS should set lineState when the port
|
|
||||||
is opened and clear lineState when the port is closed.
|
|
||||||
bytes sent before the user opens the connection or after
|
|
||||||
the connection is closed are lost - just like with a UART. */
|
|
||||||
|
|
||||||
// TODO - ZE - check behavior on different OSes and test what happens if an
|
|
||||||
// open connection isn't broken cleanly (cable is yanked out, host dies
|
|
||||||
// or locks up, or host virtual serial port hangs)
|
|
||||||
if (_usbLineInfo.lineState > 0) {
|
|
||||||
int r = USB_Send(CDC_TX,buffer,size);
|
|
||||||
if (r > 0) {
|
|
||||||
return r;
|
|
||||||
} else {
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This operator is a convenient way for a sketch to check whether the
|
|
||||||
// port has actually been configured and opened by the host (as opposed
|
|
||||||
// to just being connected to the host). It can be used, for example, in
|
|
||||||
// setup() before printing to ensure that an application on the host is
|
|
||||||
// actually ready to receive and display the data.
|
|
||||||
// We add a short delay before returning to fix a bug observed by Federico
|
|
||||||
// where the port is configured (lineState != 0) but not quite opened.
|
|
||||||
Serial_::operator bool() {
|
|
||||||
bool result = false;
|
|
||||||
if (_usbLineInfo.lineState > 0)
|
|
||||||
result = true;
|
|
||||||
delay(10);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial_ Serial;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
#endif /* if defined(USBCON) */
|
|
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
Client.h - Base class that provides Client
|
|
||||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef client_h
|
|
||||||
#define client_h
|
|
||||||
#include "Print.h"
|
|
||||||
#include "Stream.h"
|
|
||||||
#include "IPAddress.h"
|
|
||||||
|
|
||||||
class Client : public Stream {
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual int connect(IPAddress ip, uint16_t port) =0;
|
|
||||||
virtual int connect(const char *host, uint16_t port) =0;
|
|
||||||
virtual size_t write(uint8_t) =0;
|
|
||||||
virtual size_t write(const uint8_t *buf, size_t size) =0;
|
|
||||||
virtual int available() = 0;
|
|
||||||
virtual int read() = 0;
|
|
||||||
virtual int read(uint8_t *buf, size_t size) = 0;
|
|
||||||
virtual int peek() = 0;
|
|
||||||
virtual void flush() = 0;
|
|
||||||
virtual void stop() = 0;
|
|
||||||
virtual uint8_t connected() = 0;
|
|
||||||
virtual operator bool() = 0;
|
|
||||||
protected:
|
|
||||||
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,805 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
/* Copyright (c) 2011, Peter Barrett
|
|
||||||
**
|
|
||||||
** Sleep/Wakeup/SystemControl support added by Michael Dreher
|
|
||||||
**
|
|
||||||
** Permission to use, copy, modify, and/or distribute this software for
|
|
||||||
** any purpose with or without fee is hereby granted, provided that the
|
|
||||||
** above copyright notice and this permission notice appear in all copies.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
||||||
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
||||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
||||||
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
||||||
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
||||||
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
||||||
** SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "USBAPI.h"
|
|
||||||
#include "USBDesc.h"
|
|
||||||
#include "HIDTables.h"
|
|
||||||
|
|
||||||
#if defined(USBCON)
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
#define HID_MOUSE_ABS_ENABLED
|
|
||||||
//#define RAWHID_ENABLED
|
|
||||||
|
|
||||||
// Singletons for mouse and keyboard
|
|
||||||
|
|
||||||
Mouse_ Mouse;
|
|
||||||
Keyboard_ Keyboard;
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
|
|
||||||
// HID report descriptor
|
|
||||||
|
|
||||||
#define LSB(_x) ((_x) & 0xFF)
|
|
||||||
#define MSB(_x) ((_x) >> 8)
|
|
||||||
|
|
||||||
#define RAWHID_USAGE_PAGE 0xFFC0
|
|
||||||
#define RAWHID_USAGE 0x0C00
|
|
||||||
#define RAWHID_TX_SIZE 64
|
|
||||||
#define RAWHID_RX_SIZE 64
|
|
||||||
|
|
||||||
#define HID_REPORTID_KEYBOARD (1)
|
|
||||||
#define HID_REPORTID_MOUSE (2)
|
|
||||||
#define HID_REPORTID_MOUSE_ABS (3)
|
|
||||||
#define HID_REPORTID_SYSTEMCONTROL (4)
|
|
||||||
#define HID_REPORTID_CONSUMERCONTROL (5)
|
|
||||||
#define HID_REPORTID_RAWHID (6)
|
|
||||||
|
|
||||||
#define HID_REPORT_KEYBOARD /* Keyboard */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) 47 */ \
|
|
||||||
0x09, 0x06, /* USAGE (Keyboard) */ \
|
|
||||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
|
||||||
0x85, HID_REPORTID_KEYBOARD, /* REPORT_ID */ \
|
|
||||||
0x05, 0x07, /* USAGE_PAGE (Keyboard) */ \
|
|
||||||
\
|
|
||||||
/* Keyboard Modifiers (shift, alt, ...) */ \
|
|
||||||
0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */ \
|
|
||||||
0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
|
||||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
|
||||||
\
|
|
||||||
0x95, 0x08, /* REPORT_COUNT (8) */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0x95, 0x01, /* REPORT_COUNT (1) */ \
|
|
||||||
0x75, 0x08, /* REPORT_SIZE (8) */ \
|
|
||||||
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */ \
|
|
||||||
\
|
|
||||||
/* Keyboard keys */ \
|
|
||||||
0x95, 0x06, /* REPORT_COUNT (6) */ \
|
|
||||||
0x75, 0x08, /* REPORT_SIZE (8) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x26, 0xDF, 0x00, /* LOGICAL_MAXIMUM (239) */ \
|
|
||||||
0x05, 0x07, /* USAGE_PAGE (Keyboard) */ \
|
|
||||||
0x19, 0x00, /* USAGE_MINIMUM (Reserved (no event indicated)) */ \
|
|
||||||
0x29, 0xDF, /* USAGE_MAXIMUM (Left Control - 1) */ \
|
|
||||||
0x81, 0x00, /* INPUT (Data,Ary,Abs) */ \
|
|
||||||
0xc0 /* END_COLLECTION */
|
|
||||||
|
|
||||||
#define HID_REPORT_MOUSE_ABSOLUTE /* Mouse absolute */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \
|
|
||||||
0x09, 0x02, /* USAGE (Mouse) */ \
|
|
||||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
|
||||||
0x09, 0x01, /* USAGE (Pointer) */ \
|
|
||||||
0xa1, 0x00, /* COLLECTION (Physical) */ \
|
|
||||||
0x85, HID_REPORTID_MOUSE_ABS, /* REPORT_ID */ \
|
|
||||||
0x05, 0x09, /* USAGE_PAGE (Button) */ \
|
|
||||||
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ \
|
|
||||||
0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
|
||||||
0x95, 0x03, /* REPORT_COUNT (3) */ \
|
|
||||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0x95, 0x01, /* REPORT_COUNT (1) */ \
|
|
||||||
0x75, 0x05, /* REPORT_SIZE (5) */ \
|
|
||||||
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \
|
|
||||||
0x09, 0x30, /* USAGE (X) */ \
|
|
||||||
0x09, 0x31, /* USAGE (Y) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (32767) */ \
|
|
||||||
0x75, 0x10, /* REPORT_SIZE (16) */ \
|
|
||||||
0x95, 0x02, /* REPORT_COUNT (2) */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0xc0, /* END_COLLECTION */ \
|
|
||||||
0xc0 /* END_COLLECTION */
|
|
||||||
|
|
||||||
#define HID_REPORT_MOUSE_RELATIVE /* Mouse relative */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) 54 */ \
|
|
||||||
0x09, 0x02, /* USAGE (Mouse) */ \
|
|
||||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
|
||||||
0x09, 0x01, /* USAGE (Pointer) */ \
|
|
||||||
0xa1, 0x00, /* COLLECTION (Physical) */ \
|
|
||||||
0x85, HID_REPORTID_MOUSE, /* REPORT_ID */ \
|
|
||||||
0x05, 0x09, /* USAGE_PAGE (Button) */ \
|
|
||||||
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ \
|
|
||||||
0x29, 0x05, /* USAGE_MAXIMUM (Button 5) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
|
||||||
0x95, 0x05, /* REPORT_COUNT (5) */ \
|
|
||||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0x95, 0x01, /* REPORT_COUNT (1) */ \
|
|
||||||
0x75, 0x03, /* REPORT_SIZE (3) */ \
|
|
||||||
0x81, 0x01, /* INPUT (Cnst,Var,Abs) */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \
|
|
||||||
0x09, 0x30, /* USAGE (X) */ \
|
|
||||||
0x09, 0x31, /* USAGE (Y) */ \
|
|
||||||
0x09, 0x38, /* USAGE (Wheel) */ \
|
|
||||||
0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ \
|
|
||||||
0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ \
|
|
||||||
0x75, 0x08, /* REPORT_SIZE (8) */ \
|
|
||||||
0x95, 0x03, /* REPORT_COUNT (3) */ \
|
|
||||||
0x81, 0x06, /* INPUT (Data,Var,Rel) */ \
|
|
||||||
0xc0, /* END_COLLECTION */ \
|
|
||||||
0xc0 /* END_COLLECTION */
|
|
||||||
|
|
||||||
#define HID_REPORT_SYSTEMCONTROL /* System Control (Power Down, Sleep, Wakeup, ...) */ \
|
|
||||||
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ \
|
|
||||||
0x09, 0x80, /* USAGE (System Control) */ \
|
|
||||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
|
||||||
0x85, HID_REPORTID_SYSTEMCONTROL, /* REPORT_ID */ \
|
|
||||||
0x09, 0x81, /* USAGE (System Power Down) */ \
|
|
||||||
0x09, 0x82, /* USAGE (System Sleep) */ \
|
|
||||||
0x09, 0x83, /* USAGE (System Wakeup) */ \
|
|
||||||
0x09, 0x8E, /* USAGE (System Cold Restart) */ \
|
|
||||||
0x09, 0x8F, /* USAGE (System Warm Restart) */ \
|
|
||||||
0x09, 0xA0, /* USAGE (System Dock) */ \
|
|
||||||
0x09, 0xA1, /* USAGE (System Undock) */ \
|
|
||||||
0x09, 0xA7, /* USAGE (System Speaker Mute) */ \
|
|
||||||
0x09, 0xA8, /* USAGE (System Hibernate) */ \
|
|
||||||
/* although these display usages are not that important, they don't cost */ \
|
|
||||||
/* much more than declaring the otherwise necessary constant fill bits */ \
|
|
||||||
0x09, 0xB0, /* USAGE (System Display Invert) */ \
|
|
||||||
0x09, 0xB1, /* USAGE (System Display Internal) */ \
|
|
||||||
0x09, 0xB2, /* USAGE (System Display External) */ \
|
|
||||||
0x09, 0xB3, /* USAGE (System Display Both) */ \
|
|
||||||
0x09, 0xB4, /* USAGE (System Display Dual) */ \
|
|
||||||
0x09, 0xB5, /* USAGE (System Display Toggle Intern/Extern) */ \
|
|
||||||
0x09, 0xB6, /* USAGE (System Display Swap) */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
|
||||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
|
||||||
0x95, 0x10, /* REPORT_COUNT (16) */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0xc0 /* END_COLLECTION */
|
|
||||||
|
|
||||||
#define HID_REPORT_CONSUMERCONTROL /* Consumer Control (Sound/Media keys) */ \
|
|
||||||
0x05, 0x0c, /* USAGE_PAGE (Consumer Devices) */ \
|
|
||||||
0x09, 0x01, /* USAGE (Consumer Control) */ \
|
|
||||||
0xa1, 0x01, /* COLLECTION (Application) */ \
|
|
||||||
0x85, HID_REPORTID_CONSUMERCONTROL, /* REPORT_ID */ \
|
|
||||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */ \
|
|
||||||
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ \
|
|
||||||
0x75, 0x01, /* REPORT_SIZE (1) */ \
|
|
||||||
0x95, 0x08, /* REPORT_COUNT (8) */ \
|
|
||||||
0x09, 0xe2, /* USAGE (Mute) 0x01 */ \
|
|
||||||
0x09, 0xe9, /* USAGE (Volume Up) 0x02 */ \
|
|
||||||
0x09, 0xea, /* USAGE (Volume Down) 0x03 */ \
|
|
||||||
0x09, 0xcd, /* USAGE (Play/Pause) 0x04 */ \
|
|
||||||
0x09, 0xb7, /* USAGE (Stop) 0x05 */ \
|
|
||||||
0x09, 0xb6, /* USAGE (Scan Previous Track) 0x06 */ \
|
|
||||||
0x09, 0xb5, /* USAGE (Scan Next Track) 0x07 */ \
|
|
||||||
0x09, 0xb8, /* USAGE (Eject) 0x08 */ \
|
|
||||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */ \
|
|
||||||
0xc0
|
|
||||||
|
|
||||||
#define HID_REPORT_RAWHID /* RAW HID */ \
|
|
||||||
0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), /* 30 */ \
|
|
||||||
0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), \
|
|
||||||
\
|
|
||||||
0xA1, 0x01, /* Collection 0x01 */ \
|
|
||||||
0x85, HID_REPORTID_RAWHID, /* REPORT_ID */ \
|
|
||||||
0x75, 0x08, /* report size = 8 bits */ \
|
|
||||||
0x15, 0x00, /* logical minimum = 0 */ \
|
|
||||||
0x26, 0xFF, 0x00, /* logical maximum = 255 */ \
|
|
||||||
\
|
|
||||||
0x95, 64, /* report count TX */ \
|
|
||||||
0x09, 0x01, /* usage */ \
|
|
||||||
0x81, 0x02, /* Input (array) */ \
|
|
||||||
\
|
|
||||||
0x95, 64, /* report count RX */ \
|
|
||||||
0x09, 0x02, /* usage */ \
|
|
||||||
0x91, 0x02, /* Output (array) */ \
|
|
||||||
0xC0 /* end collection */
|
|
||||||
|
|
||||||
extern const u8 _hidReportDescriptor[] PROGMEM;
|
|
||||||
const u8 _hidReportDescriptor[] = {
|
|
||||||
|
|
||||||
HID_REPORT_KEYBOARD,
|
|
||||||
HID_REPORT_MOUSE_RELATIVE,
|
|
||||||
#ifdef HID_MOUSE_ABS_ENABLED
|
|
||||||
HID_REPORT_MOUSE_ABSOLUTE,
|
|
||||||
#endif
|
|
||||||
HID_REPORT_SYSTEMCONTROL,
|
|
||||||
HID_REPORT_CONSUMERCONTROL,
|
|
||||||
#if RAWHID_ENABLED
|
|
||||||
HID_REPORT_RAWHID
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
extern const HIDDescriptor _hidInterface PROGMEM;
|
|
||||||
const HIDDescriptor _hidInterface =
|
|
||||||
{
|
|
||||||
D_INTERFACE(HID_INTERFACE,1,3,0,0),
|
|
||||||
D_HIDREPORT(sizeof(_hidReportDescriptor)),
|
|
||||||
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
|
|
||||||
};
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Driver
|
|
||||||
|
|
||||||
u8 _hid_protocol = 1;
|
|
||||||
u8 _hid_idle = 1;
|
|
||||||
|
|
||||||
#define WEAK __attribute__ ((weak))
|
|
||||||
|
|
||||||
int WEAK HID_GetInterface(u8* interfaceNum)
|
|
||||||
{
|
|
||||||
interfaceNum[0] += 1; // uses 1
|
|
||||||
return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
|
|
||||||
}
|
|
||||||
|
|
||||||
int WEAK HID_GetDescriptor(int /* i */)
|
|
||||||
{
|
|
||||||
return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
|
|
||||||
}
|
|
||||||
|
|
||||||
void WEAK HID_SendReport(u8 id, const void* data, int len)
|
|
||||||
{
|
|
||||||
USB_Send(HID_TX, &id, 1);
|
|
||||||
USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WEAK HID_Setup(Setup& setup)
|
|
||||||
{
|
|
||||||
u8 r = setup.bRequest;
|
|
||||||
u8 requestType = setup.bmRequestType;
|
|
||||||
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
|
|
||||||
{
|
|
||||||
if (HID_GET_REPORT == r)
|
|
||||||
{
|
|
||||||
//HID_GetReport();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (HID_GET_PROTOCOL == r)
|
|
||||||
{
|
|
||||||
//Send8(_hid_protocol); // TODO
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
|
|
||||||
{
|
|
||||||
if (HID_SET_PROTOCOL == r)
|
|
||||||
{
|
|
||||||
_hid_protocol = setup.wValueL;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HID_SET_IDLE == r)
|
|
||||||
{
|
|
||||||
_hid_idle = setup.wValueL;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Mouse
|
|
||||||
|
|
||||||
Mouse_::Mouse_(void) : _buttons(0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::begin(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::end(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::click(uint8_t b)
|
|
||||||
{
|
|
||||||
_buttons = b;
|
|
||||||
move(0,0,0);
|
|
||||||
_buttons = 0;
|
|
||||||
move(0,0,0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::move(signed char x, signed char y, signed char wheel)
|
|
||||||
{
|
|
||||||
u8 m[4];
|
|
||||||
m[0] = _buttons;
|
|
||||||
m[1] = x;
|
|
||||||
m[2] = y;
|
|
||||||
m[3] = wheel;
|
|
||||||
HID_SendReport(HID_REPORTID_MOUSE,m,sizeof(m));
|
|
||||||
}
|
|
||||||
|
|
||||||
// X and Y have the range of 0 to 32767.
|
|
||||||
// The USB Host will convert them to pixels on the screen.
|
|
||||||
//
|
|
||||||
// x=0,y=0 is top-left corner of the screen
|
|
||||||
// x=32767,y=0 is the top right corner
|
|
||||||
// x=32767,y=32767 is the bottom right corner
|
|
||||||
// x=0,y=32767 is the bottom left corner
|
|
||||||
//
|
|
||||||
// When converting these coordinates to pixels on screen, Mac OS X's
|
|
||||||
// default HID driver maps the inner 85% of the coordinate space to
|
|
||||||
// the screen's physical dimensions. This means that any value between
|
|
||||||
// 0 and 2293 or 30474 and 32767 will move the mouse to the screen
|
|
||||||
// edge on a Mac
|
|
||||||
//
|
|
||||||
// For details, see:
|
|
||||||
// http://lists.apple.com/archives/usb/2011/Jun/msg00032.html
|
|
||||||
|
|
||||||
|
|
||||||
void Mouse_::moveAbsolute(uint16_t x, uint16_t y)
|
|
||||||
{
|
|
||||||
u8 m[5];
|
|
||||||
m[0] = _buttons;
|
|
||||||
m[1] = LSB(x);
|
|
||||||
m[2] = MSB(x);
|
|
||||||
m[3] = LSB(y);
|
|
||||||
m[4] = MSB(y);
|
|
||||||
HID_SendReport(HID_REPORTID_MOUSE_ABS,m,sizeof(m));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::buttons(uint8_t b)
|
|
||||||
{
|
|
||||||
if (b != _buttons)
|
|
||||||
{
|
|
||||||
_buttons = b;
|
|
||||||
move(0,0,0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::press(uint8_t b)
|
|
||||||
{
|
|
||||||
buttons(_buttons | b);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mouse_::release(uint8_t b)
|
|
||||||
{
|
|
||||||
buttons(_buttons & ~b);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Mouse_::isPressed(uint8_t b)
|
|
||||||
{
|
|
||||||
if ((b & _buttons) > 0)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Keyboard
|
|
||||||
|
|
||||||
Keyboard_::Keyboard_(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Keyboard_::begin(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Keyboard_::end(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Keyboard_::sendReport(KeyReport* keys)
|
|
||||||
{
|
|
||||||
HID_SendReport(HID_REPORTID_KEYBOARD,keys,sizeof(*keys));
|
|
||||||
}
|
|
||||||
|
|
||||||
extern
|
|
||||||
const uint8_t _asciimap[128] PROGMEM;
|
|
||||||
|
|
||||||
#define SHIFT 0x80
|
|
||||||
const uint8_t _asciimap[128] =
|
|
||||||
{
|
|
||||||
0x00, // NUL
|
|
||||||
0x00, // SOH
|
|
||||||
0x00, // STX
|
|
||||||
0x00, // ETX
|
|
||||||
0x00, // EOT
|
|
||||||
0x00, // ENQ
|
|
||||||
0x00, // ACK
|
|
||||||
0x00, // BEL
|
|
||||||
0x2a, // BS Backspace
|
|
||||||
0x2b, // TAB Tab
|
|
||||||
0x28, // LF Enter
|
|
||||||
0x00, // VT
|
|
||||||
0x00, // FF
|
|
||||||
0x00, // CR
|
|
||||||
0x00, // SO
|
|
||||||
0x00, // SI
|
|
||||||
0x00, // DEL
|
|
||||||
0x00, // DC1
|
|
||||||
0x00, // DC2
|
|
||||||
0x00, // DC3
|
|
||||||
0x00, // DC4
|
|
||||||
0x00, // NAK
|
|
||||||
0x00, // SYN
|
|
||||||
0x00, // ETB
|
|
||||||
0x00, // CAN
|
|
||||||
0x00, // EM
|
|
||||||
0x00, // SUB
|
|
||||||
0x00, // ESC
|
|
||||||
0x00, // FS
|
|
||||||
0x00, // GS
|
|
||||||
0x00, // RS
|
|
||||||
0x00, // US
|
|
||||||
|
|
||||||
0x2c, // ' '
|
|
||||||
0x1e|SHIFT, // !
|
|
||||||
0x34|SHIFT, // "
|
|
||||||
0x20|SHIFT, // #
|
|
||||||
0x21|SHIFT, // $
|
|
||||||
0x22|SHIFT, // %
|
|
||||||
0x24|SHIFT, // &
|
|
||||||
0x34, // '
|
|
||||||
0x26|SHIFT, // (
|
|
||||||
0x27|SHIFT, // )
|
|
||||||
0x25|SHIFT, // *
|
|
||||||
0x2e|SHIFT, // +
|
|
||||||
0x36, // ,
|
|
||||||
0x2d, // -
|
|
||||||
0x37, // .
|
|
||||||
0x38, // /
|
|
||||||
0x27, // 0
|
|
||||||
0x1e, // 1
|
|
||||||
0x1f, // 2
|
|
||||||
0x20, // 3
|
|
||||||
0x21, // 4
|
|
||||||
0x22, // 5
|
|
||||||
0x23, // 6
|
|
||||||
0x24, // 7
|
|
||||||
0x25, // 8
|
|
||||||
0x26, // 9
|
|
||||||
0x33|SHIFT, // :
|
|
||||||
0x33, // ;
|
|
||||||
0x36|SHIFT, // <
|
|
||||||
0x2e, // =
|
|
||||||
0x37|SHIFT, // >
|
|
||||||
0x38|SHIFT, // ?
|
|
||||||
0x1f|SHIFT, // @
|
|
||||||
0x04|SHIFT, // A
|
|
||||||
0x05|SHIFT, // B
|
|
||||||
0x06|SHIFT, // C
|
|
||||||
0x07|SHIFT, // D
|
|
||||||
0x08|SHIFT, // E
|
|
||||||
0x09|SHIFT, // F
|
|
||||||
0x0a|SHIFT, // G
|
|
||||||
0x0b|SHIFT, // H
|
|
||||||
0x0c|SHIFT, // I
|
|
||||||
0x0d|SHIFT, // J
|
|
||||||
0x0e|SHIFT, // K
|
|
||||||
0x0f|SHIFT, // L
|
|
||||||
0x10|SHIFT, // M
|
|
||||||
0x11|SHIFT, // N
|
|
||||||
0x12|SHIFT, // O
|
|
||||||
0x13|SHIFT, // P
|
|
||||||
0x14|SHIFT, // Q
|
|
||||||
0x15|SHIFT, // R
|
|
||||||
0x16|SHIFT, // S
|
|
||||||
0x17|SHIFT, // T
|
|
||||||
0x18|SHIFT, // U
|
|
||||||
0x19|SHIFT, // V
|
|
||||||
0x1a|SHIFT, // W
|
|
||||||
0x1b|SHIFT, // X
|
|
||||||
0x1c|SHIFT, // Y
|
|
||||||
0x1d|SHIFT, // Z
|
|
||||||
0x2f, // [
|
|
||||||
0x31, // bslash
|
|
||||||
0x30, // ]
|
|
||||||
0x23|SHIFT, // ^
|
|
||||||
0x2d|SHIFT, // _
|
|
||||||
0x35, // `
|
|
||||||
0x04, // a
|
|
||||||
0x05, // b
|
|
||||||
0x06, // c
|
|
||||||
0x07, // d
|
|
||||||
0x08, // e
|
|
||||||
0x09, // f
|
|
||||||
0x0a, // g
|
|
||||||
0x0b, // h
|
|
||||||
0x0c, // i
|
|
||||||
0x0d, // j
|
|
||||||
0x0e, // k
|
|
||||||
0x0f, // l
|
|
||||||
0x10, // m
|
|
||||||
0x11, // n
|
|
||||||
0x12, // o
|
|
||||||
0x13, // p
|
|
||||||
0x14, // q
|
|
||||||
0x15, // r
|
|
||||||
0x16, // s
|
|
||||||
0x17, // t
|
|
||||||
0x18, // u
|
|
||||||
0x19, // v
|
|
||||||
0x1a, // w
|
|
||||||
0x1b, // x
|
|
||||||
0x1c, // y
|
|
||||||
0x1d, // z
|
|
||||||
0x2f|SHIFT, //
|
|
||||||
0x31|SHIFT, // |
|
|
||||||
0x30|SHIFT, // }
|
|
||||||
0x35|SHIFT, // ~
|
|
||||||
0 // DEL
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t USBPutChar(uint8_t c);
|
|
||||||
|
|
||||||
|
|
||||||
// pressKeycode() adds the specified key (printing, non-printing, or modifier)
|
|
||||||
// to the persistent key report and sends the report. Because of the way
|
|
||||||
// USB HID works, the host acts like the key remains pressed until we
|
|
||||||
// call releaseKeycode(), releaseAll(), or otherwise clear the report and resend.
|
|
||||||
size_t Keyboard_::pressKeycode(uint8_t k)
|
|
||||||
{
|
|
||||||
if (!addKeycodeToReport(k)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Keyboard_::addKeycodeToReport(uint8_t k)
|
|
||||||
{
|
|
||||||
uint8_t index = 0;
|
|
||||||
uint8_t done = 0;
|
|
||||||
|
|
||||||
if ((k >= HID_KEYBOARD_LEFT_CONTROL) && (k <= HID_KEYBOARD_RIGHT_GUI)) {
|
|
||||||
// it's a modifier key
|
|
||||||
_keyReport.modifiers |= (0x01 << (k - HID_KEYBOARD_LEFT_CONTROL));
|
|
||||||
} else {
|
|
||||||
// it's some other key:
|
|
||||||
// Add k to the key report only if it's not already present
|
|
||||||
// and if there is an empty slot.
|
|
||||||
for (index = 0; index < KEYREPORT_KEYCOUNT; index++) {
|
|
||||||
if (_keyReport.keys[index] != k) { // is k already in list?
|
|
||||||
if (0 == _keyReport.keys[index]) { // have we found an empty slot?
|
|
||||||
_keyReport.keys[index] = k;
|
|
||||||
done = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
done = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// use separate variable to check if slot was found
|
|
||||||
// for style reasons - we do not know how the compiler
|
|
||||||
// handles the for() index when it leaves the loop
|
|
||||||
if (0 == done) {
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// press() transforms the given key to the actual keycode and calls
|
|
||||||
// pressKeycode() to actually press this key.
|
|
||||||
//
|
|
||||||
size_t Keyboard_::press(uint8_t k)
|
|
||||||
{
|
|
||||||
if (k >= KEY_RIGHT_GUI + 1) {
|
|
||||||
// it's a non-printing key (not a modifier)
|
|
||||||
k = k - (KEY_RIGHT_GUI + 1);
|
|
||||||
} else {
|
|
||||||
if (k >= KEY_LEFT_CTRL) {
|
|
||||||
// it's a modifier key
|
|
||||||
k = k - KEY_LEFT_CTRL + HID_KEYBOARD_LEFT_CONTROL;
|
|
||||||
} else {
|
|
||||||
k = pgm_read_byte(_asciimap + k);
|
|
||||||
if (k) {
|
|
||||||
if (k & SHIFT) {
|
|
||||||
// it's a capital letter or other character reached with shift
|
|
||||||
// the left shift modifier
|
|
||||||
addKeycodeToReport(HID_KEYBOARD_LEFT_SHIFT);
|
|
||||||
k = k ^ SHIFT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pressKeycode(k);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// System Control
|
|
||||||
// k is one of the SYSTEM_CONTROL defines which come from the HID usage table "Generic Desktop Page (0x01)"
|
|
||||||
// in "HID Usage Tables" (HUT1_12v2.pdf)
|
|
||||||
size_t Keyboard_::systemControl(uint8_t k)
|
|
||||||
{
|
|
||||||
if(k <= 16)
|
|
||||||
{
|
|
||||||
u16 mask = 0;
|
|
||||||
u8 m[2];
|
|
||||||
|
|
||||||
if(k > 0)
|
|
||||||
{
|
|
||||||
mask = 1 << (k - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
m[0] = LSB(mask);
|
|
||||||
m[1] = MSB(mask);
|
|
||||||
HID_SendReport(HID_REPORTID_SYSTEMCONTROL,m,sizeof(m));
|
|
||||||
|
|
||||||
// these are all OSCs, so send a clear to make it possible to send it again later
|
|
||||||
m[0] = 0;
|
|
||||||
m[1] = 0;
|
|
||||||
HID_SendReport(HID_REPORTID_SYSTEMCONTROL,m,sizeof(m));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consumer Control
|
|
||||||
// k is one of the CONSUMER_CONTROL defines which come from the HID usage table "Consumer Devices Page (0x0c)"
|
|
||||||
// in "HID Usage Tables" (HUT1_12v2.pdf)
|
|
||||||
size_t Keyboard_::consumerControl(uint8_t k)
|
|
||||||
{
|
|
||||||
if(k <= 8)
|
|
||||||
{
|
|
||||||
u16 mask = 0;
|
|
||||||
u8 m[2];
|
|
||||||
|
|
||||||
if(k > 0)
|
|
||||||
{
|
|
||||||
mask = 1 << (k - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
m[0] = LSB(mask);
|
|
||||||
m[1] = MSB(mask);
|
|
||||||
HID_SendReport(HID_REPORTID_CONSUMERCONTROL,m,sizeof(m));
|
|
||||||
|
|
||||||
// these are all OSCs, so send a clear to make it possible to send it again later
|
|
||||||
m[0] = 0;
|
|
||||||
m[1] = 0;
|
|
||||||
HID_SendReport(HID_REPORTID_CONSUMERCONTROL,m,sizeof(m));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// releaseKeycode() takes the specified key out of the persistent key report and
|
|
||||||
// sends the report. This tells the OS the key is no longer pressed and that
|
|
||||||
// it shouldn't be repeated any more.
|
|
||||||
// When send is set to FALSE (= 0) no sendReport() is executed. This comes in
|
|
||||||
// handy when combining key releases (e.g. SHIFT+A).
|
|
||||||
size_t Keyboard_::releaseKeycode(uint8_t k)
|
|
||||||
{
|
|
||||||
if (!removeKeycodeFromReport(k)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Keyboard_::removeKeycodeFromReport(uint8_t k)
|
|
||||||
{
|
|
||||||
uint8_t indexA;
|
|
||||||
uint8_t indexB;
|
|
||||||
uint8_t count;
|
|
||||||
|
|
||||||
if ((k >= HID_KEYBOARD_LEFT_CONTROL) && (k <= HID_KEYBOARD_RIGHT_GUI)) {
|
|
||||||
// it's a modifier key
|
|
||||||
_keyReport.modifiers = _keyReport.modifiers & (~(0x01 << (k - HID_KEYBOARD_LEFT_CONTROL)));
|
|
||||||
} else {
|
|
||||||
// it's some other key:
|
|
||||||
// Test the key report to see if k is present. Clear it if it exists.
|
|
||||||
// Check all positions in case the key is present more than once (which it shouldn't be)
|
|
||||||
for (indexA = 0; indexA < KEYREPORT_KEYCOUNT; indexA++) {
|
|
||||||
if (_keyReport.keys[indexA] == k) {
|
|
||||||
_keyReport.keys[indexA] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// finally rearrange the keys list so that the free (= 0x00) are at the
|
|
||||||
// end of the keys list - some implementations stop for keys at the
|
|
||||||
// first occurence of an 0x00 in the keys list
|
|
||||||
// so (0x00)(0x01)(0x00)(0x03)(0x02)(0x00) becomes
|
|
||||||
// (0x01)(0x03)(0x02)(0x00)(0x00)(0x00)
|
|
||||||
count = 0; // holds the number of zeros we've found
|
|
||||||
indexA = 0;
|
|
||||||
while ((indexA + count) < KEYREPORT_KEYCOUNT) {
|
|
||||||
if (0 == _keyReport.keys[indexA]) {
|
|
||||||
count++; // one more zero
|
|
||||||
for (indexB = indexA; indexB < KEYREPORT_KEYCOUNT-count; indexB++) {
|
|
||||||
_keyReport.keys[indexB] = _keyReport.keys[indexB+1];
|
|
||||||
}
|
|
||||||
_keyReport.keys[KEYREPORT_KEYCOUNT-count] = 0;
|
|
||||||
} else {
|
|
||||||
indexA++; // one more non-zero
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// release() transforms the given key to the actual keycode and calls
|
|
||||||
// releaseKeycode() to actually release this key.
|
|
||||||
//
|
|
||||||
size_t Keyboard_::release(uint8_t k)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
if (k >= KEY_RIGHT_GUI + 1) {
|
|
||||||
// it's a non-printing key (not a modifier)
|
|
||||||
k = k - (KEY_RIGHT_GUI + 1);
|
|
||||||
} else {
|
|
||||||
if (k >= KEY_LEFT_CTRL) {
|
|
||||||
// it's a modifier key
|
|
||||||
k = k - KEY_LEFT_CTRL + HID_KEYBOARD_LEFT_CONTROL;
|
|
||||||
} else {
|
|
||||||
k = pgm_read_byte(_asciimap + k);
|
|
||||||
if (k) {
|
|
||||||
if ((k & SHIFT)) {
|
|
||||||
// it's a capital letter or other character reached with shift
|
|
||||||
// the left shift modifier
|
|
||||||
removeKeycodeFromReport(HID_KEYBOARD_LEFT_SHIFT);
|
|
||||||
k = k ^ SHIFT;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
releaseKeycode(k);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Keyboard_::releaseAll(void)
|
|
||||||
{
|
|
||||||
memset(&_keyReport, 0x00, sizeof(_keyReport));
|
|
||||||
sendCurrentReport();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Keyboard_::sendCurrentReport(void) {
|
|
||||||
sendReport(&_keyReport);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Keyboard_::write(uint8_t c)
|
|
||||||
{
|
|
||||||
uint8_t p = press(c); // Keydown
|
|
||||||
release(c); // Keyup
|
|
||||||
return p; // just return the result of press() since release() almost always returns 1
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Keyboard_::writeKeycode(uint8_t c)
|
|
||||||
{
|
|
||||||
uint8_t p = pressKeycode(c); // Keydown
|
|
||||||
releaseKeycode(c); // Keyup
|
|
||||||
return (p); // just return the result of pressKeycode() since release() almost always returns 1
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* if defined(USBCON) */
|
|
@ -1,677 +0,0 @@
|
|||||||
#ifndef __HIDTables__
|
|
||||||
#define HID___HIDTables__
|
|
||||||
|
|
||||||
|
|
||||||
// These mappings were extracted and transcribed from
|
|
||||||
// http://www.usb.org_SLASH_developers_SLASH_devclass_docs_SLASH_Hut1_12v2.pdf
|
|
||||||
//
|
|
||||||
// In most cases, I've preserved the "official" USB Implementers forum
|
|
||||||
// "Usage Name", though I've standardized some abbreviations and spacing
|
|
||||||
// that were inconsistent in the original specification. Non alpha-numeric
|
|
||||||
// characters in symbol names were converted into those characters' names.
|
|
||||||
//
|
|
||||||
// To match Arduino code style, all hid usage names are fully upper case.
|
|
||||||
//
|
|
||||||
// Not every HID usage listed in this file is currently supported by Arduino
|
|
||||||
// In particular, any System Control or Consumer Control entry that doesn't
|
|
||||||
// have a comment indicating that it's "HID type OSC" will require additional
|
|
||||||
// code in the Arduino core to work.
|
|
||||||
//
|
|
||||||
// Non-working usages are listed here in the interest of not having to manually
|
|
||||||
// convert more usage names each and every time our HID stack gets a little bit
|
|
||||||
// better.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// -- Jesse Vincent <jesse@keyboard.io>, January 2014
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// System control mappings
|
|
||||||
|
|
||||||
#define HID_SYSTEM_POWER_DOWN 0x81 // HID type OSC
|
|
||||||
#define HID_SYSTEM_SLEEP 0x82 // HID type OSC
|
|
||||||
#define HID_SYSTEM_WAKE_UP 0x83 // HID type OSC
|
|
||||||
#define HID_SYSTEM_CONTEXT_MENU 0x84 // HID type OSC
|
|
||||||
#define HID_SYSTEM_MAIN_MENU 0x85 // HID type OSC
|
|
||||||
#define HID_SYSTEM_APP_MENU 0x86 // HID type OSC
|
|
||||||
#define HID_SYSTEM_MENU_HELP 0x87 // HID type OSC
|
|
||||||
#define HID_SYSTEM_MENU_EXIT 0x88 // HID type OSC
|
|
||||||
#define HID_SYSTEM_MENU_SELECT 0x89 // HID type OSC
|
|
||||||
#define HID_SYSTEM_MENU_RIGHT 0x8A // HID type RTC
|
|
||||||
#define HID_SYSTEM_MENU_LEFT 0x8B // HID type RTC
|
|
||||||
#define HID_SYSTEM_MENU_UP 0x8C // HID type RTC
|
|
||||||
#define HID_SYSTEM_MENU_DOWN 0x8D // HID type RTC
|
|
||||||
#define HID_SYSTEM_COLD_RESTART 0x8E // HID type OSC
|
|
||||||
#define HID_SYSTEM_WARM_RESTART 0x8F // HID type OSC
|
|
||||||
#define HID_D_PAD_UP 0x90 // HID type OOC
|
|
||||||
#define HID_D_PAD_DOWN 0x91 // HID type OOC
|
|
||||||
#define HID_D_PAD_RIGHT 0x92 // HID type OOC
|
|
||||||
#define HID_D_PAD_LEFT 0x93 // HID type OOC
|
|
||||||
// 0x94-0x9F are reserved
|
|
||||||
#define HID_SYSTEM_DOCK 0xA0 // HID type OSC
|
|
||||||
#define HID_SYSTEM_UNDOCK 0xA1 // HID type OSC
|
|
||||||
#define HID_SYSTEM_SETUP 0xA2 // HID type OSC
|
|
||||||
#define HID_SYSTEM_BREAK 0xA3 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DEBUGGER_BREAK 0xA4 // HID type OSC
|
|
||||||
#define HID_APPLICATION_BREAK 0xA5 // HID type OSC
|
|
||||||
#define HID_APPLICATION_DEBUGGER_BREAK 0xA6 // HID type OSC
|
|
||||||
#define HID_SYSTEM_SPEAKER_MUTE 0xA7 // HID type OSC
|
|
||||||
#define HID_SYSTEM_HIBERNATE 0xA8 // HID type OSC
|
|
||||||
// 0xA9-0xAF are reserved
|
|
||||||
#define HID_SYSTEM_DISPLAY_INVERT 0xB0 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_INTERNAL 0xB1 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_EXTERNAL 0xB2 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_BOTH 0xB3 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_DUAL 0xB4 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_TOGGLE_INT_SLASH_EXT 0xB5 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_SWAP_PRIMARY_SLASH_SECONDARY 0xB6 // HID type OSC
|
|
||||||
#define HID_SYSTEM_DISPLAY_LCD_AUTOSCALE 0xB7 // HID type OSC
|
|
||||||
|
|
||||||
// Keyboard HID mappings
|
|
||||||
|
|
||||||
// Reserved (no_event_indicated) 0x00
|
|
||||||
#define HID_KEYBOARD_ERROR_ROLLOVER 0x01
|
|
||||||
#define HID_KEYBOARD_POST_FAIL 0x02
|
|
||||||
#define HID_KEYBOARD_ERROR_UNDEFINED 0x03
|
|
||||||
#define HID_KEYBOARD_A_AND_A 0x04
|
|
||||||
#define HID_KEYBOARD_B_AND_B 0x05
|
|
||||||
#define HID_KEYBOARD_C_AND_C 0x06
|
|
||||||
#define HID_KEYBOARD_D_AND_D 0x07
|
|
||||||
#define HID_KEYBOARD_E_AND_E 0x08
|
|
||||||
#define HID_KEYBOARD_F_AND_F 0x09
|
|
||||||
#define HID_KEYBOARD_G_AND_G 0x0A
|
|
||||||
#define HID_KEYBOARD_H_AND_H 0x0B
|
|
||||||
#define HID_KEYBOARD_I_AND_I 0x0C
|
|
||||||
#define HID_KEYBOARD_J_AND_J 0x0D
|
|
||||||
#define HID_KEYBOARD_K_AND_K 0x0E
|
|
||||||
#define HID_KEYBOARD_L_AND_L 0x0F
|
|
||||||
#define HID_KEYBOARD_M_AND_M 0x10
|
|
||||||
#define HID_KEYBOARD_N_AND_N 0x11
|
|
||||||
#define HID_KEYBOARD_O_AND_O 0x12
|
|
||||||
#define HID_KEYBOARD_P_AND_P 0x13
|
|
||||||
#define HID_KEYBOARD_Q_AND_Q 0x14
|
|
||||||
#define HID_KEYBOARD_R_AND_R 0x15
|
|
||||||
#define HID_KEYBOARD_S_AND_S 0x16
|
|
||||||
#define HID_KEYBOARD_T_AND_T 0x17
|
|
||||||
#define HID_KEYBOARD_U_AND_U 0x18
|
|
||||||
#define HID_KEYBOARD_V_AND_V 0x19
|
|
||||||
#define HID_KEYBOARD_W_AND_W 0x1A
|
|
||||||
#define HID_KEYBOARD_X_AND_X 0x1B
|
|
||||||
#define HID_KEYBOARD_Y_AND_Y 0x1C
|
|
||||||
#define HID_KEYBOARD_Z_AND_Z 0x1D
|
|
||||||
#define HID_KEYBOARD_1_AND_EXCLAMATION_POINT 0x1E
|
|
||||||
#define HID_KEYBOARD_2_AND_AT 0x1F
|
|
||||||
#define HID_KEYBOARD_3_AND_POUND 0x20
|
|
||||||
#define HID_KEYBOARD_4_AND_DOLLAR 0x21
|
|
||||||
#define HID_KEYBOARD_5_AND_PERCENT 0x22
|
|
||||||
#define HID_KEYBOARD_6_AND_CARAT 0x23
|
|
||||||
#define HID_KEYBOARD_7_AND_AMPERSAND 0x24
|
|
||||||
#define HID_KEYBOARD_8_AND_ASTERISK 0x25
|
|
||||||
#define HID_KEYBOARD_9_AND_LEFT_PAREN 0x26
|
|
||||||
#define HID_KEYBOARD_0_AND_RIGHT_PAREN 0x27
|
|
||||||
#define HID_KEYBOARD_ENTER 0x28 // (MARKED AS ENTER_SLASH_RETURN)
|
|
||||||
#define HID_KEYBOARD_ESCAPE 0x29
|
|
||||||
#define HID_KEYBOARD_DELETE 0x2A // (BACKSPACE)
|
|
||||||
#define HID_KEYBOARD_TAB 0x2B
|
|
||||||
#define HID_KEYBOARD_SPACEBAR 0x2C
|
|
||||||
#define HID_KEYBOARD_MINUS_AND_UNDERSCORE 0x2D // (UNDERSCORE)
|
|
||||||
#define HID_KEYBOARD_EQUALS_AND_PLUS 0x2E
|
|
||||||
#define HID_KEYBOARD_LEFT_BRACKET_AND_LEFT_CURLY_BRACE 0x2F
|
|
||||||
#define HID_KEYBOARD_RIGHT_BRACKET_AND_RIGHT_CURLY_BRACE 0x30
|
|
||||||
#define HID_KEYBOARD_BACKSLASH_AND_PIPE 0x31
|
|
||||||
#define HID_KEYBOARD_NON_US_POUND_AND_TILDE 0x32
|
|
||||||
#define HID_KEYBOARD_SEMICOLON_AND_COLON 0x33
|
|
||||||
#define HID_KEYBOARD_QUOTE_AND_DOUBLEQUOTE 0x34
|
|
||||||
#define HID_KEYBOARD_GRAVE_ACCENT_AND_TILDE 0x35
|
|
||||||
#define HID_KEYBOARD_COMMA_AND_LESS_THAN 0x36
|
|
||||||
#define HID_KEYBOARD_PERIOD_AND_GREATER_THAN 0x37
|
|
||||||
#define HID_KEYBOARD_SLASH_AND_QUESTION_MARK 0x38
|
|
||||||
#define HID_KEYBOARD_CAPS_LOCK 0x39
|
|
||||||
#define HID_KEYBOARD_F1 0x3A
|
|
||||||
#define HID_KEYBOARD_F2 0x3B
|
|
||||||
#define HID_KEYBOARD_F3 0x3C
|
|
||||||
#define HID_KEYBOARD_F4 0x3D
|
|
||||||
#define HID_KEYBOARD_F5 0x3E
|
|
||||||
#define HID_KEYBOARD_F6 0x3F
|
|
||||||
#define HID_KEYBOARD_F7 0x40
|
|
||||||
#define HID_KEYBOARD_F8 0x41
|
|
||||||
#define HID_KEYBOARD_F9 0x42
|
|
||||||
#define HID_KEYBOARD_F10 0x43
|
|
||||||
#define HID_KEYBOARD_F11 0x44
|
|
||||||
#define HID_KEYBOARD_F12 0x45
|
|
||||||
#define HID_KEYBOARD_PRINTSCREEN 0x46
|
|
||||||
#define HID_KEYBOARD_SCROLL_LOCK 0x47
|
|
||||||
#define HID_KEYBOARD_PAUSE 0x48
|
|
||||||
#define HID_KEYBOARD_INSERT 0x49
|
|
||||||
#define HID_KEYBOARD_HOME 0x4A
|
|
||||||
#define HID_KEYBOARD_PAGE_UP 0x4B
|
|
||||||
#define HID_KEYBOARD_DELETE_FORWARD 0x4C
|
|
||||||
#define HID_KEYBOARD_END 0x4D
|
|
||||||
#define HID_KEYBOARD_PAGE_DOWN 0x4E
|
|
||||||
#define HID_KEYBOARD_RIGHTARROW 0x4F
|
|
||||||
#define HID_KEYBOARD_LEFTARROW 0x50
|
|
||||||
#define HID_KEYBOARD_DOWNARROW 0x51
|
|
||||||
#define HID_KEYBOARD_UPARROW 0x52
|
|
||||||
#define HID_KEYPAD_NUM_LOCK_AND_CLEAR 0x53
|
|
||||||
#define HID_KEYPAD_DIVIDE 0x54
|
|
||||||
#define HID_KEYPAD_MULTIPLY 0x55
|
|
||||||
#define HID_KEYPAD_SUBTRACT 0x56
|
|
||||||
#define HID_KEYPAD_ADD 0x57
|
|
||||||
#define HID_KEYPAD_ENTER 0x58
|
|
||||||
#define HID_KEYPAD_1_AND_END 0x59
|
|
||||||
#define HID_KEYPAD_2_AND_DOWN_ARROW 0x5A
|
|
||||||
#define HID_KEYPAD_3_AND_PAGE_DOWN 0x5B
|
|
||||||
#define HID_KEYPAD_4_AND_LEFT_ARROW 0x5C
|
|
||||||
#define HID_KEYPAD_5 0x5D
|
|
||||||
#define HID_KEYPAD_6_AND_RIGHT_ARROW 0x5E
|
|
||||||
#define HID_KEYPAD_7_AND_HOME 0x5F
|
|
||||||
#define HID_KEYPAD_8_AND_UP_ARROW 0x60
|
|
||||||
#define HID_KEYPAD_9_AND_PAGE_UP 0x61
|
|
||||||
#define HID_KEYPAD_0_AND_INSERT 0x62
|
|
||||||
#define HID_KEYPAD_PERIOD_AND_DELETE 0x63
|
|
||||||
#define HID_KEYBOARD_NON_US_BACKSLASH_AND_PIPE 0x64
|
|
||||||
#define HID_KEYBOARD_APPLICATION 0x65
|
|
||||||
#define HID_KEYBOARD_POWER 0x66
|
|
||||||
#define HID_KEYPAD_EQUALS 0x67
|
|
||||||
#define HID_KEYBOARD_F13 0x68
|
|
||||||
#define HID_KEYBOARD_F14 0x69
|
|
||||||
#define HID_KEYBOARD_F15 0x6A
|
|
||||||
#define HID_KEYBOARD_F16 0x6B
|
|
||||||
#define HID_KEYBOARD_F17 0x6C
|
|
||||||
#define HID_KEYBOARD_F18 0x6D
|
|
||||||
#define HID_KEYBOARD_F19 0x6E
|
|
||||||
#define HID_KEYBOARD_F20 0x6F
|
|
||||||
#define HID_KEYBOARD_F21 0x70
|
|
||||||
#define HID_KEYBOARD_F22 0x71
|
|
||||||
#define HID_KEYBOARD_F23 0x72
|
|
||||||
#define HID_KEYBOARD_F24 0x73
|
|
||||||
#define HID_KEYBOARD_EXECUTE 0x74
|
|
||||||
#define HID_KEYBOARD_HELP 0x75
|
|
||||||
#define HID_KEYBOARD_MENU 0x76
|
|
||||||
#define HID_KEYBOARD_SELECT 0x77
|
|
||||||
#define HID_KEYBOARD_STOP 0x78
|
|
||||||
#define HID_KEYBOARD_AGAIN 0x79
|
|
||||||
#define HID_KEYBOARD_UNDO 0x7A
|
|
||||||
#define HID_KEYBOARD_CUT 0x7B
|
|
||||||
#define HID_KEYBOARD_COPY 0x7C
|
|
||||||
#define HID_KEYBOARD_PASTE 0x7D
|
|
||||||
#define HID_KEYBOARD_FIND 0x7E
|
|
||||||
#define HID_KEYBOARD_MUTE 0x7F
|
|
||||||
#define HID_KEYBOARD_VOLUME_UP 0x80
|
|
||||||
#define HID_KEYBOARD_VOLUME_DOWN 0x81
|
|
||||||
#define HID_KEYBOARD_LOCKING_CAPS_LOCK 0x82
|
|
||||||
#define HID_KEYBOARD_LOCKING_NUM_LOCK 0x83
|
|
||||||
#define HID_KEYBOARD_LOCKING_SCROLL_LOCK 0x84
|
|
||||||
#define HID_KEYPAD_COMMA 0x85
|
|
||||||
#define HID_KEYPAD_EQUAL_SIGN 0x86
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL1 0x87
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL2 0x88
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL3 0x89
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL4 0x8A
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL5 0x8B
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL6 0x8C
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL7 0x8D
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL8 0x8E
|
|
||||||
#define HID_KEYBOARD_INTERNATIONAL9 0x8F
|
|
||||||
#define HID_KEYBOARD_LANG1 0x90
|
|
||||||
#define HID_KEYBOARD_LANG2 0x91
|
|
||||||
#define HID_KEYBOARD_LANG3 0x92
|
|
||||||
#define HID_KEYBOARD_LANG4 0x93
|
|
||||||
#define HID_KEYBOARD_LANG5 0x94
|
|
||||||
#define HID_KEYBOARD_LANG6 0x95
|
|
||||||
#define HID_KEYBOARD_LANG7 0x96
|
|
||||||
#define HID_KEYBOARD_LANG8 0x97
|
|
||||||
#define HID_KEYBOARD_LANG9 0x98
|
|
||||||
#define HID_KEYBOARD_ALTERNATE_ERASE 0x99
|
|
||||||
#define HID_KEYBOARD_SYSREQ_SLASH_ATTENTION 0x9A
|
|
||||||
#define HID_KEYBOARD_CANCEL 0x9B
|
|
||||||
#define HID_KEYBOARD_CLEAR 0x9C
|
|
||||||
#define HID_KEYBOARD_PRIOR 0x9D
|
|
||||||
#define HID_KEYBOARD_RETURN 0x9E
|
|
||||||
#define HID_KEYBOARD_SEPARATOR 0x9F
|
|
||||||
#define HID_KEYBOARD_OUT 0xA0
|
|
||||||
#define HID_KEYBOARD_OPER 0xA1
|
|
||||||
#define HID_KEYBOARD_CLEAR_SLASH_AGAIN 0xA2
|
|
||||||
#define HID_KEYBOARD_CRSEL_SLASH_PROPS 0xA3
|
|
||||||
#define HID_KEYBOARD_EXSEL 0xA4
|
|
||||||
// Reserved 0xA5-AF
|
|
||||||
#define HID_KEYPAD_00 0xB0
|
|
||||||
#define HID_KEYPAD_000 0xB1
|
|
||||||
#define HID_THOUSANDS_SEPARATOR 0xB2
|
|
||||||
#define HID_DECIMAL_SEPARATOR 0xB3
|
|
||||||
#define HID_CURRENCY_UNIT 0xB4
|
|
||||||
#define HID_CURRENCY_SUBUNIT 0xB5
|
|
||||||
#define HID_KEYPAD_LEFT_PAREN 0xB6
|
|
||||||
#define HID_KEYPAD_RIGHT_PAREN 0xB7
|
|
||||||
#define HID_KEYPAD_LEFT_CURLY_BRACE 0xB8
|
|
||||||
#define HID_KEYPAD_RIGHT_CURLY_BRACE 0xB9
|
|
||||||
#define HID_KEYPAD_TAB 0xBA
|
|
||||||
#define HID_KEYPAD_BACKSPACE 0xBB
|
|
||||||
#define HID_KEYPAD_A 0xBC
|
|
||||||
#define HID_KEYPAD_B 0xBD
|
|
||||||
#define HID_KEYPAD_C 0xBE
|
|
||||||
#define HID_KEYPAD_D 0xBF
|
|
||||||
#define HID_KEYPAD_E 0xC0
|
|
||||||
#define HID_KEYPAD_F 0xC1
|
|
||||||
#define HID_KEYPAD_XOR 0xC2
|
|
||||||
#define HID_KEYPAD_CARAT 0xC3
|
|
||||||
#define HID_KEYPAD_PERCENT 0xC4
|
|
||||||
#define HID_KEYPAD_LESS_THAN 0xC5
|
|
||||||
#define HID_KEYPAD_GREATER_THAN 0xC6
|
|
||||||
#define HID_KEYPAD_AMPERSAND 0xC7
|
|
||||||
#define HID_KEYPAD_DOUBLEAMPERSAND 0xC8
|
|
||||||
#define HID_KEYPAD_PIPE 0xC9
|
|
||||||
#define HID_KEYPAD_DOUBLEPIPE 0xCA
|
|
||||||
#define HID_KEYPAD_COLON 0xCB
|
|
||||||
#define HID_KEYPAD_POUND_SIGN 0xCC
|
|
||||||
#define HID_KEYPAD_SPACE 0xCD
|
|
||||||
#define HID_KEYPAD_AT_SIGN 0xCE
|
|
||||||
#define HID_KEYPAD_EXCLAMATION_POINT 0xCF
|
|
||||||
#define HID_KEYPAD_MEMORY_STORE 0xD0
|
|
||||||
#define HID_KEYPAD_MEMORY_RECALL 0xD1
|
|
||||||
#define HID_KEYPAD_MEMORY_CLEAR 0xD2
|
|
||||||
#define HID_KEYPAD_MEMORY_ADD 0xD3
|
|
||||||
#define HID_KEYPAD_MEMORY_SUBTRACT 0xD4
|
|
||||||
#define HID_KEYPAD_MEMORY_MULTIPLY 0xD5
|
|
||||||
#define HID_KEYPAD_MEMORY_DIVIDE 0xD6
|
|
||||||
#define HID_KEYPAD_PLUS_SLASH_MINUS 0xD7
|
|
||||||
#define HID_KEYPAD_CLEAR 0xD8
|
|
||||||
#define HID_KEYPAD_CLEAR_ENTRY 0xD9
|
|
||||||
#define HID_KEYPAD_BINARY 0xDA
|
|
||||||
#define HID_KEYPAD_OCTAL 0xDB
|
|
||||||
#define HID_KEYPAD_DECIMAL 0xDC
|
|
||||||
#define HID_KEYPAD_HEXADECIMAL 0xDD
|
|
||||||
|
|
||||||
// 0xDE-0xDF - RESERVED
|
|
||||||
#define HID_KEYBOARD_LEFT_CONTROL 0xE0
|
|
||||||
#define HID_KEYBOARD_LEFT_SHIFT 0xE1
|
|
||||||
#define HID_KEYBOARD_LEFT_ALT 0xE2
|
|
||||||
#define HID_KEYBOARD_LEFT_GUI 0xE3
|
|
||||||
#define HID_KEYBOARD_RIGHT_CONTROL 0xE4
|
|
||||||
#define HID_KEYBOARD_RIGHT_SHIFT 0xE5
|
|
||||||
#define HID_KEYBOARD_RIGHT_ALT 0xE6
|
|
||||||
#define HID_KEYBOARD_RIGHT_GUI 0xE7
|
|
||||||
|
|
||||||
|
|
||||||
// Consumer_Page_(0x0C) 0x15
|
|
||||||
#define HID_CONSUMER_NUMERIC_KEY_PAD 0x02 // HID type NARY
|
|
||||||
#define HID_CONSUMER_PROGRAMMABLE_BUTTONS 0x03 // HID type NARY
|
|
||||||
#define HID_CONSUMER_MICROPHONE_CA 0x04
|
|
||||||
#define HID_CONSUMER_HEADPHONE_CA 0x05
|
|
||||||
#define HID_CONSUMER_GRAPHIC_EQUALIZER_CA 0x06
|
|
||||||
// Reserved 0x07-1F
|
|
||||||
#define HID_CONSUMER_PLUS_10 0x20 // HID type OSC
|
|
||||||
#define HID_CONSUMER_PLUS_100 0x21 // HID type OSC
|
|
||||||
#define HID_CONSUMER_AM_SLASH_PM 0x22 // HID type OSC
|
|
||||||
// Reserved 0x23-3F
|
|
||||||
#define HID_CONSUMER_POWER 0x30 // HID type OOC
|
|
||||||
#define HID_CONSUMER_RESET 0x31 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SLEEP 0x32 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SLEEP_AFTER 0x33 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SLEEP_MODE 0x34 // HID type RTC
|
|
||||||
#define HID_CONSUMER_ILLUMINATION 0x35 // HID type OOC
|
|
||||||
#define HID_CONSUMER_FUNCTION_BUTTONS 0x36 // HID type NARY
|
|
||||||
// Reserved 0x37-3F
|
|
||||||
#define HID_CONSUMER_MENU 0x40 // HID type OOC
|
|
||||||
#define HID_CONSUMER_MENU_PICK 0x41 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_UP 0x42 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_DOWN 0x43 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_LEFT 0x44 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_RIGHT 0x45 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_ESCAPE 0x46 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_VALUE_INCREASE 0x47 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MENU_VALUE_DECREASE 0x48 // HID type OSC
|
|
||||||
// Reserved 0x49-5F
|
|
||||||
#define HID_CONSUMER_DATA_ON_SCREEN 0x60 // HID type OOC
|
|
||||||
#define HID_CONSUMER_CLOSED_CAPTION 0x61 // HID type OOC
|
|
||||||
#define HID_CONSUMER_CLOSED_CAPTION_SELECT 0x62 // HID type OSC
|
|
||||||
#define HID_CONSUMER_VCR_SLASH_TV 0x63 // HID type OOC
|
|
||||||
#define HID_CONSUMER_BROADCAST_MODE 0x64 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SNAPSHOT 0x65 // HID type OSC
|
|
||||||
#define HID_CONSUMER_STILL 0x66 // HID type OSC
|
|
||||||
// Reserved 0x67-7F
|
|
||||||
#define HID_CONSUMER_SELECTION 0x80 // HID type NARY
|
|
||||||
#define HID_CONSUMER_ASSIGN_SELECTION 0x81 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MODE_STEP 0x82 // HID type OSC
|
|
||||||
#define HID_CONSUMER_RECALL_LAST 0x83 // HID type OSC
|
|
||||||
#define HID_CONSUMER_ENTER_CHANNEL 0x84 // HID type OSC
|
|
||||||
#define HID_CONSUMER_ORDER_MOVIE 0x85 // HID type OSC
|
|
||||||
#define HID_CONSUMER_CHANNEL 0x86 // HID type LC
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECTION 0x87 // HID type NARY
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_COMPUTER 0x88 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_TV 0x89 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_WWW 0x8A // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_DVD 0x8B // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_TELEPHONE 0x8C // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_PROGRAM_GUIDE 0x8D // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_VIDEO_PHONE 0x8E // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_GAMES 0x8F // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_MESSAGES 0x90 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_CD 0x91 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_VCR 0x92 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_TUNER 0x93 // HID type SEL
|
|
||||||
#define HID_CONSUMER_QUIT 0x94 // HID type OSC
|
|
||||||
#define HID_CONSUMER_HELP 0x95 // HID type OOC
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_TAPE 0x96 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_CABLE 0x97 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_SATELLITE 0x98 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_SECURITY 0x99 // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_HOME 0x9A // HID type SEL
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_CALL 0x9B // HID type SEL
|
|
||||||
#define HID_CONSUMER_CHANNEL_INCREMENT 0x9C // HID type OSC
|
|
||||||
#define HID_CONSUMER_CHANNEL_DECREMENT 0x9D // HID type OSC
|
|
||||||
#define HID_CONSUMER_MEDIA_SELECT_SAP 0x9E // HID type SEL
|
|
||||||
// Reserved 0x9F
|
|
||||||
#define HID_CONSUMER_VCR_PLUS 0xA0 // HID type OSC
|
|
||||||
#define HID_CONSUMER_ONCE 0xA1 // HID type OSC
|
|
||||||
#define HID_CONSUMER_DAILY 0xA2 // HID type OSC
|
|
||||||
#define HID_CONSUMER_WEEKLY 0xA3 // HID type OSC
|
|
||||||
#define HID_CONSUMER_MONTHLY 0xA4 // HID type OSC
|
|
||||||
// Reserved 0xA5-AF
|
|
||||||
#define HID_CONSUMER_PLAY 0xB0 // HID type OOC
|
|
||||||
#define HID_CONSUMER_PAUSE 0xB1 // HID type OOC
|
|
||||||
#define HID_CONSUMER_RECORD 0xB2 // HID type OOC
|
|
||||||
#define HID_CONSUMER_FAST_FORWARD 0xB3 // HID type OOC
|
|
||||||
#define HID_CONSUMER_REWIND 0xB4 // HID type OOC
|
|
||||||
#define HID_CONSUMER_SCAN_NEXT_TRACK 0xB5 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SCAN_PREVIOUS_TRACK 0xB6 // HID type OSC
|
|
||||||
#define HID_CONSUMER_STOP 0xB7 // HID type OSC
|
|
||||||
#define HID_CONSUMER_EJECT 0xB8 // HID type OSC
|
|
||||||
#define HID_CONSUMER_RANDOM_PLAY 0xB9 // HID type OOC
|
|
||||||
#define HID_CONSUMER_SELECT_DISC 0xBA // HID type NARY
|
|
||||||
#define HID_CONSUMER_ENTER_DISC_MC 0xBB
|
|
||||||
#define HID_CONSUMER_REPEAT 0xBC // HID type OSC
|
|
||||||
#define HID_CONSUMER_TRACKING 0xBD // HID type LC
|
|
||||||
#define HID_CONSUMER_TRACK_NORMAL 0xBE // HID type OSC
|
|
||||||
#define HID_CONSUMER_SLOW_TRACKING 0xBF // HID type LC
|
|
||||||
#define HID_CONSUMER_FRAME_FORWARD 0xC0 // HID type RTC
|
|
||||||
#define HID_CONSUMER_FRAME_BACK 0xC1 // HID type RTC
|
|
||||||
#define HID_CONSUMER_MARK 0xC2 // HID type OSC
|
|
||||||
#define HID_CONSUMER_CLEAR_MARK 0xC3 // HID type OSC
|
|
||||||
#define HID_CONSUMER_REPEAT_FROM_MARK 0xC4 // HID type OOC
|
|
||||||
#define HID_CONSUMER_RETURN_TO_MARK 0xC5 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SEARCH_MARK_FORWARD 0xC6 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SEARCH_MARK_BACKWARDS 0xC7 // HID type OSC
|
|
||||||
#define HID_CONSUMER_COUNTER_RESET 0xC8 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SHOW_COUNTER 0xC9 // HID type OSC
|
|
||||||
#define HID_CONSUMER_TRACKING_INCREMENT 0xCA // HID type RTC
|
|
||||||
#define HID_CONSUMER_TRACKING_DECREMENT 0xCB // HID type RTC
|
|
||||||
#define HID_CONSUMER_STOP_SLASH_EJECT 0xCC // HID type OSC
|
|
||||||
#define HID_CONSUMER_PLAY_SLASH_PAUSE 0xCD // HID type OSC
|
|
||||||
#define HID_CONSUMER_PLAY_SLASH_SKIP 0xCE // HID type OSC
|
|
||||||
// Reserved 0xCF-DF
|
|
||||||
#define HID_CONSUMER_VOLUME 0xE0 // HID type LC
|
|
||||||
#define HID_CONSUMER_BALANCE 0xE1 // HID type LC
|
|
||||||
#define HID_CONSUMER_MUTE 0xE2 // HID type OOC
|
|
||||||
#define HID_CONSUMER_BASS 0xE3 // HID type LC
|
|
||||||
#define HID_CONSUMER_TREBLE 0xE4 // HID type LC
|
|
||||||
#define HID_CONSUMER_BASS_BOOST 0xE5 // HID type OOC
|
|
||||||
#define HID_CONSUMER_SURROUND_MODE 0xE6 // HID type OSC
|
|
||||||
#define HID_CONSUMER_LOUDNESS 0xE7 // HID type OOC
|
|
||||||
#define HID_CONSUMER_MPX 0xE8 // HID type OOC
|
|
||||||
#define HID_CONSUMER_VOLUME_INCREMENT 0xE9 // HID type RTC
|
|
||||||
#define HID_CONSUMER_VOLUME_DECREMENT 0xEA // HID type RTC
|
|
||||||
// Reserved 0xEB-EF
|
|
||||||
#define HID_CONSUMER_SPEED_SELECT 0xF0 // HID type OSC
|
|
||||||
#define HID_CONSUMER_PLAYBACK_SPEED 0xF1 // HID type NARY
|
|
||||||
#define HID_CONSUMER_STANDARD_PLAY 0xF2 // HID type SEL
|
|
||||||
#define HID_CONSUMER_LONG_PLAY 0xF3 // HID type SEL
|
|
||||||
#define HID_CONSUMER_EXTENDED_PLAY 0xF4 // HID type SEL
|
|
||||||
#define HID_CONSUMER_SLOW 0xF5 // HID type OSC
|
|
||||||
// Reserved 0xF6-FF
|
|
||||||
#define HID_CONSUMER_FAN_ENABLE 0x100 // HID type OOC
|
|
||||||
#define HID_CONSUMER_FAN_SPEED 0x101 // HID type LC
|
|
||||||
#define HID_CONSUMER_LIGHT_ENABLE 0x102 // HID type OOC
|
|
||||||
#define HID_CONSUMER_LIGHT_ILLUMINATION_LEVEL 0x103 // HID type LC
|
|
||||||
#define HID_CONSUMER_CLIMATE_CONTROL_ENABLE 0x104 // HID type OOC
|
|
||||||
#define HID_CONSUMER_ROOM_TEMPERATURE 0x105 // HID type LC
|
|
||||||
#define HID_CONSUMER_SECURITY_ENABLE 0x106 // HID type OOC
|
|
||||||
#define HID_CONSUMER_FIRE_ALARM 0x107 // HID type OSC
|
|
||||||
#define HID_CONSUMER_POLICE_ALARM 0x108 // HID type OSC
|
|
||||||
#define HID_CONSUMER_PROXIMITY 0x109 // HID type LC
|
|
||||||
#define HID_CONSUMER_MOTION 0x10A // HID type OSC
|
|
||||||
#define HID_CONSUMER_DURESS_ALARM 0x10B // HID type OSC
|
|
||||||
#define HID_CONSUMER_HOLDUP_ALARM 0x10C // HID type OSC
|
|
||||||
#define HID_CONSUMER_MEDICAL_ALARM 0x10D // HID type OSC
|
|
||||||
// Reserved 0x10E-14F
|
|
||||||
#define HID_CONSUMER_BALANCE_RIGHT 0x150 // HID type RTC
|
|
||||||
#define HID_CONSUMER_BALANCE_LEFT 0x151 // HID type RTC
|
|
||||||
#define HID_CONSUMER_BASS_INCREMENT 0x152 // HID type RTC
|
|
||||||
#define HID_CONSUMER_BASS_DECREMENT 0x153 // HID type RTC
|
|
||||||
#define HID_CONSUMER_TREBLE_INCREMENT 0x154 // HID type RTC
|
|
||||||
#define HID_CONSUMER_TREBLE_DECREMENT 0x155 // HID type RTC
|
|
||||||
// Reserved 0x156-15F
|
|
||||||
#define HID_CONSUMER_SPEAKER_SYSTEM 0x160 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_LEFT 0x161 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_RIGHT 0x162 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_CENTER 0x163 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_FRONT 0x164 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_CENTER_FRONT 0x165 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_SIDE 0x166 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_SURROUND 0x167 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_LOW_FREQUENCY_ENHANCEMENT 0x168 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_TOP 0x169 // HID type CL
|
|
||||||
#define HID_CONSUMER_CHANNEL_UNKNOWN 0x16A // HID type CL
|
|
||||||
// Reserved 0x16B-16F
|
|
||||||
#define HID_CONSUMER_SUB-CHANNEL 0x170 // HID type LC
|
|
||||||
#define HID_CONSUMER_SUB-CHANNEL_INCREMENT 0x171 // HID type OSC
|
|
||||||
#define HID_CONSUMER_SUB-CHANNEL_DECREMENT 0x172 // HID type OSC
|
|
||||||
#define HID_CONSUMER_ALTERNATE_AUDIO_INCREMENT 0x173 // HID type OSC
|
|
||||||
#define HID_CONSUMER_ALTERNATE_AUDIO_DECREMENT 0x174 // HID type OSC
|
|
||||||
// Reserved 0x175-17F
|
|
||||||
#define HID_CONSUMER_APPLICATION_LAUNCH_BUTTONS 0x180 // HID type NARY
|
|
||||||
#define HID_CONSUMER_AL_LAUNCH_BUTTON_CONFIGURATION_TOOL 0x181 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_PROGRAMMABLE_BUTTON_CONFIGURATION 0x182 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CONSUMER_CONTROL_CONFIGURATION 0x183 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_WORD_PROCESSOR 0x184 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_TEXT_EDITOR 0x185 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_SPREADSHEET 0x186 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_GRAPHICS_EDITOR 0x187 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_PRESENTATION_APP 0x188 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DATABASE_APP 0x189 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_EMAIL_READER 0x18A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_NEWSREADER 0x18B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_VOICEMAIL 0x18C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CONTACTS_SLASH_ADDRESS_BOOK 0x18D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CALENDAR_SLASH_SCHEDULE 0x18E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_TASK_SLASH_PROJECT_MANAGER 0x18F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LOG_SLASH_JOURNAL_SLASH_TIMECARD 0x190 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CHECKBOOK_SLASH_FINANCE 0x191 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CALCULATOR 0x192 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_A_SLASH_V_CAPTURE_SLASH_PLAYBACK 0x193 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LOCAL_MACHINE_BROWSER 0x194 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LAN_SLASH_WAN_BROWSER 0x195 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_INTERNET_BROWSER 0x196 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_REMOTE_NETWORKING_SLASH_ISP_CONNECT 0x197 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_NETWORK_CONFERENCE 0x198 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_NETWORK_CHAT 0x199 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_TELEPHONY_SLASH_DIALER 0x19A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LOGON 0x19B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LOGOFF 0x19C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_LOGON_SLASH_LOGOFF 0x19D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_TERMINAL_LOCK_SLASH_SCREENSAVER 0x19E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CONTROL_PANEL 0x19F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_COMMAND_LINE_PROCESSOR_SLASH_RUN 0x1A0 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_PROCESS_SLASH_TASK_MANAGER 0x1A1 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_SELECT_TASK_SLASH_APPLICATION 0x1A2 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_NEXT_TASK_SLASH_APPLICATION 0x1A3 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_PREVIOUS_TASK_SLASH_APPLICATION 0x1A4 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_PREEMPTIVE_HALT_TASK_SLASH_APPLICATION 0x1A5 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_INTEGRATED_HELP_CENTER 0x1A6 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DOCUMENTS 0x1A7 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_THESAURUS 0x1A8 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DICTIONARY 0x1A9 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DESKTOP 0x1AA // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_SPELL_CHECK 0x1AB // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_GRAMMAR_CHECK 0x1AC // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_WIRELESS_STATUS 0x1AD // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_KEYBOARD_LAYOUT 0x1AE // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_VIRUS_PROTECTION 0x1AF // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ENCRYPTION 0x1B0 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_SCREEN_SAVER 0x1B1 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ALARMS 0x1B2 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CLOCK 0x1B3 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_FILE_BROWSER 0x1B4 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_POWER_STATUS 0x1B5 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_IMAGE_BROWSER 0x1B6 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_AUDIO_BROWSER 0x1B7 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_MOVIE_BROWSER 0x1B8 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DIGITAL_RIGHTS_MANAGER 0x1B9 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_DIGITAL_WALLET 0x1BA // HID type SEL
|
|
||||||
// _Reserved 0x1BB
|
|
||||||
#define HID_CONSUMER_AL_INSTANT_MESSAGING 0x1BC // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_OEM_FEATURES_SLASH__TIPS_SLASH_TUTORIAL_BROWSER 0x1BD // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_OEM_HELP 0x1BE // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ONLINE_COMMUNITY 0x1BF // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ENTERTAINMENT_CONTENT_BROWSER 0x1C0 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ONLINE_SHOPPING_BROWSER 0x1C1 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_SMARTCARD_INFORMATION_SLASH_HELP 0x1C2 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_MARKET_MONITOR_SLASH_FINANCE_BROWSER 0x1C3 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_CUSTOMIZED_CORPORATE_NEWS_BROWSER 0x1C4 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_ONLINE_ACTIVITY_BROWSER 0x1C5 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_RESEARCH_SLASH_SEARCH_BROWSER 0x1C6 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AL_AUDIO_PLAYER 0x1C7 // HID type SEL
|
|
||||||
// Reserved 0x1C8-1FF
|
|
||||||
#define HID_CONSUMER_GENERIC_GUI_APPLICATION_CONTROLS 0x200 // HID type NARY
|
|
||||||
#define HID_CONSUMER_AC_NEW 0x201 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_OPEN 0x202 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_CLOSE 0x203 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_EXIT 0x204 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_MAXIMIZE 0x205 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_MINIMIZE 0x206 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SAVE 0x207 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PRINT 0x208 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PROPERTIES 0x209 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_UNDO 0x21A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_COPY 0x21B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_CUT 0x21C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PASTE 0x21D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_ALL 0x21E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FIND 0x21F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FIND_AND_REPLACE 0x220 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SEARCH 0x221 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_GO_TO 0x222 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_HOME 0x223 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_BACK 0x224 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FORWARD 0x225 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_STOP 0x226 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_REFRESH 0x227 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PREVIOUS_LINK 0x228 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_NEXT_LINK 0x229 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_BOOKMARKS 0x22A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_HISTORY 0x22B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SUBSCRIPTIONS 0x22C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ZOOM_IN 0x22D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ZOOM_OUT 0x22E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ZOOM 0x22F // HID type LC
|
|
||||||
#define HID_CONSUMER_AC_FULL_SCREEN_VIEW 0x230 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_NORMAL_VIEW 0x231 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_VIEW_TOGGLE 0x232 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SCROLL_UP 0x233 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SCROLL_DOWN 0x234 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SCROLL 0x235 // HID type LC
|
|
||||||
#define HID_CONSUMER_AC_PAN_LEFT 0x236 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PAN_RIGHT 0x237 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PAN 0x238 // HID type LC
|
|
||||||
#define HID_CONSUMER_AC_NEW_WINDOW 0x239 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_TILE_HORIZONTALLY 0x23A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_TILE_VERTICALLY 0x23B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FORMAT 0x23C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_EDIT 0x23D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_BOLD 0x23E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ITALICS 0x23F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_UNDERLINE 0x240 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_STRIKETHROUGH 0x241 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SUBSCRIPT 0x242 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SUPERSCRIPT 0x243 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ALL_CAPS 0x244 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ROTATE 0x245 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_RESIZE 0x246 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FLIP_HORIZONTAL 0x247 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FLIP_VERTICAL 0x248 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_MIRROR_HORIZONTAL 0x249 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_MIRROR_VERTICAL 0x24A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FONT_SELECT 0x24B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FONT_COLOR 0x24C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FONT_SIZE 0x24D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_LEFT 0x24E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_CENTER_H 0x24F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_RIGHT 0x250 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_BLOCK_H 0x251 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_TOP 0x252 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_CENTER_V 0x253 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_BOTTOM 0x254 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_JUSTIFY_BLOCK_V 0x255 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INDENT_DECREASE 0x256 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INDENT_INCREASE 0x257 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_NUMBERED_LIST 0x258 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_RESTART_NUMBERING 0x259 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_BULLETED_LIST 0x25A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PROMOTE 0x25B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DEMOTE 0x25C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_YES 0x25D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_NO 0x25E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_CANCEL 0x25F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_CATALOG 0x260 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_BUY_SLASH_CHECKOUT 0x261 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ADD_TO_CART 0x262 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_EXPAND 0x263 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_EXPAND_ALL 0x264 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_COLLAPSE 0x265 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_COLLAPSE_ALL 0x266 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PRINT_PREVIEW 0x267 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PASTE_SPECIAL 0x268 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_MODE 0x269 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DELETE 0x26A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_LOCK 0x26B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_UNLOCK 0x26C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_PROTECT 0x26D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_UNPROTECT 0x26E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ATTACH_COMMENT 0x26F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DELETE_COMMENT 0x270 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_VIEW_COMMENT 0x271 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_WORD 0x272 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_SENTENCE 0x273 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_PARAGRAPH 0x274 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_COLUMN 0x275 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_ROW 0x276 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_TABLE 0x277 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_OBJECT 0x278 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_REDO_SLASH_REPEAT 0x279 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SORT 0x27A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SORT_ASCENDING 0x27B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SORT_DESCENDING 0x27C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FILTER 0x27D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SET_CLOCK 0x27E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_VIEW_CLOCK 0x27F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SELECT_TIME_ZONE 0x280 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_EDIT_TIME_ZONES 0x281 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SET_ALARM 0x282 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_CLEAR_ALARM 0x283 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SNOOZE_ALARM 0x284 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_RESET_ALARM 0x285 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SYNCHRONIZE 0x286 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SEND_SLASH_RECEIVE 0x287 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SEND_TO 0x288 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_REPLY 0x289 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_REPLY_ALL 0x28A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_FORWARD_MSG 0x28B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SEND 0x28C // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_ATTACH_FILE 0x28D // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_UPLOAD 0x28E // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DOWNLOAD_(SAVE_TARGET_AS) 0x28F // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SET_BORDERS 0x290 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_ROW 0x291 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_COLUMN 0x292 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_FILE 0x293 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_PICTURE 0x294 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_OBJECT 0x295 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_INSERT_SYMBOL 0x296 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SAVE_AND_CLOSE 0x297 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_RENAME 0x298 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_MERGE 0x299 // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_SPLIT 0x29A // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DISRIBUTE_HORIZONTALLY 0x29B // HID type SEL
|
|
||||||
#define HID_CONSUMER_AC_DISTRIBUTE_VERTICALLY 0x29C // HID type SEL
|
|
||||||
|
|
||||||
#endif // __HIDTables__
|
|
@ -1,252 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial.cpp - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "HardwareSerial_private.h"
|
|
||||||
|
|
||||||
// this next line disables the entire HardwareSerial.cpp,
|
|
||||||
// this is so I can support Attiny series and any other chip without a uart
|
|
||||||
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
|
|
||||||
|
|
||||||
// SerialEvent functions are weak, so when the user doesn't define them,
|
|
||||||
// the linker just sets their address to 0 (which is checked below).
|
|
||||||
// The Serialx_available is just a wrapper around Serialx.available(),
|
|
||||||
// but we can refer to it weakly so we don't pull in the entire
|
|
||||||
// HardwareSerial instance if the user doesn't also refer to it.
|
|
||||||
#if defined(HAVE_HWSERIAL0)
|
|
||||||
void serialEvent() __attribute__((weak));
|
|
||||||
bool Serial0_available() __attribute__((weak));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL1)
|
|
||||||
void serialEvent1() __attribute__((weak));
|
|
||||||
bool Serial1_available() __attribute__((weak));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL2)
|
|
||||||
void serialEvent2() __attribute__((weak));
|
|
||||||
bool Serial2_available() __attribute__((weak));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL3)
|
|
||||||
void serialEvent3() __attribute__((weak));
|
|
||||||
bool Serial3_available() __attribute__((weak));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void serialEventRun(void)
|
|
||||||
{
|
|
||||||
#if defined(HAVE_HWSERIAL0)
|
|
||||||
if (Serial0_available && serialEvent && Serial0_available()) serialEvent();
|
|
||||||
#endif
|
|
||||||
#if defined(HAVE_HWSERIAL1)
|
|
||||||
if (Serial1_available && serialEvent1 && Serial1_available()) serialEvent1();
|
|
||||||
#endif
|
|
||||||
#if defined(HAVE_HWSERIAL2)
|
|
||||||
if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2();
|
|
||||||
#endif
|
|
||||||
#if defined(HAVE_HWSERIAL3)
|
|
||||||
if (Serial3_available && serialEvent3 && Serial3_available()) serialEvent3();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// Actual interrupt handlers //////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void HardwareSerial::_tx_udr_empty_irq(void)
|
|
||||||
{
|
|
||||||
// If interrupts are enabled, there must be more data in the output
|
|
||||||
// buffer. Send the next byte
|
|
||||||
unsigned char c = _tx_buffer[_tx_buffer_tail];
|
|
||||||
_tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE;
|
|
||||||
|
|
||||||
*_udr = c;
|
|
||||||
|
|
||||||
// clear the TXC bit -- "can be cleared by writing a one to its bit
|
|
||||||
// location". This makes sure flush() won't return until the bytes
|
|
||||||
// actually got written
|
|
||||||
sbi(*_ucsra, TXC0);
|
|
||||||
|
|
||||||
if (_tx_buffer_head == _tx_buffer_tail) {
|
|
||||||
// Buffer empty, so disable interrupts
|
|
||||||
cbi(*_ucsrb, UDRIE0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Public Methods //////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void HardwareSerial::begin(unsigned long baud, byte config)
|
|
||||||
{
|
|
||||||
// Try u2x mode first
|
|
||||||
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
|
|
||||||
*_ucsra = 1 << U2X0;
|
|
||||||
|
|
||||||
// hardcoded exception for 57600 for compatibility with the bootloader
|
|
||||||
// shipped with the Duemilanove and previous boards and the firmware
|
|
||||||
// on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot
|
|
||||||
// be > 4095, so switch back to non-u2x mode if the baud rate is too
|
|
||||||
// low.
|
|
||||||
if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095))
|
|
||||||
{
|
|
||||||
*_ucsra = 0;
|
|
||||||
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
|
|
||||||
*_ubrrh = baud_setting >> 8;
|
|
||||||
*_ubrrl = baud_setting;
|
|
||||||
|
|
||||||
_written = false;
|
|
||||||
|
|
||||||
//set the data bits, parity, and stop bits
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
config |= 0x80; // select UCSRC register (shared with UBRRH)
|
|
||||||
#endif
|
|
||||||
*_ucsrc = config;
|
|
||||||
|
|
||||||
sbi(*_ucsrb, RXEN0);
|
|
||||||
sbi(*_ucsrb, TXEN0);
|
|
||||||
sbi(*_ucsrb, RXCIE0);
|
|
||||||
cbi(*_ucsrb, UDRIE0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HardwareSerial::end()
|
|
||||||
{
|
|
||||||
// wait for transmission of outgoing data
|
|
||||||
while (_tx_buffer_head != _tx_buffer_tail)
|
|
||||||
;
|
|
||||||
|
|
||||||
cbi(*_ucsrb, RXEN0);
|
|
||||||
cbi(*_ucsrb, TXEN0);
|
|
||||||
cbi(*_ucsrb, RXCIE0);
|
|
||||||
cbi(*_ucsrb, UDRIE0);
|
|
||||||
|
|
||||||
// clear any received data
|
|
||||||
_rx_buffer_head = _rx_buffer_tail;
|
|
||||||
}
|
|
||||||
|
|
||||||
int HardwareSerial::available(void)
|
|
||||||
{
|
|
||||||
return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int HardwareSerial::peek(void)
|
|
||||||
{
|
|
||||||
if (_rx_buffer_head == _rx_buffer_tail) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return _rx_buffer[_rx_buffer_tail];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int HardwareSerial::read(void)
|
|
||||||
{
|
|
||||||
// if the head isn't ahead of the tail, we don't have any characters
|
|
||||||
if (_rx_buffer_head == _rx_buffer_tail) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
unsigned char c = _rx_buffer[_rx_buffer_tail];
|
|
||||||
_rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int HardwareSerial::availableForWrite(void)
|
|
||||||
{
|
|
||||||
#if (SERIAL_TX_BUFFER_SIZE>256)
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
#endif
|
|
||||||
tx_buffer_index_t head = _tx_buffer_head;
|
|
||||||
tx_buffer_index_t tail = _tx_buffer_tail;
|
|
||||||
#if (SERIAL_TX_BUFFER_SIZE>256)
|
|
||||||
SREG = oldSREG;
|
|
||||||
#endif
|
|
||||||
if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
|
|
||||||
return tail - head - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HardwareSerial::flush()
|
|
||||||
{
|
|
||||||
// If we have never written a byte, no need to flush. This special
|
|
||||||
// case is needed since there is no way to force the TXC (transmit
|
|
||||||
// complete) bit to 1 during initialization
|
|
||||||
if (!_written)
|
|
||||||
return;
|
|
||||||
|
|
||||||
while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) {
|
|
||||||
if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0))
|
|
||||||
// Interrupts are globally disabled, but the DR empty
|
|
||||||
// interrupt should be enabled, so poll the DR empty flag to
|
|
||||||
// prevent deadlock
|
|
||||||
if (bit_is_set(*_ucsra, UDRE0))
|
|
||||||
_tx_udr_empty_irq();
|
|
||||||
}
|
|
||||||
// If we get here, nothing is queued anymore (DRIE is disabled) and
|
|
||||||
// the hardware finished tranmission (TXC is set).
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t HardwareSerial::write(uint8_t c)
|
|
||||||
{
|
|
||||||
// If the buffer and the data register is empty, just write the byte
|
|
||||||
// to the data register and be done. This shortcut helps
|
|
||||||
// significantly improve the effective datarate at high (>
|
|
||||||
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
|
|
||||||
if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
|
|
||||||
*_udr = c;
|
|
||||||
sbi(*_ucsra, TXC0);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;
|
|
||||||
|
|
||||||
// If the output buffer is full, there's nothing for it other than to
|
|
||||||
// wait for the interrupt handler to empty it a bit
|
|
||||||
while (i == _tx_buffer_tail) {
|
|
||||||
if (bit_is_clear(SREG, SREG_I)) {
|
|
||||||
// Interrupts are disabled, so we'll have to poll the data
|
|
||||||
// register empty flag ourselves. If it is set, pretend an
|
|
||||||
// interrupt has happened and call the handler to free up
|
|
||||||
// space for us.
|
|
||||||
if(bit_is_set(*_ucsra, UDRE0))
|
|
||||||
_tx_udr_empty_irq();
|
|
||||||
} else {
|
|
||||||
// nop, the interrupt handler will free up space for us
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_tx_buffer[_tx_buffer_head] = c;
|
|
||||||
_tx_buffer_head = i;
|
|
||||||
|
|
||||||
sbi(*_ucsrb, UDRIE0);
|
|
||||||
_written = true;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif // whole file
|
|
@ -1,151 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial.h - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef HardwareSerial_h
|
|
||||||
#define HardwareSerial_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
#include "Stream.h"
|
|
||||||
|
|
||||||
// Define constants and variables for buffering incoming serial data. We're
|
|
||||||
// using a ring buffer (I think), in which head is the index of the location
|
|
||||||
// to which to write the next incoming character and tail is the index of the
|
|
||||||
// location from which to read.
|
|
||||||
// NOTE: a "power of 2" buffer size is reccomended to dramatically
|
|
||||||
// optimize all the modulo operations for ring buffers.
|
|
||||||
#if !(defined(SERIAL_TX_BUFFER_SIZE) && defined(SERIAL_RX_BUFFER_SIZE))
|
|
||||||
#if (RAMEND < 1000)
|
|
||||||
#define SERIAL_TX_BUFFER_SIZE 16
|
|
||||||
#define SERIAL_RX_BUFFER_SIZE 16
|
|
||||||
#else
|
|
||||||
#define SERIAL_TX_BUFFER_SIZE 64
|
|
||||||
#define SERIAL_RX_BUFFER_SIZE 64
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#if (SERIAL_TX_BUFFER_SIZE>256)
|
|
||||||
typedef uint16_t tx_buffer_index_t;
|
|
||||||
#else
|
|
||||||
typedef uint8_t tx_buffer_index_t;
|
|
||||||
#endif
|
|
||||||
#if (SERIAL_RX_BUFFER_SIZE>256)
|
|
||||||
typedef uint16_t rx_buffer_index_t;
|
|
||||||
#else
|
|
||||||
typedef uint8_t rx_buffer_index_t;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Define config for Serial.begin(baud, config);
|
|
||||||
#define SERIAL_5N1 0x00
|
|
||||||
#define SERIAL_6N1 0x02
|
|
||||||
#define SERIAL_7N1 0x04
|
|
||||||
#define SERIAL_8N1 0x06
|
|
||||||
#define SERIAL_5N2 0x08
|
|
||||||
#define SERIAL_6N2 0x0A
|
|
||||||
#define SERIAL_7N2 0x0C
|
|
||||||
#define SERIAL_8N2 0x0E
|
|
||||||
#define SERIAL_5E1 0x20
|
|
||||||
#define SERIAL_6E1 0x22
|
|
||||||
#define SERIAL_7E1 0x24
|
|
||||||
#define SERIAL_8E1 0x26
|
|
||||||
#define SERIAL_5E2 0x28
|
|
||||||
#define SERIAL_6E2 0x2A
|
|
||||||
#define SERIAL_7E2 0x2C
|
|
||||||
#define SERIAL_8E2 0x2E
|
|
||||||
#define SERIAL_5O1 0x30
|
|
||||||
#define SERIAL_6O1 0x32
|
|
||||||
#define SERIAL_7O1 0x34
|
|
||||||
#define SERIAL_8O1 0x36
|
|
||||||
#define SERIAL_5O2 0x38
|
|
||||||
#define SERIAL_6O2 0x3A
|
|
||||||
#define SERIAL_7O2 0x3C
|
|
||||||
#define SERIAL_8O2 0x3E
|
|
||||||
|
|
||||||
class HardwareSerial : public Stream
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
volatile uint8_t * const _ubrrh;
|
|
||||||
volatile uint8_t * const _ubrrl;
|
|
||||||
volatile uint8_t * const _ucsra;
|
|
||||||
volatile uint8_t * const _ucsrb;
|
|
||||||
volatile uint8_t * const _ucsrc;
|
|
||||||
volatile uint8_t * const _udr;
|
|
||||||
// Has any byte been written to the UART since begin()
|
|
||||||
bool _written;
|
|
||||||
|
|
||||||
volatile rx_buffer_index_t _rx_buffer_head;
|
|
||||||
volatile rx_buffer_index_t _rx_buffer_tail;
|
|
||||||
volatile tx_buffer_index_t _tx_buffer_head;
|
|
||||||
volatile tx_buffer_index_t _tx_buffer_tail;
|
|
||||||
|
|
||||||
// Don't put any members after these buffers, since only the first
|
|
||||||
// 32 bytes of this struct can be accessed quickly using the ldd
|
|
||||||
// instruction.
|
|
||||||
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
|
|
||||||
unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline HardwareSerial(
|
|
||||||
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
|
|
||||||
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
|
|
||||||
volatile uint8_t *ucsrc, volatile uint8_t *udr);
|
|
||||||
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
|
|
||||||
void begin(unsigned long, uint8_t);
|
|
||||||
void end();
|
|
||||||
virtual int available(void);
|
|
||||||
virtual int peek(void);
|
|
||||||
virtual int read(void);
|
|
||||||
int availableForWrite(void);
|
|
||||||
virtual void flush(void);
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(long n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(unsigned int n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(int n) { return write((uint8_t)n); }
|
|
||||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
|
||||||
operator bool() { return true; }
|
|
||||||
|
|
||||||
// Interrupt handlers - Not intended to be called externally
|
|
||||||
inline void _rx_complete_irq(void);
|
|
||||||
void _tx_udr_empty_irq(void);
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(UBRRH) || defined(UBRR0H)
|
|
||||||
extern HardwareSerial Serial;
|
|
||||||
#define HAVE_HWSERIAL0
|
|
||||||
#endif
|
|
||||||
#if defined(UBRR1H)
|
|
||||||
extern HardwareSerial Serial1;
|
|
||||||
#define HAVE_HWSERIAL1
|
|
||||||
#endif
|
|
||||||
#if defined(UBRR2H)
|
|
||||||
extern HardwareSerial Serial2;
|
|
||||||
#define HAVE_HWSERIAL2
|
|
||||||
#endif
|
|
||||||
#if defined(UBRR3H)
|
|
||||||
extern HardwareSerial Serial3;
|
|
||||||
#define HAVE_HWSERIAL3
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern void serialEventRun(void) __attribute__((weak));
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial0.cpp - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "HardwareSerial_private.h"
|
|
||||||
|
|
||||||
// Each HardwareSerial is defined in its own file, sine the linker pulls
|
|
||||||
// in the entire file when any element inside is used. --gc-sections can
|
|
||||||
// additionally cause unused symbols to be dropped, but ISRs have the
|
|
||||||
// "used" attribute so are never dropped and they keep the
|
|
||||||
// HardwareSerial instance in as well. Putting each instance in its own
|
|
||||||
// file prevents the linker from pulling in any unused instances in the
|
|
||||||
// first place.
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL0)
|
|
||||||
|
|
||||||
#if defined(USART_RX_vect)
|
|
||||||
ISR(USART_RX_vect)
|
|
||||||
#elif defined(USART0_RX_vect)
|
|
||||||
ISR(USART0_RX_vect)
|
|
||||||
#elif defined(USART_RXC_vect)
|
|
||||||
ISR(USART_RXC_vect) // ATmega8
|
|
||||||
#else
|
|
||||||
#error "Don't know what the Data Received vector is called for Serial"
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
Serial._rx_complete_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(UART0_UDRE_vect)
|
|
||||||
ISR(UART0_UDRE_vect)
|
|
||||||
#elif defined(UART_UDRE_vect)
|
|
||||||
ISR(UART_UDRE_vect)
|
|
||||||
#elif defined(USART0_UDRE_vect)
|
|
||||||
ISR(USART0_UDRE_vect)
|
|
||||||
#elif defined(USART_UDRE_vect)
|
|
||||||
ISR(USART_UDRE_vect)
|
|
||||||
#else
|
|
||||||
#error "Don't know what the Data Register Empty vector is called for Serial"
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
Serial._tx_udr_empty_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(UBRRH) && defined(UBRRL)
|
|
||||||
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
|
|
||||||
#else
|
|
||||||
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Function that can be weakly referenced by serialEventRun to prevent
|
|
||||||
// pulling in this file if it's not otherwise used.
|
|
||||||
bool Serial0_available() {
|
|
||||||
return Serial.available();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // HAVE_HWSERIAL0
|
|
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial1.cpp - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "HardwareSerial_private.h"
|
|
||||||
|
|
||||||
// Each HardwareSerial is defined in its own file, sine the linker pulls
|
|
||||||
// in the entire file when any element inside is used. --gc-sections can
|
|
||||||
// additionally cause unused symbols to be dropped, but ISRs have the
|
|
||||||
// "used" attribute so are never dropped and they keep the
|
|
||||||
// HardwareSerial instance in as well. Putting each instance in its own
|
|
||||||
// file prevents the linker from pulling in any unused instances in the
|
|
||||||
// first place.
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL1)
|
|
||||||
|
|
||||||
#if defined(UART1_RX_vect)
|
|
||||||
ISR(UART1_RX_vect)
|
|
||||||
#elif defined(USART1_RX_vect)
|
|
||||||
ISR(USART1_RX_vect)
|
|
||||||
#else
|
|
||||||
#error "Don't know what the Data Register Empty vector is called for Serial1"
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
Serial1._rx_complete_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(UART1_UDRE_vect)
|
|
||||||
ISR(UART1_UDRE_vect)
|
|
||||||
#elif defined(USART1_UDRE_vect)
|
|
||||||
ISR(USART1_UDRE_vect)
|
|
||||||
#else
|
|
||||||
#error "Don't know what the Data Register Empty vector is called for Serial1"
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
Serial1._tx_udr_empty_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
|
|
||||||
|
|
||||||
// Function that can be weakly referenced by serialEventRun to prevent
|
|
||||||
// pulling in this file if it's not otherwise used.
|
|
||||||
bool Serial1_available() {
|
|
||||||
return Serial1.available();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // HAVE_HWSERIAL1
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial2.cpp - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "HardwareSerial_private.h"
|
|
||||||
|
|
||||||
// Each HardwareSerial is defined in its own file, sine the linker pulls
|
|
||||||
// in the entire file when any element inside is used. --gc-sections can
|
|
||||||
// additionally cause unused symbols to be dropped, but ISRs have the
|
|
||||||
// "used" attribute so are never dropped and they keep the
|
|
||||||
// HardwareSerial instance in as well. Putting each instance in its own
|
|
||||||
// file prevents the linker from pulling in any unused instances in the
|
|
||||||
// first place.
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL2)
|
|
||||||
|
|
||||||
ISR(USART2_RX_vect)
|
|
||||||
{
|
|
||||||
Serial2._rx_complete_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(USART2_UDRE_vect)
|
|
||||||
{
|
|
||||||
Serial2._tx_udr_empty_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
|
|
||||||
|
|
||||||
// Function that can be weakly referenced by serialEventRun to prevent
|
|
||||||
// pulling in this file if it's not otherwise used.
|
|
||||||
bool Serial2_available() {
|
|
||||||
return Serial2.available();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // HAVE_HWSERIAL2
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial3.cpp - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
Modified 3 December 2013 by Matthijs Kooijman
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "HardwareSerial.h"
|
|
||||||
#include "HardwareSerial_private.h"
|
|
||||||
|
|
||||||
// Each HardwareSerial is defined in its own file, sine the linker pulls
|
|
||||||
// in the entire file when any element inside is used. --gc-sections can
|
|
||||||
// additionally cause unused symbols to be dropped, but ISRs have the
|
|
||||||
// "used" attribute so are never dropped and they keep the
|
|
||||||
// HardwareSerial instance in as well. Putting each instance in its own
|
|
||||||
// file prevents the linker from pulling in any unused instances in the
|
|
||||||
// first place.
|
|
||||||
|
|
||||||
#if defined(HAVE_HWSERIAL3)
|
|
||||||
|
|
||||||
ISR(USART3_RX_vect)
|
|
||||||
{
|
|
||||||
Serial3._rx_complete_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(USART3_UDRE_vect)
|
|
||||||
{
|
|
||||||
Serial3._tx_udr_empty_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
|
|
||||||
|
|
||||||
// Function that can be weakly referenced by serialEventRun to prevent
|
|
||||||
// pulling in this file if it's not otherwise used.
|
|
||||||
bool Serial3_available() {
|
|
||||||
return Serial3.available();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // HAVE_HWSERIAL3
|
|
@ -1,123 +0,0 @@
|
|||||||
/*
|
|
||||||
HardwareSerial_private.h - Hardware serial library for Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
Modified 14 August 2012 by Alarus
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
|
|
||||||
// this next line disables the entire HardwareSerial.cpp,
|
|
||||||
// this is so I can support Attiny series and any other chip without a uart
|
|
||||||
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
|
|
||||||
|
|
||||||
// Ensure that the various bit positions we use are available with a 0
|
|
||||||
// postfix, so we can always use the values for UART0 for all UARTs. The
|
|
||||||
// alternative, passing the various values for each UART to the
|
|
||||||
// HardwareSerial constructor also works, but makes the code bigger and
|
|
||||||
// slower.
|
|
||||||
#if !defined(TXC0)
|
|
||||||
#if defined(TXC)
|
|
||||||
// Some chips like ATmega8 don't have UPE, only PE. The other bits are
|
|
||||||
// named as expected.
|
|
||||||
#if !defined(UPE) && defined(PE)
|
|
||||||
#define UPE PE
|
|
||||||
#endif
|
|
||||||
// On ATmega8, the uart and its bits are not numbered, so there is no TXC0 etc.
|
|
||||||
#define TXC0 TXC
|
|
||||||
#define RXEN0 RXEN
|
|
||||||
#define TXEN0 TXEN
|
|
||||||
#define RXCIE0 RXCIE
|
|
||||||
#define UDRIE0 UDRIE
|
|
||||||
#define U2X0 U2X
|
|
||||||
#define UPE0 UPE
|
|
||||||
#define UDRE0 UDRE
|
|
||||||
#elif defined(TXC1)
|
|
||||||
// Some devices have uart1 but no uart0
|
|
||||||
#define TXC0 TXC1
|
|
||||||
#define RXEN0 RXEN1
|
|
||||||
#define TXEN0 TXEN1
|
|
||||||
#define RXCIE0 RXCIE1
|
|
||||||
#define UDRIE0 UDRIE1
|
|
||||||
#define U2X0 U2X1
|
|
||||||
#define UPE0 UPE1
|
|
||||||
#define UDRE0 UDRE1
|
|
||||||
#else
|
|
||||||
#error No UART found in HardwareSerial.cpp
|
|
||||||
#endif
|
|
||||||
#endif // !defined TXC0
|
|
||||||
|
|
||||||
// Check at compiletime that it is really ok to use the bit positions of
|
|
||||||
// UART0 for the other UARTs as well, in case these values ever get
|
|
||||||
// changed for future hardware.
|
|
||||||
#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \
|
|
||||||
UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \
|
|
||||||
UDRE1 != UDRE0)
|
|
||||||
#error "Not all bit positions for UART1 are the same as for UART0"
|
|
||||||
#endif
|
|
||||||
#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \
|
|
||||||
UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \
|
|
||||||
UDRE2 != UDRE0)
|
|
||||||
#error "Not all bit positions for UART2 are the same as for UART0"
|
|
||||||
#endif
|
|
||||||
#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \
|
|
||||||
UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \
|
|
||||||
UDRE3 != UDRE0)
|
|
||||||
#error "Not all bit positions for UART3 are the same as for UART0"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Constructors ////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
HardwareSerial::HardwareSerial(
|
|
||||||
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
|
|
||||||
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
|
|
||||||
volatile uint8_t *ucsrc, volatile uint8_t *udr) :
|
|
||||||
_ubrrh(ubrrh), _ubrrl(ubrrl),
|
|
||||||
_ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
|
|
||||||
_udr(udr),
|
|
||||||
_rx_buffer_head(0), _rx_buffer_tail(0),
|
|
||||||
_tx_buffer_head(0), _tx_buffer_tail(0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Actual interrupt handlers //////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void HardwareSerial::_rx_complete_irq(void)
|
|
||||||
{
|
|
||||||
if (bit_is_clear(*_ucsra, UPE0)) {
|
|
||||||
// No Parity error, read byte and store it in the buffer if there is
|
|
||||||
// room
|
|
||||||
unsigned char c = *_udr;
|
|
||||||
rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
|
|
||||||
|
|
||||||
// if we should be storing the received character into the location
|
|
||||||
// just before the tail (meaning that the head would advance to the
|
|
||||||
// current location of the tail), we're about to overflow the buffer
|
|
||||||
// and so we don't write the character or advance the head.
|
|
||||||
if (i != _rx_buffer_tail) {
|
|
||||||
_rx_buffer[_rx_buffer_head] = c;
|
|
||||||
_rx_buffer_head = i;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Parity error, read byte but discard it
|
|
||||||
*_udr;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // whole file
|
|
@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
IPAddress.cpp - Base class that provides IPAddress
|
|
||||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <IPAddress.h>
|
|
||||||
|
|
||||||
IPAddress::IPAddress()
|
|
||||||
{
|
|
||||||
_address.dword = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
|
|
||||||
{
|
|
||||||
_address.bytes[0] = first_octet;
|
|
||||||
_address.bytes[1] = second_octet;
|
|
||||||
_address.bytes[2] = third_octet;
|
|
||||||
_address.bytes[3] = fourth_octet;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPAddress::IPAddress(uint32_t address)
|
|
||||||
{
|
|
||||||
_address.dword = address;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPAddress::IPAddress(const uint8_t *address)
|
|
||||||
{
|
|
||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
|
||||||
}
|
|
||||||
|
|
||||||
IPAddress& IPAddress::operator=(const uint8_t *address)
|
|
||||||
{
|
|
||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPAddress& IPAddress::operator=(uint32_t address)
|
|
||||||
{
|
|
||||||
_address.dword = address;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IPAddress::operator==(const uint8_t* addr) const
|
|
||||||
{
|
|
||||||
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t IPAddress::printTo(Print& p) const
|
|
||||||
{
|
|
||||||
size_t n = 0;
|
|
||||||
for (int i =0; i < 3; i++)
|
|
||||||
{
|
|
||||||
n += p.print(_address.bytes[i], DEC);
|
|
||||||
n += p.print('.');
|
|
||||||
}
|
|
||||||
n += p.print(_address.bytes[3], DEC);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
IPAddress.h - Base class that provides IPAddress
|
|
||||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef IPAddress_h
|
|
||||||
#define IPAddress_h
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <Printable.h>
|
|
||||||
|
|
||||||
// A class to make it easier to handle and pass around IP addresses
|
|
||||||
|
|
||||||
class IPAddress : public Printable {
|
|
||||||
private:
|
|
||||||
union {
|
|
||||||
uint8_t bytes[4]; // IPv4 address
|
|
||||||
uint32_t dword;
|
|
||||||
} _address;
|
|
||||||
|
|
||||||
// Access the raw byte array containing the address. Because this returns a pointer
|
|
||||||
// to the internal structure rather than a copy of the address this function should only
|
|
||||||
// be used when you know that the usage of the returned uint8_t* will be transient and not
|
|
||||||
// stored.
|
|
||||||
uint8_t* raw_address() { return _address.bytes; };
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
IPAddress();
|
|
||||||
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
|
||||||
IPAddress(uint32_t address);
|
|
||||||
IPAddress(const uint8_t *address);
|
|
||||||
|
|
||||||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
|
||||||
// to a four-byte uint8_t array is expected
|
|
||||||
operator uint32_t() const { return _address.dword; };
|
|
||||||
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
|
|
||||||
bool operator==(const uint8_t* addr) const;
|
|
||||||
|
|
||||||
// Overloaded index operator to allow getting and setting individual octets of the address
|
|
||||||
uint8_t operator[](int index) const { return _address.bytes[index]; };
|
|
||||||
uint8_t& operator[](int index) { return _address.bytes[index]; };
|
|
||||||
|
|
||||||
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
|
||||||
IPAddress& operator=(const uint8_t *address);
|
|
||||||
IPAddress& operator=(uint32_t address);
|
|
||||||
|
|
||||||
virtual size_t printTo(Print& p) const;
|
|
||||||
|
|
||||||
friend class EthernetClass;
|
|
||||||
friend class UDP;
|
|
||||||
friend class Client;
|
|
||||||
friend class Server;
|
|
||||||
friend class DhcpClass;
|
|
||||||
friend class DNSClient;
|
|
||||||
};
|
|
||||||
|
|
||||||
const IPAddress INADDR_NONE(0,0,0,0);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,264 +0,0 @@
|
|||||||
/*
|
|
||||||
Print.cpp - Base class that provides print() and println()
|
|
||||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#include "Print.h"
|
|
||||||
|
|
||||||
// Public Methods //////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/* default implementation: may be overridden */
|
|
||||||
size_t Print::write(const uint8_t *buffer, size_t size)
|
|
||||||
{
|
|
||||||
size_t n = 0;
|
|
||||||
while (size--) {
|
|
||||||
n += write(*buffer++);
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(const __FlashStringHelper *ifsh)
|
|
||||||
{
|
|
||||||
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
|
|
||||||
size_t n = 0;
|
|
||||||
while (1) {
|
|
||||||
unsigned char c = pgm_read_byte(p++);
|
|
||||||
if (c == 0) break;
|
|
||||||
n += write(c);
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(const String &s)
|
|
||||||
{
|
|
||||||
return write(s.c_str(), s.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(const char str[])
|
|
||||||
{
|
|
||||||
return write(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(char c)
|
|
||||||
{
|
|
||||||
return write(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(unsigned char b, int base)
|
|
||||||
{
|
|
||||||
return print((unsigned long) b, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(int n, int base)
|
|
||||||
{
|
|
||||||
return print((long) n, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(unsigned int n, int base)
|
|
||||||
{
|
|
||||||
return print((unsigned long) n, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(long n, int base)
|
|
||||||
{
|
|
||||||
if (base == 0) {
|
|
||||||
return write(n);
|
|
||||||
} else if (base == 10) {
|
|
||||||
if (n < 0) {
|
|
||||||
int t = print('-');
|
|
||||||
n = -n;
|
|
||||||
return printNumber(n, 10) + t;
|
|
||||||
}
|
|
||||||
return printNumber(n, 10);
|
|
||||||
} else {
|
|
||||||
return printNumber(n, base);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(unsigned long n, int base)
|
|
||||||
{
|
|
||||||
if (base == 0) return write(n);
|
|
||||||
else return printNumber(n, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(double n, int digits)
|
|
||||||
{
|
|
||||||
return printFloat(n, digits);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(const __FlashStringHelper *ifsh)
|
|
||||||
{
|
|
||||||
size_t n = print(ifsh);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::print(const Printable& x)
|
|
||||||
{
|
|
||||||
return x.printTo(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(void)
|
|
||||||
{
|
|
||||||
size_t n = print('\r');
|
|
||||||
n += print('\n');
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(const String &s)
|
|
||||||
{
|
|
||||||
size_t n = print(s);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(const char c[])
|
|
||||||
{
|
|
||||||
size_t n = print(c);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(char c)
|
|
||||||
{
|
|
||||||
size_t n = print(c);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(unsigned char b, int base)
|
|
||||||
{
|
|
||||||
size_t n = print(b, base);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(int num, int base)
|
|
||||||
{
|
|
||||||
size_t n = print(num, base);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(unsigned int num, int base)
|
|
||||||
{
|
|
||||||
size_t n = print(num, base);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(long num, int base)
|
|
||||||
{
|
|
||||||
size_t n = print(num, base);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(unsigned long num, int base)
|
|
||||||
{
|
|
||||||
size_t n = print(num, base);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(double num, int digits)
|
|
||||||
{
|
|
||||||
size_t n = print(num, digits);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::println(const Printable& x)
|
|
||||||
{
|
|
||||||
size_t n = print(x);
|
|
||||||
n += println();
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Private Methods /////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
size_t Print::printNumber(unsigned long n, uint8_t base) {
|
|
||||||
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
|
||||||
char *str = &buf[sizeof(buf) - 1];
|
|
||||||
|
|
||||||
*str = '\0';
|
|
||||||
|
|
||||||
// prevent crash if called with base == 1
|
|
||||||
if (base < 2) base = 10;
|
|
||||||
|
|
||||||
do {
|
|
||||||
unsigned long m = n;
|
|
||||||
n /= base;
|
|
||||||
char c = m - base * n;
|
|
||||||
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
|
||||||
} while(n);
|
|
||||||
|
|
||||||
return write(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Print::printFloat(double number, uint8_t digits)
|
|
||||||
{
|
|
||||||
size_t n = 0;
|
|
||||||
|
|
||||||
if (isnan(number)) return print("nan");
|
|
||||||
if (isinf(number)) return print("inf");
|
|
||||||
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
|
|
||||||
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
|
|
||||||
|
|
||||||
// Handle negative numbers
|
|
||||||
if (number < 0.0)
|
|
||||||
{
|
|
||||||
n += print('-');
|
|
||||||
number = -number;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
|
||||||
double rounding = 0.5;
|
|
||||||
for (uint8_t i=0; i<digits; ++i)
|
|
||||||
rounding /= 10.0;
|
|
||||||
|
|
||||||
number += rounding;
|
|
||||||
|
|
||||||
// Extract the integer part of the number and print it
|
|
||||||
unsigned long int_part = (unsigned long)number;
|
|
||||||
double remainder = number - (double)int_part;
|
|
||||||
n += print(int_part);
|
|
||||||
|
|
||||||
// Print the decimal point, but only if there are digits beyond
|
|
||||||
if (digits > 0) {
|
|
||||||
n += print(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract digits from the remainder one at a time
|
|
||||||
while (digits-- > 0)
|
|
||||||
{
|
|
||||||
remainder *= 10.0;
|
|
||||||
int toPrint = int(remainder);
|
|
||||||
n += print(toPrint);
|
|
||||||
remainder -= toPrint;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
Print.h - Base class that provides print() and println()
|
|
||||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Print_h
|
|
||||||
#define Print_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <stdio.h> // for size_t
|
|
||||||
|
|
||||||
#include "WString.h"
|
|
||||||
#include "Printable.h"
|
|
||||||
|
|
||||||
#define DEC 10
|
|
||||||
#define HEX 16
|
|
||||||
#define OCT 8
|
|
||||||
#define BIN 2
|
|
||||||
|
|
||||||
class Print
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
int write_error;
|
|
||||||
size_t printNumber(unsigned long, uint8_t);
|
|
||||||
size_t printFloat(double, uint8_t);
|
|
||||||
protected:
|
|
||||||
void setWriteError(int err = 1) { write_error = err; }
|
|
||||||
public:
|
|
||||||
Print() : write_error(0) {}
|
|
||||||
|
|
||||||
int getWriteError() { return write_error; }
|
|
||||||
void clearWriteError() { setWriteError(0); }
|
|
||||||
|
|
||||||
virtual size_t write(uint8_t) = 0;
|
|
||||||
size_t write(const char *str) {
|
|
||||||
if (str == NULL) return 0;
|
|
||||||
return write((const uint8_t *)str, strlen(str));
|
|
||||||
}
|
|
||||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
|
||||||
size_t write(const char *buffer, size_t size) {
|
|
||||||
return write((const uint8_t *)buffer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t print(const __FlashStringHelper *);
|
|
||||||
size_t print(const String &);
|
|
||||||
size_t print(const char[]);
|
|
||||||
size_t print(char);
|
|
||||||
size_t print(unsigned char, int = DEC);
|
|
||||||
size_t print(int, int = DEC);
|
|
||||||
size_t print(unsigned int, int = DEC);
|
|
||||||
size_t print(long, int = DEC);
|
|
||||||
size_t print(unsigned long, int = DEC);
|
|
||||||
size_t print(double, int = 2);
|
|
||||||
size_t print(const Printable&);
|
|
||||||
|
|
||||||
size_t println(const __FlashStringHelper *);
|
|
||||||
size_t println(const String &s);
|
|
||||||
size_t println(const char[]);
|
|
||||||
size_t println(char);
|
|
||||||
size_t println(unsigned char, int = DEC);
|
|
||||||
size_t println(int, int = DEC);
|
|
||||||
size_t println(unsigned int, int = DEC);
|
|
||||||
size_t println(long, int = DEC);
|
|
||||||
size_t println(unsigned long, int = DEC);
|
|
||||||
size_t println(double, int = 2);
|
|
||||||
size_t println(const Printable&);
|
|
||||||
size_t println(void);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
Printable.h - Interface class that allows printing of complex types
|
|
||||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Printable_h
|
|
||||||
#define Printable_h
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
class Print;
|
|
||||||
|
|
||||||
/** The Printable class provides a way for new classes to allow themselves to be printed.
|
|
||||||
By deriving from Printable and implementing the printTo method, it will then be possible
|
|
||||||
for users to print out instances of this class by passing them into the usual
|
|
||||||
Print::print and Print::println methods.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Printable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual size_t printTo(Print& p) const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
Server.h - Base class that provides Server
|
|
||||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef server_h
|
|
||||||
#define server_h
|
|
||||||
|
|
||||||
#include "Print.h"
|
|
||||||
|
|
||||||
class Server : public Print {
|
|
||||||
public:
|
|
||||||
virtual void begin() =0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,270 +0,0 @@
|
|||||||
/*
|
|
||||||
Stream.cpp - adds parsing methods to Stream class
|
|
||||||
Copyright (c) 2008 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Created July 2011
|
|
||||||
parsing functions based on TextFinder library by Michael Margolis
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "Stream.h"
|
|
||||||
|
|
||||||
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
|
|
||||||
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
|
|
||||||
|
|
||||||
// private method to read stream with timeout
|
|
||||||
int Stream::timedRead()
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
_startMillis = millis();
|
|
||||||
do {
|
|
||||||
c = read();
|
|
||||||
if (c >= 0) return c;
|
|
||||||
} while(millis() - _startMillis < _timeout);
|
|
||||||
return -1; // -1 indicates timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
// private method to peek stream with timeout
|
|
||||||
int Stream::timedPeek()
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
_startMillis = millis();
|
|
||||||
do {
|
|
||||||
c = peek();
|
|
||||||
if (c >= 0) return c;
|
|
||||||
} while(millis() - _startMillis < _timeout);
|
|
||||||
return -1; // -1 indicates timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns peek of the next digit in the stream or -1 if timeout
|
|
||||||
// discards non-numeric characters
|
|
||||||
int Stream::peekNextDigit()
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
while (1) {
|
|
||||||
c = timedPeek();
|
|
||||||
if (c < 0) return c; // timeout
|
|
||||||
if (c == '-') return c;
|
|
||||||
if (c >= '0' && c <= '9') return c;
|
|
||||||
read(); // discard non-numeric
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Public Methods
|
|
||||||
//////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
|
|
||||||
{
|
|
||||||
_timeout = timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
// find returns true if the target string is found
|
|
||||||
bool Stream::find(char *target)
|
|
||||||
{
|
|
||||||
return findUntil(target, (char*)"");
|
|
||||||
}
|
|
||||||
|
|
||||||
// reads data from the stream until the target string of given length is found
|
|
||||||
// returns true if target string is found, false if timed out
|
|
||||||
bool Stream::find(char *target, size_t length)
|
|
||||||
{
|
|
||||||
return findUntil(target, length, NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// as find but search ends if the terminator string is found
|
|
||||||
bool Stream::findUntil(char *target, char *terminator)
|
|
||||||
{
|
|
||||||
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
|
||||||
}
|
|
||||||
|
|
||||||
// reads data from the stream until the target string of the given length is found
|
|
||||||
// search terminated if the terminator string is found
|
|
||||||
// returns true if target string is found, false if terminated or timed out
|
|
||||||
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
|
|
||||||
{
|
|
||||||
size_t index = 0; // maximum target string length is 64k bytes!
|
|
||||||
size_t termIndex = 0;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
if( *target == 0)
|
|
||||||
return true; // return true if target is a null string
|
|
||||||
while( (c = timedRead()) > 0){
|
|
||||||
|
|
||||||
if(c != target[index])
|
|
||||||
index = 0; // reset index if any char does not match
|
|
||||||
|
|
||||||
if( c == target[index]){
|
|
||||||
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
|
|
||||||
if(++index >= targetLen){ // return true if all chars in the target match
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(termLen > 0 && c == terminator[termIndex]){
|
|
||||||
if(++termIndex >= termLen)
|
|
||||||
return false; // return false if terminate string found before target string
|
|
||||||
}
|
|
||||||
else
|
|
||||||
termIndex = 0;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// returns the first valid (long) integer value from the current position.
|
|
||||||
// initial characters that are not digits (or the minus sign) are skipped
|
|
||||||
// function is terminated by the first character that is not a digit.
|
|
||||||
long Stream::parseInt()
|
|
||||||
{
|
|
||||||
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
|
|
||||||
}
|
|
||||||
|
|
||||||
// as above but a given skipChar is ignored
|
|
||||||
// this allows format characters (typically commas) in values to be ignored
|
|
||||||
long Stream::parseInt(char skipChar)
|
|
||||||
{
|
|
||||||
boolean isNegative = false;
|
|
||||||
long value = 0;
|
|
||||||
int c;
|
|
||||||
|
|
||||||
c = peekNextDigit();
|
|
||||||
// ignore non numeric leading characters
|
|
||||||
if(c < 0)
|
|
||||||
return 0; // zero returned if timeout
|
|
||||||
|
|
||||||
do{
|
|
||||||
if(c == skipChar)
|
|
||||||
; // ignore this charactor
|
|
||||||
else if(c == '-')
|
|
||||||
isNegative = true;
|
|
||||||
else if(c >= '0' && c <= '9') // is c a digit?
|
|
||||||
value = value * 10 + c - '0';
|
|
||||||
read(); // consume the character we got with peek
|
|
||||||
c = timedPeek();
|
|
||||||
}
|
|
||||||
while( (c >= '0' && c <= '9') || c == skipChar );
|
|
||||||
|
|
||||||
if(isNegative)
|
|
||||||
value = -value;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// as parseInt but returns a floating point value
|
|
||||||
float Stream::parseFloat()
|
|
||||||
{
|
|
||||||
return parseFloat(NO_SKIP_CHAR);
|
|
||||||
}
|
|
||||||
|
|
||||||
// as above but the given skipChar is ignored
|
|
||||||
// this allows format characters (typically commas) in values to be ignored
|
|
||||||
float Stream::parseFloat(char skipChar){
|
|
||||||
boolean isNegative = false;
|
|
||||||
boolean isFraction = false;
|
|
||||||
long value = 0;
|
|
||||||
int c;
|
|
||||||
float fraction = 1.0;
|
|
||||||
|
|
||||||
c = peekNextDigit();
|
|
||||||
// ignore non numeric leading characters
|
|
||||||
if(c < 0)
|
|
||||||
return 0; // zero returned if timeout
|
|
||||||
|
|
||||||
do{
|
|
||||||
if(c == skipChar)
|
|
||||||
; // ignore
|
|
||||||
else if(c == '-')
|
|
||||||
isNegative = true;
|
|
||||||
else if (c == '.')
|
|
||||||
isFraction = true;
|
|
||||||
else if(c >= '0' && c <= '9') { // is c a digit?
|
|
||||||
value = value * 10 + c - '0';
|
|
||||||
if(isFraction)
|
|
||||||
fraction *= 0.1;
|
|
||||||
}
|
|
||||||
read(); // consume the character we got with peek
|
|
||||||
c = timedPeek();
|
|
||||||
}
|
|
||||||
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
|
|
||||||
|
|
||||||
if(isNegative)
|
|
||||||
value = -value;
|
|
||||||
if(isFraction)
|
|
||||||
return value * fraction;
|
|
||||||
else
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// read characters from stream into buffer
|
|
||||||
// terminates if length characters have been read, or timeout (see setTimeout)
|
|
||||||
// returns the number of characters placed in the buffer
|
|
||||||
// the buffer is NOT null terminated.
|
|
||||||
//
|
|
||||||
size_t Stream::readBytes(char *buffer, size_t length)
|
|
||||||
{
|
|
||||||
size_t count = 0;
|
|
||||||
while (count < length) {
|
|
||||||
int c = timedRead();
|
|
||||||
if (c < 0) break;
|
|
||||||
*buffer++ = (char)c;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// as readBytes with terminator character
|
|
||||||
// terminates if length characters have been read, timeout, or if the terminator character detected
|
|
||||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
|
||||||
|
|
||||||
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
|
|
||||||
{
|
|
||||||
if (length < 1) return 0;
|
|
||||||
size_t index = 0;
|
|
||||||
while (index < length) {
|
|
||||||
int c = timedRead();
|
|
||||||
if (c < 0 || c == terminator) break;
|
|
||||||
*buffer++ = (char)c;
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
return index; // return number of characters, not including null terminator
|
|
||||||
}
|
|
||||||
|
|
||||||
String Stream::readString()
|
|
||||||
{
|
|
||||||
String ret;
|
|
||||||
int c = timedRead();
|
|
||||||
while (c >= 0)
|
|
||||||
{
|
|
||||||
ret += (char)c;
|
|
||||||
c = timedRead();
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
String Stream::readStringUntil(char terminator)
|
|
||||||
{
|
|
||||||
String ret;
|
|
||||||
int c = timedRead();
|
|
||||||
while (c >= 0 && c != terminator)
|
|
||||||
{
|
|
||||||
ret += (char)c;
|
|
||||||
c = timedRead();
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
@ -1,102 +0,0 @@
|
|||||||
/*
|
|
||||||
Stream.h - base class for character-based streams.
|
|
||||||
Copyright (c) 2010 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
parsing functions based on TextFinder library by Michael Margolis
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Stream_h
|
|
||||||
#define Stream_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include "Print.h"
|
|
||||||
|
|
||||||
// compatability macros for testing
|
|
||||||
/*
|
|
||||||
#define getInt() parseInt()
|
|
||||||
#define getInt(skipChar) parseInt(skipchar)
|
|
||||||
#define getFloat() parseFloat()
|
|
||||||
#define getFloat(skipChar) parseFloat(skipChar)
|
|
||||||
#define getString( pre_string, post_string, buffer, length)
|
|
||||||
readBytesBetween( pre_string, terminator, buffer, length)
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Stream : public Print
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
|
||||||
unsigned long _startMillis; // used for timeout measurement
|
|
||||||
int timedRead(); // private method to read stream with timeout
|
|
||||||
int timedPeek(); // private method to peek stream with timeout
|
|
||||||
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual int available() = 0;
|
|
||||||
virtual int read() = 0;
|
|
||||||
virtual int peek() = 0;
|
|
||||||
virtual void flush() = 0;
|
|
||||||
|
|
||||||
Stream() {_timeout=1000;}
|
|
||||||
|
|
||||||
// parsing methods
|
|
||||||
|
|
||||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
|
||||||
|
|
||||||
bool find(char *target); // reads data from the stream until the target string is found
|
|
||||||
bool find(uint8_t *target) { return find ((char *)target); }
|
|
||||||
// returns true if target string is found, false if timed out (see setTimeout)
|
|
||||||
|
|
||||||
bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
|
|
||||||
bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
|
|
||||||
// returns true if target string is found, false if timed out
|
|
||||||
|
|
||||||
bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
|
|
||||||
bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
|
|
||||||
|
|
||||||
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
|
|
||||||
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
|
|
||||||
|
|
||||||
|
|
||||||
long parseInt(); // returns the first valid (long) integer value from the current position.
|
|
||||||
// initial characters that are not digits (or the minus sign) are skipped
|
|
||||||
// integer is terminated by the first character that is not a digit.
|
|
||||||
|
|
||||||
float parseFloat(); // float version of parseInt
|
|
||||||
|
|
||||||
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
|
|
||||||
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
|
|
||||||
// terminates if length characters have been read or timeout (see setTimeout)
|
|
||||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
|
||||||
|
|
||||||
size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
|
|
||||||
size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
|
|
||||||
// terminates if length characters have been read, timeout, or if the terminator character detected
|
|
||||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
|
||||||
|
|
||||||
// Arduino String functions to be added here
|
|
||||||
String readString();
|
|
||||||
String readStringUntil(char terminator);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
long parseInt(char skipChar); // as above but the given skipChar is ignored
|
|
||||||
// as above but the given skipChar is ignored
|
|
||||||
// this allows format characters (typically commas) in values to be ignored
|
|
||||||
|
|
||||||
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,616 +0,0 @@
|
|||||||
/* Tone.cpp
|
|
||||||
|
|
||||||
A Tone Generator Library
|
|
||||||
|
|
||||||
Written by Brett Hagman
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Version Modified By Date Comments
|
|
||||||
------- ----------- -------- --------
|
|
||||||
0001 B Hagman 09/08/02 Initial coding
|
|
||||||
0002 B Hagman 09/08/18 Multiple pins
|
|
||||||
0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
|
|
||||||
0004 B Hagman 09/09/26 Fixed problems with ATmega8
|
|
||||||
0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
|
|
||||||
09/11/25 Changed pin toggle method to XOR
|
|
||||||
09/11/25 Fixed timer0 from being excluded
|
|
||||||
0006 D Mellis 09/12/29 Replaced objects with functions
|
|
||||||
0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
|
|
||||||
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
|
|
||||||
*************************************************/
|
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
|
|
||||||
#define TCCR2A TCCR2
|
|
||||||
#define TCCR2B TCCR2
|
|
||||||
#define COM2A1 COM21
|
|
||||||
#define COM2A0 COM20
|
|
||||||
#define OCR2A OCR2
|
|
||||||
#define TIMSK2 TIMSK
|
|
||||||
#define OCIE2A OCIE2
|
|
||||||
#define TIMER2_COMPA_vect TIMER2_COMP_vect
|
|
||||||
#define TIMSK1 TIMSK
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// timerx_toggle_count:
|
|
||||||
// > 0 - duration specified
|
|
||||||
// = 0 - stopped
|
|
||||||
// < 0 - infinitely (until stop() method called, or new play() called)
|
|
||||||
|
|
||||||
#if !defined(__AVR_ATmega8__)
|
|
||||||
volatile long timer0_toggle_count;
|
|
||||||
volatile uint8_t *timer0_pin_port;
|
|
||||||
volatile uint8_t timer0_pin_mask;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
volatile long timer1_toggle_count;
|
|
||||||
volatile uint8_t *timer1_pin_port;
|
|
||||||
volatile uint8_t timer1_pin_mask;
|
|
||||||
volatile long timer2_toggle_count;
|
|
||||||
volatile uint8_t *timer2_pin_port;
|
|
||||||
volatile uint8_t timer2_pin_mask;
|
|
||||||
|
|
||||||
#if defined(TIMSK3)
|
|
||||||
volatile long timer3_toggle_count;
|
|
||||||
volatile uint8_t *timer3_pin_port;
|
|
||||||
volatile uint8_t timer3_pin_mask;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK4)
|
|
||||||
volatile long timer4_toggle_count;
|
|
||||||
volatile uint8_t *timer4_pin_port;
|
|
||||||
volatile uint8_t timer4_pin_mask;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK5)
|
|
||||||
volatile long timer5_toggle_count;
|
|
||||||
volatile uint8_t *timer5_pin_port;
|
|
||||||
volatile uint8_t timer5_pin_mask;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
|
|
||||||
#define AVAILABLE_TONE_PINS 1
|
|
||||||
#define USE_TIMER2
|
|
||||||
|
|
||||||
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
|
|
||||||
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
|
|
||||||
|
|
||||||
#elif defined(__AVR_ATmega8__)
|
|
||||||
|
|
||||||
#define AVAILABLE_TONE_PINS 1
|
|
||||||
#define USE_TIMER2
|
|
||||||
|
|
||||||
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
|
|
||||||
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
|
|
||||||
|
|
||||||
#elif defined(__AVR_ATmega32U4__)
|
|
||||||
|
|
||||||
#define AVAILABLE_TONE_PINS 1
|
|
||||||
#define USE_TIMER3
|
|
||||||
|
|
||||||
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
|
|
||||||
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define AVAILABLE_TONE_PINS 1
|
|
||||||
#define USE_TIMER2
|
|
||||||
|
|
||||||
// Leave timer 0 to last.
|
|
||||||
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
|
|
||||||
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int8_t toneBegin(uint8_t _pin)
|
|
||||||
{
|
|
||||||
int8_t _timer = -1;
|
|
||||||
|
|
||||||
// if we're already using the pin, the timer should be configured.
|
|
||||||
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
|
||||||
if (tone_pins[i] == _pin) {
|
|
||||||
return pgm_read_byte(tone_pin_to_timer_PGM + i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// search for an unused timer.
|
|
||||||
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
|
||||||
if (tone_pins[i] == 255) {
|
|
||||||
tone_pins[i] = _pin;
|
|
||||||
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_timer != -1)
|
|
||||||
{
|
|
||||||
// Set timer specific stuff
|
|
||||||
// All timers in CTC mode
|
|
||||||
// 8 bit timers will require changing prescalar values,
|
|
||||||
// whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
|
|
||||||
switch (_timer)
|
|
||||||
{
|
|
||||||
#if defined(TCCR0A) && defined(TCCR0B)
|
|
||||||
case 0:
|
|
||||||
// 8 bit timer
|
|
||||||
TCCR0A = 0;
|
|
||||||
TCCR0B = 0;
|
|
||||||
bitWrite(TCCR0A, WGM01, 1);
|
|
||||||
bitWrite(TCCR0B, CS00, 1);
|
|
||||||
timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer0_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
|
|
||||||
case 1:
|
|
||||||
// 16 bit timer
|
|
||||||
TCCR1A = 0;
|
|
||||||
TCCR1B = 0;
|
|
||||||
bitWrite(TCCR1B, WGM12, 1);
|
|
||||||
bitWrite(TCCR1B, CS10, 1);
|
|
||||||
timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer1_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR2A) && defined(TCCR2B)
|
|
||||||
case 2:
|
|
||||||
// 8 bit timer
|
|
||||||
TCCR2A = 0;
|
|
||||||
TCCR2B = 0;
|
|
||||||
bitWrite(TCCR2A, WGM21, 1);
|
|
||||||
bitWrite(TCCR2B, CS20, 1);
|
|
||||||
timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer2_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
|
|
||||||
case 3:
|
|
||||||
// 16 bit timer
|
|
||||||
TCCR3A = 0;
|
|
||||||
TCCR3B = 0;
|
|
||||||
bitWrite(TCCR3B, WGM32, 1);
|
|
||||||
bitWrite(TCCR3B, CS30, 1);
|
|
||||||
timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer3_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
|
|
||||||
case 4:
|
|
||||||
// 16 bit timer
|
|
||||||
TCCR4A = 0;
|
|
||||||
TCCR4B = 0;
|
|
||||||
#if defined(WGM42)
|
|
||||||
bitWrite(TCCR4B, WGM42, 1);
|
|
||||||
#elif defined(CS43)
|
|
||||||
#warning this may not be correct
|
|
||||||
// atmega32u4
|
|
||||||
bitWrite(TCCR4B, CS43, 1);
|
|
||||||
#endif
|
|
||||||
bitWrite(TCCR4B, CS40, 1);
|
|
||||||
timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer4_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
|
|
||||||
case 5:
|
|
||||||
// 16 bit timer
|
|
||||||
TCCR5A = 0;
|
|
||||||
TCCR5B = 0;
|
|
||||||
bitWrite(TCCR5B, WGM52, 1);
|
|
||||||
bitWrite(TCCR5B, CS50, 1);
|
|
||||||
timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
|
|
||||||
timer5_pin_mask = digitalPinToBitMask(_pin);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return _timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// frequency (in hertz) and duration (in milliseconds).
|
|
||||||
|
|
||||||
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
|
|
||||||
{
|
|
||||||
uint8_t prescalarbits = 0b001;
|
|
||||||
long toggle_count = 0;
|
|
||||||
uint32_t ocr = 0;
|
|
||||||
int8_t _timer;
|
|
||||||
|
|
||||||
_timer = toneBegin(_pin);
|
|
||||||
|
|
||||||
if (_timer >= 0)
|
|
||||||
{
|
|
||||||
// Set the pinMode as OUTPUT
|
|
||||||
pinMode(_pin, OUTPUT);
|
|
||||||
|
|
||||||
// if we are using an 8 bit timer, scan through prescalars to find the best fit
|
|
||||||
if (_timer == 0 || _timer == 2)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 - 1;
|
|
||||||
prescalarbits = 0b001; // ck/1: same for both timers
|
|
||||||
if (ocr > 255)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 8 - 1;
|
|
||||||
prescalarbits = 0b010; // ck/8: same for both timers
|
|
||||||
|
|
||||||
if (_timer == 2 && ocr > 255)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 32 - 1;
|
|
||||||
prescalarbits = 0b011;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ocr > 255)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 64 - 1;
|
|
||||||
prescalarbits = _timer == 0 ? 0b011 : 0b100;
|
|
||||||
|
|
||||||
if (_timer == 2 && ocr > 255)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 128 - 1;
|
|
||||||
prescalarbits = 0b101;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ocr > 255)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 256 - 1;
|
|
||||||
prescalarbits = _timer == 0 ? 0b100 : 0b110;
|
|
||||||
if (ocr > 255)
|
|
||||||
{
|
|
||||||
// can't do any better than /1024
|
|
||||||
ocr = F_CPU / frequency / 2 / 1024 - 1;
|
|
||||||
prescalarbits = _timer == 0 ? 0b101 : 0b111;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(TCCR0B)
|
|
||||||
if (_timer == 0)
|
|
||||||
{
|
|
||||||
TCCR0B = prescalarbits;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR2B)
|
|
||||||
{
|
|
||||||
TCCR2B = prescalarbits;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
{
|
|
||||||
// dummy place holder to make the above ifdefs work
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// two choices for the 16 bit timers: ck/1 or ck/64
|
|
||||||
ocr = F_CPU / frequency / 2 - 1;
|
|
||||||
|
|
||||||
prescalarbits = 0b001;
|
|
||||||
if (ocr > 0xffff)
|
|
||||||
{
|
|
||||||
ocr = F_CPU / frequency / 2 / 64 - 1;
|
|
||||||
prescalarbits = 0b011;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_timer == 1)
|
|
||||||
{
|
|
||||||
#if defined(TCCR1B)
|
|
||||||
TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#if defined(TCCR3B)
|
|
||||||
else if (_timer == 3)
|
|
||||||
TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR4B)
|
|
||||||
else if (_timer == 4)
|
|
||||||
TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR5B)
|
|
||||||
else if (_timer == 5)
|
|
||||||
TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Calculate the toggle count
|
|
||||||
if (duration > 0)
|
|
||||||
{
|
|
||||||
toggle_count = 2 * frequency * duration / 1000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
toggle_count = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the OCR for the given timer,
|
|
||||||
// set the toggle count,
|
|
||||||
// then turn on the interrupts
|
|
||||||
switch (_timer)
|
|
||||||
{
|
|
||||||
|
|
||||||
#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
|
|
||||||
case 0:
|
|
||||||
OCR0A = ocr;
|
|
||||||
timer0_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK0, OCIE0A, 1);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
|
|
||||||
OCR1A = ocr;
|
|
||||||
timer1_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK1, OCIE1A, 1);
|
|
||||||
#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
|
|
||||||
// this combination is for at least the ATmega32
|
|
||||||
OCR1A = ocr;
|
|
||||||
timer1_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK, OCIE1A, 1);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
|
|
||||||
case 2:
|
|
||||||
OCR2A = ocr;
|
|
||||||
timer2_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK2, OCIE2A, 1);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK3)
|
|
||||||
case 3:
|
|
||||||
OCR3A = ocr;
|
|
||||||
timer3_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK3, OCIE3A, 1);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK4)
|
|
||||||
case 4:
|
|
||||||
OCR4A = ocr;
|
|
||||||
timer4_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK4, OCIE4A, 1);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
|
|
||||||
case 5:
|
|
||||||
OCR5A = ocr;
|
|
||||||
timer5_toggle_count = toggle_count;
|
|
||||||
bitWrite(TIMSK5, OCIE5A, 1);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// XXX: this function only works properly for timer 2 (the only one we use
|
|
||||||
// currently). for the others, it should end the tone, but won't restore
|
|
||||||
// proper PWM functionality for the timer.
|
|
||||||
void disableTimer(uint8_t _timer)
|
|
||||||
{
|
|
||||||
switch (_timer)
|
|
||||||
{
|
|
||||||
case 0:
|
|
||||||
#if defined(TIMSK0)
|
|
||||||
TIMSK0 = 0;
|
|
||||||
#elif defined(TIMSK)
|
|
||||||
TIMSK = 0; // atmega32
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
#if defined(TIMSK1) && defined(OCIE1A)
|
|
||||||
case 1:
|
|
||||||
bitWrite(TIMSK1, OCIE1A, 0);
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
#if defined(TIMSK2) && defined(OCIE2A)
|
|
||||||
bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR2A) && defined(WGM20)
|
|
||||||
TCCR2A = (1 << WGM20);
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR2B) && defined(CS22)
|
|
||||||
TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
|
|
||||||
#endif
|
|
||||||
#if defined(OCR2A)
|
|
||||||
OCR2A = 0;
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
#if defined(TIMSK3)
|
|
||||||
case 3:
|
|
||||||
TIMSK3 = 0;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK4)
|
|
||||||
case 4:
|
|
||||||
TIMSK4 = 0;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMSK5)
|
|
||||||
case 5:
|
|
||||||
TIMSK5 = 0;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void noTone(uint8_t _pin)
|
|
||||||
{
|
|
||||||
int8_t _timer = -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
|
|
||||||
if (tone_pins[i] == _pin) {
|
|
||||||
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
|
|
||||||
tone_pins[i] = 255;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
disableTimer(_timer);
|
|
||||||
|
|
||||||
digitalWrite(_pin, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef USE_TIMER0
|
|
||||||
ISR(TIMER0_COMPA_vect)
|
|
||||||
{
|
|
||||||
if (timer0_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer0_pin_port ^= timer0_pin_mask;
|
|
||||||
|
|
||||||
if (timer0_toggle_count > 0)
|
|
||||||
timer0_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
disableTimer(0);
|
|
||||||
*timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_TIMER1
|
|
||||||
ISR(TIMER1_COMPA_vect)
|
|
||||||
{
|
|
||||||
if (timer1_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer1_pin_port ^= timer1_pin_mask;
|
|
||||||
|
|
||||||
if (timer1_toggle_count > 0)
|
|
||||||
timer1_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
disableTimer(1);
|
|
||||||
*timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_TIMER2
|
|
||||||
ISR(TIMER2_COMPA_vect)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (timer2_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer2_pin_port ^= timer2_pin_mask;
|
|
||||||
|
|
||||||
if (timer2_toggle_count > 0)
|
|
||||||
timer2_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// need to call noTone() so that the tone_pins[] entry is reset, so the
|
|
||||||
// timer gets initialized next time we call tone().
|
|
||||||
// XXX: this assumes timer 2 is always the first one used.
|
|
||||||
noTone(tone_pins[0]);
|
|
||||||
// disableTimer(2);
|
|
||||||
// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_TIMER3
|
|
||||||
ISR(TIMER3_COMPA_vect)
|
|
||||||
{
|
|
||||||
if (timer3_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer3_pin_port ^= timer3_pin_mask;
|
|
||||||
|
|
||||||
if (timer3_toggle_count > 0)
|
|
||||||
timer3_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
disableTimer(3);
|
|
||||||
*timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_TIMER4
|
|
||||||
ISR(TIMER4_COMPA_vect)
|
|
||||||
{
|
|
||||||
if (timer4_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer4_pin_port ^= timer4_pin_mask;
|
|
||||||
|
|
||||||
if (timer4_toggle_count > 0)
|
|
||||||
timer4_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
disableTimer(4);
|
|
||||||
*timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_TIMER5
|
|
||||||
ISR(TIMER5_COMPA_vect)
|
|
||||||
{
|
|
||||||
if (timer5_toggle_count != 0)
|
|
||||||
{
|
|
||||||
// toggle the pin
|
|
||||||
*timer5_pin_port ^= timer5_pin_mask;
|
|
||||||
|
|
||||||
if (timer5_toggle_count > 0)
|
|
||||||
timer5_toggle_count--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
disableTimer(5);
|
|
||||||
*timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,299 +0,0 @@
|
|||||||
/*
|
|
||||||
USBAPI.h
|
|
||||||
Copyright (c) 2005-2014 Arduino. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __USBAPI__
|
|
||||||
#define __USBAPI__
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#include <avr/eeprom.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <util/delay.h>
|
|
||||||
|
|
||||||
typedef unsigned char u8;
|
|
||||||
typedef unsigned short u16;
|
|
||||||
typedef unsigned long u32;
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#if defined(USBCON)
|
|
||||||
|
|
||||||
#include "USBDesc.h"
|
|
||||||
#include "USBCore.h"
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// USB
|
|
||||||
|
|
||||||
class USBDevice_
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
USBDevice_();
|
|
||||||
bool configured();
|
|
||||||
|
|
||||||
void attach();
|
|
||||||
void detach(); // Serial port goes down too...
|
|
||||||
void poll();
|
|
||||||
bool wakeupHost(); // returns false, when wakeup cannot be processed
|
|
||||||
};
|
|
||||||
extern USBDevice_ USBDevice;
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Serial over CDC (Serial1 is the physical port)
|
|
||||||
|
|
||||||
struct ring_buffer;
|
|
||||||
|
|
||||||
#if (RAMEND < 1000)
|
|
||||||
#define SERIAL_BUFFER_SIZE 16
|
|
||||||
#else
|
|
||||||
#define SERIAL_BUFFER_SIZE 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class Serial_ : public Stream
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
int peek_buffer;
|
|
||||||
public:
|
|
||||||
Serial_() { peek_buffer = -1; };
|
|
||||||
void begin(unsigned long);
|
|
||||||
void begin(unsigned long, uint8_t);
|
|
||||||
void end(void);
|
|
||||||
|
|
||||||
virtual int available(void);
|
|
||||||
virtual int peek(void);
|
|
||||||
virtual int read(void);
|
|
||||||
virtual void flush(void);
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
virtual size_t write(const uint8_t*, size_t);
|
|
||||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
|
||||||
operator bool();
|
|
||||||
|
|
||||||
volatile uint8_t _rx_buffer_head;
|
|
||||||
volatile uint8_t _rx_buffer_tail;
|
|
||||||
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
|
|
||||||
};
|
|
||||||
extern Serial_ Serial;
|
|
||||||
|
|
||||||
#define HAVE_CDCSERIAL
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Mouse
|
|
||||||
|
|
||||||
#define MOUSE_LEFT 1
|
|
||||||
#define MOUSE_RIGHT 2
|
|
||||||
#define MOUSE_MIDDLE 4
|
|
||||||
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
|
||||||
|
|
||||||
class Mouse_
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
uint8_t _buttons;
|
|
||||||
void buttons(uint8_t b);
|
|
||||||
public:
|
|
||||||
Mouse_(void);
|
|
||||||
void begin(void);
|
|
||||||
void end(void);
|
|
||||||
void click(uint8_t b = MOUSE_LEFT);
|
|
||||||
void move(signed char x, signed char y, signed char wheel = 0);
|
|
||||||
void moveAbsolute(uint16_t x, uint16_t y);
|
|
||||||
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
|
||||||
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
|
|
||||||
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
|
|
||||||
};
|
|
||||||
extern Mouse_ Mouse;
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Keyboard
|
|
||||||
|
|
||||||
// Legacy keycodes. These are what you use with 'press()' and 'release()'
|
|
||||||
#define KEY_LEFT_CTRL 0x80
|
|
||||||
#define KEY_LEFT_SHIFT 0x81
|
|
||||||
#define KEY_LEFT_ALT 0x82
|
|
||||||
#define KEY_LEFT_GUI 0x83
|
|
||||||
#define KEY_RIGHT_CTRL 0x84
|
|
||||||
#define KEY_RIGHT_SHIFT 0x85
|
|
||||||
#define KEY_RIGHT_ALT 0x86
|
|
||||||
#define KEY_RIGHT_GUI 0x87
|
|
||||||
|
|
||||||
#define KEY_UP_ARROW 0xDA
|
|
||||||
#define KEY_DOWN_ARROW 0xD9
|
|
||||||
#define KEY_LEFT_ARROW 0xD8
|
|
||||||
#define KEY_RIGHT_ARROW 0xD7
|
|
||||||
#define KEY_BACKSPACE 0xB2
|
|
||||||
#define KEY_TAB 0xB3
|
|
||||||
#define KEY_RETURN 0xB0
|
|
||||||
#define KEY_ESC 0xB1
|
|
||||||
#define KEY_INSERT 0xD1
|
|
||||||
#define KEY_DELETE 0xD4
|
|
||||||
#define KEY_PAGE_UP 0xD3
|
|
||||||
#define KEY_PAGE_DOWN 0xD6
|
|
||||||
#define KEY_HOME 0xD2
|
|
||||||
#define KEY_END 0xD5
|
|
||||||
#define KEY_CAPS_LOCK 0xC1
|
|
||||||
#define KEY_F1 0xC2
|
|
||||||
#define KEY_F2 0xC3
|
|
||||||
#define KEY_F3 0xC4
|
|
||||||
#define KEY_F4 0xC5
|
|
||||||
#define KEY_F5 0xC6
|
|
||||||
#define KEY_F6 0xC7
|
|
||||||
#define KEY_F7 0xC8
|
|
||||||
#define KEY_F8 0xC9
|
|
||||||
#define KEY_F9 0xCA
|
|
||||||
#define KEY_F10 0xCB
|
|
||||||
#define KEY_F11 0xCC
|
|
||||||
#define KEY_F12 0xCD
|
|
||||||
|
|
||||||
|
|
||||||
// System Control values for Keyboard_::systemControl()
|
|
||||||
// these defines come from the HID usage table "Generic Desktop Page (0x01)"
|
|
||||||
// in the USB standard document "HID Usage Tables" (HUT1_12v2.pdf)
|
|
||||||
// Currently this list contains only OSC (one shot control) values,
|
|
||||||
// the implementation of systemControl will have to be changed when
|
|
||||||
// adding OOC or RTC values.
|
|
||||||
#define SYSTEM_CONTROL_POWER_DOWN 1
|
|
||||||
#define SYSTEM_CONTROL_SLEEP 2
|
|
||||||
#define SYSTEM_CONTROL_WAKEUP 3
|
|
||||||
#define SYSTEM_CONTROL_COLD_RESTART 4
|
|
||||||
#define SYSTEM_CONTROL_WARM_RESTART 5
|
|
||||||
#define SYSTEM_CONTROL_DOCK 6
|
|
||||||
#define SYSTEM_CONTROL_UNDOCK 7
|
|
||||||
#define SYSTEM_CONTROL_SPEAKER_MUTE 8
|
|
||||||
#define SYSTEM_CONTROL_HIBERNATE 9
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_INVERT 10
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_INTERNAL 11
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_EXTERNAL 12
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_BOTH 13
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_DUAL 14
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_TOGGLE_INT_EXT 15
|
|
||||||
#define SYSTEM_CONTROL_DISPLAY_SWAP 16
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Consumer Control values for Keyboard_::systemControl()
|
|
||||||
// these defines come from the HID usage table "Consumer Device Page (0x0c)"
|
|
||||||
// in the USB standard document "HID Usage Tables" (HUT1_12v2.pdf)
|
|
||||||
// Currently this list contains only OSC (one shot control) values,
|
|
||||||
// the implementation of systemControl will have to be changed when
|
|
||||||
// adding OOC or RTC values.
|
|
||||||
|
|
||||||
#define CONSUMER_CONTROL_VOLUME_MUTE 1
|
|
||||||
#define CONSUMER_CONTROL_VOLUME_UP 2
|
|
||||||
#define CONSUMER_CONTROL_VOLUME_DOWN 3
|
|
||||||
#define CONSUMER_CONTROL_PLAY_PAUSE 4
|
|
||||||
#define CONSUMER_CONTROL_STOP 5
|
|
||||||
#define CONSUMER_CONTROL_PREV_TRACK 6
|
|
||||||
#define CONSUMER_CONTROL_NEXT_TRACK 7
|
|
||||||
#define CONSUMER_CONTROL_EJECT 8
|
|
||||||
|
|
||||||
// Low level key report: up to 6 keys and shift, ctrl etc at once
|
|
||||||
#define KEYREPORT_KEYCOUNT 0x06
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint8_t modifiers;
|
|
||||||
uint8_t reserved;
|
|
||||||
uint8_t keys[KEYREPORT_KEYCOUNT];
|
|
||||||
} KeyReport;
|
|
||||||
|
|
||||||
class Keyboard_ : public Print
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
KeyReport _keyReport;
|
|
||||||
void sendReport(KeyReport* keys);
|
|
||||||
public:
|
|
||||||
Keyboard_(void);
|
|
||||||
void begin(void);
|
|
||||||
void end(void);
|
|
||||||
virtual size_t write(uint8_t k);
|
|
||||||
virtual size_t press(uint8_t k);
|
|
||||||
virtual size_t release(uint8_t k);
|
|
||||||
virtual size_t writeKeycode(uint8_t k);
|
|
||||||
virtual size_t pressKeycode(uint8_t k);
|
|
||||||
virtual size_t releaseKeycode(uint8_t k);
|
|
||||||
virtual size_t addKeycodeToReport(uint8_t k);
|
|
||||||
virtual size_t removeKeycodeFromReport(uint8_t k);
|
|
||||||
virtual void releaseAll(void);
|
|
||||||
virtual size_t systemControl(uint8_t k);
|
|
||||||
virtual size_t consumerControl(uint8_t k);
|
|
||||||
void sendCurrentReport(void);
|
|
||||||
};
|
|
||||||
extern Keyboard_ Keyboard;
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// Low level API
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint8_t bmRequestType;
|
|
||||||
uint8_t bRequest;
|
|
||||||
uint8_t wValueL;
|
|
||||||
uint8_t wValueH;
|
|
||||||
uint16_t wIndex;
|
|
||||||
uint16_t wLength;
|
|
||||||
} Setup;
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// HID 'Driver'
|
|
||||||
|
|
||||||
int HID_GetInterface(uint8_t* interfaceNum);
|
|
||||||
int HID_GetDescriptor(int i);
|
|
||||||
bool HID_Setup(Setup& setup);
|
|
||||||
void HID_SendReport(uint8_t id, const void* data, int len);
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// MSC 'Driver'
|
|
||||||
|
|
||||||
int MSC_GetInterface(uint8_t* interfaceNum);
|
|
||||||
int MSC_GetDescriptor(int i);
|
|
||||||
bool MSC_Setup(Setup& setup);
|
|
||||||
bool MSC_Data(uint8_t rx,uint8_t tx);
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
// CSC 'Driver'
|
|
||||||
|
|
||||||
int CDC_GetInterface(uint8_t* interfaceNum);
|
|
||||||
int CDC_GetDescriptor(int i);
|
|
||||||
bool CDC_Setup(Setup& setup);
|
|
||||||
|
|
||||||
//================================================================================
|
|
||||||
//================================================================================
|
|
||||||
|
|
||||||
#define TRANSFER_PGM 0x80
|
|
||||||
#define TRANSFER_RELEASE 0x40
|
|
||||||
#define TRANSFER_ZERO 0x20
|
|
||||||
|
|
||||||
int USB_SendControl(uint8_t flags, const void* d, int len);
|
|
||||||
int USB_RecvControl(void* d, int len);
|
|
||||||
|
|
||||||
uint8_t USB_Available(uint8_t ep);
|
|
||||||
int USB_Send(uint8_t ep, const void* data, int len); // blocking
|
|
||||||
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
|
|
||||||
int USB_Recv(uint8_t ep); // non-blocking
|
|
||||||
void USB_Flush(uint8_t ep);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* if defined(USBCON) */
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,820 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
/* Copyright (c) 2010, Peter Barrett
|
|
||||||
**
|
|
||||||
** Sleep/Wakeup/SystemControl support added by Michael Dreher
|
|
||||||
**
|
|
||||||
** Permission to use, copy, modify, and/or distribute this software for
|
|
||||||
** any purpose with or without fee is hereby granted, provided that the
|
|
||||||
** above copyright notice and this permission notice appear in all copies.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
||||||
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
||||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
||||||
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
||||||
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
||||||
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
||||||
** SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "USBAPI.h"
|
|
||||||
|
|
||||||
#if defined(USBCON)
|
|
||||||
|
|
||||||
#define EP_TYPE_CONTROL (0x00)
|
|
||||||
#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))
|
|
||||||
#define EP_TYPE_BULK_OUT (1<<EPTYPE1)
|
|
||||||
#define EP_TYPE_INTERRUPT_IN ((1<<EPTYPE1) | (1<<EPTYPE0) | (1<<EPDIR))
|
|
||||||
#define EP_TYPE_INTERRUPT_OUT ((1<<EPTYPE1) | (1<<EPTYPE0))
|
|
||||||
#define EP_TYPE_ISOCHRONOUS_IN ((1<<EPTYPE0) | (1<<EPDIR))
|
|
||||||
#define EP_TYPE_ISOCHRONOUS_OUT (1<<EPTYPE0)
|
|
||||||
|
|
||||||
/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
|
|
||||||
#define TX_RX_LED_PULSE_MS 100
|
|
||||||
volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
|
|
||||||
volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
|
|
||||||
|
|
||||||
//==================================================================
|
|
||||||
//==================================================================
|
|
||||||
|
|
||||||
extern const u16 STRING_LANGUAGE[] PROGMEM;
|
|
||||||
extern const u8 STRING_PRODUCT[] PROGMEM;
|
|
||||||
extern const u8 STRING_MANUFACTURER[] PROGMEM;
|
|
||||||
extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
|
|
||||||
extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
|
|
||||||
|
|
||||||
const u16 STRING_LANGUAGE[2] = {
|
|
||||||
(3<<8) | (2+2),
|
|
||||||
0x0409 // English
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifndef USB_PRODUCT
|
|
||||||
// If no product is provided, use USB IO Board
|
|
||||||
#define USB_PRODUCT "USB IO Board"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT;
|
|
||||||
|
|
||||||
#if USB_VID == 0x1209
|
|
||||||
# if defined(USB_MANUFACTURER)
|
|
||||||
# undef USB_MANUFACTURER
|
|
||||||
# endif
|
|
||||||
# define USB_MANUFACTURER "Keyboardio"
|
|
||||||
#elif USB_VID == 0x1b4f
|
|
||||||
# if defined(USB_MANUFACTURER)
|
|
||||||
# undef USB_MANUFACTURER
|
|
||||||
# endif
|
|
||||||
# define USB_MANUFACTURER "SparkFun"
|
|
||||||
#elif !defined(USB_MANUFACTURER)
|
|
||||||
// Fall through to unknown if no manufacturer name was provided in a macro
|
|
||||||
# define USB_MANUFACTURER "Unknown"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
#define DEVICE_CLASS 0x02
|
|
||||||
#else
|
|
||||||
#define DEVICE_CLASS 0x00
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// DEVICE DESCRIPTOR
|
|
||||||
const DeviceDescriptor USB_DeviceDescriptor =
|
|
||||||
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
|
||||||
|
|
||||||
const DeviceDescriptor USB_DeviceDescriptorA =
|
|
||||||
D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
|
||||||
|
|
||||||
//==================================================================
|
|
||||||
//==================================================================
|
|
||||||
|
|
||||||
volatile u8 _usbConfiguration = 0;
|
|
||||||
volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device
|
|
||||||
volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits
|
|
||||||
|
|
||||||
static inline void WaitIN(void)
|
|
||||||
{
|
|
||||||
while (!(UEINTX & (1<<TXINI)))
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void ClearIN(void)
|
|
||||||
{
|
|
||||||
UEINTX = ~(1<<TXINI);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void WaitOUT(void)
|
|
||||||
{
|
|
||||||
while (!(UEINTX & (1<<RXOUTI)))
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 WaitForINOrOUT()
|
|
||||||
{
|
|
||||||
while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
|
|
||||||
;
|
|
||||||
return (UEINTX & (1<<RXOUTI)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void ClearOUT(void)
|
|
||||||
{
|
|
||||||
UEINTX = ~(1<<RXOUTI);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Recv(volatile u8* data, u8 count)
|
|
||||||
{
|
|
||||||
while (count--)
|
|
||||||
*data++ = UEDATX;
|
|
||||||
|
|
||||||
RXLED1; // light the RX LED
|
|
||||||
RxLEDPulse = TX_RX_LED_PULSE_MS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 Recv8()
|
|
||||||
{
|
|
||||||
RXLED1; // light the RX LED
|
|
||||||
RxLEDPulse = TX_RX_LED_PULSE_MS;
|
|
||||||
|
|
||||||
return UEDATX;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void Send8(u8 d)
|
|
||||||
{
|
|
||||||
UEDATX = d;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void SetEP(u8 ep)
|
|
||||||
{
|
|
||||||
UENUM = ep;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 FifoByteCount()
|
|
||||||
{
|
|
||||||
return UEBCLX;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 ReceivedSetupInt()
|
|
||||||
{
|
|
||||||
return UEINTX & (1<<RXSTPI);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void ClearSetupInt()
|
|
||||||
{
|
|
||||||
UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void Stall()
|
|
||||||
{
|
|
||||||
UECONX = (1<<STALLRQ) | (1<<EPEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 ReadWriteAllowed()
|
|
||||||
{
|
|
||||||
return UEINTX & (1<<RWAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 Stalled()
|
|
||||||
{
|
|
||||||
return UEINTX & (1<<STALLEDI);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 FifoFree()
|
|
||||||
{
|
|
||||||
return UEINTX & (1<<FIFOCON);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void ReleaseRX()
|
|
||||||
{
|
|
||||||
UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void ReleaseTX()
|
|
||||||
{
|
|
||||||
UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u8 FrameNumber()
|
|
||||||
{
|
|
||||||
return UDFNUML;
|
|
||||||
}
|
|
||||||
|
|
||||||
//==================================================================
|
|
||||||
//==================================================================
|
|
||||||
|
|
||||||
u8 USBGetConfiguration(void)
|
|
||||||
{
|
|
||||||
return _usbConfiguration;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define USB_RECV_TIMEOUT
|
|
||||||
class LockEP
|
|
||||||
{
|
|
||||||
u8 _sreg;
|
|
||||||
public:
|
|
||||||
LockEP(u8 ep) : _sreg(SREG)
|
|
||||||
{
|
|
||||||
cli();
|
|
||||||
SetEP(ep & 7);
|
|
||||||
}
|
|
||||||
~LockEP()
|
|
||||||
{
|
|
||||||
SREG = _sreg;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Number of bytes, assumes a rx endpoint
|
|
||||||
u8 USB_Available(u8 ep)
|
|
||||||
{
|
|
||||||
LockEP lock(ep);
|
|
||||||
return FifoByteCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non Blocking receive
|
|
||||||
// Return number of bytes read
|
|
||||||
int USB_Recv(u8 ep, void* d, int len)
|
|
||||||
{
|
|
||||||
if (!_usbConfiguration || len < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
LockEP lock(ep);
|
|
||||||
u8 n = FifoByteCount();
|
|
||||||
len = min(n,len);
|
|
||||||
n = len;
|
|
||||||
u8* dst = (u8*)d;
|
|
||||||
while (n--)
|
|
||||||
*dst++ = Recv8();
|
|
||||||
if (len && !FifoByteCount()) // release empty buffer
|
|
||||||
ReleaseRX();
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recv 1 byte if ready
|
|
||||||
int USB_Recv(u8 ep)
|
|
||||||
{
|
|
||||||
u8 c;
|
|
||||||
if (USB_Recv(ep,&c,1) != 1)
|
|
||||||
return -1;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Space in send EP
|
|
||||||
u8 USB_SendSpace(u8 ep)
|
|
||||||
{
|
|
||||||
LockEP lock(ep);
|
|
||||||
if (!ReadWriteAllowed())
|
|
||||||
return 0;
|
|
||||||
return 64 - FifoByteCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blocking Send of data to an endpoint
|
|
||||||
int USB_Send(u8 ep, const void* d, int len)
|
|
||||||
{
|
|
||||||
if (!_usbConfiguration)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
int r = len;
|
|
||||||
const u8* data = (const u8*)d;
|
|
||||||
u8 timeout = 250; // 250ms timeout on send? TODO
|
|
||||||
while (len)
|
|
||||||
{
|
|
||||||
u8 n = USB_SendSpace(ep);
|
|
||||||
if (n == 0)
|
|
||||||
{
|
|
||||||
if (!(--timeout))
|
|
||||||
return -1;
|
|
||||||
delay(1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n > len)
|
|
||||||
n = len;
|
|
||||||
{
|
|
||||||
LockEP lock(ep);
|
|
||||||
// Frame may have been released by the SOF interrupt handler
|
|
||||||
if (!ReadWriteAllowed())
|
|
||||||
continue;
|
|
||||||
len -= n;
|
|
||||||
if (ep & TRANSFER_ZERO)
|
|
||||||
{
|
|
||||||
while (n--)
|
|
||||||
Send8(0);
|
|
||||||
}
|
|
||||||
else if (ep & TRANSFER_PGM)
|
|
||||||
{
|
|
||||||
while (n--)
|
|
||||||
Send8(pgm_read_byte(data++));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (n--)
|
|
||||||
Send8(*data++);
|
|
||||||
}
|
|
||||||
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
|
|
||||||
ReleaseTX();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TXLED1; // light the TX LED
|
|
||||||
TxLEDPulse = TX_RX_LED_PULSE_MS;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern const u8 _initEndpoints[] PROGMEM;
|
|
||||||
const u8 _initEndpoints[] =
|
|
||||||
{
|
|
||||||
0,
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
|
|
||||||
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
|
|
||||||
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
#define EP_SINGLE_64 0x32 // EP0
|
|
||||||
#define EP_DOUBLE_64 0x36 // Other endpoints
|
|
||||||
|
|
||||||
static
|
|
||||||
void InitEP(u8 index, u8 type, u8 size)
|
|
||||||
{
|
|
||||||
UENUM = index;
|
|
||||||
UECONX = (1<<EPEN);
|
|
||||||
UECFG0X = type;
|
|
||||||
UECFG1X = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
static
|
|
||||||
void InitEndpoints()
|
|
||||||
{
|
|
||||||
for (u8 i = 1; i < sizeof(_initEndpoints); i++)
|
|
||||||
{
|
|
||||||
UENUM = i;
|
|
||||||
UECONX = (1<<EPEN);
|
|
||||||
UECFG0X = pgm_read_byte(_initEndpoints+i);
|
|
||||||
UECFG1X = EP_DOUBLE_64;
|
|
||||||
}
|
|
||||||
UERST = 0x7E; // And reset them
|
|
||||||
UERST = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle CLASS_INTERFACE requests
|
|
||||||
static
|
|
||||||
bool ClassInterfaceRequest(Setup& setup)
|
|
||||||
{
|
|
||||||
u8 i = setup.wIndex;
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
if (CDC_ACM_INTERFACE == i)
|
|
||||||
return CDC_Setup(setup);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
if (HID_INTERFACE == i)
|
|
||||||
return HID_Setup(setup);
|
|
||||||
#endif
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int _cmark;
|
|
||||||
int _cend;
|
|
||||||
void InitControl(int end)
|
|
||||||
{
|
|
||||||
SetEP(0);
|
|
||||||
_cmark = 0;
|
|
||||||
_cend = end;
|
|
||||||
}
|
|
||||||
|
|
||||||
static
|
|
||||||
bool SendControl(u8 d)
|
|
||||||
{
|
|
||||||
if (_cmark < _cend)
|
|
||||||
{
|
|
||||||
if (!WaitForINOrOUT())
|
|
||||||
return false;
|
|
||||||
Send8(d);
|
|
||||||
if (!((_cmark + 1) & 0x3F))
|
|
||||||
ClearIN(); // Fifo is full, release this packet
|
|
||||||
}
|
|
||||||
_cmark++;
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Clipped by _cmark/_cend
|
|
||||||
int USB_SendControl(u8 flags, const void* d, int len)
|
|
||||||
{
|
|
||||||
int sent = len;
|
|
||||||
const u8* data = (const u8*)d;
|
|
||||||
bool pgm = flags & TRANSFER_PGM;
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
u8 c = pgm ? pgm_read_byte(data++) : *data++;
|
|
||||||
if (!SendControl(c))
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return sent;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a USB descriptor string. The string is stored in PROGMEM as a
|
|
||||||
// plain ASCII string but is sent out as UTF-16 with the correct 2-byte
|
|
||||||
// prefix
|
|
||||||
static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len) {
|
|
||||||
SendControl(2 + string_len * 2);
|
|
||||||
SendControl(3);
|
|
||||||
for(u8 i = 0; i < string_len; i++) {
|
|
||||||
bool r = SendControl(pgm_read_byte(&string_P[i]));
|
|
||||||
r &= SendControl(0); // high byte
|
|
||||||
if(!r) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Does not timeout or cross fifo boundaries
|
|
||||||
// Will only work for transfers <= 64 bytes
|
|
||||||
// TODO
|
|
||||||
int USB_RecvControl(void* d, int len)
|
|
||||||
{
|
|
||||||
WaitOUT();
|
|
||||||
Recv((u8*)d,len);
|
|
||||||
ClearOUT();
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SendInterfaces()
|
|
||||||
{
|
|
||||||
int total = 0;
|
|
||||||
u8 interfaces = 0;
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
total = CDC_GetInterface(&interfaces);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
total += HID_GetInterface(&interfaces);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return interfaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct a dynamic configuration descriptor
|
|
||||||
// This really needs dynamic endpoint allocation etc
|
|
||||||
// TODO
|
|
||||||
static
|
|
||||||
bool SendConfiguration(int maxlen)
|
|
||||||
{
|
|
||||||
// Count and measure interfaces
|
|
||||||
InitControl(0);
|
|
||||||
int interfaces = SendInterfaces();
|
|
||||||
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
|
|
||||||
|
|
||||||
// Now send them
|
|
||||||
InitControl(maxlen);
|
|
||||||
USB_SendControl(0,&config,sizeof(ConfigDescriptor));
|
|
||||||
SendInterfaces();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
u8 _cdcComposite = 0;
|
|
||||||
|
|
||||||
static
|
|
||||||
bool SendDescriptor(Setup& setup)
|
|
||||||
{
|
|
||||||
u8 t = setup.wValueH;
|
|
||||||
if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
|
|
||||||
return SendConfiguration(setup.wLength);
|
|
||||||
|
|
||||||
InitControl(setup.wLength);
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
if (HID_REPORT_DESCRIPTOR_TYPE == t)
|
|
||||||
return HID_GetDescriptor(t);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const u8* desc_addr = 0;
|
|
||||||
if (USB_DEVICE_DESCRIPTOR_TYPE == t)
|
|
||||||
{
|
|
||||||
if (setup.wLength == 8)
|
|
||||||
_cdcComposite = 1;
|
|
||||||
desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
|
|
||||||
}
|
|
||||||
else if (USB_STRING_DESCRIPTOR_TYPE == t)
|
|
||||||
{
|
|
||||||
if (setup.wValueL == 0) {
|
|
||||||
desc_addr = (const u8*)&STRING_LANGUAGE;
|
|
||||||
}
|
|
||||||
else if (setup.wValueL == IPRODUCT) {
|
|
||||||
return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT));
|
|
||||||
}
|
|
||||||
else if (setup.wValueL == IMANUFACTURER) {
|
|
||||||
return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (desc_addr == 0)
|
|
||||||
return false;
|
|
||||||
u8 desc_length = pgm_read_byte(desc_addr);
|
|
||||||
|
|
||||||
USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Endpoint 0 interrupt
|
|
||||||
ISR(USB_COM_vect)
|
|
||||||
{
|
|
||||||
SetEP(0);
|
|
||||||
if (!ReceivedSetupInt())
|
|
||||||
return;
|
|
||||||
|
|
||||||
Setup setup;
|
|
||||||
Recv((u8*)&setup,8);
|
|
||||||
ClearSetupInt();
|
|
||||||
|
|
||||||
u8 requestType = setup.bmRequestType;
|
|
||||||
if (requestType & REQUEST_DEVICETOHOST)
|
|
||||||
WaitIN();
|
|
||||||
else
|
|
||||||
ClearIN();
|
|
||||||
|
|
||||||
bool ok = true;
|
|
||||||
if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
|
|
||||||
{
|
|
||||||
// Standard Requests
|
|
||||||
u8 r = setup.bRequest;
|
|
||||||
u16 wValue = setup.wValueL | (setup.wValueH << 8);
|
|
||||||
if (GET_STATUS == r)
|
|
||||||
{
|
|
||||||
if(requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE))
|
|
||||||
{
|
|
||||||
Send8(_usbCurrentStatus);
|
|
||||||
Send8(0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO: handle the HALT state of an endpoint here
|
|
||||||
// see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information
|
|
||||||
Send8(0);
|
|
||||||
Send8(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (CLEAR_FEATURE == r)
|
|
||||||
{
|
|
||||||
if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
|
|
||||||
&& (wValue == DEVICE_REMOTE_WAKEUP))
|
|
||||||
{
|
|
||||||
_usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (SET_FEATURE == r)
|
|
||||||
{
|
|
||||||
if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE))
|
|
||||||
&& (wValue == DEVICE_REMOTE_WAKEUP))
|
|
||||||
{
|
|
||||||
_usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (SET_ADDRESS == r)
|
|
||||||
{
|
|
||||||
WaitIN();
|
|
||||||
UDADDR = setup.wValueL | (1<<ADDEN);
|
|
||||||
}
|
|
||||||
else if (GET_DESCRIPTOR == r)
|
|
||||||
{
|
|
||||||
ok = SendDescriptor(setup);
|
|
||||||
}
|
|
||||||
else if (SET_DESCRIPTOR == r)
|
|
||||||
{
|
|
||||||
ok = false;
|
|
||||||
}
|
|
||||||
else if (GET_CONFIGURATION == r)
|
|
||||||
{
|
|
||||||
Send8(1);
|
|
||||||
}
|
|
||||||
else if (SET_CONFIGURATION == r)
|
|
||||||
{
|
|
||||||
if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
|
|
||||||
{
|
|
||||||
InitEndpoints();
|
|
||||||
_usbConfiguration = setup.wValueL;
|
|
||||||
} else
|
|
||||||
ok = false;
|
|
||||||
}
|
|
||||||
else if (GET_INTERFACE == r)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else if (SET_INTERFACE == r)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
InitControl(setup.wLength); // Max length of transfer
|
|
||||||
ok = ClassInterfaceRequest(setup);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ok)
|
|
||||||
ClearIN();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Stall();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void USB_Flush(u8 ep)
|
|
||||||
{
|
|
||||||
SetEP(ep);
|
|
||||||
if (FifoByteCount())
|
|
||||||
ReleaseTX();
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void USB_ClockDisable()
|
|
||||||
{
|
|
||||||
USBCON = (USBCON & ~(1<<OTGPADE)) | (1<<FRZCLK); // freeze clock and disable VBUS Pad
|
|
||||||
PLLCSR &= ~(1<<PLLE); // stop PLL
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void USB_ClockEnable()
|
|
||||||
{
|
|
||||||
UHWCON |= (1<<UVREGE); // power internal reg
|
|
||||||
USBCON = (1<<USBE) | (1<<FRZCLK); // clock frozen, usb enabled
|
|
||||||
|
|
||||||
// ATmega32U4
|
|
||||||
#if defined(PINDIV)
|
|
||||||
#if F_CPU == 16000000UL
|
|
||||||
PLLCSR |= (1<<PINDIV); // Need 16 MHz xtal
|
|
||||||
#elif F_CPU == 8000000UL
|
|
||||||
PLLCSR &= ~(1<<PINDIV); // Need 8 MHz xtal
|
|
||||||
#else
|
|
||||||
#error "Clock rate of F_CPU not supported"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// AT90USB646, AT90USB647, AT90USB1286, AT90USB1287
|
|
||||||
#elif defined(PLLP2)
|
|
||||||
#if F_CPU == 16000000UL
|
|
||||||
#if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
|
|
||||||
// For Atmel AT90USB128x only. Do not use with Atmel AT90USB64x.
|
|
||||||
PLLCSR = (PLLCSR & ~(1<<PLLP1)) | ((1<<PLLP2) | (1<<PLLP0)); // Need 16 MHz xtal
|
|
||||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
|
|
||||||
// For AT90USB64x only. Do not use with AT90USB128x.
|
|
||||||
PLLCSR = (PLLCSR & ~(1<<PLLP0)) | ((1<<PLLP2) | (1<<PLLP1)); // Need 16 MHz xtal
|
|
||||||
#else
|
|
||||||
#error "USB Chip not supported, please defined method of USB PLL initialization"
|
|
||||||
#endif
|
|
||||||
#elif F_CPU == 8000000UL
|
|
||||||
// for Atmel AT90USB128x and AT90USB64x
|
|
||||||
PLLCSR = (PLLCSR & ~(1<<PLLP2)) | ((1<<PLLP1) | (1<<PLLP0)); // Need 8 MHz xtal
|
|
||||||
#else
|
|
||||||
#error "Clock rate of F_CPU not supported"
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#error "USB Chip not supported, please defined method of USB PLL initialization"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PLLCSR |= (1<<PLLE);
|
|
||||||
while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some tests on specific versions of macosx (10.7.3), reported some
|
|
||||||
// strange behaviors when the board is reset using the serial
|
|
||||||
// port touch at 1200 bps. This delay fixes this behavior.
|
|
||||||
delay(1);
|
|
||||||
USBCON = (USBCON & ~(1<<FRZCLK)) | (1<<OTGPADE); // start USB clock, enable VBUS Pad
|
|
||||||
|
|
||||||
#if defined(RSTCPU)
|
|
||||||
UDCON &= ~((1<<RSTCPU) | (1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
|
|
||||||
#else
|
|
||||||
// AT90USB64x and AT90USB128x don't have RSTCPU
|
|
||||||
UDCON &= ~((1<<LSM) | (1<<RMWKUP) | (1<<DETACH)); // enable attach resistor, set full speed mode
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// General interrupt
|
|
||||||
ISR(USB_GEN_vect)
|
|
||||||
{
|
|
||||||
u8 udint = UDINT;
|
|
||||||
UDINT &= ~((1<<EORSTI) | (1<<SOFI)); // clear the IRQ flags for the IRQs which are handled here, except WAKEUPI and SUSPI (see below)
|
|
||||||
|
|
||||||
// End of Reset
|
|
||||||
if (udint & (1<<EORSTI))
|
|
||||||
{
|
|
||||||
InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
|
|
||||||
_usbConfiguration = 0; // not configured yet
|
|
||||||
UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
|
|
||||||
if (udint & (1<<SOFI))
|
|
||||||
{
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
USB_Flush(CDC_TX); // Send a tx frame if found
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// check whether the one-shot period has elapsed. if so, turn off the LED
|
|
||||||
if (TxLEDPulse && !(--TxLEDPulse))
|
|
||||||
TXLED0;
|
|
||||||
if (RxLEDPulse && !(--RxLEDPulse))
|
|
||||||
RXLED0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the WAKEUPI interrupt is triggered as soon as there are non-idle patterns on the data
|
|
||||||
// lines. Thus, the WAKEUPI interrupt can occur even if the controller is not in the "suspend" mode.
|
|
||||||
// Therefore the we enable it only when USB is suspended
|
|
||||||
if (udint & (1<<WAKEUPI))
|
|
||||||
{
|
|
||||||
UDIEN = (UDIEN & ~(1<<WAKEUPE)) | (1<<SUSPE); // Disable interrupts for WAKEUP and enable interrupts for SUSPEND
|
|
||||||
|
|
||||||
//TODO
|
|
||||||
// WAKEUPI shall be cleared by software (USB clock inputs must be enabled before).
|
|
||||||
//USB_ClockEnable();
|
|
||||||
UDINT &= ~(1<<WAKEUPI);
|
|
||||||
_usbSuspendState = (_usbSuspendState & ~(1<<SUSPI)) | (1<<WAKEUPI);
|
|
||||||
}
|
|
||||||
else if (udint & (1<<SUSPI)) // only one of the WAKEUPI / SUSPI bits can be active at time
|
|
||||||
{
|
|
||||||
UDIEN = (UDIEN & ~(1<<SUSPE)) | (1<<WAKEUPE); // Disable interrupts for SUSPEND and enable interrupts for WAKEUP
|
|
||||||
|
|
||||||
//TODO
|
|
||||||
//USB_ClockDisable();
|
|
||||||
|
|
||||||
UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear any already pending WAKEUP IRQs and the SUSPI request
|
|
||||||
_usbSuspendState = (_usbSuspendState & ~(1<<WAKEUPI)) | (1<<SUSPI);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// VBUS or counting frames
|
|
||||||
// Any frame counting?
|
|
||||||
u8 USBConnected()
|
|
||||||
{
|
|
||||||
u8 f = UDFNUML;
|
|
||||||
delay(3);
|
|
||||||
return f != UDFNUML;
|
|
||||||
}
|
|
||||||
|
|
||||||
//=======================================================================
|
|
||||||
//=======================================================================
|
|
||||||
|
|
||||||
USBDevice_ USBDevice;
|
|
||||||
|
|
||||||
USBDevice_::USBDevice_()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void USBDevice_::attach()
|
|
||||||
{
|
|
||||||
_usbConfiguration = 0;
|
|
||||||
_usbCurrentStatus = 0;
|
|
||||||
_usbSuspendState = 0;
|
|
||||||
USB_ClockEnable();
|
|
||||||
|
|
||||||
UDINT &= ~((1<<WAKEUPI) | (1<<SUSPI)); // clear already pending WAKEUP / SUSPEND requests
|
|
||||||
UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
|
|
||||||
|
|
||||||
TX_RX_LED_INIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
void USBDevice_::detach()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for interrupts
|
|
||||||
// TODO: VBUS detection
|
|
||||||
bool USBDevice_::configured()
|
|
||||||
{
|
|
||||||
return _usbConfiguration;
|
|
||||||
}
|
|
||||||
|
|
||||||
void USBDevice_::poll()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool USBDevice_::wakeupHost()
|
|
||||||
{
|
|
||||||
// clear any previous wakeup request which might have been set but could be processed at that time
|
|
||||||
// e.g. because the host was not suspended at that time
|
|
||||||
UDCON &= ~(1 << RMWKUP);
|
|
||||||
|
|
||||||
if(!(UDCON & (1 << RMWKUP))
|
|
||||||
&& (_usbSuspendState & (1<<SUSPI))
|
|
||||||
&& (_usbCurrentStatus & FEATURE_REMOTE_WAKEUP_ENABLED))
|
|
||||||
{
|
|
||||||
// This short version will only work, when the device has not been suspended. Currently the
|
|
||||||
// Arduino core doesn't handle SUSPEND at all, so this is ok.
|
|
||||||
USB_ClockEnable();
|
|
||||||
UDCON |= (1 << RMWKUP); // send the wakeup request
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* if defined(USBCON) */
|
|
@ -1,312 +0,0 @@
|
|||||||
|
|
||||||
// Copyright (c) 2010, Peter Barrett
|
|
||||||
/*
|
|
||||||
** Permission to use, copy, modify, and/or distribute this software for
|
|
||||||
** any purpose with or without fee is hereby granted, provided that the
|
|
||||||
** above copyright notice and this permission notice appear in all copies.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
||||||
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
||||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
||||||
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
||||||
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
||||||
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
||||||
** SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __USBCORE_H__
|
|
||||||
#define __USBCORE_H__
|
|
||||||
|
|
||||||
// Standard requests
|
|
||||||
#define GET_STATUS 0
|
|
||||||
#define CLEAR_FEATURE 1
|
|
||||||
#define SET_FEATURE 3
|
|
||||||
#define SET_ADDRESS 5
|
|
||||||
#define GET_DESCRIPTOR 6
|
|
||||||
#define SET_DESCRIPTOR 7
|
|
||||||
#define GET_CONFIGURATION 8
|
|
||||||
#define SET_CONFIGURATION 9
|
|
||||||
#define GET_INTERFACE 10
|
|
||||||
#define SET_INTERFACE 11
|
|
||||||
|
|
||||||
|
|
||||||
// bmRequestType
|
|
||||||
#define REQUEST_HOSTTODEVICE 0x00
|
|
||||||
#define REQUEST_DEVICETOHOST 0x80
|
|
||||||
#define REQUEST_DIRECTION 0x80
|
|
||||||
|
|
||||||
#define REQUEST_STANDARD 0x00
|
|
||||||
#define REQUEST_CLASS 0x20
|
|
||||||
#define REQUEST_VENDOR 0x40
|
|
||||||
#define REQUEST_TYPE 0x60
|
|
||||||
|
|
||||||
#define REQUEST_DEVICE 0x00
|
|
||||||
#define REQUEST_INTERFACE 0x01
|
|
||||||
#define REQUEST_ENDPOINT 0x02
|
|
||||||
#define REQUEST_OTHER 0x03
|
|
||||||
#define REQUEST_RECIPIENT 0x03
|
|
||||||
|
|
||||||
#define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
|
|
||||||
#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
|
|
||||||
|
|
||||||
// Class requests
|
|
||||||
|
|
||||||
#define CDC_SET_LINE_CODING 0x20
|
|
||||||
#define CDC_GET_LINE_CODING 0x21
|
|
||||||
#define CDC_SET_CONTROL_LINE_STATE 0x22
|
|
||||||
|
|
||||||
#define MSC_RESET 0xFF
|
|
||||||
#define MSC_GET_MAX_LUN 0xFE
|
|
||||||
|
|
||||||
#define HID_GET_REPORT 0x01
|
|
||||||
#define HID_GET_IDLE 0x02
|
|
||||||
#define HID_GET_PROTOCOL 0x03
|
|
||||||
#define HID_SET_REPORT 0x09
|
|
||||||
#define HID_SET_IDLE 0x0A
|
|
||||||
#define HID_SET_PROTOCOL 0x0B
|
|
||||||
|
|
||||||
// Descriptors
|
|
||||||
|
|
||||||
#define USB_DEVICE_DESC_SIZE 18
|
|
||||||
#define USB_CONFIGUARTION_DESC_SIZE 9
|
|
||||||
#define USB_INTERFACE_DESC_SIZE 9
|
|
||||||
#define USB_ENDPOINT_DESC_SIZE 7
|
|
||||||
|
|
||||||
#define USB_DEVICE_DESCRIPTOR_TYPE 1
|
|
||||||
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
|
|
||||||
#define USB_STRING_DESCRIPTOR_TYPE 3
|
|
||||||
#define USB_INTERFACE_DESCRIPTOR_TYPE 4
|
|
||||||
#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
|
|
||||||
|
|
||||||
// usb_20.pdf Table 9.6 Standard Feature Selectors
|
|
||||||
#define DEVICE_REMOTE_WAKEUP 1
|
|
||||||
#define ENDPOINT_HALT 2
|
|
||||||
#define TEST_MODE 3
|
|
||||||
|
|
||||||
// usb_20.pdf Figure 9-4. Information Returned by a GetStatus() Request to a Device
|
|
||||||
#define FEATURE_SELFPOWERED_ENABLED (1 << 0)
|
|
||||||
#define FEATURE_REMOTE_WAKEUP_ENABLED (1 << 1)
|
|
||||||
|
|
||||||
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
|
|
||||||
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
|
|
||||||
#define USB_DEVICE_CLASS_STORAGE 0x08
|
|
||||||
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
|
|
||||||
|
|
||||||
#define USB_CONFIG_POWERED_MASK 0x40
|
|
||||||
#define USB_CONFIG_BUS_POWERED 0x80
|
|
||||||
#define USB_CONFIG_SELF_POWERED 0xC0
|
|
||||||
#define USB_CONFIG_REMOTE_WAKEUP 0x20
|
|
||||||
|
|
||||||
// bMaxPower in Configuration Descriptor
|
|
||||||
#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
|
|
||||||
|
|
||||||
// bEndpointAddress in Endpoint Descriptor
|
|
||||||
#define USB_ENDPOINT_DIRECTION_MASK 0x80
|
|
||||||
#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
|
|
||||||
#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
|
|
||||||
|
|
||||||
#define USB_ENDPOINT_TYPE_MASK 0x03
|
|
||||||
#define USB_ENDPOINT_TYPE_CONTROL 0x00
|
|
||||||
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
|
|
||||||
#define USB_ENDPOINT_TYPE_BULK 0x02
|
|
||||||
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
|
|
||||||
|
|
||||||
#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
|
|
||||||
|
|
||||||
#define CDC_V1_10 0x0110
|
|
||||||
#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
|
|
||||||
|
|
||||||
#define CDC_CALL_MANAGEMENT 0x01
|
|
||||||
#define CDC_ABSTRACT_CONTROL_MODEL 0x02
|
|
||||||
#define CDC_HEADER 0x00
|
|
||||||
#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
|
|
||||||
#define CDC_UNION 0x06
|
|
||||||
#define CDC_CS_INTERFACE 0x24
|
|
||||||
#define CDC_CS_ENDPOINT 0x25
|
|
||||||
#define CDC_DATA_INTERFACE_CLASS 0x0A
|
|
||||||
|
|
||||||
#define MSC_SUBCLASS_SCSI 0x06
|
|
||||||
#define MSC_PROTOCOL_BULK_ONLY 0x50
|
|
||||||
|
|
||||||
#define HID_HID_DESCRIPTOR_TYPE 0x21
|
|
||||||
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
|
|
||||||
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
|
|
||||||
|
|
||||||
|
|
||||||
// Device
|
|
||||||
typedef struct {
|
|
||||||
u8 len; // 18
|
|
||||||
u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
|
|
||||||
u16 usbVersion; // 0x200
|
|
||||||
u8 deviceClass;
|
|
||||||
u8 deviceSubClass;
|
|
||||||
u8 deviceProtocol;
|
|
||||||
u8 packetSize0; // Packet 0
|
|
||||||
u16 idVendor;
|
|
||||||
u16 idProduct;
|
|
||||||
u16 deviceVersion; // 0x100
|
|
||||||
u8 iManufacturer;
|
|
||||||
u8 iProduct;
|
|
||||||
u8 iSerialNumber;
|
|
||||||
u8 bNumConfigurations;
|
|
||||||
} DeviceDescriptor;
|
|
||||||
|
|
||||||
// Config
|
|
||||||
typedef struct {
|
|
||||||
u8 len; // 9
|
|
||||||
u8 dtype; // 2
|
|
||||||
u16 clen; // total length
|
|
||||||
u8 numInterfaces;
|
|
||||||
u8 config;
|
|
||||||
u8 iconfig;
|
|
||||||
u8 attributes;
|
|
||||||
u8 maxPower;
|
|
||||||
} ConfigDescriptor;
|
|
||||||
|
|
||||||
// String
|
|
||||||
|
|
||||||
// Interface
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 9
|
|
||||||
u8 dtype; // 4
|
|
||||||
u8 number;
|
|
||||||
u8 alternate;
|
|
||||||
u8 numEndpoints;
|
|
||||||
u8 interfaceClass;
|
|
||||||
u8 interfaceSubClass;
|
|
||||||
u8 protocol;
|
|
||||||
u8 iInterface;
|
|
||||||
} InterfaceDescriptor;
|
|
||||||
|
|
||||||
// Endpoint
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 7
|
|
||||||
u8 dtype; // 5
|
|
||||||
u8 addr;
|
|
||||||
u8 attr;
|
|
||||||
u16 packetSize;
|
|
||||||
u8 interval;
|
|
||||||
} EndpointDescriptor;
|
|
||||||
|
|
||||||
// Interface Association Descriptor
|
|
||||||
// Used to bind 2 interfaces together in CDC compostite device
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 8
|
|
||||||
u8 dtype; // 11
|
|
||||||
u8 firstInterface;
|
|
||||||
u8 interfaceCount;
|
|
||||||
u8 functionClass;
|
|
||||||
u8 funtionSubClass;
|
|
||||||
u8 functionProtocol;
|
|
||||||
u8 iInterface;
|
|
||||||
} IADDescriptor;
|
|
||||||
|
|
||||||
// CDC CS interface descriptor
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 5
|
|
||||||
u8 dtype; // 0x24
|
|
||||||
u8 subtype;
|
|
||||||
u8 d0;
|
|
||||||
u8 d1;
|
|
||||||
} CDCCSInterfaceDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 4
|
|
||||||
u8 dtype; // 0x24
|
|
||||||
u8 subtype;
|
|
||||||
u8 d0;
|
|
||||||
} CDCCSInterfaceDescriptor4;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len;
|
|
||||||
u8 dtype; // 0x24
|
|
||||||
u8 subtype; // 1
|
|
||||||
u8 bmCapabilities;
|
|
||||||
u8 bDataInterface;
|
|
||||||
} CMFunctionalDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len;
|
|
||||||
u8 dtype; // 0x24
|
|
||||||
u8 subtype; // 1
|
|
||||||
u8 bmCapabilities;
|
|
||||||
} ACMFunctionalDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
// IAD
|
|
||||||
IADDescriptor iad; // Only needed on compound device
|
|
||||||
|
|
||||||
// Control
|
|
||||||
InterfaceDescriptor cif; //
|
|
||||||
CDCCSInterfaceDescriptor header;
|
|
||||||
CMFunctionalDescriptor callManagement; // Call Management
|
|
||||||
ACMFunctionalDescriptor controlManagement; // ACM
|
|
||||||
CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
|
|
||||||
EndpointDescriptor cifin;
|
|
||||||
|
|
||||||
// Data
|
|
||||||
InterfaceDescriptor dif;
|
|
||||||
EndpointDescriptor in;
|
|
||||||
EndpointDescriptor out;
|
|
||||||
} CDCDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
InterfaceDescriptor msc;
|
|
||||||
EndpointDescriptor in;
|
|
||||||
EndpointDescriptor out;
|
|
||||||
} MSCDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
u8 len; // 9
|
|
||||||
u8 dtype; // 0x21
|
|
||||||
u8 addr;
|
|
||||||
u8 versionL; // 0x101
|
|
||||||
u8 versionH; // 0x101
|
|
||||||
u8 country;
|
|
||||||
u8 desctype; // 0x22 report
|
|
||||||
u8 descLenL;
|
|
||||||
u8 descLenH;
|
|
||||||
} HIDDescDescriptor;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
InterfaceDescriptor hid;
|
|
||||||
HIDDescDescriptor desc;
|
|
||||||
EndpointDescriptor in;
|
|
||||||
} HIDDescriptor;
|
|
||||||
|
|
||||||
|
|
||||||
#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
|
|
||||||
{ 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
|
|
||||||
|
|
||||||
#define D_CONFIG(_totalLength,_interfaces) \
|
|
||||||
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(500) }
|
|
||||||
|
|
||||||
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
|
|
||||||
{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
|
|
||||||
|
|
||||||
#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
|
|
||||||
{ 7, 5, _addr,_attr,_packetSize, _interval }
|
|
||||||
|
|
||||||
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
|
|
||||||
{ 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
|
|
||||||
|
|
||||||
#define D_HIDREPORT(_descriptorLength) \
|
|
||||||
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
|
|
||||||
|
|
||||||
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
|
|
||||||
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,63 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
/* Copyright (c) 2011, Peter Barrett
|
|
||||||
**
|
|
||||||
** Permission to use, copy, modify, and/or distribute this software for
|
|
||||||
** any purpose with or without fee is hereby granted, provided that the
|
|
||||||
** above copyright notice and this permission notice appear in all copies.
|
|
||||||
**
|
|
||||||
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
||||||
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
||||||
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
||||||
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
||||||
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
||||||
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
||||||
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
||||||
** SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define CDC_ENABLED
|
|
||||||
#define HID_ENABLED
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
#define CDC_INTERFACE_COUNT 2
|
|
||||||
#define CDC_ENPOINT_COUNT 3
|
|
||||||
#else
|
|
||||||
#define CDC_INTERFACE_COUNT 0
|
|
||||||
#define CDC_ENPOINT_COUNT 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
#define HID_INTERFACE_COUNT 1
|
|
||||||
#define HID_ENPOINT_COUNT 1
|
|
||||||
#else
|
|
||||||
#define HID_INTERFACE_COUNT 0
|
|
||||||
#define HID_ENPOINT_COUNT 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define CDC_ACM_INTERFACE 0 // CDC ACM
|
|
||||||
#define CDC_DATA_INTERFACE 1 // CDC Data
|
|
||||||
#define CDC_FIRST_ENDPOINT 1
|
|
||||||
#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
|
|
||||||
#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
|
|
||||||
#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
|
|
||||||
|
|
||||||
#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
|
|
||||||
#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
|
|
||||||
#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
|
|
||||||
|
|
||||||
#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)
|
|
||||||
|
|
||||||
#ifdef CDC_ENABLED
|
|
||||||
#define CDC_RX CDC_ENDPOINT_OUT
|
|
||||||
#define CDC_TX CDC_ENDPOINT_IN
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HID_ENABLED
|
|
||||||
#define HID_TX HID_ENDPOINT_INT
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define IMANUFACTURER 1
|
|
||||||
#define IPRODUCT 2
|
|
||||||
|
|
@ -1,88 +0,0 @@
|
|||||||
/*
|
|
||||||
* Udp.cpp: Library to send/receive UDP packets.
|
|
||||||
*
|
|
||||||
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
|
||||||
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
|
||||||
* might not happen often in practice, but in larger network topologies, a UDP
|
|
||||||
* packet can be received out of sequence.
|
|
||||||
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
|
||||||
* aware of it. Again, this may not be a concern in practice on small local networks.
|
|
||||||
* For more information, see http://www.cafeaulait.org/course/week12/35.html
|
|
||||||
*
|
|
||||||
* MIT License:
|
|
||||||
* Copyright (c) 2008 Bjoern Hartmann
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*
|
|
||||||
* bjoern@cs.stanford.edu 12/30/2008
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef udp_h
|
|
||||||
#define udp_h
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <IPAddress.h>
|
|
||||||
|
|
||||||
class UDP : public Stream {
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
|
||||||
virtual void stop() =0; // Finish with the UDP socket
|
|
||||||
|
|
||||||
// Sending UDP packets
|
|
||||||
|
|
||||||
// Start building up a packet to send to the remote host specific in ip and port
|
|
||||||
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
|
|
||||||
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
|
|
||||||
// Start building up a packet to send to the remote host specific in host and port
|
|
||||||
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
|
|
||||||
virtual int beginPacket(const char *host, uint16_t port) =0;
|
|
||||||
// Finish off this packet and send it
|
|
||||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
|
||||||
virtual int endPacket() =0;
|
|
||||||
// Write a single byte into the packet
|
|
||||||
virtual size_t write(uint8_t) =0;
|
|
||||||
// Write size bytes from buffer into the packet
|
|
||||||
virtual size_t write(const uint8_t *buffer, size_t size) =0;
|
|
||||||
|
|
||||||
// Start processing the next available incoming packet
|
|
||||||
// Returns the size of the packet in bytes, or 0 if no packets are available
|
|
||||||
virtual int parsePacket() =0;
|
|
||||||
// Number of bytes remaining in the current packet
|
|
||||||
virtual int available() =0;
|
|
||||||
// Read a single byte from the current packet
|
|
||||||
virtual int read() =0;
|
|
||||||
// Read up to len bytes from the current packet and place them into buffer
|
|
||||||
// Returns the number of bytes read, or 0 if none are available
|
|
||||||
virtual int read(unsigned char* buffer, size_t len) =0;
|
|
||||||
// Read up to len characters from the current packet and place them into buffer
|
|
||||||
// Returns the number of characters read, or 0 if none are available
|
|
||||||
virtual int read(char* buffer, size_t len) =0;
|
|
||||||
// Return the next byte from the current packet without moving on to the next byte
|
|
||||||
virtual int peek() =0;
|
|
||||||
virtual void flush() =0; // Finish reading the current packet
|
|
||||||
|
|
||||||
// Return the IP address of the host who sent the current incoming packet
|
|
||||||
virtual IPAddress remoteIP() =0;
|
|
||||||
// Return the port of the host who sent the current incoming packet
|
|
||||||
virtual uint16_t remotePort() =0;
|
|
||||||
protected:
|
|
||||||
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,168 +0,0 @@
|
|||||||
/*
|
|
||||||
WCharacter.h - Character utility functions for Wiring & Arduino
|
|
||||||
Copyright (c) 2010 Hernando Barragan. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Character_h
|
|
||||||
#define Character_h
|
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
// WCharacter.h prototypes
|
|
||||||
inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isAlpha(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isAscii(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isWhitespace(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isControl(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isDigit(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isGraph(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isLowerCase(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isPrintable(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isPunct(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isSpace(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isUpperCase(int c) __attribute__((always_inline));
|
|
||||||
inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
|
|
||||||
inline int toAscii(int c) __attribute__((always_inline));
|
|
||||||
inline int toLowerCase(int c) __attribute__((always_inline));
|
|
||||||
inline int toUpperCase(int c)__attribute__((always_inline));
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for an alphanumeric character.
|
|
||||||
// It is equivalent to (isalpha(c) || isdigit(c)).
|
|
||||||
inline boolean isAlphaNumeric(int c)
|
|
||||||
{
|
|
||||||
return ( isalnum(c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for an alphabetic character.
|
|
||||||
// It is equivalent to (isupper(c) || islower(c)).
|
|
||||||
inline boolean isAlpha(int c)
|
|
||||||
{
|
|
||||||
return ( isalpha(c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks whether c is a 7-bit unsigned char value
|
|
||||||
// that fits into the ASCII character set.
|
|
||||||
inline boolean isAscii(int c)
|
|
||||||
{
|
|
||||||
return ( isascii (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for a blank character, that is, a space or a tab.
|
|
||||||
inline boolean isWhitespace(int c)
|
|
||||||
{
|
|
||||||
return ( isblank (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for a control character.
|
|
||||||
inline boolean isControl(int c)
|
|
||||||
{
|
|
||||||
return ( iscntrl (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for a digit (0 through 9).
|
|
||||||
inline boolean isDigit(int c)
|
|
||||||
{
|
|
||||||
return ( isdigit (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for any printable character except space.
|
|
||||||
inline boolean isGraph(int c)
|
|
||||||
{
|
|
||||||
return ( isgraph (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for a lower-case character.
|
|
||||||
inline boolean isLowerCase(int c)
|
|
||||||
{
|
|
||||||
return (islower (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for any printable character including space.
|
|
||||||
inline boolean isPrintable(int c)
|
|
||||||
{
|
|
||||||
return ( isprint (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for any printable character which is not a space
|
|
||||||
// or an alphanumeric character.
|
|
||||||
inline boolean isPunct(int c)
|
|
||||||
{
|
|
||||||
return ( ispunct (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for white-space characters. For the avr-libc library,
|
|
||||||
// these are: space, formfeed ('\f'), newline ('\n'), carriage
|
|
||||||
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
|
|
||||||
inline boolean isSpace(int c)
|
|
||||||
{
|
|
||||||
return ( isspace (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for an uppercase letter.
|
|
||||||
inline boolean isUpperCase(int c)
|
|
||||||
{
|
|
||||||
return ( isupper (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
|
|
||||||
// 8 9 a b c d e f A B C D E F.
|
|
||||||
inline boolean isHexadecimalDigit(int c)
|
|
||||||
{
|
|
||||||
return ( isxdigit (c) == 0 ? false : true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Converts c to a 7-bit unsigned char value that fits into the
|
|
||||||
// ASCII character set, by clearing the high-order bits.
|
|
||||||
inline int toAscii(int c)
|
|
||||||
{
|
|
||||||
return toascii (c);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Warning:
|
|
||||||
// Many people will be unhappy if you use this function.
|
|
||||||
// This function will convert accented letters into random
|
|
||||||
// characters.
|
|
||||||
|
|
||||||
// Converts the letter c to lower case, if possible.
|
|
||||||
inline int toLowerCase(int c)
|
|
||||||
{
|
|
||||||
return tolower (c);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Converts the letter c to upper case, if possible.
|
|
||||||
inline int toUpperCase(int c)
|
|
||||||
{
|
|
||||||
return toupper (c);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,334 +0,0 @@
|
|||||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
|
||||||
|
|
||||||
/*
|
|
||||||
Part of the Wiring project - http://wiring.uniandes.edu.co
|
|
||||||
|
|
||||||
Copyright (c) 2004-05 Hernando Barragan
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
Modified 24 November 2006 by David A. Mellis
|
|
||||||
Modified 1 August 2010 by Mark Sproul
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
|
|
||||||
static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
|
|
||||||
// volatile static voidFuncPtr twiIntFunc;
|
|
||||||
|
|
||||||
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
|
||||||
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
|
||||||
intFunc[interruptNum] = userFunc;
|
|
||||||
|
|
||||||
// Configure the interrupt mode (trigger on low input, any change, rising
|
|
||||||
// edge, or falling edge). The mode constants were chosen to correspond
|
|
||||||
// to the configuration bits in the hardware register, so we simply shift
|
|
||||||
// the mode into place.
|
|
||||||
|
|
||||||
// Enable the interrupt.
|
|
||||||
|
|
||||||
switch (interruptNum) {
|
|
||||||
#if defined(__AVR_ATmega32U4__)
|
|
||||||
// I hate doing this, but the register assignment differs between the 1280/2560
|
|
||||||
// and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
|
|
||||||
// even present on the 32U4 this is the only way to distinguish between them.
|
|
||||||
case 0:
|
|
||||||
EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
|
|
||||||
EIMSK |= (1<<INT0);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
|
|
||||||
EIMSK |= (1<<INT1);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
|
|
||||||
EIMSK |= (1<<INT2);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
|
|
||||||
EIMSK |= (1<<INT3);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
EICRB = (EICRB & ~((1<<ISC60) | (1<<ISC61))) | (mode << ISC60);
|
|
||||||
EIMSK |= (1<<INT6);
|
|
||||||
break;
|
|
||||||
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
|
||||||
case 2:
|
|
||||||
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
|
||||||
EIMSK |= (1 << INT0);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
|
||||||
EIMSK |= (1 << INT1);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
|
||||||
EIMSK |= (1 << INT2);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
|
|
||||||
EIMSK |= (1 << INT3);
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
|
|
||||||
EIMSK |= (1 << INT4);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
|
|
||||||
EIMSK |= (1 << INT5);
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
|
|
||||||
EIMSK |= (1 << INT6);
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
|
|
||||||
EIMSK |= (1 << INT7);
|
|
||||||
break;
|
|
||||||
#else
|
|
||||||
case 0:
|
|
||||||
#if defined(EICRA) && defined(ISC00) && defined(EIMSK)
|
|
||||||
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
|
||||||
EIMSK |= (1 << INT0);
|
|
||||||
#elif defined(MCUCR) && defined(ISC00) && defined(GICR)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
|
||||||
GICR |= (1 << INT0);
|
|
||||||
#elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
|
||||||
GIMSK |= (1 << INT0);
|
|
||||||
#else
|
|
||||||
#error attachInterrupt not finished for this CPU (case 0)
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
#if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
|
|
||||||
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
|
||||||
EIMSK |= (1 << INT1);
|
|
||||||
#elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
|
||||||
GICR |= (1 << INT1);
|
|
||||||
#elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
|
||||||
GIMSK |= (1 << INT1);
|
|
||||||
#else
|
|
||||||
#warning attachInterrupt may need some more work for this cpu (case 1)
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
#if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
|
|
||||||
EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
|
||||||
EIMSK |= (1 << INT2);
|
|
||||||
#elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
|
||||||
GICR |= (1 << INT2);
|
|
||||||
#elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
|
|
||||||
MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
|
|
||||||
GIMSK |= (1 << INT2);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void detachInterrupt(uint8_t interruptNum) {
|
|
||||||
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
|
||||||
// Disable the interrupt. (We can't assume that interruptNum is equal
|
|
||||||
// to the number of the EIMSK bit to clear, as this isn't true on the
|
|
||||||
// ATmega8. There, INT0 is 6 and INT1 is 7.)
|
|
||||||
switch (interruptNum) {
|
|
||||||
#if defined(__AVR_ATmega32U4__)
|
|
||||||
case 0:
|
|
||||||
EIMSK &= ~(1<<INT0);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
EIMSK &= ~(1<<INT1);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
EIMSK &= ~(1<<INT2);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
EIMSK &= ~(1<<INT3);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
EIMSK &= ~(1<<INT6);
|
|
||||||
break;
|
|
||||||
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
|
||||||
case 2:
|
|
||||||
EIMSK &= ~(1 << INT0);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
EIMSK &= ~(1 << INT1);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
EIMSK &= ~(1 << INT2);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
EIMSK &= ~(1 << INT3);
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
EIMSK &= ~(1 << INT4);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
EIMSK &= ~(1 << INT5);
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
EIMSK &= ~(1 << INT6);
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
EIMSK &= ~(1 << INT7);
|
|
||||||
break;
|
|
||||||
#else
|
|
||||||
case 0:
|
|
||||||
#if defined(EIMSK) && defined(INT0)
|
|
||||||
EIMSK &= ~(1 << INT0);
|
|
||||||
#elif defined(GICR) && defined(ISC00)
|
|
||||||
GICR &= ~(1 << INT0); // atmega32
|
|
||||||
#elif defined(GIMSK) && defined(INT0)
|
|
||||||
GIMSK &= ~(1 << INT0);
|
|
||||||
#else
|
|
||||||
#error detachInterrupt not finished for this cpu
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
#if defined(EIMSK) && defined(INT1)
|
|
||||||
EIMSK &= ~(1 << INT1);
|
|
||||||
#elif defined(GICR) && defined(INT1)
|
|
||||||
GICR &= ~(1 << INT1); // atmega32
|
|
||||||
#elif defined(GIMSK) && defined(INT1)
|
|
||||||
GIMSK &= ~(1 << INT1);
|
|
||||||
#else
|
|
||||||
#warning detachInterrupt may need some more work for this cpu (case 1)
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
intFunc[interruptNum] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
void attachInterruptTwi(void (*userFunc)(void) ) {
|
|
||||||
twiIntFunc = userFunc;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega32U4__)
|
|
||||||
ISR(INT0_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_0])
|
|
||||||
intFunc[EXTERNAL_INT_0]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT1_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_1])
|
|
||||||
intFunc[EXTERNAL_INT_1]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT2_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_2])
|
|
||||||
intFunc[EXTERNAL_INT_2]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT3_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_3])
|
|
||||||
intFunc[EXTERNAL_INT_3]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT6_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_4])
|
|
||||||
intFunc[EXTERNAL_INT_4]();
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(EICRA) && defined(EICRB)
|
|
||||||
|
|
||||||
ISR(INT0_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_2])
|
|
||||||
intFunc[EXTERNAL_INT_2]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT1_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_3])
|
|
||||||
intFunc[EXTERNAL_INT_3]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT2_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_4])
|
|
||||||
intFunc[EXTERNAL_INT_4]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT3_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_5])
|
|
||||||
intFunc[EXTERNAL_INT_5]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT4_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_0])
|
|
||||||
intFunc[EXTERNAL_INT_0]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT5_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_1])
|
|
||||||
intFunc[EXTERNAL_INT_1]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT6_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_6])
|
|
||||||
intFunc[EXTERNAL_INT_6]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT7_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_7])
|
|
||||||
intFunc[EXTERNAL_INT_7]();
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
ISR(INT0_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_0])
|
|
||||||
intFunc[EXTERNAL_INT_0]();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT1_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_1])
|
|
||||||
intFunc[EXTERNAL_INT_1]();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(EICRA) && defined(ISC20)
|
|
||||||
ISR(INT2_vect) {
|
|
||||||
if(intFunc[EXTERNAL_INT_2])
|
|
||||||
intFunc[EXTERNAL_INT_2]();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
ISR(TWI_vect) {
|
|
||||||
if(twiIntFunc)
|
|
||||||
twiIntFunc();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
|||||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
|
||||||
|
|
||||||
/*
|
|
||||||
Part of the Wiring project - http://wiring.org.co
|
|
||||||
Copyright (c) 2004-06 Hernando Barragan
|
|
||||||
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id$
|
|
||||||
*/
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
#include "stdlib.h"
|
|
||||||
}
|
|
||||||
|
|
||||||
void randomSeed(unsigned int seed)
|
|
||||||
{
|
|
||||||
if (seed != 0) {
|
|
||||||
srandom(seed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long random(long howbig)
|
|
||||||
{
|
|
||||||
if (howbig == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return random() % howbig;
|
|
||||||
}
|
|
||||||
|
|
||||||
long random(long howsmall, long howbig)
|
|
||||||
{
|
|
||||||
if (howsmall >= howbig) {
|
|
||||||
return howsmall;
|
|
||||||
}
|
|
||||||
long diff = howbig - howsmall;
|
|
||||||
return random(diff) + howsmall;
|
|
||||||
}
|
|
||||||
|
|
||||||
long map(long x, long in_min, long in_max, long out_min, long out_max)
|
|
||||||
{
|
|
||||||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int makeWord(unsigned int w) { return w; }
|
|
||||||
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }
|
|
@ -1,745 +0,0 @@
|
|||||||
/*
|
|
||||||
WString.cpp - String library for Wiring & Arduino
|
|
||||||
...mostly rewritten by Paul Stoffregen...
|
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
|
||||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "WString.h"
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Constructors */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
String::String(const char *cstr)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
if (cstr) copy(cstr, strlen(cstr));
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(const String &value)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
*this = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(const __FlashStringHelper *pstr)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
*this = pstr;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
String::String(String &&rval)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
move(rval);
|
|
||||||
}
|
|
||||||
String::String(StringSumHelper &&rval)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
move(rval);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
String::String(char c)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[2];
|
|
||||||
buf[0] = c;
|
|
||||||
buf[1] = 0;
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(unsigned char value, unsigned char base)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[1 + 8 * sizeof(unsigned char)];
|
|
||||||
utoa(value, buf, base);
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(int value, unsigned char base)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[2 + 8 * sizeof(int)];
|
|
||||||
itoa(value, buf, base);
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(unsigned int value, unsigned char base)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[1 + 8 * sizeof(unsigned int)];
|
|
||||||
utoa(value, buf, base);
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(long value, unsigned char base)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[2 + 8 * sizeof(long)];
|
|
||||||
ltoa(value, buf, base);
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(unsigned long value, unsigned char base)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[1 + 8 * sizeof(unsigned long)];
|
|
||||||
ultoa(value, buf, base);
|
|
||||||
*this = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(float value, unsigned char decimalPlaces)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[33];
|
|
||||||
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
String::String(double value, unsigned char decimalPlaces)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
char buf[33];
|
|
||||||
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
String::~String()
|
|
||||||
{
|
|
||||||
free(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Memory Management */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
inline void String::init(void)
|
|
||||||
{
|
|
||||||
buffer = NULL;
|
|
||||||
capacity = 0;
|
|
||||||
len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::invalidate(void)
|
|
||||||
{
|
|
||||||
if (buffer) free(buffer);
|
|
||||||
buffer = NULL;
|
|
||||||
capacity = len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::reserve(unsigned int size)
|
|
||||||
{
|
|
||||||
if (buffer && capacity >= size) return 1;
|
|
||||||
if (changeBuffer(size)) {
|
|
||||||
if (len == 0) buffer[0] = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::changeBuffer(unsigned int maxStrLen)
|
|
||||||
{
|
|
||||||
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
|
|
||||||
if (newbuffer) {
|
|
||||||
buffer = newbuffer;
|
|
||||||
capacity = maxStrLen;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Copy and Move */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
String & String::copy(const char *cstr, unsigned int length)
|
|
||||||
{
|
|
||||||
if (!reserve(length)) {
|
|
||||||
invalidate();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
len = length;
|
|
||||||
strcpy(buffer, cstr);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
|
|
||||||
{
|
|
||||||
if (!reserve(length)) {
|
|
||||||
invalidate();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
len = length;
|
|
||||||
strcpy_P(buffer, (PGM_P)pstr);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
void String::move(String &rhs)
|
|
||||||
{
|
|
||||||
if (buffer) {
|
|
||||||
if (capacity >= rhs.len) {
|
|
||||||
strcpy(buffer, rhs.buffer);
|
|
||||||
len = rhs.len;
|
|
||||||
rhs.len = 0;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
free(buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buffer = rhs.buffer;
|
|
||||||
capacity = rhs.capacity;
|
|
||||||
len = rhs.len;
|
|
||||||
rhs.buffer = NULL;
|
|
||||||
rhs.capacity = 0;
|
|
||||||
rhs.len = 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
String & String::operator = (const String &rhs)
|
|
||||||
{
|
|
||||||
if (this == &rhs) return *this;
|
|
||||||
|
|
||||||
if (rhs.buffer) copy(rhs.buffer, rhs.len);
|
|
||||||
else invalidate();
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
String & String::operator = (String &&rval)
|
|
||||||
{
|
|
||||||
if (this != &rval) move(rval);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
String & String::operator = (StringSumHelper &&rval)
|
|
||||||
{
|
|
||||||
if (this != &rval) move(rval);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
String & String::operator = (const char *cstr)
|
|
||||||
{
|
|
||||||
if (cstr) copy(cstr, strlen(cstr));
|
|
||||||
else invalidate();
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
String & String::operator = (const __FlashStringHelper *pstr)
|
|
||||||
{
|
|
||||||
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
|
|
||||||
else invalidate();
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* concat */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
unsigned char String::concat(const String &s)
|
|
||||||
{
|
|
||||||
return concat(s.buffer, s.len);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(const char *cstr, unsigned int length)
|
|
||||||
{
|
|
||||||
unsigned int newlen = len + length;
|
|
||||||
if (!cstr) return 0;
|
|
||||||
if (length == 0) return 1;
|
|
||||||
if (!reserve(newlen)) return 0;
|
|
||||||
strcpy(buffer + len, cstr);
|
|
||||||
len = newlen;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(const char *cstr)
|
|
||||||
{
|
|
||||||
if (!cstr) return 0;
|
|
||||||
return concat(cstr, strlen(cstr));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(char c)
|
|
||||||
{
|
|
||||||
char buf[2];
|
|
||||||
buf[0] = c;
|
|
||||||
buf[1] = 0;
|
|
||||||
return concat(buf, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(unsigned char num)
|
|
||||||
{
|
|
||||||
char buf[1 + 3 * sizeof(unsigned char)];
|
|
||||||
itoa(num, buf, 10);
|
|
||||||
return concat(buf, strlen(buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(int num)
|
|
||||||
{
|
|
||||||
char buf[2 + 3 * sizeof(int)];
|
|
||||||
itoa(num, buf, 10);
|
|
||||||
return concat(buf, strlen(buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(unsigned int num)
|
|
||||||
{
|
|
||||||
char buf[1 + 3 * sizeof(unsigned int)];
|
|
||||||
utoa(num, buf, 10);
|
|
||||||
return concat(buf, strlen(buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(long num)
|
|
||||||
{
|
|
||||||
char buf[2 + 3 * sizeof(long)];
|
|
||||||
ltoa(num, buf, 10);
|
|
||||||
return concat(buf, strlen(buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(unsigned long num)
|
|
||||||
{
|
|
||||||
char buf[1 + 3 * sizeof(unsigned long)];
|
|
||||||
ultoa(num, buf, 10);
|
|
||||||
return concat(buf, strlen(buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(float num)
|
|
||||||
{
|
|
||||||
char buf[20];
|
|
||||||
char* string = dtostrf(num, 4, 2, buf);
|
|
||||||
return concat(string, strlen(string));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(double num)
|
|
||||||
{
|
|
||||||
char buf[20];
|
|
||||||
char* string = dtostrf(num, 4, 2, buf);
|
|
||||||
return concat(string, strlen(string));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::concat(const __FlashStringHelper * str)
|
|
||||||
{
|
|
||||||
if (!str) return 0;
|
|
||||||
int length = strlen_P((const char *) str);
|
|
||||||
if (length == 0) return 1;
|
|
||||||
unsigned int newlen = len + length;
|
|
||||||
if (!reserve(newlen)) return 0;
|
|
||||||
strcpy_P(buffer + len, (const char *) str);
|
|
||||||
len = newlen;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Concatenate */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, char c)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(c)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, int num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, long num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, float num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, double num)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(num)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
|
|
||||||
{
|
|
||||||
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
|
||||||
if (!a.concat(rhs)) a.invalidate();
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Comparison */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
int String::compareTo(const String &s) const
|
|
||||||
{
|
|
||||||
if (!buffer || !s.buffer) {
|
|
||||||
if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
|
|
||||||
if (buffer && len > 0) return *(unsigned char *)buffer;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return strcmp(buffer, s.buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::equals(const String &s2) const
|
|
||||||
{
|
|
||||||
return (len == s2.len && compareTo(s2) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::equals(const char *cstr) const
|
|
||||||
{
|
|
||||||
if (len == 0) return (cstr == NULL || *cstr == 0);
|
|
||||||
if (cstr == NULL) return buffer[0] == 0;
|
|
||||||
return strcmp(buffer, cstr) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::operator<(const String &rhs) const
|
|
||||||
{
|
|
||||||
return compareTo(rhs) < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::operator>(const String &rhs) const
|
|
||||||
{
|
|
||||||
return compareTo(rhs) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::operator<=(const String &rhs) const
|
|
||||||
{
|
|
||||||
return compareTo(rhs) <= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::operator>=(const String &rhs) const
|
|
||||||
{
|
|
||||||
return compareTo(rhs) >= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::equalsIgnoreCase( const String &s2 ) const
|
|
||||||
{
|
|
||||||
if (this == &s2) return 1;
|
|
||||||
if (len != s2.len) return 0;
|
|
||||||
if (len == 0) return 1;
|
|
||||||
const char *p1 = buffer;
|
|
||||||
const char *p2 = s2.buffer;
|
|
||||||
while (*p1) {
|
|
||||||
if (tolower(*p1++) != tolower(*p2++)) return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::startsWith( const String &s2 ) const
|
|
||||||
{
|
|
||||||
if (len < s2.len) return 0;
|
|
||||||
return startsWith(s2, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::startsWith( const String &s2, unsigned int offset ) const
|
|
||||||
{
|
|
||||||
if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
|
|
||||||
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char String::endsWith( const String &s2 ) const
|
|
||||||
{
|
|
||||||
if ( len < s2.len || !buffer || !s2.buffer) return 0;
|
|
||||||
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Character Access */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
char String::charAt(unsigned int loc) const
|
|
||||||
{
|
|
||||||
return operator[](loc);
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::setCharAt(unsigned int loc, char c)
|
|
||||||
{
|
|
||||||
if (loc < len) buffer[loc] = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
char & String::operator[](unsigned int index)
|
|
||||||
{
|
|
||||||
static char dummy_writable_char;
|
|
||||||
if (index >= len || !buffer) {
|
|
||||||
dummy_writable_char = 0;
|
|
||||||
return dummy_writable_char;
|
|
||||||
}
|
|
||||||
return buffer[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
char String::operator[]( unsigned int index ) const
|
|
||||||
{
|
|
||||||
if (index >= len || !buffer) return 0;
|
|
||||||
return buffer[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
|
|
||||||
{
|
|
||||||
if (!bufsize || !buf) return;
|
|
||||||
if (index >= len) {
|
|
||||||
buf[0] = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
unsigned int n = bufsize - 1;
|
|
||||||
if (n > len - index) n = len - index;
|
|
||||||
strncpy((char *)buf, buffer + index, n);
|
|
||||||
buf[n] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Search */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
int String::indexOf(char c) const
|
|
||||||
{
|
|
||||||
return indexOf(c, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( char ch, unsigned int fromIndex ) const
|
|
||||||
{
|
|
||||||
if (fromIndex >= len) return -1;
|
|
||||||
const char* temp = strchr(buffer + fromIndex, ch);
|
|
||||||
if (temp == NULL) return -1;
|
|
||||||
return temp - buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf(const String &s2) const
|
|
||||||
{
|
|
||||||
return indexOf(s2, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf(const String &s2, unsigned int fromIndex) const
|
|
||||||
{
|
|
||||||
if (fromIndex >= len) return -1;
|
|
||||||
const char *found = strstr(buffer + fromIndex, s2.buffer);
|
|
||||||
if (found == NULL) return -1;
|
|
||||||
return found - buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf( char theChar ) const
|
|
||||||
{
|
|
||||||
return lastIndexOf(theChar, len - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf(char ch, unsigned int fromIndex) const
|
|
||||||
{
|
|
||||||
if (fromIndex >= len) return -1;
|
|
||||||
char tempchar = buffer[fromIndex + 1];
|
|
||||||
buffer[fromIndex + 1] = '\0';
|
|
||||||
char* temp = strrchr( buffer, ch );
|
|
||||||
buffer[fromIndex + 1] = tempchar;
|
|
||||||
if (temp == NULL) return -1;
|
|
||||||
return temp - buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf(const String &s2) const
|
|
||||||
{
|
|
||||||
return lastIndexOf(s2, len - s2.len);
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
|
|
||||||
{
|
|
||||||
if (s2.len == 0 || len == 0 || s2.len > len) return -1;
|
|
||||||
if (fromIndex >= len) fromIndex = len - 1;
|
|
||||||
int found = -1;
|
|
||||||
for (char *p = buffer; p <= buffer + fromIndex; p++) {
|
|
||||||
p = strstr(p, s2.buffer);
|
|
||||||
if (!p) break;
|
|
||||||
if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
|
|
||||||
}
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::substring(unsigned int left, unsigned int right) const
|
|
||||||
{
|
|
||||||
if (left > right) {
|
|
||||||
unsigned int temp = right;
|
|
||||||
right = left;
|
|
||||||
left = temp;
|
|
||||||
}
|
|
||||||
String out;
|
|
||||||
if (left >= len) return out;
|
|
||||||
if (right > len) right = len;
|
|
||||||
char temp = buffer[right]; // save the replaced character
|
|
||||||
buffer[right] = '\0';
|
|
||||||
out = buffer + left; // pointer arithmetic
|
|
||||||
buffer[right] = temp; //restore character
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Modification */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
void String::replace(char find, char replace)
|
|
||||||
{
|
|
||||||
if (!buffer) return;
|
|
||||||
for (char *p = buffer; *p; p++) {
|
|
||||||
if (*p == find) *p = replace;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::replace(const String& find, const String& replace)
|
|
||||||
{
|
|
||||||
if (len == 0 || find.len == 0) return;
|
|
||||||
int diff = replace.len - find.len;
|
|
||||||
char *readFrom = buffer;
|
|
||||||
char *foundAt;
|
|
||||||
if (diff == 0) {
|
|
||||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
|
||||||
memcpy(foundAt, replace.buffer, replace.len);
|
|
||||||
readFrom = foundAt + replace.len;
|
|
||||||
}
|
|
||||||
} else if (diff < 0) {
|
|
||||||
char *writeTo = buffer;
|
|
||||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
|
||||||
unsigned int n = foundAt - readFrom;
|
|
||||||
memcpy(writeTo, readFrom, n);
|
|
||||||
writeTo += n;
|
|
||||||
memcpy(writeTo, replace.buffer, replace.len);
|
|
||||||
writeTo += replace.len;
|
|
||||||
readFrom = foundAt + find.len;
|
|
||||||
len += diff;
|
|
||||||
}
|
|
||||||
strcpy(writeTo, readFrom);
|
|
||||||
} else {
|
|
||||||
unsigned int size = len; // compute size needed for result
|
|
||||||
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
|
||||||
readFrom = foundAt + find.len;
|
|
||||||
size += diff;
|
|
||||||
}
|
|
||||||
if (size == len) return;
|
|
||||||
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
|
|
||||||
int index = len - 1;
|
|
||||||
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
|
|
||||||
readFrom = buffer + index + find.len;
|
|
||||||
memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
|
|
||||||
len += diff;
|
|
||||||
buffer[len] = 0;
|
|
||||||
memcpy(buffer + index, replace.buffer, replace.len);
|
|
||||||
index--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::remove(unsigned int index){
|
|
||||||
// Pass the biggest integer as the count. The remove method
|
|
||||||
// below will take care of truncating it at the end of the
|
|
||||||
// string.
|
|
||||||
remove(index, (unsigned int)-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::remove(unsigned int index, unsigned int count){
|
|
||||||
if (index >= len) { return; }
|
|
||||||
if (count <= 0) { return; }
|
|
||||||
if (count > len - index) { count = len - index; }
|
|
||||||
char *writeTo = buffer + index;
|
|
||||||
len = len - count;
|
|
||||||
strncpy(writeTo, buffer + index + count,len - index);
|
|
||||||
buffer[len] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::toLowerCase(void)
|
|
||||||
{
|
|
||||||
if (!buffer) return;
|
|
||||||
for (char *p = buffer; *p; p++) {
|
|
||||||
*p = tolower(*p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::toUpperCase(void)
|
|
||||||
{
|
|
||||||
if (!buffer) return;
|
|
||||||
for (char *p = buffer; *p; p++) {
|
|
||||||
*p = toupper(*p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void String::trim(void)
|
|
||||||
{
|
|
||||||
if (!buffer || len == 0) return;
|
|
||||||
char *begin = buffer;
|
|
||||||
while (isspace(*begin)) begin++;
|
|
||||||
char *end = buffer + len - 1;
|
|
||||||
while (isspace(*end) && end >= begin) end--;
|
|
||||||
len = end + 1 - begin;
|
|
||||||
if (begin > buffer) memcpy(buffer, begin, len);
|
|
||||||
buffer[len] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************/
|
|
||||||
/* Parsing / Conversion */
|
|
||||||
/*********************************************/
|
|
||||||
|
|
||||||
long String::toInt(void) const
|
|
||||||
{
|
|
||||||
if (buffer) return atol(buffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
float String::toFloat(void) const
|
|
||||||
{
|
|
||||||
if (buffer) return float(atof(buffer));
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,224 +0,0 @@
|
|||||||
/*
|
|
||||||
WString.h - String library for Wiring & Arduino
|
|
||||||
...mostly rewritten by Paul Stoffregen...
|
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
|
||||||
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef String_class_h
|
|
||||||
#define String_class_h
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
|
|
||||||
// When compiling programs with this class, the following gcc parameters
|
|
||||||
// dramatically increase performance and memory (RAM) efficiency, typically
|
|
||||||
// with little or no increase in code size.
|
|
||||||
// -felide-constructors
|
|
||||||
// -std=c++0x
|
|
||||||
|
|
||||||
class __FlashStringHelper;
|
|
||||||
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
|
|
||||||
|
|
||||||
// An inherited class for holding the result of a concatenation. These
|
|
||||||
// result objects are assumed to be writable by subsequent concatenations.
|
|
||||||
class StringSumHelper;
|
|
||||||
|
|
||||||
// The string class
|
|
||||||
class String
|
|
||||||
{
|
|
||||||
// use a function pointer to allow for "if (s)" without the
|
|
||||||
// complications of an operator bool(). for more information, see:
|
|
||||||
// http://www.artima.com/cppsource/safebool.html
|
|
||||||
typedef void (String::*StringIfHelperType)() const;
|
|
||||||
void StringIfHelper() const {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
// constructors
|
|
||||||
// creates a copy of the initial value.
|
|
||||||
// if the initial value is null or invalid, or if memory allocation
|
|
||||||
// fails, the string will be marked as invalid (i.e. "if (s)" will
|
|
||||||
// be false).
|
|
||||||
String(const char *cstr = "");
|
|
||||||
String(const String &str);
|
|
||||||
String(const __FlashStringHelper *str);
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
String(String &&rval);
|
|
||||||
String(StringSumHelper &&rval);
|
|
||||||
#endif
|
|
||||||
explicit String(char c);
|
|
||||||
explicit String(unsigned char, unsigned char base=10);
|
|
||||||
explicit String(int, unsigned char base=10);
|
|
||||||
explicit String(unsigned int, unsigned char base=10);
|
|
||||||
explicit String(long, unsigned char base=10);
|
|
||||||
explicit String(unsigned long, unsigned char base=10);
|
|
||||||
explicit String(float, unsigned char decimalPlaces=2);
|
|
||||||
explicit String(double, unsigned char decimalPlaces=2);
|
|
||||||
~String(void);
|
|
||||||
|
|
||||||
// memory management
|
|
||||||
// return true on success, false on failure (in which case, the string
|
|
||||||
// is left unchanged). reserve(0), if successful, will validate an
|
|
||||||
// invalid string (i.e., "if (s)" will be true afterwards)
|
|
||||||
unsigned char reserve(unsigned int size);
|
|
||||||
inline unsigned int length(void) const {return len;}
|
|
||||||
|
|
||||||
// creates a copy of the assigned value. if the value is null or
|
|
||||||
// invalid, or if the memory allocation fails, the string will be
|
|
||||||
// marked as invalid ("if (s)" will be false).
|
|
||||||
String & operator = (const String &rhs);
|
|
||||||
String & operator = (const char *cstr);
|
|
||||||
String & operator = (const __FlashStringHelper *str);
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
String & operator = (String &&rval);
|
|
||||||
String & operator = (StringSumHelper &&rval);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// concatenate (works w/ built-in types)
|
|
||||||
|
|
||||||
// returns true on success, false on failure (in which case, the string
|
|
||||||
// is left unchanged). if the argument is null or invalid, the
|
|
||||||
// concatenation is considered unsucessful.
|
|
||||||
unsigned char concat(const String &str);
|
|
||||||
unsigned char concat(const char *cstr);
|
|
||||||
unsigned char concat(char c);
|
|
||||||
unsigned char concat(unsigned char c);
|
|
||||||
unsigned char concat(int num);
|
|
||||||
unsigned char concat(unsigned int num);
|
|
||||||
unsigned char concat(long num);
|
|
||||||
unsigned char concat(unsigned long num);
|
|
||||||
unsigned char concat(float num);
|
|
||||||
unsigned char concat(double num);
|
|
||||||
unsigned char concat(const __FlashStringHelper * str);
|
|
||||||
|
|
||||||
// if there's not enough memory for the concatenated value, the string
|
|
||||||
// will be left unchanged (but this isn't signalled in any way)
|
|
||||||
String & operator += (const String &rhs) {concat(rhs); return (*this);}
|
|
||||||
String & operator += (const char *cstr) {concat(cstr); return (*this);}
|
|
||||||
String & operator += (char c) {concat(c); return (*this);}
|
|
||||||
String & operator += (unsigned char num) {concat(num); return (*this);}
|
|
||||||
String & operator += (int num) {concat(num); return (*this);}
|
|
||||||
String & operator += (unsigned int num) {concat(num); return (*this);}
|
|
||||||
String & operator += (long num) {concat(num); return (*this);}
|
|
||||||
String & operator += (unsigned long num) {concat(num); return (*this);}
|
|
||||||
String & operator += (float num) {concat(num); return (*this);}
|
|
||||||
String & operator += (double num) {concat(num); return (*this);}
|
|
||||||
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
|
|
||||||
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
|
||||||
|
|
||||||
// comparison (only works w/ Strings and "strings")
|
|
||||||
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
|
|
||||||
int compareTo(const String &s) const;
|
|
||||||
unsigned char equals(const String &s) const;
|
|
||||||
unsigned char equals(const char *cstr) const;
|
|
||||||
unsigned char operator == (const String &rhs) const {return equals(rhs);}
|
|
||||||
unsigned char operator == (const char *cstr) const {return equals(cstr);}
|
|
||||||
unsigned char operator != (const String &rhs) const {return !equals(rhs);}
|
|
||||||
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
|
|
||||||
unsigned char operator < (const String &rhs) const;
|
|
||||||
unsigned char operator > (const String &rhs) const;
|
|
||||||
unsigned char operator <= (const String &rhs) const;
|
|
||||||
unsigned char operator >= (const String &rhs) const;
|
|
||||||
unsigned char equalsIgnoreCase(const String &s) const;
|
|
||||||
unsigned char startsWith( const String &prefix) const;
|
|
||||||
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
|
||||||
unsigned char endsWith(const String &suffix) const;
|
|
||||||
|
|
||||||
// character acccess
|
|
||||||
char charAt(unsigned int index) const;
|
|
||||||
void setCharAt(unsigned int index, char c);
|
|
||||||
char operator [] (unsigned int index) const;
|
|
||||||
char& operator [] (unsigned int index);
|
|
||||||
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
|
|
||||||
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
|
|
||||||
{getBytes((unsigned char *)buf, bufsize, index);}
|
|
||||||
const char * c_str() const { return buffer; }
|
|
||||||
|
|
||||||
// search
|
|
||||||
int indexOf( char ch ) const;
|
|
||||||
int indexOf( char ch, unsigned int fromIndex ) const;
|
|
||||||
int indexOf( const String &str ) const;
|
|
||||||
int indexOf( const String &str, unsigned int fromIndex ) const;
|
|
||||||
int lastIndexOf( char ch ) const;
|
|
||||||
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
|
||||||
int lastIndexOf( const String &str ) const;
|
|
||||||
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
|
||||||
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
|
|
||||||
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
|
||||||
|
|
||||||
// modification
|
|
||||||
void replace(char find, char replace);
|
|
||||||
void replace(const String& find, const String& replace);
|
|
||||||
void remove(unsigned int index);
|
|
||||||
void remove(unsigned int index, unsigned int count);
|
|
||||||
void toLowerCase(void);
|
|
||||||
void toUpperCase(void);
|
|
||||||
void trim(void);
|
|
||||||
|
|
||||||
// parsing/conversion
|
|
||||||
long toInt(void) const;
|
|
||||||
float toFloat(void) const;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
char *buffer; // the actual char array
|
|
||||||
unsigned int capacity; // the array length minus one (for the '\0')
|
|
||||||
unsigned int len; // the String length (not counting the '\0')
|
|
||||||
protected:
|
|
||||||
void init(void);
|
|
||||||
void invalidate(void);
|
|
||||||
unsigned char changeBuffer(unsigned int maxStrLen);
|
|
||||||
unsigned char concat(const char *cstr, unsigned int length);
|
|
||||||
|
|
||||||
// copy and move
|
|
||||||
String & copy(const char *cstr, unsigned int length);
|
|
||||||
String & copy(const __FlashStringHelper *pstr, unsigned int length);
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
|
||||||
void move(String &rhs);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
class StringSumHelper : public String
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
StringSumHelper(const String &s) : String(s) {}
|
|
||||||
StringSumHelper(const char *p) : String(p) {}
|
|
||||||
StringSumHelper(char c) : String(c) {}
|
|
||||||
StringSumHelper(unsigned char num) : String(num) {}
|
|
||||||
StringSumHelper(int num) : String(num) {}
|
|
||||||
StringSumHelper(unsigned int num) : String(num) {}
|
|
||||||
StringSumHelper(long num) : String(num) {}
|
|
||||||
StringSumHelper(unsigned long num) : String(num) {}
|
|
||||||
StringSumHelper(float num) : String(num) {}
|
|
||||||
StringSumHelper(double num) : String(num) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // __cplusplus
|
|
||||||
#endif // String_class_h
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2014 Arduino. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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 Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
|
|
||||||
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
|
|
||||||
|
|
||||||
void __cxa_pure_virtual(void) {
|
|
||||||
// We might want to write some diagnostics to uart in this case
|
|
||||||
//std::terminate();
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
void __cxa_deleted_virtual(void) {
|
|
||||||
// We might want to write some diagnostics to uart in this case
|
|
||||||
//std::terminate();
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
@ -1,534 +0,0 @@
|
|||||||
/*
|
|
||||||
binary.h - Definitions for binary constants
|
|
||||||
Copyright (c) 2006 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Binary_h
|
|
||||||
#define Binary_h
|
|
||||||
|
|
||||||
#define B0 0
|
|
||||||
#define B00 0
|
|
||||||
#define B000 0
|
|
||||||
#define B0000 0
|
|
||||||
#define B00000 0
|
|
||||||
#define B000000 0
|
|
||||||
#define B0000000 0
|
|
||||||
#define B00000000 0
|
|
||||||
#define B1 1
|
|
||||||
#define B01 1
|
|
||||||
#define B001 1
|
|
||||||
#define B0001 1
|
|
||||||
#define B00001 1
|
|
||||||
#define B000001 1
|
|
||||||
#define B0000001 1
|
|
||||||
#define B00000001 1
|
|
||||||
#define B10 2
|
|
||||||
#define B010 2
|
|
||||||
#define B0010 2
|
|
||||||
#define B00010 2
|
|
||||||
#define B000010 2
|
|
||||||
#define B0000010 2
|
|
||||||
#define B00000010 2
|
|
||||||
#define B11 3
|
|
||||||
#define B011 3
|
|
||||||
#define B0011 3
|
|
||||||
#define B00011 3
|
|
||||||
#define B000011 3
|
|
||||||
#define B0000011 3
|
|
||||||
#define B00000011 3
|
|
||||||
#define B100 4
|
|
||||||
#define B0100 4
|
|
||||||
#define B00100 4
|
|
||||||
#define B000100 4
|
|
||||||
#define B0000100 4
|
|
||||||
#define B00000100 4
|
|
||||||
#define B101 5
|
|
||||||
#define B0101 5
|
|
||||||
#define B00101 5
|
|
||||||
#define B000101 5
|
|
||||||
#define B0000101 5
|
|
||||||
#define B00000101 5
|
|
||||||
#define B110 6
|
|
||||||
#define B0110 6
|
|
||||||
#define B00110 6
|
|
||||||
#define B000110 6
|
|
||||||
#define B0000110 6
|
|
||||||
#define B00000110 6
|
|
||||||
#define B111 7
|
|
||||||
#define B0111 7
|
|
||||||
#define B00111 7
|
|
||||||
#define B000111 7
|
|
||||||
#define B0000111 7
|
|
||||||
#define B00000111 7
|
|
||||||
#define B1000 8
|
|
||||||
#define B01000 8
|
|
||||||
#define B001000 8
|
|
||||||
#define B0001000 8
|
|
||||||
#define B00001000 8
|
|
||||||
#define B1001 9
|
|
||||||
#define B01001 9
|
|
||||||
#define B001001 9
|
|
||||||
#define B0001001 9
|
|
||||||
#define B00001001 9
|
|
||||||
#define B1010 10
|
|
||||||
#define B01010 10
|
|
||||||
#define B001010 10
|
|
||||||
#define B0001010 10
|
|
||||||
#define B00001010 10
|
|
||||||
#define B1011 11
|
|
||||||
#define B01011 11
|
|
||||||
#define B001011 11
|
|
||||||
#define B0001011 11
|
|
||||||
#define B00001011 11
|
|
||||||
#define B1100 12
|
|
||||||
#define B01100 12
|
|
||||||
#define B001100 12
|
|
||||||
#define B0001100 12
|
|
||||||
#define B00001100 12
|
|
||||||
#define B1101 13
|
|
||||||
#define B01101 13
|
|
||||||
#define B001101 13
|
|
||||||
#define B0001101 13
|
|
||||||
#define B00001101 13
|
|
||||||
#define B1110 14
|
|
||||||
#define B01110 14
|
|
||||||
#define B001110 14
|
|
||||||
#define B0001110 14
|
|
||||||
#define B00001110 14
|
|
||||||
#define B1111 15
|
|
||||||
#define B01111 15
|
|
||||||
#define B001111 15
|
|
||||||
#define B0001111 15
|
|
||||||
#define B00001111 15
|
|
||||||
#define B10000 16
|
|
||||||
#define B010000 16
|
|
||||||
#define B0010000 16
|
|
||||||
#define B00010000 16
|
|
||||||
#define B10001 17
|
|
||||||
#define B010001 17
|
|
||||||
#define B0010001 17
|
|
||||||
#define B00010001 17
|
|
||||||
#define B10010 18
|
|
||||||
#define B010010 18
|
|
||||||
#define B0010010 18
|
|
||||||
#define B00010010 18
|
|
||||||
#define B10011 19
|
|
||||||
#define B010011 19
|
|
||||||
#define B0010011 19
|
|
||||||
#define B00010011 19
|
|
||||||
#define B10100 20
|
|
||||||
#define B010100 20
|
|
||||||
#define B0010100 20
|
|
||||||
#define B00010100 20
|
|
||||||
#define B10101 21
|
|
||||||
#define B010101 21
|
|
||||||
#define B0010101 21
|
|
||||||
#define B00010101 21
|
|
||||||
#define B10110 22
|
|
||||||
#define B010110 22
|
|
||||||
#define B0010110 22
|
|
||||||
#define B00010110 22
|
|
||||||
#define B10111 23
|
|
||||||
#define B010111 23
|
|
||||||
#define B0010111 23
|
|
||||||
#define B00010111 23
|
|
||||||
#define B11000 24
|
|
||||||
#define B011000 24
|
|
||||||
#define B0011000 24
|
|
||||||
#define B00011000 24
|
|
||||||
#define B11001 25
|
|
||||||
#define B011001 25
|
|
||||||
#define B0011001 25
|
|
||||||
#define B00011001 25
|
|
||||||
#define B11010 26
|
|
||||||
#define B011010 26
|
|
||||||
#define B0011010 26
|
|
||||||
#define B00011010 26
|
|
||||||
#define B11011 27
|
|
||||||
#define B011011 27
|
|
||||||
#define B0011011 27
|
|
||||||
#define B00011011 27
|
|
||||||
#define B11100 28
|
|
||||||
#define B011100 28
|
|
||||||
#define B0011100 28
|
|
||||||
#define B00011100 28
|
|
||||||
#define B11101 29
|
|
||||||
#define B011101 29
|
|
||||||
#define B0011101 29
|
|
||||||
#define B00011101 29
|
|
||||||
#define B11110 30
|
|
||||||
#define B011110 30
|
|
||||||
#define B0011110 30
|
|
||||||
#define B00011110 30
|
|
||||||
#define B11111 31
|
|
||||||
#define B011111 31
|
|
||||||
#define B0011111 31
|
|
||||||
#define B00011111 31
|
|
||||||
#define B100000 32
|
|
||||||
#define B0100000 32
|
|
||||||
#define B00100000 32
|
|
||||||
#define B100001 33
|
|
||||||
#define B0100001 33
|
|
||||||
#define B00100001 33
|
|
||||||
#define B100010 34
|
|
||||||
#define B0100010 34
|
|
||||||
#define B00100010 34
|
|
||||||
#define B100011 35
|
|
||||||
#define B0100011 35
|
|
||||||
#define B00100011 35
|
|
||||||
#define B100100 36
|
|
||||||
#define B0100100 36
|
|
||||||
#define B00100100 36
|
|
||||||
#define B100101 37
|
|
||||||
#define B0100101 37
|
|
||||||
#define B00100101 37
|
|
||||||
#define B100110 38
|
|
||||||
#define B0100110 38
|
|
||||||
#define B00100110 38
|
|
||||||
#define B100111 39
|
|
||||||
#define B0100111 39
|
|
||||||
#define B00100111 39
|
|
||||||
#define B101000 40
|
|
||||||
#define B0101000 40
|
|
||||||
#define B00101000 40
|
|
||||||
#define B101001 41
|
|
||||||
#define B0101001 41
|
|
||||||
#define B00101001 41
|
|
||||||
#define B101010 42
|
|
||||||
#define B0101010 42
|
|
||||||
#define B00101010 42
|
|
||||||
#define B101011 43
|
|
||||||
#define B0101011 43
|
|
||||||
#define B00101011 43
|
|
||||||
#define B101100 44
|
|
||||||
#define B0101100 44
|
|
||||||
#define B00101100 44
|
|
||||||
#define B101101 45
|
|
||||||
#define B0101101 45
|
|
||||||
#define B00101101 45
|
|
||||||
#define B101110 46
|
|
||||||
#define B0101110 46
|
|
||||||
#define B00101110 46
|
|
||||||
#define B101111 47
|
|
||||||
#define B0101111 47
|
|
||||||
#define B00101111 47
|
|
||||||
#define B110000 48
|
|
||||||
#define B0110000 48
|
|
||||||
#define B00110000 48
|
|
||||||
#define B110001 49
|
|
||||||
#define B0110001 49
|
|
||||||
#define B00110001 49
|
|
||||||
#define B110010 50
|
|
||||||
#define B0110010 50
|
|
||||||
#define B00110010 50
|
|
||||||
#define B110011 51
|
|
||||||
#define B0110011 51
|
|
||||||
#define B00110011 51
|
|
||||||
#define B110100 52
|
|
||||||
#define B0110100 52
|
|
||||||
#define B00110100 52
|
|
||||||
#define B110101 53
|
|
||||||
#define B0110101 53
|
|
||||||
#define B00110101 53
|
|
||||||
#define B110110 54
|
|
||||||
#define B0110110 54
|
|
||||||
#define B00110110 54
|
|
||||||
#define B110111 55
|
|
||||||
#define B0110111 55
|
|
||||||
#define B00110111 55
|
|
||||||
#define B111000 56
|
|
||||||
#define B0111000 56
|
|
||||||
#define B00111000 56
|
|
||||||
#define B111001 57
|
|
||||||
#define B0111001 57
|
|
||||||
#define B00111001 57
|
|
||||||
#define B111010 58
|
|
||||||
#define B0111010 58
|
|
||||||
#define B00111010 58
|
|
||||||
#define B111011 59
|
|
||||||
#define B0111011 59
|
|
||||||
#define B00111011 59
|
|
||||||
#define B111100 60
|
|
||||||
#define B0111100 60
|
|
||||||
#define B00111100 60
|
|
||||||
#define B111101 61
|
|
||||||
#define B0111101 61
|
|
||||||
#define B00111101 61
|
|
||||||
#define B111110 62
|
|
||||||
#define B0111110 62
|
|
||||||
#define B00111110 62
|
|
||||||
#define B111111 63
|
|
||||||
#define B0111111 63
|
|
||||||
#define B00111111 63
|
|
||||||
#define B1000000 64
|
|
||||||
#define B01000000 64
|
|
||||||
#define B1000001 65
|
|
||||||
#define B01000001 65
|
|
||||||
#define B1000010 66
|
|
||||||
#define B01000010 66
|
|
||||||
#define B1000011 67
|
|
||||||
#define B01000011 67
|
|
||||||
#define B1000100 68
|
|
||||||
#define B01000100 68
|
|
||||||
#define B1000101 69
|
|
||||||
#define B01000101 69
|
|
||||||
#define B1000110 70
|
|
||||||
#define B01000110 70
|
|
||||||
#define B1000111 71
|
|
||||||
#define B01000111 71
|
|
||||||
#define B1001000 72
|
|
||||||
#define B01001000 72
|
|
||||||
#define B1001001 73
|
|
||||||
#define B01001001 73
|
|
||||||
#define B1001010 74
|
|
||||||
#define B01001010 74
|
|
||||||
#define B1001011 75
|
|
||||||
#define B01001011 75
|
|
||||||
#define B1001100 76
|
|
||||||
#define B01001100 76
|
|
||||||
#define B1001101 77
|
|
||||||
#define B01001101 77
|
|
||||||
#define B1001110 78
|
|
||||||
#define B01001110 78
|
|
||||||
#define B1001111 79
|
|
||||||
#define B01001111 79
|
|
||||||
#define B1010000 80
|
|
||||||
#define B01010000 80
|
|
||||||
#define B1010001 81
|
|
||||||
#define B01010001 81
|
|
||||||
#define B1010010 82
|
|
||||||
#define B01010010 82
|
|
||||||
#define B1010011 83
|
|
||||||
#define B01010011 83
|
|
||||||
#define B1010100 84
|
|
||||||
#define B01010100 84
|
|
||||||
#define B1010101 85
|
|
||||||
#define B01010101 85
|
|
||||||
#define B1010110 86
|
|
||||||
#define B01010110 86
|
|
||||||
#define B1010111 87
|
|
||||||
#define B01010111 87
|
|
||||||
#define B1011000 88
|
|
||||||
#define B01011000 88
|
|
||||||
#define B1011001 89
|
|
||||||
#define B01011001 89
|
|
||||||
#define B1011010 90
|
|
||||||
#define B01011010 90
|
|
||||||
#define B1011011 91
|
|
||||||
#define B01011011 91
|
|
||||||
#define B1011100 92
|
|
||||||
#define B01011100 92
|
|
||||||
#define B1011101 93
|
|
||||||
#define B01011101 93
|
|
||||||
#define B1011110 94
|
|
||||||
#define B01011110 94
|
|
||||||
#define B1011111 95
|
|
||||||
#define B01011111 95
|
|
||||||
#define B1100000 96
|
|
||||||
#define B01100000 96
|
|
||||||
#define B1100001 97
|
|
||||||
#define B01100001 97
|
|
||||||
#define B1100010 98
|
|
||||||
#define B01100010 98
|
|
||||||
#define B1100011 99
|
|
||||||
#define B01100011 99
|
|
||||||
#define B1100100 100
|
|
||||||
#define B01100100 100
|
|
||||||
#define B1100101 101
|
|
||||||
#define B01100101 101
|
|
||||||
#define B1100110 102
|
|
||||||
#define B01100110 102
|
|
||||||
#define B1100111 103
|
|
||||||
#define B01100111 103
|
|
||||||
#define B1101000 104
|
|
||||||
#define B01101000 104
|
|
||||||
#define B1101001 105
|
|
||||||
#define B01101001 105
|
|
||||||
#define B1101010 106
|
|
||||||
#define B01101010 106
|
|
||||||
#define B1101011 107
|
|
||||||
#define B01101011 107
|
|
||||||
#define B1101100 108
|
|
||||||
#define B01101100 108
|
|
||||||
#define B1101101 109
|
|
||||||
#define B01101101 109
|
|
||||||
#define B1101110 110
|
|
||||||
#define B01101110 110
|
|
||||||
#define B1101111 111
|
|
||||||
#define B01101111 111
|
|
||||||
#define B1110000 112
|
|
||||||
#define B01110000 112
|
|
||||||
#define B1110001 113
|
|
||||||
#define B01110001 113
|
|
||||||
#define B1110010 114
|
|
||||||
#define B01110010 114
|
|
||||||
#define B1110011 115
|
|
||||||
#define B01110011 115
|
|
||||||
#define B1110100 116
|
|
||||||
#define B01110100 116
|
|
||||||
#define B1110101 117
|
|
||||||
#define B01110101 117
|
|
||||||
#define B1110110 118
|
|
||||||
#define B01110110 118
|
|
||||||
#define B1110111 119
|
|
||||||
#define B01110111 119
|
|
||||||
#define B1111000 120
|
|
||||||
#define B01111000 120
|
|
||||||
#define B1111001 121
|
|
||||||
#define B01111001 121
|
|
||||||
#define B1111010 122
|
|
||||||
#define B01111010 122
|
|
||||||
#define B1111011 123
|
|
||||||
#define B01111011 123
|
|
||||||
#define B1111100 124
|
|
||||||
#define B01111100 124
|
|
||||||
#define B1111101 125
|
|
||||||
#define B01111101 125
|
|
||||||
#define B1111110 126
|
|
||||||
#define B01111110 126
|
|
||||||
#define B1111111 127
|
|
||||||
#define B01111111 127
|
|
||||||
#define B10000000 128
|
|
||||||
#define B10000001 129
|
|
||||||
#define B10000010 130
|
|
||||||
#define B10000011 131
|
|
||||||
#define B10000100 132
|
|
||||||
#define B10000101 133
|
|
||||||
#define B10000110 134
|
|
||||||
#define B10000111 135
|
|
||||||
#define B10001000 136
|
|
||||||
#define B10001001 137
|
|
||||||
#define B10001010 138
|
|
||||||
#define B10001011 139
|
|
||||||
#define B10001100 140
|
|
||||||
#define B10001101 141
|
|
||||||
#define B10001110 142
|
|
||||||
#define B10001111 143
|
|
||||||
#define B10010000 144
|
|
||||||
#define B10010001 145
|
|
||||||
#define B10010010 146
|
|
||||||
#define B10010011 147
|
|
||||||
#define B10010100 148
|
|
||||||
#define B10010101 149
|
|
||||||
#define B10010110 150
|
|
||||||
#define B10010111 151
|
|
||||||
#define B10011000 152
|
|
||||||
#define B10011001 153
|
|
||||||
#define B10011010 154
|
|
||||||
#define B10011011 155
|
|
||||||
#define B10011100 156
|
|
||||||
#define B10011101 157
|
|
||||||
#define B10011110 158
|
|
||||||
#define B10011111 159
|
|
||||||
#define B10100000 160
|
|
||||||
#define B10100001 161
|
|
||||||
#define B10100010 162
|
|
||||||
#define B10100011 163
|
|
||||||
#define B10100100 164
|
|
||||||
#define B10100101 165
|
|
||||||
#define B10100110 166
|
|
||||||
#define B10100111 167
|
|
||||||
#define B10101000 168
|
|
||||||
#define B10101001 169
|
|
||||||
#define B10101010 170
|
|
||||||
#define B10101011 171
|
|
||||||
#define B10101100 172
|
|
||||||
#define B10101101 173
|
|
||||||
#define B10101110 174
|
|
||||||
#define B10101111 175
|
|
||||||
#define B10110000 176
|
|
||||||
#define B10110001 177
|
|
||||||
#define B10110010 178
|
|
||||||
#define B10110011 179
|
|
||||||
#define B10110100 180
|
|
||||||
#define B10110101 181
|
|
||||||
#define B10110110 182
|
|
||||||
#define B10110111 183
|
|
||||||
#define B10111000 184
|
|
||||||
#define B10111001 185
|
|
||||||
#define B10111010 186
|
|
||||||
#define B10111011 187
|
|
||||||
#define B10111100 188
|
|
||||||
#define B10111101 189
|
|
||||||
#define B10111110 190
|
|
||||||
#define B10111111 191
|
|
||||||
#define B11000000 192
|
|
||||||
#define B11000001 193
|
|
||||||
#define B11000010 194
|
|
||||||
#define B11000011 195
|
|
||||||
#define B11000100 196
|
|
||||||
#define B11000101 197
|
|
||||||
#define B11000110 198
|
|
||||||
#define B11000111 199
|
|
||||||
#define B11001000 200
|
|
||||||
#define B11001001 201
|
|
||||||
#define B11001010 202
|
|
||||||
#define B11001011 203
|
|
||||||
#define B11001100 204
|
|
||||||
#define B11001101 205
|
|
||||||
#define B11001110 206
|
|
||||||
#define B11001111 207
|
|
||||||
#define B11010000 208
|
|
||||||
#define B11010001 209
|
|
||||||
#define B11010010 210
|
|
||||||
#define B11010011 211
|
|
||||||
#define B11010100 212
|
|
||||||
#define B11010101 213
|
|
||||||
#define B11010110 214
|
|
||||||
#define B11010111 215
|
|
||||||
#define B11011000 216
|
|
||||||
#define B11011001 217
|
|
||||||
#define B11011010 218
|
|
||||||
#define B11011011 219
|
|
||||||
#define B11011100 220
|
|
||||||
#define B11011101 221
|
|
||||||
#define B11011110 222
|
|
||||||
#define B11011111 223
|
|
||||||
#define B11100000 224
|
|
||||||
#define B11100001 225
|
|
||||||
#define B11100010 226
|
|
||||||
#define B11100011 227
|
|
||||||
#define B11100100 228
|
|
||||||
#define B11100101 229
|
|
||||||
#define B11100110 230
|
|
||||||
#define B11100111 231
|
|
||||||
#define B11101000 232
|
|
||||||
#define B11101001 233
|
|
||||||
#define B11101010 234
|
|
||||||
#define B11101011 235
|
|
||||||
#define B11101100 236
|
|
||||||
#define B11101101 237
|
|
||||||
#define B11101110 238
|
|
||||||
#define B11101111 239
|
|
||||||
#define B11110000 240
|
|
||||||
#define B11110001 241
|
|
||||||
#define B11110010 242
|
|
||||||
#define B11110011 243
|
|
||||||
#define B11110100 244
|
|
||||||
#define B11110101 245
|
|
||||||
#define B11110110 246
|
|
||||||
#define B11110111 247
|
|
||||||
#define B11111000 248
|
|
||||||
#define B11111001 249
|
|
||||||
#define B11111010 250
|
|
||||||
#define B11111011 251
|
|
||||||
#define B11111100 252
|
|
||||||
#define B11111101 253
|
|
||||||
#define B11111110 254
|
|
||||||
#define B11111111 255
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2012 Arduino. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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 Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Empty yield() hook.
|
|
||||||
*
|
|
||||||
* This function is intended to be used by library writers to build
|
|
||||||
* libraries or sketches that supports cooperative threads.
|
|
||||||
*
|
|
||||||
* Its defined as a weak symbol and it can be redefined to implement a
|
|
||||||
* real cooperative scheduler.
|
|
||||||
*/
|
|
||||||
static void __empty() {
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
void yield(void) __attribute__ ((weak, alias("__empty")));
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
main.cpp - Main loop for Arduino sketches
|
|
||||||
Copyright (c) 2005-2013 Arduino Team. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
//Declared weak in Arduino.h to allow user redefinitions.
|
|
||||||
int atexit(void (*func)()) { return 0; }
|
|
||||||
|
|
||||||
// Weak empty variant initialization function.
|
|
||||||
// May be redefined by variant files.
|
|
||||||
void initVariant() __attribute__((weak));
|
|
||||||
void initVariant() { }
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
init();
|
|
||||||
|
|
||||||
initVariant();
|
|
||||||
|
|
||||||
#if defined(USBCON)
|
|
||||||
USBDevice.attach();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
setup();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
loop();
|
|
||||||
if (serialEventRun) serialEventRun();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2014 Arduino. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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 Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void *operator new(size_t size) {
|
|
||||||
return malloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *operator new[](size_t size) {
|
|
||||||
return malloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete(void * ptr) {
|
|
||||||
free(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete[](void * ptr) {
|
|
||||||
free(ptr);
|
|
||||||
}
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2014 Arduino. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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 Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef NEW_H
|
|
||||||
#define NEW_H
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void * operator new(size_t size);
|
|
||||||
void * operator new[](size_t size);
|
|
||||||
void operator delete(void * ptr);
|
|
||||||
void operator delete[](void * ptr);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,325 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring.c - Partial implementation of the Wiring API for the ATmega8.
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id$
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
|
|
||||||
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
|
|
||||||
// the overflow handler is called every 256 ticks.
|
|
||||||
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
|
|
||||||
|
|
||||||
// the whole number of milliseconds per timer0 overflow
|
|
||||||
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
|
|
||||||
|
|
||||||
// the fractional number of milliseconds per timer0 overflow. we shift right
|
|
||||||
// by three to fit these numbers into a byte. (for the clock speeds we care
|
|
||||||
// about - 8 and 16 MHz - this doesn't lose precision.)
|
|
||||||
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
|
|
||||||
#define FRACT_MAX (1000 >> 3)
|
|
||||||
|
|
||||||
volatile unsigned long timer0_overflow_count = 0;
|
|
||||||
volatile unsigned long timer0_millis = 0;
|
|
||||||
static unsigned char timer0_fract = 0;
|
|
||||||
|
|
||||||
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
|
||||||
ISR(TIM0_OVF_vect)
|
|
||||||
#else
|
|
||||||
ISR(TIMER0_OVF_vect)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
// copy these to local variables so they can be stored in registers
|
|
||||||
// (volatile variables must be read from memory on every access)
|
|
||||||
unsigned long m = timer0_millis;
|
|
||||||
unsigned char f = timer0_fract;
|
|
||||||
|
|
||||||
m += MILLIS_INC;
|
|
||||||
f += FRACT_INC;
|
|
||||||
if (f >= FRACT_MAX) {
|
|
||||||
f -= FRACT_MAX;
|
|
||||||
m += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer0_fract = f;
|
|
||||||
timer0_millis = m;
|
|
||||||
timer0_overflow_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long millis()
|
|
||||||
{
|
|
||||||
unsigned long m;
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
|
|
||||||
// disable interrupts while we read timer0_millis or we might get an
|
|
||||||
// inconsistent value (e.g. in the middle of a write to timer0_millis)
|
|
||||||
cli();
|
|
||||||
m = timer0_millis;
|
|
||||||
SREG = oldSREG;
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long micros() {
|
|
||||||
unsigned long m;
|
|
||||||
uint8_t oldSREG = SREG, t;
|
|
||||||
|
|
||||||
cli();
|
|
||||||
m = timer0_overflow_count;
|
|
||||||
#if defined(TCNT0)
|
|
||||||
t = TCNT0;
|
|
||||||
#elif defined(TCNT0L)
|
|
||||||
t = TCNT0L;
|
|
||||||
#else
|
|
||||||
#error TIMER 0 not defined
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef TIFR0
|
|
||||||
if ((TIFR0 & _BV(TOV0)) && (t < 255))
|
|
||||||
m++;
|
|
||||||
#else
|
|
||||||
if ((TIFR & _BV(TOV0)) && (t < 255))
|
|
||||||
m++;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SREG = oldSREG;
|
|
||||||
|
|
||||||
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
|
|
||||||
}
|
|
||||||
|
|
||||||
void delay(unsigned long ms)
|
|
||||||
{
|
|
||||||
uint16_t start = (uint16_t)micros();
|
|
||||||
|
|
||||||
while (ms > 0) {
|
|
||||||
yield();
|
|
||||||
if (((uint16_t)micros() - start) >= 1000) {
|
|
||||||
ms--;
|
|
||||||
start += 1000;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
|
|
||||||
void delayMicroseconds(unsigned int us)
|
|
||||||
{
|
|
||||||
// calling avrlib's delay_us() function with low values (e.g. 1 or
|
|
||||||
// 2 microseconds) gives delays longer than desired.
|
|
||||||
//delay_us(us);
|
|
||||||
#if F_CPU >= 20000000L
|
|
||||||
// for the 20 MHz clock on rare Arduino boards
|
|
||||||
|
|
||||||
// for a one-microsecond delay, simply wait 2 cycle and return. The overhead
|
|
||||||
// of the function call yields a delay of exactly a one microsecond.
|
|
||||||
__asm__ __volatile__ (
|
|
||||||
"nop" "\n\t"
|
|
||||||
"nop"); //just waiting 2 cycle
|
|
||||||
if (--us == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// the following loop takes a 1/5 of a microsecond (4 cycles)
|
|
||||||
// per iteration, so execute it five times for each microsecond of
|
|
||||||
// delay requested.
|
|
||||||
us = (us<<2) + us; // x5 us
|
|
||||||
|
|
||||||
// account for the time taken in the preceeding commands.
|
|
||||||
us -= 2;
|
|
||||||
|
|
||||||
#elif F_CPU >= 16000000L
|
|
||||||
// for the 16 MHz clock on most Arduino boards
|
|
||||||
|
|
||||||
// for a one-microsecond delay, simply return. the overhead
|
|
||||||
// of the function call yields a delay of approximately 1 1/8 us.
|
|
||||||
if (--us == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// the following loop takes a quarter of a microsecond (4 cycles)
|
|
||||||
// per iteration, so execute it four times for each microsecond of
|
|
||||||
// delay requested.
|
|
||||||
us <<= 2;
|
|
||||||
|
|
||||||
// account for the time taken in the preceeding commands.
|
|
||||||
us -= 2;
|
|
||||||
#else
|
|
||||||
// for the 8 MHz internal clock on the ATmega168
|
|
||||||
|
|
||||||
// for a one- or two-microsecond delay, simply return. the overhead of
|
|
||||||
// the function calls takes more than two microseconds. can't just
|
|
||||||
// subtract two, since us is unsigned; we'd overflow.
|
|
||||||
if (--us == 0)
|
|
||||||
return;
|
|
||||||
if (--us == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// the following loop takes half of a microsecond (4 cycles)
|
|
||||||
// per iteration, so execute it twice for each microsecond of
|
|
||||||
// delay requested.
|
|
||||||
us <<= 1;
|
|
||||||
|
|
||||||
// partially compensate for the time taken by the preceeding commands.
|
|
||||||
// we can't subtract any more than this or we'd overflow w/ small delays.
|
|
||||||
us--;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// busy wait
|
|
||||||
__asm__ __volatile__ (
|
|
||||||
"1: sbiw %0,1" "\n\t" // 2 cycles
|
|
||||||
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init()
|
|
||||||
{
|
|
||||||
// this needs to be called before setup() or some functions won't
|
|
||||||
// work there
|
|
||||||
sei();
|
|
||||||
|
|
||||||
// on the ATmega168, timer 0 is also used for fast hardware pwm
|
|
||||||
// (using phase-correct PWM would mean that timer 0 overflowed half as often
|
|
||||||
// resulting in different millis() behavior on the ATmega8 and ATmega168)
|
|
||||||
#if defined(TCCR0A) && defined(WGM01)
|
|
||||||
sbi(TCCR0A, WGM01);
|
|
||||||
sbi(TCCR0A, WGM00);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// set timer 0 prescale factor to 64
|
|
||||||
#if defined(__AVR_ATmega128__)
|
|
||||||
// CPU specific: different values for the ATmega128
|
|
||||||
sbi(TCCR0, CS02);
|
|
||||||
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
|
|
||||||
// this combination is for the standard atmega8
|
|
||||||
sbi(TCCR0, CS01);
|
|
||||||
sbi(TCCR0, CS00);
|
|
||||||
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
|
|
||||||
// this combination is for the standard 168/328/1280/2560
|
|
||||||
sbi(TCCR0B, CS01);
|
|
||||||
sbi(TCCR0B, CS00);
|
|
||||||
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
|
|
||||||
// this combination is for the __AVR_ATmega645__ series
|
|
||||||
sbi(TCCR0A, CS01);
|
|
||||||
sbi(TCCR0A, CS00);
|
|
||||||
#else
|
|
||||||
#error Timer 0 prescale factor 64 not set correctly
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// enable timer 0 overflow interrupt
|
|
||||||
#if defined(TIMSK) && defined(TOIE0)
|
|
||||||
sbi(TIMSK, TOIE0);
|
|
||||||
#elif defined(TIMSK0) && defined(TOIE0)
|
|
||||||
sbi(TIMSK0, TOIE0);
|
|
||||||
#else
|
|
||||||
#error Timer 0 overflow interrupt not set correctly
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// timers 1 and 2 are used for phase-correct hardware pwm
|
|
||||||
// this is better for motors as it ensures an even waveform
|
|
||||||
// note, however, that fast pwm mode can achieve a frequency of up
|
|
||||||
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
|
|
||||||
|
|
||||||
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
|
|
||||||
TCCR1B = 0;
|
|
||||||
|
|
||||||
// set timer 1 prescale factor to 64
|
|
||||||
sbi(TCCR1B, CS11);
|
|
||||||
#if F_CPU >= 8000000L
|
|
||||||
sbi(TCCR1B, CS10);
|
|
||||||
#endif
|
|
||||||
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
|
|
||||||
sbi(TCCR1, CS11);
|
|
||||||
#if F_CPU >= 8000000L
|
|
||||||
sbi(TCCR1, CS10);
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
// put timer 1 in 8-bit phase correct pwm mode
|
|
||||||
#if defined(TCCR1A) && defined(WGM10)
|
|
||||||
sbi(TCCR1A, WGM10);
|
|
||||||
#elif defined(TCCR1)
|
|
||||||
#warning this needs to be finished
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// set timer 2 prescale factor to 64
|
|
||||||
#if defined(TCCR2) && defined(CS22)
|
|
||||||
sbi(TCCR2, CS22);
|
|
||||||
#elif defined(TCCR2B) && defined(CS22)
|
|
||||||
sbi(TCCR2B, CS22);
|
|
||||||
#else
|
|
||||||
#warning Timer 2 not finished (may not be present on this CPU)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// configure timer 2 for phase correct pwm (8-bit)
|
|
||||||
#if defined(TCCR2) && defined(WGM20)
|
|
||||||
sbi(TCCR2, WGM20);
|
|
||||||
#elif defined(TCCR2A) && defined(WGM20)
|
|
||||||
sbi(TCCR2A, WGM20);
|
|
||||||
#else
|
|
||||||
#warning Timer 2 not finished (may not be present on this CPU)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
|
|
||||||
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
|
|
||||||
sbi(TCCR3B, CS30);
|
|
||||||
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
|
|
||||||
sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
|
|
||||||
sbi(TCCR4B, CS41);
|
|
||||||
sbi(TCCR4B, CS40);
|
|
||||||
sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
|
|
||||||
sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A
|
|
||||||
sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D
|
|
||||||
#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
|
|
||||||
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
|
|
||||||
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
|
|
||||||
sbi(TCCR4B, CS40);
|
|
||||||
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
|
|
||||||
#endif
|
|
||||||
#endif /* end timer4 block for ATMEGA1280/2560 and similar */
|
|
||||||
|
|
||||||
#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
|
|
||||||
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
|
|
||||||
sbi(TCCR5B, CS50);
|
|
||||||
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ADCSRA)
|
|
||||||
// set a2d prescale factor to 128
|
|
||||||
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
|
|
||||||
// XXX: this will not work properly for other clock speeds, and
|
|
||||||
// this code should use F_CPU to determine the prescale factor.
|
|
||||||
sbi(ADCSRA, ADPS2);
|
|
||||||
sbi(ADCSRA, ADPS1);
|
|
||||||
sbi(ADCSRA, ADPS0);
|
|
||||||
|
|
||||||
// enable a2d conversions
|
|
||||||
sbi(ADCSRA, ADEN);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// the bootloader connects pins 0 and 1 to the USART; disconnect them
|
|
||||||
// here so they can be used as normal digital i/o; they will be
|
|
||||||
// reconnected in Serial.begin()
|
|
||||||
#if defined(UCSRB)
|
|
||||||
UCSRB = 0;
|
|
||||||
#elif defined(UCSR0B)
|
|
||||||
UCSR0B = 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
@ -1,292 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring_analog.c - analog input and output
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
|
|
||||||
uint8_t analog_reference = DEFAULT;
|
|
||||||
|
|
||||||
void analogReference(uint8_t mode)
|
|
||||||
{
|
|
||||||
// can't actually set the register here because the default setting
|
|
||||||
// will connect AVCC and the AREF pin, which would cause a short if
|
|
||||||
// there's something connected to AREF.
|
|
||||||
analog_reference = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
int analogRead(uint8_t pin)
|
|
||||||
{
|
|
||||||
uint8_t low, high;
|
|
||||||
|
|
||||||
#if defined(analogPinToChannel)
|
|
||||||
#if defined(__AVR_ATmega32U4__)
|
|
||||||
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
|
|
||||||
#endif
|
|
||||||
pin = analogPinToChannel(pin);
|
|
||||||
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
|
|
||||||
#elif defined(__AVR_ATmega32U4__)
|
|
||||||
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
|
|
||||||
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
|
|
||||||
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
|
|
||||||
#else
|
|
||||||
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ADCSRB) && defined(MUX5)
|
|
||||||
// the MUX5 bit of ADCSRB selects whether we're reading from channels
|
|
||||||
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
|
|
||||||
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// set the analog reference (high two bits of ADMUX) and select the
|
|
||||||
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
|
|
||||||
// to 0 (the default).
|
|
||||||
#if defined(ADMUX)
|
|
||||||
ADMUX = (analog_reference << 6) | (pin & 0x07);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// without a delay, we seem to read from the wrong channel
|
|
||||||
//delay(1);
|
|
||||||
|
|
||||||
#if defined(ADCSRA) && defined(ADCL)
|
|
||||||
// start the conversion
|
|
||||||
sbi(ADCSRA, ADSC);
|
|
||||||
|
|
||||||
// ADSC is cleared when the conversion finishes
|
|
||||||
while (bit_is_set(ADCSRA, ADSC));
|
|
||||||
|
|
||||||
// we have to read ADCL first; doing so locks both ADCL
|
|
||||||
// and ADCH until ADCH is read. reading ADCL second would
|
|
||||||
// cause the results of each conversion to be discarded,
|
|
||||||
// as ADCL and ADCH would be locked when it completed.
|
|
||||||
low = ADCL;
|
|
||||||
high = ADCH;
|
|
||||||
#else
|
|
||||||
// we dont have an ADC, return 0
|
|
||||||
low = 0;
|
|
||||||
high = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// combine the two bytes
|
|
||||||
return (high << 8) | low;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Right now, PWM output only works on the pins with
|
|
||||||
// hardware support. These are defined in the appropriate
|
|
||||||
// pins_*.c file. For the rest of the pins, we default
|
|
||||||
// to digital output.
|
|
||||||
void analogWrite(uint8_t pin, int val)
|
|
||||||
{
|
|
||||||
// We need to make sure the PWM output is enabled for those pins
|
|
||||||
// that support it, as we turn it off when digitally reading or
|
|
||||||
// writing with them. Also, make sure the pin is in output mode
|
|
||||||
// for consistenty with Wiring, which doesn't require a pinMode
|
|
||||||
// call for the analog output pins.
|
|
||||||
pinMode(pin, OUTPUT);
|
|
||||||
if (val == 0)
|
|
||||||
{
|
|
||||||
digitalWrite(pin, LOW);
|
|
||||||
}
|
|
||||||
else if (val == 255)
|
|
||||||
{
|
|
||||||
digitalWrite(pin, HIGH);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
switch(digitalPinToTimer(pin))
|
|
||||||
{
|
|
||||||
// XXX fix needed for atmega8
|
|
||||||
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
|
|
||||||
case TIMER0A:
|
|
||||||
// connect pwm to pin on timer 0
|
|
||||||
sbi(TCCR0, COM00);
|
|
||||||
OCR0 = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR0A) && defined(COM0A1)
|
|
||||||
case TIMER0A:
|
|
||||||
// connect pwm to pin on timer 0, channel A
|
|
||||||
sbi(TCCR0A, COM0A1);
|
|
||||||
OCR0A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR0A) && defined(COM0B1)
|
|
||||||
case TIMER0B:
|
|
||||||
// connect pwm to pin on timer 0, channel B
|
|
||||||
sbi(TCCR0A, COM0B1);
|
|
||||||
OCR0B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR1A) && defined(COM1A1)
|
|
||||||
case TIMER1A:
|
|
||||||
// connect pwm to pin on timer 1, channel A
|
|
||||||
sbi(TCCR1A, COM1A1);
|
|
||||||
OCR1A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR1A) && defined(COM1B1)
|
|
||||||
case TIMER1B:
|
|
||||||
// connect pwm to pin on timer 1, channel B
|
|
||||||
sbi(TCCR1A, COM1B1);
|
|
||||||
OCR1B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR1A) && defined(COM1C1)
|
|
||||||
case TIMER1C:
|
|
||||||
// connect pwm to pin on timer 1, channel B
|
|
||||||
sbi(TCCR1A, COM1C1);
|
|
||||||
OCR1C = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR2) && defined(COM21)
|
|
||||||
case TIMER2:
|
|
||||||
// connect pwm to pin on timer 2
|
|
||||||
sbi(TCCR2, COM21);
|
|
||||||
OCR2 = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR2A) && defined(COM2A1)
|
|
||||||
case TIMER2A:
|
|
||||||
// connect pwm to pin on timer 2, channel A
|
|
||||||
sbi(TCCR2A, COM2A1);
|
|
||||||
OCR2A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR2A) && defined(COM2B1)
|
|
||||||
case TIMER2B:
|
|
||||||
// connect pwm to pin on timer 2, channel B
|
|
||||||
sbi(TCCR2A, COM2B1);
|
|
||||||
OCR2B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3A) && defined(COM3A1)
|
|
||||||
case TIMER3A:
|
|
||||||
// connect pwm to pin on timer 3, channel A
|
|
||||||
sbi(TCCR3A, COM3A1);
|
|
||||||
OCR3A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3A) && defined(COM3B1)
|
|
||||||
case TIMER3B:
|
|
||||||
// connect pwm to pin on timer 3, channel B
|
|
||||||
sbi(TCCR3A, COM3B1);
|
|
||||||
OCR3B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3A) && defined(COM3C1)
|
|
||||||
case TIMER3C:
|
|
||||||
// connect pwm to pin on timer 3, channel C
|
|
||||||
sbi(TCCR3A, COM3C1);
|
|
||||||
OCR3C = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A)
|
|
||||||
case TIMER4A:
|
|
||||||
//connect pwm to pin on timer 4, channel A
|
|
||||||
sbi(TCCR4A, COM4A1);
|
|
||||||
#if defined(COM4A0) // only used on 32U4
|
|
||||||
cbi(TCCR4A, COM4A0);
|
|
||||||
#endif
|
|
||||||
OCR4A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A) && defined(COM4B1)
|
|
||||||
case TIMER4B:
|
|
||||||
// connect pwm to pin on timer 4, channel B
|
|
||||||
sbi(TCCR4A, COM4B1);
|
|
||||||
OCR4B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A) && defined(COM4C1)
|
|
||||||
case TIMER4C:
|
|
||||||
// connect pwm to pin on timer 4, channel C
|
|
||||||
sbi(TCCR4A, COM4C1);
|
|
||||||
OCR4C = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4C) && defined(COM4D1)
|
|
||||||
case TIMER4D:
|
|
||||||
// connect pwm to pin on timer 4, channel D
|
|
||||||
sbi(TCCR4C, COM4D1);
|
|
||||||
#if defined(COM4D0) // only used on 32U4
|
|
||||||
cbi(TCCR4C, COM4D0);
|
|
||||||
#endif
|
|
||||||
OCR4D = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(TCCR5A) && defined(COM5A1)
|
|
||||||
case TIMER5A:
|
|
||||||
// connect pwm to pin on timer 5, channel A
|
|
||||||
sbi(TCCR5A, COM5A1);
|
|
||||||
OCR5A = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR5A) && defined(COM5B1)
|
|
||||||
case TIMER5B:
|
|
||||||
// connect pwm to pin on timer 5, channel B
|
|
||||||
sbi(TCCR5A, COM5B1);
|
|
||||||
OCR5B = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR5A) && defined(COM5C1)
|
|
||||||
case TIMER5C:
|
|
||||||
// connect pwm to pin on timer 5, channel C
|
|
||||||
sbi(TCCR5A, COM5C1);
|
|
||||||
OCR5C = val; // set pwm duty
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
case NOT_ON_TIMER:
|
|
||||||
default:
|
|
||||||
if (val < 128) {
|
|
||||||
digitalWrite(pin, LOW);
|
|
||||||
} else {
|
|
||||||
digitalWrite(pin, HIGH);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,181 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring_digital.c - digital input and output functions
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
Modified 28 September 2010 by Mark Sproul
|
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define ARDUINO_MAIN
|
|
||||||
#include "wiring_private.h"
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
|
|
||||||
void pinMode(uint8_t pin, uint8_t mode)
|
|
||||||
{
|
|
||||||
uint8_t bit = digitalPinToBitMask(pin);
|
|
||||||
uint8_t port = digitalPinToPort(pin);
|
|
||||||
volatile uint8_t *reg, *out;
|
|
||||||
|
|
||||||
if (port == NOT_A_PIN) return;
|
|
||||||
|
|
||||||
// JWS: can I let the optimizer do this?
|
|
||||||
reg = portModeRegister(port);
|
|
||||||
out = portOutputRegister(port);
|
|
||||||
|
|
||||||
if (mode == INPUT) {
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
*reg &= ~bit;
|
|
||||||
*out &= ~bit;
|
|
||||||
SREG = oldSREG;
|
|
||||||
} else if (mode == INPUT_PULLUP) {
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
*reg &= ~bit;
|
|
||||||
*out |= bit;
|
|
||||||
SREG = oldSREG;
|
|
||||||
} else {
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
*reg |= bit;
|
|
||||||
SREG = oldSREG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forcing this inline keeps the callers from having to push their own stuff
|
|
||||||
// on the stack. It is a good performance win and only takes 1 more byte per
|
|
||||||
// user than calling. (It will take more bytes on the 168.)
|
|
||||||
//
|
|
||||||
// But shouldn't this be moved into pinMode? Seems silly to check and do on
|
|
||||||
// each digitalread or write.
|
|
||||||
//
|
|
||||||
// Mark Sproul:
|
|
||||||
// - Removed inline. Save 170 bytes on atmega1280
|
|
||||||
// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
|
|
||||||
// - Added more #ifdefs, now compiles for atmega645
|
|
||||||
//
|
|
||||||
//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
|
|
||||||
//static inline void turnOffPWM(uint8_t timer)
|
|
||||||
static void turnOffPWM(uint8_t timer)
|
|
||||||
{
|
|
||||||
switch (timer)
|
|
||||||
{
|
|
||||||
#if defined(TCCR1A) && defined(COM1A1)
|
|
||||||
case TIMER1A: cbi(TCCR1A, COM1A1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR1A) && defined(COM1B1)
|
|
||||||
case TIMER1B: cbi(TCCR1A, COM1B1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR1A) && defined(COM1C1)
|
|
||||||
case TIMER1C: cbi(TCCR1A, COM1C1); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR2) && defined(COM21)
|
|
||||||
case TIMER2: cbi(TCCR2, COM21); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR0A) && defined(COM0A1)
|
|
||||||
case TIMER0A: cbi(TCCR0A, COM0A1); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TIMER0B) && defined(COM0B1)
|
|
||||||
case TIMER0B: cbi(TCCR0A, COM0B1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR2A) && defined(COM2A1)
|
|
||||||
case TIMER2A: cbi(TCCR2A, COM2A1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR2A) && defined(COM2B1)
|
|
||||||
case TIMER2B: cbi(TCCR2A, COM2B1); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR3A) && defined(COM3A1)
|
|
||||||
case TIMER3A: cbi(TCCR3A, COM3A1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR3A) && defined(COM3B1)
|
|
||||||
case TIMER3B: cbi(TCCR3A, COM3B1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR3A) && defined(COM3C1)
|
|
||||||
case TIMER3C: cbi(TCCR3A, COM3C1); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR4A) && defined(COM4A1)
|
|
||||||
case TIMER4A: cbi(TCCR4A, COM4A1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR4A) && defined(COM4B1)
|
|
||||||
case TIMER4B: cbi(TCCR4A, COM4B1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR4A) && defined(COM4C1)
|
|
||||||
case TIMER4C: cbi(TCCR4A, COM4C1); break;
|
|
||||||
#endif
|
|
||||||
#if defined(TCCR4C) && defined(COM4D1)
|
|
||||||
case TIMER4D: cbi(TCCR4C, COM4D1); break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(TCCR5A)
|
|
||||||
case TIMER5A: cbi(TCCR5A, COM5A1); break;
|
|
||||||
case TIMER5B: cbi(TCCR5A, COM5B1); break;
|
|
||||||
case TIMER5C: cbi(TCCR5A, COM5C1); break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void digitalWrite(uint8_t pin, uint8_t val)
|
|
||||||
{
|
|
||||||
uint8_t timer = digitalPinToTimer(pin);
|
|
||||||
uint8_t bit = digitalPinToBitMask(pin);
|
|
||||||
uint8_t port = digitalPinToPort(pin);
|
|
||||||
volatile uint8_t *out;
|
|
||||||
|
|
||||||
if (port == NOT_A_PIN) return;
|
|
||||||
|
|
||||||
// If the pin that support PWM output, we need to turn it off
|
|
||||||
// before doing a digital write.
|
|
||||||
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
|
|
||||||
|
|
||||||
out = portOutputRegister(port);
|
|
||||||
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
|
|
||||||
if (val == LOW) {
|
|
||||||
*out &= ~bit;
|
|
||||||
} else {
|
|
||||||
*out |= bit;
|
|
||||||
}
|
|
||||||
|
|
||||||
SREG = oldSREG;
|
|
||||||
}
|
|
||||||
|
|
||||||
int digitalRead(uint8_t pin)
|
|
||||||
{
|
|
||||||
uint8_t timer = digitalPinToTimer(pin);
|
|
||||||
uint8_t bit = digitalPinToBitMask(pin);
|
|
||||||
uint8_t port = digitalPinToPort(pin);
|
|
||||||
|
|
||||||
if (port == NOT_A_PIN) return LOW;
|
|
||||||
|
|
||||||
// If the pin that support PWM output, we need to turn it off
|
|
||||||
// before getting a digital reading.
|
|
||||||
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
|
|
||||||
|
|
||||||
if (*portInputRegister(port) & bit) return HIGH;
|
|
||||||
return LOW;
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring_private.h - Internal header file.
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id: wiring.h 239 2007-01-12 17:58:39Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef WiringPrivate_h
|
|
||||||
#define WiringPrivate_h
|
|
||||||
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C"{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef cbi
|
|
||||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
|
||||||
#endif
|
|
||||||
#ifndef sbi
|
|
||||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define EXTERNAL_INT_0 0
|
|
||||||
#define EXTERNAL_INT_1 1
|
|
||||||
#define EXTERNAL_INT_2 2
|
|
||||||
#define EXTERNAL_INT_3 3
|
|
||||||
#define EXTERNAL_INT_4 4
|
|
||||||
#define EXTERNAL_INT_5 5
|
|
||||||
#define EXTERNAL_INT_6 6
|
|
||||||
#define EXTERNAL_INT_7 7
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__)
|
|
||||||
#define EXTERNAL_NUM_INTERRUPTS 8
|
|
||||||
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
|
|
||||||
#define EXTERNAL_NUM_INTERRUPTS 3
|
|
||||||
#elif defined(__AVR_ATmega32U4__)
|
|
||||||
#define EXTERNAL_NUM_INTERRUPTS 5
|
|
||||||
#else
|
|
||||||
#define EXTERNAL_NUM_INTERRUPTS 2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef void (*voidFuncPtr)(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
} // extern "C"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring_pulse.c - pulseIn() function
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
|
|
||||||
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
|
|
||||||
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
|
|
||||||
* to 3 minutes in length, but must be called at least a few dozen microseconds
|
|
||||||
* before the start of the pulse. */
|
|
||||||
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
|
|
||||||
{
|
|
||||||
// cache the port and bit of the pin in order to speed up the
|
|
||||||
// pulse width measuring loop and achieve finer resolution. calling
|
|
||||||
// digitalRead() instead yields much coarser resolution.
|
|
||||||
uint8_t bit = digitalPinToBitMask(pin);
|
|
||||||
uint8_t port = digitalPinToPort(pin);
|
|
||||||
uint8_t stateMask = (state ? bit : 0);
|
|
||||||
unsigned long width = 0; // keep initialization out of time critical area
|
|
||||||
|
|
||||||
// convert the timeout from microseconds to a number of times through
|
|
||||||
// the initial loop; it takes 16 clock cycles per iteration.
|
|
||||||
unsigned long numloops = 0;
|
|
||||||
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
|
|
||||||
|
|
||||||
// wait for any previous pulse to end
|
|
||||||
while ((*portInputRegister(port) & bit) == stateMask)
|
|
||||||
if (numloops++ == maxloops)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// wait for the pulse to start
|
|
||||||
while ((*portInputRegister(port) & bit) != stateMask)
|
|
||||||
if (numloops++ == maxloops)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// wait for the pulse to stop
|
|
||||||
while ((*portInputRegister(port) & bit) == stateMask) {
|
|
||||||
if (numloops++ == maxloops)
|
|
||||||
return 0;
|
|
||||||
width++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert the reading to microseconds. The loop has been determined
|
|
||||||
// to be 20 clock cycles long and have about 16 clocks between the edge
|
|
||||||
// and the start of the loop. There will be some error introduced by
|
|
||||||
// the interrupt handlers.
|
|
||||||
return clockCyclesToMicroseconds(width * 21 + 16);
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
wiring_shift.c - shiftOut() function
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2005-2006 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "wiring_private.h"
|
|
||||||
|
|
||||||
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
|
|
||||||
uint8_t value = 0;
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < 8; ++i) {
|
|
||||||
digitalWrite(clockPin, HIGH);
|
|
||||||
if (bitOrder == LSBFIRST)
|
|
||||||
value |= digitalRead(dataPin) << i;
|
|
||||||
else
|
|
||||||
value |= digitalRead(dataPin) << (7 - i);
|
|
||||||
digitalWrite(clockPin, LOW);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < 8; i++) {
|
|
||||||
if (bitOrder == LSBFIRST)
|
|
||||||
digitalWrite(dataPin, !!(val & (1 << i)));
|
|
||||||
else
|
|
||||||
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
|
|
||||||
|
|
||||||
digitalWrite(clockPin, HIGH);
|
|
||||||
digitalWrite(clockPin, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
EEPROM.cpp - EEPROM library
|
|
||||||
Copyright (c) 2006 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Includes
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#include <avr/eeprom.h>
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "EEPROM.h"
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Definitions
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Constructors
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* User API
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
uint8_t EEPROMClass::read(int address)
|
|
||||||
{
|
|
||||||
return eeprom_read_byte((unsigned char *) address);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EEPROMClass::write(int address, uint8_t value)
|
|
||||||
{
|
|
||||||
eeprom_write_byte((unsigned char *) address, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
EEPROMClass EEPROM;
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
EEPROM.h - EEPROM library
|
|
||||||
Copyright (c) 2006 David A. Mellis. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef EEPROM_h
|
|
||||||
#define EEPROM_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
class EEPROMClass
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
uint8_t read(int);
|
|
||||||
void write(int, uint8_t);
|
|
||||||
};
|
|
||||||
|
|
||||||
extern EEPROMClass EEPROM;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
|||||||
/*
|
|
||||||
* EEPROM Clear
|
|
||||||
*
|
|
||||||
* Sets all of the bytes of the EEPROM to 0.
|
|
||||||
* This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <EEPROM.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
// write a 0 to all 512 bytes of the EEPROM
|
|
||||||
for (int i = 0; i < 512; i++)
|
|
||||||
EEPROM.write(i, 0);
|
|
||||||
|
|
||||||
// turn the LED on when we're done
|
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* EEPROM Read
|
|
||||||
*
|
|
||||||
* Reads the value of each byte of the EEPROM and prints it
|
|
||||||
* to the computer.
|
|
||||||
* This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <EEPROM.h>
|
|
||||||
|
|
||||||
// start reading from the first byte (address 0) of the EEPROM
|
|
||||||
int address = 0;
|
|
||||||
byte value;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
// initialize serial and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for Leonardo only
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// read a byte from the current address of the EEPROM
|
|
||||||
value = EEPROM.read(address);
|
|
||||||
|
|
||||||
Serial.print(address);
|
|
||||||
Serial.print("\t");
|
|
||||||
Serial.print(value, DEC);
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
// advance to the next address of the EEPROM
|
|
||||||
address = address + 1;
|
|
||||||
|
|
||||||
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
|
|
||||||
// on address 512, wrap around to address 0
|
|
||||||
if (address == 512)
|
|
||||||
address = 0;
|
|
||||||
|
|
||||||
delay(500);
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* EEPROM Write
|
|
||||||
*
|
|
||||||
* Stores values read from analog input 0 into the EEPROM.
|
|
||||||
* These values will stay in the EEPROM when the board is
|
|
||||||
* turned off and may be retrieved later by another sketch.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <EEPROM.h>
|
|
||||||
|
|
||||||
// the current address in the EEPROM (i.e. which byte
|
|
||||||
// we're going to write to next)
|
|
||||||
int addr = 0;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// need to divide by 4 because analog inputs range from
|
|
||||||
// 0 to 1023 and each byte of the EEPROM can only hold a
|
|
||||||
// value from 0 to 255.
|
|
||||||
int val = analogRead(0) / 4;
|
|
||||||
|
|
||||||
// write the value to the appropriate byte of the EEPROM.
|
|
||||||
// these values will remain there when the board is
|
|
||||||
// turned off.
|
|
||||||
EEPROM.write(addr, val);
|
|
||||||
|
|
||||||
// advance to the next address. there are 512 bytes in
|
|
||||||
// the EEPROM, so go back to 0 when we hit 512.
|
|
||||||
addr = addr + 1;
|
|
||||||
if (addr == 512)
|
|
||||||
addr = 0;
|
|
||||||
|
|
||||||
delay(100);
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
#######################################
|
|
||||||
# Syntax Coloring Map For Ultrasound
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
EEPROM KEYWORD1
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
name=EEPROM
|
|
||||||
version=1.0
|
|
||||||
author=Arduino
|
|
||||||
maintainer=Arduino <info@arduino.cc>
|
|
||||||
sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE.
|
|
||||||
paragraph=
|
|
||||||
url=http://arduino.cc/en/Reference/EEPROM
|
|
||||||
architectures=avr
|
|
@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
|
||||||
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
|
|
||||||
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
|
|
||||||
* SPI Master library for arduino.
|
|
||||||
*
|
|
||||||
* This file is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of either the GNU General Public License version 2
|
|
||||||
* or the GNU Lesser General Public License version 2.1, both as
|
|
||||||
* published by the Free Software Foundation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "SPI.h"
|
|
||||||
|
|
||||||
SPIClass SPI;
|
|
||||||
|
|
||||||
uint8_t SPIClass::interruptMode = 0;
|
|
||||||
uint8_t SPIClass::interruptMask = 0;
|
|
||||||
uint8_t SPIClass::interruptSave = 0;
|
|
||||||
#ifdef SPI_TRANSACTION_MISMATCH_LED
|
|
||||||
uint8_t SPIClass::inTransactionFlag = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void SPIClass::begin()
|
|
||||||
{
|
|
||||||
// Set SS to high so a connected chip will be "deselected" by default
|
|
||||||
digitalWrite(SS, HIGH);
|
|
||||||
|
|
||||||
// When the SS pin is set as OUTPUT, it can be used as
|
|
||||||
// a general purpose output port (it doesn't influence
|
|
||||||
// SPI operations).
|
|
||||||
pinMode(SS, OUTPUT);
|
|
||||||
|
|
||||||
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
|
|
||||||
// automatically switches to Slave, so the data direction of
|
|
||||||
// the SS pin MUST be kept as OUTPUT.
|
|
||||||
SPCR |= _BV(MSTR);
|
|
||||||
SPCR |= _BV(SPE);
|
|
||||||
|
|
||||||
// Set direction register for SCK and MOSI pin.
|
|
||||||
// MISO pin automatically overrides to INPUT.
|
|
||||||
// By doing this AFTER enabling SPI, we avoid accidentally
|
|
||||||
// clocking in a single bit since the lines go directly
|
|
||||||
// from "input" to SPI control.
|
|
||||||
// http://code.google.com/p/arduino/issues/detail?id=888
|
|
||||||
pinMode(SCK, OUTPUT);
|
|
||||||
pinMode(MOSI, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SPIClass::end() {
|
|
||||||
SPCR &= ~_BV(SPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// mapping of interrupt numbers to bits within SPI_AVR_EIMSK
|
|
||||||
#if defined(__AVR_ATmega32U4__)
|
|
||||||
#define SPI_INT0_MASK (1<<INT0)
|
|
||||||
#define SPI_INT1_MASK (1<<INT1)
|
|
||||||
#define SPI_INT2_MASK (1<<INT2)
|
|
||||||
#define SPI_INT3_MASK (1<<INT3)
|
|
||||||
#define SPI_INT4_MASK (1<<INT6)
|
|
||||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
|
|
||||||
#define SPI_INT0_MASK (1<<INT0)
|
|
||||||
#define SPI_INT1_MASK (1<<INT1)
|
|
||||||
#define SPI_INT2_MASK (1<<INT2)
|
|
||||||
#define SPI_INT3_MASK (1<<INT3)
|
|
||||||
#define SPI_INT4_MASK (1<<INT4)
|
|
||||||
#define SPI_INT5_MASK (1<<INT5)
|
|
||||||
#define SPI_INT6_MASK (1<<INT6)
|
|
||||||
#define SPI_INT7_MASK (1<<INT7)
|
|
||||||
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
|
||||||
#define SPI_INT0_MASK (1<<INT4)
|
|
||||||
#define SPI_INT1_MASK (1<<INT5)
|
|
||||||
#define SPI_INT2_MASK (1<<INT0)
|
|
||||||
#define SPI_INT3_MASK (1<<INT1)
|
|
||||||
#define SPI_INT4_MASK (1<<INT2)
|
|
||||||
#define SPI_INT5_MASK (1<<INT3)
|
|
||||||
#define SPI_INT6_MASK (1<<INT6)
|
|
||||||
#define SPI_INT7_MASK (1<<INT7)
|
|
||||||
#else
|
|
||||||
#ifdef INT0
|
|
||||||
#define SPI_INT0_MASK (1<<INT0)
|
|
||||||
#endif
|
|
||||||
#ifdef INT1
|
|
||||||
#define SPI_INT1_MASK (1<<INT1)
|
|
||||||
#endif
|
|
||||||
#ifdef INT2
|
|
||||||
#define SPI_INT2_MASK (1<<INT2)
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void SPIClass::usingInterrupt(uint8_t interruptNumber)
|
|
||||||
{
|
|
||||||
uint8_t mask;
|
|
||||||
|
|
||||||
if (interruptMode > 1) return;
|
|
||||||
|
|
||||||
noInterrupts();
|
|
||||||
switch (interruptNumber) {
|
|
||||||
#ifdef SPI_INT0_MASK
|
|
||||||
case 0: mask = SPI_INT0_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT1_MASK
|
|
||||||
case 1: mask = SPI_INT1_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT2_MASK
|
|
||||||
case 2: mask = SPI_INT2_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT3_MASK
|
|
||||||
case 3: mask = SPI_INT3_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT4_MASK
|
|
||||||
case 4: mask = SPI_INT4_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT5_MASK
|
|
||||||
case 5: mask = SPI_INT5_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT6_MASK
|
|
||||||
case 6: mask = SPI_INT6_MASK; break;
|
|
||||||
#endif
|
|
||||||
#ifdef SPI_INT7_MASK
|
|
||||||
case 7: mask = SPI_INT7_MASK; break;
|
|
||||||
#endif
|
|
||||||
default:
|
|
||||||
interruptMode = 2;
|
|
||||||
interrupts();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
interruptMode = 1;
|
|
||||||
interruptMask |= mask;
|
|
||||||
interrupts();
|
|
||||||
}
|
|
||||||
|
|
@ -1,284 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
|
|
||||||
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
|
|
||||||
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
|
|
||||||
* SPI Master library for arduino.
|
|
||||||
*
|
|
||||||
* This file is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of either the GNU General Public License version 2
|
|
||||||
* or the GNU Lesser General Public License version 2.1, both as
|
|
||||||
* published by the Free Software Foundation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _SPI_H_INCLUDED
|
|
||||||
#define _SPI_H_INCLUDED
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
// SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
|
|
||||||
// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
|
|
||||||
#define SPI_HAS_TRANSACTION 1
|
|
||||||
|
|
||||||
// Uncomment this line to add detection of mismatched begin/end transactions.
|
|
||||||
// A mismatch occurs if other libraries fail to use SPI.endTransaction() for
|
|
||||||
// each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
|
|
||||||
// on if any mismatch is ever detected.
|
|
||||||
//#define SPI_TRANSACTION_MISMATCH_LED 5
|
|
||||||
|
|
||||||
#ifndef LSBFIRST
|
|
||||||
#define LSBFIRST 0
|
|
||||||
#endif
|
|
||||||
#ifndef MSBFIRST
|
|
||||||
#define MSBFIRST 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define SPI_CLOCK_DIV4 0x00
|
|
||||||
#define SPI_CLOCK_DIV16 0x01
|
|
||||||
#define SPI_CLOCK_DIV64 0x02
|
|
||||||
#define SPI_CLOCK_DIV128 0x03
|
|
||||||
#define SPI_CLOCK_DIV2 0x04
|
|
||||||
#define SPI_CLOCK_DIV8 0x05
|
|
||||||
#define SPI_CLOCK_DIV32 0x06
|
|
||||||
|
|
||||||
#define SPI_MODE0 0x00
|
|
||||||
#define SPI_MODE1 0x04
|
|
||||||
#define SPI_MODE2 0x08
|
|
||||||
#define SPI_MODE3 0x0C
|
|
||||||
|
|
||||||
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
|
|
||||||
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
|
|
||||||
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
|
|
||||||
|
|
||||||
// define SPI_AVR_EIMSK for AVR boards with external interrupt pins
|
|
||||||
#if defined(EIMSK)
|
|
||||||
#define SPI_AVR_EIMSK EIMSK
|
|
||||||
#elif defined(GICR)
|
|
||||||
#define SPI_AVR_EIMSK GICR
|
|
||||||
#elif defined(GIMSK)
|
|
||||||
#define SPI_AVR_EIMSK GIMSK
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class SPISettings {
|
|
||||||
public:
|
|
||||||
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
|
|
||||||
if (__builtin_constant_p(clock)) {
|
|
||||||
init_AlwaysInline(clock, bitOrder, dataMode);
|
|
||||||
} else {
|
|
||||||
init_MightInline(clock, bitOrder, dataMode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SPISettings() {
|
|
||||||
init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
|
|
||||||
init_AlwaysInline(clock, bitOrder, dataMode);
|
|
||||||
}
|
|
||||||
void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
|
|
||||||
__attribute__((__always_inline__)) {
|
|
||||||
// Clock settings are defined as follows. Note that this shows SPI2X
|
|
||||||
// inverted, so the bits form increasing numbers. Also note that
|
|
||||||
// fosc/64 appears twice
|
|
||||||
// SPR1 SPR0 ~SPI2X Freq
|
|
||||||
// 0 0 0 fosc/2
|
|
||||||
// 0 0 1 fosc/4
|
|
||||||
// 0 1 0 fosc/8
|
|
||||||
// 0 1 1 fosc/16
|
|
||||||
// 1 0 0 fosc/32
|
|
||||||
// 1 0 1 fosc/64
|
|
||||||
// 1 1 0 fosc/64
|
|
||||||
// 1 1 1 fosc/128
|
|
||||||
|
|
||||||
// We find the fastest clock that is less than or equal to the
|
|
||||||
// given clock rate. The clock divider that results in clock_setting
|
|
||||||
// is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
|
|
||||||
// slowest (128 == 2 ^^ 7, so clock_div = 6).
|
|
||||||
uint8_t clockDiv;
|
|
||||||
|
|
||||||
// When the clock is known at compiletime, use this if-then-else
|
|
||||||
// cascade, which the compiler knows how to completely optimize
|
|
||||||
// away. When clock is not known, use a loop instead, which generates
|
|
||||||
// shorter code.
|
|
||||||
if (__builtin_constant_p(clock)) {
|
|
||||||
if (clock >= F_CPU / 2) {
|
|
||||||
clockDiv = 0;
|
|
||||||
} else if (clock >= F_CPU / 4) {
|
|
||||||
clockDiv = 1;
|
|
||||||
} else if (clock >= F_CPU / 8) {
|
|
||||||
clockDiv = 2;
|
|
||||||
} else if (clock >= F_CPU / 16) {
|
|
||||||
clockDiv = 3;
|
|
||||||
} else if (clock >= F_CPU / 32) {
|
|
||||||
clockDiv = 4;
|
|
||||||
} else if (clock >= F_CPU / 64) {
|
|
||||||
clockDiv = 5;
|
|
||||||
} else {
|
|
||||||
clockDiv = 6;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
uint32_t clockSetting = F_CPU / 2;
|
|
||||||
clockDiv = 0;
|
|
||||||
while (clockDiv < 6 && clock < clockSetting) {
|
|
||||||
clockSetting /= 2;
|
|
||||||
clockDiv++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compensate for the duplicate fosc/64
|
|
||||||
if (clockDiv == 6)
|
|
||||||
clockDiv = 7;
|
|
||||||
|
|
||||||
// Invert the SPI2X bit
|
|
||||||
clockDiv ^= 0x1;
|
|
||||||
|
|
||||||
// Pack into the SPISettings class
|
|
||||||
spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
|
|
||||||
(dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
|
|
||||||
spsr = clockDiv & SPI_2XCLOCK_MASK;
|
|
||||||
}
|
|
||||||
uint8_t spcr;
|
|
||||||
uint8_t spsr;
|
|
||||||
friend class SPIClass;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class SPIClass {
|
|
||||||
public:
|
|
||||||
// Initialize the SPI library
|
|
||||||
static void begin();
|
|
||||||
|
|
||||||
// If SPI is used from within an interrupt, this function registers
|
|
||||||
// that interrupt with the SPI library, so beginTransaction() can
|
|
||||||
// prevent conflicts. The input interruptNumber is the number used
|
|
||||||
// with attachInterrupt. If SPI is used from a different interrupt
|
|
||||||
// (eg, a timer), interruptNumber should be 255.
|
|
||||||
static void usingInterrupt(uint8_t interruptNumber);
|
|
||||||
|
|
||||||
// Before using SPI.transfer() or asserting chip select pins,
|
|
||||||
// this function is used to gain exclusive access to the SPI bus
|
|
||||||
// and configure the correct settings.
|
|
||||||
inline static void beginTransaction(SPISettings settings) {
|
|
||||||
if (interruptMode > 0) {
|
|
||||||
#ifdef SPI_AVR_EIMSK
|
|
||||||
if (interruptMode == 1) {
|
|
||||||
interruptSave = SPI_AVR_EIMSK;
|
|
||||||
SPI_AVR_EIMSK &= ~interruptMask;
|
|
||||||
} else
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
interruptSave = SREG;
|
|
||||||
cli();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef SPI_TRANSACTION_MISMATCH_LED
|
|
||||||
if (inTransactionFlag) {
|
|
||||||
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
|
|
||||||
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
|
|
||||||
}
|
|
||||||
inTransactionFlag = 1;
|
|
||||||
#endif
|
|
||||||
SPCR = settings.spcr;
|
|
||||||
SPSR = settings.spsr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to the SPI bus (MOSI pin) and also receive (MISO pin)
|
|
||||||
inline static uint8_t transfer(uint8_t data) {
|
|
||||||
SPDR = data;
|
|
||||||
asm volatile("nop");
|
|
||||||
while (!(SPSR & _BV(SPIF))) ; // wait
|
|
||||||
return SPDR;
|
|
||||||
}
|
|
||||||
inline static uint16_t transfer16(uint16_t data) {
|
|
||||||
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
|
|
||||||
in.val = data;
|
|
||||||
if (!(SPCR & _BV(DORD))) {
|
|
||||||
SPDR = in.msb;
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
out.msb = SPDR;
|
|
||||||
SPDR = in.lsb;
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
out.lsb = SPDR;
|
|
||||||
} else {
|
|
||||||
SPDR = in.lsb;
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
out.lsb = SPDR;
|
|
||||||
SPDR = in.msb;
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
out.msb = SPDR;
|
|
||||||
}
|
|
||||||
return out.val;
|
|
||||||
}
|
|
||||||
inline static void transfer(void *buf, size_t count) {
|
|
||||||
if (count == 0) return;
|
|
||||||
uint8_t *p = (uint8_t *)buf;
|
|
||||||
SPDR = *p;
|
|
||||||
while (--count > 0) {
|
|
||||||
uint8_t out = *(p + 1);
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
uint8_t in = SPDR;
|
|
||||||
SPDR = out;
|
|
||||||
*p++ = in;
|
|
||||||
}
|
|
||||||
while (!(SPSR & _BV(SPIF))) ;
|
|
||||||
*p = SPDR;
|
|
||||||
}
|
|
||||||
// After performing a group of transfers and releasing the chip select
|
|
||||||
// signal, this function allows others to access the SPI bus
|
|
||||||
inline static void endTransaction(void) {
|
|
||||||
#ifdef SPI_TRANSACTION_MISMATCH_LED
|
|
||||||
if (!inTransactionFlag) {
|
|
||||||
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
|
|
||||||
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
|
|
||||||
}
|
|
||||||
inTransactionFlag = 0;
|
|
||||||
#endif
|
|
||||||
if (interruptMode > 0) {
|
|
||||||
#ifdef SPI_AVR_EIMSK
|
|
||||||
if (interruptMode == 1) {
|
|
||||||
SPI_AVR_EIMSK = interruptSave;
|
|
||||||
} else
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
SREG = interruptSave;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disable the SPI bus
|
|
||||||
static void end();
|
|
||||||
|
|
||||||
// This function is deprecated. New applications should use
|
|
||||||
// beginTransaction() to configure SPI settings.
|
|
||||||
inline static void setBitOrder(uint8_t bitOrder) {
|
|
||||||
if (bitOrder == LSBFIRST) SPCR |= _BV(DORD);
|
|
||||||
else SPCR &= ~(_BV(DORD));
|
|
||||||
}
|
|
||||||
// This function is deprecated. New applications should use
|
|
||||||
// beginTransaction() to configure SPI settings.
|
|
||||||
inline static void setDataMode(uint8_t dataMode) {
|
|
||||||
SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
|
|
||||||
}
|
|
||||||
// This function is deprecated. New applications should use
|
|
||||||
// beginTransaction() to configure SPI settings.
|
|
||||||
inline static void setClockDivider(uint8_t clockDiv) {
|
|
||||||
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK);
|
|
||||||
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK);
|
|
||||||
}
|
|
||||||
// These undocumented functions should not be used. SPI.transfer()
|
|
||||||
// polls the hardware flag which is automatically cleared as the
|
|
||||||
// AVR responds to SPI's interrupt
|
|
||||||
inline static void attachInterrupt() { SPCR |= _BV(SPIE); }
|
|
||||||
inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
static uint8_t interruptMode; // 0=none, 1=mask, 2=global
|
|
||||||
static uint8_t interruptMask; // which interrupts to mask
|
|
||||||
static uint8_t interruptSave; // temp storage, to restore state
|
|
||||||
#ifdef SPI_TRANSACTION_MISMATCH_LED
|
|
||||||
static uint8_t inTransactionFlag;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
extern SPIClass SPI;
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,143 +0,0 @@
|
|||||||
/*
|
|
||||||
SCP1000 Barometric Pressure Sensor Display
|
|
||||||
|
|
||||||
Shows the output of a Barometric Pressure Sensor on a
|
|
||||||
Uses the SPI library. For details on the sensor, see:
|
|
||||||
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
|
||||||
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
|
||||||
|
|
||||||
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
|
||||||
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
|
||||||
|
|
||||||
Circuit:
|
|
||||||
SCP1000 sensor attached to pins 6, 7, 10 - 13:
|
|
||||||
DRDY: pin 6
|
|
||||||
CSB: pin 7
|
|
||||||
MOSI: pin 11
|
|
||||||
MISO: pin 12
|
|
||||||
SCK: pin 13
|
|
||||||
|
|
||||||
created 31 July 2010
|
|
||||||
modified 14 August 2010
|
|
||||||
by Tom Igoe
|
|
||||||
*/
|
|
||||||
|
|
||||||
// the sensor communicates using SPI, so include the library:
|
|
||||||
#include <SPI.h>
|
|
||||||
|
|
||||||
//Sensor's memory register addresses:
|
|
||||||
const int PRESSURE = 0x1F; //3 most significant bits of pressure
|
|
||||||
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
|
|
||||||
const int TEMPERATURE = 0x21; //16 bit temperature reading
|
|
||||||
const byte READ = 0b11111100; // SCP1000's read command
|
|
||||||
const byte WRITE = 0b00000010; // SCP1000's write command
|
|
||||||
|
|
||||||
// pins used for the connection with the sensor
|
|
||||||
// the other you need are controlled by the SPI library):
|
|
||||||
const int dataReadyPin = 6;
|
|
||||||
const int chipSelectPin = 7;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// start the SPI library:
|
|
||||||
SPI.begin();
|
|
||||||
|
|
||||||
// initalize the data ready and chip select pins:
|
|
||||||
pinMode(dataReadyPin, INPUT);
|
|
||||||
pinMode(chipSelectPin, OUTPUT);
|
|
||||||
|
|
||||||
//Configure SCP1000 for low noise configuration:
|
|
||||||
writeRegister(0x02, 0x2D);
|
|
||||||
writeRegister(0x01, 0x03);
|
|
||||||
writeRegister(0x03, 0x02);
|
|
||||||
// give the sensor time to set up:
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
//Select High Resolution Mode
|
|
||||||
writeRegister(0x03, 0x0A);
|
|
||||||
|
|
||||||
// don't do anything until the data ready pin is high:
|
|
||||||
if (digitalRead(dataReadyPin) == HIGH) {
|
|
||||||
//Read the temperature data
|
|
||||||
int tempData = readRegister(0x21, 2);
|
|
||||||
|
|
||||||
// convert the temperature to celsius and display it:
|
|
||||||
float realTemp = (float)tempData / 20.0;
|
|
||||||
Serial.print("Temp[C]=");
|
|
||||||
Serial.print(realTemp);
|
|
||||||
|
|
||||||
|
|
||||||
//Read the pressure data highest 3 bits:
|
|
||||||
byte pressure_data_high = readRegister(0x1F, 1);
|
|
||||||
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
|
|
||||||
|
|
||||||
//Read the pressure data lower 16 bits:
|
|
||||||
unsigned int pressure_data_low = readRegister(0x20, 2);
|
|
||||||
//combine the two parts into one 19-bit number:
|
|
||||||
long pressure = ((pressure_data_high << 16) | pressure_data_low) / 4;
|
|
||||||
|
|
||||||
// display the temperature:
|
|
||||||
Serial.println("\tPressure [Pa]=" + String(pressure));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Read from or write to register from the SCP1000:
|
|
||||||
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
|
||||||
byte inByte = 0; // incoming byte from the SPI
|
|
||||||
unsigned int result = 0; // result to return
|
|
||||||
Serial.print(thisRegister, BIN);
|
|
||||||
Serial.print("\t");
|
|
||||||
// SCP1000 expects the register name in the upper 6 bits
|
|
||||||
// of the byte. So shift the bits left by two bits:
|
|
||||||
thisRegister = thisRegister << 2;
|
|
||||||
// now combine the address and the command into one byte
|
|
||||||
byte dataToSend = thisRegister & READ;
|
|
||||||
Serial.println(thisRegister, BIN);
|
|
||||||
// take the chip select low to select the device:
|
|
||||||
digitalWrite(chipSelectPin, LOW);
|
|
||||||
// send the device the register you want to read:
|
|
||||||
SPI.transfer(dataToSend);
|
|
||||||
// send a value of 0 to read the first byte returned:
|
|
||||||
result = SPI.transfer(0x00);
|
|
||||||
// decrement the number of bytes left to read:
|
|
||||||
bytesToRead--;
|
|
||||||
// if you still have another byte to read:
|
|
||||||
if (bytesToRead > 0) {
|
|
||||||
// shift the first byte left, then get the second byte:
|
|
||||||
result = result << 8;
|
|
||||||
inByte = SPI.transfer(0x00);
|
|
||||||
// combine the byte you just got with the previous one:
|
|
||||||
result = result | inByte;
|
|
||||||
// decrement the number of bytes left to read:
|
|
||||||
bytesToRead--;
|
|
||||||
}
|
|
||||||
// take the chip select high to de-select:
|
|
||||||
digitalWrite(chipSelectPin, HIGH);
|
|
||||||
// return the result:
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Sends a write command to SCP1000
|
|
||||||
|
|
||||||
void writeRegister(byte thisRegister, byte thisValue) {
|
|
||||||
|
|
||||||
// SCP1000 expects the register address in the upper 6 bits
|
|
||||||
// of the byte. So shift the bits left by two bits:
|
|
||||||
thisRegister = thisRegister << 2;
|
|
||||||
// now combine the register address and the command into one byte:
|
|
||||||
byte dataToSend = thisRegister | WRITE;
|
|
||||||
|
|
||||||
// take the chip select low to select the device:
|
|
||||||
digitalWrite(chipSelectPin, LOW);
|
|
||||||
|
|
||||||
SPI.transfer(dataToSend); //Send register location
|
|
||||||
SPI.transfer(thisValue); //Send value to record into register
|
|
||||||
|
|
||||||
// take the chip select high to de-select:
|
|
||||||
digitalWrite(chipSelectPin, HIGH);
|
|
||||||
}
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
Digital Pot Control
|
|
||||||
|
|
||||||
This example controls an Analog Devices AD5206 digital potentiometer.
|
|
||||||
The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
|
|
||||||
A - connect this to voltage
|
|
||||||
W - this is the pot's wiper, which changes when you set it
|
|
||||||
B - connect this to ground.
|
|
||||||
|
|
||||||
The AD5206 is SPI-compatible,and to command it, you send two bytes,
|
|
||||||
one with the channel number (0 - 5) and one with the resistance value for the
|
|
||||||
channel (0 - 255).
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* All A pins of AD5206 connected to +5V
|
|
||||||
* All B pins of AD5206 connected to ground
|
|
||||||
* An LED and a 220-ohm resisor in series connected from each W pin to ground
|
|
||||||
* CS - to digital pin 10 (SS pin)
|
|
||||||
* SDI - to digital pin 11 (MOSI pin)
|
|
||||||
* CLK - to digital pin 13 (SCK pin)
|
|
||||||
|
|
||||||
created 10 Aug 2010
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// inslude the SPI library:
|
|
||||||
#include <SPI.h>
|
|
||||||
|
|
||||||
|
|
||||||
// set pin 10 as the slave select for the digital pot:
|
|
||||||
const int slaveSelectPin = 10;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// set the slaveSelectPin as an output:
|
|
||||||
pinMode (slaveSelectPin, OUTPUT);
|
|
||||||
// initialize SPI:
|
|
||||||
SPI.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// go through the six channels of the digital pot:
|
|
||||||
for (int channel = 0; channel < 6; channel++) {
|
|
||||||
// change the resistance on this channel from min to max:
|
|
||||||
for (int level = 0; level < 255; level++) {
|
|
||||||
digitalPotWrite(channel, level);
|
|
||||||
delay(10);
|
|
||||||
}
|
|
||||||
// wait a second at the top:
|
|
||||||
delay(100);
|
|
||||||
// change the resistance on this channel from max to min:
|
|
||||||
for (int level = 0; level < 255; level++) {
|
|
||||||
digitalPotWrite(channel, 255 - level);
|
|
||||||
delay(10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void digitalPotWrite(int address, int value) {
|
|
||||||
// take the SS pin low to select the chip:
|
|
||||||
digitalWrite(slaveSelectPin, LOW);
|
|
||||||
// send in the address and value via SPI:
|
|
||||||
SPI.transfer(address);
|
|
||||||
SPI.transfer(value);
|
|
||||||
// take the SS pin high to de-select the chip:
|
|
||||||
digitalWrite(slaveSelectPin, HIGH);
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
#######################################
|
|
||||||
# Syntax Coloring Map SPI
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
SPI KEYWORD1
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
begin KEYWORD2
|
|
||||||
end KEYWORD2
|
|
||||||
transfer KEYWORD2
|
|
||||||
setBitOrder KEYWORD2
|
|
||||||
setDataMode KEYWORD2
|
|
||||||
setClockDivider KEYWORD2
|
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
SPI_CLOCK_DIV4 LITERAL1
|
|
||||||
SPI_CLOCK_DIV16 LITERAL1
|
|
||||||
SPI_CLOCK_DIV64 LITERAL1
|
|
||||||
SPI_CLOCK_DIV128 LITERAL1
|
|
||||||
SPI_CLOCK_DIV2 LITERAL1
|
|
||||||
SPI_CLOCK_DIV8 LITERAL1
|
|
||||||
SPI_CLOCK_DIV32 LITERAL1
|
|
||||||
SPI_CLOCK_DIV64 LITERAL1
|
|
||||||
SPI_MODE0 LITERAL1
|
|
||||||
SPI_MODE1 LITERAL1
|
|
||||||
SPI_MODE2 LITERAL1
|
|
||||||
SPI_MODE3 LITERAL1
|
|
@ -1,8 +0,0 @@
|
|||||||
name=SPI
|
|
||||||
version=1.0
|
|
||||||
author=Arduino
|
|
||||||
maintainer=Arduino <info@arduino.cc>
|
|
||||||
sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. For all Arduino boards, BUT Arduino DUE.
|
|
||||||
paragraph=
|
|
||||||
url=http://arduino.cc/en/Reference/SPI
|
|
||||||
architectures=avr
|
|
@ -1,518 +0,0 @@
|
|||||||
/*
|
|
||||||
SoftwareSerial.cpp (formerly NewSoftSerial.cpp) -
|
|
||||||
Multi-instance software serial library for Arduino/Wiring
|
|
||||||
-- Interrupt-driven receive and other improvements by ladyada
|
|
||||||
(http://ladyada.net)
|
|
||||||
-- Tuning, circular buffer, derivation from class Print/Stream,
|
|
||||||
multi-instance support, porting to 8MHz processors,
|
|
||||||
various optimizations, PROGMEM delay tables, inverse logic and
|
|
||||||
direct port writing by Mikal Hart (http://www.arduiniana.org)
|
|
||||||
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
|
|
||||||
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
|
|
||||||
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
The latest version of this library can always be found at
|
|
||||||
http://arduiniana.org.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// When set, _DEBUG co-opts pins 11 and 13 for debugging with an
|
|
||||||
// oscilloscope or logic analyzer. Beware: it also slightly modifies
|
|
||||||
// the bit times, so don't rely on it too much at high baud rates
|
|
||||||
#define _DEBUG 0
|
|
||||||
#define _DEBUG_PIN1 11
|
|
||||||
#define _DEBUG_PIN2 13
|
|
||||||
//
|
|
||||||
// Includes
|
|
||||||
//
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <SoftwareSerial.h>
|
|
||||||
//
|
|
||||||
// Lookup table
|
|
||||||
//
|
|
||||||
typedef struct _DELAY_TABLE
|
|
||||||
{
|
|
||||||
long baud;
|
|
||||||
unsigned short rx_delay_centering;
|
|
||||||
unsigned short rx_delay_intrabit;
|
|
||||||
unsigned short rx_delay_stopbit;
|
|
||||||
unsigned short tx_delay;
|
|
||||||
} DELAY_TABLE;
|
|
||||||
|
|
||||||
#if F_CPU == 16000000
|
|
||||||
|
|
||||||
static const DELAY_TABLE PROGMEM table[] =
|
|
||||||
{
|
|
||||||
// baud rxcenter rxintra rxstop tx
|
|
||||||
{ 115200, 1, 17, 17, 12, },
|
|
||||||
{ 57600, 10, 37, 37, 33, },
|
|
||||||
{ 38400, 25, 57, 57, 54, },
|
|
||||||
{ 31250, 31, 70, 70, 68, },
|
|
||||||
{ 28800, 34, 77, 77, 74, },
|
|
||||||
{ 19200, 54, 117, 117, 114, },
|
|
||||||
{ 14400, 74, 156, 156, 153, },
|
|
||||||
{ 9600, 114, 236, 236, 233, },
|
|
||||||
{ 4800, 233, 474, 474, 471, },
|
|
||||||
{ 2400, 471, 950, 950, 947, },
|
|
||||||
{ 1200, 947, 1902, 1902, 1899, },
|
|
||||||
{ 600, 1902, 3804, 3804, 3800, },
|
|
||||||
{ 300, 3804, 7617, 7617, 7614, },
|
|
||||||
};
|
|
||||||
|
|
||||||
const int XMIT_START_ADJUSTMENT = 5;
|
|
||||||
|
|
||||||
#elif F_CPU == 8000000
|
|
||||||
|
|
||||||
static const DELAY_TABLE table[] PROGMEM =
|
|
||||||
{
|
|
||||||
// baud rxcenter rxintra rxstop tx
|
|
||||||
{ 115200, 1, 5, 5, 3, },
|
|
||||||
{ 57600, 1, 15, 15, 13, },
|
|
||||||
{ 38400, 2, 25, 26, 23, },
|
|
||||||
{ 31250, 7, 32, 33, 29, },
|
|
||||||
{ 28800, 11, 35, 35, 32, },
|
|
||||||
{ 19200, 20, 55, 55, 52, },
|
|
||||||
{ 14400, 30, 75, 75, 72, },
|
|
||||||
{ 9600, 50, 114, 114, 112, },
|
|
||||||
{ 4800, 110, 233, 233, 230, },
|
|
||||||
{ 2400, 229, 472, 472, 469, },
|
|
||||||
{ 1200, 467, 948, 948, 945, },
|
|
||||||
{ 600, 948, 1895, 1895, 1890, },
|
|
||||||
{ 300, 1895, 3805, 3805, 3802, },
|
|
||||||
};
|
|
||||||
|
|
||||||
const int XMIT_START_ADJUSTMENT = 4;
|
|
||||||
|
|
||||||
#elif F_CPU == 20000000
|
|
||||||
|
|
||||||
// 20MHz support courtesy of the good people at macegr.com.
|
|
||||||
// Thanks, Garrett!
|
|
||||||
|
|
||||||
static const DELAY_TABLE PROGMEM table[] =
|
|
||||||
{
|
|
||||||
// baud rxcenter rxintra rxstop tx
|
|
||||||
{ 115200, 3, 21, 21, 18, },
|
|
||||||
{ 57600, 20, 43, 43, 41, },
|
|
||||||
{ 38400, 37, 73, 73, 70, },
|
|
||||||
{ 31250, 45, 89, 89, 88, },
|
|
||||||
{ 28800, 46, 98, 98, 95, },
|
|
||||||
{ 19200, 71, 148, 148, 145, },
|
|
||||||
{ 14400, 96, 197, 197, 194, },
|
|
||||||
{ 9600, 146, 297, 297, 294, },
|
|
||||||
{ 4800, 296, 595, 595, 592, },
|
|
||||||
{ 2400, 592, 1189, 1189, 1186, },
|
|
||||||
{ 1200, 1187, 2379, 2379, 2376, },
|
|
||||||
{ 600, 2379, 4759, 4759, 4755, },
|
|
||||||
{ 300, 4759, 9523, 9523, 9520, },
|
|
||||||
};
|
|
||||||
|
|
||||||
const int XMIT_START_ADJUSTMENT = 6;
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
|
||||||
// Statics
|
|
||||||
//
|
|
||||||
SoftwareSerial *SoftwareSerial::active_object = 0;
|
|
||||||
char SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF];
|
|
||||||
volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0;
|
|
||||||
volatile uint8_t SoftwareSerial::_receive_buffer_head = 0;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Debugging
|
|
||||||
//
|
|
||||||
// This function generates a brief pulse
|
|
||||||
// for debugging or measuring on an oscilloscope.
|
|
||||||
inline void DebugPulse(uint8_t pin, uint8_t count)
|
|
||||||
{
|
|
||||||
#if _DEBUG
|
|
||||||
volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
|
|
||||||
|
|
||||||
uint8_t val = *pport;
|
|
||||||
while (count--)
|
|
||||||
{
|
|
||||||
*pport = val | digitalPinToBitMask(pin);
|
|
||||||
*pport = val;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Private methods
|
|
||||||
//
|
|
||||||
|
|
||||||
/* static */
|
|
||||||
inline void SoftwareSerial::tunedDelay(uint16_t delay) {
|
|
||||||
uint8_t tmp=0;
|
|
||||||
|
|
||||||
asm volatile("sbiw %0, 0x01 \n\t"
|
|
||||||
"ldi %1, 0xFF \n\t"
|
|
||||||
"cpi %A0, 0xFF \n\t"
|
|
||||||
"cpc %B0, %1 \n\t"
|
|
||||||
"brne .-10 \n\t"
|
|
||||||
: "+r" (delay), "+a" (tmp)
|
|
||||||
: "0" (delay)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function sets the current object as the "listening"
|
|
||||||
// one and returns true if it replaces another
|
|
||||||
bool SoftwareSerial::listen()
|
|
||||||
{
|
|
||||||
if (active_object != this)
|
|
||||||
{
|
|
||||||
_buffer_overflow = false;
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
_receive_buffer_head = _receive_buffer_tail = 0;
|
|
||||||
active_object = this;
|
|
||||||
SREG = oldSREG;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// The receive routine called by the interrupt handler
|
|
||||||
//
|
|
||||||
void SoftwareSerial::recv()
|
|
||||||
{
|
|
||||||
|
|
||||||
#if GCC_VERSION < 40302
|
|
||||||
// Work-around for avr-gcc 4.3.0 OSX version bug
|
|
||||||
// Preserve the registers that the compiler misses
|
|
||||||
// (courtesy of Arduino forum user *etracer*)
|
|
||||||
asm volatile(
|
|
||||||
"push r18 \n\t"
|
|
||||||
"push r19 \n\t"
|
|
||||||
"push r20 \n\t"
|
|
||||||
"push r21 \n\t"
|
|
||||||
"push r22 \n\t"
|
|
||||||
"push r23 \n\t"
|
|
||||||
"push r26 \n\t"
|
|
||||||
"push r27 \n\t"
|
|
||||||
::);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t d = 0;
|
|
||||||
|
|
||||||
// If RX line is high, then we don't see any start bit
|
|
||||||
// so interrupt is probably not for us
|
|
||||||
if (_inverse_logic ? rx_pin_read() : !rx_pin_read())
|
|
||||||
{
|
|
||||||
// Wait approximately 1/2 of a bit width to "center" the sample
|
|
||||||
tunedDelay(_rx_delay_centering);
|
|
||||||
DebugPulse(_DEBUG_PIN2, 1);
|
|
||||||
|
|
||||||
// Read each of the 8 bits
|
|
||||||
for (uint8_t i=0x1; i; i <<= 1)
|
|
||||||
{
|
|
||||||
tunedDelay(_rx_delay_intrabit);
|
|
||||||
DebugPulse(_DEBUG_PIN2, 1);
|
|
||||||
uint8_t noti = ~i;
|
|
||||||
if (rx_pin_read())
|
|
||||||
d |= i;
|
|
||||||
else // else clause added to ensure function timing is ~balanced
|
|
||||||
d &= noti;
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip the stop bit
|
|
||||||
tunedDelay(_rx_delay_stopbit);
|
|
||||||
DebugPulse(_DEBUG_PIN2, 1);
|
|
||||||
|
|
||||||
if (_inverse_logic)
|
|
||||||
d = ~d;
|
|
||||||
|
|
||||||
// if buffer full, set the overflow flag and return
|
|
||||||
if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head)
|
|
||||||
{
|
|
||||||
// save new data in buffer: tail points to where byte goes
|
|
||||||
_receive_buffer[_receive_buffer_tail] = d; // save new byte
|
|
||||||
_receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#if _DEBUG // for scope: pulse pin as overflow indictator
|
|
||||||
DebugPulse(_DEBUG_PIN1, 1);
|
|
||||||
#endif
|
|
||||||
_buffer_overflow = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if GCC_VERSION < 40302
|
|
||||||
// Work-around for avr-gcc 4.3.0 OSX version bug
|
|
||||||
// Restore the registers that the compiler misses
|
|
||||||
asm volatile(
|
|
||||||
"pop r27 \n\t"
|
|
||||||
"pop r26 \n\t"
|
|
||||||
"pop r23 \n\t"
|
|
||||||
"pop r22 \n\t"
|
|
||||||
"pop r21 \n\t"
|
|
||||||
"pop r20 \n\t"
|
|
||||||
"pop r19 \n\t"
|
|
||||||
"pop r18 \n\t"
|
|
||||||
::);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoftwareSerial::tx_pin_write(uint8_t pin_state)
|
|
||||||
{
|
|
||||||
if (pin_state == LOW)
|
|
||||||
*_transmitPortRegister &= ~_transmitBitMask;
|
|
||||||
else
|
|
||||||
*_transmitPortRegister |= _transmitBitMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t SoftwareSerial::rx_pin_read()
|
|
||||||
{
|
|
||||||
return *_receivePortRegister & _receiveBitMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Interrupt handling
|
|
||||||
//
|
|
||||||
|
|
||||||
/* static */
|
|
||||||
inline void SoftwareSerial::handle_interrupt()
|
|
||||||
{
|
|
||||||
if (active_object)
|
|
||||||
{
|
|
||||||
active_object->recv();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(PCINT0_vect)
|
|
||||||
ISR(PCINT0_vect)
|
|
||||||
{
|
|
||||||
SoftwareSerial::handle_interrupt();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(PCINT1_vect)
|
|
||||||
ISR(PCINT1_vect)
|
|
||||||
{
|
|
||||||
SoftwareSerial::handle_interrupt();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(PCINT2_vect)
|
|
||||||
ISR(PCINT2_vect)
|
|
||||||
{
|
|
||||||
SoftwareSerial::handle_interrupt();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(PCINT3_vect)
|
|
||||||
ISR(PCINT3_vect)
|
|
||||||
{
|
|
||||||
SoftwareSerial::handle_interrupt();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
|
||||||
// Constructor
|
|
||||||
//
|
|
||||||
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
|
|
||||||
_rx_delay_centering(0),
|
|
||||||
_rx_delay_intrabit(0),
|
|
||||||
_rx_delay_stopbit(0),
|
|
||||||
_tx_delay(0),
|
|
||||||
_buffer_overflow(false),
|
|
||||||
_inverse_logic(inverse_logic)
|
|
||||||
{
|
|
||||||
setTX(transmitPin);
|
|
||||||
setRX(receivePin);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Destructor
|
|
||||||
//
|
|
||||||
SoftwareSerial::~SoftwareSerial()
|
|
||||||
{
|
|
||||||
end();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoftwareSerial::setTX(uint8_t tx)
|
|
||||||
{
|
|
||||||
pinMode(tx, OUTPUT);
|
|
||||||
digitalWrite(tx, _inverse_logic ? LOW : HIGH);
|
|
||||||
_transmitBitMask = digitalPinToBitMask(tx);
|
|
||||||
uint8_t port = digitalPinToPort(tx);
|
|
||||||
_transmitPortRegister = portOutputRegister(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoftwareSerial::setRX(uint8_t rx)
|
|
||||||
{
|
|
||||||
pinMode(rx, INPUT);
|
|
||||||
if (!_inverse_logic)
|
|
||||||
digitalWrite(rx, HIGH); // pullup for normal logic!
|
|
||||||
_receivePin = rx;
|
|
||||||
_receiveBitMask = digitalPinToBitMask(rx);
|
|
||||||
uint8_t port = digitalPinToPort(rx);
|
|
||||||
_receivePortRegister = portInputRegister(port);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Public methods
|
|
||||||
//
|
|
||||||
|
|
||||||
void SoftwareSerial::begin(long speed)
|
|
||||||
{
|
|
||||||
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
|
|
||||||
|
|
||||||
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
|
|
||||||
{
|
|
||||||
long baud = pgm_read_dword(&table[i].baud);
|
|
||||||
if (baud == speed)
|
|
||||||
{
|
|
||||||
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
|
|
||||||
_rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
|
|
||||||
_rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
|
|
||||||
_tx_delay = pgm_read_word(&table[i].tx_delay);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up RX interrupts, but only if we have a valid RX baud rate
|
|
||||||
if (_rx_delay_stopbit)
|
|
||||||
{
|
|
||||||
if (digitalPinToPCICR(_receivePin))
|
|
||||||
{
|
|
||||||
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
|
|
||||||
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
|
|
||||||
}
|
|
||||||
tunedDelay(_tx_delay); // if we were low this establishes the end
|
|
||||||
}
|
|
||||||
|
|
||||||
#if _DEBUG
|
|
||||||
pinMode(_DEBUG_PIN1, OUTPUT);
|
|
||||||
pinMode(_DEBUG_PIN2, OUTPUT);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
listen();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoftwareSerial::end()
|
|
||||||
{
|
|
||||||
if (digitalPinToPCMSK(_receivePin))
|
|
||||||
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Read data from buffer
|
|
||||||
int SoftwareSerial::read()
|
|
||||||
{
|
|
||||||
if (!isListening())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// Empty buffer?
|
|
||||||
if (_receive_buffer_head == _receive_buffer_tail)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// Read from "head"
|
|
||||||
uint8_t d = _receive_buffer[_receive_buffer_head]; // grab next byte
|
|
||||||
_receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF;
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SoftwareSerial::available()
|
|
||||||
{
|
|
||||||
if (!isListening())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t SoftwareSerial::write(uint8_t b)
|
|
||||||
{
|
|
||||||
if (_tx_delay == 0) {
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli(); // turn off interrupts for a clean txmit
|
|
||||||
|
|
||||||
// Write the start bit
|
|
||||||
tx_pin_write(_inverse_logic ? HIGH : LOW);
|
|
||||||
tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);
|
|
||||||
|
|
||||||
// Write each of the 8 bits
|
|
||||||
if (_inverse_logic)
|
|
||||||
{
|
|
||||||
for (byte mask = 0x01; mask; mask <<= 1)
|
|
||||||
{
|
|
||||||
if (b & mask) // choose bit
|
|
||||||
tx_pin_write(LOW); // send 1
|
|
||||||
else
|
|
||||||
tx_pin_write(HIGH); // send 0
|
|
||||||
|
|
||||||
tunedDelay(_tx_delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
tx_pin_write(LOW); // restore pin to natural state
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (byte mask = 0x01; mask; mask <<= 1)
|
|
||||||
{
|
|
||||||
if (b & mask) // choose bit
|
|
||||||
tx_pin_write(HIGH); // send 1
|
|
||||||
else
|
|
||||||
tx_pin_write(LOW); // send 0
|
|
||||||
|
|
||||||
tunedDelay(_tx_delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
tx_pin_write(HIGH); // restore pin to natural state
|
|
||||||
}
|
|
||||||
|
|
||||||
SREG = oldSREG; // turn interrupts back on
|
|
||||||
tunedDelay(_tx_delay);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SoftwareSerial::flush()
|
|
||||||
{
|
|
||||||
if (!isListening())
|
|
||||||
return;
|
|
||||||
|
|
||||||
uint8_t oldSREG = SREG;
|
|
||||||
cli();
|
|
||||||
_receive_buffer_head = _receive_buffer_tail = 0;
|
|
||||||
SREG = oldSREG;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SoftwareSerial::peek()
|
|
||||||
{
|
|
||||||
if (!isListening())
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// Empty buffer?
|
|
||||||
if (_receive_buffer_head == _receive_buffer_tail)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// Read from "head"
|
|
||||||
return _receive_buffer[_receive_buffer_head];
|
|
||||||
}
|
|
@ -1,112 +0,0 @@
|
|||||||
/*
|
|
||||||
SoftwareSerial.h (formerly NewSoftSerial.h) -
|
|
||||||
Multi-instance software serial library for Arduino/Wiring
|
|
||||||
-- Interrupt-driven receive and other improvements by ladyada
|
|
||||||
(http://ladyada.net)
|
|
||||||
-- Tuning, circular buffer, derivation from class Print/Stream,
|
|
||||||
multi-instance support, porting to 8MHz processors,
|
|
||||||
various optimizations, PROGMEM delay tables, inverse logic and
|
|
||||||
direct port writing by Mikal Hart (http://www.arduiniana.org)
|
|
||||||
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
|
|
||||||
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
|
|
||||||
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
The latest version of this library can always be found at
|
|
||||||
http://arduiniana.org.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef SoftwareSerial_h
|
|
||||||
#define SoftwareSerial_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* Definitions
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#define _SS_MAX_RX_BUFF 64 // RX buffer size
|
|
||||||
#ifndef GCC_VERSION
|
|
||||||
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class SoftwareSerial : public Stream
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
// per object data
|
|
||||||
uint8_t _receivePin;
|
|
||||||
uint8_t _receiveBitMask;
|
|
||||||
volatile uint8_t *_receivePortRegister;
|
|
||||||
uint8_t _transmitBitMask;
|
|
||||||
volatile uint8_t *_transmitPortRegister;
|
|
||||||
|
|
||||||
uint16_t _rx_delay_centering;
|
|
||||||
uint16_t _rx_delay_intrabit;
|
|
||||||
uint16_t _rx_delay_stopbit;
|
|
||||||
uint16_t _tx_delay;
|
|
||||||
|
|
||||||
uint16_t _buffer_overflow:1;
|
|
||||||
uint16_t _inverse_logic:1;
|
|
||||||
|
|
||||||
// static data
|
|
||||||
static char _receive_buffer[_SS_MAX_RX_BUFF];
|
|
||||||
static volatile uint8_t _receive_buffer_tail;
|
|
||||||
static volatile uint8_t _receive_buffer_head;
|
|
||||||
static SoftwareSerial *active_object;
|
|
||||||
|
|
||||||
// private methods
|
|
||||||
void recv();
|
|
||||||
uint8_t rx_pin_read();
|
|
||||||
void tx_pin_write(uint8_t pin_state);
|
|
||||||
void setTX(uint8_t transmitPin);
|
|
||||||
void setRX(uint8_t receivePin);
|
|
||||||
|
|
||||||
// private static method for timing
|
|
||||||
static inline void tunedDelay(uint16_t delay);
|
|
||||||
|
|
||||||
public:
|
|
||||||
// public methods
|
|
||||||
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
|
|
||||||
~SoftwareSerial();
|
|
||||||
void begin(long speed);
|
|
||||||
bool listen();
|
|
||||||
void end();
|
|
||||||
bool isListening() { return this == active_object; }
|
|
||||||
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
|
|
||||||
int peek();
|
|
||||||
|
|
||||||
virtual size_t write(uint8_t byte);
|
|
||||||
virtual int read();
|
|
||||||
virtual int available();
|
|
||||||
virtual void flush();
|
|
||||||
|
|
||||||
using Print::write;
|
|
||||||
|
|
||||||
// public only for easy access by interrupt handlers
|
|
||||||
static inline void handle_interrupt();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Arduino 0012 workaround
|
|
||||||
#undef int
|
|
||||||
#undef char
|
|
||||||
#undef long
|
|
||||||
#undef byte
|
|
||||||
#undef float
|
|
||||||
#undef abs
|
|
||||||
#undef round
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
Software serial multple serial test
|
|
||||||
|
|
||||||
Receives from the hardware serial, sends to software serial.
|
|
||||||
Receives from software serial, sends to hardware serial.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* RX is digital pin 10 (connect to TX of other device)
|
|
||||||
* TX is digital pin 11 (connect to RX of other device)
|
|
||||||
|
|
||||||
Note:
|
|
||||||
Not all pins on the Mega and Mega 2560 support change interrupts,
|
|
||||||
so only the following can be used for RX:
|
|
||||||
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
|
|
||||||
|
|
||||||
Not all pins on the Leonardo support change interrupts,
|
|
||||||
so only the following can be used for RX:
|
|
||||||
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
|
|
||||||
|
|
||||||
created back in the mists of time
|
|
||||||
modified 25 May 2012
|
|
||||||
by Tom Igoe
|
|
||||||
based on Mikal Hart's example
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
#include <SoftwareSerial.h>
|
|
||||||
|
|
||||||
SoftwareSerial mySerial(10, 11); // RX, TX
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(57600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for Leonardo only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Goodnight moon!");
|
|
||||||
|
|
||||||
// set the data rate for the SoftwareSerial port
|
|
||||||
mySerial.begin(4800);
|
|
||||||
mySerial.println("Hello, world?");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() // run over and over
|
|
||||||
{
|
|
||||||
if (mySerial.available())
|
|
||||||
Serial.write(mySerial.read());
|
|
||||||
if (Serial.available())
|
|
||||||
mySerial.write(Serial.read());
|
|
||||||
}
|
|
||||||
|
|
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
Software serial multple serial test
|
|
||||||
|
|
||||||
Receives from the two software serial ports,
|
|
||||||
sends to the hardware serial port.
|
|
||||||
|
|
||||||
In order to listen on a software port, you call port.listen().
|
|
||||||
When using two software serial ports, you have to switch ports
|
|
||||||
by listen()ing on each one in turn. Pick a logical time to switch
|
|
||||||
ports, like the end of an expected transmission, or when the
|
|
||||||
buffer is empty. This example switches ports when there is nothing
|
|
||||||
more to read from a port
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
Two devices which communicate serially are needed.
|
|
||||||
* First serial device's TX attached to digital pin 2, RX to pin 3
|
|
||||||
* Second serial device's TX attached to digital pin 4, RX to pin 5
|
|
||||||
|
|
||||||
Note:
|
|
||||||
Not all pins on the Mega and Mega 2560 support change interrupts,
|
|
||||||
so only the following can be used for RX:
|
|
||||||
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
|
|
||||||
|
|
||||||
Not all pins on the Leonardo support change interrupts,
|
|
||||||
so only the following can be used for RX:
|
|
||||||
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
|
|
||||||
|
|
||||||
created 18 Apr. 2011
|
|
||||||
modified 25 May 2012
|
|
||||||
by Tom Igoe
|
|
||||||
based on Mikal Hart's twoPortRXExample
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SoftwareSerial.h>
|
|
||||||
// software serial #1: TX = digital pin 10, RX = digital pin 11
|
|
||||||
SoftwareSerial portOne(10, 11);
|
|
||||||
|
|
||||||
// software serial #2: TX = digital pin 8, RX = digital pin 9
|
|
||||||
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
|
|
||||||
SoftwareSerial portTwo(8, 9);
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for Leonardo only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Start each software serial port
|
|
||||||
portOne.begin(9600);
|
|
||||||
portTwo.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// By default, the last intialized port is listening.
|
|
||||||
// when you want to listen on a port, explicitly select it:
|
|
||||||
portOne.listen();
|
|
||||||
Serial.println("Data from port one:");
|
|
||||||
// while there is data coming in, read it
|
|
||||||
// and send to the hardware serial port:
|
|
||||||
while (portOne.available() > 0) {
|
|
||||||
char inByte = portOne.read();
|
|
||||||
Serial.write(inByte);
|
|
||||||
}
|
|
||||||
|
|
||||||
// blank line to separate data from the two ports:
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
// Now listen on the second port
|
|
||||||
portTwo.listen();
|
|
||||||
// while there is data coming in, read it
|
|
||||||
// and send to the hardware serial port:
|
|
||||||
Serial.println("Data from port two:");
|
|
||||||
while (portTwo.available() > 0) {
|
|
||||||
char inByte = portTwo.read();
|
|
||||||
Serial.write(inByte);
|
|
||||||
}
|
|
||||||
|
|
||||||
// blank line to separate data from the two ports:
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
|||||||
#######################################
|
|
||||||
# Syntax Coloring Map for SoftwareSerial
|
|
||||||
# (formerly NewSoftSerial)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
SoftwareSerial KEYWORD1
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
begin KEYWORD2
|
|
||||||
end KEYWORD2
|
|
||||||
read KEYWORD2
|
|
||||||
write KEYWORD2
|
|
||||||
available KEYWORD2
|
|
||||||
isListening KEYWORD2
|
|
||||||
overflow KEYWORD2
|
|
||||||
flush KEYWORD2
|
|
||||||
listen KEYWORD2
|
|
||||||
peek KEYWORD2
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
name=SoftwareSerial
|
|
||||||
version=1.0
|
|
||||||
author=Arduino
|
|
||||||
maintainer=Arduino <info@arduino.cc>
|
|
||||||
sentence=Enables serial communication on digital pins. For all Arduino boards, BUT Arduino DUE.
|
|
||||||
paragraph=
|
|
||||||
url=http://arduino.cc/en/Reference/SoftwareSerial
|
|
||||||
architectures=avr
|
|
@ -1,303 +0,0 @@
|
|||||||
/*
|
|
||||||
TwoWire.cpp - TWI/I2C library for Wiring & Arduino
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
||||||
*/
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include "twi.h"
|
|
||||||
}
|
|
||||||
|
|
||||||
#include "Wire.h"
|
|
||||||
|
|
||||||
// Initialize Class Variables //////////////////////////////////////////////////
|
|
||||||
|
|
||||||
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
|
|
||||||
uint8_t TwoWire::rxBufferIndex = 0;
|
|
||||||
uint8_t TwoWire::rxBufferLength = 0;
|
|
||||||
|
|
||||||
uint8_t TwoWire::txAddress = 0;
|
|
||||||
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
|
|
||||||
uint8_t TwoWire::txBufferIndex = 0;
|
|
||||||
uint8_t TwoWire::txBufferLength = 0;
|
|
||||||
|
|
||||||
uint8_t TwoWire::transmitting = 0;
|
|
||||||
void (*TwoWire::user_onRequest)(void);
|
|
||||||
void (*TwoWire::user_onReceive)(int);
|
|
||||||
|
|
||||||
// Constructors ////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
TwoWire::TwoWire()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Public Methods //////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void TwoWire::begin(void)
|
|
||||||
{
|
|
||||||
rxBufferIndex = 0;
|
|
||||||
rxBufferLength = 0;
|
|
||||||
|
|
||||||
txBufferIndex = 0;
|
|
||||||
txBufferLength = 0;
|
|
||||||
|
|
||||||
twi_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::begin(uint8_t address)
|
|
||||||
{
|
|
||||||
twi_setAddress(address);
|
|
||||||
twi_attachSlaveTxEvent(onRequestService);
|
|
||||||
twi_attachSlaveRxEvent(onReceiveService);
|
|
||||||
begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::begin(int address)
|
|
||||||
{
|
|
||||||
begin((uint8_t)address);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::setClock(uint32_t frequency)
|
|
||||||
{
|
|
||||||
TWBR = ((F_CPU / frequency) - 16) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
|
|
||||||
{
|
|
||||||
// clamp to buffer length
|
|
||||||
if(quantity > BUFFER_LENGTH){
|
|
||||||
quantity = BUFFER_LENGTH;
|
|
||||||
}
|
|
||||||
// perform blocking read into buffer
|
|
||||||
uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
|
|
||||||
// set rx buffer iterator vars
|
|
||||||
rxBufferIndex = 0;
|
|
||||||
rxBufferLength = read;
|
|
||||||
|
|
||||||
return read;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
|
|
||||||
{
|
|
||||||
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t TwoWire::requestFrom(int address, int quantity)
|
|
||||||
{
|
|
||||||
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
|
|
||||||
{
|
|
||||||
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::beginTransmission(uint8_t address)
|
|
||||||
{
|
|
||||||
// indicate that we are transmitting
|
|
||||||
transmitting = 1;
|
|
||||||
// set address of targeted slave
|
|
||||||
txAddress = address;
|
|
||||||
// reset tx buffer iterator vars
|
|
||||||
txBufferIndex = 0;
|
|
||||||
txBufferLength = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::beginTransmission(int address)
|
|
||||||
{
|
|
||||||
beginTransmission((uint8_t)address);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Originally, 'endTransmission' was an f(void) function.
|
|
||||||
// It has been modified to take one parameter indicating
|
|
||||||
// whether or not a STOP should be performed on the bus.
|
|
||||||
// Calling endTransmission(false) allows a sketch to
|
|
||||||
// perform a repeated start.
|
|
||||||
//
|
|
||||||
// WARNING: Nothing in the library keeps track of whether
|
|
||||||
// the bus tenure has been properly ended with a STOP. It
|
|
||||||
// is very possible to leave the bus in a hung state if
|
|
||||||
// no call to endTransmission(true) is made. Some I2C
|
|
||||||
// devices will behave oddly if they do not see a STOP.
|
|
||||||
//
|
|
||||||
uint8_t TwoWire::endTransmission(uint8_t sendStop)
|
|
||||||
{
|
|
||||||
// transmit buffer (blocking)
|
|
||||||
int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
|
|
||||||
// reset tx buffer iterator vars
|
|
||||||
txBufferIndex = 0;
|
|
||||||
txBufferLength = 0;
|
|
||||||
// indicate that we are done transmitting
|
|
||||||
transmitting = 0;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This provides backwards compatibility with the original
|
|
||||||
// definition, and expected behaviour, of endTransmission
|
|
||||||
//
|
|
||||||
uint8_t TwoWire::endTransmission(void)
|
|
||||||
{
|
|
||||||
return endTransmission(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be called in:
|
|
||||||
// slave tx event callback
|
|
||||||
// or after beginTransmission(address)
|
|
||||||
size_t TwoWire::write(uint8_t data)
|
|
||||||
{
|
|
||||||
if(transmitting){
|
|
||||||
// in master transmitter mode
|
|
||||||
// don't bother if buffer is full
|
|
||||||
if(txBufferLength >= BUFFER_LENGTH){
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// put byte in tx buffer
|
|
||||||
txBuffer[txBufferIndex] = data;
|
|
||||||
++txBufferIndex;
|
|
||||||
// update amount in buffer
|
|
||||||
txBufferLength = txBufferIndex;
|
|
||||||
}else{
|
|
||||||
// in slave send mode
|
|
||||||
// reply to master
|
|
||||||
twi_transmit(&data, 1);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be called in:
|
|
||||||
// slave tx event callback
|
|
||||||
// or after beginTransmission(address)
|
|
||||||
size_t TwoWire::write(const uint8_t *data, size_t quantity)
|
|
||||||
{
|
|
||||||
if(transmitting){
|
|
||||||
// in master transmitter mode
|
|
||||||
for(size_t i = 0; i < quantity; ++i){
|
|
||||||
write(data[i]);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
// in slave send mode
|
|
||||||
// reply to master
|
|
||||||
twi_transmit(data, quantity);
|
|
||||||
}
|
|
||||||
return quantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be called in:
|
|
||||||
// slave rx event callback
|
|
||||||
// or after requestFrom(address, numBytes)
|
|
||||||
int TwoWire::available(void)
|
|
||||||
{
|
|
||||||
return rxBufferLength - rxBufferIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be called in:
|
|
||||||
// slave rx event callback
|
|
||||||
// or after requestFrom(address, numBytes)
|
|
||||||
int TwoWire::read(void)
|
|
||||||
{
|
|
||||||
int value = -1;
|
|
||||||
|
|
||||||
// get each successive byte on each call
|
|
||||||
if(rxBufferIndex < rxBufferLength){
|
|
||||||
value = rxBuffer[rxBufferIndex];
|
|
||||||
++rxBufferIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// must be called in:
|
|
||||||
// slave rx event callback
|
|
||||||
// or after requestFrom(address, numBytes)
|
|
||||||
int TwoWire::peek(void)
|
|
||||||
{
|
|
||||||
int value = -1;
|
|
||||||
|
|
||||||
if(rxBufferIndex < rxBufferLength){
|
|
||||||
value = rxBuffer[rxBufferIndex];
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TwoWire::flush(void)
|
|
||||||
{
|
|
||||||
// XXX: to be implemented.
|
|
||||||
}
|
|
||||||
|
|
||||||
// behind the scenes function that is called when data is received
|
|
||||||
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
|
|
||||||
{
|
|
||||||
// don't bother if user hasn't registered a callback
|
|
||||||
if(!user_onReceive){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// don't bother if rx buffer is in use by a master requestFrom() op
|
|
||||||
// i know this drops data, but it allows for slight stupidity
|
|
||||||
// meaning, they may not have read all the master requestFrom() data yet
|
|
||||||
if(rxBufferIndex < rxBufferLength){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// copy twi rx buffer into local read buffer
|
|
||||||
// this enables new reads to happen in parallel
|
|
||||||
for(uint8_t i = 0; i < numBytes; ++i){
|
|
||||||
rxBuffer[i] = inBytes[i];
|
|
||||||
}
|
|
||||||
// set rx iterator vars
|
|
||||||
rxBufferIndex = 0;
|
|
||||||
rxBufferLength = numBytes;
|
|
||||||
// alert user program
|
|
||||||
user_onReceive(numBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// behind the scenes function that is called when data is requested
|
|
||||||
void TwoWire::onRequestService(void)
|
|
||||||
{
|
|
||||||
// don't bother if user hasn't registered a callback
|
|
||||||
if(!user_onRequest){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// reset tx buffer iterator vars
|
|
||||||
// !!! this will kill any pending pre-master sendTo() activity
|
|
||||||
txBufferIndex = 0;
|
|
||||||
txBufferLength = 0;
|
|
||||||
// alert user program
|
|
||||||
user_onRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
// sets function called on slave write
|
|
||||||
void TwoWire::onReceive( void (*function)(int) )
|
|
||||||
{
|
|
||||||
user_onReceive = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sets function called on slave read
|
|
||||||
void TwoWire::onRequest( void (*function)(void) )
|
|
||||||
{
|
|
||||||
user_onRequest = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preinstantiate Objects //////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
TwoWire Wire = TwoWire();
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
|||||||
/*
|
|
||||||
TwoWire.h - TWI/I2C library for Arduino & Wiring
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef TwoWire_h
|
|
||||||
#define TwoWire_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include "Stream.h"
|
|
||||||
|
|
||||||
#define BUFFER_LENGTH 32
|
|
||||||
|
|
||||||
class TwoWire : public Stream
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
static uint8_t rxBuffer[];
|
|
||||||
static uint8_t rxBufferIndex;
|
|
||||||
static uint8_t rxBufferLength;
|
|
||||||
|
|
||||||
static uint8_t txAddress;
|
|
||||||
static uint8_t txBuffer[];
|
|
||||||
static uint8_t txBufferIndex;
|
|
||||||
static uint8_t txBufferLength;
|
|
||||||
|
|
||||||
static uint8_t transmitting;
|
|
||||||
static void (*user_onRequest)(void);
|
|
||||||
static void (*user_onReceive)(int);
|
|
||||||
static void onRequestService(void);
|
|
||||||
static void onReceiveService(uint8_t*, int);
|
|
||||||
public:
|
|
||||||
TwoWire();
|
|
||||||
void begin();
|
|
||||||
void begin(uint8_t);
|
|
||||||
void begin(int);
|
|
||||||
void setClock(uint32_t);
|
|
||||||
void beginTransmission(uint8_t);
|
|
||||||
void beginTransmission(int);
|
|
||||||
uint8_t endTransmission(void);
|
|
||||||
uint8_t endTransmission(uint8_t);
|
|
||||||
uint8_t requestFrom(uint8_t, uint8_t);
|
|
||||||
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
|
|
||||||
uint8_t requestFrom(int, int);
|
|
||||||
uint8_t requestFrom(int, int, int);
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
virtual size_t write(const uint8_t *, size_t);
|
|
||||||
virtual int available(void);
|
|
||||||
virtual int read(void);
|
|
||||||
virtual int peek(void);
|
|
||||||
virtual void flush(void);
|
|
||||||
void onReceive( void (*)(int) );
|
|
||||||
void onRequest( void (*)(void) );
|
|
||||||
|
|
||||||
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(long n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(unsigned int n) { return write((uint8_t)n); }
|
|
||||||
inline size_t write(int n) { return write((uint8_t)n); }
|
|
||||||
using Print::write;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern TwoWire Wire;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,87 +0,0 @@
|
|||||||
// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
// and James Tichenor <http://www.jamestichenor.net>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library reading data from the
|
|
||||||
// Devantech Utrasonic Rangers SFR08 and SFR10
|
|
||||||
|
|
||||||
// Created 29 April 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
|
||||||
Serial.begin(9600); // start serial communication at 9600bps
|
|
||||||
}
|
|
||||||
|
|
||||||
int reading = 0;
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// step 1: instruct sensor to read echoes
|
|
||||||
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
|
||||||
// the address specified in the datasheet is 224 (0xE0)
|
|
||||||
// but i2c adressing uses the high 7 bits so it's 112
|
|
||||||
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
|
|
||||||
Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
|
|
||||||
// use 0x51 for centimeters
|
|
||||||
// use 0x52 for ping microseconds
|
|
||||||
Wire.endTransmission(); // stop transmitting
|
|
||||||
|
|
||||||
// step 2: wait for readings to happen
|
|
||||||
delay(70); // datasheet suggests at least 65 milliseconds
|
|
||||||
|
|
||||||
// step 3: instruct sensor to return a particular echo reading
|
|
||||||
Wire.beginTransmission(112); // transmit to device #112
|
|
||||||
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
|
|
||||||
Wire.endTransmission(); // stop transmitting
|
|
||||||
|
|
||||||
// step 4: request reading from sensor
|
|
||||||
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
|
|
||||||
|
|
||||||
// step 5: receive reading from sensor
|
|
||||||
if (2 <= Wire.available()) // if two bytes were received
|
|
||||||
{
|
|
||||||
reading = Wire.read(); // receive high byte (overwrites previous reading)
|
|
||||||
reading = reading << 8; // shift high byte to be high 8 bits
|
|
||||||
reading |= Wire.read(); // receive low byte as lower 8 bits
|
|
||||||
Serial.println(reading); // print the reading
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(250); // wait a bit since people have to read the output :)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
|
|
||||||
// usage: changeAddress(0x70, 0xE6);
|
|
||||||
|
|
||||||
void changeAddress(byte oldAddress, byte newAddress)
|
|
||||||
{
|
|
||||||
Wire.beginTransmission(oldAddress);
|
|
||||||
Wire.write(byte(0x00));
|
|
||||||
Wire.write(byte(0xA0));
|
|
||||||
Wire.endTransmission();
|
|
||||||
|
|
||||||
Wire.beginTransmission(oldAddress);
|
|
||||||
Wire.write(byte(0x00));
|
|
||||||
Wire.write(byte(0xAA));
|
|
||||||
Wire.endTransmission();
|
|
||||||
|
|
||||||
Wire.beginTransmission(oldAddress);
|
|
||||||
Wire.write(byte(0x00));
|
|
||||||
Wire.write(byte(0xA5));
|
|
||||||
Wire.endTransmission();
|
|
||||||
|
|
||||||
Wire.beginTransmission(oldAddress);
|
|
||||||
Wire.write(byte(0x00));
|
|
||||||
Wire.write(newAddress);
|
|
||||||
Wire.endTransmission();
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
@ -1,39 +0,0 @@
|
|||||||
// I2C Digital Potentiometer
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library
|
|
||||||
// Controls AD5171 digital potentiometer via I2C/TWI
|
|
||||||
|
|
||||||
// Created 31 March 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
|
||||||
}
|
|
||||||
|
|
||||||
byte val = 0;
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
|
||||||
// device address is specified in datasheet
|
|
||||||
Wire.write(byte(0x00)); // sends instruction byte
|
|
||||||
Wire.write(val); // sends potentiometer value byte
|
|
||||||
Wire.endTransmission(); // stop transmitting
|
|
||||||
|
|
||||||
val++; // increment value
|
|
||||||
if (val == 64) // if reached 64th position (max)
|
|
||||||
{
|
|
||||||
val = 0; // start over from lowest value
|
|
||||||
}
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
|||||||
// Wire Master Reader
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library
|
|
||||||
// Reads data from an I2C/TWI slave device
|
|
||||||
// Refer to the "Wire Slave Sender" example for use with this
|
|
||||||
|
|
||||||
// Created 29 March 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
|
||||||
Serial.begin(9600); // start serial for output
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
|
|
||||||
|
|
||||||
while (Wire.available()) // slave may send less than requested
|
|
||||||
{
|
|
||||||
char c = Wire.read(); // receive a byte as character
|
|
||||||
Serial.print(c); // print the character
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(500);
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
// Wire Master Writer
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library
|
|
||||||
// Writes data to an I2C/TWI slave device
|
|
||||||
// Refer to the "Wire Slave Receiver" example for use with this
|
|
||||||
|
|
||||||
// Created 29 March 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
|
||||||
}
|
|
||||||
|
|
||||||
byte x = 0;
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
Wire.beginTransmission(4); // transmit to device #4
|
|
||||||
Wire.write("x is "); // sends five bytes
|
|
||||||
Wire.write(x); // sends one byte
|
|
||||||
Wire.endTransmission(); // stop transmitting
|
|
||||||
|
|
||||||
x++;
|
|
||||||
delay(500);
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
// Wire Slave Receiver
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library
|
|
||||||
// Receives data as an I2C/TWI slave device
|
|
||||||
// Refer to the "Wire Master Writer" example for use with this
|
|
||||||
|
|
||||||
// Created 29 March 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(4); // join i2c bus with address #4
|
|
||||||
Wire.onReceive(receiveEvent); // register event
|
|
||||||
Serial.begin(9600); // start serial for output
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
// function that executes whenever data is received from master
|
|
||||||
// this function is registered as an event, see setup()
|
|
||||||
void receiveEvent(int howMany)
|
|
||||||
{
|
|
||||||
while (1 < Wire.available()) // loop through all but the last
|
|
||||||
{
|
|
||||||
char c = Wire.read(); // receive byte as a character
|
|
||||||
Serial.print(c); // print the character
|
|
||||||
}
|
|
||||||
int x = Wire.read(); // receive byte as an integer
|
|
||||||
Serial.println(x); // print the integer
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
// Wire Slave Sender
|
|
||||||
// by Nicholas Zambetti <http://www.zambetti.com>
|
|
||||||
|
|
||||||
// Demonstrates use of the Wire library
|
|
||||||
// Sends data as an I2C/TWI slave device
|
|
||||||
// Refer to the "Wire Master Reader" example for use with this
|
|
||||||
|
|
||||||
// Created 29 March 2006
|
|
||||||
|
|
||||||
// This example code is in the public domain.
|
|
||||||
|
|
||||||
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Wire.begin(2); // join i2c bus with address #2
|
|
||||||
Wire.onRequest(requestEvent); // register event
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
// function that executes whenever data is requested by master
|
|
||||||
// this function is registered as an event, see setup()
|
|
||||||
void requestEvent()
|
|
||||||
{
|
|
||||||
Wire.write("hello "); // respond with message of 6 bytes
|
|
||||||
// as expected by master
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
#######################################
|
|
||||||
# Syntax Coloring Map For Wire
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
begin KEYWORD2
|
|
||||||
setClock KEYWORD2
|
|
||||||
beginTransmission KEYWORD2
|
|
||||||
endTransmission KEYWORD2
|
|
||||||
requestFrom KEYWORD2
|
|
||||||
send KEYWORD2
|
|
||||||
receive KEYWORD2
|
|
||||||
onReceive KEYWORD2
|
|
||||||
onRequest KEYWORD2
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Instances (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
Wire KEYWORD2
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
name=Wire
|
|
||||||
version=1.0
|
|
||||||
author=Arduino
|
|
||||||
maintainer=Arduino <info@arduino.cc>
|
|
||||||
sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus. For all Arduino boards, BUT Arduino DUE.
|
|
||||||
paragraph=
|
|
||||||
url=http://arduino.cc/en/Reference/Wire
|
|
||||||
architectures=avr
|
|
@ -1,527 +0,0 @@
|
|||||||
/*
|
|
||||||
twi.c - TWI/I2C library for Wiring & Arduino
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <avr/io.h>
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
#include <compat/twi.h>
|
|
||||||
#include "Arduino.h" // for digitalWrite
|
|
||||||
|
|
||||||
#ifndef cbi
|
|
||||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef sbi
|
|
||||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "pins_arduino.h"
|
|
||||||
#include "twi.h"
|
|
||||||
|
|
||||||
static volatile uint8_t twi_state;
|
|
||||||
static volatile uint8_t twi_slarw;
|
|
||||||
static volatile uint8_t twi_sendStop; // should the transaction end with a stop
|
|
||||||
static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
|
|
||||||
|
|
||||||
static void (*twi_onSlaveTransmit)(void);
|
|
||||||
static void (*twi_onSlaveReceive)(uint8_t*, int);
|
|
||||||
|
|
||||||
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
|
|
||||||
static volatile uint8_t twi_masterBufferIndex;
|
|
||||||
static volatile uint8_t twi_masterBufferLength;
|
|
||||||
|
|
||||||
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
|
|
||||||
static volatile uint8_t twi_txBufferIndex;
|
|
||||||
static volatile uint8_t twi_txBufferLength;
|
|
||||||
|
|
||||||
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
|
|
||||||
static volatile uint8_t twi_rxBufferIndex;
|
|
||||||
|
|
||||||
static volatile uint8_t twi_error;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_init
|
|
||||||
* Desc readys twi pins and sets twi bitrate
|
|
||||||
* Input none
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_init(void)
|
|
||||||
{
|
|
||||||
// initialize state
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
twi_sendStop = true; // default value
|
|
||||||
twi_inRepStart = false;
|
|
||||||
|
|
||||||
// activate internal pullups for twi.
|
|
||||||
digitalWrite(SDA, 1);
|
|
||||||
digitalWrite(SCL, 1);
|
|
||||||
|
|
||||||
// initialize twi prescaler and bit rate
|
|
||||||
cbi(TWSR, TWPS0);
|
|
||||||
cbi(TWSR, TWPS1);
|
|
||||||
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
|
|
||||||
|
|
||||||
/* twi bit rate formula from atmega128 manual pg 204
|
|
||||||
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
|
|
||||||
note: TWBR should be 10 or higher for master mode
|
|
||||||
It is 72 for a 16mhz Wiring board with 100kHz TWI */
|
|
||||||
|
|
||||||
// enable twi module, acks, and twi interrupt
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_slaveInit
|
|
||||||
* Desc sets slave address and enables interrupt
|
|
||||||
* Input none
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_setAddress(uint8_t address)
|
|
||||||
{
|
|
||||||
// set twi slave address (skip over TWGCE bit)
|
|
||||||
TWAR = address << 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_readFrom
|
|
||||||
* Desc attempts to become twi bus master and read a
|
|
||||||
* series of bytes from a device on the bus
|
|
||||||
* Input address: 7bit i2c device address
|
|
||||||
* data: pointer to byte array
|
|
||||||
* length: number of bytes to read into array
|
|
||||||
* sendStop: Boolean indicating whether to send a stop at the end
|
|
||||||
* Output number of bytes read
|
|
||||||
*/
|
|
||||||
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
// ensure data will fit into buffer
|
|
||||||
if(TWI_BUFFER_LENGTH < length){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait until twi is ready, become master receiver
|
|
||||||
while(TWI_READY != twi_state){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
twi_state = TWI_MRX;
|
|
||||||
twi_sendStop = sendStop;
|
|
||||||
// reset error state (0xFF.. no error occured)
|
|
||||||
twi_error = 0xFF;
|
|
||||||
|
|
||||||
// initialize buffer iteration vars
|
|
||||||
twi_masterBufferIndex = 0;
|
|
||||||
twi_masterBufferLength = length-1; // This is not intuitive, read on...
|
|
||||||
// On receive, the previously configured ACK/NACK setting is transmitted in
|
|
||||||
// response to the received byte before the interrupt is signalled.
|
|
||||||
// Therefor we must actually set NACK when the _next_ to last byte is
|
|
||||||
// received, causing that NACK to be sent in response to receiving the last
|
|
||||||
// expected byte of data.
|
|
||||||
|
|
||||||
// build sla+w, slave device address + w bit
|
|
||||||
twi_slarw = TW_READ;
|
|
||||||
twi_slarw |= address << 1;
|
|
||||||
|
|
||||||
if (true == twi_inRepStart) {
|
|
||||||
// if we're in the repeated start state, then we've already sent the start,
|
|
||||||
// (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
|
|
||||||
// We need to remove ourselves from the repeated start state before we enable interrupts,
|
|
||||||
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
|
|
||||||
// up. Also, don't enable the START interrupt. There may be one pending from the
|
|
||||||
// repeated start that we sent outselves, and that would really confuse things.
|
|
||||||
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
|
|
||||||
TWDR = twi_slarw;
|
|
||||||
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
|
|
||||||
}
|
|
||||||
else
|
|
||||||
// send start condition
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
|
|
||||||
|
|
||||||
// wait for read operation to complete
|
|
||||||
while(TWI_MRX == twi_state){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (twi_masterBufferIndex < length)
|
|
||||||
length = twi_masterBufferIndex;
|
|
||||||
|
|
||||||
// copy twi buffer to data
|
|
||||||
for(i = 0; i < length; ++i){
|
|
||||||
data[i] = twi_masterBuffer[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_writeTo
|
|
||||||
* Desc attempts to become twi bus master and write a
|
|
||||||
* series of bytes to a device on the bus
|
|
||||||
* Input address: 7bit i2c device address
|
|
||||||
* data: pointer to byte array
|
|
||||||
* length: number of bytes in array
|
|
||||||
* wait: boolean indicating to wait for write or not
|
|
||||||
* sendStop: boolean indicating whether or not to send a stop at the end
|
|
||||||
* Output 0 .. success
|
|
||||||
* 1 .. length to long for buffer
|
|
||||||
* 2 .. address send, NACK received
|
|
||||||
* 3 .. data send, NACK received
|
|
||||||
* 4 .. other twi error (lost bus arbitration, bus error, ..)
|
|
||||||
*/
|
|
||||||
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
// ensure data will fit into buffer
|
|
||||||
if(TWI_BUFFER_LENGTH < length){
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait until twi is ready, become master transmitter
|
|
||||||
while(TWI_READY != twi_state){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
twi_state = TWI_MTX;
|
|
||||||
twi_sendStop = sendStop;
|
|
||||||
// reset error state (0xFF.. no error occured)
|
|
||||||
twi_error = 0xFF;
|
|
||||||
|
|
||||||
// initialize buffer iteration vars
|
|
||||||
twi_masterBufferIndex = 0;
|
|
||||||
twi_masterBufferLength = length;
|
|
||||||
|
|
||||||
// copy data to twi buffer
|
|
||||||
for(i = 0; i < length; ++i){
|
|
||||||
twi_masterBuffer[i] = data[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// build sla+w, slave device address + w bit
|
|
||||||
twi_slarw = TW_WRITE;
|
|
||||||
twi_slarw |= address << 1;
|
|
||||||
|
|
||||||
// if we're in a repeated start, then we've already sent the START
|
|
||||||
// in the ISR. Don't do it again.
|
|
||||||
//
|
|
||||||
if (true == twi_inRepStart) {
|
|
||||||
// if we're in the repeated start state, then we've already sent the start,
|
|
||||||
// (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
|
|
||||||
// We need to remove ourselves from the repeated start state before we enable interrupts,
|
|
||||||
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
|
|
||||||
// up. Also, don't enable the START interrupt. There may be one pending from the
|
|
||||||
// repeated start that we sent outselves, and that would really confuse things.
|
|
||||||
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
|
|
||||||
TWDR = twi_slarw;
|
|
||||||
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
|
|
||||||
}
|
|
||||||
else
|
|
||||||
// send start condition
|
|
||||||
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs
|
|
||||||
|
|
||||||
// wait for write operation to complete
|
|
||||||
while(wait && (TWI_MTX == twi_state)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (twi_error == 0xFF)
|
|
||||||
return 0; // success
|
|
||||||
else if (twi_error == TW_MT_SLA_NACK)
|
|
||||||
return 2; // error: address send, nack received
|
|
||||||
else if (twi_error == TW_MT_DATA_NACK)
|
|
||||||
return 3; // error: data send, nack received
|
|
||||||
else
|
|
||||||
return 4; // other twi error
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_transmit
|
|
||||||
* Desc fills slave tx buffer with data
|
|
||||||
* must be called in slave tx event callback
|
|
||||||
* Input data: pointer to byte array
|
|
||||||
* length: number of bytes in array
|
|
||||||
* Output 1 length too long for buffer
|
|
||||||
* 2 not slave transmitter
|
|
||||||
* 0 ok
|
|
||||||
*/
|
|
||||||
uint8_t twi_transmit(const uint8_t* data, uint8_t length)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
// ensure data will fit into buffer
|
|
||||||
if(TWI_BUFFER_LENGTH < length){
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensure we are currently a slave transmitter
|
|
||||||
if(TWI_STX != twi_state){
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set length and copy data into tx buffer
|
|
||||||
twi_txBufferLength = length;
|
|
||||||
for(i = 0; i < length; ++i){
|
|
||||||
twi_txBuffer[i] = data[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_attachSlaveRxEvent
|
|
||||||
* Desc sets function called before a slave read operation
|
|
||||||
* Input function: callback function to use
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
|
|
||||||
{
|
|
||||||
twi_onSlaveReceive = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_attachSlaveTxEvent
|
|
||||||
* Desc sets function called before a slave write operation
|
|
||||||
* Input function: callback function to use
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_attachSlaveTxEvent( void (*function)(void) )
|
|
||||||
{
|
|
||||||
twi_onSlaveTransmit = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_reply
|
|
||||||
* Desc sends byte or readys receive line
|
|
||||||
* Input ack: byte indicating to ack or to nack
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_reply(uint8_t ack)
|
|
||||||
{
|
|
||||||
// transmit master read ready signal, with or without ack
|
|
||||||
if(ack){
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
|
|
||||||
}else{
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_stop
|
|
||||||
* Desc relinquishes bus master status
|
|
||||||
* Input none
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_stop(void)
|
|
||||||
{
|
|
||||||
// send stop condition
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
|
|
||||||
|
|
||||||
// wait for stop condition to be exectued on bus
|
|
||||||
// TWINT is not set after a stop condition!
|
|
||||||
while(TWCR & _BV(TWSTO)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// update twi state
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Function twi_releaseBus
|
|
||||||
* Desc releases bus control
|
|
||||||
* Input none
|
|
||||||
* Output none
|
|
||||||
*/
|
|
||||||
void twi_releaseBus(void)
|
|
||||||
{
|
|
||||||
// release bus
|
|
||||||
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
|
|
||||||
|
|
||||||
// update twi state
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(TWI_vect)
|
|
||||||
{
|
|
||||||
switch(TW_STATUS){
|
|
||||||
// All Master
|
|
||||||
case TW_START: // sent start condition
|
|
||||||
case TW_REP_START: // sent repeated start condition
|
|
||||||
// copy device address and r/w bit to output register and ack
|
|
||||||
TWDR = twi_slarw;
|
|
||||||
twi_reply(1);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Master Transmitter
|
|
||||||
case TW_MT_SLA_ACK: // slave receiver acked address
|
|
||||||
case TW_MT_DATA_ACK: // slave receiver acked data
|
|
||||||
// if there is data to send, send it, otherwise stop
|
|
||||||
if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
||||||
// copy data to output register and ack
|
|
||||||
TWDR = twi_masterBuffer[twi_masterBufferIndex++];
|
|
||||||
twi_reply(1);
|
|
||||||
}else{
|
|
||||||
if (twi_sendStop)
|
|
||||||
twi_stop();
|
|
||||||
else {
|
|
||||||
twi_inRepStart = true; // we're gonna send the START
|
|
||||||
// don't enable the interrupt. We'll generate the start, but we
|
|
||||||
// avoid handling the interrupt until we're in the next transaction,
|
|
||||||
// at the point where we would normally issue the start.
|
|
||||||
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TW_MT_SLA_NACK: // address sent, nack received
|
|
||||||
twi_error = TW_MT_SLA_NACK;
|
|
||||||
twi_stop();
|
|
||||||
break;
|
|
||||||
case TW_MT_DATA_NACK: // data sent, nack received
|
|
||||||
twi_error = TW_MT_DATA_NACK;
|
|
||||||
twi_stop();
|
|
||||||
break;
|
|
||||||
case TW_MT_ARB_LOST: // lost bus arbitration
|
|
||||||
twi_error = TW_MT_ARB_LOST;
|
|
||||||
twi_releaseBus();
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Master Receiver
|
|
||||||
case TW_MR_DATA_ACK: // data received, ack sent
|
|
||||||
// put byte into buffer
|
|
||||||
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
||||||
case TW_MR_SLA_ACK: // address sent, ack received
|
|
||||||
// ack if more bytes are expected, otherwise nack
|
|
||||||
if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
||||||
twi_reply(1);
|
|
||||||
}else{
|
|
||||||
twi_reply(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TW_MR_DATA_NACK: // data received, nack sent
|
|
||||||
// put final byte into buffer
|
|
||||||
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
||||||
if (twi_sendStop)
|
|
||||||
twi_stop();
|
|
||||||
else {
|
|
||||||
twi_inRepStart = true; // we're gonna send the START
|
|
||||||
// don't enable the interrupt. We'll generate the start, but we
|
|
||||||
// avoid handling the interrupt until we're in the next transaction,
|
|
||||||
// at the point where we would normally issue the start.
|
|
||||||
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TW_MR_SLA_NACK: // address sent, nack received
|
|
||||||
twi_stop();
|
|
||||||
break;
|
|
||||||
// TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
|
|
||||||
|
|
||||||
// Slave Receiver
|
|
||||||
case TW_SR_SLA_ACK: // addressed, returned ack
|
|
||||||
case TW_SR_GCALL_ACK: // addressed generally, returned ack
|
|
||||||
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack
|
|
||||||
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
|
|
||||||
// enter slave receiver mode
|
|
||||||
twi_state = TWI_SRX;
|
|
||||||
// indicate that rx buffer can be overwritten and ack
|
|
||||||
twi_rxBufferIndex = 0;
|
|
||||||
twi_reply(1);
|
|
||||||
break;
|
|
||||||
case TW_SR_DATA_ACK: // data received, returned ack
|
|
||||||
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
|
|
||||||
// if there is still room in the rx buffer
|
|
||||||
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
||||||
// put byte in buffer and ack
|
|
||||||
twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
|
|
||||||
twi_reply(1);
|
|
||||||
}else{
|
|
||||||
// otherwise nack
|
|
||||||
twi_reply(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TW_SR_STOP: // stop or repeated start condition received
|
|
||||||
// put a null char after data if there's room
|
|
||||||
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
||||||
twi_rxBuffer[twi_rxBufferIndex] = '\0';
|
|
||||||
}
|
|
||||||
// sends ack and stops interface for clock stretching
|
|
||||||
twi_stop();
|
|
||||||
// callback to user defined callback
|
|
||||||
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
|
|
||||||
// since we submit rx buffer to "wire" library, we can reset it
|
|
||||||
twi_rxBufferIndex = 0;
|
|
||||||
// ack future responses and leave slave receiver state
|
|
||||||
twi_releaseBus();
|
|
||||||
break;
|
|
||||||
case TW_SR_DATA_NACK: // data received, returned nack
|
|
||||||
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
|
|
||||||
// nack back at master
|
|
||||||
twi_reply(0);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Slave Transmitter
|
|
||||||
case TW_ST_SLA_ACK: // addressed, returned ack
|
|
||||||
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
|
|
||||||
// enter slave transmitter mode
|
|
||||||
twi_state = TWI_STX;
|
|
||||||
// ready the tx buffer index for iteration
|
|
||||||
twi_txBufferIndex = 0;
|
|
||||||
// set tx buffer length to be zero, to verify if user changes it
|
|
||||||
twi_txBufferLength = 0;
|
|
||||||
// request for txBuffer to be filled and length to be set
|
|
||||||
// note: user must call twi_transmit(bytes, length) to do this
|
|
||||||
twi_onSlaveTransmit();
|
|
||||||
// if they didn't change buffer & length, initialize it
|
|
||||||
if(0 == twi_txBufferLength){
|
|
||||||
twi_txBufferLength = 1;
|
|
||||||
twi_txBuffer[0] = 0x00;
|
|
||||||
}
|
|
||||||
// transmit first byte from buffer, fall
|
|
||||||
case TW_ST_DATA_ACK: // byte sent, ack returned
|
|
||||||
// copy data to output register
|
|
||||||
TWDR = twi_txBuffer[twi_txBufferIndex++];
|
|
||||||
// if there is more to send, ack, otherwise nack
|
|
||||||
if(twi_txBufferIndex < twi_txBufferLength){
|
|
||||||
twi_reply(1);
|
|
||||||
}else{
|
|
||||||
twi_reply(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TW_ST_DATA_NACK: // received nack, we are done
|
|
||||||
case TW_ST_LAST_DATA: // received ack, but we are done already!
|
|
||||||
// ack future responses
|
|
||||||
twi_reply(1);
|
|
||||||
// leave slave receiver state
|
|
||||||
twi_state = TWI_READY;
|
|
||||||
break;
|
|
||||||
|
|
||||||
// All
|
|
||||||
case TW_NO_INFO: // no state information
|
|
||||||
break;
|
|
||||||
case TW_BUS_ERROR: // bus error, illegal stop/start
|
|
||||||
twi_error = TW_BUS_ERROR;
|
|
||||||
twi_stop();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
|||||||
/*
|
|
||||||
twi.h - TWI/I2C library for Wiring & Arduino
|
|
||||||
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef twi_h
|
|
||||||
#define twi_h
|
|
||||||
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
//#define ATMEGA8
|
|
||||||
|
|
||||||
#ifndef TWI_FREQ
|
|
||||||
#define TWI_FREQ 100000L
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef TWI_BUFFER_LENGTH
|
|
||||||
#define TWI_BUFFER_LENGTH 32
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TWI_READY 0
|
|
||||||
#define TWI_MRX 1
|
|
||||||
#define TWI_MTX 2
|
|
||||||
#define TWI_SRX 3
|
|
||||||
#define TWI_STX 4
|
|
||||||
|
|
||||||
void twi_init(void);
|
|
||||||
void twi_setAddress(uint8_t);
|
|
||||||
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
|
|
||||||
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
|
|
||||||
uint8_t twi_transmit(const uint8_t*, uint8_t);
|
|
||||||
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
|
|
||||||
void twi_attachSlaveTxEvent( void (*)(void) );
|
|
||||||
void twi_reply(uint8_t);
|
|
||||||
void twi_stop(void);
|
|
||||||
void twi_releaseBus(void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
|||||||
|
|
||||||
# Arduino AVR Core and platform.
|
|
||||||
# ------------------------------
|
|
||||||
|
|
||||||
# For more info:
|
|
||||||
# https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification
|
|
||||||
|
|
||||||
name=Keyboardio Keyboards
|
|
||||||
|
|
||||||
# AVR compile variables
|
|
||||||
# ---------------------
|
|
||||||
|
|
||||||
# Default "compiler.path" is correct, change only if you want to overidde the initial value
|
|
||||||
compiler.path={runtime.ide.path}/hardware/tools/avr/bin/
|
|
||||||
compiler.c.cmd=avr-gcc
|
|
||||||
compiler.c.flags=-c -g -Os -w -ffunction-sections -fdata-sections -MMD
|
|
||||||
# -w flag added to avoid printing a wrong warning http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59396
|
|
||||||
# This is fixed in gcc 4.8.3 and will be removed as soon as we update the toolchain
|
|
||||||
compiler.c.elf.flags=-w -Os -Wl,--gc-sections
|
|
||||||
compiler.c.elf.cmd=avr-gcc
|
|
||||||
compiler.S.flags=-c -g -x assembler-with-cpp
|
|
||||||
compiler.cpp.cmd=avr-g++
|
|
||||||
compiler.cpp.flags=-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD
|
|
||||||
compiler.ar.cmd=avr-ar
|
|
||||||
compiler.ar.flags=rcs
|
|
||||||
compiler.objcopy.cmd=avr-objcopy
|
|
||||||
compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
|
|
||||||
compiler.elf2hex.flags=-O ihex -R .eeprom
|
|
||||||
compiler.elf2hex.cmd=avr-objcopy
|
|
||||||
compiler.ldflags=
|
|
||||||
compiler.size.cmd=avr-size
|
|
||||||
|
|
||||||
# This can be overriden in boards.txt
|
|
||||||
build.extra_flags=
|
|
||||||
|
|
||||||
# These can be overridden in platform.local.txt
|
|
||||||
compiler.c.extra_flags=
|
|
||||||
compiler.c.elf.extra_flags=
|
|
||||||
compiler.S.extra_flags=
|
|
||||||
compiler.cpp.extra_flags=
|
|
||||||
compiler.ar.extra_flags=
|
|
||||||
compiler.objcopy.eep.extra_flags=
|
|
||||||
compiler.elf2hex.extra_flags=
|
|
||||||
|
|
||||||
# AVR compile patterns
|
|
||||||
# --------------------
|
|
||||||
|
|
||||||
## Compile c files
|
|
||||||
recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.c.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"
|
|
||||||
|
|
||||||
## Compile c++ files
|
|
||||||
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"
|
|
||||||
|
|
||||||
## Compile S files
|
|
||||||
recipe.S.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.S.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.S.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"
|
|
||||||
|
|
||||||
## Create archives
|
|
||||||
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{build.path}/{archive_file}" "{object_file}"
|
|
||||||
|
|
||||||
## Combine gc-sections, archives, and objects
|
|
||||||
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm
|
|
||||||
|
|
||||||
## Create eeprom
|
|
||||||
recipe.objcopy.eep.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.objcopy.eep.flags} {compiler.objcopy.eep.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.eep"
|
|
||||||
|
|
||||||
## Create hex
|
|
||||||
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
|
|
||||||
|
|
||||||
## Compute size
|
|
||||||
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
|
|
||||||
recipe.size.regex=^(?:\.text|\.data|\.bootloader)\s+([0-9]+).*
|
|
||||||
recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
|
|
||||||
recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*
|
|
||||||
|
|
||||||
|
|
||||||
# AVR Uploader/Programmers tools
|
|
||||||
# ------------------------------
|
|
||||||
|
|
||||||
tools.avrdude.cmd.path={runtime.ide.path}/hardware/tools/avr/bin/avrdude
|
|
||||||
tools.avrdude.config.path={runtime.ide.path}/hardware/tools/avr/etc/avrdude.conf
|
|
||||||
|
|
||||||
tools.avrdude.upload.params.verbose=-v
|
|
||||||
tools.avrdude.upload.params.quiet=-q -q
|
|
||||||
tools.avrdude.upload.pattern="{cmd.path}" "-C{config.path}" {upload.verbose} -p{build.mcu} -c{upload.protocol} -P{serial.port} -b{upload.speed} -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"
|
|
||||||
|
|
||||||
tools.avrdude.program.params.verbose=-v
|
|
||||||
tools.avrdude.program.params.quiet=-q -q
|
|
||||||
tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i"
|
|
||||||
|
|
||||||
tools.avrdude.erase.params.verbose=-v
|
|
||||||
tools.avrdude.erase.params.quiet=-q -q
|
|
||||||
tools.avrdude.erase.pattern="{cmd.path}" "-C{config.path}" {erase.verbose} -p{build.mcu} -c{protocol} {program.extra_params} -e -Ulock:w:{bootloader.unlock_bits}:m -Uefuse:w:{bootloader.extended_fuses}:m -Uhfuse:w:{bootloader.high_fuses}:m -Ulfuse:w:{bootloader.low_fuses}:m
|
|
||||||
|
|
||||||
tools.avrdude.bootloader.params.verbose=-v
|
|
||||||
tools.avrdude.bootloader.params.quiet=-q -q
|
|
||||||
tools.avrdude.bootloader.pattern="{cmd.path}" "-C{config.path}" {bootloader.verbose} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{runtime.platform.path}/bootloaders/{bootloader.file}:i" -Ulock:w:{bootloader.lock_bits}:m
|
|
||||||
|
|
||||||
|
|
||||||
# USB Default Flags
|
|
||||||
# Default blank usb manufacturer will be filled it at compile time
|
|
||||||
# - from numeric vendor ID, set to Unknown otherwise
|
|
||||||
build.usb_manufacturer=Keyboardio
|
|
||||||
build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}'
|
|
@ -1,359 +0,0 @@
|
|||||||
/*
|
|
||||||
pins_arduino.h - Pin definition functions for Arduino
|
|
||||||
Part of Arduino - http://www.arduino.cc/
|
|
||||||
|
|
||||||
Copyright (c) 2007 David A. Mellis
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library 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
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General
|
|
||||||
Public License along with this library; if not, write to the
|
|
||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
||||||
Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
$Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Pins_Arduino_h
|
|
||||||
#define Pins_Arduino_h
|
|
||||||
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
|
|
||||||
// Workaround for wrong definitions in "iom32u4.h".
|
|
||||||
// This should be fixed in the AVR toolchain.
|
|
||||||
#undef UHCON
|
|
||||||
#undef UHINT
|
|
||||||
#undef UHIEN
|
|
||||||
#undef UHADDR
|
|
||||||
#undef UHFNUM
|
|
||||||
#undef UHFNUML
|
|
||||||
#undef UHFNUMH
|
|
||||||
#undef UHFLEN
|
|
||||||
#undef UPINRQX
|
|
||||||
#undef UPINTX
|
|
||||||
#undef UPNUM
|
|
||||||
#undef UPRST
|
|
||||||
#undef UPCONX
|
|
||||||
#undef UPCFG0X
|
|
||||||
#undef UPCFG1X
|
|
||||||
#undef UPSTAX
|
|
||||||
#undef UPCFG2X
|
|
||||||
#undef UPIENX
|
|
||||||
#undef UPDATX
|
|
||||||
#undef TCCR2A
|
|
||||||
#undef WGM20
|
|
||||||
#undef WGM21
|
|
||||||
#undef COM2B0
|
|
||||||
#undef COM2B1
|
|
||||||
#undef COM2A0
|
|
||||||
#undef COM2A1
|
|
||||||
#undef TCCR2B
|
|
||||||
#undef CS20
|
|
||||||
#undef CS21
|
|
||||||
#undef CS22
|
|
||||||
#undef WGM22
|
|
||||||
#undef FOC2B
|
|
||||||
#undef FOC2A
|
|
||||||
#undef TCNT2
|
|
||||||
#undef TCNT2_0
|
|
||||||
#undef TCNT2_1
|
|
||||||
#undef TCNT2_2
|
|
||||||
#undef TCNT2_3
|
|
||||||
#undef TCNT2_4
|
|
||||||
#undef TCNT2_5
|
|
||||||
#undef TCNT2_6
|
|
||||||
#undef TCNT2_7
|
|
||||||
#undef OCR2A
|
|
||||||
#undef OCR2_0
|
|
||||||
#undef OCR2_1
|
|
||||||
#undef OCR2_2
|
|
||||||
#undef OCR2_3
|
|
||||||
#undef OCR2_4
|
|
||||||
#undef OCR2_5
|
|
||||||
#undef OCR2_6
|
|
||||||
#undef OCR2_7
|
|
||||||
#undef OCR2B
|
|
||||||
#undef OCR2_0
|
|
||||||
#undef OCR2_1
|
|
||||||
#undef OCR2_2
|
|
||||||
#undef OCR2_3
|
|
||||||
#undef OCR2_4
|
|
||||||
#undef OCR2_5
|
|
||||||
#undef OCR2_6
|
|
||||||
#undef OCR2_7
|
|
||||||
|
|
||||||
#define NUM_DIGITAL_PINS 30
|
|
||||||
#define NUM_ANALOG_INPUTS 12
|
|
||||||
|
|
||||||
#define TXLED0 PORTD &= ~(1<<5)
|
|
||||||
#define TXLED1 PORTD |= (1<<5)
|
|
||||||
#define RXLED0 PORTB &= ~(1<<0)
|
|
||||||
#define RXLED1 PORTB |= (1<<0)
|
|
||||||
#define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0), TXLED0, RXLED0
|
|
||||||
|
|
||||||
#define SDA 2
|
|
||||||
#define SCL 3
|
|
||||||
#define LED_BUILTIN 13
|
|
||||||
|
|
||||||
// Map SPI port to 'new' pins D14..D17
|
|
||||||
#define SS 17
|
|
||||||
#define MOSI 16
|
|
||||||
#define MISO 14
|
|
||||||
#define SCK 15
|
|
||||||
|
|
||||||
// Mapping of analog pins as digital I/O
|
|
||||||
// A6-A11 share with digital pins
|
|
||||||
#define A0 18
|
|
||||||
#define A1 19
|
|
||||||
#define A2 20
|
|
||||||
#define A3 21
|
|
||||||
#define A4 22
|
|
||||||
#define A5 23
|
|
||||||
#define A6 24 // D4
|
|
||||||
#define A7 25 // D6
|
|
||||||
#define A8 26 // D8
|
|
||||||
#define A9 27 // D9
|
|
||||||
#define A10 28 // D10
|
|
||||||
#define A11 29 // D12
|
|
||||||
|
|
||||||
#define digitalPinToPCICR(p) ((((p) >= 8 && (p) <= 11) || ((p) >= 14 && (p) <= 17) || ((p) >= A8 && (p) <= A10)) ? (&PCICR) : ((uint8_t *)0))
|
|
||||||
#define digitalPinToPCICRbit(p) 0
|
|
||||||
#define digitalPinToPCMSK(p) ((((p) >= 8 && (p) <= 11) || ((p) >= 14 && (p) <= 17) || ((p) >= A8 && (p) <= A10)) ? (&PCMSK0) : ((uint8_t *)0))
|
|
||||||
#define digitalPinToPCMSKbit(p) ( ((p) >= 8 && (p) <= 11) ? (p) - 4 : ((p) == 14 ? 3 : ((p) == 15 ? 1 : ((p) == 16 ? 2 : ((p) == 17 ? 0 : (p - A8 + 4))))))
|
|
||||||
|
|
||||||
// __AVR_ATmega32U4__ has an unusual mapping of pins to channels
|
|
||||||
extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
|
|
||||||
#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
|
|
||||||
|
|
||||||
#ifdef ARDUINO_MAIN
|
|
||||||
|
|
||||||
// On the Arduino board, digital pins are also used
|
|
||||||
// for the analog output (software PWM). Analog input
|
|
||||||
// pins are a separate set.
|
|
||||||
|
|
||||||
// ATMEL ATMEGA32U4 / ARDUINO LEONARDO
|
|
||||||
//
|
|
||||||
// D0 PD2 RXD1/INT2
|
|
||||||
// D1 PD3 TXD1/INT3
|
|
||||||
// D2 PD1 SDA SDA/INT1
|
|
||||||
// D3# PD0 PWM8/SCL OC0B/SCL/INT0
|
|
||||||
// D4 A6 PD4 ADC8
|
|
||||||
// D5# PC6 ??? OC3A/#OC4A
|
|
||||||
// D6# A7 PD7 FastPWM #OC4D/ADC10
|
|
||||||
// D7 PE6 INT6/AIN0
|
|
||||||
//
|
|
||||||
// D8 A8 PB4 ADC11/PCINT4
|
|
||||||
// D9# A9 PB5 PWM16 OC1A/#OC4B/ADC12/PCINT5
|
|
||||||
// D10# A10 PB6 PWM16 OC1B/0c4B/ADC13/PCINT6
|
|
||||||
// D11# PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
|
|
||||||
// D12 A11 PD6 T1/#OC4D/ADC9
|
|
||||||
// D13# PC7 PWM10 CLK0/OC4A
|
|
||||||
//
|
|
||||||
// A0 D18 PF7 ADC7
|
|
||||||
// A1 D19 PF6 ADC6
|
|
||||||
// A2 D20 PF5 ADC5
|
|
||||||
// A3 D21 PF4 ADC4
|
|
||||||
// A4 D22 PF1 ADC1
|
|
||||||
// A5 D23 PF0 ADC0
|
|
||||||
//
|
|
||||||
// New pins D14..D17 to map SPI port to digital pins
|
|
||||||
//
|
|
||||||
// MISO D14 PB3 MISO,PCINT3
|
|
||||||
// SCK D15 PB1 SCK,PCINT1
|
|
||||||
// MOSI D16 PB2 MOSI,PCINT2
|
|
||||||
// SS D17 PB0 RXLED,SS/PCINT0
|
|
||||||
//
|
|
||||||
// TXLED PD5
|
|
||||||
// RXLED PB0
|
|
||||||
// HWB PE2 HWB
|
|
||||||
|
|
||||||
// these arrays map port names (e.g. port B) to the
|
|
||||||
// appropriate addresses for various functions (e.g. reading
|
|
||||||
// and writing)
|
|
||||||
const uint16_t PROGMEM port_to_mode_PGM[] = {
|
|
||||||
NOT_A_PORT,
|
|
||||||
NOT_A_PORT,
|
|
||||||
(uint16_t) &DDRB,
|
|
||||||
(uint16_t) &DDRC,
|
|
||||||
(uint16_t) &DDRD,
|
|
||||||
(uint16_t) &DDRE,
|
|
||||||
(uint16_t) &DDRF,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint16_t PROGMEM port_to_output_PGM[] = {
|
|
||||||
NOT_A_PORT,
|
|
||||||
NOT_A_PORT,
|
|
||||||
(uint16_t) &PORTB,
|
|
||||||
(uint16_t) &PORTC,
|
|
||||||
(uint16_t) &PORTD,
|
|
||||||
(uint16_t) &PORTE,
|
|
||||||
(uint16_t) &PORTF,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint16_t PROGMEM port_to_input_PGM[] = {
|
|
||||||
NOT_A_PORT,
|
|
||||||
NOT_A_PORT,
|
|
||||||
(uint16_t) &PINB,
|
|
||||||
(uint16_t) &PINC,
|
|
||||||
(uint16_t) &PIND,
|
|
||||||
(uint16_t) &PINE,
|
|
||||||
(uint16_t) &PINF,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
|
|
||||||
PD, // D0 - PD2
|
|
||||||
PD, // D1 - PD3
|
|
||||||
PD, // D2 - PD1
|
|
||||||
PD, // D3 - PD0
|
|
||||||
PD, // D4 - PD4
|
|
||||||
PC, // D5 - PC6
|
|
||||||
PD, // D6 - PD7
|
|
||||||
PE, // D7 - PE6
|
|
||||||
|
|
||||||
PB, // D8 - PB4
|
|
||||||
PB, // D9 - PB5
|
|
||||||
PB, // D10 - PB6
|
|
||||||
PB, // D11 - PB7
|
|
||||||
PD, // D12 - PD6
|
|
||||||
PC, // D13 - PC7
|
|
||||||
|
|
||||||
PB, // D14 - MISO - PB3
|
|
||||||
PB, // D15 - SCK - PB1
|
|
||||||
PB, // D16 - MOSI - PB2
|
|
||||||
PB, // D17 - SS - PB0
|
|
||||||
|
|
||||||
PF, // D18 - A0 - PF7
|
|
||||||
PF, // D19 - A1 - PF6
|
|
||||||
PF, // D20 - A2 - PF5
|
|
||||||
PF, // D21 - A3 - PF4
|
|
||||||
PF, // D22 - A4 - PF1
|
|
||||||
PF, // D23 - A5 - PF0
|
|
||||||
|
|
||||||
PD, // D24 / D4 - A6 - PD4
|
|
||||||
PD, // D25 / D6 - A7 - PD7
|
|
||||||
PB, // D26 / D8 - A8 - PB4
|
|
||||||
PB, // D27 / D9 - A9 - PB5
|
|
||||||
PB, // D28 / D10 - A10 - PB6
|
|
||||||
PD, // D29 / D12 - A11 - PD6
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
|
|
||||||
_BV(2), // D0 - PD2
|
|
||||||
_BV(3), // D1 - PD3
|
|
||||||
_BV(1), // D2 - PD1
|
|
||||||
_BV(0), // D3 - PD0
|
|
||||||
_BV(4), // D4 - PD4
|
|
||||||
_BV(6), // D5 - PC6
|
|
||||||
_BV(7), // D6 - PD7
|
|
||||||
_BV(6), // D7 - PE6
|
|
||||||
|
|
||||||
_BV(4), // D8 - PB4
|
|
||||||
_BV(5), // D9 - PB5
|
|
||||||
_BV(6), // D10 - PB6
|
|
||||||
_BV(7), // D11 - PB7
|
|
||||||
_BV(6), // D12 - PD6
|
|
||||||
_BV(7), // D13 - PC7
|
|
||||||
|
|
||||||
_BV(3), // D14 - MISO - PB3
|
|
||||||
_BV(1), // D15 - SCK - PB1
|
|
||||||
_BV(2), // D16 - MOSI - PB2
|
|
||||||
_BV(0), // D17 - SS - PB0
|
|
||||||
|
|
||||||
_BV(7), // D18 - A0 - PF7
|
|
||||||
_BV(6), // D19 - A1 - PF6
|
|
||||||
_BV(5), // D20 - A2 - PF5
|
|
||||||
_BV(4), // D21 - A3 - PF4
|
|
||||||
_BV(1), // D22 - A4 - PF1
|
|
||||||
_BV(0), // D23 - A5 - PF0
|
|
||||||
|
|
||||||
_BV(4), // D24 / D4 - A6 - PD4
|
|
||||||
_BV(7), // D25 / D6 - A7 - PD7
|
|
||||||
_BV(4), // D26 / D8 - A8 - PB4
|
|
||||||
_BV(5), // D27 / D9 - A9 - PB5
|
|
||||||
_BV(6), // D28 / D10 - A10 - PB6
|
|
||||||
_BV(6), // D29 / D12 - A11 - PD6
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
TIMER0B, /* 3 */
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
TIMER3A, /* 5 */
|
|
||||||
TIMER4D, /* 6 */
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
TIMER1A, /* 9 */
|
|
||||||
TIMER1B, /* 10 */
|
|
||||||
TIMER0A, /* 11 */
|
|
||||||
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
TIMER4A, /* 13 */
|
|
||||||
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
NOT_ON_TIMER,
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint8_t PROGMEM analog_pin_to_channel_PGM[] = {
|
|
||||||
7, // A0 PF7 ADC7
|
|
||||||
6, // A1 PF6 ADC6
|
|
||||||
5, // A2 PF5 ADC5
|
|
||||||
4, // A3 PF4 ADC4
|
|
||||||
1, // A4 PF1 ADC1
|
|
||||||
0, // A5 PF0 ADC0
|
|
||||||
8, // A6 D4 PD4 ADC8
|
|
||||||
10, // A7 D6 PD7 ADC10
|
|
||||||
11, // A8 D8 PB4 ADC11
|
|
||||||
12, // A9 D9 PB5 ADC12
|
|
||||||
13, // A10 D10 PB6 ADC13
|
|
||||||
9 // A11 D12 PD6 ADC9
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* ARDUINO_MAIN */
|
|
||||||
|
|
||||||
// These serial port names are intended to allow libraries and architecture-neutral
|
|
||||||
// sketches to automatically default to the correct port name for a particular type
|
|
||||||
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
|
|
||||||
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
|
|
||||||
//
|
|
||||||
// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor
|
|
||||||
//
|
|
||||||
// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial
|
|
||||||
//
|
|
||||||
// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library
|
|
||||||
//
|
|
||||||
// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins.
|
|
||||||
//
|
|
||||||
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
|
|
||||||
// pins are NOT connected to anything by default.
|
|
||||||
#define SERIAL_PORT_MONITOR Serial
|
|
||||||
#define SERIAL_PORT_USBVIRTUAL Serial
|
|
||||||
#define SERIAL_PORT_HARDWARE Serial1
|
|
||||||
#define SERIAL_PORT_HARDWARE_OPEN Serial1
|
|
||||||
|
|
||||||
#endif /* Pins_Arduino_h */
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue