From 0bddc26e19e94f6e6e6e59f41b1a86e3fde50fb3 Mon Sep 17 00:00:00 2001 From: NicoHood Date: Tue, 1 Jul 2014 22:13:53 +0200 Subject: [PATCH] Update 1.3 --- 1.0.x/HID.cpp | 239 +++---- 1.0.x/HID.h | 3 + 1.5.x/HID.cpp | 239 +++---- 1.5.x/HID.h | 3 + Firmwares/Hoodloader.hex | 847 +++++++++++++------------ Hoodloader/Descriptors.c | 2 +- Hoodloader/Hoodloader.c | 191 +++--- Hoodloader/Lib/LightweightRingBuff.h | 4 +- Hoodloader/Lib/NicoHoodProtocol_c.c | 257 ++++---- Hoodloader/Lib/NicoHoodProtocol_c.h | 13 +- Hoodloader/Lib/t/NicoHoodProtocol_c.c | 257 ++++++++ Hoodloader/Lib/t/NicoHoodProtocol_c.h | 104 +++ Hoodloader/lufa-LUFA-140302/.gitignore | 2 +- Hoodloader/makefile | 5 +- Readme.md | 34 +- Tutorials/Readme.md | 2 +- 16 files changed, 1308 insertions(+), 894 deletions(-) create mode 100644 Hoodloader/Lib/t/NicoHoodProtocol_c.c create mode 100644 Hoodloader/Lib/t/NicoHoodProtocol_c.h diff --git a/1.0.x/HID.cpp b/1.0.x/HID.cpp index 4979dbf..e9a843f 100644 --- a/1.0.x/HID.cpp +++ b/1.0.x/HID.cpp @@ -369,9 +369,9 @@ const u8 _hidReportDescriptor[] = { extern const HIDDescriptor _hidInterface PROGMEM; const HIDDescriptor _hidInterface = { - D_INTERFACE(HID_INTERFACE,1,3,0,0), + D_INTERFACE(HID_INTERFACE, 1, 3, 0, 0), D_HIDREPORT(sizeof(_hidReportDescriptor)), - D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01) + D_ENDPOINT(USB_ENDPOINT_IN(HID_ENDPOINT_INT), USB_ENDPOINT_TYPE_INTERRUPT, 0x40, 0x01) }; //================================================================================ @@ -386,18 +386,18 @@ u8 _hid_idle = 1; int WEAK HID_GetInterface(u8* interfaceNum) { interfaceNum[0] += 1; // uses 1 - return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface)); + return USB_SendControl(TRANSFER_PGM, &_hidInterface, sizeof(_hidInterface)); } int WEAK HID_GetDescriptor(int /* i */) { - return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor)); + return USB_SendControl(TRANSFER_PGM, _hidReportDescriptor, sizeof(_hidReportDescriptor)); } void WEAK HID_SendReport(u8 id, const void* data, int len) { USB_Send(HID_TX, &id, 1); - USB_Send(HID_TX | TRANSFER_RELEASE,data,len); + USB_Send(HID_TX | TRANSFER_RELEASE, data, len); } bool WEAK HID_Setup(Setup& setup) @@ -450,25 +450,25 @@ HID_::HID_(void){ } void HID_::begin(void){ - Serial.begin(115200); + HID_SERIAL.begin(115200); } void HID_::end(void){ - Serial.end(); + HID_SERIAL.end(); } void HID_::sendReport(uint8_t ReportID, const void* HIDReport, uint8_t length){ // write the Report via Protocol and checksum. 16bit for each sending // send control address - NHPwriteChecksum(NHP_ADDRESS_CONTROL, (NHP_USAGE_ARDUINOHID<<8)|ReportID); + NHPwriteChecksum(NHP_ADDRESS_CONTROL, (NHP_USAGE_ARDUINOHID << 8) | ReportID); const uint8_t* report = (const uint8_t*)HIDReport; - for(int i = 0; i2){ - uint8_t nextvalue=(data>>(7*(blocks-3))); - if(nextvalue>NHP_MASK_DATA_3BIT){ + while (blocks > 2){ + uint8_t nextvalue = (data >> (7 * (blocks - 3))); + + if (nextvalue > NHP_MASK_DATA_3BIT){ // special case for the MSB - if(blocks==7) { + if (blocks == 7) { writebuffer[0] = nextvalue; blocks--; } + // this block is too big, write this into the next data block break; } else{ - // write the possible first 3 bits and check again after + // write the possible first 3 bits and check again after if zero writebuffer[0] = nextvalue; blocks--; + // we have our first bits, stop (nonzero) + if (nextvalue) + break; } } // write the rest of the data bits - uint8_t datablocks=blocks-2; - while(datablocks>0){ + uint8_t datablocks = blocks - 2; + while (datablocks > 0){ writebuffer[datablocks] = data & NHP_MASK_DATA_7BIT; - data>>=7; + data >>= 7; datablocks--; } // write lead + length mask - writebuffer[0] |= NHP_MASK_LEAD | (blocks <<3); + writebuffer[0] |= NHP_MASK_LEAD | (blocks << 3); // write end mask - writebuffer[blocks-1] = NHP_MASK_END | ((address-1) & NHP_MASK_ADDRESS); + writebuffer[blocks - 1] = NHP_MASK_END | ((address - 1) & NHP_MASK_ADDRESS); // write the buffer - Serial.write(writebuffer, blocks); + HID_SERIAL.write(writebuffer, blocks); } //================================================================================ @@ -542,22 +547,22 @@ void Mouse_::end(void){ void Mouse_::click(uint8_t b){ _report.buttons = b; - move(0,0,0); + move(0, 0, 0); _report.buttons = 0; - move(0,0,0); + move(0, 0, 0); } void Mouse_::move(signed char x, signed char y, signed char wheel){ - _report.xAxis=x; - _report.yAxis=y; - _report.wheel=wheel; + _report.xAxis = x; + _report.yAxis = y; + _report.wheel = wheel; HID.sendReport(HID_REPORTID_MouseReport, &_report, sizeof(HID_MouseReport_Data_t)); } void Mouse_::buttons(uint8_t b){ if (b != _report.buttons) { _report.buttons = b; - move(0,0,0); + move(0, 0, 0); } } @@ -574,7 +579,7 @@ void Mouse_::releaseAll(void){ } bool Mouse_::isPressed(uint8_t b){ - if ((b & _report.buttons) > 0) + if ((b & _report.buttons) > 0) return true; return false; } @@ -599,7 +604,7 @@ void Keyboard_::end(void){ } extern - const uint8_t _asciimap[128] PROGMEM; +const uint8_t _asciimap[128] PROGMEM; #define SHIFT 0x80 const uint8_t _asciimap[128] = @@ -638,17 +643,17 @@ const uint8_t _asciimap[128] = 0x00, // US 0x2c, // ' ' - 0x1e|SHIFT, // ! - 0x34|SHIFT, // " - 0x20|SHIFT, // # - 0x21|SHIFT, // $ - 0x22|SHIFT, // % - 0x24|SHIFT, // & + 0x1e | SHIFT, // ! + 0x34 | SHIFT, // " + 0x20 | SHIFT, // # + 0x21 | SHIFT, // $ + 0x22 | SHIFT, // % + 0x24 | SHIFT, // & 0x34, // ' - 0x26|SHIFT, // ( - 0x27|SHIFT, // ) - 0x25|SHIFT, // * - 0x2e|SHIFT, // + + 0x26 | SHIFT, // ( + 0x27 | SHIFT, // ) + 0x25 | SHIFT, // * + 0x2e | SHIFT, // + 0x36, // , 0x2d, // - 0x37, // . @@ -663,44 +668,44 @@ const uint8_t _asciimap[128] = 0x24, // 7 0x25, // 8 0x26, // 9 - 0x33|SHIFT, // : + 0x33 | SHIFT, // : 0x33, // ; - 0x36|SHIFT, // < + 0x36 | SHIFT, // < 0x2e, // = - 0x37|SHIFT, // > - 0x38|SHIFT, // ? - 0x1f|SHIFT, // @ - 0x04|SHIFT, // A - 0x05|SHIFT, // B - 0x06|SHIFT, // C - 0x07|SHIFT, // D - 0x08|SHIFT, // E - 0x09|SHIFT, // F - 0x0a|SHIFT, // G - 0x0b|SHIFT, // H - 0x0c|SHIFT, // I - 0x0d|SHIFT, // J - 0x0e|SHIFT, // K - 0x0f|SHIFT, // L - 0x10|SHIFT, // M - 0x11|SHIFT, // N - 0x12|SHIFT, // O - 0x13|SHIFT, // P - 0x14|SHIFT, // Q - 0x15|SHIFT, // R - 0x16|SHIFT, // S - 0x17|SHIFT, // T - 0x18|SHIFT, // U - 0x19|SHIFT, // V - 0x1a|SHIFT, // W - 0x1b|SHIFT, // X - 0x1c|SHIFT, // Y - 0x1d|SHIFT, // Z + 0x37 | SHIFT, // > + 0x38 | SHIFT, // ? + 0x1f | SHIFT, // @ + 0x04 | SHIFT, // A + 0x05 | SHIFT, // B + 0x06 | SHIFT, // C + 0x07 | SHIFT, // D + 0x08 | SHIFT, // E + 0x09 | SHIFT, // F + 0x0a | SHIFT, // G + 0x0b | SHIFT, // H + 0x0c | SHIFT, // I + 0x0d | SHIFT, // J + 0x0e | SHIFT, // K + 0x0f | SHIFT, // L + 0x10 | SHIFT, // M + 0x11 | SHIFT, // N + 0x12 | SHIFT, // O + 0x13 | SHIFT, // P + 0x14 | SHIFT, // Q + 0x15 | SHIFT, // R + 0x16 | SHIFT, // S + 0x17 | SHIFT, // T + 0x18 | SHIFT, // U + 0x19 | SHIFT, // V + 0x1a | SHIFT, // W + 0x1b | SHIFT, // X + 0x1c | SHIFT, // Y + 0x1d | SHIFT, // Z 0x2f, // [ 0x31, // bslash 0x30, // ] - 0x23|SHIFT, // ^ - 0x2d|SHIFT, // _ + 0x23 | SHIFT, // ^ + 0x2d | SHIFT, // _ 0x35, // ` 0x04, // a 0x05, // b @@ -728,10 +733,10 @@ const uint8_t _asciimap[128] = 0x1b, // x 0x1c, // y 0x1d, // z - 0x2f|SHIFT, // - 0x31|SHIFT, // | - 0x30|SHIFT, // } - 0x35|SHIFT, // ~ + 0x2f | SHIFT, // + 0x31 | SHIFT, // | + 0x30 | SHIFT, // } + 0x35 | SHIFT, // ~ 0 // DEL }; @@ -739,7 +744,7 @@ const uint8_t _asciimap[128] = //uint8_t USBPutChar(uint8_t c); size_t Keyboard_::write(uint8_t c) -{ +{ uint8_t p = press(c); // Keydown //uint8_t r = release(c); // Keyup @@ -750,15 +755,17 @@ size_t Keyboard_::write(uint8_t c) // to the persistent key report and sends the report. Because of the way // USB HID works, the host acts like the key remains pressed until we // call release(), releaseAll(), or otherwise clear the report and resend. -size_t Keyboard_::press(uint8_t k) +size_t Keyboard_::press(uint8_t k) { uint8_t i; if (k >= 136) { // it's a non-printing key (not a modifier) k = k - 136; - } else if (k >= 128) { // it's a modifier key - _report.modifiers |= (1<<(k-128)); + } + else if (k >= 128) { // it's a modifier key + _report.modifiers |= (1 << (k - 128)); k = 0; - } else { // it's a printing key + } + else { // it's a printing key k = pgm_read_byte(_asciimap + k); if (!k) { setWriteError(); @@ -772,20 +779,20 @@ size_t Keyboard_::press(uint8_t k) // Add k to the key report only if it's not already present // and if there is an empty slot. - if (_report.keys[0] != k && _report.keys[1] != k && + if (_report.keys[0] != k && _report.keys[1] != k && _report.keys[2] != k && _report.keys[3] != k && _report.keys[4] != k && _report.keys[5] != k) { - for (i=0; i<6; i++) { - if (_report.keys[i] == 0x00) { - _report.keys[i] = k; - break; - } + for (i = 0; i < 6; i++) { + if (_report.keys[i] == 0x00) { + _report.keys[i] = k; + break; } - if (i == 6) { - setWriteError(); - return 0; - } + } + if (i == 6) { + setWriteError(); + return 0; + } } HID.sendReport(HID_REPORTID_KeyboardReport, &_report, sizeof(_report)); return 1; @@ -794,15 +801,17 @@ size_t Keyboard_::press(uint8_t k) // release() takes the specified key out of the persistent key report and // sends the report. This tells the OS the key is no longer pressed and that // it shouldn't be repeated any more. -size_t Keyboard_::release(uint8_t k) +size_t Keyboard_::release(uint8_t k) { uint8_t i; if (k >= 136) { // it's a non-printing key (not a modifier) k = k - 136; - } else if (k >= 128) { // it's a modifier key - _report.modifiers &= ~(1<<(k-128)); + } + else if (k >= 128) { // it's a modifier key + _report.modifiers &= ~(1 << (k - 128)); k = 0; - } else { // it's a printing key + } + else { // it's a printing key k = pgm_read_byte(_asciimap + k); if (!k) { return 0; @@ -815,7 +824,7 @@ size_t Keyboard_::release(uint8_t k) // Test the key report to see if k is present. Clear it if it exists. // Check all positions in case the key is present more than once (which it shouldn't be) - for (i=0; i<6; i++) { + for (i = 0; i < 6; i++) { if (0 != k && _report.keys[i] == k) { _report.keys[i] = 0x00; } @@ -855,8 +864,8 @@ void Media_::write(uint16_t m){ void Media_::press(uint16_t m){ // search for a free spot - for (int i=0; i2){ - uint8_t nextvalue=(data>>(7*(blocks-3))); - if(nextvalue>NHP_MASK_DATA_3BIT){ + while (blocks > 2){ + uint8_t nextvalue = (data >> (7 * (blocks - 3))); + + if (nextvalue > NHP_MASK_DATA_3BIT){ // special case for the MSB - if(blocks==7) { + if (blocks == 7) { writebuffer[0] = nextvalue; blocks--; } + // this block is too big, write this into the next data block break; } else{ - // write the possible first 3 bits and check again after + // write the possible first 3 bits and check again after if zero writebuffer[0] = nextvalue; blocks--; + // we have our first bits, stop (nonzero) + if (nextvalue) + break; } } // write the rest of the data bits - uint8_t datablocks=blocks-2; - while(datablocks>0){ + uint8_t datablocks = blocks - 2; + while (datablocks > 0){ writebuffer[datablocks] = data & NHP_MASK_DATA_7BIT; - data>>=7; + data >>= 7; datablocks--; } // write lead + length mask - writebuffer[0] |= NHP_MASK_LEAD | (blocks <<3); + writebuffer[0] |= NHP_MASK_LEAD | (blocks << 3); // write end mask - writebuffer[blocks-1] = NHP_MASK_END | ((address-1) & NHP_MASK_ADDRESS); + writebuffer[blocks - 1] = NHP_MASK_END | ((address - 1) & NHP_MASK_ADDRESS); // write the buffer - Serial.write(writebuffer, blocks); + HID_SERIAL.write(writebuffer, blocks); } //================================================================================ @@ -542,22 +547,22 @@ void Mouse_::end(void){ void Mouse_::click(uint8_t b){ _report.buttons = b; - move(0,0,0); + move(0, 0, 0); _report.buttons = 0; - move(0,0,0); + move(0, 0, 0); } void Mouse_::move(signed char x, signed char y, signed char wheel){ - _report.xAxis=x; - _report.yAxis=y; - _report.wheel=wheel; + _report.xAxis = x; + _report.yAxis = y; + _report.wheel = wheel; HID.sendReport(HID_REPORTID_MouseReport, &_report, sizeof(HID_MouseReport_Data_t)); } void Mouse_::buttons(uint8_t b){ if (b != _report.buttons) { _report.buttons = b; - move(0,0,0); + move(0, 0, 0); } } @@ -574,7 +579,7 @@ void Mouse_::releaseAll(void){ } bool Mouse_::isPressed(uint8_t b){ - if ((b & _report.buttons) > 0) + if ((b & _report.buttons) > 0) return true; return false; } @@ -599,7 +604,7 @@ void Keyboard_::end(void){ } extern - const uint8_t _asciimap[128] PROGMEM; +const uint8_t _asciimap[128] PROGMEM; #define SHIFT 0x80 const uint8_t _asciimap[128] = @@ -638,17 +643,17 @@ const uint8_t _asciimap[128] = 0x00, // US 0x2c, // ' ' - 0x1e|SHIFT, // ! - 0x34|SHIFT, // " - 0x20|SHIFT, // # - 0x21|SHIFT, // $ - 0x22|SHIFT, // % - 0x24|SHIFT, // & + 0x1e | SHIFT, // ! + 0x34 | SHIFT, // " + 0x20 | SHIFT, // # + 0x21 | SHIFT, // $ + 0x22 | SHIFT, // % + 0x24 | SHIFT, // & 0x34, // ' - 0x26|SHIFT, // ( - 0x27|SHIFT, // ) - 0x25|SHIFT, // * - 0x2e|SHIFT, // + + 0x26 | SHIFT, // ( + 0x27 | SHIFT, // ) + 0x25 | SHIFT, // * + 0x2e | SHIFT, // + 0x36, // , 0x2d, // - 0x37, // . @@ -663,44 +668,44 @@ const uint8_t _asciimap[128] = 0x24, // 7 0x25, // 8 0x26, // 9 - 0x33|SHIFT, // : + 0x33 | SHIFT, // : 0x33, // ; - 0x36|SHIFT, // < + 0x36 | SHIFT, // < 0x2e, // = - 0x37|SHIFT, // > - 0x38|SHIFT, // ? - 0x1f|SHIFT, // @ - 0x04|SHIFT, // A - 0x05|SHIFT, // B - 0x06|SHIFT, // C - 0x07|SHIFT, // D - 0x08|SHIFT, // E - 0x09|SHIFT, // F - 0x0a|SHIFT, // G - 0x0b|SHIFT, // H - 0x0c|SHIFT, // I - 0x0d|SHIFT, // J - 0x0e|SHIFT, // K - 0x0f|SHIFT, // L - 0x10|SHIFT, // M - 0x11|SHIFT, // N - 0x12|SHIFT, // O - 0x13|SHIFT, // P - 0x14|SHIFT, // Q - 0x15|SHIFT, // R - 0x16|SHIFT, // S - 0x17|SHIFT, // T - 0x18|SHIFT, // U - 0x19|SHIFT, // V - 0x1a|SHIFT, // W - 0x1b|SHIFT, // X - 0x1c|SHIFT, // Y - 0x1d|SHIFT, // Z + 0x37 | SHIFT, // > + 0x38 | SHIFT, // ? + 0x1f | SHIFT, // @ + 0x04 | SHIFT, // A + 0x05 | SHIFT, // B + 0x06 | SHIFT, // C + 0x07 | SHIFT, // D + 0x08 | SHIFT, // E + 0x09 | SHIFT, // F + 0x0a | SHIFT, // G + 0x0b | SHIFT, // H + 0x0c | SHIFT, // I + 0x0d | SHIFT, // J + 0x0e | SHIFT, // K + 0x0f | SHIFT, // L + 0x10 | SHIFT, // M + 0x11 | SHIFT, // N + 0x12 | SHIFT, // O + 0x13 | SHIFT, // P + 0x14 | SHIFT, // Q + 0x15 | SHIFT, // R + 0x16 | SHIFT, // S + 0x17 | SHIFT, // T + 0x18 | SHIFT, // U + 0x19 | SHIFT, // V + 0x1a | SHIFT, // W + 0x1b | SHIFT, // X + 0x1c | SHIFT, // Y + 0x1d | SHIFT, // Z 0x2f, // [ 0x31, // bslash 0x30, // ] - 0x23|SHIFT, // ^ - 0x2d|SHIFT, // _ + 0x23 | SHIFT, // ^ + 0x2d | SHIFT, // _ 0x35, // ` 0x04, // a 0x05, // b @@ -728,10 +733,10 @@ const uint8_t _asciimap[128] = 0x1b, // x 0x1c, // y 0x1d, // z - 0x2f|SHIFT, // - 0x31|SHIFT, // | - 0x30|SHIFT, // } - 0x35|SHIFT, // ~ + 0x2f | SHIFT, // + 0x31 | SHIFT, // | + 0x30 | SHIFT, // } + 0x35 | SHIFT, // ~ 0 // DEL }; @@ -739,7 +744,7 @@ const uint8_t _asciimap[128] = //uint8_t USBPutChar(uint8_t c); size_t Keyboard_::write(uint8_t c) -{ +{ uint8_t p = press(c); // Keydown //uint8_t r = release(c); // Keyup @@ -750,15 +755,17 @@ size_t Keyboard_::write(uint8_t c) // to the persistent key report and sends the report. Because of the way // USB HID works, the host acts like the key remains pressed until we // call release(), releaseAll(), or otherwise clear the report and resend. -size_t Keyboard_::press(uint8_t k) +size_t Keyboard_::press(uint8_t k) { uint8_t i; if (k >= 136) { // it's a non-printing key (not a modifier) k = k - 136; - } else if (k >= 128) { // it's a modifier key - _report.modifiers |= (1<<(k-128)); + } + else if (k >= 128) { // it's a modifier key + _report.modifiers |= (1 << (k - 128)); k = 0; - } else { // it's a printing key + } + else { // it's a printing key k = pgm_read_byte(_asciimap + k); if (!k) { setWriteError(); @@ -772,20 +779,20 @@ size_t Keyboard_::press(uint8_t k) // Add k to the key report only if it's not already present // and if there is an empty slot. - if (_report.keys[0] != k && _report.keys[1] != k && + if (_report.keys[0] != k && _report.keys[1] != k && _report.keys[2] != k && _report.keys[3] != k && _report.keys[4] != k && _report.keys[5] != k) { - for (i=0; i<6; i++) { - if (_report.keys[i] == 0x00) { - _report.keys[i] = k; - break; - } + for (i = 0; i < 6; i++) { + if (_report.keys[i] == 0x00) { + _report.keys[i] = k; + break; } - if (i == 6) { - setWriteError(); - return 0; - } + } + if (i == 6) { + setWriteError(); + return 0; + } } HID.sendReport(HID_REPORTID_KeyboardReport, &_report, sizeof(_report)); return 1; @@ -794,15 +801,17 @@ size_t Keyboard_::press(uint8_t k) // release() takes the specified key out of the persistent key report and // sends the report. This tells the OS the key is no longer pressed and that // it shouldn't be repeated any more. -size_t Keyboard_::release(uint8_t k) +size_t Keyboard_::release(uint8_t k) { uint8_t i; if (k >= 136) { // it's a non-printing key (not a modifier) k = k - 136; - } else if (k >= 128) { // it's a modifier key - _report.modifiers &= ~(1<<(k-128)); + } + else if (k >= 128) { // it's a modifier key + _report.modifiers &= ~(1 << (k - 128)); k = 0; - } else { // it's a printing key + } + else { // it's a printing key k = pgm_read_byte(_asciimap + k); if (!k) { return 0; @@ -815,7 +824,7 @@ size_t Keyboard_::release(uint8_t k) // Test the key report to see if k is present. Clear it if it exists. // Check all positions in case the key is present more than once (which it shouldn't be) - for (i=0; i<6; i++) { + for (i = 0; i < 6; i++) { if (0 != k && _report.keys[i] == k) { _report.keys[i] = 0x00; } @@ -855,8 +864,8 @@ void Media_::write(uint16_t m){ void Media_::press(uint16_t m){ // search for a free spot - for (int i=0; iState.LineEncoding.ParityType) { case CDC_PARITY_Odd: - ConfigMask = ((1 << UPM11) | (1 << UPM10)); + ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; case CDC_PARITY_Even: - ConfigMask = (1 << UPM11); + ConfigMask = (1 << UPM11); break; } @@ -465,10 +469,10 @@ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCI UCSR1A = 0; UCSR1C = 0; - /* Special case 57600 baud for compatibility with the ATmega328 bootloader. */ - UBRR1 = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) + /* Special case 57600 baud for compatibility with the ATmega328 bootloader. */ + UBRR1 = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? SERIAL_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS) - : SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); + : SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); UCSR1C = ConfigMask; UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1); @@ -528,17 +532,20 @@ void EVENT_USB_Device_StartOfFrame(void) * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, - uint8_t* const ReportID, - const uint8_t ReportType, - void* ReportData, - uint16_t* const ReportSize) + uint8_t* const ReportID, + const uint8_t ReportType, + void* ReportData, + uint16_t* const ReportSize) { //write report and reset ID - memcpy(ReportData, CurrentHIDReportBuffer, HIDReportState.length); - *ReportID = HIDReportState.ID; + memcpy(ReportData, HIDReportBuffer, HIDReportState.length); + *ReportID = HIDReportState.ID; *ReportSize = HIDReportState.length; - HIDReportState.ID=0; - return HIDReportState.forcewrite; + HIDReportState.ID = 0; + HIDReportState.recvlength = 0; //just to be sure if you call HID_Task by accident + // always return true, because we cannot compare with >1 report due to ram limit + // this will forcewrite the report everytime + return true; } /** HID class driver callback function for the processing of HID reports from the host. @@ -550,10 +557,10 @@ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDIn * \param[in] ReportSize Size in bytes of the received HID report */ void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, - const uint8_t ReportID, - const uint8_t ReportType, - const void* ReportData, - const uint16_t ReportSize) + const uint8_t ReportID, + const uint8_t ReportType, + const void* ReportData, + const uint16_t ReportSize) { // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports // uint8_t* LEDReport = (uint8_t*)ReportData; diff --git a/Hoodloader/Lib/LightweightRingBuff.h b/Hoodloader/Lib/LightweightRingBuff.h index 616ba62..b3ed6a0 100644 --- a/Hoodloader/Lib/LightweightRingBuff.h +++ b/Hoodloader/Lib/LightweightRingBuff.h @@ -44,12 +44,12 @@ /* Defines: */ /** Size of each ring buffer, in data elements - must be between 1 and 255. */ - #define BUFFER_SIZE (128-28) //reduced, needed some ram + #define BUFFER_SIZE (128-16) //reduced, needed some ram /** Maximum number of data elements to buffer before forcing a flush. * Must be less than BUFFER_SIZE */ - #define BUFFER_NEARLY_FULL (96-28) + #define BUFFER_NEARLY_FULL (96-16) /** Type of data to store into the buffer. */ #define RingBuff_Data_t uint8_t diff --git a/Hoodloader/Lib/NicoHoodProtocol_c.c b/Hoodloader/Lib/NicoHoodProtocol_c.c index 8d8c91b..02651a4 100644 --- a/Hoodloader/Lib/NicoHoodProtocol_c.c +++ b/Hoodloader/Lib/NicoHoodProtocol_c.c @@ -27,54 +27,54 @@ THE SOFTWARE. // Public variables //================================================================================ -uint8_t NHPreadbuffer[6]={0}; -uint8_t NHPreadlength=0; -uint8_t NHPwritebuffer[6]={0}; -uint8_t NHPwritelength=0; +uint8_t NHPreadbuffer[6] = { 0 }; +uint8_t NHPreadlength = 0; +//uint8_t NHPwritebuffer[6] = { 0 }; +//uint8_t NHPwritelength = 0; //================================================================================ // Private variables //================================================================================ // Fully read data -static uint8_t mCommand=0; -static uint8_t mAddress=0; -static uint32_t mData=0; -static uint8_t mErrorLevel=NHP_INPUT_RESET; +static uint8_t mCommand = 0; +static uint8_t mAddress = 0; +static uint32_t mData = 0; +static uint8_t mErrorLevel = NHP_INPUT_RESET; // in progress reading data -static uint8_t mBlocks=0; -static uint32_t mWorkData=0; +static uint8_t mBlocks = 0; +static uint32_t mWorkData = 0; //================================================================================ //General Functions //================================================================================ // reset Protocol on the next reading to start a new clean reading -void NHPreset(void){ mErrorLevel=NHP_INPUT_RESET; } +void NHPreset(void){ mErrorLevel = NHP_INPUT_RESET; } // access for the variables -uint8_t NHPgetCommand() { return mCommand; } -uint8_t NHPgetAddress() { return mAddress; } -uint32_t NHPgetData() { return mData; } -uint16_t NHPgetChecksumData() { return mData; } -uint8_t NHPgetChecksumData0() { return mData; } -uint8_t NHPgetChecksumData1() { return mData>>8;} +uint8_t NHPgetCommand() { return mCommand; } +uint8_t NHPgetAddress() { return mAddress; } +uint32_t NHPgetData() { return mData; } +uint16_t NHPgetChecksumData() { return mData; } +uint8_t NHPgetChecksumData0() { return mData; } +uint8_t NHPgetChecksumData1() { return mData >> 8; } uint8_t NHPgetErrorLevel(){ return mErrorLevel; } // reset buffer for read/write operations -void NHPresetreadbuffer() { - while(NHPreadlength){ +void NHPresetreadbuffer() { + while (NHPreadlength){ NHPreadlength--; - NHPreadbuffer[NHPreadlength]=0; - } -} -void NHPresetwritebuffer(){ - while(NHPwritelength){ - NHPwritelength--; - NHPwritebuffer[NHPwritelength]=0; + NHPreadbuffer[NHPreadlength] = 0; } } +//void NHPresetwritebuffer(){ +// while (NHPwritelength){ +// NHPwritelength--; +// NHPwritebuffer[NHPwritelength] = 0; +// } +//} //================================================================================ //Read @@ -82,110 +82,110 @@ void NHPresetwritebuffer(){ bool NHPread(uint8_t input){ //reset if previous read was with an input/error - if(mErrorLevel){ + if (mErrorLevel){ // cancel any pending data reads if a reset was triggered - if(mErrorLevel & NHP_INPUT_RESET){ - mBlocks=0; - mWorkData=0; + if (mErrorLevel & NHP_INPUT_RESET){ + mBlocks = 0; + mWorkData = 0; } // if previous read was a lead error keep this byte - if(mErrorLevel&NHP_ERR_LEAD){ - NHPreadbuffer[0]=NHPreadbuffer[NHPreadlength]; - NHPreadlength=1; + if (mErrorLevel&NHP_ERR_LEAD){ + NHPreadbuffer[0] = NHPreadbuffer[NHPreadlength]; + NHPreadlength = 1; } - else NHPreadlength=0; + else NHPreadlength = 0; } // reset fully read data - mCommand=0; - mAddress=0; - mData=0; - mErrorLevel=0; + mCommand = 0; + mAddress = 0; + mData = 0; + mErrorLevel = 0; //write input to the buffer - NHPreadbuffer[NHPreadlength]=input; + NHPreadbuffer[NHPreadlength] = input; NHPreadlength++; // check the lead/end/data indicator - switch(input&NHP_MASK_START){ + switch (input&NHP_MASK_START){ - case(NHP_MASK_LEAD): - { - // we were still reading! Log an error - if(mBlocks){ - mErrorLevel |= NHP_ERR_LEAD | NHP_ERR_READ; - NHPreadlength--; - } - - // read command indicator or block length - mBlocks = (input & NHP_MASK_LENGTH)>>3; - switch(mBlocks){ - case 0: - case 1: - // save 4 bit command - mCommand=(input & NHP_MASK_COMMAND)+1; - mBlocks = 0; - mErrorLevel |= NHP_INPUT_COMMAND | NHP_INPUT_NEW; - return true; - break; - case 7: - // save block length + first 4 data bits (special case) - mWorkData = input & NHP_MASK_DATA_4BIT; - mBlocks -=2; - break; - default: - // save block length + first 3 data bits - mWorkData = input & NHP_MASK_DATA_3BIT; - mBlocks--; - break; - } + case(NHP_MASK_LEAD) : + { + // we were still reading! Log an error + if (mBlocks){ + mErrorLevel |= NHP_ERR_LEAD | NHP_ERR_READ; + NHPreadlength--; } - break; - case(NHP_MASK_END): - { - if(mBlocks--==1){ - // save data + address - mAddress=(input&0x3F)+1; - mData=mWorkData; - mErrorLevel |= NHP_INPUT_ADDRESS | NHP_INPUT_NEW; - return true; - } - else{ - // log an error, not ready for an address byte, and reset data counters - mErrorLevel |= NHP_ERR_DATA | NHP_ERR_READ; - mBlocks=0; - } + // read command indicator or block length + mBlocks = (input & NHP_MASK_LENGTH) >> 3; + switch (mBlocks){ + case 0: + case 1: + // save 4 bit command + mCommand = (input & NHP_MASK_COMMAND) + 1; + mBlocks = 0; + mErrorLevel |= NHP_INPUT_COMMAND | NHP_INPUT_NEW; + return true; + break; + case 7: + // save block length + first 4 data bits (special case) + mWorkData = input & NHP_MASK_DATA_4BIT; + mBlocks -= 2; + break; + default: + // save block length + first 3 data bits + mWorkData = input & NHP_MASK_DATA_3BIT; + mBlocks--; + break; } - break; + } + break; - //case NHP_MASK_DATA1/2?? <-- + case(NHP_MASK_END) : + { + if (mBlocks-- == 1){ + // save data + address + mAddress = (input & 0x3F) + 1; + mData = mWorkData; + mErrorLevel |= NHP_INPUT_ADDRESS | NHP_INPUT_NEW; + return true; + } + else{ + // log an error, not ready for an address byte, and reset data counters + mErrorLevel |= NHP_ERR_DATA | NHP_ERR_READ; + mBlocks = 0; + } + } + break; + + //case NHP_MASK_DATA1/2?? <-- default: //NHP_MASK_DATA1/2 - { - if(mBlocks--<2){ - // log an error, expecting an address or header byte - mErrorLevel |= NHP_ERR_END | NHP_ERR_READ; - mBlocks=0; - } - else{ - // get next 7 bits of data - mWorkData<<=7; - // dont need &NHP_MASK_DATA_7BIT because first bit is zero! - mWorkData|=input; - } - } + { + if (mBlocks-- < 2){ + // log an error, expecting an address or header byte + mErrorLevel |= NHP_ERR_END | NHP_ERR_READ; + mBlocks = 0; + } + else{ + // get next 7 bits of data + mWorkData <<= 7; + // dont need &NHP_MASK_DATA_7BIT because first bit is zero! + mWorkData |= input; + } + } break; } // end switch // no new input - return false; + return false; } // reads two bytes and check its inverse bool NHPreadChecksum(uint8_t input){ - if(NHPread(input)){ + if (NHPread(input)){ // if there is an address input (comand invalid, too insecure) - if(NHPgetAddress() && (((NHPgetData()&0xFFFF) ^ (NHPgetData()>>16))==0xFFFF)){ + if (NHPgetAddress() && (((NHPgetData() & 0xFFFF) ^ (NHPgetData() >> 16)) == 0xFFFF)){ // make sure to use getAddress() and getData()&0xFFFF return true; } @@ -198,57 +198,60 @@ bool NHPreadChecksum(uint8_t input){ //Write //================================================================================ -void NHPwriteCommand(uint8_t command){ +uint8_t NHPwriteCommand(uint8_t command){ // send lead mask 11 + length 00|0 or 00|1 including the last bit for the 4 bit command - NHPwritebuffer[0] = NHP_MASK_LEAD | ((command-1) & NHP_MASK_COMMAND); - NHPwritelength=1; + // return the Command itself + return NHP_MASK_LEAD | ((command - 1) & NHP_MASK_COMMAND); } -void NHPwriteAddress(uint8_t address, uint32_t data){ +uint8_t NHPwriteAddress(uint8_t address, uint32_t data, uint8_t* buff){ // start with the maximum size of blocks - uint8_t blocks=7; + uint8_t blocks = 7; // check for the first 7 bit block that doesnt fit into the first 3 bits - while(blocks>2){ - uint8_t nextvalue=(data>>(7*(blocks-3))); - if(nextvalue>NHP_MASK_DATA_3BIT){ + while (blocks > 2){ + uint8_t nextvalue = (data >> (7 * (blocks - 3))); + if (nextvalue > NHP_MASK_DATA_3BIT){ // special case for the MSB - if(blocks==7) { - NHPwritebuffer[0] = nextvalue; + if (blocks == 7) { + buff[0] = nextvalue; blocks--; } break; } else{ - // write the possible first 3 bits and check again after - NHPwritebuffer[0] = nextvalue; + // write the possible first 3 bits and check again after if zero + buff[0] = nextvalue; blocks--; + // we have our first bits, stop (nonzero) + if (nextvalue) + break; } } // write the rest of the data bits - uint8_t datablocks=blocks-2; - while(datablocks>0){ - NHPwritebuffer[datablocks] = data & NHP_MASK_DATA_7BIT; - data>>=7; + uint8_t datablocks = blocks - 2; + while (datablocks > 0){ + buff[datablocks] = data & NHP_MASK_DATA_7BIT; + data >>= 7; datablocks--; } // write lead + length mask - NHPwritebuffer[0] |= NHP_MASK_LEAD | (blocks <<3); + buff[0] |= NHP_MASK_LEAD | (blocks << 3); // write end mask - NHPwritebuffer[blocks-1] = NHP_MASK_END | ((address-1) & NHP_MASK_ADDRESS); + buff[blocks - 1] = NHP_MASK_END | ((address - 1) & NHP_MASK_ADDRESS); - // save the length - NHPwritelength=blocks; + // return the length + return blocks; } // writes two bytes with its inverse -void NHPwriteChecksum(uint8_t address, uint16_t data){ - uint32_t temp=~data; - uint32_t checksum=(temp<<16)|data; - NHPwriteAddress(address,checksum); +uint8_t NHPwriteChecksum(uint8_t address, uint16_t data, uint8_t* buff){ + uint32_t temp = ~data; + uint32_t checksum = (temp << 16) | data; + return NHPwriteAddress(address, checksum, buff); } diff --git a/Hoodloader/Lib/NicoHoodProtocol_c.h b/Hoodloader/Lib/NicoHoodProtocol_c.h index 3010585..da79f8e 100644 --- a/Hoodloader/Lib/NicoHoodProtocol_c.h +++ b/Hoodloader/Lib/NicoHoodProtocol_c.h @@ -88,17 +88,18 @@ uint8_t NHPgetErrorLevel(void); // buffer for read/write operations extern uint8_t NHPreadbuffer[6]; extern uint8_t NHPreadlength; -extern uint8_t NHPwritebuffer[6]; -extern uint8_t NHPwritelength; +//extern uint8_t NHPwritebuffer[6]; +//extern uint8_t NHPwritelength; void NHPresetreadbuffer(void); -void NHPresetwritebuffer(void); +//void NHPresetwritebuffer(void); // general multifunctional read/write functions void NHPreset(void); bool NHPread(uint8_t input); bool NHPreadChecksum(uint8_t input); -void NHPwriteCommand(uint8_t command); -void NHPwriteAddress(uint8_t address, uint32_t data); -void NHPwriteChecksum(uint8_t address, uint16_t data); +uint8_t NHPwriteCommand(uint8_t command); // returns the command +// returns the length, writes data to the passed in buff (6 bytes max) +uint8_t NHPwriteAddress(uint8_t address, uint32_t data, uint8_t* buff); +uint8_t NHPwriteChecksum(uint8_t address, uint16_t data, uint8_t* buff); #endif diff --git a/Hoodloader/Lib/t/NicoHoodProtocol_c.c b/Hoodloader/Lib/t/NicoHoodProtocol_c.c new file mode 100644 index 0000000..9b77c5a --- /dev/null +++ b/Hoodloader/Lib/t/NicoHoodProtocol_c.c @@ -0,0 +1,257 @@ +/* +Copyright (c) 2014 NicoHood +See the readme for credit to other people. + +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. +*/ + +#include "NicoHoodProtocol_c.h" + +//================================================================================ +// Public variables +//================================================================================ + +uint8_t NHPreadbuffer[6] = { 0 }; +uint8_t NHPreadlength = 0; +uint8_t NHPwritebuffer[6] = { 0 }; +uint8_t NHPwritelength = 0; + +//================================================================================ +// Private variables +//================================================================================ + +// Fully read data +static uint8_t mCommand = 0; +static uint8_t mAddress = 0; +static uint32_t mData = 0; +static uint8_t mErrorLevel = NHP_INPUT_RESET; + +// in progress reading data +static uint8_t mBlocks = 0; +static uint32_t mWorkData = 0; + +//================================================================================ +//General Functions +//================================================================================ + +// reset Protocol on the next reading to start a new clean reading +void NHPreset(void){ mErrorLevel = NHP_INPUT_RESET; } + +// access for the variables +uint8_t NHPgetCommand() { return mCommand; } +uint8_t NHPgetAddress() { return mAddress; } +uint32_t NHPgetData() { return mData; } +uint16_t NHPgetChecksumData() { return mData; } +uint8_t NHPgetChecksumData0() { return mData; } +uint8_t NHPgetChecksumData1() { return mData >> 8; } +uint8_t NHPgetErrorLevel(){ return mErrorLevel; } + +// reset buffer for read/write operations +void NHPresetreadbuffer() { + while (NHPreadlength){ + NHPreadlength--; + NHPreadbuffer[NHPreadlength] = 0; + } +} +void NHPresetwritebuffer(){ + while (NHPwritelength){ + NHPwritelength--; + NHPwritebuffer[NHPwritelength] = 0; + } +} + +//================================================================================ +//Read +//================================================================================ + +bool NHPread(uint8_t input){ + //reset if previous read was with an input/error + if (mErrorLevel){ + // cancel any pending data reads if a reset was triggered + if (mErrorLevel & NHP_INPUT_RESET){ + mBlocks = 0; + mWorkData = 0; + } + // if previous read was a lead error keep this byte + if (mErrorLevel&NHP_ERR_LEAD){ + NHPreadbuffer[0] = NHPreadbuffer[NHPreadlength]; + NHPreadlength = 1; + } + else NHPreadlength = 0; + } + + // reset fully read data + mCommand = 0; + mAddress = 0; + mData = 0; + mErrorLevel = 0; + + //write input to the buffer + NHPreadbuffer[NHPreadlength] = input; + NHPreadlength++; + + // check the lead/end/data indicator + switch (input&NHP_MASK_START){ + + case(NHP_MASK_LEAD) : + { + // we were still reading! Log an error + if (mBlocks){ + mErrorLevel |= NHP_ERR_LEAD | NHP_ERR_READ; + NHPreadlength--; + } + + // read command indicator or block length + mBlocks = (input & NHP_MASK_LENGTH) >> 3; + switch (mBlocks){ + case 0: + case 1: + // save 4 bit command + mCommand = (input & NHP_MASK_COMMAND) + 1; + mBlocks = 0; + mErrorLevel |= NHP_INPUT_COMMAND | NHP_INPUT_NEW; + return true; + break; + case 7: + // save block length + first 4 data bits (special case) + mWorkData = input & NHP_MASK_DATA_4BIT; + mBlocks -= 2; + break; + default: + // save block length + first 3 data bits + mWorkData = input & NHP_MASK_DATA_3BIT; + mBlocks--; + break; + } + } + break; + + case(NHP_MASK_END) : + { + if (mBlocks-- == 1){ + // save data + address + mAddress = (input & 0x3F) + 1; + mData = mWorkData; + mErrorLevel |= NHP_INPUT_ADDRESS | NHP_INPUT_NEW; + return true; + } + else{ + // log an error, not ready for an address byte, and reset data counters + mErrorLevel |= NHP_ERR_DATA | NHP_ERR_READ; + mBlocks = 0; + } + } + break; + + //case NHP_MASK_DATA1/2?? <-- + default: //NHP_MASK_DATA1/2 + { + if (mBlocks-- < 2){ + // log an error, expecting an address or header byte + mErrorLevel |= NHP_ERR_END | NHP_ERR_READ; + mBlocks = 0; + } + else{ + // get next 7 bits of data + mWorkData <<= 7; + // dont need &NHP_MASK_DATA_7BIT because first bit is zero! + mWorkData |= input; + } + } + break; + } // end switch + + // no new input + return false; +} + +// reads two bytes and check its inverse +bool NHPreadChecksum(uint8_t input){ + if (NHPread(input)){ + // if there is an address input (comand invalid, too insecure) + if (NHPgetAddress() && (((NHPgetData() & 0xFFFF) ^ (NHPgetData() >> 16)) == 0xFFFF)){ + // make sure to use getAddress() and getData()&0xFFFF + return true; + } + // else you can forward the buffer or pass -1 as error + } + return false; +} + +//================================================================================ +//Write +//================================================================================ + +void NHPwriteCommand(uint8_t command){ + // send lead mask 11 + length 00|0 or 00|1 including the last bit for the 4 bit command + NHPwritebuffer[0] = NHP_MASK_LEAD | ((command - 1) & NHP_MASK_COMMAND); + NHPwritelength = 1; +} + +void NHPwriteAddress(uint8_t address, uint32_t data){ + // start with the maximum size of blocks + uint8_t blocks = 7; + + // check for the first 7 bit block that doesnt fit into the first 3 bits + while (blocks > 2){ + uint8_t nextvalue = (data >> (7 * (blocks - 3))); + if (nextvalue > NHP_MASK_DATA_3BIT){ + // special case for the MSB + if (blocks == 7) { + NHPwritebuffer[0] = nextvalue; + blocks--; + } + break; + } + else{ + // write the possible first 3 bits and check again after if zero + NHPwritebuffer[0] = nextvalue; + blocks--; + // we have our first bits, stop (nonzero) + if (nextvalue) + break; + } + } + + // write the rest of the data bits + uint8_t datablocks = blocks - 2; + while (datablocks > 0){ + NHPwritebuffer[datablocks] = data & NHP_MASK_DATA_7BIT; + data >>= 7; + datablocks--; + } + + // write lead + length mask + NHPwritebuffer[0] |= NHP_MASK_LEAD | (blocks << 3); + + // write end mask + NHPwritebuffer[blocks - 1] = NHP_MASK_END | ((address - 1) & NHP_MASK_ADDRESS); + + // save the length + NHPwritelength = blocks; +} + + +// writes two bytes with its inverse +void NHPwriteChecksum(uint8_t address, uint16_t data){ + uint32_t temp = ~data; + uint32_t checksum = (temp << 16) | data; + NHPwriteAddress(address, checksum); +} + diff --git a/Hoodloader/Lib/t/NicoHoodProtocol_c.h b/Hoodloader/Lib/t/NicoHoodProtocol_c.h new file mode 100644 index 0000000..3010585 --- /dev/null +++ b/Hoodloader/Lib/t/NicoHoodProtocol_c.h @@ -0,0 +1,104 @@ +/* +Copyright (c) 2014 NicoHood +See the readme for credit to other people. + +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 NICOHOODPROTOCOL_C_h +#define NICOHOODPROTOCOL_C_h + +#include //uint_t definitions +#include //bool type + +//================================================================================ +//Settings +//================================================================================ + +// empty + +//================================================================================ +//Definitions +//================================================================================ + +// ErrorLevel +#define NHP_MASK_INPUT 0x0F +#define NHP_INPUT_NO 0x00 +#define NHP_INPUT_NEW 0x01 +#define NHP_INPUT_ADDRESS 0x02 +#define NHP_INPUT_COMMAND 0x04 +#define NHP_INPUT_RESET 0x08 +#define NHP_MASK_ERR 0xF0 +#define NHP_ERR_NO 0x00 +#define NHP_ERR_READ 0x10 +#define NHP_ERR_END 0x20 +#define NHP_ERR_DATA 0x40 +#define NHP_ERR_LEAD 0x80 +#define NHP_ERR_LIMIT 20 //0-255, only for the user function + +// Start Mask +#define NHP_MASK_START 0xC0 //B11|000000 the two MSB bits +#define NHP_MASK_LEAD 0xC0 //B11|000000 +#define NHP_MASK_DATA 0x00 //B0|0000000 only the first MSB is important +#define NHP_MASK_END 0x80 //B10|000000 + +// Content Mask +#define NHP_MASK_LENGTH 0x38 //B00|111|000 +#define NHP_MASK_COMMAND 0x0F //B0000|1111 +#define NHP_MASK_DATA_7BIT 0x7F //B0|1111111 +#define NHP_MASK_DATA_4BIT 0x0F //B0000|1111 +#define NHP_MASK_DATA_3BIT 0x07 //B00000|111 +#define NHP_MASK_ADDRESS 0x3F //B00|111111 + +// Reserved Addresses +#define NHP_ADDRESS_CONTROL 0x01 + +// Reserved Usages +#define NHP_USAGE_ARDUINOHID 0x01 + +//================================================================================ +//Protocol Function Prototypes +//================================================================================ + +// access for the variables +uint8_t NHPgetCommand(void); +uint8_t NHPgetAddress(void); +uint32_t NHPgetData(void); +uint16_t NHPgetChecksumData(void); +uint8_t NHPgetChecksumData0(void); +uint8_t NHPgetChecksumData1(void); +uint8_t NHPgetErrorLevel(void); + +// buffer for read/write operations +extern uint8_t NHPreadbuffer[6]; +extern uint8_t NHPreadlength; +extern uint8_t NHPwritebuffer[6]; +extern uint8_t NHPwritelength; +void NHPresetreadbuffer(void); +void NHPresetwritebuffer(void); + +// general multifunctional read/write functions +void NHPreset(void); +bool NHPread(uint8_t input); +bool NHPreadChecksum(uint8_t input); +void NHPwriteCommand(uint8_t command); +void NHPwriteAddress(uint8_t address, uint32_t data); +void NHPwriteChecksum(uint8_t address, uint16_t data); + +#endif diff --git a/Hoodloader/lufa-LUFA-140302/.gitignore b/Hoodloader/lufa-LUFA-140302/.gitignore index 8e07f3c..4486e48 100644 --- a/Hoodloader/lufa-LUFA-140302/.gitignore +++ b/Hoodloader/lufa-LUFA-140302/.gitignore @@ -17,4 +17,4 @@ Projects/* Maintenance/* BuildTests/* Bootloaders/* - +Documentation/* diff --git a/Hoodloader/makefile b/Hoodloader/makefile index eb54072..26bc641 100644 --- a/Hoodloader/makefile +++ b/Hoodloader/makefile @@ -19,8 +19,8 @@ MCU_DFU = atmega8u2 F_CPU = 16000000 BOARD = USER -#Vendor ID from lufa -#Product ID created my own +#Vendor ID from lufa 0x03EB +#Product ID created my own 0x4E68 ARDUNIOHID_OPTS = -DHOODLOADER_VID=0x03EB ARDUNIOHID_OPTS += -DHOODLOADER_PID=0x4E68 #You can also use the native Arduino VID and PID @@ -28,6 +28,7 @@ ARDUNIOHID_OPTS += -DHOODLOADER_PID=0x4E68 #ProductID Arduino Uno 0x0001 #ProductID Arduino Mega 0x0010 + ARCH = AVR8 F_USB = $(F_CPU) OPTIMIZATION = s diff --git a/Readme.md b/Readme.md index 64c1fe3..ba75ab9 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -Arduino HID Project BETA 1.2 +Arduino HID Project BETA 1.3 =================== Dont you always wanted to turn your Arduino in a Generic HID device like a Keyboard or a Gamepad? Disappointed that the Uno doesnt support this at all and the Micro/Leonardo only Mouse + Keyboard? @@ -21,7 +21,8 @@ Software includes: * Arduino HID Uno/Mega library * Arduino HID Micro/Leonardo library -* Arduino HID Bootloader (Hoodloader) +* Arduino HID Bootloader (Hoodloader) + driver +*Compatible with Linux/Mac/Windows XP/7/8.1* The following devices are supported: @@ -33,11 +34,13 @@ The following devices are supported: * 2 Joysticks (2 buttons, 2 10bit axis) Projects can be found here: -http://nicohood.wordpress.com/ +* [Gamecube to PC adapter](https://github.com/NicoHood/Nintendo) +* [Other projects](http://nicohood.wordpress.com/) Installation Leonardo/Micro/Uno/Mega ==================================== -Download the library and **move and replace** all 4 .h/cpp files from the download folder to: +Download the library and install like you are used to. +Then **move and replace** all files from the folder that matches your Arduino IDE version to one of these paths: ``` C:\Arduino\arduino-1.0.5-r2\hardware\arduino\cores\arduino C:\Arduino\arduino-1.5.6-r2\hardware\arduino\avr\cores\arduino @@ -127,38 +130,47 @@ Todo ==== * Remove debug stuff (shouldnt effect anything for you) * Add more devices (even more?) -* Add ICSP Programmer function +* Add Midi (do you want that?) +* Add ICSP Programmer function (ram limit is a problem) * Add Led/SPI support (discarded, not needed, too slow) * Add rumble support (very hard) * Add Xbox Support (too hard) -* Add Midi (do you want that?) * Add Report Out function (for Keyboard Leds etc, maybe the 4 pin header?) * RAW HID Known Bugs ========== +XBMC 13.1 (a Media Center) uses Gamepad input. Its seems to not work and cause weird errors. +Even with a standard Gamepad i have these errors. Just want to mention it here. + System Wakeup is currently not working on all versions! -Not tested on the 8u2 (message me if it works!) +Not tested on the 8u2 (should only work without DFU due to size. message me if it works!) Not tested on the Due (message me if it works!) If you get a checksum Error after Uploading please message me and send me the whole project. Same if your Arduino crashes and dont want to upload sketches anymore (Replug usb fixes this). These bugs occured while devoloping the bootloader and should be fixed. Just in case it happens again I noted it here. +USB can behave weird, so please check your code for errors first. If you cannot find a mistake open a Github issue. -HID only works with baud 115200 because there is no "programming finished" indicator. If you dont use HID you can still choose the baud of your choice +HID only works with baud 115200 (on Uno/Mega) because there is no "programming finished" indicator. If you dont use HID you can still choose the baud of your choice. Oh and by the way: I also removed some bugs from the official firmware. Version History =============== ``` -1.2 Beta Release (xx.06.2014) +1.3 Beta Release (01.07.2014) +* Bugfixes in the Hoodloader: + * Improved ram usage +* **Important NHP fix inside the HID Class** + +1.2 Beta Release (22.06.2014) * Added 1.0.x/1.5.x support * Bugfixes in the Hoodloader: - * Sometimes HID Devices weren't updating when using more than 1 Device (set forcewrite to true) - * Fast updates crashed the bootloader (ram usage was too much, set CDC buffer from 128 to 100 byte each) + * Sometimes HID Devices weren't updating when using more than 1 Device (set forcewrite to true) + * Fast updates crashed the bootloader (too much ram usage, set CDC buffer from 128b to 100b each) * Minor file structure changes 1.1 Beta Release (05.06.2014) diff --git a/Tutorials/Readme.md b/Tutorials/Readme.md index 3d8f468..cdb3fc8 100644 --- a/Tutorials/Readme.md +++ b/Tutorials/Readme.md @@ -1 +1 @@ -See http://nicohood.wordpress.com/ for tutorials \ No newline at end of file +See http://nicohood.wordpress.com/ for tutorials and projects \ No newline at end of file