--- /dev/null
+:1000000000800010C5000000110100001301000075\r
+:100010001501000017010000190100000000000098\r
+:100020000000000000000000000000001D010000B2\r
+:100030001F0100000000000021010000230100005A\r
+:100040002501000025010000250100002501000018\r
+:100050002501000025010000250100002501000008\r
+:1000600025010000250100002501000025010000F8\r
+:1000700025010000250100002501000025010000E8\r
+:1000800025010000250100002501000025010000D8\r
+:1000900025010000250100002501000025010000C8\r
+:1000A00025010000250100002501000025010000B8\r
+:1000B00025010000250100002501000025010000A8\r
+:1000C000250100000B490C4A0C4B9A423EBF51F8E7\r
+:1000D000040B42F8040BF8E700200949094A914251\r
+:1000E0003CBF41F8040BFAE7074880470748004740\r
+:1000F000FEE70000581E000000000010080000107D\r
+:100100000800001098060010290100005115000099\r
+:10011000FEE7FEE7FEE7FEE7FEE700BFFEE7FEE7DD\r
+:10012000FEE7FEE7FEE70000364B2022C3F8A021E1\r
+:10013000344BD3F8A02112F0400FF9D00322C3F8BA\r
+:100140000421033AC3F8A821C3F8AC212E4A0120A8\r
+:10015000C3F80C01AA21C3F884205522C3F88C10DF\r
+:10016000C3F88C20C3F88000C3F88C10C3F88C202F\r
+:10017000244BD3F8882012F0806FF9D00322C3F803\r
+:100180008020A732C3F88C20553AC3F88C201D4B31\r
+:10019000D3F8882012F0407FF9D02322C3F8A4209E\r
+:1001A000552187320120C3F88C20C3F8AC20C3F856\r
+:1001B000AC10C3F8A000C3F8AC20C3F8AC10114BCE\r
+:1001C000D3F8A82012F4806FF9D00322C3F8A0203E\r
+:1001D000A732C3F8AC20553AC3F8AC20094BD3F88A\r
+:1001E000A82012F4407FF9D0084AC3F8C4200022A6\r
+:1001F000C3F8C8211A6822F4704242F480421A609F\r
+:10020000704700BF00C00F4063000500DE87280470\r
+:1002100080F30988704700000A4B4FF0FF3101223C\r
+:10022000C3F88010C3F88420C3F88011C3F8842178\r
+:100230000023044A00219A180133202B82F800136E\r
+:10024000F7D1704700E100E00B4B4FF020620B4903\r
+:100250005A6000229A60D9601A615A61064B0020E8\r
+:10026000D1180132202A0876F8D1213A58629A62D0\r
+:10027000DA621A63704700BF00ED00E00000FA0583\r
+:10028000014B9860704700BF00ED00E070B5427808\r
+:10029000037884785B000F2A01D9103A01335200A9\r
+:1002A0000321914014FA02F29B0003F1804303F50D\r
+:1002B00030331D6825EA010141EA02021A604278E2\r
+:1002C0000378C5785B000F2A01D9103A0133520038\r
+:1002D0000324944015FA02F20F4951F8236026EAEC\r
+:1002E000040444EA020241F82320027841780079AC\r
+:1002F0000A4B012806D110FA01F153F8220040EA16\r
+:10030000010106E0012010FA01F153F8220020EA71\r
+:10031000010143F8221070BD40C0024068C0024095\r
+:1003200004289ABF024B53F820000020704700BFFA\r
+:10033000881C000038B50C461546FFF7F1FF40B1A8\r
+:100340001DB103681C43046038BD036823EA04043C\r
+:10035000046038BD08B5FFF7E3FF00B1406908BD90\r
+:10036000012909D10A4A0B4BD2F8C41000EA030351\r
+:100370000B43C2F8C430704749B9054A054BD2F85F\r
+:10038000C41001EA030323EA0000C2F8C400704766\r
+:1003900000C00F40DEF7EFEF074B984202D14FF459\r
+:1003A000001004E0054B984204D14FF48060002116\r
+:1003B000FFF7D6BF704700BF008008400000034031\r
+:1003C000436801290CBF43F0020303F00D034360AF\r
+:1003D000704703E011F8013B00F8013B013AF9D204\r
+:1003E000704701E000F8011B013AFBD2704708B5E5\r
+:1003F00060B1037853B1C3888B4209D1407801F0D2\r
+:1004000064FA10F0010018BF032008BD092008BDE0\r
+:10041000092008BD10B504460A46407804F13001B1\r
+:10042000012301F057FA80BB94F82E3294F82F2262\r
+:1004300043EA0222174B12B29A4228D194F868205C\r
+:1004400094F86930120442EA036294F866301A4361\r
+:1004500094F8673042EA03220F4B22F07F429A421F\r
+:1004600016D094F8840094F88520000440EA0260D5\r
+:1004700094F88220104394F8832040EA022020F070\r
+:100480007F40C01A18BF012010BD032010BD0220FC\r
+:1004900010BD00BF55AAFFFF464154002DE9F041B1\r
+:1004A000036815461A78303A092A07D85C783A2C3E\r
+:1004B00004D102330360002A40F06081BA4B1C680B\r
+:1004C0000C60002C00F05D81237873B1607801F03E\r
+:1004D000FCF910F0010F08D1002D00F0558110F04B\r
+:1004E000040018BF0A20BDE8F08100202070607071\r
+:1004F00001F0E6F910F0010F40F049811DB110F054\r
+:10050000040F40F0478100212046FFF783FF0128B8\r
+:1005100017D194F8F231002B00F0428194F8F86181\r
+:1005200094F8F931360446EA036694F8F631204629\r
+:100530001E4394F8F73146EA03263146FFF76AFF77\r
+:1005400000E00026032800F02881002840F02881E0\r
+:1005500094F83C2094F83B3043EA0223B3F5007F43\r
+:1005600040F01E8194F8470094F8463053EA00208A\r
+:100570000DD194F8560094F85730000440EA036017\r
+:1005800094F85430184394F8553040EA032094F816\r
+:100590004050E0616B1EDBB2012BE57000F2008180\r
+:1005A00094F83D10A170002900F0FA804B1E0B4218\r
+:1005B00040F0F68094F8422094F8413043EA022259\r
+:1005C00012F00F0F228140F0EB8094F8447094F801\r
+:1005D000433053EA07230DD194F8523094F8537006\r
+:1005E0001B0443EA076394F850703B4394F851703E\r
+:1005F00043EA072394F83FC094F83E7057EA0C2C66\r
+:1006000000F0CE8005FB00F80CEB12174744BB420C\r
+:10061000C0F0C680DB1BB3FBF1F3002B00F0C08001\r
+:1006200040F6F5718B4206D94FF6F575AB428CBF9B\r
+:100630000325022500E001250233BF1906EB0C015A\r
+:10064000032DA361A762216213D1002A40F0A88084\r
+:1006500094F85E2094F85F10120442EA016294F864\r
+:100660005C109B000A4394F85D1042EA012262622A\r
+:1006700010E0002A00F09480022D4FEA430218BFD8\r
+:10068000D218414418BF03F0010361620CBF134646\r
+:1006900003EB520303F5FE730333B0EB532FC0F0AB\r
+:1006A0008D804FF0FF3323610133032DE3605AD176\r
+:1006B000637194F8612094F86030607843EA022214\r
+:1006C000B218626104F13001012301F003F900283E\r
+:1006D00049D194F82E3294F82F2243EA0222334B68\r
+:1006E00012B29A423FD194F8322094F83330120477\r
+:1006F00042EA036294F830301A4394F8313042EA07\r
+:1007000003222B4B9A422ED194F8162294F81732DA\r
+:10071000120442EA036294F814321A4394F8153230\r
+:1007200042EA0322234B9A421DD194F81E3294F8D8\r
+:100730001F221B0443EA026394F81C22134394F81B\r
+:100740001D2243EA0223E36094F81A3294F81B2234\r
+:100750001B0443EA026394F81822134394F8192205\r
+:1007600043EA02232361144A2570138800200133D1\r
+:100770001380E380E0622071BDE8F0810B20BDE8CA\r
+:10078000F0810C20BDE8F0812846BDE8F08103200F\r
+:10079000BDE8F0810A20BDE8F0810120BDE8F081CC\r
+:1007A0000D20BDE8F08100BF0C00001055AAFFFF2E\r
+:1007B0005252614172724161080000100D20BDE883\r
+:1007C000F08100BF2DE9F041C56A04468D420E4616\r
+:1007D0002AD00379E3B100F13008414640782A4637\r
+:1007E000012301F08EF810BBE269236A2071D3184F\r
+:1007F0009D420DD2E77809E0E3696078ED18414643\r
+:100800002A46012301F07DF8013FFFB2012FF3D802\r
+:1008100086B1607804F130013246012301F05AF8C4\r
+:1008200058B9E662BDE8F0810020BDE8F081012002\r
+:10083000BDE8F0813046BDE8F0810120BDE8F081DF\r
+:1008400070B500210446FFF7BDFF054600284FD1D3\r
+:100850002378032B44D16379002B41D004F1300677\r
+:10086000E062294630464FF40072FFF7BAFD552387\r
+:1008700084F82E32DB1884F82F32583B84F830305D\r
+:1008800084F831300F3384F8323084F8173223691A\r
+:10089000412284F8332084F816221A0A84F8183288\r
+:1008A00084F819221A0C1B0E84F81B32E36884F8B2\r
+:1008B0001A221A0A722184F81C3284F81D221A0C9A\r
+:1008C0001B0E84F8141284F8151284F81E2284F882\r
+:1008D0001F32607831466269012301F012F86571B8\r
+:1008E000002160780A4601F023F8003818BF012083\r
+:1008F00070BD83690239023B994204D28278836ACF\r
+:1009000002FB01307047002070470129F8B504460A\r
+:100910000D465AD98369994257D20378022B29D0C0\r
+:10092000032B38D0012B52D101EB5106016A01EBA8\r
+:100930005621FFF747FF002849D1F305216ADB0D57\r
+:100940000136E318204601EB562193F83070FFF78B\r
+:1009500039FF00283BD1F605F60DA61996F83000B0\r
+:1009600015F0010F47EA002001D00009F8BD00058D\r
+:10097000000DF8BD016A01EB1521FFF723FF30BB25\r
+:100980002D06ED0D6319641993F8303094F8310099\r
+:1009900043EA0020F8BD016A01EBD511FFF712FF11\r
+:1009A000C0B96D06ED0D05F13003E318D8789A78DB\r
+:1009B00000065B7840EA0240641940EA032094F89C\r
+:1009C0003030184320F07040F8BD0120F8BD4FF0E2\r
+:1009D000FF30F8BD4FF0FF30F8BD70B50D46C18057\r
+:1009E00081680446012939D003689A69914235D259\r
+:1009F00071B91A78032A01D1596A49B90022E26013\r
+:100A00001A89AA422AD95B6A03EB151323611AE0FB\r
+:100A10009E7836010EE02068FFF777FFB0F1FF3FC8\r
+:100A2000014619D0012819D923689B69984217D229\r
+:100A3000AD1BADB2B542EED2E1602068FFF759FFC1\r
+:100A400000EB15102061236805F00F0530336D01B0\r
+:100A50005D196561002070BD012070BD022070BD70\r
+:100A6000022070BD10B5C1880446FFF7B6FF50B92B\r
+:100A700020682169FFF7A6FE28B96369E5221A708C\r
+:100A80002368E43A1A7110BD0129F8B504460D46F1\r
+:100A900016466ED9836999426DD20378022B3AD0FB\r
+:100AA000032B47D0012B60D101EB5107016A01EB09\r
+:100AB0005721FFF787FE002858D1FB05DB0D15F005\r
+:100AC0000105F2B206D0E11891F8301001F00F01E3\r
+:100AD00041EA0212E318216A83F83020013701232A\r
+:100AE0002371204601EB5721FFF76CFE00283DD112\r
+:100AF000FF05FF0D0DB1360909E0E31993F8303019\r
+:100B0000360A23F00F0306F00F0643EA0606E7193C\r
+:100B100087F830602AE0016A01EB1521FFF752FEE9\r
+:100B200020BB2D06ED0D631983F83060360A83F87B\r
+:100B300031601BE0016A01EBD511FFF743FEA8B954\r
+:100B40006D06ED0D05F13003E318DA78651912062C\r
+:100B500002F0704242EA0606320A85F830605A70A6\r
+:100B6000320C360E9A70DE7000E0022001232371F1\r
+:100B7000F8BD0220F8BD0220F8BDF8B505460E46C6\r
+:100B800031B9C7686FB183699F4228BF012709E067\r
+:100B9000FFF7BBFE012839D9AB6998423BD33746F2\r
+:100BA00000E001273C46AB6901349C4202D3012F8F\r
+:100BB0002ED9022428462146FFF7A7FE38B1B0F10E\r
+:100BC000FF3F28D0012826D0BC42ECD120E02846A7\r
+:100BD00021466FF07042FFF757FF88B92EB12846C3\r
+:100BE00031462246FFF750FF50B92B69EC60B3F154\r
+:100BF000FF3F0FD0013B2B6101236B712046F8BDF5\r
+:100C0000012814BF01204FF0FF30F8BD0120F8BDCE\r
+:100C10000020F8BD2046F8BD2DE9F041C688044605\r
+:100C20000136B6B20F46002E6BD00369002B68D098\r
+:100C300016F00F0F5AD1C16801330361006819B96A\r
+:100C40000389B34252D85CE08378013B13EA161360\r
+:100C50004CD1FFF75AFE0128054658D9B0F1FF3FA5\r
+:100C600058D0206883699D423AD3002F49D0E1686B\r
+:100C7000FFF783FF0546002846D0012847D0B0F192\r
+:100C8000FF3F47D020680021FFF79CFD0746002862\r
+:100C900040D12068394630304FF40072FFF7A1FB95\r
+:100CA000D4F8008029464046FFF723FEC8F82C0000\r
+:100CB0000FE04FF0010883F8048020680021FFF75F\r
+:100CC00081FD002829D123684744DA6AFFB24244F3\r
+:100CD000DA6223689A78BA42EBD8DA6AD71BDF6205\r
+:100CE000E56020682946FFF704FE20612368E6805E\r
+:100CF00006F00F06303376019E1966610020BDE8CC\r
+:100D0000F0810420BDE8F0810720BDE8F0810220D9\r
+:100D1000BDE8F0810120BDE8F0814046BDE8F081EA\r
+:100D20002DE9F0410B7804462F2B0D4601D05C2BAA\r
+:100D300000D101350026A6602B781F2B06D820464F\r
+:100D40003146FFF74AFE6661BDE8F081A6692021C1\r
+:100D500030460B22FFF745FB00210F4608200A46CC\r
+:100D6000EB5D0137202B3ED92F2B3ED05C2B3CD0A6\r
+:100D70002E2B01D081420AD3082840F092802E2BDE\r
+:100D800040F08F8092000146D2B20330E8E713F0C2\r
+:100D9000800F06D0DFF818C1803B1CF8033042F00A\r
+:100DA0000302DFF810C11CF8018FB8F1000F02D068\r
+:100DB0009845F8D175E0A3F1410C5FFA8CFCBCF1C9\r
+:100DC000190F02D842F002020AE0A3F1610C5FFAA7\r
+:100DD0008CFCBCF1190F03D8203B42F00102DBB2BE\r
+:100DE00073540131BCE7042300E00023002958D0EC\r
+:100DF0003178E52901D1E0393170082801D192001C\r
+:100E0000D2B202F00301012902F00C0208BF43F044\r
+:100E10001003042A08BF43F00803F37220460021A0\r
+:100E2000FFF7DBFDE0B920682169FFF7CBFCB8B91B\r
+:100E3000626913789BB1D37A13F0080F0AD1A169C4\r
+:100E4000034601E00B2B0BD012F803C0CE5C01333C\r
+:100E5000B445F7D020460021FFF7DEFEE2E704208C\r
+:100E6000A369DB7A40B104281FD103F00403002BEF\r
+:100E700008BF0520BDE8F08113F0040F15D16369A8\r
+:100E8000DA7A12F0100F0FD0597D1A7DED1942EA6F\r
+:100E90000122D97E9B7E43EA012343EA0243A360F9\r
+:100EA00054E70620BDE8F0810520BDE8F08100BFD1\r
+:100EB000AB1C00009B1C000070B500210546FFF72D\r
+:100EC0008CFD0446E8B928682969FFF77BFC0446D5\r
+:100ED000B8B96B691B78E52B15D0A3B12846012161\r
+:100EE000FFF79AFEEDE76E69214630462022FFF7B4\r
+:100EF00078FA0B223046A969FFF76BFA2B680122BA\r
+:100F00001A71204670BD28682969FFF75BFC04460A\r
+:100F10000028F6D1E7E710B50423044614E0206862\r
+:100F2000FFF750FCD0B962691378B3B1E52B05D057\r
+:100F30002E2B03D0D37A13F0080F0DD020460021BA\r
+:100F4000FFF76AFE034648B921690029E7D1184630\r
+:100F500013B100222261184610BD04200346F8E7B1\r
+:100F6000012970B504460E4623D9836999421AD3E4\r
+:100F70001FE020463146FFF7C8FC0546C0B10128F6\r
+:100F800017D0B0F1FF3F16D0204631460022FFF7C0\r
+:100F90007BFD88B92369B3F1FF3F03D0013323619F\r
+:100FA000012363712E46A3699E42E2D3002070BDE7\r
+:100FB00070BD022070BD012070BD000048B9064A16\r
+:100FC000136803B1187009B100230B701160002081\r
+:100FD00070470B20704700BF0C0000102DE9F04156\r
+:100FE0008CB00023044602F01F050CA840F82C1D0D\r
+:100FF000236002A905F01E02FFF750FA09AB08931F\r
+:1010000018B902A80199FFF78BFE079E10B9002EB0\r
+:1010100008BF062015F01C0F45D050B1042802D19E\r
+:1010200002A8FFF749FF079E00286FD145F0080589\r
+:1010300006E015F0040F6CD1F37A13F0110F6CD1A8\r
+:1010400015F0080F3BD000F05DFA327D030A777D82\r
+:10105000F373030CB0733374000E0023F17E42EA85\r
+:101060000727B27EF37233777377B377F377B3766C\r
+:10107000F37633757375707442EA01220298013376\r
+:1010800052EA074703711AD03946D0F82C80FFF78F\r
+:1010900067FF00283AD10298013FC7604146FFF739\r
+:1010A00091FB60B132E0002830D1F37A13F0100FD9\r
+:1010B00031D115F0020F02D013F0010F2DD1029B98\r
+:1010C00015F0080FDA6A18BF45F02005A571E26136\r
+:1010D0002662327D717DF07E42EA0121B27E42EAD3\r
+:1010E000002242EA01422261B27FF17F120442EA09\r
+:1010F0000162317F00200A43717F2360DB8842EA6E\r
+:101100000122E260A060A0616062A3800CB0BDE833\r
+:10111000F0810820FAE70420F8E70720F6E72DE938\r
+:10112000F74F01931646019A044600238846136040\r
+:101130000068A188FFF75BF90546002840F0A88009\r
+:10114000A37913F0800F40F0A08013F0010F00F09E\r
+:101150009E80E268A368C3EB02094E4538BFB146E2\r
+:101160008EE0A368DA05D20D002A70D1206890F8CD\r
+:1011700002B00BF1FF3B0BEA532B5FFA8BFBBBF189\r
+:10118000000F0BD10BB9206902E06169FFF7BDFBCD\r
+:1011900001280AD9B0F1FF3F52D06061D4F800A015\r
+:1011A00061695046FFF7A5FB20B9A37963F07F037F\r
+:1011B000A3716AE05FEA592700EB0B0623D09AF887\r
+:1011C000023007EB0B029A4288BFCBEB0307FBB25E\r
+:1011D0009AF801004146324600F07CFBA3790028D2\r
+:1011E0002FD113F0400F0CD0A3699E1BBE4208D232\r
+:1011F000760208EB060004F128014FF40072FFF7B5\r
+:10120000E8F87F0232E0A37913F0400F0CD00123FD\r
+:101210009AF8010004F12801A26900F072FBA37999\r
+:1012200078B923F04003A371A369B3420ED02146DD\r
+:1012300051F8283B32465878012300F04BFB28B187\r
+:10124000A37963F07F03A371012521E0A661A36860\r
+:1012500004F12801DB05DB0DC3F50077B94538BF84\r
+:101260004F464046C9183A46FFF7B3F8A368019ABB\r
+:10127000DB19A3601368B844DB191360C7EB0909D5\r
+:10128000B9F1000F7FF46DAF02E0022500E0072501\r
+:101290002846BDE8FE8F70B50446A1880068FFF7B8\r
+:1012A000A6F8002848D1A37913F0200F44D013F0FA\r
+:1012B000400F0DD0214651F8283BA26958780123F0\r
+:1012C00000F01FFB002836D1A37923F04003A3715F\r
+:1012D0002068E169FFF776FA00282DD1256AEB7ABC\r
+:1012E00043F02003EB72E3682B77A3891B0A6B772B\r
+:1012F000E389AB77E37BEB772369AB76238A1B0A21\r
+:10130000EB76638A2B75638A1B0A6B7500F0FAF81B\r
+:10131000030AA875EB75030C000E2B766876A3798B\r
+:10132000012223F02003A37123681A712068BDE80D\r
+:101330007040FFF785BA012070BD10B50446FFF775\r
+:10134000AAFF00B9206010BD30B595B014AB43F8CA\r
+:101350004C0D18460AA90122FFF7A0F8002842D137\r
+:1013600011AB0AA801991093FFF7DAFC00283AD1D3\r
+:101370000F9B3BB1D87A00F00100002814BF072072\r
+:10138000002000E006205D7D1C7DD97E9A7E50BB4A\r
+:10139000DB7A44EA052442EA012213F0100F42EA04\r
+:1013A000044422D0012C11D903A80AA91C22FFF75A\r
+:1013B00010F803A802210594FFF70FFB98B903A8C2\r
+:1013C000FFF7A9FD20B104280FD00CE002200AE0AD\r
+:1013D000072008E024B10A982146FFF7C1FD10B9A3\r
+:1013E0000A98FFF72DFA15B030BD0AA8FFF73AFBAF\r
+:1013F0000028EFD0F7E730B59BB01AAB43F8640D87\r
+:101400000C4618460AA90122FFF748F8002875D1B2\r
+:101410000A9B0AA80393019917AB1093FFF780FC6E\r
+:1014200000286BD10F99002965D011A80B31152226\r
+:10143000FEF7CFFF03A80AA91C22FEF7CAFF03A8E4\r
+:101440002146FFF76DFC002857D0042856D103A889\r
+:10145000FFF732FD002851D1089C132204F10D0042\r
+:101460000DF14601FEF7B5FF9DF84430012243F02F\r
+:101470002003E3720A9B1A71059B0C9A9A4232D0A0\r
+:10148000E37A13F0100F2ED0237D617DE27E43EAD4\r
+:101490000121A37E039D43EA022343EA014128463A\r
+:1014A000FFF727FA014600282AD02846FFF78AF9D5\r
+:1014B000039B18BB93F851202E2A14D11A78032AC3\r
+:1014C000059A03D1596A8A4208BF0022110A83F89B\r
+:1014D0006A20120C83F86420120A83F86520012226\r
+:1014E00083F86B101A710AA8FFF7BCFA30B90A9892\r
+:1014F000FFF7A6F902E0042000E008201BB030BD91\r
+:101500000220FBE7004870475C64173F37B51D2396\r
+:101510000025042468468DF801308DF800408DF8D0\r
+:1015200002508DF803508DF80450FEF7AFFE2046B0\r
+:101530004FF000512A46FEF7FDFE2046FEF70AFF57\r
+:1015400000F00050431B584240EB03003EBD00003A\r
+:10155000F0B5ADF5037DFEF75FFEFEF775FE3D4984\r
+:101560004FF6FF02CB68002003EA02023A4B42EA40\r
+:101570000303CB60FEF784FE00F025F9002862D15A\r
+:10158000FFF7C4FF002853D13449FFF717FD344E4D\r
+:101590003070002845D1334833490122FFF71EFD42\r
+:1015A000307000283DD12F4BDD68002D39D02F4BF6\r
+:1015B0001F692F4B5B6F0133DB1B9D4231D8044603\r
+:1015C0004FF400738193E318AB4284BFC4EB05036F\r
+:1015D0008193244801A9819A81ABFFF7A0FD307067\r
+:1015E00068B9819A02E0FF238B540132B2F5007F83\r
+:1015F00001A9F8D3E019819200F03CFA00E02C46F2\r
+:1016000004F50074AC42DBD31648FFF796FE307049\r
+:101610001848FFF799FE174930701348FFF7EBFEA3\r
+:10162000307000F0C1FA00F077FA08B100F066FA05\r
+:10163000084A114BD16801F4E06141EA0303D36029\r
+:10164000BFF34F8FFEE700F059FA00200DF5037D40\r
+:10165000F0BD00BF00ED00E00005FA0568020010D3\r
+:10166000640200103C0000102B1D0000C01D000093\r
+:10167000481D0000391D00000400FA0511F0030F99\r
+:1016800070B504460E4601D0002509E089B2BDE8D8\r
+:10169000704000F0C3BAFF2000F0B4FA6055013585\r
+:1016A000B542F8DB70BD70B504460E46002503E078\r
+:1016B000605D00F0A7FA0135B542F9DB70BD07B5F2\r
+:1016C0000A0E8DF802200A0C40F040008DF803202D\r
+:1016D0000A0AFF238DF801008DF804208DF805100B\r
+:1016E0009522684608218DF800308DF806208DF887\r
+:1016F0000730FFF7D8FF0EBD10B50824FF2000F01B\r
+:1017000081FAFF2803D1013CE4B2002CF6D110BDD0\r
+:1017100030B50021044685B00920FFF7D0FFFFF760\r
+:10172000EBFFFE28FBD10025FF2000F06BFA0DF83F\r
+:1017300005000135102DF7D1FF2000F063FAFF20DE\r
+:1017400000F060FA9DF807109DF80820890001EB71\r
+:1017500092119DF80520012302F00F0213FA02F204\r
+:101760009DF8065092B205F00305AD02491901310A\r
+:101770004A439DF809009DF80A1000F00300C909CA\r
+:1017800001EB400102318B409BB2534300202360A8\r
+:1017900005B030BD10B5FFF7AFFF000284B2FF20E7\r
+:1017A00000F030FA44EA000080B210BD08B50D2008\r
+:1017B0000021FFF784FFFFF7EDFF00280CBF012099\r
+:1017C0004FF0FF3008BD10B500F0BAF94FF47A744D\r
+:1017D00000200146FFF773FFFFF78EFF012802D1BB\r
+:1017E0004FF4FA4402E0DCB1013CF1E701200021B2\r
+:1017F000FFF765FFFFF780FF012802D154B1013CDC\r
+:10180000F4E750B9FFF7D2FF0028ACBF00206FF01B\r
+:10181000010010BD6FF0020010BD6FF0020010BD9E\r
+:10182000FF2814BF6FF001004FF0FF3010BD10B55E\r
+:101830000C4641021820FFF742FFFFF75DFFFE2034\r
+:1018400000F0E0F94FF400712046FFF72CFFFF2075\r
+:1018500000F0D8F9FF2000F0D5F9FF2000F0D2F910\r
+:10186000FF2000F0CFF9FF28FAD1FF3810BD70B586\r
+:101870000D4641021120FFF722FFFFF73DFF4FF613\r
+:10188000FF760446FFF738FFFF2803D116B1013E6B\r
+:10189000B6B2F7E774B9FE280FD14FF400712846AD\r
+:1018A000FFF7ECFEFF2000F0ADF9FF2000F0AAF9F1\r
+:1018B000204670BD4FF0FF3070BD4FF0FF3070BD5F\r
+:1018C00008B9FFF780BF02207047002814BF02202C\r
+:1018D00000207047F8B514461F4670B90E460546FD\r
+:1018E00007E031462819FFF7C2FF06F5007630B948\r
+:1018F0000135BD42F5DB0020F8BD0220F8BD012016\r
+:10190000F8BDF8B514461F4670B90E46054607E007\r
+:1019100031462819FFF78BFF06F5007630B90135FF\r
+:10192000BD42F5DB0020F8BD0220F8BD0120F8BD66\r
+:1019300010B5044690B9022904D003290AD0012920\r
+:101940000DD103E04FF40073138008E01046FFF759\r
+:10195000DFFE04E04FF40073136000E00224204631\r
+:1019600010BD62B6704772B67047000008B50248F5\r
+:101970000249034B984708BD2400001010000010D6\r
+:10198000F11FFF1F2DE9F04104460D4616461F4684\r
+:10199000FFF7E9FF064B33221A605D609E60DF604F\r
+:1019A0001C61FFF7E3FFBDE8F041FFF7DABF00BFBE\r
+:1019B000240000102DE9F04104460D4616461F464E\r
+:1019C000FFF7D1FF064B38221A605D609E60DF6032\r
+:1019D0001C61FFF7CBFFBDE8F041FFF7C2BF00BFBE\r
+:1019E0002400001010B5054B34241C605860996029\r
+:1019F000DA60BDE81040FFF7B9BF00BF2400001057\r
+:101A000010B5054B32241C6058609960DA60BDE85F\r
+:101A10001040FFF7ABBF00BF2400001070B50546B3\r
+:101A20000E46FFF7A0FF114B042453F8042F9642F3\r
+:101A300014D20F4B53F824309E4209D12046214640\r
+:101A40002A46FFF7DDFF204621462A46FFF7CAFF58\r
+:101A5000204621462A46FFF7D3FF02E001341E2C20\r
+:101A6000E3D1BDE87040FFF77CBF00BF541D00000C\r
+:101A7000C01D00002DE9F04180461C480023046889\r
+:101A800004E01B4DCE5C6519EE5401339342F8D14E\r
+:101A900004681C19B4F5007F046024D1154F4FF47D\r
+:101AA0007A763868144DB0FBF6F04146FFF7B6FF82\r
+:101AB0002B6803B1FEE738682346B0FBF6F04146D9\r
+:101AC0000B4AFFF75FFF2B6803B1FEE73868234638\r
+:101AD000B0FBF6F04146064AFFF76CFF2B6803B1F6\r
+:101AE000FEE7024A13600020BDE8F08138000010D4\r
+:101AF00098040010000000101000001008B5054BFD\r
+:101B00004FF480409860FEF783FB44F204031B68A7\r
+:101B1000984708BD00ED00E008B5074B35221A6074\r
+:101B2000313A5A609A60FFF721FF044B1868003879\r
+:101B300018BF012008BD00BF2400001010000010D5\r
+:101B4000154BD3F8C42042F48062C3F8C420D3F804\r
+:101B5000A82122F44012C3F8A821D3F8A82142F406\r
+:101B60008012C3F8A8210D4B1A6842F430721A6033\r
+:101B70001A6822F480721A60094B1A6822F47F22D4\r
+:101B80001A601A6842F428221A60064B07221A606B\r
+:101B9000053A5A60704700BF00C00F4000C00920DE\r
+:101BA00000C002400000034013B500211448FEF7B6\r
+:101BB00007FC1348FEF7F0FB032200238DF80320F7\r
+:101BC0006846921802AC8DF801208DF802308DF82D\r
+:101BD000043004F8083DFEF759FB072368468DF8EA\r
+:101BE0000130FEF753FB082368468DF80130FEF7FD\r
+:101BF0004DFB092368468DF80130FEF747FB1CBDFD\r
+:101C000000000340044B9860034BDA6812F0100F99\r
+:101C1000FAD19868C0B2704700000340194B70B504\r
+:101C20001C684A08072994BF1146082144F00F0494\r
+:101C30001C600B4605E0134C4FF6FF75013BA56099\r
+:101C40009BB2002BF7D10F4CE56815F0040FFAD0CA\r
+:101C5000A568561AB342B8BF4FF6FF76ADB2B8BF0B\r
+:101C6000A6602C0A013304709CB29442457001D2E4\r
+:101C70000230E8E7034B1A6822F00F0242F0070235\r
+:101C80001A6070BD0000034000C0092020C0092078\r
+:101C900040C0092060C0092080C00920222A2B2CC6\r
+:101CA0003A3B3C3D3E3F5B5D7C7F0080818283848C\r
+:101CB00085868788898A8B8C8D8E8F90919293945C\r
+:101CC00095969798998A9B8C8D8E8FA0A1A2A3A49C\r
+:101CD000A5A6A7A8A9AAABACADAEAFB0B1B2A3B44C\r
+:101CE000B5B6B7B8A5AABBBCBDBCAFC0C1C2C3C462\r
+:101CF000C5C6C7C8C9CACBCCCDCECFD0D1D2D3D41C\r
+:101D0000D5D6D7D8D9DADBDCDDDEDFC0C1C2C3C4AB\r
+:101D1000C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4FB\r
+:101D2000D5D6F7D8D9DADBDCDDDEFF2F6669726D38\r
+:101D3000776172652E62696E002F6669726D7761D8\r
+:101D400072652E62636B0000FF0F0000FF1F000032\r
+:101D5000FF2F0000FF3F0000FF4F0000FF5F00006B\r
+:101D6000FF6F0000FF7F0000FF8F0000FF9F00005B\r
+:101D7000FFAF0000FFBF0000FFCF0000FFDF00004B\r
+:101D8000FFEF0000FFFF0000FF7F0100FFFF0100E9\r
+:101D9000FF7F0200FFFF0200FF7F0300FFFF030041\r
+:101DA000FF7F0400FFFF0400FF7F0500FFFF050029\r
+:101DB000FF7F0600FFFF0600FF7F0700FFFF070011\r
+:101DC00000000000001000000020000000300000B3\r
+:101DD00000400000005000000060000000700000A3\r
+:101DE000008000000090000000A0000000B0000093\r
+:101DF00000C0000000D0000000E0000000F0000083\r
+:101E000000000100008001000000020000800200CC\r
+:101E100000000300008003000000040000800400B4\r
+:101E2000000005000080050000000600008006009C\r
+:101E3000000007000080070000000000581E00009E\r
+:0C1E4000000000100800000090060000E8\r
+:081E4C0078E2FF7F01000000B5\r
+:081E580000E1F50500000000A7\r
+:04000003000000C534\r
+:00000001FF\r
--- /dev/null
+<?xml version="1.0"?>
+<root>
+ <global/>
+ <data>
+ <created_ts>1325892529</created_ts>
+ <last_mod_ts>1325892530</last_mod_ts>
+ <is_archive>0</is_archive>
+ <is_published>1</is_published>
+ <is_svn>0</is_svn>
+ <is_module>1</is_module>
+ <is_lib>0</is_lib>
+ <url>http://mbed.org/users/NeoBelerophon/libraries/ChaNFS</url>
+ <url_full>http://mbed.org/users/NeoBelerophon/libraries/ChaNFS/llxkji</url_full>
+ <rev>llxkji</rev>
+ </data>
+</root>
--- /dev/null
+/*-----------------------------------------------------------------------*/\r
+/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */\r
+/*-----------------------------------------------------------------------*/\r
+/* This is a stub disk I/O module that acts as front end of the existing */\r
+/* disk I/O modules and attach it to FatFs module with common interface. */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+#include "diskio.h"\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include "FATFileSystem.h"\r
+\r
+#include "mbed.h"\r
+\r
+DSTATUS disk_initialize (\r
+ BYTE drv /* Physical drive nmuber (0..) */\r
+) \r
+{\r
+ FFSDEBUG("disk_initialize on drv [%d]\n", drv);\r
+ return (DSTATUS)FATFileSystem::_ffs[drv]->disk_initialize();\r
+}\r
+\r
+DSTATUS disk_status (\r
+ BYTE drv /* Physical drive nmuber (0..) */\r
+) \r
+{\r
+ FFSDEBUG("disk_status on drv [%d]\n", drv);\r
+ return (DSTATUS)FATFileSystem::_ffs[drv]->disk_status();\r
+}\r
+\r
+DRESULT disk_read (\r
+ BYTE drv, /* Physical drive nmuber (0..) */\r
+ BYTE *buff, /* Data buffer to store read data */\r
+ DWORD sector, /* Sector address (LBA) */\r
+ BYTE count /* Number of sectors to read (1..255) */\r
+)\r
+{\r
+ FFSDEBUG("disk_read(sector %d, count %d) on drv [%d]\n", sector, count, drv);\r
+ for(int s=sector; s<sector+count; s++) {\r
+ FFSDEBUG(" disk_read(sector %d)\n", s);\r
+ int res = FATFileSystem::_ffs[drv]->disk_read((char*)buff, s);\r
+ if(res) {\r
+ return RES_PARERR;\r
+ }\r
+ buff += 512;\r
+ }\r
+ return RES_OK;\r
+}\r
+\r
+#if _READONLY == 0\r
+DRESULT disk_write (\r
+ BYTE drv, /* Physical drive nmuber (0..) */\r
+ const BYTE *buff, /* Data to be written */\r
+ DWORD sector, /* Sector address (LBA) */\r
+ BYTE count /* Number of sectors to write (1..255) */\r
+)\r
+{\r
+ FFSDEBUG("disk_write(sector %d, count %d) on drv [%d]\n", sector, count, drv);\r
+ for(int s=sector; s<sector+count; s++) {\r
+ FFSDEBUG(" disk_write(sector %d)\n", s);\r
+ int res = FATFileSystem::_ffs[drv]->disk_write((char*)buff, sector);\r
+ if(res) {\r
+ return RES_PARERR;\r
+ }\r
+ buff += 512;\r
+ }\r
+ return RES_OK;\r
+}\r
+#endif /* _READONLY */\r
+\r
+DRESULT disk_ioctl (\r
+ BYTE drv, /* Physical drive nmuber (0..) */\r
+ BYTE ctrl, /* Control code */\r
+ void *buff /* Buffer to send/receive control data */\r
+)\r
+{\r
+ FFSDEBUG("disk_ioctl(%d)\n", ctrl);\r
+ switch(ctrl) {\r
+ case CTRL_SYNC:\r
+ if(FATFileSystem::_ffs[drv] == NULL) {\r
+ return RES_NOTRDY;\r
+ } else if(FATFileSystem::_ffs[drv]->disk_sync()) {\r
+ return RES_ERROR;\r
+ } \r
+ return RES_OK;\r
+ case GET_SECTOR_COUNT:\r
+ if(FATFileSystem::_ffs[drv] == NULL) {\r
+ return RES_NOTRDY;\r
+ } else {\r
+ int res = FATFileSystem::_ffs[drv]->disk_sectors();\r
+ if(res > 0) {\r
+ *((DWORD*)buff) = res; // minimum allowed\r
+ return RES_OK;\r
+ } else {\r
+ return RES_ERROR;\r
+ }\r
+ }\r
+ case GET_BLOCK_SIZE:\r
+ *((DWORD*)buff) = 1; // default when not known\r
+ return RES_OK;\r
+\r
+ }\r
+ return RES_PARERR;\r
+}\r
+\r
--- /dev/null
+/*-----------------------------------------------------------------------\r
+/ Low level disk interface modlue include file\r
+/-----------------------------------------------------------------------*/\r
+\r
+#ifndef _DISKIO\r
+\r
+#define _READONLY 0 /* 1: Remove write functions */\r
+#define _USE_IOCTL 1 /* 1: Use disk_ioctl fucntion */\r
+\r
+#include "integer.h"\r
+\r
+\r
+/* Status of Disk Functions */\r
+typedef BYTE DSTATUS;\r
+\r
+/* Results of Disk Functions */\r
+typedef enum {\r
+ RES_OK = 0, /* 0: Successful */\r
+ RES_ERROR, /* 1: R/W Error */\r
+ RES_WRPRT, /* 2: Write Protected */\r
+ RES_NOTRDY, /* 3: Not Ready */\r
+ RES_PARERR /* 4: Invalid Parameter */\r
+} DRESULT;\r
+\r
+\r
+/*---------------------------------------*/\r
+/* Prototypes for disk control functions */\r
+\r
+int assign_drives (int, int);\r
+DSTATUS disk_initialize (BYTE);\r
+DSTATUS disk_status (BYTE);\r
+DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);\r
+#if _READONLY == 0\r
+DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);\r
+#endif\r
+DRESULT disk_ioctl (BYTE, BYTE, void*);\r
+void disk_timerproc (void);\r
+\r
+\r
+\r
+\r
+/* Disk Status Bits (DSTATUS) */\r
+\r
+#define STA_NOINIT 0x01 /* Drive not initialized */\r
+#define STA_NODISK 0x02 /* No medium in the drive */\r
+#define STA_PROTECT 0x04 /* Write protected */\r
+\r
+\r
+/* Command code for disk_ioctrl fucntion */\r
+\r
+/* Generic command (defined for FatFs) */\r
+#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */\r
+#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */\r
+#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */\r
+#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */\r
+#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */\r
+\r
+/* Generic command */\r
+#define CTRL_POWER 5 /* Get/Set power status */\r
+#define CTRL_LOCK 6 /* Lock/Unlock media removal */\r
+#define CTRL_EJECT 7 /* Eject media */\r
+\r
+/* MMC/SDC specific ioctl command */\r
+#define MMC_GET_TYPE 10 /* Get card type */\r
+#define MMC_GET_CSD 11 /* Get CSD */\r
+#define MMC_GET_CID 12 /* Get CID */\r
+#define MMC_GET_OCR 13 /* Get OCR */\r
+#define MMC_GET_SDSTAT 14 /* Get SD status */\r
+\r
+/* ATA/CF specific ioctl command */\r
+#define ATA_GET_REV 20 /* Get F/W revision */\r
+#define ATA_GET_MODEL 21 /* Get model name */\r
+#define ATA_GET_SN 22 /* Get serial number */\r
+\r
+/* NAND specific ioctl command */\r
+#define NAND_FORMAT 30 /* Create physical format */\r
+\r
+\r
+#define _DISKIO\r
+#endif\r
--- /dev/null
+/*----------------------------------------------------------------------------/\r
+/ FatFs - FAT file system module R0.08b (C)ChaN, 2011\r
+/-----------------------------------------------------------------------------/\r
+/ FatFs module is a generic FAT file system module for small embedded systems.\r
+/ This is a free software that opened for education, research and commercial\r
+/ developments under license policy of following terms.\r
+/\r
+/ Copyright (C) 2011, ChaN, all right reserved.\r
+/\r
+/ * The FatFs module is a free software and there is NO WARRANTY.\r
+/ * No restriction on use. You can use, modify and redistribute it for\r
+/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.\r
+/ * Redistributions of source code must retain the above copyright notice.\r
+/\r
+/-----------------------------------------------------------------------------/\r
+/ Feb 26,'06 R0.00 Prototype.\r
+/\r
+/ Apr 29,'06 R0.01 First stable version.\r
+/\r
+/ Jun 01,'06 R0.02 Added FAT12 support.\r
+/ Removed unbuffered mode.\r
+/ Fixed a problem on small (<32M) partition.\r
+/ Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).\r
+/\r
+/ Sep 22,'06 R0.03 Added f_rename().\r
+/ Changed option _FS_MINIMUM to _FS_MINIMIZE.\r
+/ Dec 11,'06 R0.03a Improved cluster scan algorithm to write files fast.\r
+/ Fixed f_mkdir() creates incorrect directory on FAT32.\r
+/\r
+/ Feb 04,'07 R0.04 Supported multiple drive system.\r
+/ Changed some interfaces for multiple drive system.\r
+/ Changed f_mountdrv() to f_mount().\r
+/ Added f_mkfs().\r
+/ Apr 01,'07 R0.04a Supported multiple partitions on a physical drive.\r
+/ Added a capability of extending file size to f_lseek().\r
+/ Added minimization level 3.\r
+/ Fixed an endian sensitive code in f_mkfs().\r
+/ May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.\r
+/ Added FSInfo support.\r
+/ Fixed DBCS name can result FR_INVALID_NAME.\r
+/ Fixed short seek (<= csize) collapses the file object.\r
+/\r
+/ Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().\r
+/ Fixed f_mkfs() on FAT32 creates incorrect FSInfo.\r
+/ Fixed f_mkdir() on FAT32 creates incorrect directory.\r
+/ Feb 03,'08 R0.05a Added f_truncate() and f_utime().\r
+/ Fixed off by one error at FAT sub-type determination.\r
+/ Fixed btr in f_read() can be mistruncated.\r
+/ Fixed cached sector is not flushed when create and close without write.\r
+/\r
+/ Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().\r
+/ Improved performance of f_lseek() on moving to the same or following cluster.\r
+/\r
+/ Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)\r
+/ Added long file name feature.\r
+/ Added multiple code page feature.\r
+/ Added re-entrancy for multitask operation.\r
+/ Added auto cluster size selection to f_mkfs().\r
+/ Added rewind option to f_readdir().\r
+/ Changed result code of critical errors.\r
+/ Renamed string functions to avoid name collision.\r
+/ Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.\r
+/ Added multiple sector size feature.\r
+/ Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.\r
+/ Fixed wrong cache control in f_lseek().\r
+/ Added relative path feature.\r
+/ Added f_chdir() and f_chdrive().\r
+/ Added proper case conversion to extended char.\r
+/ Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.\r
+/ Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.\r
+/ Fixed name matching error on the 13 char boundary.\r
+/ Added a configuration option, _LFN_UNICODE.\r
+/ Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.\r
+/\r
+/ May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)\r
+/ Added file lock feature. (_FS_SHARE)\r
+/ Added fast seek feature. (_USE_FASTSEEK)\r
+/ Changed some types on the API, XCHAR->TCHAR.\r
+/ Changed fname member in the FILINFO structure on Unicode cfg.\r
+/ String functions support UTF-8 encoding files on Unicode cfg.\r
+/ Aug 16,'10 R0.08a Added f_getcwd(). (_FS_RPATH = 2)\r
+/ Added sector erase feature. (_USE_ERASE)\r
+/ Moved file lock semaphore table from fs object to the bss.\r
+/ Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.\r
+/ Fixed f_mkfs() creates wrong FAT32 volume.\r
+/ Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().\r
+/ f_lseek() reports required table size on creating CLMP.\r
+/ Extended format syntax of f_printf function.\r
+/ Ignores duplicated directory separators in given path names.\r
+/---------------------------------------------------------------------------*/\r
+\r
+#include "ff.h" /* FatFs configurations and declarations */\r
+#include "diskio.h" /* Declarations of low level disk I/O functions */\r
+#include "mbed.h"\r
+\r
+\r
+/*--------------------------------------------------------------------------\r
+\r
+ Module Private Definitions\r
+\r
+---------------------------------------------------------------------------*/\r
+\r
+#if _FATFS != 8237\r
+#error Wrong include file (ff.h).\r
+#endif\r
+\r
+\r
+/* Definitions on sector size */\r
+#if _MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096\r
+#error Wrong sector size.\r
+#endif\r
+#if _MAX_SS != 512\r
+#define SS(fs) ((fs)->ssize) /* Multiple sector size */\r
+#else\r
+#define SS(fs) 512U /* Fixed sector size */\r
+#endif\r
+\r
+\r
+/* Reentrancy related */\r
+#if _FS_REENTRANT\r
+#if _USE_LFN == 1\r
+#error Static LFN work area must not be used in re-entrant configuration.\r
+#endif\r
+#define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }\r
+#define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }\r
+#else\r
+#define ENTER_FF(fs)\r
+#define LEAVE_FF(fs, res) return res\r
+#endif\r
+\r
+#define ABORT(fs, res) { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); }\r
+\r
+\r
+/* File shareing feature */\r
+#if _FS_SHARE\r
+#if _FS_READONLY\r
+#error _FS_SHARE must be 0 on read-only cfg.\r
+#endif\r
+typedef struct {\r
+ FATFS *fs; /* File ID 1, volume (NULL:blank entry) */\r
+ DWORD clu; /* File ID 2, directory */\r
+ WORD idx; /* File ID 3, directory index */\r
+ WORD ctr; /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */\r
+} FILESEM;\r
+#endif\r
+\r
+\r
+/* Misc definitions */\r
+#define LD_CLUST(dir) (((DWORD)LD_WORD(dir+DIR_FstClusHI)<<16) | LD_WORD(dir+DIR_FstClusLO))\r
+#define ST_CLUST(dir,cl) {ST_WORD(dir+DIR_FstClusLO, cl); ST_WORD(dir+DIR_FstClusHI, (DWORD)cl>>16);}\r
+\r
+\r
+/* DBCS code ranges and SBCS extend char conversion table */\r
+\r
+#if _CODE_PAGE == 932 /* Japanese Shift-JIS */\r
+#define _DF1S 0x81 /* DBC 1st byte range 1 start */\r
+#define _DF1E 0x9F /* DBC 1st byte range 1 end */\r
+#define _DF2S 0xE0 /* DBC 1st byte range 2 start */\r
+#define _DF2E 0xFC /* DBC 1st byte range 2 end */\r
+#define _DS1S 0x40 /* DBC 2nd byte range 1 start */\r
+#define _DS1E 0x7E /* DBC 2nd byte range 1 end */\r
+#define _DS2S 0x80 /* DBC 2nd byte range 2 start */\r
+#define _DS2E 0xFC /* DBC 2nd byte range 2 end */\r
+\r
+#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */\r
+#define _DF1S 0x81\r
+#define _DF1E 0xFE\r
+#define _DS1S 0x40\r
+#define _DS1E 0x7E\r
+#define _DS2S 0x80\r
+#define _DS2E 0xFE\r
+\r
+#elif _CODE_PAGE == 949 /* Korean */\r
+#define _DF1S 0x81\r
+#define _DF1E 0xFE\r
+#define _DS1S 0x41\r
+#define _DS1E 0x5A\r
+#define _DS2S 0x61\r
+#define _DS2E 0x7A\r
+#define _DS3S 0x81\r
+#define _DS3E 0xFE\r
+\r
+#elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */\r
+#define _DF1S 0x81\r
+#define _DF1E 0xFE\r
+#define _DS1S 0x40\r
+#define _DS1E 0x7E\r
+#define _DS2S 0xA1\r
+#define _DS2E 0xFE\r
+\r
+#elif _CODE_PAGE == 437 /* U.S. (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 720 /* Arabic (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x49,0x49,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 737 /* Greek (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \\r
+ 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xE7,0xE8,0xF1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 775 /* Baltic (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 852 /* Latin 2 (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F,0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0x9F, \\r
+ 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 855 /* Cyrillic (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F,0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \\r
+ 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \\r
+ 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 857 /* Turkish (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x98,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \\r
+ 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0x59,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 862 /* Hebrew (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 866 /* Russian (OEM) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 1250 /* Central Europe (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xA3,0xB4,0xB5,0xB6,0xB7,0xB8,0xA5,0xAA,0xBB,0xBC,0xBD,0xBC,0xAF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}\r
+\r
+#elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x82,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x80,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \\r
+ 0xA0,0xA2,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB2,0xA5,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xA3,0xBD,0xBD,0xAF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF}\r
+\r
+#elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \\r
+ 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}\r
+\r
+#elif _CODE_PAGE == 1253 /* Greek (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \\r
+ 0xE0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xFB,0xBC,0xFD,0xBF,0xFF}\r
+\r
+#elif _CODE_PAGE == 1254 /* Turkish (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}\r
+\r
+#elif _CODE_PAGE == 1255 /* Hebrew (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 1256 /* Arabic (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF}\r
+\r
+#elif _CODE_PAGE == 1257 /* Baltic (Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \\r
+ 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}\r
+\r
+#elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */\r
+#define _DF1S 0\r
+#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \\r
+ 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \\r
+ 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F}\r
+\r
+#elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */\r
+#if _USE_LFN\r
+#error Cannot use LFN feature without valid code page.\r
+#endif\r
+#define _DF1S 0\r
+\r
+#else\r
+#error Unknown code page\r
+\r
+#endif\r
+\r
+\r
+/* Character code support macros */\r
+#define IsUpper(c) (((c)>='A')&&((c)<='Z'))\r
+#define IsLower(c) (((c)>='a')&&((c)<='z'))\r
+#define IsDigit(c) (((c)>='0')&&((c)<='9'))\r
+\r
+#if _DF1S /* Code page is DBCS */\r
+\r
+#ifdef _DF2S /* Two 1st byte areas */\r
+#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))\r
+#else /* One 1st byte area */\r
+#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)\r
+#endif\r
+\r
+#ifdef _DS3S /* Three 2nd byte areas */\r
+#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))\r
+#else /* Two 2nd byte areas */\r
+#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))\r
+#endif\r
+\r
+#else /* Code page is SBCS */\r
+\r
+#define IsDBCS1(c) 0\r
+#define IsDBCS2(c) 0\r
+\r
+#endif /* _DF1S */\r
+\r
+\r
+/* Name status flags */\r
+#define NS 11 /* Index of name status byte in fn[] */\r
+#define NS_LOSS 0x01 /* Out of 8.3 format */\r
+#define NS_LFN 0x02 /* Force to create LFN entry */\r
+#define NS_LAST 0x04 /* Last segment */\r
+#define NS_BODY 0x08 /* Lower case flag (body) */\r
+#define NS_EXT 0x10 /* Lower case flag (ext) */\r
+#define NS_DOT 0x20 /* Dot entry */\r
+\r
+\r
+/* FAT sub-type boundaries */\r
+/* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */\r
+#define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */\r
+#define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */\r
+\r
+\r
+/* FatFs refers the members in the FAT structures as byte array instead of\r
+/ structure member because the structure is not binary compatible between\r
+/ different platforms */\r
+\r
+#define BS_jmpBoot 0 /* Jump instruction (3) */\r
+#define BS_OEMName 3 /* OEM name (8) */\r
+#define BPB_BytsPerSec 11 /* Sector size [byte] (2) */\r
+#define BPB_SecPerClus 13 /* Cluster size [sector] (1) */\r
+#define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */\r
+#define BPB_NumFATs 16 /* Number of FAT copies (1) */\r
+#define BPB_RootEntCnt 17 /* Number of root dir entries for FAT12/16 (2) */\r
+#define BPB_TotSec16 19 /* Volume size [sector] (2) */\r
+#define BPB_Media 21 /* Media descriptor (1) */\r
+#define BPB_FATSz16 22 /* FAT size [sector] (2) */\r
+#define BPB_SecPerTrk 24 /* Track size [sector] (2) */\r
+#define BPB_NumHeads 26 /* Number of heads (2) */\r
+#define BPB_HiddSec 28 /* Number of special hidden sectors (4) */\r
+#define BPB_TotSec32 32 /* Volume size [sector] (4) */\r
+#define BS_DrvNum 36 /* Physical drive number (2) */\r
+#define BS_BootSig 38 /* Extended boot signature (1) */\r
+#define BS_VolID 39 /* Volume serial number (4) */\r
+#define BS_VolLab 43 /* Volume label (8) */\r
+#define BS_FilSysType 54 /* File system type (1) */\r
+#define BPB_FATSz32 36 /* FAT size [sector] (4) */\r
+#define BPB_ExtFlags 40 /* Extended flags (2) */\r
+#define BPB_FSVer 42 /* File system version (2) */\r
+#define BPB_RootClus 44 /* Root dir first cluster (4) */\r
+#define BPB_FSInfo 48 /* Offset of FSInfo sector (2) */\r
+#define BPB_BkBootSec 50 /* Offset of backup boot sectot (2) */\r
+#define BS_DrvNum32 64 /* Physical drive number (2) */\r
+#define BS_BootSig32 66 /* Extended boot signature (1) */\r
+#define BS_VolID32 67 /* Volume serial number (4) */\r
+#define BS_VolLab32 71 /* Volume label (8) */\r
+#define BS_FilSysType32 82 /* File system type (1) */\r
+#define FSI_LeadSig 0 /* FSI: Leading signature (4) */\r
+#define FSI_StrucSig 484 /* FSI: Structure signature (4) */\r
+#define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */\r
+#define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */\r
+#define MBR_Table 446 /* MBR: Partition table offset (2) */\r
+#define SZ_PTE 16 /* MBR: Size of a partition table entry */\r
+#define BS_55AA 510 /* Boot sector signature (2) */\r
+\r
+#define DIR_Name 0 /* Short file name (11) */\r
+#define DIR_Attr 11 /* Attribute (1) */\r
+#define DIR_NTres 12 /* NT flag (1) */\r
+#define DIR_CrtTime 14 /* Created time (2) */\r
+#define DIR_CrtDate 16 /* Created date (2) */\r
+#define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */\r
+#define DIR_WrtTime 22 /* Modified time (2) */\r
+#define DIR_WrtDate 24 /* Modified date (2) */\r
+#define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */\r
+#define DIR_FileSize 28 /* File size (4) */\r
+#define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */\r
+#define LDIR_Attr 11 /* LFN attribute (1) */\r
+#define LDIR_Type 12 /* LFN type (1) */\r
+#define LDIR_Chksum 13 /* Sum of corresponding SFN entry */\r
+#define LDIR_FstClusLO 26 /* Filled by zero (0) */\r
+#define SZ_DIR 32 /* Size of a directory entry */\r
+#define LLE 0x40 /* Last long entry flag in LDIR_Ord */\r
+#define DDE 0xE5 /* Deleted directory enrty mark in DIR_Name[0] */\r
+#define NDDE 0x05 /* Replacement of a character collides with DDE */\r
+\r
+\r
+/*------------------------------------------------------------*/\r
+/* Work area */\r
+\r
+#if _VOLUMES\r
+static\r
+FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */\r
+#else\r
+#error Number of drives must not be 0.\r
+#endif\r
+\r
+static\r
+WORD Fsid; /* File system mount ID */\r
+\r
+#if _FS_RPATH\r
+static\r
+BYTE CurrVol; /* Current drive */\r
+#endif\r
+\r
+#if _FS_SHARE\r
+static\r
+FILESEM Files[_FS_SHARE]; /* File lock semaphores */\r
+#endif\r
+\r
+#if _USE_LFN == 0 /* No LFN */\r
+#define DEF_NAMEBUF BYTE sfn[12]\r
+#define INIT_BUF(dobj) (dobj).fn = sfn\r
+#define FREE_BUF()\r
+\r
+#elif _USE_LFN == 1 /* LFN with static LFN working buffer */\r
+static WCHAR LfnBuf[_MAX_LFN+1];\r
+#define DEF_NAMEBUF BYTE sfn[12]\r
+#define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }\r
+#define FREE_BUF()\r
+\r
+#elif _USE_LFN == 2 /* LFN with dynamic LFN working buffer on the stack */\r
+#define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]\r
+#define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }\r
+#define FREE_BUF()\r
+\r
+#elif _USE_LFN == 3 /* LFN with dynamic LFN working buffer on the heap */\r
+#define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn\r
+#define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \\r
+ if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \\r
+ (dobj).lfn = lfn; (dobj).fn = sfn; }\r
+#define FREE_BUF() ff_memfree(lfn)\r
+\r
+#else\r
+#error Wrong LFN configuration.\r
+#endif\r
+\r
+\r
+\r
+\r
+/*--------------------------------------------------------------------------\r
+\r
+ Module Private Functions\r
+\r
+---------------------------------------------------------------------------*/\r
+\r
+//static FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */\r
+//static WORD fsid; /* File system mount ID */\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* String functions */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+/* Copy memory to memory */\r
+static\r
+void mem_cpy (void* dst, const void* src, UINT cnt) {\r
+ BYTE *d = (BYTE*)dst;\r
+ const BYTE *s = (const BYTE*)src;\r
+\r
+#if _WORD_ACCESS == 1\r
+ while (cnt >= sizeof(int)) {\r
+ *(int*)d = *(int*)s;\r
+ d += sizeof(int); s += sizeof(int);\r
+ cnt -= sizeof(int);\r
+ }\r
+#endif\r
+ while (cnt--)\r
+ *d++ = *s++;\r
+}\r
+\r
+/* Fill memory */\r
+static\r
+void mem_set (void* dst, int val, UINT cnt) {\r
+ BYTE *d = (BYTE*)dst;\r
+\r
+ while (cnt--)\r
+ *d++ = (BYTE)val;\r
+}\r
+\r
+/* Compare memory to memory */\r
+static\r
+int mem_cmp (const void* dst, const void* src, UINT cnt) {\r
+ const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;\r
+ int r = 0;\r
+\r
+ while (cnt-- && (r = *d++ - *s++) == 0) ;\r
+ return r;\r
+}\r
+\r
+/* Check if chr is contained in the string */\r
+static\r
+int chk_chr (const char* str, int chr) {\r
+ while (*str && *str != chr) str++;\r
+ return *str;\r
+}\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Request/Release grant to access the volume */\r
+/*-----------------------------------------------------------------------*/\r
+#if _FS_REENTRANT\r
+\r
+static\r
+int lock_fs (\r
+ FATFS *fs /* File system object */\r
+)\r
+{\r
+ return ff_req_grant(fs->sobj);\r
+}\r
+\r
+\r
+static\r
+void unlock_fs (\r
+ FATFS *fs, /* File system object */\r
+ FRESULT res /* Result code to be returned */\r
+)\r
+{\r
+ if (res != FR_NOT_ENABLED &&\r
+ res != FR_INVALID_DRIVE &&\r
+ res != FR_INVALID_OBJECT &&\r
+ res != FR_TIMEOUT) {\r
+ ff_rel_grant(fs->sobj);\r
+ }\r
+}\r
+#endif\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* File shareing control functions */\r
+/*-----------------------------------------------------------------------*/\r
+#if _FS_SHARE\r
+\r
+static\r
+FRESULT chk_lock ( /* Check if the file can be accessed */\r
+ DIR* dj, /* Directory object pointing the file to be checked */\r
+ int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */\r
+)\r
+{\r
+ UINT i, be;\r
+\r
+ /* Search file semaphore table */\r
+ for (i = be = 0; i < _FS_SHARE; i++) {\r
+ if (Files[i].fs) { /* Existing entry */\r
+ if (Files[i].fs == dj->fs && /* Check if the file matched with an open file */\r
+ Files[i].clu == dj->sclust &&\r
+ Files[i].idx == dj->index) break;\r
+ } else { /* Blank entry */\r
+ be++;\r
+ }\r
+ }\r
+ if (i == _FS_SHARE) /* The file is not opened */\r
+ return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */\r
+\r
+ /* The file has been opened. Reject any open against writing file and all write mode open */\r
+ return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;\r
+}\r
+\r
+\r
+static\r
+int enq_lock ( /* Check if an entry is available for a new file */\r
+ FATFS* fs /* File system object */\r
+)\r
+{\r
+ UINT i;\r
+\r
+ for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;\r
+ return (i == _FS_SHARE) ? 0 : 1;\r
+}\r
+\r
+\r
+static\r
+UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */\r
+ DIR* dj, /* Directory object pointing the file to register or increment */\r
+ int acc /* Desired access mode (0:Read, !0:Write) */\r
+)\r
+{\r
+ UINT i;\r
+\r
+\r
+ for (i = 0; i < _FS_SHARE; i++) { /* Find the file */\r
+ if (Files[i].fs == dj->fs &&\r
+ Files[i].clu == dj->sclust &&\r
+ Files[i].idx == dj->index) break;\r
+ }\r
+\r
+ if (i == _FS_SHARE) { /* Not opened. Register it as new. */\r
+ for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;\r
+ if (i == _FS_SHARE) return 0; /* No space to register (int err) */\r
+ Files[i].fs = dj->fs;\r
+ Files[i].clu = dj->sclust;\r
+ Files[i].idx = dj->index;\r
+ Files[i].ctr = 0;\r
+ }\r
+\r
+ if (acc && Files[i].ctr) return 0; /* Access violation (int err) */\r
+\r
+ Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */\r
+\r
+ return i + 1;\r
+}\r
+\r
+\r
+static\r
+FRESULT dec_lock ( /* Decrement file open counter */\r
+ UINT i /* Semaphore index */\r
+)\r
+{\r
+ WORD n;\r
+ FRESULT res;\r
+\r
+\r
+ if (--i < _FS_SHARE) {\r
+ n = Files[i].ctr;\r
+ if (n == 0x100) n = 0;\r
+ if (n) n--;\r
+ Files[i].ctr = n;\r
+ if (!n) Files[i].fs = 0;\r
+ res = FR_OK;\r
+ } else {\r
+ res = FR_INT_ERR;\r
+ }\r
+ return res;\r
+}\r
+\r
+\r
+static\r
+void clear_lock ( /* Clear lock entries of the volume */\r
+ FATFS *fs\r
+)\r
+{\r
+ UINT i;\r
+\r
+ for (i = 0; i < _FS_SHARE; i++) {\r
+ if (Files[i].fs == fs) Files[i].fs = 0;\r
+ }\r
+}\r
+#endif\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Change window offset */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT move_window (\r
+ FATFS *fs, /* File system object */\r
+ DWORD sector /* Sector number to make appearance in the fs->win[] */\r
+) /* Move to zero only writes back dirty window */\r
+{\r
+ DWORD wsect;\r
+\r
+\r
+ wsect = fs->winsect;\r
+ if (wsect != sector) { /* Changed current window */\r
+#if !_FS_READONLY\r
+ if (fs->wflag) { /* Write back dirty window if needed */\r
+ if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK)\r
+ return FR_DISK_ERR;\r
+ fs->wflag = 0;\r
+ if (wsect < (fs->fatbase + fs->fsize)) { /* In FAT area */\r
+ BYTE nf;\r
+ for (nf = fs->n_fats; nf > 1; nf--) { /* Reflect the change to all FAT copies */\r
+ wsect += fs->fsize;\r
+ disk_write(fs->drv, fs->win, wsect, 1);\r
+ }\r
+ }\r
+ }\r
+#endif\r
+ if (sector) {\r
+ if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK)\r
+ return FR_DISK_ERR;\r
+ fs->winsect = sector;\r
+ }\r
+ }\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Clean-up cached data */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY\r
+static\r
+FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */\r
+ FATFS *fs /* File system object */\r
+)\r
+{\r
+ FRESULT res;\r
+\r
+\r
+ res = move_window(fs, 0);\r
+ if (res == FR_OK) {\r
+ /* Update FSInfo sector if needed */\r
+ if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {\r
+ fs->winsect = 0;\r
+ /* Create FSInfo structure */\r
+ mem_set(fs->win, 0, 512);\r
+ ST_WORD(fs->win+BS_55AA, 0xAA55);\r
+ ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);\r
+ ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);\r
+ ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);\r
+ ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);\r
+ /* Write it into the FSInfo sector */\r
+ disk_write(fs->drv, fs->win, fs->fsi_sector, 1);\r
+ fs->fsi_flag = 0;\r
+ }\r
+ /* Make sure that no pending write process in the physical drive */\r
+ if (disk_ioctl(fs->drv, CTRL_SYNC, (void*)0) != RES_OK)\r
+ res = FR_DISK_ERR;\r
+ }\r
+\r
+ return res;\r
+}\r
+#endif\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Get sector# from cluster# */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+\r
+DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */\r
+ FATFS *fs, /* File system object */\r
+ DWORD clst /* Cluster# to be converted */\r
+)\r
+{\r
+ clst -= 2;\r
+ if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */\r
+ return clst * fs->csize + fs->database;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* FAT access - Read value of a FAT entry */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+\r
+DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */\r
+ FATFS *fs, /* File system object */\r
+ DWORD clst /* Cluster# to get the link information */\r
+)\r
+{\r
+ UINT wc, bc;\r
+ BYTE *p;\r
+\r
+\r
+ if (clst < 2 || clst >= fs->n_fatent) /* Chack range */\r
+ return 1;\r
+\r
+ switch (fs->fs_type) {\r
+ case FS_FAT12 :\r
+ bc = (UINT)clst; bc += bc / 2;\r
+ if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;\r
+ wc = fs->win[bc % SS(fs)]; bc++;\r
+ if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;\r
+ wc |= fs->win[bc % SS(fs)] << 8;\r
+ return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);\r
+\r
+ case FS_FAT16 :\r
+ if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;\r
+ p = &fs->win[clst * 2 % SS(fs)];\r
+ return LD_WORD(p);\r
+\r
+ case FS_FAT32 :\r
+ if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;\r
+ p = &fs->win[clst * 4 % SS(fs)];\r
+ return LD_DWORD(p) & 0x0FFFFFFF;\r
+ }\r
+\r
+ return 0xFFFFFFFF; /* An error occurred at the disk I/O layer */\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* FAT access - Change value of a FAT entry */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY\r
+\r
+FRESULT put_fat (\r
+ FATFS *fs, /* File system object */\r
+ DWORD clst, /* Cluster# to be changed in range of 2 to fs->n_fatent - 1 */\r
+ DWORD val /* New value to mark the cluster */\r
+)\r
+{\r
+ UINT bc;\r
+ BYTE *p;\r
+ FRESULT res;\r
+\r
+\r
+ if (clst < 2 || clst >= fs->n_fatent) { /* Check range */\r
+ res = FR_INT_ERR;\r
+\r
+ } else {\r
+ switch (fs->fs_type) {\r
+ case FS_FAT12 :\r
+ bc = clst; bc += bc / 2;\r
+ res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
+ if (res != FR_OK) break;\r
+ p = &fs->win[bc % SS(fs)];\r
+ *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;\r
+ bc++;\r
+ fs->wflag = 1;\r
+ res = move_window(fs, fs->fatbase + (bc / SS(fs)));\r
+ if (res != FR_OK) break;\r
+ p = &fs->win[bc % SS(fs)];\r
+ *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));\r
+ break;\r
+\r
+ case FS_FAT16 :\r
+ res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));\r
+ if (res != FR_OK) break;\r
+ p = &fs->win[clst * 2 % SS(fs)];\r
+ ST_WORD(p, (WORD)val);\r
+ break;\r
+\r
+ case FS_FAT32 :\r
+ res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));\r
+ if (res != FR_OK) break;\r
+ p = &fs->win[clst * 4 % SS(fs)];\r
+ val |= LD_DWORD(p) & 0xF0000000;\r
+ ST_DWORD(p, val);\r
+ break;\r
+\r
+ default :\r
+ res = FR_INT_ERR;\r
+ }\r
+ fs->wflag = 1;\r
+ }\r
+\r
+ return res;\r
+}\r
+#endif /* !_FS_READONLY */\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* FAT handling - Remove a cluster chain */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY\r
+static\r
+FRESULT remove_chain (\r
+ FATFS *fs, /* File system object */\r
+ DWORD clst /* Cluster# to remove a chain from */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD nxt;\r
+#if _USE_ERASE\r
+ DWORD scl = clst, ecl = clst, resion[2];\r
+#endif\r
+\r
+ if (clst < 2 || clst >= fs->n_fatent) { /* Check range */\r
+ res = FR_INT_ERR;\r
+\r
+ } else {\r
+ res = FR_OK;\r
+ while (clst < fs->n_fatent) { /* Not a last link? */\r
+ nxt = get_fat(fs, clst); /* Get cluster status */\r
+ if (nxt == 0) break; /* Empty cluster? */\r
+ if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */\r
+ if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */\r
+ res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */\r
+ if (res != FR_OK) break;\r
+ if (fs->free_clust != 0xFFFFFFFF) { /* Update FSInfo */\r
+ fs->free_clust++;\r
+ fs->fsi_flag = 1;\r
+ }\r
+#if _USE_ERASE\r
+ if (ecl + 1 == nxt) { /* Next cluster is contiguous */\r
+ ecl = nxt;\r
+ } else { /* End of contiguous clusters */ \r
+ resion[0] = clust2sect(fs, scl); /* Start sector */\r
+ resion[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */\r
+ disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, resion); /* Erase the block */\r
+ scl = ecl = nxt;\r
+ }\r
+#endif\r
+ clst = nxt; /* Next cluster */\r
+ }\r
+ }\r
+\r
+ return res;\r
+}\r
+#endif\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* FAT handling - Stretch or Create a cluster chain */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY\r
+static\r
+DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */\r
+ FATFS *fs, /* File system object */\r
+ DWORD clst /* Cluster# to stretch. 0 means create a new chain. */\r
+)\r
+{\r
+ DWORD cs, ncl, scl;\r
+ FRESULT res;\r
+\r
+\r
+ if (clst == 0) { /* Create a new chain */\r
+ scl = fs->last_clust; /* Get suggested start point */\r
+ if (!scl || scl >= fs->n_fatent) scl = 1;\r
+ }\r
+ else { /* Stretch the current chain */\r
+ cs = get_fat(fs, clst); /* Check the cluster status */\r
+ if (cs < 2) return 1; /* It is an invalid cluster */\r
+ if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */\r
+ scl = clst;\r
+ }\r
+\r
+ ncl = scl; /* Start cluster */\r
+ for (;;) {\r
+ ncl++; /* Next cluster */\r
+ if (ncl >= fs->n_fatent) { /* Wrap around */\r
+ ncl = 2;\r
+ if (ncl > scl) return 0; /* No free cluster */\r
+ }\r
+ cs = get_fat(fs, ncl); /* Get the cluster status */\r
+ if (cs == 0) break; /* Found a free cluster */\r
+ if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */\r
+ return cs;\r
+ if (ncl == scl) return 0; /* No free cluster */\r
+ }\r
+\r
+ res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */\r
+ if (res == FR_OK && clst != 0) {\r
+ res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */\r
+ }\r
+ if (res == FR_OK) {\r
+ fs->last_clust = ncl; /* Update FSINFO */\r
+ if (fs->free_clust != 0xFFFFFFFF) {\r
+ fs->free_clust--;\r
+ fs->fsi_flag = 1;\r
+ }\r
+ } else {\r
+ ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;\r
+ }\r
+\r
+ return ncl; /* Return new cluster number or error code */\r
+}\r
+#endif /* !_FS_READONLY */\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* FAT handling - Convert offset into cluster with link map table */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+#if _USE_FASTSEEK\r
+static\r
+DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */\r
+ FIL* fp, /* Pointer to the file object */\r
+ DWORD ofs /* File offset to be converted to cluster# */\r
+)\r
+{\r
+ DWORD cl, ncl, *tbl;\r
+\r
+\r
+ tbl = fp->cltbl + 1; /* Top of CLMT */\r
+ cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */\r
+ for (;;) {\r
+ ncl = *tbl++; /* Number of cluters in the fragment */\r
+ if (!ncl) return 0; /* End of table? (error) */\r
+ if (cl < ncl) break; /* In this fragment? */\r
+ cl -= ncl; tbl++; /* Next fragment */\r
+ }\r
+ return cl + *tbl; /* Return the cluster number */\r
+}\r
+#endif /* _USE_FASTSEEK */\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Directory handling - Set directory index */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT dir_sdi (\r
+ DIR_t *dj, /* Pointer to directory object */\r
+ WORD idx /* Directory index number */\r
+)\r
+{\r
+ DWORD clst;\r
+ WORD ic;\r
+\r
+\r
+ dj->index = idx;\r
+ clst = dj->sclust;\r
+ if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */\r
+ return FR_INT_ERR;\r
+ if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */\r
+ clst = dj->fs->dirbase;\r
+\r
+ if (clst == 0) { /* Static table (root-dir in FAT12/16) */\r
+ dj->clust = clst;\r
+ if (idx >= dj->fs->n_rootdir) /* Index is out of range */\r
+ return FR_INT_ERR;\r
+ dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */\r
+ }\r
+ else { /* Dynamic table (sub-dirs or root-dir in FAT32) */\r
+ ic = SS(dj->fs) / SZ_DIR * dj->fs->csize; /* Entries per cluster */\r
+ while (idx >= ic) { /* Follow cluster chain */\r
+ clst = get_fat(dj->fs, clst); /* Get next cluster */\r
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */\r
+ if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */\r
+ return FR_INT_ERR;\r
+ idx -= ic;\r
+ }\r
+ dj->clust = clst;\r
+ dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */\r
+ }\r
+\r
+ dj->dir = dj->fs->win + (idx % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */\r
+\r
+ return FR_OK; /* Seek succeeded */\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Directory handling - Move directory index next */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */\r
+ DIR_t *dj, /* Pointer to directory object */\r
+ int stretch /* 0: Do not stretch table, 1: Stretch table if needed */\r
+)\r
+{\r
+ DWORD clst;\r
+ WORD i;\r
+\r
+\r
+ i = dj->index + 1;\r
+ if (!i || !dj->sect) /* Report EOT when index has reached 65535 */\r
+ return FR_NO_FILE;\r
+\r
+ if (!(i % (SS(dj->fs) / SZ_DIR))) { /* Sector changed? */\r
+ dj->sect++; /* Next sector */\r
+\r
+ if (dj->clust == 0) { /* Static table */\r
+ if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */\r
+ return FR_NO_FILE;\r
+ }\r
+ else { /* Dynamic table */\r
+ if (((i / (SS(dj->fs) / SZ_DIR)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */\r
+ clst = get_fat(dj->fs, dj->clust); /* Get next cluster */\r
+ if (clst <= 1) return FR_INT_ERR;\r
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR;\r
+ if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */\r
+#if !_FS_READONLY\r
+ BYTE c;\r
+ if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */\r
+ clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */\r
+ if (clst == 0) return FR_DENIED; /* No free cluster */\r
+ if (clst == 1) return FR_INT_ERR;\r
+ if (clst == 0xFFFFFFFF) return FR_DISK_ERR;\r
+ /* Clean-up stretched table */\r
+ if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */\r
+ mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */\r
+ dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */\r
+ for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */\r
+ dj->fs->wflag = 1;\r
+ if (move_window(dj->fs, 0)) return FR_DISK_ERR;\r
+ dj->fs->winsect++;\r
+ }\r
+ dj->fs->winsect -= c; /* Rewind window address */\r
+#else\r
+ return FR_NO_FILE; /* Report EOT */\r
+#endif\r
+ }\r
+ dj->clust = clst; /* Initialize data for new cluster */\r
+ dj->sect = clust2sect(dj->fs, clst);\r
+ }\r
+ }\r
+ }\r
+\r
+ dj->index = i;\r
+ dj->dir = dj->fs->win + (i % (SS(dj->fs) / SZ_DIR)) * SZ_DIR;\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */\r
+/*-----------------------------------------------------------------------*/\r
+#if _USE_LFN\r
+static\r
+const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */\r
+\r
+\r
+static\r
+int cmp_lfn ( /* 1:Matched, 0:Not matched */\r
+ WCHAR *lfnbuf, /* Pointer to the LFN to be compared */\r
+ BYTE *dir /* Pointer to the directory entry containing a part of LFN */\r
+)\r
+{\r
+ UINT i, s;\r
+ WCHAR wc, uc;\r
+\r
+\r
+ i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */\r
+ s = 0; wc = 1;\r
+ do {\r
+ uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */\r
+ if (wc) { /* Last char has not been processed */\r
+ wc = ff_wtoupper(uc); /* Convert it to upper case */\r
+ if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */\r
+ return 0; /* Not matched */\r
+ } else {\r
+ if (uc != 0xFFFF) return 0; /* Check filler */\r
+ }\r
+ } while (++s < 13); /* Repeat until all chars in the entry are checked */\r
+\r
+ if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */\r
+ return 0;\r
+\r
+ return 1; /* The part of LFN matched */\r
+ }\r
+\r
+\r
+\r
+static\r
+int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */\r
+ WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */\r
+ BYTE *dir /* Pointer to the directory entry */\r
+)\r
+{\r
+ UINT i, s;\r
+ WCHAR wc, uc;\r
+\r
+\r
+ i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */\r
+\r
+ s = 0; wc = 1;\r
+ do {\r
+ uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */\r
+ if (wc) { /* Last char has not been processed */\r
+ if (i >= _MAX_LFN) return 0; /* Buffer overflow? */\r
+ lfnbuf[i++] = wc = uc; /* Store it */\r
+ } else {\r
+ if (uc != 0xFFFF) return 0; /* Check filler */\r
+ }\r
+ } while (++s < 13); /* Read all character in the entry */\r
+\r
+ if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */\r
+ if (i >= _MAX_LFN) return 0; /* Buffer overflow? */\r
+ lfnbuf[i] = 0;\r
+}\r
+\r
+ return 1;\r
+}\r
+\r
+\r
+#if !_FS_READONLY\r
+static\r
+void fit_lfn (\r
+ const WCHAR *lfnbuf, /* Pointer to the LFN buffer */\r
+ BYTE *dir, /* Pointer to the directory entry */\r
+ BYTE ord, /* LFN order (1-20) */\r
+ BYTE sum /* SFN sum */\r
+)\r
+{\r
+ UINT i, s;\r
+ WCHAR wc;\r
+\r
+\r
+ dir[LDIR_Chksum] = sum; /* Set check sum */\r
+ dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */\r
+ dir[LDIR_Type] = 0;\r
+ ST_WORD(dir+LDIR_FstClusLO, 0);\r
+\r
+ i = (ord - 1) * 13; /* Get offset in the LFN buffer */\r
+ s = wc = 0;\r
+ do {\r
+ if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */\r
+ ST_WORD(dir+LfnOfs[s], wc); /* Put it */\r
+ if (!wc) wc = 0xFFFF; /* Padding chars following last char */\r
+ } while (++s < 13);\r
+ if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */\r
+ dir[LDIR_Ord] = ord; /* Set the LFN order */\r
+}\r
+\r
+#endif\r
+#endif\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Create numbered name */\r
+/*-----------------------------------------------------------------------*/\r
+#if _USE_LFN\r
+void gen_numname (\r
+ BYTE *dst, /* Pointer to generated SFN */\r
+ const BYTE *src, /* Pointer to source SFN to be modified */\r
+ const WCHAR *lfn, /* Pointer to LFN */\r
+ WORD seq /* Sequence number */\r
+)\r
+{\r
+ BYTE ns[8], c;\r
+ UINT i, j;\r
+\r
+\r
+ mem_cpy(dst, src, 11);\r
+\r
+ if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */\r
+ do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn);\r
+ }\r
+\r
+ /* itoa (hexdecimal) */\r
+ i = 7;\r
+ do {\r
+ c = (seq % 16) + '0';\r
+ if (c > '9') c += 7;\r
+ ns[i--] = c;\r
+ seq /= 16;\r
+ } while (seq);\r
+ ns[i] = '~';\r
+\r
+ /* Append the number */\r
+ for (j = 0; j < i && dst[j] != ' '; j++) {\r
+ if (IsDBCS1(dst[j])) {\r
+ if (j == i - 1) break;\r
+ j++;\r
+ }\r
+ }\r
+ do {\r
+ dst[j++] = (i < 8) ? ns[i++] : ' ';\r
+ } while (j < 8);\r
+}\r
+#endif\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Calculate sum of an SFN */\r
+/*-----------------------------------------------------------------------*/\r
+#if _USE_LFN\r
+static\r
+BYTE sum_sfn (\r
+ const BYTE *dir /* Ptr to directory entry */\r
+)\r
+{\r
+ BYTE sum = 0;\r
+ UINT n = 11;\r
+\r
+ do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);\r
+ return sum;\r
+}\r
+#endif\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Directory handling - Find an object in the directory */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT dir_find (\r
+ DIR_t *dj /* Pointer to the directory object linked to the file name */\r
+)\r
+{\r
+ FRESULT res;\r
+ BYTE c, *dir;\r
+#if _USE_LFN\r
+ BYTE a, ord, sum;\r
+#endif\r
+\r
+ res = dir_sdi(dj, 0); /* Rewind directory object */\r
+ if (res != FR_OK) return res;\r
+\r
+#if _USE_LFN\r
+ ord = sum = 0xFF;\r
+#endif\r
+ do {\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ dir = dj->dir; /* Ptr to the directory entry of current index */\r
+ c = dir[DIR_Name];\r
+ if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */\r
+#if _USE_LFN /* LFN configuration */\r
+ a = dir[DIR_Attr] & AM_MASK;\r
+ if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */\r
+ ord = 0xFF;\r
+ } else {\r
+ if (a == AM_LFN) { /* An LFN entry is found */\r
+ if (dj->lfn) {\r
+ if (c & LLE) { /* Is it start of LFN sequence? */\r
+ sum = dir[LDIR_Chksum];\r
+ c &= ~LLE; ord = c; /* LFN start order */\r
+ dj->lfn_idx = dj->index;\r
+ }\r
+ /* Check validity of the LFN entry and compare it with given name */\r
+ ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;\r
+ }\r
+ } else { /* An SFN entry is found */\r
+ if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */\r
+ ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */\r
+ if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */\r
+ }\r
+ }\r
+#else /* Non LFN configuration */\r
+ if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */\r
+ break;\r
+#endif\r
+ res = dir_next(dj, 0); /* Next entry */\r
+ } while (res == FR_OK);\r
+\r
+ return res;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Read an object from the directory */\r
+/*-----------------------------------------------------------------------*/\r
+#if _FS_MINIMIZE <= 1\r
+static\r
+FRESULT dir_read (\r
+ DIR_t *dj /* Pointer to the directory object that pointing the entry to be read */\r
+)\r
+{\r
+ FRESULT res;\r
+ BYTE c, *dir;\r
+#if _USE_LFN\r
+ BYTE a, ord = 0xFF, sum = 0xFF;\r
+#endif\r
+\r
+ res = FR_NO_FILE;\r
+ while (dj->sect) {\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ dir = dj->dir; /* Ptr to the directory entry of current index */\r
+ c = dir[DIR_Name];\r
+ if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */\r
+#if _USE_LFN /* LFN configuration */\r
+ a = dir[DIR_Attr] & AM_MASK;\r
+ if (c == DDE || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */\r
+ ord = 0xFF;\r
+ } else {\r
+ if (a == AM_LFN) { /* An LFN entry is found */\r
+ if (c & LLE) { /* Is it start of LFN sequence? */\r
+ sum = dir[LDIR_Chksum];\r
+ c &= ~LLE; ord = c;\r
+ dj->lfn_idx = dj->index;\r
+ }\r
+ /* Check LFN validity and capture it */\r
+ ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;\r
+ } else { /* An SFN entry is found */\r
+ if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */\r
+ dj->lfn_idx = 0xFFFF; /* It has no LFN. */\r
+ break;\r
+ }\r
+ }\r
+#else /* Non LFN configuration */\r
+ if (c != DDE && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */\r
+ break;\r
+#endif\r
+ res = dir_next(dj, 0); /* Next entry */\r
+ if (res != FR_OK) break;\r
+ }\r
+\r
+ if (res != FR_OK) dj->sect = 0;\r
+\r
+ return res;\r
+ }\r
+#endif\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Register an object to the directory */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY\r
+static\r
+FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */\r
+ DIR_t *dj /* Target directory with object name to be created */\r
+)\r
+{\r
+ FRESULT res;\r
+ BYTE c, *dir;\r
+#if _USE_LFN /* LFN configuration */\r
+ WORD n, ne, is;\r
+ BYTE sn[12], *fn, sum;\r
+ WCHAR *lfn;\r
+\r
+\r
+ fn = dj->fn; lfn = dj->lfn;\r
+ mem_cpy(sn, fn, 12);\r
+\r
+ if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */\r
+ return FR_INVALID_NAME;\r
+\r
+ if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */\r
+ fn[NS] = 0; dj->lfn = 0; /* Find only SFN */\r
+ for (n = 1; n < 100; n++) {\r
+ gen_numname(fn, sn, lfn, n); /* Generate a numbered name */\r
+ res = dir_find(dj); /* Check if the name collides with existing SFN */\r
+ if (res != FR_OK) break;\r
+ }\r
+ if (n == 100) return FR_DENIED; /* Abort if too many collisions */\r
+ if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */\r
+ fn[NS] = sn[NS]; dj->lfn = lfn;\r
+}\r
+\r
+ if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */\r
+ for (ne = 0; lfn[ne]; ne++) ;\r
+ ne = (ne + 25) / 13;\r
+ } else { /* Otherwise reserve only an SFN entry. */\r
+ ne = 1;\r
+ }\r
+\r
+ /* Reserve contiguous entries */\r
+ res = dir_sdi(dj, 0);\r
+ if (res != FR_OK) return res;\r
+ n = is = 0;\r
+ do {\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ c = *dj->dir; /* Check the entry status */\r
+ if (c == DDE || c == 0) { /* Is it a blank entry? */\r
+ if (n == 0) is = dj->index; /* First index of the contiguous entry */\r
+ if (++n == ne) break; /* A contiguous entry that required count is found */\r
+ } else {\r
+ n = 0; /* Not a blank entry. Restart to search */\r
+ }\r
+ res = dir_next(dj, 1); /* Next entry with table stretch */\r
+ } while (res == FR_OK);\r
+\r
+ if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */\r
+ res = dir_sdi(dj, is);\r
+ if (res == FR_OK) {\r
+ sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */\r
+ ne--;\r
+ do { /* Store LFN entries in bottom first */\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum);\r
+ dj->fs->wflag = 1;\r
+ res = dir_next(dj, 0); /* Next entry */\r
+ } while (res == FR_OK && --ne);\r
+ }\r
+ }\r
+\r
+#else /* Non LFN configuration */\r
+ res = dir_sdi(dj, 0);\r
+ if (res == FR_OK) {\r
+ do { /* Find a blank entry for the SFN */\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ c = *dj->dir;\r
+ if (c == DDE || c == 0) break; /* Is it a blank entry? */\r
+ res = dir_next(dj, 1); /* Next entry with table stretch */\r
+ } while (res == FR_OK);\r
+ }\r
+#endif\r
+\r
+ if (res == FR_OK) { /* Initialize the SFN entry */\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res == FR_OK) {\r
+ dir = dj->dir;\r
+ mem_set(dir, 0, SZ_DIR); /* Clean the entry */\r
+ mem_cpy(dir, dj->fn, 11); /* Put SFN */\r
+#if _USE_LFN\r
+ dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */\r
+#endif\r
+ dj->fs->wflag = 1;\r
+ }\r
+ }\r
+\r
+ return res;\r
+}\r
+#endif /* !_FS_READONLY */\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Remove an object from the directory */\r
+/*-----------------------------------------------------------------------*/\r
+#if !_FS_READONLY && !_FS_MINIMIZE\r
+static\r
+FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */\r
+ DIR_t *dj /* Directory object pointing the entry to be removed */\r
+)\r
+{\r
+ FRESULT res;\r
+#if _USE_LFN /* LFN configuration */\r
+ WORD i;\r
+\r
+ i = dj->index; /* SFN index */\r
+ res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */\r
+ if (res == FR_OK) {\r
+ do {\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res != FR_OK) break;\r
+ *dj->dir = DDE; /* Mark the entry "deleted" */\r
+ dj->fs->wflag = 1;\r
+ if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */\r
+ res = dir_next(dj, 0); /* Next entry */\r
+ } while (res == FR_OK);\r
+ if (res == FR_NO_FILE) res = FR_INT_ERR;\r
+ }\r
+\r
+#else /* Non LFN configuration */\r
+ res = dir_sdi(dj, dj->index);\r
+ if (res == FR_OK) {\r
+ res = move_window(dj->fs, dj->sect);\r
+ if (res == FR_OK) {\r
+ *dj->dir = DDE; /* Mark the entry "deleted" */\r
+ dj->fs->wflag = 1;\r
+ }\r
+ }\r
+#endif\r
+\r
+ return res;\r
+}\r
+#endif /* !_FS_READONLY */\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Pick a segment and create the object name in directory form */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT create_name (\r
+ DIR_t *dj, /* Pointer to the directory object */\r
+ const TCHAR **path /* Pointer to pointer to the segment in the path string */\r
+)\r
+{\r
+#ifdef _EXCVT\r
+ static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */\r
+#endif\r
+\r
+#if _USE_LFN /* LFN configuration */\r
+ BYTE b, cf;\r
+ WCHAR w, *lfn;\r
+ UINT i, ni, si, di;\r
+ const TCHAR *p;\r
+\r
+ /* Create LFN in Unicode */\r
+ for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */\r
+ lfn = dj->lfn;\r
+ si = di = 0;\r
+ for (;;) {\r
+ w = p[si++]; /* Get a character */\r
+ if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */\r
+ if (di >= _MAX_LFN) /* Reject too long name */\r
+ return FR_INVALID_NAME;\r
+#if !_LFN_UNICODE\r
+ w &= 0xFF;\r
+ if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */\r
+ b = (BYTE)p[si++]; /* Get 2nd byte */\r
+ if (!IsDBCS2(b))\r
+ return FR_INVALID_NAME; /* Reject invalid sequence */\r
+ w = (w << 8) + b; /* Create a DBC */\r
+ }\r
+ w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */\r
+ if (!w) return FR_INVALID_NAME; /* Reject invalid code */\r
+#endif\r
+ if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */\r
+ return FR_INVALID_NAME;\r
+ lfn[di++] = w; /* Store the Unicode char */\r
+ }\r
+ *path = &p[si]; /* Return pointer to the next segment */\r
+ cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */\r
+#if _FS_RPATH\r
+ if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */\r
+ (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {\r
+ lfn[di] = 0;\r
+ for (i = 0; i < 11; i++)\r
+ dj->fn[i] = (i < di) ? '.' : ' ';\r
+ dj->fn[i] = cf | NS_DOT; /* This is a dot entry */\r
+ return FR_OK;\r
+ }\r
+#endif\r
+ while (di) { /* Strip trailing spaces and dots */\r
+ w = lfn[di-1];\r
+ if (w != ' ' && w != '.') break;\r
+ di--;\r
+ }\r
+ if (!di) return FR_INVALID_NAME; /* Reject nul string */\r
+\r
+ lfn[di] = 0; /* LFN is created */\r
+\r
+ /* Create SFN in directory form */\r
+ mem_set(dj->fn, ' ', 11);\r
+ for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */\r
+ if (si) cf |= NS_LOSS | NS_LFN;\r
+ while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */\r
+\r
+ b = i = 0; ni = 8;\r
+ for (;;) {\r
+ w = lfn[si++]; /* Get an LFN char */\r
+ if (!w) break; /* Break on end of the LFN */\r
+ if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */\r
+ cf |= NS_LOSS | NS_LFN; continue;\r
+ }\r
+\r
+ if (i >= ni || si == di) { /* Extension or end of SFN */\r
+ if (ni == 11) { /* Long extension */\r
+ cf |= NS_LOSS | NS_LFN; break;\r
+ }\r
+ if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */\r
+ if (si > di) break; /* No extension */\r
+ si = di; i = 8; ni = 11; /* Enter extension section */\r
+ b <<= 2; continue;\r
+ }\r
+\r
+ if (w >= 0x80) { /* Non ASCII char */\r
+#ifdef _EXCVT\r
+ w = ff_convert(w, 0); /* Unicode -> OEM code */\r
+ if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */\r
+#else\r
+ w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */\r
+#endif\r
+ cf |= NS_LFN; /* Force create LFN entry */\r
+ }\r
+\r
+ if (_DF1S && w >= 0x100) { /* Double byte char (always false on SBCS cfg) */\r
+ if (i >= ni - 1) {\r
+ cf |= NS_LOSS | NS_LFN; i = ni; continue;\r
+ }\r
+ dj->fn[i++] = (BYTE)(w >> 8);\r
+ } else { /* Single byte char */\r
+ if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */\r
+ w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */\r
+ } else {\r
+ if (IsUpper(w)) { /* ASCII large capital */\r
+ b |= 2;\r
+ } else {\r
+ if (IsLower(w)) { /* ASCII small capital */\r
+ b |= 1; w -= 0x20;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ dj->fn[i++] = (BYTE)w;\r
+ }\r
+\r
+ if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first char collides with deleted mark, replace it with 0x05 */\r
+\r
+ if (ni == 8) b <<= 2;\r
+ if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */\r
+ cf |= NS_LFN;\r
+ if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */\r
+ if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */\r
+ if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */\r
+ }\r
+\r
+ dj->fn[NS] = cf; /* SFN is created */\r
+\r
+ return FR_OK;\r
+\r
+\r
+#else /* Non-LFN configuration */\r
+ BYTE b, c, d, *sfn;\r
+ UINT ni, si, i;\r
+ const char *p;\r
+\r
+ /* Create file name in directory form */\r
+ for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */\r
+ sfn = dj->fn;\r
+ mem_set(sfn, ' ', 11);\r
+ si = i = b = 0; ni = 8;\r
+#if _FS_RPATH\r
+ if (p[si] == '.') { /* Is this a dot entry? */\r
+ for (;;) {\r
+ c = (BYTE)p[si++];\r
+ if (c != '.' || si >= 3) break;\r
+ sfn[i++] = c;\r
+ }\r
+ if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;\r
+ *path = &p[si]; /* Return pointer to the next segment */\r
+ sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */\r
+ return FR_OK;\r
+ }\r
+#endif\r
+ for (;;) {\r
+ c = (BYTE)p[si++];\r
+ if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */\r
+ if (c == '.' || i >= ni) {\r
+ if (ni != 8 || c != '.') return FR_INVALID_NAME;\r
+ i = 8; ni = 11;\r
+ b <<= 2; continue;\r
+ }\r
+ if (c >= 0x80) { /* Extended char? */\r
+ b |= 3; /* Eliminate NT flag */\r
+#ifdef _EXCVT\r
+ c = excvt[c-0x80]; /* Upper conversion (SBCS) */\r
+#else\r
+#if !_DF1S /* ASCII only cfg */\r
+ return FR_INVALID_NAME;\r
+#endif\r
+#endif\r
+ }\r
+ if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */\r
+ d = (BYTE)p[si++]; /* Get 2nd byte */\r
+ if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */\r
+ return FR_INVALID_NAME;\r
+ sfn[i++] = c;\r
+ sfn[i++] = d;\r
+ } else { /* Single byte code */\r
+ if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */\r
+ return FR_INVALID_NAME;\r
+ if (IsUpper(c)) { /* ASCII large capital? */\r
+ b |= 2;\r
+ } else {\r
+ if (IsLower(c)) { /* ASCII small capital? */\r
+ b |= 1; c -= 0x20;\r
+ }\r
+ }\r
+ sfn[i++] = c;\r
+ }\r
+ }\r
+ *path = &p[si]; /* Return pointer to the next segment */\r
+ c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */\r
+\r
+ if (!i) return FR_INVALID_NAME; /* Reject nul string */\r
+ if (sfn[0] == DDE) sfn[0] = NDDE; /* When first char collides with DDE, replace it with 0x05 */\r
+\r
+ if (ni == 8) b <<= 2;\r
+ if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */\r
+ if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */\r
+\r
+ sfn[NS] = c; /* Store NT flag, File name is created */\r
+\r
+ return FR_OK;\r
+#endif\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Get file information from directory entry */\r
+/*-----------------------------------------------------------------------*/\r
+#if _FS_MINIMIZE <= 1\r
+static\r
+void get_fileinfo ( /* No return code */\r
+ DIR_t *dj, /* Pointer to the directory object */\r
+ FILINFO *fno /* Pointer to the file information to be filled */\r
+)\r
+{\r
+ UINT i;\r
+ BYTE nt, *dir;\r
+ TCHAR *p, c;\r
+ p = fno->fname;\r
+ \r
+ if (dj->sect) {\r
+ dir = dj->dir;\r
+ nt = dir[DIR_NTres]; /* NT flag */\r
+ for (i = 0; i < 8; i++) { /* Copy name body */\r
+ c = dir[i];\r
+ if (c == ' ') break;\r
+ if (c == NDDE) c = (TCHAR)DDE;\r
+ if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20;\r
+#if _LFN_UNICODE\r
+ if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i+1]))\r
+ c = (c << 8) | dir[++i];\r
+ c = ff_convert(c, 1);\r
+ if (!c) c = '?';\r
+#endif\r
+ *p++ = c;\r
+ }\r
+ if (dir[8] != ' ') { /* Copy name extension */\r
+ *p++ = '.';\r
+ for (i = 8; i < 11; i++) {\r
+ c = dir[i];\r
+ if (c == ' ') break;\r
+ if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20;\r
+#if _LFN_UNICODE\r
+ if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i+1]))\r
+ c = (c << 8) | dir[++i];\r
+ c = ff_convert(c, 1);\r
+ if (!c) c = '?';\r
+#endif\r
+ *p++ = c;\r
+ }\r
+ }\r
+ fno->fattrib = dir[DIR_Attr]; /* Attribute */\r
+ fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */\r
+ fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */\r
+ fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */\r
+ }\r
+ *p = 0; /* Terminate SFN str by a \0 */\r
+\r
+#if _USE_LFN\r
+ if (fno->lfname && fno->lfsize) {\r
+ TCHAR *tp = fno->lfname;\r
+ WCHAR w, *lfn;\r
+\r
+ i = 0;\r
+ if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */\r
+ lfn = dj->lfn;\r
+ while ((w = *lfn++) != 0) { /* Get an LFN char */\r
+#if !_LFN_UNICODE\r
+ w = ff_convert(w, 0); /* Unicode -> OEM conversion */\r
+ if (!w) { i = 0; break; } /* Could not convert, no LFN */\r
+ if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */\r
+ tp[i++] = (TCHAR)(w >> 8);\r
+#endif\r
+ if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */\r
+ tp[i++] = (TCHAR)w;\r
+ }\r
+\r
+ }\r
+ tp[i] = 0; /* Terminate the LFN str by a \0 */\r
+ }\r
+#endif\r
+}\r
+#endif /* _FS_MINIMIZE <= 1 */\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Follow a file path */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */\r
+ DIR_t *dj, /* Directory object to return last directory and found object */\r
+ const TCHAR *path /* Full-path string to find a file or directory */\r
+)\r
+{\r
+ FRESULT res;\r
+ BYTE *dir, ns;\r
+\r
+\r
+#if _FS_RPATH\r
+ if (*path == '/' || *path == '\\') { /* There is a heading separator */\r
+ path++; dj->sclust = 0; /* Strip it and start from the root dir */\r
+ } else { /* No heading separator */\r
+ dj->sclust = dj->fs->cdir; /* Start from the current dir */\r
+ }\r
+#else\r
+ if (*path == '/' || *path == '\\') /* Strip heading separator if exist */\r
+ path++;\r
+ dj->sclust = 0; /* Start from the root dir */\r
+#endif\r
+\r
+ if ((UINT)*path < ' ') { /* Nul path means the start directory itself */\r
+ res = dir_sdi(dj, 0);\r
+ dj->dir = 0;\r
+\r
+ } else { /* Follow path */\r
+ for (;;) {\r
+ res = create_name(dj, &path); /* Get a segment */\r
+ if (res != FR_OK) break;\r
+ res = dir_find(dj); /* Find it */\r
+ ns = *(dj->fn+NS);\r
+ if (res != FR_OK) { /* Failed to find the object */\r
+ if (res != FR_NO_FILE) break; /* Abort if any hard error occured */\r
+ /* Object not found */\r
+ if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */\r
+ dj->sclust = 0; dj->dir = 0; /* It is the root dir */\r
+ res = FR_OK;\r
+ if (!(ns & NS_LAST)) continue;\r
+ } else { /* Could not find the object */\r
+ if (!(ns & NS_LAST)) res = FR_NO_PATH;\r
+ }\r
+ break;\r
+ }\r
+ if (ns & NS_LAST) break; /* Last segment match. Function completed. */\r
+ dir = dj->dir; /* There is next segment. Follow the sub directory */\r
+ if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */\r
+ res = FR_NO_PATH; break;\r
+ }\r
+ dj->sclust = LD_CLUST(dir);\r
+ }\r
+ }\r
+\r
+ return res;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Load boot record and check if it is an FAT boot record */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+BYTE check_fs ( /* 0:The FAT BR, 1:Valid BR but not an FAT, 2:Not a BR, 3:Disk error */\r
+ FATFS *fs, /* File system object */\r
+ DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */\r
+)\r
+{\r
+ if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */\r
+ return 3;\r
+ if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */\r
+ return 2;\r
+\r
+ if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */\r
+ return 0;\r
+ if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)\r
+ return 0;\r
+\r
+ return 1;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Check if the file system object is valid or not */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */\r
+ const TCHAR **path, /* Pointer to pointer to the path name (drive number) */\r
+ FATFS **rfs, /* Pointer to pointer to the found file system object */\r
+ BYTE chk_wp /* !=0: Check media write protection for write access */\r
+)\r
+{\r
+ BYTE fmt, b, *tbl;\r
+ UINT vol;\r
+ DSTATUS stat;\r
+ DWORD bsect, fasize, tsect, sysect, nclst, szbfat;\r
+ WORD nrsv;\r
+ const TCHAR *p = *path;\r
+ FATFS *fs;\r
+\r
+ /* Get logical drive number from the path name */\r
+ vol = p[0] - '0'; /* Is there a drive number? */\r
+ if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */\r
+ p += 2; *path = p; /* Return pointer to the path name */\r
+ } else { /* No drive number is given */\r
+#if _FS_RPATH\r
+ vol = CurrVol; /* Use current drive */\r
+#else\r
+ vol = 0; /* Use drive 0 */\r
+#endif\r
+ }\r
+\r
+ /* Check if the logical drive is valid or not */\r
+ if (vol >= _VOLUMES) /* Is the drive number valid? */\r
+ return FR_INVALID_DRIVE;\r
+ *rfs = fs = FatFs[vol]; /* Return pointer to the corresponding file system object */\r
+ if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */\r
+\r
+ ENTER_FF(fs); /* Lock file system */\r
+\r
+ if (fs->fs_type) { /* If the logical drive has been mounted */\r
+ stat = disk_status(fs->drv);\r
+ if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */\r
+#if !_FS_READONLY\r
+ if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */\r
+ return FR_WRITE_PROTECTED;\r
+#endif\r
+ return FR_OK; /* The file system object is valid */\r
+ }\r
+ }\r
+\r
+ /* The logical drive must be mounted. */\r
+ /* Following code attempts to mount a volume. (analyze BPB and initialize the fs object) */\r
+\r
+ fs->fs_type = 0; /* Clear the file system object */\r
+ fs->drv = (BYTE)LD2PD(vol); /* Bind the logical drive and a physical drive */\r
+ stat = disk_initialize(fs->drv); /* Initialize low level disk I/O layer */\r
+ if (stat & STA_NOINIT) /* Check if the initialization succeeded */\r
+ return FR_NOT_READY; /* Failed to initialize due to no media or hard error */\r
+#if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */\r
+ if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK)\r
+ return FR_DISK_ERR;\r
+#endif\r
+#if !_FS_READONLY\r
+ if (chk_wp && (stat & STA_PROTECT)) /* Check disk write protection if needed */\r
+ return FR_WRITE_PROTECTED;\r
+#endif\r
+ /* Search FAT partition on the drive. Supports only generic partitionings, FDISK and SFD. */\r
+ fmt = check_fs(fs, bsect = 0); /* Check sector 0 if it is a VBR */\r
+ if (fmt == 1) { /* Not an FAT-VBR, the disk may be partitioned */\r
+ /* Check the partition listed in top of the partition table */\r
+ tbl = &fs->win[MBR_Table + LD2PT(vol) * SZ_PTE];/* Partition table */\r
+ if (tbl[4]) { /* Is the partition existing? */\r
+ bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */\r
+ fmt = check_fs(fs, bsect); /* Check the partition */\r
+ }\r
+ }\r
+ if (fmt == 3) return FR_DISK_ERR;\r
+ if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */\r
+\r
+ /* Following code initializes the file system object */\r
+\r
+ if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */\r
+ return FR_NO_FILESYSTEM;\r
+\r
+ fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */\r
+ if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);\r
+ fs->fsize = fasize;\r
+\r
+ fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */\r
+ if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */\r
+ fasize *= b; /* Number of sectors for FAT area */\r
+\r
+ fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */\r
+ if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */\r
+\r
+ fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */\r
+ if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */\r
+\r
+ tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */\r
+ if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);\r
+\r
+ nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */\r
+ if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */\r
+\r
+ /* Determine the FAT sub type */\r
+ sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */\r
+ if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
+ nclst = (tsect - sysect) / fs->csize; /* Number of clusters */\r
+ if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */\r
+ fmt = FS_FAT12;\r
+ if (nclst >= MIN_FAT16) fmt = FS_FAT16;\r
+ if (nclst >= MIN_FAT32) fmt = FS_FAT32;\r
+\r
+ /* Boundaries and Limits */\r
+ fs->n_fatent = nclst + 2; /* Number of FAT entries */\r
+ fs->database = bsect + sysect; /* Data start sector */\r
+ fs->fatbase = bsect + nrsv; /* FAT start sector */\r
+ if (fmt == FS_FAT32) {\r
+ if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */\r
+ fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */\r
+ szbfat = fs->n_fatent * 4; /* (Required FAT size) */\r
+ } else {\r
+ if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */\r
+ fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */\r
+ szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */\r
+ fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);\r
+ }\r
+ if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */\r
+ return FR_NO_FILESYSTEM;\r
+\r
+#if !_FS_READONLY\r
+ /* Initialize cluster allocation information */\r
+ fs->free_clust = 0xFFFFFFFF;\r
+ fs->last_clust = 0;\r
+\r
+ /* Get fsinfo if available */\r
+ if (fmt == FS_FAT32) {\r
+ fs->fsi_flag = 0;\r
+ fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo);\r
+ if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK &&\r
+ LD_WORD(fs->win+BS_55AA) == 0xAA55 &&\r
+ LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 &&\r
+ LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) {\r
+ fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);\r
+ fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);\r
+ }\r
+ }\r
+#endif\r
+ fs->fs_type = fmt; /* FAT sub-type */\r
+ fs->id = ++Fsid; /* File system mount ID */\r
+ fs->winsect = 0; /* Invalidate sector cache */\r
+ fs->wflag = 0;\r
+#if _FS_RPATH\r
+ fs->cdir = 0; /* Current directory (root dir) */\r
+#endif\r
+#if _FS_SHARE /* Clear file lock semaphores */\r
+ clear_lock(fs);\r
+#endif\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Check if the file/dir object is valid or not */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+static\r
+FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */\r
+ FATFS *fs, /* Pointer to the file system object */\r
+ WORD id /* Member id of the target object to be checked */\r
+)\r
+{\r
+ if (!fs || !fs->fs_type || fs->id != id)\r
+ return FR_INVALID_OBJECT;\r
+\r
+ ENTER_FF(fs); /* Lock file system */\r
+\r
+ if (disk_status(fs->drv) & STA_NOINIT)\r
+ return FR_NOT_READY;\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+\r
+/*--------------------------------------------------------------------------\r
+\r
+ Public Functions\r
+\r
+--------------------------------------------------------------------------*/\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Mount/Unmount a Logical Drive */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_mount (\r
+ BYTE vol, /* Logical drive number to be mounted/unmounted */\r
+ FATFS *fs /* Pointer to new file system object (NULL for unmount)*/\r
+)\r
+{\r
+ FATFS *rfs;\r
+\r
+\r
+ if (vol >= _VOLUMES) /* Check if the drive number is valid */\r
+ return FR_INVALID_DRIVE;\r
+ rfs = FatFs[vol]; /* Get current fs object */\r
+\r
+ if (rfs) {\r
+#if _FS_SHARE\r
+ clear_lock(rfs);\r
+#endif\r
+#if _FS_REENTRANT /* Discard sync object of the current volume */\r
+ if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;\r
+#endif\r
+ rfs->fs_type = 0; /* Clear old fs object */\r
+ }\r
+\r
+ if (fs) {\r
+ fs->fs_type = 0; /* Clear new fs object */\r
+#if _FS_REENTRANT /* Create sync object for the new volume */\r
+ if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;\r
+#endif\r
+ }\r
+ FatFs[vol] = fs; /* Register new fs object */\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Open or Create a File */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_open (\r
+ FIL_t *fp, /* Pointer to the blank file object */\r
+ const TCHAR *path, /* Pointer to the file name */\r
+ BYTE mode /* Access mode and file open mode flags */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ BYTE *dir;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ fp->fs = 0; /* Clear file object */\r
+\r
+#if !_FS_READONLY\r
+ mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;\r
+ res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));\r
+#else\r
+ mode &= FA_READ;\r
+ res = chk_mounted(&path, &dj.fs, 0);\r
+#endif\r
+ INIT_BUF(dj);\r
+ if (res == FR_OK)\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ dir = dj.dir;\r
+\r
+#if !_FS_READONLY /* R/W configuration */\r
+ if (res == FR_OK) {\r
+ if (!dir) /* Current dir itself */\r
+ res = FR_INVALID_NAME;\r
+#if _FS_SHARE\r
+ else\r
+ res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);\r
+#endif\r
+ }\r
+ /* Create or Open a file */\r
+ if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {\r
+ DWORD dw, cl;\r
+\r
+ if (res != FR_OK) { /* No file, create new */\r
+ if (res == FR_NO_FILE) /* There is no file to open, create a new entry */\r
+#if _FS_SHARE\r
+ res = enq_lock(dj.fs) ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;\r
+#else\r
+ res = dir_register(&dj);\r
+#endif\r
+ mode |= FA_CREATE_ALWAYS; /* File is created */\r
+ dir = dj.dir; /* New entry */\r
+ }\r
+ else { /* Any object is already existing */\r
+ if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */\r
+ res = FR_DENIED;\r
+ } else {\r
+ if (mode & FA_CREATE_NEW) /* Cannot create as new file */\r
+ res = FR_EXIST;\r
+ }\r
+ }\r
+ if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */\r
+ dw = get_fattime(); /* Created time */\r
+ ST_DWORD(dir+DIR_CrtTime, dw);\r
+ dir[DIR_Attr] = 0; /* Reset attribute */\r
+ ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */\r
+ cl = LD_CLUST(dir); /* Get start cluster */\r
+ ST_CLUST(dir, 0); /* cluster = 0 */\r
+ dj.fs->wflag = 1;\r
+ if (cl) { /* Remove the cluster chain if exist */\r
+ dw = dj.fs->winsect;\r
+ res = remove_chain(dj.fs, cl);\r
+ if (res == FR_OK) {\r
+ dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */\r
+ res = move_window(dj.fs, dw);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else { /* Open an existing file */\r
+ if (res == FR_OK) { /* Follow succeeded */\r
+ if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */\r
+ res = FR_NO_FILE;\r
+ } else {\r
+ if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */\r
+ res = FR_DENIED;\r
+ }\r
+ }\r
+ }\r
+ if (res == FR_OK) {\r
+ if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */\r
+ mode |= FA__WRITTEN;\r
+ fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */\r
+ fp->dir_ptr = dir;\r
+#if _FS_SHARE\r
+ fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);\r
+ if (!fp->lockid) res = FR_INT_ERR;\r
+#endif\r
+ }\r
+\r
+#else /* R/O configuration */\r
+ if (res == FR_OK) { /* Follow succeeded */\r
+ if (!dir) { /* Current dir itself */\r
+ res = FR_INVALID_NAME;\r
+ } else {\r
+ if (dir[DIR_Attr] & AM_DIR) /* It is a directory */\r
+ res = FR_NO_FILE;\r
+ }\r
+ }\r
+#endif\r
+ FREE_BUF();\r
+\r
+ if (res == FR_OK) {\r
+ fp->flag = mode; /* File access mode */\r
+ fp->sclust = LD_CLUST(dir); /* File start cluster */\r
+ fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */\r
+ fp->fptr = 0; /* File pointer */\r
+ fp->dsect = 0;\r
+#if _USE_FASTSEEK\r
+ fp->cltbl = 0; /* Normal seek mode */\r
+#endif\r
+ fp->fs = dj.fs; fp->id = dj.fs->id; /* Validate file object */\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Read File */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_read (\r
+ FIL_t *fp, /* Pointer to the file object */\r
+ void *buff, /* Pointer to data buffer */\r
+ UINT btr, /* Number of bytes to read */\r
+ UINT *br /* Pointer to number of bytes read */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD clst, sect, remain;\r
+ UINT rcnt, cc;\r
+ BYTE csect, *rbuff = (BYTE*)buff;\r
+\r
+\r
+ *br = 0; /* Initialize byte counter */\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity */\r
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);\r
+ if (fp->flag & FA__ERROR) /* Aborted file? */\r
+ LEAVE_FF(fp->fs, FR_INT_ERR);\r
+ if (!(fp->flag & FA_READ)) /* Check access mode */\r
+ LEAVE_FF(fp->fs, FR_DENIED);\r
+ remain = fp->fsize - fp->fptr;\r
+ if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */\r
+\r
+ for ( ; btr; /* Repeat until all data read */\r
+ rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {\r
+ if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */\r
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */\r
+ if (!csect) { /* On the cluster boundary? */\r
+ if (fp->fptr == 0) { /* On the top of the file? */\r
+ clst = fp->sclust; /* Follow from the origin */\r
+ } else { /* Middle or end of the file */\r
+#if _USE_FASTSEEK\r
+ if (fp->cltbl)\r
+ clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
+ else\r
+#endif\r
+ clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */\r
+ }\r
+ if (clst < 2) ABORT(fp->fs, FR_INT_ERR);\r
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->clust = clst; /* Update current cluster */\r
+ }\r
+ sect = clust2sect(fp->fs, fp->clust); /* Get current sector */\r
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);\r
+ sect += csect;\r
+ cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */\r
+ if (cc) { /* Read maximum contiguous sectors directly */\r
+ if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */\r
+ cc = fp->fs->csize - csect;\r
+ if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+#if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */\r
+#if _FS_TINY\r
+ if (fp->fs->wflag && fp->fs->winsect - sect < cc)\r
+ mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));\r
+#else\r
+ if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)\r
+ mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));\r
+#endif\r
+#endif\r
+ rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */\r
+ continue;\r
+ }\r
+#if !_FS_TINY\r
+ if (fp->dsect != sect) { /* Load data sector if not in cache */\r
+#if !_FS_READONLY\r
+ if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */\r
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ }\r
+#endif\r
+ fp->dsect = sect;\r
+ }\r
+ rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */\r
+ if (rcnt > btr) rcnt = btr;\r
+#if _FS_TINY\r
+ if (move_window(fp->fs, fp->dsect)) /* Move sector window */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */\r
+#else\r
+ mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */\r
+#endif\r
+ }\r
+\r
+ LEAVE_FF(fp->fs, FR_OK);\r
+}\r
+\r
+\r
+\r
+\r
+#if !_FS_READONLY\r
+/*-----------------------------------------------------------------------*/\r
+/* Write File */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_write (\r
+ FIL_t *fp, /* Pointer to the file object */\r
+ const void *buff, /* Pointer to the data to be written */\r
+ UINT btw, /* Number of bytes to write */\r
+ UINT *bw /* Pointer to number of bytes written */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD clst, sect;\r
+ UINT wcnt, cc;\r
+ const BYTE *wbuff = (BYTE*)buff;\r
+ BYTE csect;\r
+\r
+\r
+ *bw = 0; /* Initialize byte counter */\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity */\r
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);\r
+ if (fp->flag & FA__ERROR) /* Aborted file? */\r
+ LEAVE_FF(fp->fs, FR_INT_ERR);\r
+ if (!(fp->flag & FA_WRITE)) /* Check access mode */\r
+ LEAVE_FF(fp->fs, FR_DENIED);\r
+ if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0; /* File size cannot reach 4GB */\r
+\r
+ for ( ; btw; /* Repeat until all data written */\r
+ wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {\r
+ if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */\r
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */\r
+ if (!csect) { /* On the cluster boundary? */\r
+ if (fp->fptr == 0) { /* On the top of the file? */\r
+ clst = fp->sclust; /* Follow from the origin */\r
+ if (clst == 0) /* When no cluster is allocated, */\r
+ fp->sclust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */\r
+ } else { /* Middle or end of the file */\r
+#if _USE_FASTSEEK\r
+ if (fp->cltbl)\r
+ clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */\r
+ else\r
+#endif\r
+ clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */\r
+ }\r
+ if (clst == 0) break; /* Could not allocate a new cluster (disk full) */\r
+ if (clst == 1) ABORT(fp->fs, FR_INT_ERR);\r
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->clust = clst; /* Update current cluster */\r
+ }\r
+#if _FS_TINY\r
+ if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+#else\r
+ if (fp->flag & FA__DIRTY) { /* Write-back sector cache */\r
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ sect = clust2sect(fp->fs, fp->clust); /* Get current sector */\r
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);\r
+ sect += csect;\r
+ cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */\r
+ if (cc) { /* Write maximum contiguous sectors directly */\r
+ if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */\r
+ cc = fp->fs->csize - csect;\r
+ if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+#if _FS_TINY\r
+ if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
+ mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));\r
+ fp->fs->wflag = 0;\r
+ }\r
+#else\r
+ if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */\r
+ mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */\r
+ continue;\r
+ }\r
+#if _FS_TINY\r
+ if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */\r
+ if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->fs->winsect = sect;\r
+ }\r
+#else\r
+ if (fp->dsect != sect) { /* Fill sector cache with file data */\r
+ if (fp->fptr < fp->fsize &&\r
+ disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ }\r
+#endif\r
+ fp->dsect = sect;\r
+ }\r
+ wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */\r
+ if (wcnt > btw) wcnt = btw;\r
+#if _FS_TINY\r
+ if (move_window(fp->fs, fp->dsect)) /* Move sector window */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */\r
+ fp->fs->wflag = 1;\r
+#else\r
+ mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */\r
+ fp->flag |= FA__DIRTY;\r
+#endif\r
+ }\r
+\r
+ if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */\r
+ fp->flag |= FA__WRITTEN; /* Set file change flag */\r
+\r
+ LEAVE_FF(fp->fs, FR_OK);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Synchronize the File Object */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_sync (\r
+ FIL_t *fp /* Pointer to the file object */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD tim;\r
+ BYTE *dir;\r
+\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity of the object */\r
+ if (res == FR_OK) {\r
+ if (fp->flag & FA__WRITTEN) { /* Has the file been written? */\r
+#if !_FS_TINY /* Write-back dirty buffer */\r
+ if (fp->flag & FA__DIRTY) {\r
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)\r
+ LEAVE_FF(fp->fs, FR_DISK_ERR);\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ /* Update the directory entry */\r
+ res = move_window(fp->fs, fp->dir_sect);\r
+ if (res == FR_OK) {\r
+ dir = fp->dir_ptr;\r
+ dir[DIR_Attr] |= AM_ARC; /* Set archive bit */\r
+ ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */\r
+ ST_CLUST(dir, fp->sclust); /* Update start cluster */\r
+ tim = get_fattime(); /* Update updated time */\r
+ ST_DWORD(dir+DIR_WrtTime, tim);\r
+ fp->flag &= ~FA__WRITTEN;\r
+ fp->fs->wflag = 1;\r
+ res = sync(fp->fs);\r
+ }\r
+ }\r
+ }\r
+\r
+ LEAVE_FF(fp->fs, res);\r
+}\r
+\r
+#endif /* !_FS_READONLY */\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Close File */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_close (\r
+ FIL_t *fp /* Pointer to the file object to be closed */\r
+)\r
+{\r
+ FRESULT res;\r
+\r
+#if _FS_READONLY\r
+ FATFS *fs = fp->fs;\r
+ res = validate(fs, fp->id);\r
+ if (res == FR_OK) fp->fs = 0; /* Discard file object */\r
+ LEAVE_FF(fs, res);\r
+\r
+#else\r
+ res = f_sync(fp); /* Flush cached data */\r
+#if _FS_SHARE\r
+ if (res == FR_OK) { /* Decrement open counter */\r
+#if _FS_REENTRANT\r
+ res = validate(fp->fs, fp->id);\r
+ if (res == FR_OK) {\r
+ res = dec_lock(fp->lockid); \r
+ unlock_fs(fp->fs, FR_OK);\r
+ }\r
+#else\r
+ res = dec_lock(fp->lockid);\r
+#endif\r
+ }\r
+#endif\r
+ if (res == FR_OK) fp->fs = 0; /* Discard file object */\r
+ return res;\r
+#endif\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Current Drive/Directory Handlings */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+#if _FS_RPATH >= 1\r
+\r
+FRESULT f_chdrive (\r
+ BYTE drv /* Drive number */\r
+)\r
+{\r
+ if (drv >= _VOLUMES) return FR_INVALID_DRIVE;\r
+\r
+ CurrVol = drv;\r
+\r
+ return FR_OK;\r
+}\r
+\r
+\r
+\r
+FRESULT f_chdir (\r
+ const TCHAR *path /* Pointer to the directory path */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 0);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the path */\r
+ FREE_BUF();\r
+ if (res == FR_OK) { /* Follow completed */\r
+ if (!dj.dir) {\r
+ dj.fs->cdir = dj.sclust; /* Start directory itself */\r
+ } else {\r
+ if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */\r
+ dj.fs->cdir = LD_CLUST(dj.dir);\r
+ else\r
+ res = FR_NO_PATH; /* Reached but a file */\r
+ }\r
+ }\r
+ if (res == FR_NO_FILE) res = FR_NO_PATH;\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+#if _FS_RPATH >= 2\r
+FRESULT f_getcwd (\r
+ TCHAR *path, /* Pointer to the directory path */\r
+ UINT sz_path /* Size of path */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ UINT i, n;\r
+ DWORD ccl;\r
+ TCHAR *tp;\r
+ FILINFO fno;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ *path = 0;\r
+ res = chk_mounted((const TCHAR**)&path, &dj.fs, 0); /* Get current volume */\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ i = sz_path; /* Bottom of buffer (dir stack base) */\r
+ dj.sclust = dj.fs->cdir; /* Start to follow upper dir from current dir */\r
+ while ((ccl = dj.sclust) != 0) { /* Repeat while current dir is a sub-dir */\r
+ res = dir_sdi(&dj, 1); /* Get parent dir */\r
+ if (res != FR_OK) break;\r
+ res = dir_read(&dj);\r
+ if (res != FR_OK) break;\r
+ dj.sclust = LD_CLUST(dj.dir); /* Goto parent dir */\r
+ res = dir_sdi(&dj, 0);\r
+ if (res != FR_OK) break;\r
+ do { /* Find the entry links to the child dir */\r
+ res = dir_read(&dj);\r
+ if (res != FR_OK) break;\r
+ if (ccl == LD_CLUST(dj.dir)) break; /* Found the entry */\r
+ res = dir_next(&dj, 0); \r
+ } while (res == FR_OK);\r
+ if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */\r
+ if (res != FR_OK) break;\r
+#if _USE_LFN\r
+ fno.lfname = path;\r
+ fno.lfsize = i;\r
+#endif\r
+ get_fileinfo(&dj, &fno); /* Get the dir name and push it to the buffer */\r
+ tp = fno.fname;\r
+ if (_USE_LFN && *path) tp = path;\r
+ for (n = 0; tp[n]; n++) ;\r
+ if (i < n + 3) {\r
+ res = FR_NOT_ENOUGH_CORE; break;\r
+ }\r
+ while (n) path[--i] = tp[--n];\r
+ path[--i] = '/';\r
+ }\r
+ tp = path;\r
+ if (res == FR_OK) {\r
+ *tp++ = '0' + CurrVol; /* Put drive number */\r
+ *tp++ = ':';\r
+ if (i == sz_path) { /* Root-dir */\r
+ *tp++ = '/';\r
+ } else { /* Sub-dir */\r
+ do /* Add stacked path str */\r
+ *tp++ = path[i++];\r
+ while (i < sz_path);\r
+ }\r
+ }\r
+ *tp = 0;\r
+ FREE_BUF();\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+#endif /* _FS_RPATH >= 2 */\r
+#endif /* _FS_RPATH >= 1 */\r
+\r
+\r
+\r
+#if _FS_MINIMIZE <= 2\r
+/*-----------------------------------------------------------------------*/\r
+/* Seek File R/W Pointer */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_lseek (\r
+ FIL_t *fp, /* Pointer to the file object */\r
+ DWORD ofs /* File pointer from top of file */\r
+)\r
+{\r
+ FRESULT res;\r
+\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity of the object */\r
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);\r
+ if (fp->flag & FA__ERROR) /* Check abort flag */\r
+ LEAVE_FF(fp->fs, FR_INT_ERR);\r
+\r
+#if _USE_FASTSEEK\r
+ if (fp->cltbl) { /* Fast seek */\r
+ DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;\r
+\r
+ if (ofs == CREATE_LINKMAP) { /* Create CLMT */\r
+ tbl = fp->cltbl;\r
+ tlen = *tbl++; ulen = 2; /* Given table size and required table size */\r
+ cl = fp->sclust; /* Top of the chain */\r
+ if (cl) {\r
+ do {\r
+ /* Get a fragment */\r
+ tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */\r
+ do {\r
+ pcl = cl; ncl++;\r
+ cl = get_fat(fp->fs, cl);\r
+ if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);\r
+ if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ } while (cl == pcl + 1);\r
+ if (ulen <= tlen) { /* Store the length and top of the fragment */\r
+ *tbl++ = ncl; *tbl++ = tcl;\r
+ }\r
+ } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */\r
+ }\r
+ *fp->cltbl = ulen; /* Number of items used */\r
+ if (ulen <= tlen)\r
+ *tbl = 0; /* Terminate table */\r
+ else\r
+ res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */\r
+\r
+ } else { /* Fast seek */\r
+ if (ofs > fp->fsize) /* Clip offset at the file size */\r
+ ofs = fp->fsize;\r
+ fp->fptr = ofs; /* Set file pointer */\r
+ if (ofs) {\r
+ fp->clust = clmt_clust(fp, ofs - 1);\r
+ dsc = clust2sect(fp->fs, fp->clust);\r
+ if (!dsc) ABORT(fp->fs, FR_INT_ERR);\r
+ dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);\r
+ if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */\r
+#if !_FS_TINY\r
+#if !_FS_READONLY\r
+ if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */\r
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+#endif\r
+ fp->dsect = dsc;\r
+ }\r
+ }\r
+ }\r
+ } else\r
+#endif\r
+\r
+ /* Normal Seek */\r
+ {\r
+ DWORD clst, bcs, nsect, ifptr;\r
+\r
+ if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */\r
+#if !_FS_READONLY\r
+ && !(fp->flag & FA_WRITE)\r
+#endif\r
+ ) ofs = fp->fsize;\r
+\r
+ ifptr = fp->fptr;\r
+ fp->fptr = nsect = 0;\r
+ if (ofs) {\r
+ bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */\r
+ if (ifptr > 0 &&\r
+ (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */\r
+ fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */\r
+ ofs -= fp->fptr;\r
+ clst = fp->clust;\r
+ } else { /* When seek to back cluster, */\r
+ clst = fp->sclust; /* start from the first cluster */\r
+#if !_FS_READONLY\r
+ if (clst == 0) { /* If no cluster chain, create a new chain */\r
+ clst = create_chain(fp->fs, 0);\r
+ if (clst == 1) ABORT(fp->fs, FR_INT_ERR);\r
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->sclust = clst;\r
+ }\r
+#endif\r
+ fp->clust = clst;\r
+ }\r
+ if (clst != 0) {\r
+ while (ofs > bcs) { /* Cluster following loop */\r
+#if !_FS_READONLY\r
+ if (fp->flag & FA_WRITE) { /* Check if in write mode or not */\r
+ clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */\r
+ if (clst == 0) { /* When disk gets full, clip file size */\r
+ ofs = bcs; break;\r
+ }\r
+ } else\r
+#endif\r
+ clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */\r
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);\r
+ fp->clust = clst;\r
+ fp->fptr += bcs;\r
+ ofs -= bcs;\r
+ }\r
+ fp->fptr += ofs;\r
+ if (ofs % SS(fp->fs)) {\r
+ nsect = clust2sect(fp->fs, clst); /* Current sector */\r
+ if (!nsect) ABORT(fp->fs, FR_INT_ERR);\r
+ nsect += ofs / SS(fp->fs);\r
+ }\r
+ }\r
+ }\r
+ if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */\r
+#if !_FS_TINY\r
+#if !_FS_READONLY\r
+ if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */\r
+ if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->flag &= ~FA__DIRTY;\r
+ }\r
+#endif\r
+ if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+#endif\r
+ fp->dsect = nsect;\r
+ }\r
+#if !_FS_READONLY\r
+ if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */\r
+ fp->fsize = fp->fptr;\r
+ fp->flag |= FA__WRITTEN;\r
+ }\r
+#endif\r
+}\r
+\r
+ LEAVE_FF(fp->fs, res);\r
+}\r
+\r
+\r
+\r
+#if _FS_MINIMIZE <= 1\r
+/*-----------------------------------------------------------------------*/\r
+/* Create a Directroy Object */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_opendir (\r
+ DIR_t *dj, /* Pointer to directory object to create */\r
+ const TCHAR *path /* Pointer to the directory path */\r
+)\r
+{\r
+ FRESULT res;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj->fs, 0);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(*dj);\r
+ res = follow_path(dj, path); /* Follow the path to the directory */\r
+ FREE_BUF();\r
+ if (res == FR_OK) { /* Follow completed */\r
+ if (dj->dir) { /* It is not the root dir */\r
+ if (dj->dir[DIR_Attr] & AM_DIR) { /* The object is a directory */\r
+ dj->sclust = LD_CLUST(dj->dir);\r
+ } else { /* The object is not a directory */\r
+ res = FR_NO_PATH;\r
+ }\r
+ }\r
+ if (res == FR_OK) {\r
+ dj->id = dj->fs->id;\r
+ res = dir_sdi(dj, 0); /* Rewind dir */\r
+ }\r
+ }\r
+ if (res == FR_NO_FILE) res = FR_NO_PATH;\r
+ }\r
+\r
+ LEAVE_FF(dj->fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Read Directory Entry in Sequense */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_readdir (\r
+ DIR_t *dj, /* Pointer to the open directory object */\r
+ FILINFO *fno /* Pointer to file information to return */\r
+)\r
+{\r
+ FRESULT res;\r
+ DEF_NAMEBUF;\r
+\r
+ res = validate(dj->fs, dj->id); /* Check validity of the object */\r
+ if (res == FR_OK) {\r
+ if (!fno) {\r
+ res = dir_sdi(dj, 0); /* Rewind the directory object */\r
+ } else {\r
+ INIT_BUF(*dj);\r
+ res = dir_read(dj); /* Read an directory item */\r
+ if (res == FR_NO_FILE) { /* Reached end of dir */\r
+ dj->sect = 0;\r
+ res = FR_OK;\r
+ }\r
+ if (res == FR_OK) { /* A valid entry is found */\r
+ get_fileinfo(dj, fno); /* Get the object information */\r
+ res = dir_next(dj, 0); /* Increment index for next */\r
+ if (res == FR_NO_FILE) {\r
+ dj->sect = 0;\r
+ res = FR_OK;\r
+ }\r
+ }\r
+ FREE_BUF();\r
+ }\r
+ }\r
+\r
+ LEAVE_FF(dj->fs, res);\r
+}\r
+\r
+\r
+\r
+#if _FS_MINIMIZE == 0\r
+/*-----------------------------------------------------------------------*/\r
+/* Get File Status */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_stat (\r
+ const TCHAR *path, /* Pointer to the file path */\r
+ FILINFO *fno /* Pointer to file information to return */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 0);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ if (res == FR_OK) { /* Follow completed */\r
+ if (dj.dir) /* Found an object */\r
+ get_fileinfo(&dj, fno);\r
+ else /* It is root dir */\r
+ res = FR_INVALID_NAME;\r
+ }\r
+ FREE_BUF();\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+#if !_FS_READONLY\r
+/*-----------------------------------------------------------------------*/\r
+/* Get Number of Free Clusters */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_getfree (\r
+ const TCHAR *path, /* Pointer to the logical drive number (root dir) */\r
+ DWORD *nclst, /* Pointer to the variable to return number of free clusters */\r
+ FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD n, clst, sect, stat;\r
+ UINT i;\r
+ BYTE fat, *p;\r
+\r
+\r
+ /* Get drive number */\r
+ res = chk_mounted(&path, fatfs, 0);\r
+ if (res == FR_OK) {\r
+ /* If free_clust is valid, return it without full cluster scan */\r
+ if ((*fatfs)->free_clust <= (*fatfs)->n_fatent - 2) {\r
+ *nclst = (*fatfs)->free_clust;\r
+ } else {\r
+ /* Get number of free clusters */\r
+ fat = (*fatfs)->fs_type;\r
+ n = 0;\r
+ if (fat == FS_FAT12) {\r
+ clst = 2;\r
+ do {\r
+ stat = get_fat(*fatfs, clst);\r
+ if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }\r
+ if (stat == 1) { res = FR_INT_ERR; break; }\r
+ if (stat == 0) n++;\r
+ } while (++clst < (*fatfs)->n_fatent);\r
+ } else {\r
+ clst = (*fatfs)->n_fatent;\r
+ sect = (*fatfs)->fatbase;\r
+ i = 0; p = 0;\r
+ do {\r
+ if (!i) {\r
+ res = move_window(*fatfs, sect++);\r
+ if (res != FR_OK) break;\r
+ p = (*fatfs)->win;\r
+ i = SS(*fatfs);\r
+ }\r
+ if (fat == FS_FAT16) {\r
+ if (LD_WORD(p) == 0) n++;\r
+ p += 2; i -= 2;\r
+ } else {\r
+ if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;\r
+ p += 4; i -= 4;\r
+ }\r
+ } while (--clst);\r
+ }\r
+ (*fatfs)->free_clust = n;\r
+ if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;\r
+ *nclst = n;\r
+ }\r
+ }\r
+ LEAVE_FF(*fatfs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Truncate File */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_truncate (\r
+ FIL_t *fp /* Pointer to the file object */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD ncl;\r
+\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity of the object */\r
+ if (res == FR_OK) {\r
+ if (fp->flag & FA__ERROR) { /* Check abort flag */\r
+ res = FR_INT_ERR;\r
+ } else {\r
+ if (!(fp->flag & FA_WRITE)) /* Check access mode */\r
+ res = FR_DENIED;\r
+ }\r
+ }\r
+ if (res == FR_OK) {\r
+ if (fp->fsize > fp->fptr) {\r
+ fp->fsize = fp->fptr; /* Set file size to current R/W point */\r
+ fp->flag |= FA__WRITTEN;\r
+ if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */\r
+ res = remove_chain(fp->fs, fp->sclust);\r
+ fp->sclust = 0;\r
+ } else { /* When truncate a part of the file, remove remaining clusters */\r
+ ncl = get_fat(fp->fs, fp->clust);\r
+ res = FR_OK;\r
+ if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
+ if (ncl == 1) res = FR_INT_ERR;\r
+ if (res == FR_OK && ncl < fp->fs->n_fatent) {\r
+ res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);\r
+ if (res == FR_OK) res = remove_chain(fp->fs, ncl);\r
+ }\r
+ }\r
+ }\r
+ if (res != FR_OK) fp->flag |= FA__ERROR;\r
+ }\r
+\r
+ LEAVE_FF(fp->fs, res);\r
+ }\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Delete a File or Directory */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_unlink (\r
+ const TCHAR *path /* Pointer to the file or directory path */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj, sdj;\r
+ BYTE *dir;\r
+ DWORD dclst;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 1);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))\r
+ res = FR_INVALID_NAME; /* Cannot remove dot entry */\r
+#if _FS_SHARE\r
+ if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open file */\r
+#endif\r
+ if (res == FR_OK) { /* The object is accessible */\r
+ dir = dj.dir;\r
+ if (!dir) {\r
+ res = FR_INVALID_NAME; /* Cannot remove the start directory */\r
+ } else {\r
+ if (dir[DIR_Attr] & AM_RDO)\r
+ res = FR_DENIED; /* Cannot remove R/O object */\r
+ }\r
+ dclst = LD_CLUST(dir);\r
+ if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-dir? */\r
+ if (dclst < 2) {\r
+ res = FR_INT_ERR;\r
+ } else {\r
+ mem_cpy(&sdj, &dj, sizeof(DIR_t)); /* Check if the sub-dir is empty or not */\r
+ sdj.sclust = dclst;\r
+ res = dir_sdi(&sdj, 2); /* Exclude dot entries */\r
+ if (res == FR_OK) {\r
+ res = dir_read(&sdj);\r
+ if (res == FR_OK /* Not empty dir */\r
+#if _FS_RPATH\r
+ || dclst == sdj.fs->cdir /* Current dir */\r
+#endif\r
+ ) res = FR_DENIED;\r
+ if (res == FR_NO_FILE) res = FR_OK; /* Empty */\r
+ }\r
+ }\r
+ }\r
+ if (res == FR_OK) {\r
+ res = dir_remove(&dj); /* Remove the directory entry */\r
+ if (res == FR_OK) {\r
+ if (dclst) /* Remove the cluster chain if exist */\r
+ res = remove_chain(dj.fs, dclst);\r
+ if (res == FR_OK) res = sync(dj.fs);\r
+ }\r
+ }\r
+ }\r
+ FREE_BUF();\r
+ }\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Create a Directory */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_mkdir (\r
+ const TCHAR *path /* Pointer to the directory path */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ BYTE *dir, n;\r
+ DWORD dsc, dcl, pcl, tim = get_fattime();\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 1);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */\r
+ if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))\r
+ res = FR_INVALID_NAME;\r
+ if (res == FR_NO_FILE) { /* Can create a new directory */\r
+ dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */\r
+ res = FR_OK;\r
+ if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */\r
+ if (dcl == 1) res = FR_INT_ERR;\r
+ if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;\r
+ if (res == FR_OK) /* Flush FAT */\r
+ res = move_window(dj.fs, 0);\r
+ if (res == FR_OK) { /* Initialize the new directory table */\r
+ dsc = clust2sect(dj.fs, dcl);\r
+ dir = dj.fs->win;\r
+ mem_set(dir, 0, SS(dj.fs));\r
+ mem_set(dir+DIR_Name, ' ', 8+3); /* Create "." entry */\r
+ dir[DIR_Name] = '.';\r
+ dir[DIR_Attr] = AM_DIR;\r
+ ST_DWORD(dir+DIR_WrtTime, tim);\r
+ ST_CLUST(dir, dcl);\r
+ mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */\r
+ dir[33] = '.'; pcl = dj.sclust;\r
+ if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)\r
+ pcl = 0;\r
+ ST_CLUST(dir+SZ_DIR, pcl);\r
+ for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */\r
+ dj.fs->winsect = dsc++;\r
+ dj.fs->wflag = 1;\r
+ res = move_window(dj.fs, 0);\r
+ if (res != FR_OK) break;\r
+ mem_set(dir, 0, SS(dj.fs));\r
+ }\r
+ }\r
+ if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */\r
+ if (res != FR_OK) {\r
+ remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */\r
+ } else {\r
+ dir = dj.dir;\r
+ dir[DIR_Attr] = AM_DIR; /* Attribute */\r
+ ST_DWORD(dir+DIR_WrtTime, tim); /* Created time */\r
+ ST_CLUST(dir, dcl); /* Table start cluster */\r
+ dj.fs->wflag = 1;\r
+ res = sync(dj.fs);\r
+ }\r
+ }\r
+ FREE_BUF();\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Change Attribute */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_chmod (\r
+ const TCHAR *path, /* Pointer to the file path */\r
+ BYTE value, /* Attribute bits */\r
+ BYTE mask /* Attribute mask to change */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ BYTE *dir;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 1);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ FREE_BUF();\r
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))\r
+ res = FR_INVALID_NAME;\r
+ if (res == FR_OK) {\r
+ dir = dj.dir;\r
+ if (!dir) { /* Is it a root directory? */\r
+ res = FR_INVALID_NAME;\r
+ } else { /* File or sub directory */\r
+ mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */\r
+ dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */\r
+ dj.fs->wflag = 1;\r
+ res = sync(dj.fs);\r
+ }\r
+ }\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Change Timestamp */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_utime (\r
+ const TCHAR *path, /* Pointer to the file/directory name */\r
+ const FILINFO *fno /* Pointer to the time stamp to be set */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t dj;\r
+ BYTE *dir;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path, &dj.fs, 1);\r
+ if (res == FR_OK) {\r
+ INIT_BUF(dj);\r
+ res = follow_path(&dj, path); /* Follow the file path */\r
+ FREE_BUF();\r
+ if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))\r
+ res = FR_INVALID_NAME;\r
+ if (res == FR_OK) {\r
+ dir = dj.dir;\r
+ if (!dir) { /* Root directory */\r
+ res = FR_INVALID_NAME;\r
+ } else { /* File or sub-directory */\r
+ ST_WORD(dir+DIR_WrtTime, fno->ftime);\r
+ ST_WORD(dir+DIR_WrtDate, fno->fdate);\r
+ dj.fs->wflag = 1;\r
+ res = sync(dj.fs);\r
+ }\r
+ }\r
+ }\r
+\r
+ LEAVE_FF(dj.fs, res);\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Rename File/Directory */\r
+/*-----------------------------------------------------------------------*/\r
+\r
+FRESULT f_rename (\r
+ const TCHAR *path_old, /* Pointer to the old name */\r
+ const TCHAR *path_new /* Pointer to the new name */\r
+)\r
+{\r
+ FRESULT res;\r
+ DIR_t djo, djn;\r
+ BYTE buf[21], *dir;\r
+ DWORD dw;\r
+ DEF_NAMEBUF;\r
+\r
+\r
+ res = chk_mounted(&path_old, &djo.fs, 1);\r
+ if (res == FR_OK) {\r
+ djn.fs = djo.fs;\r
+ INIT_BUF(djo);\r
+ res = follow_path(&djo, path_old); /* Check old object */\r
+ if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))\r
+ res = FR_INVALID_NAME;\r
+#if _FS_SHARE\r
+ if (res == FR_OK) res = chk_lock(&djo, 2);\r
+#endif\r
+ if (res == FR_OK) { /* Old object is found */\r
+ if (!djo.dir) { /* Is root dir? */\r
+ res = FR_NO_FILE;\r
+ } else {\r
+ mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except for name */\r
+ mem_cpy(&djn, &djo, sizeof(DIR_t)); /* Check new object */\r
+ res = follow_path(&djn, path_new);\r
+ if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */\r
+ if (res == FR_NO_FILE) { /* Is it a valid path and no name collision? */\r
+/* Start critical section that any interruption or error can cause cross-link */\r
+ res = dir_register(&djn); /* Register the new entry */\r
+ if (res == FR_OK) {\r
+ dir = djn.dir; /* Copy object information except for name */\r
+ mem_cpy(dir+13, buf+2, 19);\r
+ dir[DIR_Attr] = buf[0] | AM_ARC;\r
+ djo.fs->wflag = 1;\r
+ if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) { /* Update .. entry in the directory if needed */\r
+ dw = clust2sect(djn.fs, LD_CLUST(dir));\r
+ if (!dw) {\r
+ res = FR_INT_ERR;\r
+ } else {\r
+ res = move_window(djn.fs, dw);\r
+ dir = djn.fs->win+SZ_DIR; /* .. entry */\r
+ if (res == FR_OK && dir[1] == '.') {\r
+ dw = (djn.fs->fs_type == FS_FAT32 && djn.sclust == djn.fs->dirbase) ? 0 : djn.sclust;\r
+ ST_CLUST(dir, dw);\r
+ djn.fs->wflag = 1;\r
+ }\r
+ }\r
+ }\r
+ if (res == FR_OK) {\r
+ res = dir_remove(&djo); /* Remove old entry */\r
+ if (res == FR_OK)\r
+ res = sync(djo.fs);\r
+ }\r
+ }\r
+/* End critical section */\r
+ }\r
+ }\r
+ }\r
+ FREE_BUF();\r
+ }\r
+ LEAVE_FF(djo.fs, res);\r
+}\r
+\r
+#endif /* !_FS_READONLY */\r
+#endif /* _FS_MINIMIZE == 0 */\r
+#endif /* _FS_MINIMIZE <= 1 */\r
+#endif /* _FS_MINIMIZE <= 2 */\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Forward data to the stream directly (available on only tiny cfg) */\r
+/*-----------------------------------------------------------------------*/\r
+#if _USE_FORWARD && _FS_TINY\r
+\r
+FRESULT f_forward (\r
+ FIL_t *fp, /* Pointer to the file object */\r
+ UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */\r
+ UINT btr, /* Number of bytes to forward */\r
+ UINT *bf /* Pointer to number of bytes forwarded */\r
+)\r
+{\r
+ FRESULT res;\r
+ DWORD remain, clst, sect;\r
+ UINT rcnt;\r
+ BYTE csect;\r
+\r
+\r
+ *bf = 0; /* Initialize byte counter */\r
+\r
+ res = validate(fp->fs, fp->id); /* Check validity of the object */\r
+ if (res != FR_OK) LEAVE_FF(fp->fs, res);\r
+ if (fp->flag & FA__ERROR) /* Check error flag */\r
+ LEAVE_FF(fp->fs, FR_INT_ERR);\r
+ if (!(fp->flag & FA_READ)) /* Check access mode */\r
+ LEAVE_FF(fp->fs, FR_DENIED);\r
+\r
+ remain = fp->fsize - fp->fptr;\r
+ if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */\r
+\r
+ for ( ; btr && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */\r
+ fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) {\r
+ csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */\r
+ if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */\r
+ if (!csect) { /* On the cluster boundary? */\r
+ clst = (fp->fptr == 0) ? /* On the top of the file? */\r
+ fp->sclust : get_fat(fp->fs, fp->clust);\r
+ if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);\r
+ if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->clust = clst; /* Update current cluster */\r
+ }\r
+ }\r
+ sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */\r
+ if (!sect) ABORT(fp->fs, FR_INT_ERR);\r
+ sect += csect;\r
+ if (move_window(fp->fs, sect)) /* Move sector window */\r
+ ABORT(fp->fs, FR_DISK_ERR);\r
+ fp->dsect = sect;\r
+ rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */\r
+ if (rcnt > btr) rcnt = btr;\r
+ rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);\r
+ if (!rcnt) ABORT(fp->fs, FR_INT_ERR);\r
+}\r
+\r
+ LEAVE_FF(fp->fs, FR_OK);\r
+}\r
+#endif /* _USE_FORWARD */\r
+\r
+\r
+\r
+#if _USE_MKFS && !_FS_READONLY\r
+/*-----------------------------------------------------------------------*/\r
+/* Create File System on the Drive */\r
+/*-----------------------------------------------------------------------*/\r
+#define N_ROOTDIR 512 /* Number of root dir entries for FAT12/16 */\r
+#define N_FATS 1 /* Number of FAT copies (1 or 2) */\r
+\r
+\r
+FRESULT f_mkfs (\r
+ BYTE drv, /* Logical drive number */\r
+ BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */\r
+ UINT au /* Allocation unit size [bytes] */\r
+)\r
+{\r
+ static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};\r
+ static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};\r
+ BYTE fmt, md, *tbl;\r
+ DWORD n_clst, vs, n, wsect;\r
+ UINT i;\r
+ DWORD b_vol, b_fat, b_dir, b_data; /* Offset (LBA) */\r
+ DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */\r
+ FATFS *fs;\r
+ DSTATUS stat;\r
+\r
+\r
+ /* Check mounted drive and clear work area */\r
+ if (drv >= _VOLUMES) return FR_INVALID_DRIVE;\r
+ fs = FatFs[drv];\r
+ if (!fs) return FR_NOT_ENABLED;\r
+ fs->fs_type = 0;\r
+ drv = LD2PD(drv);\r
+\r
+ /* Get disk statics */\r
+ stat = disk_initialize(drv);\r
+ if (stat & STA_NOINIT) return FR_NOT_READY;\r
+ if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;\r
+#if _MAX_SS != 512 /* Get disk sector size */\r
+ if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK)\r
+ return FR_DISK_ERR;\r
+#endif\r
+ if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)\r
+ return FR_DISK_ERR;\r
+ b_vol = (sfd) ? 0 : 63; /* Volume start sector */\r
+ n_vol -= b_vol;\r
+ if (au & (au - 1)) au = 0; /* Check validity of the AU size */\r
+ if (!au) { /* AU auto selection */\r
+ vs = n_vol / (2000 / (SS(fs) / 512));\r
+ for (i = 0; vs < vst[i]; i++) ;\r
+ au = cst[i];\r
+ }\r
+ au /= SS(fs); /* Number of sectors per cluster */\r
+ if (au == 0) au = 1;\r
+ if (au > 128) au = 128;\r
+\r
+ /* Pre-compute number of clusters and FAT syb-type */\r
+ n_clst = n_vol / au;\r
+ fmt = FS_FAT12;\r
+ if (n_clst >= MIN_FAT16) fmt = FS_FAT16;\r
+ if (n_clst >= MIN_FAT32) fmt = FS_FAT32;\r
+\r
+ /* Determine offset and size of FAT structure */\r
+ if (fmt == FS_FAT32) {\r
+ n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);\r
+ n_rsv = 32;\r
+ n_dir = 0;\r
+ } else {\r
+ n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;\r
+ n_fat = (n_fat + SS(fs) - 1) / SS(fs);\r
+ n_rsv = 1;\r
+ n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);\r
+ }\r
+ b_fat = b_vol + n_rsv; /* FAT area start sector */\r
+ b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */\r
+ b_data = b_dir + n_dir; /* Data area start sector */\r
+ if (n_vol < b_data + au) return FR_MKFS_ABORTED; /* Too small volume */\r
+\r
+ /* Align data start sector to erase block boundary (for flash memory media) */\r
+ if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;\r
+ n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */\r
+ n = (n - b_data) / N_FATS;\r
+ if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */\r
+ n_rsv += n;\r
+ b_fat += n;\r
+ } else { /* FAT12/16: Expand FAT size */\r
+ n_fat += n;\r
+ }\r
+\r
+ /* Determine number of clusters and final check of validity of the FAT sub-type */\r
+ n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;\r
+ if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)\r
+ || (fmt == FS_FAT32 && n_clst < MIN_FAT32))\r
+ return FR_MKFS_ABORTED;\r
+\r
+ /* Create partition table if required */\r
+ if (sfd) { /* No patition table (SFD) */\r
+ md = 0xF0;\r
+ } else { /* With patition table (FDISK) */\r
+ DWORD n_disk = b_vol + n_vol;\r
+\r
+ mem_set(fs->win, 0, SS(fs));\r
+ tbl = fs->win+MBR_Table;\r
+ ST_DWORD(tbl, 0x00010180); /* Partition start in CHS */\r
+ if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */\r
+ n_disk = n_disk / 63 / 255;\r
+ tbl[7] = (BYTE)n_disk;\r
+ tbl[6] = (BYTE)((n_disk >> 2) | 63);\r
+ } else {\r
+ ST_WORD(&tbl[6], 0xFFFF); /* CHS saturated */\r
+ }\r
+ tbl[5] = 254;\r
+ if (fmt != FS_FAT32) /* System ID */\r
+ tbl[4] = (n_vol < 0x10000) ? 0x04 : 0x06;\r
+ else\r
+ tbl[4] = 0x0c;\r
+ ST_DWORD(tbl+8, 63); /* Partition start in LBA */\r
+ ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */\r
+ ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */\r
+ if (disk_write(drv, fs->win, 0, 1) != RES_OK) /* Put the MBR into first physical sector */\r
+ return FR_DISK_ERR;\r
+ md = 0xF8;\r
+ }\r
+\r
+ /* Create volume boot record */\r
+ tbl = fs->win; /* Clear sector */\r
+ mem_set(tbl, 0, SS(fs));\r
+ mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */\r
+ i = SS(fs); /* Sector size */\r
+ ST_WORD(tbl+BPB_BytsPerSec, i);\r
+ tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */\r
+ ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */\r
+ tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */\r
+ i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of rootdir entries */\r
+ ST_WORD(tbl+BPB_RootEntCnt, i);\r
+ if (n_vol < 0x10000) { /* Number of total sectors */\r
+ ST_WORD(tbl+BPB_TotSec16, n_vol);\r
+ } else {\r
+ ST_DWORD(tbl+BPB_TotSec32, n_vol);\r
+ }\r
+ tbl[BPB_Media] = md; /* Media descriptor */\r
+ ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */\r
+ ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */\r
+ ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */\r
+ n = get_fattime(); /* Use current time as VSN */\r
+ if (fmt == FS_FAT32) {\r
+ ST_DWORD(tbl+BS_VolID32, n); /* VSN */\r
+ ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */\r
+ ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */\r
+ ST_WORD(tbl+BPB_FSInfo, 1); /* FSInfo record offset (VBR+1) */\r
+ ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */\r
+ tbl[BS_DrvNum32] = 0x80; /* Drive number */\r
+ tbl[BS_BootSig32] = 0x29; /* Extended boot signature */\r
+ mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */\r
+ } else {\r
+ ST_DWORD(tbl+BS_VolID, n); /* VSN */\r
+ ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */\r
+ tbl[BS_DrvNum] = 0x80; /* Drive number */\r
+ tbl[BS_BootSig] = 0x29; /* Extended boot signature */\r
+ mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */\r
+ }\r
+ ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */\r
+ if (disk_write(drv, tbl, b_vol, 1) != RES_OK) /* Write VBR */\r
+ return FR_DISK_ERR;\r
+ if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */\r
+ disk_write(drv, tbl, b_vol + 6, 1);\r
+\r
+ /* Initialize FAT area */\r
+ wsect = b_fat;\r
+ for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */\r
+ mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */\r
+ n = md; /* Media descriptor byte */\r
+ if (fmt != FS_FAT32) {\r
+ n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;\r
+ ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */\r
+ } else {\r
+ n |= 0xFFFFFF00;\r
+ ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */\r
+ ST_DWORD(tbl+4, 0xFFFFFFFF);\r
+ ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root dir */\r
+ }\r
+ if (disk_write(drv, tbl, wsect++, 1) != RES_OK)\r
+ return FR_DISK_ERR;\r
+ mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */\r
+ for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */\r
+ if (disk_write(drv, tbl, wsect++, 1) != RES_OK)\r
+ return FR_DISK_ERR;\r
+ }\r
+ }\r
+\r
+ /* Initialize root directory */\r
+ i = (fmt == FS_FAT32) ? au : n_dir;\r
+ do {\r
+ if (disk_write(drv, tbl, wsect++, 1) != RES_OK)\r
+ return FR_DISK_ERR;\r
+ } while (--i);\r
+\r
+#if _USE_ERASE /* Erase data area if needed */\r
+ {\r
+ DWORD eb[2];\r
+\r
+ eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;\r
+ disk_ioctl(drv, CTRL_ERASE_SECTOR, eb);\r
+ }\r
+#endif\r
+\r
+ /* Create FSInfo if needed */\r
+ if (fmt == FS_FAT32) {\r
+ ST_DWORD(tbl+FSI_LeadSig, 0x41615252);\r
+ ST_DWORD(tbl+FSI_StrucSig, 0x61417272);\r
+ ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */\r
+ ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */\r
+ ST_WORD(tbl+BS_55AA, 0xAA55);\r
+ disk_write(drv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */\r
+ disk_write(drv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */\r
+ }\r
+\r
+ return (disk_ioctl(drv, CTRL_SYNC, (void*)0) == RES_OK) ? FR_OK : FR_DISK_ERR;\r
+}\r
+\r
+#endif /* _USE_MKFS && !_FS_READONLY */\r
+\r
+\r
+\r
+\r
+#if _USE_STRFUNC\r
+/*-----------------------------------------------------------------------*/\r
+/* Get a string from the file */\r
+/*-----------------------------------------------------------------------*/\r
+TCHAR* f_gets (\r
+ TCHAR* buff, /* Pointer to the string buffer to read */\r
+ int len, /* Size of string buffer (characters) */\r
+ FIL_t* fil /* Pointer to the file object */\r
+)\r
+{\r
+ int n = 0;\r
+ TCHAR c, *p = buff;\r
+ BYTE s[2];\r
+ UINT rc;\r
+\r
+\r
+ while (n < len - 1) { /* Read bytes until buffer gets filled */\r
+ f_read(fil, s, 1, &rc);\r
+ if (rc != 1) break; /* Break on EOF or error */\r
+ c = s[0];\r
+#if _LFN_UNICODE /* Read a character in UTF-8 encoding */\r
+ if (c >= 0x80) {\r
+ if (c < 0xC0) continue; /* Skip stray trailer */\r
+ if (c < 0xE0) { /* Two-byte sequense */\r
+ f_read(fil, s, 1, &rc);\r
+ if (rc != 1) break;\r
+ c = ((c & 0x1F) << 6) | (s[0] & 0x3F);\r
+ if (c < 0x80) c = '?';\r
+ } else {\r
+ if (c < 0xF0) { /* Three-byte sequense */\r
+ f_read(fil, s, 2, &rc);\r
+ if (rc != 2) break;\r
+ c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);\r
+ if (c < 0x800) c = '?';\r
+ } else { /* Reject four-byte sequense */\r
+ c = '?';\r
+ }\r
+ }\r
+ }\r
+#endif\r
+#if _USE_STRFUNC >= 2\r
+ if (c == '\r') continue; /* Strip '\r' */\r
+#endif\r
+ *p++ = c;\r
+ n++;\r
+ if (c == '\n') break; /* Break on EOL */\r
+ }\r
+ *p = 0;\r
+ return n ? buff : 0; /* When no data read (eof or error), return with error. */\r
+}\r
+\r
+\r
+\r
+#if !_FS_READONLY\r
+#include <stdarg.h>\r
+/*-----------------------------------------------------------------------*/\r
+/* Put a character to the file */\r
+/*-----------------------------------------------------------------------*/\r
+int f_putc (\r
+ TCHAR c, /* A character to be output */\r
+ FIL_t* fil /* Pointer to the file object */\r
+)\r
+{\r
+ UINT bw, btw;\r
+ BYTE s[3];\r
+\r
+\r
+#if _USE_STRFUNC >= 2\r
+ if (c == '\n') f_putc ('\r', fil); /* LF -> CRLF conversion */\r
+#endif\r
+\r
+#if _LFN_UNICODE /* Write the character in UTF-8 encoding */\r
+ if (c < 0x80) { /* 7-bit */\r
+ s[0] = (BYTE)c;\r
+ btw = 1;\r
+ } else {\r
+ if (c < 0x800) { /* 11-bit */\r
+ s[0] = (BYTE)(0xC0 | (c >> 6));\r
+ s[1] = (BYTE)(0x80 | (c & 0x3F));\r
+ btw = 2;\r
+ } else { /* 16-bit */\r
+ s[0] = (BYTE)(0xE0 | (c >> 12));\r
+ s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));\r
+ s[2] = (BYTE)(0x80 | (c & 0x3F));\r
+ btw = 3;\r
+ }\r
+ }\r
+#else /* Write the character without conversion */\r
+ s[0] = (BYTE)c;\r
+ btw = 1;\r
+#endif\r
+ f_write(fil, s, btw, &bw); /* Write the char to the file */\r
+ return (bw == btw) ? 1 : EOF; /* Return the result */\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Put a string to the file */\r
+/*-----------------------------------------------------------------------*/\r
+int f_puts (\r
+ const TCHAR* str, /* Pointer to the string to be output */\r
+ FIL_t* fil /* Pointer to the file object */\r
+)\r
+{\r
+ int n;\r
+\r
+\r
+ for (n = 0; *str; str++, n++) {\r
+ if (f_putc(*str, fil) == EOF) return EOF;\r
+ }\r
+ return n;\r
+}\r
+\r
+\r
+\r
+\r
+/*-----------------------------------------------------------------------*/\r
+/* Put a formatted string to the file */\r
+/*-----------------------------------------------------------------------*/\r
+int f_printf (\r
+ FIL_t* fil, /* Pointer to the file object */\r
+ const TCHAR* str, /* Pointer to the format string */\r
+ ... /* Optional arguments... */\r
+)\r
+{\r
+ va_list arp;\r
+ BYTE f, r;\r
+ UINT i, j, w;\r
+ ULONG v;\r
+ TCHAR c, d, s[16], *p;\r
+ int res, cc;\r
+\r
+\r
+ va_start(arp, str);\r
+\r
+ for (cc = res = 0; cc != EOF; res += cc) {\r
+ c = *str++;\r
+ if (c == 0) break; /* End of string */\r
+ if (c != '%') { /* Non escape character */\r
+ cc = f_putc(c, fil);\r
+ if (cc != EOF) cc = 1;\r
+ continue;\r
+ }\r
+ w = f = 0;\r
+ c = *str++;\r
+ if (c == '0') { /* Flag: '0' padding */\r
+ f = 1; c = *str++;\r
+ } else {\r
+ if (c == '-') { /* Flag: left justified */\r
+ f = 2; c = *str++;\r
+ }\r
+ }\r
+ while (IsDigit(c)) { /* Precision */\r
+ w = w * 10 + c - '0';\r
+ c = *str++;\r
+ }\r
+ if (c == 'l' || c == 'L') { /* Prefix: Size is long int */\r
+ f |= 4; c = *str++;\r
+ }\r
+ if (!c) break;\r
+ d = c;\r
+ if (IsLower(d)) d -= 0x20;\r
+ switch (d) { /* Type is... */\r
+ case 'S' : /* String */\r
+ p = va_arg(arp, TCHAR*);\r
+ for (j = 0; p[j]; j++) ;\r
+ res = 0;\r
+ while (!(f & 2) && j++ < w) res += (cc = f_putc(' ', fil));\r
+ res += (cc = f_puts(p, fil));\r
+ while (j++ < w) res += (cc = f_putc(' ', fil));\r
+ if (cc != EOF) cc = res;\r
+ continue;\r
+ case 'C' : /* Character */\r
+ cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;\r
+ case 'B' : /* Binary */\r
+ r = 2; break;\r
+ case 'O' : /* Octal */\r
+ r = 8; break;\r
+ case 'D' : /* Signed decimal */\r
+ case 'U' : /* Unsigned decimal */\r
+ r = 10; break;\r
+ case 'X' : /* Hexdecimal */\r
+ r = 16; break;\r
+ default: /* Unknown type (passthrough) */\r
+ cc = f_putc(c, fil); continue;\r
+ }\r
+\r
+ /* Get an argument and put it in numeral */\r
+ v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : va_arg(arp, unsigned int));\r
+ if (d == 'D' && (v & 0x80000000)) {\r
+ v = 0 - v;\r
+ f |= 8;\r
+ }\r
+ i = 0;\r
+ do {\r
+ d = (TCHAR)(v % r); v /= r;\r
+ if (d > 9) d += (c == 'x') ? 0x27 : 0x07;\r
+ s[i++] = d + '0';\r
+ } while (v && i < sizeof(s) / sizeof(s[0]));\r
+ if (f & 8) s[i++] = '-';\r
+ j = i; d = (f & 1) ? '0' : ' ';\r
+ res = 0;\r
+ while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));\r
+ do res += (cc = f_putc(s[--i], fil)); while(i);\r
+ while (j++ < w) res += (cc = f_putc(' ', fil));\r
+ if (cc != EOF) cc = res;\r
+ }\r
+\r
+ va_end(arp);\r
+ return (cc == EOF) ? cc : res;\r
+}\r
+\r
+#endif /* !_FS_READONLY */\r
+#endif /* _USE_STRFUNC */\r
--- /dev/null
+/*---------------------------------------------------------------------------/\r
+/ FatFs - FAT file system module include file R0.08b (C)ChaN, 2011\r
+/----------------------------------------------------------------------------/\r
+/ FatFs module is a generic FAT file system module for small embedded systems.\r
+/ This is a free software that opened for education, research and commercial\r
+/ developments under license policy of following trems.\r
+/\r
+/ Copyright (C) 2011, ChaN, all right reserved.\r
+/\r
+/ * The FatFs module is a free software and there is NO WARRANTY.\r
+/ * No restriction on use. You can use, modify and redistribute it for\r
+/ personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.\r
+/ * Redistributions of source code must retain the above copyright notice.\r
+/\r
+/----------------------------------------------------------------------------*/\r
+\r
+#ifndef _FATFS\r
+#define _FATFS 8237 /* Revision ID */\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+#include "integer.h" /* Basic integer types */\r
+#include "ffconf.h" /* FatFs configuration options */\r
+\r
+#if _FATFS != _FFCONF\r
+#error Wrong configuration file (ffconf.h).\r
+#endif\r
+\r
+\r
+\r
+#define _DRIVES 4\r
+/* Number of logical drives to be used. This affects the size of internal table. */\r
+/* Definitions of volume management */\r
+\r
+#if _MULTI_PARTITION /* Multiple partition configuration */\r
+#define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive# */\r
+#define LD2PT(vol) (VolToPart[vol].pt) /* Get partition# */\r
+typedef struct {\r
+ BYTE pd; /* Physical drive# */\r
+ BYTE pt; /* Partition # (0-3) */\r
+} PARTITION;\r
+extern const PARTITION VolToPart[]; /* Volume - Physical location resolution table */\r
+\r
+#else /* Single partition configuration */\r
+#define LD2PD(vol) (vol) /* Logical drive# is bound to the same physical drive# */\r
+#define LD2PT(vol) 0 /* Always mounts the 1st partition */\r
+\r
+#endif\r
+\r
+\r
+\r
+/* Type of path name strings on FatFs API */\r
+\r
+#if _LFN_UNICODE /* Unicode string */\r
+#if !_USE_LFN\r
+#error _LFN_UNICODE must be 0 in non-LFN cfg.\r
+#endif\r
+#ifndef _INC_TCHAR\r
+typedef WCHAR TCHAR;\r
+#define _T(x) L ## x\r
+#define _TEXT(x) L ## x\r
+#endif\r
+\r
+#else /* ANSI/OEM string */\r
+#ifndef _INC_TCHAR\r
+typedef char TCHAR;\r
+#define _T(x) x\r
+#define _TEXT(x) x\r
+#endif\r
+\r
+#endif\r
+\r
+\r
+\r
+/* File system object structure (FATFS) */\r
+\r
+typedef struct {\r
+ BYTE fs_type; /* FAT sub-type (0:Not mounted) */\r
+ BYTE drv; /* Physical drive number */\r
+ BYTE csize; /* Sectors per cluster (1,2,4...128) */\r
+ BYTE n_fats; /* Number of FAT copies (1,2) */\r
+ BYTE wflag; /* win[] dirty flag (1:must be written back) */\r
+ BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */\r
+ WORD id; /* File system mount ID */\r
+ WORD n_rootdir; /* Number of root directory entries (FAT12/16) */\r
+#if _MAX_SS != 512\r
+ WORD ssize; /* Bytes per sector (512,1024,2048,4096) */\r
+#endif\r
+#if _FS_REENTRANT\r
+ _SYNC_t sobj; /* Identifier of sync object */\r
+#endif\r
+#if !_FS_READONLY\r
+ DWORD last_clust; /* Last allocated cluster */\r
+ DWORD free_clust; /* Number of free clusters */\r
+ DWORD fsi_sector; /* fsinfo sector (FAT32) */\r
+#endif\r
+#if _FS_RPATH\r
+ DWORD cdir; /* Current directory start cluster (0:root) */\r
+#endif\r
+ DWORD n_fatent; /* Number of FAT entries (= number of clusters + 2) */\r
+ DWORD fsize; /* Sectors per FAT */\r
+ DWORD fatbase; /* FAT start sector */\r
+ DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */\r
+ DWORD database; /* Data start sector */\r
+ DWORD winsect; /* Current sector appearing in the win[] */\r
+ BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and Data on tiny cfg) */\r
+} FATFS;\r
+\r
+\r
+\r
+/* File object structure (FIL) */\r
+\r
+typedef struct {\r
+ FATFS* fs; /* Pointer to the owner file system object */\r
+ WORD id; /* Owner file system mount ID */\r
+ BYTE flag; /* File status flags */\r
+ BYTE pad1;\r
+ DWORD fptr; /* File read/write pointer (0 on file open) */\r
+ DWORD fsize; /* File size */\r
+ DWORD sclust; /* File start cluster (0 when fsize==0) */\r
+ DWORD clust; /* Current cluster */\r
+ DWORD dsect; /* Current data sector */\r
+#if !_FS_READONLY\r
+ DWORD dir_sect; /* Sector containing the directory entry */\r
+ BYTE* dir_ptr; /* Ponter to the directory entry in the window */\r
+#endif\r
+#if _USE_FASTSEEK\r
+ DWORD* cltbl; /* Pointer to the cluster link map table (null on file open) */\r
+#endif\r
+#if _FS_SHARE\r
+ UINT lockid; /* File lock ID (index of file semaphore table) */\r
+#endif\r
+#if !_FS_TINY\r
+ BYTE buf[_MAX_SS]; /* File data read/write buffer */\r
+#endif\r
+} FIL_t;\r
+\r
+\r
+\r
+/* Directory object structure (DIR) */\r
+\r
+typedef struct {\r
+ FATFS* fs; /* Pointer to the owner file system object */\r
+ WORD id; /* Owner file system mount ID */\r
+ WORD index; /* Current read/write index number */\r
+ DWORD sclust; /* Table start cluster (0:Root dir) */\r
+ DWORD clust; /* Current cluster */\r
+ DWORD sect; /* Current sector */\r
+ BYTE* dir; /* Pointer to the current SFN entry in the win[] */\r
+ BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */\r
+#if _USE_LFN\r
+ WCHAR* lfn; /* Pointer to the LFN working buffer */\r
+ WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */\r
+#endif\r
+} DIR_t;\r
+\r
+\r
+\r
+/* File status structure (FILINFO) */\r
+\r
+typedef struct {\r
+ DWORD fsize; /* File size */\r
+ WORD fdate; /* Last modified date */\r
+ WORD ftime; /* Last modified time */\r
+ BYTE fattrib; /* Attribute */\r
+ TCHAR fname[13]; /* Short file name (8.3 format) */\r
+#if _USE_LFN\r
+ TCHAR* lfname; /* Pointer to the LFN buffer */\r
+ UINT lfsize; /* Size of LFN buffer in TCHAR */\r
+#endif\r
+} FILINFO;\r
+\r
+\r
+\r
+/* File function return code (FRESULT) */\r
+\r
+typedef enum {\r
+ FR_OK = 0, /* (0) Succeeded */\r
+ FR_DISK_ERR, /* (1) A hard error occured in the low level disk I/O layer */\r
+ FR_INT_ERR, /* (2) Assertion failed */\r
+ FR_NOT_READY, /* (3) The physical drive cannot work */\r
+ FR_NO_FILE, /* (4) Could not find the file */\r
+ FR_NO_PATH, /* (5) Could not find the path */\r
+ FR_INVALID_NAME, /* (6) The path name format is invalid */\r
+ FR_DENIED, /* (7) Acces denied due to prohibited access or directory full */\r
+ FR_EXIST, /* (8) Acces denied due to prohibited access */\r
+ FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */\r
+ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */\r
+ FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */\r
+ FR_NOT_ENABLED, /* (12) The volume has no work area */\r
+ FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume on the physical drive */\r
+ FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */\r
+ FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */\r
+ FR_LOCKED, /* (16) The operation is rejected according to the file shareing policy */\r
+ FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */\r
+ FR_TOO_MANY_OPEN_FILES /* (18) Number of open files > _FS_SHARE */\r
+} FRESULT;\r
+\r
+\r
+\r
+/*--------------------------------------------------------------*/\r
+/* FatFs module application interface */\r
+\r
+FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */\r
+FRESULT f_open (FIL_t*, const TCHAR*, BYTE); /* Open or create a file */\r
+FRESULT f_read (FIL_t*, void*, UINT, UINT*); /* Read data from a file */\r
+FRESULT f_lseek (FIL_t*, DWORD); /* Move file pointer of a file object */\r
+FRESULT f_close (FIL_t*); /* Close an open file object */\r
+FRESULT f_opendir (DIR_t*, const TCHAR*); /* Open an existing directory */\r
+FRESULT f_readdir (DIR_t*, FILINFO*); /* Read a directory item */\r
+FRESULT f_stat (const TCHAR*, FILINFO*); /* Get file status */\r
+FRESULT f_write (FIL_t*, const void*, UINT, UINT*); /* Write data to a file */\r
+FRESULT f_getfree (const TCHAR*, DWORD*, FATFS**); /* Get number of free clusters on the drive */\r
+FRESULT f_truncate (FIL_t*); /* Truncate file */\r
+FRESULT f_sync (FIL_t*); /* Flush cached data of a writing file */\r
+FRESULT f_unlink (const TCHAR*); /* Delete an existing file or directory */\r
+FRESULT f_mkdir (const TCHAR*); /* Create a new directory */\r
+FRESULT f_chmod (const TCHAR*, BYTE, BYTE); /* Change attriburte of the file/dir */\r
+FRESULT f_utime (const TCHAR*, const FILINFO*); /* Change timestamp of the file/dir */\r
+FRESULT f_rename (const TCHAR*, const TCHAR*); /* Rename/Move a file or directory */\r
+FRESULT f_forward (FIL_t*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */\r
+FRESULT f_mkfs (BYTE, BYTE, UINT); /* Create a file system on the drive */\r
+FRESULT f_chdrive (BYTE); /* Change current drive */\r
+FRESULT f_chdir (const TCHAR*); /* Change current directory */\r
+FRESULT f_getcwd (TCHAR*, UINT); /* Get current directory */\r
+int f_putc (TCHAR, FIL_t*); /* Put a character to the file */\r
+int f_puts (const TCHAR*, FIL_t*); /* Put a string to the file */\r
+int f_printf (FIL_t*, const TCHAR*, ...); /* Put a formatted string to the file */\r
+TCHAR* f_gets (TCHAR*, int, FIL_t*); /* Get a string from the file */\r
+\r
+#ifndef EOF\r
+#define EOF (-1)\r
+#endif\r
+\r
+#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)\r
+#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)\r
+#define f_tell(fp) ((fp)->fptr)\r
+#define f_size(fp) ((fp)->fsize)\r
+\r
+\r
+\r
+\r
+/*--------------------------------------------------------------*/\r
+/* Additional user defined functions */\r
+\r
+/* RTC function */\r
+#if !_FS_READONLY\r
+DWORD get_fattime (void);\r
+#endif\r
+\r
+/* Unicode support functions */\r
+#if _USE_LFN /* Unicode - OEM code conversion */\r
+WCHAR ff_convert (WCHAR, UINT); /* OEM-Unicode bidirectional conversion */\r
+WCHAR ff_wtoupper (WCHAR); /* Unicode upper-case conversion */\r
+#if _USE_LFN == 3 /* Memory functions */\r
+void* ff_memalloc (UINT); /* Allocate memory block */\r
+void ff_memfree (void*); /* Free memory block */\r
+#endif\r
+#endif\r
+\r
+/* Sync functions */\r
+#if _FS_REENTRANT\r
+int ff_cre_syncobj (BYTE, _SYNC_t*);/* Create a sync object */\r
+int ff_req_grant (_SYNC_t); /* Lock sync object */\r
+void ff_rel_grant (_SYNC_t); /* Unlock sync object */\r
+int ff_del_syncobj (_SYNC_t); /* Delete a sync object */\r
+#endif\r
+\r
+\r
+\r
+\r
+/*--------------------------------------------------------------*/\r
+/* Flags and offset address */\r
+\r
+\r
+/* File access control and file status flags (FIL.flag) */\r
+\r
+#define FA_READ 0x01\r
+#define FA_OPEN_EXISTING 0x00\r
+#define FA__ERROR 0x80\r
+\r
+#if !_FS_READONLY\r
+#define FA_WRITE 0x02\r
+#define FA_CREATE_NEW 0x04\r
+#define FA_CREATE_ALWAYS 0x08\r
+#define FA_OPEN_ALWAYS 0x10\r
+#define FA__WRITTEN 0x20\r
+#define FA__DIRTY 0x40\r
+#endif\r
+\r
+\r
+/* FAT sub type (FATFS.fs_type) */\r
+\r
+#define FS_FAT12 1\r
+#define FS_FAT16 2\r
+#define FS_FAT32 3\r
+\r
+\r
+/* File attribute bits for directory entry */\r
+\r
+#define AM_RDO 0x01 /* Read only */\r
+#define AM_HID 0x02 /* Hidden */\r
+#define AM_SYS 0x04 /* System */\r
+#define AM_VOL 0x08 /* Volume label */\r
+#define AM_LFN 0x0F /* LFN entry */\r
+#define AM_DIR 0x10 /* Directory */\r
+#define AM_ARC 0x20 /* Archive */\r
+#define AM_MASK 0x3F /* Mask of defined bits */\r
+\r
+\r
+/* Fast seek function */\r
+#define CREATE_LINKMAP 0xFFFFFFFF\r
+\r
+\r
+\r
+/*--------------------------------*/\r
+/* Multi-byte word access macros */\r
+\r
+#if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */\r
+#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))\r
+#define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))\r
+#define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)\r
+#define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)\r
+#else /* Use byte-by-byte access to the FAT structure */\r
+#define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))\r
+#define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))\r
+#define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)\r
+#define ST_DWORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)\r
+#endif\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* _FATFS */\r
--- /dev/null
+/*---------------------------------------------------------------------------/\r
+/ FatFs - FAT file system module configuration file R0.08b (C)ChaN, 2011\r
+/----------------------------------------------------------------------------/\r
+/\r
+/ CAUTION! Do not forget to make clean the project after any changes to\r
+/ the configuration options.\r
+/\r
+/----------------------------------------------------------------------------*/\r
+#ifndef _FFCONF\r
+#define _FFCONF 8237 /* Revision ID */\r
+\r
+\r
+/*---------------------------------------------------------------------------/\r
+/ Function and Buffer Configurations\r
+/----------------------------------------------------------------------------*/\r
+\r
+#define _FS_TINY 0 /* 0:Normal or 1:Tiny */\r
+/* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system\r
+/ object instead of the sector buffer in the individual file object for file\r
+/ data transfer. This reduces memory consumption 512 bytes each file object. */\r
+\r
+\r
+#define _FS_READONLY 0 /* 0:Read/Write or 1:Read only */\r
+/* Setting _FS_READONLY to 1 defines read only configuration. This removes\r
+/ writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,\r
+/ f_truncate and useless f_getfree. */\r
+\r
+\r
+#define _FS_MINIMIZE 0 /* 0 to 3 */\r
+/* The _FS_MINIMIZE option defines minimization level to remove some functions.\r
+/\r
+/ 0: Full function.\r
+/ 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename\r
+/ are removed.\r
+/ 2: f_opendir and f_readdir are removed in addition to 1.\r
+/ 3: f_lseek is removed in addition to 2. */\r
+\r
+\r
+#define _USE_STRFUNC 1 /* 0:Disable or 1/2:Enable */\r
+/* To enable string functions, set _USE_STRFUNC to 1 or 2. */\r
+\r
+\r
+#define _USE_MKFS 1 /* 0:Disable or 1:Enable */\r
+/* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */\r
+\r
+\r
+#define _USE_FORWARD 0 /* 0:Disable or 1:Enable */\r
+/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */\r
+\r
+\r
+#define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable */\r
+/* To enable fast seek feature, set _USE_FASTSEEK to 1. */\r
+\r
+\r
+\r
+/*---------------------------------------------------------------------------/\r
+/ Locale and Namespace Configurations\r
+/----------------------------------------------------------------------------*/\r
+\r
+#define _CODE_PAGE 1252\r
+/* The _CODE_PAGE specifies the OEM code page to be used on the target system.\r
+/ Incorrect setting of the code page can cause a file open failure.\r
+/\r
+/ 932 - Japanese Shift-JIS (DBCS, OEM, Windows)\r
+/ 936 - Simplified Chinese GBK (DBCS, OEM, Windows)\r
+/ 949 - Korean (DBCS, OEM, Windows)\r
+/ 950 - Traditional Chinese Big5 (DBCS, OEM, Windows)\r
+/ 1250 - Central Europe (Windows)\r
+/ 1251 - Cyrillic (Windows)\r
+/ 1252 - Latin 1 (Windows)\r
+/ 1253 - Greek (Windows)\r
+/ 1254 - Turkish (Windows)\r
+/ 1255 - Hebrew (Windows)\r
+/ 1256 - Arabic (Windows)\r
+/ 1257 - Baltic (Windows)\r
+/ 1258 - Vietnam (OEM, Windows)\r
+/ 437 - U.S. (OEM)\r
+/ 720 - Arabic (OEM)\r
+/ 737 - Greek (OEM)\r
+/ 775 - Baltic (OEM)\r
+/ 850 - Multilingual Latin 1 (OEM)\r
+/ 858 - Multilingual Latin 1 + Euro (OEM)\r
+/ 852 - Latin 2 (OEM)\r
+/ 855 - Cyrillic (OEM)\r
+/ 866 - Russian (OEM)\r
+/ 857 - Turkish (OEM)\r
+/ 862 - Hebrew (OEM)\r
+/ 874 - Thai (OEM, Windows)\r
+/ 1 - ASCII only (Valid for non LFN cfg.)\r
+*/\r
+\r
+\r
+#define _USE_LFN 1 /* 0 to 3 */\r
+#define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */\r
+/* The _USE_LFN option switches the LFN support.\r
+/\r
+/ 0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.\r
+/ 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant.\r
+/ 2: Enable LFN with dynamic working buffer on the STACK.\r
+/ 3: Enable LFN with dynamic working buffer on the HEAP.\r
+/\r
+/ The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN,\r
+/ Unicode handling functions ff_convert() and ff_wtoupper() must be added\r
+/ to the project. When enable to use heap, memory control functions\r
+/ ff_memalloc() and ff_memfree() must be added to the project. */\r
+\r
+\r
+#define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */\r
+/* To switch the character code set on FatFs API to Unicode,\r
+/ enable LFN feature and set _LFN_UNICODE to 1. */\r
+\r
+\r
+#define _FS_RPATH 0 /* 0 to 2 */\r
+/* The _FS_RPATH option configures relative path feature.\r
+/\r
+/ 0: Disable relative path feature and remove related functions.\r
+/ 1: Enable relative path. f_chdrive() and f_chdir() are available.\r
+/ 2: f_getcwd() is available in addition to 1.\r
+/\r
+/ Note that output of the f_readdir fnction is affected by this option. */\r
+\r
+\r
+\r
+/*---------------------------------------------------------------------------/\r
+/ Physical Drive Configurations\r
+/----------------------------------------------------------------------------*/\r
+\r
+#define _VOLUMES 1\r
+/* Number of volumes (logical drives) to be used. */\r
+\r
+\r
+#define _MAX_SS 512 /* 512, 1024, 2048 or 4096 */\r
+/* Maximum sector size to be handled.\r
+/ Always set 512 for memory card and hard disk but a larger value may be\r
+/ required for on-board flash memory, floppy disk and optical disk.\r
+/ When _MAX_SS is larger than 512, it configures FatFs to variable sector size\r
+/ and GET_SECTOR_SIZE command must be implememted to the disk_ioctl function. */\r
+\r
+\r
+#define _MULTI_PARTITION 0 /* 0:Single partition or 1:Multiple partition */\r
+/* When set to 0, each volume is bound to the same physical drive number and\r
+/ it can mount only first primaly partition. When it is set to 1, each volume\r
+/ is tied to the partitions listed in VolToPart[]. */\r
+\r
+\r
+#define _USE_ERASE 0 /* 0:Disable or 1:Enable */\r
+/* To enable sector erase feature, set _USE_ERASE to 1. CTRL_ERASE_SECTOR command\r
+/ should be added to the disk_ioctl functio. */\r
+\r
+\r
+\r
+/*---------------------------------------------------------------------------/\r
+/ System Configurations\r
+/----------------------------------------------------------------------------*/\r
+\r
+#define _WORD_ACCESS 0 /* 0 or 1 */\r
+/* Set 0 first and it is always compatible with all platforms. The _WORD_ACCESS\r
+/ option defines which access method is used to the word data on the FAT volume.\r
+/\r
+/ 0: Byte-by-byte access.\r
+/ 1: Word access. Do not choose this unless following condition is met.\r
+/\r
+/ When the byte order on the memory is big-endian or address miss-aligned word\r
+/ access results incorrect behavior, the _WORD_ACCESS must be set to 0.\r
+/ If it is not the case, the value can also be set to 1 to improve the\r
+/ performance and code size. */\r
+\r
+\r
+/* A header file that defines sync object types on the O/S, such as\r
+/ windows.h, ucos_ii.h and semphr.h, must be included prior to ff.h. */\r
+\r
+#define _FS_REENTRANT 0 /* 0:Disable or 1:Enable */\r
+#define _FS_TIMEOUT 1000 /* Timeout period in unit of time ticks */\r
+#define _SYNC_t HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */\r
+\r
+/* The _FS_REENTRANT option switches the reentrancy (thread safe) of the FatFs module.\r
+/\r
+/ 0: Disable reentrancy. _SYNC_t and _FS_TIMEOUT have no effect.\r
+/ 1: Enable reentrancy. Also user provided synchronization handlers,\r
+/ ff_req_grant, ff_rel_grant, ff_del_syncobj and ff_cre_syncobj\r
+/ function must be added to the project. */\r
+\r
+\r
+#define _FS_SHARE 0 /* 0:Disable or >=1:Enable */\r
+/* To enable file shareing feature, set _FS_SHARE to 1 or greater. The value\r
+ defines how many files can be opened simultaneously. */\r
+\r
+\r
+#endif /* _FFCONFIG */\r
--- /dev/null
+/*-------------------------------------------*/\r
+/* Integer type definitions for FatFs module */\r
+/*-------------------------------------------*/\r
+\r
+#ifndef _INTEGER\r
+#define _INTEGER\r
+\r
+#ifdef _WIN32 /* FatFs development platform */\r
+\r
+#include <windows.h>\r
+#include <tchar.h>\r
+\r
+#else /* Embedded platform */\r
+\r
+/* These types must be 16-bit, 32-bit or larger integer */\r
+typedef int INT;\r
+typedef unsigned int UINT;\r
+\r
+/* These types must be 8-bit integer */\r
+typedef char CHAR;\r
+typedef unsigned char UCHAR;\r
+typedef unsigned char BYTE;\r
+\r
+/* These types must be 16-bit integer */\r
+typedef short SHORT;\r
+typedef unsigned short USHORT;\r
+typedef unsigned short WORD;\r
+typedef unsigned short WCHAR;\r
+\r
+/* These types must be 32-bit integer */\r
+typedef long LONG;\r
+typedef unsigned long ULONG;\r
+typedef unsigned long DWORD;\r
+\r
+#endif\r
+\r
+#endif\r
--- /dev/null
+/*------------------------------------------------------------------------*/\r
+/* Unicode - Local code bidirectional converter (C)ChaN, 2009 */\r
+/* (SBCS code pages) */\r
+/*------------------------------------------------------------------------*/\r
+/* 437 U.S. (OEM)\r
+/ 720 Arabic (OEM)\r
+/ 1256 Arabic (Windows)\r
+/ 737 Greek (OEM)\r
+/ 1253 Greek (Windows)\r
+/ 1250 Central Europe (Windows)\r
+/ 775 Baltic (OEM)\r
+/ 1257 Baltic (Windows)\r
+/ 850 Multilingual Latin 1 (OEM)\r
+/ 852 Latin 2 (OEM)\r
+/ 1252 Latin 1 (Windows)\r
+/ 855 Cyrillic (OEM)\r
+/ 1251 Cyrillic (Windows)\r
+/ 866 Russian (OEM)\r
+/ 857 Turkish (OEM)\r
+/ 1254 Turkish (Windows)\r
+/ 858 Multilingual Latin 1 + Euro (OEM)\r
+/ 862 Hebrew (OEM)\r
+/ 1255 Hebrew (Windows)\r
+/ 874 Thai (OEM, Windows)\r
+/ 1258 Vietnam (OEM, Windows)\r
+*/\r
+\r
+#include "../ff.h"\r
+\r
+\r
+#if _CODE_PAGE == 437\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP437(0x80-0xFF) to Unicode conversion table */\r
+ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,\r
+ 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,\r
+ 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,\r
+ 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,\r
+ 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,\r
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,\r
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,\r
+ 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,\r
+ 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,\r
+ 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,\r
+ 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 720\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP720(0x80-0xFF) to Unicode conversion table */\r
+ 0x0000, 0x0000, 0x00E9, 0x00E2, 0x0000, 0x00E0, 0x0000, 0x00E7,\r
+ 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0651, 0x0652, 0x00F4, 0x00A4, 0x0640, 0x00FB, 0x00F9,\r
+ 0x0621, 0x0622, 0x0623, 0x0624, 0x00A3, 0x0625, 0x0626, 0x0627,\r
+ 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,\r
+ 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,\r
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,\r
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,\r
+ 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x0636, 0x0637, 0x0638, 0x0639, 0x063A, 0x0641, 0x00B5, 0x0642,\r
+ 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, 0x0648, 0x0649, 0x064A,\r
+ 0x2261, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F, 0xO650, 0x2248,\r
+ 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 737\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP737(0x80-0xFF) to Unicode conversion table */\r
+ 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398,\r
+ 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0,\r
+ 0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9,\r
+ 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8,\r
+ 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0,\r
+ 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,\r
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,\r
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,\r
+ 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD,\r
+ 0x03CB, 0x03CE, 0x0386, 0x0388, 0x0389, 0x038A, 0x038C, 0x038E,\r
+ 0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248,\r
+ 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 775\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP775(0x80-0xFF) to Unicode conversion table */\r
+ 0x0106, 0x00FC, 0x00E9, 0x0101, 0x00E4, 0x0123, 0x00E5, 0x0107,\r
+ 0x0142, 0x0113, 0x0156, 0x0157, 0x012B, 0x0179, 0x00C4, 0x00C5,\r
+ 0x00C9, 0x00E6, 0x00C6, 0x014D, 0x00F6, 0x0122, 0x00A2, 0x015A,\r
+ 0x015B, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x00A4,\r
+ 0x0100, 0x012A, 0x00F3, 0x017B, 0x017C, 0x017A, 0x201D, 0x00A6,\r
+ 0x00A9, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x0141, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010C, 0x0118,\r
+ 0x0116, 0x2563, 0x2551, 0x2557, 0x255D, 0x012E, 0x0160, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0172, 0x016A,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x017D,\r
+ 0x0105, 0x010D, 0x0119, 0x0117, 0x012F, 0x0161, 0x0173, 0x016B,\r
+ 0x017E, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x00D3, 0x00DF, 0x014C, 0x0143, 0x00F5, 0x00D5, 0x00B5, 0x0144,\r
+ 0x0136, 0x0137, 0x013B, 0x013C, 0x0146, 0x0112, 0x0145, 0x2019,\r
+ 0x00AD, 0x00B1, 0x201C, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x201E,\r
+ 0x00B0, 0x2219, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 850\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP850(0x80-0xFF) to Unicode conversion table */\r
+ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,\r
+ 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,\r
+ 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,\r
+ 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,\r
+ 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0,\r
+ 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,\r
+ 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE,\r
+ 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,\r
+ 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE,\r
+ 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,\r
+ 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8,\r
+ 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 852\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP852(0x80-0xFF) to Unicode conversion table */\r
+ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7,\r
+ 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106,\r
+ 0x00C9, 0x0139, 0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A,\r
+ 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x0104, 0x0105, 0x017D, 0x017E,\r
+ 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x011A,\r
+ 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,\r
+ 0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE,\r
+ 0x011B, 0x2518, 0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580,\r
+ 0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161,\r
+ 0x0154, 0x00DA, 0x0155, 0x0170, 0x00FD, 0x00DD, 0x0163, 0x00B4,\r
+ 0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8,\r
+ 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 855\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP855(0x80-0xFF) to Unicode conversion table */\r
+ 0x0452, 0x0402, 0x0453, 0x0403, 0x0451, 0x0401, 0x0454, 0x0404,\r
+ 0x0455, 0x0405, 0x0456, 0x0406, 0x0457, 0x0407, 0x0458, 0x0408,\r
+ 0x0459, 0x0409, 0x045A, 0x040A, 0x045B, 0x040B, 0x045C, 0x040C,\r
+ 0x045E, 0x040E, 0x045F, 0x040F, 0x044E, 0x042E, 0x044A, 0x042A,\r
+ 0x0430, 0x0410, 0x0431, 0x0411, 0x0446, 0x0426, 0x0434, 0x0414,\r
+ 0x0435, 0x0415, 0x0444, 0x0424, 0x0433, 0x0413, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0445, 0x0425, 0x0438,\r
+ 0x0418, 0x2563, 0x2551, 0x2557, 0x255D, 0x0439, 0x0419, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x043A, 0x041A,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,\r
+ 0x043B, 0x041B, 0x043C, 0x041C, 0x043D, 0x041D, 0x043E, 0x041E,\r
+ 0x043F, 0x2518, 0x250C, 0x2588, 0x2584, 0x041F, 0x044F, 0x2580,\r
+ 0x042F, 0x0440, 0x0420, 0x0441, 0x0421, 0x0442, 0x0422, 0x0443,\r
+ 0x0423, 0x0436, 0x0416, 0x0432, 0x0412, 0x044C, 0x042C, 0x2116,\r
+ 0x00AD, 0x044B, 0x042B, 0x0437, 0x0417, 0x0448, 0x0428, 0x044D,\r
+ 0x042D, 0x0449, 0x0429, 0x0447, 0x0427, 0x00A7, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 857\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP857(0x80-0xFF) to Unicode conversion table */\r
+ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,\r
+ 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5,\r
+ 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,\r
+ 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x011E, 0x011F,\r
+ 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0,\r
+ 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,\r
+ 0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0x0000, 0x00CD, 0x00CE,\r
+ 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,\r
+ 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x0000,\r
+ 0x00D7, 0x00DA, 0x00DB, 0x00D9, 0x00EC, 0x00FF, 0x00AF, 0x00B4,\r
+ 0x00AD, 0x00B1, 0x0000, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8,\r
+ 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 858\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP858(0x80-0xFF) to Unicode conversion table */\r
+ 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,\r
+ 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,\r
+ 0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,\r
+ 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,\r
+ 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0,\r
+ 0x00A9, 0x2563, 0x2551, 0x2557, 0x2550, 0x00A2, 0x00A5, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,\r
+ 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x20AC, 0x00CD, 0x00CE,\r
+ 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00C6, 0x00CC, 0x2580,\r
+ 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE,\r
+ 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,\r
+ 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8,\r
+ 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 862\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP862(0x80-0xFF) to Unicode conversion table */\r
+ 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,\r
+ 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,\r
+ 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,\r
+ 0x05E8, 0x05E9, 0x05EA, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,\r
+ 0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,\r
+ 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,\r
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,\r
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,\r
+ 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,\r
+ 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,\r
+ 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,\r
+ 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 866\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP866(0x80-0xFF) to Unicode conversion table */\r
+ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,\r
+ 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,\r
+ 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,\r
+ 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,\r
+ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,\r
+ 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,\r
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,\r
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,\r
+ 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,\r
+ 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,\r
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,\r
+ 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,\r
+ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,\r
+ 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,\r
+ 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E,\r
+ 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0\r
+};\r
+\r
+#elif _CODE_PAGE == 874\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP874(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x0000, 0x0000, 0x0000, 0x2026, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07,\r
+ 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F,\r
+ 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17,\r
+ 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F,\r
+ 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27,\r
+ 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F,\r
+ 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37,\r
+ 0x0E38, 0x0E39, 0x0E3A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0E3F,\r
+ 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47,\r
+ 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F,\r
+ 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57,\r
+ 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0x0000, 0x0000, 0x0000, 0x0000\r
+};\r
+\r
+#elif _CODE_PAGE == 1250\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1250(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x0000, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x0000, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A,\r
+ 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B,\r
+ 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C,\r
+ 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,\r
+ 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,\r
+ 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,\r
+ 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,\r
+ 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,\r
+ 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,\r
+ 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,\r
+ 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9\r
+};\r
+\r
+#elif _CODE_PAGE == 1251\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1251(0x80-0xFF) to Unicode conversion table */\r
+ 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,\r
+ 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x0000, 0x2111, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,\r
+ 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,\r
+ 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,\r
+ 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,\r
+ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,\r
+ 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,\r
+ 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,\r
+ 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,\r
+ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,\r
+ 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,\r
+ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,\r
+ 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F\r
+};\r
+\r
+#elif _CODE_PAGE == 1252\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1252(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0000, 0x017D, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x0000, 0x017E, 0x0178,\r
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,\r
+ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,\r
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,\r
+ 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,\r
+ 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,\r
+ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,\r
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,\r
+ 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,\r
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF\r
+};\r
+\r
+#elif _CODE_PAGE == 1253\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1253(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x0000, 0x2030, 0x0000, 0x2039, 0x000C, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x0000, 0x2122, 0x0000, 0x203A, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x0000, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,\r
+ 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,\r
+ 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,\r
+ 0x03A0, 0x03A1, 0x0000, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7,\r
+ 0x03A8, 0x03A9, 0x03AA, 0x03AD, 0x03AC, 0x03AD, 0x03AE, 0x03AF,\r
+ 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,\r
+ 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,\r
+ 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,\r
+ 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0x0000\r
+};\r
+\r
+#elif _CODE_PAGE == 1254\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1254(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x210A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x0000, 0x0000, 0x0178,\r
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,\r
+ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,\r
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,\r
+ 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,\r
+ 0x00D8, 0x00D9, 0x00DA, 0x00BD, 0x00DC, 0x0130, 0x015E, 0x00DF,\r
+ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,\r
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,\r
+ 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,\r
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF\r
+};\r
+\r
+#elif _CODE_PAGE == 1255\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1255(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x02C6, 0x2030, 0x0000, 0x2039, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x02DC, 0x2122, 0x0000, 0x203A, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,\r
+ 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,\r
+ 0x05B8, 0x05B9, 0x0000, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,\r
+ 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3,\r
+ 0x05F4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,\r
+ 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,\r
+ 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,\r
+ 0x05E8, 0x05E9, 0x05EA, 0x0000, 0x0000, 0x200E, 0x200F, 0x0000\r
+};\r
+\r
+#elif _CODE_PAGE == 1256\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1256(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,\r
+ 0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA,\r
+ 0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F,\r
+ 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,\r
+ 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,\r
+ 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7,\r
+ 0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0640, 0x0642, 0x0643,\r
+ 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7,\r
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,\r
+ 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7,\r
+ 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2\r
+}\r
+\r
+#elif _CODE_PAGE == 1257\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1257(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0000, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x0000, 0x2030, 0x0000, 0x2039, 0x0000, 0x00A8, 0x02C7, 0x00B8,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x0000, 0x2122, 0x0000, 0x203A, 0x0000, 0x00AF, 0x02DB, 0x0000,\r
+ 0x00A0, 0x0000, 0x00A2, 0x00A3, 0x00A4, 0x0000, 0x00A6, 0x00A7,\r
+ 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,\r
+ 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,\r
+ 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,\r
+ 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7,\r
+ 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,\r
+ 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113,\r
+ 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,\r
+ 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,\r
+ 0x0173, 0x014E, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9\r
+};\r
+\r
+#elif _CODE_PAGE == 1258\r
+#define _TBLDEF 1\r
+static\r
+const WCHAR Tbl[] = { /* CP1258(0x80-0xFF) to Unicode conversion table */\r
+ 0x20AC, 0x0000, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,\r
+ 0x02C6, 0x2030, 0x0000, 0x2039, 0x0152, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,\r
+ 0x02DC, 0x2122, 0x0000, 0x203A, 0x0153, 0x0000, 0x0000, 0x0178,\r
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,\r
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,\r
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,\r
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,\r
+ 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,\r
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF,\r
+ 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7,\r
+ 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF,\r
+ 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7,\r
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF,\r
+ 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7,\r
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF\r
+};\r
+\r
+#endif\r
+\r
+\r
+#if !_TBLDEF || !_USE_LFN\r
+#error This file is not needed in current configuration. Remove from the project.\r
+#endif\r
+\r
+\r
+WCHAR ff_convert ( /* Converted character, Returns zero on error */\r
+ WCHAR src, /* Character code to be converted */\r
+ UINT dir /* 0: Unicode to OEMCP, 1: OEMCP to Unicode */\r
+)\r
+{\r
+ WCHAR c;\r
+\r
+\r
+ if (src < 0x80) { /* ASCII */\r
+ c = src;\r
+\r
+ } else {\r
+ if (dir) { /* OEMCP to Unicode */\r
+ c = (src >= 0x100) ? 0 : Tbl[src - 0x80];\r
+\r
+ } else { /* Unicode to OEMCP */\r
+ for (c = 0; c < 0x80; c++) {\r
+ if (src == Tbl[c]) break;\r
+ }\r
+ c = (c + 0x80) & 0xFF;\r
+ }\r
+ }\r
+\r
+ return c;\r
+}\r
+\r
+\r
+WCHAR ff_wtoupper ( /* Upper converted character */\r
+ WCHAR chr /* Input character */\r
+)\r
+{\r
+ static const WCHAR tbl_lower[] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0xA1, 0x00A2, 0x00A3, 0x00A5, 0x00AC, 0x00AF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0x0FF, 0x101, 0x103, 0x105, 0x107, 0x109, 0x10B, 0x10D, 0x10F, 0x111, 0x113, 0x115, 0x117, 0x119, 0x11B, 0x11D, 0x11F, 0x121, 0x123, 0x125, 0x127, 0x129, 0x12B, 0x12D, 0x12F, 0x131, 0x133, 0x135, 0x137, 0x13A, 0x13C, 0x13E, 0x140, 0x142, 0x144, 0x146, 0x148, 0x14B, 0x14D, 0x14F, 0x151, 0x153, 0x155, 0x157, 0x159, 0x15B, 0x15D, 0x15F, 0x161, 0x163, 0x165, 0x167, 0x169, 0x16B, 0x16D, 0x16F, 0x171, 0x173, 0x175, 0x177, 0x17A, 0x17C, 0x17E, 0x192, 0x3B1, 0x3B2, 0x3B3, 0x3B4, 0x3B5, 0x3B6, 0x3B7, 0x3B8, 0x3B9, 0x3BA, 0x3BB, 0x3BC, 0x3BD, 0x3BE, 0x3BF, 0x3C0, 0x3C1, 0x3C3, 0x3C4, 0x3C5, 0x3C6, 0x3C7, 0x3C8, 0x3C9, 0x3CA, 0x430, 0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F, 0x440, 0x441, 0x442, 0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, 0x44C, 0x44D, 0x44E, 0x44F, 0x451, 0x452, 0x453, 0x454, 0x455, 0x456, 0x457, 0x458, 0x459, 0x45A, 0x45B, 0x45C, 0x45E, 0x45F, 0x2170, 0x2171, 0x2172, 0x2173, 0x2174, 0x2175, 0x2176, 0x2177, 0x2178, 0x2179, 0x217A, 0x217B, 0x217C, 0x217D, 0x217E, 0x217F, 0xFF41, 0xFF42, 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47, 0xFF48, 0xFF49, 0xFF4A, 0xFF4B, 0xFF4C, 0xFF4D, 0xFF4E, 0xFF4F, 0xFF50, 0xFF51, 0xFF52, 0xFF53, 0xFF54, 0xFF55, 0xFF56, 0xFF57, 0xFF58, 0xFF59, 0xFF5A, 0 };\r
+ static const WCHAR tbl_upper[] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x21, 0xFFE0, 0xFFE1, 0xFFE5, 0xFFE2, 0xFFE3, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0x178, 0x100, 0x102, 0x104, 0x106, 0x108, 0x10A, 0x10C, 0x10E, 0x110, 0x112, 0x114, 0x116, 0x118, 0x11A, 0x11C, 0x11E, 0x120, 0x122, 0x124, 0x126, 0x128, 0x12A, 0x12C, 0x12E, 0x130, 0x132, 0x134, 0x136, 0x139, 0x13B, 0x13D, 0x13F, 0x141, 0x143, 0x145, 0x147, 0x14A, 0x14C, 0x14E, 0x150, 0x152, 0x154, 0x156, 0x158, 0x15A, 0x15C, 0x15E, 0x160, 0x162, 0x164, 0x166, 0x168, 0x16A, 0x16C, 0x16E, 0x170, 0x172, 0x174, 0x176, 0x179, 0x17B, 0x17D, 0x191, 0x391, 0x392, 0x393, 0x394, 0x395, 0x396, 0x397, 0x398, 0x399, 0x39A, 0x39B, 0x39C, 0x39D, 0x39E, 0x39F, 0x3A0, 0x3A1, 0x3A3, 0x3A4, 0x3A5, 0x3A6, 0x3A7, 0x3A8, 0x3A9, 0x3AA, 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E, 0x41F, 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, 0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F, 0x401, 0x402, 0x403, 0x404, 0x405, 0x406, 0x407, 0x408, 0x409, 0x40A, 0x40B, 0x40C, 0x40E, 0x40F, 0x2160, 0x2161, 0x2162, 0x2163, 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216A, 0x216B, 0x216C, 0x216D, 0x216E, 0x216F, 0xFF21, 0xFF22, 0xFF23, 0xFF24, 0xFF25, 0xFF26, 0xFF27, 0xFF28, 0xFF29, 0xFF2A, 0xFF2B, 0xFF2C, 0xFF2D, 0xFF2E, 0xFF2F, 0xFF30, 0xFF31, 0xFF32, 0xFF33, 0xFF34, 0xFF35, 0xFF36, 0xFF37, 0xFF38, 0xFF39, 0xFF3A, 0 };\r
+ int i;\r
+\r
+\r
+ for (i = 0; tbl_lower[i] && chr != tbl_lower[i]; i++) ;\r
+\r
+ return tbl_lower[i] ? tbl_upper[i] : chr;\r
+}\r
--- /dev/null
+/* mbed Microcontroller Library - FATDirHandle
+ * Copyright (c) 2008, sford
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "ff.h"
+#include "FATDirHandle.h"
+#include "FATFileSystem.h"
+
+namespace mbed {
+
+FATDirHandle::FATDirHandle(const DIR_t &the_dir) {
+ dir = the_dir;
+}
+
+int FATDirHandle::closedir() {
+ delete this;
+ return 0;
+}
+
+struct dirent *FATDirHandle::readdir() {
+ FILINFO finfo;
+#if _USE_LFN
+ static char lfn[_MAX_LFN * (_LFN_UNICODE ? 2 : 1) + 1];
+ finfo.lfname = lfn;
+ finfo.lfsize = sizeof(lfn);
+#endif
+ FRESULT res = f_readdir(&dir, &finfo);
+ if(res != 0 || finfo.fname[0]==0) {
+ return NULL;
+ } else {
+ char* fn;
+ int stringSize = 0;
+#if _USE_LFN
+ fn = *finfo.lfname ? finfo.lfname : finfo.fname;
+ stringSize = finfo.lfsize;
+#else
+ fn = fno.fname;
+ stringSize = sizeof(finfo.fname);
+#endif
+ memcpy(cur_entry.d_name, fn, stringSize);
+ return &cur_entry;
+ }
+}
+
+void FATDirHandle::rewinddir() {
+ dir.index = 0;
+}
+
+off_t FATDirHandle::telldir() {
+ return dir.index;
+}
+
+void FATDirHandle::seekdir(off_t location) {
+ dir.index = location;
+}
+
+}
+
--- /dev/null
+/* mbed Microcontroller Library - FATDirHandle\r
+ * Copyright (c) 2008, sford\r
+ */\r
+\r
+#ifndef MBED_FATDIRHANDLE_H\r
+#define MBED_FATDIRHANDLE_H\r
+\r
+#include "DirHandle.h"\r
+#include "ff.h"\r
+\r
+namespace mbed {\r
+\r
+class FATDirHandle : public DirHandle {\r
+\r
+ public:\r
+ FATDirHandle(const DIR_t &the_dir);\r
+ virtual int closedir();\r
+ virtual struct dirent *readdir();\r
+ virtual void rewinddir();\r
+ virtual off_t telldir();\r
+ virtual void seekdir(off_t location);\r
+\r
+ private:\r
+ DIR_t dir;\r
+ struct dirent cur_entry;\r
+\r
+};\r
+\r
+}\r
+\r
+#endif\r
--- /dev/null
+/* mbed Microcontroller Library - FATFileHandle
+ * Copyright (c) 2008, sford
+ */
+
+#include "FATFileHandle.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "ff.h"
+#include "FATFileSystem.h"
+
+namespace mbed {
+
+#if FFSDEBUG_ENABLED
+static const char *FR_ERRORS[] = {
+ "FR_OK = 0",
+ "FR_NOT_READY",
+ "FR_NO_FILE",
+ "FR_NO_PATH",
+ "FR_INVALID_NAME",
+ "FR_INVALID_DRIVE",
+ "FR_DENIED",
+ "FR_EXIST",
+ "FR_RW_ERROR",
+ "FR_WRITE_PROTECTED",
+ "FR_NOT_ENABLED",
+ "FR_NO_FILESYSTEM",
+ "FR_INVALID_OBJECT",
+ "FR_MKFS_ABORTED"
+};
+#endif
+
+FATFileHandle::FATFileHandle(FIL_t fh) {
+ _fh = fh;
+}
+
+int FATFileHandle::close() {
+ FFSDEBUG("close\n");
+ int retval = f_close(&_fh);
+ delete this;
+ return retval;
+}
+
+ssize_t FATFileHandle::write(const void* buffer, size_t length) {
+ FFSDEBUG("write(%d)\n", length);
+ UINT n;
+ FRESULT res = f_write(&_fh, buffer, length, &n);
+ if(res) {
+ FFSDEBUG("f_write() failed (%d, %s)", res, FR_ERRORS[res]);
+ return -1;
+ }
+ return n;
+}
+
+ssize_t FATFileHandle::read(void* buffer, size_t length) {
+ FFSDEBUG("read(%d)\n", length);
+ UINT n;
+ FRESULT res = f_read(&_fh, buffer, length, &n);
+ if(res) {
+ FFSDEBUG("f_read() failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return -1;
+ }
+ return n;
+}
+
+int FATFileHandle::isatty() {
+ return 0;
+}
+
+off_t FATFileHandle::lseek(off_t position, int whence) {
+ FFSDEBUG("lseek(%i,%i)\n",position,whence);
+ if(whence == SEEK_END) {
+ position += _fh.fsize;
+ } else if(whence==SEEK_CUR) {
+ position += _fh.fptr;
+ }
+ FRESULT res = f_lseek(&_fh, position);
+ if(res) {
+ FFSDEBUG("lseek failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return -1;
+ } else {
+ FFSDEBUG("lseek OK, returning %i\n", _fh.fptr);
+ return _fh.fptr;
+ }
+}
+
+int FATFileHandle::fsync() {
+ FFSDEBUG("fsync()\n");
+ FRESULT res = f_sync(&_fh);
+ if (res) {
+ FFSDEBUG("f_sync() failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return -1;
+ }
+ return 0;
+}
+
+off_t FATFileHandle::flen() {
+ FFSDEBUG("flen\n");
+ return _fh.fsize;
+}
+
+} // namespace mbed
--- /dev/null
+/* mbed Microcontroller Library - FATFileHandle\r
+ * Copyright (c) 2008, sford\r
+ */\r
+\r
+#ifndef MBED_FATFILEHANDLE_H\r
+#define MBED_FATFILEHANDLE_H\r
+\r
+#include "FileHandle.h"\r
+#include "ff.h"\r
+\r
+namespace mbed {\r
+\r
+class FATFileHandle : public FileHandle {\r
+public:\r
+\r
+ FATFileHandle(FIL_t fh);\r
+ virtual int close();\r
+ virtual ssize_t write(const void* buffer, size_t length);\r
+ virtual ssize_t read(void* buffer, size_t length);\r
+ virtual int isatty();\r
+ virtual off_t lseek(off_t position, int whence);\r
+ virtual int fsync();\r
+ virtual off_t flen();\r
+\r
+protected:\r
+\r
+ FIL_t _fh;\r
+\r
+};\r
+\r
+}\r
+\r
+#endif\r
--- /dev/null
+/* mbed Microcontroller Library - FATFileSystem
+ * Copyright (c) 2008, sford
+ */
+
+#include "FATFileSystem.h"
+
+#include "mbed.h"
+
+#include "FileSystemLike.h"
+#include "FATFileHandle.h"
+#include "FATDirHandle.h"
+#include "ff.h"
+//#include "Debug.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+DWORD get_fattime (void) {
+ return 999;
+}
+
+namespace mbed {
+
+#if FFSDEBUG_ENABLED
+static const char *FR_ERRORS[] = {
+ "FR_OK = 0",
+ "FR_NOT_READY",
+ "FR_NO_FILE",
+ "FR_NO_PATH",
+ "FR_INVALID_NAME",
+ "FR_INVALID_DRIVE",
+ "FR_DENIED",
+ "FR_EXIST",
+ "FR_RW_ERROR",
+ "FR_WRITE_PROTECTED",
+ "FR_NOT_ENABLED",
+ "FR_NO_FILESYSTEM",
+ "FR_INVALID_OBJECT",
+ "FR_MKFS_ABORTED"
+};
+#endif
+
+FATFileSystem *FATFileSystem::_ffs[_DRIVES] = {0};
+
+FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
+ FFSDEBUG("FATFileSystem(%s)\n", n);
+ for(int i=0; i<_DRIVES; i++) {
+ if(_ffs[i] == 0) {
+ _ffs[i] = this;
+ _fsid = i;
+ FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
+ f_mount(i, &_fs);
+ return;
+ }
+ }
+ error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n);
+}
+
+FATFileSystem::~FATFileSystem() {
+ for(int i=0; i<_DRIVES; i++) {
+ if(_ffs[i] == this) {
+ _ffs[i] = 0;
+ f_mount(i, NULL);
+ }
+ }
+}
+
+FileHandle *FATFileSystem::open(const char* name, int flags) {
+ FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
+ char n[64];
+ sprintf(n, "%d:/%s", _fsid, name);
+
+ /* POSIX flags -> FatFS open mode */
+ BYTE openmode;
+ if(flags & O_RDWR) {
+ openmode = FA_READ|FA_WRITE;
+ } else if(flags & O_WRONLY) {
+ openmode = FA_WRITE;
+ } else {
+ openmode = FA_READ;
+ }
+ if(flags & O_CREAT) {
+ if(flags & O_TRUNC) {
+ openmode |= FA_CREATE_ALWAYS;
+ } else {
+ openmode |= FA_OPEN_ALWAYS;
+ }
+ }
+
+ FIL_t fh;
+ FRESULT res = f_open(&fh, n, openmode);
+ if(res) {
+ FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return NULL;
+ }
+ if(flags & O_APPEND) {
+ f_lseek(&fh, fh.fsize);
+ }
+ return new FATFileHandle(fh);
+}
+
+int FATFileSystem::remove(const char *filename) {
+ FRESULT res = f_unlink(filename);
+ if(res) {
+ FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return -1;
+ }
+ return 0;
+}
+
+int FATFileSystem::format() {
+ FFSDEBUG("format()\n");
+ FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
+ if(res) {
+ FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]);
+ return -1;
+ }
+ return 0;
+}
+
+DirHandle *FATFileSystem::opendir(const char *name) {
+ DIR_t dir;
+ FRESULT res = f_opendir(&dir, name);
+ if(res != 0) {
+ return NULL;
+ }
+ return new FATDirHandle(dir);
+}
+
+int FATFileSystem::mkdir(const char *name, mode_t mode) {
+ FRESULT res = f_mkdir(name);
+ return res == 0 ? 0 : -1;
+}
+
+} // namespace mbed
--- /dev/null
+/* mbed Microcontroller Library - FATFileSystem\r
+ * Copyright (c) 2008, sford\r
+ */\r
+\r
+/* Library: FATFileSystem.h\r
+ * A library of stuff to make a fat filesystem on top of a block device\r
+ */\r
+\r
+#ifndef MBED_FATFILESYSTEM_H\r
+#define MBED_FATFILESYSTEM_H\r
+\r
+#ifndef FFSDEBUG_ENABLED\r
+#define FFSDEBUG_ENABLED 0\r
+#endif\r
+\r
+#if FFSDEBUG_ENABLED\r
+#define FFSDEBUG(FMT, ...) printf(FMT, ##__VA_ARGS__)\r
+#else\r
+#define FFSDEBUG(FMT, ...)\r
+#endif\r
+\r
+#include "FileSystemLike.h"\r
+#include "FileHandle.h"\r
+#include "ff.h"\r
+#include "diskio.h"\r
+\r
+namespace mbed {\r
+/* Class: FATFileSystem\r
+ * The class itself\r
+ */\r
+class FATFileSystem : public FileSystemLike {\r
+public:\r
+\r
+ FATFileSystem(const char* n);\r
+ virtual ~FATFileSystem();\r
+ \r
+ /* Function: open\r
+ * open a file on the filesystem. never called directly\r
+ */\r
+ virtual FileHandle *open(const char* name, int flags);\r
+ virtual int remove(const char *filename);\r
+ virtual int format();\r
+ virtual DirHandle *opendir(const char *name);\r
+ virtual int mkdir(const char *name, mode_t mode);\r
+ \r
+ FATFS _fs; // Work area (file system object) for logical drive \r
+ static FATFileSystem *_ffs[_DRIVES]; // FATFileSystem objects, as parallel to FatFs drives array\r
+ int _fsid;\r
+ \r
+ virtual int disk_initialize() { return 0; }\r
+ virtual int disk_status() { return 0; }\r
+ virtual int disk_read(char *buffer, int sector) = 0;\r
+ virtual int disk_write(const char *buffer, int sector) = 0;\r
+ virtual int disk_sync() { return 0; }\r
+ virtual int disk_sectors() = 0;\r
+ \r
+};\r
+ \r
+}\r
+\r
+#endif\r
--- /dev/null
+<?xml version="1.0"?>
+<root>
+ <global/>
+ <data>
+ <created_ts>1325892529</created_ts>
+ <last_mod_ts>1325892529</last_mod_ts>
+ <is_archive>0</is_archive>
+ <is_published>1</is_published>
+ <is_svn>0</is_svn>
+ <is_module>1</is_module>
+ <is_lib>0</is_lib>
+ <url>http://mbed.org/users/NeoBelerophon/libraries/ChaNFSSD</url>
+ <url_full>http://mbed.org/users/NeoBelerophon/libraries/ChaNFSSD/llyfwh</url_full>
+ <rev>llyfwh</rev>
+ </data>
+</root>
--- /dev/null
+/* mbed SDFileSystem Library, for providing file access to SD cards\r
+ * Copyright (c) 2008-2010, sford\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ */\r
+\r
+/* Introduction\r
+ * ------------\r
+ * SD and MMC cards support a number of interfaces, but common to them all\r
+ * is one based on SPI. This is the one I'm implmenting because it means\r
+ * it is much more portable even though not so performant, and we already \r
+ * have the mbed SPI Interface!\r
+ *\r
+ * The main reference I'm using is Chapter 7, "SPI Mode" of: \r
+ * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf\r
+ *\r
+ * SPI Startup\r
+ * -----------\r
+ * The SD card powers up in SD mode. The SPI interface mode is selected by\r
+ * asserting CS low and sending the reset command (CMD0). The card will \r
+ * respond with a (R1) response.\r
+ *\r
+ * CMD8 is optionally sent to determine the voltage range supported, and \r
+ * indirectly determine whether it is a version 1.x SD/non-SD card or \r
+ * version 2.x. I'll just ignore this for now.\r
+ *\r
+ * ACMD41 is repeatedly issued to initialise the card, until "in idle"\r
+ * (bit 0) of the R1 response goes to '0', indicating it is initialised.\r
+ *\r
+ * You should also indicate whether the host supports High Capicity cards,\r
+ * and check whether the card is high capacity - i'll also ignore this\r
+ *\r
+ * SPI Protocol\r
+ * ------------\r
+ * The SD SPI protocol is based on transactions made up of 8-bit words, with\r
+ * the host starting every bus transaction by asserting the CS signal low. The\r
+ * card always responds to commands, data blocks and errors.\r
+ * \r
+ * The protocol supports a CRC, but by default it is off (except for the \r
+ * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)\r
+ * I'll leave the CRC off I think! \r
+ * \r
+ * Standard capacity cards have variable data block sizes, whereas High \r
+ * Capacity cards fix the size of data block to 512 bytes. I'll therefore\r
+ * just always use the Standard Capacity cards with a block size of 512 bytes.\r
+ * This is set with CMD16.\r
+ *\r
+ * You can read and write single blocks (CMD17, CMD25) or multiple blocks \r
+ * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When\r
+ * the card gets a read command, it responds with a response token, and then \r
+ * a data token or an error.\r
+ * \r
+ * SPI Command Format\r
+ * ------------------\r
+ * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.\r
+ *\r
+ * +---------------+------------+------------+-----------+----------+--------------+\r
+ * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |\r
+ * +---------------+------------+------------+-----------+----------+--------------+\r
+ *\r
+ * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)\r
+ *\r
+ * All Application Specific commands shall be preceded with APP_CMD (CMD55).\r
+ *\r
+ * SPI Response Format\r
+ * -------------------\r
+ * The main response format (R1) is a status byte (normally zero). Key flags:\r
+ * idle - 1 if the card is in an idle state/initialising \r
+ * cmd - 1 if an illegal command code was detected\r
+ *\r
+ * +-------------------------------------------------+\r
+ * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |\r
+ * +-------------------------------------------------+\r
+ *\r
+ * R1b is the same, except it is followed by a busy signal (zeros) until\r
+ * the first non-zero byte when it is ready again.\r
+ *\r
+ * Data Response Token\r
+ * -------------------\r
+ * Every data block written to the card is acknowledged by a byte \r
+ * response token\r
+ *\r
+ * +----------------------+\r
+ * | xxx | 0 | status | 1 |\r
+ * +----------------------+\r
+ * 010 - OK!\r
+ * 101 - CRC Error\r
+ * 110 - Write Error\r
+ *\r
+ * Single Block Read and Write\r
+ * ---------------------------\r
+ *\r
+ * Block transfers have a byte header, followed by the data, followed\r
+ * by a 16-bit CRC. In our case, the data will always be 512 bytes.\r
+ * \r
+ * +------+---------+---------+- - - -+---------+-----------+----------+\r
+ * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] | \r
+ * +------+---------+---------+- - - -+---------+-----------+----------+\r
+ */\r
+ \r
+#include "SDFileSystem.h"\r
+\r
+#define SD_COMMAND_TIMEOUT 5000\r
+\r
+SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :\r
+ FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {\r
+ _cs = 1; \r
+}\r
+\r
+#define R1_IDLE_STATE (1 << 0)\r
+#define R1_ERASE_RESET (1 << 1)\r
+#define R1_ILLEGAL_COMMAND (1 << 2)\r
+#define R1_COM_CRC_ERROR (1 << 3)\r
+#define R1_ERASE_SEQUENCE_ERROR (1 << 4)\r
+#define R1_ADDRESS_ERROR (1 << 5)\r
+#define R1_PARAMETER_ERROR (1 << 6)\r
+\r
+// Types\r
+// - v1.x Standard Capacity\r
+// - v2.x Standard Capacity\r
+// - v2.x High Capacity\r
+// - Not recognised as an SD Card\r
+\r
+#define SDCARD_FAIL 0\r
+#define SDCARD_V1 1\r
+#define SDCARD_V2 2\r
+#define SDCARD_V2HC 3\r
+\r
+int SDFileSystem::initialise_card() {\r
+ // Set to 100kHz for initialisation, and clock card with cs = 1\r
+ _spi.frequency(100000); \r
+ _cs = 1;\r
+ for(int i=0; i<16; i++) { \r
+ _spi.write(0xFF);\r
+ }\r
+\r
+ // send CMD0, should return with all zeros except IDLE STATE set (bit 0)\r
+ if(_cmd(0, 0) != R1_IDLE_STATE) { \r
+ fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");\r
+ return SDCARD_FAIL;\r
+ }\r
+\r
+ // send CMD8 to determine whther it is ver 2.x\r
+ int r = _cmd8();\r
+ if(r == R1_IDLE_STATE) {\r
+ return initialise_card_v2();\r
+ } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {\r
+ return initialise_card_v1();\r
+ } else {\r
+ fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n");\r
+ return SDCARD_FAIL;\r
+ }\r
+}\r
+\r
+int SDFileSystem::initialise_card_v1() {\r
+ for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
+ _cmd(55, 0); \r
+ if(_cmd(41, 0) == 0) { \r
+ return SDCARD_V1;\r
+ }\r
+ }\r
+\r
+ fprintf(stderr, "Timeout waiting for v1.x card\n");\r
+ return SDCARD_FAIL;\r
+}\r
+\r
+int SDFileSystem::initialise_card_v2() {\r
+ \r
+ for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
+ _cmd(55, 0); \r
+ if(_cmd(41, 0) == 0) { \r
+ _cmd58();\r
+ return SDCARD_V2;\r
+ }\r
+ }\r
+\r
+ fprintf(stderr, "Timeout waiting for v2.x card\n");\r
+ return SDCARD_FAIL;\r
+}\r
+\r
+int SDFileSystem::disk_initialize() {\r
+\r
+ int i = initialise_card();\r
+// printf("init card = %d\n", i);\r
+// printf("OK\n");\r
+\r
+ _sectors = _sd_sectors();\r
+\r
+ // Set block length to 512 (CMD16)\r
+ if(_cmd(16, 512) != 0) {\r
+ fprintf(stderr, "Set 512-byte block timed out\n");\r
+ return 1;\r
+ }\r
+ \r
+ _spi.frequency(1000000); // Set to 1MHz for data transfer\r
+ return 0;\r
+}\r
+\r
+int SDFileSystem::disk_write(const char *buffer, int block_number) {\r
+ // set write address for single block (CMD24)\r
+ if(_cmd(24, block_number * 512) != 0) {\r
+ return 1;\r
+ }\r
+\r
+ // send the data block\r
+ _write(buffer, 512); \r
+ return 0; \r
+}\r
+\r
+int SDFileSystem::disk_read(char *buffer, int block_number) { \r
+ // set read address for single block (CMD17)\r
+ if(_cmd(17, block_number * 512) != 0) {\r
+ return 1;\r
+ }\r
+ \r
+ // receive the data\r
+ _read(buffer, 512);\r
+ return 0;\r
+}\r
+\r
+int SDFileSystem::disk_status() { return 0; }\r
+int SDFileSystem::disk_sync() { return 0; }\r
+int SDFileSystem::disk_sectors() { return _sectors; }\r
+\r
+// PRIVATE FUNCTIONS\r
+\r
+int SDFileSystem::_cmd(int cmd, int arg) {\r
+ _cs = 0; \r
+\r
+ // send a command\r
+ _spi.write(0x40 | cmd);\r
+ _spi.write(arg >> 24);\r
+ _spi.write(arg >> 16);\r
+ _spi.write(arg >> 8);\r
+ _spi.write(arg >> 0);\r
+ _spi.write(0x95);\r
+\r
+ // wait for the repsonse (response[7] == 0)\r
+ for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
+ int response = _spi.write(0xFF);\r
+ if(!(response & 0x80)) {\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return response;\r
+ }\r
+ }\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return -1; // timeout\r
+}\r
+int SDFileSystem::_cmdx(int cmd, int arg) {\r
+ _cs = 0; \r
+\r
+ // send a command\r
+ _spi.write(0x40 | cmd);\r
+ _spi.write(arg >> 24);\r
+ _spi.write(arg >> 16);\r
+ _spi.write(arg >> 8);\r
+ _spi.write(arg >> 0);\r
+ _spi.write(0x95);\r
+\r
+ // wait for the repsonse (response[7] == 0)\r
+ for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
+ int response = _spi.write(0xFF);\r
+ if(!(response & 0x80)) {\r
+ return response;\r
+ }\r
+ }\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return -1; // timeout\r
+}\r
+\r
+\r
+int SDFileSystem::_cmd58() {\r
+ _cs = 0; \r
+ int arg = 0;\r
+ \r
+ // send a command\r
+ _spi.write(0x40 | 58);\r
+ _spi.write(arg >> 24);\r
+ _spi.write(arg >> 16);\r
+ _spi.write(arg >> 8);\r
+ _spi.write(arg >> 0);\r
+ _spi.write(0x95);\r
+\r
+ // wait for the repsonse (response[7] == 0)\r
+ for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
+ int response = _spi.write(0xFF);\r
+ if(!(response & 0x80)) {\r
+ int ocr = _spi.write(0xFF) << 24;\r
+ ocr |= _spi.write(0xFF) << 16;\r
+ ocr |= _spi.write(0xFF) << 8;\r
+ ocr |= _spi.write(0xFF) << 0;\r
+// printf("OCR = 0x%08X\n", ocr);\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return response;\r
+ }\r
+ }\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return -1; // timeout\r
+}\r
+\r
+int SDFileSystem::_cmd8() {\r
+ _cs = 0; \r
+ \r
+ // send a command\r
+ _spi.write(0x40 | 8); // CMD8\r
+ _spi.write(0x00); // reserved\r
+ _spi.write(0x00); // reserved\r
+ _spi.write(0x01); // 3.3v\r
+ _spi.write(0xAA); // check pattern\r
+ _spi.write(0x87); // crc\r
+\r
+ // wait for the repsonse (response[7] == 0)\r
+ for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {\r
+ char response[5];\r
+ response[0] = _spi.write(0xFF);\r
+ if(!(response[0] & 0x80)) {\r
+ for(int j=1; j<5; j++) {\r
+ response[i] = _spi.write(0xFF);\r
+ }\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return response[0];\r
+ }\r
+ }\r
+ _cs = 1;\r
+ _spi.write(0xFF);\r
+ return -1; // timeout\r
+}\r
+\r
+int SDFileSystem::_read(char *buffer, int length) {\r
+ _cs = 0;\r
+\r
+ // read until start byte (0xFF)\r
+ while(_spi.write(0xFF) != 0xFE);\r
+\r
+ // read data\r
+ for(int i=0; i<length; i++) {\r
+ buffer[i] = _spi.write(0xFF);\r
+ }\r
+ _spi.write(0xFF); // checksum\r
+ _spi.write(0xFF);\r
+\r
+ _cs = 1; \r
+ _spi.write(0xFF);\r
+ return 0;\r
+}\r
+\r
+int SDFileSystem::_write(const char *buffer, int length) {\r
+ _cs = 0;\r
+ \r
+ // indicate start of block\r
+ _spi.write(0xFE);\r
+ \r
+ // write the data\r
+ for(int i=0; i<length; i++) {\r
+ _spi.write(buffer[i]);\r
+ }\r
+ \r
+ // write the checksum\r
+ _spi.write(0xFF); \r
+ _spi.write(0xFF);\r
+\r
+ // check the repsonse token\r
+ if((_spi.write(0xFF) & 0x1F) != 0x05) {\r
+ _cs = 1;\r
+ _spi.write(0xFF); \r
+ return 1;\r
+ }\r
+\r
+ // wait for write to finish\r
+ while(_spi.write(0xFF) == 0);\r
+\r
+ _cs = 1; \r
+ _spi.write(0xFF);\r
+ return 0;\r
+}\r
+\r
+static int ext_bits(char *data, int msb, int lsb) {\r
+ int bits = 0;\r
+ int size = 1 + msb - lsb; \r
+ for(int i=0; i<size; i++) {\r
+ int position = lsb + i;\r
+ int byte = 15 - (position >> 3);\r
+ int bit = position & 0x7;\r
+ int value = (data[byte] >> bit) & 1;\r
+ bits |= value << i;\r
+ }\r
+ return bits;\r
+}\r
+\r
+int SDFileSystem::_sd_sectors() {\r
+\r
+ // CMD9, Response R2 (R1 byte + 16-byte block read)\r
+ if(_cmdx(9, 0) != 0) {\r
+ fprintf(stderr, "Didn't get a response from the disk\n");\r
+ return 0;\r
+ }\r
+ \r
+ char csd[16]; \r
+ if(_read(csd, 16) != 0) {\r
+ fprintf(stderr, "Couldn't read csd response from disk\n");\r
+ return 0;\r
+ }\r
+\r
+ // csd_structure : csd[127:126]\r
+ // c_size : csd[73:62]\r
+ // c_size_mult : csd[49:47]\r
+ // read_bl_len : csd[83:80] - the *maximum* read block length\r
+\r
+ int csd_structure = ext_bits(csd, 127, 126);\r
+ int c_size = ext_bits(csd, 73, 62);\r
+ int c_size_mult = ext_bits(csd, 49, 47);\r
+ int read_bl_len = ext_bits(csd, 83, 80);\r
+\r
+// printf("CSD_STRUCT = %d\n", csd_structure);\r
+ \r
+ if(csd_structure != 0) {\r
+ fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");\r
+ return 0;\r
+ }\r
+ \r
+ // memory capacity = BLOCKNR * BLOCK_LEN\r
+ // where\r
+ // BLOCKNR = (C_SIZE+1) * MULT\r
+ // MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)\r
+ // BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12) \r
+ \r
+ int block_len = 1 << read_bl_len;\r
+ int mult = 1 << (c_size_mult + 2);\r
+ int blocknr = (c_size + 1) * mult;\r
+ int capacity = blocknr * block_len;\r
+ \r
+ int blocks = capacity / 512;\r
+ \r
+ return blocks;\r
+}\r
--- /dev/null
+/* mbed SDFileSystem Library, for providing file access to SD cards\r
+ * Copyright (c) 2008-2010, sford\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ */\r
+\r
+#ifndef MBED_SDFILESYSTEM_H\r
+#define MBED_SDFILESYSTEM_H\r
+\r
+#include "mbed.h"\r
+#include "FATFileSystem.h"\r
+\r
+/** Access the filesystem on an SD Card using SPI\r
+ *\r
+ * @code\r
+ * #include "mbed.h"\r
+ * #include "SDFileSystem.h"\r
+ *\r
+ * SDFileSystem sd(p5, p6, p7, p12, "sd"); // mosi, miso, sclk, cs\r
+ * \r
+ * int main() {\r
+ * FILE *fp = fopen("/sd/myfile.txt", "w");\r
+ * fprintf(fp, "Hello World!\n");\r
+ * fclose(fp);\r
+ * }\r
+ */\r
+class SDFileSystem : public FATFileSystem {\r
+public:\r
+\r
+ /** Create the File System for accessing an SD Card using SPI\r
+ *\r
+ * @param mosi SPI mosi pin connected to SD Card\r
+ * @param miso SPI miso pin conencted to SD Card\r
+ * @param sclk SPI sclk pin connected to SD Card\r
+ * @param cs DigitalOut pin used as SD Card chip select\r
+ * @param name The name used to access the virtual filesystem\r
+ */\r
+ SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);\r
+ virtual int disk_initialize();\r
+ virtual int disk_write(const char *buffer, int block_number);\r
+ virtual int disk_read(char *buffer, int block_number); \r
+ virtual int disk_status();\r
+ virtual int disk_sync();\r
+ virtual int disk_sectors();\r
+\r
+protected:\r
+\r
+ int _cmd(int cmd, int arg);\r
+ int _cmdx(int cmd, int arg);\r
+ int _cmd8();\r
+ int _cmd58();\r
+ int initialise_card();\r
+ int initialise_card_v1();\r
+ int initialise_card_v2();\r
+ \r
+ int _read(char *buffer, int length);\r
+ int _write(const char *buffer, int length);\r
+ int _sd_sectors();\r
+ int _sectors;\r
+ \r
+ SPI _spi;\r
+ DigitalOut _cs; \r
+};\r
+\r
+#endif\r
--- /dev/null
+#include "Hook.h"
+
+Hook::Hook(){}
--- /dev/null
+#ifndef HOOK_H
+#define HOOK_H
+
+#include "mbed.h"
+
+class Hook : public FunctionPointer {
+ public:
+ Hook();
+ double frequency;
+ double counter;
+};
+
+
+
+
+#endif
this->add_module( this->serial );
this->slow_ticker = new SlowTicker();
+ this->slow_ticker->kernel = this; // DEBUG: To remove
this->step_ticker = new StepTicker();
//Module manager
class Module;
class Player;
+class SlowTicker;
class Kernel {
public:
Kernel();
#include "libs/Module.h"
#include "libs/Kernel.h"
#include "SlowTicker.h"
+#include "libs/Hook.h"
SlowTicker* global_slow_ticker;
SlowTicker::SlowTicker(){
+ this->max_frequency = 1;
global_slow_ticker = this;
LPC_SC->PCONP |= (1 << 22); // Power Ticker ON
- LPC_TIM2->MR0 = 1000000; // Initial dummy value for Match Register
+ LPC_TIM2->MR0 = 10000; // Initial dummy value for Match Register
LPC_TIM2->MCR = 3; // Match on MR0, reset on MR0
LPC_TIM2->TCR = 1; // Enable interrupt
NVIC_EnableIRQ(TIMER2_IRQn); // Enable interrupt handler
}
void SlowTicker::set_frequency( int frequency ){
+ //this->kernel->serial->printf("setting:%d size:%d \r\n", frequency, this->hooks.size() );
LPC_TIM2->MR0 = int(floor((SystemCoreClock/4)/frequency)); // SystemCoreClock/4 = Timer increments in a second
LPC_TIM2->TCR = 3; // Reset
LPC_TIM2->TCR = 1; // Reset
}
void SlowTicker::tick(){
+ if( this->max_frequency < 0.1 ){
+ this->kernel->serial->printf("empty\r\n");
+ }
for (int i=0; i<this->hooks.size(); i++){
- this->hooks.at(i)->call();
+ Hook* hook = this->hooks.at(i);
+ hook->counter += ( hook->frequency / this->max_frequency );
+ //this->kernel->serial->printf("%p cand size:%d, hook->freq:%f this->max:%f hook->counter:%f added:%f \r\n", hook, this->hooks.size(), hook->frequency, this->max_frequency, hook->counter, ( hook->frequency / this->max_frequency ) );
+ if( hook->counter > 0 ){
+ hook->counter-=1;
+ //this->kernel->serial->printf("%p exec size:%d, hook->freq:%f this->max:%f hook->counter:%f added:%f \r\n", hook, this->hooks.size(), hook->frequency, this->max_frequency, hook->counter, ( hook->frequency / this->max_frequency ) );
+ hook->call();
+ }
}
}
#include "libs/nuts_bolts.h"
#include "libs/Module.h"
#include "libs/Kernel.h"
+#include "libs/Hook.h"
-class SlowTicker{
+class SlowTicker : public Module{
public:
SlowTicker();
void set_frequency( int frequency );
void tick();
// For some reason this can't go in the .cpp, see : http://mbed.org/forum/mbed/topic/2774/?page=1#comment-14221
- template<typename T> void attach( T *optr, void ( T::*fptr )( void ) ){
- FunctionPointer* hook = new FunctionPointer();
+ template<typename T> void attach( double frequency, T *optr, void ( T::*fptr )( void ) ){
+ Hook* hook = new Hook();
+ hook->frequency = frequency;
hook->attach(optr, fptr);
+ hook->counter = -2;
+ if( frequency > this->max_frequency ){
+ this->max_frequency = frequency;
+ }
+ this->set_frequency(this->max_frequency);
this->hooks.push_back(hook);
}
- vector<FunctionPointer*> hooks;
-
+ vector<Hook*> hooks;
+ double max_frequency;
};
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#endif
--- /dev/null
+
\ No newline at end of file
--- /dev/null
+<?xml version="1.0"?>
+<root>
+ <global/>
+ <data>
+ <created_ts>1325892546</created_ts>
+ <last_mod_ts>1325892546</last_mod_ts>
+ <is_archive>1</is_archive>
+ <is_published>0</is_published>
+ <is_svn>0</is_svn>
+ <is_module>0</is_module>
+ <is_lib>1</is_lib>
+ <path>/USB_CDC_MSD_Hello/USBCDCMSC</path>
+ <duration>9</duration>
+ <csrftoken>c9876159eda1415cb42171cae52170ab</csrftoken>
+ <compiler_version>1.6.0.10</compiler_version>
+ </data>
+</root>
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBCDCMSC.h"\r
+#include "USBBusInterface.h"\r
+\r
+#include "libs/RingBuffer.h"\r
+\r
+static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+#define CDC_SET_LINE_CODING 0x20\r
+#define CDC_GET_LINE_CODING 0x21\r
+#define CDC_SET_CONTROL_LINE_STATE 0x22\r
+#define CDC_SEND_BREAK 0x23\r
+\r
+#define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK\r
+\r
+\r
+\r
+#define DISK_OK 0x00\r
+#define NO_INIT 0x01\r
+#define NO_DISK 0x02\r
+#define WRITE_PROTECT 0x04\r
+\r
+#define CBW_Signature 0x43425355\r
+#define CSW_Signature 0x53425355\r
+\r
+// SCSI Commands\r
+#define TEST_UNIT_READY 0x00\r
+#define REQUEST_SENSE 0x03\r
+#define FORMAT_UNIT 0x04\r
+#define INQUIRY 0x12\r
+#define MODE_SELECT6 0x15\r
+#define MODE_SENSE6 0x1A\r
+#define START_STOP_UNIT 0x1B\r
+#define MEDIA_REMOVAL 0x1E\r
+#define READ_FORMAT_CAPACITIES 0x23\r
+#define READ_CAPACITY 0x25\r
+#define READ10 0x28\r
+#define WRITE10 0x2A\r
+#define VERIFY10 0x2F\r
+#define READ12 0xA8\r
+#define WRITE12 0xAA\r
+#define MODE_SELECT10 0x55\r
+#define MODE_SENSE10 0x5A\r
+\r
+// MSC class specific requests\r
+#define MSC_REQUEST_RESET 0xFF\r
+#define MSC_REQUEST_GET_MAX_LUN 0xFE\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+// max packet size\r
+#define MAX_PACKET MAX_PACKET_SIZE_EPBULK\r
+\r
+// CSW Status\r
+enum Status {\r
+ CSW_PASSED,\r
+ CSW_FAILED,\r
+ CSW_ERROR,\r
+};\r
+\r
+\r
+USBCDCMSC::USBCDCMSC(SDFileSystem *sd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release), cdcbuf(128), _sd(sd) {\r
+ cdcbreak = 0;\r
+ _status = NO_INIT;\r
+ connect();\r
+// USBDevice::connect();\r
+ USBHAL::connect();\r
+}\r
+\r
+bool USBCDCMSC::USBCallback_request(void) {\r
+ /* Called in ISR context */\r
+\r
+ bool success = false;\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+ static uint8_t maxLUN[1] = {0};\r
+\r
+ /* Process class-specific requests */\r
+\r
+ if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {\r
+ switch (transfer->setup.bRequest) {\r
+ case CDC_GET_LINE_CODING:\r
+ transfer->remaining = 7;\r
+ transfer->ptr = cdc_line_coding;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case CDC_SET_LINE_CODING:\r
+ transfer->remaining = 7;\r
+ success = true;\r
+ break;\r
+ case CDC_SET_CONTROL_LINE_STATE:\r
+ success = true;\r
+ break;\r
+ case CDC_SEND_BREAK:\r
+ cdcbreak = 1;\r
+ success = true;\r
+ break;\r
+ case MSC_REQUEST_RESET:\r
+ reset();\r
+ success = true;\r
+ break;\r
+ case MSC_REQUEST_GET_MAX_LUN:\r
+ transfer->remaining = 1;\r
+ transfer->ptr = maxLUN;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the\r
+// configuration is not supported.\r
+bool USBCDCMSC::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);\r
+ addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);\r
+ addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(MSDBULK_IN, MAX_PACKET_SIZE_MSDBULK);\r
+ addEndpoint(MSDBULK_OUT, MAX_PACKET_SIZE_MSDBULK);\r
+\r
+ // We activate the endpoint to be able to recceive data\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ //activate readings\r
+ readStart(MSDBULK_OUT, MAX_PACKET_SIZE_MSDBULK);\r
+ return true;\r
+}\r
+\r
+bool USBCDCMSC::send(uint8_t * buffer, uint16_t size) {\r
+ return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);\r
+}\r
+\r
+bool USBCDCMSC::readEP(uint8_t * buffer, uint16_t * size) {\r
+ if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+bool USBCDCMSC::readEP_NB(uint8_t * buffer, uint16_t * size) {\r
+ if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+\r
+uint8_t * USBCDCMSC::deviceDesc() {\r
+ static uint8_t deviceDescriptor[] = {\r
+ 18, // bLength\r
+ 1, // bDescriptorType\r
+ 0x10, 0x01, // bcdUSB\r
+ 0xef, // bDeviceClass\r
+ 0x02, // bDeviceSubClass\r
+ 0x01, // bDeviceProtocol\r
+ MAX_PACKET_SIZE_EP0, // bMaxPacketSize0\r
+ LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor\r
+ LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct\r
+ 0x00, 0x01, // bcdDevice\r
+ 1, // iManufacturer\r
+ 2, // iProduct\r
+ 3, // iSerialNumber\r
+ 1 // bNumConfigurations\r
+ };\r
+ return deviceDescriptor;\r
+}\r
+\r
+uint8_t * USBCDCMSC::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x0e,\r
+ STRING_DESCRIPTOR,\r
+ 'C',0,'D',0,'C',0,'M',0,'S',0,'C',0,\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBCDCMSC::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x1c,\r
+ STRING_DESCRIPTOR,\r
+ 'C',0,'D',0,'C',0,'M',0,'S',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
+\r
+\r
+uint8_t * USBCDCMSC::configurationDesc() {\r
+ static uint8_t configDescriptor[] = {\r
+ 9, // bLength;\r
+ 2, // bDescriptorType;\r
+ LSB(0x62), // wTotalLength\r
+ MSB(0x62),\r
+ 3, // bNumInterfaces\r
+ 1, // bConfigurationValue\r
+ 0, // iConfiguration\r
+ 0xc0, // bmAttributes\r
+ 50, // bMaxPower\r
+\r
+ // IAD\r
+// 0x08, 0x0B, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00,\r
+ 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x00,\r
+\r
+ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 0, // bInterfaceNumber\r
+ 0, // bAlternateSetting\r
+ 1, // bNumEndpoints\r
+ 0x02, // bInterfaceClass\r
+ 0x02, // bInterfaceSubClass\r
+ 0x01, // bInterfaceProtocol\r
+ 0, // iInterface\r
+\r
+ // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x00, // bDescriptorSubtype\r
+ 0x10, 0x01, // bcdCDC\r
+\r
+ // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x01, // bDescriptorSubtype\r
+ 0x03, // bmCapabilities\r
+ 1, // bDataInterface\r
+\r
+ // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28\r
+ 4, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x02, // bDescriptorSubtype\r
+ 0x06, // bmCapabilities\r
+\r
+ // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x06, // bDescriptorSubtype\r
+ 0, // bMasterInterface\r
+ 1, // bSlaveInterface0\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ ENDPOINT_DESCRIPTOR_LENGTH, // bLength\r
+ ENDPOINT_DESCRIPTOR, // bDescriptorType\r
+ PHY_TO_DESC(EPINT_IN), // bEndpointAddress\r
+ E_INTERRUPT, // bmAttributes (0x03=intr)\r
+ LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)\r
+ 16, // bInterval\r
+\r
+\r
+\r
+\r
+ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 1, // bInterfaceNumber\r
+ 0, // bAlternateSetting\r
+ 2, // bNumEndpoints\r
+ 0x0A, // bInterfaceClass\r
+ 0x00, // bInterfaceSubClass\r
+ 0x00, // bInterfaceProtocol\r
+ 0, // iInterface\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_IN), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)\r
+ 0, // bInterval\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)\r
+ 0, // bInterval\r
+\r
+ // Interface 2, Alternate Setting 0, MSC Class\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 0x02, // bInterfaceNumber\r
+ 0x00, // bAlternateSetting\r
+ 0x02, // bNumEndpoints\r
+ 0x08, // bInterfaceClass\r
+ 0x06, // bInterfaceSubClass\r
+ 0x50, // bInterfaceProtocol\r
+ 0x04, // iInterface\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(MSDBULK_IN), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)\r
+ 0, // bInterval\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(MSDBULK_OUT), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)\r
+ 0 // bInterval\r
+ };\r
+ return configDescriptor;\r
+}\r
+\r
+int USBCDCMSC::_putc(int c) {\r
+ send((uint8_t *)&c, 1);\r
+ return 1;\r
+}\r
+\r
+int USBCDCMSC::_getc() {\r
+ uint8_t c;\r
+ while (cdcbuf.isEmpty());\r
+ cdcbuf.dequeue(&c);\r
+ return c;\r
+}\r
+\r
+\r
+bool USBCDCMSC::writeBlock(uint8_t * buf, uint16_t size) {\r
+ if(size > MAX_PACKET_SIZE_EPBULK) {\r
+ return false;\r
+ }\r
+ if(!send(buf, size)) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+\r
+bool USBCDCMSC::EP2_OUT_callback() {\r
+ uint8_t c[65];\r
+ uint16_t size = 0;\r
+\r
+ //we read the packet received and put it on the circular buffer\r
+ readEP(c, &size);\r
+ for (int i = 0; i < size; i++) {\r
+ cdcbuf.queue(c[i]);\r
+ }\r
+\r
+ //call a potential handler\r
+ rx.call();\r
+\r
+ // We reactivate the endpoint to receive next characters\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+uint8_t USBCDCMSC::available() {\r
+ return cdcbuf.available();\r
+}\r
+\r
+\r
+bool USBCDCMSC::connect() {\r
+\r
+ //disk initialization\r
+ if (disk_status() & NO_INIT) {\r
+ if (disk_initialize()) {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // get number of blocks\r
+ BlockCount = disk_sectors();\r
+\r
+ // get memory size\r
+ MemorySize = disk_size();\r
+\r
+ if (BlockCount >= 0) {\r
+ BlockSize = MemorySize / BlockCount;\r
+ if (BlockSize != 0) {\r
+ page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));\r
+ if (page == NULL)\r
+ return false;\r
+ }\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ //connect the device\r
+// USBDevice::connect();\r
+ return true;\r
+}\r
+\r
+\r
+void USBCDCMSC::reset() {\r
+ stage = READ_CBW;\r
+}\r
+\r
+\r
+// Called in ISR context called when a data is received\r
+bool USBCDCMSC::EP5_OUT_callback() {\r
+ uint16_t size = 0;\r
+ uint8_t buf[MAX_PACKET_SIZE_EPBULK];\r
+ USBDevice::readEP(MSDBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);\r
+ switch (stage) {\r
+ // the device has to decode the CBW received\r
+ case READ_CBW:\r
+ CBWDecode(buf, size);\r
+ break;\r
+\r
+ // the device has to receive data from the host\r
+ case PROCESS_CBW:\r
+ switch (cbw.CB[0]) {\r
+ case WRITE10:\r
+ case WRITE12:\r
+ memoryWrite(buf, size);\r
+ break;\r
+ case VERIFY10:\r
+ memoryVerify(buf, size);\r
+ break;\r
+ }\r
+ break;\r
+\r
+ // an error has occured: stall endpoint and send CSW\r
+ default:\r
+ stallEndpoint(MSDBULK_OUT);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ break;\r
+ }\r
+\r
+ //reactivate readings on the OUT bulk endpoint\r
+ readStart(MSDBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+// Called in ISR context when a data has been transferred\r
+bool USBCDCMSC::EP5_IN_callback() {\r
+ switch (stage) {\r
+\r
+ // the device has to send data to the host\r
+ case PROCESS_CBW:\r
+ switch (cbw.CB[0]) {\r
+ case READ10:\r
+ case READ12:\r
+ memoryRead();\r
+ break;\r
+ }\r
+ break;\r
+\r
+ //the device has to send a CSW\r
+ case SEND_CSW:\r
+ sendCSW();\r
+ break;\r
+\r
+ // an error has occured\r
+ case ERROR:\r
+ stallEndpoint(MSDBULK_IN);\r
+ sendCSW();\r
+ break;\r
+\r
+ // the host has received the CSW -> we wait a CBW\r
+ case WAIT_CSW:\r
+ stage = READ_CBW;\r
+ break;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+void USBCDCMSC::memoryWrite (uint8_t * buf, uint16_t size) {\r
+\r
+ if ((addr + size) > MemorySize) {\r
+ size = MemorySize - addr;\r
+ stage = ERROR;\r
+ stallEndpoint(MSDBULK_OUT);\r
+ }\r
+\r
+ // we fill an array in RAM of 1 block before writing it in memory\r
+ for (int i = 0; i < size; i++)\r
+ page[addr%BlockSize + i] = buf[i];\r
+\r
+ // if the array is filled, write it in memory\r
+ if (!((addr + size)%BlockSize)) {\r
+ if (!(disk_status() & WRITE_PROTECT)) {\r
+ disk_write((const char *)page, addr/BlockSize);\r
+ }\r
+ }\r
+\r
+ addr += size;\r
+ length -= size;\r
+ csw.DataResidue -= size;\r
+\r
+ if ((!length) || (stage != PROCESS_CBW)) {\r
+ csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;\r
+ sendCSW();\r
+ }\r
+}\r
+\r
+void USBCDCMSC::memoryVerify (uint8_t * buf, uint16_t size) {\r
+ uint32_t n;\r
+\r
+ if ((addr + size) > MemorySize) {\r
+ size = MemorySize - addr;\r
+ stage = ERROR;\r
+ stallEndpoint(MSDBULK_OUT);\r
+ }\r
+\r
+ // beginning of a new block -> load a whole block in RAM\r
+ if (!(addr%BlockSize))\r
+ disk_read((char *)page, addr/BlockSize);\r
+\r
+ // info are in RAM -> no need to re-read memory\r
+ for (n = 0; n < size; n++) {\r
+ if (page[addr%BlockSize + n] != buf[n]) {\r
+ memOK = false;\r
+ break;\r
+ }\r
+ }\r
+\r
+ addr += size;\r
+ length -= size;\r
+ csw.DataResidue -= size;\r
+\r
+ if ( !length || (stage != PROCESS_CBW)) {\r
+ csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;\r
+ sendCSW();\r
+ }\r
+}\r
+\r
+\r
+bool USBCDCMSC::inquiryRequest (void) {\r
+ uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,\r
+ 36 - 4, 0x80, 0x00, 0x00,\r
+ 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',\r
+ 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',\r
+ '1', '.', '0', ' ',\r
+ };\r
+ if (!msd_write(inquiry, sizeof(inquiry))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool USBCDCMSC::readFormatCapacity() {\r
+ uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,\r
+ (BlockCount >> 24) & 0xff,\r
+ (BlockCount >> 16) & 0xff,\r
+ (BlockCount >> 8) & 0xff,\r
+ (BlockCount >> 0) & 0xff,\r
+\r
+ 0x02,\r
+ (BlockSize >> 16) & 0xff,\r
+ (BlockSize >> 8) & 0xff,\r
+ (BlockSize >> 0) & 0xff,\r
+ };\r
+ if (!msd_write(capacity, sizeof(capacity))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool USBCDCMSC::readCapacity (void) {\r
+ uint8_t capacity[] = {\r
+ ((BlockCount - 1) >> 24) & 0xff,\r
+ ((BlockCount - 1) >> 16) & 0xff,\r
+ ((BlockCount - 1) >> 8) & 0xff,\r
+ ((BlockCount - 1) >> 0) & 0xff,\r
+\r
+ (BlockSize >> 24) & 0xff,\r
+ (BlockSize >> 16) & 0xff,\r
+ (BlockSize >> 8) & 0xff,\r
+ (BlockSize >> 0) & 0xff,\r
+ };\r
+ if (!msd_write(capacity, sizeof(capacity))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+bool USBCDCMSC::msd_write (uint8_t * buf, uint16_t size) {\r
+\r
+ if (size >= cbw.DataLength) {\r
+ size = cbw.DataLength;\r
+ }\r
+ stage = SEND_CSW;\r
+\r
+ if (!writeNB(MSDBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {\r
+ return false;\r
+ }\r
+\r
+ csw.DataResidue -= size;\r
+ csw.Status = CSW_PASSED;\r
+ return true;\r
+}\r
+\r
+\r
+bool USBCDCMSC::modeSense6 (void) {\r
+ uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };\r
+ if (!msd_write(sense6, sizeof(sense6))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+void USBCDCMSC::sendCSW() {\r
+ csw.Signature = CSW_Signature;\r
+ writeNB(MSDBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);\r
+ stage = WAIT_CSW;\r
+}\r
+\r
+bool USBCDCMSC::requestSense (void) {\r
+ uint8_t request_sense[] = {\r
+ 0x70,\r
+ 0x00,\r
+ 0x05, // Sense Key: illegal request\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x0A,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x30,\r
+ 0x01,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ };\r
+\r
+ if (!msd_write(request_sense, sizeof(request_sense))) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+void USBCDCMSC::fail() {\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+}\r
+\r
+\r
+void USBCDCMSC::CBWDecode(uint8_t * buf, uint16_t size) {\r
+ if (size == sizeof(cbw)) {\r
+ memcpy((uint8_t *)&cbw, buf, size);\r
+ if (cbw.Signature == CBW_Signature) {\r
+ csw.Tag = cbw.Tag;\r
+ csw.DataResidue = cbw.DataLength;\r
+ if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {\r
+ fail();\r
+ } else {\r
+ switch (cbw.CB[0]) {\r
+ case TEST_UNIT_READY:\r
+ testUnitReady();\r
+ break;\r
+ case REQUEST_SENSE:\r
+ requestSense();\r
+ break;\r
+ case INQUIRY:\r
+ inquiryRequest();\r
+ break;\r
+ case MODE_SENSE6:\r
+ modeSense6();\r
+ break;\r
+ case READ_FORMAT_CAPACITIES:\r
+ readFormatCapacity();\r
+ break;\r
+ case READ_CAPACITY:\r
+ readCapacity();\r
+ break;\r
+ case READ10:\r
+ case READ12:\r
+ if (infoTransfer()) {\r
+ if ((cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ memoryRead();\r
+ } else {\r
+ stallEndpoint(MSDBULK_OUT);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ case WRITE10:\r
+ case WRITE12:\r
+ if (infoTransfer()) {\r
+ if (!(cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ } else {\r
+ stallEndpoint(MSDBULK_IN);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ case VERIFY10:\r
+ if (!(cbw.CB[1] & 0x02)) {\r
+ csw.Status = CSW_PASSED;\r
+ sendCSW();\r
+ break;\r
+ }\r
+ if (infoTransfer()) {\r
+ if (!(cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ memOK = true;\r
+ } else {\r
+ stallEndpoint(MSDBULK_IN);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ default:\r
+ fail();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+void USBCDCMSC::testUnitReady (void) {\r
+\r
+ if (cbw.DataLength != 0) {\r
+ if ((cbw.Flags & 0x80) != 0) {\r
+ stallEndpoint(MSDBULK_IN);\r
+ } else {\r
+ stallEndpoint(MSDBULK_OUT);\r
+ }\r
+ }\r
+\r
+ csw.Status = CSW_PASSED;\r
+ sendCSW();\r
+}\r
+\r
+\r
+void USBCDCMSC::memoryRead (void) {\r
+ uint32_t n;\r
+\r
+ n = (length > MAX_PACKET) ? MAX_PACKET : length;\r
+\r
+ if ((addr + n) > MemorySize) {\r
+ n = MemorySize - addr;\r
+ stage = ERROR;\r
+ }\r
+\r
+ // we read an entire block\r
+ if (!(addr%BlockSize))\r
+ disk_read((char *)page, addr/BlockSize);\r
+\r
+ // write data which are in RAM\r
+ writeNB(MSDBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ addr += n;\r
+ length -= n;\r
+\r
+ csw.DataResidue -= n;\r
+\r
+ if ( !length || (stage != PROCESS_CBW)) {\r
+ csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;\r
+ stage = (stage == PROCESS_CBW) ? SEND_CSW : stage;\r
+ }\r
+}\r
+\r
+\r
+bool USBCDCMSC::infoTransfer (void) {\r
+ uint32_t n;\r
+\r
+ // Logical Block Address of First Block\r
+ n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);\r
+\r
+ addr = n * BlockSize;\r
+\r
+ // Number of Blocks to transfer\r
+ switch (cbw.CB[0]) {\r
+ case READ10:\r
+ case WRITE10:\r
+ case VERIFY10:\r
+ n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);\r
+ break;\r
+\r
+ case READ12:\r
+ case WRITE12:\r
+ n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);\r
+ break;\r
+ }\r
+\r
+ length = n * BlockSize;\r
+\r
+ if (!cbw.DataLength) { // host requests no data\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+ return false;\r
+ }\r
+\r
+ if (cbw.DataLength != length) {\r
+ if ((cbw.Flags & 0x80) != 0) {\r
+ stallEndpoint(MSDBULK_IN);\r
+ } else {\r
+ stallEndpoint(MSDBULK_OUT);\r
+ }\r
+\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+int USBCDCMSC::isBreak () {\r
+ int ret = cdcbreak;\r
+ cdcbreak = 0;\r
+ return ret;\r
+}\r
+\r
+int USBCDCMSC::disk_initialize() {\r
+ if (_sd->disk_initialize()) {\r
+ _status |= NO_DISK;\r
+ return 1;\r
+ } else {\r
+ _status = DISK_OK;\r
+ return 0;\r
+ }\r
+}\r
+\r
+int USBCDCMSC::disk_write(const char *buffer, int block_number) {\r
+ return _sd->disk_write(buffer, block_number); \r
+}\r
+\r
+int USBCDCMSC::disk_read(char *buffer, int block_number) { \r
+ return _sd->disk_read(buffer, block_number); \r
+}\r
+\r
+int USBCDCMSC::disk_status() {\r
+ return _status;\r
+}\r
+\r
+int USBCDCMSC::disk_sectors() {\r
+ return _sd->disk_sectors(); \r
+}\r
+int USBCDCMSC::disk_size() {\r
+ return _sd->disk_sectors() * 512;\r
+}\r
+\r
+\r
+void USBCDCMSC::on_module_loaded(){\r
+ // We want to be called every time a new char is received\r
+ this->attach(this, &USBCDCMSC::on_serial_char_received);\r
+\r
+ // We only call the command dispatcher in the main loop, nowhere else\r
+ this->register_for_event(ON_MAIN_LOOP);\r
+}\r
+\r
+void USBCDCMSC::on_main_loop(void* argument){\r
+ //if( this->configured() ){\r
+ // this->kernel->serial->printf("a:%d\r\n", this->buffer.size());\r
+ //}\r
+ if( this->has_char('\n') ){\r
+ int index = 0;\r
+ string received;\r
+ while(1){\r
+ char c;\r
+ this->buffer.pop_front(c);\r
+ if( c == '\n' ){\r
+ struct SerialMessage message; \r
+ message.message = received;\r
+ message.stream = this;\r
+ this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); \r
+ //this->kernel->serial->printf("received: %s \r\n", received.c_str() ); \r
+ //this->printf("received: %s\r\n", received.c_str() ); \r
+ return;\r
+ }else{\r
+ received += c;\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+}\r
+\r
+void USBCDCMSC::on_serial_char_received(){\r
+ if(this->available()){\r
+ char received = this->getc();\r
+ //On newline, we have received a line, else concatenate in buffer\r
+ if( received == '\r' ){ return; }\r
+ //if( this->kernel != NULL ){ \r
+ // this->kernel->serial->printf("received:%c\r\n", received); \r
+ //} \r
+ this->buffer.push_back(received); \r
+ }\r
+\r
+\r
+\r
+}\r
+\r
+\r
+bool USBCDCMSC::has_char(char letter){\r
+ int index = this->buffer.head;\r
+ while( index != this->buffer.tail ){\r
+ if( this->buffer.buffer[index] == letter ){\r
+ return true;\r
+ }\r
+ index = this->buffer.next_block_index(index);\r
+ }\r
+ return false;\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBCDCMSC_H\r
+#define USBCDCMSC_H\r
+\r
+#include <string>\r
+using std::string;\r
+#include "mbed.h"\r
+#include "libs/Module.h"\r
+#include "libs/Kernel.h"\r
+\r
+\r
+/* These headers are included for child class. */\r
+#include "USBEndpoints.h"\r
+#include "USBDescriptor.h"\r
+#include "USBDevice_Types.h"\r
+\r
+#include "USBDevice.h"\r
+\r
+#include "Stream.h"\r
+#include "CircBuffer.h"\r
+\r
+#include "libs/RingBuffer.h"\r
+\r
+#include "SDFileSystem.h"\r
+\r
+#ifdef __GNUC__\r
+ /* Packing for structs in GCC. */\r
+ #define PACK_STRUCT_STRUCT __attribute__((packed))\r
+ #define PACK_STRUCT_BEGIN\r
+ #define PACK_STRUCT_END\r
+#else /* !__GNUC__ */\r
+ /* Packing for structs in ARM compiler. */\r
+ #define PACK_STRUCT_STRUCT\r
+ #define PACK_STRUCT_BEGIN __packed\r
+ #define PACK_STRUCT_END\r
+#endif /* __GNUC__ */\r
+\r
+\r
+\r
+\r
+\r
+class USBCDCMSC: public USBDevice, public Module, public Stream {\r
+public:\r
+\r
+ /*\r
+ * Constructor\r
+ *\r
+ * @param vendor_id Your vendor_id\r
+ * @param product_id Your product_id\r
+ * @param product_release Your preoduct_release\r
+ */\r
+ USBCDCMSC(SDFileSystem *sd, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);\r
+\r
+ /**\r
+ * Send a character. You can use puts, printf.\r
+ *\r
+ * @param c character to be sent\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ virtual int _putc(int c);\r
+ \r
+ /**\r
+ * Read a character: blocking\r
+ *\r
+ * @returns character read\r
+ */\r
+ virtual int _getc();\r
+ \r
+ /**\r
+ * Check the number of bytes available.\r
+ *\r
+ * @returns the number of bytes available\r
+ */\r
+ uint8_t available(); \r
+ \r
+ /**\r
+ * Write a block of data. \r
+ *\r
+ * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.\r
+ *\r
+ * @param buf pointer on data which will be written\r
+ * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)\r
+ *\r
+ * @returns true if successfull\r
+ */\r
+ bool writeBlock(uint8_t * buf, uint16_t size);\r
+\r
+ /**\r
+ * Attach a member function to call when a packet is received. \r
+ *\r
+ * @param tptr pointer to the object to call the member function on\r
+ * @param mptr pointer to the member function to be called\r
+ */\r
+ template<typename T>\r
+ void attach(T* tptr, void (T::*mptr)(void)) {\r
+ if((mptr != NULL) && (tptr != NULL)) {\r
+ rx.attach(tptr, mptr);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Attach a callback called when a packet is received\r
+ *\r
+ * @param fptr function pointer\r
+ */\r
+ void attach(void (*fn)(void)) {\r
+ if(fn != NULL) {\r
+ rx.attach(fn);\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Connect the USB MSD device. Establish disk initialization before really connect the device.\r
+ *\r
+ * @returns true if successful\r
+ */\r
+ bool connect();\r
+ void on_module_loaded();\r
+ void on_serial_char_received();\r
+ void on_main_loop(void* argument);\r
+ bool has_char(char letter);\r
+\r
+ RingBuffer<char,256> buffer; // Receive buffer\r
+\r
+protected:\r
+ \r
+ /*\r
+ * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.\r
+ *\r
+ * @returns pointer to the device descriptor\r
+ */\r
+ virtual uint8_t * deviceDesc();\r
+ \r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+ \r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+\r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc();\r
+ \r
+ /*\r
+ * Send a buffer\r
+ *\r
+ * @param endpoint endpoint which will be sent the buffer\r
+ * @param buffer buffer to be sent\r
+ * @param size length of the buffer\r
+ * @returns true if successful\r
+ */\r
+ bool send(uint8_t * buffer, uint16_t size);\r
+ \r
+ /*\r
+ * Read a buffer from a certain endpoint. Warning: blocking\r
+ *\r
+ * @param endpoint endpoint to read\r
+ * @param buffer buffer where will be stored bytes\r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP(uint8_t * buffer, uint16_t * size);\r
+ \r
+ /*\r
+ * Read a buffer from a certain endpoint. Warning: non blocking\r
+ *\r
+ * @param endpoint endpoint to read\r
+ * @param buffer buffer where will be stored bytes\r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP_NB(uint8_t * buffer, uint16_t * size);\r
+\r
+ virtual bool USBCallback_request();\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);\r
+\r
+ virtual bool EP2_OUT_callback();\r
+\r
+ /*\r
+ * Callback called when a packet is received\r
+ */\r
+ virtual bool EP5_OUT_callback();\r
+\r
+ /*\r
+ * Callback called when a packet has been sent\r
+ */\r
+ virtual bool EP5_IN_callback();\r
+\r
+private:\r
+\r
+ FunctionPointer rx;\r
+ CircBuffer<uint8_t> cdcbuf;\r
+ int cdcbreak;\r
+\r
+ // MSC Bulk-only Stage\r
+ enum Stage {\r
+ READ_CBW, // wait a CBW\r
+ ERROR, // error\r
+ PROCESS_CBW, // process a CBW request\r
+ SEND_CSW, // send a CSW\r
+ WAIT_CSW, // wait that a CSW has been effectively sent\r
+ };\r
+\r
+\r
+ // Bulk-only CBW\r
+ typedef PACK_STRUCT_BEGIN struct {\r
+ uint32_t Signature;\r
+ uint32_t Tag;\r
+ uint32_t DataLength;\r
+ uint8_t Flags;\r
+ uint8_t LUN;\r
+ uint8_t CBLength;\r
+ uint8_t CB[16];\r
+ } PACK_STRUCT_STRUCT CBW;\r
+\r
+ // Bulk-only CSW\r
+ typedef PACK_STRUCT_BEGIN struct {\r
+ uint32_t Signature;\r
+ uint32_t Tag;\r
+ uint32_t DataResidue;\r
+ uint8_t Status;\r
+ } PACK_STRUCT_STRUCT CSW;\r
+\r
+\r
+\r
+ //state of the bulk-only state machine\r
+ Stage stage;\r
+\r
+ // current CBW\r
+ CBW cbw;\r
+\r
+ // CSW which will be sent\r
+ CSW csw;\r
+\r
+ // addr where will be read or written data\r
+ uint32_t addr;\r
+\r
+ // length of a reading or writing\r
+ uint32_t length;\r
+\r
+ // memory OK (after a memoryVerify)\r
+ bool memOK;\r
+\r
+ // cache in RAM before writing in memory. Useful also to read a block.\r
+ uint8_t * page;\r
+\r
+ int BlockSize;\r
+ int MemorySize;\r
+ int BlockCount;\r
+\r
+ int _status;\r
+ SDFileSystem *_sd;\r
+\r
+ void CBWDecode(uint8_t * buf, uint16_t size);\r
+ void sendCSW (void);\r
+ bool inquiryRequest (void);\r
+ bool msd_write (uint8_t * buf, uint16_t size);\r
+ bool readFormatCapacity();\r
+ bool readCapacity (void);\r
+ bool infoTransfer (void);\r
+ void memoryRead (void);\r
+ bool modeSense6 (void);\r
+ void testUnitReady (void);\r
+ bool requestSense (void);\r
+ void memoryVerify (uint8_t * buf, uint16_t size);\r
+ void memoryWrite (uint8_t * buf, uint16_t size);\r
+ void reset();\r
+ void fail();\r
+\r
+ int isBreak();\r
+\r
+ int disk_initialize();\r
+ int disk_write(const char *buffer, int block_number);\r
+ int disk_read(char *buffer, int block_number); \r
+ int disk_status();\r
+ int disk_sectors();\r
+ int disk_size();\r
+\r
+};\r
+\r
+#endif\r
--- /dev/null
+http://mbed.org/users/samux/libraries/USBDevice/m24owv
\ No newline at end of file
--- /dev/null
+<?xml version="1.0"?>
+<root>
+ <global/>
+ <data>
+ <url>http://mbed.org/users/samux/libraries/USBDevice</url>
+ <rev>m24owv</rev>
+ <url_full>http://mbed.org/users/samux/libraries/USBDevice/m24owv</url_full>
+ <created_ts>1325892546</created_ts>
+ <last_mod_ts>1325892546</last_mod_ts>
+ <is_archive>0</is_archive>
+ <is_published>1</is_published>
+ <is_svn>0</is_svn>
+ <is_module>0</is_module>
+ <is_lib>1</is_lib>
+ </data>
+</root>
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBAudio.h"\r
+#include "USBBusInterface.h"\r
+#include "USBAudio_Types.h"\r
+\r
+\r
+\r
+USBAudio::USBAudio(uint32_t frequency, uint8_t channel_nb, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {\r
+ mute = 0;\r
+ volCur = 0x0080;\r
+ volMin = 0x0000;\r
+ volMax = 0x0100;\r
+ volRes = 0x0004;\r
+ available = false;\r
+\r
+ FREQ = frequency;\r
+\r
+ this->channel_nb = channel_nb;\r
+\r
+ // stereo -> *2, mono -> *1\r
+ PACKET_SIZE_ISO = (FREQ / 500) * channel_nb;\r
+\r
+ // STEREO -> left and right\r
+ channel_config = (channel_nb == 1) ? CHANNEL_M : CHANNEL_L + CHANNEL_R;\r
+\r
+ SOF_handler = false;\r
+ \r
+ buf_stream = NULL;\r
+\r
+ // connect the device\r
+ USBDevice::connect();\r
+}\r
+\r
+bool USBAudio::read(uint8_t * buf) {\r
+ buf_stream = buf;\r
+ while (!available);\r
+ available = false;\r
+ buf_stream = NULL;\r
+ return true;\r
+}\r
+\r
+bool USBAudio::readNB(uint8_t * buf) {\r
+ buf_stream = buf;\r
+ SOF_handler = false;\r
+ while (!SOF_handler);\r
+ if (available) {\r
+ available = false;\r
+ buf_stream = NULL;\r
+ return true;\r
+ }\r
+ buf_stream = NULL;\r
+ return false;\r
+}\r
+\r
+\r
+float USBAudio::getVolume() {\r
+ return (mute) ? 0.0 : (float)volCur/(float)volMax;\r
+}\r
+\r
+\r
+// Called in ISR context on each start of frame\r
+void USBAudio::SOF(int frameNumber) {\r
+ uint16_t size = 0;\r
+\r
+ // read the isochronous endpoint\r
+ if (buf_stream != NULL) {\r
+ USBDevice::readEP_NB(EP3OUT, buf_stream, &size, PACKET_SIZE_ISO);\r
+ }\r
+\r
+ // if we read something, modify the flag "available"\r
+ available = (size) ? true : false;\r
+\r
+ // activate readings on the isochronous\r
+ readStart(EP3OUT, PACKET_SIZE_ISO);\r
+\r
+ SOF_handler = true;\r
+}\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the configuration is not supported.\r
+bool USBAudio::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure isochronous endpoint\r
+ realiseEndpoint(EP3OUT, PACKET_SIZE_ISO, ISOCHRONOUS);\r
+\r
+ // activate readings on this endpoint\r
+ readStart(EP3OUT, PACKET_SIZE_ISO);\r
+ return true;\r
+}\r
+\r
+\r
+// Called in ISR context\r
+// Set alternate setting. Return false if the alternate setting is not supported\r
+bool USBAudio::USBCallback_setInterface(uint16_t interface, uint8_t alternate) {\r
+ if (interface == 0 && alternate == 0) {\r
+ return true;\r
+ }\r
+ if (interface == 1 && (alternate == 0 || alternate == 1)) {\r
+ return true;\r
+ }\r
+ return false;\r
+}\r
+\r
+\r
+\r
+// Called in ISR context\r
+// Called by USBDevice on Endpoint0 request\r
+// This is used to handle extensions to standard requests and class specific requests.\r
+// Return true if class handles this request\r
+bool USBAudio::USBCallback_request() {\r
+ bool success = false;\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+\r
+ // Process class-specific requests\r
+ if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {\r
+\r
+ // Feature Unit: Interface = 0, ID = 2\r
+ if (transfer->setup.wIndex == 0x0200) {\r
+\r
+ // Master Channel\r
+ if ((transfer->setup.wValue & 0xff) == 0) {\r
+\r
+ switch (transfer->setup.wValue >> 8) {\r
+ case MUTE_CONTROL:\r
+ switch (transfer->setup.bRequest) {\r
+ case REQUEST_GET_CUR:\r
+ transfer->remaining = 1;\r
+ transfer->ptr = &mute;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+\r
+ case REQUEST_SET_CUR:\r
+ transfer->remaining = 1;\r
+ transfer->notify = true;\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ success = true;\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ break;\r
+ case VOLUME_CONTROL:\r
+ switch (transfer->setup.bRequest) {\r
+ case REQUEST_GET_CUR:\r
+ transfer->remaining = 2;\r
+ transfer->ptr = (uint8_t *)&volCur;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case REQUEST_GET_MIN:\r
+ transfer->remaining = 2;\r
+ transfer->ptr = (uint8_t *)&volMin;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case REQUEST_GET_MAX:\r
+ transfer->remaining = 2;\r
+ transfer->ptr = (uint8_t *)&volMax;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case REQUEST_GET_RES:\r
+ transfer->remaining = 2;\r
+ transfer->ptr = (uint8_t *)&volRes;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+\r
+ case REQUEST_SET_CUR:\r
+ transfer->remaining = 2;\r
+ transfer->notify = true;\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ success = true;\r
+ break;\r
+ case REQUEST_SET_MIN:\r
+ transfer->remaining = 2;\r
+ transfer->notify = true;\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ success = true;\r
+ break;\r
+ case REQUEST_SET_MAX:\r
+ transfer->remaining = 2;\r
+ transfer->notify = true;\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ success = true;\r
+ break;\r
+ case REQUEST_SET_RES:\r
+ transfer->remaining = 2;\r
+ transfer->notify = true;\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ success = true;\r
+ break;\r
+ }\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return success;\r
+}\r
+\r
+\r
+// Called in ISR context when a data OUT stage has been performed\r
+void USBAudio::USBCallback_requestCompleted(uint8_t * buf, uint16_t length) {\r
+ uint16_t data = *((uint16_t *)buf);\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+ switch (transfer->setup.wValue >> 8) {\r
+ case MUTE_CONTROL:\r
+ switch (transfer->setup.bRequest) {\r
+ case REQUEST_SET_CUR:\r
+ mute = data & 0xff;\r
+ updateVol.call();\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ break;\r
+ case VOLUME_CONTROL:\r
+ switch (transfer->setup.bRequest) {\r
+ case REQUEST_SET_CUR:\r
+ volCur = data;\r
+ updateVol.call();\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+}\r
+\r
+\r
+\r
+#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \\r
+ + (3 * INTERFACE_DESCRIPTOR_LENGTH) \\r
+ + (1 * CONTROL_INTERFACE_DESCRIPTOR_LENGTH) \\r
+ + (1 * INPUT_TERMINAL_DESCRIPTOR_LENGTH) \\r
+ + (1 * FEATURE_UNIT_DESCRIPTOR_LENGTH) \\r
+ + (1 * OUTPUT_TERMINAL_DESCRIPTOR_LENGTH) \\r
+ + (1 * STREAMING_INTERFACE_DESCRIPTOR_LENGTH) \\r
+ + (1 * FORMAT_TYPE_I_DESCRIPTOR_LENGTH) \\r
+ + (1 * (ENDPOINT_DESCRIPTOR_LENGTH + 2)) \\r
+ + (1 * STREAMING_ENDPOINT_DESCRIPTOR_LENGTH) )\r
+\r
+#define TOTAL_CONTROL_INTF_LENGTH (CONTROL_INTERFACE_DESCRIPTOR_LENGTH + \\r
+ INPUT_TERMINAL_DESCRIPTOR_LENGTH + \\r
+ FEATURE_UNIT_DESCRIPTOR_LENGTH + \\r
+ OUTPUT_TERMINAL_DESCRIPTOR_LENGTH)\r
+\r
+uint8_t * USBAudio::configurationDesc() {\r
+ static uint8_t configDescriptor[] = {\r
+ // Configuration 1\r
+ CONFIGURATION_DESCRIPTOR_LENGTH, // bLength\r
+ CONFIGURATION_DESCRIPTOR, // bDescriptorType\r
+ LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)\r
+ MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)\r
+ 0x02, // bNumInterfaces\r
+ DEFAULT_CONFIGURATION, // bConfigurationValue\r
+ 0x00, // iConfiguration\r
+ 0x80, // bmAttributes\r
+ 50, // bMaxPower\r
+\r
+ // Interface 0, Alternate Setting 0, Audio Control\r
+ INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR, // bDescriptorType\r
+ 0x00, // bInterfaceNumber\r
+ 0x00, // bAlternateSetting\r
+ 0x00, // bNumEndpoints\r
+ AUDIO_CLASS, // bInterfaceClass\r
+ SUBCLASS_AUDIOCONTROL, // bInterfaceSubClass\r
+ 0x00, // bInterfaceProtocol\r
+ 0x00, // iInterface\r
+\r
+\r
+ // Audio Control Interface\r
+ CONTROL_INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ CONTROL_HEADER, // bDescriptorSubtype\r
+ LSB(0x0100), // bcdADC (LSB)\r
+ MSB(0x0100), // bcdADC (MSB)\r
+ LSB(TOTAL_CONTROL_INTF_LENGTH), // wTotalLength\r
+ MSB(TOTAL_CONTROL_INTF_LENGTH), // wTotalLength\r
+ 0x01, // bInCollection\r
+ 0x01, // baInterfaceNr\r
+\r
+ // Audio Input Terminal\r
+ INPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ CONTROL_INPUT_TERMINAL, // bDescriptorSubtype\r
+ 0x01, // bTerminalID\r
+ LSB(TERMINAL_USB_STREAMING), // wTerminalType\r
+ MSB(TERMINAL_USB_STREAMING), // wTerminalType\r
+ 0x00, // bAssocTerminal\r
+ channel_nb, // bNrChannels\r
+ LSB(channel_config), // wChannelConfig\r
+ MSB(channel_config), // wChannelConfig\r
+ 0x00, // iChannelNames\r
+ 0x00, // iTerminal\r
+\r
+ // Audio Feature Unit\r
+ FEATURE_UNIT_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ CONTROL_FEATURE_UNIT, // bDescriptorSubtype\r
+ 0x02, // bUnitID\r
+ 0x01, // bSourceID\r
+ 0x01, // bControlSize\r
+ CONTROL_MUTE |\r
+ CONTROL_VOLUME, // bmaControls(0)\r
+ 0x00, // bmaControls(1)\r
+ 0x00, // iTerminal\r
+\r
+ // Audio Output Terminal\r
+ OUTPUT_TERMINAL_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ CONTROL_OUTPUT_TERMINAL, // bDescriptorSubtype\r
+ 0x03, // bTerminalID\r
+ LSB(TERMINAL_SPEAKER), // wTerminalType\r
+ MSB(TERMINAL_SPEAKER), // wTerminalType\r
+ 0x00, // bAssocTerminal\r
+ 0x02, // bSourceID\r
+ 0x00, // iTerminal\r
+\r
+\r
+ // Interface 1, Alternate Setting 0, Audio Streaming - Zero Bandwith\r
+ INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR, // bDescriptorType\r
+ 0x01, // bInterfaceNumber\r
+ 0x00, // bAlternateSetting\r
+ 0x00, // bNumEndpoints\r
+ AUDIO_CLASS, // bInterfaceClass\r
+ SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass\r
+ 0x00, // bInterfaceProtocol\r
+ 0x00, // iInterface\r
+\r
+ // Interface 1, Alternate Setting 1, Audio Streaming - Operational\r
+ INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR, // bDescriptorType\r
+ 0x01, // bInterfaceNumber\r
+ 0x01, // bAlternateSetting\r
+ 0x01, // bNumEndpoints\r
+ AUDIO_CLASS, // bInterfaceClass\r
+ SUBCLASS_AUDIOSTREAMING, // bInterfaceSubClass\r
+ 0x00, // bInterfaceProtocol\r
+ 0x00, // iInterface\r
+\r
+ // Audio Streaming Interface\r
+ STREAMING_INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ STREAMING_GENERAL, // bDescriptorSubtype\r
+ 0x01, // bTerminalLink\r
+ 0x00, // bDelay\r
+ LSB(FORMAT_PCM), // wFormatTag\r
+ MSB(FORMAT_PCM), // wFormatTag\r
+\r
+ // Audio Type I Format\r
+ FORMAT_TYPE_I_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType\r
+ STREAMING_FORMAT_TYPE, // bDescriptorSubtype\r
+ FORMAT_TYPE_I, // bFormatType\r
+ channel_nb, // bNrChannels\r
+ 0x02, // bSubFrameSize\r
+ 16, // bBitResolution\r
+ 0x01, // bSamFreqType\r
+ LSB(FREQ), // tSamFreq\r
+ (FREQ >> 8) & 0xff, // tSamFreq\r
+ (FREQ >> 16) & 0xff, // tSamFreq\r
+\r
+ // Endpoint - Standard Descriptor\r
+ ENDPOINT_DESCRIPTOR_LENGTH + 2, // bLength\r
+ ENDPOINT_DESCRIPTOR, // bDescriptorType\r
+ PHY_TO_DESC(EPISO_OUT), // bEndpointAddress\r
+ E_ISOCHRONOUS, // bmAttributes\r
+ LSB(PACKET_SIZE_ISO), // wMaxPacketSize\r
+ MSB(PACKET_SIZE_ISO), // wMaxPacketSize\r
+ 0x01, // bInterval\r
+ 0x00, // bRefresh\r
+ 0x00, // bSynchAddress\r
+\r
+ // Endpoint - Audio Streaming\r
+ STREAMING_ENDPOINT_DESCRIPTOR_LENGTH, // bLength\r
+ ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType\r
+ ENDPOINT_GENERAL, // bDescriptor\r
+ 0x00, // bmAttributes\r
+ 0x00, // bLockDelayUnits\r
+ LSB(0x0000), // wLockDelay\r
+ MSB(0x0000), // wLockDelay\r
+\r
+ // Terminator\r
+ 0 // bLength\r
+ };\r
+ return configDescriptor;\r
+}\r
+\r
+uint8_t * USBAudio::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x0c, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBAudio::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x16, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBAudio_H\r
+#define USBAudio_H\r
+\r
+/* These headers are included for child class. */\r
+#include "USBEndpoints.h"\r
+#include "USBDescriptor.h"\r
+#include "USBDevice_Types.h"\r
+\r
+#include "USBDevice.h"\r
+\r
+\r
+/**\r
+* USBAudio example\r
+*\r
+* #include "mbed.h"\r
+* #include "USBAudio.h"\r
+*\r
+* Serial pc(USBTX, USBRX);\r
+*\r
+* // frequency: 48 kHz\r
+* #define FREQ 48000\r
+*\r
+* // 1 channel: mono\r
+* #define NB_CHA 1\r
+*\r
+* // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1\r
+* #define AUDIO_LENGTH_PACKET 48 * 2 * 1\r
+*\r
+* // USBAudio\r
+* USBAudio audio(FREQ, NB_CHA);\r
+*\r
+* int main() {\r
+* int16_t buf[AUDIO_LENGTH_PACKET/2];\r
+* \r
+* while (1) {\r
+* // read an audio packet\r
+* audio.read((uint8_t *)buf);\r
+*\r
+*\r
+* // print packet received\r
+* pc.printf("recv: ");\r
+* for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {\r
+* pc.printf("%d ", buf[i]);\r
+* }\r
+* pc.printf("\r\n");\r
+* }\r
+* }\r
+* @endcode\r
+*/\r
+class USBAudio: public USBDevice {\r
+public:\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param frequency frequency in Hz (default: 48000)\r
+ * @param channel_nb channel number (1 or 2) (default: 1)\r
+ * @param vendor_id Your vendor_id\r
+ * @param product_id Your product_id\r
+ * @param product_release Your preoduct_release\r
+ */\r
+ USBAudio(uint32_t frequency = 48000, uint8_t channel_nb = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);\r
+\r
+ /**\r
+ * Get current volume between 0.0 and 1.0\r
+ *\r
+ * @returns volume\r
+ */\r
+ float getVolume();\r
+ \r
+ /**\r
+ * Read an audio packet. warning: blocking\r
+ *\r
+ * @param buf pointer on a buffer which will be filled with an audio packet\r
+ *\r
+ * @returns true if successfull\r
+ */\r
+ bool read(uint8_t * buf);\r
+ \r
+ /**\r
+ * Try to read an audio packet. warning: non blocking\r
+ *\r
+ * @param buf pointer on a buffer which will be filled if an audio packet is available\r
+ *\r
+ * @returns true if successfull\r
+ */\r
+ bool readNB(uint8_t * buf);\r
+ \r
+\r
+ /** attach a handler to update the volume\r
+ *\r
+ * @param function Function to attach\r
+ *\r
+ */\r
+ void attach(void(*fptr)(void)) {\r
+ updateVol.attach(fptr);\r
+ }\r
+\r
+ /** Attach a nonstatic void/void member function to update the volume\r
+ *\r
+ * @param tptr Object pointer\r
+ * @param mptr Member function pointer\r
+ *\r
+ */\r
+ template<typename T>\r
+ void attach(T *tptr, void(T::*mptr)(void)) {\r
+ updateVol.attach(tptr, mptr);\r
+ }\r
+\r
+\r
+protected:\r
+\r
+ /*\r
+ * Called by USBDevice layer. Set configuration of the device.\r
+ * For instance, you can add all endpoints that you need on this function.\r
+ *\r
+ * @param configuration Number of the configuration\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);\r
+\r
+ /*\r
+ * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context\r
+ * This is used to handle extensions to standard requests\r
+ * and class specific requests\r
+ *\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_request();\r
+\r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+\r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+\r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc();\r
+\r
+ /*\r
+ * Called by USBDevice layer. Set interface/alternate of the device.\r
+ *\r
+ * @param interface Number of the interface to be configured\r
+ * @param alternate Number of the alternate to be configured\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);\r
+\r
+ /*\r
+ * Called by USBDevice on Endpoint0 request completion\r
+ * if the 'notify' flag has been set to true. Warning: Called in ISR context\r
+ *\r
+ * In this case it is used to indicate that a HID report has\r
+ * been received from the host on endpoint 0\r
+ *\r
+ * @param buf buffer received on endpoint 0\r
+ * @param length length of this buffer\r
+ */\r
+ virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length);\r
+\r
+ /*\r
+ * Callback called on each Start of Frame event\r
+ */\r
+ virtual void SOF(int frameNumber);\r
+\r
+private:\r
+\r
+ // stream available ?\r
+ volatile bool available;\r
+\r
+ // FREQ\r
+ uint32_t FREQ;\r
+\r
+ // size of the maximum packet for the isochronous endpoint\r
+ uint32_t PACKET_SIZE_ISO;\r
+\r
+ // mono, stereo,...\r
+ uint8_t channel_nb;\r
+ \r
+ // channel config: master, left, right\r
+ uint8_t channel_config;\r
+\r
+ // mute state\r
+ uint8_t mute;\r
+\r
+ // Volume Current Value\r
+ uint16_t volCur;\r
+\r
+ // Volume Minimum Value\r
+ uint16_t volMin;\r
+\r
+ // Volume Maximum Value\r
+ uint16_t volMax;\r
+\r
+ // Volume Resolution\r
+ uint16_t volRes;\r
+\r
+ // Buffer containing one audio packet\r
+ uint8_t * buf_stream;\r
+ \r
+ // callback to update volume\r
+ FunctionPointer updateVol;\r
+ \r
+ // boolean showing that the SOF handler has been called. Useful for readNB.\r
+ volatile bool SOF_handler;\r
+\r
+};\r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBAUDIO_TYPES_H\r
+#define USBAUDIO_TYPES_H\r
+\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+// Audio Request Codes\r
+#define REQUEST_SET_CUR 0x01\r
+#define REQUEST_GET_CUR 0x81\r
+#define REQUEST_SET_MIN 0x02\r
+#define REQUEST_GET_MIN 0x82\r
+#define REQUEST_SET_MAX 0x03\r
+#define REQUEST_GET_MAX 0x83\r
+#define REQUEST_SET_RES 0x04\r
+#define REQUEST_GET_RES 0x84\r
+\r
+#define MUTE_CONTROL 0x01\r
+#define VOLUME_CONTROL 0x02\r
+\r
+\r
+// Audio Descriptor Sizes\r
+#define CONTROL_INTERFACE_DESCRIPTOR_LENGTH 0x09\r
+#define STREAMING_INTERFACE_DESCRIPTOR_LENGTH 0x07\r
+#define INPUT_TERMINAL_DESCRIPTOR_LENGTH 0x0C\r
+#define OUTPUT_TERMINAL_DESCRIPTOR_LENGTH 0x09\r
+#define FEATURE_UNIT_DESCRIPTOR_LENGTH 0x09\r
+#define STREAMING_ENDPOINT_DESCRIPTOR_LENGTH 0x07\r
+\r
+// Audio Format Type Descriptor Sizes\r
+#define FORMAT_TYPE_I_DESCRIPTOR_LENGTH 0x0b\r
+\r
+#define AUDIO_CLASS 0x01\r
+#define SUBCLASS_AUDIOCONTROL 0x01\r
+#define SUBCLASS_AUDIOSTREAMING 0x02\r
+\r
+// Audio Descriptor Types\r
+#define INTERFACE_DESCRIPTOR_TYPE 0x24\r
+#define ENDPOINT_DESCRIPTOR_TYPE 0x25\r
+\r
+// Audio Control Interface Descriptor Subtypes\r
+#define CONTROL_HEADER 0x01\r
+#define CONTROL_INPUT_TERMINAL 0x02\r
+#define CONTROL_OUTPUT_TERMINAL 0x03\r
+#define CONTROL_FEATURE_UNIT 0x06\r
+\r
+// USB Terminal Types\r
+#define TERMINAL_USB_STREAMING 0x0101\r
+\r
+// Predefined Audio Channel Configuration Bits\r
+// Mono\r
+#define CHANNEL_M 0x0000\r
+#define CHANNEL_L 0x0001 /* Left Front */\r
+#define CHANNEL_R 0x0002 /* Right Front */\r
+\r
+// Feature Unit Control Bits\r
+#define CONTROL_MUTE 0x0001\r
+#define CONTROL_VOLUME 0x0002\r
+\r
+// Output Terminal Types\r
+#define TERMINAL_SPEAKER 0x0301\r
+#define TERMINAL_HEADPHONES 0x0302\r
+\r
+// Audio Streaming Interface Descriptor Subtypes\r
+#define STREAMING_GENERAL 0x01\r
+#define STREAMING_FORMAT_TYPE 0x02\r
+\r
+// Audio Data Format Type I Codes\r
+#define FORMAT_PCM 0x0001\r
+\r
+// Audio Format Types\r
+#define FORMAT_TYPE_I 0x01\r
+\r
+// Audio Endpoint Descriptor Subtypes\r
+#define ENDPOINT_GENERAL 0x01\r
+\r
+#endif\r
--- /dev/null
+/* USBBusInterface.h */\r
+/* USB Bus Interface */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#ifndef USBBUSINTERFACE_H\r
+#define USBBUSINTERFACE_H\r
+\r
+#include "mbed.h"\r
+#include "USBEndpoints.h"\r
+\r
+class USBHAL {\r
+public:\r
+ /* Configuration */\r
+ USBHAL();\r
+ ~USBHAL();\r
+ void connect(void);\r
+ void disconnect(void);\r
+ void configureDevice(void);\r
+ void unconfigureDevice(void);\r
+ void setAddress(uint8_t address);\r
+ void remoteWakeup(void);\r
+\r
+ /* Endpoint 0 */\r
+ void EP0setup(uint8_t *buffer);\r
+ void EP0read(void);\r
+ uint32_t EP0getReadResult(uint8_t *buffer);\r
+ void EP0write(uint8_t *buffer, uint32_t size);\r
+ void EP0getWriteResult(void);\r
+ void EP0stall(void);\r
+\r
+ /* Other endpoints */\r
+ EP_STATUS endpointRead(uint8_t endpoint, uint32_t maximumSize);\r
+ EP_STATUS endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead);\r
+ EP_STATUS endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size);\r
+ EP_STATUS endpointWriteResult(uint8_t endpoint);\r
+ void stallEndpoint(uint8_t endpoint);\r
+ void unstallEndpoint(uint8_t endpoint);\r
+ bool realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options);\r
+ bool getEndpointStallState(unsigned char endpoint);\r
+ uint32_t endpointReadcore(uint8_t endpoint, uint8_t *buffer);\r
+ \r
+protected:\r
+ virtual void busReset(void){};\r
+ virtual void EP0setupCallback(void){};\r
+ virtual void EP0out(void){};\r
+ virtual void EP0in(void){};\r
+ virtual void connectStateChanged(unsigned int connected){};\r
+ virtual void suspendStateChanged(unsigned int suspended){};\r
+ virtual void SOF(int frameNumber){};\r
+ virtual bool EP1_OUT_callback(){return false;};\r
+ virtual bool EP1_IN_callback(){return false;};\r
+ virtual bool EP2_OUT_callback(){return false;};\r
+ virtual bool EP2_IN_callback(){return false;};\r
+ virtual bool EP3_OUT_callback(){return false;};\r
+ virtual bool EP3_IN_callback(){return false;};\r
+ virtual bool EP5_OUT_callback(){return false;};\r
+ virtual bool EP5_IN_callback(){return false;};\r
+ \r
+private:\r
+ void usbisr(void);\r
+ static void _usbisr(void);\r
+ static USBHAL * instance;\r
+};\r
+#endif\r
+\r
+\r
--- /dev/null
+// USBBusInterface_LPC11U.c\r
+// USB Bus Interface for NXP LPC11Uxx\r
+// Copyright (c) 2011 ARM Limited. All rights reserved.\r
+\r
+// Reference:\r
+// NXP UM10462 LPC11U1x User manual Rev. 1 � 14 April 2011\r
+\r
+#ifdef TARGET_LPC11U24\r
+\r
+#include "USBBusInterface.h"\r
+\r
+USBHAL * USBHAL::instance;\r
+\r
+\r
+// Valid physical endpoint numbers are 0 to (NUMBER_OF_PHYSICAL_ENDPOINTS-1)\r
+#define LAST_PHYSICAL_ENDPOINT (NUMBER_OF_PHYSICAL_ENDPOINTS-1)\r
+\r
+// Convert physical endpoint number to register bit\r
+#define EP(endpoint) (1UL<<endpoint)\r
+\r
+// Convert physical to logical\r
+#define PHY_TO_LOG(endpoint) ((endpoint)>>1)\r
+\r
+// Get endpoint direction\r
+#define IN_EP(endpoint) ((endpoint) & 1U ? true : false)\r
+#define OUT_EP(endpoint) ((endpoint) & 1U ? false : true)\r
+\r
+// USB RAM\r
+#define USB_RAM_START (0x20004000)\r
+#define USB_RAM_SIZE (0x00000800)\r
+\r
+// SYSAHBCLKCTRL\r
+#define CLK_USB (1UL<<14)\r
+#define CLK_USBRAM (1UL<<27)\r
+\r
+// USB Information register\r
+#define FRAME_NR(a) ((a) & 0x7ff) // Frame number\r
+\r
+// USB Device Command/Status register\r
+#define DEV_ADDR_MASK (0x7f) // Device address\r
+#define DEV_ADDR(a) ((a) & DEV_ADDR_MASK)\r
+#define DEV_EN (1UL<<7) // Device enable\r
+#define SETUP (1UL<<8) // SETUP token received\r
+#define PLL_ON (1UL<<9) // PLL enabled in suspend\r
+#define DCON (1UL<<16) // Device status - connect\r
+#define DSUS (1UL<<17) // Device status - suspend\r
+#define DCON_C (1UL<<24) // Connect change\r
+#define DSUS_C (1UL<<25) // Suspend change\r
+#define DRES_C (1UL<<26) // Reset change\r
+#define VBUSDEBOUNCED (1UL<<28) // Vbus detected\r
+\r
+// Endpoint Command/Status list\r
+#define CMDSTS_A (1UL<<31) // Active\r
+#define CMDSTS_D (1UL<<30) // Disable\r
+#define CMDSTS_S (1UL<<29) // Stall\r
+#define CMDSTS_TR (1UL<<28) // Toggle Reset\r
+#define CMDSTS_RF (1UL<<27) // Rate Feedback mode\r
+#define CMDSTS_TV (1UL<<27) // Toggle Value\r
+#define CMDSTS_T (1UL<<26) // Endpoint Type\r
+#define CMDSTS_NBYTES(n) (((n)&0x3ff)<<16) // Number of bytes\r
+#define CMDSTS_ADDRESS_OFFSET(a) (((a)>>6)&0xffff) // Buffer start address\r
+\r
+#define BYTES_REMAINING(s) (((s)>>16)&0x3ff) // Bytes remaining after transfer\r
+\r
+// USB Non-endpoint interrupt sources\r
+#define FRAME_INT (1UL<<30)\r
+#define DEV_INT (1UL<<31)\r
+\r
+static volatile int epComplete = 0;\r
+\r
+// One entry for a double-buffered logical endpoint in the endpoint\r
+// command/status list. Endpoint 0 is single buffered, out[1] is used\r
+// for the SETUP packet and in[1] is not used\r
+typedef __packed struct {\r
+ uint32_t out[2];\r
+ uint32_t in[2];\r
+} EP_COMMAND_STATUS;\r
+\r
+typedef __packed struct {\r
+ uint8_t out[MAX_PACKET_SIZE_EP0];\r
+ uint8_t in[MAX_PACKET_SIZE_EP0];\r
+ uint8_t setup[SETUP_PACKET_SIZE];\r
+} CONTROL_TRANSFER;\r
+\r
+typedef __packed struct {\r
+ uint32_t maxPacket;\r
+ uint32_t buffer[2];\r
+ uint32_t options;\r
+} EP_STATE;\r
+\r
+static volatile EP_STATE endpointState[NUMBER_OF_PHYSICAL_ENDPOINTS];\r
+\r
+// Pointer to the endpoint command/status list\r
+static EP_COMMAND_STATUS *ep = NULL;\r
+\r
+// Pointer to endpoint 0 data (IN/OUT and SETUP)\r
+static CONTROL_TRANSFER *ct = NULL;\r
+\r
+// Shadow DEVCMDSTAT register to avoid accidentally clearing flags or\r
+// initiating a remote wakeup event.\r
+static volatile uint32_t devCmdStat;\r
+\r
+// Pointers used to allocate USB RAM\r
+static uint32_t usbRamPtr = USB_RAM_START;\r
+static uint32_t epRamPtr = 0; // Buffers for endpoints > 0 start here\r
+\r
+#define ROUND_UP_TO_MULTIPLE(x, m) ((((x)+((m)-1))/(m))*(m))\r
+\r
+void USBMemCopy(uint8_t *dst, uint8_t *src, uint32_t size);\r
+void USBMemCopy(uint8_t *dst, uint8_t *src, uint32_t size) {\r
+ if (size > 0) {\r
+ do {\r
+ *dst++ = *src++;\r
+ } while (--size > 0);\r
+ }\r
+}\r
+\r
+\r
+USBHAL::USBHAL(void) {\r
+ NVIC_DisableIRQ(USB_IRQn);\r
+\r
+ // nUSB_CONNECT output\r
+ LPC_IOCON->PIO0_6 = 0x00000001;\r
+\r
+ // Enable clocks (USB registers, USB RAM)\r
+ LPC_SYSCON->SYSAHBCLKCTRL |= CLK_USB | CLK_USBRAM;\r
+\r
+ // Ensure device disconnected (DCON not set)\r
+ LPC_USB->DEVCMDSTAT = 0;\r
+\r
+ // to ensure that the USB host sees the device as\r
+ // disconnected if the target CPU is reset.\r
+ wait(0.3);\r
+\r
+ // Reserve space in USB RAM for endpoint command/status list\r
+ // Must be 256 byte aligned\r
+ usbRamPtr = ROUND_UP_TO_MULTIPLE(usbRamPtr, 256);\r
+ ep = (EP_COMMAND_STATUS *)usbRamPtr;\r
+ usbRamPtr += (sizeof(EP_COMMAND_STATUS) * NUMBER_OF_LOGICAL_ENDPOINTS);\r
+ LPC_USB->EPLISTSTART = (uint32_t)(ep) & 0xffffff00;\r
+\r
+ // Reserve space in USB RAM for Endpoint 0\r
+ // Must be 64 byte aligned\r
+ usbRamPtr = ROUND_UP_TO_MULTIPLE(usbRamPtr, 64);\r
+ ct = (CONTROL_TRANSFER *)usbRamPtr;\r
+ usbRamPtr += sizeof(CONTROL_TRANSFER);\r
+ LPC_USB->DATABUFSTART =(uint32_t)(ct) & 0xffc00000;\r
+\r
+ // Setup command/status list for EP0\r
+ ep[0].out[0] = 0;\r
+ ep[0].in[0] = 0;\r
+ ep[0].out[1] = CMDSTS_ADDRESS_OFFSET((uint32_t)ct->setup);\r
+\r
+ // Route all interrupts to IRQ, some can be routed to\r
+ // USB_FIQ if you wish.\r
+ LPC_USB->INTROUTING = 0;\r
+\r
+ // Set device address 0, enable USB device, no remote wakeup\r
+ devCmdStat = DEV_ADDR(0) | DEV_EN | DSUS;\r
+ LPC_USB->DEVCMDSTAT = devCmdStat;\r
+\r
+ // Enable interrupts for device events and EP0\r
+ LPC_USB->INTEN = DEV_INT | EP(EP0IN) | EP(EP0OUT) | FRAME_INT;\r
+ instance = this;\r
+\r
+ //attach IRQ handler and enable interrupts\r
+ NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);\r
+ NVIC_EnableIRQ(USB_IRQn);\r
+}\r
+\r
+USBHAL::~USBHAL(void) {\r
+ // Ensure device disconnected (DCON not set)\r
+ LPC_USB->DEVCMDSTAT = 0;\r
+\r
+ // Disable USB interrupts\r
+ NVIC_DisableIRQ(USB_IRQn);\r
+}\r
+\r
+void USBHAL::connect(void) {\r
+ devCmdStat |= DCON;\r
+ LPC_USB->DEVCMDSTAT = devCmdStat;\r
+}\r
+\r
+void USBHAL::disconnect(void) {\r
+ devCmdStat &= ~DCON;\r
+ LPC_USB->DEVCMDSTAT = devCmdStat;\r
+}\r
+\r
+void USBHAL::configureDevice(void) {\r
+}\r
+\r
+void USBHAL::unconfigureDevice(void) {\r
+}\r
+\r
+void USBHAL::EP0setup(uint8_t *buffer) {\r
+ // Copy setup packet data\r
+ USBMemCopy(buffer, ct->setup, SETUP_PACKET_SIZE);\r
+}\r
+\r
+void USBHAL::EP0read(void) {\r
+ // Start an endpoint 0 read\r
+\r
+ // The USB ISR will call USBDevice_EP0out() when a packet has been read,\r
+ // the USBDevice layer then calls USBBusInterface_EP0getReadResult() to\r
+ // read the data.\r
+\r
+ ep[0].out[0] = CMDSTS_A |CMDSTS_NBYTES(MAX_PACKET_SIZE_EP0) \\r
+ | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->out);\r
+}\r
+\r
+uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {\r
+ // Complete an endpoint 0 read\r
+ uint32_t bytesRead;\r
+\r
+ // Find how many bytes were read\r
+ bytesRead = MAX_PACKET_SIZE_EP0 - BYTES_REMAINING(ep[0].out[0]);\r
+\r
+ // Copy data\r
+ USBMemCopy(buffer, ct->out, bytesRead);\r
+ return bytesRead;\r
+}\r
+\r
+void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {\r
+ // Start and endpoint 0 write\r
+\r
+ // The USB ISR will call USBDevice_EP0in() when the data has\r
+ // been written, the USBDevice layer then calls\r
+ // USBBusInterface_EP0getWriteResult() to complete the transaction.\r
+\r
+ // Copy data\r
+ USBMemCopy(ct->in, buffer, size);\r
+\r
+ // Start transfer\r
+ ep[0].in[0] = CMDSTS_A | CMDSTS_NBYTES(size) \\r
+ | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->in);\r
+}\r
+\r
+\r
+EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {\r
+ uint8_t bf = 0;\r
+ uint32_t flags = 0;\r
+\r
+ //check which buffer must be filled\r
+ if (LPC_USB->EPBUFCFG & EP(endpoint)) {\r
+ // Double buffered\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ bf = 1;\r
+ } else {\r
+ bf = 0;\r
+ }\r
+ }\r
+ \r
+ // if isochronous endpoint, T = 1\r
+ if(endpointState[endpoint].options & ISOCHRONOUS)\r
+ {\r
+ flags |= CMDSTS_T;\r
+ }\r
+ \r
+ //Active the endpoint for reading\r
+ ep[PHY_TO_LOG(endpoint)].out[bf] = CMDSTS_A | CMDSTS_NBYTES(maximumSize) \\r
+ | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->out) | flags;\r
+ return EP_PENDING;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead) {\r
+\r
+ uint8_t bf = 0;\r
+\r
+ if (!(epComplete & EP(endpoint)))\r
+ return EP_PENDING;\r
+ else {\r
+ epComplete &= ~EP(endpoint);\r
+\r
+ //check which buffer has been filled\r
+ if (LPC_USB->EPBUFCFG & EP(endpoint)) {\r
+ // Double buffered (here we read the previous buffer which was used)\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ bf = 0;\r
+ } else {\r
+ bf = 1;\r
+ }\r
+ }\r
+\r
+ // Find how many bytes were read\r
+ *bytesRead = (uint32_t) (endpointState[endpoint].maxPacket - BYTES_REMAINING(ep[PHY_TO_LOG(endpoint)].out[bf]));\r
+\r
+ // Copy data\r
+ USBMemCopy(data, ct->out, *bytesRead);\r
+ return EP_COMPLETED;\r
+ }\r
+}\r
+\r
+void USBHAL::EP0getWriteResult(void) {\r
+ // Complete an endpoint 0 write\r
+\r
+ // Nothing required for this target\r
+ return;\r
+}\r
+\r
+void USBHAL::EP0stall(void) {\r
+ ep[0].in[0] = CMDSTS_S;\r
+ ep[0].out[0] = CMDSTS_S;\r
+}\r
+\r
+void USBHAL::setAddress(uint8_t address) {\r
+ devCmdStat &= ~DEV_ADDR_MASK;\r
+ devCmdStat |= DEV_ADDR(address);\r
+ LPC_USB->DEVCMDSTAT = devCmdStat;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {\r
+ uint32_t flags = 0;\r
+ uint32_t bf;\r
+\r
+ // Validate parameters\r
+ if (data == NULL) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if (endpoint > LAST_PHYSICAL_ENDPOINT) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if ((endpoint==EP0IN) || (endpoint==EP0OUT)) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if (size > endpointState[endpoint].maxPacket) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if (LPC_USB->EPBUFCFG & EP(endpoint)) {\r
+ // Double buffered\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ bf = 1;\r
+ } else {\r
+ bf = 0;\r
+ }\r
+ } else {\r
+ // Single buffered\r
+ bf = 0;\r
+ }\r
+\r
+ // Check if already active\r
+ if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_A) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ // Check if stalled\r
+ if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_S) {\r
+ return EP_STALLED;\r
+ }\r
+\r
+ // Copy data to USB RAM\r
+ USBMemCopy((uint8_t *)endpointState[endpoint].buffer[bf], data, size);\r
+\r
+ // Add options\r
+ if (endpointState[endpoint].options & RATE_FEEDBACK_MODE) {\r
+ flags |= CMDSTS_RF;\r
+ }\r
+\r
+ if (endpointState[endpoint].options & ISOCHRONOUS) {\r
+ flags |= CMDSTS_T;\r
+ }\r
+\r
+ // Add transfer\r
+ ep[PHY_TO_LOG(endpoint)].in[bf] = CMDSTS_ADDRESS_OFFSET( \\r
+ endpointState[endpoint].buffer[bf]) \\r
+ | CMDSTS_NBYTES(size) | CMDSTS_A | flags;\r
+\r
+ return EP_PENDING;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {\r
+ uint32_t bf;\r
+ // Validate parameters\r
+\r
+ if (endpoint > LAST_PHYSICAL_ENDPOINT) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if (OUT_EP(endpoint)) {\r
+ return EP_INVALID;\r
+ }\r
+\r
+ if (LPC_USB->EPBUFCFG & EP(endpoint)) {\r
+ // Double buffered // TODO: FIX THIS\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ bf = 1;\r
+ } else {\r
+ bf = 0;\r
+ }\r
+ } else {\r
+ // Single buffered\r
+ bf = 0;\r
+ }\r
+\r
+ // Check if endpoint still active\r
+ if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_A) {\r
+ return EP_PENDING;\r
+ }\r
+\r
+ // Check if stalled\r
+ if (ep[PHY_TO_LOG(endpoint)].in[bf] & CMDSTS_S) {\r
+ return EP_STALLED;\r
+ }\r
+\r
+ return EP_COMPLETED;\r
+}\r
+\r
+void USBHAL::stallEndpoint(uint8_t endpoint) {\r
+\r
+ // TODO: should this clear active bit?\r
+\r
+ if (IN_EP(endpoint)) {\r
+ ep[PHY_TO_LOG(endpoint)].in[0] |= CMDSTS_S;\r
+ ep[PHY_TO_LOG(endpoint)].in[1] |= CMDSTS_S;\r
+ } else {\r
+ ep[PHY_TO_LOG(endpoint)].out[0] |= CMDSTS_S;\r
+ ep[PHY_TO_LOG(endpoint)].out[1] |= CMDSTS_S;\r
+ }\r
+}\r
+\r
+void USBHAL::unstallEndpoint(uint8_t endpoint) {\r
+ if (LPC_USB->EPBUFCFG & EP(endpoint)) {\r
+ // Double buffered\r
+ if (IN_EP(endpoint)) {\r
+ ep[PHY_TO_LOG(endpoint)].in[0] = 0; // S = 0\r
+ ep[PHY_TO_LOG(endpoint)].in[1] = 0; // S = 0\r
+\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ ep[PHY_TO_LOG(endpoint)].in[1] = CMDSTS_TR; // S =0, TR=1, TV = 0\r
+ } else {\r
+ ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S =0, TR=1, TV = 0\r
+ }\r
+ } else {\r
+ ep[PHY_TO_LOG(endpoint)].out[0] = 0; // S = 0\r
+ ep[PHY_TO_LOG(endpoint)].out[1] = 0; // S = 0\r
+\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ ep[PHY_TO_LOG(endpoint)].out[1] = CMDSTS_TR; // S =0, TR=1, TV = 0\r
+ } else {\r
+ ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S =0, TR=1, TV = 0\r
+ }\r
+ }\r
+ } else {\r
+ // Single buffered\r
+ if (IN_EP(endpoint)) {\r
+ ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S=0, TR=1, TV = 0\r
+ } else {\r
+ ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S=0, TR=1, TV = 0\r
+ }\r
+ }\r
+}\r
+\r
+bool USBHAL::getEndpointStallState(unsigned char endpoint) {\r
+ if (IN_EP(endpoint)) {\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ if (ep[PHY_TO_LOG(endpoint)].in[1] & CMDSTS_S) {\r
+ return true;\r
+ }\r
+ } else {\r
+ if (ep[PHY_TO_LOG(endpoint)].in[0] & CMDSTS_S) {\r
+ return true;\r
+ }\r
+ }\r
+ } else {\r
+ if (LPC_USB->EPINUSE & EP(endpoint)) {\r
+ if (ep[PHY_TO_LOG(endpoint)].out[1] & CMDSTS_S) {\r
+ return true;\r
+ }\r
+ } else {\r
+ if (ep[PHY_TO_LOG(endpoint)].out[0] & CMDSTS_S) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options) {\r
+ uint32_t tmpEpRamPtr;\r
+\r
+ if (endpoint > LAST_PHYSICAL_ENDPOINT) {\r
+ return false;\r
+ }\r
+\r
+ // Not applicable to the control endpoints\r
+ if ((endpoint==EP0IN) || (endpoint==EP0OUT)) {\r
+ return false;\r
+ }\r
+\r
+ // Allocate buffers in USB RAM\r
+ tmpEpRamPtr = epRamPtr;\r
+\r
+ // Must be 64 byte aligned\r
+ tmpEpRamPtr = ROUND_UP_TO_MULTIPLE(tmpEpRamPtr, 64);\r
+\r
+ if ((tmpEpRamPtr + maxPacket) > (USB_RAM_START + USB_RAM_SIZE)) {\r
+ // Out of memory\r
+ return false;\r
+ }\r
+\r
+ // Allocate first buffer\r
+ endpointState[endpoint].buffer[0] = tmpEpRamPtr;\r
+ tmpEpRamPtr += maxPacket;\r
+\r
+ if (!(options & SINGLE_BUFFERED)) {\r
+ // Must be 64 byte aligned\r
+ tmpEpRamPtr = ROUND_UP_TO_MULTIPLE(tmpEpRamPtr, 64);\r
+\r
+ if ((tmpEpRamPtr + maxPacket) > (USB_RAM_START + USB_RAM_SIZE)) {\r
+ // Out of memory\r
+ return false;\r
+ }\r
+\r
+ // Allocate second buffer\r
+ endpointState[endpoint].buffer[1] = tmpEpRamPtr;\r
+ tmpEpRamPtr += maxPacket;\r
+ }\r
+\r
+ // Commit to this USB RAM allocation\r
+ epRamPtr = tmpEpRamPtr;\r
+\r
+ // Remaining endpoint state values\r
+ endpointState[endpoint].maxPacket = maxPacket;\r
+ endpointState[endpoint].options = options;\r
+\r
+ // Enable double buffering if required\r
+ if (options & SINGLE_BUFFERED) {\r
+ LPC_USB->EPBUFCFG &= ~EP(endpoint);\r
+ } else {\r
+ // Double buffered\r
+ LPC_USB->EPBUFCFG |= EP(endpoint);\r
+ }\r
+\r
+ // Enable interrupt\r
+ LPC_USB->INTEN |= EP(endpoint);\r
+\r
+ // Enable endpoint\r
+ unstallEndpoint(endpoint);\r
+ return true;\r
+}\r
+\r
+void USBHAL::remoteWakeup(void) {\r
+ // Clearing DSUS bit initiates a remote wakeup if the\r
+ // device is currently enabled and suspended - otherwise\r
+ // it has no effect.\r
+ LPC_USB->DEVCMDSTAT = devCmdStat & ~DSUS;\r
+}\r
+\r
+\r
+static void disableEndpoints(void) {\r
+ uint32_t logEp;\r
+\r
+ // Ref. Table 158 "When a bus reset is received, software\r
+ // must set the disable bit of all endpoints to 1".\r
+\r
+ for (logEp = 1; logEp < NUMBER_OF_LOGICAL_ENDPOINTS; logEp++) {\r
+ ep[logEp].out[0] = CMDSTS_D;\r
+ ep[logEp].out[1] = CMDSTS_D;\r
+ ep[logEp].in[0] = CMDSTS_D;\r
+ ep[logEp].in[1] = CMDSTS_D;\r
+ }\r
+\r
+ // Start of USB RAM for endpoints > 0\r
+ epRamPtr = usbRamPtr;\r
+}\r
+\r
+\r
+\r
+void USBHAL::_usbisr(void) {\r
+ instance->usbisr();\r
+}\r
+\r
+\r
+void USBHAL::usbisr(void) {\r
+ // Start of frame\r
+ if (LPC_USB->INTSTAT & FRAME_INT) {\r
+ // Clear SOF interrupt\r
+ LPC_USB->INTSTAT = FRAME_INT;\r
+\r
+ // SOF event, read frame number\r
+ SOF(FRAME_NR(LPC_USB->INFO));\r
+ }\r
+\r
+ // Device state\r
+ if (LPC_USB->INTSTAT & DEV_INT) {\r
+ LPC_USB->INTSTAT = DEV_INT;\r
+\r
+ if (LPC_USB->DEVCMDSTAT & DCON_C) {\r
+ // Connect status changed\r
+ LPC_USB->DEVCMDSTAT = devCmdStat | DCON_C;\r
+\r
+ connectStateChanged((LPC_USB->DEVCMDSTAT & DCON) != 0);\r
+ }\r
+\r
+ if (LPC_USB->DEVCMDSTAT & DSUS_C) {\r
+ // Suspend status changed\r
+ LPC_USB->DEVCMDSTAT = devCmdStat | DSUS_C;\r
+\r
+ suspendStateChanged((LPC_USB->DEVCMDSTAT & DSUS) != 0);\r
+ }\r
+\r
+ if (LPC_USB->DEVCMDSTAT & DRES_C) {\r
+ // Bus reset\r
+ LPC_USB->DEVCMDSTAT = devCmdStat | DRES_C;\r
+\r
+ // Disable endpoints > 0\r
+ disableEndpoints();\r
+\r
+ // Bus reset event\r
+ busReset();\r
+ }\r
+ }\r
+\r
+ // Endpoint 0\r
+ if (LPC_USB->INTSTAT & EP(EP0OUT)) {\r
+ // Clear EP0OUT/SETUP interrupt\r
+ LPC_USB->INTSTAT = EP(EP0OUT);\r
+\r
+ // Check if SETUP\r
+ if (LPC_USB->DEVCMDSTAT & SETUP) {\r
+ // Clear Active and Stall bits for EP0\r
+ // Documentation does not make it clear if we must use the\r
+ // EPSKIP register to achieve this, Fig. 16 and NXP reference\r
+ // code suggests we can just clear the Active bits - check with\r
+ // NXP to be sure.\r
+ ep[0].in[0] = 0;\r
+ ep[0].out[0] = 0;\r
+\r
+ // Clear EP0IN interrupt\r
+ LPC_USB->INTSTAT = EP(EP0IN);\r
+\r
+ // Clear SETUP (and INTONNAK_CI/O) in device status register\r
+ LPC_USB->DEVCMDSTAT = devCmdStat | SETUP;\r
+\r
+ // EP0 SETUP event (SETUP data received)\r
+ EP0setupCallback();\r
+ } else {\r
+ // EP0OUT ACK event (OUT data received)\r
+ EP0out();\r
+ }\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP0IN)) {\r
+ // Clear EP0IN interrupt\r
+ LPC_USB->INTSTAT = EP(EP0IN);\r
+\r
+ // EP0IN ACK event (IN data sent)\r
+ EP0in();\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP1IN)) {\r
+ // Clear EP1IN interrupt\r
+ LPC_USB->INTSTAT = EP(EP1IN);\r
+ epComplete |= EP(EP1IN);\r
+ if (EP1_IN_callback())\r
+ epComplete &= ~EP(EP1IN);\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP1OUT)) {\r
+ // Clear EP1OUT interrupt\r
+ LPC_USB->INTSTAT = EP(EP1OUT);\r
+ epComplete |= EP(EP1OUT);\r
+ if (EP1_OUT_callback())\r
+ epComplete &= ~EP(EP1OUT);\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP2IN)) {\r
+ // Clear EPBULK_IN interrupt\r
+ LPC_USB->INTSTAT = EP(EP2IN);\r
+ epComplete |= EP(EP2IN);\r
+ if (EP2_IN_callback())\r
+ epComplete &= ~EP(EP2IN);\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP2OUT)) {\r
+ // Clear EPBULK_OUT interrupt\r
+ LPC_USB->INTSTAT = EP(EP2OUT);\r
+ epComplete |= EP(EP2OUT);\r
+ //Call callback function. If true, clear epComplete\r
+ if (EP2_OUT_callback())\r
+ epComplete &= ~EP(EP2OUT);\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP3IN)) {\r
+ // Clear EP3_IN interrupt\r
+ LPC_USB->INTSTAT = EP(EP3IN);\r
+ epComplete |= EP(EP3IN);\r
+ if (EP3_IN_callback())\r
+ epComplete &= ~EP(EP3IN);\r
+ }\r
+\r
+ if (LPC_USB->INTSTAT & EP(EP3OUT)) {\r
+ // Clear EP3_OUT interrupt\r
+ LPC_USB->INTSTAT = EP(EP3OUT);\r
+ epComplete |= EP(EP3OUT);\r
+ //Call callback function. If true, clear epComplete\r
+ if (EP3_OUT_callback())\r
+ epComplete &= ~EP(EP3OUT);\r
+ }\r
+}\r
+\r
+#endif
\ No newline at end of file
--- /dev/null
+// USBBusInterface_LPC17_LPC23.c\r
+// USB Bus Interface for NXP LPC1768 and LPC2368\r
+// Copyright (c) 2011 ARM Limited. All rights reserved.\r
+\r
+#ifdef TARGET_LPC1768\r
+\r
+#include "USBBusInterface.h"\r
+\r
+\r
+// Get endpoint direction\r
+#define IN_EP(endpoint) ((endpoint) & 1U ? true : false)\r
+#define OUT_EP(endpoint) ((endpoint) & 1U ? false : true)\r
+\r
+// Convert physical endpoint number to register bit\r
+#define EP(endpoint) (1UL<<endpoint)\r
+\r
+// Power Control for Peripherals register\r
+#define PCUSB (1UL<<31)\r
+\r
+// USB Clock Control register\r
+#define DEV_CLK_EN (1UL<<1)\r
+#define AHB_CLK_EN (1UL<<4)\r
+\r
+// USB Clock Status register\r
+#define DEV_CLK_ON (1UL<<1)\r
+#define AHB_CLK_ON (1UL<<4)\r
+\r
+// USB Device Interupt registers\r
+#define FRAME (1UL<<0)\r
+#define EP_FAST (1UL<<1)\r
+#define EP_SLOW (1UL<<2)\r
+#define DEV_STAT (1UL<<3)\r
+#define CCEMPTY (1UL<<4)\r
+#define CDFULL (1UL<<5)\r
+#define RxENDPKT (1UL<<6)\r
+#define TxENDPKT (1UL<<7)\r
+#define EP_RLZED (1UL<<8)\r
+#define ERR_INT (1UL<<9)\r
+\r
+// USB Control register\r
+#define RD_EN (1<<0)\r
+#define WR_EN (1<<1)\r
+#define LOG_ENDPOINT(endpoint) ((endpoint>>1)<<2)\r
+\r
+// USB Receive Packet Length register\r
+#define DV (1UL<<10)\r
+#define PKT_RDY (1UL<<11)\r
+#define PKT_LNGTH_MASK (0x3ff)\r
+\r
+// Serial Interface Engine (SIE)\r
+#define SIE_WRITE (0x01)\r
+#define SIE_READ (0x02)\r
+#define SIE_COMMAND (0x05)\r
+#define SIE_CMD_CODE(phase, data) ((phase<<8)|(data<<16))\r
+\r
+// SIE Command codes\r
+#define SIE_CMD_SET_ADDRESS (0xD0)\r
+#define SIE_CMD_CONFIGURE_DEVICE (0xD8)\r
+#define SIE_CMD_SET_MODE (0xF3)\r
+#define SIE_CMD_READ_FRAME_NUMBER (0xF5)\r
+#define SIE_CMD_READ_TEST_REGISTER (0xFD)\r
+#define SIE_CMD_SET_DEVICE_STATUS (0xFE)\r
+#define SIE_CMD_GET_DEVICE_STATUS (0xFE)\r
+#define SIE_CMD_GET_ERROR_CODE (0xFF)\r
+#define SIE_CMD_READ_ERROR_STATUS (0xFB)\r
+\r
+#define SIE_CMD_SELECT_ENDPOINT(endpoint) (0x00+endpoint)\r
+#define SIE_CMD_SELECT_ENDPOINT_CLEAR_INTERRUPT(endpoint) (0x40+endpoint)\r
+#define SIE_CMD_SET_ENDPOINT_STATUS(endpoint) (0x40+endpoint)\r
+\r
+#define SIE_CMD_CLEAR_BUFFER (0xF2)\r
+#define SIE_CMD_VALIDATE_BUFFER (0xFA)\r
+\r
+// SIE Device Status register\r
+#define SIE_DS_CON (1<<0)\r
+#define SIE_DS_CON_CH (1<<1)\r
+#define SIE_DS_SUS (1<<2)\r
+#define SIE_DS_SUS_CH (1<<3)\r
+#define SIE_DS_RST (1<<4)\r
+\r
+// SIE Device Set Address register\r
+#define SIE_DSA_DEV_EN (1<<7)\r
+\r
+// SIE Configue Device register\r
+#define SIE_CONF_DEVICE (1<<0)\r
+\r
+// Select Endpoint register\r
+#define SIE_SE_FE (1<<0)\r
+#define SIE_SE_ST (1<<1)\r
+#define SIE_SE_STP (1<<2)\r
+#define SIE_SE_PO (1<<3)\r
+#define SIE_SE_EPN (1<<4)\r
+#define SIE_SE_B_1_FULL (1<<5)\r
+#define SIE_SE_B_2_FULL (1<<6)\r
+\r
+// Set Endpoint Status command\r
+#define SIE_SES_ST (1<<0)\r
+#define SIE_SES_DA (1<<5)\r
+#define SIE_SES_RF_MO (1<<6)\r
+#define SIE_SES_CND_ST (1<<7)\r
+\r
+\r
+USBHAL * USBHAL::instance;\r
+\r
+volatile int epComplete;\r
+uint32_t endpointStallState;\r
+\r
+static void SIECommand(uint32_t command) {\r
+ // The command phase of a SIE transaction\r
+ LPC_USB->USBDevIntClr = CCEMPTY;\r
+ LPC_USB->USBCmdCode = SIE_CMD_CODE(SIE_COMMAND, command);\r
+ while (!(LPC_USB->USBDevIntSt & CCEMPTY));\r
+}\r
+\r
+static void SIEWriteData(uint8_t data) {\r
+ // The data write phase of a SIE transaction\r
+ LPC_USB->USBDevIntClr = CCEMPTY;\r
+ LPC_USB->USBCmdCode = SIE_CMD_CODE(SIE_WRITE, data);\r
+ while (!(LPC_USB->USBDevIntSt & CCEMPTY));\r
+}\r
+\r
+static uint8_t SIEReadData(uint32_t command) {\r
+ // The data read phase of a SIE transaction\r
+ LPC_USB->USBDevIntClr = CDFULL;\r
+ LPC_USB->USBCmdCode = SIE_CMD_CODE(SIE_READ, command);\r
+ while (!(LPC_USB->USBDevIntSt & CDFULL));\r
+ return (uint8_t)LPC_USB->USBCmdData;\r
+}\r
+\r
+static void SIEsetDeviceStatus(uint8_t status) {\r
+ // Write SIE device status register\r
+ SIECommand(SIE_CMD_SET_DEVICE_STATUS);\r
+ SIEWriteData(status);\r
+}\r
+\r
+static uint8_t SIEgetDeviceStatus(void) {\r
+ // Read SIE device status register\r
+ SIECommand(SIE_CMD_GET_DEVICE_STATUS);\r
+ return SIEReadData(SIE_CMD_GET_DEVICE_STATUS);\r
+}\r
+\r
+void SIEsetAddress(uint8_t address) {\r
+ // Write SIE device address register\r
+ SIECommand(SIE_CMD_SET_ADDRESS);\r
+ SIEWriteData((address & 0x7f) | SIE_DSA_DEV_EN);\r
+}\r
+\r
+static uint8_t SIEselectEndpoint(uint8_t endpoint) {\r
+ // SIE select endpoint command\r
+ SIECommand(SIE_CMD_SELECT_ENDPOINT(endpoint));\r
+ return SIEReadData(SIE_CMD_SELECT_ENDPOINT(endpoint));\r
+}\r
+\r
+static uint8_t SIEclearBuffer(void) {\r
+ // SIE clear buffer command\r
+ SIECommand(SIE_CMD_CLEAR_BUFFER);\r
+ return SIEReadData(SIE_CMD_CLEAR_BUFFER);\r
+}\r
+\r
+static void SIEvalidateBuffer(void) {\r
+ // SIE validate buffer command\r
+ SIECommand(SIE_CMD_VALIDATE_BUFFER);\r
+}\r
+\r
+static void SIEsetEndpointStatus(uint8_t endpoint, uint8_t status) {\r
+ // SIE set endpoint status command\r
+ SIECommand(SIE_CMD_SET_ENDPOINT_STATUS(endpoint));\r
+ SIEWriteData(status);\r
+}\r
+\r
+static uint16_t SIEgetFrameNumber(void) __attribute__ ((unused));\r
+static uint16_t SIEgetFrameNumber(void) {\r
+ // Read current frame number\r
+ uint16_t lowByte;\r
+ uint16_t highByte;\r
+\r
+ SIECommand(SIE_CMD_READ_FRAME_NUMBER);\r
+ lowByte = SIEReadData(SIE_CMD_READ_FRAME_NUMBER);\r
+ highByte = SIEReadData(SIE_CMD_READ_FRAME_NUMBER);\r
+\r
+ return (highByte << 8) | lowByte;\r
+}\r
+\r
+static void SIEconfigureDevice(void) {\r
+ // SIE Configure device command\r
+ SIECommand(SIE_CMD_CONFIGURE_DEVICE);\r
+ SIEWriteData(SIE_CONF_DEVICE);\r
+}\r
+\r
+static void SIEunconfigureDevice(void) {\r
+ // SIE Configure device command\r
+ SIECommand(SIE_CMD_CONFIGURE_DEVICE);\r
+ SIEWriteData(0);\r
+}\r
+\r
+static void SIEconnect(void) {\r
+ // Connect USB device\r
+ uint8_t status;\r
+\r
+ status = SIEgetDeviceStatus();\r
+ SIEsetDeviceStatus(status | SIE_DS_CON);\r
+}\r
+\r
+\r
+static void SIEdisconnect(void) {\r
+ // Disconnect USB device\r
+ uint8_t status;\r
+\r
+ status = SIEgetDeviceStatus();\r
+ SIEsetDeviceStatus(status & ~SIE_DS_CON);\r
+}\r
+\r
+\r
+static uint8_t selectEndpointClearInterrupt(uint8_t endpoint) {\r
+ // Implemented using using EP_INT_CLR.\r
+ LPC_USB->USBEpIntClr = EP(endpoint);\r
+ while (!(LPC_USB->USBDevIntSt & CDFULL));\r
+ return (uint8_t)LPC_USB->USBCmdData;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+static void enableEndpointEvent(uint8_t endpoint) {\r
+ // Enable an endpoint interrupt\r
+ LPC_USB->USBEpIntEn |= EP(endpoint);\r
+}\r
+\r
+static void disableEndpointEvent(uint8_t endpoint) __attribute__ ((unused));\r
+static void disableEndpointEvent(uint8_t endpoint) {\r
+ // Disable an endpoint interrupt\r
+ LPC_USB->USBEpIntEn &= ~EP(endpoint);\r
+}\r
+\r
+static volatile uint32_t __attribute__((used)) dummyRead;\r
+\r
+\r
+uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {\r
+ // Read from an OUT endpoint\r
+ uint32_t size;\r
+ uint32_t i;\r
+ uint32_t data = 0;\r
+ uint8_t offset;\r
+\r
+ LPC_USB->USBCtrl = LOG_ENDPOINT(endpoint) | RD_EN;\r
+ while (!(LPC_USB->USBRxPLen & PKT_RDY));\r
+\r
+ size = LPC_USB->USBRxPLen & PKT_LNGTH_MASK;\r
+\r
+ offset = 0;\r
+\r
+ if (size > 0) {\r
+ for (i=0; i<size; i++) {\r
+ if (offset==0) {\r
+ // Fetch up to four bytes of data as a word\r
+ data = LPC_USB->USBRxData;\r
+ }\r
+\r
+ // extract a byte\r
+ *buffer = (data>>offset) & 0xff;\r
+ buffer++;\r
+\r
+ // move on to the next byte\r
+ offset = (offset + 8) % 32;\r
+ }\r
+ } else {\r
+ dummyRead = LPC_USB->USBRxData;\r
+ }\r
+\r
+ LPC_USB->USBCtrl = 0;\r
+\r
+ if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {\r
+ SIEselectEndpoint(endpoint);\r
+ SIEclearBuffer();\r
+ }\r
+ \r
+ return size;\r
+}\r
+\r
+static void endpointWritecore(uint8_t endpoint, uint8_t *buffer, uint32_t size) {\r
+ // Write to an IN endpoint\r
+ uint32_t temp, data;\r
+ uint8_t offset;\r
+\r
+ LPC_USB->USBCtrl = LOG_ENDPOINT(endpoint) | WR_EN;\r
+\r
+ LPC_USB->USBTxPLen = size;\r
+ offset = 0;\r
+ data = 0;\r
+\r
+ if (size>0) {\r
+ do {\r
+ // Fetch next data byte into a word-sized temporary variable\r
+ temp = *buffer++;\r
+\r
+ // Add to current data word\r
+ temp = temp << offset;\r
+ data = data | temp;\r
+\r
+ // move on to the next byte\r
+ offset = (offset + 8) % 32;\r
+ size--;\r
+\r
+ if ((offset==0) || (size==0)) {\r
+ // Write the word to the endpoint\r
+ LPC_USB->USBTxData = data;\r
+ data = 0;\r
+ }\r
+ } while (size>0);\r
+ } else {\r
+ LPC_USB->USBTxData = 0;\r
+ }\r
+\r
+ // Clear WR_EN to cover zero length packet case\r
+ LPC_USB->USBCtrl=0;\r
+\r
+ SIEselectEndpoint(endpoint);\r
+ SIEvalidateBuffer();\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+USBHAL::USBHAL(void) {\r
+ // Disable IRQ\r
+ NVIC_DisableIRQ(USB_IRQn);\r
+\r
+ // Enable power to USB device controller\r
+ LPC_SC->PCONP |= PCUSB;\r
+\r
+ // Enable USB clocks\r
+ LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN;\r
+ while (LPC_USB->USBClkSt != (DEV_CLK_ON | AHB_CLK_ON));\r
+\r
+ // Configure pins P0.29 and P0.30 to be USB D+ and USB D-\r
+ LPC_PINCON->PINSEL1 &= 0xc3ffffff;\r
+ LPC_PINCON->PINSEL1 |= 0x14000000;\r
+\r
+ // Disconnect USB device\r
+ SIEdisconnect();\r
+\r
+ // Configure pin P2.9 to be Connect\r
+ LPC_PINCON->PINSEL4 &= 0xfffcffff;\r
+ LPC_PINCON->PINSEL4 |= 0x00040000;\r
+\r
+ // Connect must be low for at least 2.5uS\r
+ wait(0.3);\r
+\r
+ // Set the maximum packet size for the control endpoints\r
+ realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);\r
+ realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);\r
+\r
+ // Attach IRQ\r
+ instance = this;\r
+ NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);\r
+ NVIC_EnableIRQ(USB_IRQn);\r
+\r
+ // Enable interrupts for device events and EP0\r
+ LPC_USB->USBDevIntEn = EP_SLOW | DEV_STAT | FRAME;\r
+ enableEndpointEvent(EP0IN);\r
+ enableEndpointEvent(EP0OUT);\r
+}\r
+\r
+USBHAL::~USBHAL(void) {\r
+ // Ensure device disconnected\r
+ SIEdisconnect();\r
+\r
+ // Disable USB interrupts\r
+ NVIC_DisableIRQ(USB_IRQn);\r
+}\r
+\r
+void USBHAL::connect(void) {\r
+ // Connect USB device\r
+ SIEconnect();\r
+}\r
+\r
+void USBHAL::disconnect(void) {\r
+ // Disconnect USB device\r
+ SIEdisconnect();\r
+}\r
+\r
+void USBHAL::configureDevice(void) {\r
+ SIEconfigureDevice();\r
+}\r
+\r
+void USBHAL::unconfigureDevice(void) {\r
+ SIEunconfigureDevice();\r
+}\r
+\r
+void USBHAL::setAddress(uint8_t address) {\r
+ SIEsetAddress(address);\r
+}\r
+\r
+void USBHAL::EP0setup(uint8_t *buffer) {\r
+ endpointReadcore(EP0OUT, buffer);\r
+}\r
+\r
+void USBHAL::EP0read(void) {\r
+ // Not required\r
+}\r
+\r
+uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {\r
+ return endpointReadcore(EP0OUT, buffer);\r
+}\r
+\r
+void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {\r
+ endpointWritecore(EP0IN, buffer, size);\r
+}\r
+\r
+void USBHAL::EP0getWriteResult(void) {\r
+ // Not required\r
+}\r
+\r
+void USBHAL::EP0stall(void) {\r
+ // This will stall both control endpoints\r
+ stallEndpoint(EP0OUT);\r
+}\r
+\r
+EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {\r
+ return EP_PENDING;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {\r
+\r
+ //for isochronous endpoint, we don't wait an interrupt\r
+ if ((endpoint >> 1) % 3 || (endpoint >> 1) == 0) {\r
+ if (!(epComplete & EP(endpoint)))\r
+ return EP_PENDING;\r
+ }\r
+ \r
+ *bytesRead = endpointReadcore(endpoint, buffer);\r
+ epComplete &= ~EP(endpoint);\r
+ return EP_COMPLETED;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {\r
+ if (getEndpointStallState(endpoint)) {\r
+ return EP_STALLED;\r
+ }\r
+\r
+ epComplete &= ~EP(endpoint);\r
+\r
+ endpointWritecore(endpoint, data, size);\r
+ return EP_PENDING;\r
+}\r
+\r
+EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {\r
+ if (epComplete & EP(endpoint)) {\r
+ epComplete &= ~EP(endpoint);\r
+ return EP_COMPLETED;\r
+ }\r
+\r
+ return EP_PENDING;\r
+}\r
+\r
+bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {\r
+ // Realise an endpoint\r
+ LPC_USB->USBDevIntClr = EP_RLZED;\r
+ LPC_USB->USBReEp |= EP(endpoint);\r
+ LPC_USB->USBEpInd = endpoint;\r
+ LPC_USB->USBMaxPSize = maxPacket;\r
+\r
+ while (!(LPC_USB->USBDevIntSt & EP_RLZED));\r
+ LPC_USB->USBDevIntClr = EP_RLZED;\r
+\r
+ // Clear stall state\r
+ endpointStallState &= ~EP(endpoint);\r
+\r
+ enableEndpointEvent(endpoint);\r
+ return true;\r
+}\r
+\r
+void USBHAL::stallEndpoint(uint8_t endpoint) {\r
+ // Stall an endpoint\r
+ if ( (endpoint==EP0IN) || (endpoint==EP0OUT) ) {\r
+ // Conditionally stall both control endpoints\r
+ SIEsetEndpointStatus(EP0OUT, SIE_SES_CND_ST);\r
+ } else {\r
+ SIEsetEndpointStatus(endpoint, SIE_SES_ST);\r
+\r
+ // Update stall state\r
+ endpointStallState |= EP(endpoint);\r
+ }\r
+}\r
+\r
+void USBHAL::unstallEndpoint(uint8_t endpoint) {\r
+ // Unstall an endpoint. The endpoint will also be reinitialised\r
+ SIEsetEndpointStatus(endpoint, 0);\r
+\r
+ // Update stall state\r
+ endpointStallState &= ~EP(endpoint);\r
+}\r
+\r
+bool USBHAL::getEndpointStallState(uint8_t endpoint) {\r
+ // Returns true if endpoint stalled\r
+ return endpointStallState & EP(endpoint);\r
+}\r
+\r
+void USBHAL::remoteWakeup(void) {\r
+ // Remote wakeup\r
+ uint8_t status;\r
+\r
+ // Enable USB clocks\r
+ LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN;\r
+ while (LPC_USB->USBClkSt != (DEV_CLK_ON | AHB_CLK_ON));\r
+\r
+ status = SIEgetDeviceStatus();\r
+ SIEsetDeviceStatus(status & ~SIE_DS_SUS);\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void USBHAL::_usbisr(void) {\r
+ instance->usbisr();\r
+}\r
+\r
+\r
+void USBHAL::usbisr(void) {\r
+ uint8_t devStat;\r
+\r
+ if (LPC_USB->USBDevIntSt & FRAME) {\r
+ // Start of frame event\r
+ SOF(SIEgetFrameNumber());\r
+ // Clear interrupt status flag\r
+ LPC_USB->USBDevIntClr = FRAME;\r
+ }\r
+\r
+ if (LPC_USB->USBDevIntSt & DEV_STAT) {\r
+ // Device Status interrupt\r
+ // Must clear the interrupt status flag before reading the device status from the SIE\r
+ LPC_USB->USBDevIntClr = DEV_STAT;\r
+\r
+ // Read device status from SIE\r
+ devStat = SIEgetDeviceStatus();\r
+\r
+ if (devStat & SIE_DS_RST) {\r
+ // Bus reset\r
+ busReset();\r
+ }\r
+ }\r
+\r
+ if (LPC_USB->USBDevIntSt & EP_SLOW) {\r
+ // (Slow) Endpoint Interrupt\r
+\r
+ // Process each endpoint interrupt\r
+ if (LPC_USB->USBEpIntSt & EP(EP0OUT)) {\r
+ if (selectEndpointClearInterrupt(EP0OUT) & SIE_SE_STP) {\r
+ // this is a setup packet\r
+ EP0setupCallback();\r
+ } else {\r
+ EP0out();\r
+ }\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP0IN)) {\r
+ selectEndpointClearInterrupt(EP0IN);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ EP0in();\r
+ }\r
+\r
+ // TODO: This should cover all endpoints, not just EP1,2,3:\r
+ if (LPC_USB->USBEpIntSt & EP(EP1IN)) {\r
+ selectEndpointClearInterrupt(EP1IN);\r
+ epComplete |= EP(EP1IN);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP1_IN_callback())\r
+ epComplete &= ~EP(EP1IN);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP1OUT)) {\r
+ selectEndpointClearInterrupt(EP1OUT);\r
+ epComplete |= EP(EP1OUT);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP1_OUT_callback())\r
+ epComplete &= ~EP(EP1OUT);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP2IN)) {\r
+ selectEndpointClearInterrupt(EP2IN);\r
+ epComplete |= EP(EP2IN);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP2_IN_callback())\r
+ epComplete &= ~EP(EP2IN);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP2OUT)) {\r
+ selectEndpointClearInterrupt(EP2OUT);\r
+ epComplete |= EP(EP2OUT);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP2_OUT_callback())\r
+ epComplete &= ~EP(EP2OUT);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP3IN)) {\r
+ selectEndpointClearInterrupt(EP3IN);\r
+ epComplete |= EP(EP3IN);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP3_IN_callback())\r
+ epComplete &= ~EP(EP3IN);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP3OUT)) {\r
+ selectEndpointClearInterrupt(EP3OUT);\r
+ epComplete |= EP(EP3OUT);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP3_OUT_callback())\r
+ epComplete &= ~EP(EP3OUT);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP5IN)) {\r
+ selectEndpointClearInterrupt(EP5IN);\r
+ epComplete |= EP(EP5IN);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP5_IN_callback())\r
+ epComplete &= ~EP(EP5IN);\r
+ }\r
+\r
+ if (LPC_USB->USBEpIntSt & EP(EP5OUT)) {\r
+ selectEndpointClearInterrupt(EP5OUT);\r
+ epComplete |= EP(EP5OUT);\r
+ LPC_USB->USBDevIntClr = EP_SLOW;\r
+ if (EP5_OUT_callback())\r
+ epComplete &= ~EP(EP5OUT);\r
+ }\r
+ }\r
+}\r
+\r
+#endif\r
--- /dev/null
+/* USBDescriptor.h */\r
+/* Definitions and macros for constructing USB descriptors */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+/* Standard descriptor types */\r
+#define DEVICE_DESCRIPTOR (1)\r
+#define CONFIGURATION_DESCRIPTOR (2)\r
+#define STRING_DESCRIPTOR (3)\r
+#define INTERFACE_DESCRIPTOR (4)\r
+#define ENDPOINT_DESCRIPTOR (5)\r
+#define QUALIFIER_DESCRIPTOR (6)\r
+\r
+/* Standard descriptor lengths */\r
+#define DEVICE_DESCRIPTOR_LENGTH (0x12)\r
+#define CONFIGURATION_DESCRIPTOR_LENGTH (0x09)\r
+#define INTERFACE_DESCRIPTOR_LENGTH (0x09)\r
+#define ENDPOINT_DESCRIPTOR_LENGTH (0x07)\r
+\r
+\r
+/*string offset*/\r
+#define STRING_OFFSET_LANGID (0) \r
+#define STRING_OFFSET_IMANUFACTURER (1)\r
+#define STRING_OFFSET_IPRODUCT (2)\r
+#define STRING_OFFSET_ISERIAL (3)\r
+#define STRING_OFFSET_ICONFIGURATION (4)\r
+#define STRING_OFFSET_IINTERFACE (5)\r
+\r
+/* USB Specification Release Number */\r
+#define USB_VERSION_2_0 (0x0200)\r
+\r
+/* Least/Most significant byte of short integer */\r
+#define LSB(n) ((n)&0xff)\r
+#define MSB(n) (((n)&0xff00)>>8)\r
+\r
+/* Convert physical endpoint number to descriptor endpoint number */\r
+#define PHY_TO_DESC(endpoint) (((endpoint)>>1) | (((endpoint) & 1) ? 0x80:0))\r
+\r
+/* bmAttributes in configuration descriptor */\r
+/* C_RESERVED must always be set */\r
+#define C_RESERVED (1U<<7)\r
+#define C_SELF_POWERED (1U<<6)\r
+#define C_REMOTE_WAKEUP (1U<<5)\r
+\r
+/* bMaxPower in configuration descriptor */\r
+#define C_POWER(mA) ((mA)/2)\r
+\r
+/* bmAttributes in endpoint descriptor */\r
+#define E_CONTROL (0x00)\r
+#define E_ISOCHRONOUS (0x01)\r
+#define E_BULK (0x02)\r
+#define E_INTERRUPT (0x03)\r
+\r
+/* For isochronous endpoints only: */\r
+#define E_NO_SYNCHRONIZATION (0x00)\r
+#define E_ASYNCHRONOUS (0x04)\r
+#define E_ADAPTIVE (0x08)\r
+#define E_SYNCHRONOUS (0x0C)\r
+#define E_DATA (0x00)\r
+#define E_FEEDBACK (0x10)\r
+#define E_IMPLICIT_FEEDBACK (0x20)\r
--- /dev/null
+/* USBDevice.c */\r
+/* Generic USB device */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+/* Reference: */\r
+/* Universal Serial Bus Specification Revision 2.0, Chapter 9 "USB Device Framework" */\r
+\r
+#include "stdint.h"\r
+\r
+#include "USBEndpoints.h"\r
+#include "USBDevice.h"\r
+#include "USBDescriptor.h"\r
+#include "USBHID_Types.h"\r
+\r
+\r
+/* Device status */\r
+#define DEVICE_STATUS_SELF_POWERED (1U<<0)\r
+#define DEVICE_STATUS_REMOTE_WAKEUP (1U<<1)\r
+\r
+/* Endpoint status */\r
+#define ENDPOINT_STATUS_HALT (1U<<0)\r
+\r
+/* Standard feature selectors */\r
+#define DEVICE_REMOTE_WAKEUP (1)\r
+#define ENDPOINT_HALT (0)\r
+\r
+/* Macro to convert wIndex endpoint number to physical endpoint number */\r
+#define WINDEX_TO_PHYSICAL(endpoint) (((endpoint & 0x0f) << 1) + \\r
+ ((endpoint & 0x80) ? 1 : 0))\r
+\r
+\r
+bool USBDevice::requestGetDescriptor(void)\r
+{\r
+ bool success = false;\r
+\r
+ switch (DESCRIPTOR_TYPE(transfer.setup.wValue))\r
+ {\r
+ case DEVICE_DESCRIPTOR:\r
+ if (deviceDesc() != NULL)\r
+ {\r
+ if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \\r
+ && (deviceDesc()[1] == DEVICE_DESCRIPTOR))\r
+ {\r
+ transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;\r
+ transfer.ptr = deviceDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ }\r
+ }\r
+ break;\r
+ case CONFIGURATION_DESCRIPTOR:\r
+ if (configurationDesc() != NULL)\r
+ {\r
+ if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH) \\r
+ && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))\r
+ {\r
+ /* Get wTotalLength */\r
+ transfer.remaining = configurationDesc()[2] \\r
+ | (configurationDesc()[3] << 8);\r
+\r
+ transfer.ptr = configurationDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ }\r
+ }\r
+ break;\r
+ case STRING_DESCRIPTOR:\r
+ switch (DESCRIPTOR_INDEX(transfer.setup.wValue))\r
+ {\r
+ case STRING_OFFSET_LANGID:\r
+ transfer.remaining = stringLangidDesc()[0];\r
+ transfer.ptr = stringLangidDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case STRING_OFFSET_IMANUFACTURER:\r
+ transfer.remaining = stringImanufacturerDesc()[0];\r
+ transfer.ptr = stringImanufacturerDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break; \r
+ case STRING_OFFSET_IPRODUCT:\r
+ transfer.remaining = stringIproductDesc()[0];\r
+ transfer.ptr = stringIproductDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break; \r
+ case STRING_OFFSET_ISERIAL:\r
+ transfer.remaining = stringIserialDesc()[0];\r
+ transfer.ptr = stringIserialDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break; \r
+ case STRING_OFFSET_ICONFIGURATION:\r
+ transfer.remaining = stringIConfigurationDesc()[0];\r
+ transfer.ptr = stringIConfigurationDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break; \r
+ case STRING_OFFSET_IINTERFACE:\r
+ transfer.remaining = stringIinterfaceDesc()[0];\r
+ transfer.ptr = stringIinterfaceDesc();\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break; \r
+ }\r
+ break;\r
+ case INTERFACE_DESCRIPTOR:\r
+ case ENDPOINT_DESCRIPTOR:\r
+ /* TODO: Support is optional, not implemented here */\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+void USBDevice::decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet)\r
+{\r
+ /* Fill in the elements of a SETUP_PACKET structure from raw data */\r
+ packet->bmRequestType.dataTransferDirection = (data[0] & 0x80) >> 7;\r
+ packet->bmRequestType.Type = (data[0] & 0x60) >> 5;\r
+ packet->bmRequestType.Recipient = data[0] & 0x1f;\r
+ packet->bRequest = data[1];\r
+ packet->wValue = (data[2] | (uint16_t)data[3] << 8);\r
+ packet->wIndex = (data[4] | (uint16_t)data[5] << 8);\r
+ packet->wLength = (data[6] | (uint16_t)data[7] << 8);\r
+}\r
+\r
+\r
+bool USBDevice::controlOut(void)\r
+{\r
+ /* Control transfer data OUT stage */\r
+ uint8_t buffer[MAX_PACKET_SIZE_EP0];\r
+ uint32_t packetSize;\r
+\r
+ /* Check we should be transferring data OUT */\r
+ if (transfer.direction != HOST_TO_DEVICE)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Read from endpoint */\r
+ packetSize = EP0getReadResult(buffer);\r
+\r
+ /* Check if transfer size is valid */\r
+ if (packetSize > transfer.remaining)\r
+ {\r
+ /* Too big */\r
+ return false;\r
+ }\r
+\r
+ /* Update transfer */\r
+ transfer.ptr += packetSize;\r
+ transfer.remaining -= packetSize;\r
+\r
+ /* Check if transfer has completed */\r
+ if (transfer.remaining == 0)\r
+ {\r
+ /* Transfer completed */\r
+ if (transfer.notify)\r
+ {\r
+ /* Notify class layer. */\r
+ USBCallback_requestCompleted(buffer, packetSize);\r
+ transfer.notify = false;\r
+ }\r
+ /* Status stage */\r
+ EP0write(NULL, 0);\r
+ }\r
+ else\r
+ {\r
+ EP0read();\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool USBDevice::controlIn(void)\r
+{\r
+ /* Control transfer data IN stage */\r
+ uint32_t packetSize;\r
+\r
+ /* Check if transfer has completed (status stage transactions */\r
+ /* also have transfer.remaining == 0) */\r
+ if (transfer.remaining == 0)\r
+ {\r
+ if (transfer.zlp)\r
+ {\r
+ /* Send zero length packet */\r
+ EP0write(NULL, 0);\r
+ transfer.zlp = false;\r
+ }\r
+\r
+ /* Transfer completed */\r
+ if (transfer.notify)\r
+ {\r
+ /* Notify class layer. */\r
+ USBCallback_requestCompleted(NULL, 0);\r
+ transfer.notify = false;\r
+ }\r
+\r
+ EP0read();\r
+\r
+ /* Completed */\r
+ return true;\r
+ }\r
+\r
+ /* Check we should be transferring data IN */\r
+ if (transfer.direction != DEVICE_TO_HOST)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ packetSize = transfer.remaining;\r
+\r
+ if (packetSize > MAX_PACKET_SIZE_EP0)\r
+ {\r
+ packetSize = MAX_PACKET_SIZE_EP0;\r
+ }\r
+\r
+ /* Write to endpoint */\r
+ EP0write(transfer.ptr, packetSize);\r
+\r
+ /* Update transfer */\r
+ transfer.ptr += packetSize;\r
+ transfer.remaining -= packetSize;\r
+\r
+ return true;\r
+}\r
+\r
+bool USBDevice::requestSetAddress(void)\r
+{\r
+ /* Set the device address */\r
+ setAddress(transfer.setup.wValue);\r
+\r
+ if (transfer.setup.wValue == 0)\r
+ {\r
+ device.state = DEFAULT;\r
+ }\r
+ else\r
+ {\r
+ device.state = ADDRESS;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool USBDevice::requestSetConfiguration(void)\r
+{\r
+\r
+ device.configuration = transfer.setup.wValue;\r
+ /* Set the device configuration */\r
+ if (device.configuration == 0)\r
+ {\r
+ /* Not configured */\r
+ unconfigureDevice();\r
+ device.state = ADDRESS;\r
+ }\r
+ else\r
+ {\r
+ if (USBCallback_setConfiguration(device.configuration))\r
+ {\r
+ /* Valid configuration */\r
+ configureDevice();\r
+ device.state = CONFIGURED;\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+bool USBDevice::requestGetConfiguration(void)\r
+{\r
+ /* Send the device configuration */\r
+ transfer.ptr = &device.configuration;\r
+ transfer.remaining = sizeof(device.configuration);\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ return true;\r
+}\r
+\r
+bool USBDevice::requestGetInterface(void)\r
+{\r
+ /* Return the selected alternate setting for an interface */\r
+\r
+ if (device.state != CONFIGURED)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Send the alternate setting */\r
+ transfer.setup.wIndex = currentInterface;\r
+ transfer.ptr = ¤tAlternate;\r
+ transfer.remaining = sizeof(currentAlternate);\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ return true;\r
+}\r
+\r
+bool USBDevice::requestSetInterface(void)\r
+{\r
+ bool success = false;\r
+ if(USBCallback_setInterface(transfer.setup.wIndex, transfer.setup.wValue))\r
+ {\r
+ success = true;\r
+ currentInterface = transfer.setup.wIndex;\r
+ currentAlternate = transfer.setup.wValue; \r
+ }\r
+ return success;\r
+}\r
+\r
+bool USBDevice::requestSetFeature()\r
+{\r
+ bool success = false;\r
+\r
+ if (device.state != CONFIGURED)\r
+ {\r
+ /* Endpoint or interface must be zero */\r
+ if (transfer.setup.wIndex != 0)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ switch (transfer.setup.bmRequestType.Recipient)\r
+ {\r
+ case DEVICE_RECIPIENT:\r
+ /* TODO: Remote wakeup feature not supported */\r
+ break;\r
+ case ENDPOINT_RECIPIENT:\r
+ if (transfer.setup.wValue == ENDPOINT_HALT)\r
+ {\r
+ /* TODO: We should check that the endpoint number is valid */\r
+ stallEndpoint(\r
+ WINDEX_TO_PHYSICAL(transfer.setup.wIndex));\r
+ success = true;\r
+ }\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+bool USBDevice::requestClearFeature()\r
+{\r
+ bool success = false;\r
+\r
+ if (device.state != CONFIGURED)\r
+ {\r
+ /* Endpoint or interface must be zero */\r
+ if (transfer.setup.wIndex != 0)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ switch (transfer.setup.bmRequestType.Recipient)\r
+ {\r
+ case DEVICE_RECIPIENT:\r
+ /* TODO: Remote wakeup feature not supported */\r
+ break;\r
+ case ENDPOINT_RECIPIENT:\r
+ /* TODO: We should check that the endpoint number is valid */\r
+ if (transfer.setup.wValue == ENDPOINT_HALT)\r
+ {\r
+ unstallEndpoint( WINDEX_TO_PHYSICAL(transfer.setup.wIndex));\r
+ success = true;\r
+ }\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+bool USBDevice::requestGetStatus(void)\r
+{\r
+ static uint16_t status;\r
+ bool success = false;\r
+\r
+ if (device.state != CONFIGURED)\r
+ {\r
+ /* Endpoint or interface must be zero */\r
+ if (transfer.setup.wIndex != 0)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ switch (transfer.setup.bmRequestType.Recipient)\r
+ {\r
+ case DEVICE_RECIPIENT:\r
+ /* TODO: Currently only supports self powered devices */\r
+ status = DEVICE_STATUS_SELF_POWERED;\r
+ success = true;\r
+ break;\r
+ case INTERFACE_RECIPIENT:\r
+ status = 0;\r
+ success = true;\r
+ break;\r
+ case ENDPOINT_RECIPIENT:\r
+ /* TODO: We should check that the endpoint number is valid */\r
+ if (getEndpointStallState(\r
+ WINDEX_TO_PHYSICAL(transfer.setup.wIndex)))\r
+ {\r
+ status = ENDPOINT_STATUS_HALT;\r
+ }\r
+ else\r
+ {\r
+ status = 0;\r
+ }\r
+ success = true;\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+\r
+ if (success)\r
+ {\r
+ /* Send the status */ \r
+ transfer.ptr = (uint8_t *)&status; /* Assumes little endian */\r
+ transfer.remaining = sizeof(status);\r
+ transfer.direction = DEVICE_TO_HOST;\r
+ }\r
+ \r
+ return success;\r
+}\r
+\r
+bool USBDevice::requestSetup(void)\r
+{\r
+ bool success = false;\r
+\r
+ /* Process standard requests */\r
+ if ((transfer.setup.bmRequestType.Type == STANDARD_TYPE))\r
+ {\r
+ switch (transfer.setup.bRequest)\r
+ {\r
+ case GET_STATUS:\r
+ success = requestGetStatus();\r
+ break;\r
+ case CLEAR_FEATURE:\r
+ success = requestClearFeature();\r
+ break;\r
+ case SET_FEATURE:\r
+ success = requestSetFeature();\r
+ break;\r
+ case SET_ADDRESS:\r
+ success = requestSetAddress();\r
+ break;\r
+ case GET_DESCRIPTOR:\r
+ success = requestGetDescriptor();\r
+ break;\r
+ case SET_DESCRIPTOR:\r
+ /* TODO: Support is optional, not implemented here */\r
+ success = false;\r
+ break;\r
+ case GET_CONFIGURATION:\r
+ success = requestGetConfiguration();\r
+ break;\r
+ case SET_CONFIGURATION:\r
+ success = requestSetConfiguration();\r
+ break;\r
+ case GET_INTERFACE:\r
+ success = requestGetInterface();\r
+ break;\r
+ case SET_INTERFACE:\r
+ success = requestSetInterface();\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+bool USBDevice::controlSetup(void)\r
+{\r
+ bool success = false;\r
+\r
+ /* Control transfer setup stage */\r
+ uint8_t buffer[MAX_PACKET_SIZE_EP0];\r
+\r
+ EP0setup(buffer);\r
+\r
+ /* Initialise control transfer state */\r
+ decodeSetupPacket(buffer, &transfer.setup);\r
+ transfer.ptr = NULL;\r
+ transfer.remaining = 0;\r
+ transfer.direction = 0;\r
+ transfer.zlp = false;\r
+ transfer.notify = false;\r
+\r
+ /* Class / vendor specific */\r
+ success = USBCallback_request();\r
+\r
+ if (!success)\r
+ {\r
+ /* Standard requests */\r
+ if (!requestSetup())\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /* Check transfer size and direction */\r
+ if (transfer.setup.wLength>0)\r
+ {\r
+ if (transfer.setup.bmRequestType.dataTransferDirection \\r
+ == DEVICE_TO_HOST)\r
+ {\r
+ /* IN data stage is required */\r
+ if (transfer.direction != DEVICE_TO_HOST)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Transfer must be less than or equal to the size */\r
+ /* requested by the host */\r
+ if (transfer.remaining > transfer.setup.wLength)\r
+ {\r
+ transfer.remaining = transfer.setup.wLength;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ \r
+ /* OUT data stage is required */\r
+ if (transfer.direction != HOST_TO_DEVICE)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Transfer must be equal to the size requested by the host */\r
+ if (transfer.remaining != transfer.setup.wLength)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* No data stage; transfer size must be zero */\r
+ if (transfer.remaining != 0)\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /* Data or status stage if applicable */\r
+ if (transfer.setup.wLength>0)\r
+ {\r
+ if (transfer.setup.bmRequestType.dataTransferDirection \\r
+ == DEVICE_TO_HOST)\r
+ {\r
+ /* Check if we'll need to send a zero length packet at */\r
+ /* the end of this transfer */\r
+ if (transfer.setup.wLength > transfer.remaining)\r
+ {\r
+ /* Device wishes to transfer less than host requested */\r
+ if ((transfer.remaining % MAX_PACKET_SIZE_EP0) == 0)\r
+ {\r
+ /* Transfer is a multiple of EP0 max packet size */\r
+ transfer.zlp = true;\r
+ }\r
+ }\r
+\r
+ /* IN stage */\r
+ controlIn();\r
+ }\r
+ else\r
+ {\r
+ /* OUT stage */\r
+ EP0read();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* Status stage */\r
+ EP0write(NULL, 0);\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+void USBDevice::busReset(void)\r
+{\r
+ device.state = DEFAULT;\r
+ device.configuration = 0;\r
+ device.suspended = false;\r
+\r
+ /* Call class / vendor specific busReset function */\r
+ USBCallback_busReset();\r
+}\r
+\r
+void USBDevice::EP0setupCallback(void)\r
+{\r
+ /* Endpoint 0 setup event */\r
+ if (!controlSetup())\r
+ {\r
+ /* Protocol stall */\r
+ EP0stall();\r
+ }\r
+\r
+ /* Return true if an OUT data stage is expected */\r
+}\r
+\r
+void USBDevice::EP0out(void)\r
+{\r
+ /* Endpoint 0 OUT data event */\r
+ if (!controlOut())\r
+ {\r
+ /* Protocol stall; this will stall both endpoints */\r
+ EP0stall();\r
+ }\r
+}\r
+\r
+void USBDevice::EP0in(void)\r
+{\r
+ /* Endpoint 0 IN data event */\r
+ if (!controlIn())\r
+ {\r
+ /* Protocol stall; this will stall both endpoints */\r
+ EP0stall();\r
+ }\r
+}\r
+\r
+bool USBDevice::configured(void)\r
+{\r
+ /* Returns true if device is in the CONFIGURED state */\r
+ return (device.state == CONFIGURED);\r
+}\r
+\r
+void USBDevice::connect(void)\r
+{\r
+ /* Connect device */\r
+ USBHAL::connect();\r
+ /* Block if not configured */\r
+ while (!configured());\r
+}\r
+\r
+void USBDevice::disconnect(void)\r
+{\r
+ /* Disconnect device */\r
+ USBHAL::disconnect();\r
+}\r
+\r
+CONTROL_TRANSFER * USBDevice::getTransferPtr(void)\r
+{\r
+ return &transfer;\r
+}\r
+\r
+bool USBDevice::addEndpoint(uint8_t endpoint, uint32_t maxPacket)\r
+{\r
+ return realiseEndpoint(endpoint, maxPacket, 0);\r
+}\r
+\r
+bool USBDevice::addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket)\r
+{\r
+ /* For interrupt endpoints only */\r
+ return realiseEndpoint(endpoint, maxPacket, RATE_FEEDBACK_MODE);\r
+}\r
+\r
+uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)\r
+{\r
+ /* Find a descriptor within the list of descriptors */\r
+ /* following a configuration descriptor. */\r
+ uint16_t wTotalLength;\r
+ uint8_t *ptr;\r
+\r
+ if (configurationDesc() == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ /* Check this is a configuration descriptor */\r
+ if ((configurationDesc()[0] != CONFIGURATION_DESCRIPTOR_LENGTH) \\r
+ || (configurationDesc()[1] != CONFIGURATION_DESCRIPTOR))\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ wTotalLength = configurationDesc()[2] | (configurationDesc()[3] << 8);\r
+\r
+ /* Check there are some more descriptors to follow */\r
+ if (wTotalLength <= (CONFIGURATION_DESCRIPTOR_LENGTH+2))\r
+ /* +2 is for bLength and bDescriptorType of next descriptor */\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Start at first descriptor after the configuration descriptor */\r
+ ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]);\r
+\r
+ do {\r
+ if (ptr[1] /* bDescriptorType */ == descriptorType)\r
+ {\r
+ /* Found */\r
+ return ptr;\r
+ }\r
+\r
+ /* Skip to next descriptor */\r
+ ptr += ptr[0]; /* bLength */\r
+ } while (ptr < (configurationDesc() + wTotalLength));\r
+\r
+ /* Reached end of the descriptors - not found */\r
+ return NULL;\r
+}\r
+\r
+\r
+void USBDevice::connectStateChanged(unsigned int connected)\r
+{\r
+}\r
+\r
+void USBDevice::suspendStateChanged(unsigned int suspended)\r
+{\r
+}\r
+\r
+\r
+USBDevice::USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release){\r
+ VENDOR_ID = vendor_id; \r
+ PRODUCT_ID = product_id; \r
+ PRODUCT_RELEASE = product_release;\r
+\r
+ /* Set initial device state */\r
+ device.state = POWERED;\r
+ device.configuration = 0;\r
+ device.suspended = false;\r
+};\r
+\r
+\r
+bool USBDevice::readStart(uint8_t endpoint, uint16_t maxSize)\r
+{\r
+ return endpointRead(endpoint, maxSize) == EP_PENDING;\r
+}\r
+\r
+\r
+bool USBDevice::write(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize)\r
+{\r
+ EP_STATUS result;\r
+\r
+ if (size > maxSize)\r
+ {\r
+ return false;\r
+ }\r
+ \r
+ \r
+ if(!configured()) {\r
+ return false;\r
+ }\r
+ \r
+ /* Send report */\r
+ result = endpointWrite(endpoint, buffer, size);\r
+\r
+ if (result != EP_PENDING)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /* Wait for completion */\r
+ do {\r
+ result = endpointWriteResult(endpoint);\r
+ } while ((result == EP_PENDING) && configured());\r
+\r
+ return (result == EP_COMPLETED);\r
+}\r
+\r
+\r
+bool USBDevice::writeNB(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize)\r
+{\r
+ EP_STATUS result;\r
+\r
+ if (size > maxSize)\r
+ {\r
+ return false;\r
+ }\r
+ \r
+ if(!configured()) {\r
+ return false;\r
+ }\r
+\r
+ /* Send report */\r
+ result = endpointWrite(endpoint, buffer, size);\r
+\r
+ if (result != EP_PENDING)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ result = endpointWriteResult(endpoint);\r
+\r
+ return (result == EP_COMPLETED);\r
+}\r
+\r
+\r
+\r
+bool USBDevice::readEP(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize)\r
+{\r
+ EP_STATUS result;\r
+ \r
+ if(!configured()) {\r
+ return false;\r
+ }\r
+\r
+ /* Wait for completion */\r
+ do {\r
+ result = endpointReadResult(endpoint, buffer, (uint32_t *)size);\r
+ } while ((result == EP_PENDING) && configured());\r
+\r
+ return (result == EP_COMPLETED);\r
+}\r
+\r
+\r
+bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize)\r
+{\r
+ EP_STATUS result;\r
+ \r
+ if(!configured()) {\r
+ return false;\r
+ }\r
+\r
+ result = endpointReadResult(endpoint, buffer, (uint32_t *)size);\r
+ \r
+ return (result == EP_COMPLETED);\r
+}\r
+\r
+\r
+\r
+uint8_t * USBDevice::deviceDesc() {\r
+ static uint8_t deviceDescriptor[] = {\r
+ DEVICE_DESCRIPTOR_LENGTH, /* bLength */\r
+ DEVICE_DESCRIPTOR, /* bDescriptorType */\r
+ LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */\r
+ MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */\r
+ 0x00, /* bDeviceClass */\r
+ 0x00, /* bDeviceSubClass */\r
+ 0x00, /* bDeviceprotocol */\r
+ MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */\r
+ LSB(VENDOR_ID), /* idVendor (LSB) */\r
+ MSB(VENDOR_ID), /* idVendor (MSB) */\r
+ LSB(PRODUCT_ID), /* idProduct (LSB) */\r
+ MSB(PRODUCT_ID), /* idProduct (MSB) */\r
+ LSB(PRODUCT_RELEASE), /* bcdDevice (LSB) */\r
+ MSB(PRODUCT_RELEASE), /* bcdDevice (MSB) */\r
+ STRING_OFFSET_IMANUFACTURER, /* iManufacturer */\r
+ STRING_OFFSET_IPRODUCT, /* iProduct */\r
+ STRING_OFFSET_ISERIAL, /* iSerialNumber */\r
+ 0x01 /* bNumConfigurations */\r
+ };\r
+ return deviceDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringLangidDesc() {\r
+ static uint8_t stringLangidDescriptor[] = {\r
+ 0x04, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ 0x09,0x00, /*bString Lang ID - 0x009 - English*/\r
+ };\r
+ return stringLangidDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringImanufacturerDesc() {\r
+ static uint8_t stringImanufacturerDescriptor[] = {\r
+ 0x12, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ 'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/\r
+ };\r
+ return stringImanufacturerDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringIserialDesc() {\r
+ static uint8_t stringIserialDescriptor[] = {\r
+ 0x16, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ '0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/\r
+ };\r
+ return stringIserialDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringIConfigurationDesc() {\r
+ static uint8_t stringIconfigurationDescriptor[] = {\r
+ 0x06, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ '0',0,'1',0, /*bString iConfiguration - 01*/\r
+ };\r
+ return stringIconfigurationDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x08, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ 'U',0,'S',0,'B',0, /*bString iInterface - USB*/\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBDevice::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x16, /*bLength*/\r
+ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/\r
+ 'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
--- /dev/null
+/* USBDevice.h */\r
+/* Generic USB device */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#ifndef USBDEVICE_H\r
+#define USBDEVICE_H\r
+\r
+#include "mbed.h"\r
+#include "USBDevice_Types.h"\r
+#include "USBBusInterface.h"\r
+\r
+\r
+\r
+class USBDevice: public USBHAL\r
+{\r
+public:\r
+ USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);\r
+ \r
+ /*\r
+ * Check if the device is configured\r
+ *\r
+ * @returns true if configured, false otherwise\r
+ */\r
+ bool configured(void);\r
+ \r
+ /*\r
+ * Connect a device\r
+ */\r
+ void connect(void);\r
+ \r
+ /*\r
+ * Disconnect a device\r
+ */\r
+ void disconnect(void);\r
+ \r
+ /*\r
+ * Add an endpoint\r
+ *\r
+ * @param endpoint endpoint which will be added\r
+ * @param maxPacket Maximum size of a packet which can be sent for this endpoint\r
+ * @returns true if successful, false otherwise\r
+ */\r
+ bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);\r
+\r
+ /*\r
+ * Start a reading on a certain endpoint.\r
+ * You can access the result of the reading by USBDevice_read\r
+ *\r
+ * @param endpoint endpoint which will be read\r
+ * @param maxSize the maximum length that can be read\r
+ * @return true if successful\r
+ */\r
+ bool readStart(uint8_t endpoint, uint16_t maxSize);\r
+ \r
+ /*\r
+ * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart\r
+ * must be called.\r
+ *\r
+ * Warning: blocking\r
+ *\r
+ * @param endpoint endpoint which will be read\r
+ * @param buffer buffer will be filled with the data received\r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);\r
+ \r
+ /*\r
+ * Read a certain endpoint.\r
+ *\r
+ * Warning: non blocking\r
+ *\r
+ * @param endpoint endpoint which will be read\r
+ * @param buffer buffer will be filled with the data received (if data are available) \r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint16_t * size, uint16_t maxSize);\r
+ \r
+ /*\r
+ * Write a certain endpoint.\r
+ *\r
+ * Warning: blocking\r
+ *\r
+ * @param endpoint endpoint to write\r
+ * @param buffer data contained in buffer will be write\r
+ * @param size the number of bytes to write\r
+ * @param maxSize the maximum length that can be written on this endpoint\r
+ */\r
+ bool write(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);\r
+ \r
+ \r
+ /*\r
+ * Write a certain endpoint.\r
+ *\r
+ * Warning: non blocking\r
+ *\r
+ * @param endpoint endpoint to write\r
+ * @param buffer data contained in buffer will be write\r
+ * @param size the number of bytes to write\r
+ * @param maxSize the maximum length that can be written on this endpoint\r
+ */\r
+ bool writeNB(uint8_t endpoint, uint8_t * buffer, uint16_t size, uint16_t maxSize);\r
+\r
+ \r
+ /*\r
+ * Called by USBDevice layer on bus reset. Warning: Called in ISR context\r
+ *\r
+ * May be used to reset state\r
+ */\r
+ virtual void USBCallback_busReset(void) {};\r
+ \r
+ /*\r
+ * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context\r
+ * This is used to handle extensions to standard requests\r
+ * and class specific requests\r
+ *\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_request() { return false; }; \r
+ \r
+ /*\r
+ * Called by USBDevice on Endpoint0 request completion\r
+ * if the 'notify' flag has been set to true. Warning: Called in ISR context\r
+ *\r
+ * In this case it is used to indicate that a HID report has\r
+ * been received from the host on endpoint 0\r
+ *\r
+ * @param buf buffer received on endpoint 0\r
+ * @param length length of this buffer\r
+ */\r
+ virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length) {};\r
+ \r
+ /*\r
+ * Called by USBDevice layer. Set configuration of the device.\r
+ * For instance, you can add all endpoints that you need on this function.\r
+ *\r
+ * @param configuration Number of the configuration\r
+ */\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };\r
+ \r
+ /*\r
+ * Called by USBDevice layer. Set interface/alternate of the device.\r
+ *\r
+ * @param interface Number of the interface to be configured\r
+ * @param alternate Number of the alternate to be configured\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };\r
+\r
+ /*\r
+ * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.\r
+ *\r
+ * @returns pointer to the device descriptor\r
+ */\r
+ virtual uint8_t * deviceDesc();\r
+ \r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc(){return NULL;};\r
+ \r
+ /*\r
+ * Get string lang id descriptor\r
+ *\r
+ * @return pointer to the string lang id descriptor\r
+ */\r
+ virtual uint8_t * stringLangidDesc();\r
+ \r
+ /*\r
+ * Get string manufacturer descriptor\r
+ *\r
+ * @returns pointer to the string manufacturer descriptor\r
+ */\r
+ virtual uint8_t * stringImanufacturerDesc();\r
+ \r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+ \r
+ /*\r
+ * Get string serial descriptor\r
+ *\r
+ * @returns pointer to the string serial descriptor\r
+ */\r
+ virtual uint8_t * stringIserialDesc();\r
+ \r
+ /*\r
+ * Get string configuration descriptor\r
+ *\r
+ * @returns pointer to the string configuration descriptor\r
+ */\r
+ virtual uint8_t * stringIConfigurationDesc();\r
+ \r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+ \r
+ /*\r
+ * Get the length of the report descriptor\r
+ *\r
+ * @returns length of the report descriptor\r
+ */\r
+ virtual uint16_t reportDescLength() { return 0; };\r
+ \r
+\r
+\r
+protected:\r
+ virtual void busReset(void);\r
+ virtual void EP0setupCallback(void);\r
+ virtual void EP0out(void);\r
+ virtual void EP0in(void);\r
+ virtual void connectStateChanged(unsigned int connected);\r
+ virtual void suspendStateChanged(unsigned int suspended);\r
+ uint8_t * findDescriptor(uint8_t descriptorType);\r
+ CONTROL_TRANSFER * getTransferPtr(void);\r
+ \r
+ uint16_t VENDOR_ID;\r
+ uint16_t PRODUCT_ID;\r
+ uint16_t PRODUCT_RELEASE;\r
+\r
+private:\r
+ bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);\r
+ bool requestGetDescriptor(void);\r
+ bool controlOut(void);\r
+ bool controlIn(void);\r
+ bool requestSetAddress(void);\r
+ bool requestSetConfiguration(void);\r
+ bool requestSetFeature(void);\r
+ bool requestClearFeature(void);\r
+ bool requestGetStatus(void);\r
+ bool requestSetup(void);\r
+ bool controlSetup(void);\r
+ void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);\r
+ bool requestGetConfiguration(void);\r
+ bool requestGetInterface(void);\r
+ bool requestSetInterface(void);\r
+\r
+ CONTROL_TRANSFER transfer;\r
+ USB_DEVICE device;\r
+ \r
+ uint16_t currentInterface;\r
+ uint8_t currentAlternate;\r
+};\r
+\r
+\r
+#endif\r
--- /dev/null
+/* USBDevice_Types.h */\r
+/* USB Device type definitions, conversions and constants */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#ifndef USBDEVICE_TYPES_H\r
+#define USBDEVICE_TYPES_H\r
+\r
+/* Standard requests */\r
+#define GET_STATUS (0)\r
+#define CLEAR_FEATURE (1)\r
+#define SET_FEATURE (3)\r
+#define SET_ADDRESS (5)\r
+#define GET_DESCRIPTOR (6)\r
+#define SET_DESCRIPTOR (7)\r
+#define GET_CONFIGURATION (8)\r
+#define SET_CONFIGURATION (9)\r
+#define GET_INTERFACE (10)\r
+#define SET_INTERFACE (11)\r
+\r
+/* bmRequestType.dataTransferDirection */\r
+#define HOST_TO_DEVICE (0)\r
+#define DEVICE_TO_HOST (1)\r
+\r
+/* bmRequestType.Type*/\r
+#define STANDARD_TYPE (0)\r
+#define CLASS_TYPE (1)\r
+#define VENDOR_TYPE (2)\r
+#define RESERVED_TYPE (3)\r
+\r
+/* bmRequestType.Recipient */\r
+#define DEVICE_RECIPIENT (0)\r
+#define INTERFACE_RECIPIENT (1)\r
+#define ENDPOINT_RECIPIENT (2)\r
+#define OTHER_RECIPIENT (3)\r
+\r
+/* Descriptors */\r
+#define DESCRIPTOR_TYPE(wValue) (wValue >> 8)\r
+#define DESCRIPTOR_INDEX(wValue) (wValue & 0xf)\r
+\r
+typedef struct {\r
+ struct {\r
+ uint8_t dataTransferDirection;\r
+ uint8_t Type;\r
+ uint8_t Recipient;\r
+ } bmRequestType;\r
+ uint8_t bRequest;\r
+ uint16_t wValue;\r
+ uint16_t wIndex;\r
+ uint16_t wLength;\r
+} SETUP_PACKET;\r
+\r
+typedef struct {\r
+ SETUP_PACKET setup;\r
+ uint8_t *ptr;\r
+ uint32_t remaining;\r
+ uint8_t direction;\r
+ bool zlp;\r
+ bool notify;\r
+} CONTROL_TRANSFER;\r
+\r
+typedef enum {ATTACHED, POWERED, DEFAULT, ADDRESS, CONFIGURED} DEVICE_STATE;\r
+\r
+typedef struct {\r
+ volatile DEVICE_STATE state;\r
+ uint8_t configuration;\r
+ bool suspended;\r
+} USB_DEVICE;\r
+\r
+#endif\r
--- /dev/null
+/* USBEndpoints.h */\r
+/* USB endpoint configuration */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#ifndef USBENDPOINTS_H\r
+#define USBENDPOINTS_H\r
+\r
+/* SETUP packet size */\r
+#define SETUP_PACKET_SIZE (8)\r
+\r
+/* Options flags for configuring endpoints */\r
+#define DEFAULT_OPTIONS (0)\r
+#define SINGLE_BUFFERED (1U << 0)\r
+#define ISOCHRONOUS (1U << 1)\r
+#define RATE_FEEDBACK_MODE (1U << 2) /* Interrupt endpoints only */\r
+\r
+/* Endpoint transfer status, for endpoints > 0 */\r
+typedef enum {\r
+ EP_COMPLETED, /* Transfer completed */\r
+ EP_PENDING, /* Transfer in progress */\r
+ EP_INVALID, /* Invalid parameter */\r
+ EP_STALLED, /* Endpoint stalled */\r
+} EP_STATUS;\r
+\r
+/* Include configuration for specific target */\r
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)\r
+#include "USBEndpoints_LPC17_LPC23.h"\r
+#elif defined(TARGET_LPC11U24)\r
+#include "USBEndpoints_LPC11U.h"\r
+#else\r
+#error "Unknown target type"\r
+#endif\r
+\r
+#endif\r
--- /dev/null
+/* USBEndpoints_LPC11U.h */\r
+/* Endpoint configuration for LPC11U */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#define NUMBER_OF_LOGICAL_ENDPOINTS (5)\r
+#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)\r
+\r
+/* Define physical endpoint numbers */\r
+\r
+/* Endpoint No. Type(s) MaxPacket DoubleBuffer */\r
+/* ---------------- ------------ ---------- --- */\r
+#define EP0OUT (0) /* Control 64 No */\r
+#define EP0IN (1) /* Control 64 No */\r
+#define EP1OUT (2) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP1IN (3) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP2OUT (4) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP2IN (5) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP3OUT (6) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP3IN (7) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP4OUT (8) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+#define EP4IN (9) /* Int/Bulk/Iso 64/64/1023 Yes */\r
+\r
+/* Maximum Packet sizes */\r
+\r
+#define MAX_PACKET_SIZE_EP0 (64)\r
+#define MAX_PACKET_SIZE_EP1 (64) /* Int/Bulk */\r
+#define MAX_PACKET_SIZE_EP2 (64) /* Int/Bulk */\r
+#define MAX_PACKET_SIZE_EP3 (64) /* Int/Bulk */\r
+#define MAX_PACKET_SIZE_EP4 (64) /* Int/Bulk */\r
+\r
+#define MAX_PACKET_SIZE_EP1_ISO (1023) /* Isochronous */\r
+#define MAX_PACKET_SIZE_EP2_ISO (1023) /* Isochronous */\r
+#define MAX_PACKET_SIZE_EP3_ISO (1023) /* Isochronous */\r
+#define MAX_PACKET_SIZE_EP4_ISO (1023) /* Isochronous */\r
+\r
+/* Generic endpoints - intended to be portable accross devices */\r
+/* and be suitable for simple USB devices. */\r
+\r
+/* Bulk endpoint */\r
+#define EPBULK_OUT (EP2OUT)\r
+#define EPBULK_IN (EP2IN)\r
+/* Interrupt endpoint */\r
+#define EPINT_OUT (EP1OUT)\r
+#define EPINT_IN (EP1IN)\r
+/* Isochronous endpoint */\r
+#define EPISO_OUT (EP3OUT)\r
+#define EPISO_IN (EP3IN)\r
+\r
+#define MAX_PACKET_SIZE_EPBULK (MAX_PACKET_SIZE_EP2)\r
+#define MAX_PACKET_SIZE_EPINT (MAX_PACKET_SIZE_EP1)\r
+#define MAX_PACKET_SIZE_EPISO (MAX_PACKET_SIZE_EP3_ISO)\r
--- /dev/null
+/* USBEndpoints_LPC17_LPC23.h */\r
+/* Endpoint configuration for LPC1768 and LPC2368 */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#define NUMBER_OF_LOGICAL_ENDPOINTS (16)\r
+#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)\r
+\r
+/* Define physical endpoint numbers */\r
+\r
+/* Endpoint No. Type(s) MaxPacket DoubleBuffer */\r
+/* ---------------- ------------ ---------- --- */\r
+#define EP0OUT (0) /* Control 64 No */\r
+#define EP0IN (1) /* Control 64 No */\r
+#define EP1OUT (2) /* Interrupt 64 No */\r
+#define EP1IN (3) /* Interrupt 64 No */\r
+#define EP2OUT (4) /* Bulk 64 Yes */\r
+#define EP2IN (5) /* Bulk 64 Yes */\r
+#define EP3OUT (6) /* Isochronous 1023 Yes */\r
+#define EP3IN (7) /* Isochronous 1023 Yes */\r
+#define EP4OUT (8) /* Interrupt 64 No */\r
+#define EP4IN (9) /* Interrupt 64 No */\r
+#define EP5OUT (10) /* Bulk 64 Yes */\r
+#define EP5IN (11) /* Bulk 64 Yes */\r
+#define EP6OUT (12) /* Isochronous 1023 Yes */\r
+#define EP6IN (13) /* Isochronous 1023 Yes */\r
+#define EP7OUT (14) /* Interrupt 64 No */\r
+#define EP7IN (15) /* Interrupt 64 No */\r
+#define EP8OUT (16) /* Bulk 64 Yes */\r
+#define EP8IN (17) /* Bulk 64 Yes */\r
+#define EP9OUT (18) /* Isochronous 1023 Yes */\r
+#define EP9IN (19) /* Isochronous 1023 Yes */\r
+#define EP10OUT (20) /* Interrupt 64 No */\r
+#define EP10IN (21) /* Interrupt 64 No */\r
+#define EP11OUT (22) /* Bulk 64 Yes */\r
+#define EP11IN (23) /* Bulk 64 Yes */\r
+#define EP12OUT (24) /* Isochronous 1023 Yes */\r
+#define EP12IN (25) /* Isochronous 1023 Yes */\r
+#define EP13OUT (26) /* Interrupt 64 No */\r
+#define EP13IN (27) /* Interrupt 64 No */\r
+#define EP14OUT (28) /* Bulk 64 Yes */\r
+#define EP14IN (29) /* Bulk 64 Yes */\r
+#define EP15OUT (30) /* Bulk 64 Yes */\r
+#define EP15IN (31) /* Bulk 64 Yes */\r
+\r
+/* Maximum Packet sizes */\r
+\r
+#define MAX_PACKET_SIZE_EP0 (64)\r
+#define MAX_PACKET_SIZE_EP1 (64)\r
+#define MAX_PACKET_SIZE_EP2 (64)\r
+#define MAX_PACKET_SIZE_EP3 (1023)\r
+#define MAX_PACKET_SIZE_EP4 (64)\r
+#define MAX_PACKET_SIZE_EP5 (64)\r
+#define MAX_PACKET_SIZE_EP6 (1023)\r
+#define MAX_PACKET_SIZE_EP7 (64)\r
+#define MAX_PACKET_SIZE_EP8 (64)\r
+#define MAX_PACKET_SIZE_EP9 (1023)\r
+#define MAX_PACKET_SIZE_EP10 (64)\r
+#define MAX_PACKET_SIZE_EP11 (64)\r
+#define MAX_PACKET_SIZE_EP12 (1023)\r
+#define MAX_PACKET_SIZE_EP13 (64)\r
+#define MAX_PACKET_SIZE_EP14 (64)\r
+#define MAX_PACKET_SIZE_EP15 (64)\r
+\r
+/* Generic endpoints - intended to be portable accross devices */\r
+/* and be suitable for simple USB devices. */\r
+\r
+/* Bulk endpoints */\r
+#define EPBULK_OUT (EP2OUT)\r
+#define EPBULK_IN (EP2IN)\r
+/* Interrupt endpoints */\r
+#define EPINT_OUT (EP1OUT)\r
+#define EPINT_IN (EP1IN)\r
+/* Isochronous endpoints */\r
+#define EPISO_OUT (EP3OUT)\r
+#define EPISO_IN (EP3IN)\r
+\r
+#define MAX_PACKET_SIZE_EPBULK (MAX_PACKET_SIZE_EP2)\r
+#define MAX_PACKET_SIZE_EPINT (MAX_PACKET_SIZE_EP1)\r
+#define MAX_PACKET_SIZE_EPISO (MAX_PACKET_SIZE_EP3)\r
+\r
+/* Bulk endpoints */\r
+#define MSDBULK_OUT (EP5OUT)\r
+#define MSDBULK_IN (EP5IN)\r
+#define MAX_PACKET_SIZE_MSDBULK (MAX_PACKET_SIZE_EP5)\r
--- /dev/null
+// USBHID.c\r
+// Human Interface Device (HID) class\r
+// Copyright (c) 2011 ARM Limited. All rights reserved.\r
+\r
+#include "stdint.h"\r
+#include "USBBusInterface.h"\r
+#include "USBHID.h"\r
+\r
+\r
+USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)\r
+{\r
+ output_length = output_report_length;\r
+ input_length = input_report_length;\r
+ if(connect) {\r
+ USBDevice::connect();\r
+ }\r
+}\r
+\r
+\r
+bool USBHID::send(HID_REPORT *report)\r
+{\r
+ return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);\r
+}\r
+\r
+bool USBHID::sendNB(HID_REPORT *report)\r
+{\r
+ return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);\r
+}\r
+\r
+\r
+bool USBHID::read(HID_REPORT *report)\r
+{\r
+ uint16_t bytesRead = 0;\r
+ bool result;\r
+ result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);\r
+ if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))\r
+ return false;\r
+ report->length = bytesRead;\r
+ return result;\r
+}\r
+\r
+\r
+bool USBHID::readNB(HID_REPORT *report)\r
+{\r
+ uint16_t bytesRead = 0;\r
+ bool result;\r
+ result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);\r
+ report->length = bytesRead;\r
+ if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))\r
+ return false;\r
+ return result;\r
+}\r
+\r
+\r
+uint16_t USBHID::reportDescLength() {\r
+ reportDesc();\r
+ return reportLength;\r
+}\r
+\r
+\r
+\r
+//\r
+// Route callbacks from lower layers to class(es)\r
+//\r
+\r
+\r
+// Called in ISR context\r
+// Called by USBDevice on Endpoint0 request\r
+// This is used to handle extensions to standard requests\r
+// and class specific requests\r
+// Return true if class handles this request\r
+bool USBHID::USBCallback_request() {\r
+ bool success = false;\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+ uint8_t *hidDescriptor;\r
+\r
+ // Process additional standard requests\r
+\r
+ if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))\r
+ {\r
+ switch (transfer->setup.bRequest)\r
+ {\r
+ case GET_DESCRIPTOR:\r
+ switch (DESCRIPTOR_TYPE(transfer->setup.wValue))\r
+ {\r
+ case REPORT_DESCRIPTOR:\r
+ if ((reportDesc() != NULL) \\r
+ && (reportDescLength() != 0))\r
+ {\r
+ transfer->remaining = reportDescLength();\r
+ transfer->ptr = reportDesc();\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ }\r
+ break;\r
+ case HID_DESCRIPTOR:\r
+ // Find the HID descriptor, after the configuration descriptor\r
+ hidDescriptor = findDescriptor(HID_DESCRIPTOR);\r
+ if (hidDescriptor != NULL)\r
+ {\r
+ transfer->remaining = HID_DESCRIPTOR_LENGTH;\r
+ transfer->ptr = hidDescriptor;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ }\r
+ break;\r
+ \r
+ default:\r
+ break;\r
+ }\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ // Process class-specific requests\r
+\r
+ if (transfer->setup.bmRequestType.Type == CLASS_TYPE)\r
+ {\r
+ switch (transfer->setup.bRequest)\r
+ {\r
+ case SET_REPORT:\r
+ // First byte will be used for report ID\r
+ outputReport.data[0] = transfer->setup.wValue & 0xff;\r
+ outputReport.length = transfer->setup.wLength + 1;\r
+\r
+ transfer->remaining = sizeof(outputReport.data) - 1;\r
+ transfer->ptr = &outputReport.data[1];\r
+ transfer->direction = HOST_TO_DEVICE;\r
+ transfer->notify = true;\r
+ success = true;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the\r
+// configuration is not supported\r
+bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);\r
+ addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);\r
+\r
+ // We activate the endpoint to be able to recceive data\r
+ readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);\r
+ return true;\r
+}\r
+\r
+\r
+uint8_t * USBHID::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x08, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'H',0,'I',0,'D',0, //bString iInterface - HID\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBHID::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x16, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
+\r
+\r
+\r
+uint8_t * USBHID::reportDesc() {\r
+ static uint8_t reportDescriptor[] = {\r
+ 0x06, LSB(0xFFAB), MSB(0xFFAB),\r
+ 0x0A, LSB(0x0200), MSB(0x0200),\r
+ 0xA1, 0x01, // Collection 0x01\r
+ 0x75, 0x08, // report size = 8 bits\r
+ 0x15, 0x00, // logical minimum = 0\r
+ 0x26, 0xFF, 0x00, // logical maximum = 255\r
+ 0x95, input_length, // report count\r
+ 0x09, 0x01, // usage\r
+ 0x81, 0x02, // Input (array)\r
+ 0x95, output_length, // report count\r
+ 0x09, 0x02, // usage\r
+ 0x91, 0x02, // Output (array)\r
+ 0xC0 // end collection\r
+\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+}\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \\r
+ + (1 * INTERFACE_DESCRIPTOR_LENGTH) \\r
+ + (1 * HID_DESCRIPTOR_LENGTH) \\r
+ + (2 * ENDPOINT_DESCRIPTOR_LENGTH))\r
+\r
+uint8_t * USBHID::configurationDesc() {\r
+ static uint8_t configurationDescriptor[] = {\r
+ CONFIGURATION_DESCRIPTOR_LENGTH,// bLength\r
+ CONFIGURATION_DESCRIPTOR, // bDescriptorType\r
+ LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)\r
+ MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)\r
+ 0x01, // bNumInterfaces\r
+ DEFAULT_CONFIGURATION, // bConfigurationValue\r
+ 0x00, // iConfiguration\r
+ C_RESERVED | C_SELF_POWERED, // bmAttributes\r
+ C_POWER(0), // bMaxPower\r
+\r
+ INTERFACE_DESCRIPTOR_LENGTH, // bLength\r
+ INTERFACE_DESCRIPTOR, // bDescriptorType\r
+ 0x00, // bInterfaceNumber\r
+ 0x00, // bAlternateSetting\r
+ 0x02, // bNumEndpoints\r
+ HID_CLASS, // bInterfaceClass\r
+ HID_SUBCLASS_NONE, // bInterfaceSubClass\r
+ HID_PROTOCOL_NONE, // bInterfaceProtocol\r
+ 0x00, // iInterface\r
+\r
+ HID_DESCRIPTOR_LENGTH, // bLength\r
+ HID_DESCRIPTOR, // bDescriptorType\r
+ LSB(HID_VERSION_1_11), // bcdHID (LSB)\r
+ MSB(HID_VERSION_1_11), // bcdHID (MSB)\r
+ 0x00, // bCountryCode\r
+ 0x01, // bNumDescriptors\r
+ REPORT_DESCRIPTOR, // bDescriptorType\r
+ LSB(this->reportDescLength()), // wDescriptorLength (LSB)\r
+ MSB(this->reportDescLength()), // wDescriptorLength (MSB)\r
+\r
+ ENDPOINT_DESCRIPTOR_LENGTH, // bLength\r
+ ENDPOINT_DESCRIPTOR, // bDescriptorType\r
+ PHY_TO_DESC(EPINT_IN), // bEndpointAddress\r
+ E_INTERRUPT, // bmAttributes\r
+ LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)\r
+ 1, // bInterval (milliseconds)\r
+\r
+ ENDPOINT_DESCRIPTOR_LENGTH, // bLength\r
+ ENDPOINT_DESCRIPTOR, // bDescriptorType\r
+ PHY_TO_DESC(EPINT_OUT), // bEndpointAddress\r
+ E_INTERRUPT, // bmAttributes\r
+ LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)\r
+ 1, // bInterval (milliseconds)\r
+ };\r
+ return configurationDescriptor;\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USB_HID_H\r
+#define USB_HID_H\r
+\r
+/* These headers are included for child class. */\r
+#include "USBEndpoints.h"\r
+#include "USBDescriptor.h"\r
+#include "USBDevice_Types.h"\r
+\r
+#include "USBHID_Types.h"\r
+#include "USBDevice.h"\r
+\r
+\r
+/**\r
+ * USBHID example\r
+ * @code\r
+ * #include "mbed.h"\r
+ * #include "USBHID.h"\r
+ *\r
+ * USBHID hid;\r
+ * HID_REPORT recv;\r
+ * BusOut leds(LED1,LED2,LED3,LED4);\r
+ *\r
+ * int main(void) {\r
+ * while (1) {\r
+ * hid.read(&recv);\r
+ * leds = recv.data[0];\r
+ * }\r
+ * }\r
+ * @endcode\r
+ */\r
+\r
+class USBHID: public USBDevice {\r
+public:\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param output_report_length Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)\r
+ * @param input_report_length Maximum length of a received report (up to 64 bytes) (default: 64 bytes)\r
+ * @param vendor_id Your vendor_id\r
+ * @param product_id Your product_id\r
+ * @param product_release Your preoduct_release\r
+ * @param connect Connect the device\r
+ */\r
+ USBHID(uint8_t output_report_length = 64, uint8_t input_report_length = 64, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001, bool connect = true);\r
+\r
+\r
+ /**\r
+ * Send a Report. warning: blocking\r
+ *\r
+ * @param report Report which will be sent (a report is defined by all data and the length)\r
+ * @returns true if successful\r
+ */\r
+ bool send(HID_REPORT *report);\r
+ \r
+ \r
+ /**\r
+ * Send a Report. warning: non blocking\r
+ *\r
+ * @param report Report which will be sent (a report is defined by all data and the length)\r
+ * @returns true if successful\r
+ */\r
+ bool sendNB(HID_REPORT *report);\r
+ \r
+ /**\r
+ * Read a report: blocking\r
+ *\r
+ * @param report pointer to the report to fill\r
+ * @returns true if successful\r
+ */\r
+ bool read(HID_REPORT * report);\r
+ \r
+ /**\r
+ * Read a report: non blocking\r
+ *\r
+ * @param report pointer to the report to fill\r
+ * @returns true if successful\r
+ */\r
+ bool readNB(HID_REPORT * report);\r
+\r
+protected:\r
+ uint16_t reportLength;\r
+ \r
+ /*\r
+ * Get the Report descriptor\r
+ *\r
+ * @returns pointer to the report descriptor\r
+ */\r
+ virtual uint8_t * reportDesc();\r
+\r
+ /*\r
+ * Get the length of the report descriptor\r
+ *\r
+ * @returns the length of the report descriptor\r
+ */\r
+ virtual uint16_t reportDescLength();\r
+\r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+ \r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+ \r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc();\r
+\r
+\r
+ /*\r
+ * HID Report received by SET_REPORT request. Warning: Called in ISR context\r
+ * First byte of data will be the report ID\r
+ *\r
+ * @param report Data and length received\r
+ */\r
+ virtual void HID_callbackSetReport(HID_REPORT *report){};\r
+\r
+\r
+ /*\r
+ * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context\r
+ * This is used to handle extensions to standard requests\r
+ * and class specific requests\r
+ *\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_request();\r
+\r
+\r
+ /*\r
+ * Called by USBDevice layer. Set configuration of the device.\r
+ * For instance, you can add all endpoints that you need on this function.\r
+ *\r
+ * @param configuration Number of the configuration\r
+ * @returns true if class handles this request\r
+ */\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);\r
+\r
+private:\r
+ HID_REPORT outputReport;\r
+ uint8_t output_length;\r
+ uint8_t input_length;\r
+};\r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBCLASS_HID_TYPES\r
+#define USBCLASS_HID_TYPES\r
+\r
+#include <stdint.h>\r
+\r
+/* */\r
+#define HID_VERSION_1_11 (0x0111)\r
+\r
+/* HID Class */\r
+#define HID_CLASS (3)\r
+#define HID_SUBCLASS_NONE (0)\r
+#define HID_PROTOCOL_NONE (0)\r
+\r
+/* Descriptors */\r
+#define HID_DESCRIPTOR (33)\r
+#define HID_DESCRIPTOR_LENGTH (0x09)\r
+#define REPORT_DESCRIPTOR (34)\r
+\r
+/* Class requests */\r
+#define GET_REPORT (0x1)\r
+#define GET_IDLE (0x2)\r
+#define SET_REPORT (0x9)\r
+#define SET_IDLE (0xa)\r
+\r
+/* HID Class Report Descriptor */\r
+/* Short items: size is 0, 1, 2 or 3 specifying 0, 1, 2 or 4 (four) bytes */\r
+/* of data as per HID Class standard */\r
+\r
+/* Main items */\r
+#define INPUT(size) (0x80 | size)\r
+#define OUTPUT(size) (0x90 | size)\r
+#define FEATURE(size) (0xb0 | size)\r
+#define COLLECTION(size) (0xa0 | size)\r
+#define END_COLLECTION(size) (0xc0 | size)\r
+\r
+/* Global items */\r
+#define USAGE_PAGE(size) (0x04 | size)\r
+#define LOGICAL_MINIMUM(size) (0x14 | size)\r
+#define LOGICAL_MAXIMUM(size) (0x24 | size)\r
+#define PHYSICAL_MINIMUM(size) (0x34 | size)\r
+#define PHYSICAL_MAXIMUM(size) (0x44 | size)\r
+#define UNIT_EXPONENT(size) (0x54 | size)\r
+#define UNIT(size) (0x64 | size)\r
+#define REPORT_SIZE(size) (0x74 | size)\r
+#define REPORT_ID(size) (0x84 | size)\r
+#define REPORT_COUNT(size) (0x94 | size)\r
+#define PUSH(size) (0xa4 | size)\r
+#define POP(size) (0xb4 | size)\r
+\r
+/* Local items */\r
+#define USAGE(size) (0x08 | size)\r
+#define USAGE_MINIMUM(size) (0x18 | size)\r
+#define USAGE_MAXIMUM(size) (0x28 | size)\r
+#define DESIGNATOR_INDEX(size) (0x38 | size)\r
+#define DESIGNATOR_MINIMUM(size) (0x48 | size)\r
+#define DESIGNATOR_MAXIMUM(size) (0x58 | size)\r
+#define STRING_INDEX(size) (0x78 | size)\r
+#define STRING_MINIMUM(size) (0x88 | size)\r
+#define STRING_MAXIMUM(size) (0x98 | size)\r
+#define DELIMITER(size) (0xa8 | size)\r
+\r
+/* HID Report */\r
+/* Where report IDs are used the first byte of 'data' will be the */\r
+/* report ID and 'length' will include this report ID byte. */\r
+\r
+#define MAX_HID_REPORT_SIZE (64)\r
+\r
+typedef struct {\r
+ uint32_t length;\r
+ uint8_t data[MAX_HID_REPORT_SIZE];\r
+} HID_REPORT;\r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+\r
+#include "USBKeyboard.h"\r
+\r
+#define REPORT_ID_KEYBOARD 1\r
+#define REPORT_ID_VOLUME 3\r
+\r
+\r
+typedef struct {\r
+ unsigned char usage;\r
+ unsigned char modifier;\r
+} KEYMAP;\r
+\r
+#ifdef US_KEYBOARD\r
+/* US keyboard (as HID standard) */\r
+#define KEYMAP_SIZE (148)\r
+const KEYMAP keymap[KEYMAP_SIZE] = {\r
+ {0, 0}, /* NUL */\r
+ {0, 0}, /* SOH */\r
+ {0, 0}, /* STX */\r
+ {0, 0}, /* ETX */\r
+ {0, 0}, /* EOT */\r
+ {0, 0}, /* ENQ */\r
+ {0, 0}, /* ACK */\r
+ {0, 0}, /* BEL */\r
+ {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */\r
+ {0x2b, 0}, /* TAB */ /* Keyboard Tab */\r
+ {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */\r
+ {0, 0}, /* VT */\r
+ {0, 0}, /* FF */\r
+ {0, 0}, /* CR */\r
+ {0, 0}, /* SO */\r
+ {0, 0}, /* SI */\r
+ {0, 0}, /* DEL */\r
+ {0, 0}, /* DC1 */\r
+ {0, 0}, /* DC2 */\r
+ {0, 0}, /* DC3 */\r
+ {0, 0}, /* DC4 */\r
+ {0, 0}, /* NAK */\r
+ {0, 0}, /* SYN */\r
+ {0, 0}, /* ETB */\r
+ {0, 0}, /* CAN */\r
+ {0, 0}, /* EM */\r
+ {0, 0}, /* SUB */\r
+ {0, 0}, /* ESC */\r
+ {0, 0}, /* FS */\r
+ {0, 0}, /* GS */\r
+ {0, 0}, /* RS */\r
+ {0, 0}, /* US */\r
+ {0x2c, 0}, /* */\r
+ {0x1e, KEY_SHIFT}, /* ! */\r
+ {0x34, KEY_SHIFT}, /* " */\r
+ {0x20, KEY_SHIFT}, /* # */\r
+ {0x21, KEY_SHIFT}, /* $ */\r
+ {0x22, KEY_SHIFT}, /* % */\r
+ {0x24, KEY_SHIFT}, /* & */\r
+ {0x34, 0}, /* ' */\r
+ {0x26, KEY_SHIFT}, /* ( */\r
+ {0x27, KEY_SHIFT}, /* ) */\r
+ {0x25, KEY_SHIFT}, /* * */\r
+ {0x2e, KEY_SHIFT}, /* + */\r
+ {0x36, 0}, /* , */\r
+ {0x2d, 0}, /* - */\r
+ {0x37, 0}, /* . */\r
+ {0x38, 0}, /* / */\r
+ {0x27, 0}, /* 0 */\r
+ {0x1e, 0}, /* 1 */\r
+ {0x1f, 0}, /* 2 */\r
+ {0x20, 0}, /* 3 */\r
+ {0x21, 0}, /* 4 */\r
+ {0x22, 0}, /* 5 */\r
+ {0x23, 0}, /* 6 */\r
+ {0x24, 0}, /* 7 */\r
+ {0x25, 0}, /* 8 */\r
+ {0x26, 0}, /* 9 */\r
+ {0x33, KEY_SHIFT}, /* : */\r
+ {0x33, 0}, /* ; */\r
+ {0x36, KEY_SHIFT}, /* < */\r
+ {0x2e, 0}, /* = */\r
+ {0x37, KEY_SHIFT}, /* > */\r
+ {0x38, KEY_SHIFT}, /* ? */\r
+ {0x1f, KEY_SHIFT}, /* @ */\r
+ {0x04, KEY_SHIFT}, /* A */\r
+ {0x05, KEY_SHIFT}, /* B */\r
+ {0x06, KEY_SHIFT}, /* C */\r
+ {0x07, KEY_SHIFT}, /* D */\r
+ {0x08, KEY_SHIFT}, /* E */\r
+ {0x09, KEY_SHIFT}, /* F */\r
+ {0x0a, KEY_SHIFT}, /* G */\r
+ {0x0b, KEY_SHIFT}, /* H */\r
+ {0x0c, KEY_SHIFT}, /* I */\r
+ {0x0d, KEY_SHIFT}, /* J */\r
+ {0x0e, KEY_SHIFT}, /* K */\r
+ {0x0f, KEY_SHIFT}, /* L */\r
+ {0x10, KEY_SHIFT}, /* M */\r
+ {0x11, KEY_SHIFT}, /* N */\r
+ {0x12, KEY_SHIFT}, /* O */\r
+ {0x13, KEY_SHIFT}, /* P */\r
+ {0x14, KEY_SHIFT}, /* Q */\r
+ {0x15, KEY_SHIFT}, /* R */\r
+ {0x16, KEY_SHIFT}, /* S */\r
+ {0x17, KEY_SHIFT}, /* T */\r
+ {0x18, KEY_SHIFT}, /* U */\r
+ {0x19, KEY_SHIFT}, /* V */\r
+ {0x1a, KEY_SHIFT}, /* W */\r
+ {0x1b, KEY_SHIFT}, /* X */\r
+ {0x1c, KEY_SHIFT}, /* Y */\r
+ {0x1d, KEY_SHIFT}, /* Z */\r
+ {0x2f, 0}, /* [ */\r
+ {0x31, 0}, /* \ */\r
+ {0x30, 0}, /* ] */\r
+ {0x23, KEY_SHIFT}, /* ^ */\r
+ {0x2d, KEY_SHIFT}, /* _ */\r
+ {0x35, 0}, /* ` */\r
+ {0x04, 0}, /* a */\r
+ {0x05, 0}, /* b */\r
+ {0x06, 0}, /* c */\r
+ {0x07, 0}, /* d */\r
+ {0x08, 0}, /* e */\r
+ {0x09, 0}, /* f */\r
+ {0x0a, 0}, /* g */\r
+ {0x0b, 0}, /* h */\r
+ {0x0c, 0}, /* i */\r
+ {0x0d, 0}, /* j */\r
+ {0x0e, 0}, /* k */\r
+ {0x0f, 0}, /* l */\r
+ {0x10, 0}, /* m */\r
+ {0x11, 0}, /* n */\r
+ {0x12, 0}, /* o */\r
+ {0x13, 0}, /* p */\r
+ {0x14, 0}, /* q */\r
+ {0x15, 0}, /* r */\r
+ {0x16, 0}, /* s */\r
+ {0x17, 0}, /* t */\r
+ {0x18, 0}, /* u */\r
+ {0x19, 0}, /* v */\r
+ {0x1a, 0}, /* w */\r
+ {0x1b, 0}, /* x */\r
+ {0x1c, 0}, /* y */\r
+ {0x1d, 0}, /* z */\r
+ {0x2f, KEY_SHIFT}, /* { */\r
+ {0x31, KEY_SHIFT}, /* | */\r
+ {0x30, KEY_SHIFT}, /* } */\r
+ {0x35, KEY_SHIFT}, /* ~ */\r
+ {0,0}, /* DEL */\r
+\r
+ {0x3a, 0}, /* F1 */\r
+ {0x3b, 0}, /* F2 */\r
+ {0x3c, 0}, /* F3 */\r
+ {0x3d, 0}, /* F4 */\r
+ {0x3e, 0}, /* F5 */\r
+ {0x3f, 0}, /* F6 */\r
+ {0x40, 0}, /* F7 */\r
+ {0x41, 0}, /* F8 */\r
+ {0x42, 0}, /* F9 */\r
+ {0x43, 0}, /* F10 */\r
+ {0x44, 0}, /* F11 */\r
+ {0x45, 0}, /* F12 */\r
+\r
+ {0x46, 0}, /* PRINT_SCREEN */\r
+ {0x47, 0}, /* SCROLL_LOCK */\r
+ {0x39, 0}, /* CAPS_LOCK */\r
+ {0x53, 0}, /* NUM_LOCK */\r
+ {0x49, 0}, /* INSERT */\r
+ {0x4a, 0}, /* HOME */\r
+ {0x4b, 0}, /* PAGE_UP */\r
+ {0x4e, 0}, /* PAGE_DOWN */\r
+};\r
+\r
+#else\r
+/* UK keyboard */\r
+#define KEYMAP_SIZE (148)\r
+const KEYMAP keymap[KEYMAP_SIZE] = {\r
+ {0, 0}, /* NUL */\r
+ {0, 0}, /* SOH */\r
+ {0, 0}, /* STX */\r
+ {0, 0}, /* ETX */\r
+ {0, 0}, /* EOT */\r
+ {0, 0}, /* ENQ */\r
+ {0, 0}, /* ACK */\r
+ {0, 0}, /* BEL */\r
+ {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */\r
+ {0x2b, 0}, /* TAB */ /* Keyboard Tab */\r
+ {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */\r
+ {0, 0}, /* VT */\r
+ {0, 0}, /* FF */\r
+ {0, 0}, /* CR */\r
+ {0, 0}, /* SO */\r
+ {0, 0}, /* SI */\r
+ {0, 0}, /* DEL */\r
+ {0, 0}, /* DC1 */\r
+ {0, 0}, /* DC2 */\r
+ {0, 0}, /* DC3 */\r
+ {0, 0}, /* DC4 */\r
+ {0, 0}, /* NAK */\r
+ {0, 0}, /* SYN */\r
+ {0, 0}, /* ETB */\r
+ {0, 0}, /* CAN */\r
+ {0, 0}, /* EM */\r
+ {0, 0}, /* SUB */\r
+ {0, 0}, /* ESC */\r
+ {0, 0}, /* FS */\r
+ {0, 0}, /* GS */\r
+ {0, 0}, /* RS */\r
+ {0, 0}, /* US */\r
+ {0x2c, 0}, /* */\r
+ {0x1e, KEY_SHIFT}, /* ! */\r
+ {0x1f, KEY_SHIFT}, /* " */\r
+ {0x32, 0}, /* # */\r
+ {0x21, KEY_SHIFT}, /* $ */\r
+ {0x22, KEY_SHIFT}, /* % */\r
+ {0x24, KEY_SHIFT}, /* & */\r
+ {0x34, 0}, /* ' */\r
+ {0x26, KEY_SHIFT}, /* ( */\r
+ {0x27, KEY_SHIFT}, /* ) */\r
+ {0x25, KEY_SHIFT}, /* * */\r
+ {0x2e, KEY_SHIFT}, /* + */\r
+ {0x36, 0}, /* , */\r
+ {0x2d, 0}, /* - */\r
+ {0x37, 0}, /* . */\r
+ {0x38, 0}, /* / */\r
+ {0x27, 0}, /* 0 */\r
+ {0x1e, 0}, /* 1 */\r
+ {0x1f, 0}, /* 2 */\r
+ {0x20, 0}, /* 3 */\r
+ {0x21, 0}, /* 4 */\r
+ {0x22, 0}, /* 5 */\r
+ {0x23, 0}, /* 6 */\r
+ {0x24, 0}, /* 7 */\r
+ {0x25, 0}, /* 8 */\r
+ {0x26, 0}, /* 9 */\r
+ {0x33, KEY_SHIFT}, /* : */\r
+ {0x33, 0}, /* ; */\r
+ {0x36, KEY_SHIFT}, /* < */\r
+ {0x2e, 0}, /* = */\r
+ {0x37, KEY_SHIFT}, /* > */\r
+ {0x38, KEY_SHIFT}, /* ? */\r
+ {0x34, KEY_SHIFT}, /* @ */\r
+ {0x04, KEY_SHIFT}, /* A */\r
+ {0x05, KEY_SHIFT}, /* B */\r
+ {0x06, KEY_SHIFT}, /* C */\r
+ {0x07, KEY_SHIFT}, /* D */\r
+ {0x08, KEY_SHIFT}, /* E */\r
+ {0x09, KEY_SHIFT}, /* F */\r
+ {0x0a, KEY_SHIFT}, /* G */\r
+ {0x0b, KEY_SHIFT}, /* H */\r
+ {0x0c, KEY_SHIFT}, /* I */\r
+ {0x0d, KEY_SHIFT}, /* J */\r
+ {0x0e, KEY_SHIFT}, /* K */\r
+ {0x0f, KEY_SHIFT}, /* L */\r
+ {0x10, KEY_SHIFT}, /* M */\r
+ {0x11, KEY_SHIFT}, /* N */\r
+ {0x12, KEY_SHIFT}, /* O */\r
+ {0x13, KEY_SHIFT}, /* P */\r
+ {0x14, KEY_SHIFT}, /* Q */\r
+ {0x15, KEY_SHIFT}, /* R */\r
+ {0x16, KEY_SHIFT}, /* S */\r
+ {0x17, KEY_SHIFT}, /* T */\r
+ {0x18, KEY_SHIFT}, /* U */\r
+ {0x19, KEY_SHIFT}, /* V */\r
+ {0x1a, KEY_SHIFT}, /* W */\r
+ {0x1b, KEY_SHIFT}, /* X */\r
+ {0x1c, KEY_SHIFT}, /* Y */\r
+ {0x1d, KEY_SHIFT}, /* Z */\r
+ {0x2f, 0}, /* [ */\r
+ {0x64, 0}, /* \ */\r
+ {0x30, 0}, /* ] */\r
+ {0x23, KEY_SHIFT}, /* ^ */\r
+ {0x2d, KEY_SHIFT}, /* _ */\r
+ {0x35, 0}, /* ` */\r
+ {0x04, 0}, /* a */\r
+ {0x05, 0}, /* b */\r
+ {0x06, 0}, /* c */\r
+ {0x07, 0}, /* d */\r
+ {0x08, 0}, /* e */\r
+ {0x09, 0}, /* f */\r
+ {0x0a, 0}, /* g */\r
+ {0x0b, 0}, /* h */\r
+ {0x0c, 0}, /* i */\r
+ {0x0d, 0}, /* j */\r
+ {0x0e, 0}, /* k */\r
+ {0x0f, 0}, /* l */\r
+ {0x10, 0}, /* m */\r
+ {0x11, 0}, /* n */\r
+ {0x12, 0}, /* o */\r
+ {0x13, 0}, /* p */\r
+ {0x14, 0}, /* q */\r
+ {0x15, 0}, /* r */\r
+ {0x16, 0}, /* s */\r
+ {0x17, 0}, /* t */\r
+ {0x18, 0}, /* u */\r
+ {0x19, 0}, /* v */\r
+ {0x1a, 0}, /* w */\r
+ {0x1b, 0}, /* x */\r
+ {0x1c, 0}, /* y */\r
+ {0x1d, 0}, /* z */\r
+ {0x2f, KEY_SHIFT}, /* { */\r
+ {0x64, KEY_SHIFT}, /* | */\r
+ {0x30, KEY_SHIFT}, /* } */\r
+ {0x32, KEY_SHIFT}, /* ~ */\r
+ {0,0}, /* DEL */\r
+\r
+ {0x3a, 0}, /* F1 */\r
+ {0x3b, 0}, /* F2 */\r
+ {0x3c, 0}, /* F3 */\r
+ {0x3d, 0}, /* F4 */\r
+ {0x3e, 0}, /* F5 */\r
+ {0x3f, 0}, /* F6 */\r
+ {0x40, 0}, /* F7 */\r
+ {0x41, 0}, /* F8 */\r
+ {0x42, 0}, /* F9 */\r
+ {0x43, 0}, /* F10 */\r
+ {0x44, 0}, /* F11 */\r
+ {0x45, 0}, /* F12 */\r
+\r
+ {0x46, 0}, /* PRINT_SCREEN */\r
+ {0x47, 0}, /* SCROLL_LOCK */\r
+ {0x39, 0}, /* CAPS_LOCK */\r
+ {0x53, 0}, /* NUM_LOCK */\r
+ {0x49, 0}, /* INSERT */\r
+ {0x4a, 0}, /* HOME */\r
+ {0x4b, 0}, /* PAGE_UP */\r
+ {0x4e, 0}, /* PAGE_DOWN */\r
+};\r
+#endif\r
+\r
+uint8_t * USBKeyboard::reportDesc() {\r
+ static uint8_t reportDescriptor[] = {\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x06, // Keyboard\r
+ COLLECTION(1), 0x01, // Application\r
+ REPORT_ID(1), REPORT_ID_KEYBOARD,\r
+\r
+ USAGE_PAGE(1), 0x07, // Key Codes\r
+ USAGE_MINIMUM(1), 0xE0,\r
+ USAGE_MAXIMUM(1), 0xE7,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x08,\r
+ INPUT(1), 0x02, // Data, Variable, Absolute\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x08,\r
+ INPUT(1), 0x01, // Constant\r
+\r
+\r
+ REPORT_COUNT(1), 0x05,\r
+ REPORT_SIZE(1), 0x01,\r
+ USAGE_PAGE(1), 0x08, // LEDs\r
+ USAGE_MINIMUM(1), 0x01,\r
+ USAGE_MAXIMUM(1), 0x05,\r
+ OUTPUT(1), 0x02, // Data, Variable, Absolute\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x03,\r
+ OUTPUT(1), 0x01, // Constant\r
+\r
+\r
+ REPORT_COUNT(1), 0x06,\r
+ REPORT_SIZE(1), 0x08,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x65,\r
+ USAGE_PAGE(1), 0x07, // Key Codes\r
+ USAGE_MINIMUM(1), 0x00,\r
+ USAGE_MAXIMUM(1), 0x65,\r
+ INPUT(1), 0x00, // Data, Array\r
+ END_COLLECTION(0),\r
+\r
+ // Media Control\r
+ USAGE_PAGE(1), 0x0C,\r
+ USAGE(1), 0x01,\r
+ COLLECTION(1), 0x01,\r
+ REPORT_ID(1), REPORT_ID_VOLUME,\r
+ USAGE_PAGE(1), 0x0C,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x07,\r
+ USAGE(1), 0xB5, // Next Track\r
+ USAGE(1), 0xB6, // Previous Track\r
+ USAGE(1), 0xB7, // Stop\r
+ USAGE(1), 0xCD, // Play / Pause\r
+ USAGE(1), 0xE2, // Mute\r
+ USAGE(1), 0xE9, // Volume Up\r
+ USAGE(1), 0xEA, // Volume Down\r
+ INPUT(1), 0x02, // Input (Data, Variable, Absolute)\r
+ REPORT_COUNT(1), 0x01,\r
+ INPUT(1), 0x01,\r
+ END_COLLECTION(0),\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+}\r
+\r
+\r
+bool USBKeyboard::EP1_OUT_callback() {\r
+ uint16_t bytesRead = 0;\r
+ uint8_t led[65];\r
+ USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);\r
+ \r
+ // we take led[1] because led[0] is the report ID\r
+ lock_status = led[1] & 0x07;\r
+ \r
+ // We activate the endpoint to be able to recceive data\r
+ if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+uint8_t USBKeyboard::lockStatus() {\r
+ return lock_status;\r
+}\r
+\r
+int USBKeyboard::_putc(int c) {\r
+ return keyCode(c, keymap[c].modifier);\r
+}\r
+\r
+bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) {\r
+ // Send a simulated keyboard keypress. Returns true if successful.\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = REPORT_ID_KEYBOARD;\r
+ report.data[1] = modifier;\r
+ report.data[2] = 0;\r
+ report.data[3] = keymap[key].usage;\r
+ report.data[4] = 0;\r
+ report.data[5] = 0;\r
+ report.data[6] = 0;\r
+ report.data[7] = 0;\r
+ report.data[8] = 0;\r
+\r
+ report.length = 9;\r
+\r
+ if (!send(&report)) {\r
+ return false;\r
+ }\r
+\r
+ report.data[1] = 0;\r
+ report.data[3] = 0;\r
+\r
+ if (!send(&report)) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+\r
+}\r
+\r
+\r
+bool USBKeyboard::mediaControl(MEDIA_KEY key) {\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = REPORT_ID_VOLUME;\r
+ report.data[1] = (1 << key) & 0x7f;\r
+\r
+ report.length = 2;\r
+\r
+ if (!send(&report)) {\r
+ return false;\r
+ }\r
+\r
+ report.data[0] = REPORT_ID_VOLUME;\r
+ report.data[1] = 0;\r
+\r
+ report.length = 2;\r
+\r
+ return send(&report);\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* 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.
+*/
+
+#ifndef USBKEYBOARD_H
+#define USBKEYBOARD_H
+
+#include "USBHID.h"
+#include "Stream.h"
+
+/* Modifiers */
+enum MODIFIER_KEY
+{
+ KEY_CTRL = 1,
+ KEY_SHIFT = 2,
+ KEY_ALT = 4,
+};
+
+
+enum MEDIA_KEY
+{
+ KEY_NEXT_TRACK, /*!< next Track Button */
+ KEY_PREVIOUS_TRACK, /*!< Previous track Button */
+ KEY_STOP, /*!< Stop Button */
+ KEY_PLAY_PAUSE, /*!< Play/Pause Button */
+ KEY_MUTE, /*!< Mute Button */
+ KEY_VOLUME_UP, /*!< Volume Up Button */
+ KEY_VOLUME_DOWN, /*!< Volume Down Button */
+};
+
+enum FUNCTION_KEY
+{
+ KEY_F1 = 128, /* F1 key */
+ KEY_F2, /* F2 key */
+ KEY_F3, /* F3 key */
+ KEY_F4, /* F4 key */
+ KEY_F5, /* F5 key */
+ KEY_F6, /* F6 key */
+ KEY_F7, /* F7 key */
+ KEY_F8, /* F8 key */
+ KEY_F9, /* F9 key */
+ KEY_F10, /* F10 key */
+ KEY_F11, /* F11 key */
+ KEY_F12, /* F12 key */
+ KEY_PRINT_SCREEN, /* Print Screen key */
+ KEY_SCROLL_LOCK, /* Scroll lock */
+ KEY_CAPS_LOCK, /* caps lock */
+ KEY_NUM_LOCK, /* num lock */
+ KEY_INSERT, /* Insert key */
+ KEY_HOME, /* Home key */
+ KEY_PAGE_UP, /* Page Up key */
+ KEY_PAGE_DOWN, /* Page Down key */
+};
+
+/**
+ * USBKeyboard example
+ * @code
+ *
+ * #include "mbed.h"
+ * #include "USBKeyboard.h"
+ *
+ * USBKeyboard key;
+ *
+ * int main(void)
+ * {
+ * while (1)
+ * {
+ * key.printf("Hello World\r\n");
+ * wait(1);
+ * }
+ * }
+ *
+ * @endcode
+ */
+class USBKeyboard: public USBHID, public Stream
+{
+ public:
+
+ /**
+ * Constructor
+ *
+ *
+ * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
+ * @param vendor_id Your vendor_id (default: 0x1235)
+ * @param product_id Your product_id (default: 0x0050)
+ * @param product_release Your preoduct_release (default: 0x0001)
+ *
+ */
+ USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001):
+ USBHID(0, 0, vendor_id, product_id, product_release, false){
+ lock_status = 0;
+ connect();
+ };
+
+ /**
+ * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
+ *
+ * @code
+ * //To send CTRL + s (save)
+ * keyboard.keyCode('s', KEY_CTRL);
+ * @endcode
+ *
+ * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
+ * @param key character to send
+ * @returns true if there is no error, false otherwise
+ */
+ bool keyCode(uint8_t key, uint8_t modifier = 0);
+
+ /**
+ * Send a character
+ *
+ * @param c character to be sent
+ * @returns true if there is no error, false otherwise
+ */
+ virtual int _putc(int c);
+
+ /**
+ * Control media keys
+ *
+ * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
+ * @returns true if there is no error, false otherwise
+ */
+ bool mediaControl(MEDIA_KEY key);
+
+ /*
+ * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
+ *
+ * @returns pointer to the report descriptor
+ */
+ virtual uint8_t * reportDesc();
+
+ /*
+ * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
+ *
+ * @returns if handle by subclass, return true
+ */
+ virtual bool EP1_OUT_callback();
+
+ /**
+ * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
+ * - First bit: NUM_LOCK
+ * - Second bit: CAPS_LOCK
+ * - Third bit: SCROLL_LOCK
+ *
+ * @returns status of lock keys
+ */
+ uint8_t lockStatus();
+
+ private:
+ //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
+ virtual int _getc() { return -1;};
+
+ uint8_t lock_status;
+};
+
+#endif
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBMouse.h"\r
+\r
+bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {\r
+ switch (mouse_type) {\r
+ case REL_MOUSE:\r
+ while (x > 127) {\r
+ if (!mouseSend(127, 0, button, z)) return false;\r
+ x = x - 127;\r
+ }\r
+ while (x < -128) {\r
+ if (!mouseSend(-128, 0, button, z)) return false;\r
+ x = x + 128;\r
+ }\r
+ while (y > 127) {\r
+ if (!mouseSend(0, 127, button, z)) return false;\r
+ y = y - 127;\r
+ }\r
+ while (y < -128) {\r
+ if (!mouseSend(0, -128, button, z)) return false;\r
+ y = y + 128;\r
+ }\r
+ return mouseSend(x, y, button, z);\r
+ case ABS_MOUSE:\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = x & 0xff;\r
+ report.data[1] = (x >> 8) & 0xff;\r
+ report.data[2] = y & 0xff;\r
+ report.data[3] = (y >> 8) & 0xff;\r
+ report.data[4] = -z;\r
+ report.data[5] = button & 0x07;\r
+\r
+ report.length = 6;\r
+\r
+ return send(&report);\r
+ default:\r
+ return false;\r
+ }\r
+}\r
+\r
+bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {\r
+ HID_REPORT report;\r
+ report.data[0] = buttons & 0x07;\r
+ report.data[1] = x;\r
+ report.data[2] = y;\r
+ report.data[3] = -z; // >0 to scroll down, <0 to scroll up\r
+\r
+ report.length = 4;\r
+\r
+ return send(&report);\r
+}\r
+\r
+bool USBMouse::move(int16_t x, int16_t y) {\r
+ return update(x, y, button, 0);\r
+}\r
+\r
+bool USBMouse::scroll(int8_t z) {\r
+ return update(0, 0, button, z);\r
+}\r
+\r
+\r
+bool USBMouse::doubleClick() {\r
+ if (!click(MOUSE_LEFT))\r
+ return false;\r
+ wait(0.1);\r
+ return click(MOUSE_LEFT);\r
+}\r
+\r
+bool USBMouse::click(uint8_t button) {\r
+ if (!update(0, 0, button, 0))\r
+ return false;\r
+ wait(0.01);\r
+ return update(0, 0, 0, 0);\r
+}\r
+\r
+bool USBMouse::press(uint8_t button_) {\r
+ button = button_ & 0x07;\r
+ return update(0, 0, button, 0);\r
+}\r
+\r
+bool USBMouse::release(uint8_t button_) {\r
+ button = (button & (~button_)) & 0x07;\r
+ return update(0, 0, button, 0);\r
+}\r
+\r
+\r
+uint8_t * USBMouse::reportDesc() {\r
+\r
+ if (mouse_type == REL_MOUSE) {\r
+ static uint8_t reportDescriptor[] = {\r
+ USAGE_PAGE(1), 0x01, // Genric Desktop\r
+ USAGE(1), 0x02, // Mouse\r
+ COLLECTION(1), 0x01, // Application\r
+ USAGE(1), 0x01, // Pointer\r
+ COLLECTION(1), 0x00, // Physical\r
+\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x01,\r
+ USAGE_PAGE(1), 0x09, // Buttons\r
+ USAGE_MINIMUM(1), 0x1,\r
+ USAGE_MAXIMUM(1), 0x3,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ INPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x05,\r
+ INPUT(1), 0x01,\r
+\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x08,\r
+ USAGE_PAGE(1), 0x01,\r
+ USAGE(1), 0x30, // X\r
+ USAGE(1), 0x31, // Y\r
+ USAGE(1), 0x38, // scroll\r
+ LOGICAL_MINIMUM(1), 0x81,\r
+ LOGICAL_MAXIMUM(1), 0x7f,\r
+ INPUT(1), 0x06, // Relative data\r
+\r
+ END_COLLECTION(0),\r
+ END_COLLECTION(0),\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+ } else if (mouse_type == ABS_MOUSE) {\r
+ static uint8_t reportDescriptor[] = {\r
+\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x02, // Mouse\r
+ COLLECTION(1), 0x01, // Application\r
+ USAGE(1), 0x01, // Pointer\r
+ COLLECTION(1), 0x00, // Physical\r
+\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x30, // X\r
+ USAGE(1), 0x31, // Y\r
+ LOGICAL_MINIMUM(1), 0x00, // 0\r
+ LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767\r
+ REPORT_SIZE(1), 0x10,\r
+ REPORT_COUNT(1), 0x02,\r
+ INPUT(1), 0x02, // Data, Variable, Absolute\r
+\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x38, // scroll\r
+ LOGICAL_MINIMUM(1), 0x81, // -127\r
+ LOGICAL_MAXIMUM(1), 0x7f, // 127\r
+ REPORT_SIZE(1), 0x08,\r
+ REPORT_COUNT(1), 0x01,\r
+ INPUT(1), 0x06, // Data, Variable, Relative\r
+\r
+ USAGE_PAGE(1), 0x09, // Buttons\r
+ USAGE_MINIMUM(1), 0x01,\r
+ USAGE_MAXIMUM(1), 0x03,\r
+ LOGICAL_MINIMUM(1), 0x00, // 0\r
+ LOGICAL_MAXIMUM(1), 0x01, // 1\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x01,\r
+ INPUT(1), 0x02, // Data, Variable, Absolute\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x05,\r
+ INPUT(1), 0x01, // Constant\r
+\r
+ END_COLLECTION(0),\r
+ END_COLLECTION(0)\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+ }\r
+ return NULL;\r
+}\r
+\r
+\r
--- /dev/null
+/* USBMouse.h */\r
+/* USB device example: relative mouse */\r
+/* Copyright (c) 2011 ARM Limited. All rights reserved. */\r
+\r
+#ifndef USBMOUSE_H\r
+#define USBMOUSE_H\r
+\r
+#include "USBHID.h"\r
+\r
+#define REPORT_ID_MOUSE 2\r
+\r
+/* Common usage */\r
+\r
+enum MOUSE_BUTTON\r
+{\r
+ MOUSE_LEFT = 1,\r
+ MOUSE_RIGHT = 2,\r
+ MOUSE_MIDDLE = 4,\r
+};\r
+\r
+/* X and Y limits */\r
+/* These values do not directly map to screen pixels */\r
+/* Zero may be interpreted as meaning 'no movement' */\r
+#define X_MIN_ABS (1) /*!< Minimum value on x-axis */ \r
+#define Y_MIN_ABS (1) /*!< Minimum value on y-axis */\r
+#define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */\r
+#define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */\r
+\r
+#define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */\r
+#define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */\r
+#define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */\r
+#define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */\r
+\r
+enum MOUSE_TYPE\r
+{\r
+ ABS_MOUSE,\r
+ REL_MOUSE,\r
+};\r
+\r
+/**\r
+ *\r
+ * USBMouse example\r
+ * @code\r
+ * #include "mbed.h"\r
+ * #include "USBMouse.h"\r
+ *\r
+ * USBMouse mouse;\r
+ *\r
+ * int main(void)\r
+ * {\r
+ * while (1)\r
+ * {\r
+ * mouse.move(20, 0);\r
+ * wait(0.5);\r
+ * }\r
+ * }\r
+ *\r
+ * @endcode\r
+ *\r
+ *\r
+ * @code\r
+ * #include "mbed.h"\r
+ * #include "USBMouse.h"\r
+ * #include <math.h>\r
+ *\r
+ * USBMouse mouse(ABS_MOUSE);\r
+ *\r
+ * int main(void)\r
+ * {\r
+ * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;\r
+ * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;\r
+ * uint16_t x_screen = 0;\r
+ * uint16_t y_screen = 0;\r
+ * \r
+ * uint32_t x_origin = x_center;\r
+ * uint32_t y_origin = y_center;\r
+ * uint32_t radius = 5000;\r
+ * uint32_t angle = 0;\r
+ *\r
+ * while (1)\r
+ * {\r
+ * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;\r
+ * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;\r
+ * \r
+ * mouse.move(x_screen, y_screen);\r
+ * angle += 3;\r
+ * wait(0.01);\r
+ * }\r
+ * }\r
+ *\r
+ * @endcode\r
+ */\r
+class USBMouse: public USBHID\r
+{\r
+ public:\r
+ \r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)\r
+ * @param vendor_id Your vendor_id (default: 0x1234)\r
+ * @param product_id Your product_id (default: 0x0001)\r
+ * @param product_release Your preoduct_release (default: 0x0001)\r
+ *\r
+ */\r
+ USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001): \r
+ USBHID(0, 0, vendor_id, product_id, product_release, false)\r
+ { \r
+ button = 0;\r
+ this->mouse_type = mouse_type;\r
+ connect();\r
+ };\r
+ \r
+ /**\r
+ * Write a state of the mouse\r
+ *\r
+ * @param x x-axis position\r
+ * @param y y-axis position\r
+ * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)\r
+ * @param z wheel state (>0 to scroll down, <0 to scroll up)\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);\r
+ \r
+ \r
+ /**\r
+ * Move the cursor to (x, y)\r
+ *\r
+ * @param x-axis position\r
+ * @param y-axis position\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool move(int16_t x, int16_t y);\r
+ \r
+ /**\r
+ * Press one or several buttons\r
+ *\r
+ * @param button button state (ex: press(MOUSE_LEFT))\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool press(uint8_t button);\r
+ \r
+ /**\r
+ * Release one or several buttons\r
+ *\r
+ * @param button button state (ex: release(MOUSE_LEFT))\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool release(uint8_t button);\r
+ \r
+ /**\r
+ * Double click (MOUSE_LEFT)\r
+ *\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool doubleClick();\r
+ \r
+ /**\r
+ * Click\r
+ *\r
+ * @param button state of the buttons ( ex: clic(MOUSE_LEFT))\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool click(uint8_t button); \r
+ \r
+ /**\r
+ * Scrolling\r
+ *\r
+ * @param z value of the wheel (>0 to go down, <0 to go up)\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ bool scroll(int8_t z);\r
+ \r
+ /*\r
+ * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.\r
+ *\r
+ * @returns pointer to the report descriptor\r
+ */\r
+ virtual uint8_t * reportDesc();\r
+ \r
+ private:\r
+ MOUSE_TYPE mouse_type;\r
+ uint8_t button;\r
+ bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);\r
+};\r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBMouseKeyboard.h"\r
+\r
+typedef struct {\r
+ unsigned char usage;\r
+ unsigned char modifier;\r
+} KEYMAP;\r
+\r
+#ifdef US_KEYBOARD\r
+/* US keyboard (as HID standard) */\r
+#define KEYMAP_SIZE (148)\r
+const KEYMAP keymap[KEYMAP_SIZE] = {\r
+ {0, 0}, /* NUL */\r
+ {0, 0}, /* SOH */\r
+ {0, 0}, /* STX */\r
+ {0, 0}, /* ETX */\r
+ {0, 0}, /* EOT */\r
+ {0, 0}, /* ENQ */\r
+ {0, 0}, /* ACK */\r
+ {0, 0}, /* BEL */\r
+ {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */\r
+ {0x2b, 0}, /* TAB */ /* Keyboard Tab */\r
+ {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */\r
+ {0, 0}, /* VT */\r
+ {0, 0}, /* FF */\r
+ {0, 0}, /* CR */\r
+ {0, 0}, /* SO */\r
+ {0, 0}, /* SI */\r
+ {0, 0}, /* DEL */\r
+ {0, 0}, /* DC1 */\r
+ {0, 0}, /* DC2 */\r
+ {0, 0}, /* DC3 */\r
+ {0, 0}, /* DC4 */\r
+ {0, 0}, /* NAK */\r
+ {0, 0}, /* SYN */\r
+ {0, 0}, /* ETB */\r
+ {0, 0}, /* CAN */\r
+ {0, 0}, /* EM */\r
+ {0, 0}, /* SUB */\r
+ {0, 0}, /* ESC */\r
+ {0, 0}, /* FS */\r
+ {0, 0}, /* GS */\r
+ {0, 0}, /* RS */\r
+ {0, 0}, /* US */\r
+ {0x2c, 0}, /* */\r
+ {0x1e, KEY_SHIFT}, /* ! */\r
+ {0x34, KEY_SHIFT}, /* " */\r
+ {0x20, KEY_SHIFT}, /* # */\r
+ {0x21, KEY_SHIFT}, /* $ */\r
+ {0x22, KEY_SHIFT}, /* % */\r
+ {0x24, KEY_SHIFT}, /* & */\r
+ {0x34, 0}, /* ' */\r
+ {0x26, KEY_SHIFT}, /* ( */\r
+ {0x27, KEY_SHIFT}, /* ) */\r
+ {0x25, KEY_SHIFT}, /* * */\r
+ {0x2e, KEY_SHIFT}, /* + */\r
+ {0x36, 0}, /* , */\r
+ {0x2d, 0}, /* - */\r
+ {0x37, 0}, /* . */\r
+ {0x38, 0}, /* / */\r
+ {0x27, 0}, /* 0 */\r
+ {0x1e, 0}, /* 1 */\r
+ {0x1f, 0}, /* 2 */\r
+ {0x20, 0}, /* 3 */\r
+ {0x21, 0}, /* 4 */\r
+ {0x22, 0}, /* 5 */\r
+ {0x23, 0}, /* 6 */\r
+ {0x24, 0}, /* 7 */\r
+ {0x25, 0}, /* 8 */\r
+ {0x26, 0}, /* 9 */\r
+ {0x33, KEY_SHIFT}, /* : */\r
+ {0x33, 0}, /* ; */\r
+ {0x36, KEY_SHIFT}, /* < */\r
+ {0x2e, 0}, /* = */\r
+ {0x37, KEY_SHIFT}, /* > */\r
+ {0x38, KEY_SHIFT}, /* ? */\r
+ {0x1f, KEY_SHIFT}, /* @ */\r
+ {0x04, KEY_SHIFT}, /* A */\r
+ {0x05, KEY_SHIFT}, /* B */\r
+ {0x06, KEY_SHIFT}, /* C */\r
+ {0x07, KEY_SHIFT}, /* D */\r
+ {0x08, KEY_SHIFT}, /* E */\r
+ {0x09, KEY_SHIFT}, /* F */\r
+ {0x0a, KEY_SHIFT}, /* G */\r
+ {0x0b, KEY_SHIFT}, /* H */\r
+ {0x0c, KEY_SHIFT}, /* I */\r
+ {0x0d, KEY_SHIFT}, /* J */\r
+ {0x0e, KEY_SHIFT}, /* K */\r
+ {0x0f, KEY_SHIFT}, /* L */\r
+ {0x10, KEY_SHIFT}, /* M */\r
+ {0x11, KEY_SHIFT}, /* N */\r
+ {0x12, KEY_SHIFT}, /* O */\r
+ {0x13, KEY_SHIFT}, /* P */\r
+ {0x14, KEY_SHIFT}, /* Q */\r
+ {0x15, KEY_SHIFT}, /* R */\r
+ {0x16, KEY_SHIFT}, /* S */\r
+ {0x17, KEY_SHIFT}, /* T */\r
+ {0x18, KEY_SHIFT}, /* U */\r
+ {0x19, KEY_SHIFT}, /* V */\r
+ {0x1a, KEY_SHIFT}, /* W */\r
+ {0x1b, KEY_SHIFT}, /* X */\r
+ {0x1c, KEY_SHIFT}, /* Y */\r
+ {0x1d, KEY_SHIFT}, /* Z */\r
+ {0x2f, 0}, /* [ */\r
+ {0x31, 0}, /* \ */\r
+ {0x30, 0}, /* ] */\r
+ {0x23, KEY_SHIFT}, /* ^ */\r
+ {0x2d, KEY_SHIFT}, /* _ */\r
+ {0x35, 0}, /* ` */\r
+ {0x04, 0}, /* a */\r
+ {0x05, 0}, /* b */\r
+ {0x06, 0}, /* c */\r
+ {0x07, 0}, /* d */\r
+ {0x08, 0}, /* e */\r
+ {0x09, 0}, /* f */\r
+ {0x0a, 0}, /* g */\r
+ {0x0b, 0}, /* h */\r
+ {0x0c, 0}, /* i */\r
+ {0x0d, 0}, /* j */\r
+ {0x0e, 0}, /* k */\r
+ {0x0f, 0}, /* l */\r
+ {0x10, 0}, /* m */\r
+ {0x11, 0}, /* n */\r
+ {0x12, 0}, /* o */\r
+ {0x13, 0}, /* p */\r
+ {0x14, 0}, /* q */\r
+ {0x15, 0}, /* r */\r
+ {0x16, 0}, /* s */\r
+ {0x17, 0}, /* t */\r
+ {0x18, 0}, /* u */\r
+ {0x19, 0}, /* v */\r
+ {0x1a, 0}, /* w */\r
+ {0x1b, 0}, /* x */\r
+ {0x1c, 0}, /* y */\r
+ {0x1d, 0}, /* z */\r
+ {0x2f, KEY_SHIFT}, /* { */\r
+ {0x31, KEY_SHIFT}, /* | */\r
+ {0x30, KEY_SHIFT}, /* } */\r
+ {0x35, KEY_SHIFT}, /* ~ */\r
+ {0,0}, /* DEL */\r
+\r
+ {0x3a, 0}, /* F1 */\r
+ {0x3b, 0}, /* F2 */\r
+ {0x3c, 0}, /* F3 */\r
+ {0x3d, 0}, /* F4 */\r
+ {0x3e, 0}, /* F5 */\r
+ {0x3f, 0}, /* F6 */\r
+ {0x40, 0}, /* F7 */\r
+ {0x41, 0}, /* F8 */\r
+ {0x42, 0}, /* F9 */\r
+ {0x43, 0}, /* F10 */\r
+ {0x44, 0}, /* F11 */\r
+ {0x45, 0}, /* F12 */\r
+\r
+ {0x46, 0}, /* PRINT_SCREEN */\r
+ {0x47, 0}, /* SCROLL_LOCK */\r
+ {0x39, 0}, /* CAPS_LOCK */\r
+ {0x53, 0}, /* NUM_LOCK */\r
+ {0x49, 0}, /* INSERT */\r
+ {0x4a, 0}, /* HOME */\r
+ {0x4b, 0}, /* PAGE_UP */\r
+ {0x4e, 0}, /* PAGE_DOWN */\r
+};\r
+\r
+#else\r
+/* UK keyboard */\r
+#define KEYMAP_SIZE (148)\r
+const KEYMAP keymap[KEYMAP_SIZE] = {\r
+ {0, 0}, /* NUL */\r
+ {0, 0}, /* SOH */\r
+ {0, 0}, /* STX */\r
+ {0, 0}, /* ETX */\r
+ {0, 0}, /* EOT */\r
+ {0, 0}, /* ENQ */\r
+ {0, 0}, /* ACK */\r
+ {0, 0}, /* BEL */\r
+ {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */\r
+ {0x2b, 0}, /* TAB */ /* Keyboard Tab */\r
+ {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */\r
+ {0, 0}, /* VT */\r
+ {0, 0}, /* FF */\r
+ {0, 0}, /* CR */\r
+ {0, 0}, /* SO */\r
+ {0, 0}, /* SI */\r
+ {0, 0}, /* DEL */\r
+ {0, 0}, /* DC1 */\r
+ {0, 0}, /* DC2 */\r
+ {0, 0}, /* DC3 */\r
+ {0, 0}, /* DC4 */\r
+ {0, 0}, /* NAK */\r
+ {0, 0}, /* SYN */\r
+ {0, 0}, /* ETB */\r
+ {0, 0}, /* CAN */\r
+ {0, 0}, /* EM */\r
+ {0, 0}, /* SUB */\r
+ {0, 0}, /* ESC */\r
+ {0, 0}, /* FS */\r
+ {0, 0}, /* GS */\r
+ {0, 0}, /* RS */\r
+ {0, 0}, /* US */\r
+ {0x2c, 0}, /* */\r
+ {0x1e, KEY_SHIFT}, /* ! */\r
+ {0x1f, KEY_SHIFT}, /* " */\r
+ {0x32, 0}, /* # */\r
+ {0x21, KEY_SHIFT}, /* $ */\r
+ {0x22, KEY_SHIFT}, /* % */\r
+ {0x24, KEY_SHIFT}, /* & */\r
+ {0x34, 0}, /* ' */\r
+ {0x26, KEY_SHIFT}, /* ( */\r
+ {0x27, KEY_SHIFT}, /* ) */\r
+ {0x25, KEY_SHIFT}, /* * */\r
+ {0x2e, KEY_SHIFT}, /* + */\r
+ {0x36, 0}, /* , */\r
+ {0x2d, 0}, /* - */\r
+ {0x37, 0}, /* . */\r
+ {0x38, 0}, /* / */\r
+ {0x27, 0}, /* 0 */\r
+ {0x1e, 0}, /* 1 */\r
+ {0x1f, 0}, /* 2 */\r
+ {0x20, 0}, /* 3 */\r
+ {0x21, 0}, /* 4 */\r
+ {0x22, 0}, /* 5 */\r
+ {0x23, 0}, /* 6 */\r
+ {0x24, 0}, /* 7 */\r
+ {0x25, 0}, /* 8 */\r
+ {0x26, 0}, /* 9 */\r
+ {0x33, KEY_SHIFT}, /* : */\r
+ {0x33, 0}, /* ; */\r
+ {0x36, KEY_SHIFT}, /* < */\r
+ {0x2e, 0}, /* = */\r
+ {0x37, KEY_SHIFT}, /* > */\r
+ {0x38, KEY_SHIFT}, /* ? */\r
+ {0x34, KEY_SHIFT}, /* @ */\r
+ {0x04, KEY_SHIFT}, /* A */\r
+ {0x05, KEY_SHIFT}, /* B */\r
+ {0x06, KEY_SHIFT}, /* C */\r
+ {0x07, KEY_SHIFT}, /* D */\r
+ {0x08, KEY_SHIFT}, /* E */\r
+ {0x09, KEY_SHIFT}, /* F */\r
+ {0x0a, KEY_SHIFT}, /* G */\r
+ {0x0b, KEY_SHIFT}, /* H */\r
+ {0x0c, KEY_SHIFT}, /* I */\r
+ {0x0d, KEY_SHIFT}, /* J */\r
+ {0x0e, KEY_SHIFT}, /* K */\r
+ {0x0f, KEY_SHIFT}, /* L */\r
+ {0x10, KEY_SHIFT}, /* M */\r
+ {0x11, KEY_SHIFT}, /* N */\r
+ {0x12, KEY_SHIFT}, /* O */\r
+ {0x13, KEY_SHIFT}, /* P */\r
+ {0x14, KEY_SHIFT}, /* Q */\r
+ {0x15, KEY_SHIFT}, /* R */\r
+ {0x16, KEY_SHIFT}, /* S */\r
+ {0x17, KEY_SHIFT}, /* T */\r
+ {0x18, KEY_SHIFT}, /* U */\r
+ {0x19, KEY_SHIFT}, /* V */\r
+ {0x1a, KEY_SHIFT}, /* W */\r
+ {0x1b, KEY_SHIFT}, /* X */\r
+ {0x1c, KEY_SHIFT}, /* Y */\r
+ {0x1d, KEY_SHIFT}, /* Z */\r
+ {0x2f, 0}, /* [ */\r
+ {0x64, 0}, /* \ */\r
+ {0x30, 0}, /* ] */\r
+ {0x23, KEY_SHIFT}, /* ^ */\r
+ {0x2d, KEY_SHIFT}, /* _ */\r
+ {0x35, 0}, /* ` */\r
+ {0x04, 0}, /* a */\r
+ {0x05, 0}, /* b */\r
+ {0x06, 0}, /* c */\r
+ {0x07, 0}, /* d */\r
+ {0x08, 0}, /* e */\r
+ {0x09, 0}, /* f */\r
+ {0x0a, 0}, /* g */\r
+ {0x0b, 0}, /* h */\r
+ {0x0c, 0}, /* i */\r
+ {0x0d, 0}, /* j */\r
+ {0x0e, 0}, /* k */\r
+ {0x0f, 0}, /* l */\r
+ {0x10, 0}, /* m */\r
+ {0x11, 0}, /* n */\r
+ {0x12, 0}, /* o */\r
+ {0x13, 0}, /* p */\r
+ {0x14, 0}, /* q */\r
+ {0x15, 0}, /* r */\r
+ {0x16, 0}, /* s */\r
+ {0x17, 0}, /* t */\r
+ {0x18, 0}, /* u */\r
+ {0x19, 0}, /* v */\r
+ {0x1a, 0}, /* w */\r
+ {0x1b, 0}, /* x */\r
+ {0x1c, 0}, /* y */\r
+ {0x1d, 0}, /* z */\r
+ {0x2f, KEY_SHIFT}, /* { */\r
+ {0x64, KEY_SHIFT}, /* | */\r
+ {0x30, KEY_SHIFT}, /* } */\r
+ {0x32, KEY_SHIFT}, /* ~ */\r
+ {0,0}, /* DEL */\r
+\r
+ {0x3a, 0}, /* F1 */\r
+ {0x3b, 0}, /* F2 */\r
+ {0x3c, 0}, /* F3 */\r
+ {0x3d, 0}, /* F4 */\r
+ {0x3e, 0}, /* F5 */\r
+ {0x3f, 0}, /* F6 */\r
+ {0x40, 0}, /* F7 */\r
+ {0x41, 0}, /* F8 */\r
+ {0x42, 0}, /* F9 */\r
+ {0x43, 0}, /* F10 */\r
+ {0x44, 0}, /* F11 */\r
+ {0x45, 0}, /* F12 */\r
+\r
+ {0x46, 0}, /* PRINT_SCREEN */\r
+ {0x47, 0}, /* SCROLL_LOCK */\r
+ {0x39, 0}, /* CAPS_LOCK */\r
+ {0x53, 0}, /* NUM_LOCK */\r
+ {0x49, 0}, /* INSERT */\r
+ {0x4a, 0}, /* HOME */\r
+ {0x4b, 0}, /* PAGE_UP */\r
+ {0x4e, 0}, /* PAGE_DOWN */\r
+};\r
+#endif\r
+\r
+\r
+uint8_t * USBMouseKeyboard::reportDesc() {\r
+ if (mouse_type == REL_MOUSE) {\r
+ static uint8_t reportDescriptor[] = {\r
+ // Keyboard\r
+ USAGE_PAGE(1), 0x01,\r
+ USAGE(1), 0x06,\r
+ COLLECTION(1), 0x01,\r
+ REPORT_ID(1), REPORT_ID_KEYBOARD,\r
+ USAGE_PAGE(1), 0x07,\r
+ USAGE_MINIMUM(1), 0xE0,\r
+ USAGE_MAXIMUM(1), 0xE7,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x08,\r
+ INPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x08,\r
+ INPUT(1), 0x01,\r
+ REPORT_COUNT(1), 0x05,\r
+ REPORT_SIZE(1), 0x01,\r
+ USAGE_PAGE(1), 0x08,\r
+ USAGE_MINIMUM(1), 0x01,\r
+ USAGE_MAXIMUM(1), 0x05,\r
+ OUTPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x03,\r
+ OUTPUT(1), 0x01,\r
+ REPORT_COUNT(1), 0x06,\r
+ REPORT_SIZE(1), 0x08,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(2), 0xff, 0x00,\r
+ USAGE_PAGE(1), 0x07,\r
+ USAGE_MINIMUM(1), 0x00,\r
+ USAGE_MAXIMUM(2), 0xff, 0x00,\r
+ INPUT(1), 0x00,\r
+ END_COLLECTION(0),\r
+\r
+ // Mouse\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x02, // Mouse\r
+ COLLECTION(1), 0x01, // Application\r
+ USAGE(1), 0x01, // Pointer\r
+ COLLECTION(1), 0x00, // Physical\r
+ REPORT_ID(1), REPORT_ID_MOUSE,\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x01,\r
+ USAGE_PAGE(1), 0x09, // Buttons\r
+ USAGE_MINIMUM(1), 0x1,\r
+ USAGE_MAXIMUM(1), 0x3,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ INPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x05,\r
+ INPUT(1), 0x01,\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x08,\r
+ USAGE_PAGE(1), 0x01,\r
+ USAGE(1), 0x30, // X\r
+ USAGE(1), 0x31, // Y\r
+ USAGE(1), 0x38, // scroll\r
+ LOGICAL_MINIMUM(1), 0x81,\r
+ LOGICAL_MAXIMUM(1), 0x7f,\r
+ INPUT(1), 0x06,\r
+ END_COLLECTION(0),\r
+ END_COLLECTION(0),\r
+\r
+\r
+ // Media Control\r
+ USAGE_PAGE(1), 0x0C,\r
+ USAGE(1), 0x01,\r
+ COLLECTION(1), 0x01,\r
+ REPORT_ID(1), REPORT_ID_VOLUME,\r
+ USAGE_PAGE(1), 0x0C,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x07,\r
+ USAGE(1), 0xB5, // Next Track\r
+ USAGE(1), 0xB6, // Previous Track\r
+ USAGE(1), 0xB7, // Stop\r
+ USAGE(1), 0xCD, // Play / Pause\r
+ USAGE(1), 0xE2, // Mute\r
+ USAGE(1), 0xE9, // Volume Up\r
+ USAGE(1), 0xEA, // Volume Down\r
+ INPUT(1), 0x02, // Input (Data, Variable, Absolute)\r
+ REPORT_COUNT(1), 0x01,\r
+ INPUT(1), 0x01,\r
+ END_COLLECTION(0),\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+ } else if (mouse_type == ABS_MOUSE) {\r
+ static uint8_t reportDescriptor[] = {\r
+\r
+ // Keyboard\r
+ USAGE_PAGE(1), 0x01,\r
+ USAGE(1), 0x06,\r
+ COLLECTION(1), 0x01,\r
+ REPORT_ID(1), REPORT_ID_KEYBOARD,\r
+ USAGE_PAGE(1), 0x07,\r
+ USAGE_MINIMUM(1), 0xE0,\r
+ USAGE_MAXIMUM(1), 0xE7,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x08,\r
+ INPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x08,\r
+ INPUT(1), 0x01,\r
+ REPORT_COUNT(1), 0x05,\r
+ REPORT_SIZE(1), 0x01,\r
+ USAGE_PAGE(1), 0x08,\r
+ USAGE_MINIMUM(1), 0x01,\r
+ USAGE_MAXIMUM(1), 0x05,\r
+ OUTPUT(1), 0x02,\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x03,\r
+ OUTPUT(1), 0x01,\r
+ REPORT_COUNT(1), 0x06,\r
+ REPORT_SIZE(1), 0x08,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(2), 0xff, 0x00,\r
+ USAGE_PAGE(1), 0x07,\r
+ USAGE_MINIMUM(1), 0x00,\r
+ USAGE_MAXIMUM(2), 0xff, 0x00,\r
+ INPUT(1), 0x00,\r
+ END_COLLECTION(0),\r
+\r
+ // Mouse\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x02, // Mouse\r
+ COLLECTION(1), 0x01, // Application\r
+ USAGE(1), 0x01, // Pointer\r
+ COLLECTION(1), 0x00, // Physical\r
+ REPORT_ID(1), REPORT_ID_MOUSE,\r
+\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x30, // X\r
+ USAGE(1), 0x31, // Y\r
+ LOGICAL_MINIMUM(1), 0x00, // 0\r
+ LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767\r
+ REPORT_SIZE(1), 0x10,\r
+ REPORT_COUNT(1), 0x02,\r
+ INPUT(1), 0x02, // Data, Variable, Absolute\r
+\r
+ USAGE_PAGE(1), 0x01, // Generic Desktop\r
+ USAGE(1), 0x38, // scroll\r
+ LOGICAL_MINIMUM(1), 0x81, // -127\r
+ LOGICAL_MAXIMUM(1), 0x7f, // 127\r
+ REPORT_SIZE(1), 0x08,\r
+ REPORT_COUNT(1), 0x01,\r
+ INPUT(1), 0x06, // Data, Variable, Relative\r
+\r
+ USAGE_PAGE(1), 0x09, // Buttons\r
+ USAGE_MINIMUM(1), 0x01,\r
+ USAGE_MAXIMUM(1), 0x03,\r
+ LOGICAL_MINIMUM(1), 0x00, // 0\r
+ LOGICAL_MAXIMUM(1), 0x01, // 1\r
+ REPORT_COUNT(1), 0x03,\r
+ REPORT_SIZE(1), 0x01,\r
+ INPUT(1), 0x02, // Data, Variable, Absolute\r
+ REPORT_COUNT(1), 0x01,\r
+ REPORT_SIZE(1), 0x05,\r
+ INPUT(1), 0x01, // Constant\r
+\r
+ END_COLLECTION(0),\r
+ END_COLLECTION(0),\r
+\r
+ // Media Control\r
+ USAGE_PAGE(1), 0x0C,\r
+ USAGE(1), 0x01,\r
+ COLLECTION(1), 0x01,\r
+ REPORT_ID(1), REPORT_ID_VOLUME,\r
+ USAGE_PAGE(1), 0x0C,\r
+ LOGICAL_MINIMUM(1), 0x00,\r
+ LOGICAL_MAXIMUM(1), 0x01,\r
+ REPORT_SIZE(1), 0x01,\r
+ REPORT_COUNT(1), 0x07,\r
+ USAGE(1), 0xB5, // Next Track\r
+ USAGE(1), 0xB6, // Previous Track\r
+ USAGE(1), 0xB7, // Stop\r
+ USAGE(1), 0xCD, // Play / Pause\r
+ USAGE(1), 0xE2, // Mute\r
+ USAGE(1), 0xE9, // Volume Up\r
+ USAGE(1), 0xEA, // Volume Down\r
+ INPUT(1), 0x02, // Input (Data, Variable, Absolute)\r
+ REPORT_COUNT(1), 0x01,\r
+ INPUT(1), 0x01,\r
+ END_COLLECTION(0),\r
+ };\r
+ reportLength = sizeof(reportDescriptor);\r
+ return reportDescriptor;\r
+ }\r
+\r
+ return NULL;\r
+}\r
+\r
+bool USBMouseKeyboard::EP1_OUT_callback() {\r
+ uint16_t bytesRead = 0;\r
+ uint8_t led[65];\r
+ USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);\r
+ \r
+ // we take led[1] because led[0] is the report ID\r
+ lock_status = led[1] & 0x07;\r
+ \r
+ // We activate the endpoint to be able to recceive data\r
+ if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+uint8_t USBMouseKeyboard::lockStatus() {\r
+ return lock_status;\r
+}\r
+\r
+bool USBMouseKeyboard::update(int16_t x, int16_t y, uint8_t button, int8_t z) {\r
+ switch (mouse_type) {\r
+ case REL_MOUSE:\r
+ while (x > 127) {\r
+ if (!mouseSend(127, 0, button, z)) return false;\r
+ x = x - 127;\r
+ }\r
+ while (x < -128) {\r
+ if (!mouseSend(-128, 0, button, z)) return false;\r
+ x = x + 128;\r
+ }\r
+ while (y > 127) {\r
+ if (!mouseSend(0, 127, button, z)) return false;\r
+ y = y - 127;\r
+ }\r
+ while (y < -128) {\r
+ if (!mouseSend(0, -128, button, z)) return false;\r
+ y = y + 128;\r
+ }\r
+ return mouseSend(x, y, button, z);\r
+ case ABS_MOUSE:\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = REPORT_ID_MOUSE;\r
+ report.data[1] = x & 0xff;\r
+ report.data[2] = (x >> 8) & 0xff;\r
+ report.data[3] = y & 0xff;\r
+ report.data[4] = (y >> 8) & 0xff;\r
+ report.data[5] = -z;\r
+ report.data[6] = button & 0x07;\r
+\r
+ report.length = 7;\r
+\r
+ return send(&report);\r
+ default:\r
+ return false;\r
+ }\r
+}\r
+\r
+bool USBMouseKeyboard::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {\r
+ HID_REPORT report;\r
+ report.data[0] = REPORT_ID_MOUSE;\r
+ report.data[1] = buttons & 0x07;\r
+ report.data[2] = x;\r
+ report.data[3] = y;\r
+ report.data[4] = -z; // >0 to scroll down, <0 to scroll up\r
+\r
+ report.length = 5;\r
+\r
+ return send(&report);\r
+}\r
+\r
+bool USBMouseKeyboard::move(int16_t x, int16_t y) {\r
+ return update(x, y, button, 0);\r
+}\r
+\r
+bool USBMouseKeyboard::scroll(int8_t z) {\r
+ return update(0, 0, button, z);\r
+}\r
+\r
+bool USBMouseKeyboard::doubleClick() {\r
+ if (!click(MOUSE_LEFT))\r
+ return false;\r
+ wait(0.1);\r
+ return click(MOUSE_LEFT);\r
+}\r
+\r
+bool USBMouseKeyboard::click(uint8_t button) {\r
+ if (!update(0, 0, button, 0))\r
+ return false;\r
+ wait(0.01);\r
+ return update(0, 0, 0, 0);\r
+}\r
+\r
+bool USBMouseKeyboard::press(uint8_t button_) {\r
+ button = button_ & 0x07;\r
+ return update(0, 0, button, 0);\r
+}\r
+\r
+bool USBMouseKeyboard::release(uint8_t button_) {\r
+ button = (button & (~button_)) & 0x07;\r
+ return update(0, 0, button, 0);\r
+}\r
+\r
+int USBMouseKeyboard::_putc(int c) {\r
+ return keyCode(c, keymap[c].modifier);\r
+}\r
+\r
+bool USBMouseKeyboard::keyCode(uint8_t key, uint8_t modifier) {\r
+ // Send a simulated keyboard keypress. Returns true if successful.\r
+\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = REPORT_ID_KEYBOARD;\r
+ report.data[1] = modifier;\r
+ report.data[2] = 0;\r
+ report.data[3] = keymap[key].usage;\r
+ report.data[4] = 0;\r
+ report.data[5] = 0;\r
+ report.data[6] = 0;\r
+ report.data[7] = 0;\r
+ report.data[8] = 0;\r
+\r
+ report.length = 9;\r
+\r
+ if (!send(&report)) {\r
+ return false;\r
+ }\r
+\r
+ report.data[1] = 0;\r
+ report.data[3] = 0;\r
+\r
+ if (!send(&report)) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+\r
+}\r
+\r
+\r
+bool USBMouseKeyboard::mediaControl(MEDIA_KEY key) {\r
+ HID_REPORT report;\r
+\r
+ report.data[0] = REPORT_ID_VOLUME;\r
+ report.data[1] = (1 << key) & 0x7f;\r
+\r
+ report.length = 2;\r
+\r
+ send(&report);\r
+ \r
+ report.data[0] = REPORT_ID_VOLUME;\r
+ report.data[1] = 0;\r
+\r
+ report.length = 2;\r
+\r
+ return send(&report);\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* 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.
+*/
+
+#ifndef USBMOUSEKEYBOARD_H
+#define USBMOUSEKEYBOARD_H
+
+#define REPORT_ID_KEYBOARD 1
+#define REPORT_ID_MOUSE 2
+#define REPORT_ID_VOLUME 3
+
+#include "USBMouse.h"
+#include "USBKeyboard.h"
+#include "Stream.h"
+#include "USBHID.h"
+
+/**
+ * USBMouseKeyboard example
+ * @code
+ *
+ * #include "mbed.h"
+ * #include "USBMouseKeyboard.h"
+ *
+ * USBMouseKeyboard key_mouse;
+ *
+ * int main(void)
+ * {
+ * while(1)
+ * {
+ * key_mouse.move(20, 0);
+ * key_mouse.printf("Hello From MBED\r\n");
+ * wait(1);
+ * }
+ * }
+ * @endcode
+ *
+ *
+ * @code
+ *
+ * #include "mbed.h"
+ * #include "USBMouseKeyboard.h"
+ *
+ * USBMouseKeyboard key_mouse(ABS_MOUSE);
+ *
+ * int main(void)
+ * {
+ * while(1)
+ * {
+ * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
+ * key_mouse.printf("Hello from MBED\r\n");
+ * wait(1);
+ * }
+ * }
+ * @endcode
+ */
+class USBMouseKeyboard: public USBHID, public Stream
+{
+ public:
+
+ /**
+ * Constructor
+ *
+ * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
+ * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
+ * @param vendor_id Your vendor_id (default: 0x1234)
+ * @param product_id Your product_id (default: 0x0001)
+ * @param product_release Your preoduct_release (default: 0x0001)
+ *
+ */
+ USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
+ USBHID(0, 0, vendor_id, product_id, product_release, false)
+ {
+ lock_status = 0;
+ button = 0;
+ this->mouse_type = mouse_type;
+ connect();
+ };
+
+ /**
+ * Write a state of the mouse
+ *
+ * @param x x-axis position
+ * @param y y-axis position
+ * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
+ * @param z wheel state (>0 to scroll down, <0 to scroll up)
+ * @returns true if there is no error, false otherwise
+ */
+ bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
+
+
+ /**
+ * Move the cursor to (x, y)
+ *
+ * @param x x-axis position
+ * @param y y-axis position
+ * @returns true if there is no error, false otherwise
+ */
+ bool move(int16_t x, int16_t y);
+
+ /**
+ * Press one or several buttons
+ *
+ * @param button button state (ex: press(MOUSE_LEFT))
+ * @returns true if there is no error, false otherwise
+ */
+ bool press(uint8_t button);
+
+ /**
+ * Release one or several buttons
+ *
+ * @param button button state (ex: release(MOUSE_LEFT))
+ * @returns true if there is no error, false otherwise
+ */
+ bool release(uint8_t button);
+
+ /**
+ * Double click (MOUSE_LEFT)
+ *
+ * @returns true if there is no error, false otherwise
+ */
+ bool doubleClick();
+
+ /**
+ * Click
+ *
+ * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
+ * @returns true if there is no error, false otherwise
+ */
+ bool click(uint8_t button);
+
+ /**
+ * Scrolling
+ *
+ * @param z value of the wheel (>0 to go down, <0 to go up)
+ * @returns true if there is no error, false otherwise
+ */
+ bool scroll(int8_t z);
+
+ /**
+ * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
+ *
+ * @code
+ * //To send CTRL + s (save)
+ * keyboard.keyCode('s', KEY_CTRL);
+ * @endcode
+ *
+ * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
+ * @param key character to send
+ * @returns true if there is no error, false otherwise
+ */
+ bool keyCode(uint8_t key, uint8_t modifier = 0);
+
+ /**
+ * Send a character
+ *
+ * @param c character to be sent
+ * @returns true if there is no error, false otherwise
+ */
+ virtual int _putc(int c);
+
+ /**
+ * Control media keys
+ *
+ * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
+ * @returns true if there is no error, false otherwise
+ */
+ bool mediaControl(MEDIA_KEY key);
+
+ /**
+ * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important:
+ * - First bit: NUM_LOCK
+ * - Second bit: CAPS_LOCK
+ * - Third bit: SCROLL_LOCK
+ *
+ * @returns status of lock keys
+ */
+ uint8_t lockStatus();
+
+ /*
+ * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
+ *
+ * @returns pointer to the report descriptor
+ */
+ virtual uint8_t * reportDesc();
+
+ /*
+ * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
+ *
+ * @returns if handle by subclass, return true
+ */
+ virtual bool EP1_OUT_callback();
+
+
+ private:
+ bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
+ MOUSE_TYPE mouse_type;
+ uint8_t button;
+ bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
+
+ uint8_t lock_status;
+
+ //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
+ virtual int _getc() { return -1;}
+};
+
+#endif
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef MIDIMESSAGE_H\r
+#define MIDIMESSAGE_H\r
+\r
+#include "mbed.h"\r
+\r
+// MIDI Message Format\r
+// \r
+// [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]\r
+//\r
+// MIDI Data Messages (Channel Specific)\r
+//\r
+// Message msg n m\r
+// ---------------------------------------------\r
+// Note Off 0x8 Key Velocity\r
+// Note On 0x9 Key Velocity\r
+// Polyphonic Aftertouch 0xA Key Pressure\r
+// Control Change 0xB Controller Value\r
+// Program Change 0xC Program -\r
+// Channel Aftertouch 0xD Pressure -\r
+// Pitch Wheel 0xE LSB MSB\r
+\r
+#define CABLE_NUM (0<<4)\r
+\r
+/** A MIDI message container */\r
+class MIDIMessage {\r
+public:\r
+ MIDIMessage() {}\r
+ \r
+ MIDIMessage(uint8_t *buf) {\r
+ *((uint32_t *)data) = *((uint32_t *)buf);\r
+ }\r
+ \r
+ // create messages\r
+ \r
+ /** Create a NoteOff message \r
+ * @param key Key ID\r
+ * @param velocity Key velocity (0-127, default = 127)\r
+ * @param channel Key channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */\r
+ static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x08;\r
+ msg.data[1] = 0x80 | (channel & 0x0F);\r
+ msg.data[2] = key & 0x7F;\r
+ msg.data[3] = velocity & 0x7F; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a NoteOn message \r
+ * @param key Key ID\r
+ * @param velocity Key velocity (0-127, default = 127)\r
+ * @param channel Key channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */\r
+ static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x09;\r
+ msg.data[1] = 0x90 | (channel & 0x0F);\r
+ msg.data[2] = key & 0x7F;\r
+ msg.data[3] = velocity & 0x7F; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a PolyPhonic Aftertouch message \r
+ * @param key Key ID\r
+ * @param pressure Aftertouch pressure (0-127)\r
+ * @param channel Key channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */ \r
+ static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x0A;\r
+ msg.data[1] = 0xA0 | (channel & 0x0F);\r
+ msg.data[2] = key & 0x7F;\r
+ msg.data[3] = pressure & 0x7F; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a Control Change message \r
+ * @param control Controller ID\r
+ * @param value Controller value (0-127)\r
+ * @param channel Controller channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */\r
+ static MIDIMessage ControlChange(int control, int value, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x0B;\r
+ msg.data[1] = 0xB0 | (channel & 0x0F);\r
+ msg.data[2] = control & 0x7F;\r
+ msg.data[3] = value & 0x7F; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a Program Change message \r
+ * @param program Program ID\r
+ * @param channel Channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */ \r
+ static MIDIMessage ProgramChange(int program, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x0C;\r
+ msg.data[1] = 0xC0 | (channel & 0x0F);\r
+ msg.data[2] = program & 0x7F;\r
+ msg.data[3] = 0x00; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a Channel Aftertouch message \r
+ * @param pressure Pressure \r
+ * @param channel Key channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */ \r
+ static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {\r
+ MIDIMessage msg;\r
+ msg.data[0] = CABLE_NUM | 0x0D;\r
+ msg.data[1] = 0xD0 | (channel & 0x0F);\r
+ msg.data[2] = pressure & 0x7F;\r
+ msg.data[3] = 0x00; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create a Pitch Wheel message \r
+ * @param pitch Pitch (-8192 - 8191, default = 0)\r
+ * @param channel Channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */ \r
+ static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {\r
+ MIDIMessage msg;\r
+ int p = pitch + 8192; // 0 - 16383, 8192 is center\r
+ msg.data[0] = CABLE_NUM | 0x0E;\r
+ msg.data[1] = 0xE0 | (channel & 0x0F);\r
+ msg.data[2] = p & 0x7F;\r
+ msg.data[3] = (p >> 7) & 0x7F; \r
+ return msg;\r
+ }\r
+ \r
+ /** Create an All Notes Off message \r
+ * @param channel Channel (0-15, default 0)\r
+ * @returns A MIDIMessage\r
+ */ \r
+ static MIDIMessage AllNotesOff(int channel = 0) {\r
+ return ControlChange(123, 0, channel);\r
+ }\r
+ \r
+ // decode messages\r
+ \r
+ /** MIDI Message Types */\r
+ enum MIDIMessageType {\r
+ ErrorType,\r
+ NoteOffType,\r
+ NoteOnType,\r
+ PolyphonicAftertouchType,\r
+ ControlChangeType,\r
+ ProgramChangeType,\r
+ ChannelAftertouchType,\r
+ PitchWheelType,\r
+ AllNotesOffType\r
+ };\r
+ \r
+ /** Read the message type\r
+ * @returns MIDIMessageType\r
+ */ \r
+ MIDIMessageType type() {\r
+ switch((data[1] >> 4) & 0xF) {\r
+ case 0x8: return NoteOffType;\r
+ case 0x9: return NoteOnType;\r
+ case 0xA: return PolyphonicAftertouchType;\r
+ case 0xB: \r
+ if(controller() < 120) { // standard controllers\r
+ return ControlChangeType;\r
+ } else if(controller() == 123) {\r
+ return AllNotesOffType;\r
+ } else {\r
+ return ErrorType; // unsupported atm\r
+ }\r
+ case 0xC: return ProgramChangeType;\r
+ case 0xD: return ChannelAftertouchType;\r
+ case 0xE: return PitchWheelType;\r
+ default: return ErrorType;\r
+ }\r
+ }\r
+\r
+ /** Read the channel number */ \r
+ int channel() {\r
+ return (data[1] & 0x0F);\r
+ }\r
+ \r
+ /** Read the key ID */ \r
+ int key() {\r
+ return (data[2] & 0x7F); \r
+ }\r
+ \r
+ /** Read the velocity */ \r
+ int velocity() {\r
+ return (data[3] & 0x7F); \r
+ }\r
+\r
+ /** Read the controller value */ \r
+ int value() {\r
+ return (data[3] & 0x7F); \r
+ }\r
+ \r
+ /** Read the aftertouch pressure */ \r
+ int pressure() {\r
+ if(type() == PolyphonicAftertouchType) {\r
+ return (data[3] & 0x7F); \r
+ } else {\r
+ return (data[2] & 0x7F); \r
+ }\r
+ }\r
+\r
+ /** Read the controller number */ \r
+ int controller() {\r
+ return (data[2] & 0x7F); \r
+ }\r
+\r
+ /** Read the program number */ \r
+ int program() {\r
+ return (data[2] & 0x7F); \r
+ }\r
+ \r
+ /** Read the pitch value */ \r
+ int pitch() {\r
+ int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);\r
+ return p - 8192; // 0 - 16383, 8192 is center\r
+ }\r
+ \r
+ uint8_t data[4];\r
+}; \r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBMIDI.h"\r
+#include "USBBusInterface.h"\r
+\r
+\r
+USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {\r
+ midi_evt = NULL;\r
+ USBDevice::connect();\r
+}\r
+\r
+void USBMIDI::write(MIDIMessage m) {\r
+ USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK);\r
+}\r
+\r
+\r
+void USBMIDI::attach(void (*fptr)(MIDIMessage)) {\r
+ midi_evt = fptr;\r
+}\r
+\r
+\r
+bool USBMIDI::EP2_OUT_callback() {\r
+ uint8_t buf[64];\r
+ uint16_t len;\r
+ readEP(EPBULK_OUT, buf, &len, 64);\r
+\r
+ if (midi_evt != NULL) {\r
+ for (int i=0; i<len; i+=4) {\r
+ midi_evt(MIDIMessage(buf+i));\r
+ }\r
+ }\r
+\r
+ // We reactivate the endpoint to receive next characters\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the\r
+// configuration is not supported.\r
+bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);\r
+ addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ // We activate the endpoint to be able to receive data\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+\r
+uint8_t * USBMIDI::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x0c, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBMIDI::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x16, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
+\r
+\r
+uint8_t * USBMIDI::configurationDesc() {\r
+ static uint8_t configDescriptor[] = {\r
+ // configuration descriptor\r
+ 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,\r
+\r
+ // The Audio Interface Collection\r
+ 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor\r
+ 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor\r
+ 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors\r
+ 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor\r
+\r
+ // MIDI IN JACKS\r
+ 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,\r
+ 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,\r
+\r
+ // MIDI OUT JACKS\r
+ 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,\r
+ 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,\r
+\r
+ // OUT endpoint descriptor\r
+ 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,\r
+ 0x05, 0x25, 0x01, 0x01, 0x01,\r
+\r
+ // IN endpoint descriptor\r
+ 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,\r
+ 0x05, 0x25, 0x01, 0x01, 0x03,\r
+ };\r
+ return configDescriptor;\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* 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.
+*/
+
+#ifndef USBMIDI_H
+#define USBMIDI_H
+
+/* These headers are included for child class. */
+#include "USBEndpoints.h"
+#include "USBDescriptor.h"
+#include "USBDevice_Types.h"
+
+#include "USBDevice.h"
+#include "MIDIMessage.h"
+
+#define DEFAULT_CONFIGURATION (1)
+
+/**
+* USBMIDI example
+*
+* @code
+* #include "mbed.h"
+* #include "USBMIDI.h"
+*
+* USBMIDI midi;
+*
+* int main() {
+* while (1) {
+* for(int i=48; i<83; i++) { // send some messages!
+* midi.write(MIDIMessage::NoteOn(i));
+* wait(0.25);
+* midi.write(MIDIMessage::NoteOff(i));
+* wait(0.5);
+* }
+* }
+* }
+* @endcode
+*/
+class USBMIDI: public USBDevice {
+public:
+
+ /**
+ * Constructor
+ *
+ * @param vendor_id Your vendor_id
+ * @param product_id Your product_id
+ * @param product_release Your preoduct_release
+ */
+ USBMIDI(uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001);
+
+ /**
+ * Send a MIDIMessage
+ *
+ * @param m The MIDIMessage to send
+ */
+ void write(MIDIMessage m);
+
+ /**
+ * Attach a callback for when a MIDIEvent is received
+ *
+ * @param fptr function pointer
+ */
+ void attach(void (*fptr)(MIDIMessage));
+
+
+protected:
+ virtual bool EP2_OUT_callback();
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);
+ /*
+ * Get string product descriptor
+ *
+ * @returns pointer to the string product descriptor
+ */
+ virtual uint8_t * stringIproductDesc();
+
+ /*
+ * Get string interface descriptor
+ *
+ * @returns pointer to the string interface descriptor
+ */
+ virtual uint8_t * stringIinterfaceDesc();
+
+ /*
+ * Get configuration descriptor
+ *
+ * @returns pointer to the configuration descriptor
+ */
+ virtual uint8_t * configurationDesc();
+
+private:
+ void (*midi_evt)(MIDIMessage);
+
+};
+
+#endif
+
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBMSD.h"\r
+\r
+#define DISK_OK 0x00\r
+#define NO_INIT 0x01\r
+#define NO_DISK 0x02\r
+#define WRITE_PROTECT 0x04\r
+\r
+#define CBW_Signature 0x43425355\r
+#define CSW_Signature 0x53425355\r
+\r
+// SCSI Commands\r
+#define TEST_UNIT_READY 0x00\r
+#define REQUEST_SENSE 0x03\r
+#define FORMAT_UNIT 0x04\r
+#define INQUIRY 0x12\r
+#define MODE_SELECT6 0x15\r
+#define MODE_SENSE6 0x1A\r
+#define START_STOP_UNIT 0x1B\r
+#define MEDIA_REMOVAL 0x1E\r
+#define READ_FORMAT_CAPACITIES 0x23\r
+#define READ_CAPACITY 0x25\r
+#define READ10 0x28\r
+#define WRITE10 0x2A\r
+#define VERIFY10 0x2F\r
+#define READ12 0xA8\r
+#define WRITE12 0xAA\r
+#define MODE_SELECT10 0x55\r
+#define MODE_SENSE10 0x5A\r
+\r
+// MSC class specific requests\r
+#define MSC_REQUEST_RESET 0xFF\r
+#define MSC_REQUEST_GET_MAX_LUN 0xFE\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+// max packet size\r
+#define MAX_PACKET MAX_PACKET_SIZE_EPBULK\r
+\r
+// CSW Status\r
+enum Status {\r
+ CSW_PASSED,\r
+ CSW_FAILED,\r
+ CSW_ERROR,\r
+};\r
+\r
+\r
+USBMSD::USBMSD(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {\r
+}\r
+\r
+\r
+\r
+// Called in ISR context to process a class specific request\r
+bool USBMSD::USBCallback_request(void) {\r
+\r
+ bool success = false;\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+ static uint8_t maxLUN[1] = {0};\r
+\r
+ if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {\r
+ switch (transfer->setup.bRequest) {\r
+ case MSC_REQUEST_RESET:\r
+ reset();\r
+ success = true;\r
+ break;\r
+ case MSC_REQUEST_GET_MAX_LUN:\r
+ transfer->remaining = 1;\r
+ transfer->ptr = maxLUN;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+\r
+bool USBMSD::connect() {\r
+\r
+ //disk initialization\r
+ if (disk_status() & NO_INIT) {\r
+ if (disk_initialize()) {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // get number of blocks\r
+ BlockCount = disk_sectors();\r
+\r
+ // get memory size\r
+ MemorySize = disk_size();\r
+\r
+ if (BlockCount >= 0) {\r
+ BlockSize = MemorySize / BlockCount;\r
+ if (BlockSize != 0) {\r
+ page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));\r
+ if (page == NULL)\r
+ return false;\r
+ }\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ //connect the device\r
+ USBDevice::connect();\r
+ return true;\r
+}\r
+\r
+\r
+void USBMSD::reset() {\r
+ stage = READ_CBW;\r
+}\r
+\r
+\r
+// Called in ISR context called when a data is received\r
+bool USBMSD::EP2_OUT_callback() {\r
+ uint16_t size = 0;\r
+ uint8_t buf[MAX_PACKET_SIZE_EPBULK];\r
+ readEP(EPBULK_OUT, buf, &size, MAX_PACKET_SIZE_EPBULK);\r
+ switch (stage) {\r
+ // the device has to decode the CBW received\r
+ case READ_CBW:\r
+ CBWDecode(buf, size);\r
+ break;\r
+\r
+ // the device has to receive data from the host\r
+ case PROCESS_CBW:\r
+ switch (cbw.CB[0]) {\r
+ case WRITE10:\r
+ case WRITE12:\r
+ memoryWrite(buf, size);\r
+ break;\r
+ case VERIFY10:\r
+ memoryVerify(buf, size);\r
+ break;\r
+ }\r
+ break;\r
+\r
+ // an error has occured: stall endpoint and send CSW\r
+ default:\r
+ stallEndpoint(EPBULK_OUT);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ break;\r
+ }\r
+\r
+ //reactivate readings on the OUT bulk endpoint\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+// Called in ISR context when a data has been transferred\r
+bool USBMSD::EP2_IN_callback() {\r
+ switch (stage) {\r
+\r
+ // the device has to send data to the host\r
+ case PROCESS_CBW:\r
+ switch (cbw.CB[0]) {\r
+ case READ10:\r
+ case READ12:\r
+ memoryRead();\r
+ break;\r
+ }\r
+ break;\r
+\r
+ //the device has to send a CSW\r
+ case SEND_CSW:\r
+ sendCSW();\r
+ break;\r
+\r
+ // an error has occured\r
+ case ERROR:\r
+ stallEndpoint(EPBULK_IN);\r
+ sendCSW();\r
+ break;\r
+\r
+ // the host has received the CSW -> we wait a CBW\r
+ case WAIT_CSW:\r
+ stage = READ_CBW;\r
+ break;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+void USBMSD::memoryWrite (uint8_t * buf, uint16_t size) {\r
+\r
+ if ((addr + size) > MemorySize) {\r
+ size = MemorySize - addr;\r
+ stage = ERROR;\r
+ stallEndpoint(EPBULK_OUT);\r
+ }\r
+\r
+ // we fill an array in RAM of 1 block before writing it in memory\r
+ for (int i = 0; i < size; i++)\r
+ page[addr%BlockSize + i] = buf[i];\r
+\r
+ // if the array is filled, write it in memory\r
+ if (!((addr + size)%BlockSize)) {\r
+ if (!(disk_status() & WRITE_PROTECT)) {\r
+ disk_write((const char *)page, addr/BlockSize);\r
+ }\r
+ }\r
+\r
+ addr += size;\r
+ length -= size;\r
+ csw.DataResidue -= size;\r
+\r
+ if ((!length) || (stage != PROCESS_CBW)) {\r
+ csw.Status = (stage == ERROR) ? CSW_FAILED : CSW_PASSED;\r
+ sendCSW();\r
+ }\r
+}\r
+\r
+void USBMSD::memoryVerify (uint8_t * buf, uint16_t size) {\r
+ uint32_t n;\r
+\r
+ if ((addr + size) > MemorySize) {\r
+ size = MemorySize - addr;\r
+ stage = ERROR;\r
+ stallEndpoint(EPBULK_OUT);\r
+ }\r
+\r
+ // beginning of a new block -> load a whole block in RAM\r
+ if (!(addr%BlockSize))\r
+ disk_read((char *)page, addr/BlockSize);\r
+\r
+ // info are in RAM -> no need to re-read memory\r
+ for (n = 0; n < size; n++) {\r
+ if (page[addr%BlockSize + n] != buf[n]) {\r
+ memOK = false;\r
+ break;\r
+ }\r
+ }\r
+\r
+ addr += size;\r
+ length -= size;\r
+ csw.DataResidue -= size;\r
+\r
+ if ( !length || (stage != PROCESS_CBW)) {\r
+ csw.Status = (memOK && (stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;\r
+ sendCSW();\r
+ }\r
+}\r
+\r
+\r
+bool USBMSD::inquiryRequest (void) {\r
+ uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,\r
+ 36 - 4, 0x80, 0x00, 0x00,\r
+ 'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',\r
+ 'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',\r
+ '1', '.', '0', ' ',\r
+ };\r
+ if (!write(inquiry, sizeof(inquiry))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool USBMSD::readFormatCapacity() {\r
+ uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,\r
+ (BlockCount >> 24) & 0xff,\r
+ (BlockCount >> 16) & 0xff,\r
+ (BlockCount >> 8) & 0xff,\r
+ (BlockCount >> 0) & 0xff,\r
+\r
+ 0x02,\r
+ (BlockSize >> 16) & 0xff,\r
+ (BlockSize >> 8) & 0xff,\r
+ (BlockSize >> 0) & 0xff,\r
+ };\r
+ if (!write(capacity, sizeof(capacity))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+bool USBMSD::readCapacity (void) {\r
+ uint8_t capacity[] = {\r
+ ((BlockCount - 1) >> 24) & 0xff,\r
+ ((BlockCount - 1) >> 16) & 0xff,\r
+ ((BlockCount - 1) >> 8) & 0xff,\r
+ ((BlockCount - 1) >> 0) & 0xff,\r
+\r
+ (BlockSize >> 24) & 0xff,\r
+ (BlockSize >> 16) & 0xff,\r
+ (BlockSize >> 8) & 0xff,\r
+ (BlockSize >> 0) & 0xff,\r
+ };\r
+ if (!write(capacity, sizeof(capacity))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+bool USBMSD::write (uint8_t * buf, uint16_t size) {\r
+\r
+ if (size >= cbw.DataLength) {\r
+ size = cbw.DataLength;\r
+ }\r
+ stage = SEND_CSW;\r
+\r
+ if (!writeNB(EPBULK_IN, buf, size, MAX_PACKET_SIZE_EPBULK)) {\r
+ return false;\r
+ }\r
+\r
+ csw.DataResidue -= size;\r
+ csw.Status = CSW_PASSED;\r
+ return true;\r
+}\r
+\r
+\r
+bool USBMSD::modeSense6 (void) {\r
+ uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };\r
+ if (!write(sense6, sizeof(sense6))) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+void USBMSD::sendCSW() {\r
+ csw.Signature = CSW_Signature;\r
+ writeNB(EPBULK_IN, (uint8_t *)&csw, sizeof(CSW), MAX_PACKET_SIZE_EPBULK);\r
+ stage = WAIT_CSW;\r
+}\r
+\r
+bool USBMSD::requestSense (void) {\r
+ uint8_t request_sense[] = {\r
+ 0x70,\r
+ 0x00,\r
+ 0x05, // Sense Key: illegal request\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x0A,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x30,\r
+ 0x01,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ };\r
+\r
+ if (!write(request_sense, sizeof(request_sense))) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+void USBMSD::fail() {\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+}\r
+\r
+\r
+void USBMSD::CBWDecode(uint8_t * buf, uint16_t size) {\r
+ if (size == sizeof(cbw)) {\r
+ memcpy((uint8_t *)&cbw, buf, size);\r
+ if (cbw.Signature == CBW_Signature) {\r
+ csw.Tag = cbw.Tag;\r
+ csw.DataResidue = cbw.DataLength;\r
+ if ((cbw.CBLength < 1) || (cbw.CBLength > 16) ) {\r
+ fail();\r
+ } else {\r
+ switch (cbw.CB[0]) {\r
+ case TEST_UNIT_READY:\r
+ testUnitReady();\r
+ break;\r
+ case REQUEST_SENSE:\r
+ requestSense();\r
+ break;\r
+ case INQUIRY:\r
+ inquiryRequest();\r
+ break;\r
+ case MODE_SENSE6:\r
+ modeSense6();\r
+ break;\r
+ case READ_FORMAT_CAPACITIES:\r
+ readFormatCapacity();\r
+ break;\r
+ case READ_CAPACITY:\r
+ readCapacity();\r
+ break;\r
+ case READ10:\r
+ case READ12:\r
+ if (infoTransfer()) {\r
+ if ((cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ memoryRead();\r
+ } else {\r
+ stallEndpoint(EPBULK_OUT);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ case WRITE10:\r
+ case WRITE12:\r
+ if (infoTransfer()) {\r
+ if (!(cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ } else {\r
+ stallEndpoint(EPBULK_IN);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ case VERIFY10:\r
+ if (!(cbw.CB[1] & 0x02)) {\r
+ csw.Status = CSW_PASSED;\r
+ sendCSW();\r
+ break;\r
+ }\r
+ if (infoTransfer()) {\r
+ if (!(cbw.Flags & 0x80)) {\r
+ stage = PROCESS_CBW;\r
+ memOK = true;\r
+ } else {\r
+ stallEndpoint(EPBULK_IN);\r
+ csw.Status = CSW_ERROR;\r
+ sendCSW();\r
+ }\r
+ }\r
+ break;\r
+ default:\r
+ fail();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+void USBMSD::testUnitReady (void) {\r
+\r
+ if (cbw.DataLength != 0) {\r
+ if ((cbw.Flags & 0x80) != 0) {\r
+ stallEndpoint(EPBULK_IN);\r
+ } else {\r
+ stallEndpoint(EPBULK_OUT);\r
+ }\r
+ }\r
+\r
+ csw.Status = CSW_PASSED;\r
+ sendCSW();\r
+}\r
+\r
+\r
+void USBMSD::memoryRead (void) {\r
+ uint32_t n;\r
+\r
+ n = (length > MAX_PACKET) ? MAX_PACKET : length;\r
+\r
+ if ((addr + n) > MemorySize) {\r
+ n = MemorySize - addr;\r
+ stage = ERROR;\r
+ }\r
+\r
+ // we read an entire block\r
+ if (!(addr%BlockSize))\r
+ disk_read((char *)page, addr/BlockSize);\r
+\r
+ // write data which are in RAM\r
+ writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ addr += n;\r
+ length -= n;\r
+\r
+ csw.DataResidue -= n;\r
+\r
+ if ( !length || (stage != PROCESS_CBW)) {\r
+ csw.Status = (stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;\r
+ stage = (stage == PROCESS_CBW) ? SEND_CSW : stage;\r
+ }\r
+}\r
+\r
+\r
+bool USBMSD::infoTransfer (void) {\r
+ uint32_t n;\r
+\r
+ // Logical Block Address of First Block\r
+ n = (cbw.CB[2] << 24) | (cbw.CB[3] << 16) | (cbw.CB[4] << 8) | (cbw.CB[5] << 0);\r
+\r
+ addr = n * BlockSize;\r
+\r
+ // Number of Blocks to transfer\r
+ switch (cbw.CB[0]) {\r
+ case READ10:\r
+ case WRITE10:\r
+ case VERIFY10:\r
+ n = (cbw.CB[7] << 8) | (cbw.CB[8] << 0);\r
+ break;\r
+\r
+ case READ12:\r
+ case WRITE12:\r
+ n = (cbw.CB[6] << 24) | (cbw.CB[7] << 16) | (cbw.CB[8] << 8) | (cbw.CB[9] << 0);\r
+ break;\r
+ }\r
+\r
+ length = n * BlockSize;\r
+\r
+ if (!cbw.DataLength) { // host requests no data\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+ return false;\r
+ }\r
+\r
+ if (cbw.DataLength != length) {\r
+ if ((cbw.Flags & 0x80) != 0) {\r
+ stallEndpoint(EPBULK_IN);\r
+ } else {\r
+ stallEndpoint(EPBULK_OUT);\r
+ }\r
+\r
+ csw.Status = CSW_FAILED;\r
+ sendCSW();\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the\r
+// configuration is not supported.\r
+bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);\r
+ addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ //activate readings\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+\r
+uint8_t * USBMSD::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x08, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'M',0,'S',0,'D',0 //bString iInterface - MSD\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBMSD::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x12, //bLength\r
+ STRING_DESCRIPTOR, //bDescriptorType 0x03\r
+ 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
+\r
+\r
+uint8_t * USBMSD::configurationDesc() {\r
+ static uint8_t configDescriptor[] = {\r
+\r
+ // Configuration 1\r
+ 9, // bLength\r
+ 2, // bDescriptorType\r
+ LSB(9 + 9 + 7 + 7), // wTotalLength\r
+ MSB(9 + 9 + 7 + 7),\r
+ 0x01, // bNumInterfaces\r
+ 0x01, // bConfigurationValue: 0x01 is used to select this configuration\r
+ 0x00, // iConfiguration: no string to describe this configuration\r
+ 0xC0, // bmAttributes\r
+ 100, // bMaxPower, device power consumption is 100 mA\r
+\r
+ // Interface 0, Alternate Setting 0, MSC Class\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 0x00, // bInterfaceNumber\r
+ 0x00, // bAlternateSetting\r
+ 0x02, // bNumEndpoints\r
+ 0x08, // bInterfaceClass\r
+ 0x06, // bInterfaceSubClass\r
+ 0x50, // bInterfaceProtocol\r
+ 0x04, // iInterface\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_IN), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)\r
+ 0, // bInterval\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)\r
+ 0 // bInterval\r
+ };\r
+ return configDescriptor;\r
+}
\ No newline at end of file
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+\r
+#ifndef USBMSD_H\r
+#define USBMSD_H\r
+\r
+/* These headers are included for child class. */\r
+#include "USBEndpoints.h"\r
+#include "USBDescriptor.h"\r
+#include "USBDevice_Types.h"\r
+\r
+#include "USBDevice.h"\r
+\r
+\r
+\r
+#ifdef __GNUC__\r
+ /* Packing for structs in GCC. */\r
+ #define PACK_STRUCT_STRUCT __attribute__((packed))\r
+ #define PACK_STRUCT_BEGIN\r
+ #define PACK_STRUCT_END\r
+#else /* !__GNUC__ */\r
+ /* Packing for structs in ARM compiler. */\r
+ #define PACK_STRUCT_STRUCT\r
+ #define PACK_STRUCT_BEGIN __packed\r
+ #define PACK_STRUCT_END\r
+#endif /* __GNUC__ */\r
+\r
+\r
+\r
+\r
+/**\r
+ * USBMSD class: generic class in order to use all kinds of blocks storage chip\r
+ *\r
+ * Introduction\r
+ *\r
+ * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)\r
+ * from a computer over USB. But this class doesn't work standalone, you need to subclass this class\r
+ * and define virtual functions which are called in USBMSD.\r
+ *\r
+ * How to use this class with your chip ?\r
+ *\r
+ * You have to inherit and define some pure virtual functions (mandatory step):\r
+ * - virtual int disk_read(char * data, int block): function to read a block\r
+ * - virtual int disk_write(const char * data, int block): function to write a block\r
+ * - virtual int disk_initialize(): function to initialize the memory\r
+ * - virtual int disk_sectors(): return the number of blocks\r
+ * - virtual int disk_size(): return the memory size\r
+ * - virtual int disk_status(): return the status of the storage chip (0: OK, 1: not initialized, 2: no medium in the drive, 4: write protection)\r
+ *\r
+ * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with\r
+ * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which\r
+ * will access the sd card. You can do a master/slave system using the disk_status method.\r
+ *\r
+ * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)\r
+ * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.\r
+ * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information\r
+ * such as the number of blocks and the memory size.\r
+ */\r
+class USBMSD: public USBDevice {\r
+public:\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param vendor_id Your vendor_id\r
+ * @param product_id Your product_id\r
+ * @param product_release Your preoduct_release\r
+ */\r
+ USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);\r
+\r
+ /**\r
+ * Connect the USB MSD device. Establish disk initialization before really connect the device.\r
+ *\r
+ * @returns true if successful\r
+ */\r
+ bool connect();\r
+\r
+\r
+protected:\r
+\r
+ /*\r
+ * read a block on a storage chip\r
+ *\r
+ * @param data pointer where will be stored read data\r
+ * @param block block number\r
+ * @returns 0 if successful\r
+ */\r
+ virtual int disk_read(char * data, int block) = 0;\r
+\r
+ /*\r
+ * write a block on a storage chip\r
+ *\r
+ * @param data data to write\r
+ * @param block block number\r
+ * @returns 0 if successful\r
+ */\r
+ virtual int disk_write(const char * data, int block) = 0;\r
+\r
+ /*\r
+ * Disk initilization\r
+ */\r
+ virtual int disk_initialize() = 0;\r
+\r
+ /*\r
+ * Return the number of blocks\r
+ *\r
+ * @returns number of blocks\r
+ */\r
+ virtual int disk_sectors() = 0;\r
+\r
+ /*\r
+ * Return memory size\r
+ *\r
+ * @returns memory size\r
+ */\r
+ virtual int disk_size() = 0;\r
+\r
+\r
+ /*\r
+ * To check the status of the storage chip\r
+ *\r
+ * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected\r
+ */\r
+ virtual int disk_status() = 0;\r
+\r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+\r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+\r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc();\r
+\r
+ /*\r
+ * Callback called when a packet is received\r
+ */\r
+ virtual bool EP2_OUT_callback();\r
+\r
+ /*\r
+ * Callback called when a packet has been sent\r
+ */\r
+ virtual bool EP2_IN_callback();\r
+\r
+ /*\r
+ * Set configuration of device. Add endpoints\r
+ */\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);\r
+\r
+ /*\r
+ * Callback called to process class specific requests\r
+ */\r
+ virtual bool USBCallback_request();\r
+\r
+\r
+private:\r
+\r
+ // MSC Bulk-only Stage\r
+ enum Stage {\r
+ READ_CBW, // wait a CBW\r
+ ERROR, // error\r
+ PROCESS_CBW, // process a CBW request\r
+ SEND_CSW, // send a CSW\r
+ WAIT_CSW, // wait that a CSW has been effectively sent\r
+ };\r
+\r
+ // Bulk-only CBW\r
+ typedef PACK_STRUCT_BEGIN struct {\r
+ uint32_t Signature;\r
+ uint32_t Tag;\r
+ uint32_t DataLength;\r
+ uint8_t Flags;\r
+ uint8_t LUN;\r
+ uint8_t CBLength;\r
+ uint8_t CB[16];\r
+ } PACK_STRUCT_STRUCT CBW;\r
+\r
+ // Bulk-only CSW\r
+ typedef PACK_STRUCT_BEGIN struct {\r
+ uint32_t Signature;\r
+ uint32_t Tag;\r
+ uint32_t DataResidue;\r
+ uint8_t Status;\r
+ } PACK_STRUCT_STRUCT CSW;\r
+\r
+\r
+ //state of the bulk-only state machine\r
+ Stage stage;\r
+\r
+ // current CBW\r
+ CBW cbw;\r
+\r
+ // CSW which will be sent\r
+ CSW csw;\r
+\r
+ // addr where will be read or written data\r
+ uint32_t addr;\r
+\r
+ // length of a reading or writing\r
+ uint32_t length;\r
+\r
+ // memory OK (after a memoryVerify)\r
+ bool memOK;\r
+\r
+ // cache in RAM before writing in memory. Useful also to read a block.\r
+ uint8_t * page;\r
+\r
+ int BlockSize;\r
+ int MemorySize;\r
+ int BlockCount;\r
+\r
+ void CBWDecode(uint8_t * buf, uint16_t size);\r
+ void sendCSW (void);\r
+ bool inquiryRequest (void);\r
+ bool write (uint8_t * buf, uint16_t size);\r
+ bool readFormatCapacity();\r
+ bool readCapacity (void);\r
+ bool infoTransfer (void);\r
+ void memoryRead (void);\r
+ bool modeSense6 (void);\r
+ void testUnitReady (void);\r
+ bool requestSense (void);\r
+ void memoryVerify (uint8_t * buf, uint16_t size);\r
+ void memoryWrite (uint8_t * buf, uint16_t size);\r
+ void reset();\r
+ void fail();\r
+};\r
+\r
+#endif\r
--- /dev/null
+#ifndef CIRCBUFFER_H
+#define CIRCBUFFER_H
+
+template <class T>
+class CircBuffer {
+public:
+ CircBuffer(int length) {
+ write = 0;
+ read = 0;
+ size = length + 1;
+ buf = (T *)malloc(size * sizeof(T));
+ };
+
+ bool isFull() {
+ return ((write + 1) % size == read);
+ };
+
+ bool isEmpty() {
+ return (read == write);
+ };
+
+ void queue(T k) {
+ if (isFull()) {
+ read++;
+ read %= size;
+ }
+ buf[write++] = k;
+ write %= size;
+ }
+
+ uint16_t available() {
+ return (write >= read) ? write - read : size - read + write;
+ };
+
+ bool dequeue(T * c) {
+ bool empty = isEmpty();
+ if (!empty) {
+ *c = buf[read++];
+ read %= size;
+ }
+ return(!empty);
+ };
+
+private:
+ volatile uint16_t write;
+ volatile uint16_t read;
+ uint16_t size;
+ T * buf;
+};
+
+#endif
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBCDC.h"\r
+#include "USBBusInterface.h"\r
+\r
+static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};\r
+\r
+#define DEFAULT_CONFIGURATION (1)\r
+\r
+#define CDC_SET_LINE_CODING 0x20\r
+#define CDC_GET_LINE_CODING 0x21\r
+#define CDC_SET_CONTROL_LINE_STATE 0x22\r
+\r
+#define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK\r
+\r
+USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {\r
+ USBDevice::connect();\r
+}\r
+\r
+bool USBCDC::USBCallback_request(void) {\r
+ /* Called in ISR context */\r
+\r
+ bool success = false;\r
+ CONTROL_TRANSFER * transfer = getTransferPtr();\r
+\r
+ /* Process class-specific requests */\r
+\r
+ if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {\r
+ switch (transfer->setup.bRequest) {\r
+ case CDC_GET_LINE_CODING:\r
+ transfer->remaining = 7;\r
+ transfer->ptr = cdc_line_coding;\r
+ transfer->direction = DEVICE_TO_HOST;\r
+ success = true;\r
+ break;\r
+ case CDC_SET_LINE_CODING:\r
+ transfer->remaining = 7;\r
+ success = true;\r
+ break;\r
+ case CDC_SET_CONTROL_LINE_STATE:\r
+ success = true;\r
+ break;\r
+ default:\r
+ break;\r
+ }\r
+ }\r
+\r
+ return success;\r
+}\r
+\r
+\r
+// Called in ISR context\r
+// Set configuration. Return false if the\r
+// configuration is not supported.\r
+bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {\r
+ if (configuration != DEFAULT_CONFIGURATION) {\r
+ return false;\r
+ }\r
+\r
+ // Configure endpoints > 0\r
+ addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);\r
+ addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);\r
+ addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+\r
+ // We activate the endpoint to be able to recceive data\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+bool USBCDC::send(uint8_t * buffer, uint16_t size) {\r
+ return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);\r
+}\r
+\r
+bool USBCDC::readEP(uint8_t * buffer, uint16_t * size) {\r
+ if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+bool USBCDC::readEP_NB(uint8_t * buffer, uint16_t * size) {\r
+ if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))\r
+ return false;\r
+ return true;\r
+}\r
+\r
+\r
+uint8_t * USBCDC::deviceDesc() {\r
+ static uint8_t deviceDescriptor[] = {\r
+ 18, // bLength\r
+ 1, // bDescriptorType\r
+ 0x10, 0x01, // bcdUSB\r
+ 2, // bDeviceClass\r
+ 0, // bDeviceSubClass\r
+ 0, // bDeviceProtocol\r
+ MAX_PACKET_SIZE_EP0, // bMaxPacketSize0\r
+ LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor\r
+ LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct\r
+ 0x00, 0x01, // bcdDevice\r
+ 1, // iManufacturer\r
+ 2, // iProduct\r
+ 3, // iSerialNumber\r
+ 1 // bNumConfigurations\r
+ };\r
+ return deviceDescriptor;\r
+}\r
+\r
+uint8_t * USBCDC::stringIinterfaceDesc() {\r
+ static uint8_t stringIinterfaceDescriptor[] = {\r
+ 0x08,\r
+ STRING_DESCRIPTOR,\r
+ 'C',0,'D',0,'C',0,\r
+ };\r
+ return stringIinterfaceDescriptor;\r
+}\r
+\r
+uint8_t * USBCDC::stringIproductDesc() {\r
+ static uint8_t stringIproductDescriptor[] = {\r
+ 0x16,\r
+ STRING_DESCRIPTOR,\r
+ 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0\r
+ };\r
+ return stringIproductDescriptor;\r
+}\r
+\r
+\r
+#define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)\r
+\r
+uint8_t * USBCDC::configurationDesc() {\r
+ static uint8_t configDescriptor[] = {\r
+ 9, // bLength;\r
+ 2, // bDescriptorType;\r
+ LSB(CONFIG1_DESC_SIZE), // wTotalLength\r
+ MSB(CONFIG1_DESC_SIZE),\r
+ 2, // bNumInterfaces\r
+ 1, // bConfigurationValue\r
+ 0, // iConfiguration\r
+ 0x80, // bmAttributes\r
+ 50, // bMaxPower\r
+\r
+ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 0, // bInterfaceNumber\r
+ 0, // bAlternateSetting\r
+ 1, // bNumEndpoints\r
+ 0x02, // bInterfaceClass\r
+ 0x02, // bInterfaceSubClass\r
+ 0x01, // bInterfaceProtocol\r
+ 0, // iInterface\r
+\r
+ // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x00, // bDescriptorSubtype\r
+ 0x10, 0x01, // bcdCDC\r
+\r
+ // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x01, // bDescriptorSubtype\r
+ 0x03, // bmCapabilities\r
+ 1, // bDataInterface\r
+\r
+ // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28\r
+ 4, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x02, // bDescriptorSubtype\r
+ 0x06, // bmCapabilities\r
+\r
+ // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33\r
+ 5, // bFunctionLength\r
+ 0x24, // bDescriptorType\r
+ 0x06, // bDescriptorSubtype\r
+ 0, // bMasterInterface\r
+ 1, // bSlaveInterface0\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ ENDPOINT_DESCRIPTOR_LENGTH, // bLength\r
+ ENDPOINT_DESCRIPTOR, // bDescriptorType\r
+ PHY_TO_DESC(EPINT_IN), // bEndpointAddress\r
+ E_INTERRUPT, // bmAttributes (0x03=intr)\r
+ LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)\r
+ 16, // bInterval\r
+\r
+\r
+\r
+\r
+ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12\r
+ 9, // bLength\r
+ 4, // bDescriptorType\r
+ 1, // bInterfaceNumber\r
+ 0, // bAlternateSetting\r
+ 2, // bNumEndpoints\r
+ 0x0A, // bInterfaceClass\r
+ 0x00, // bInterfaceSubClass\r
+ 0x00, // bInterfaceProtocol\r
+ 0, // iInterface\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_IN), // bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)\r
+ 0, // bInterval\r
+\r
+ // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13\r
+ 7, // bLength\r
+ 5, // bDescriptorType\r
+ PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress\r
+ 0x02, // bmAttributes (0x02=bulk)\r
+ LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)\r
+ MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)\r
+ 0 // bInterval\r
+ };\r
+ return configDescriptor;\r
+}\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBCDC_H\r
+#define USBCDC_H\r
+\r
+/* These headers are included for child class. */\r
+#include "USBEndpoints.h"\r
+#include "USBDescriptor.h"\r
+#include "USBDevice_Types.h"\r
+\r
+#include "USBDevice.h"\r
+\r
+\r
+\r
+class USBCDC: public USBDevice {\r
+public:\r
+\r
+ /*\r
+ * Constructor\r
+ *\r
+ * @param vendor_id Your vendor_id\r
+ * @param product_id Your product_id\r
+ * @param product_release Your preoduct_release\r
+ */\r
+ USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);\r
+\r
+protected:\r
+ \r
+ /*\r
+ * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.\r
+ *\r
+ * @returns pointer to the device descriptor\r
+ */\r
+ virtual uint8_t * deviceDesc();\r
+ \r
+ /*\r
+ * Get string product descriptor\r
+ *\r
+ * @returns pointer to the string product descriptor\r
+ */\r
+ virtual uint8_t * stringIproductDesc();\r
+ \r
+ /*\r
+ * Get string interface descriptor\r
+ *\r
+ * @returns pointer to the string interface descriptor\r
+ */\r
+ virtual uint8_t * stringIinterfaceDesc();\r
+ \r
+ /*\r
+ * Get configuration descriptor\r
+ *\r
+ * @returns pointer to the configuration descriptor\r
+ */\r
+ virtual uint8_t * configurationDesc();\r
+ \r
+ /*\r
+ * Send a buffer\r
+ *\r
+ * @param endpoint endpoint which will be sent the buffer\r
+ * @param buffer buffer to be sent\r
+ * @param size length of the buffer\r
+ * @returns true if successful\r
+ */\r
+ bool send(uint8_t * buffer, uint16_t size);\r
+ \r
+ /*\r
+ * Read a buffer from a certain endpoint. Warning: blocking\r
+ *\r
+ * @param endpoint endpoint to read\r
+ * @param buffer buffer where will be stored bytes\r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP(uint8_t * buffer, uint16_t * size);\r
+ \r
+ /*\r
+ * Read a buffer from a certain endpoint. Warning: non blocking\r
+ *\r
+ * @param endpoint endpoint to read\r
+ * @param buffer buffer where will be stored bytes\r
+ * @param size the number of bytes read will be stored in *size\r
+ * @param maxSize the maximum length that can be read\r
+ * @returns true if successful\r
+ */\r
+ bool readEP_NB(uint8_t * buffer, uint16_t * size);\r
+\r
+ virtual bool USBCallback_request();\r
+ virtual bool USBCallback_setConfiguration(uint8_t configuration);\r
+\r
+};\r
+\r
+#endif\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#include "stdint.h"\r
+#include "USBSerial.h"\r
+#include "USBBusInterface.h"\r
+\r
+\r
+int USBSerial::_putc(int c) {\r
+ send((uint8_t *)&c, 1);\r
+ return 1;\r
+}\r
+\r
+int USBSerial::_getc() {\r
+ uint8_t c;\r
+ while (buf.isEmpty());\r
+ buf.dequeue(&c);\r
+ return c;\r
+}\r
+\r
+\r
+bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) {\r
+ if(size > MAX_PACKET_SIZE_EPBULK) {\r
+ return false;\r
+ }\r
+ if(!send(buf, size)) {\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
+\r
+\r
+\r
+bool USBSerial::EP2_OUT_callback() {\r
+ uint8_t c[65];\r
+ uint16_t size = 0;\r
+\r
+ //we read the packet received and put it on the circular buffer\r
+ readEP(c, &size);\r
+ for (int i = 0; i < size; i++) {\r
+ buf.queue(c[i]);\r
+ }\r
+\r
+ //call a potential handler\r
+ rx.call();\r
+\r
+ // We reactivate the endpoint to receive next characters\r
+ readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);\r
+ return true;\r
+}\r
+\r
+uint8_t USBSerial::available() {\r
+ return buf.available();\r
+}\r
+\r
--- /dev/null
+/* Copyright (c) 2010-2011 mbed.org, MIT License\r
+*\r
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software\r
+* and associated documentation files (the "Software"), to deal in the Software without\r
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,\r
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the\r
+* Software is furnished to do so, subject to the following conditions:\r
+*\r
+* The above copyright notice and this permission notice shall be included in all copies or\r
+* substantial portions of the Software.\r
+*\r
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING\r
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+*/\r
+\r
+#ifndef USBSERIAL_H\r
+#define USBSERIAL_H\r
+\r
+#include "USBCDC.h"\r
+#include "Stream.h"\r
+#include "CircBuffer.h"\r
+\r
+\r
+/**\r
+* USBSerial example\r
+*\r
+* @code\r
+* #include "mbed.h"\r
+* #include "USBSerial.h"\r
+*\r
+* //Virtual serial port over USB\r
+* USBSerial serial;\r
+*\r
+* int main(void) {\r
+*\r
+* while(1)\r
+* {\r
+* serial.printf("I am a virtual serial port\n");\r
+* wait(1);\r
+* }\r
+* }\r
+* @endcode\r
+*/\r
+class USBSerial: public USBCDC, public Stream {\r
+public:\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param vendor_id Your vendor_id (default: 0x1f00)\r
+ * @param product_id Your product_id (default: 0x2012)\r
+ * @param product_release Your preoduct_release (default: 0x0001)\r
+ *\r
+ */\r
+ USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ };\r
+\r
+\r
+ /**\r
+ * Send a character. You can use puts, printf.\r
+ *\r
+ * @param c character to be sent\r
+ * @returns true if there is no error, false otherwise\r
+ */\r
+ virtual int _putc(int c);\r
+ \r
+ /**\r
+ * Read a character: blocking\r
+ *\r
+ * @returns character read\r
+ */\r
+ virtual int _getc();\r
+ \r
+ /**\r
+ * Check the number of bytes available.\r
+ *\r
+ * @returns the number of bytes available\r
+ */\r
+ uint8_t available(); \r
+ \r
+ /**\r
+ * Write a block of data. \r
+ *\r
+ * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.\r
+ *\r
+ * @param buf pointer on data which will be written\r
+ * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)\r
+ *\r
+ * @returns true if successfull\r
+ */\r
+ bool writeBlock(uint8_t * buf, uint16_t size);\r
+\r
+ /**\r
+ * Attach a member function to call when a packet is received. \r
+ *\r
+ * @param tptr pointer to the object to call the member function on\r
+ * @param mptr pointer to the member function to be called\r
+ */\r
+ template<typename T>\r
+ void attach(T* tptr, void (T::*mptr)(void)) {\r
+ if((mptr != NULL) && (tptr != NULL)) {\r
+ rx.attach(tptr, mptr);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Attach a callback called when a packet is received\r
+ *\r
+ * @param fptr function pointer\r
+ */\r
+ void attach(void (*fn)(void)) {\r
+ if(fn != NULL) {\r
+ rx.attach(fn);\r
+ }\r
+ }\r
+\r
+\r
+protected:\r
+ virtual bool EP2_OUT_callback();\r
+\r
+private:\r
+ FunctionPointer rx;\r
+ CircBuffer<uint8_t> buf;\r
+};\r
+\r
+#endif\r
--- /dev/null
+/*
+ This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
+ Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "libs/Module.h"
+#include "libs/Kernel.h"
+#include "libs/nuts_bolts.h"
+#include <math.h>
+#include "mbed.h"
+#include <string>
+#include "Block.h"
+#include "Planner.h"
+#include "Player.h"
+using std::string;
+#include <vector>
+#include "../communication/utils/Gcode.h"
+
+Block::Block(){
+ clear_vector(this->steps);
+ this->times_taken = 0; // A block can be "taken" by any number of modules, and the next block is not moved to until all the modules have "released" it. This value serves as a tracker.
+}
+
+void Block::debug(Kernel* kernel){
+ kernel->serial->printf(" steps:%4d|%4d|%4d(max:%4d) nominal:r%10d/s%6.1f mm:%9.6f rdelta:%8d acc:%5d dec:%5d rates:%10d>%10d taken:%d \r\n", this->steps[0], this->steps[1], this->steps[2], this->steps_event_count, this->nominal_rate, this->nominal_speed, this->millimeters, this->rate_delta, this->accelerate_until, this->decelerate_after, this->initial_rate, this->final_rate, this->times_taken );
+}
+
+
+// Calculate a braking factor to reach baseline speed which is max_jerk/2, e.g. the
+// speed under which you cannot exceed max_jerk no matter what you do.
+double Block::compute_factor_for_safe_speed(){
+ return( this->planner->max_jerk / this->nominal_speed );
+}
+
+
+// Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
+// The factors represent a factor of braking and must be in the range 0.0-1.0.
+// +--------+ <- nominal_rate
+// / \
+// nominal_rate*entry_factor -> + \
+// | + <- nominal_rate*exit_factor
+// +-------------+
+// time -->
+void Block::calculate_trapezoid( double entryfactor, double exitfactor ){
+
+ this->initial_rate = ceil(this->nominal_rate * entryfactor); // (step/min)
+ this->final_rate = ceil(this->nominal_rate * exitfactor); // (step/min)
+ double acceleration_per_minute = this->rate_delta * this->planner->kernel->stepper->acceleration_ticks_per_second * 60.0;
+ int accelerate_steps = ceil( this->estimate_acceleration_distance( this->initial_rate, this->nominal_rate, acceleration_per_minute ) );
+ int decelerate_steps = ceil( this->estimate_acceleration_distance( this->nominal_rate, this->final_rate, -acceleration_per_minute ) );
+
+ // Calculate the size of Plateau of Nominal Rate.
+ int plateau_steps = this->steps_event_count-accelerate_steps-decelerate_steps;
+
+ // Is the Plateau of Nominal Rate smaller than nothing? That means no cruising, and we will
+ // have to use intersection_distance() to calculate when to abort acceleration and start braking
+ // in order to reach the final_rate exactly at the end of this block.
+ if (plateau_steps < 0) {
+ accelerate_steps = ceil(this->intersection_distance(this->initial_rate, this->final_rate, acceleration_per_minute, this->steps_event_count));
+ accelerate_steps = max( accelerate_steps, 0 ); // Check limits due to numerical round-off
+ accelerate_steps = min( accelerate_steps, int(this->steps_event_count) );
+ plateau_steps = 0;
+ }
+
+ this->accelerate_until = accelerate_steps;
+ this->decelerate_after = accelerate_steps+plateau_steps;
+
+}
+
+// Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
+// given acceleration:
+double Block::estimate_acceleration_distance(double initialrate, double targetrate, double acceleration) {
+ return( (targetrate*targetrate-initialrate*initialrate)/(2L*acceleration));
+}
+
+// This function gives you the point at which you must start braking (at the rate of -acceleration) if
+// you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
+// a total travel of distance. This can be used to compute the intersection point between acceleration and
+// deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
+//
+/* + <- some maximum rate we don't care about
+ /|\
+ / | \
+ / | + <- final_rate
+ / | |
+ initial_rate -> +----+--+
+ ^ ^
+ | |
+ intersection_distance distance */
+double Block::intersection_distance(double initialrate, double finalrate, double acceleration, double distance) {
+ return((2*acceleration*distance-initialrate*initialrate+finalrate*finalrate)/(4*acceleration));
+}
+
+// Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
+// acceleration within the allotted distance.
+inline double max_allowable_speed(double acceleration, double target_velocity, double distance) {
+ return(
+ sqrt(target_velocity*target_velocity-2L*acceleration*60*60*distance) //Was acceleration*60*60*distance, in case this breaks, but here we prefer to use seconds instead of minutes
+ );
+}
+
+
+// Called by Planner::recalculate() when scanning the plan from last to first entry.
+void Block::reverse_pass(Block* next, Block* previous){
+
+ if (next) {
+ // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
+ // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
+ // check for maximum allowable speed reductions to ensure maximum possible planned speed.
+ if (this->entry_speed != this->max_entry_speed) {
+
+ // If nominal length true, max junction speed is guaranteed to be reached. Only compute
+ // for max allowable speed if block is decelerating and nominal length is false.
+ if ((!this->nominal_length_flag) && (this->max_entry_speed > next->entry_speed)) {
+ this->entry_speed = min( this->max_entry_speed, max_allowable_speed(-this->planner->acceleration,next->entry_speed,this->millimeters));
+ } else {
+ this->entry_speed = this->max_entry_speed;
+ }
+ this->recalculate_flag = true;
+
+ }
+ } // Skip last block. Already initialized and set for recalculation.
+
+}
+
+
+// Called by Planner::recalculate() when scanning the plan from first to last entry.
+void Block::forward_pass(Block* previous, Block* next){
+
+ if(!previous) { return; } // Begin planning after buffer_tail
+
+ // If the previous block is an acceleration block, but it is not long enough to complete the
+ // full speed change within the block, we need to adjust the entry speed accordingly. Entry
+ // speeds have already been reset, maximized, and reverse planned by reverse planner.
+ // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
+ if (!previous->nominal_length_flag) {
+ if (previous->entry_speed < this->entry_speed) {
+ double entry_speed = min( this->entry_speed,
+ max_allowable_speed(-this->planner->acceleration,previous->entry_speed,previous->millimeters) );
+
+ // Check for junction speed change
+ if (this->entry_speed != entry_speed) {
+ this->entry_speed = entry_speed;
+ this->recalculate_flag = true;
+ }
+ }
+ }
+
+}
+
+
+// Gcodes are attached to their respective blocks so that on_gcode_execute can be called with it
+void Block::append_gcode(Gcode* gcode){
+ this->commands.push_back(gcode->command);
+ this->travel_distances.push_back(gcode->millimeters_of_travel);
+}
+
+// The attached gcodes are then poped and the on_gcode_execute event is called with them as a parameter
+void Block::pop_and_execute_gcode(Kernel* &kernel){
+ Block* block = const_cast<Block*>(this);
+ for(unsigned short index=0; index<block->commands.size(); index++){
+ Gcode gcode = Gcode();
+ gcode.command = block->commands.at(index);
+ gcode.millimeters_of_travel = block->travel_distances.at(index);
+ kernel->call_event(ON_GCODE_EXECUTE, &gcode );
+ }
+}
+
+// Signal the player that this block is ready to be injected into the system
+void Block::ready(){
+ this->player->new_block_added();
+}
+
+// Mark the block as taken by one more module
+void Block::take(){
+ this->times_taken++;
+}
+
+// Mark the block as no longer taken by one module, go to next block if this free's it
+void Block::release(){
+ this->times_taken--;
+ if( this->times_taken < 1 ){
+ this->player->kernel->call_event(ON_BLOCK_END, this);
+ this->pop_and_execute_gcode(this->player->kernel);
+ Player* player = this->player;
+
+ //this->player->kernel->serial->printf("a %d\r\n", this->player->queue.size() );
+ if( player->queue.size() > 0 ){
+ player->queue.delete_first();
+ }
+
+ //this->player->kernel->serial->printf("b %d %d\r\n", this->player->queue.size(), player->looking_for_new_block );
+
+ if( player->looking_for_new_block == false ){
+ //player->pop_and_process_new_block(123);
+ if( player->queue.size() > 0 ){
+
+ //this->player->kernel->serial->printf("c %d %d\r\n", this->player->queue.size(), player->looking_for_new_block );
+ player->current_block = player->queue.get_ref(0);
+ player->kernel->call_event(ON_BLOCK_BEGIN, player->current_block);
+ if( player->current_block->times_taken < 1 ){
+ player->current_block->release();
+ }
+ }else{
+ player->current_block->debug(player->kernel);
+ //this->player->kernel->serial->printf("d %d %d\r\n", this->player->queue.size(), player->looking_for_new_block );
+ wait(0.1);
+ player->current_block = NULL;
+ }
+ }
+ }
+}
+
+
+
this->accelerate_until = accelerate_steps;
this->decelerate_after = accelerate_steps+plateau_steps;
+ // DIRTY HACK so that we don't end too early for blocks with 0 as final_rate. Doing the math right would be better. Probably fixed in latest grbl
+ if( this->final_rate < 0.01 ){
+ this->decelerate_after += floor( this->nominal_rate / 60 / this->planner->kernel->stepper->acceleration_ticks_per_second ) * 3;
+ }
+
}
// Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
this->on_config_reload(this);
// Acceleration ticker
- this->kernel->slow_ticker->set_frequency(this->acceleration_ticks_per_second/10);
- this->kernel->slow_ticker->attach( this, &Stepper::trapezoid_generator_tick );
+ //this->kernel->slow_ticker->set_frequency(this->acceleration_ticks_per_second/10);
+ this->kernel->slow_ticker->attach( this->acceleration_ticks_per_second, this, &Stepper::trapezoid_generator_tick );
// Initiate main_interrupt timer and step reset timer
this->kernel->step_ticker->attach( this, &Stepper::main_interrupt );
// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
void Stepper::on_pause(void* argument){
- //TODO: Implement pause here
+ //TODO: reImplement pause here
}
// When the play/pause button is set to play, or a module calls the ON_PLAY event
void Stepper::on_play(void* argument){
- //TODO: Implement pause here
+ //TODO: reImplement pause here
}
// A new block is popped from the queue
// Mark the new block as of interrest to us
block->take();
-
+
+ if( block->final_rate < 0.1 ){
+ //block->debug(this->kernel);
+ }
+
// Setup
for( int stpr=ALPHA_STEPPER; stpr<=GAMMA_STEPPER; stpr++){ this->counters[stpr] = 0; this->stepped[stpr] = 0; }
this->step_events_completed = 0;
if (this->trapezoid_adjusted_rate > double(this->current_block->rate_delta) * 1.5) {
this->trapezoid_adjusted_rate -= this->current_block->rate_delta;
}else{
- this->trapezoid_adjusted_rate = floor(double(this->trapezoid_adjusted_rate / 2 ));
+ this->trapezoid_adjusted_rate = double(this->current_block->rate_delta) * 1.5;
+ //this->trapezoid_adjusted_rate = floor(double(this->trapezoid_adjusted_rate / 2 ));
+ //this->kernel->serial->printf("over!\r\n");
}
if (this->trapezoid_adjusted_rate < this->current_block->final_rate ) {
this->trapezoid_adjusted_rate = this->current_block->final_rate;
this->absolute_mode = true;
this->direction = 1;
this->acceleration_lock = false;
+ this->step_counter = 0;
+ this->counter_increment = 0;
}
void Extruder::on_module_loaded() {
// Update speed every *acceleration_ticks_per_second*
// TODO: Make this an independent setting
- this->kernel->slow_ticker->attach( this, &Extruder::acceleration_tick );
+ this->kernel->slow_ticker->attach( this->kernel->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick );
// Initiate main_interrupt timer and step reset timer
this->kernel->step_ticker->attach( this, &Extruder::stepping_tick );
// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
void Extruder::on_block_begin(void* argument){
Block* block = static_cast<Block*>(argument);
-
if( this->mode == SOLO ){
// In solo mode we take the block so we can move even if the stepper has nothing to do
block->take();
// Advance
// TODO: Proper advance configuration
- //double advance = double(next_stepper_rate) * 0.00001 * 0.15;
- double advance = 0;
+ double advance = double(next_stepper_rate) * ( 0.00001 * 0.15 ) * 0.4 ;
+ //double advance = 0;
next_relative_position += ( advance );
// TODO : all of those "if->return" is very hacky, we should do the math in a way where most of those don't happen, but that requires doing tons of drawing ...
// Convenience function to set stepping speed
void Extruder::set_speed( int steps_per_second ){
+
+ if( steps_per_second < 10 ){
+ steps_per_second = 10;
+ }
// TODO : Proper limit config value
if( steps_per_second > (this->feed_rate*double(this->steps_per_millimeter))/60 ){
}
this->counter_increment = int(floor(double(1<<16)/double(this->kernel->stepper->base_stepping_frequency / steps_per_second)));
-
+
}
inline void Extruder::stepping_tick(){
// Setup pins and timer
this->thermistor_pin = new AnalogIn(p20);
- this->kernel->slow_ticker->attach( this, &TemperatureControl::thermistor_read_tick );
+ this->kernel->slow_ticker->attach( 20, this, &TemperatureControl::thermistor_read_tick );
this->heater_pwm = new PwmOut(p22);
this->heater_pwm->write(0);
this->pwm_value = 0;