diff options
author | skullY <skullydazed@gmail.com> | 2019-08-30 11:19:03 -0700 |
---|---|---|
committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-30 15:01:52 -0700 |
commit | b624f32f944acdc59dcb130674c09090c5c404cb (patch) | |
tree | bc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /tmk_core | |
parent | 61af76a10d00aba185b8338604171de490a13e3b (diff) | |
download | qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz |
clang-format changes
Diffstat (limited to 'tmk_core')
229 files changed, 15513 insertions, 18026 deletions
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c index f47fd20fc..1ba38a285 100644 --- a/tmk_core/common/action.c +++ b/tmk_core/common/action.c @@ -29,9 +29,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "wait.h" #ifdef DEBUG_ACTION -#include "debug.h" +# include "debug.h" #else -#include "nodebug.h" +# include "nodebug.h" #endif int tp_buttons; @@ -41,24 +41,25 @@ int retro_tapping_counter = 0; #endif #ifdef FAUXCLICKY_ENABLE -#include <fauxclicky.h> +# include <fauxclicky.h> #endif #ifndef TAP_CODE_DELAY -# define TAP_CODE_DELAY 0 +# define TAP_CODE_DELAY 0 #endif #ifndef TAP_HOLD_CAPS_DELAY -# define TAP_HOLD_CAPS_DELAY 80 +# define TAP_HOLD_CAPS_DELAY 80 #endif /** \brief Called to execute an action. * * FIXME: Needs documentation. */ -void action_exec(keyevent_t event) -{ +void action_exec(keyevent_t event) { if (!IS_NOEVENT(event)) { dprint("\n---- action_exec: start -----\n"); - dprint("EVENT: "); debug_event(event); dprintln(); + dprint("EVENT: "); + debug_event(event); + dprintln(); #ifdef RETRO_TAPPING retro_tapping_counter++; #endif @@ -80,7 +81,7 @@ void action_exec(keyevent_t event) } #endif - keyrecord_t record = { .event = event }; + keyrecord_t record = {.event = event}; #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) if (has_oneshot_layer_timed_out()) { @@ -96,14 +97,16 @@ void action_exec(keyevent_t event) #else process_record(&record); if (!IS_NOEVENT(record.event)) { - dprint("processed: "); debug_record(record); dprintln(); + dprint("processed: "); + debug_record(record); + dprintln(); } #endif } #ifdef SWAP_HANDS_ENABLE bool swap_hands = false; -bool swap_held = false; +bool swap_held = false; /** \brief Process Hand Swap * @@ -112,10 +115,9 @@ bool swap_held = false; void process_hand_swap(keyevent_t *event) { static swap_state_row_t swap_state[MATRIX_ROWS]; - keypos_t pos = event->key; - swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col; - bool do_swap = event->pressed ? swap_hands : - swap_state[pos.row] & (col_bit); + keypos_t pos = event->key; + swap_state_row_t col_bit = (swap_state_row_t)1 << pos.col; + bool do_swap = event->pressed ? swap_hands : swap_state[pos.row] & (col_bit); if (do_swap) { event->key = hand_swap_config[pos.row][pos.col]; @@ -129,44 +131,36 @@ void process_hand_swap(keyevent_t *event) { #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) bool disable_action_cache = false; -void process_record_nocache(keyrecord_t *record) -{ +void process_record_nocache(keyrecord_t *record) { disable_action_cache = true; process_record(record); disable_action_cache = false; } #else -void process_record_nocache(keyrecord_t *record) -{ - process_record(record); -} +void process_record_nocache(keyrecord_t *record) { process_record(record); } #endif -__attribute__ ((weak)) -bool process_record_quantum(keyrecord_t *record) { - return true; -} +__attribute__((weak)) bool process_record_quantum(keyrecord_t *record) { return true; } #ifndef NO_ACTION_TAPPING /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress. * * FIXME: Needs documentation. */ -void process_record_tap_hint(keyrecord_t *record) -{ +void process_record_tap_hint(keyrecord_t *record) { action_t action = layer_switch_get_action(record->event.key); switch (action.kind.id) { -#ifdef SWAP_HANDS_ENABLE +# ifdef SWAP_HANDS_ENABLE case ACT_SWAP_HANDS: switch (action.swap.code) { case OP_SH_TAP_TOGGLE: default: swap_hands = !swap_hands; - swap_held = true; + swap_held = true; } break; -#endif +# endif } } #endif @@ -175,18 +169,21 @@ void process_record_tap_hint(keyrecord_t *record) * * FIXME: Needs documentation. */ -void process_record(keyrecord_t *record) -{ - if (IS_NOEVENT(record->event)) { return; } - - if(!process_record_quantum(record)) +void process_record(keyrecord_t *record) { + if (IS_NOEVENT(record->event)) { return; + } + + if (!process_record_quantum(record)) return; action_t action = store_or_get_action(record->event.pressed, record->event.key); - dprint("ACTION: "); debug_action(action); + dprint("ACTION: "); + debug_action(action); #ifndef NO_ACTION_LAYER - dprint(" layer_state: "); layer_debug(); - dprint(" default_layer_state: "); default_layer_debug(); + dprint(" layer_state: "); + layer_debug(); + dprint(" default_layer_state: "); + default_layer_debug(); #endif dprintln(); @@ -197,8 +194,7 @@ void process_record(keyrecord_t *record) * * FIXME: Needs documentation. */ -void process_action(keyrecord_t *record, action_t action) -{ +void process_action(keyrecord_t *record, action_t action) { keyevent_t event = record->event; #ifndef NO_ACTION_TAPPING uint8_t tap_count = record->tap.count; @@ -221,131 +217,125 @@ void process_action(keyrecord_t *record, action_t action) switch (action.kind.id) { /* Key and Mods */ case ACT_LMODS: - case ACT_RMODS: - { - uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods : - action.key.mods<<4; - if (event.pressed) { - if (mods) { - if (IS_MOD(action.key.code) || action.key.code == KC_NO) { - // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless. - // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT). - // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO). - add_mods(mods); - } else { - add_weak_mods(mods); - } - send_keyboard_report(); + case ACT_RMODS: { + uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods : action.key.mods << 4; + if (event.pressed) { + if (mods) { + if (IS_MOD(action.key.code) || action.key.code == KC_NO) { + // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless. + // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT). + // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO). + add_mods(mods); + } else { + add_weak_mods(mods); } - register_code(action.key.code); - } else { - unregister_code(action.key.code); - if (mods) { - if (IS_MOD(action.key.code) || action.key.code == KC_NO) { - del_mods(mods); - } else { - del_weak_mods(mods); - } - send_keyboard_report(); + send_keyboard_report(); + } + register_code(action.key.code); + } else { + unregister_code(action.key.code); + if (mods) { + if (IS_MOD(action.key.code) || action.key.code == KC_NO) { + del_mods(mods); + } else { + del_weak_mods(mods); } + send_keyboard_report(); } } - break; + } break; #ifndef NO_ACTION_TAPPING case ACT_LMODS_TAP: - case ACT_RMODS_TAP: - { - uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : - action.key.mods<<4; - switch (action.layer_tap.code) { - #ifndef NO_ACTION_ONESHOT - case MODS_ONESHOT: - // Oneshot modifier - if (event.pressed) { - if (tap_count == 0) { - dprint("MODS_TAP: Oneshot: 0\n"); - register_mods(mods | get_oneshot_mods()); - } else if (tap_count == 1) { - dprint("MODS_TAP: Oneshot: start\n"); - set_oneshot_mods(mods | get_oneshot_mods()); - #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 - } else if (tap_count == ONESHOT_TAP_TOGGLE) { - dprint("MODS_TAP: Toggling oneshot"); - clear_oneshot_mods(); - set_oneshot_locked_mods(mods); - register_mods(mods); - #endif - } else { - register_mods(mods | get_oneshot_mods()); - } + case ACT_RMODS_TAP: { + uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : action.key.mods << 4; + switch (action.layer_tap.code) { +# ifndef NO_ACTION_ONESHOT + case MODS_ONESHOT: + // Oneshot modifier + if (event.pressed) { + if (tap_count == 0) { + dprint("MODS_TAP: Oneshot: 0\n"); + register_mods(mods | get_oneshot_mods()); + } else if (tap_count == 1) { + dprint("MODS_TAP: Oneshot: start\n"); + set_oneshot_mods(mods | get_oneshot_mods()); +# if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 + } else if (tap_count == ONESHOT_TAP_TOGGLE) { + dprint("MODS_TAP: Toggling oneshot"); + clear_oneshot_mods(); + set_oneshot_locked_mods(mods); + register_mods(mods); +# endif } else { - if (tap_count == 0) { - clear_oneshot_mods(); - unregister_mods(mods); - } else if (tap_count == 1) { - // Retain Oneshot mods - #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 - if (mods & get_mods()) { - clear_oneshot_locked_mods(); - clear_oneshot_mods(); - unregister_mods(mods); - } - } else if (tap_count == ONESHOT_TAP_TOGGLE) { - // Toggle Oneshot Layer - #endif - } else { + register_mods(mods | get_oneshot_mods()); + } + } else { + if (tap_count == 0) { + clear_oneshot_mods(); + unregister_mods(mods); + } else if (tap_count == 1) { + // Retain Oneshot mods +# if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 + if (mods & get_mods()) { + clear_oneshot_locked_mods(); clear_oneshot_mods(); unregister_mods(mods); } + } else if (tap_count == ONESHOT_TAP_TOGGLE) { + // Toggle Oneshot Layer +# endif + } else { + clear_oneshot_mods(); + unregister_mods(mods); } - break; - #endif - case MODS_TAP_TOGGLE: - if (event.pressed) { - if (tap_count <= TAPPING_TOGGLE) { + } + break; +# endif + case MODS_TAP_TOGGLE: + if (event.pressed) { + if (tap_count <= TAPPING_TOGGLE) { + register_mods(mods); + } + } else { + if (tap_count < TAPPING_TOGGLE) { + unregister_mods(mods); + } + } + break; + default: + if (event.pressed) { + if (tap_count > 0) { +# ifndef IGNORE_MOD_TAP_INTERRUPT + if (record->tap.interrupted) { + dprint("mods_tap: tap: cancel: add_mods\n"); + // ad hoc: set 0 to cancel tap + record->tap.count = 0; register_mods(mods); + } else +# endif + { + dprint("MODS_TAP: Tap: register_code\n"); + register_code(action.key.code); } } else { - if (tap_count < TAPPING_TOGGLE) { - unregister_mods(mods); - } + dprint("MODS_TAP: No tap: add_mods\n"); + register_mods(mods); } - break; - default: - if (event.pressed) { - if (tap_count > 0) { -#ifndef IGNORE_MOD_TAP_INTERRUPT - if (record->tap.interrupted) { - dprint("mods_tap: tap: cancel: add_mods\n"); - // ad hoc: set 0 to cancel tap - record->tap.count = 0; - register_mods(mods); - } else -#endif - { - dprint("MODS_TAP: Tap: register_code\n"); - register_code(action.key.code); - } - } else { - dprint("MODS_TAP: No tap: add_mods\n"); - register_mods(mods); + } else { + if (tap_count > 0) { + dprint("MODS_TAP: Tap: unregister_code\n"); + if (action.layer_tap.code == KC_CAPS) { + wait_ms(TAP_HOLD_CAPS_DELAY); } + unregister_code(action.key.code); } else { - if (tap_count > 0) { - dprint("MODS_TAP: Tap: unregister_code\n"); - if (action.layer_tap.code == KC_CAPS) { - wait_ms(TAP_HOLD_CAPS_DELAY); - } - unregister_code(action.key.code); - } else { - dprint("MODS_TAP: No tap: add_mods\n"); - unregister_mods(mods); - } + dprint("MODS_TAP: No tap: add_mods\n"); + unregister_mods(mods); } - break; - } + } + break; } - break; + } break; #endif #ifdef EXTRAKEY_ENABLE /* other HID usage */ @@ -374,13 +364,13 @@ void process_action(keyrecord_t *record, action_t action) if (event.pressed) { switch (action.key.code) { case KC_MS_BTN1: - tp_buttons |= (1<<0); + tp_buttons |= (1 << 0); break; case KC_MS_BTN2: - tp_buttons |= (1<<1); + tp_buttons |= (1 << 1); break; case KC_MS_BTN3: - tp_buttons |= (1<<2); + tp_buttons |= (1 << 2); break; default: break; @@ -390,13 +380,13 @@ void process_action(keyrecord_t *record, action_t action) } else { switch (action.key.code) { case KC_MS_BTN1: - tp_buttons &= ~(1<<0); + tp_buttons &= ~(1 << 0); break; case KC_MS_BTN2: - tp_buttons &= ~(1<<1); + tp_buttons &= ~(1 << 1); break; case KC_MS_BTN3: - tp_buttons &= ~(1<<2); + tp_buttons &= ~(1 << 2); break; default: break; @@ -411,28 +401,43 @@ void process_action(keyrecord_t *record, action_t action) if (action.layer_bitop.on == 0) { /* Default Layer Bitwise Operation */ if (!event.pressed) { - uint8_t shift = action.layer_bitop.part*4; - layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift; - layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0; + uint8_t shift = action.layer_bitop.part * 4; + layer_state_t bits = ((layer_state_t)action.layer_bitop.bits) << shift; + layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf) << shift) : 0; switch (action.layer_bitop.op) { - case OP_BIT_AND: default_layer_and(bits | mask); break; - case OP_BIT_OR: default_layer_or(bits | mask); break; - case OP_BIT_XOR: default_layer_xor(bits | mask); break; - case OP_BIT_SET: default_layer_set(bits | mask); break; + case OP_BIT_AND: + default_layer_and(bits | mask); + break; + case OP_BIT_OR: + default_layer_or(bits | mask); + break; + case OP_BIT_XOR: + default_layer_xor(bits | mask); + break; + case OP_BIT_SET: + default_layer_set(bits | mask); + break; } } } else { /* Layer Bitwise Operation */ - if (event.pressed ? (action.layer_bitop.on & ON_PRESS) : - (action.layer_bitop.on & ON_RELEASE)) { - uint8_t shift = action.layer_bitop.part*4; - layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift; - layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0; + if (event.pressed ? (action.layer_bitop.on & ON_PRESS) : (action.layer_bitop.on & ON_RELEASE)) { + uint8_t shift = action.layer_bitop.part * 4; + layer_state_t bits = ((layer_state_t)action.layer_bitop.bits) << shift; + layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf) << shift) : 0; switch (action.layer_bitop.op) { - case OP_BIT_AND: layer_and(bits | mask); break; - case OP_BIT_OR: layer_or(bits | mask); break; - case OP_BIT_XOR: layer_xor(bits | mask); break; - case OP_BIT_SET: layer_state_set(bits | mask); break; + case OP_BIT_AND: + layer_and(bits | mask); + break; + case OP_BIT_OR: + layer_or(bits | mask); + break; + case OP_BIT_XOR: + layer_xor(bits | mask); + break; + case OP_BIT_SET: + layer_state_set(bits | mask); + break; } } } @@ -446,7 +451,7 @@ void process_action(keyrecord_t *record, action_t action) layer_off(action.layer_mods.layer); } break; - #ifndef NO_ACTION_TAPPING +# ifndef NO_ACTION_TAPPING case ACT_LAYER_TAP: case ACT_LAYER_TAP_EXT: switch (action.layer_tap.code) { @@ -463,21 +468,18 @@ void process_action(keyrecord_t *record, action_t action) } break; case OP_ON_OFF: - event.pressed ? layer_on(action.layer_tap.val) : - layer_off(action.layer_tap.val); + event.pressed ? layer_on(action.layer_tap.val) : layer_off(action.layer_tap.val); break; case OP_OFF_ON: - event.pressed ? layer_off(action.layer_tap.val) : - layer_on(action.layer_tap.val); + event.pressed ? layer_off(action.layer_tap.val) : layer_on(action.layer_tap.val); break; case OP_SET_CLEAR: - event.pressed ? layer_move(action.layer_tap.val) : - layer_clear(); + event.pressed ? layer_move(action.layer_tap.val) : layer_clear(); break; - #ifndef NO_ACTION_ONESHOT +# ifndef NO_ACTION_ONESHOT case OP_ONESHOT: // Oneshot modifier - #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 +# if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1 do_release_oneshot = false; if (event.pressed) { del_mods(get_oneshot_locked_mods()); @@ -499,7 +501,7 @@ void process_action(keyrecord_t *record, action_t action) clear_oneshot_layer_state(ONESHOT_PRESSED); } } - #else +# else if (event.pressed) { layer_on(action.layer_tap.val); set_oneshot_layer(action.layer_tap.val, ONESHOT_START); @@ -509,9 +511,9 @@ void process_action(keyrecord_t *record, action_t action) clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED); } } - #endif +# endif break; - #endif +# endif default: /* tap key */ if (event.pressed) { @@ -529,7 +531,7 @@ void process_action(keyrecord_t *record, action_t action) wait_ms(TAP_HOLD_CAPS_DELAY); } else { wait_ms(TAP_CODE_DELAY); - } + } unregister_code(action.layer_tap.code); } else { dprint("KEYMAP_TAP_KEY: No tap: Off on release\n"); @@ -539,9 +541,9 @@ void process_action(keyrecord_t *record, action_t action) break; } break; - #endif +# endif #endif - /* Extentions */ + /* Extentions */ #ifndef NO_ACTION_MACRO case ACT_MACRO: action_macro_play(action_get_macro(record, action.func.id, action.func.opt)); @@ -599,7 +601,7 @@ void process_action(keyrecord_t *record, action_t action) swap_hands = false; } break; - #ifndef NO_ACTION_TAPPING +# ifndef NO_ACTION_TAPPING case OP_SH_TAP_TOGGLE: /* tap toggle */ @@ -619,23 +621,23 @@ void process_action(keyrecord_t *record, action_t action) /* tap key */ if (tap_count > 0) { if (swap_held) { - swap_hands = !swap_hands; // undo hold set up in _tap_hint - swap_held = false; + swap_hands = !swap_hands; // undo hold set up in _tap_hint + swap_held = false; } if (event.pressed) { register_code(action.swap.code); } else { wait_ms(TAP_CODE_DELAY); unregister_code(action.swap.code); - *record = (keyrecord_t){}; // hack: reset tap mode + *record = (keyrecord_t){}; // hack: reset tap mode } } else { if (swap_held && !event.pressed) { - swap_hands = !swap_hands; // undo hold set up in _tap_hint - swap_held = false; + swap_hands = !swap_hands; // undo hold set up in _tap_hint + swap_held = false; } } - #endif +# endif } #endif #ifndef NO_ACTION_FUNCTION @@ -652,10 +654,10 @@ void process_action(keyrecord_t *record, action_t action) switch (action.kind.id) { case ACT_LAYER: case ACT_LAYER_MODS: - #ifndef NO_ACTION_TAPPING +# ifndef NO_ACTION_TAPPING case ACT_LAYER_TAP: case ACT_LAYER_TAP_EXT: - #endif +# endif led_set(host_keyboard_leds()); break; default: @@ -664,35 +666,34 @@ void process_action(keyrecord_t *record, action_t action) #endif #ifndef NO_ACTION_TAPPING - #ifdef RETRO_TAPPING - if (!is_tap_action(action)) { - retro_tapping_counter = 0; - } else { - if (event.pressed) { - if (tap_count > 0) { - retro_tapping_counter = 0; - } else { - - } - } else { - if (tap_count > 0) { +# ifdef RETRO_TAPPING + if (!is_tap_action(action)) { retro_tapping_counter = 0; - } else { - if (retro_tapping_counter == 2) { - tap_code(action.layer_tap.code); + } else { + if (event.pressed) { + if (tap_count > 0) { + retro_tapping_counter = 0; + } else { + } + } else { + if (tap_count > 0) { + retro_tapping_counter = 0; + } else { + if (retro_tapping_counter == 2) { + tap_code(action.layer_tap.code); + } + retro_tapping_counter = 0; + } } - retro_tapping_counter = 0; - } } - } - #endif +# endif #endif #ifndef NO_ACTION_ONESHOT /* Because we switch layers after a oneshot event, we need to release the * key before we leave the layer or no key up event will be generated. */ - if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) { + if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED)) { record->event.pressed = false; layer_on(get_oneshot_layer()); process_record(record); @@ -701,25 +702,20 @@ void process_action(keyrecord_t *record, action_t action) #endif } - - - /** \brief Utilities for actions. (FIXME: Needs better description) * * FIXME: Needs documentation. */ -void register_code(uint8_t code) -{ +void register_code(uint8_t code) { if (code == KC_NO) { return; } - #ifdef LOCKING_SUPPORT_ENABLE else if (KC_LOCKING_CAPS == code) { -#ifdef LOCKING_RESYNC_ENABLE +# ifdef LOCKING_RESYNC_ENABLE // Resync: ignore if caps lock already is on - if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return; -#endif + if (host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK)) return; +# endif add_key(KC_CAPSLOCK); send_keyboard_report(); wait_ms(100); @@ -728,9 +724,9 @@ void register_code(uint8_t code) } else if (KC_LOCKING_NUM == code) { -#ifdef LOCKING_RESYNC_ENABLE - if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return; -#endif +# ifdef LOCKING_RESYNC_ENABLE + if (host_keyboard_leds() & (1 << USB_LED_NUM_LOCK)) return; +# endif add_key(KC_NUMLOCK); send_keyboard_report(); wait_ms(100); @@ -739,9 +735,9 @@ void register_code(uint8_t code) } else if (KC_LOCKING_SCROLL == code) { -#ifdef LOCKING_RESYNC_ENABLE - if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return; -#endif +# ifdef LOCKING_RESYNC_ENABLE + if (host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK)) return; +# endif add_key(KC_SCROLLLOCK); send_keyboard_report(); wait_ms(100); @@ -750,9 +746,10 @@ void register_code(uint8_t code) } #endif - else if IS_KEY(code) { - // TODO: should push command_proc out of this block? - if (command_proc(code)) return; + else if + IS_KEY(code) { + // TODO: should push command_proc out of this block? + if (command_proc(code)) return; #ifndef NO_ACTION_ONESHOT /* TODO: remove @@ -769,46 +766,44 @@ void register_code(uint8_t code) } else */ #endif - { - add_key(code); + { + add_key(code); + send_keyboard_report(); + } + } + else if + IS_MOD(code) { + add_mods(MOD_BIT(code)); send_keyboard_report(); } - } - else if IS_MOD(code) { - add_mods(MOD_BIT(code)); - send_keyboard_report(); - } - else if IS_SYSTEM(code) { - host_system_send(KEYCODE2SYSTEM(code)); - } - else if IS_CONSUMER(code) { - host_consumer_send(KEYCODE2CONSUMER(code)); - } + else if + IS_SYSTEM(code) { host_system_send(KEYCODE2SYSTEM(code)); } + else if + IS_CONSUMER(code) { host_consumer_send(KEYCODE2CONSUMER(code)); } - #ifdef MOUSEKEY_ENABLE - else if IS_MOUSEKEY(code) { - mousekey_on(code); - mousekey_send(); - } - #endif +#ifdef MOUSEKEY_ENABLE + else if + IS_MOUSEKEY(code) { + mousekey_on(code); + mousekey_send(); + } +#endif } /** \brief Utilities for actions. (FIXME: Needs better description) * * FIXME: Needs documentation. */ -void unregister_code(uint8_t code) -{ +void unregister_code(uint8_t code) { if (code == KC_NO) { return; } - #ifdef LOCKING_SUPPORT_ENABLE else if (KC_LOCKING_CAPS == code) { -#ifdef LOCKING_RESYNC_ENABLE +# ifdef LOCKING_RESYNC_ENABLE // Resync: ignore if caps lock already is off - if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return; -#endif + if (!(host_keyboard_leds() & (1 << USB_LED_CAPS_LOCK))) return; +# endif add_key(KC_CAPSLOCK); send_keyboard_report(); del_key(KC_CAPSLOCK); @@ -816,9 +811,9 @@ void unregister_code(uint8_t code) } else if (KC_LOCKING_NUM == code) { -#ifdef LOCKING_RESYNC_ENABLE - if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return; -#endif +# ifdef LOCKING_RESYNC_ENABLE + if (!(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK))) return; +# endif add_key(KC_NUMLOCK); send_keyboard_report(); del_key(KC_NUMLOCK); @@ -826,9 +821,9 @@ void unregister_code(uint8_t code) } else if (KC_LOCKING_SCROLL == code) { -#ifdef LOCKING_RESYNC_ENABLE - if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return; -#endif +# ifdef LOCKING_RESYNC_ENABLE + if (!(host_keyboard_leds() & (1 << USB_LED_SCROLL_LOCK))) return; +# endif add_key(KC_SCROLLLOCK); send_keyboard_report(); del_key(KC_SCROLLLOCK); @@ -836,26 +831,27 @@ void unregister_code(uint8_t code) } #endif - else if IS_KEY(code) { - del_key(code); - send_keyboard_report(); - } - else if IS_MOD(code) { - del_mods(MOD_BIT(code)); - send_keyboard_report(); - } - else if IS_SYSTEM(code) { - host_system_send(0); - } - else if IS_CONSUMER(code) { - host_consumer_send(0); - } - #ifdef MOUSEKEY_ENABLE - else if IS_MOUSEKEY(code) { - mousekey_off(code); - mousekey_send(); - } - #endif + else if + IS_KEY(code) { + del_key(code); + send_keyboard_report(); + } + else if + IS_MOD(code) { + del_mods(MOD_BIT(code)); + send_keyboard_report(); + } + else if + IS_SYSTEM(code) { host_system_send(0); } + else if + IS_CONSUMER(code) { host_consumer_send(0); } +#ifdef MOUSEKEY_ENABLE + else if + IS_MOUSEKEY(code) { + mousekey_off(code); + mousekey_send(); + } +#endif } /** \brief Utilities for actions. (FIXME: Needs better description) @@ -863,21 +859,20 @@ void unregister_code(uint8_t code) * FIXME: Needs documentation. */ void tap_code(uint8_t code) { - register_code(code); - if (code == KC_CAPS) { - wait_ms(TAP_HOLD_CAPS_DELAY); - } else { - wait_ms(TAP_CODE_DELAY); - } - unregister_code(code); + register_code(code); + if (code == KC_CAPS) { + wait_ms(TAP_HOLD_CAPS_DELAY); + } else { + wait_ms(TAP_CODE_DELAY); + } + unregister_code(code); } /** \brief Utilities for actions. (FIXME: Needs better description) * * FIXME: Needs documentation. */ -void register_mods(uint8_t mods) -{ +void register_mods(uint8_t mods) { if (mods) { add_mods(mods); send_keyboard_report(); @@ -888,8 +883,7 @@ void register_mods(uint8_t mods) * * FIXME: Needs documentation. */ -void unregister_mods(uint8_t mods) -{ +void unregister_mods(uint8_t mods) { if (mods) { del_mods(mods); send_keyboard_report(); @@ -900,8 +894,7 @@ void unregister_mods(uint8_t mods) * * FIXME: Needs documentation. */ -void clear_keyboard(void) -{ +void clear_keyboard(void) { clear_mods(); clear_keyboard_but_mods(); } @@ -910,8 +903,7 @@ void clear_keyboard(void) * * FIXME: Needs documentation. */ -void clear_keyboard_but_mods(void) -{ +void clear_keyboard_but_mods(void) { clear_keys(); clear_keyboard_but_mods_and_keys(); } @@ -920,8 +912,7 @@ void clear_keyboard_but_mods(void) * * FIXME: Needs documentation. */ -void clear_keyboard_but_mods_and_keys() -{ +void clear_keyboard_but_mods_and_keys() { clear_weak_mods(); clear_macro_mods(); send_keyboard_report(); @@ -939,8 +930,7 @@ void clear_keyboard_but_mods_and_keys() * * FIXME: Needs documentation. */ -bool is_tap_key(keypos_t key) -{ +bool is_tap_key(keypos_t key) { action_t action = layer_switch_get_action(key); return is_tap_action(action); } @@ -949,8 +939,7 @@ bool is_tap_key(keypos_t key) * * FIXME: Needs documentation. */ -bool is_tap_action(action_t action) -{ +bool is_tap_action(action_t action) { switch (action.kind.id) { case ACT_LMODS_TAP: case ACT_RMODS_TAP: @@ -972,28 +961,25 @@ bool is_tap_action(action_t action) return false; case ACT_MACRO: case ACT_FUNCTION: - if (action.func.opt & FUNC_TAP) { return true; } + if (action.func.opt & FUNC_TAP) { + return true; + } return false; } return false; } - /** \brief Debug print (FIXME: Needs better description) * * FIXME: Needs documentation. */ -void debug_event(keyevent_t event) -{ - dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time); -} +void debug_event(keyevent_t event) { dprintf("%04X%c(%u)", (event.key.row << 8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time); } /** \brief Debug print (FIXME: Needs better description) * * FIXME: Needs documentation. */ -void debug_record(keyrecord_t record) -{ +void debug_record(keyrecord_t record) { debug_event(record.event); #ifndef NO_ACTION_TAPPING dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' ')); @@ -1004,24 +990,53 @@ void debug_record(keyrecord_t record) * * FIXME: Needs documentation. */ -void debug_action(action_t action) -{ +void debug_action(action_t action) { switch (action.kind.id) { - case ACT_LMODS: dprint("ACT_LMODS"); break; - case ACT_RMODS: dprint("ACT_RMODS"); break; - case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break; - case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break; - case ACT_USAGE: dprint("ACT_USAGE"); break; - case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break; - case ACT_LAYER: dprint("ACT_LAYER"); break; - case ACT_LAYER_MODS: dprint("ACT_LAYER_MODS"); break; - case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break; - case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break; - case ACT_MACRO: dprint("ACT_MACRO"); break; - case ACT_COMMAND: dprint("ACT_COMMAND"); break; - case ACT_FUNCTION: dprint("ACT_FUNCTION"); break; - case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break; - default: dprint("UNKNOWN"); break; + case ACT_LMODS: + dprint("ACT_LMODS"); + break; + case ACT_RMODS: + dprint("ACT_RMODS"); + break; + case ACT_LMODS_TAP: + dprint("ACT_LMODS_TAP"); + break; + case ACT_RMODS_TAP: + dprint("ACT_RMODS_TAP"); + break; + case ACT_USAGE: + dprint("ACT_USAGE"); + break; + case ACT_MOUSEKEY: + dprint("ACT_MOUSEKEY"); + break; + case ACT_LAYER: + dprint("ACT_LAYER"); + break; + case ACT_LAYER_MODS: + dprint("ACT_LAYER_MODS"); + break; + case ACT_LAYER_TAP: + dprint("ACT_LAYER_TAP"); + break; + case ACT_LAYER_TAP_EXT: + dprint("ACT_LAYER_TAP_EXT"); + break; + case ACT_MACRO: + dprint("ACT_MACRO"); + break; + case ACT_COMMAND: + dprint("ACT_COMMAND"); + break; + case ACT_FUNCTION: + dprint("ACT_FUNCTION"); + break; + case ACT_SWAP_HANDS: + dprint("ACT_SWAP_HANDS"); + break; + default: + dprint("UNKNOWN"); + break; } - dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff); + dprintf("[%X:%02X]", action.kind.param >> 8, action.kind.param & 0xff); } diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h index 799e3bb0e..633cedb06 100644 --- a/tmk_core/common/action.h +++ b/tmk_core/common/action.h @@ -24,23 +24,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "action_code.h" #include "action_macro.h" - #ifdef __cplusplus extern "C" { #endif /* tapping count and state */ typedef struct { - bool interrupted :1; - bool reserved2 :1; - bool reserved1 :1; - bool reserved0 :1; - uint8_t count :4; + bool interrupted : 1; + bool reserved2 : 1; + bool reserved1 : 1; + bool reserved0 : 1; + uint8_t count : 4; } tap_t; /* Key event container for recording */ typedef struct { - keyevent_t event; + keyevent_t event; #ifndef NO_ACTION_TAPPING tap_t tap; #endif @@ -68,17 +67,17 @@ extern bool disable_action_cache; /* Code for handling one-handed key modifiers. */ #ifdef SWAP_HANDS_ENABLE -extern bool swap_hands; +extern bool swap_hands; extern const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS]; -#if (MATRIX_COLS <= 8) -typedef uint8_t swap_state_row_t; -#elif (MATRIX_COLS <= 16) -typedef uint16_t swap_state_row_t; -#elif (MATRIX_COLS <= 32) -typedef uint32_t swap_state_row_t; -#else -#error "MATRIX_COLS: invalid value" -#endif +# if (MATRIX_COLS <= 8) +typedef uint8_t swap_state_row_t; +# elif (MATRIX_COLS <= 16) +typedef uint16_t swap_state_row_t; +# elif (MATRIX_COLS <= 32) +typedef uint32_t swap_state_row_t; +# else +# error "MATRIX_COLS: invalid value" +# endif void process_hand_swap(keyevent_t *record); #endif @@ -91,7 +90,7 @@ void unregister_code(uint8_t code); void tap_code(uint8_t code); void register_mods(uint8_t mods); void unregister_mods(uint8_t mods); -//void set_mods(uint8_t mods); +// void set_mods(uint8_t mods); void clear_keyboard(void); void clear_keyboard_but_mods(void); void clear_keyboard_but_mods_and_keys(void); @@ -112,4 +111,4 @@ void debug_action(action_t action); } #endif -#endif /* ACTION_H */ +#endif /* ACTION_H */ diff --git a/tmk_core/common/action_code.h b/tmk_core/common/action_code.h index 5b5d0e566..03fcb3382 100644 --- a/tmk_core/common/action_code.h +++ b/tmk_core/common/action_code.h @@ -98,30 +98,29 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ enum action_kind_id { /* Key Actions */ - ACT_MODS = 0b0000, - ACT_LMODS = 0b0000, - ACT_RMODS = 0b0001, - ACT_MODS_TAP = 0b0010, - ACT_LMODS_TAP = 0b0010, - ACT_RMODS_TAP = 0b0011, + ACT_MODS = 0b0000, + ACT_LMODS = 0b0000, + ACT_RMODS = 0b0001, + ACT_MODS_TAP = 0b0010, + ACT_LMODS_TAP = 0b0010, + ACT_RMODS_TAP = 0b0011, /* Other Keys */ - ACT_USAGE = 0b0100, - ACT_MOUSEKEY = 0b0101, + ACT_USAGE = 0b0100, + ACT_MOUSEKEY = 0b0101, /* One-hand Support */ - ACT_SWAP_HANDS = 0b0110, + ACT_SWAP_HANDS = 0b0110, /* Layer Actions */ - ACT_LAYER = 0b1000, - ACT_LAYER_MODS = 0b1001, - ACT_LAYER_TAP = 0b1010, /* Layer 0-15 */ - ACT_LAYER_TAP_EXT = 0b1011, /* Layer 16-31 */ + ACT_LAYER = 0b1000, + ACT_LAYER_MODS = 0b1001, + ACT_LAYER_TAP = 0b1010, /* Layer 0-15 */ + ACT_LAYER_TAP_EXT = 0b1011, /* Layer 16-31 */ /* Extensions */ - ACT_MACRO = 0b1100, - ACT_BACKLIGHT = 0b1101, - ACT_COMMAND = 0b1110, - ACT_FUNCTION = 0b1111 + ACT_MACRO = 0b1100, + ACT_BACKLIGHT = 0b1101, + ACT_COMMAND = 0b1110, + ACT_FUNCTION = 0b1111 }; - /** \brief Action Code Struct * * NOTE: @@ -139,66 +138,63 @@ enum action_kind_id { typedef union { uint16_t code; struct action_kind { - uint16_t param :12; - uint8_t id :4; + uint16_t param : 12; + uint8_t id : 4; } kind; struct action_key { - uint8_t code :8; - uint8_t mods :4; - uint8_t kind :4; + uint8_t code : 8; + uint8_t mods : 4; + uint8_t kind : 4; } key; struct action_layer_bitop { - uint8_t bits :4; - uint8_t xbit :1; - uint8_t part :3; - uint8_t on :2; - uint8_t op :2; - uint8_t kind :4; + uint8_t bits : 4; + uint8_t xbit : 1; + uint8_t part : 3; + uint8_t on : 2; + uint8_t op : 2; + uint8_t kind : 4; } layer_bitop; - struct action_layer_mods - { - uint8_t mods :8; - uint8_t layer :4; - uint8_t kind :4; + struct action_layer_mods { + uint8_t mods : 8; + uint8_t layer : 4; + uint8_t kind : 4; } layer_mods; struct action_layer_tap { - uint8_t code :8; - uint8_t val :5; - uint8_t kind :3; + uint8_t code : 8; + uint8_t val : 5; + uint8_t kind : 3; } layer_tap; struct action_usage { - uint16_t code :10; - uint8_t page :2; - uint8_t kind :4; + uint16_t code : 10; + uint8_t page : 2; + uint8_t kind : 4; } usage; struct action_backlight { - uint8_t level :8; - uint8_t opt :4; - uint8_t kind :4; + uint8_t level : 8; + uint8_t opt : 4; + uint8_t kind : 4; } backlight; struct action_command { - uint8_t id :8; - uint8_t opt :4; - uint8_t kind :4; + uint8_t id : 8; + uint8_t opt : 4; + uint8_t kind : 4; } command; struct action_function { - uint8_t id :8; - uint8_t opt :4; - uint8_t kind :4; + uint8_t id : 8; + uint8_t opt : 4; + uint8_t kind : 4; } func; struct action_swap { - uint8_t code :8; - uint8_t opt :4; - uint8_t kind :4; + uint8_t code : 8; + uint8_t opt : 4; + uint8_t kind : 4; } swap; } action_t; - /* action utility */ -#define ACTION_NO 0 -#define ACTION_TRANSPARENT 1 -#define ACTION(kind, param) ((kind)<<12 | (param)) - +#define ACTION_NO 0 +#define ACTION_TRANSPARENT 1 +#define ACTION(kind, param) ((kind) << 12 | (param)) /** \brief Key Actions * @@ -220,35 +216,29 @@ enum mods_bit { MOD_RGUI = 0x18, }; enum mods_codes { - MODS_ONESHOT = 0x00, + MODS_ONESHOT = 0x00, MODS_TAP_TOGGLE = 0x01, }; -#define ACTION_KEY(key) ACTION(ACT_MODS, (key)) -#define ACTION_MODS(mods) ACTION(ACT_MODS, ((mods)&0x1f)<<8 | 0) -#define ACTION_MODS_KEY(mods, key) ACTION(ACT_MODS, ((mods)&0x1f)<<8 | (key)) -#define ACTION_MODS_TAP_KEY(mods, key) ACTION(ACT_MODS_TAP, ((mods)&0x1f)<<8 | (key)) -#define ACTION_MODS_ONESHOT(mods) ACTION(ACT_MODS_TAP, ((mods)&0x1f)<<8 | MODS_ONESHOT) -#define ACTION_MODS_TAP_TOGGLE(mods) ACTION(ACT_MODS_TAP, ((mods)&0x1f)<<8 | MODS_TAP_TOGGLE) - +#define ACTION_KEY(key) ACTION(ACT_MODS, (key)) +#define ACTION_MODS(mods) ACTION(ACT_MODS, ((mods)&0x1f) << 8 | 0) +#define ACTION_MODS_KEY(mods, key) ACTION(ACT_MODS, ((mods)&0x1f) << 8 | (key)) +#define ACTION_MODS_TAP_KEY(mods, key) ACTION(ACT_MODS_TAP, ((mods)&0x1f) << 8 | (key)) +#define ACTION_MODS_ONESHOT(mods) ACTION(ACT_MODS_TAP, ((mods)&0x1f) << 8 | MODS_ONESHOT) +#define ACTION_MODS_TAP_TOGGLE(mods) ACTION(ACT_MODS_TAP, ((mods)&0x1f) << 8 | MODS_TAP_TOGGLE) /** \brief Other Keys */ -enum usage_pages { - PAGE_SYSTEM, - PAGE_CONSUMER -}; -#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id)) -#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id)) -#define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) - - +enum usage_pages { PAGE_SYSTEM, PAGE_CONSUMER }; +#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM << 10 | (id)) +#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER << 10 | (id)) +#define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key) /** \brief Layer Actions */ enum layer_param_on { - ON_PRESS = 1, - ON_RELEASE = 2, - ON_BOTH = 3, + ON_PRESS = 1, + ON_RELEASE = 2, + ON_BOTH = 3, }; /** \brief Layer Actions @@ -269,37 +259,36 @@ enum layer_param_tap_op { OP_SET_CLEAR, OP_ONESHOT, }; -#define ACTION_LAYER_BITOP(op, part, bits, on) ACTION(ACT_LAYER, (op)<<10 | (on)<<8 | (part)<<5 | ((bits)&0x1f)) -#define ACTION_LAYER_TAP(layer, key) ACTION(ACT_LAYER_TAP, (layer)<<8 | (key)) +#define ACTION_LAYER_BITOP(op, part, bits, on) ACTION(ACT_LAYER, (op) << 10 | (on) << 8 | (part) << 5 | ((bits)&0x1f)) +#define ACTION_LAYER_TAP(layer, key) ACTION(ACT_LAYER_TAP, (layer) << 8 | (key)) /* Default Layer */ -#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_BIT_SET((layer)/4, 1<<((layer)%4)) +#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_BIT_SET((layer) / 4, 1 << ((layer) % 4)) /* Layer Operation */ -#define ACTION_LAYER_CLEAR(on) ACTION_LAYER_BIT_AND(0, 0, (on)) -#define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer) -#define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INVERT(layer, ON_RELEASE) -#define ACTION_LAYER_INVERT(layer, on) ACTION_LAYER_BIT_XOR((layer)/4, 1<<((layer)%4), (on)) -#define ACTION_LAYER_ON(layer, on) ACTION_LAYER_BIT_OR( (layer)/4, 1<<((layer)%4), (on)) -#define ACTION_LAYER_OFF(layer, on) ACTION_LAYER_BIT_AND((layer)/4, ~(1<<((layer)%4)), (on)) -#define ACTION_LAYER_SET(layer, on) ACTION_LAYER_BIT_SET((layer)/4, 1<<((layer)%4), (on)) -#define ACTION_LAYER_ON_OFF(layer) ACTION_LAYER_TAP((layer), OP_ON_OFF) -#define ACTION_LAYER_OFF_ON(layer) ACTION_LAYER_TAP((layer), OP_OFF_ON) -#define ACTION_LAYER_SET_CLEAR(layer) ACTION_LAYER_TAP((layer), OP_SET_CLEAR) -#define ACTION_LAYER_ONESHOT(layer) ACTION_LAYER_TAP((layer), OP_ONESHOT) -#define ACTION_LAYER_MODS(layer, mods) ACTION(ACT_LAYER_MODS, (layer) << 8 | (mods)) +#define ACTION_LAYER_CLEAR(on) ACTION_LAYER_BIT_AND(0, 0, (on)) +#define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer) +#define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INVERT(layer, ON_RELEASE) +#define ACTION_LAYER_INVERT(layer, on) ACTION_LAYER_BIT_XOR((layer) / 4, 1 << ((layer) % 4), (on)) +#define ACTION_LAYER_ON(layer, on) ACTION_LAYER_BIT_OR((layer) / 4, 1 << ((layer) % 4), (on)) +#define ACTION_LAYER_OFF(layer, on) ACTION_LAYER_BIT_AND((layer) / 4, ~(1 << ((layer) % 4)), (on)) +#define ACTION_LAYER_SET(layer, on) ACTION_LAYER_BIT_SET((layer) / 4, 1 << ((layer) % 4), (on)) +#define ACTION_LAYER_ON_OFF(layer) ACTION_LAYER_TAP((layer), OP_ON_OFF) +#define ACTION_LAYER_OFF_ON(layer) ACTION_LAYER_TAP((layer), OP_OFF_ON) +#define ACTION_LAYER_SET_CLEAR(layer) ACTION_LAYER_TAP((layer), OP_SET_CLEAR) +#define ACTION_LAYER_ONESHOT(layer) ACTION_LAYER_TAP((layer), OP_ONESHOT) +#define ACTION_LAYER_MODS(layer, mods) ACTION(ACT_LAYER_MODS, (layer) << 8 | (mods)) /* With Tapping */ -#define ACTION_LAYER_TAP_KEY(layer, key) ACTION_LAYER_TAP((layer), (key)) -#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION_LAYER_TAP((layer), OP_TAP_TOGGLE) +#define ACTION_LAYER_TAP_KEY(layer, key) ACTION_LAYER_TAP((layer), (key)) +#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION_LAYER_TAP((layer), OP_TAP_TOGGLE) /* Bitwise Operation */ -#define ACTION_LAYER_BIT_AND(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), (on)) -#define ACTION_LAYER_BIT_OR( part, bits, on) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), (on)) -#define ACTION_LAYER_BIT_XOR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), (on)) -#define ACTION_LAYER_BIT_SET(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), (on)) +#define ACTION_LAYER_BIT_AND(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), (on)) +#define ACTION_LAYER_BIT_OR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), (on)) +#define ACTION_LAYER_BIT_XOR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), (on)) +#define ACTION_LAYER_BIT_SET(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), (on)) /* Default Layer Bitwise Operation */ -#define ACTION_DEFAULT_LAYER_BIT_AND(part, bits) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), 0) -#define ACTION_DEFAULT_LAYER_BIT_OR( part, bits) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), 0) -#define ACTION_DEFAULT_LAYER_BIT_XOR(part, bits) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), 0) -#define ACTION_DEFAULT_LAYER_BIT_SET(part, bits) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), 0) - +#define ACTION_DEFAULT_LAYER_BIT_AND(part, bits) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), 0) +#define ACTION_DEFAULT_LAYER_BIT_OR(part, bits) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), 0) +#define ACTION_DEFAULT_LAYER_BIT_XOR(part, bits) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), 0) +#define ACTION_DEFAULT_LAYER_BIT_SET(part, bits) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), 0) /** \brief Extensions */ @@ -313,25 +302,25 @@ enum backlight_opt { }; /* Macro */ -#define ACTION_MACRO(id) ACTION(ACT_MACRO, (id)) -#define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id)) -#define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id)) +#define ACTION_MACRO(id) ACTION(ACT_MACRO, (id)) +#define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP << 8 | (id)) +#define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt) << 8 | (id)) /* Backlight */ -#define ACTION_BACKLIGHT_INCREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_INCREASE << 8) -#define ACTION_BACKLIGHT_DECREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_DECREASE << 8) -#define ACTION_BACKLIGHT_TOGGLE() ACTION(ACT_BACKLIGHT, BACKLIGHT_TOGGLE << 8) -#define ACTION_BACKLIGHT_STEP() ACTION(ACT_BACKLIGHT, BACKLIGHT_STEP << 8) -#define ACTION_BACKLIGHT_ON() ACTION(ACT_BACKLIGHT, BACKLIGHT_ON << 8) -#define ACTION_BACKLIGHT_OFF() ACTION(ACT_BACKLIGHT, BACKLIGHT_OFF << 8) +#define ACTION_BACKLIGHT_INCREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_INCREASE << 8) +#define ACTION_BACKLIGHT_DECREASE() ACTION(ACT_BACKLIGHT, BACKLIGHT_DECREASE << 8) +#define ACTION_BACKLIGHT_TOGGLE() ACTION(ACT_BACKLIGHT, BACKLIGHT_TOGGLE << 8) +#define ACTION_BACKLIGHT_STEP() ACTION(ACT_BACKLIGHT, BACKLIGHT_STEP << 8) +#define ACTION_BACKLIGHT_ON() ACTION(ACT_BACKLIGHT, BACKLIGHT_ON << 8) +#define ACTION_BACKLIGHT_OFF() ACTION(ACT_BACKLIGHT, BACKLIGHT_OFF << 8) /* Command */ -#define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (id)) +#define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt) << 8 | (id)) /* Function */ enum function_opts { - FUNC_TAP = 0x8, /* indciates function is tappable */ + FUNC_TAP = 0x8, /* indciates function is tappable */ }; -#define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id)) -#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id)) -#define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id)) +#define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id)) +#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP << 8 | (id)) +#define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt) << 8 | (id)) /* OneHand Support */ enum swap_hands_param_tap_op { OP_SH_TOGGLE = 0xF0, @@ -342,13 +331,13 @@ enum swap_hands_param_tap_op { OP_SH_ON, }; -#define ACTION_SWAP_HANDS() ACTION_SWAP_HANDS_ON_OFF() -#define ACTION_SWAP_HANDS_TOGGLE() ACTION(ACT_SWAP_HANDS, OP_SH_TOGGLE) -#define ACTION_SWAP_HANDS_TAP_TOGGLE() ACTION(ACT_SWAP_HANDS, OP_SH_TAP_TOGGLE) -#define ACTION_SWAP_HANDS_TAP_KEY(key) ACTION(ACT_SWAP_HANDS, key) -#define ACTION_SWAP_HANDS_ON_OFF() ACTION(ACT_SWAP_HANDS, OP_SH_ON_OFF) -#define ACTION_SWAP_HANDS_OFF_ON() ACTION(ACT_SWAP_HANDS, OP_SH_OFF_ON) -#define ACTION_SWAP_HANDS_ON() ACTION(ACT_SWAP_HANDS, OP_SH_ON) -#define ACTION_SWAP_HANDS_OFF() ACTION(ACT_SWAP_HANDS, OP_SH_OFF) +#define ACTION_SWAP_HANDS() ACTION_SWAP_HANDS_ON_OFF() +#define ACTION_SWAP_HANDS_TOGGLE() ACTION(ACT_SWAP_HANDS, OP_SH_TOGGLE) +#define ACTION_SWAP_HANDS_TAP_TOGGLE() ACTION(ACT_SWAP_HANDS, OP_SH_TAP_TOGGLE) +#define ACTION_SWAP_HANDS_TAP_KEY(key) ACTION(ACT_SWAP_HANDS, key) +#define ACTION_SWAP_HANDS_ON_OFF() ACTION(ACT_SWAP_HANDS, OP_SH_ON_OFF) +#define ACTION_SWAP_HANDS_OFF_ON() ACTION(ACT_SWAP_HANDS, OP_SH_OFF_ON) +#define ACTION_SWAP_HANDS_ON() ACTION(ACT_SWAP_HANDS, OP_SH_ON) +#define ACTION_SWAP_HANDS_OFF() ACTION(ACT_SWAP_HANDS, OP_SH_OFF) #endif /* ACTION_CODE_H */ diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c index dfcbc110a..07d78c56d 100644 --- a/tmk_core/common/action_layer.c +++ b/tmk_core/common/action_layer.c @@ -5,12 +5,11 @@ #include "action_layer.h" #ifdef DEBUG_ACTION -#include "debug.h" +# include "debug.h" #else -#include "nodebug.h" +# include "nodebug.h" #endif - /** \brief Default Layer State */ layer_state_t default_layer_state = 0; @@ -19,34 +18,30 @@ layer_state_t default_layer_state = 0; * * Run user code on default layer state change */ -__attribute__((weak)) -layer_state_t default_layer_state_set_user(layer_state_t state) { - return state; -} +__attribute__((weak)) layer_state_t default_layer_state_set_user(layer_state_t state) { return state; } /** \brief Default Layer State Set At Keyboard Level * * Run keyboard code on default layer state change */ -__attribute__((weak)) -layer_state_t default_layer_state_set_kb(layer_state_t state) { - return default_layer_state_set_user(state); -} +__attribute__((weak)) layer_state_t default_layer_state_set_kb(layer_state_t state) { return default_layer_state_set_user(state); } /** \brief Default Layer State Set * * Static function to set the default layer state, prints debug info and clears keys */ static void default_layer_state_set(layer_state_t state) { - state = default_layer_state_set_kb(state); - debug("default_layer_state: "); - default_layer_debug(); debug(" to "); - default_layer_state = state; - default_layer_debug(); debug("\n"); + state = default_layer_state_set_kb(state); + debug("default_layer_state: "); + default_layer_debug(); + debug(" to "); + default_layer_state = state; + default_layer_debug(); + debug("\n"); #ifdef STRICT_LAYER_RELEASE - clear_keyboard_but_mods(); // To avoid stuck keys + clear_keyboard_but_mods(); // To avoid stuck keys #else - clear_keyboard_but_mods_and_keys(); // Don't reset held keys + clear_keyboard_but_mods_and_keys(); // Don't reset held keys #endif } @@ -54,43 +49,32 @@ static void default_layer_state_set(layer_state_t state) { * * Print out the hex value of the 32-bit default layer state, as well as the value of the highest bit. */ -void default_layer_debug(void) { - dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state)); -} +void default_layer_debug(void) { dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state)); } /** \brief Default Layer Set * * Sets the default layer state. */ -void default_layer_set(layer_state_t state) { - default_layer_state_set(state); -} +void default_layer_set(layer_state_t state) { default_layer_state_set(state); } #ifndef NO_ACTION_LAYER /** \brief Default Layer Or * * Turns on the default layer based on matching bits between specifed layer and existing layer state */ -void default_layer_or(layer_state_t state) { - default_layer_state_set(default_layer_state | state); -} +void default_layer_or(layer_state_t state) { default_layer_state_set(default_layer_state | state); } /** \brief Default Layer And * * Turns on default layer based on matching enabled bits between specifed layer and existing layer state */ -void default_layer_and(layer_state_t state) { - default_layer_state_set(default_layer_state & state); -} +void default_layer_and(layer_state_t state) { default_layer_state_set(default_layer_state & state); } /** \brief Default Layer Xor * * Turns on default layer based on non-matching bits between specifed layer and existing layer state */ -void default_layer_xor(layer_state_t state) { - default_layer_state_set(default_layer_state ^ state); -} +void default_layer_xor(layer_state_t state) { default_layer_state_set(default_layer_state ^ state); } #endif - #ifndef NO_ACTION_LAYER /** \brief Keymap Layer State */ @@ -100,123 +84,101 @@ layer_state_t layer_state = 0; * * Runs user code on layer state change */ -__attribute__((weak)) -layer_state_t layer_state_set_user(layer_state_t state) { - return state; -} +__attribute__((weak)) layer_state_t layer_state_set_user(layer_state_t state) { return state; } /** \brief Layer state set keyboard * * Runs keyboard code on layer state change */ -__attribute__((weak)) -layer_state_t layer_state_set_kb(layer_state_t state) { - return layer_state_set_user(state); -} +__attribute__((weak)) layer_state_t layer_state_set_kb(layer_state_t state) { return layer_state_set_user(state); } /** \brief Layer state set * * Sets the layer to match the specifed state (a bitmask) */ void layer_state_set(layer_state_t state) { - state = layer_state_set_kb(state); - dprint("layer_state: "); - layer_debug(); dprint(" to "); - layer_state = state; - layer_debug(); dprintln(); -#ifdef STRICT_LAYER_RELEASE - clear_keyboard_but_mods(); // To avoid stuck keys -#else - clear_keyboard_but_mods_and_keys(); // Don't reset held keys -#endif + state = layer_state_set_kb(state); + dprint("layer_state: "); + layer_debug(); + dprint(" to "); + layer_state = state; + layer_debug(); + dprintln(); +# ifdef STRICT_LAYER_RELEASE + clear_keyboard_but_mods(); // To avoid stuck keys +# else + clear_keyboard_but_mods_and_keys(); // Don't reset held keys +# endif } /** \brief Layer clear * * Turn off all layers */ -void layer_clear(void) { - layer_state_set(0); -} +void layer_clear(void) { layer_state_set(0); } /** \brief Layer state is * * Return whether the given state is on (it might still be shadowed by a higher state, though) */ -bool layer_state_is(uint8_t layer) { - return layer_state_cmp(layer_state, layer); -} +bool layer_state_is(uint8_t layer) { return layer_state_cmp(layer_state, layer); } /** \brief Layer state compare * * Used for comparing layers {mostly used for unit testing} */ bool layer_state_cmp(layer_state_t cmp_layer_state, uint8_t layer) { - if (!cmp_layer_state) { return layer == 0; } - return (cmp_layer_state & (1UL<<layer)) != 0; + if (!cmp_layer_state) { + return layer == 0; + } + return (cmp_layer_state & (1UL << layer)) != 0; } /** \brief Layer move * * Turns on the given layer and turn off all other layers */ -void layer_move(uint8_t layer) { - layer_state_set(1UL<<layer); -} +void layer_move(uint8_t layer) { layer_state_set(1UL << layer); } /** \brief Layer on * * Turns on given layer */ -void layer_on(uint8_t layer) { - layer_state_set(layer_state | (1UL<<layer)); -} +void layer_on(uint8_t layer) { layer_state_set(layer_state | (1UL << layer)); } /** \brief Layer off * * Turns off given layer */ -void layer_off(uint8_t layer) { - layer_state_set(layer_state & ~(1UL<<layer)); -} +void layer_off(uint8_t layer) { layer_state_set(layer_state & ~(1UL << layer)); } /** \brief Layer invert * * Toggle the given layer (set it if it's unset, or unset it if it's set) */ -void layer_invert(uint8_t layer) { - layer_state_set(layer_state ^ (1UL<<layer)); -} +void layer_invert(uint8_t layer) { layer_state_set(layer_state ^ (1UL << layer)); } /** \brief Layer or * * Turns on layers based on matching bits between specifed layer and existing layer state */ -void layer_or(layer_state_t state) { - layer_state_set(layer_state | state); -} +void layer_or(layer_state_t state) { layer_state_set(layer_state | state); } /** \brief Layer and * * Turns on layers based on matching enabled bits between specifed layer and existing layer state */ -void layer_and(layer_state_t state) { - layer_state_set(layer_state & state); -} +void layer_and(layer_state_t state) { layer_state_set(layer_state & state); } /** \brief Layer xor * * Turns on layers based on non-matching bits between specifed layer and existing layer state */ -void layer_xor(layer_state_t state) { - layer_state_set(layer_state ^ state); -} +void layer_xor(layer_state_t state) { layer_state_set(layer_state ^ state); } /** \brief Layer debug printing * * Print out the hex value of the 32-bit layer state, as well as the value of the highest bit. */ -void layer_debug(void) { - dprintf("%08lX(%u)", layer_state, biton32(layer_state)); -} +void layer_debug(void) { dprintf("%08lX(%u)", layer_state, biton32(layer_state)); } #endif #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) @@ -230,16 +192,13 @@ uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] * Updates the cached keys when changing layers */ void update_source_layers_cache(keypos_t key, uint8_t layer) { - const uint8_t key_number = key.col + (key.row * MATRIX_COLS); - const uint8_t storage_row = key_number / 8; - const uint8_t storage_bit = key_number % 8; - - for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { - source_layers_cache[storage_row][bit_number] ^= - (-((layer & (1U << bit_number)) != 0) - ^ source_layers_cache[storage_row][bit_number]) - & (1U << storage_bit); - } + const uint8_t key_number = key.col + (key.row * MATRIX_COLS); + const uint8_t storage_row = key_number / 8; + const uint8_t storage_bit = key_number % 8; + + for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { + source_layers_cache[storage_row][bit_number] ^= (-((layer & (1U << bit_number)) != 0) ^ source_layers_cache[storage_row][bit_number]) & (1U << storage_bit); + } } /** \brief read source layers cache @@ -247,19 +206,16 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { * reads the cached keys stored when the layer was changed */ uint8_t read_source_layers_cache(keypos_t key) { - const uint8_t key_number = key.col + (key.row * MATRIX_COLS); - const uint8_t storage_row = key_number / 8; - const uint8_t storage_bit = key_number % 8; - uint8_t layer = 0; - - for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { - layer |= - ((source_layers_cache[storage_row][bit_number] - & (1U << storage_bit)) != 0) - << bit_number; - } - - return layer; + const uint8_t key_number = key.col + (key.row * MATRIX_COLS); + const uint8_t storage_row = key_number / 8; + const uint8_t storage_bit = key_number % 8; + uint8_t layer = 0; + + for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) { + layer |= ((source_layers_cache[storage_row][bit_number] & (1U << storage_bit)) != 0) << bit_number; + } + + return layer; } #endif @@ -272,49 +228,47 @@ uint8_t read_source_layers_cache(keypos_t key) { */ action_t store_or_get_action(bool pressed, keypos_t key) { #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) - if (disable_action_cache) { - return layer_switch_get_action(key); - } - - uint8_t layer; - - if (pressed) { - layer = layer_switch_get_layer(key); - update_source_layers_cache(key, layer); - } - else { - layer = read_source_layers_cache(key); - } - return action_for_key(layer, key); + if (disable_action_cache) { + return layer_switch_get_action(key); + } + + uint8_t layer; + + if (pressed) { + layer = layer_switch_get_layer(key); + update_source_layers_cache(key, layer); + } else { + layer = read_source_layers_cache(key); + } + return action_for_key(layer, key); #else - return layer_switch_get_action(key); + return layer_switch_get_action(key); #endif } - /** \brief Layer switch get layer * * Gets the layer based on key info */ uint8_t layer_switch_get_layer(keypos_t key) { #ifndef NO_ACTION_LAYER - action_t action; - action.code = ACTION_TRANSPARENT; - - layer_state_t layers = layer_state | default_layer_state; - /* check top layer first */ - for (int8_t i = sizeof(layer_state_t) * 8 - 1; i >= 0; i--) { - if (layers & (1UL << i)) { - action = action_for_key(i, key); - if (action.code != ACTION_TRANSPARENT) { - return i; - } + action_t action; + action.code = ACTION_TRANSPARENT; + + layer_state_t layers = layer_state | default_layer_state; + /* check top layer first */ + for (int8_t i = sizeof(layer_state_t) * 8 - 1; i >= 0; i--) { + if (layers & (1UL << i)) { + action = action_for_key(i, key); + if (action.code != ACTION_TRANSPARENT) { + return i; + } + } } - } - /* fall back to layer 0 */ - return 0; + /* fall back to layer 0 */ + return 0; #else - return biton32(default_layer_state); + return biton32(default_layer_state); #endif } @@ -322,6 +276,4 @@ uint8_t layer_switch_get_layer(keypos_t key) { * * Gets action code based on key position */ -action_t layer_switch_get_action(keypos_t key) { - return action_for_key(layer_switch_get_layer(key), key); -} +action_t layer_switch_get_action(keypos_t key) { return action_for_key(layer_switch_get_layer(key), key); } diff --git a/tmk_core/common/action_layer.h b/tmk_core/common/action_layer.h index a2734a29e..fee9b244d 100644 --- a/tmk_core/common/action_layer.h +++ b/tmk_core/common/action_layer.h @@ -23,27 +23,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #if defined(LAYER_STATE_8BIT) typedef uint8_t layer_state_t; -#define get_highest_layer(state) biton8(state) +# define get_highest_layer(state) biton8(state) #elif defined(LAYER_STATE_16BIT) typedef uint16_t layer_state_t; -#define get_highest_layer(state) biton16(state) +# define get_highest_layer(state) biton16(state) #else typedef uint32_t layer_state_t; -#define get_highest_layer(state) biton32(state) +# define get_highest_layer(state) biton32(state) #endif - /* * Default Layer */ extern layer_state_t default_layer_state; -void default_layer_debug(void); -void default_layer_set(layer_state_t state); +void default_layer_debug(void); +void default_layer_set(layer_state_t state); -__attribute__((weak)) -layer_state_t default_layer_state_set_kb(layer_state_t state); -__attribute__((weak)) -layer_state_t default_layer_state_set_user(layer_state_t state); +__attribute__((weak)) layer_state_t default_layer_state_set_kb(layer_state_t state); +__attribute__((weak)) layer_state_t default_layer_state_set_user(layer_state_t state); #ifndef NO_ACTION_LAYER /* bitwise operation */ @@ -51,12 +48,11 @@ void default_layer_or(layer_state_t state); void default_layer_and(layer_state_t state); void default_layer_xor(layer_state_t state); #else -#define default_layer_or(state) -#define default_layer_and(state) -#define default_layer_xor(state) +# define default_layer_or(state) +# define default_layer_and(state) +# define default_layer_xor(state) #endif - /* * Keymap Layer */ @@ -78,21 +74,21 @@ void layer_or(layer_state_t state); void layer_and(layer_state_t state); void layer_xor(layer_state_t state); #else -#define layer_state 0 - -#define layer_state_set(layer) -#define layer_state_is(layer) (layer == 0) -#define layer_state_cmp(state, layer) (state == 0 ? layer == 0 : (state & 1UL << layer) != 0) - -#define layer_debug() -#define layer_clear() -#define layer_move(layer) -#define layer_on(layer) -#define layer_off(layer) -#define layer_invert(layer) -#define layer_or(state) -#define layer_and(state) -#define layer_xor(state) +# define layer_state 0 + +# define layer_state_set(layer) +# define layer_state_is(layer) (layer == 0) +# define layer_state_cmp(state, layer) (state == 0 ? layer == 0 : (state & 1UL << layer) != 0) + +# define layer_debug() +# define layer_clear() +# define layer_move(layer) +# define layer_on(layer) +# define layer_off(layer) +# define layer_invert(layer) +# define layer_or(state) +# define layer_and(state) +# define layer_xor(state) #endif layer_state_t layer_state_set_user(layer_state_t state); @@ -101,8 +97,8 @@ layer_state_t layer_state_set_kb(layer_state_t state); /* pressed actions cache */ #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) /* The number of bits needed to represent the layer number: log2(32). */ -#define MAX_LAYER_BITS 5 -void update_source_layers_cache(keypos_t key, uint8_t layer); +# define MAX_LAYER_BITS 5 +void update_source_layers_cache(keypos_t key, uint8_t layer); uint8_t read_source_layers_cache(keypos_t key); #endif action_t store_or_get_action(bool pressed, keypos_t key); diff --git a/tmk_core/common/action_macro.c b/tmk_core/common/action_macro.c index 12bef2958..92228c0ba 100644 --- a/tmk_core/common/action_macro.c +++ b/tmk_core/common/action_macro.c @@ -20,22 +20,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "wait.h" #ifdef DEBUG_ACTION -#include "debug.h" +# include "debug.h" #else -#include "nodebug.h" +# include "nodebug.h" #endif - #ifndef NO_ACTION_MACRO -#define MACRO_READ() (macro = MACRO_GET(macro_p++)) +# define MACRO_READ() (macro = MACRO_GET(macro_p++)) /** \brief Action Macro Play * * FIXME: Needs doc */ -void action_macro_play(const macro_t *macro_p) -{ - macro_t macro = END; +void action_macro_play(const macro_t *macro_p) { + macro_t macro = END; uint8_t interval = 0; if (!macro_p) return; @@ -64,7 +62,10 @@ void action_macro_play(const macro_t *macro_p) case WAIT: MACRO_READ(); dprintf("WAIT(%u)\n", macro); - { uint8_t ms = macro; while (ms--) wait_ms(1); } + { + uint8_t ms = macro; + while (ms--) wait_ms(1); + } break; case INTERVAL: interval = MACRO_READ(); @@ -76,14 +77,17 @@ void action_macro_play(const macro_t *macro_p) break; case 0x84 ... 0xF3: dprintf("UP(%02X)\n", macro); - unregister_code(macro&0x7F); + unregister_code(macro & 0x7F); break; case END: default: return; } // interval - { uint8_t ms = interval; while (ms--) wait_ms(1); } + { + uint8_t ms = interval; + while (ms--) wait_ms(1); + } } } #endif diff --git a/tmk_core/common/action_macro.h b/tmk_core/common/action_macro.h index f373f5068..21004ead6 100644 --- a/tmk_core/common/action_macro.h +++ b/tmk_core/common/action_macro.h @@ -19,18 +19,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include "progmem.h" - - typedef uint8_t macro_t; -#define MACRO_NONE (macro_t*)0 -#define MACRO(...) ({ static const macro_t __m[] PROGMEM = { __VA_ARGS__ }; &__m[0]; }) -#define MACRO_GET(p) pgm_read_byte(p) +#define MACRO_NONE (macro_t *)0 +#define MACRO(...) \ + ({ \ + static const macro_t __m[] PROGMEM = {__VA_ARGS__}; \ + &__m[0]; \ + }) +#define MACRO_GET(p) pgm_read_byte(p) // Sends press when the macro key is pressed, release when release, or tap_macro when the key has been tapped -#define MACRO_TAP_HOLD(record, press, release, tap_macro) ( ((record)->event.pressed) ? \ - ( ((record)->tap.count <= 0 || (record)->tap.interrupted) ? (press) : MACRO_NONE ) : \ - ( ((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (tap_macro) : (release) ) ) +#define MACRO_TAP_HOLD(record, press, release, tap_macro) (((record)->event.pressed) ? (((record)->tap.count <= 0 || (record)->tap.interrupted) ? (press) : MACRO_NONE) : (((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (tap_macro) : (release))) // Holds down the modifier mod when the macro key is held, or sends macro instead when tapped #define MACRO_TAP_HOLD_MOD(record, macro, mod) MACRO_TAP_HOLD(record, (MACRO(D(mod), END)), MACRO(U(mod), END), macro) @@ -38,25 +38,27 @@ typedef uint8_t macro_t; // Holds down the modifier mod when the macro key is held, or pressed a shifted key when tapped (eg: shift+3 for #) #define MACRO_TAP_SHFT_KEY_HOLD_MOD(record, key, mod) MACRO_TAP_HOLD_MOD(record, (MACRO(I(10), D(LSFT), T(key), U(LSFT), END)), mod) - // Momentary switch layer when held, sends macro if tapped -#define MACRO_TAP_HOLD_LAYER(record, macro, layer) ( ((record)->event.pressed) ? \ - ( ((record)->tap.count <= 0 || (record)->tap.interrupted) ? ({layer_on((layer)); MACRO_NONE; }) : MACRO_NONE ) : \ - ( ((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (macro) : ({layer_off((layer)); MACRO_NONE; }) ) ) +#define MACRO_TAP_HOLD_LAYER(record, macro, layer) \ + (((record)->event.pressed) ? (((record)->tap.count <= 0 || (record)->tap.interrupted) ? ({ \ + layer_on((layer)); \ + MACRO_NONE; \ + }) \ + : MACRO_NONE) \ + : (((record)->tap.count > 0 && !((record)->tap.interrupted)) ? (macro) : ({ \ + layer_off((layer)); \ + MACRO_NONE; \ + }))) // Momentary switch layer when held, presses a shifted key when tapped (eg: shift+3 for #) #define MACRO_TAP_SHFT_KEY_HOLD_LAYER(record, key, layer) MACRO_TAP_HOLD_LAYER(record, MACRO(I(10), D(LSFT), T(key), U(LSFT), END), layer) - - #ifndef NO_ACTION_MACRO void action_macro_play(const macro_t *macro_p); #else -#define action_macro_play(macro) +# define action_macro_play(macro) #endif - - /* Macro commands * code(0x04-73) // key down(1byte) * code(0x04-73) | 0x80 // key up(1byte) @@ -75,16 +77,16 @@ void action_macro_play(const macro_t *macro_p); * conditionals * loop */ -enum macro_command_id{ +enum macro_command_id { /* 0x00 - 0x03 */ - END = 0x00, + END = 0x00, KEY_DOWN, KEY_UP, /* 0x04 - 0x73 (reserved for keycode down) */ /* 0x74 - 0x83 */ - WAIT = 0x74, + WAIT = 0x74, INTERVAL, /* 0x84 - 0xf3 (reserved for keycode up) */ @@ -92,33 +94,31 @@ enum macro_command_id{ /* 0xf4 - 0xff */ }; - /* TODO: keycode:0x04-0x73 can be handled by 1byte command else 2bytes are needed * if keycode between 0x04 and 0x73 * keycode / (keycode|0x80) * else * {KEY_DOWN, keycode} / {KEY_UP, keycode} -*/ -#define DOWN(key) KEY_DOWN, (key) -#define UP(key) KEY_UP, (key) -#define TYPE(key) DOWN(key), UP(key) -#define WAIT(ms) WAIT, (ms) -#define INTERVAL(ms) INTERVAL, (ms) + */ +#define DOWN(key) KEY_DOWN, (key) +#define UP(key) KEY_UP, (key) +#define TYPE(key) DOWN(key), UP(key) +#define WAIT(ms) WAIT, (ms) +#define INTERVAL(ms) INTERVAL, (ms) /* key down */ -#define D(key) DOWN(KC_##key) +#define D(key) DOWN(KC_##key) /* key up */ -#define U(key) UP(KC_##key) +#define U(key) UP(KC_##key) /* key type */ -#define T(key) TYPE(KC_##key) +#define T(key) TYPE(KC_##key) /* wait */ -#define W(ms) WAIT(ms) +#define W(ms) WAIT(ms) /* interval */ -#define I(ms) INTERVAL(ms) +#define I(ms) INTERVAL(ms) /* for backward comaptibility */ -#define MD(key) DOWN(KC_##key) -#define MU(key) UP(KC_##key) - +#define MD(key) DOWN(KC_##key) +#define MU(key) UP(KC_##key) #endif /* ACTION_MACRO_H */ diff --git a/tmk_core/common/action_tapping.c b/tmk_core/common/action_tapping.c index 3b67ed152..e0f524ad7 100644 --- a/tmk_core/common/action_tapping.c +++ b/tmk_core/common/action_tapping.c @@ -7,33 +7,30 @@ #include "timer.h" #ifdef DEBUG_ACTION -#include "debug.h" +# include "debug.h" #else -#include "nodebug.h" +# include "nodebug.h" #endif #ifndef NO_ACTION_TAPPING -#define IS_TAPPING() !IS_NOEVENT(tapping_key.event) -#define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) -#define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) -#define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) +# define IS_TAPPING() !IS_NOEVENT(tapping_key.event) +# define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) +# define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) +# define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) -__attribute__ ((weak)) -uint16_t get_tapping_term(uint16_t keycode) { - return TAPPING_TERM; -} +__attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode) { return TAPPING_TERM; } -#ifdef TAPPING_TERM_PER_KEY -#define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event))) -#else -#define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) -#endif +# ifdef TAPPING_TERM_PER_KEY +# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event))) +# else +# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) +# endif -static keyrecord_t tapping_key = {}; +static keyrecord_t tapping_key = {}; static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {}; -static uint8_t waiting_buffer_head = 0; -static uint8_t waiting_buffer_tail = 0; +static uint8_t waiting_buffer_head = 0; +static uint8_t waiting_buffer_tail = 0; static bool process_tapping(keyrecord_t *record); static bool waiting_buffer_enq(keyrecord_t record); @@ -44,16 +41,16 @@ static void waiting_buffer_scan_tap(void); static void debug_tapping_key(void); static void debug_waiting_buffer(void); - /** \brief Action Tapping Process * * FIXME: Needs doc */ -void action_tapping_process(keyrecord_t record) -{ +void action_tapping_process(keyrecord_t record) { if (process_tapping(&record)) { if (!IS_NOEVENT(record.event)) { - debug("processed: "); debug_record(record); debug("\n"); + debug("processed: "); + debug_record(record); + debug("\n"); } } else { if (!waiting_buffer_enq(record)) { @@ -71,8 +68,11 @@ void action_tapping_process(keyrecord_t record) } for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) { if (process_tapping(&waiting_buffer[waiting_buffer_tail])) { - debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = "); - debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n"); + debug("processed: waiting_buffer["); + debug_dec(waiting_buffer_tail); + debug("] = "); + debug_record(waiting_buffer[waiting_buffer_tail]); + debug("\n\n"); } else { break; } @@ -82,15 +82,13 @@ void action_tapping_process(keyrecord_t record) } } - /** \brief Tapping * * Rule: Tap key is typed(pressed and released) within TAPPING_TERM. * (without interfering by typing other key) */ /* return true when key event is processed or consumed. */ -bool process_tapping(keyrecord_t *keyp) -{ +bool process_tapping(keyrecord_t *keyp) { keyevent_t event = keyp->event; // if tapping @@ -113,12 +111,12 @@ bool process_tapping(keyrecord_t *keyp) * This can register the key before settlement of tapping, * useful for long TAPPING_TERM but may prevent fast typing. */ -#if defined(TAPPING_TERM_PER_KEY) || (!defined(PER_KEY_TAPPING_TERM) && TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) -#ifdef TAPPING_TERM_PER_KEY - else if ( ( get_tapping_term(get_event_keycode(tapping_key.event)) >= 500) && IS_RELEASED(event) && waiting_buffer_typed(event)) -#else - else if ( IS_RELEASED(event) && waiting_buffer_typed(event)) -#endif +# if defined(TAPPING_TERM_PER_KEY) || (!defined(PER_KEY_TAPPING_TERM) && TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) +# ifdef TAPPING_TERM_PER_KEY + else if ((get_tapping_term(get_event_keycode(tapping_key.event)) >= 500) && IS_RELEASED(event) && waiting_buffer_typed(event)) +# else + else if (IS_RELEASED(event) && waiting_buffer_typed(event)) +# endif { debug("Tapping: End. No tap. Interfered by typing key\n"); process_record(&tapping_key); @@ -127,7 +125,7 @@ bool process_tapping(keyrecord_t *keyp) // enqueue return false; } -#endif +# endif /* Process release event of a key pressed before tapping starts * Without this unexpected repeating will occur with having fast repeating setting * https://github.com/tmk/tmk_keyboard/issues/60 @@ -151,8 +149,7 @@ bool process_tapping(keyrecord_t *keyp) debug("Tapping: release event of a key pressed before tapping\n"); process_record(keyp); return true; - } - else { + } else { // set interrupted flag when other key preesed during tapping if (event.pressed) { tapping_key.tap.interrupted = true; @@ -164,23 +161,19 @@ bool process_tapping(keyrecord_t *keyp) // tap_count > 0 else { if (IS_TAPPING_KEY(event.key) && !event.pressed) { - debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n"); + debug("Tapping: Tap release("); + debug_dec(tapping_key.tap.count); + debug(")\n"); keyp->tap = tapping_key.tap; process_record(keyp); tapping_key = *keyp; debug_tapping_key(); return true; - } - else if (is_tap_key(event.key) && event.pressed) { + } else if (is_tap_key(event.key) && event.pressed) { if (tapping_key.tap.count > 1) { debug("Tapping: Start new tap with releasing last tap(>1).\n"); // unregister key - process_record(&(keyrecord_t){ - .tap = tapping_key.tap, - .event.key = tapping_key.event.key, - .event.time = event.time, - .event.pressed = false - }); + process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false}); } else { debug("Tapping: Start while last tap(1).\n"); } @@ -188,8 +181,7 @@ bool process_tapping(keyrecord_t *keyp) waiting_buffer_scan_tap(); debug_tapping_key(); return true; - } - else { + } else { if (!IS_NOEVENT(event)) { debug("Tapping: key event while last tap(>0).\n"); } @@ -202,29 +194,24 @@ bool process_tapping(keyrecord_t *keyp) else { if (tapping_key.tap.count == 0) { debug("Tapping: End. Timeout. Not tap(0): "); - debug_event(event); debug("\n"); + debug_event(event); + debug("\n"); process_record(&tapping_key); tapping_key = (keyrecord_t){}; debug_tapping_key(); return false; - } else { + } else { if (IS_TAPPING_KEY(event.key) && !event.pressed) { debug("Tapping: End. last timeout tap release(>0)."); keyp->tap = tapping_key.tap; process_record(keyp); tapping_key = (keyrecord_t){}; return true; - } - else if (is_tap_key(event.key) && event.pressed) { + } else if (is_tap_key(event.key) && event.pressed) { if (tapping_key.tap.count > 1) { debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); // unregister key - process_record(&(keyrecord_t){ - .tap = tapping_key.tap, - .event.key = tapping_key.event.key, - .event.time = event.time, - .event.pressed = false - }); + process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false}); } else { debug("Tapping: Start while last timeout tap(1).\n"); } @@ -232,8 +219,7 @@ bool process_tapping(keyrecord_t *keyp) waiting_buffer_scan_tap(); debug_tapping_key(); return true; - } - else { + } else { if (!IS_NOEVENT(event)) { debug("Tapping: key event while last timeout tap(>0).\n"); } @@ -246,18 +232,20 @@ bool process_tapping(keyrecord_t *keyp) if (WITHIN_TAPPING_TERM(event)) { if (event.pressed) { if (IS_TAPPING_KEY(event.key)) { -#ifndef TAPPING_FORCE_HOLD +# ifndef TAPPING_FORCE_HOLD if (!tapping_key.tap.interrupted && tapping_key.tap.count > 0) { // sequential tap. keyp->tap = tapping_key.tap; if (keyp->tap.count < 15) keyp->tap.count += 1; - debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n"); + debug("Tapping: Tap press("); + debug_dec(keyp->tap.count); + debug(")\n"); process_record(keyp); tapping_key = *keyp; debug_tapping_key(); return true; } -#endif +# endif // FIX: start new tap again tapping_key = *keyp; return true; @@ -284,7 +272,8 @@ bool process_tapping(keyrecord_t *keyp) // FIX: process_action here? // timeout. no sequential tap. debug("Tapping: End(Timeout after releasing last tap): "); - debug_event(event); debug("\n"); + debug_event(event); + debug("\n"); tapping_key = (keyrecord_t){}; debug_tapping_key(); return false; @@ -306,13 +295,11 @@ bool process_tapping(keyrecord_t *keyp) } } - /** \brief Waiting buffer enq * * FIXME: Needs docs */ -bool waiting_buffer_enq(keyrecord_t record) -{ +bool waiting_buffer_enq(keyrecord_t record) { if (IS_NOEVENT(record.event)) { return true; } @@ -323,9 +310,10 @@ bool waiting_buffer_enq(keyrecord_t record) } waiting_buffer[waiting_buffer_head] = record; - waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; + waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE; - debug("waiting_buffer_enq: "); debug_waiting_buffer(); + debug("waiting_buffer_enq: "); + debug_waiting_buffer(); return true; } @@ -333,8 +321,7 @@ bool waiting_buffer_enq(keyrecord_t record) * * FIXME: Needs docs */ -void waiting_buffer_clear(void) -{ +void waiting_buffer_clear(void) { waiting_buffer_head = 0; waiting_buffer_tail = 0; } @@ -343,10 +330,9 @@ void waiting_buffer_clear(void) * * FIXME: Needs docs */ -bool waiting_buffer_typed(keyevent_t event) -{ +bool waiting_buffer_typed(keyevent_t event) { for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { - if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { + if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) { return true; } } @@ -357,9 +343,7 @@ bool waiting_buffer_typed(keyevent_t event) * * FIXME: Needs docs */ -__attribute__((unused)) -bool waiting_buffer_has_anykey_pressed(void) -{ +__attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) { for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { if (waiting_buffer[i].event.pressed) return true; } @@ -370,47 +354,49 @@ bool waiting_buffer_has_anykey_pressed(void) * * FIXME: Needs docs */ -void waiting_buffer_scan_tap(void) -{ +void waiting_buffer_scan_tap(void) { // tapping already is settled if (tapping_key.tap.count > 0) return; // invalid state: tapping_key released && tap.count == 0 if (!tapping_key.event.pressed) return; for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { - if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && - !waiting_buffer[i].event.pressed && - WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { - tapping_key.tap.count = 1; + if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) { + tapping_key.tap.count = 1; waiting_buffer[i].tap.count = 1; process_record(&tapping_key); - debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n"); + debug("waiting_buffer_scan_tap: found at ["); + debug_dec(i); + debug("]\n"); debug_waiting_buffer(); return; } } } - /** \brief Tapping key debug print * * FIXME: Needs docs */ -static void debug_tapping_key(void) -{ - debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n"); +static void debug_tapping_key(void) { + debug("TAPPING_KEY="); + debug_record(tapping_key); + debug("\n"); } /** \brief Waiting buffer debug print * * FIXME: Needs docs */ -static void debug_waiting_buffer(void) -{ +static void debug_waiting_buffer(void) { debug("{ "); for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) { - debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" "); + debug("["); + debug_dec(i); + debug("]="); + debug_record(waiting_buffer[i]); + debug(" "); } debug("}\n"); } diff --git a/tmk_core/common/action_tapping.h b/tmk_core/common/action_tapping.h index 1db43a442..509d5eabd 100644 --- a/tmk_core/common/action_tapping.h +++ b/tmk_core/common/action_tapping.h @@ -17,27 +17,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef ACTION_TAPPING_H #define ACTION_TAPPING_H - - /* period of tapping(ms) */ #ifndef TAPPING_TERM -#define TAPPING_TERM 200 +# define TAPPING_TERM 200 #endif //#define RETRO_TAPPING // Tap anyway, even after TAPPING_TERM, as long as there was no interruption /* tap count needed for toggling a feature */ #ifndef TAPPING_TOGGLE -#define TAPPING_TOGGLE 5 +# define TAPPING_TOGGLE 5 #endif #define WAITING_BUFFER_SIZE 8 - #ifndef NO_ACTION_TAPPING uint16_t get_event_keycode(keyevent_t event); uint16_t get_tapping_term(uint16_t keycode); -void action_tapping_process(keyrecord_t record); +void action_tapping_process(keyrecord_t record); #endif #endif diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c index 365ed6a1d..335aa36e6 100644 --- a/tmk_core/common/action_util.c +++ b/tmk_core/common/action_util.c @@ -24,23 +24,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. extern keymap_config_t keymap_config; - -static uint8_t real_mods = 0; -static uint8_t weak_mods = 0; +static uint8_t real_mods = 0; +static uint8_t weak_mods = 0; static uint8_t macro_mods = 0; #ifdef USB_6KRO_ENABLE -#define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) -#define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS) -#define RO_INC(a) RO_ADD(a, 1) -#define RO_DEC(a) RO_SUB(a, 1) -static int8_t cb_head = 0; -static int8_t cb_tail = 0; +# define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) +# define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS) +# define RO_INC(a) RO_ADD(a, 1) +# define RO_DEC(a) RO_SUB(a, 1) +static int8_t cb_head = 0; +static int8_t cb_tail = 0; static int8_t cb_count = 0; #endif // TODO: pointer variable is not needed -//report_keyboard_t keyboard_report = {}; +// report_keyboard_t keyboard_report = {}; report_keyboard_t *keyboard_report = &(report_keyboard_t){}; extern inline void add_key(uint8_t key); @@ -48,10 +47,10 @@ extern inline void del_key(uint8_t key); extern inline void clear_keys(void); #ifndef NO_ACTION_ONESHOT -static uint8_t oneshot_mods = 0; +static uint8_t oneshot_mods = 0; static uint8_t oneshot_locked_mods = 0; -uint8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; } -void set_oneshot_locked_mods(uint8_t mods) { +uint8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; } +void set_oneshot_locked_mods(uint8_t mods) { if (mods != oneshot_locked_mods) { oneshot_locked_mods = mods; oneshot_locked_mods_changed_kb(oneshot_locked_mods); @@ -63,16 +62,12 @@ void clear_oneshot_locked_mods(void) { oneshot_locked_mods_changed_kb(oneshot_locked_mods); } } -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) static uint16_t oneshot_time = 0; -bool has_oneshot_mods_timed_out(void) { - return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT; -} -#else -bool has_oneshot_mods_timed_out(void) { - return false; -} -#endif +bool has_oneshot_mods_timed_out(void) { return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT; } +# else +bool has_oneshot_mods_timed_out(void) { return false; } +# endif #endif /* oneshot layer */ @@ -88,44 +83,39 @@ static int8_t oneshot_layer_data = 0; inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; } inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; } -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) static uint16_t oneshot_layer_time = 0; -inline bool has_oneshot_layer_timed_out() { - return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && - !(get_oneshot_layer_state() & ONESHOT_TOGGLED); -} -#endif +inline bool has_oneshot_layer_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED); } +# endif -/** \brief Set oneshot layer +/** \brief Set oneshot layer * * FIXME: needs doc */ -void set_oneshot_layer(uint8_t layer, uint8_t state) -{ +void set_oneshot_layer(uint8_t layer, uint8_t state) { oneshot_layer_data = layer << 3 | state; layer_on(layer); -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) oneshot_layer_time = timer_read(); -#endif +# endif oneshot_layer_changed_kb(get_oneshot_layer()); } -/** \brief Reset oneshot layer +/** \brief Reset oneshot layer * * FIXME: needs doc */ void reset_oneshot_layer(void) { oneshot_layer_data = 0; -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) oneshot_layer_time = 0; -#endif +# endif oneshot_layer_changed_kb(get_oneshot_layer()); } -/** \brief Clear oneshot layer +/** \brief Clear oneshot layer * * FIXME: needs doc */ -void clear_oneshot_layer_state(oneshot_fullfillment_t state) -{ +void clear_oneshot_layer_state(oneshot_fullfillment_t state) { uint8_t start_state = oneshot_layer_data; oneshot_layer_data &= ~state; if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) { @@ -137,10 +127,7 @@ void clear_oneshot_layer_state(oneshot_fullfillment_t state) * * FIXME: needs doc */ -bool is_oneshot_layer_active(void) -{ - return get_oneshot_layer_state(); -} +bool is_oneshot_layer_active(void) { return get_oneshot_layer_state(); } #endif /** \brief Send keyboard report @@ -148,17 +135,17 @@ bool is_oneshot_layer_active(void) * FIXME: needs doc */ void send_keyboard_report(void) { - keyboard_report->mods = real_mods; + keyboard_report->mods = real_mods; keyboard_report->mods |= weak_mods; keyboard_report->mods |= macro_mods; #ifndef NO_ACTION_ONESHOT if (oneshot_mods) { -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) if (has_oneshot_mods_timed_out()) { dprintf("Oneshot: timeout\n"); clear_oneshot_mods(); } -#endif +# endif keyboard_report->mods |= oneshot_mods; if (has_anykey(keyboard_report)) { clear_oneshot_mods(); @@ -254,90 +241,72 @@ void clear_macro_mods(void) { macro_mods = 0; } * FIXME: needs doc */ void set_oneshot_mods(uint8_t mods) { - if (oneshot_mods != mods) { -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) - oneshot_time = timer_read(); -#endif - oneshot_mods = mods; - oneshot_mods_changed_kb(mods); - } + if (oneshot_mods != mods) { +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) + oneshot_time = timer_read(); +# endif + oneshot_mods = mods; + oneshot_mods_changed_kb(mods); + } } /** \brief clear oneshot mods * * FIXME: needs doc */ void clear_oneshot_mods(void) { - if (oneshot_mods) { - oneshot_mods = 0; -#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) - oneshot_time = 0; -#endif - oneshot_mods_changed_kb(oneshot_mods); - } + if (oneshot_mods) { + oneshot_mods = 0; +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) + oneshot_time = 0; +# endif + oneshot_mods_changed_kb(oneshot_mods); + } } /** \brief get oneshot mods * * FIXME: needs doc */ -uint8_t get_oneshot_mods(void) -{ - return oneshot_mods; -} +uint8_t get_oneshot_mods(void) { return oneshot_mods; } #endif /** \brief Called when the one shot modifiers have been changed. - * - * \param mods Contains the active modifiers active after the change. + * + * \param mods Contains the active modifiers active after the change. */ -__attribute__((weak)) -void oneshot_locked_mods_changed_user(uint8_t mods) { } +__attribute__((weak)) void oneshot_locked_mods_changed_user(uint8_t mods) {} /** \brief Called when the locked one shot modifiers have been changed. - * - * \param mods Contains the active modifiers active after the change. + * + * \param mods Contains the active modifiers active after the change. */ -__attribute__((weak)) -void oneshot_locked_mods_changed_kb(uint8_t mods) { - oneshot_locked_mods_changed_user(mods); -} +__attribute__((weak)) void oneshot_locked_mods_changed_kb(uint8_t mods) { oneshot_locked_mods_changed_user(mods); } /** \brief Called when the one shot modifiers have been changed. - * + * * \param mods Contains the active modifiers active after the change. */ -__attribute__((weak)) -void oneshot_mods_changed_user(uint8_t mods) { } +__attribute__((weak)) void oneshot_mods_changed_user(uint8_t mods) {} /** \brief Called when the one shot modifiers have been changed. - * + * * \param mods Contains the active modifiers active after the change. */ -__attribute__((weak)) -void oneshot_mods_changed_kb(uint8_t mods) { - oneshot_mods_changed_user(mods); -} +__attribute__((weak)) void oneshot_mods_changed_kb(uint8_t mods) { oneshot_mods_changed_user(mods); } /** \brief Called when the one shot layers have been changed. - * - * \param layer Contains the layer that is toggled on, or zero when toggled off. + * + * \param layer Contains the layer that is toggled on, or zero when toggled off. */ -__attribute__((weak)) -void oneshot_layer_changed_user(uint8_t layer) { } +__attribute__((weak)) void oneshot_layer_changed_user(uint8_t layer) {} /** \brief Called when the one shot layers have been changed. - * + * * \param layer Contains the layer that is toggled on, or zero when toggled off. */ -__attribute__((weak)) -void oneshot_layer_changed_kb(uint8_t layer) { - oneshot_layer_changed_user(layer); -} +__attribute__((weak)) void oneshot_layer_changed_kb(uint8_t layer) { oneshot_layer_changed_user(layer); } /** \brief inspect keyboard state * * FIXME: needs doc */ -uint8_t has_anymod(void) -{ - return bitpop(real_mods); -} +uint8_t has_anymod(void) { return bitpop(real_mods); } diff --git a/tmk_core/common/action_util.h b/tmk_core/common/action_util.h index d03f5682a..1ce03ed0e 100644 --- a/tmk_core/common/action_util.h +++ b/tmk_core/common/action_util.h @@ -29,65 +29,54 @@ extern report_keyboard_t *keyboard_report; void send_keyboard_report(void); /* key */ -inline void add_key(uint8_t key) { - add_key_to_report(keyboard_report, key); -} +inline void add_key(uint8_t key) { add_key_to_report(keyboard_report, key); } -inline void del_key(uint8_t key) { - del_key_from_report(keyboard_report, key); -} +inline void del_key(uint8_t key) { del_key_from_report(keyboard_report, key); } -inline void clear_keys(void) { - clear_keys_from_report(keyboard_report); -} +inline void clear_keys(void) { clear_keys_from_report(keyboard_report); } /* modifier */ uint8_t get_mods(void); -void add_mods(uint8_t mods); -void del_mods(uint8_t mods); -void set_mods(uint8_t mods); -void clear_mods(void); +void add_mods(uint8_t mods); +void del_mods(uint8_t mods); +void set_mods(uint8_t mods); +void clear_mods(void); /* weak modifier */ uint8_t get_weak_mods(void); -void add_weak_mods(uint8_t mods); -void del_weak_mods(uint8_t mods); -void set_weak_mods(uint8_t mods); -void clear_weak_mods(void); +void add_weak_mods(uint8_t mods); +void del_weak_mods(uint8_t mods); +void set_weak_mods(uint8_t mods); +void clear_weak_mods(void); /* macro modifier */ uint8_t get_macro_mods(void); -void add_macro_mods(uint8_t mods); -void del_macro_mods(uint8_t mods); -void set_macro_mods(uint8_t mods); -void clear_macro_mods(void); +void add_macro_mods(uint8_t mods); +void del_macro_mods(uint8_t mods); +void set_macro_mods(uint8_t mods); +void clear_macro_mods(void); /* oneshot modifier */ -void set_oneshot_mods(uint8_t mods); +void set_oneshot_mods(uint8_t mods); uint8_t get_oneshot_mods(void); -void clear_oneshot_mods(void); -void oneshot_toggle(void); -void oneshot_enable(void); -void oneshot_disable(void); -bool has_oneshot_mods_timed_out(void); +void clear_oneshot_mods(void); +void oneshot_toggle(void); +void oneshot_enable(void); +void oneshot_disable(void); +bool has_oneshot_mods_timed_out(void); uint8_t get_oneshot_locked_mods(void); -void set_oneshot_locked_mods(uint8_t mods); -void clear_oneshot_locked_mods(void); - -typedef enum { - ONESHOT_PRESSED = 0b01, - ONESHOT_OTHER_KEY_PRESSED = 0b10, - ONESHOT_START = 0b11, - ONESHOT_TOGGLED = 0b100 -} oneshot_fullfillment_t; -void set_oneshot_layer(uint8_t layer, uint8_t state); +void set_oneshot_locked_mods(uint8_t mods); +void clear_oneshot_locked_mods(void); + +typedef enum { ONESHOT_PRESSED = 0b01, ONESHOT_OTHER_KEY_PRESSED = 0b10, ONESHOT_START = 0b11, ONESHOT_TOGGLED = 0b100 } oneshot_fullfillment_t; +void set_oneshot_layer(uint8_t layer, uint8_t state); uint8_t get_oneshot_layer(void); -void clear_oneshot_layer_state(oneshot_fullfillment_t state); -void reset_oneshot_layer(void); -bool is_oneshot_layer_active(void); +void clear_oneshot_layer_state(oneshot_fullfillment_t state); +void reset_oneshot_layer(void); +bool is_oneshot_layer_active(void); uint8_t get_oneshot_layer_state(void); -bool has_oneshot_layer_timed_out(void); +bool has_oneshot_layer_timed_out(void); void oneshot_locked_mods_changed_user(uint8_t mods); void oneshot_locked_mods_changed_kb(uint8_t mods); diff --git a/tmk_core/common/arm_atsam/bootloader.c b/tmk_core/common/arm_atsam/bootloader.c index ba71bfeb0..9015b00aa 100644 --- a/tmk_core/common/arm_atsam/bootloader.c +++ b/tmk_core/common/arm_atsam/bootloader.c @@ -18,34 +18,40 @@ #include "samd51j18a.h" #include "md_bootloader.h" -//Set watchdog timer to reset. Directs the bootloader to stay in programming mode. +// Set watchdog timer to reset. Directs the bootloader to stay in programming mode. void bootloader_jump(void) { #ifdef KEYBOARD_massdrop_ctrl - //CTRL keyboards released with bootloader version below must use RAM method. Otherwise use WDT method. - uint8_t ver_ram_method[] = "v2.18Jun 22 2018 17:28:08"; //The version to match (NULL terminated by compiler) - uint8_t *ver_check = ver_ram_method; //Pointer to version match string for traversal - uint8_t *ver_rom = (uint8_t *)0x21A0; //Pointer to address in ROM where this specific bootloader version would exist + // CTRL keyboards released with bootloader version below must use RAM method. Otherwise use WDT method. + uint8_t ver_ram_method[] = "v2.18Jun 22 2018 17:28:08"; // The version to match (NULL terminated by compiler) + uint8_t *ver_check = ver_ram_method; // Pointer to version match string for traversal + uint8_t *ver_rom = (uint8_t *)0x21A0; // Pointer to address in ROM where this specific bootloader version would exist - while (*ver_check && *ver_rom == *ver_check) { //While there are check version characters to match and bootloader's version matches check's version - ver_check++; //Move check version pointer to next character - ver_rom++; //Move ROM version pointer to next character + while (*ver_check && *ver_rom == *ver_check) { // While there are check version characters to match and bootloader's version matches check's version + ver_check++; // Move check version pointer to next character + ver_rom++; // Move ROM version pointer to next character } - if (!*ver_check) { //If check version pointer is NULL, all characters have matched - *MAGIC_ADDR = BOOTLOADER_MAGIC; //Set magic number into RAM - NVIC_SystemReset(); //Perform system reset - while (1) {} //Won't get here + if (!*ver_check) { // If check version pointer is NULL, all characters have matched + *MAGIC_ADDR = BOOTLOADER_MAGIC; // Set magic number into RAM + NVIC_SystemReset(); // Perform system reset + while (1) { + } // Won't get here } #endif WDT->CTRLA.bit.ENABLE = 0; - while (WDT->SYNCBUSY.bit.ENABLE) {} - while (WDT->CTRLA.bit.ENABLE) {} - WDT->CONFIG.bit.WINDOW = 0; - WDT->CONFIG.bit.PER = 0; + while (WDT->SYNCBUSY.bit.ENABLE) { + } + while (WDT->CTRLA.bit.ENABLE) { + } + WDT->CONFIG.bit.WINDOW = 0; + WDT->CONFIG.bit.PER = 0; WDT->EWCTRL.bit.EWOFFSET = 0; - WDT->CTRLA.bit.ENABLE = 1; - while (WDT->SYNCBUSY.bit.ENABLE) {} - while (!WDT->CTRLA.bit.ENABLE) {} - while (1) {} //Wait on timeout + WDT->CTRLA.bit.ENABLE = 1; + while (WDT->SYNCBUSY.bit.ENABLE) { + } + while (!WDT->CTRLA.bit.ENABLE) { + } + while (1) { + } // Wait on timeout } diff --git a/tmk_core/common/arm_atsam/eeprom.c b/tmk_core/common/arm_atsam/eeprom.c index 61cc039ef..44a0bf4d7 100644 --- a/tmk_core/common/arm_atsam/eeprom.c +++ b/tmk_core/common/arm_atsam/eeprom.c @@ -21,78 +21,75 @@ static uint8_t buffer[EEPROM_SIZE]; uint8_t eeprom_read_byte(const uint8_t *addr) { - uintptr_t offset = (uintptr_t)addr; - return buffer[offset]; + uintptr_t offset = (uintptr_t)addr; + return buffer[offset]; } void eeprom_write_byte(uint8_t *addr, uint8_t value) { - uintptr_t offset = (uintptr_t)addr; - buffer[offset] = value; + uintptr_t offset = (uintptr_t)addr; + buffer[offset] = value; } uint16_t eeprom_read_word(const uint16_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); } uint32_t eeprom_read_dword(const uint32_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8) - | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } void eeprom_read_block(void *buf, const void *addr, uint32_t len) { - const uint8_t *p = (const uint8_t *)addr; - uint8_t *dest = (uint8_t *)buf; - while (len--) { - *dest++ = eeprom_read_byte(p++); - } + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte(p++); + } } void eeprom_write_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_write_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_write_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } -void eeprom_update_byte(uint8_t *addr, uint8_t value) { - eeprom_write_byte(addr, value); -} +void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); } void eeprom_update_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_update_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_update_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } diff --git a/tmk_core/common/arm_atsam/printf.c b/tmk_core/common/arm_atsam/printf.c index 7f298d1fd..cd7cdb52e 100644 --- a/tmk_core/common/arm_atsam/printf.c +++ b/tmk_core/common/arm_atsam/printf.c @@ -17,50 +17,52 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifdef CONSOLE_ENABLE -#include "samd51j18a.h" -#include "arm_atsam_protocol.h" -#include "printf.h" -#include <string.h> -#include <stdarg.h> +# include "samd51j18a.h" +# include "arm_atsam_protocol.h" +# include "printf.h" +# include <string.h> +# include <stdarg.h> void console_printf(char *fmt, ...) { - while (udi_hid_con_b_report_trans_ongoing) {} //Wait for any previous transfers to complete + while (udi_hid_con_b_report_trans_ongoing) { + } // Wait for any previous transfers to complete - static char console_printbuf[CONSOLE_PRINTBUF_SIZE]; //Print and send buffer - va_list va; - int result; + static char console_printbuf[CONSOLE_PRINTBUF_SIZE]; // Print and send buffer + va_list va; + int result; va_start(va, fmt); result = vsnprintf(console_printbuf, CONSOLE_PRINTBUF_SIZE, fmt, va); va_end(va); uint32_t irqflags; - char *pconbuf = console_printbuf; //Pointer to start send from - int send_out = CONSOLE_EPSIZE; //Bytes to send per transfer + char * pconbuf = console_printbuf; // Pointer to start send from + int send_out = CONSOLE_EPSIZE; // Bytes to send per transfer - while (result > 0) { //While not error and bytes remain - while (udi_hid_con_b_report_trans_ongoing) {} //Wait for any previous transfers to complete + while (result > 0) { // While not error and bytes remain + while (udi_hid_con_b_report_trans_ongoing) { + } // Wait for any previous transfers to complete irqflags = __get_PRIMASK(); __disable_irq(); __DMB(); - if (result < CONSOLE_EPSIZE) { //If remaining bytes are less than console epsize - memset(udi_hid_con_report, 0, CONSOLE_EPSIZE); //Clear the buffer - send_out = result; //Send remaining size + if (result < CONSOLE_EPSIZE) { // If remaining bytes are less than console epsize + memset(udi_hid_con_report, 0, CONSOLE_EPSIZE); // Clear the buffer + send_out = result; // Send remaining size } - memcpy(udi_hid_con_report, pconbuf, send_out); //Copy data into the send buffer + memcpy(udi_hid_con_report, pconbuf, send_out); // Copy data into the send buffer - udi_hid_con_b_report_valid = 1; //Set report valid - udi_hid_con_send_report(); //Send report + udi_hid_con_b_report_valid = 1; // Set report valid + udi_hid_con_send_report(); // Send report __DMB(); __set_PRIMASK(irqflags); - result -= send_out; //Decrement result by bytes sent - pconbuf += send_out; //Increment buffer point by bytes sent + result -= send_out; // Decrement result by bytes sent + pconbuf += send_out; // Increment buffer point by bytes sent } } -#endif //CONSOLE_ENABLE +#endif // CONSOLE_ENABLE diff --git a/tmk_core/common/arm_atsam/printf.h b/tmk_core/common/arm_atsam/printf.h index 1f1c2280b..ad6672220 100644 --- a/tmk_core/common/arm_atsam/printf.h +++ b/tmk_core/common/arm_atsam/printf.h @@ -7,5 +7,4 @@ void console_printf(char *fmt, ...); #define __xprintf console_printf -#endif //_PRINTF_H_ - +#endif //_PRINTF_H_ diff --git a/tmk_core/common/arm_atsam/suspend.c b/tmk_core/common/arm_atsam/suspend.c index ecf8f0ed1..2dad00570 100644 --- a/tmk_core/common/arm_atsam/suspend.c +++ b/tmk_core/common/arm_atsam/suspend.c @@ -7,44 +7,35 @@ * * FIXME: needs doc */ -void suspend_idle(uint8_t time) { - /* Note: Not used anywhere currently */ -} +void suspend_idle(uint8_t time) { /* Note: Not used anywhere currently */ } /** \brief Run user level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_user (void) { - -} +__attribute__((weak)) void suspend_power_down_user(void) {} /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_kb(void) { - suspend_power_down_user(); -} +__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); } /** \brief Suspend power down * * FIXME: needs doc */ -void suspend_power_down(void) -{ +void suspend_power_down(void) { #ifdef RGB_MATRIX_ENABLE - I2C3733_Control_Set(0); //Disable LED driver + I2C3733_Control_Set(0); // Disable LED driver #endif suspend_power_down_kb(); } -__attribute__ ((weak)) void matrix_power_up(void) {} -__attribute__ ((weak)) void matrix_power_down(void) {} -bool suspend_wakeup_condition(void) { +__attribute__((weak)) void matrix_power_up(void) {} +__attribute__((weak)) void matrix_power_down(void) {} +bool suspend_wakeup_condition(void) { matrix_power_up(); matrix_scan(); matrix_power_down(); @@ -58,19 +49,13 @@ bool suspend_wakeup_condition(void) { * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_user(void) { - -} +__attribute__((weak)) void suspend_wakeup_init_user(void) {} /** \brief run keyboard level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_kb(void) { - suspend_wakeup_init_user(); -} +__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); } /** \brief run immediately after wakeup * @@ -78,15 +63,14 @@ void suspend_wakeup_init_kb(void) { */ void suspend_wakeup_init(void) { #ifdef RGB_MATRIX_ENABLE -#ifdef USE_MASSDROP_CONFIGURATOR +# ifdef USE_MASSDROP_CONFIGURATOR if (led_enabled) { I2C3733_Control_Set(1); } -#else +# else I2C3733_Control_Set(1); -#endif +# endif #endif suspend_wakeup_init_kb(); } - diff --git a/tmk_core/common/arm_atsam/timer.c b/tmk_core/common/arm_atsam/timer.c index 6c3905e30..b835dd5e7 100644 --- a/tmk_core/common/arm_atsam/timer.c +++ b/tmk_core/common/arm_atsam/timer.c @@ -2,42 +2,18 @@ #include "timer.h" #include "tmk_core/protocol/arm_atsam/clks.h" -void set_time(uint64_t tset) -{ - ms_clk = tset; -} - -void timer_init(void) -{ - timer_clear(); -} - -uint16_t timer_read(void) -{ - return (uint16_t)ms_clk; -} - -uint32_t timer_read32(void) -{ - return (uint32_t)ms_clk; -} - -uint64_t timer_read64(void) -{ - return ms_clk; -} - -uint16_t timer_elapsed(uint16_t tlast) -{ - return TIMER_DIFF_16(timer_read(), tlast); -} - -uint32_t timer_elapsed32(uint32_t tlast) -{ - return TIMER_DIFF_32(timer_read32(), tlast); -} - -void timer_clear(void) -{ - set_time(0); -} +void set_time(uint64_t tset) { ms_clk = tset; } + +void timer_init(void) { timer_clear(); } + +uint16_t timer_read(void) { return (uint16_t)ms_clk; } + +uint32_t timer_read32(void) { return (uint32_t)ms_clk; } + +uint64_t timer_read64(void) { return ms_clk; } + +uint16_t timer_elapsed(uint16_t tlast) { return TIMER_DIFF_16(timer_read(), tlast); } + +uint32_t timer_elapsed32(uint32_t tlast) { return TIMER_DIFF_32(timer_read32(), tlast); } + +void timer_clear(void) { set_time(0); } diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 29036f7c5..5f9ecc510 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -9,10 +9,9 @@ #include <avr/boot.h> #ifdef PROTOCOL_LUFA -#include <LUFA/Drivers/USB/USB.h> +# include <LUFA/Drivers/USB/USB.h> #endif - /** \brief Bootloader Size in *bytes* * * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet. @@ -57,19 +56,19 @@ #define FLASH_SIZE (FLASHEND + 1L) #if !defined(BOOTLOADER_SIZE) - uint16_t bootloader_start; +uint16_t bootloader_start; #endif -#define BOOT_SIZE_256 0b110 -#define BOOT_SIZE_512 0b100 +#define BOOT_SIZE_256 0b110 +#define BOOT_SIZE_512 0b100 #define BOOT_SIZE_1024 0b010 #define BOOT_SIZE_2048 0b000 -//compatibility between ATMega8 and ATMega88 -#if !defined (MCUCSR) - #if defined (MCUSR) - #define MCUCSR MCUSR - #endif +// compatibility between ATMega8 and ATMega88 +#if !defined(MCUCSR) +# if defined(MCUSR) +# define MCUCSR MCUSR +# endif #endif /** \brief Entering the Bootloader via Software @@ -77,163 +76,223 @@ * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html */ #define BOOTLOADER_RESET_KEY 0xB007B007 -uint32_t reset_key __attribute__ ((section (".noinit,\"aw\",@nobits;"))); +uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;"))); /** \brief initialize MCU status by watchdog reset * * FIXME: needs doc */ void bootloader_jump(void) { +#if !defined(BOOTLOADER_SIZE) + uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - #if !defined(BOOTLOADER_SIZE) - uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - - if (high_fuse & BOOT_SIZE_256) { - bootloader_start = (FLASH_SIZE - 512) >> 1; - } else if (high_fuse & BOOT_SIZE_512) { - bootloader_start = (FLASH_SIZE - 1024) >> 1; - } else if (high_fuse & BOOT_SIZE_1024) { - bootloader_start = (FLASH_SIZE - 2048) >> 1; - } else { - bootloader_start = (FLASH_SIZE - 4096) >> 1; - } - #endif + if (high_fuse & BOOT_SIZE_256) { + bootloader_start = (FLASH_SIZE - 512) >> 1; + } else if (high_fuse & BOOT_SIZE_512) { + bootloader_start = (FLASH_SIZE - 1024) >> 1; + } else if (high_fuse & BOOT_SIZE_1024) { + bootloader_start = (FLASH_SIZE - 2048) >> 1; + } else { + bootloader_start = (FLASH_SIZE - 4096) >> 1; + } +#endif // Something like this might work, but it compiled larger than the block above // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1)); +#if defined(BOOTLOADER_HALFKAY) + // http://www.pjrc.com/teensy/jump_to_bootloader.html + cli(); + // disable watchdog, if enabled (it's not) + // disable all peripherals + // a shutdown call might make sense here + UDCON = 1; + USBCON = (1 << FRZCLK); // disable USB + UCSR1B = 0; + _delay_ms(5); +# if defined(__AVR_AT90USB162__) // Teensy 1.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + TIMSK0 = 0; + TIMSK1 = 0; + UCSR1B = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + asm volatile("jmp 0x3E00"); +# elif defined(__AVR_ATmega32U4__) // Teensy 2.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK3 = 0; + TIMSK4 = 0; + UCSR1B = 0; + TWCR = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + TWCR = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0x7E00"); +# elif defined(__AVR_AT90USB646__) // Teensy++ 1.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK2 = 0; + TIMSK3 = 0; + UCSR1B = 0; + TWCR = 0; + DDRA = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + PORTA = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0xFC00"); +# elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK2 = 0; + TIMSK3 = 0; + UCSR1B = 0; + TWCR = 0; + DDRA = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + PORTA = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0x1FC00"); +# endif + +#elif defined(BOOTLOADER_CATERINA) + // this block may be optional + // TODO: figure it out + + uint16_t *const bootKeyPtr = (uint16_t *)0x0800; + + // Value used by Caterina bootloader use to determine whether to run the + // sketch or the bootloader programmer. + uint16_t bootKey = 0x7777; + + *bootKeyPtr = bootKey; + + // setup watchdog timeout + wdt_enable(WDTO_60MS); - #if defined(BOOTLOADER_HALFKAY) - // http://www.pjrc.com/teensy/jump_to_bootloader.html - cli(); - // disable watchdog, if enabled (it's not) - // disable all peripherals - // a shutdown call might make sense here - UDCON = 1; - USBCON = (1<<FRZCLK); // disable USB - UCSR1B = 0; - _delay_ms(5); - #if defined(__AVR_AT90USB162__) // Teensy 1.0 - EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; - TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; - DDRB = 0; DDRC = 0; DDRD = 0; - PORTB = 0; PORTC = 0; PORTD = 0; - asm volatile("jmp 0x3E00"); - #elif defined(__AVR_ATmega32U4__) // Teensy 2.0 - EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; - TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0; - DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0; - PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; - asm volatile("jmp 0x7E00"); - #elif defined(__AVR_AT90USB646__) // Teensy++ 1.0 - EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; - TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0; - DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; - PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; - asm volatile("jmp 0xFC00"); - #elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0 - EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0; - TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0; - DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; - PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; - asm volatile("jmp 0x1FC00"); - #endif - - #elif defined(BOOTLOADER_CATERINA) - // this block may be optional - // TODO: figure it out - - uint16_t *const bootKeyPtr = (uint16_t *)0x0800; - - // Value used by Caterina bootloader use to determine whether to run the - // sketch or the bootloader programmer. - uint16_t bootKey = 0x7777; - - *bootKeyPtr = bootKey; - - // setup watchdog timeout - wdt_enable(WDTO_60MS); - - while(1) {} // wait for watchdog timer to trigger - - #elif defined(BOOTLOADER_USBASP) - // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c - wdt_enable(WDTO_15MS); - wdt_reset(); - asm volatile ( - "cli \n\t" - "ldi r29 , %[ramendhi] \n\t" - "ldi r28 , %[ramendlo] \n\t" - #if (FLASHEND>131071) - "ldi r18 , %[bootaddrhi] \n\t" - "st Y+, r18 \n\t" - #endif - "ldi r18 , %[bootaddrme] \n\t" - "st Y+, r18 \n\t" - "ldi r18 , %[bootaddrlo] \n\t" - "st Y+, r18 \n\t" - "out %[mcucsrio], __zero_reg__ \n\t" - "bootloader_startup_loop%=: \n\t" - "rjmp bootloader_startup_loop%= \n\t" - : - : [mcucsrio] "I" (_SFR_IO_ADDR(MCUCSR)), - #if (FLASHEND>131071) - [ramendhi] "M" (((RAMEND - 2) >> 8) & 0xff), - [ramendlo] "M" (((RAMEND - 2) >> 0) & 0xff), - [bootaddrhi] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >>16) & 0xff), - #else - [ramendhi] "M" (((RAMEND - 1) >> 8) & 0xff), - [ramendlo] "M" (((RAMEND - 1) >> 0) & 0xff), - #endif - [bootaddrme] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), - [bootaddrlo] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff) - ); - - #else // Assume remaining boards are DFU, even if the flag isn't set - - #if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? - UDCON = 1; - USBCON = (1<<FRZCLK); // disable USB - UCSR1B = 0; - _delay_ms(5); // 5 seems to work fine - #endif - - #ifdef BOOTLOADER_BOOTLOADHID - // force bootloadHID to stay in bootloader mode, so that it waits - // for a new firmware to be flashed - eeprom_write_byte((uint8_t *)1, 0x00); - #endif - - // watchdog reset - reset_key = BOOTLOADER_RESET_KEY; - wdt_enable(WDTO_250MS); - for (;;); - #endif + while (1) { + } // wait for watchdog timer to trigger +#elif defined(BOOTLOADER_USBASP) + // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c + wdt_enable(WDTO_15MS); + wdt_reset(); + asm volatile("cli \n\t" + "ldi r29 , %[ramendhi] \n\t" + "ldi r28 , %[ramendlo] \n\t" +# if (FLASHEND > 131071) + "ldi r18 , %[bootaddrhi] \n\t" + "st Y+, r18 \n\t" +# endif + "ldi r18 , %[bootaddrme] \n\t" + "st Y+, r18 \n\t" + "ldi r18 , %[bootaddrlo] \n\t" + "st Y+, r18 \n\t" + "out %[mcucsrio], __zero_reg__ \n\t" + "bootloader_startup_loop%=: \n\t" + "rjmp bootloader_startup_loop%= \n\t" + : + : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)), +# if (FLASHEND > 131071) + [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), +# else + [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff), +# endif + [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); + +#else // Assume remaining boards are DFU, even if the flag isn't set + +# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? + UDCON = 1; + USBCON = (1 << FRZCLK); // disable USB + UCSR1B = 0; + _delay_ms(5); // 5 seems to work fine +# endif + +# ifdef BOOTLOADER_BOOTLOADHID + // force bootloadHID to stay in bootloader mode, so that it waits + // for a new firmware to be flashed + eeprom_write_byte((uint8_t *)1, 0x00); +# endif + + // watchdog reset + reset_key = BOOTLOADER_RESET_KEY; + wdt_enable(WDTO_250MS); + for (;;) + ; +#endif } /* this runs before main() */ -void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3"))); -void bootloader_jump_after_watchdog_reset(void) -{ - #ifndef BOOTLOADER_HALFKAY - if ((MCUCSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) { - reset_key = 0; - - // My custom USBasploader requires this to come up. - MCUCSR = 0; - - // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog. - MCUCSR &= ~(1<<WDRF); - wdt_disable(); - - - // This is compled into 'icall', address should be in word unit, not byte. - #ifdef BOOTLOADER_SIZE - ((void (*)(void))( (FLASH_SIZE - BOOTLOADER_SIZE) >> 1))(); - #else - asm("ijmp" :: "z" (bootloader_start)); - #endif - } - #endif +void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3"))); +void bootloader_jump_after_watchdog_reset(void) { +#ifndef BOOTLOADER_HALFKAY + if ((MCUCSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) { + reset_key = 0; + + // My custom USBasploader requires this to come up. + MCUCSR = 0; + + // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog. + MCUCSR &= ~(1 << WDRF); + wdt_disable(); + +// This is compled into 'icall', address should be in word unit, not byte. +# ifdef BOOTLOADER_SIZE + ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))(); +# else + asm("ijmp" ::"z"(bootloader_start)); +# endif + } +#endif } diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c index 0cb774c81..61fa70dc3 100644 --- a/tmk_core/common/avr/sleep_led.c +++ b/tmk_core/common/avr/sleep_led.c @@ -16,14 +16,13 @@ * 256*64 interrupts/second * F_CPU/(256*64) clocks/interrupt */ -#define SLEEP_LED_TIMER_TOP F_CPU/(256*64) +#define SLEEP_LED_TIMER_TOP F_CPU / (256 * 64) /** \brief Sleep LED initialization * * FIXME: needs doc */ -void sleep_led_init(void) -{ +void sleep_led_init(void) { /* Timer1 setup */ /* CTC mode */ TCCR1B |= _BV(WGM12); @@ -32,17 +31,16 @@ void sleep_led_init(void) /* Set TOP value */ uint8_t sreg = SREG; cli(); - OCR1AH = (SLEEP_LED_TIMER_TOP>>8)&0xff; - OCR1AL = SLEEP_LED_TIMER_TOP&0xff; - SREG = sreg; + OCR1AH = (SLEEP_LED_TIMER_TOP >> 8) & 0xff; + OCR1AL = SLEEP_LED_TIMER_TOP & 0xff; + SREG = sreg; } /** \brief Sleep LED enable * * FIXME: needs doc */ -void sleep_led_enable(void) -{ +void sleep_led_enable(void) { /* Enable Compare Match Interrupt */ TIMSK1 |= _BV(OCIE1A); } @@ -51,8 +49,7 @@ void sleep_led_enable(void) * * FIXME: needs doc */ -void sleep_led_disable(void) -{ +void sleep_led_disable(void) { /* Disable Compare Match Interrupt */ TIMSK1 &= ~_BV(OCIE1A); } @@ -61,13 +58,11 @@ void sleep_led_disable(void) * * FIXME: needs doc */ -void sleep_led_toggle(void) -{ +void sleep_led_toggle(void) { /* Disable Compare Match Interrupt */ TIMSK1 ^= _BV(OCIE1A); } - /** \brief Breathing Sleep LED brighness(PWM On period) table * * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle @@ -75,15 +70,9 @@ void sleep_led_toggle(void) * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } */ -static const uint8_t breathing_table[64] PROGMEM = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, -15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, -255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, -15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; +static const uint8_t breathing_table[64] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -ISR(TIMER1_COMPA_vect) -{ +ISR(TIMER1_COMPA_vect) { /* Software PWM * timer:1111 1111 1111 1111 * \_____/\/ \_______/____ count(0-255) @@ -93,17 +82,17 @@ ISR(TIMER1_COMPA_vect) static union { uint16_t row; struct { - uint8_t count:8; - uint8_t duration:2; - uint8_t index:6; + uint8_t count : 8; + uint8_t duration : 2; + uint8_t index : 6; } pwm; - } timer = { .row = 0 }; + } timer = {.row = 0}; timer.row++; - + // LED on if (timer.pwm.count == 0) { - led_set(1<<USB_LED_CAPS_LOCK); + led_set(1 << USB_LED_CAPS_LOCK); } // LED off if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) { diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 2259201b5..574000fcd 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -13,37 +13,36 @@ #include "rgblight_reconfig.h" #ifdef PROTOCOL_LUFA - #include "lufa.h" +# include "lufa.h" #endif #ifdef AUDIO_ENABLE - #include "audio.h" +# include "audio.h" #endif /* AUDIO_ENABLE */ #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - #include "rgblight.h" - extern rgblight_config_t rgblight_config; - static bool rgblight_enabled; - static bool is_suspended; +# include "rgblight.h" +extern rgblight_config_t rgblight_config; +static bool rgblight_enabled; +static bool is_suspended; #endif - -#define wdt_intr_enable(value) \ -__asm__ __volatile__ ( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %0,%1" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - "sts %0,%2" "\n\t" \ - : /* no outputs */ \ - : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ - "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ - "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ - _BV(WDIE) | (value & 0x07)) ) \ - : "r0" \ -) - +#define wdt_intr_enable(value) \ + __asm__ __volatile__("in __tmp_reg__,__SREG__" \ + "\n\t" \ + "cli" \ + "\n\t" \ + "wdr" \ + "\n\t" \ + "sts %0,%1" \ + "\n\t" \ + "out __SREG__,__tmp_reg__" \ + "\n\t" \ + "sts %0,%2" \ + "\n\t" \ + : /* no outputs */ \ + : "M"(_SFR_MEM_ADDR(_WD_CONTROL_REG)), "r"(_BV(_WD_CHANGE_BIT) | _BV(WDE)), "r"((uint8_t)((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ + : "r0") /** \brief Suspend idle * @@ -58,23 +57,18 @@ void suspend_idle(uint8_t time) { sleep_disable(); } - // TODO: This needs some cleanup /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_user (void) { } +__attribute__((weak)) void suspend_power_down_user(void) {} /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_kb(void) { - suspend_power_down_user(); -} +__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); } #ifndef NO_SUSPEND_POWER_DOWN /** \brief Power down MCU with watchdog timer @@ -98,43 +92,43 @@ static uint8_t wdt_timeout = 0; * FIXME: needs doc */ static void power_down(uint8_t wdto) { -#ifdef PROTOCOL_LUFA - if (USB_DeviceState == DEVICE_STATE_Configured) return; -#endif - wdt_timeout = wdto; - - // Watchdog Interrupt Mode - wdt_intr_enable(wdto); - -#ifdef BACKLIGHT_ENABLE - backlight_set(0); -#endif - - // Turn off LED indicators - uint8_t leds_off = 0; -#if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) - if (is_backlight_enabled()) { - // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off - leds_off |= (1<<USB_LED_CAPS_LOCK); - } -#endif - led_set(leds_off); - -#ifdef AUDIO_ENABLE - // This sometimes disables the start-up noise, so it's been disabled - // stop_all_notes(); -#endif /* AUDIO_ENABLE */ -#if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) -#ifdef RGBLIGHT_ANIMATIONS - rgblight_timer_disable(); -#endif - if (!is_suspended) { - is_suspended = true; - rgblight_enabled = rgblight_config.enable; - rgblight_disable_noeeprom(); - } -#endif - suspend_power_down_kb(); +# ifdef PROTOCOL_LUFA + if (USB_DeviceState == DEVICE_STATE_Configured) return; +# endif + wdt_timeout = wdto; + + // Watchdog Interrupt Mode + wdt_intr_enable(wdto); + +# ifdef BACKLIGHT_ENABLE + backlight_set(0); +# endif + + // Turn off LED indicators + uint8_t leds_off = 0; +# if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) + if (is_backlight_enabled()) { + // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off + leds_off |= (1 << USB_LED_CAPS_LOCK); + } +# endif + led_set(leds_off); + +# ifdef AUDIO_ENABLE + // This sometimes disables the start-up noise, so it's been disabled + // stop_all_notes(); +# endif /* AUDIO_ENABLE */ +# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) +# ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_disable(); +# endif + if (!is_suspended) { + is_suspended = true; + rgblight_enabled = rgblight_config.enable; + rgblight_disable_noeeprom(); + } +# endif + suspend_power_down_kb(); // TODO: more power saving // See PicoPower application note @@ -158,40 +152,36 @@ static void power_down(uint8_t wdto) { * FIXME: needs doc */ void suspend_power_down(void) { - suspend_power_down_kb(); + suspend_power_down_kb(); #ifndef NO_SUSPEND_POWER_DOWN power_down(WDTO_15MS); #endif } -__attribute__ ((weak)) void matrix_power_up(void) {} -__attribute__ ((weak)) void matrix_power_down(void) {} -bool suspend_wakeup_condition(void) { +__attribute__((weak)) void matrix_power_up(void) {} +__attribute__((weak)) void matrix_power_down(void) {} +bool suspend_wakeup_condition(void) { matrix_power_up(); matrix_scan(); matrix_power_down(); for (uint8_t r = 0; r < MATRIX_ROWS; r++) { if (matrix_get_row(r)) return true; } - return false; + return false; } /** \brief run user level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_user(void) { } +__attribute__((weak)) void suspend_wakeup_init_user(void) {} /** \brief run keyboard level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_kb(void) { - suspend_wakeup_init_user(); -} +__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); } /** \brief run immediately after wakeup * * FIXME: needs doc @@ -202,18 +192,18 @@ void suspend_wakeup_init(void) { #ifdef BACKLIGHT_ENABLE backlight_init(); #endif - led_set(host_keyboard_leds()); + led_set(host_keyboard_leds()); #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - is_suspended = false; - if (rgblight_enabled) { - #ifdef BOOTLOADER_TEENSY - wait_ms(10); - #endif - rgblight_enable_noeeprom(); - } -#ifdef RGBLIGHT_ANIMATIONS - rgblight_timer_enable(); -#endif + is_suspended = false; + if (rgblight_enabled) { +# ifdef BOOTLOADER_TEENSY + wait_ms(10); +# endif + rgblight_enable_noeeprom(); + } +# ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_enable(); +# endif #endif suspend_wakeup_init_kb(); } @@ -226,8 +216,7 @@ ISR(WDT_vect) { case WDTO_15MS: timer_count += 15 + 2; // WDTO_15MS + 2(from observation) break; - default: - ; + default:; } } #endif diff --git a/tmk_core/common/avr/suspend_avr.h b/tmk_core/common/avr/suspend_avr.h index 357102da4..d73852ba1 100644 --- a/tmk_core/common/avr/suspend_avr.h +++ b/tmk_core/common/avr/suspend_avr.h @@ -7,21 +7,21 @@ #include <avr/wdt.h> #include <avr/interrupt.h> - -#define wdt_intr_enable(value) \ -__asm__ __volatile__ ( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %0,%1" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - "sts %0,%2" "\n\t" \ - : /* no outputs */ \ - : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ - "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ - "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ - _BV(WDIE) | (value & 0x07)) ) \ - : "r0" \ -) +#define wdt_intr_enable(value) \ + __asm__ __volatile__("in __tmp_reg__,__SREG__" \ + "\n\t" \ + "cli" \ + "\n\t" \ + "wdr" \ + "\n\t" \ + "sts %0,%1" \ + "\n\t" \ + "out __SREG__,__tmp_reg__" \ + "\n\t" \ + "sts %0,%2" \ + "\n\t" \ + : /* no outputs */ \ + : "M"(_SFR_MEM_ADDR(_WD_CONTROL_REG)), "r"(_BV(_WD_CHANGE_BIT) | _BV(WDE)), "r"((uint8_t)((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ + : "r0") #endif diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c index b7d4f060e..63ec549df 100644 --- a/tmk_core/common/avr/timer.c +++ b/tmk_core/common/avr/timer.c @@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "timer_avr.h" #include "timer.h" - // counter resolution 1ms // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} volatile uint32_t timer_count; @@ -31,8 +30,7 @@ volatile uint32_t timer_count; * * FIXME: needs doc */ -void timer_init(void) -{ +void timer_init(void) { #if TIMER_PRESCALER == 1 uint8_t prescaler = 0x01; #elif TIMER_PRESCALER == 8 @@ -44,7 +42,7 @@ void timer_init(void) #elif TIMER_PRESCALER == 1024 uint8_t prescaler = 0x05; #else -# error "Timer prescaler value is NOT vaild." +# error "Timer prescaler value is NOT vaild." #endif #ifndef __AVR_ATmega32A__ @@ -53,13 +51,13 @@ void timer_init(void) TCCR0B = prescaler; - OCR0A = TIMER_RAW_TOP; - TIMSK0 = (1<<OCIE0A); + OCR0A = TIMER_RAW_TOP; + TIMSK0 = (1 << OCIE0A); #else // Timer0 CTC mode TCCR0 = (1 << WGM01) | prescaler; - OCR0 = TIMER_RAW_TOP; + OCR0 = TIMER_RAW_TOP; TIMSK = (1 << OCIE0); #endif } @@ -68,26 +66,18 @@ void timer_init(void) * * FIXME: needs doc */ -inline -void timer_clear(void) -{ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - timer_count = 0; - } +inline void timer_clear(void) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; } } /** \brief timer read * * FIXME: needs doc */ -inline -uint16_t timer_read(void) -{ +inline uint16_t timer_read(void) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return (t & 0xFFFF); } @@ -96,14 +86,10 @@ uint16_t timer_read(void) * * FIXME: needs doc */ -inline -uint32_t timer_read32(void) -{ +inline uint32_t timer_read32(void) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return t; } @@ -112,14 +98,10 @@ uint32_t timer_read32(void) * * FIXME: needs doc */ -inline -uint16_t timer_elapsed(uint16_t last) -{ +inline uint16_t timer_elapsed(uint16_t last) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return TIMER_DIFF_16((t & 0xFFFF), last); } @@ -128,25 +110,18 @@ uint16_t timer_elapsed(uint16_t last) * * FIXME: needs doc */ -inline -uint32_t timer_elapsed32(uint32_t last) -{ +inline uint32_t timer_elapsed32(uint32_t last) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return TIMER_DIFF_32(t, last); } // excecuted once per 1ms.(excess for just timer count?) #ifndef __AVR_ATmega32A__ -#define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect +# define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect #else -#define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect +# define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect #endif -ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) -{ - timer_count++; -} +ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; } diff --git a/tmk_core/common/avr/timer_avr.h b/tmk_core/common/avr/timer_avr.h index 0e85eb101..9aea21c65 100644 --- a/tmk_core/common/avr/timer_avr.h +++ b/tmk_core/common/avr/timer_avr.h @@ -21,22 +21,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #ifndef TIMER_PRESCALER -# if F_CPU > 16000000 -# define TIMER_PRESCALER 256 -# elif F_CPU > 2000000 -# define TIMER_PRESCALER 64 -# elif F_CPU > 250000 -# define TIMER_PRESCALER 8 -# else -# define TIMER_PRESCALER 1 -# endif +# if F_CPU > 16000000 +# define TIMER_PRESCALER 256 +# elif F_CPU > 2000000 +# define TIMER_PRESCALER 64 +# elif F_CPU > 250000 +# define TIMER_PRESCALER 8 +# else +# define TIMER_PRESCALER 1 +# endif #endif -#define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER) -#define TIMER_RAW TCNT0 -#define TIMER_RAW_TOP (TIMER_RAW_FREQ/1000) +#define TIMER_RAW_FREQ (F_CPU / TIMER_PRESCALER) +#define TIMER_RAW TCNT0 +#define TIMER_RAW_TOP (TIMER_RAW_FREQ / 1000) #if (TIMER_RAW_TOP > 255) -# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." +# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." #endif #endif diff --git a/tmk_core/common/avr/xprintf.h b/tmk_core/common/avr/xprintf.h index 08d9f93a0..70e0f8e48 100644 --- a/tmk_core/common/avr/xprintf.h +++ b/tmk_core/common/avr/xprintf.h @@ -13,7 +13,7 @@ extern "C" { #endif extern void (*xfunc_out)(uint8_t); -#define xdev_out(func) xfunc_out = (void(*)(uint8_t))(func) +#define xdev_out(func) xfunc_out = (void (*)(uint8_t))(func) /* This is a pointer to user defined output function. It must be initialized before using this modle. @@ -25,13 +25,11 @@ void xputc(char chr); All outputs from this module are output via this function. */ - /*-----------------------------------------------------------------------------*/ void xputs(const char *string_p); /* The string placed in the ROM is forwarded to xputc() directly. -*/ - + */ /*-----------------------------------------------------------------------------*/ void xitoa(long value, char radix, char width); @@ -49,13 +47,12 @@ void xitoa(long value, char radix, char width); 0x55 2 -8 "01010101" */ - /*-----------------------------------------------------------------------------*/ -#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__) -#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__) -#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__) +#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__) +#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__) +#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__) -void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */ +void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */ // void __xsprintf(char*, const char *format_p, ...); /* Put formatted string to the memory */ // void __xfprintf(void(*func)(uint8_t), const char *format_p, ...); /* Send formatted string to the specified device */ @@ -84,7 +81,6 @@ void __xprintf(const char *format_p, ...); /* Send formatted string to the regis */ - /*-----------------------------------------------------------------------------*/ char xatoi(char **str, long *ret); @@ -108,4 +104,3 @@ char xatoi(char **str, long *ret); #endif #endif - diff --git a/tmk_core/common/backlight.c b/tmk_core/common/backlight.c index c0e9fb5ee..708022f68 100644 --- a/tmk_core/common/backlight.c +++ b/tmk_core/common/backlight.c @@ -25,15 +25,14 @@ backlight_config_t backlight_config; * * FIXME: needs doc */ -void backlight_init(void) -{ +void backlight_init(void) { /* check signature */ if (!eeconfig_is_enabled()) { eeconfig_init(); } backlight_config.raw = eeconfig_read_backlight(); if (backlight_config.level > BACKLIGHT_LEVELS) { - backlight_config.level = BACKLIGHT_LEVELS; + backlight_config.level = BACKLIGHT_LEVELS; } backlight_set(backlight_config.enable ? backlight_config.level : 0); } @@ -42,10 +41,8 @@ void backlight_init(void) * * FIXME: needs doc */ -void backlight_increase(void) -{ - if(backlight_config.level < BACKLIGHT_LEVELS) - { +void backlight_increase(void) { + if (backlight_config.level < BACKLIGHT_LEVELS) { backlight_config.level++; } backlight_config.enable = 1; @@ -58,10 +55,8 @@ void backlight_increase(void) * * FIXME: needs doc */ -void backlight_decrease(void) -{ - if(backlight_config.level > 0) - { +void backlight_decrease(void) { + if (backlight_config.level > 0) { backlight_config.level--; backlight_config.enable = !!backlight_config.level; eeconfig_update_backlight(backlight_config.raw); @@ -74,64 +69,56 @@ void backlight_decrease(void) * * FIXME: needs doc */ -void backlight_toggle(void) -{ - bool enabled = backlight_config.enable; - dprintf("backlight toggle: %u\n", enabled); - if (enabled) - backlight_disable(); - else - backlight_enable(); +void backlight_toggle(void) { + bool enabled = backlight_config.enable; + dprintf("backlight toggle: %u\n", enabled); + if (enabled) + backlight_disable(); + else + backlight_enable(); } /** \brief Enable backlight * * FIXME: needs doc */ -void backlight_enable(void) -{ - if (backlight_config.enable) return; // do nothing if backlight is already on - - backlight_config.enable = true; - if (backlight_config.raw == 1) // enabled but level == 0 - backlight_config.level = 1; - eeconfig_update_backlight(backlight_config.raw); - dprintf("backlight enable\n"); - backlight_set(backlight_config.level); +void backlight_enable(void) { + if (backlight_config.enable) return; // do nothing if backlight is already on + + backlight_config.enable = true; + if (backlight_config.raw == 1) // enabled but level == 0 + backlight_config.level = 1; + eeconfig_update_backlight(backlight_config.raw); + dprintf("backlight enable\n"); + backlight_set(backlight_config.level); } /** \brief Disable backlight * * FIXME: needs doc */ -void backlight_disable(void) -{ - if (!backlight_config.enable) return; // do nothing if backlight is already off - - backlight_config.enable = false; - eeconfig_update_backlight(backlight_config.raw); - dprintf("backlight disable\n"); - backlight_set(0); +void backlight_disable(void) { + if (!backlight_config.enable) return; // do nothing if backlight is already off + + backlight_config.enable = false; + eeconfig_update_backlight(backlight_config.raw); + dprintf("backlight disable\n"); + backlight_set(0); } /** /brief Get the backlight status * * FIXME: needs doc */ -bool is_backlight_enabled(void) -{ - return backlight_config.enable; -} +bool is_backlight_enabled(void) { return backlight_config.enable; } /** \brief Backlight step through levels * * FIXME: needs doc */ -void backlight_step(void) -{ +void backlight_step(void) { backlight_config.level++; - if(backlight_config.level > BACKLIGHT_LEVELS) - { + if (backlight_config.level > BACKLIGHT_LEVELS) { backlight_config.level = 0; } backlight_config.enable = !!backlight_config.level; @@ -144,11 +131,9 @@ void backlight_step(void) * * FIXME: needs doc */ -void backlight_level(uint8_t level) -{ - if (level > BACKLIGHT_LEVELS) - level = BACKLIGHT_LEVELS; - backlight_config.level = level; +void backlight_level(uint8_t level) { + if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; + backlight_config.level = level; backlight_config.enable = !!backlight_config.level; eeconfig_update_backlight(backlight_config.raw); backlight_set(backlight_config.level); @@ -158,21 +143,17 @@ void backlight_level(uint8_t level) * * FIXME: needs doc */ -uint8_t get_backlight_level(void) -{ - return backlight_config.level; -} +uint8_t get_backlight_level(void) { return backlight_config.level; } #ifdef BACKLIGHT_BREATHING /** \brief Backlight breathing toggle * * FIXME: needs doc */ -void backlight_toggle_breathing(void) -{ +void backlight_toggle_breathing(void) { bool breathing = backlight_config.breathing; dprintf("backlight breathing toggle: %u\n", breathing); - if (breathing) + if (breathing) backlight_disable_breathing(); else backlight_enable_breathing(); @@ -182,9 +163,8 @@ void backlight_toggle_breathing(void) * * FIXME: needs doc */ -void backlight_enable_breathing(void) -{ - if (backlight_config.breathing) return; // do nothing if breathing is already on +void backlight_enable_breathing(void) { + if (backlight_config.breathing) return; // do nothing if breathing is already on backlight_config.breathing = true; eeconfig_update_backlight(backlight_config.raw); @@ -196,9 +176,8 @@ void backlight_enable_breathing(void) * * FIXME: needs doc */ -void backlight_disable_breathing(void) -{ - if (!backlight_config.breathing) return; // do nothing if breathing is already off +void backlight_disable_breathing(void) { + if (!backlight_config.breathing) return; // do nothing if breathing is already off backlight_config.breathing = false; eeconfig_update_backlight(backlight_config.raw); @@ -210,8 +189,5 @@ void backlight_disable_breathing(void) * * FIXME: needs doc */ -bool is_backlight_breathing(void) -{ - return backlight_config.breathing; -} +bool is_backlight_breathing(void) { return backlight_config.breathing; } #endif diff --git a/tmk_core/common/backlight.h b/tmk_core/common/backlight.h index 0a3877120..bb1f897ee 100644 --- a/tmk_core/common/backlight.h +++ b/tmk_core/common/backlight.h @@ -21,31 +21,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> #ifndef BACKLIGHT_LEVELS - #define BACKLIGHT_LEVELS 3 +# define BACKLIGHT_LEVELS 3 #elif BACKLIGHT_LEVELS > 31 - #error "Maximum value of BACKLIGHT_LEVELS is 31" +# error "Maximum value of BACKLIGHT_LEVELS is 31" #endif typedef union { uint8_t raw; struct { - bool enable :1; - bool breathing :1; - uint8_t reserved :1; // Reserved for possible future backlight modes - uint8_t level :5; + bool enable : 1; + bool breathing : 1; + uint8_t reserved : 1; // Reserved for possible future backlight modes + uint8_t level : 5; }; } backlight_config_t; -void backlight_init(void); -void backlight_increase(void); -void backlight_decrease(void); -void backlight_toggle(void); -void backlight_enable(void); -void backlight_disable(void); -bool is_backlight_enabled(void); -void backlight_step(void); -void backlight_set(uint8_t level); -void backlight_level(uint8_t level); +void backlight_init(void); +void backlight_increase(void); +void backlight_decrease(void); +void backlight_toggle(void); +void backlight_enable(void); +void backlight_disable(void); +bool is_backlight_enabled(void); +void backlight_step(void); +void backlight_set(uint8_t level); +void backlight_level(uint8_t level); uint8_t get_backlight_level(void); #ifdef BACKLIGHT_BREATHING diff --git a/tmk_core/common/bootloader.h b/tmk_core/common/bootloader.h index 44775039d..19260a079 100644 --- a/tmk_core/common/bootloader.h +++ b/tmk_core/common/bootloader.h @@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef BOOTLOADER_H #define BOOTLOADER_H - /* give code for your bootloader to come up if needed */ void bootloader_jump(void); diff --git a/tmk_core/common/bootmagic.c b/tmk_core/common/bootmagic.c index cc780d17a..09b1664c9 100644 --- a/tmk_core/common/bootmagic.c +++ b/tmk_core/common/bootmagic.c @@ -16,8 +16,7 @@ keymap_config_t keymap_config; * * FIXME: needs doc */ -void bootmagic(void) -{ +void bootmagic(void) { /* check signature */ if (!eeconfig_is_enabled()) { eeconfig_init(); @@ -26,7 +25,10 @@ void bootmagic(void) /* do scans in case of bounce */ print("bootmagic scan: ... "); uint8_t scan = 100; - while (scan--) { matrix_scan(); wait_ms(10); } + while (scan--) { + matrix_scan(); + wait_ms(10); + } print("done.\n"); /* bootmagic skip */ @@ -89,14 +91,30 @@ void bootmagic(void) /* default layer */ uint8_t default_layer = 0; - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { + default_layer |= (1 << 0); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { + default_layer |= (1 << 1); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { + default_layer |= (1 << 2); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { + default_layer |= (1 << 3); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { + default_layer |= (1 << 4); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { + default_layer |= (1 << 5); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { + default_layer |= (1 << 6); + } + if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { + default_layer |= (1 << 7); + } if (default_layer) { eeconfig_update_default_layer(default_layer); default_layer_set((layer_state_t)default_layer); @@ -110,13 +128,12 @@ void bootmagic(void) * * FIXME: needs doc */ -static bool scan_keycode(uint8_t keycode) -{ +static bool scan_keycode(uint8_t keycode) { for (uint8_t r = 0; r < MATRIX_ROWS; r++) { matrix_row_t matrix_row = matrix_get_row(r); for (uint8_t c = 0; c < MATRIX_COLS; c++) { - if (matrix_row & ((matrix_row_t)1<<c)) { - if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) { + if (matrix_row & ((matrix_row_t)1 << c)) { + if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) { return true; } } @@ -129,8 +146,7 @@ static bool scan_keycode(uint8_t keycode) * * FIXME: needs doc */ -bool bootmagic_scan_keycode(uint8_t keycode) -{ +bool bootmagic_scan_keycode(uint8_t keycode) { if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false; return scan_keycode(keycode); diff --git a/tmk_core/common/bootmagic.h b/tmk_core/common/bootmagic.h index f3ea6a24d..fb7647257 100644 --- a/tmk_core/common/bootmagic.h +++ b/tmk_core/common/bootmagic.h @@ -1,101 +1,98 @@ #ifndef BOOTMAGIC_H #define BOOTMAGIC_H - /* FIXME: Add special doxygen comments for defines here. */ /* bootmagic salt key */ #ifndef BOOTMAGIC_KEY_SALT -#define BOOTMAGIC_KEY_SALT KC_SPACE +# define BOOTMAGIC_KEY_SALT KC_SPACE #endif /* skip bootmagic and eeconfig */ #ifndef BOOTMAGIC_KEY_SKIP -#define BOOTMAGIC_KEY_SKIP KC_ESC +# define BOOTMAGIC_KEY_SKIP KC_ESC #endif /* eeprom clear */ #ifndef BOOTMAGIC_KEY_EEPROM_CLEAR -#define BOOTMAGIC_KEY_EEPROM_CLEAR KC_BSPACE +# define BOOTMAGIC_KEY_EEPROM_CLEAR KC_BSPACE #endif /* kick up bootloader */ #ifndef BOOTMAGIC_KEY_BOOTLOADER -#define BOOTMAGIC_KEY_BOOTLOADER KC_B +# define BOOTMAGIC_KEY_BOOTLOADER KC_B #endif /* debug enable */ #ifndef BOOTMAGIC_KEY_DEBUG_ENABLE -#define BOOTMAGIC_KEY_DEBUG_ENABLE KC_D +# define BOOTMAGIC_KEY_DEBUG_ENABLE KC_D #endif #ifndef BOOTMAGIC_KEY_DEBUG_MATRIX -#define BOOTMAGIC_KEY_DEBUG_MATRIX KC_X +# define BOOTMAGIC_KEY_DEBUG_MATRIX KC_X #endif #ifndef BOOTMAGIC_KEY_DEBUG_KEYBOARD -#define BOOTMAGIC_KEY_DEBUG_KEYBOARD KC_K +# define BOOTMAGIC_KEY_DEBUG_KEYBOARD KC_K #endif #ifndef BOOTMAGIC_KEY_DEBUG_MOUSE -#define BOOTMAGIC_KEY_DEBUG_MOUSE KC_M +# define BOOTMAGIC_KEY_DEBUG_MOUSE KC_M #endif /* * keymap config */ #ifndef BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK -#define BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK KC_LCTRL +# define BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK KC_LCTRL #endif #ifndef BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL -#define BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL KC_CAPSLOCK +# define BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL KC_CAPSLOCK #endif #ifndef BOOTMAGIC_KEY_SWAP_LALT_LGUI -#define BOOTMAGIC_KEY_SWAP_LALT_LGUI KC_LALT +# define BOOTMAGIC_KEY_SWAP_LALT_LGUI KC_LALT #endif #ifndef BOOTMAGIC_KEY_SWAP_RALT_RGUI -#define BOOTMAGIC_KEY_SWAP_RALT_RGUI KC_RALT +# define BOOTMAGIC_KEY_SWAP_RALT_RGUI KC_RALT #endif #ifndef BOOTMAGIC_KEY_NO_GUI -#define BOOTMAGIC_KEY_NO_GUI KC_LGUI +# define BOOTMAGIC_KEY_NO_GUI KC_LGUI #endif #ifndef BOOTMAGIC_KEY_SWAP_GRAVE_ESC -#define BOOTMAGIC_KEY_SWAP_GRAVE_ESC KC_GRAVE +# define BOOTMAGIC_KEY_SWAP_GRAVE_ESC KC_GRAVE #endif #ifndef BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE -#define BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE KC_BSLASH +# define BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE KC_BSLASH #endif #ifndef BOOTMAGIC_HOST_NKRO -#define BOOTMAGIC_HOST_NKRO KC_N +# define BOOTMAGIC_HOST_NKRO KC_N #endif - /* * change default layer */ #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_0 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_0 KC_0 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_0 KC_0 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_1 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_1 KC_1 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_1 KC_1 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_2 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_2 KC_2 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_2 KC_2 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_3 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_3 KC_3 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_3 KC_3 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_4 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_4 KC_4 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_4 KC_4 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_5 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_5 KC_5 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_5 KC_5 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_6 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_6 KC_6 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_6 KC_6 #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_7 -#define BOOTMAGIC_KEY_DEFAULT_LAYER_7 KC_7 +# define BOOTMAGIC_KEY_DEFAULT_LAYER_7 KC_7 #endif - void bootmagic(void); bool bootmagic_scan_keycode(uint8_t keycode); diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c index f9895237b..4cf5dae7e 100644 --- a/tmk_core/common/chibios/bootloader.c +++ b/tmk_core/common/chibios/bootloader.c @@ -7,63 +7,62 @@ /* STM32 */ /* This code should be checked whether it runs correctly on platforms */ -#define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) +# define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) extern uint32_t __ram0_end__; -#define BOOTLOADER_MAGIC 0xDEADBEEF -#define MAGIC_ADDR (unsigned long*)(SYMVAL(__ram0_end__) - 4) - +# define BOOTLOADER_MAGIC 0xDEADBEEF +# define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4) /** \brief Jump to the bootloader * * FIXME: needs doc */ void bootloader_jump(void) { - *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader - NVIC_SystemReset(); + *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader + NVIC_SystemReset(); } /** \brief Enter bootloader mode if requested * * FIXME: needs doc */ -void enter_bootloader_mode_if_requested(void) { - unsigned long* check = MAGIC_ADDR; - if(*check == BOOTLOADER_MAGIC) { - *check = 0; - __set_CONTROL(0); - __set_MSP(*(__IO uint32_t*)STM32_BOOTLOADER_ADDRESS); - __enable_irq(); +void enter_bootloader_mode_if_requested(void) { + unsigned long *check = MAGIC_ADDR; + if (*check == BOOTLOADER_MAGIC) { + *check = 0; + __set_CONTROL(0); + __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS); + __enable_irq(); - typedef void (*BootJump_t)(void); - BootJump_t boot_jump = *(BootJump_t*)(STM32_BOOTLOADER_ADDRESS + 4); - boot_jump(); - while(1); - } - } + typedef void (*BootJump_t)(void); + BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4); + boot_jump(); + while (1) + ; + } +} #elif defined(KL2x) || defined(K20x) /* STM32_BOOTLOADER_ADDRESS */ /* Kinetis */ -#if defined(KIIBOHD_BOOTLOADER) +# if defined(KIIBOHD_BOOTLOADER) /* Kiibohd Bootloader (MCHCK and Infinity KB) */ -#define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 +# define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; -void bootloader_jump(void) { - __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic)); - // request reset - SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; +void bootloader_jump(void) { + __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic)); + // request reset + SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; } -#else /* defined(KIIBOHD_BOOTLOADER) */ +# else /* defined(KIIBOHD_BOOTLOADER) */ /* Default for Kinetis - expecting an ARM Teensy */ -#include "wait.h" +# include "wait.h" void bootloader_jump(void) { - wait_ms(100); - __BKPT(0); + wait_ms(100); + __BKPT(0); } -#endif /* defined(KIIBOHD_BOOTLOADER) */ +# endif /* defined(KIIBOHD_BOOTLOADER) */ #else /* neither STM32 nor KINETIS */ -__attribute__((weak)) -void bootloader_jump(void) {} +__attribute__((weak)) void bootloader_jump(void) {} #endif diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c index 4b1abc968..926b581c6 100755..100644 --- a/tmk_core/common/chibios/eeprom_stm32.c +++ b/tmk_core/common/chibios/eeprom_stm32.c @@ -24,7 +24,7 @@ * the functionality use the EEPROM_Init() function. Be sure that by reprogramming * of the controller just affected pages will be deleted. In other case the non * volatile data will be lost. -******************************************************************************/ + ******************************************************************************/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ @@ -32,23 +32,22 @@ uint8_t DataBuf[FEE_PAGE_SIZE]; /***************************************************************************** -* Delete Flash Space used for user Data, deletes the whole space between -* RW_PAGE_BASE_ADDRESS and the last uC Flash Page -******************************************************************************/ + * Delete Flash Space used for user Data, deletes the whole space between + * RW_PAGE_BASE_ADDRESS and the last uC Flash Page + ******************************************************************************/ uint16_t EEPROM_Init(void) { // unlock flash FLASH_Unlock(); // Clear Flags - //FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); + // FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); return FEE_DENSITY_BYTES; } /***************************************************************************** -* Erase the whole reserved Flash Space used for user Data -******************************************************************************/ -void EEPROM_Erase (void) { - + * Erase the whole reserved Flash Space used for user Data + ******************************************************************************/ +void EEPROM_Erase(void) { int page_num = 0; // delete all pages from specified start page to the last page @@ -58,16 +57,15 @@ void EEPROM_Erase (void) { } while (page_num < FEE_DENSITY_PAGES); } /***************************************************************************** -* Writes once data byte to flash on specified address. If a byte is already -* written, the whole page must be copied to a buffer, the byte changed and -* the manipulated buffer written after PageErase. -*******************************************************************************/ -uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) { - + * Writes once data byte to flash on specified address. If a byte is already + * written, the whole page must be copied to a buffer, the byte changed and + * the manipulated buffer written after PageErase. + *******************************************************************************/ +uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { FLASH_Status FlashStatus = FLASH_COMPLETE; uint32_t page; - int i; + int i; // exit if desired address is above the limit (e.G. under 2048 Bytes for 4 pages) if (Address > FEE_DENSITY_BYTES) { @@ -78,27 +76,25 @@ uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) { page = FEE_ADDR_OFFSET(Address) / FEE_PAGE_SIZE; // if current data is 0xFF, the byte is empty, just overwrite with the new one - if ((*(__IO uint16_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) { - + if ((*(__IO uint16_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) { FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte)); } else { - // Copy Page to a buffer - memcpy(DataBuf, (uint8_t*)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page + memcpy(DataBuf, (uint8_t *)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page // check if new data is differ to current data, return if not, proceed if yes - if (DataByte == *(__IO uint8_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) { + if (DataByte == *(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) { return 0; } // manipulate desired data byte in temp data array if new byte is differ to the current DataBuf[FEE_ADDR_OFFSET(Address) % FEE_PAGE_SIZE] = DataByte; - //Erase Page + // Erase Page FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)); // Write new data (whole page) to flash if data has been changed - for(i = 0; i < (FEE_PAGE_SIZE / 2); i++) { + for (i = 0; i < (FEE_PAGE_SIZE / 2); i++) { if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) { FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)])); } @@ -107,98 +103,86 @@ uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) { return FlashStatus; } /***************************************************************************** -* Read once data byte from a specified address. -*******************************************************************************/ -uint8_t EEPROM_ReadDataByte (uint16_t Address) { - + * Read once data byte from a specified address. + *******************************************************************************/ +uint8_t EEPROM_ReadDataByte(uint16_t Address) { uint8_t DataByte = 0xFF; // Get Byte from specified address - DataByte = (*(__IO uint8_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))); + DataByte = (*(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))); return DataByte; } /***************************************************************************** -* Wrap library in AVR style functions. -*******************************************************************************/ -uint8_t eeprom_read_byte (const uint8_t *Address) -{ - const uint16_t p = (const uint32_t) Address; + * Wrap library in AVR style functions. + *******************************************************************************/ +uint8_t eeprom_read_byte(const uint8_t *Address) { + const uint16_t p = (const uint32_t)Address; return EEPROM_ReadDataByte(p); } -void eeprom_write_byte (uint8_t *Address, uint8_t Value) -{ - uint16_t p = (uint32_t) Address; +void eeprom_write_byte(uint8_t *Address, uint8_t Value) { + uint16_t p = (uint32_t)Address; EEPROM_WriteDataByte(p, Value); } -void eeprom_update_byte (uint8_t *Address, uint8_t Value) -{ - uint16_t p = (uint32_t) Address; +void eeprom_update_byte(uint8_t *Address, uint8_t Value) { + uint16_t p = (uint32_t)Address; EEPROM_WriteDataByte(p, Value); } -uint16_t eeprom_read_word (const uint16_t *Address) -{ - const uint16_t p = (const uint32_t) Address; - return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8); +uint16_t eeprom_read_word(const uint16_t *Address) { + const uint16_t p = (const uint32_t)Address; + return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8); } -void eeprom_write_word (uint16_t *Address, uint16_t Value) -{ - uint16_t p = (uint32_t) Address; - EEPROM_WriteDataByte(p, (uint8_t) Value); - EEPROM_WriteDataByte(p + 1, (uint8_t) (Value >> 8)); +void eeprom_write_word(uint16_t *Address, uint16_t Value) { + uint16_t p = (uint32_t)Address; + EEPROM_WriteDataByte(p, (uint8_t)Value); + EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); } -void eeprom_update_word (uint16_t *Address, uint16_t Value) -{ - uint16_t p = (uint32_t) Address; - EEPROM_WriteDataByte(p, (uint8_t) Value); - EEPROM_WriteDataByte(p + 1, (uint8_t) (Value >> 8)); +void eeprom_update_word(uint16_t *Address, uint16_t Value) { + uint16_t p = (uint32_t)Address; + EEPROM_WriteDataByte(p, (uint8_t)Value); + EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); } -uint32_t eeprom_read_dword (const uint32_t *Address) -{ - const uint16_t p = (const uint32_t) Address; - return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8) - | (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24); +uint32_t eeprom_read_dword(const uint32_t *Address) { + const uint16_t p = (const uint32_t)Address; + return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24); } -void eeprom_write_dword (uint32_t *Address, uint32_t Value) -{ - uint16_t p = (const uint32_t) Address; - EEPROM_WriteDataByte(p, (uint8_t) Value); - EEPROM_WriteDataByte(p+1, (uint8_t) (Value >> 8)); - EEPROM_WriteDataByte(p+2, (uint8_t) (Value >> 16)); - EEPROM_WriteDataByte(p+3, (uint8_t) (Value >> 24)); +void eeprom_write_dword(uint32_t *Address, uint32_t Value) { + uint16_t p = (const uint32_t)Address; + EEPROM_WriteDataByte(p, (uint8_t)Value); + EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); + EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16)); + EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24)); } -void eeprom_update_dword (uint32_t *Address, uint32_t Value) -{ - uint16_t p = (const uint32_t) Address; - uint32_t existingValue = EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p+1) << 8) - | (EEPROM_ReadDataByte(p+2) << 16) | (EEPROM_ReadDataByte(p+3) << 24); - if(Value != existingValue){ - EEPROM_WriteDataByte(p, (uint8_t) Value); - EEPROM_WriteDataByte(p+1, (uint8_t) (Value >> 8)); - EEPROM_WriteDataByte(p+2, (uint8_t) (Value >> 16)); - EEPROM_WriteDataByte(p+3, (uint8_t) (Value >> 24)); +void eeprom_update_dword(uint32_t *Address, uint32_t Value) { + uint16_t p = (const uint32_t)Address; + uint32_t existingValue = EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24); + if (Value != existingValue) { + EEPROM_WriteDataByte(p, (uint8_t)Value); + EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); + EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16)); + EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24)); } } void eeprom_read_block(void *buf, const void *addr, uint32_t len) { - const uint8_t *p = (const uint8_t *)addr; - uint8_t *dest = (uint8_t *)buf; + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; while (len--) { *dest++ = eeprom_read_byte(p++); } } void eeprom_write_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; + uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { eeprom_write_byte(p++, *src++); @@ -206,7 +190,7 @@ void eeprom_write_block(const void *buf, void *addr, uint32_t len) { } void eeprom_update_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; + uint8_t * p = (uint8_t *)addr; const uint8_t *src = (const uint8_t *)buf; while (len--) { eeprom_write_byte(p++, *src++); diff --git a/tmk_core/common/chibios/eeprom_stm32.h b/tmk_core/common/chibios/eeprom_stm32.h index 083eb16ee..e68914595 100755..100644 --- a/tmk_core/common/chibios/eeprom_stm32.h +++ b/tmk_core/common/chibios/eeprom_stm32.h @@ -31,53 +31,53 @@ // HACK ALERT. This definition may not match your processor // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc #if defined(EEPROM_EMU_STM32F303xC) - #define MCU_STM32F303CC +# define MCU_STM32F303CC #elif defined(EEPROM_EMU_STM32F103xB) - #define MCU_STM32F103RB +# define MCU_STM32F103RB #elif defined(EEPROM_EMU_STM32F072xB) - #define MCU_STM32F072CB +# define MCU_STM32F072CB #else - #error "not implemented." +# error "not implemented." #endif #ifndef EEPROM_PAGE_SIZE - #if defined (MCU_STM32F103RB) - #define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte - #define FEE_DENSITY_PAGES 2 // How many pages are used - #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) || defined (MCU_STM32F103RD) || defined (MCU_STM32F303CC) || defined(MCU_STM32F072CB) - #define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte - #define FEE_DENSITY_PAGES 4 // How many pages are used - #else - #error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." - #endif +# if defined(MCU_STM32F103RB) +# define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte +# define FEE_DENSITY_PAGES 2 // How many pages are used +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) +# define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif #endif #ifndef EEPROM_START_ADDRESS - #if defined (MCU_STM32F103RB) || defined(MCU_STM32F072CB) - #define FEE_MCU_FLASH_SIZE 128 // Size in Kb - #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) - #define FEE_MCU_FLASH_SIZE 512 // Size in Kb - #elif defined (MCU_STM32F103RD) - #define FEE_MCU_FLASH_SIZE 384 // Size in Kb - #elif defined (MCU_STM32F303CC) - #define FEE_MCU_FLASH_SIZE 256 // Size in Kb - #else - #error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." - #endif +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) +# define FEE_MCU_FLASH_SIZE 128 // Size in Kb +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) +# define FEE_MCU_FLASH_SIZE 512 // Size in Kb +# elif defined(MCU_STM32F103RD) +# define FEE_MCU_FLASH_SIZE 384 // Size in Kb +# elif defined(MCU_STM32F303CC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif #endif // DONT CHANGE // Choose location for the first EEPROM Page address on the top of flash #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) -#define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) -#define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) -#define FEE_EMPTY_WORD ((uint16_t)0xFFFF) -#define FEE_ADDR_OFFSET(Address)(Address * 2) // 1Byte per Word will be saved to preserve Flash +#define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) +#define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +#define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +#define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash // Use this function to initialize the functionality uint16_t EEPROM_Init(void); -void EEPROM_Erase (void); -uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte); -uint8_t EEPROM_ReadDataByte (uint16_t Address); +void EEPROM_Erase(void); +uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte); +uint8_t EEPROM_ReadDataByte(uint16_t Address); -#endif /* __EEPROM_H */ +#endif /* __EEPROM_H */ diff --git a/tmk_core/common/chibios/eeprom_teensy.c b/tmk_core/common/chibios/eeprom_teensy.c index 9061b790c..a4093fb3b 100644 --- a/tmk_core/common/chibios/eeprom_teensy.c +++ b/tmk_core/common/chibios/eeprom_teensy.c @@ -21,10 +21,10 @@ * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * - * 1. The above copyright notice and this permission notice shall be + * 1. The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * - * 2. If the Software is incorporated into a build system that allows + * 2. If the Software is incorporated into a build system that allows * selection among a list of target devices, then similar target * devices manufactured by PJRC.COM must be included in the list of * target devices and selectable in the same manner. @@ -39,7 +39,6 @@ * SOFTWARE. */ - #if defined(K20x) /* chip selection */ /* Teensy 3.0, 3.1, 3.2; mchck; infinity keyboard */ @@ -51,7 +50,7 @@ // (aligned to 2 or 4 byte boundaries) has twice the endurance // compared to writing 8 bit bytes. // -#define EEPROM_SIZE 32 +# define EEPROM_SIZE 32 // Writing unaligned 16 or 32 bit data is handled automatically when // this is defined, but at a cost of extra code size. Without this, @@ -59,286 +58,271 @@ // absolutely sure all 16 and 32 bit writes will be aligned, you can // remove the extra unnecessary code. // -#define HANDLE_UNALIGNED_WRITES +# define HANDLE_UNALIGNED_WRITES // Minimum EEPROM Endurance // ------------------------ -#if (EEPROM_SIZE == 2048) // 35000 writes/byte or 70000 writes/word - #define EEESIZE 0x33 -#elif (EEPROM_SIZE == 1024) // 75000 writes/byte or 150000 writes/word - #define EEESIZE 0x34 -#elif (EEPROM_SIZE == 512) // 155000 writes/byte or 310000 writes/word - #define EEESIZE 0x35 -#elif (EEPROM_SIZE == 256) // 315000 writes/byte or 630000 writes/word - #define EEESIZE 0x36 -#elif (EEPROM_SIZE == 128) // 635000 writes/byte or 1270000 writes/word - #define EEESIZE 0x37 -#elif (EEPROM_SIZE == 64) // 1275000 writes/byte or 2550000 writes/word - #define EEESIZE 0x38 -#elif (EEPROM_SIZE == 32) // 2555000 writes/byte or 5110000 writes/word - #define EEESIZE 0x39 -#endif +# if (EEPROM_SIZE == 2048) // 35000 writes/byte or 70000 writes/word +# define EEESIZE 0x33 +# elif (EEPROM_SIZE == 1024) // 75000 writes/byte or 150000 writes/word +# define EEESIZE 0x34 +# elif (EEPROM_SIZE == 512) // 155000 writes/byte or 310000 writes/word +# define EEESIZE 0x35 +# elif (EEPROM_SIZE == 256) // 315000 writes/byte or 630000 writes/word +# define EEESIZE 0x36 +# elif (EEPROM_SIZE == 128) // 635000 writes/byte or 1270000 writes/word +# define EEESIZE 0x37 +# elif (EEPROM_SIZE == 64) // 1275000 writes/byte or 2550000 writes/word +# define EEESIZE 0x38 +# elif (EEPROM_SIZE == 32) // 2555000 writes/byte or 5110000 writes/word +# define EEESIZE 0x39 +# endif /** \brief eeprom initialization * * FIXME: needs doc */ -void eeprom_initialize(void) -{ - uint32_t count=0; - uint16_t do_flash_cmd[] = { - 0xf06f, 0x037f, 0x7003, 0x7803, - 0xf013, 0x0f80, 0xd0fb, 0x4770}; - uint8_t status; - - if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) { - // FlexRAM is configured as traditional RAM - // We need to reconfigure for EEPROM usage - FTFL->FCCOB0 = 0x80; // PGMPART = Program Partition Command - FTFL->FCCOB4 = EEESIZE; // EEPROM Size - FTFL->FCCOB5 = 0x03; // 0K for Dataflash, 32K for EEPROM backup - __disable_irq(); - // do_flash_cmd() must execute from RAM. Luckily the C syntax is simple... - (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT)); - __enable_irq(); - status = FTFL->FSTAT; - if (status & (FTFL_FSTAT_RDCOLERR|FTFL_FSTAT_ACCERR|FTFL_FSTAT_FPVIOL)) { - FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR|FTFL_FSTAT_ACCERR|FTFL_FSTAT_FPVIOL)); - return; // error - } - } - // wait for eeprom to become ready (is this really necessary?) - while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) { - if (++count > 20000) break; - } -} - -#define FlexRAM ((uint8_t *)0x14000000) +void eeprom_initialize(void) { + uint32_t count = 0; + uint16_t do_flash_cmd[] = {0xf06f, 0x037f, 0x7003, 0x7803, 0xf013, 0x0f80, 0xd0fb, 0x4770}; + uint8_t status; + + if (FTFL->FCNFG & FTFL_FCNFG_RAMRDY) { + // FlexRAM is configured as traditional RAM + // We need to reconfigure for EEPROM usage + FTFL->FCCOB0 = 0x80; // PGMPART = Program Partition Command + FTFL->FCCOB4 = EEESIZE; // EEPROM Size + FTFL->FCCOB5 = 0x03; // 0K for Dataflash, 32K for EEPROM backup + __disable_irq(); + // do_flash_cmd() must execute from RAM. Luckily the C syntax is simple... + (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFL->FSTAT)); + __enable_irq(); + status = FTFL->FSTAT; + if (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)) { + FTFL->FSTAT = (status & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL)); + return; // error + } + } + // wait for eeprom to become ready (is this really necessary?) + while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) { + if (++count > 20000) break; + } +} + +# define FlexRAM ((uint8_t *)0x14000000) /** \brief eeprom read byte * * FIXME: needs doc */ -uint8_t eeprom_read_byte(const uint8_t *addr) -{ - uint32_t offset = (uint32_t)addr; - if (offset >= EEPROM_SIZE) return 0; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - return FlexRAM[offset]; +uint8_t eeprom_read_byte(const uint8_t *addr) { + uint32_t offset = (uint32_t)addr; + if (offset >= EEPROM_SIZE) return 0; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + return FlexRAM[offset]; } /** \brief eeprom read word * * FIXME: needs doc */ -uint16_t eeprom_read_word(const uint16_t *addr) -{ - uint32_t offset = (uint32_t)addr; - if (offset >= EEPROM_SIZE-1) return 0; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - return *(uint16_t *)(&FlexRAM[offset]); +uint16_t eeprom_read_word(const uint16_t *addr) { + uint32_t offset = (uint32_t)addr; + if (offset >= EEPROM_SIZE - 1) return 0; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + return *(uint16_t *)(&FlexRAM[offset]); } /** \brief eeprom read dword * * FIXME: needs doc */ -uint32_t eeprom_read_dword(const uint32_t *addr) -{ - uint32_t offset = (uint32_t)addr; - if (offset >= EEPROM_SIZE-3) return 0; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - return *(uint32_t *)(&FlexRAM[offset]); +uint32_t eeprom_read_dword(const uint32_t *addr) { + uint32_t offset = (uint32_t)addr; + if (offset >= EEPROM_SIZE - 3) return 0; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + return *(uint32_t *)(&FlexRAM[offset]); } /** \brief eeprom read block * * FIXME: needs doc */ -void eeprom_read_block(void *buf, const void *addr, uint32_t len) -{ - uint32_t offset = (uint32_t)addr; - uint8_t *dest = (uint8_t *)buf; - uint32_t end = offset + len; - - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - if (end > EEPROM_SIZE) end = EEPROM_SIZE; - while (offset < end) { - *dest++ = FlexRAM[offset++]; - } +void eeprom_read_block(void *buf, const void *addr, uint32_t len) { + uint32_t offset = (uint32_t)addr; + uint8_t *dest = (uint8_t *)buf; + uint32_t end = offset + len; + + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + if (end > EEPROM_SIZE) end = EEPROM_SIZE; + while (offset < end) { + *dest++ = FlexRAM[offset++]; + } } /** \brief eeprom is ready * * FIXME: needs doc */ -int eeprom_is_ready(void) -{ - return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; -} +int eeprom_is_ready(void) { return (FTFL->FCNFG & FTFL_FCNFG_EEERDY) ? 1 : 0; } /** \brief flexram wait * * FIXME: needs doc */ -static void flexram_wait(void) -{ - while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) { - // TODO: timeout - } +static void flexram_wait(void) { + while (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) { + // TODO: timeout + } } /** \brief eeprom_write_byte * * FIXME: needs doc */ -void eeprom_write_byte(uint8_t *addr, uint8_t value) -{ - uint32_t offset = (uint32_t)addr; +void eeprom_write_byte(uint8_t *addr, uint8_t value) { + uint32_t offset = (uint32_t)addr; - if (offset >= EEPROM_SIZE) return; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - if (FlexRAM[offset] != value) { - FlexRAM[offset] = value; - flexram_wait(); - } + if (offset >= EEPROM_SIZE) return; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + if (FlexRAM[offset] != value) { + FlexRAM[offset] = value; + flexram_wait(); + } } /** \brief eeprom write word * * FIXME: needs doc */ -void eeprom_write_word(uint16_t *addr, uint16_t value) -{ - uint32_t offset = (uint32_t)addr; - - if (offset >= EEPROM_SIZE-1) return; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); -#ifdef HANDLE_UNALIGNED_WRITES - if ((offset & 1) == 0) { -#endif - if (*(uint16_t *)(&FlexRAM[offset]) != value) { - *(uint16_t *)(&FlexRAM[offset]) = value; - flexram_wait(); - } -#ifdef HANDLE_UNALIGNED_WRITES - } else { - if (FlexRAM[offset] != value) { - FlexRAM[offset] = value; - flexram_wait(); - } - if (FlexRAM[offset + 1] != (value >> 8)) { - FlexRAM[offset + 1] = value >> 8; - flexram_wait(); - } - } -#endif +void eeprom_write_word(uint16_t *addr, uint16_t value) { + uint32_t offset = (uint32_t)addr; + + if (offset >= EEPROM_SIZE - 1) return; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); +# ifdef HANDLE_UNALIGNED_WRITES + if ((offset & 1) == 0) { +# endif + if (*(uint16_t *)(&FlexRAM[offset]) != value) { + *(uint16_t *)(&FlexRAM[offset]) = value; + flexram_wait(); + } +# ifdef HANDLE_UNALIGNED_WRITES + } else { + if (FlexRAM[offset] != value) { + FlexRAM[offset] = value; + flexram_wait(); + } + if (FlexRAM[offset + 1] != (value >> 8)) { + FlexRAM[offset + 1] = value >> 8; + flexram_wait(); + } + } +# endif } /** \brief eeprom write dword * * FIXME: needs doc */ -void eeprom_write_dword(uint32_t *addr, uint32_t value) -{ - uint32_t offset = (uint32_t)addr; - - if (offset >= EEPROM_SIZE-3) return; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); -#ifdef HANDLE_UNALIGNED_WRITES - switch (offset & 3) { - case 0: -#endif - if (*(uint32_t *)(&FlexRAM[offset]) != value) { - *(uint32_t *)(&FlexRAM[offset]) = value; - flexram_wait(); - } - return; -#ifdef HANDLE_UNALIGNED_WRITES - case 2: - if (*(uint16_t *)(&FlexRAM[offset]) != value) { - *(uint16_t *)(&FlexRAM[offset]) = value; - flexram_wait(); - } - if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) { - *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16; - flexram_wait(); - } - return; - default: - if (FlexRAM[offset] != value) { - FlexRAM[offset] = value; - flexram_wait(); - } - if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) { - *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8; - flexram_wait(); - } - if (FlexRAM[offset + 3] != (value >> 24)) { - FlexRAM[offset + 3] = value >> 24; - flexram_wait(); - } - } -#endif +void eeprom_write_dword(uint32_t *addr, uint32_t value) { + uint32_t offset = (uint32_t)addr; + + if (offset >= EEPROM_SIZE - 3) return; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); +# ifdef HANDLE_UNALIGNED_WRITES + switch (offset & 3) { + case 0: +# endif + if (*(uint32_t *)(&FlexRAM[offset]) != value) { + *(uint32_t *)(&FlexRAM[offset]) = value; + flexram_wait(); + } + return; +# ifdef HANDLE_UNALIGNED_WRITES + case 2: + if (*(uint16_t *)(&FlexRAM[offset]) != value) { + *(uint16_t *)(&FlexRAM[offset]) = value; + flexram_wait(); + } + if (*(uint16_t *)(&FlexRAM[offset + 2]) != (value >> 16)) { + *(uint16_t *)(&FlexRAM[offset + 2]) = value >> 16; + flexram_wait(); + } + return; + default: + if (FlexRAM[offset] != value) { + FlexRAM[offset] = value; + flexram_wait(); + } + if (*(uint16_t *)(&FlexRAM[offset + 1]) != (value >> 8)) { + *(uint16_t *)(&FlexRAM[offset + 1]) = value >> 8; + flexram_wait(); + } + if (FlexRAM[offset + 3] != (value >> 24)) { + FlexRAM[offset + 3] = value >> 24; + flexram_wait(); + } + } +# endif } /** \brief eeprom write block * * FIXME: needs doc */ -void eeprom_write_block(const void *buf, void *addr, uint32_t len) -{ - uint32_t offset = (uint32_t)addr; - const uint8_t *src = (const uint8_t *)buf; - - if (offset >= EEPROM_SIZE) return; - if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); - if (len >= EEPROM_SIZE) len = EEPROM_SIZE; - if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset; - while (len > 0) { - uint32_t lsb = offset & 3; - if (lsb == 0 && len >= 4) { - // write aligned 32 bits - uint32_t val32; - val32 = *src++; - val32 |= (*src++ << 8); - val32 |= (*src++ << 16); - val32 |= (*src++ << 24); - if (*(uint32_t *)(&FlexRAM[offset]) != val32) { - *(uint32_t *)(&FlexRAM[offset]) = val32; - flexram_wait(); - } - offset += 4; - len -= 4; - } else if ((lsb == 0 || lsb == 2) && len >= 2) { - // write aligned 16 bits - uint16_t val16; - val16 = *src++; - val16 |= (*src++ << 8); - if (*(uint16_t *)(&FlexRAM[offset]) != val16) { - *(uint16_t *)(&FlexRAM[offset]) = val16; - flexram_wait(); - } - offset += 2; - len -= 2; - } else { - // write 8 bits - uint8_t val8 = *src++; - if (FlexRAM[offset] != val8) { - FlexRAM[offset] = val8; - flexram_wait(); - } - offset++; - len--; - } - } +void eeprom_write_block(const void *buf, void *addr, uint32_t len) { + uint32_t offset = (uint32_t)addr; + const uint8_t *src = (const uint8_t *)buf; + + if (offset >= EEPROM_SIZE) return; + if (!(FTFL->FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); + if (len >= EEPROM_SIZE) len = EEPROM_SIZE; + if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset; + while (len > 0) { + uint32_t lsb = offset & 3; + if (lsb == 0 && len >= 4) { + // write aligned 32 bits + uint32_t val32; + val32 = *src++; + val32 |= (*src++ << 8); + val32 |= (*src++ << 16); + val32 |= (*src++ << 24); + if (*(uint32_t *)(&FlexRAM[offset]) != val32) { + *(uint32_t *)(&FlexRAM[offset]) = val32; + flexram_wait(); + } + offset += 4; + len -= 4; + } else if ((lsb == 0 || lsb == 2) && len >= 2) { + // write aligned 16 bits + uint16_t val16; + val16 = *src++; + val16 |= (*src++ << 8); + if (*(uint16_t *)(&FlexRAM[offset]) != val16) { + *(uint16_t *)(&FlexRAM[offset]) = val16; + flexram_wait(); + } + offset += 2; + len -= 2; + } else { + // write 8 bits + uint8_t val8 = *src++; + if (FlexRAM[offset] != val8) { + FlexRAM[offset] = val8; + flexram_wait(); + } + offset++; + len--; + } + } } /* void do_flash_cmd(volatile uint8_t *fstat) { - *fstat = 0x80; - while ((*fstat & 0x80) == 0) ; // wait + *fstat = 0x80; + while ((*fstat & 0x80) == 0) ; // wait } 00000000 <do_flash_cmd>: 0: f06f 037f mvn.w r3, #127 ; 0x7f @@ -352,128 +336,124 @@ void do_flash_cmd(volatile uint8_t *fstat) #elif defined(KL2x) /* chip selection */ /* Teensy LC (emulated) */ -#define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) +# define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) extern uint32_t __eeprom_workarea_start__; extern uint32_t __eeprom_workarea_end__; -#define EEPROM_SIZE 128 +# define EEPROM_SIZE 128 static uint32_t flashend = 0; -void eeprom_initialize(void) -{ - const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); +void eeprom_initialize(void) { + const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); - do { - if (*p++ == 0xFFFF) { - flashend = (uint32_t)(p - 2); - return; - } - } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__)); - flashend = (uint32_t)((uint16_t *)SYMVAL(__eeprom_workarea_end__) - 1); + do { + if (*p++ == 0xFFFF) { + flashend = (uint32_t)(p - 2); + return; + } + } while (p < (uint16_t *)SYMVAL(__eeprom_workarea_end__)); + flashend = (uint32_t)((uint16_t *)SYMVAL(__eeprom_workarea_end__) - 1); } -uint8_t eeprom_read_byte(const uint8_t *addr) -{ - uint32_t offset = (uint32_t)addr; - const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); - const uint16_t *end = (const uint16_t *)((uint32_t)flashend); - uint16_t val; - uint8_t data=0xFF; - - if (!end) { - eeprom_initialize(); - end = (const uint16_t *)((uint32_t)flashend); - } - if (offset < EEPROM_SIZE) { - while (p <= end) { - val = *p++; - if ((val & 255) == offset) data = val >> 8; - } - } - return data; -} - -static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) -{ - // with great power comes great responsibility.... - uint32_t stat; - *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC); - *(uint32_t *)&(FTFA->FCCOB7) = data; - __disable_irq(); - (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT)); - __enable_irq(); - stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR|FTFA_FSTAT_ACCERR|FTFA_FSTAT_FPVIOL); - if (stat) { - FTFA->FSTAT = stat; - } - MCM->PLACR |= MCM_PLACR_CFCC; -} - -void eeprom_write_byte(uint8_t *addr, uint8_t data) -{ - uint32_t offset = (uint32_t)addr; - const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend); - uint32_t i, val, flashaddr; - uint16_t do_flash_cmd[] = { - 0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770}; - uint8_t buf[EEPROM_SIZE]; - - if (offset >= EEPROM_SIZE) return; - if (!end) { - eeprom_initialize(); - end = (const uint16_t *)((uint32_t)flashend); - } - if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) { - val = (data << 8) | offset; - flashaddr = (uint32_t)end; - flashend = flashaddr; - if ((flashaddr & 2) == 0) { - val |= 0xFFFF0000; - } else { - val <<= 16; - val |= 0x0000FFFF; - } - flash_write(do_flash_cmd, flashaddr, val); - } else { - for (i=0; i < EEPROM_SIZE; i++) { - buf[i] = 0xFF; - } - val = 0; - for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) { - val = *p; - if ((val & 255) < EEPROM_SIZE) { - buf[val & 255] = val >> 8; - } - } - buf[offset] = data; - for (flashaddr=(uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) { - *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr; - __disable_irq(); - (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT)); - __enable_irq(); - val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR|FTFA_FSTAT_ACCERR|FTFA_FSTAT_FPVIOL);; - if (val) FTFA->FSTAT = val; - MCM->PLACR |= MCM_PLACR_CFCC; - } - flashaddr=(uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); - for (i=0; i < EEPROM_SIZE; i++) { - if (buf[i] == 0xFF) continue; - if ((flashaddr & 2) == 0) { - val = (buf[i] << 8) | i; - } else { - val = val | (buf[i] << 24) | (i << 16); - flash_write(do_flash_cmd, flashaddr, val); - } - flashaddr += 2; - } - flashend = flashaddr; - if ((flashaddr & 2)) { - val |= 0xFFFF0000; - flash_write(do_flash_cmd, flashaddr, val); - } - } +uint8_t eeprom_read_byte(const uint8_t *addr) { + uint32_t offset = (uint32_t)addr; + const uint16_t *p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); + const uint16_t *end = (const uint16_t *)((uint32_t)flashend); + uint16_t val; + uint8_t data = 0xFF; + + if (!end) { + eeprom_initialize(); + end = (const uint16_t *)((uint32_t)flashend); + } + if (offset < EEPROM_SIZE) { + while (p <= end) { + val = *p++; + if ((val & 255) == offset) data = val >> 8; + } + } + return data; +} + +static void flash_write(const uint16_t *code, uint32_t addr, uint32_t data) { + // with great power comes great responsibility.... + uint32_t stat; + *(uint32_t *)&(FTFA->FCCOB3) = 0x06000000 | (addr & 0x00FFFFFC); + *(uint32_t *)&(FTFA->FCCOB7) = data; + __disable_irq(); + (*((void (*)(volatile uint8_t *))((uint32_t)code | 1)))(&(FTFA->FSTAT)); + __enable_irq(); + stat = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL); + if (stat) { + FTFA->FSTAT = stat; + } + MCM->PLACR |= MCM_PLACR_CFCC; +} + +void eeprom_write_byte(uint8_t *addr, uint8_t data) { + uint32_t offset = (uint32_t)addr; + const uint16_t *p, *end = (const uint16_t *)((uint32_t)flashend); + uint32_t i, val, flashaddr; + uint16_t do_flash_cmd[] = {0x2380, 0x7003, 0x7803, 0xb25b, 0x2b00, 0xdafb, 0x4770}; + uint8_t buf[EEPROM_SIZE]; + + if (offset >= EEPROM_SIZE) return; + if (!end) { + eeprom_initialize(); + end = (const uint16_t *)((uint32_t)flashend); + } + if (++end < (uint16_t *)SYMVAL(__eeprom_workarea_end__)) { + val = (data << 8) | offset; + flashaddr = (uint32_t)end; + flashend = flashaddr; + if ((flashaddr & 2) == 0) { + val |= 0xFFFF0000; + } else { + val <<= 16; + val |= 0x0000FFFF; + } + flash_write(do_flash_cmd, flashaddr, val); + } else { + for (i = 0; i < EEPROM_SIZE; i++) { + buf[i] = 0xFF; + } + val = 0; + for (p = (uint16_t *)SYMVAL(__eeprom_workarea_start__); p < (uint16_t *)SYMVAL(__eeprom_workarea_end__); p++) { + val = *p; + if ((val & 255) < EEPROM_SIZE) { + buf[val & 255] = val >> 8; + } + } + buf[offset] = data; + for (flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); flashaddr < (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_end__); flashaddr += 1024) { + *(uint32_t *)&(FTFA->FCCOB3) = 0x09000000 | flashaddr; + __disable_irq(); + (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&(FTFA->FSTAT)); + __enable_irq(); + val = FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR | FTFA_FSTAT_ACCERR | FTFA_FSTAT_FPVIOL); + ; + if (val) FTFA->FSTAT = val; + MCM->PLACR |= MCM_PLACR_CFCC; + } + flashaddr = (uint32_t)(uint16_t *)SYMVAL(__eeprom_workarea_start__); + for (i = 0; i < EEPROM_SIZE; i++) { + if (buf[i] == 0xFF) continue; + if ((flashaddr & 2) == 0) { + val = (buf[i] << 8) | i; + } else { + val = val | (buf[i] << 24) | (i << 16); + flash_write(do_flash_cmd, flashaddr, val); + } + flashaddr += 2; + } + flashend = flashaddr; + if ((flashaddr & 2)) { + val |= 0xFFFF0000; + flash_write(do_flash_cmd, flashaddr, val); + } + } } /* @@ -492,141 +472,127 @@ void do_flash_cmd(volatile uint8_t *fstat) c: 4770 bx lr */ - -uint16_t eeprom_read_word(const uint16_t *addr) -{ - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8); +uint16_t eeprom_read_word(const uint16_t *addr) { + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); } -uint32_t eeprom_read_dword(const uint32_t *addr) -{ - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8) - | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24); +uint32_t eeprom_read_dword(const uint32_t *addr) { + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } -void eeprom_read_block(void *buf, const void *addr, uint32_t len) -{ - const uint8_t *p = (const uint8_t *)addr; - uint8_t *dest = (uint8_t *)buf; - while (len--) { - *dest++ = eeprom_read_byte(p++); - } +void eeprom_read_block(void *buf, const void *addr, uint32_t len) { + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte(p++); + } } -int eeprom_is_ready(void) -{ - return 1; -} +int eeprom_is_ready(void) { return 1; } -void eeprom_write_word(uint16_t *addr, uint16_t value) -{ - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); +void eeprom_write_word(uint16_t *addr, uint16_t value) { + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } -void eeprom_write_dword(uint32_t *addr, uint32_t value) -{ - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); +void eeprom_write_dword(uint32_t *addr, uint32_t value) { + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } -void eeprom_write_block(const void *buf, void *addr, uint32_t len) -{ - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } +void eeprom_write_block(const void *buf, void *addr, uint32_t len) { + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } #else // No EEPROM supported, so emulate it -#define EEPROM_SIZE 32 +# define EEPROM_SIZE 32 static uint8_t buffer[EEPROM_SIZE]; uint8_t eeprom_read_byte(const uint8_t *addr) { - uint32_t offset = (uint32_t)addr; - return buffer[offset]; + uint32_t offset = (uint32_t)addr; + return buffer[offset]; } void eeprom_write_byte(uint8_t *addr, uint8_t value) { - uint32_t offset = (uint32_t)addr; - buffer[offset] = value; + uint32_t offset = (uint32_t)addr; + buffer[offset] = value; } uint16_t eeprom_read_word(const uint16_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); } uint32_t eeprom_read_dword(const uint32_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8) - | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } void eeprom_read_block(void *buf, const void *addr, uint32_t len) { - const uint8_t *p = (const uint8_t *)addr; - uint8_t *dest = (uint8_t *)buf; - while (len--) { - *dest++ = eeprom_read_byte(p++); - } + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte(p++); + } } void eeprom_write_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_write_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_write_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } #endif /* chip selection */ // The update functions just calls write for now, but could probably be optimized -void eeprom_update_byte(uint8_t *addr, uint8_t value) { - eeprom_write_byte(addr, value); -} +void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); } void eeprom_update_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_update_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_update_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } diff --git a/tmk_core/common/chibios/flash_stm32.c b/tmk_core/common/chibios/flash_stm32.c index 832bf3908..e166fc5e6 100755..100644 --- a/tmk_core/common/chibios/flash_stm32.c +++ b/tmk_core/common/chibios/flash_stm32.c @@ -17,105 +17,95 @@ */ #if defined(EEPROM_EMU_STM32F303xC) - #define STM32F303xC - #include "stm32f3xx.h" +# define STM32F303xC +# include "stm32f3xx.h" #elif defined(EEPROM_EMU_STM32F103xB) - #define STM32F103xB - #include "stm32f1xx.h" +# define STM32F103xB +# include "stm32f1xx.h" #elif defined(EEPROM_EMU_STM32F072xB) - #define STM32F072xB - #include "stm32f0xx.h" +# define STM32F072xB +# include "stm32f0xx.h" #else - #error "not implemented." +# error "not implemented." #endif #include "flash_stm32.h" #if defined(EEPROM_EMU_STM32F103xB) - #define FLASH_SR_WRPERR FLASH_SR_WRPRTERR +# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR #endif /* Delay definition */ -#define EraseTimeout ((uint32_t)0x00000FFF) -#define ProgramTimeout ((uint32_t)0x0000001F) +#define EraseTimeout ((uint32_t)0x00000FFF) +#define ProgramTimeout ((uint32_t)0x0000001F) #define ASSERT(exp) (void)((0)) /** - * @brief Inserts a time delay. - * @param None - * @retval None - */ -static void delay(void) -{ + * @brief Inserts a time delay. + * @param None + * @retval None + */ +static void delay(void) { __IO uint32_t i = 0; - for(i = 0xFF; i != 0; i--) { } + for (i = 0xFF; i != 0; i--) { + } } /** - * @brief Returns the FLASH Status. - * @param None - * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, - * FLASH_ERROR_WRP or FLASH_COMPLETE - */ -FLASH_Status FLASH_GetStatus(void) -{ - if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) - return FLASH_BUSY; + * @brief Returns the FLASH Status. + * @param None + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP or FLASH_COMPLETE + */ +FLASH_Status FLASH_GetStatus(void) { + if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY; - if ((FLASH->SR & FLASH_SR_PGERR) != 0) - return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG; - if ((FLASH->SR & FLASH_SR_WRPERR) != 0 ) - return FLASH_ERROR_WRP; + if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP; - if ((FLASH->SR & FLASH_OBR_OPTERR) != 0 ) - return FLASH_ERROR_OPT; + if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT; return FLASH_COMPLETE; } /** - * @brief Waits for a Flash operation to complete or a TIMEOUT to occur. - * @param Timeout: FLASH progamming Timeout - * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, - * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. - */ -FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) -{ + * @brief Waits for a Flash operation to complete or a TIMEOUT to occur. + * @param Timeout: FLASH progamming Timeout + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { FLASH_Status status; /* Check for the Flash Status */ status = FLASH_GetStatus(); /* Wait for a Flash operation to complete or a TIMEOUT to occur */ - while ((status == FLASH_BUSY) && (Timeout != 0x00)) - { + while ((status == FLASH_BUSY) && (Timeout != 0x00)) { delay(); status = FLASH_GetStatus(); Timeout--; } - if (Timeout == 0) - status = FLASH_TIMEOUT; + if (Timeout == 0) status = FLASH_TIMEOUT; /* Return the operation status */ return status; } /** - * @brief Erases a specified FLASH page. - * @param Page_Address: The page address to be erased. - * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, - * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. - */ -FLASH_Status FLASH_ErasePage(uint32_t Page_Address) -{ + * @brief Erases a specified FLASH page. + * @param Page_Address: The page address to be erased. + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { FLASH_Status status = FLASH_COMPLETE; /* Check the parameters */ ASSERT(IS_FLASH_ADDRESS(Page_Address)); /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation(EraseTimeout); - if(status == FLASH_COMPLETE) - { + if (status == FLASH_COMPLETE) { /* if the previous operation is completed, proceed to erase the page */ FLASH->CR |= FLASH_CR_PER; FLASH->AR = Page_Address; @@ -123,8 +113,7 @@ FLASH_Status FLASH_ErasePage(uint32_t Page_Address) /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation(EraseTimeout); - if(status != FLASH_TIMEOUT) - { + if (status != FLASH_TIMEOUT) { /* if the erase operation is completed, disable the PER Bit */ FLASH->CR &= ~FLASH_CR_PER; } @@ -135,29 +124,25 @@ FLASH_Status FLASH_ErasePage(uint32_t Page_Address) } /** - * @brief Programs a half word at a specified address. - * @param Address: specifies the address to be programmed. - * @param Data: specifies the data to be programmed. - * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, - * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. - */ -FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) -{ + * @brief Programs a half word at a specified address. + * @param Address: specifies the address to be programmed. + * @param Data: specifies the data to be programmed. + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { FLASH_Status status = FLASH_BAD_ADDRESS; - if (IS_FLASH_ADDRESS(Address)) - { + if (IS_FLASH_ADDRESS(Address)) { /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation(ProgramTimeout); - if(status == FLASH_COMPLETE) - { + if (status == FLASH_COMPLETE) { /* if the previous operation is completed, proceed to program the new data */ FLASH->CR |= FLASH_CR_PG; *(__IO uint16_t*)Address = Data; /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation(ProgramTimeout); - if(status != FLASH_TIMEOUT) - { + if (status != FLASH_TIMEOUT) { /* if the program operation is completed, disable the PG Bit */ FLASH->CR &= ~FLASH_CR_PG; } @@ -168,39 +153,36 @@ FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) } /** - * @brief Unlocks the FLASH Program Erase Controller. - * @param None - * @retval None - */ -void FLASH_Unlock(void) -{ + * @brief Unlocks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Unlock(void) { /* Authorize the FPEC Access */ FLASH->KEYR = FLASH_KEY1; FLASH->KEYR = FLASH_KEY2; } /** - * @brief Locks the FLASH Program Erase Controller. - * @param None - * @retval None - */ -void FLASH_Lock(void) -{ + * @brief Locks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Lock(void) { /* Set the Lock Bit to lock the FPEC and the FCR */ FLASH->CR |= FLASH_CR_LOCK; } /** - * @brief Clears the FLASH's pending flags. - * @param FLASH_FLAG: specifies the FLASH flags to clear. - * This parameter can be any combination of the following values: - * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag - * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag - * @arg FLASH_FLAG_EOP: FLASH End of Programming flag - * @retval None - */ -void FLASH_ClearFlag(uint32_t FLASH_FLAG) -{ + * @brief Clears the FLASH's pending flags. + * @param FLASH_FLAG: specifies the FLASH flags to clear. + * This parameter can be any combination of the following values: + * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag + * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag + * @arg FLASH_FLAG_EOP: FLASH End of Programming flag + * @retval None + */ +void FLASH_ClearFlag(uint32_t FLASH_FLAG) { /* Clear the flags */ FLASH->SR = FLASH_FLAG; } diff --git a/tmk_core/common/chibios/flash_stm32.h b/tmk_core/common/chibios/flash_stm32.h index 3c99cc566..33ab7867d 100755..100644 --- a/tmk_core/common/chibios/flash_stm32.h +++ b/tmk_core/common/chibios/flash_stm32.h @@ -10,7 +10,7 @@ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * - * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and + * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and * https://github.com/leaflabs/libmaple * * Modifications for QMK and STM32F303 by Yiancar @@ -20,22 +20,13 @@ #define __FLASH_STM32_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif #include "ch.h" #include "hal.h" -typedef enum - { - FLASH_BUSY = 1, - FLASH_ERROR_PG, - FLASH_ERROR_WRP, - FLASH_ERROR_OPT, - FLASH_COMPLETE, - FLASH_TIMEOUT, - FLASH_BAD_ADDRESS - } FLASH_Status; +typedef enum { FLASH_BUSY = 1, FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_ERROR_OPT, FLASH_COMPLETE, FLASH_TIMEOUT, FLASH_BAD_ADDRESS } FLASH_Status; #define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x0807FFFF)) diff --git a/tmk_core/common/chibios/printf.c b/tmk_core/common/chibios/printf.c index 72e3d4f8c..dbd059448 100644 --- a/tmk_core/common/chibios/printf.c +++ b/tmk_core/common/chibios/printf.c @@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "printf.h" -typedef void (*putcf) (void*,char); +typedef void (*putcf)(void*, char); static putcf stdout_putf; static void* stdout_putp; @@ -35,206 +35,185 @@ static void* stdout_putp; #ifdef PRINTF_LONG_SUPPORT -static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf) - { - int n=0; - unsigned int d=1; - while (num/d >= base) - d*=base; - while (d!=0) { +static void uli2a(unsigned long int num, unsigned int base, int uc, char* bf) { + int n = 0; + unsigned int d = 1; + while (num / d >= base) d *= base; + while (d != 0) { int dgt = num / d; - num%=d; - d/=base; - if (n || dgt>0|| d==0) { - *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); + num %= d; + d /= base; + if (n || dgt > 0 || d == 0) { + *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); ++n; - } } - *bf=0; } + *bf = 0; +} -static void li2a (long num, char * bf) - { - if (num<0) { - num=-num; +static void li2a(long num, char* bf) { + if (num < 0) { + num = -num; *bf++ = '-'; - } - uli2a(num,10,0,bf); } + uli2a(num, 10, 0, bf); +} #endif -static void ui2a(unsigned int num, unsigned int base, int uc,char * bf) - { - int n=0; - unsigned int d=1; - while (num/d >= base) - d*=base; - while (d!=0) { +static void ui2a(unsigned int num, unsigned int base, int uc, char* bf) { + int n = 0; + unsigned int d = 1; + while (num / d >= base) d *= base; + while (d != 0) { int dgt = num / d; - num%= d; - d/=base; - if (n || dgt>0 || d==0) { - *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); + num %= d; + d /= base; + if (n || dgt > 0 || d == 0) { + *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); ++n; - } } - *bf=0; } + *bf = 0; +} -static void i2a (int num, char * bf) - { - if (num<0) { - num=-num; +static void i2a(int num, char* bf) { + if (num < 0) { + num = -num; *bf++ = '-'; - } - ui2a(num,10,0,bf); } - -static int a2d(char ch) - { - if (ch>='0' && ch<='9') - return ch-'0'; - else if (ch>='a' && ch<='f') - return ch-'a'+10; - else if (ch>='A' && ch<='F') - return ch-'A'+10; - else return -1; + ui2a(num, 10, 0, bf); +} + +static int a2d(char ch) { + if (ch >= '0' && ch <= '9') + return ch - '0'; + else if (ch >= 'a' && ch <= 'f') + return ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'F') + return ch - 'A' + 10; + else + return -1; +} + +static char a2i(char ch, char** src, int base, int* nump) { + char* p = *src; + int num = 0; + int digit; + while ((digit = a2d(ch)) >= 0) { + if (digit > base) break; + num = num * base + digit; + ch = *p++; } - -static char a2i(char ch, char** src,int base,int* nump) - { - char* p= *src; - int num=0; - int digit; - while ((digit=a2d(ch))>=0) { - if (digit>base) break; - num=num*base+digit; - ch=*p++; - } - *src=p; - *nump=num; + *src = p; + *nump = num; return ch; - } - -static void putchw(void* putp,putcf putf,int n, char z, char* bf) - { - char fc=z? '0' : ' '; - char ch; - char* p=bf; - while (*p++ && n > 0) - n--; - while (n-- > 0) - putf(putp,fc); - while ((ch= *bf++)) - putf(putp,ch); - } - -void tfp_format(void* putp,putcf putf,char *fmt, va_list va) - { +} + +static void putchw(void* putp, putcf putf, int n, char z, char* bf) { + char fc = z ? '0' : ' '; + char ch; + char* p = bf; + while (*p++ && n > 0) n--; + while (n-- > 0) putf(putp, fc); + while ((ch = *bf++)) putf(putp, ch); +} + +void tfp_format(void* putp, putcf putf, char* fmt, va_list va) { char bf[12]; - - char ch; + char ch; - while ((ch=*(fmt++))) { - if (ch!='%') - putf(putp,ch); + while ((ch = *(fmt++))) { + if (ch != '%') + putf(putp, ch); else { - char lz=0; -#ifdef PRINTF_LONG_SUPPORT - char lng=0; + char lz = 0; +#ifdef PRINTF_LONG_SUPPORT + char lng = 0; #endif - int w=0; - ch=*(fmt++); - if (ch=='0') { - ch=*(fmt++); - lz=1; - } - if (ch>='0' && ch<='9') { - ch=a2i(ch,&fmt,10,&w); - } -#ifdef PRINTF_LONG_SUPPORT - if (ch=='l') { - ch=*(fmt++); - lng=1; + int w = 0; + ch = *(fmt++); + if (ch == '0') { + ch = *(fmt++); + lz = 1; + } + if (ch >= '0' && ch <= '9') { + ch = a2i(ch, &fmt, 10, &w); + } +#ifdef PRINTF_LONG_SUPPORT + if (ch == 'l') { + ch = *(fmt++); + lng = 1; } #endif switch (ch) { - case 0: + case 0: goto abort; - case 'u' : { -#ifdef PRINTF_LONG_SUPPORT + case 'u': { +#ifdef PRINTF_LONG_SUPPORT if (lng) - uli2a(va_arg(va, unsigned long int),10,0,bf); + uli2a(va_arg(va, unsigned long int), 10, 0, bf); else #endif - ui2a(va_arg(va, unsigned int),10,0,bf); - putchw(putp,putf,w,lz,bf); + ui2a(va_arg(va, unsigned int), 10, 0, bf); + putchw(putp, putf, w, lz, bf); break; - } - case 'd' : { -#ifdef PRINTF_LONG_SUPPORT + } + case 'd': { +#ifdef PRINTF_LONG_SUPPORT if (lng) - li2a(va_arg(va, unsigned long int),bf); + li2a(va_arg(va, unsigned long int), bf); else #endif - i2a(va_arg(va, int),bf); - putchw(putp,putf,w,lz,bf); + i2a(va_arg(va, int), bf); + putchw(putp, putf, w, lz, bf); break; - } - case 'x': case 'X' : -#ifdef PRINTF_LONG_SUPPORT + } + case 'x': + case 'X': +#ifdef PRINTF_LONG_SUPPORT if (lng) - uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf); + uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf); else #endif - ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf); - putchw(putp,putf,w,lz,bf); + ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf); + putchw(putp, putf, w, lz, bf); break; - case 'c' : - putf(putp,(char)(va_arg(va, int))); + case 'c': + putf(putp, (char)(va_arg(va, int))); break; - case 's' : - putchw(putp,putf,w,0,va_arg(va, char*)); + case 's': + putchw(putp, putf, w, 0, va_arg(va, char*)); break; - case '%' : - putf(putp,ch); + case '%': + putf(putp, ch); default: break; - } } } - abort:; } +abort:; +} +void init_printf(void* putp, void (*putf)(void*, char)) { + stdout_putf = putf; + stdout_putp = putp; +} -void init_printf(void* putp,void (*putf) (void*,char)) - { - stdout_putf=putf; - stdout_putp=putp; - } - -void tfp_printf(char *fmt, ...) - { +void tfp_printf(char* fmt, ...) { va_list va; - va_start(va,fmt); - tfp_format(stdout_putp,stdout_putf,fmt,va); + va_start(va, fmt); + tfp_format(stdout_putp, stdout_putf, fmt, va); va_end(va); - } - -static void putcp(void* p,char c) - { - *(*((char**)p))++ = c; - } +} +static void putcp(void* p, char c) { *(*((char**)p))++ = c; } - -void tfp_sprintf(char* s,char *fmt, ...) - { +void tfp_sprintf(char* s, char* fmt, ...) { va_list va; - va_start(va,fmt); - tfp_format(&s,putcp,fmt,va); - putcp(&s,0); + va_start(va, fmt); + tfp_format(&s, putcp, fmt, va); + putcp(&s, 0); va_end(va); - } +} diff --git a/tmk_core/common/chibios/printf.h b/tmk_core/common/chibios/printf.h index 678a100c6..2cdf55ed9 100644 --- a/tmk_core/common/chibios/printf.h +++ b/tmk_core/common/chibios/printf.h @@ -15,7 +15,7 @@ version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public @@ -24,35 +24,35 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA This library is realy just two files: 'printf.h' and 'printf.c'. -They provide a simple and small (+200 loc) printf functionality to +They provide a simple and small (+200 loc) printf functionality to be used in embedded systems. -I've found them so usefull in debugging that I do not bother with a +I've found them so usefull in debugging that I do not bother with a debugger at all. -They are distributed in source form, so to use them, just compile them -into your project. +They are distributed in source form, so to use them, just compile them +into your project. -Two printf variants are provided: printf and sprintf. +Two printf variants are provided: printf and sprintf. The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'. Zero padding and field width are also supported. -If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the +If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the long specifier is also supported. Note that this will pull in some long math routines (pun intended!) and thus make your executable noticably longer. -The memory foot print of course depends on the target cpu, compiler and -compiler options, but a rough guestimate (based on a H8S target) is about -1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space. -Not too bad. Your milage may vary. By hacking the source code you can -get rid of some hunred bytes, I'm sure, but personally I feel the balance of +The memory foot print of course depends on the target cpu, compiler and +compiler options, but a rough guestimate (based on a H8S target) is about +1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space. +Not too bad. Your milage may vary. By hacking the source code you can +get rid of some hunred bytes, I'm sure, but personally I feel the balance of functionality and flexibility versus code size is close to optimal for many embedded systems. -To use the printf you need to supply your own character output function, +To use the printf you need to supply your own character output function, something like : void putc ( void* p, char c) @@ -61,25 +61,25 @@ something like : SERIAL_PORT_TX_REGISTER = c; } -Before you can call printf you need to initialize it to use your +Before you can call printf you need to initialize it to use your character output function with something like: init_printf(NULL,putc); -Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc', -the NULL (or any pointer) you pass into the 'init_printf' will eventually be -passed to your 'putc' routine. This allows you to pass some storage space (or -anything realy) to the character output function, if necessary. -This is not often needed but it was implemented like that because it made +Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc', +the NULL (or any pointer) you pass into the 'init_printf' will eventually be +passed to your 'putc' routine. This allows you to pass some storage space (or +anything realy) to the character output function, if necessary. +This is not often needed but it was implemented like that because it made implementing the sprintf function so neat (look at the source code). -The code is re-entrant, except for the 'init_printf' function, so it -is safe to call it from interupts too, although this may result in mixed output. +The code is re-entrant, except for the 'init_printf' function, so it +is safe to call it from interupts too, although this may result in mixed output. If you rely on re-entrancy, take care that your 'putc' function is re-entrant! -The printf and sprintf functions are actually macros that translate to +The printf and sprintf functions are actually macros that translate to 'tfp_printf' and 'tfp_sprintf'. This makes it possible -to use them along with 'stdio.h' printf's in a single source file. +to use them along with 'stdio.h' printf's in a single source file. You just need to undef the names before you include the 'stdio.h'. Note that these are not function like macros, so if you have variables or struct members with these names, things will explode in your face. @@ -92,20 +92,19 @@ For further details see source code. regs Kusti, 23.10.2004 */ - #ifndef __TFP_PRINTF__ #define __TFP_PRINTF__ #include <stdarg.h> -void init_printf(void* putp,void (*putf) (void*,char)); +void init_printf(void* putp, void (*putf)(void*, char)); -void tfp_printf(char *fmt, ...); -void tfp_sprintf(char* s,char *fmt, ...); +void tfp_printf(char* fmt, ...); +void tfp_sprintf(char* s, char* fmt, ...); -void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va); +void tfp_format(void* putp, void (*putf)(void*, char), char* fmt, va_list va); -#define printf tfp_printf -#define sprintf tfp_sprintf +#define printf tfp_printf +#define sprintf tfp_sprintf #endif diff --git a/tmk_core/common/chibios/sleep_led.c b/tmk_core/common/chibios/sleep_led.c index 4c35cfcba..df1a1b5fc 100644 --- a/tmk_core/common/chibios/sleep_led.c +++ b/tmk_core/common/chibios/sleep_led.c @@ -12,14 +12,14 @@ #if defined(KL2x) || defined(K20x) /* Use Low Power Timer (LPTMR) */ -#define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR -#define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF +# define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR +# define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF #elif defined(STM32F0XX) /* Use TIM14 manually */ -#define TIMER_INTERRUPT_VECTOR STM32_TIM14_HANDLER -#define RESET_COUNTER STM32_TIM14->SR &= ~STM32_TIM_SR_UIF +# define TIMER_INTERRUPT_VECTOR STM32_TIM14_HANDLER +# define RESET_COUNTER STM32_TIM14->SR &= ~STM32_TIM_SR_UIF #endif @@ -31,39 +31,34 @@ * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } */ -static const uint8_t breathing_table[64] = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, -15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, -255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, -15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; +static const uint8_t breathing_table[64] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* interrupt handler */ OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) { OSAL_IRQ_PROLOGUE(); /* Software PWM - * timer:1111 1111 1111 1111 - * \_____/\/ \_______/____ count(0-255) - * \ \______________ duration of step(4) - * \__________________ index of step table(0-63) - */ + * timer:1111 1111 1111 1111 + * \_____/\/ \_______/____ count(0-255) + * \ \______________ duration of step(4) + * \__________________ index of step table(0-63) + */ // this works for cca 65536 irqs/sec static union { - uint16_t row; - struct { - uint8_t count:8; - uint8_t duration:2; - uint8_t index:6; - } pwm; - } timer = { .row = 0 }; + uint16_t row; + struct { + uint8_t count : 8; + uint8_t duration : 2; + uint8_t index : 6; + } pwm; + } timer = {.row = 0}; timer.row++; // LED on if (timer.pwm.count == 0) { - led_set(1<<USB_LED_CAPS_LOCK); + led_set(1 << USB_LED_CAPS_LOCK); } // LED off if (timer.pwm.count == breathing_table[timer.pwm.index]) { @@ -78,19 +73,18 @@ OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) { #endif /* common parts for known platforms */ - #if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */ /* LPTMR clock options */ -#define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */ -#define LPTMR_CLOCK_LPO 1 /* 1kHz clock */ -#define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */ -#define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */ +# define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */ +# define LPTMR_CLOCK_LPO 1 /* 1kHz clock */ +# define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */ +# define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */ /* Work around inconsistencies in Freescale naming */ -#if !defined(SIM_SCGC5_LPTMR) -#define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER -#endif +# if !defined(SIM_SCGC5_LPTMR) +# define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER +# endif /* Initialise the timer */ void sleep_led_init(void) { @@ -101,52 +95,52 @@ void sleep_led_init(void) { /* Set the compare value */ LPTMR0->CMR = 0; // trigger on counter value (i.e. every time) - /* Set up clock source and prescaler */ - /* Software PWM - * ______ ______ __ - * | ON |___OFF___| ON |___OFF___| .... - * |<-------------->|<-------------->|<- .... - * PWM period PWM period - * - * R interrupts/period[resolution] - * F periods/second[frequency] - * R * F interrupts/second - */ - - /* === OPTION 1 === */ - #if 0 +/* Set up clock source and prescaler */ +/* Software PWM + * ______ ______ __ + * | ON |___OFF___| ON |___OFF___| .... + * |<-------------->|<-------------->|<- .... + * PWM period PWM period + * + * R interrupts/period[resolution] + * F periods/second[frequency] + * R * F interrupts/second + */ + +/* === OPTION 1 === */ +# if 0 // 1kHz LPO // No prescaler => 1024 irqs/sec // Note: this is too slow for a smooth breathe LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_LPO)|LPTMRx_PSR_PBYP; - #endif /* OPTION 1 */ +# endif /* OPTION 1 */ - /* === OPTION 2 === */ - #if 1 +/* === OPTION 2 === */ +# if 1 // nMHz IRC (n=4 on KL25Z, KL26Z and K20x; n=2 or 8 on KL27Z) - MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock - #if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others + MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock +# if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others MCG->MC |= MCG_MC_LIRC_DIV2_DIV2; - #endif /* KL27 */ - MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock +# endif /* KL27 */ + MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock // to work in stop mode, also MCG_C1_IREFSTEN // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec => // => approx F=61, R=256, duration = 4 - LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK)|LPTMRx_PSR_PRESCALE(6); - #endif /* OPTION 2 */ + LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK) | LPTMRx_PSR_PRESCALE(6); +# endif /* OPTION 2 */ - /* === OPTION 3 === */ - #if 0 +/* === OPTION 3 === */ +# if 0 // OSC output (external crystal), usually 8MHz or 16MHz OSC0->CR |= OSC_CR_ERCLKEN; // enable ext ref clock // to work in stop mode, also OSC_CR_EREFSTEN // Divide by 2^N LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_OSCERCLK)|LPTMRx_PSR_PRESCALE(7); - #endif /* OPTION 3 */ +# endif /* OPTION 3 */ /* === END OPTIONS === */ /* Interrupt on TCF set (compare flag) */ - nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority + nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority LPTMR0->CSR |= LPTMRx_CSR_TIE; } @@ -205,20 +199,14 @@ void sleep_led_toggle(void) { STM32_TIM14->CR1 ^= STM32_TIM_CR1_CEN; } - #else /* platform selection: not on familiar chips */ -void sleep_led_init(void) { -} - -void sleep_led_enable(void) { - led_set(1<<USB_LED_CAPS_LOCK); -} - -void sleep_led_disable(void) { - led_set(0); -} - +void sleep_led_init(void) {} + +void sleep_led_enable(void) { led_set(1 << USB_LED_CAPS_LOCK); } + +void sleep_led_disable(void) { led_set(0); } + void sleep_led_toggle(void) { // not implemented } diff --git a/tmk_core/common/chibios/suspend.c b/tmk_core/common/chibios/suspend.c index 6027d69b7..ae1c6f53e 100644 --- a/tmk_core/common/chibios/suspend.c +++ b/tmk_core/common/chibios/suspend.c @@ -17,49 +17,44 @@ * FIXME: needs doc */ void suspend_idle(uint8_t time) { - // TODO: this is not used anywhere - what units is 'time' in? - wait_ms(time); + // TODO: this is not used anywhere - what units is 'time' in? + wait_ms(time); } /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_user (void) { } +__attribute__((weak)) void suspend_power_down_user(void) {} /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_kb(void) { - suspend_power_down_user(); -} +__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); } /** \brief suspend power down * * FIXME: needs doc */ void suspend_power_down(void) { - // TODO: figure out what to power down and how - // shouldn't power down TPM/FTM if we want a breathing LED - // also shouldn't power down USB + // TODO: figure out what to power down and how + // shouldn't power down TPM/FTM if we want a breathing LED + // also shouldn't power down USB - suspend_power_down_kb(); - // on AVR, this enables the watchdog for 15ms (max), and goes to - // SLEEP_MODE_PWR_DOWN + suspend_power_down_kb(); + // on AVR, this enables the watchdog for 15ms (max), and goes to + // SLEEP_MODE_PWR_DOWN - wait_ms(17); + wait_ms(17); } /** \brief suspend wakeup condition * * FIXME: needs doc */ -__attribute__ ((weak)) void matrix_power_up(void) {} -__attribute__ ((weak)) void matrix_power_down(void) {} -bool suspend_wakeup_condition(void) -{ +__attribute__((weak)) void matrix_power_up(void) {} +__attribute__((weak)) void matrix_power_down(void) {} +bool suspend_wakeup_condition(void) { matrix_power_up(); matrix_scan(); matrix_power_down(); @@ -73,25 +68,20 @@ bool suspend_wakeup_condition(void) * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_user(void) { } +__attribute__((weak)) void suspend_wakeup_init_user(void) {} /** \brief run keyboard level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_kb(void) { - suspend_wakeup_init_user(); -} +__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); } /** \brief suspend wakeup condition * * run immediately after wakeup * FIXME: needs doc */ -void suspend_wakeup_init(void) -{ +void suspend_wakeup_init(void) { // clear keyboard state // need to do it manually, because we're running from ISR // and clear_keyboard() calls print @@ -111,5 +101,5 @@ void suspend_wakeup_init(void) #ifdef BACKLIGHT_ENABLE backlight_init(); #endif /* BACKLIGHT_ENABLE */ - suspend_wakeup_init_kb(); + suspend_wakeup_init_kb(); } diff --git a/tmk_core/common/chibios/timer.c b/tmk_core/common/chibios/timer.c index 473e533ca..1ce9d1d17 100644 --- a/tmk_core/common/chibios/timer.c +++ b/tmk_core/common/chibios/timer.c @@ -2,40 +2,32 @@ #include "timer.h" -static systime_t last_systime = 0; -static systime_t overflow = 0; -static uint32_t current_time_ms = 0; +static systime_t last_systime = 0; +static systime_t overflow = 0; +static uint32_t current_time_ms = 0; -void timer_init(void) { - timer_clear(); -} +void timer_init(void) { timer_clear(); } void timer_clear(void) { - last_systime = chVTGetSystemTime(); - overflow = 0; - current_time_ms = 0; + last_systime = chVTGetSystemTime(); + overflow = 0; + current_time_ms = 0; } -uint16_t timer_read(void) { - return (uint16_t)timer_read32(); -} +uint16_t timer_read(void) { return (uint16_t)timer_read32(); } uint32_t timer_read32(void) { - // Note: We assume that the timer update is called at least once betweeen every wrap around of the system time - systime_t current_systime = chVTGetSystemTime(); - systime_t elapsed = current_systime - last_systime + overflow; - uint32_t elapsed_ms = ST2MS(elapsed); - current_time_ms += elapsed_ms; - overflow = elapsed - MS2ST(elapsed_ms); - last_systime = current_systime; - - return current_time_ms; + // Note: We assume that the timer update is called at least once betweeen every wrap around of the system time + systime_t current_systime = chVTGetSystemTime(); + systime_t elapsed = current_systime - last_systime + overflow; + uint32_t elapsed_ms = ST2MS(elapsed); + current_time_ms += elapsed_ms; + overflow = elapsed - MS2ST(elapsed_ms); + last_systime = current_systime; + + return current_time_ms; } -uint16_t timer_elapsed(uint16_t last) { - return timer_read() - last; -} +uint16_t timer_elapsed(uint16_t last) { return timer_read() - last; } -uint32_t timer_elapsed32(uint32_t last) { - return timer_read32() - last; -} +uint32_t timer_elapsed32(uint32_t last) { return timer_read32() - last; } diff --git a/tmk_core/common/command.c b/tmk_core/common/command.c index 949615ecf..8bf72ef25 100644 --- a/tmk_core/common/command.c +++ b/tmk_core/common/command.c @@ -37,25 +37,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "version.h" #ifdef MOUSEKEY_ENABLE -#include "mousekey.h" +# include "mousekey.h" #endif #ifdef PROTOCOL_PJRC - #include "usb_keyboard.h" - #ifdef EXTRAKEY_ENABLE - #include "usb_extra.h" - #endif +# include "usb_keyboard.h" +# ifdef EXTRAKEY_ENABLE +# include "usb_extra.h" +# endif #endif #ifdef PROTOCOL_VUSB - #include "usbdrv.h" +# include "usbdrv.h" #endif #ifdef AUDIO_ENABLE - #include "audio.h" +# include "audio.h" #endif /* AUDIO_ENABLE */ - static bool command_common(uint8_t code); static void command_common_help(void); static void print_version(void); @@ -69,16 +68,12 @@ static void mousekey_console_help(void); static void switch_default_layer(uint8_t layer); - command_state_t command_state = ONESHOT; - -bool command_proc(uint8_t code) -{ +bool command_proc(uint8_t code) { switch (command_state) { case ONESHOT: - if (!IS_COMMAND()) - return false; + if (!IS_COMMAND()) return false; return (command_extra(code) || command_common(code)); break; case CONSOLE: @@ -101,86 +96,63 @@ bool command_proc(uint8_t code) /* TODO: Refactoring is needed. */ /* This allows to define extra commands. return false when not processed. */ -bool command_extra(uint8_t code) __attribute__ ((weak)); -bool command_extra(uint8_t code) -{ +bool command_extra(uint8_t code) __attribute__((weak)); +bool command_extra(uint8_t code) { (void)code; return false; } -bool command_console_extra(uint8_t code) __attribute__ ((weak)); -bool command_console_extra(uint8_t code) -{ +bool command_console_extra(uint8_t code) __attribute__((weak)); +bool command_console_extra(uint8_t code) { (void)code; return false; } - /*********************************************************** * Command common ***********************************************************/ -static void command_common_help(void) -{ - print( "\n\t- Magic -\n" - STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n" - STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" - STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n" - STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n" - STR(MAGIC_KEY_VERSION ) ": Version\n" - STR(MAGIC_KEY_STATUS ) ": Status\n" - STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n" +static void command_common_help(void) { + print("\n\t- Magic -\n" STR(MAGIC_KEY_DEBUG) ": Debug Message Toggle\n" STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" STR(MAGIC_KEY_DEBUG_KBD) ": Keyboard Debug Toggle - Show keypress report\n" STR(MAGIC_KEY_DEBUG_MOUSE) ": Debug Mouse Toggle\n" STR(MAGIC_KEY_VERSION) ": Version\n" STR(MAGIC_KEY_STATUS) ": Status\n" STR(MAGIC_KEY_CONSOLE) ": Activate Console Mode\n" #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM - STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n" - STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n" - STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n" - STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n" - STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n" - STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n" - STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n" - STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n" - STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n" - STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n" + STR(MAGIC_KEY_LAYER0) ": Switch to Layer 0\n" STR(MAGIC_KEY_LAYER1) ": Switch to Layer 1\n" STR(MAGIC_KEY_LAYER2) ": Switch to Layer 2\n" STR(MAGIC_KEY_LAYER3) ": Switch to Layer 3\n" STR(MAGIC_KEY_LAYER4) ": Switch to Layer 4\n" STR(MAGIC_KEY_LAYER5) ": Switch to Layer 5\n" STR(MAGIC_KEY_LAYER6) ": Switch to Layer 6\n" STR(MAGIC_KEY_LAYER7) ": Switch to Layer 7\n" STR(MAGIC_KEY_LAYER8) ": Switch to Layer 8\n" STR(MAGIC_KEY_LAYER9) ": Switch to Layer 9\n" #endif #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS - "F1-F10: Switch to Layer 0-9 (F10 = L0)\n" + "F1-F10: Switch to Layer 0-9 (F10 = L0)\n" #endif #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS - "0-9: Switch to Layer 0-9\n" + "0-9: Switch to Layer 0-9\n" #endif - STR(MAGIC_KEY_LAYER0_ALT ) ": Switch to Layer 0 (alternate)\n" + STR(MAGIC_KEY_LAYER0_ALT) ": Switch to Layer 0 (alternate)\n" - STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader\n" - STR(MAGIC_KEY_BOOTLOADER_ALT) ": Jump to Bootloader (alternate)\n" + STR(MAGIC_KEY_BOOTLOADER) ": Jump to Bootloader\n" STR(MAGIC_KEY_BOOTLOADER_ALT) ": Jump to Bootloader (alternate)\n" #ifdef KEYBOARD_LOCK_ENABLE - STR(MAGIC_KEY_LOCK ) ": Lock Keyboard\n" + STR(MAGIC_KEY_LOCK) ": Lock Keyboard\n" #endif - STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n" - STR(MAGIC_KEY_EEPROM_CLEAR) ": Clear EEPROM\n" + STR(MAGIC_KEY_EEPROM) ": Print EEPROM Settings\n" STR(MAGIC_KEY_EEPROM_CLEAR) ": Clear EEPROM\n" #ifdef NKRO_ENABLE - STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n" + STR(MAGIC_KEY_NKRO) ": NKRO Toggle\n" #endif #ifdef SLEEP_LED_ENABLE - STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n" + STR(MAGIC_KEY_SLEEP_LED) ": Sleep LED Test\n" #endif ); } -static void print_version(void) -{ - // print version & information +static void print_version(void) { + // print version & information print("\n\t- Version -\n"); print("DESC: " STR(DESCRIPTION) "\n"); print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") " - "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") " - "VER: " STR(DEVICE_VER) "\n"); + "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") " + "VER: " STR(DEVICE_VER) "\n"); #ifdef SKIP_VERSION print("BUILD: (" __DATE__ ")\n"); #else @@ -191,51 +163,48 @@ static void print_version(void) print("OPTIONS:" #ifdef PROTOCOL_PJRC - " PJRC" + " PJRC" #endif #ifdef PROTOCOL_LUFA - " LUFA" + " LUFA" #endif #ifdef PROTOCOL_VUSB - " VUSB" + " VUSB" #endif #ifdef BOOTMAGIC_ENABLE - " BOOTMAGIC" + " BOOTMAGIC" #endif #ifdef MOUSEKEY_ENABLE - " MOUSEKEY" + " MOUSEKEY" #endif #ifdef EXTRAKEY_ENABLE - " EXTRAKEY" + " EXTRAKEY" #endif #ifdef CONSOLE_ENABLE - " CONSOLE" + " CONSOLE" #endif #ifdef COMMAND_ENABLE - " COMMAND" + " COMMAND" #endif #ifdef NKRO_ENABLE - " NKRO" + " NKRO" #endif #ifdef KEYMAP_SECTION_ENABLE - " KEYMAP_SECTION" + " KEYMAP_SECTION" #endif - " " STR(BOOTLOADER_SIZE) "\n"); + " " STR(BOOTLOADER_SIZE) "\n"); print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__) #if defined(__AVR__) - " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ - " AVR_ARCH: avr" STR(__AVR_ARCH__) + " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ " AVR_ARCH: avr" STR(__AVR_ARCH__) #endif - "\n"); + "\n"); - return; + return; } -static void print_status(void) -{ - +static void print_status(void) { print("\n\t- Status -\n"); print_val_hex8(host_keyboard_leds()); @@ -258,67 +227,101 @@ static void print_status(void) #endif #ifdef PROTOCOL_PJRC -# if USB_COUNT_SOF +# if USB_COUNT_SOF print_val_hex8(usbSofCount); -# endif +# endif #endif - return; + return; } -static void print_eeconfig(void) -{ - +static void print_eeconfig(void) { // Print these variables if NO_PRINT or USER_PRINT are not defined. #if !defined(NO_PRINT) && !defined(USER_PRINT) - print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n"); + print("default_layer: "); + print_dec(eeconfig_read_default_layer()); + print("\n"); debug_config_t dc; dc.raw = eeconfig_read_debug(); - print("debug_config.raw: "); print_hex8(dc.raw); print("\n"); - print(".enable: "); print_dec(dc.enable); print("\n"); - print(".matrix: "); print_dec(dc.matrix); print("\n"); - print(".keyboard: "); print_dec(dc.keyboard); print("\n"); - print(".mouse: "); print_dec(dc.mouse); print("\n"); + print("debug_config.raw: "); + print_hex8(dc.raw); + print("\n"); + print(".enable: "); + print_dec(dc.enable); + print("\n"); + print(".matrix: "); + print_dec(dc.matrix); + print("\n"); + print(".keyboard: "); + print_dec(dc.keyboard); + print("\n"); + print(".mouse: "); + print_dec(dc.mouse); + print("\n"); keymap_config_t kc; kc.raw = eeconfig_read_keymap(); - print("keymap_config.raw: "); print_hex8(kc.raw); print("\n"); - print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n"); - print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n"); - print(".swap_lctl_lgui: "); print_dec(kc.swap_lctl_lgui); print("\n"); - print(".swap_rctl_rgui: "); print_dec(kc.swap_rctl_rgui); print("\n"); - print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n"); - print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n"); - print(".no_gui: "); print_dec(kc.no_gui); print("\n"); - print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n"); - print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n"); - print(".nkro: "); print_dec(kc.nkro); print("\n"); - -#ifdef BACKLIGHT_ENABLE + print("keymap_config.raw: "); + print_hex8(kc.raw); + print("\n"); + print(".swap_control_capslock: "); + print_dec(kc.swap_control_capslock); + print("\n"); + print(".capslock_to_control: "); + print_dec(kc.capslock_to_control); + print("\n"); + print(".swap_lctl_lgui: "); + print_dec(kc.swap_lctl_lgui); + print("\n"); + print(".swap_rctl_rgui: "); + print_dec(kc.swap_rctl_rgui); + print("\n"); + print(".swap_lalt_lgui: "); + print_dec(kc.swap_lalt_lgui); + print("\n"); + print(".swap_ralt_rgui: "); + print_dec(kc.swap_ralt_rgui); + print("\n"); + print(".no_gui: "); + print_dec(kc.no_gui); + print("\n"); + print(".swap_grave_esc: "); + print_dec(kc.swap_grave_esc); + print("\n"); + print(".swap_backslash_backspace: "); + print_dec(kc.swap_backslash_backspace); + print("\n"); + print(".nkro: "); + print_dec(kc.nkro); + print("\n"); + +# ifdef BACKLIGHT_ENABLE backlight_config_t bc; bc.raw = eeconfig_read_backlight(); - print("backlight_config.raw: "); print_hex8(bc.raw); print("\n"); - print(".enable: "); print_dec(bc.enable); print("\n"); - print(".level: "); print_dec(bc.level); print("\n"); -#endif /* BACKLIGHT_ENABLE */ + print("backlight_config.raw: "); + print_hex8(bc.raw); + print("\n"); + print(".enable: "); + print_dec(bc.enable); + print("\n"); + print(".level: "); + print_dec(bc.level); + print("\n"); +# endif /* BACKLIGHT_ENABLE */ #endif /* !NO_PRINT */ - } -static bool command_common(uint8_t code) -{ - +static bool command_common(uint8_t code) { #ifdef KEYBOARD_LOCK_ENABLE static host_driver_t *host_driver = 0; #endif switch (code) { - #ifdef SLEEP_LED_ENABLE - // test breathing sleep LED + // test breathing sleep LED case MAGIC_KC(MAGIC_KEY_SLEEP_LED): print("Sleep LED Test\n"); sleep_led_toggle(); @@ -326,21 +329,21 @@ static bool command_common(uint8_t code) break; #endif - // print stored eeprom config + // print stored eeprom config case MAGIC_KC(MAGIC_KEY_EEPROM): print("eeconfig:\n"); print_eeconfig(); break; - // clear eeprom + // clear eeprom case MAGIC_KC(MAGIC_KEY_EEPROM_CLEAR): print("Clearing EEPROM\n"); - eeconfig_init(); + eeconfig_init(); break; #ifdef KEYBOARD_LOCK_ENABLE - // lock/unlock keyboard + // lock/unlock keyboard case MAGIC_KC(MAGIC_KEY_LOCK): if (host_get_driver()) { host_driver = host_get_driver(); @@ -354,13 +357,13 @@ static bool command_common(uint8_t code) break; #endif - // print help + // print help case MAGIC_KC(MAGIC_KEY_HELP): case MAGIC_KC(MAGIC_KEY_HELP_ALT): command_common_help(); break; - // activate console + // activate console case MAGIC_KC(MAGIC_KEY_CONSOLE): debug_matrix = false; debug_keyboard = false; @@ -374,15 +377,15 @@ static bool command_common(uint8_t code) // jump to bootloader case MAGIC_KC(MAGIC_KEY_BOOTLOADER): case MAGIC_KC(MAGIC_KEY_BOOTLOADER_ALT): - clear_keyboard(); // clear to prevent stuck keys + clear_keyboard(); // clear to prevent stuck keys print("\n\nJumping to bootloader... "); - #ifdef AUDIO_ENABLE - stop_all_notes(); - shutdown_user(); - #else - wait_ms(1000); - #endif - bootloader_jump(); // not return +#ifdef AUDIO_ENABLE + stop_all_notes(); + shutdown_user(); +#else + wait_ms(1000); +#endif + bootloader_jump(); // not return break; // debug toggle @@ -427,25 +430,25 @@ static bool command_common(uint8_t code) print("\nmouse: on\n"); debug_enable = true; } else { - print("\nmouse: off\n"); + print("\nmouse: off\n"); } break; - // print version + // print version case MAGIC_KC(MAGIC_KEY_VERSION): - print_version(); - break; + print_version(); + break; - // print status - case MAGIC_KC(MAGIC_KEY_STATUS): - print_status(); + // print status + case MAGIC_KC(MAGIC_KEY_STATUS): + print_status(); break; #ifdef NKRO_ENABLE - // NKRO toggle + // NKRO toggle case MAGIC_KC(MAGIC_KEY_NKRO): - clear_keyboard(); // clear to prevent stuck keys + clear_keyboard(); // clear to prevent stuck keys keymap_config.nkro = !keymap_config.nkro; if (keymap_config.nkro) { print("NKRO: on\n"); @@ -455,56 +458,55 @@ static bool command_common(uint8_t code) break; #endif - // switch layers + // switch layers - case MAGIC_KC(MAGIC_KEY_LAYER0_ALT): + case MAGIC_KC(MAGIC_KEY_LAYER0_ALT): switch_default_layer(0); break; #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM - case MAGIC_KC(MAGIC_KEY_LAYER0): + case MAGIC_KC(MAGIC_KEY_LAYER0): switch_default_layer(0); break; - case MAGIC_KC(MAGIC_KEY_LAYER1): + case MAGIC_KC(MAGIC_KEY_LAYER1): switch_default_layer(1); break; - case MAGIC_KC(MAGIC_KEY_LAYER2): + case MAGIC_KC(MAGIC_KEY_LAYER2): switch_default_layer(2); break; - case MAGIC_KC(MAGIC_KEY_LAYER3): + case MAGIC_KC(MAGIC_KEY_LAYER3): switch_default_layer(3); break; - case MAGIC_KC(MAGIC_KEY_LAYER4): + case MAGIC_KC(MAGIC_KEY_LAYER4): switch_default_layer(4); break; - case MAGIC_KC(MAGIC_KEY_LAYER5): + case MAGIC_KC(MAGIC_KEY_LAYER5): switch_default_layer(5); break; - case MAGIC_KC(MAGIC_KEY_LAYER6): + case MAGIC_KC(MAGIC_KEY_LAYER6): switch_default_layer(6); break; - case MAGIC_KC(MAGIC_KEY_LAYER7): + case MAGIC_KC(MAGIC_KEY_LAYER7): switch_default_layer(7); break; - case MAGIC_KC(MAGIC_KEY_LAYER8): + case MAGIC_KC(MAGIC_KEY_LAYER8): switch_default_layer(8); break; - case MAGIC_KC(MAGIC_KEY_LAYER9): + case MAGIC_KC(MAGIC_KEY_LAYER9): switch_default_layer(9); break; #endif - #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS case KC_F1 ... KC_F9: @@ -532,12 +534,10 @@ static bool command_common(uint8_t code) return true; } - /*********************************************************** * Command console ***********************************************************/ -static void command_console_help(void) -{ +static void command_console_help(void) { print("\n\t- Console -\n" "ESC/q: quit\n" #ifdef MOUSEKEY_ENABLE @@ -546,8 +546,7 @@ static void command_console_help(void) ); } -static bool command_console(uint8_t code) -{ +static bool command_console(uint8_t code) { switch (code) { case KC_H: case KC_SLASH: /* ? */ @@ -572,32 +571,40 @@ static bool command_console(uint8_t code) return true; } - #ifdef MOUSEKEY_ENABLE /*********************************************************** * Mousekey console ***********************************************************/ static uint8_t mousekey_param = 0; -static void mousekey_param_print(void) -{ +static void mousekey_param_print(void) { // Print these variables if NO_PRINT or USER_PRINT are not defined. -#if !defined(NO_PRINT) && !defined(USER_PRINT) +# if !defined(NO_PRINT) && !defined(USER_PRINT) print("\n\t- Values -\n"); - print("1: delay(*10ms): "); pdec(mk_delay); print("\n"); - print("2: interval(ms): "); pdec(mk_interval); print("\n"); - print("3: max_speed: "); pdec(mk_max_speed); print("\n"); - print("4: time_to_max: "); pdec(mk_time_to_max); print("\n"); - print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n"); - print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n"); -#endif /* !NO_PRINT */ - + print("1: delay(*10ms): "); + pdec(mk_delay); + print("\n"); + print("2: interval(ms): "); + pdec(mk_interval); + print("\n"); + print("3: max_speed: "); + pdec(mk_max_speed); + print("\n"); + print("4: time_to_max: "); + pdec(mk_time_to_max); + print("\n"); + print("5: wheel_max_speed: "); + pdec(mk_wheel_max_speed); + print("\n"); + print("6: wheel_time_to_max: "); + pdec(mk_wheel_time_to_max); + print("\n"); +# endif /* !NO_PRINT */ } //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n"); -#define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v)) -static void mousekey_param_inc(uint8_t param, uint8_t inc) -{ +# define PRINT_SET_VAL(v) xprintf(# v " = %d\n", (v)) +static void mousekey_param_inc(uint8_t param, uint8_t inc) { switch (param) { case 1: if (mk_delay + inc < UINT8_MAX) @@ -644,8 +651,7 @@ static void mousekey_param_inc(uint8_t param, uint8_t inc) } } -static void mousekey_param_dec(uint8_t param, uint8_t dec) -{ +static void mousekey_param_dec(uint8_t param, uint8_t dec) { switch (param) { case 1: if (mk_delay > dec) @@ -692,8 +698,7 @@ static void mousekey_param_dec(uint8_t param, uint8_t dec) } } -static void mousekey_console_help(void) -{ +static void mousekey_console_help(void) { print("\n\t- Mousekey -\n" "ESC/q: quit\n" "1: delay(*10ms)\n" @@ -712,11 +717,11 @@ static void mousekey_console_help(void) "\n" "speed = delta * max_speed * (repeat / time_to_max)\n"); xprintf("where delta: cursor=%d, wheel=%d\n" - "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA); + "See http://en.wikipedia.org/wiki/Mouse_keys\n", + MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA); } -static bool mousekey_console(uint8_t code) -{ +static bool mousekey_console(uint8_t code) { switch (code) { case KC_H: case KC_SLASH: /* ? */ @@ -756,11 +761,11 @@ static bool mousekey_console(uint8_t code) mousekey_param_dec(mousekey_param, 10); break; case KC_D: - mk_delay = MOUSEKEY_DELAY/10; - mk_interval = MOUSEKEY_INTERVAL; - mk_max_speed = MOUSEKEY_MAX_SPEED; - mk_time_to_max = MOUSEKEY_TIME_TO_MAX; - mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; + mk_delay = MOUSEKEY_DELAY / 10; + mk_interval = MOUSEKEY_INTERVAL; + mk_max_speed = MOUSEKEY_MAX_SPEED; + mk_time_to_max = MOUSEKEY_TIME_TO_MAX; + mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; print("set default\n"); break; @@ -771,36 +776,43 @@ static bool mousekey_console(uint8_t code) if (mousekey_param) { xprintf("M%d> ", mousekey_param); } else { - print("M>" ); + print("M>"); } return true; } #endif - /*********************************************************** * Utilities ***********************************************************/ -uint8_t numkey2num(uint8_t code) -{ +uint8_t numkey2num(uint8_t code) { switch (code) { - case KC_1: return 1; - case KC_2: return 2; - case KC_3: return 3; - case KC_4: return 4; - case KC_5: return 5; - case KC_6: return 6; - case KC_7: return 7; - case KC_8: return 8; - case KC_9: return 9; - case KC_0: return 0; + case KC_1: + return 1; + case KC_2: + return 2; + case KC_3: + return 3; + case KC_4: + return 4; + case KC_5: + return 5; + case KC_6: + return 6; + case KC_7: + return 7; + case KC_8: + return 8; + case KC_9: + return 9; + case KC_0: + return 0; } return 0; } -static void switch_default_layer(uint8_t layer) -{ +static void switch_default_layer(uint8_t layer) { xprintf("L%d\n", layer); - default_layer_set(1UL<<layer); + default_layer_set(1UL << layer); clear_keyboard(); } diff --git a/tmk_core/common/command.h b/tmk_core/common/command.h index e6e67fae4..4f77be085 100644 --- a/tmk_core/common/command.h +++ b/tmk_core/common/command.h @@ -29,135 +29,135 @@ bool command_console_extra(uint8_t code); #ifdef COMMAND_ENABLE uint8_t numkey2num(uint8_t code); -bool command_proc(uint8_t code); +bool command_proc(uint8_t code); #else -#define command_proc(code) false +# define command_proc(code) false #endif #ifndef IS_COMMAND -#define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT) +# define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT) #endif #ifndef MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS -#define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS true +# define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS true #endif #ifndef MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS -#define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS true +# define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS true #endif #ifndef MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM -#define MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM false +# define MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM false #endif #ifndef MAGIC_KEY_HELP -#define MAGIC_KEY_HELP H +# define MAGIC_KEY_HELP H #endif #ifndef MAGIC_KEY_HELP_ALT -#define MAGIC_KEY_HELP_ALT SLASH +# define MAGIC_KEY_HELP_ALT SLASH #endif #ifndef MAGIC_KEY_DEBUG -#define MAGIC_KEY_DEBUG D +# define MAGIC_KEY_DEBUG D #endif #ifndef MAGIC_KEY_DEBUG_MATRIX -#define MAGIC_KEY_DEBUG_MATRIX X +# define MAGIC_KEY_DEBUG_MATRIX X #endif #ifndef MAGIC_KEY_DEBUG_KBD -#define MAGIC_KEY_DEBUG_KBD K +# define MAGIC_KEY_DEBUG_KBD K #endif #ifndef MAGIC_KEY_DEBUG_MOUSE -#define MAGIC_KEY_DEBUG_MOUSE M +# define MAGIC_KEY_DEBUG_MOUSE M #endif #ifndef MAGIC_KEY_VERSION -#define MAGIC_KEY_VERSION V +# define MAGIC_KEY_VERSION V #endif #ifndef MAGIC_KEY_STATUS -#define MAGIC_KEY_STATUS S +# define MAGIC_KEY_STATUS S #endif #ifndef MAGIC_KEY_CONSOLE -#define MAGIC_KEY_CONSOLE C +# define MAGIC_KEY_CONSOLE C #endif #ifndef MAGIC_KEY_LAYER0 -#define MAGIC_KEY_LAYER0 0 +# define MAGIC_KEY_LAYER0 0 #endif #ifndef MAGIC_KEY_LAYER0_ALT -#define MAGIC_KEY_LAYER0_ALT GRAVE +# define MAGIC_KEY_LAYER0_ALT GRAVE #endif #ifndef MAGIC_KEY_LAYER1 -#define MAGIC_KEY_LAYER1 1 +# define MAGIC_KEY_LAYER1 1 #endif #ifndef MAGIC_KEY_LAYER2 -#define MAGIC_KEY_LAYER2 2 +# define MAGIC_KEY_LAYER2 2 #endif #ifndef MAGIC_KEY_LAYER3 -#define MAGIC_KEY_LAYER3 3 +# define MAGIC_KEY_LAYER3 3 #endif #ifndef MAGIC_KEY_LAYER4 -#define MAGIC_KEY_LAYER4 4 +# define MAGIC_KEY_LAYER4 4 #endif #ifndef MAGIC_KEY_LAYER5 -#define MAGIC_KEY_LAYER5 5 +# define MAGIC_KEY_LAYER5 5 #endif #ifndef MAGIC_KEY_LAYER6 -#define MAGIC_KEY_LAYER6 6 +# define MAGIC_KEY_LAYER6 6 #endif #ifndef MAGIC_KEY_LAYER7 -#define MAGIC_KEY_LAYER7 7 +# define MAGIC_KEY_LAYER7 7 #endif #ifndef MAGIC_KEY_LAYER8 -#define MAGIC_KEY_LAYER8 8 +# define MAGIC_KEY_LAYER8 8 #endif #ifndef MAGIC_KEY_LAYER9 -#define MAGIC_KEY_LAYER9 9 +# define MAGIC_KEY_LAYER9 9 #endif #ifndef MAGIC_KEY_BOOTLOADER -#define MAGIC_KEY_BOOTLOADER B +# define MAGIC_KEY_BOOTLOADER B #endif #ifndef MAGIC_KEY_BOOTLOADER_ALT -#define MAGIC_KEY_BOOTLOADER_ALT ESC +# define MAGIC_KEY_BOOTLOADER_ALT ESC #endif #ifndef MAGIC_KEY_LOCK -#define MAGIC_KEY_LOCK CAPS +# define MAGIC_KEY_LOCK CAPS #endif #ifndef MAGIC_KEY_EEPROM -#define MAGIC_KEY_EEPROM E +# define MAGIC_KEY_EEPROM E #endif #ifndef MAGIC_KEY_EEPROM_CLEAR -#define MAGIC_KEY_EEPROM_CLEAR BSPACE +# define MAGIC_KEY_EEPROM_CLEAR BSPACE #endif #ifndef MAGIC_KEY_NKRO -#define MAGIC_KEY_NKRO N +# define MAGIC_KEY_NKRO N #endif #ifndef MAGIC_KEY_SLEEP_LED -#define MAGIC_KEY_SLEEP_LED Z +# define MAGIC_KEY_SLEEP_LED Z #endif -#define XMAGIC_KC(key) KC_ ## key -#define MAGIC_KC(key) XMAGIC_KC(key) +#define XMAGIC_KC(key) KC_##key +#define MAGIC_KC(key) XMAGIC_KC(key) diff --git a/tmk_core/common/debug.c b/tmk_core/common/debug.c index 18613fc28..bea96dfc1 100644 --- a/tmk_core/common/debug.c +++ b/tmk_core/common/debug.c @@ -7,10 +7,10 @@ debug_config_t debug_config = { /* GCC Bug 10676 - Using unnamed fields in initializers * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10676 */ #if GCC_VERSION >= 40600 - .enable = false, - .matrix = false, + .enable = false, + .matrix = false, .keyboard = false, - .mouse = false, + .mouse = false, .reserved = 0 #else { diff --git a/tmk_core/common/debug.h b/tmk_core/common/debug.h index 3cbe2092d..f9d611bb0 100644 --- a/tmk_core/common/debug.h +++ b/tmk_core/common/debug.h @@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> #include "print.h" - #ifdef __cplusplus extern "C" { #endif @@ -31,11 +30,11 @@ extern "C" { */ typedef union { struct { - bool enable:1; - bool matrix:1; - bool keyboard:1; - bool mouse:1; - uint8_t reserved:4; + bool enable : 1; + bool matrix : 1; + bool keyboard : 1; + bool mouse : 1; + uint8_t reserved : 4; }; uint8_t raw; } debug_config_t; @@ -47,70 +46,126 @@ extern debug_config_t debug_config; #endif /* for backward compatibility */ -#define debug_enable (debug_config.enable) -#define debug_matrix (debug_config.matrix) -#define debug_keyboard (debug_config.keyboard) -#define debug_mouse (debug_config.mouse) - +#define debug_enable (debug_config.enable) +#define debug_matrix (debug_config.matrix) +#define debug_keyboard (debug_config.keyboard) +#define debug_mouse (debug_config.mouse) /* * Debug print utils */ #ifndef NO_DEBUG -#define dprint(s) do { if (debug_enable) print(s); } while (0) -#define dprintln(s) do { if (debug_enable) println(s); } while (0) -#define dprintf(fmt, ...) do { if (debug_enable) xprintf(fmt, ##__VA_ARGS__); } while (0) -#define dmsg(s) dprintf("%s at %s: %S\n", __FILE__, __LINE__, PSTR(s)) +# define dprint(s) \ + do { \ + if (debug_enable) print(s); \ + } while (0) +# define dprintln(s) \ + do { \ + if (debug_enable) println(s); \ + } while (0) +# define dprintf(fmt, ...) \ + do { \ + if (debug_enable) xprintf(fmt, ##__VA_ARGS__); \ + } while (0) +# define dmsg(s) dprintf("%s at %s: %S\n", __FILE__, __LINE__, PSTR(s)) /* Deprecated. DO NOT USE these anymore, use dprintf instead. */ -#define debug(s) do { if (debug_enable) print(s); } while (0) -#define debugln(s) do { if (debug_enable) println(s); } while (0) -#define debug_msg(s) do { \ - if (debug_enable) { \ - print(__FILE__); print(" at "); print_dec(__LINE__); print(" in "); print(": "); print(s); \ - } \ -} while (0) -#define debug_dec(data) do { if (debug_enable) print_dec(data); } while (0) -#define debug_decs(data) do { if (debug_enable) print_decs(data); } while (0) -#define debug_hex4(data) do { if (debug_enable) print_hex4(data); } while (0) -#define debug_hex8(data) do { if (debug_enable) print_hex8(data); } while (0) -#define debug_hex16(data) do { if (debug_enable) print_hex16(data); } while (0) -#define debug_hex32(data) do { if (debug_enable) print_hex32(data); } while (0) -#define debug_bin8(data) do { if (debug_enable) print_bin8(data); } while (0) -#define debug_bin16(data) do { if (debug_enable) print_bin16(data); } while (0) -#define debug_bin32(data) do { if (debug_enable) print_bin32(data); } while (0) -#define debug_bin_reverse8(data) do { if (debug_enable) print_bin_reverse8(data); } while (0) -#define debug_bin_reverse16(data) do { if (debug_enable) print_bin_reverse16(data); } while (0) -#define debug_bin_reverse32(data) do { if (debug_enable) print_bin_reverse32(data); } while (0) -#define debug_hex(data) debug_hex8(data) -#define debug_bin(data) debug_bin8(data) -#define debug_bin_reverse(data) debug_bin8(data) +# define debug(s) \ + do { \ + if (debug_enable) print(s); \ + } while (0) +# define debugln(s) \ + do { \ + if (debug_enable) println(s); \ + } while (0) +# define debug_msg(s) \ + do { \ + if (debug_enable) { \ + print(__FILE__); \ + print(" at "); \ + print_dec(__LINE__); \ + print(" in "); \ + print(": "); \ + print(s); \ + } \ + } while (0) +# define debug_dec(data) \ + do { \ + if (debug_enable) print_dec(data); \ + } while (0) +# define debug_decs(data) \ + do { \ + if (debug_enable) print_decs(data); \ + } while (0) +# define debug_hex4(data) \ + do { \ + if (debug_enable) print_hex4(data); \ + } while (0) +# define debug_hex8(data) \ + do { \ + if (debug_enable) print_hex8(data); \ + } while (0) +# define debug_hex16(data) \ + do { \ + if (debug_enable) print_hex16(data); \ + } while (0) +# define debug_hex32(data) \ + do { \ + if (debug_enable) print_hex32(data); \ + } while (0) +# define debug_bin8(data) \ + do { \ + if (debug_enable) print_bin8(data); \ + } while (0) +# define debug_bin16(data) \ + do { \ + if (debug_enable) print_bin16(data); \ + } while (0) +# define debug_bin32(data) \ + do { \ + if (debug_enable) print_bin32(data); \ + } while (0) +# define debug_bin_reverse8(data) \ + do { \ + if (debug_enable) print_bin_reverse8(data); \ + } while (0) +# define debug_bin_reverse16(data) \ + do { \ + if (debug_enable) print_bin_reverse16(data); \ + } while (0) +# define debug_bin_reverse32(data) \ + do { \ + if (debug_enable) print_bin_reverse32(data); \ + } while (0) +# define debug_hex(data) debug_hex8(data) +# define debug_bin(data) debug_bin8(data) +# define debug_bin_reverse(data) debug_bin8(data) #else /* NO_DEBUG */ -#define dprint(s) -#define dprintln(s) -#define dprintf(fmt, ...) -#define dmsg(s) -#define debug(s) -#define debugln(s) -#define debug_msg(s) -#define debug_dec(data) -#define debug_decs(data) -#define debug_hex4(data) -#define debug_hex8(data) -#define debug_hex16(data) -#define debug_hex32(data) -#define debug_bin8(data) -#define debug_bin16(data) -#define debug_bin32(data) -#define debug_bin_reverse8(data) -#define debug_bin_reverse16(data) -#define debug_bin_reverse32(data) -#define debug_hex(data) -#define debug_bin(data) -#define debug_bin_reverse(data) +# define dprint(s) +# define dprintln(s) +# define dprintf(fmt, ...) +# define dmsg(s) +# define debug(s) +# define debugln(s) +# define debug_msg(s) +# define debug_dec(data) +# define debug_decs(data) +# define debug_hex4(data) +# define debug_hex8(data) +# define debug_hex16(data) +# define debug_hex32(data) +# define debug_bin8(data) +# define debug_bin16(data) +# define debug_bin32(data) +# define debug_bin_reverse8(data) +# define debug_bin_reverse16(data) +# define debug_bin_reverse32(data) +# define debug_hex(data) +# define debug_bin(data) +# define debug_bin_reverse(data) #endif /* NO_DEBUG */ diff --git a/tmk_core/common/eeconfig.c b/tmk_core/common/eeconfig.c index 28f5d3ad5..61aaec205 100644 --- a/tmk_core/common/eeconfig.c +++ b/tmk_core/common/eeconfig.c @@ -4,8 +4,8 @@ #include "eeconfig.h" #ifdef STM32_EEPROM_ENABLE -#include "hal.h" -#include "eeprom_stm32.h" +# include "hal.h" +# include "eeprom_stm32.h" #endif extern uint32_t default_layer_state; @@ -13,21 +13,18 @@ extern uint32_t default_layer_state; * * FIXME: needs doc */ -__attribute__ ((weak)) -void eeconfig_init_user(void) { - // Reset user EEPROM value to blank, rather than to a set value - eeconfig_update_user(0); +__attribute__((weak)) void eeconfig_init_user(void) { + // Reset user EEPROM value to blank, rather than to a set value + eeconfig_update_user(0); } -__attribute__ ((weak)) -void eeconfig_init_kb(void) { - // Reset Keyboard EEPROM value to blank, rather than to a set value - eeconfig_update_kb(0); +__attribute__((weak)) void eeconfig_init_kb(void) { + // Reset Keyboard EEPROM value to blank, rather than to a set value + eeconfig_update_kb(0); - eeconfig_init_user(); + eeconfig_init_user(); } - /* * FIXME: needs doc */ @@ -35,49 +32,42 @@ void eeconfig_init_quantum(void) { #ifdef STM32_EEPROM_ENABLE EEPROM_Erase(); #endif - eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); - eeprom_update_byte(EECONFIG_DEBUG, 0); - eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0); - default_layer_state = 0; - eeprom_update_byte(EECONFIG_KEYMAP_LOWER_BYTE, 0); - eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, 0); - eeprom_update_byte(EECONFIG_MOUSEKEY_ACCEL, 0); - eeprom_update_byte(EECONFIG_BACKLIGHT, 0); - eeprom_update_byte(EECONFIG_AUDIO, 0xFF); // On by default - eeprom_update_dword(EECONFIG_RGBLIGHT, 0); - eeprom_update_byte(EECONFIG_STENOMODE, 0); - eeprom_update_dword(EECONFIG_HAPTIC, 0); - eeprom_update_byte(EECONFIG_VELOCIKEY, 0); - eeprom_update_dword(EECONFIG_RGB_MATRIX, 0); - eeprom_update_byte(EECONFIG_RGB_MATRIX_SPEED, 0); - - eeconfig_init_kb(); + eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); + eeprom_update_byte(EECONFIG_DEBUG, 0); + eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0); + default_layer_state = 0; + eeprom_update_byte(EECONFIG_KEYMAP_LOWER_BYTE, 0); + eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, 0); + eeprom_update_byte(EECONFIG_MOUSEKEY_ACCEL, 0); + eeprom_update_byte(EECONFIG_BACKLIGHT, 0); + eeprom_update_byte(EECONFIG_AUDIO, 0xFF); // On by default + eeprom_update_dword(EECONFIG_RGBLIGHT, 0); + eeprom_update_byte(EECONFIG_STENOMODE, 0); + eeprom_update_dword(EECONFIG_HAPTIC, 0); + eeprom_update_byte(EECONFIG_VELOCIKEY, 0); + eeprom_update_dword(EECONFIG_RGB_MATRIX, 0); + eeprom_update_byte(EECONFIG_RGB_MATRIX_SPEED, 0); + + eeconfig_init_kb(); } /** \brief eeconfig initialization * * FIXME: needs doc */ -void eeconfig_init(void) { - - eeconfig_init_quantum(); -} +void eeconfig_init(void) { eeconfig_init_quantum(); } /** \brief eeconfig enable * * FIXME: needs doc */ -void eeconfig_enable(void) -{ - eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); -} +void eeconfig_enable(void) { eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); } /** \brief eeconfig disable * * FIXME: needs doc */ -void eeconfig_disable(void) -{ +void eeconfig_disable(void) { #ifdef STM32_EEPROM_ENABLE EEPROM_Erase(); #endif @@ -88,25 +78,19 @@ void eeconfig_disable(void) * * FIXME: needs doc */ -bool eeconfig_is_enabled(void) -{ - return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER); -} +bool eeconfig_is_enabled(void) { return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER); } /** \brief eeconfig is disabled * * FIXME: needs doc */ -bool eeconfig_is_disabled(void) -{ - return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF); -} +bool eeconfig_is_disabled(void) { return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF); } /** \brief eeconfig read debug * * FIXME: needs doc */ -uint8_t eeconfig_read_debug(void) { return eeprom_read_byte(EECONFIG_DEBUG); } +uint8_t eeconfig_read_debug(void) { return eeprom_read_byte(EECONFIG_DEBUG); } /** \brief eeconfig update debug * * FIXME: needs doc @@ -117,7 +101,7 @@ void eeconfig_update_debug(uint8_t val) { eeprom_update_byte(EECONFIG_DEBUG, val * * FIXME: needs doc */ -uint8_t eeconfig_read_default_layer(void) { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); } +uint8_t eeconfig_read_default_layer(void) { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); } /** \brief eeconfig update default layer * * FIXME: needs doc @@ -128,47 +112,43 @@ void eeconfig_update_default_layer(uint8_t val) { eeprom_update_byte(EECONFIG_DE * * FIXME: needs doc */ -uint16_t eeconfig_read_keymap(void) { - return ( eeprom_read_byte(EECONFIG_KEYMAP_LOWER_BYTE) | (eeprom_read_byte(EECONFIG_KEYMAP_UPPER_BYTE) << 8) ); -} +uint16_t eeconfig_read_keymap(void) { return (eeprom_read_byte(EECONFIG_KEYMAP_LOWER_BYTE) | (eeprom_read_byte(EECONFIG_KEYMAP_UPPER_BYTE) << 8)); } /** \brief eeconfig update keymap * * FIXME: needs doc */ void eeconfig_update_keymap(uint16_t val) { eeprom_update_byte(EECONFIG_KEYMAP_LOWER_BYTE, val & 0xFF); - eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, ( val >> 8 ) & 0xFF ); + eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, (val >> 8) & 0xFF); } /** \brief eeconfig read backlight * * FIXME: needs doc */ -uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } +uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); } /** \brief eeconfig update backlight * * FIXME: needs doc */ void eeconfig_update_backlight(uint8_t val) { eeprom_update_byte(EECONFIG_BACKLIGHT, val); } - /** \brief eeconfig read audio * * FIXME: needs doc */ -uint8_t eeconfig_read_audio(void) { return eeprom_read_byte(EECONFIG_AUDIO); } +uint8_t eeconfig_read_audio(void) { return eeprom_read_byte(EECONFIG_AUDIO); } /** \brief eeconfig update audio * * FIXME: needs doc */ void eeconfig_update_audio(uint8_t val) { eeprom_update_byte(EECONFIG_AUDIO, val); } - /** \brief eeconfig read kb * * FIXME: needs doc */ -uint32_t eeconfig_read_kb(void) { return eeprom_read_dword(EECONFIG_KEYBOARD); } +uint32_t eeconfig_read_kb(void) { return eeprom_read_dword(EECONFIG_KEYBOARD); } /** \brief eeconfig update kb * * FIXME: needs doc @@ -179,15 +159,14 @@ void eeconfig_update_kb(uint32_t val) { eeprom_update_dword(EECONFIG_KEYBOARD, v * * FIXME: needs doc */ -uint32_t eeconfig_read_user(void) { return eeprom_read_dword(EECONFIG_USER); } +uint32_t eeconfig_read_user(void) { return eeprom_read_dword(EECONFIG_USER); } /** \brief eeconfig update user * * FIXME: needs doc */ void eeconfig_update_user(uint32_t val) { eeprom_update_dword(EECONFIG_USER, val); } - -uint32_t eeconfig_read_haptic(void) { return eeprom_read_dword(EECONFIG_HAPTIC); } +uint32_t eeconfig_read_haptic(void) { return eeprom_read_dword(EECONFIG_HAPTIC); } /** \brief eeconfig update user * * FIXME: needs doc diff --git a/tmk_core/common/eeconfig.h b/tmk_core/common/eeconfig.h index a5fc801a3..aea4eff9f 100644 --- a/tmk_core/common/eeconfig.h +++ b/tmk_core/common/eeconfig.h @@ -21,49 +21,48 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include <stdbool.h> - #ifndef EECONFIG_MAGIC_NUMBER -# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEEC +# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEEC #endif -#define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF +#define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF /* EEPROM parameter address */ -#define EECONFIG_MAGIC (uint16_t *)0 -#define EECONFIG_DEBUG (uint8_t *)2 -#define EECONFIG_DEFAULT_LAYER (uint8_t *)3 -#define EECONFIG_KEYMAP (uint8_t *)4 -#define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 -#define EECONFIG_BACKLIGHT (uint8_t *)6 -#define EECONFIG_AUDIO (uint8_t *)7 -#define EECONFIG_RGBLIGHT (uint32_t *)8 -#define EECONFIG_UNICODEMODE (uint8_t *)12 -#define EECONFIG_STENOMODE (uint8_t *)13 +#define EECONFIG_MAGIC (uint16_t *)0 +#define EECONFIG_DEBUG (uint8_t *)2 +#define EECONFIG_DEFAULT_LAYER (uint8_t *)3 +#define EECONFIG_KEYMAP (uint8_t *)4 +#define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 +#define EECONFIG_BACKLIGHT (uint8_t *)6 +#define EECONFIG_AUDIO (uint8_t *)7 +#define EECONFIG_RGBLIGHT (uint32_t *)8 +#define EECONFIG_UNICODEMODE (uint8_t *)12 +#define EECONFIG_STENOMODE (uint8_t *)13 // EEHANDS for two handed boards -#define EECONFIG_HANDEDNESS (uint8_t *)14 -#define EECONFIG_KEYBOARD (uint32_t *)15 -#define EECONFIG_USER (uint32_t *)19 -#define EECONFIG_VELOCIKEY (uint8_t *)23 - -#define EECONFIG_HAPTIC (uint32_t *)24 -#define EECONFIG_RGB_MATRIX (uint32_t *)28 -#define EECONFIG_RGB_MATRIX_SPEED (uint8_t *)32 +#define EECONFIG_HANDEDNESS (uint8_t *)14 +#define EECONFIG_KEYBOARD (uint32_t *)15 +#define EECONFIG_USER (uint32_t *)19 +#define EECONFIG_VELOCIKEY (uint8_t *)23 + +#define EECONFIG_HAPTIC (uint32_t *)24 +#define EECONFIG_RGB_MATRIX (uint32_t *)28 +#define EECONFIG_RGB_MATRIX_SPEED (uint8_t *)32 // TODO: Combine these into a single word and single block of EEPROM -#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)33 +#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)33 /* debug bit */ -#define EECONFIG_DEBUG_ENABLE (1<<0) -#define EECONFIG_DEBUG_MATRIX (1<<1) -#define EECONFIG_DEBUG_KEYBOARD (1<<2) -#define EECONFIG_DEBUG_MOUSE (1<<3) +#define EECONFIG_DEBUG_ENABLE (1 << 0) +#define EECONFIG_DEBUG_MATRIX (1 << 1) +#define EECONFIG_DEBUG_KEYBOARD (1 << 2) +#define EECONFIG_DEBUG_MOUSE (1 << 3) /* keyconf bit */ -#define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1<<0) -#define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1<<1) -#define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1<<2) -#define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1<<3) -#define EECONFIG_KEYMAP_NO_GUI (1<<4) -#define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1<<5) -#define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1<<6) -#define EECONFIG_KEYMAP_NKRO (1<<7) +#define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0) +#define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1) +#define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2) +#define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3) +#define EECONFIG_KEYMAP_NO_GUI (1 << 4) +#define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5) +#define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6) +#define EECONFIG_KEYMAP_NKRO (1 << 7) #define EECONFIG_KEYMAP_LOWER_BYTE EECONFIG_KEYMAP @@ -80,32 +79,32 @@ void eeconfig_enable(void); void eeconfig_disable(void); uint8_t eeconfig_read_debug(void); -void eeconfig_update_debug(uint8_t val); +void eeconfig_update_debug(uint8_t val); uint8_t eeconfig_read_default_layer(void); -void eeconfig_update_default_layer(uint8_t val); +void eeconfig_update_default_layer(uint8_t val); uint16_t eeconfig_read_keymap(void); -void eeconfig_update_keymap(uint16_t val); +void eeconfig_update_keymap(uint16_t val); #ifdef BACKLIGHT_ENABLE uint8_t eeconfig_read_backlight(void); -void eeconfig_update_backlight(uint8_t val); +void eeconfig_update_backlight(uint8_t val); #endif #ifdef AUDIO_ENABLE uint8_t eeconfig_read_audio(void); -void eeconfig_update_audio(uint8_t val); +void eeconfig_update_audio(uint8_t val); #endif uint32_t eeconfig_read_kb(void); -void eeconfig_update_kb(uint32_t val); +void eeconfig_update_kb(uint32_t val); uint32_t eeconfig_read_user(void); -void eeconfig_update_user(uint32_t val); +void eeconfig_update_user(uint32_t val); #ifdef HAPTIC_ENABLE uint32_t eeconfig_read_haptic(void); -void eeconfig_update_haptic(uint32_t val); +void eeconfig_update_haptic(uint32_t val); #endif #endif diff --git a/tmk_core/common/eeprom.h b/tmk_core/common/eeprom.h index 5ae0f6eeb..fcd1667c0 100644 --- a/tmk_core/common/eeprom.h +++ b/tmk_core/common/eeprom.h @@ -2,22 +2,22 @@ #define TMK_CORE_COMMON_EEPROM_H_ #if defined(__AVR__) -#include <avr/eeprom.h> +# include <avr/eeprom.h> #else -#include <stdint.h> +# include <stdint.h> -uint8_t eeprom_read_byte (const uint8_t *__p); -uint16_t eeprom_read_word (const uint16_t *__p); -uint32_t eeprom_read_dword (const uint32_t *__p); -void eeprom_read_block (void *__dst, const void *__src, uint32_t __n); -void eeprom_write_byte (uint8_t *__p, uint8_t __value); -void eeprom_write_word (uint16_t *__p, uint16_t __value); -void eeprom_write_dword (uint32_t *__p, uint32_t __value); -void eeprom_write_block (const void *__src, void *__dst, uint32_t __n); -void eeprom_update_byte (uint8_t *__p, uint8_t __value); -void eeprom_update_word (uint16_t *__p, uint16_t __value); -void eeprom_update_dword (uint32_t *__p, uint32_t __value); -void eeprom_update_block (const void *__src, void *__dst, uint32_t __n); +uint8_t eeprom_read_byte(const uint8_t *__p); +uint16_t eeprom_read_word(const uint16_t *__p); +uint32_t eeprom_read_dword(const uint32_t *__p); +void eeprom_read_block(void *__dst, const void *__src, uint32_t __n); +void eeprom_write_byte(uint8_t *__p, uint8_t __value); +void eeprom_write_word(uint16_t *__p, uint16_t __value); +void eeprom_write_dword(uint32_t *__p, uint32_t __value); +void eeprom_write_block(const void *__src, void *__dst, uint32_t __n); +void eeprom_update_byte(uint8_t *__p, uint8_t __value); +void eeprom_update_word(uint16_t *__p, uint16_t __value); +void eeprom_update_dword(uint32_t *__p, uint32_t __value); +void eeprom_update_block(const void *__src, void *__dst, uint32_t __n); #endif #endif /* TMK_CORE_COMMON_EEPROM_H_ */ diff --git a/tmk_core/common/host.c b/tmk_core/common/host.c index f5d041699..ce39760a5 100644 --- a/tmk_core/common/host.c +++ b/tmk_core/common/host.c @@ -23,40 +23,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "debug.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" - extern keymap_config_t keymap_config; +# include "keycode_config.h" +extern keymap_config_t keymap_config; #endif static host_driver_t *driver; -static uint16_t last_system_report = 0; -static uint16_t last_consumer_report = 0; +static uint16_t last_system_report = 0; +static uint16_t last_consumer_report = 0; +void host_set_driver(host_driver_t *d) { driver = d; } -void host_set_driver(host_driver_t *d) -{ - driver = d; -} - -host_driver_t *host_get_driver(void) -{ - return driver; -} +host_driver_t *host_get_driver(void) { return driver; } -uint8_t host_keyboard_leds(void) -{ +uint8_t host_keyboard_leds(void) { if (!driver) return 0; return (*driver->keyboard_leds)(); } /* send report */ -void host_keyboard_send(report_keyboard_t *report) -{ +void host_keyboard_send(report_keyboard_t *report) { if (!driver) return; #if defined(NKRO_ENABLE) && defined(NKRO_SHARED_EP) if (keyboard_protocol && keymap_config.nkro) { /* The callers of this function assume that report->mods is where mods go in. * But report->nkro.mods can be at a different offset if core keyboard does not have a report ID. */ - report->nkro.mods = report->mods; + report->nkro.mods = report->mods; report->nkro.report_id = REPORT_ID_NKRO; } else #endif @@ -76,8 +67,7 @@ void host_keyboard_send(report_keyboard_t *report) } } -void host_mouse_send(report_mouse_t *report) -{ +void host_mouse_send(report_mouse_t *report) { if (!driver) return; #ifdef MOUSE_SHARED_EP report->report_id = REPORT_ID_MOUSE; @@ -85,8 +75,7 @@ void host_mouse_send(report_mouse_t *report) (*driver->send_mouse)(report); } -void host_system_send(uint16_t report) -{ +void host_system_send(uint16_t report) { if (report == last_system_report) return; last_system_report = report; @@ -94,8 +83,7 @@ void host_system_send(uint16_t report) (*driver->send_system)(report); } -void host_consumer_send(uint16_t report) -{ +void host_consumer_send(uint16_t report) { if (report == last_consumer_report) return; last_consumer_report = report; @@ -103,12 +91,6 @@ void host_consumer_send(uint16_t report) (*driver->send_consumer)(report); } -uint16_t host_last_system_report(void) -{ - return last_system_report; -} +uint16_t host_last_system_report(void) { return last_system_report; } -uint16_t host_last_consumer_report(void) -{ - return last_consumer_report; -} +uint16_t host_last_consumer_report(void) { return last_consumer_report; } diff --git a/tmk_core/common/host.h b/tmk_core/common/host.h index 3d172eed6..b2a7f9842 100644 --- a/tmk_core/common/host.h +++ b/tmk_core/common/host.h @@ -22,11 +22,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "report.h" #include "host_driver.h" -#define IS_LED_ON(leds, led_name) ( (leds) & (1 << (led_name))) -#define IS_LED_OFF(leds, led_name) (~(leds) & (1 << (led_name))) +#define IS_LED_ON(leds, led_name) ((leds) & (1 << (led_name))) +#define IS_LED_OFF(leds, led_name) (~(leds) & (1 << (led_name))) -#define IS_HOST_LED_ON(led_name) IS_LED_ON(host_keyboard_leds(), led_name) -#define IS_HOST_LED_OFF(led_name) IS_LED_OFF(host_keyboard_leds(), led_name) +#define IS_HOST_LED_ON(led_name) IS_LED_ON(host_keyboard_leds(), led_name) +#define IS_HOST_LED_OFF(led_name) IS_LED_OFF(host_keyboard_leds(), led_name) #ifdef __cplusplus extern "C" { @@ -36,15 +36,15 @@ extern uint8_t keyboard_idle; extern uint8_t keyboard_protocol; /* host driver */ -void host_set_driver(host_driver_t *driver); +void host_set_driver(host_driver_t *driver); host_driver_t *host_get_driver(void); /* host driver interface */ uint8_t host_keyboard_leds(void); -void host_keyboard_send(report_keyboard_t *report); -void host_mouse_send(report_mouse_t *report); -void host_system_send(uint16_t data); -void host_consumer_send(uint16_t data); +void host_keyboard_send(report_keyboard_t *report); +void host_mouse_send(report_mouse_t *report); +void host_system_send(uint16_t data); +void host_consumer_send(uint16_t data); uint16_t host_last_system_report(void); uint16_t host_last_consumer_report(void); diff --git a/tmk_core/common/host_driver.h b/tmk_core/common/host_driver.h index e40f0bfd6..3cfec40cc 100644 --- a/tmk_core/common/host_driver.h +++ b/tmk_core/common/host_driver.h @@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include "report.h" #ifdef MIDI_ENABLE - #include "midi.h" +# include "midi.h" #endif typedef struct { diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index 8f0257cf6..f4d2cd738 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -32,84 +32,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "backlight.h" #include "action_layer.h" #ifdef BOOTMAGIC_ENABLE -# include "bootmagic.h" +# include "bootmagic.h" #else -# include "magic.h" +# include "magic.h" #endif #ifdef MOUSEKEY_ENABLE -# include "mousekey.h" +# include "mousekey.h" #endif #ifdef PS2_MOUSE_ENABLE -# include "ps2_mouse.h" +# include "ps2_mouse.h" #endif #ifdef SERIAL_MOUSE_ENABLE -# include "serial_mouse.h" +# include "serial_mouse.h" #endif #ifdef ADB_MOUSE_ENABLE -# include "adb.h" +# include "adb.h" #endif #ifdef RGBLIGHT_ENABLE -# include "rgblight.h" +# include "rgblight.h" #endif #ifdef STENO_ENABLE -# include "process_steno.h" +# include "process_steno.h" #endif #ifdef FAUXCLICKY_ENABLE -# include "fauxclicky.h" +# include "fauxclicky.h" #endif #ifdef SERIAL_LINK_ENABLE -# include "serial_link/system/serial_link.h" +# include "serial_link/system/serial_link.h" #endif #ifdef VISUALIZER_ENABLE -# include "visualizer/visualizer.h" +# include "visualizer/visualizer.h" #endif #ifdef POINTING_DEVICE_ENABLE -# include "pointing_device.h" +# include "pointing_device.h" #endif #ifdef MIDI_ENABLE -# include "process_midi.h" +# include "process_midi.h" #endif #ifdef HD44780_ENABLE -# include "hd44780.h" +# include "hd44780.h" #endif #ifdef QWIIC_ENABLE -# include "qwiic.h" +# include "qwiic.h" #endif #ifdef OLED_DRIVER_ENABLE - #include "oled_driver.h" +# include "oled_driver.h" #endif #ifdef VELOCIKEY_ENABLE - #include "velocikey.h" +# include "velocikey.h" #endif #ifdef MATRIX_HAS_GHOST extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; -static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata){ +static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) { matrix_row_t out = 0; for (uint8_t col = 0; col < MATRIX_COLS; col++) { - //read each key in the row data and check if the keymap defines it as a real key - if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1<<col))){ - //this creates new row data, if a key is defined in the keymap, it will be set here - out |= 1<<col; + // read each key in the row data and check if the keymap defines it as a real key + if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1 << col))) { + // this creates new row data, if a key is defined in the keymap, it will be set here + out |= 1 << col; } } return out; } -static inline bool popcount_more_than_one(matrix_row_t rowdata) -{ - rowdata &= rowdata-1; //if there are less than two bits (keys) set, rowdata will become zero +static inline bool popcount_more_than_one(matrix_row_t rowdata) { + rowdata &= rowdata - 1; // if there are less than two bits (keys) set, rowdata will become zero return rowdata; } -static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) -{ +static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) { /* No ghost exists when less than 2 keys are down on the row. If there are "active" blanks in the matrix, the key can't be pressed by the user, there is no doubt as to which keys are really being pressed. The ghosts will be ignored, they are KC_NO. */ rowdata = get_real_keys(row, rowdata); - if ((popcount_more_than_one(rowdata)) == 0){ + if ((popcount_more_than_one(rowdata)) == 0) { return false; } /* Ghost occurs when the row shares a column line with other row, @@ -119,8 +117,8 @@ static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) at least two of another row's real keys, the row will be ignored. Keep in mind, we are checking one row at a time, not all of them at once. */ - for (uint8_t i=0; i < MATRIX_ROWS; i++) { - if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)){ + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)) { return true; } } @@ -131,9 +129,7 @@ static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) void disable_jtag(void) { // To use PF4-7 (PC2-5 on ATmega32A), disable JTAG by writing JTD bit twice within four cycles. -#if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || \ - defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || \ - defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) +#if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) MCUCR |= _BV(JTD); MCUCR |= _BV(JTD); #elif defined(__AVR_ATmega32A__) @@ -146,43 +142,33 @@ void disable_jtag(void) { * * FIXME: needs doc */ -__attribute__ ((weak)) -void matrix_setup(void) { -} +__attribute__((weak)) void matrix_setup(void) {} /** \brief keyboard_pre_init_user * * FIXME: needs doc */ -__attribute__ ((weak)) -void keyboard_pre_init_user(void) { } +__attribute__((weak)) void keyboard_pre_init_user(void) {} /** \brief keyboard_pre_init_kb * * FIXME: needs doc */ -__attribute__ ((weak)) -void keyboard_pre_init_kb(void) { - keyboard_pre_init_user(); -} +__attribute__((weak)) void keyboard_pre_init_kb(void) { keyboard_pre_init_user(); } /** \brief keyboard_post_init_user * * FIXME: needs doc */ -__attribute__ ((weak)) -void keyboard_post_init_user() {} +__attribute__((weak)) void keyboard_post_init_user() {} /** \brief keyboard_post_init_kb * * FIXME: needs doc */ -__attribute__ ((weak)) -void keyboard_post_init_kb(void) { - keyboard_post_init_user(); -} +__attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); } /** \brief keyboard_setup * @@ -200,10 +186,7 @@ void keyboard_setup(void) { * * FIXME: needs doc */ -__attribute__((weak)) -bool is_keyboard_master(void) { - return true; -} +__attribute__((weak)) bool is_keyboard_master(void) { return true; } /** \brief keyboard_init * @@ -265,12 +248,11 @@ void keyboard_init(void) { * * This is repeatedly called as fast as possible. */ -void keyboard_task(void) -{ +void keyboard_task(void) { static matrix_row_t matrix_prev[MATRIX_ROWS]; - static uint8_t led_status = 0; - matrix_row_t matrix_row = 0; - matrix_row_t matrix_change = 0; + static uint8_t led_status = 0; + matrix_row_t matrix_row = 0; + matrix_row_t matrix_change = 0; #ifdef QMK_KEYS_PER_SCAN uint8_t keys_processed = 0; #endif @@ -283,28 +265,28 @@ void keyboard_task(void) if (is_keyboard_master()) { for (uint8_t r = 0; r < MATRIX_ROWS; r++) { - matrix_row = matrix_get_row(r); + matrix_row = matrix_get_row(r); matrix_change = matrix_row ^ matrix_prev[r]; if (matrix_change) { #ifdef MATRIX_HAS_GHOST - if (has_ghost_in_row(r, matrix_row)) { continue; } + if (has_ghost_in_row(r, matrix_row)) { + continue; + } #endif if (debug_matrix) matrix_print(); for (uint8_t c = 0; c < MATRIX_COLS; c++) { - if (matrix_change & ((matrix_row_t)1<<c)) { + if (matrix_change & ((matrix_row_t)1 << c)) { action_exec((keyevent_t){ - .key = (keypos_t){ .row = r, .col = c }, - .pressed = (matrix_row & ((matrix_row_t)1<<c)), - .time = (timer_read() | 1) /* time should not be 0 */ + .key = (keypos_t){.row = r, .col = c}, .pressed = (matrix_row & ((matrix_row_t)1 << c)), .time = (timer_read() | 1) /* time should not be 0 */ }); // record a processed key - matrix_prev[r] ^= ((matrix_row_t)1<<c); + matrix_prev[r] ^= ((matrix_row_t)1 << c); #ifdef QMK_KEYS_PER_SCAN // only jump out if we have processed "enough" keys. if (++keys_processed >= QMK_KEYS_PER_SCAN) #endif - // process a key per task call - goto MATRIX_LOOP_END; + // process a key per task call + goto MATRIX_LOOP_END; } } } @@ -315,7 +297,7 @@ void keyboard_task(void) // we can get here with some keys processed now. if (!keys_processed) #endif - action_exec(TICK); + action_exec(TICK); MATRIX_LOOP_END: @@ -325,11 +307,10 @@ MATRIX_LOOP_END: #ifdef OLED_DRIVER_ENABLE oled_task(); -#ifndef OLED_DISABLE_TIMEOUT +# ifndef OLED_DISABLE_TIMEOUT // Wake up oled if user is using those fabulous keys! - if (ret) - oled_on(); -#endif + if (ret) oled_on(); +# endif #endif #ifdef MOUSEKEY_ENABLE @@ -350,7 +331,7 @@ MATRIX_LOOP_END: #endif #ifdef SERIAL_LINK_ENABLE - serial_link_update(); + serial_link_update(); #endif #ifdef VISUALIZER_ENABLE @@ -366,7 +347,9 @@ MATRIX_LOOP_END: #endif #ifdef VELOCIKEY_ENABLE - if (velocikey_enabled()) { velocikey_decelerate(); } + if (velocikey_enabled()) { + velocikey_decelerate(); + } #endif // update LED @@ -380,8 +363,11 @@ MATRIX_LOOP_END: * * FIXME: needs doc */ -void keyboard_set_leds(uint8_t leds) -{ - if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); } +void keyboard_set_leds(uint8_t leds) { + if (debug_keyboard) { + debug("keyboard_set_led: "); + debug_hex8(leds); + debug("\n"); + } led_set(leds); } diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h index 19f98c74e..98ceca49b 100644 --- a/tmk_core/common/keyboard.h +++ b/tmk_core/common/keyboard.h @@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> #include <stdint.h> - #ifdef __cplusplus extern "C" { #endif @@ -40,7 +39,7 @@ typedef struct { } keyevent_t; /* equivalent test of keypos_t */ -#define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col) +#define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col) /* Rules for No Event: * 1) (time == 0) to handle (keyevent_t){} as empty event @@ -51,11 +50,8 @@ static inline bool IS_PRESSED(keyevent_t event) { return (!IS_NOEVENT(event) && static inline bool IS_RELEASED(keyevent_t event) { return (!IS_NOEVENT(event) && !event.pressed); } /* Tick event */ -#define TICK (keyevent_t){ \ - .key = (keypos_t){ .row = 255, .col = 255 }, \ - .pressed = false, \ - .time = (timer_read() | 1) \ -} +#define TICK \ + (keyevent_t) { .key = (keypos_t){.row = 255, .col = 255}, .pressed = false, .time = (timer_read() | 1) } /* it runs once at early stage of startup before keyboard_init. */ void keyboard_setup(void); diff --git a/tmk_core/common/keycode.h b/tmk_core/common/keycode.h index d5904276e..fd5d60680 100644 --- a/tmk_core/common/keycode.h +++ b/tmk_core/common/keycode.h @@ -26,68 +26,68 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* FIXME: Add doxygen comments here */ -#define IS_ERROR(code) (KC_ROLL_OVER <= (code) && (code) <= KC_UNDEFINED) -#define IS_ANY(code) (KC_A <= (code) && (code) <= 0xFF) -#define IS_KEY(code) (KC_A <= (code) && (code) <= KC_EXSEL) -#define IS_MOD(code) (KC_LCTRL <= (code) && (code) <= KC_RGUI) - -#define IS_SPECIAL(code) ((0xA5 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) -#define IS_SYSTEM(code) (KC_PWR <= (code) && (code) <= KC_WAKE) -#define IS_CONSUMER(code) (KC_MUTE <= (code) && (code) <= KC_BRID) - -#define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN31) - -#define IS_MOUSEKEY(code) (KC_MS_UP <= (code) && (code) <= KC_MS_ACCEL2) -#define IS_MOUSEKEY_MOVE(code) (KC_MS_UP <= (code) && (code) <= KC_MS_RIGHT) -#define IS_MOUSEKEY_BUTTON(code) (KC_MS_BTN1 <= (code) && (code) <= KC_MS_BTN5) -#define IS_MOUSEKEY_WHEEL(code) (KC_MS_WH_UP <= (code) && (code) <= KC_MS_WH_RIGHT) -#define IS_MOUSEKEY_ACCEL(code) (KC_MS_ACCEL0 <= (code) && (code) <= KC_MS_ACCEL2) - -#define MOD_BIT(code) (1 << MOD_INDEX(code)) -#define MOD_INDEX(code) ((code) & 0x07) - -#define MOD_MASK_CTRL (MOD_BIT(KC_LCTRL) | MOD_BIT(KC_RCTRL)) -#define MOD_MASK_SHIFT (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) -#define MOD_MASK_ALT (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT)) -#define MOD_MASK_GUI (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI)) -#define MOD_MASK_CS (MOD_MASK_CTRL | MOD_MASK_SHIFT) -#define MOD_MASK_CA (MOD_MASK_CTRL | MOD_MASK_ALT) -#define MOD_MASK_CG (MOD_MASK_CTRL | MOD_MASK_GUI) -#define MOD_MASK_SA (MOD_MASK_SHIFT | MOD_MASK_ALT) -#define MOD_MASK_SG (MOD_MASK_SHIFT | MOD_MASK_GUI) -#define MOD_MASK_AG (MOD_MASK_ALT | MOD_MASK_GUI) -#define MOD_MASK_CSA (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT) -#define MOD_MASK_CSG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_GUI) -#define MOD_MASK_CAG (MOD_MASK_CTRL | MOD_MASK_ALT | MOD_MASK_GUI) -#define MOD_MASK_SAG (MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI) -#define MOD_MASK_CSAG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI) - -#define FN_BIT(code) (1 << FN_INDEX(code)) -#define FN_INDEX(code) ((code) - KC_FN0) -#define FN_MIN KC_FN0 -#define FN_MAX KC_FN31 +#define IS_ERROR(code) (KC_ROLL_OVER <= (code) && (code) <= KC_UNDEFINED) +#define IS_ANY(code) (KC_A <= (code) && (code) <= 0xFF) +#define IS_KEY(code) (KC_A <= (code) && (code) <= KC_EXSEL) +#define IS_MOD(code) (KC_LCTRL <= (code) && (code) <= KC_RGUI) + +#define IS_SPECIAL(code) ((0xA5 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) +#define IS_SYSTEM(code) (KC_PWR <= (code) && (code) <= KC_WAKE) +#define IS_CONSUMER(code) (KC_MUTE <= (code) && (code) <= KC_BRID) + +#define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN31) + +#define IS_MOUSEKEY(code) (KC_MS_UP <= (code) && (code) <= KC_MS_ACCEL2) +#define IS_MOUSEKEY_MOVE(code) (KC_MS_UP <= (code) && (code) <= KC_MS_RIGHT) +#define IS_MOUSEKEY_BUTTON(code) (KC_MS_BTN1 <= (code) && (code) <= KC_MS_BTN5) +#define IS_MOUSEKEY_WHEEL(code) (KC_MS_WH_UP <= (code) && (code) <= KC_MS_WH_RIGHT) +#define IS_MOUSEKEY_ACCEL(code) (KC_MS_ACCEL0 <= (code) && (code) <= KC_MS_ACCEL2) + +#define MOD_BIT(code) (1 << MOD_INDEX(code)) +#define MOD_INDEX(code) ((code)&0x07) + +#define MOD_MASK_CTRL (MOD_BIT(KC_LCTRL) | MOD_BIT(KC_RCTRL)) +#define MOD_MASK_SHIFT (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) +#define MOD_MASK_ALT (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT)) +#define MOD_MASK_GUI (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI)) +#define MOD_MASK_CS (MOD_MASK_CTRL | MOD_MASK_SHIFT) +#define MOD_MASK_CA (MOD_MASK_CTRL | MOD_MASK_ALT) +#define MOD_MASK_CG (MOD_MASK_CTRL | MOD_MASK_GUI) +#define MOD_MASK_SA (MOD_MASK_SHIFT | MOD_MASK_ALT) +#define MOD_MASK_SG (MOD_MASK_SHIFT | MOD_MASK_GUI) +#define MOD_MASK_AG (MOD_MASK_ALT | MOD_MASK_GUI) +#define MOD_MASK_CSA (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT) +#define MOD_MASK_CSG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_GUI) +#define MOD_MASK_CAG (MOD_MASK_CTRL | MOD_MASK_ALT | MOD_MASK_GUI) +#define MOD_MASK_SAG (MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI) +#define MOD_MASK_CSAG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI) + +#define FN_BIT(code) (1 << FN_INDEX(code)) +#define FN_INDEX(code) ((code)-KC_FN0) +#define FN_MIN KC_FN0 +#define FN_MAX KC_FN31 /* * Short names for ease of definition of keymap */ /* Transparent */ #define KC_TRANSPARENT 0x01 -#define KC_TRNS KC_TRANSPARENT +#define KC_TRNS KC_TRANSPARENT /* Punctuation */ -#define KC_ENT KC_ENTER -#define KC_ESC KC_ESCAPE +#define KC_ENT KC_ENTER +#define KC_ESC KC_ESCAPE #define KC_BSPC KC_BSPACE -#define KC_SPC KC_SPACE +#define KC_SPC KC_SPACE #define KC_MINS KC_MINUS -#define KC_EQL KC_EQUAL +#define KC_EQL KC_EQUAL #define KC_LBRC KC_LBRACKET #define KC_RBRC KC_RBRACKET #define KC_BSLS KC_BSLASH #define KC_NUHS KC_NONUS_HASH #define KC_SCLN KC_SCOLON #define KC_QUOT KC_QUOTE -#define KC_GRV KC_GRAVE +#define KC_GRV KC_GRAVE #define KC_COMM KC_COMMA #define KC_SLSH KC_SLASH #define KC_NUBS KC_NONUS_BSLASH @@ -104,18 +104,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* Commands */ #define KC_PSCR KC_PSCREEN #define KC_PAUS KC_PAUSE -#define KC_BRK KC_PAUSE -#define KC_INS KC_INSERT -#define KC_DEL KC_DELETE +#define KC_BRK KC_PAUSE +#define KC_INS KC_INSERT +#define KC_DEL KC_DELETE #define KC_PGDN KC_PGDOWN #define KC_RGHT KC_RIGHT -#define KC_APP KC_APPLICATION +#define KC_APP KC_APPLICATION #define KC_EXEC KC_EXECUTE #define KC_SLCT KC_SELECT #define KC_AGIN KC_AGAIN #define KC_PSTE KC_PASTE #define KC_ERAS KC_ALT_ERASE -#define KC_CLR KC_CLEAR +#define KC_CLR KC_CLEAR /* Keypad */ #define KC_PSLS KC_KP_SLASH @@ -123,23 +123,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define KC_PMNS KC_KP_MINUS #define KC_PPLS KC_KP_PLUS #define KC_PENT KC_KP_ENTER -#define KC_P1 KC_KP_1 -#define KC_P2 KC_KP_2 -#define KC_P3 KC_KP_3 -#define KC_P4 KC_KP_4 -#define KC_P5 KC_KP_5 -#define KC_P6 KC_KP_6 -#define KC_P7 KC_KP_7 -#define KC_P8 KC_KP_8 -#define KC_P9 KC_KP_9 -#define KC_P0 KC_KP_0 +#define KC_P1 KC_KP_1 +#define KC_P2 KC_KP_2 +#define KC_P3 KC_KP_3 +#define KC_P4 KC_KP_4 +#define KC_P5 KC_KP_5 +#define KC_P6 KC_KP_6 +#define KC_P7 KC_KP_7 +#define KC_P8 KC_KP_8 +#define KC_P9 KC_KP_9 +#define KC_P0 KC_KP_0 #define KC_PDOT KC_KP_DOT #define KC_PEQL KC_KP_EQUAL #define KC_PCMM KC_KP_COMMA /* Japanese specific */ #define KC_ZKHK KC_GRAVE -#define KC_RO KC_INT1 +#define KC_RO KC_INT1 #define KC_KANA KC_INT2 #define KC_JYEN KC_INT3 #define KC_HENK KC_INT4 @@ -161,7 +161,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define KC_RWIN KC_RGUI /* Generic Desktop Page (0x01) */ -#define KC_PWR KC_SYSTEM_POWER +#define KC_PWR KC_SYSTEM_POWER #define KC_SLEP KC_SYSTEM_SLEEP #define KC_WAKE KC_SYSTEM_WAKE @@ -214,171 +214,171 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* Keyboard/Keypad Page (0x07) */ enum hid_keyboard_keypad_usage { - KC_NO = 0x00, - KC_ROLL_OVER, - KC_POST_FAIL, - KC_UNDEFINED, - KC_A, - KC_B, - KC_C, - KC_D, - KC_E, - KC_F, - KC_G, - KC_H, - KC_I, - KC_J, - KC_K, - KC_L, - KC_M, //0x10 - KC_N, - KC_O, - KC_P, - KC_Q, - KC_R, - KC_S, - KC_T, - KC_U, - KC_V, - KC_W, - KC_X, - KC_Y, - KC_Z, - KC_1, - KC_2, - KC_3, //0x20 - KC_4, - KC_5, - KC_6, - KC_7, - KC_8, - KC_9, - KC_0, - KC_ENTER, - KC_ESCAPE, - KC_BSPACE, - KC_TAB, - KC_SPACE, - KC_MINUS, - KC_EQUAL, - KC_LBRACKET, - KC_RBRACKET, //0x30 - KC_BSLASH, - KC_NONUS_HASH, - KC_SCOLON, - KC_QUOTE, - KC_GRAVE, - KC_COMMA, - KC_DOT, - KC_SLASH, - KC_CAPSLOCK, - KC_F1, - KC_F2, - KC_F3, - KC_F4, - KC_F5, - KC_F6, - KC_F7, //0x40 - KC_F8, - KC_F9, - KC_F10, - KC_F11, - KC_F12, - KC_PSCREEN, - KC_SCROLLLOCK, - KC_PAUSE, - KC_INSERT, - KC_HOME, - KC_PGUP, - KC_DELETE, - KC_END, - KC_PGDOWN, - KC_RIGHT, - KC_LEFT, //0x50 - KC_DOWN, - KC_UP, - KC_NUMLOCK, - KC_KP_SLASH, - KC_KP_ASTERISK, - KC_KP_MINUS, - KC_KP_PLUS, - KC_KP_ENTER, - KC_KP_1, - KC_KP_2, - KC_KP_3, - KC_KP_4, - KC_KP_5, - KC_KP_6, - KC_KP_7, - KC_KP_8, //0x60 - KC_KP_9, - KC_KP_0, - KC_KP_DOT, - KC_NONUS_BSLASH, - KC_APPLICATION, - KC_POWER, - KC_KP_EQUAL, - KC_F13, - KC_F14, - KC_F15, - KC_F16, - KC_F17, - KC_F18, - KC_F19, - KC_F20, - KC_F21, //0x70 - KC_F22, - KC_F23, - KC_F24, - KC_EXECUTE, - KC_HELP, - KC_MENU, - KC_SELECT, - KC_STOP, - KC_AGAIN, - KC_UNDO, - KC_CUT, - KC_COPY, - KC_PASTE, - KC_FIND, - KC__MUTE, - KC__VOLUP, //0x80 - KC__VOLDOWN, - KC_LOCKING_CAPS, - KC_LOCKING_NUM, - KC_LOCKING_SCROLL, - KC_KP_COMMA, - KC_KP_EQUAL_AS400, - KC_INT1, - KC_INT2, - KC_INT3, - KC_INT4, - KC_INT5, - KC_INT6, - KC_INT7, - KC_INT8, - KC_INT9, - KC_LANG1, //0x90 - KC_LANG2, - KC_LANG3, - KC_LANG4, - KC_LANG5, - KC_LANG6, - KC_LANG7, - KC_LANG8, - KC_LANG9, - KC_ALT_ERASE, - KC_SYSREQ, - KC_CANCEL, - KC_CLEAR, - KC_PRIOR, - KC_RETURN, - KC_SEPARATOR, - KC_OUT, //0xA0 - KC_OPER, - KC_CLEAR_AGAIN, - KC_CRSEL, - KC_EXSEL, + KC_NO = 0x00, + KC_ROLL_OVER, + KC_POST_FAIL, + KC_UNDEFINED, + KC_A, + KC_B, + KC_C, + KC_D, + KC_E, + KC_F, + KC_G, + KC_H, + KC_I, + KC_J, + KC_K, + KC_L, + KC_M, // 0x10 + KC_N, + KC_O, + KC_P, + KC_Q, + KC_R, + KC_S, + KC_T, + KC_U, + KC_V, + KC_W, + KC_X, + KC_Y, + KC_Z, + KC_1, + KC_2, + KC_3, // 0x20 + KC_4, + KC_5, + KC_6, + KC_7, + KC_8, + KC_9, + KC_0, + KC_ENTER, + KC_ESCAPE, + KC_BSPACE, + KC_TAB, + KC_SPACE, + KC_MINUS, + KC_EQUAL, + KC_LBRACKET, + KC_RBRACKET, // 0x30 + KC_BSLASH, + KC_NONUS_HASH, + KC_SCOLON, + KC_QUOTE, + KC_GRAVE, + KC_COMMA, + KC_DOT, + KC_SLASH, + KC_CAPSLOCK, + KC_F1, + KC_F2, + KC_F3, + KC_F4, + KC_F5, + KC_F6, + KC_F7, // 0x40 + KC_F8, + KC_F9, + KC_F10, + KC_F11, + KC_F12, + KC_PSCREEN, + KC_SCROLLLOCK, + KC_PAUSE, + KC_INSERT, + KC_HOME, + KC_PGUP, + KC_DELETE, + KC_END, + KC_PGDOWN, + KC_RIGHT, + KC_LEFT, // 0x50 + KC_DOWN, + KC_UP, + KC_NUMLOCK, + KC_KP_SLASH, + KC_KP_ASTERISK, + KC_KP_MINUS, + KC_KP_PLUS, + KC_KP_ENTER, + KC_KP_1, + KC_KP_2, + KC_KP_3, + KC_KP_4, + KC_KP_5, + KC_KP_6, + KC_KP_7, + KC_KP_8, // 0x60 + KC_KP_9, + KC_KP_0, + KC_KP_DOT, + KC_NONUS_BSLASH, + KC_APPLICATION, + KC_POWER, + KC_KP_EQUAL, + KC_F13, + KC_F14, + KC_F15, + KC_F16, + KC_F17, + KC_F18, + KC_F19, + KC_F20, + KC_F21, // 0x70 + KC_F22, + KC_F23, + KC_F24, + KC_EXECUTE, + KC_HELP, + KC_MENU, + KC_SELECT, + KC_STOP, + KC_AGAIN, + KC_UNDO, + KC_CUT, + KC_COPY, + KC_PASTE, + KC_FIND, + KC__MUTE, + KC__VOLUP, // 0x80 + KC__VOLDOWN, + KC_LOCKING_CAPS, + KC_LOCKING_NUM, + KC_LOCKING_SCROLL, + KC_KP_COMMA, + KC_KP_EQUAL_AS400, + KC_INT1, + KC_INT2, + KC_INT3, + KC_INT4, + KC_INT5, + KC_INT6, + KC_INT7, + KC_INT8, + KC_INT9, + KC_LANG1, // 0x90 + KC_LANG2, + KC_LANG3, + KC_LANG4, + KC_LANG5, + KC_LANG6, + KC_LANG7, + KC_LANG8, + KC_LANG9, + KC_ALT_ERASE, + KC_SYSREQ, + KC_CANCEL, + KC_CLEAR, + KC_PRIOR, + KC_RETURN, + KC_SEPARATOR, + KC_OUT, // 0xA0 + KC_OPER, + KC_CLEAR_AGAIN, + KC_CRSEL, + KC_EXSEL, #if 0 // *************************************************************** @@ -435,110 +435,110 @@ enum hid_keyboard_keypad_usage { KC_KP_HEXADECIMAL, #endif - /* Modifiers */ - KC_LCTRL = 0xE0, - KC_LSHIFT, - KC_LALT, - KC_LGUI, - KC_RCTRL, - KC_RSHIFT, - KC_RALT, - KC_RGUI - - // ********************************************** - // * 0xF0-0xFF are unallocated in the HID spec. * - // * QMK uses these for Mouse Keys - see below. * - // ********************************************** + /* Modifiers */ + KC_LCTRL = 0xE0, + KC_LSHIFT, + KC_LALT, + KC_LGUI, + KC_RCTRL, + KC_RSHIFT, + KC_RALT, + KC_RGUI + + // ********************************************** + // * 0xF0-0xFF are unallocated in the HID spec. * + // * QMK uses these for Mouse Keys - see below. * + // ********************************************** }; /* Media and Function keys */ enum internal_special_keycodes { - /* Generic Desktop Page (0x01) */ - KC_SYSTEM_POWER = 0xA5, - KC_SYSTEM_SLEEP, - KC_SYSTEM_WAKE, - - /* Consumer Page (0x0C) */ - KC_AUDIO_MUTE, - KC_AUDIO_VOL_UP, - KC_AUDIO_VOL_DOWN, - KC_MEDIA_NEXT_TRACK, - KC_MEDIA_PREV_TRACK, - KC_MEDIA_STOP, - KC_MEDIA_PLAY_PAUSE, - KC_MEDIA_SELECT, - KC_MEDIA_EJECT, //0xB0 - KC_MAIL, - KC_CALCULATOR, - KC_MY_COMPUTER, - KC_WWW_SEARCH, - KC_WWW_HOME, - KC_WWW_BACK, - KC_WWW_FORWARD, - KC_WWW_STOP, - KC_WWW_REFRESH, - KC_WWW_FAVORITES, - KC_MEDIA_FAST_FORWARD, - KC_MEDIA_REWIND, - KC_BRIGHTNESS_UP, - KC_BRIGHTNESS_DOWN, - - /* Fn keys */ - KC_FN0 = 0xC0, - KC_FN1, - KC_FN2, - KC_FN3, - KC_FN4, - KC_FN5, - KC_FN6, - KC_FN7, - KC_FN8, - KC_FN9, - KC_FN10, - KC_FN11, - KC_FN12, - KC_FN13, - KC_FN14, - KC_FN15, - KC_FN16, //0xD0 - KC_FN17, - KC_FN18, - KC_FN19, - KC_FN20, - KC_FN21, - KC_FN22, - KC_FN23, - KC_FN24, - KC_FN25, - KC_FN26, - KC_FN27, - KC_FN28, - KC_FN29, - KC_FN30, - KC_FN31 + /* Generic Desktop Page (0x01) */ + KC_SYSTEM_POWER = 0xA5, + KC_SYSTEM_SLEEP, + KC_SYSTEM_WAKE, + + /* Consumer Page (0x0C) */ + KC_AUDIO_MUTE, + KC_AUDIO_VOL_UP, + KC_AUDIO_VOL_DOWN, + KC_MEDIA_NEXT_TRACK, + KC_MEDIA_PREV_TRACK, + KC_MEDIA_STOP, + KC_MEDIA_PLAY_PAUSE, + KC_MEDIA_SELECT, + KC_MEDIA_EJECT, // 0xB0 + KC_MAIL, + KC_CALCULATOR, + KC_MY_COMPUTER, + KC_WWW_SEARCH, + KC_WWW_HOME, + KC_WWW_BACK, + KC_WWW_FORWARD, + KC_WWW_STOP, + KC_WWW_REFRESH, + KC_WWW_FAVORITES, + KC_MEDIA_FAST_FORWARD, + KC_MEDIA_REWIND, + KC_BRIGHTNESS_UP, + KC_BRIGHTNESS_DOWN, + + /* Fn keys */ + KC_FN0 = 0xC0, + KC_FN1, + KC_FN2, + KC_FN3, + KC_FN4, + KC_FN5, + KC_FN6, + KC_FN7, + KC_FN8, + KC_FN9, + KC_FN10, + KC_FN11, + KC_FN12, + KC_FN13, + KC_FN14, + KC_FN15, + KC_FN16, // 0xD0 + KC_FN17, + KC_FN18, + KC_FN19, + KC_FN20, + KC_FN21, + KC_FN22, + KC_FN23, + KC_FN24, + KC_FN25, + KC_FN26, + KC_FN27, + KC_FN28, + KC_FN29, + KC_FN30, + KC_FN31 }; enum mouse_keys { - /* Mouse Buttons */ - KC_MS_UP = 0xF0, - KC_MS_DOWN, - KC_MS_LEFT, - KC_MS_RIGHT, - KC_MS_BTN1, - KC_MS_BTN2, - KC_MS_BTN3, - KC_MS_BTN4, - KC_MS_BTN5, - - /* Mouse Wheel */ - KC_MS_WH_UP, - KC_MS_WH_DOWN, - KC_MS_WH_LEFT, - KC_MS_WH_RIGHT, - - /* Acceleration */ - KC_MS_ACCEL0, - KC_MS_ACCEL1, - KC_MS_ACCEL2 + /* Mouse Buttons */ + KC_MS_UP = 0xF0, + KC_MS_DOWN, + KC_MS_LEFT, + KC_MS_RIGHT, + KC_MS_BTN1, + KC_MS_BTN2, + KC_MS_BTN3, + KC_MS_BTN4, + KC_MS_BTN5, + + /* Mouse Wheel */ + KC_MS_WH_UP, + KC_MS_WH_DOWN, + KC_MS_WH_LEFT, + KC_MS_WH_RIGHT, + + /* Acceleration */ + KC_MS_ACCEL0, + KC_MS_ACCEL1, + KC_MS_ACCEL2 }; #endif diff --git a/tmk_core/common/led.h b/tmk_core/common/led.h index c16305129..2c28fe540 100644 --- a/tmk_core/common/led.h +++ b/tmk_core/common/led.h @@ -22,12 +22,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* FIXME: Add doxygen comments here. */ /* keyboard LEDs */ -#define USB_LED_NUM_LOCK 0 -#define USB_LED_CAPS_LOCK 1 -#define USB_LED_SCROLL_LOCK 2 -#define USB_LED_COMPOSE 3 -#define USB_LED_KANA 4 - +#define USB_LED_NUM_LOCK 0 +#define USB_LED_CAPS_LOCK 1 +#define USB_LED_SCROLL_LOCK 2 +#define USB_LED_COMPOSE 3 +#define USB_LED_KANA 4 #ifdef __cplusplus extern "C" { diff --git a/tmk_core/common/magic.c b/tmk_core/common/magic.c index 2b1a6a6ad..d8ab52573 100644 --- a/tmk_core/common/magic.c +++ b/tmk_core/common/magic.c @@ -1,7 +1,7 @@ #include <stdint.h> #include <stdbool.h> #if defined(__AVR__) -#include <util/delay.h> +# include <util/delay.h> #endif #include "matrix.h" #include "bootloader.h" @@ -18,8 +18,7 @@ keymap_config_t keymap_config; * * FIXME: Needs doc */ -void magic(void) -{ +void magic(void) { /* check signature */ if (!eeconfig_is_enabled()) { eeconfig_init(); @@ -32,7 +31,6 @@ void magic(void) keymap_config.raw = eeconfig_read_keymap(); uint8_t default_layer = 0; - default_layer = eeconfig_read_default_layer(); + default_layer = eeconfig_read_default_layer(); default_layer_set((layer_state_t)default_layer); - } diff --git a/tmk_core/common/matrix.h b/tmk_core/common/matrix.h index e5665bf40..7624d5137 100644 --- a/tmk_core/common/matrix.h +++ b/tmk_core/common/matrix.h @@ -20,29 +20,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include <stdbool.h> - #if (MATRIX_COLS <= 8) -typedef uint8_t matrix_row_t; +typedef uint8_t matrix_row_t; #elif (MATRIX_COLS <= 16) -typedef uint16_t matrix_row_t; +typedef uint16_t matrix_row_t; #elif (MATRIX_COLS <= 32) -typedef uint32_t matrix_row_t; +typedef uint32_t matrix_row_t; #else -#error "MATRIX_COLS: invalid value" +# error "MATRIX_COLS: invalid value" #endif #if (MATRIX_ROWS <= 8) -typedef uint8_t matrix_col_t; +typedef uint8_t matrix_col_t; #elif (MATRIX_ROWS <= 16) -typedef uint16_t matrix_col_t; +typedef uint16_t matrix_col_t; #elif (MATRIX_ROWS <= 32) -typedef uint32_t matrix_col_t; +typedef uint32_t matrix_col_t; #else -#error "MATRIX_ROWS: invalid value" +# error "MATRIX_ROWS: invalid value" #endif -#define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1<<col)) - +#define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1 << col)) #ifdef __cplusplus extern "C" { @@ -59,7 +57,7 @@ void matrix_init(void); /* scan all key states on matrix */ uint8_t matrix_scan(void); /* whether modified from previous scan. used after matrix_scan. */ -bool matrix_is_modified(void) __attribute__ ((deprecated)); +bool matrix_is_modified(void) __attribute__((deprecated)); /* whether a switch is on */ bool matrix_is_on(uint8_t row, uint8_t col); /* matrix state on row */ @@ -67,7 +65,6 @@ matrix_row_t matrix_get_row(uint8_t row); /* print matrix for debug */ void matrix_print(void); - /* power control */ void matrix_power_up(void); void matrix_power_down(void); @@ -83,8 +80,8 @@ void matrix_init_user(void); void matrix_scan_user(void); #ifdef I2C_SPLIT - void slave_matrix_init(void); - uint8_t slave_matrix_scan(void); +void slave_matrix_init(void); +uint8_t slave_matrix_scan(void); #endif #ifdef __cplusplus diff --git a/tmk_core/common/mbed/bootloader.c b/tmk_core/common/mbed/bootloader.c index b51e83943..88945eb05 100644 --- a/tmk_core/common/mbed/bootloader.c +++ b/tmk_core/common/mbed/bootloader.c @@ -1,4 +1,3 @@ #include "bootloader.h" - void bootloader_jump(void) {} diff --git a/tmk_core/common/mbed/suspend.c b/tmk_core/common/mbed/suspend.c index 32651574f..3d0554f87 100644 --- a/tmk_core/common/mbed/suspend.c +++ b/tmk_core/common/mbed/suspend.c @@ -1,6 +1,5 @@ #include <stdbool.h> - void suspend_power_down(void) {} bool suspend_wakeup_condition(void) { return true; } void suspend_wakeup_init(void) {} diff --git a/tmk_core/common/mbed/timer.c b/tmk_core/common/mbed/timer.c index c357ceb78..7e4070af2 100644 --- a/tmk_core/common/mbed/timer.c +++ b/tmk_core/common/mbed/timer.c @@ -5,37 +5,19 @@ volatile uint32_t timer_count = 0; /* Timer interrupt handler */ -void SysTick_Handler(void) { - timer_count++; -} +void SysTick_Handler(void) { timer_count++; } -void timer_init(void) -{ +void timer_init(void) { timer_count = 0; SysTick_Config(SystemCoreClock / 1000); /* 1ms tick */ } -void timer_clear(void) -{ - timer_count = 0; -} +void timer_clear(void) { timer_count = 0; } -uint16_t timer_read(void) -{ - return (uint16_t)(timer_count & 0xFFFF); -} +uint16_t timer_read(void) { return (uint16_t)(timer_count & 0xFFFF); } -uint32_t timer_read32(void) -{ - return timer_count; -} +uint32_t timer_read32(void) { return timer_count; } -uint16_t timer_elapsed(uint16_t last) -{ - return TIMER_DIFF_16(timer_read(), last); -} +uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); } -uint32_t timer_elapsed32(uint32_t last) -{ - return TIMER_DIFF_32(timer_read32(), last); -} +uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); } diff --git a/tmk_core/common/mbed/xprintf.cpp b/tmk_core/common/mbed/xprintf.cpp index b1aac2c99..184b7fa7a 100644 --- a/tmk_core/common/mbed/xprintf.cpp +++ b/tmk_core/common/mbed/xprintf.cpp @@ -3,10 +3,9 @@ #include "mbed.h" #include "mbed/xprintf.h" +#define STRING_STACK_LIMIT 120 -#define STRING_STACK_LIMIT 120 - -//TODO +// TODO int __xprintf(const char* format, ...) { return 0; } #if 0 diff --git a/tmk_core/common/mbed/xprintf.h b/tmk_core/common/mbed/xprintf.h index 1e7a48c06..e27822d3a 100644 --- a/tmk_core/common/mbed/xprintf.h +++ b/tmk_core/common/mbed/xprintf.h @@ -13,5 +13,4 @@ int __xprintf(const char *format, ...); } #endif - #endif diff --git a/tmk_core/common/mousekey.c b/tmk_core/common/mousekey.c index bb08576b9..2df7728a4 100644 --- a/tmk_core/common/mousekey.c +++ b/tmk_core/common/mousekey.c @@ -24,25 +24,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "mousekey.h" inline int8_t times_inv_sqrt2(int8_t x) { - // 181/256 is pretty close to 1/sqrt(2) - // 0.70703125 0.707106781 - // 1 too small for x=99 and x=198 - // This ends up being a mult and discard lower 8 bits - return (x * 181) >> 8; + // 181/256 is pretty close to 1/sqrt(2) + // 0.70703125 0.707106781 + // 1 too small for x=99 and x=198 + // This ends up being a mult and discard lower 8 bits + return (x * 181) >> 8; } static report_mouse_t mouse_report = {0}; -static void mousekey_debug(void); -static uint8_t mousekey_accel = 0; -static uint8_t mousekey_repeat = 0; -static uint16_t last_timer = 0; - - +static void mousekey_debug(void); +static uint8_t mousekey_accel = 0; +static uint8_t mousekey_repeat = 0; +static uint16_t last_timer = 0; #ifndef MK_3_SPEED - - /* * Mouse keys acceleration algorithm * http://en.wikipedia.org/wiki/Mouse_keys @@ -50,7 +46,7 @@ static uint16_t last_timer = 0; * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000) */ /* milliseconds between the initial key press and first repeated motion event (0-2550) */ -uint8_t mk_delay = MOUSEKEY_DELAY/10; +uint8_t mk_delay = MOUSEKEY_DELAY / 10; /* milliseconds between repeated motion events (0-255) */ uint8_t mk_interval = MOUSEKEY_INTERVAL; /* steady speed (in action_delta units) applied each event (0-255) */ @@ -58,268 +54,320 @@ uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED; /* number of events (count) accelerating to steady speed (0-255) */ uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX; /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */ -//int8_t mk_curve = 0; +// int8_t mk_curve = 0; /* wheel params */ -uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; +uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED; uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; static uint8_t move_unit(void) { - uint16_t unit; - if (mousekey_accel & (1<<0)) { - unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4; - } else if (mousekey_accel & (1<<1)) { - unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2; - } else if (mousekey_accel & (1<<2)) { - unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); - } else if (mousekey_repeat == 0) { - unit = MOUSEKEY_MOVE_DELTA; - } else if (mousekey_repeat >= mk_time_to_max) { - unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; - } else { - unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; - } - return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); + uint16_t unit; + if (mousekey_accel & (1 << 0)) { + unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4; + } else if (mousekey_accel & (1 << 1)) { + unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2; + } else if (mousekey_accel & (1 << 2)) { + unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); + } else if (mousekey_repeat == 0) { + unit = MOUSEKEY_MOVE_DELTA; + } else if (mousekey_repeat >= mk_time_to_max) { + unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; + } else { + unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; + } + return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); } static uint8_t wheel_unit(void) { - uint16_t unit; - if (mousekey_accel & (1<<0)) { - unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4; - } else if (mousekey_accel & (1<<1)) { - unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2; - } else if (mousekey_accel & (1<<2)) { - unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); - } else if (mousekey_repeat == 0) { - unit = MOUSEKEY_WHEEL_DELTA; - } else if (mousekey_repeat >= mk_wheel_time_to_max) { - unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; - } else { - unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; - } - return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); + uint16_t unit; + if (mousekey_accel & (1 << 0)) { + unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4; + } else if (mousekey_accel & (1 << 1)) { + unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2; + } else if (mousekey_accel & (1 << 2)) { + unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed); + } else if (mousekey_repeat == 0) { + unit = MOUSEKEY_WHEEL_DELTA; + } else if (mousekey_repeat >= mk_wheel_time_to_max) { + unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; + } else { + unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; + } + return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit)); } void mousekey_task(void) { - if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) { - return; - } - if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) { - return; - } - if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; - if (mouse_report.x > 0) mouse_report.x = move_unit(); - if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; - if (mouse_report.y > 0) mouse_report.y = move_unit(); - if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; - /* diagonal move [1/sqrt(2)] */ - if (mouse_report.x && mouse_report.y) { - mouse_report.x = times_inv_sqrt2(mouse_report.x); - if (mouse_report.x == 0) { mouse_report.x = 1; } - mouse_report.y = times_inv_sqrt2(mouse_report.y); - if (mouse_report.y == 0) { mouse_report.y = 1; } - } - if (mouse_report.v > 0) mouse_report.v = wheel_unit(); - if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; - if (mouse_report.h > 0) mouse_report.h = wheel_unit(); - if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; - mousekey_send(); + if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay * 10)) { + return; + } + if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) { + return; + } + if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; + if (mouse_report.x > 0) mouse_report.x = move_unit(); + if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; + if (mouse_report.y > 0) mouse_report.y = move_unit(); + if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; + /* diagonal move [1/sqrt(2)] */ + if (mouse_report.x && mouse_report.y) { + mouse_report.x = times_inv_sqrt2(mouse_report.x); + if (mouse_report.x == 0) { + mouse_report.x = 1; + } + mouse_report.y = times_inv_sqrt2(mouse_report.y); + if (mouse_report.y == 0) { + mouse_report.y = 1; + } + } + if (mouse_report.v > 0) mouse_report.v = wheel_unit(); + if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; + if (mouse_report.h > 0) mouse_report.h = wheel_unit(); + if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; + mousekey_send(); } void mousekey_on(uint8_t code) { - if (code == KC_MS_UP) mouse_report.y = move_unit() * -1; - else if (code == KC_MS_DOWN) mouse_report.y = move_unit(); - else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1; - else if (code == KC_MS_RIGHT) mouse_report.x = move_unit(); - else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit(); - else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1; - else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1; - else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit(); - else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; - else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; - else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; - else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; - else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; - else if (code == KC_MS_ACCEL0) mousekey_accel |= (1<<0); - else if (code == KC_MS_ACCEL1) mousekey_accel |= (1<<1); - else if (code == KC_MS_ACCEL2) mousekey_accel |= (1<<2); + if (code == KC_MS_UP) + mouse_report.y = move_unit() * -1; + else if (code == KC_MS_DOWN) + mouse_report.y = move_unit(); + else if (code == KC_MS_LEFT) + mouse_report.x = move_unit() * -1; + else if (code == KC_MS_RIGHT) + mouse_report.x = move_unit(); + else if (code == KC_MS_WH_UP) + mouse_report.v = wheel_unit(); + else if (code == KC_MS_WH_DOWN) + mouse_report.v = wheel_unit() * -1; + else if (code == KC_MS_WH_LEFT) + mouse_report.h = wheel_unit() * -1; + else if (code == KC_MS_WH_RIGHT) + mouse_report.h = wheel_unit(); + else if (code == KC_MS_BTN1) + mouse_report.buttons |= MOUSE_BTN1; + else if (code == KC_MS_BTN2) + mouse_report.buttons |= MOUSE_BTN2; + else if (code == KC_MS_BTN3) + mouse_report.buttons |= MOUSE_BTN3; + else if (code == KC_MS_BTN4) + mouse_report.buttons |= MOUSE_BTN4; + else if (code == KC_MS_BTN5) + mouse_report.buttons |= MOUSE_BTN5; + else if (code == KC_MS_ACCEL0) + mousekey_accel |= (1 << 0); + else if (code == KC_MS_ACCEL1) + mousekey_accel |= (1 << 1); + else if (code == KC_MS_ACCEL2) + mousekey_accel |= (1 << 2); } void mousekey_off(uint8_t code) { - if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; - else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; - else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; - else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; - else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; - else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; - else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; - else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; - else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; - else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; - else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; - else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; - else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; - else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0); - else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1); - else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2); - if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) - mousekey_repeat = 0; + if (code == KC_MS_UP && mouse_report.y < 0) + mouse_report.y = 0; + else if (code == KC_MS_DOWN && mouse_report.y > 0) + mouse_report.y = 0; + else if (code == KC_MS_LEFT && mouse_report.x < 0) + mouse_report.x = 0; + else if (code == KC_MS_RIGHT && mouse_report.x > 0) + mouse_report.x = 0; + else if (code == KC_MS_WH_UP && mouse_report.v > 0) + mouse_report.v = 0; + else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) + mouse_report.v = 0; + else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) + mouse_report.h = 0; + else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) + mouse_report.h = 0; + else if (code == KC_MS_BTN1) + mouse_report.buttons &= ~MOUSE_BTN1; + else if (code == KC_MS_BTN2) + mouse_report.buttons &= ~MOUSE_BTN2; + else if (code == KC_MS_BTN3) + mouse_report.buttons &= ~MOUSE_BTN3; + else if (code == KC_MS_BTN4) + mouse_report.buttons &= ~MOUSE_BTN4; + else if (code == KC_MS_BTN5) + mouse_report.buttons &= ~MOUSE_BTN5; + else if (code == KC_MS_ACCEL0) + mousekey_accel &= ~(1 << 0); + else if (code == KC_MS_ACCEL1) + mousekey_accel &= ~(1 << 1); + else if (code == KC_MS_ACCEL2) + mousekey_accel &= ~(1 << 2); + if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0; } +#else /* #ifndef MK_3_SPEED */ - - -#else /* #ifndef MK_3_SPEED */ - - - -enum { - mkspd_unmod, - mkspd_0, - mkspd_1, - mkspd_2, - mkspd_COUNT -}; -#ifndef MK_MOMENTARY_ACCEL -static uint8_t mk_speed = mkspd_1; -#else -static uint8_t mk_speed = mkspd_unmod; +enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT }; +# ifndef MK_MOMENTARY_ACCEL +static uint8_t mk_speed = mkspd_1; +# else +static uint8_t mk_speed = mkspd_unmod; static uint8_t mkspd_DEFAULT = mkspd_unmod; -#endif -static uint16_t last_timer_c = 0; -static uint16_t last_timer_w = 0; -uint16_t c_offsets[mkspd_COUNT] = { - MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2 -}; -uint16_t c_intervals[mkspd_COUNT] = { - MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2 -}; -uint16_t w_offsets[mkspd_COUNT] = { - MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2 -}; -uint16_t w_intervals[mkspd_COUNT] = { - MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2 -}; - +# endif +static uint16_t last_timer_c = 0; +static uint16_t last_timer_w = 0; +uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2}; +uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2}; +uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2}; +uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2}; void mousekey_task(void) { - // report cursor and scroll movement independently - report_mouse_t const tmpmr = mouse_report; - if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) { - mouse_report.h = 0; - mouse_report.v = 0; - mousekey_send(); - last_timer_c = last_timer; - mouse_report = tmpmr; - } - if ((mouse_report.h || mouse_report.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) { - mouse_report.x = 0; - mouse_report.y = 0; - mousekey_send(); - last_timer_w = last_timer; - mouse_report = tmpmr; - } + // report cursor and scroll movement independently + report_mouse_t const tmpmr = mouse_report; + if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) { + mouse_report.h = 0; + mouse_report.v = 0; + mousekey_send(); + last_timer_c = last_timer; + mouse_report = tmpmr; + } + if ((mouse_report.h || mouse_report.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) { + mouse_report.x = 0; + mouse_report.y = 0; + mousekey_send(); + last_timer_w = last_timer; + mouse_report = tmpmr; + } } void adjust_speed(void) { - uint16_t const c_offset = c_offsets[mk_speed]; - uint16_t const w_offset = w_offsets[mk_speed]; - if (mouse_report.x > 0) mouse_report.x = c_offset; - if (mouse_report.x < 0) mouse_report.x = c_offset * -1; - if (mouse_report.y > 0) mouse_report.y = c_offset; - if (mouse_report.y < 0) mouse_report.y = c_offset * -1; - if (mouse_report.h > 0) mouse_report.h = w_offset; - if (mouse_report.h < 0) mouse_report.h = w_offset * -1; - if (mouse_report.v > 0) mouse_report.v = w_offset; - if (mouse_report.v < 0) mouse_report.v = w_offset * -1; - // adjust for diagonals - if (mouse_report.x && mouse_report.y) { - mouse_report.x = times_inv_sqrt2(mouse_report.x); - if (mouse_report.x == 0) { mouse_report.x = 1; } - mouse_report.y = times_inv_sqrt2(mouse_report.y); - if (mouse_report.y == 0) { mouse_report.y = 1; } - } - if (mouse_report.h && mouse_report.v) { - mouse_report.h = times_inv_sqrt2(mouse_report.h); - mouse_report.v = times_inv_sqrt2(mouse_report.v); - } + uint16_t const c_offset = c_offsets[mk_speed]; + uint16_t const w_offset = w_offsets[mk_speed]; + if (mouse_report.x > 0) mouse_report.x = c_offset; + if (mouse_report.x < 0) mouse_report.x = c_offset * -1; + if (mouse_report.y > 0) mouse_report.y = c_offset; + if (mouse_report.y < 0) mouse_report.y = c_offset * -1; + if (mouse_report.h > 0) mouse_report.h = w_offset; + if (mouse_report.h < 0) mouse_report.h = w_offset * -1; + if (mouse_report.v > 0) mouse_report.v = w_offset; + if (mouse_report.v < 0) mouse_report.v = w_offset * -1; + // adjust for diagonals + if (mouse_report.x && mouse_report.y) { + mouse_report.x = times_inv_sqrt2(mouse_report.x); + if (mouse_report.x == 0) { + mouse_report.x = 1; + } + mouse_report.y = times_inv_sqrt2(mouse_report.y); + if (mouse_report.y == 0) { + mouse_report.y = 1; + } + } + if (mouse_report.h && mouse_report.v) { + mouse_report.h = times_inv_sqrt2(mouse_report.h); + mouse_report.v = times_inv_sqrt2(mouse_report.v); + } } void mousekey_on(uint8_t code) { - uint16_t const c_offset = c_offsets[mk_speed]; - uint16_t const w_offset = w_offsets[mk_speed]; - uint8_t const old_speed = mk_speed; - if (code == KC_MS_UP) mouse_report.y = c_offset * -1; - else if (code == KC_MS_DOWN) mouse_report.y = c_offset; - else if (code == KC_MS_LEFT) mouse_report.x = c_offset * -1; - else if (code == KC_MS_RIGHT) mouse_report.x = c_offset; - else if (code == KC_MS_WH_UP) mouse_report.v = w_offset; - else if (code == KC_MS_WH_DOWN) mouse_report.v = w_offset * -1; - else if (code == KC_MS_WH_LEFT) mouse_report.h = w_offset * -1; - else if (code == KC_MS_WH_RIGHT) mouse_report.h = w_offset; - else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; - else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; - else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; - else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; - else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; - else if (code == KC_MS_ACCEL0) mk_speed = mkspd_0; - else if (code == KC_MS_ACCEL1) mk_speed = mkspd_1; - else if (code == KC_MS_ACCEL2) mk_speed = mkspd_2; - if (mk_speed != old_speed) adjust_speed(); + uint16_t const c_offset = c_offsets[mk_speed]; + uint16_t const w_offset = w_offsets[mk_speed]; + uint8_t const old_speed = mk_speed; + if (code == KC_MS_UP) + mouse_report.y = c_offset * -1; + else if (code == KC_MS_DOWN) + mouse_report.y = c_offset; + else if (code == KC_MS_LEFT) + mouse_report.x = c_offset * -1; + else if (code == KC_MS_RIGHT) + mouse_report.x = c_offset; + else if (code == KC_MS_WH_UP) + mouse_report.v = w_offset; + else if (code == KC_MS_WH_DOWN) + mouse_report.v = w_offset * -1; + else if (code == KC_MS_WH_LEFT) + mouse_report.h = w_offset * -1; + else if (code == KC_MS_WH_RIGHT) + mouse_report.h = w_offset; + else if (code == KC_MS_BTN1) + mouse_report.buttons |= MOUSE_BTN1; + else if (code == KC_MS_BTN2) + mouse_report.buttons |= MOUSE_BTN2; + else if (code == KC_MS_BTN3) + mouse_report.buttons |= MOUSE_BTN3; + else if (code == KC_MS_BTN4) + mouse_report.buttons |= MOUSE_BTN4; + else if (code == KC_MS_BTN5) + mouse_report.buttons |= MOUSE_BTN5; + else if (code == KC_MS_ACCEL0) + mk_speed = mkspd_0; + else if (code == KC_MS_ACCEL1) + mk_speed = mkspd_1; + else if (code == KC_MS_ACCEL2) + mk_speed = mkspd_2; + if (mk_speed != old_speed) adjust_speed(); } void mousekey_off(uint8_t code) { -#ifdef MK_MOMENTARY_ACCEL - uint8_t const old_speed = mk_speed; -#endif - if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; - else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; - else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; - else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; - else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; - else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; - else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; - else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; - else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; - else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; - else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; - else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; - else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; -#ifdef MK_MOMENTARY_ACCEL - else if (code == KC_MS_ACCEL0) mk_speed = mkspd_DEFAULT; - else if (code == KC_MS_ACCEL1) mk_speed = mkspd_DEFAULT; - else if (code == KC_MS_ACCEL2) mk_speed = mkspd_DEFAULT; - if (mk_speed != old_speed) adjust_speed(); -#endif +# ifdef MK_MOMENTARY_ACCEL + uint8_t const old_speed = mk_speed; +# endif + if (code == KC_MS_UP && mouse_report.y < 0) + mouse_report.y = 0; + else if (code == KC_MS_DOWN && mouse_report.y > 0) + mouse_report.y = 0; + else if (code == KC_MS_LEFT && mouse_report.x < 0) + mouse_report.x = 0; + else if (code == KC_MS_RIGHT && mouse_report.x > 0) + mouse_report.x = 0; + else if (code == KC_MS_WH_UP && mouse_report.v > 0) + mouse_report.v = 0; + else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) + mouse_report.v = 0; + else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) + mouse_report.h = 0; + else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) + mouse_report.h = 0; + else if (code == KC_MS_BTN1) + mouse_report.buttons &= ~MOUSE_BTN1; + else if (code == KC_MS_BTN2) + mouse_report.buttons &= ~MOUSE_BTN2; + else if (code == KC_MS_BTN3) + mouse_report.buttons &= ~MOUSE_BTN3; + else if (code == KC_MS_BTN4) + mouse_report.buttons &= ~MOUSE_BTN4; + else if (code == KC_MS_BTN5) + mouse_report.buttons &= ~MOUSE_BTN5; +# ifdef MK_MOMENTARY_ACCEL + else if (code == KC_MS_ACCEL0) + mk_speed = mkspd_DEFAULT; + else if (code == KC_MS_ACCEL1) + mk_speed = mkspd_DEFAULT; + else if (code == KC_MS_ACCEL2) + mk_speed = mkspd_DEFAULT; + if (mk_speed != old_speed) adjust_speed(); +# endif } - - - #endif /* #ifndef MK_3_SPEED */ - - - void mousekey_send(void) { - mousekey_debug(); - host_mouse_send(&mouse_report); - last_timer = timer_read(); + mousekey_debug(); + host_mouse_send(&mouse_report); + last_timer = timer_read(); } void mousekey_clear(void) { - mouse_report = (report_mouse_t){}; - mousekey_repeat = 0; - mousekey_accel = 0; + mouse_report = (report_mouse_t){}; + mousekey_repeat = 0; + mousekey_accel = 0; } static void mousekey_debug(void) { - if (!debug_mouse) return; - print("mousekey [btn|x y v h](rep/acl): ["); - phex(mouse_report.buttons); print("|"); - print_decs(mouse_report.x); print(" "); - print_decs(mouse_report.y); print(" "); - print_decs(mouse_report.v); print(" "); - print_decs(mouse_report.h); print("]("); - print_dec(mousekey_repeat); print("/"); - print_dec(mousekey_accel); print(")\n"); + if (!debug_mouse) return; + print("mousekey [btn|x y v h](rep/acl): ["); + phex(mouse_report.buttons); + print("|"); + print_decs(mouse_report.x); + print(" "); + print_decs(mouse_report.y); + print(" "); + print_decs(mouse_report.v); + print(" "); + print_decs(mouse_report.h); + print("]("); + print_dec(mousekey_repeat); + print("/"); + print_dec(mousekey_accel); + print(")\n"); } diff --git a/tmk_core/common/mousekey.h b/tmk_core/common/mousekey.h index 89c1eaf11..48946987c 100644 --- a/tmk_core/common/mousekey.h +++ b/tmk_core/common/mousekey.h @@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef MOUSEKEY_H -#define MOUSEKEY_H +# define MOUSEKEY_H #endif #include <stdbool.h> @@ -25,94 +25,94 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef MK_3_SPEED /* max value on report descriptor */ -#ifndef MOUSEKEY_MOVE_MAX - #define MOUSEKEY_MOVE_MAX 127 -#elif MOUSEKEY_MOVE_MAX > 127 - #error MOUSEKEY_MOVE_MAX needs to be smaller than 127 -#endif - -#ifndef MOUSEKEY_WHEEL_MAX - #define MOUSEKEY_WHEEL_MAX 127 -#elif MOUSEKEY_WHEEL_MAX > 127 - #error MOUSEKEY_WHEEL_MAX needs to be smaller than 127 -#endif - -#ifndef MOUSEKEY_MOVE_DELTA -#define MOUSEKEY_MOVE_DELTA 5 -#endif -#ifndef MOUSEKEY_WHEEL_DELTA -#define MOUSEKEY_WHEEL_DELTA 1 -#endif -#ifndef MOUSEKEY_DELAY -#define MOUSEKEY_DELAY 300 -#endif -#ifndef MOUSEKEY_INTERVAL -#define MOUSEKEY_INTERVAL 50 -#endif -#ifndef MOUSEKEY_MAX_SPEED -#define MOUSEKEY_MAX_SPEED 10 -#endif -#ifndef MOUSEKEY_TIME_TO_MAX -#define MOUSEKEY_TIME_TO_MAX 20 -#endif -#ifndef MOUSEKEY_WHEEL_MAX_SPEED -#define MOUSEKEY_WHEEL_MAX_SPEED 8 -#endif -#ifndef MOUSEKEY_WHEEL_TIME_TO_MAX -#define MOUSEKEY_WHEEL_TIME_TO_MAX 40 -#endif +# ifndef MOUSEKEY_MOVE_MAX +# define MOUSEKEY_MOVE_MAX 127 +# elif MOUSEKEY_MOVE_MAX > 127 +# error MOUSEKEY_MOVE_MAX needs to be smaller than 127 +# endif + +# ifndef MOUSEKEY_WHEEL_MAX +# define MOUSEKEY_WHEEL_MAX 127 +# elif MOUSEKEY_WHEEL_MAX > 127 +# error MOUSEKEY_WHEEL_MAX needs to be smaller than 127 +# endif + +# ifndef MOUSEKEY_MOVE_DELTA +# define MOUSEKEY_MOVE_DELTA 5 +# endif +# ifndef MOUSEKEY_WHEEL_DELTA +# define MOUSEKEY_WHEEL_DELTA 1 +# endif +# ifndef MOUSEKEY_DELAY +# define MOUSEKEY_DELAY 300 +# endif +# ifndef MOUSEKEY_INTERVAL +# define MOUSEKEY_INTERVAL 50 +# endif +# ifndef MOUSEKEY_MAX_SPEED +# define MOUSEKEY_MAX_SPEED 10 +# endif +# ifndef MOUSEKEY_TIME_TO_MAX +# define MOUSEKEY_TIME_TO_MAX 20 +# endif +# ifndef MOUSEKEY_WHEEL_MAX_SPEED +# define MOUSEKEY_WHEEL_MAX_SPEED 8 +# endif +# ifndef MOUSEKEY_WHEEL_TIME_TO_MAX +# define MOUSEKEY_WHEEL_TIME_TO_MAX 40 +# endif #else /* #ifndef MK_3_SPEED */ -#ifndef MK_C_OFFSET_UNMOD -#define MK_C_OFFSET_UNMOD 16 -#endif -#ifndef MK_C_INTERVAL_UNMOD -#define MK_C_INTERVAL_UNMOD 16 -#endif -#ifndef MK_C_OFFSET_0 -#define MK_C_OFFSET_0 1 -#endif -#ifndef MK_C_INTERVAL_0 -#define MK_C_INTERVAL_0 32 -#endif -#ifndef MK_C_OFFSET_1 -#define MK_C_OFFSET_1 4 -#endif -#ifndef MK_C_INTERVAL_1 -#define MK_C_INTERVAL_1 16 -#endif -#ifndef MK_C_OFFSET_2 -#define MK_C_OFFSET_2 32 -#endif -#ifndef MK_C_INTERVAL_2 -#define MK_C_INTERVAL_2 16 -#endif - -#ifndef MK_W_OFFSET_UNMOD -#define MK_W_OFFSET_UNMOD 1 -#endif -#ifndef MK_W_INTERVAL_UNMOD -#define MK_W_INTERVAL_UNMOD 40 -#endif -#ifndef MK_W_OFFSET_0 -#define MK_W_OFFSET_0 1 -#endif -#ifndef MK_W_INTERVAL_0 -#define MK_W_INTERVAL_0 360 -#endif -#ifndef MK_W_OFFSET_1 -#define MK_W_OFFSET_1 1 -#endif -#ifndef MK_W_INTERVAL_1 -#define MK_W_INTERVAL_1 120 -#endif -#ifndef MK_W_OFFSET_2 -#define MK_W_OFFSET_2 1 -#endif -#ifndef MK_W_INTERVAL_2 -#define MK_W_INTERVAL_2 20 -#endif +# ifndef MK_C_OFFSET_UNMOD +# define MK_C_OFFSET_UNMOD 16 +# endif +# ifndef MK_C_INTERVAL_UNMOD +# define MK_C_INTERVAL_UNMOD 16 +# endif +# ifndef MK_C_OFFSET_0 +# define MK_C_OFFSET_0 1 +# endif +# ifndef MK_C_INTERVAL_0 +# define MK_C_INTERVAL_0 32 +# endif +# ifndef MK_C_OFFSET_1 +# define MK_C_OFFSET_1 4 +# endif +# ifndef MK_C_INTERVAL_1 +# define MK_C_INTERVAL_1 16 +# endif +# ifndef MK_C_OFFSET_2 +# define MK_C_OFFSET_2 32 +# endif +# ifndef MK_C_INTERVAL_2 +# define MK_C_INTERVAL_2 16 +# endif + +# ifndef MK_W_OFFSET_UNMOD +# define MK_W_OFFSET_UNMOD 1 +# endif +# ifndef MK_W_INTERVAL_UNMOD +# define MK_W_INTERVAL_UNMOD 40 +# endif +# ifndef MK_W_OFFSET_0 +# define MK_W_OFFSET_0 1 +# endif +# ifndef MK_W_INTERVAL_0 +# define MK_W_INTERVAL_0 360 +# endif +# ifndef MK_W_OFFSET_1 +# define MK_W_OFFSET_1 1 +# endif +# ifndef MK_W_INTERVAL_1 +# define MK_W_INTERVAL_1 120 +# endif +# ifndef MK_W_OFFSET_2 +# define MK_W_OFFSET_2 1 +# endif +# ifndef MK_W_INTERVAL_2 +# define MK_W_INTERVAL_2 20 +# endif #endif /* #ifndef MK_3_SPEED */ diff --git a/tmk_core/common/nodebug.h b/tmk_core/common/nodebug.h index 5e18656e5..b9c8b3ac0 100644 --- a/tmk_core/common/nodebug.h +++ b/tmk_core/common/nodebug.h @@ -19,11 +19,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define NODEBUG_H #ifndef NO_DEBUG - #define NO_DEBUG - #include "debug.h" - #undef NO_DEBUG +# define NO_DEBUG +# include "debug.h" +# undef NO_DEBUG #else - #include "debug.h" +# include "debug.h" #endif #endif diff --git a/tmk_core/common/print.c b/tmk_core/common/print.c index 00489557f..07aef0b0e 100644 --- a/tmk_core/common/print.c +++ b/tmk_core/common/print.c @@ -2,17 +2,17 @@ /* Very basic print functions, intended to be used with usb_debug_only.c * http://www.pjrc.com/teensy/ * Copyright (c) 2008 PJRC.COM, LLC - * + * * 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 @@ -25,28 +25,23 @@ #include <stdint.h> #include "print.h" - #ifndef NO_PRINT -#if defined(__AVR__) - -#define sendchar(c) xputc(c) +# if defined(__AVR__) +# define sendchar(c) xputc(c) -void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) -{ - xdev_out(sendchar_func); -} +void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) { xdev_out(sendchar_func); } -#elif defined(PROTOCOL_CHIBIOS) /* __AVR__ */ +# elif defined(PROTOCOL_CHIBIOS) /* __AVR__ */ // don't need anything extra -#elif defined(__arm__) /* __AVR__ */ +# elif defined(__arm__) /* __AVR__ */ // TODO -//void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) { } +// void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) { } -#endif /* __AVR__ */ +# endif /* __AVR__ */ #endif diff --git a/tmk_core/common/print.h b/tmk_core/common/print.h index 2d7184bd0..20189838f 100644 --- a/tmk_core/common/print.h +++ b/tmk_core/common/print.h @@ -30,137 +30,136 @@ #include "util.h" #if defined(PROTOCOL_CHIBIOS) || defined(PROTOCOL_ARM_ATSAM) -#define PSTR(x) x +# define PSTR(x) x #endif - #ifndef NO_PRINT -#if defined(__AVR__) /* __AVR__ */ +# if defined(__AVR__) /* __AVR__ */ -# include "avr/xprintf.h" +# include "avr/xprintf.h" -# ifdef USER_PRINT /* USER_PRINT */ +# ifdef USER_PRINT /* USER_PRINT */ // Remove normal print defines -# define print(s) -# define println(s) -# undef xprintf -# define xprintf(fmt, ...) +# define print(s) +# define println(s) +# undef xprintf +# define xprintf(fmt, ...) // Create user print defines -# define uprint(s) xputs(PSTR(s)) -# define uprintln(s) xputs(PSTR(s "\r\n")) -# define uprintf(fmt, ...) __xprintf(PSTR(fmt), ##__VA_ARGS__) +# define uprint(s) xputs(PSTR(s)) +# define uprintln(s) xputs(PSTR(s "\r\n")) +# define uprintf(fmt, ...) __xprintf(PSTR(fmt), ##__VA_ARGS__) -# else /* NORMAL PRINT */ +# else /* NORMAL PRINT */ // Create user & normal print defines -# define print(s) xputs(PSTR(s)) -# define println(s) xputs(PSTR(s "\r\n")) -# define uprint(s) print(s) -# define uprintln(s) println(s) -# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) +# define print(s) xputs(PSTR(s)) +# define println(s) xputs(PSTR(s "\r\n")) +# define uprint(s) print(s) +# define uprintln(s) println(s) +# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) -# endif /* USER_PRINT / NORMAL PRINT */ +# endif /* USER_PRINT / NORMAL PRINT */ -# ifdef __cplusplus +# ifdef __cplusplus extern "C" -# endif +# endif -/* function pointer of sendchar to be used by print utility */ -void print_set_sendchar(int8_t (*print_sendchar_func)(uint8_t)); + /* function pointer of sendchar to be used by print utility */ + void print_set_sendchar(int8_t (*print_sendchar_func)(uint8_t)); -#elif defined(PROTOCOL_CHIBIOS) /* PROTOCOL_CHIBIOS */ +# elif defined(PROTOCOL_CHIBIOS) /* PROTOCOL_CHIBIOS */ -#ifndef TERMINAL_ENABLE -# include "chibios/printf.h" -#endif +# ifndef TERMINAL_ENABLE +# include "chibios/printf.h" +# endif -# ifdef USER_PRINT /* USER_PRINT */ +# ifdef USER_PRINT /* USER_PRINT */ // Remove normal print defines -# define print(s) -# define println(s) -# define xprintf(fmt, ...) +# define print(s) +# define println(s) +# define xprintf(fmt, ...) // Create user print defines -# define uprint(s) printf(s) -# define uprintln(s) printf(s "\r\n") -# define uprintf printf +# define uprint(s) printf(s) +# define uprintln(s) printf(s "\r\n") +# define uprintf printf -# else /* NORMAL PRINT */ +# else /* NORMAL PRINT */ // Create user & normal print defines -# define print(s) printf(s) -# define println(s) printf(s "\r\n") -# define xprintf printf -# define uprint(s) printf(s) -# define uprintln(s) printf(s "\r\n") -# define uprintf printf +# define print(s) printf(s) +# define println(s) printf(s "\r\n") +# define xprintf printf +# define uprint(s) printf(s) +# define uprintln(s) printf(s "\r\n") +# define uprintf printf -# endif /* USER_PRINT / NORMAL PRINT */ +# endif /* USER_PRINT / NORMAL PRINT */ -#elif defined(PROTOCOL_ARM_ATSAM) /* PROTOCOL_ARM_ATSAM */ +# elif defined(PROTOCOL_ARM_ATSAM) /* PROTOCOL_ARM_ATSAM */ -# include "arm_atsam/printf.h" +# include "arm_atsam/printf.h" -# ifdef USER_PRINT /* USER_PRINT */ +# ifdef USER_PRINT /* USER_PRINT */ // Remove normal print defines -# define print(s) -# define println(s) -# define xprintf(fmt, ...) +# define print(s) +# define println(s) +# define xprintf(fmt, ...) // Create user print defines -# define uprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) -# define uprint(s) xprintf(s) -# define uprintln(s) xprintf(s "\r\n") +# define uprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) +# define uprint(s) xprintf(s) +# define uprintln(s) xprintf(s "\r\n") -# else /* NORMAL PRINT */ +# else /* NORMAL PRINT */ // Create user & normal print defines -# define xprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) -# define print(s) xprintf(s) -# define println(s) xprintf(s "\r\n") -# define uprint(s) print(s) -# define uprintln(s) println(s) -# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) +# define xprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) +# define print(s) xprintf(s) +# define println(s) xprintf(s "\r\n") +# define uprint(s) print(s) +# define uprintln(s) println(s) +# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) -# endif /* USER_PRINT / NORMAL PRINT */ +# endif /* USER_PRINT / NORMAL PRINT */ -#elif defined(__arm__) /* __arm__ */ +# elif defined(__arm__) /* __arm__ */ -# include "mbed/xprintf.h" +# include "mbed/xprintf.h" -# ifdef USER_PRINT /* USER_PRINT */ +# ifdef USER_PRINT /* USER_PRINT */ // Remove normal print defines -# define print(s) -# define println(s) -# define xprintf(fmt, ...) +# define print(s) +# define println(s) +# define xprintf(fmt, ...) // Create user print defines -# define uprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) -# define uprint(s) xprintf(s) -# define uprintln(s) xprintf(s "\r\n") +# define uprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) +# define uprint(s) xprintf(s) +# define uprintln(s) xprintf(s "\r\n") -# else /* NORMAL PRINT */ +# else /* NORMAL PRINT */ // Create user & normal print defines -# define xprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) -# define print(s) xprintf(s) -# define println(s) xprintf(s "\r\n") -# define uprint(s) print(s) -# define uprintln(s) println(s) -# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) +# define xprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__) +# define print(s) xprintf(s) +# define println(s) xprintf(s "\r\n") +# define uprint(s) print(s) +# define uprintln(s) println(s) +# define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) -# endif /* USER_PRINT / NORMAL PRINT */ +# endif /* USER_PRINT / NORMAL PRINT */ /* TODO: to select output destinations: UART/USBSerial */ -# define print_set_sendchar(func) +# define print_set_sendchar(func) -#endif /* __AVR__ / PROTOCOL_CHIBIOS / PROTOCOL_ARM_ATSAM / __arm__ */ +# endif /* __AVR__ / PROTOCOL_CHIBIOS / PROTOCOL_ARM_ATSAM / __arm__ */ // User print disables the normal print messages in the body of QMK/TMK code and // is meant as a lightweight alternative to NOPRINT. Use it when you only want to do @@ -169,141 +168,140 @@ void print_set_sendchar(int8_t (*print_sendchar_func)(uint8_t)); // // !!! DO NOT USE USER PRINT CALLS IN THE BODY OF QMK/TMK !!! // -#ifdef USER_PRINT +# ifdef USER_PRINT // Disable normal print -#define print_dec(data) -#define print_decs(data) -#define print_hex4(data) -#define print_hex8(data) -#define print_hex16(data) -#define print_hex32(data) -#define print_bin4(data) -#define print_bin8(data) -#define print_bin16(data) -#define print_bin32(data) -#define print_bin_reverse8(data) -#define print_bin_reverse16(data) -#define print_bin_reverse32(data) -#define print_val_dec(v) -#define print_val_decs(v) -#define print_val_hex8(v) -#define print_val_hex16(v) -#define print_val_hex32(v) -#define print_val_bin8(v) -#define print_val_bin16(v) -#define print_val_bin32(v) -#define print_val_bin_reverse8(v) -#define print_val_bin_reverse16(v) -#define print_val_bin_reverse32(v) - -#else /* NORMAL_PRINT */ - -//Enable normal print +# define print_dec(data) +# define print_decs(data) +# define print_hex4(data) +# define print_hex8(data) +# define print_hex16(data) +# define print_hex32(data) +# define print_bin4(data) +# define print_bin8(data) +# define print_bin16(data) +# define print_bin32(data) +# define print_bin_reverse8(data) +# define print_bin_reverse16(data) +# define print_bin_reverse32(data) +# define print_val_dec(v) +# define print_val_decs(v) +# define print_val_hex8(v) +# define print_val_hex16(v) +# define print_val_hex32(v) +# define print_val_bin8(v) +# define print_val_bin16(v) +# define print_val_bin32(v) +# define print_val_bin_reverse8(v) +# define print_val_bin_reverse16(v) +# define print_val_bin_reverse32(v) + +# else /* NORMAL_PRINT */ + +// Enable normal print /* decimal */ -#define print_dec(i) xprintf("%u", i) -#define print_decs(i) xprintf("%d", i) +# define print_dec(i) xprintf("%u", i) +# define print_decs(i) xprintf("%d", i) /* hex */ -#define print_hex4(i) xprintf("%X", i) -#define print_hex8(i) xprintf("%02X", i) -#define print_hex16(i) xprintf("%04X", i) -#define print_hex32(i) xprintf("%08lX", i) +# define print_hex4(i) xprintf("%X", i) +# define print_hex8(i) xprintf("%02X", i) +# define print_hex16(i) xprintf("%04X", i) +# define print_hex32(i) xprintf("%08lX", i) /* binary */ -#define print_bin4(i) xprintf("%04b", i) -#define print_bin8(i) xprintf("%08b", i) -#define print_bin16(i) xprintf("%016b", i) -#define print_bin32(i) xprintf("%032lb", i) -#define print_bin_reverse8(i) xprintf("%08b", bitrev(i)) -#define print_bin_reverse16(i) xprintf("%016b", bitrev16(i)) -#define print_bin_reverse32(i) xprintf("%032lb", bitrev32(i)) +# define print_bin4(i) xprintf("%04b", i) +# define print_bin8(i) xprintf("%08b", i) +# define print_bin16(i) xprintf("%016b", i) +# define print_bin32(i) xprintf("%032lb", i) +# define print_bin_reverse8(i) xprintf("%08b", bitrev(i)) +# define print_bin_reverse16(i) xprintf("%016b", bitrev16(i)) +# define print_bin_reverse32(i) xprintf("%032lb", bitrev32(i)) /* print value utility */ -#define print_val_dec(v) xprintf(#v ": %u\n", v) -#define print_val_decs(v) xprintf(#v ": %d\n", v) -#define print_val_hex8(v) xprintf(#v ": %X\n", v) -#define print_val_hex16(v) xprintf(#v ": %02X\n", v) -#define print_val_hex32(v) xprintf(#v ": %04lX\n", v) -#define print_val_bin8(v) xprintf(#v ": %08b\n", v) -#define print_val_bin16(v) xprintf(#v ": %016b\n", v) -#define print_val_bin32(v) xprintf(#v ": %032lb\n", v) -#define print_val_bin_reverse8(v) xprintf(#v ": %08b\n", bitrev(v)) -#define print_val_bin_reverse16(v) xprintf(#v ": %016b\n", bitrev16(v)) -#define print_val_bin_reverse32(v) xprintf(#v ": %032lb\n", bitrev32(v)) - -#endif /* USER_PRINT / NORMAL_PRINT */ +# define print_val_dec(v) xprintf(# v ": %u\n", v) +# define print_val_decs(v) xprintf(# v ": %d\n", v) +# define print_val_hex8(v) xprintf(# v ": %X\n", v) +# define print_val_hex16(v) xprintf(# v ": %02X\n", v) +# define print_val_hex32(v) xprintf(# v ": %04lX\n", v) +# define print_val_bin8(v) xprintf(# v ": %08b\n", v) +# define print_val_bin16(v) xprintf(# v ": %016b\n", v) +# define print_val_bin32(v) xprintf(# v ": %032lb\n", v) +# define print_val_bin_reverse8(v) xprintf(# v ": %08b\n", bitrev(v)) +# define print_val_bin_reverse16(v) xprintf(# v ": %016b\n", bitrev16(v)) +# define print_val_bin_reverse32(v) xprintf(# v ": %032lb\n", bitrev32(v)) + +# endif /* USER_PRINT / NORMAL_PRINT */ // User Print /* decimal */ -#define uprint_dec(i) uprintf("%u", i) -#define uprint_decs(i) uprintf("%d", i) +# define uprint_dec(i) uprintf("%u", i) +# define uprint_decs(i) uprintf("%d", i) /* hex */ -#define uprint_hex4(i) uprintf("%X", i) -#define uprint_hex8(i) uprintf("%02X", i) -#define uprint_hex16(i) uprintf("%04X", i) -#define uprint_hex32(i) uprintf("%08lX", i) +# define uprint_hex4(i) uprintf("%X", i) +# define uprint_hex8(i) uprintf("%02X", i) +# define uprint_hex16(i) uprintf("%04X", i) +# define uprint_hex32(i) uprintf("%08lX", i) /* binary */ -#define uprint_bin4(i) uprintf("%04b", i) -#define uprint_bin8(i) uprintf("%08b", i) -#define uprint_bin16(i) uprintf("%016b", i) -#define uprint_bin32(i) uprintf("%032lb", i) -#define uprint_bin_reverse8(i) uprintf("%08b", bitrev(i)) -#define uprint_bin_reverse16(i) uprintf("%016b", bitrev16(i)) -#define uprint_bin_reverse32(i) uprintf("%032lb", bitrev32(i)) +# define uprint_bin4(i) uprintf("%04b", i) +# define uprint_bin8(i) uprintf("%08b", i) +# define uprint_bin16(i) uprintf("%016b", i) +# define uprint_bin32(i) uprintf("%032lb", i) +# define uprint_bin_reverse8(i) uprintf("%08b", bitrev(i)) +# define uprint_bin_reverse16(i) uprintf("%016b", bitrev16(i)) +# define uprint_bin_reverse32(i) uprintf("%032lb", bitrev32(i)) /* print value utility */ -#define uprint_val_dec(v) uprintf(#v ": %u\n", v) -#define uprint_val_decs(v) uprintf(#v ": %d\n", v) -#define uprint_val_hex8(v) uprintf(#v ": %X\n", v) -#define uprint_val_hex16(v) uprintf(#v ": %02X\n", v) -#define uprint_val_hex32(v) uprintf(#v ": %04lX\n", v) -#define uprint_val_bin8(v) uprintf(#v ": %08b\n", v) -#define uprint_val_bin16(v) uprintf(#v ": %016b\n", v) -#define uprint_val_bin32(v) uprintf(#v ": %032lb\n", v) -#define uprint_val_bin_reverse8(v) uprintf(#v ": %08b\n", bitrev(v)) -#define uprint_val_bin_reverse16(v) uprintf(#v ": %016b\n", bitrev16(v)) -#define uprint_val_bin_reverse32(v) uprintf(#v ": %032lb\n", bitrev32(v)) - -#else /* NO_PRINT */ - -#define xprintf(fmt, ...) -#define print(s) -#define println(s) -#define print_set_sendchar(func) -#define print_dec(data) -#define print_decs(data) -#define print_hex4(data) -#define print_hex8(data) -#define print_hex16(data) -#define print_hex32(data) -#define print_bin4(data) -#define print_bin8(data) -#define print_bin16(data) -#define print_bin32(data) -#define print_bin_reverse8(data) -#define print_bin_reverse16(data) -#define print_bin_reverse32(data) -#define print_val_dec(v) -#define print_val_decs(v) -#define print_val_hex8(v) -#define print_val_hex16(v) -#define print_val_hex32(v) -#define print_val_bin8(v) -#define print_val_bin16(v) -#define print_val_bin32(v) -#define print_val_bin_reverse8(v) -#define print_val_bin_reverse16(v) -#define print_val_bin_reverse32(v) - -#endif /* NO_PRINT */ +# define uprint_val_dec(v) uprintf(# v ": %u\n", v) +# define uprint_val_decs(v) uprintf(# v ": %d\n", v) +# define uprint_val_hex8(v) uprintf(# v ": %X\n", v) +# define uprint_val_hex16(v) uprintf(# v ": %02X\n", v) +# define uprint_val_hex32(v) uprintf(# v ": %04lX\n", v) +# define uprint_val_bin8(v) uprintf(# v ": %08b\n", v) +# define uprint_val_bin16(v) uprintf(# v ": %016b\n", v) +# define uprint_val_bin32(v) uprintf(# v ": %032lb\n", v) +# define uprint_val_bin_reverse8(v) uprintf(# v ": %08b\n", bitrev(v)) +# define uprint_val_bin_reverse16(v) uprintf(# v ": %016b\n", bitrev16(v)) +# define uprint_val_bin_reverse32(v) uprintf(# v ": %032lb\n", bitrev32(v)) + +#else /* NO_PRINT */ +# define xprintf(fmt, ...) +# define print(s) +# define println(s) +# define print_set_sendchar(func) +# define print_dec(data) +# define print_decs(data) +# define print_hex4(data) +# define print_hex8(data) +# define print_hex16(data) +# define print_hex32(data) +# define print_bin4(data) +# define print_bin8(data) +# define print_bin16(data) +# define print_bin32(data) +# define print_bin_reverse8(data) +# define print_bin_reverse16(data) +# define print_bin_reverse32(data) +# define print_val_dec(v) +# define print_val_decs(v) +# define print_val_hex8(v) +# define print_val_hex16(v) +# define print_val_hex32(v) +# define print_val_bin8(v) +# define print_val_bin16(v) +# define print_val_bin32(v) +# define print_val_bin_reverse8(v) +# define print_val_bin_reverse16(v) +# define print_val_bin_reverse32(v) + +#endif /* NO_PRINT */ /* Backward compatiblitly for old name */ -#define pdec(data) print_dec(data) -#define pdec16(data) print_dec(data) -#define phex(data) print_hex8(data) -#define phex16(data) print_hex16(data) -#define pbin(data) print_bin8(data) -#define pbin16(data) print_bin16(data) -#define pbin_reverse(data) print_bin_reverse8(data) -#define pbin_reverse16(data) print_bin_reverse16(data) +#define pdec(data) print_dec(data) +#define pdec16(data) print_dec(data) +#define phex(data) print_hex8(data) +#define phex16(data) print_hex16(data) +#define pbin(data) print_bin8(data) +#define pbin16(data) print_bin16(data) +#define pbin_reverse(data) print_bin_reverse8(data) +#define pbin_reverse16(data) print_bin_reverse16(data) #endif diff --git a/tmk_core/common/progmem.h b/tmk_core/common/progmem.h index de9313839..88373f478 100644 --- a/tmk_core/common/progmem.h +++ b/tmk_core/common/progmem.h @@ -2,12 +2,12 @@ #define PROGMEM_H 1 #if defined(__AVR__) -# include <avr/pgmspace.h> +# include <avr/pgmspace.h> #else -# define PROGMEM -# define pgm_read_byte(p) *((unsigned char*)(p)) -# define pgm_read_word(p) *((uint16_t*)(p)) -# define pgm_read_dword(p) *((uint32_t*)(p)) +# define PROGMEM +# define pgm_read_byte(p) *((unsigned char*)(p)) +# define pgm_read_word(p) *((uint16_t*)(p)) +# define pgm_read_dword(p) *((uint32_t*)(p)) #endif #endif diff --git a/tmk_core/common/raw_hid.h b/tmk_core/common/raw_hid.h index 86da02fd1..c579157f1 100644 --- a/tmk_core/common/raw_hid.h +++ b/tmk_core/common/raw_hid.h @@ -1,8 +1,8 @@ #ifndef _RAW_HID_H_ #define _RAW_HID_H_ -void raw_hid_receive( uint8_t *data, uint8_t length ); +void raw_hid_receive(uint8_t *data, uint8_t length); -void raw_hid_send( uint8_t *data, uint8_t length ); +void raw_hid_send(uint8_t *data, uint8_t length); #endif diff --git a/tmk_core/common/report.c b/tmk_core/common/report.c index 6a06b70c6..f4758b48e 100644 --- a/tmk_core/common/report.c +++ b/tmk_core/common/report.c @@ -25,20 +25,18 @@ * * FIXME: Needs doc */ -uint8_t has_anykey(report_keyboard_t* keyboard_report) -{ - uint8_t cnt = 0; - uint8_t *p = keyboard_report->keys; - uint8_t lp = sizeof(keyboard_report->keys); +uint8_t has_anykey(report_keyboard_t* keyboard_report) { + uint8_t cnt = 0; + uint8_t* p = keyboard_report->keys; + uint8_t lp = sizeof(keyboard_report->keys); #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { - p = keyboard_report->nkro.bits; + p = keyboard_report->nkro.bits; lp = sizeof(keyboard_report->nkro.bits); } #endif while (lp--) { - if (*p++) - cnt++; + if (*p++) cnt++; } return cnt; } @@ -47,14 +45,13 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) * * FIXME: Needs doc */ -uint8_t get_first_key(report_keyboard_t* keyboard_report) -{ +uint8_t get_first_key(report_keyboard_t* keyboard_report) { #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { uint8_t i = 0; for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) ; - return i<<3 | biton(keyboard_report->nkro.bits[i]); + return i << 3 | biton(keyboard_report->nkro.bits[i]); } #endif #ifdef USB_6KRO_ENABLE @@ -75,10 +72,9 @@ uint8_t get_first_key(report_keyboard_t* keyboard_report) * * FIXME: Needs doc */ -void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) -{ +void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { #ifdef USB_6KRO_ENABLE - int8_t i = cb_head; + int8_t i = cb_head; int8_t empty = -1; if (cb_count) { do { @@ -97,18 +93,16 @@ void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) // pop head when has no empty space cb_head = RO_INC(cb_head); cb_count--; - } - else { + } else { // left shift when has empty space uint8_t offset = 1; - i = RO_INC(empty); + i = RO_INC(empty); do { if (keyboard_report->keys[i] != 0) { keyboard_report->keys[empty] = keyboard_report->keys[i]; - keyboard_report->keys[i] = 0; - empty = RO_INC(empty); - } - else { + keyboard_report->keys[i] = 0; + empty = RO_INC(empty); + } else { offset++; } i = RO_INC(i); @@ -120,10 +114,10 @@ void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) } // add to tail keyboard_report->keys[cb_tail] = code; - cb_tail = RO_INC(cb_tail); + cb_tail = RO_INC(cb_tail); cb_count++; #else - int8_t i = 0; + int8_t i = 0; int8_t empty = -1; for (; i < KEYBOARD_REPORT_KEYS; i++) { if (keyboard_report->keys[i] == code) { @@ -145,8 +139,7 @@ void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) * * FIXME: Needs doc */ -void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) -{ +void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { #ifdef USB_6KRO_ENABLE uint8_t i = cb_head; if (cb_count) { @@ -186,10 +179,9 @@ void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) * * FIXME: Needs doc */ -void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) -{ - if ((code>>3) < KEYBOARD_REPORT_BITS) { - keyboard_report->nkro.bits[code>>3] |= 1<<(code&7); +void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { + if ((code >> 3) < KEYBOARD_REPORT_BITS) { + keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7); } else { dprintf("add_key_bit: can't add: %02X\n", code); } @@ -199,10 +191,9 @@ void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) * * FIXME: Needs doc */ -void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) -{ - if ((code>>3) < KEYBOARD_REPORT_BITS) { - keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7)); +void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { + if ((code >> 3) < KEYBOARD_REPORT_BITS) { + keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7)); } else { dprintf("del_key_bit: can't del: %02X\n", code); } @@ -213,8 +204,7 @@ void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) * * FIXME: Needs doc */ -void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) -{ +void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { add_key_bit(keyboard_report, key); @@ -228,8 +218,7 @@ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) * * FIXME: Needs doc */ -void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) -{ +void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { del_key_bit(keyboard_report, key); @@ -243,8 +232,7 @@ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) * * FIXME: Needs doc */ -void clear_keys_from_report(report_keyboard_t* keyboard_report) -{ +void clear_keys_from_report(report_keyboard_t* keyboard_report) { // not clear mods #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { diff --git a/tmk_core/common/report.h b/tmk_core/common/report.h index e7c31bd37..ccc6d599b 100644 --- a/tmk_core/common/report.h +++ b/tmk_core/common/report.h @@ -21,90 +21,88 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #include "keycode.h" - /* report id */ -#define REPORT_ID_KEYBOARD 1 -#define REPORT_ID_MOUSE 2 -#define REPORT_ID_SYSTEM 3 -#define REPORT_ID_CONSUMER 4 -#define REPORT_ID_NKRO 5 +#define REPORT_ID_KEYBOARD 1 +#define REPORT_ID_MOUSE 2 +#define REPORT_ID_SYSTEM 3 +#define REPORT_ID_CONSUMER 4 +#define REPORT_ID_NKRO 5 /* mouse buttons */ -#define MOUSE_BTN1 (1<<0) -#define MOUSE_BTN2 (1<<1) -#define MOUSE_BTN3 (1<<2) -#define MOUSE_BTN4 (1<<3) -#define MOUSE_BTN5 (1<<4) +#define MOUSE_BTN1 (1 << 0) +#define MOUSE_BTN2 (1 << 1) +#define MOUSE_BTN3 (1 << 2) +#define MOUSE_BTN4 (1 << 3) +#define MOUSE_BTN5 (1 << 4) /* Consumer Page(0x0C) * following are supported by Windows: http://msdn.microsoft.com/en-us/windows/hardware/gg463372.aspx * see also https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/display-brightness-control */ -#define AUDIO_MUTE 0x00E2 -#define AUDIO_VOL_UP 0x00E9 -#define AUDIO_VOL_DOWN 0x00EA -#define TRANSPORT_NEXT_TRACK 0x00B5 -#define TRANSPORT_PREV_TRACK 0x00B6 -#define TRANSPORT_STOP 0x00B7 -#define TRANSPORT_STOP_EJECT 0x00CC -#define TRANSPORT_PLAY_PAUSE 0x00CD -#define BRIGHTNESS_UP 0x006F -#define BRIGHTNESS_DOWN 0x0070 +#define AUDIO_MUTE 0x00E2 +#define AUDIO_VOL_UP 0x00E9 +#define AUDIO_VOL_DOWN 0x00EA +#define TRANSPORT_NEXT_TRACK 0x00B5 +#define TRANSPORT_PREV_TRACK 0x00B6 +#define TRANSPORT_STOP 0x00B7 +#define TRANSPORT_STOP_EJECT 0x00CC +#define TRANSPORT_PLAY_PAUSE 0x00CD +#define BRIGHTNESS_UP 0x006F +#define BRIGHTNESS_DOWN 0x0070 /* application launch */ -#define AL_CC_CONFIG 0x0183 -#define AL_EMAIL 0x018A -#define AL_CALCULATOR 0x0192 -#define AL_LOCAL_BROWSER 0x0194 +#define AL_CC_CONFIG 0x0183 +#define AL_EMAIL 0x018A +#define AL_CALCULATOR 0x0192 +#define AL_LOCAL_BROWSER 0x0194 /* application control */ -#define AC_SEARCH 0x0221 -#define AC_HOME 0x0223 -#define AC_BACK 0x0224 -#define AC_FORWARD 0x0225 -#define AC_STOP 0x0226 -#define AC_REFRESH 0x0227 -#define AC_BOOKMARKS 0x022A +#define AC_SEARCH 0x0221 +#define AC_HOME 0x0223 +#define AC_BACK 0x0224 +#define AC_FORWARD 0x0225 +#define AC_STOP 0x0226 +#define AC_REFRESH 0x0227 +#define AC_BOOKMARKS 0x022A /* supplement for Bluegiga iWRAP HID(not supported by Windows?) */ -#define AL_LOCK 0x019E -#define TRANSPORT_RECORD 0x00B2 -#define TRANSPORT_FAST_FORWARD 0x00B3 -#define TRANSPORT_REWIND 0x00B4 -#define TRANSPORT_EJECT 0x00B8 -#define AC_MINIMIZE 0x0206 +#define AL_LOCK 0x019E +#define TRANSPORT_RECORD 0x00B2 +#define TRANSPORT_FAST_FORWARD 0x00B3 +#define TRANSPORT_REWIND 0x00B4 +#define TRANSPORT_EJECT 0x00B8 +#define AC_MINIMIZE 0x0206 /* Generic Desktop Page(0x01) - system power control */ -#define SYSTEM_POWER_DOWN 0x0081 -#define SYSTEM_SLEEP 0x0082 -#define SYSTEM_WAKE_UP 0x0083 - +#define SYSTEM_POWER_DOWN 0x0081 +#define SYSTEM_SLEEP 0x0082 +#define SYSTEM_WAKE_UP 0x0083 #define NKRO_SHARED_EP /* key report size(NKRO or boot mode) */ #if defined(NKRO_ENABLE) - #if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS) - #include "protocol/usb_descriptor.h" - #define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2) - #elif defined(PROTOCOL_ARM_ATSAM) - #include "protocol/arm_atsam/usb/udi_device_epsize.h" - #define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1) - #undef NKRO_SHARED_EP - #undef MOUSE_SHARED_EP - #else - #error "NKRO not supported with this protocol" - #endif +# if defined(PROTOCOL_LUFA) || defined(PROTOCOL_CHIBIOS) +# include "protocol/usb_descriptor.h" +# define KEYBOARD_REPORT_BITS (SHARED_EPSIZE - 2) +# elif defined(PROTOCOL_ARM_ATSAM) +# include "protocol/arm_atsam/usb/udi_device_epsize.h" +# define KEYBOARD_REPORT_BITS (NKRO_EPSIZE - 1) +# undef NKRO_SHARED_EP +# undef MOUSE_SHARED_EP +# else +# error "NKRO not supported with this protocol" +# endif #endif #ifdef KEYBOARD_SHARED_EP -# define KEYBOARD_REPORT_SIZE 9 +# define KEYBOARD_REPORT_SIZE 9 #else -# define KEYBOARD_REPORT_SIZE 8 +# define KEYBOARD_REPORT_SIZE 8 #endif #define KEYBOARD_REPORT_KEYS 6 /* VUSB hardcodes keyboard and mouse+extrakey only */ #if defined(PROTOCOL_VUSB) - #undef KEYBOARD_SHARED_EP - #undef MOUSE_SHARED_EP +# undef KEYBOARD_SHARED_EP +# undef MOUSE_SHARED_EP #endif #ifdef __cplusplus @@ -143,58 +141,32 @@ typedef union { }; #ifdef NKRO_ENABLE struct nkro_report { -#ifdef NKRO_SHARED_EP +# ifdef NKRO_SHARED_EP uint8_t report_id; -#endif +# endif uint8_t mods; uint8_t bits[KEYBOARD_REPORT_BITS]; } nkro; #endif -} __attribute__ ((packed)) report_keyboard_t; +} __attribute__((packed)) report_keyboard_t; typedef struct { #ifdef MOUSE_SHARED_EP uint8_t report_id; #endif uint8_t buttons; - int8_t x; - int8_t y; - int8_t v; - int8_t h; -} __attribute__ ((packed)) report_mouse_t; - + int8_t x; + int8_t y; + int8_t v; + int8_t h; +} __attribute__((packed)) report_mouse_t; /* keycode to system usage */ -#define KEYCODE2SYSTEM(key) \ - (key == KC_SYSTEM_POWER ? SYSTEM_POWER_DOWN : \ - (key == KC_SYSTEM_SLEEP ? SYSTEM_SLEEP : \ - (key == KC_SYSTEM_WAKE ? SYSTEM_WAKE_UP : 0))) +#define KEYCODE2SYSTEM(key) (key == KC_SYSTEM_POWER ? SYSTEM_POWER_DOWN : (key == KC_SYSTEM_SLEEP ? SYSTEM_SLEEP : (key == KC_SYSTEM_WAKE ? SYSTEM_WAKE_UP : 0))) /* keycode to consumer usage */ #define KEYCODE2CONSUMER(key) \ - (key == KC_AUDIO_MUTE ? AUDIO_MUTE : \ - (key == KC_AUDIO_VOL_UP ? AUDIO_VOL_UP : \ - (key == KC_AUDIO_VOL_DOWN ? AUDIO_VOL_DOWN : \ - (key == KC_MEDIA_NEXT_TRACK ? TRANSPORT_NEXT_TRACK : \ - (key == KC_MEDIA_PREV_TRACK ? TRANSPORT_PREV_TRACK : \ - (key == KC_MEDIA_FAST_FORWARD ? TRANSPORT_FAST_FORWARD : \ - (key == KC_MEDIA_REWIND ? TRANSPORT_REWIND : \ - (key == KC_MEDIA_STOP ? TRANSPORT_STOP : \ - (key == KC_MEDIA_EJECT ? TRANSPORT_STOP_EJECT : \ - (key == KC_MEDIA_PLAY_PAUSE ? TRANSPORT_PLAY_PAUSE : \ - (key == KC_MEDIA_SELECT ? AL_CC_CONFIG : \ - (key == KC_MAIL ? AL_EMAIL : \ - (key == KC_CALCULATOR ? AL_CALCULATOR : \ - (key == KC_MY_COMPUTER ? AL_LOCAL_BROWSER : \ - (key == KC_WWW_SEARCH ? AC_SEARCH : \ - (key == KC_WWW_HOME ? AC_HOME : \ - (key == KC_WWW_BACK ? AC_BACK : \ - (key == KC_WWW_FORWARD ? AC_FORWARD : \ - (key == KC_WWW_STOP ? AC_STOP : \ - (key == KC_WWW_REFRESH ? AC_REFRESH : \ - (key == KC_BRIGHTNESS_UP ? BRIGHTNESS_UP : \ - (key == KC_BRIGHTNESS_DOWN ? BRIGHTNESS_DOWN : \ - (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0))))))))))))))))))))))) + (key == KC_AUDIO_MUTE ? AUDIO_MUTE : (key == KC_AUDIO_VOL_UP ? AUDIO_VOL_UP : (key == KC_AUDIO_VOL_DOWN ? AUDIO_VOL_DOWN : (key == KC_MEDIA_NEXT_TRACK ? TRANSPORT_NEXT_TRACK : (key == KC_MEDIA_PREV_TRACK ? TRANSPORT_PREV_TRACK : (key == KC_MEDIA_FAST_FORWARD ? TRANSPORT_FAST_FORWARD : (key == KC_MEDIA_REWIND ? TRANSPORT_REWIND : (key == KC_MEDIA_STOP ? TRANSPORT_STOP : (key == KC_MEDIA_EJECT ? TRANSPORT_STOP_EJECT : (key == KC_MEDIA_PLAY_PAUSE ? TRANSPORT_PLAY_PAUSE : (key == KC_MEDIA_SELECT ? AL_CC_CONFIG : (key == KC_MAIL ? AL_EMAIL : (key == KC_CALCULATOR ? AL_CALCULATOR : (key == KC_MY_COMPUTER ? AL_LOCAL_BROWSER : (key == KC_WWW_SEARCH ? AC_SEARCH : (key == KC_WWW_HOME ? AC_HOME : (key == KC_WWW_BACK ? AC_BACK : (key == KC_WWW_FORWARD ? AC_FORWARD : (key == KC_WWW_STOP ? AC_STOP : (key == KC_WWW_REFRESH ? AC_REFRESH : (key == KC_BRIGHTNESS_UP ? BRIGHTNESS_UP : (key == KC_BRIGHTNESS_DOWN ? BRIGHTNESS_DOWN : (key == KC_WWW_FAVORITES ? AC_BOOKMARKS : 0))))))))))))))))))))))) uint8_t has_anykey(report_keyboard_t* keyboard_report); uint8_t get_first_key(report_keyboard_t* keyboard_report); diff --git a/tmk_core/common/sendchar.h b/tmk_core/common/sendchar.h index 7a64d00c7..bd7b94fd9 100644 --- a/tmk_core/common/sendchar.h +++ b/tmk_core/common/sendchar.h @@ -20,7 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> - #ifdef __cplusplus extern "C" { #endif diff --git a/tmk_core/common/sendchar_null.c b/tmk_core/common/sendchar_null.c index 293330622..f6cab1b9d 100644 --- a/tmk_core/common/sendchar_null.c +++ b/tmk_core/common/sendchar_null.c @@ -16,8 +16,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "sendchar.h" - -int8_t sendchar(uint8_t c) -{ - return 0; -} +int8_t sendchar(uint8_t c) { return 0; } diff --git a/tmk_core/common/sendchar_uart.c b/tmk_core/common/sendchar_uart.c index 0241859eb..2fc48baff 100644 --- a/tmk_core/common/sendchar_uart.c +++ b/tmk_core/common/sendchar_uart.c @@ -17,9 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "uart.h" #include "sendchar.h" - -int8_t sendchar(uint8_t c) -{ +int8_t sendchar(uint8_t c) { uart_putchar(c); return 0; } diff --git a/tmk_core/common/sleep_led.h b/tmk_core/common/sleep_led.h index 6bdcf558a..d160213f1 100644 --- a/tmk_core/common/sleep_led.h +++ b/tmk_core/common/sleep_led.h @@ -1,7 +1,6 @@ #ifndef SLEEP_LED_H #define SLEEP_LED_H - #ifdef SLEEP_LED_ENABLE void sleep_led_init(void); @@ -11,10 +10,10 @@ void sleep_led_toggle(void); #else -#define sleep_led_init() -#define sleep_led_enable() -#define sleep_led_disable() -#define sleep_led_toggle() +# define sleep_led_init() +# define sleep_led_enable() +# define sleep_led_disable() +# define sleep_led_toggle() #endif diff --git a/tmk_core/common/suspend.h b/tmk_core/common/suspend.h index c3e444708..87f5025da 100644 --- a/tmk_core/common/suspend.h +++ b/tmk_core/common/suspend.h @@ -4,7 +4,6 @@ #include <stdint.h> #include <stdbool.h> - void suspend_idle(uint8_t timeout); void suspend_power_down(void); bool suspend_wakeup_condition(void); @@ -12,7 +11,7 @@ void suspend_wakeup_init(void); void suspend_wakeup_init_user(void); void suspend_wakeup_init_kb(void); -void suspend_power_down_user (void); +void suspend_power_down_user(void); void suspend_power_down_kb(void); #endif diff --git a/tmk_core/common/test/eeprom.c b/tmk_core/common/test/eeprom.c index 61cc039ef..44a0bf4d7 100644 --- a/tmk_core/common/test/eeprom.c +++ b/tmk_core/common/test/eeprom.c @@ -21,78 +21,75 @@ static uint8_t buffer[EEPROM_SIZE]; uint8_t eeprom_read_byte(const uint8_t *addr) { - uintptr_t offset = (uintptr_t)addr; - return buffer[offset]; + uintptr_t offset = (uintptr_t)addr; + return buffer[offset]; } void eeprom_write_byte(uint8_t *addr, uint8_t value) { - uintptr_t offset = (uintptr_t)addr; - buffer[offset] = value; + uintptr_t offset = (uintptr_t)addr; + buffer[offset] = value; } uint16_t eeprom_read_word(const uint16_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); } uint32_t eeprom_read_dword(const uint32_t *addr) { - const uint8_t *p = (const uint8_t *)addr; - return eeprom_read_byte(p) | (eeprom_read_byte(p+1) << 8) - | (eeprom_read_byte(p+2) << 16) | (eeprom_read_byte(p+3) << 24); + const uint8_t *p = (const uint8_t *)addr; + return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); } void eeprom_read_block(void *buf, const void *addr, uint32_t len) { - const uint8_t *p = (const uint8_t *)addr; - uint8_t *dest = (uint8_t *)buf; - while (len--) { - *dest++ = eeprom_read_byte(p++); - } + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte(p++); + } } void eeprom_write_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_write_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_write_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } -void eeprom_update_byte(uint8_t *addr, uint8_t value) { - eeprom_write_byte(addr, value); -} +void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); } void eeprom_update_word(uint16_t *addr, uint16_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p, value >> 8); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p, value >> 8); } void eeprom_update_dword(uint32_t *addr, uint32_t value) { - uint8_t *p = (uint8_t *)addr; - eeprom_write_byte(p++, value); - eeprom_write_byte(p++, value >> 8); - eeprom_write_byte(p++, value >> 16); - eeprom_write_byte(p, value >> 24); + uint8_t *p = (uint8_t *)addr; + eeprom_write_byte(p++, value); + eeprom_write_byte(p++, value >> 8); + eeprom_write_byte(p++, value >> 16); + eeprom_write_byte(p, value >> 24); } void eeprom_update_block(const void *buf, void *addr, uint32_t len) { - uint8_t *p = (uint8_t *)addr; - const uint8_t *src = (const uint8_t *)buf; - while (len--) { - eeprom_write_byte(p++, *src++); - } + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte(p++, *src++); + } } diff --git a/tmk_core/common/test/suspend.c b/tmk_core/common/test/suspend.c index 01d1930ea..76b705967 100644 --- a/tmk_core/common/test/suspend.c +++ b/tmk_core/common/test/suspend.c @@ -13,5 +13,3 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ - - diff --git a/tmk_core/common/test/timer.c b/tmk_core/common/test/timer.c index 19e79e1f5..3c786ae29 100644 --- a/tmk_core/common/test/timer.c +++ b/tmk_core/common/test/timer.c @@ -18,9 +18,9 @@ static uint32_t current_time = 0; -void timer_init(void) {current_time = 0;} +void timer_init(void) { current_time = 0; } -void timer_clear(void) {current_time = 0;} +void timer_clear(void) { current_time = 0; } uint16_t timer_read(void) { return current_time & 0xFFFF; } uint32_t timer_read32(void) { return current_time; } @@ -30,6 +30,4 @@ uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), l void set_time(uint32_t t) { current_time = t; } void advance_time(uint32_t ms) { current_time += ms; } -void wait_ms(uint32_t ms) { - advance_time(ms); -}
\ No newline at end of file +void wait_ms(uint32_t ms) { advance_time(ms); }
\ No newline at end of file diff --git a/tmk_core/common/timer.h b/tmk_core/common/timer.h index a8dd85663..853cb9839 100644 --- a/tmk_core/common/timer.h +++ b/tmk_core/common/timer.h @@ -22,16 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> #if defined(__AVR__) -#include "avr/timer_avr.h" +# include "avr/timer_avr.h" #endif - -#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) -#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) -#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) -#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) -#define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b) - +#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) +#define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) +#define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) +#define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) +#define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b) #ifdef __cplusplus extern "C" { @@ -39,23 +37,17 @@ extern "C" { extern volatile uint32_t timer_count; - -void timer_init(void); -void timer_clear(void); +void timer_init(void); +void timer_clear(void); uint16_t timer_read(void); uint32_t timer_read32(void); uint16_t timer_elapsed(uint16_t last); uint32_t timer_elapsed32(uint32_t last); // Utility functions to check if a future time has expired & autmatically handle time wrapping if checked / reset frequently (half of max value) -inline bool timer_expired(uint16_t current, uint16_t last) -{ - return current - last < 0x8000; -} +inline bool timer_expired(uint16_t current, uint16_t last) { return current - last < 0x8000; } -inline bool timer_expired32(uint32_t current, uint32_t future) { - return current - future < 0x80000000; -} +inline bool timer_expired32(uint32_t current, uint32_t future) { return current - future < 0x80000000; } #ifdef __cplusplus } diff --git a/tmk_core/common/uart.c b/tmk_core/common/uart.c index c17649b08..f2e4bc4f3 100644 --- a/tmk_core/common/uart.c +++ b/tmk_core/common/uart.c @@ -3,17 +3,17 @@ /* UART Example for Teensy USB Development Board * http://www.pjrc.com/teensy/ * Copyright (c) 2009 PJRC.COM, LLC - * + * * 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 @@ -26,7 +26,6 @@ // Version 1.0: Initial Release // Version 1.1: Add support for Teensy 2.0, minor optimizations - #include <avr/io.h> #include <avr/interrupt.h> @@ -44,86 +43,81 @@ static volatile uint8_t rx_buffer_head; static volatile uint8_t rx_buffer_tail; // Initialize the UART -void uart_init(uint32_t baud) -{ - cli(); - UBRR0 = (F_CPU / 4 / baud - 1) / 2; - UCSR0A = (1<<U2X0); - UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); - UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); - tx_buffer_head = tx_buffer_tail = 0; - rx_buffer_head = rx_buffer_tail = 0; - sei(); +void uart_init(uint32_t baud) { + cli(); + UBRR0 = (F_CPU / 4 / baud - 1) / 2; + UCSR0A = (1 << U2X0); + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); + tx_buffer_head = tx_buffer_tail = 0; + rx_buffer_head = rx_buffer_tail = 0; + sei(); } // Transmit a byte -void uart_putchar(uint8_t c) -{ - uint8_t i; - - i = tx_buffer_head + 1; - if (i >= TX_BUFFER_SIZE) i = 0; - while (tx_buffer_tail == i) ; // wait until space in buffer - //cli(); - tx_buffer[i] = c; - tx_buffer_head = i; - UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0); - //sei(); +void uart_putchar(uint8_t c) { + uint8_t i; + + i = tx_buffer_head + 1; + if (i >= TX_BUFFER_SIZE) i = 0; + while (tx_buffer_tail == i) + ; // wait until space in buffer + // cli(); + tx_buffer[i] = c; + tx_buffer_head = i; + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0); + // sei(); } // Receive a byte -uint8_t uart_getchar(void) -{ - uint8_t c, i; - - while (rx_buffer_head == rx_buffer_tail) ; // wait for character - i = rx_buffer_tail + 1; - if (i >= RX_BUFFER_SIZE) i = 0; - c = rx_buffer[i]; - rx_buffer_tail = i; - return c; +uint8_t uart_getchar(void) { + uint8_t c, i; + + while (rx_buffer_head == rx_buffer_tail) + ; // wait for character + i = rx_buffer_tail + 1; + if (i >= RX_BUFFER_SIZE) i = 0; + c = rx_buffer[i]; + rx_buffer_tail = i; + return c; } // Return the number of bytes waiting in the receive buffer. // Call this before uart_getchar() to check if it will need // to wait for a byte to arrive. -uint8_t uart_available(void) -{ - uint8_t head, tail; - - head = rx_buffer_head; - tail = rx_buffer_tail; - if (head >= tail) return head - tail; - return RX_BUFFER_SIZE + head - tail; +uint8_t uart_available(void) { + uint8_t head, tail; + + head = rx_buffer_head; + tail = rx_buffer_tail; + if (head >= tail) return head - tail; + return RX_BUFFER_SIZE + head - tail; } // Transmit Interrupt -ISR(USART_UDRE_vect) -{ - uint8_t i; - - if (tx_buffer_head == tx_buffer_tail) { - // buffer is empty, disable transmit interrupt - UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); - } else { - i = tx_buffer_tail + 1; - if (i >= TX_BUFFER_SIZE) i = 0; - UDR0 = tx_buffer[i]; - tx_buffer_tail = i; - } +ISR(USART_UDRE_vect) { + uint8_t i; + + if (tx_buffer_head == tx_buffer_tail) { + // buffer is empty, disable transmit interrupt + UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); + } else { + i = tx_buffer_tail + 1; + if (i >= TX_BUFFER_SIZE) i = 0; + UDR0 = tx_buffer[i]; + tx_buffer_tail = i; + } } // Receive Interrupt -ISR(USART_RX_vect) -{ - uint8_t c, i; - - c = UDR0; - i = rx_buffer_head + 1; - if (i >= RX_BUFFER_SIZE) i = 0; - if (i != rx_buffer_tail) { - rx_buffer[i] = c; - rx_buffer_head = i; - } +ISR(USART_RX_vect) { + uint8_t c, i; + + c = UDR0; + i = rx_buffer_head + 1; + if (i >= RX_BUFFER_SIZE) i = 0; + if (i != rx_buffer_tail) { + rx_buffer[i] = c; + rx_buffer_head = i; + } } - diff --git a/tmk_core/common/uart.h b/tmk_core/common/uart.h index 41136a396..59a1a7cd1 100644 --- a/tmk_core/common/uart.h +++ b/tmk_core/common/uart.h @@ -3,8 +3,8 @@ #include <stdint.h> -void uart_init(uint32_t baud); -void uart_putchar(uint8_t c); +void uart_init(uint32_t baud); +void uart_putchar(uint8_t c); uint8_t uart_getchar(void); uint8_t uart_available(void); diff --git a/tmk_core/common/util.c b/tmk_core/common/util.c index 7e0d54299..f4f018de8 100644 --- a/tmk_core/common/util.c +++ b/tmk_core/common/util.c @@ -18,84 +18,106 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "util.h" // bit population - return number of on-bit -uint8_t bitpop(uint8_t bits) -{ +uint8_t bitpop(uint8_t bits) { uint8_t c; - for (c = 0; bits; c++) - bits &= bits - 1; + for (c = 0; bits; c++) bits &= bits - 1; return c; -/* - const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; - return bit_count[bits>>4] + bit_count[bits&0x0F] -*/ + /* + const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; + return bit_count[bits>>4] + bit_count[bits&0x0F] + */ } -uint8_t bitpop16(uint16_t bits) -{ +uint8_t bitpop16(uint16_t bits) { uint8_t c; - for (c = 0; bits; c++) - bits &= bits - 1; + for (c = 0; bits; c++) bits &= bits - 1; return c; } -uint8_t bitpop32(uint32_t bits) -{ +uint8_t bitpop32(uint32_t bits) { uint8_t c; - for (c = 0; bits; c++) - bits &= bits - 1; + for (c = 0; bits; c++) bits &= bits - 1; return c; } // most significant on-bit - return highest location of on-bit // NOTE: return 0 when bit0 is on or all bits are off -uint8_t biton(uint8_t bits) -{ +uint8_t biton(uint8_t bits) { uint8_t n = 0; - if (bits >> 4) { bits >>= 4; n += 4;} - if (bits >> 2) { bits >>= 2; n += 2;} - if (bits >> 1) { bits >>= 1; n += 1;} + if (bits >> 4) { + bits >>= 4; + n += 4; + } + if (bits >> 2) { + bits >>= 2; + n += 2; + } + if (bits >> 1) { + bits >>= 1; + n += 1; + } return n; } -uint8_t biton16(uint16_t bits) -{ +uint8_t biton16(uint16_t bits) { uint8_t n = 0; - if (bits >> 8) { bits >>= 8; n += 8;} - if (bits >> 4) { bits >>= 4; n += 4;} - if (bits >> 2) { bits >>= 2; n += 2;} - if (bits >> 1) { bits >>= 1; n += 1;} + if (bits >> 8) { + bits >>= 8; + n += 8; + } + if (bits >> 4) { + bits >>= 4; + n += 4; + } + if (bits >> 2) { + bits >>= 2; + n += 2; + } + if (bits >> 1) { + bits >>= 1; + n += 1; + } return n; } -uint8_t biton32(uint32_t bits) -{ +uint8_t biton32(uint32_t bits) { uint8_t n = 0; - if (bits >>16) { bits >>=16; n +=16;} - if (bits >> 8) { bits >>= 8; n += 8;} - if (bits >> 4) { bits >>= 4; n += 4;} - if (bits >> 2) { bits >>= 2; n += 2;} - if (bits >> 1) { bits >>= 1; n += 1;} + if (bits >> 16) { + bits >>= 16; + n += 16; + } + if (bits >> 8) { + bits >>= 8; + n += 8; + } + if (bits >> 4) { + bits >>= 4; + n += 4; + } + if (bits >> 2) { + bits >>= 2; + n += 2; + } + if (bits >> 1) { + bits >>= 1; + n += 1; + } return n; } - - -uint8_t bitrev(uint8_t bits) -{ - bits = (bits & 0x0f)<<4 | (bits & 0xf0)>>4; - bits = (bits & 0b00110011)<<2 | (bits & 0b11001100)>>2; - bits = (bits & 0b01010101)<<1 | (bits & 0b10101010)>>1; +uint8_t bitrev(uint8_t bits) { + bits = (bits & 0x0f) << 4 | (bits & 0xf0) >> 4; + bits = (bits & 0b00110011) << 2 | (bits & 0b11001100) >> 2; + bits = (bits & 0b01010101) << 1 | (bits & 0b10101010) >> 1; return bits; } -uint16_t bitrev16(uint16_t bits) -{ - bits = bitrev(bits & 0x00ff)<<8 | bitrev((bits & 0xff00)>>8); +uint16_t bitrev16(uint16_t bits) { + bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8); return bits; } -uint32_t bitrev32(uint32_t bits) -{ - bits = (uint32_t)bitrev16(bits & 0x0000ffff)<<16 | bitrev16((bits & 0xffff0000)>>16); +uint32_t bitrev32(uint32_t bits) { + bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16); return bits; } diff --git a/tmk_core/common/util.h b/tmk_core/common/util.h index 7451cc084..5706b047d 100644 --- a/tmk_core/common/util.h +++ b/tmk_core/common/util.h @@ -22,12 +22,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. // convert to L string #define LSTR(s) XLSTR(s) -#define XLSTR(s) L ## #s +#define XLSTR(s) L## #s // convert to string #define STR(s) XSTR(s) #define XSTR(s) #s - uint8_t bitpop(uint8_t bits); uint8_t bitpop16(uint16_t bits); uint8_t bitpop32(uint32_t bits); diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h index 9aed372b7..cb1f386a6 100644 --- a/tmk_core/common/wait.h +++ b/tmk_core/common/wait.h @@ -8,22 +8,36 @@ extern "C" { #endif #if defined(__AVR__) -# include <util/delay.h> -# define wait_ms(ms) _delay_ms(ms) -# define wait_us(us) _delay_us(us) +# include <util/delay.h> +# define wait_ms(ms) _delay_ms(ms) +# define wait_us(us) _delay_us(us) #elif defined PROTOCOL_CHIBIOS -# include "ch.h" -# define wait_ms(ms) do { if (ms != 0) { chThdSleepMilliseconds(ms); } else { chThdSleepMicroseconds(1); } } while (0) -# define wait_us(us) do { if (us != 0) { chThdSleepMicroseconds(us); } else { chThdSleepMicroseconds(1); } } while (0) +# include "ch.h" +# define wait_ms(ms) \ + do { \ + if (ms != 0) { \ + chThdSleepMilliseconds(ms); \ + } else { \ + chThdSleepMicroseconds(1); \ + } \ + } while (0) +# define wait_us(us) \ + do { \ + if (us != 0) { \ + chThdSleepMicroseconds(us); \ + } else { \ + chThdSleepMicroseconds(1); \ + } \ + } while (0) #elif defined PROTOCOL_ARM_ATSAM -# include "clks.h" -# define wait_ms(ms) CLK_delay_ms(ms) -# define wait_us(us) CLK_delay_us(us) +# include "clks.h" +# define wait_ms(ms) CLK_delay_ms(ms) +# define wait_us(us) CLK_delay_us(us) #elif defined(__arm__) -# include "wait_api.h" +# include "wait_api.h" #else // Unit tests void wait_ms(uint32_t ms); -#define wait_us(us) wait_ms(us / 1000) +# define wait_us(us) wait_ms(us / 1000) #endif #ifdef __cplusplus diff --git a/tmk_core/protocol/adb.c b/tmk_core/protocol/adb.c index 5c6c99b4f..a23c91961 100644 --- a/tmk_core/protocol/adb.c +++ b/tmk_core/protocol/adb.c @@ -42,11 +42,10 @@ POSSIBILITY OF SUCH DAMAGE. #include <avr/interrupt.h> #include "adb.h" - // GCC doesn't inline functions normally -#define data_lo() (ADB_DDR |= (1<<ADB_DATA_BIT)) -#define data_hi() (ADB_DDR &= ~(1<<ADB_DATA_BIT)) -#define data_in() (ADB_PIN & (1<<ADB_DATA_BIT)) +#define data_lo() (ADB_DDR |= (1 << ADB_DATA_BIT)) +#define data_hi() (ADB_DDR &= ~(1 << ADB_DATA_BIT)) +#define data_in() (ADB_PIN & (1 << ADB_DATA_BIT)) #ifdef ADB_PSW_BIT static inline void psw_lo(void); @@ -54,18 +53,16 @@ static inline void psw_hi(void); static inline bool psw_in(void); #endif -static inline void attention(void); -static inline void place_bit0(void); -static inline void place_bit1(void); -static inline void send_byte(uint8_t data); +static inline void attention(void); +static inline void place_bit0(void); +static inline void place_bit1(void); +static inline void send_byte(uint8_t data); static inline uint16_t wait_data_lo(uint16_t us); static inline uint16_t wait_data_hi(uint16_t us); static inline uint16_t adb_host_dev_recv(uint8_t device); - -void adb_host_init(void) -{ - ADB_PORT &= ~(1<<ADB_DATA_BIT); +void adb_host_init(void) { + ADB_PORT &= ~(1 << ADB_DATA_BIT); data_hi(); #ifdef ADB_PSW_BIT psw_hi(); @@ -73,10 +70,7 @@ void adb_host_init(void) } #ifdef ADB_PSW_BIT -bool adb_host_psw(void) -{ - return psw_in(); -} +bool adb_host_psw(void) { return psw_in(); } #endif /* @@ -105,11 +99,11 @@ bool adb_host_psw(void) // bit0: // 70us bit cell: // ____________~~~~~~ -// 42-49 21-28 +// 42-49 21-28 // // 130us bit cell: // ____________~~~~~~ -// 78-91 39-52 +// 78-91 39-52 // // bit1: // 70us bit cell: @@ -122,66 +116,50 @@ bool adb_host_psw(void) // // [from Apple IIgs Hardware Reference Second Edition] -enum { - ADDR_KEYB = 0x20, - ADDR_MOUSE = 0x30 -}; +enum { ADDR_KEYB = 0x20, ADDR_MOUSE = 0x30 }; -uint16_t adb_host_kbd_recv(void) -{ - return adb_host_dev_recv(ADDR_KEYB); -} +uint16_t adb_host_kbd_recv(void) { return adb_host_dev_recv(ADDR_KEYB); } #ifdef ADB_MOUSE_ENABLE -void adb_mouse_init(void) { - return; -} +void adb_mouse_init(void) { return; } -uint16_t adb_host_mouse_recv(void) -{ - return adb_host_dev_recv(ADDR_MOUSE); -} +uint16_t adb_host_mouse_recv(void) { return adb_host_dev_recv(ADDR_MOUSE); } #endif -static inline uint16_t adb_host_dev_recv(uint8_t device) -{ +static inline uint16_t adb_host_dev_recv(uint8_t device) { uint16_t data = 0; cli(); attention(); - send_byte(device|0x0C); // Addr:Keyboard(0010)/Mouse(0011), Cmd:Talk(11), Register0(00) - place_bit0(); // Stopbit(0) - if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored + send_byte(device | 0x0C); // Addr:Keyboard(0010)/Mouse(0011), Cmd:Talk(11), Register0(00) + place_bit0(); // Stopbit(0) + if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored sei(); - return -30; // something wrong + return -30; // something wrong } - if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) + if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) sei(); - return 0; // No data to send + return 0; // No data to send } - - uint8_t n = 17; // start bit + 16 data bits + + uint8_t n = 17; // start bit + 16 data bits do { - uint8_t lo = (uint8_t) wait_data_hi(130); - if (!lo) - goto error; - - uint8_t hi = (uint8_t) wait_data_lo(lo); - if (!hi) - goto error; - + uint8_t lo = (uint8_t)wait_data_hi(130); + if (!lo) goto error; + + uint8_t hi = (uint8_t)wait_data_lo(lo); + if (!hi) goto error; + hi = lo - hi; lo = 130 - lo; - + data <<= 1; if (lo < hi) { data |= 1; - } - else if (n == 17) { + } else if (n == 17) { sei(); return -20; } - } - while ( --n ); + } while (--n); // Stop bit can't be checked normally since it could have service request lenghtening // and its high state never goes low. @@ -197,76 +175,66 @@ error: return -n; } -void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) -{ +void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) { cli(); attention(); send_byte(cmd); - place_bit0(); // Stopbit(0) - _delay_us(200); // Tlt/Stop to Start - place_bit1(); // Startbit(1) - send_byte(data_h); + place_bit0(); // Stopbit(0) + _delay_us(200); // Tlt/Stop to Start + place_bit1(); // Startbit(1) + send_byte(data_h); send_byte(data_l); - place_bit0(); // Stopbit(0); + place_bit0(); // Stopbit(0); sei(); } // send state of LEDs -void adb_host_kbd_led(uint8_t led) -{ +void adb_host_kbd_led(uint8_t led) { // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10) // send upper byte (not used) // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0: - adb_host_listen(0x2A,0,led&0x07); + adb_host_listen(0x2A, 0, led & 0x07); } - #ifdef ADB_PSW_BIT -static inline void psw_lo() -{ - ADB_DDR |= (1<<ADB_PSW_BIT); - ADB_PORT &= ~(1<<ADB_PSW_BIT); +static inline void psw_lo() { + ADB_DDR |= (1 << ADB_PSW_BIT); + ADB_PORT &= ~(1 << ADB_PSW_BIT); } -static inline void psw_hi() -{ - ADB_PORT |= (1<<ADB_PSW_BIT); - ADB_DDR &= ~(1<<ADB_PSW_BIT); +static inline void psw_hi() { + ADB_PORT |= (1 << ADB_PSW_BIT); + ADB_DDR &= ~(1 << ADB_PSW_BIT); } -static inline bool psw_in() -{ - ADB_PORT |= (1<<ADB_PSW_BIT); - ADB_DDR &= ~(1<<ADB_PSW_BIT); - return ADB_PIN&(1<<ADB_PSW_BIT); +static inline bool psw_in() { + ADB_PORT |= (1 << ADB_PSW_BIT); + ADB_DDR &= ~(1 << ADB_PSW_BIT); + return ADB_PIN & (1 << ADB_PSW_BIT); } #endif -static inline void attention(void) -{ +static inline void attention(void) { data_lo(); - _delay_us(800-35); // bit1 holds lo for 35 more + _delay_us(800 - 35); // bit1 holds lo for 35 more place_bit1(); } -static inline void place_bit0(void) -{ +static inline void place_bit0(void) { data_lo(); _delay_us(65); data_hi(); _delay_us(35); } -static inline void place_bit1(void) -{ +static inline void place_bit1(void) { data_lo(); _delay_us(35); data_hi(); _delay_us(65); } -static inline void send_byte(uint8_t data) -{ +static inline void send_byte(uint8_t data) { for (int i = 0; i < 8; i++) { - if (data&(0x80>>i)) + if (data & (0x80 >> i)) place_bit1(); else place_bit0(); @@ -275,29 +243,22 @@ static inline void send_byte(uint8_t data) // These are carefully coded to take 6 cycles of overhead. // inline asm approach became too convoluted -static inline uint16_t wait_data_lo(uint16_t us) -{ +static inline uint16_t wait_data_lo(uint16_t us) { do { - if ( !data_in() ) - break; + if (!data_in()) break; _delay_us(1 - (6 * 1000000.0 / F_CPU)); - } - while ( --us ); + } while (--us); return us; } -static inline uint16_t wait_data_hi(uint16_t us) -{ +static inline uint16_t wait_data_hi(uint16_t us) { do { - if ( data_in() ) - break; + if (data_in()) break; _delay_us(1 - (6 * 1000000.0 / F_CPU)); - } - while ( --us ); + } while (--us); return us; } - /* ADB Protocol ============ @@ -375,7 +336,7 @@ Commands A A A A 1 1 R R Talk(read from a device) The command to read keycodes from keyboard is 0x2C which - consist of keyboard address 2 and Talk against register 0. + consist of keyboard address 2 and Talk against register 0. Address: 2: keyboard @@ -457,7 +418,7 @@ Keyboard Data(Register0) Keyboard LEDs & state of keys(Register2) This register hold current state of three LEDs and nine keys. The state of LEDs can be changed by sending Listen command. - + 1514 . . . . . . 7 6 5 . 3 2 1 0 | | | | | | | | | | | | | | | +- LED1(NumLock) | | | | | | | | | | | | | | +--- LED2(CapsLock) diff --git a/tmk_core/protocol/adb.h b/tmk_core/protocol/adb.h index b4b3633cf..7d37485fc 100644 --- a/tmk_core/protocol/adb.h +++ b/tmk_core/protocol/adb.h @@ -41,16 +41,12 @@ POSSIBILITY OF SUCH DAMAGE. #include <stdint.h> #include <stdbool.h> -#if !(defined(ADB_PORT) && \ - defined(ADB_PIN) && \ - defined(ADB_DDR) && \ - defined(ADB_DATA_BIT)) -# error "ADB port setting is required in config.h" +#if !(defined(ADB_PORT) && defined(ADB_PIN) && defined(ADB_DDR) && defined(ADB_DATA_BIT)) +# error "ADB port setting is required in config.h" #endif -#define ADB_POWER 0x7F -#define ADB_CAPS 0x39 - +#define ADB_POWER 0x7F +#define ADB_CAPS 0x39 // ADB host void adb_host_init(void); @@ -62,5 +58,4 @@ void adb_host_kbd_led(uint8_t led); void adb_mouse_task(void); void adb_mouse_init(void); - #endif diff --git a/tmk_core/protocol/arm_atsam/adc.c b/tmk_core/protocol/arm_atsam/adc.c index cb5c349b7..1ef1b11d5 100644 --- a/tmk_core/protocol/arm_atsam/adc.c +++ b/tmk_core/protocol/arm_atsam/adc.c @@ -24,76 +24,92 @@ uint16_t v_con_2; uint16_t v_con_1_boot; uint16_t v_con_2_boot; -void ADC0_clock_init(void) -{ +void ADC0_clock_init(void) { DBGC(DC_ADC0_CLOCK_INIT_BEGIN); - MCLK->APBDMASK.bit.ADC0_ = 1; //ADC0 Clock Enable + MCLK->APBDMASK.bit.ADC0_ = 1; // ADC0 Clock Enable - GCLK->PCHCTRL[ADC0_GCLK_ID].bit.GEN = GEN_OSC0; //Select generator clock - GCLK->PCHCTRL[ADC0_GCLK_ID].bit.CHEN = 1; //Enable peripheral clock + GCLK->PCHCTRL[ADC0_GCLK_ID].bit.GEN = GEN_OSC0; // Select generator clock + GCLK->PCHCTRL[ADC0_GCLK_ID].bit.CHEN = 1; // Enable peripheral clock DBGC(DC_ADC0_CLOCK_INIT_COMPLETE); } -void ADC0_init(void) -{ +void ADC0_init(void) { DBGC(DC_ADC0_INIT_BEGIN); - //MCU - PORT->Group[1].DIRCLR.reg = 1 << 0; //PB00 as input 5V - PORT->Group[1].DIRCLR.reg = 1 << 1; //PB01 as input CON2 - PORT->Group[1].DIRCLR.reg = 1 << 2; //PB02 as input CON1 - PORT->Group[1].PMUX[0].bit.PMUXE = 1; //PB00 mux select B ADC 5V - PORT->Group[1].PMUX[0].bit.PMUXO = 1; //PB01 mux select B ADC CON2 - PORT->Group[1].PMUX[1].bit.PMUXE = 1; //PB02 mux select B ADC CON1 - PORT->Group[1].PINCFG[0].bit.PMUXEN = 1; //PB01 mux ADC Enable 5V - PORT->Group[1].PINCFG[1].bit.PMUXEN = 1; //PB01 mux ADC Enable CON2 - PORT->Group[1].PINCFG[2].bit.PMUXEN = 1; //PB02 mux ADC Enable CON1 - - //ADC + // MCU + PORT->Group[1].DIRCLR.reg = 1 << 0; // PB00 as input 5V + PORT->Group[1].DIRCLR.reg = 1 << 1; // PB01 as input CON2 + PORT->Group[1].DIRCLR.reg = 1 << 2; // PB02 as input CON1 + PORT->Group[1].PMUX[0].bit.PMUXE = 1; // PB00 mux select B ADC 5V + PORT->Group[1].PMUX[0].bit.PMUXO = 1; // PB01 mux select B ADC CON2 + PORT->Group[1].PMUX[1].bit.PMUXE = 1; // PB02 mux select B ADC CON1 + PORT->Group[1].PINCFG[0].bit.PMUXEN = 1; // PB01 mux ADC Enable 5V + PORT->Group[1].PINCFG[1].bit.PMUXEN = 1; // PB01 mux ADC Enable CON2 + PORT->Group[1].PINCFG[2].bit.PMUXEN = 1; // PB02 mux ADC Enable CON1 + + // ADC ADC0->CTRLA.bit.SWRST = 1; - while (ADC0->SYNCBUSY.bit.SWRST) { DBGC(DC_ADC0_SWRST_SYNCING_1); } - while (ADC0->CTRLA.bit.SWRST) { DBGC(DC_ADC0_SWRST_SYNCING_2); } - - //Clock divide + while (ADC0->SYNCBUSY.bit.SWRST) { + DBGC(DC_ADC0_SWRST_SYNCING_1); + } + while (ADC0->CTRLA.bit.SWRST) { + DBGC(DC_ADC0_SWRST_SYNCING_2); + } + + // Clock divide ADC0->CTRLA.bit.PRESCALER = ADC_CTRLA_PRESCALER_DIV2_Val; - //Averaging + // Averaging ADC0->AVGCTRL.bit.SAMPLENUM = ADC_AVGCTRL_SAMPLENUM_4_Val; - while (ADC0->SYNCBUSY.bit.AVGCTRL) { DBGC(DC_ADC0_AVGCTRL_SYNCING_1); } - if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_1_Val) ADC0->AVGCTRL.bit.ADJRES = 0; - else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_2_Val) ADC0->AVGCTRL.bit.ADJRES = 1; - else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_4_Val) ADC0->AVGCTRL.bit.ADJRES = 2; - else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_8_Val) ADC0->AVGCTRL.bit.ADJRES = 3; - else ADC0->AVGCTRL.bit.ADJRES = 4; - while (ADC0->SYNCBUSY.bit.AVGCTRL) { DBGC(DC_ADC0_AVGCTRL_SYNCING_2); } - - //Settling - ADC0->SAMPCTRL.bit.SAMPLEN = 45; //Sampling Time Length: 1-63, 1 ADC CLK per - while (ADC0->SYNCBUSY.bit.SAMPCTRL) { DBGC(DC_ADC0_SAMPCTRL_SYNCING_1); } - - //Load factory calibration data - ADC0->CALIB.bit.BIASCOMP = ((*(uint32_t *)ADC0_FUSES_BIASCOMP_ADDR) & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; - ADC0->CALIB.bit.BIASR2R = ((*(uint32_t *)ADC0_FUSES_BIASR2R_ADDR) & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; + while (ADC0->SYNCBUSY.bit.AVGCTRL) { + DBGC(DC_ADC0_AVGCTRL_SYNCING_1); + } + if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_1_Val) + ADC0->AVGCTRL.bit.ADJRES = 0; + else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_2_Val) + ADC0->AVGCTRL.bit.ADJRES = 1; + else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_4_Val) + ADC0->AVGCTRL.bit.ADJRES = 2; + else if (ADC0->AVGCTRL.bit.SAMPLENUM == ADC_AVGCTRL_SAMPLENUM_8_Val) + ADC0->AVGCTRL.bit.ADJRES = 3; + else + ADC0->AVGCTRL.bit.ADJRES = 4; + while (ADC0->SYNCBUSY.bit.AVGCTRL) { + DBGC(DC_ADC0_AVGCTRL_SYNCING_2); + } + + // Settling + ADC0->SAMPCTRL.bit.SAMPLEN = 45; // Sampling Time Length: 1-63, 1 ADC CLK per + while (ADC0->SYNCBUSY.bit.SAMPCTRL) { + DBGC(DC_ADC0_SAMPCTRL_SYNCING_1); + } + + // Load factory calibration data + ADC0->CALIB.bit.BIASCOMP = ((*(uint32_t *)ADC0_FUSES_BIASCOMP_ADDR) & ADC0_FUSES_BIASCOMP_Msk) >> ADC0_FUSES_BIASCOMP_Pos; + ADC0->CALIB.bit.BIASR2R = ((*(uint32_t *)ADC0_FUSES_BIASR2R_ADDR) & ADC0_FUSES_BIASR2R_Msk) >> ADC0_FUSES_BIASR2R_Pos; ADC0->CALIB.bit.BIASREFBUF = ((*(uint32_t *)ADC0_FUSES_BIASREFBUF_ADDR) & ADC0_FUSES_BIASREFBUF_Msk) >> ADC0_FUSES_BIASREFBUF_Pos; - //Enable + // Enable ADC0->CTRLA.bit.ENABLE = 1; - while (ADC0->SYNCBUSY.bit.ENABLE) { DBGC(DC_ADC0_ENABLE_SYNCING_1); } + while (ADC0->SYNCBUSY.bit.ENABLE) { + DBGC(DC_ADC0_ENABLE_SYNCING_1); + } DBGC(DC_ADC0_INIT_COMPLETE); } -uint16_t adc_get(uint8_t muxpos) -{ +uint16_t adc_get(uint8_t muxpos) { ADC0->INPUTCTRL.bit.MUXPOS = muxpos; - while (ADC0->SYNCBUSY.bit.INPUTCTRL) {} + while (ADC0->SYNCBUSY.bit.INPUTCTRL) { + } ADC0->SWTRIG.bit.START = 1; - while (ADC0->SYNCBUSY.bit.SWTRIG) {} - while (!ADC0->INTFLAG.bit.RESRDY) {} + while (ADC0->SYNCBUSY.bit.SWTRIG) { + } + while (!ADC0->INTFLAG.bit.RESRDY) { + } return ADC0->RESULT.reg; } - diff --git a/tmk_core/protocol/arm_atsam/adc.h b/tmk_core/protocol/arm_atsam/adc.h index 5a90ece3f..9ab653e5a 100644 --- a/tmk_core/protocol/arm_atsam/adc.h +++ b/tmk_core/protocol/arm_atsam/adc.h @@ -18,11 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef _ADC_H_ #define _ADC_H_ -#define ADC_5V_START_LEVEL 2365 +#define ADC_5V_START_LEVEL 2365 -#define ADC_5V ADC_INPUTCTRL_MUXPOS_AIN12_Val -#define ADC_CON1 ADC_INPUTCTRL_MUXPOS_AIN14_Val -#define ADC_CON2 ADC_INPUTCTRL_MUXPOS_AIN13_Val +#define ADC_5V ADC_INPUTCTRL_MUXPOS_AIN12_Val +#define ADC_CON1 ADC_INPUTCTRL_MUXPOS_AIN14_Val +#define ADC_CON2 ADC_INPUTCTRL_MUXPOS_AIN13_Val extern uint16_t v_5v; extern uint16_t v_5v_avg; @@ -34,4 +34,4 @@ extern uint16_t v_con_2_boot; void ADC0_clock_init(void); void ADC0_init(void); -#endif //_ADC_H_ +#endif //_ADC_H_ diff --git a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h index 88109186a..8cb00b872 100644 --- a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h +++ b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h @@ -33,17 +33,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef MD_BOOTLOADER -#include "main_arm_atsam.h" -#ifdef RGB_MATRIX_ENABLE -#include "led_matrix.h" -#include "rgb_matrix.h" -#endif -#include "issi3733_driver.h" -#include "./usb/compiler.h" -#include "./usb/udc.h" -#include "./usb/udi_cdc.h" - -#endif //MD_BOOTLOADER - -#endif //_ARM_ATSAM_PROTOCOL_H_ - +# include "main_arm_atsam.h" +# ifdef RGB_MATRIX_ENABLE +# include "led_matrix.h" +# include "rgb_matrix.h" +# endif +# include "issi3733_driver.h" +# include "./usb/compiler.h" +# include "./usb/udc.h" +# include "./usb/udi_cdc.h" + +#endif // MD_BOOTLOADER + +#endif //_ARM_ATSAM_PROTOCOL_H_ diff --git a/tmk_core/protocol/arm_atsam/clks.c b/tmk_core/protocol/arm_atsam/clks.c index 1ff318e59..84ed6d83a 100644 --- a/tmk_core/protocol/arm_atsam/clks.c +++ b/tmk_core/protocol/arm_atsam/clks.c @@ -19,83 +19,105 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <string.h> -volatile clk_t system_clks; +volatile clk_t system_clks; volatile uint64_t ms_clk; -uint32_t usec_delay_mult; -#define USEC_DELAY_LOOP_CYCLES 3 //Sum of instruction cycles in us delay loop +uint32_t usec_delay_mult; +#define USEC_DELAY_LOOP_CYCLES 3 // Sum of instruction cycles in us delay loop -const uint32_t sercom_apbbase[] = {(uint32_t)SERCOM0,(uint32_t)SERCOM1,(uint32_t)SERCOM2,(uint32_t)SERCOM3,(uint32_t)SERCOM4,(uint32_t)SERCOM5}; -const uint8_t sercom_pchan[] = {7, 8, 23, 24, 34, 35}; +const uint32_t sercom_apbbase[] = {(uint32_t)SERCOM0, (uint32_t)SERCOM1, (uint32_t)SERCOM2, (uint32_t)SERCOM3, (uint32_t)SERCOM4, (uint32_t)SERCOM5}; +const uint8_t sercom_pchan[] = {7, 8, 23, 24, 34, 35}; -#define USE_DPLL_IND 0 -#define USE_DPLL_DEF GCLK_SOURCE_DPLL0 +#define USE_DPLL_IND 0 +#define USE_DPLL_DEF GCLK_SOURCE_DPLL0 -void CLK_oscctrl_init(void) -{ +void CLK_oscctrl_init(void) { Oscctrl *posctrl = OSCCTRL; - Gclk *pgclk = GCLK; + Gclk * pgclk = GCLK; DBGC(DC_CLK_OSC_INIT_BEGIN); - //default setup on por - system_clks.freq_dfll = FREQ_DFLL_DEFAULT; + // default setup on por + system_clks.freq_dfll = FREQ_DFLL_DEFAULT; system_clks.freq_gclk[0] = system_clks.freq_dfll; - //configure and startup 16MHz xosc0 - posctrl->XOSCCTRL[0].bit.ENABLE = 0; - posctrl->XOSCCTRL[0].bit.STARTUP = 0xD; - posctrl->XOSCCTRL[0].bit.ENALC = 1; - posctrl->XOSCCTRL[0].bit.IMULT = 5; - posctrl->XOSCCTRL[0].bit.IPTAT = 3; + // configure and startup 16MHz xosc0 + posctrl->XOSCCTRL[0].bit.ENABLE = 0; + posctrl->XOSCCTRL[0].bit.STARTUP = 0xD; + posctrl->XOSCCTRL[0].bit.ENALC = 1; + posctrl->XOSCCTRL[0].bit.IMULT = 5; + posctrl->XOSCCTRL[0].bit.IPTAT = 3; posctrl->XOSCCTRL[0].bit.ONDEMAND = 0; - posctrl->XOSCCTRL[0].bit.XTALEN = 1; - posctrl->XOSCCTRL[0].bit.ENABLE = 1; - while (posctrl->STATUS.bit.XOSCRDY0 == 0) { DBGC(DC_CLK_OSC_INIT_XOSC0_SYNC); } + posctrl->XOSCCTRL[0].bit.XTALEN = 1; + posctrl->XOSCCTRL[0].bit.ENABLE = 1; + while (posctrl->STATUS.bit.XOSCRDY0 == 0) { + DBGC(DC_CLK_OSC_INIT_XOSC0_SYNC); + } system_clks.freq_xosc0 = FREQ_XOSC0; - //configure and startup DPLL + // configure and startup DPLL posctrl->Dpll[USE_DPLL_IND].DPLLCTRLA.bit.ENABLE = 0; - while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.ENABLE) { DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_DISABLE); } - posctrl->Dpll[USE_DPLL_IND].DPLLCTRLB.bit.REFCLK = 2; //select XOSC0 (16MHz) - posctrl->Dpll[USE_DPLL_IND].DPLLCTRLB.bit.DIV = 7; //16 MHz / (2 * (7 + 1)) = 1 MHz - posctrl->Dpll[USE_DPLL_IND].DPLLRATIO.bit.LDR = PLL_RATIO; //1 MHz * (PLL_RATIO(47) + 1) = 48MHz - while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.DPLLRATIO) { DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_RATIO); } + while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.ENABLE) { + DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_DISABLE); + } + posctrl->Dpll[USE_DPLL_IND].DPLLCTRLB.bit.REFCLK = 2; // select XOSC0 (16MHz) + posctrl->Dpll[USE_DPLL_IND].DPLLCTRLB.bit.DIV = 7; // 16 MHz / (2 * (7 + 1)) = 1 MHz + posctrl->Dpll[USE_DPLL_IND].DPLLRATIO.bit.LDR = PLL_RATIO; // 1 MHz * (PLL_RATIO(47) + 1) = 48MHz + while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.DPLLRATIO) { + DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_RATIO); + } posctrl->Dpll[USE_DPLL_IND].DPLLCTRLA.bit.ONDEMAND = 0; - posctrl->Dpll[USE_DPLL_IND].DPLLCTRLA.bit.ENABLE = 1; - while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.ENABLE) { DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_ENABLE); } - while (posctrl->Dpll[USE_DPLL_IND].DPLLSTATUS.bit.LOCK == 0) { DBGC(DC_CLK_OSC_INIT_DPLL_WAIT_LOCK); } - while (posctrl->Dpll[USE_DPLL_IND].DPLLSTATUS.bit.CLKRDY == 0) { DBGC(DC_CLK_OSC_INIT_DPLL_WAIT_CLKRDY); } + posctrl->Dpll[USE_DPLL_IND].DPLLCTRLA.bit.ENABLE = 1; + while (posctrl->Dpll[USE_DPLL_IND].DPLLSYNCBUSY.bit.ENABLE) { + DBGC(DC_CLK_OSC_INIT_DPLL_SYNC_ENABLE); + } + while (posctrl->Dpll[USE_DPLL_IND].DPLLSTATUS.bit.LOCK == 0) { + DBGC(DC_CLK_OSC_INIT_DPLL_WAIT_LOCK); + } + while (posctrl->Dpll[USE_DPLL_IND].DPLLSTATUS.bit.CLKRDY == 0) { + DBGC(DC_CLK_OSC_INIT_DPLL_WAIT_CLKRDY); + } system_clks.freq_dpll[0] = (system_clks.freq_xosc0 / 2 / (posctrl->Dpll[USE_DPLL_IND].DPLLCTRLB.bit.DIV + 1)) * (posctrl->Dpll[USE_DPLL_IND].DPLLRATIO.bit.LDR + 1); - //change gclk0 to DPLL + // change gclk0 to DPLL pgclk->GENCTRL[GEN_DPLL0].bit.SRC = USE_DPLL_DEF; - while (pgclk->SYNCBUSY.bit.GENCTRL0) { DBGC(DC_CLK_OSC_INIT_GCLK_SYNC_GENCTRL0); } + while (pgclk->SYNCBUSY.bit.GENCTRL0) { + DBGC(DC_CLK_OSC_INIT_GCLK_SYNC_GENCTRL0); + } system_clks.freq_gclk[0] = system_clks.freq_dpll[0]; usec_delay_mult = system_clks.freq_gclk[0] / (USEC_DELAY_LOOP_CYCLES * 1000000); - if (usec_delay_mult < 1) usec_delay_mult = 1; //Never allow a multiplier of zero + if (usec_delay_mult < 1) usec_delay_mult = 1; // Never allow a multiplier of zero DBGC(DC_CLK_OSC_INIT_COMPLETE); } -//configure for 1MHz (1 usec timebase) -//call CLK_set_gclk_freq(GEN_TC45, FREQ_TC45_DEFAULT); -uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq) -{ +// configure for 1MHz (1 usec timebase) +// call CLK_set_gclk_freq(GEN_TC45, FREQ_TC45_DEFAULT); +uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq) { Gclk *pgclk = GCLK; DBGC(DC_CLK_SET_GCLK_FREQ_BEGIN); - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_1); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_1); + } pgclk->GENCTRL[gclkn].bit.SRC = USE_DPLL_DEF; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_2); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_2); + } pgclk->GENCTRL[gclkn].bit.DIV = (uint8_t)(system_clks.freq_dpll[0] / freq); - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_3); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_3); + } pgclk->GENCTRL[gclkn].bit.DIVSEL = 0; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_4); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_4); + } pgclk->GENCTRL[gclkn].bit.GENEN = 1; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_5); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_SET_GCLK_FREQ_SYNC_5); + } system_clks.freq_gclk[gclkn] = system_clks.freq_dpll[0] / pgclk->GENCTRL[gclkn].bit.DIV; DBGC(DC_CLK_SET_GCLK_FREQ_COMPLETE); @@ -103,29 +125,37 @@ uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq) return system_clks.freq_gclk[gclkn]; } -void CLK_init_osc(void) -{ +void CLK_init_osc(void) { uint8_t gclkn = GEN_OSC0; - Gclk *pgclk = GCLK; + Gclk * pgclk = GCLK; DBGC(DC_CLK_INIT_OSC_BEGIN); - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_INIT_OSC_SYNC_1); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_INIT_OSC_SYNC_1); + } pgclk->GENCTRL[gclkn].bit.SRC = GCLK_SOURCE_XOSC0; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_INIT_OSC_SYNC_2); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_INIT_OSC_SYNC_2); + } pgclk->GENCTRL[gclkn].bit.DIV = 1; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_INIT_OSC_SYNC_3); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_INIT_OSC_SYNC_3); + } pgclk->GENCTRL[gclkn].bit.DIVSEL = 0; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_INIT_OSC_SYNC_4); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_INIT_OSC_SYNC_4); + } pgclk->GENCTRL[gclkn].bit.GENEN = 1; - while (pgclk->SYNCBUSY.vec.GENCTRL) { DBGC(DC_CLK_INIT_OSC_SYNC_5); } + while (pgclk->SYNCBUSY.vec.GENCTRL) { + DBGC(DC_CLK_INIT_OSC_SYNC_5); + } system_clks.freq_gclk[gclkn] = system_clks.freq_xosc0; DBGC(DC_CLK_INIT_OSC_COMPLETE); } -void CLK_reset_time(void) -{ +void CLK_reset_time(void) { Tc *ptc4 = TC4; Tc *ptc0 = TC0; @@ -133,72 +163,85 @@ void CLK_reset_time(void) DBGC(DC_CLK_RESET_TIME_BEGIN); - //stop counters + // stop counters ptc4->COUNT16.CTRLA.bit.ENABLE = 0; - while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) {} + while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) { + } ptc0->COUNT32.CTRLA.bit.ENABLE = 0; - while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) {} - //zero counters + while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) { + } + // zero counters ptc4->COUNT16.COUNT.reg = 0; - while (ptc4->COUNT16.SYNCBUSY.bit.COUNT) {} + while (ptc4->COUNT16.SYNCBUSY.bit.COUNT) { + } ptc0->COUNT32.COUNT.reg = 0; - while (ptc0->COUNT32.SYNCBUSY.bit.COUNT) {} - //start counters + while (ptc0->COUNT32.SYNCBUSY.bit.COUNT) { + } + // start counters ptc0->COUNT32.CTRLA.bit.ENABLE = 1; - while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) {} + while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) { + } ptc4->COUNT16.CTRLA.bit.ENABLE = 1; - while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) {} + while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) { + } DBGC(DC_CLK_RESET_TIME_COMPLETE); } -void TC4_Handler() -{ - if (TC4->COUNT16.INTFLAG.bit.MC0) - { +void TC4_Handler() { + if (TC4->COUNT16.INTFLAG.bit.MC0) { TC4->COUNT16.INTFLAG.reg = TC_INTENCLR_MC0; ms_clk++; } } -uint32_t CLK_enable_timebase(void) -{ - Gclk *pgclk = GCLK; - Mclk *pmclk = MCLK; - Tc *ptc4 = TC4; - Tc *ptc0 = TC0; +uint32_t CLK_enable_timebase(void) { + Gclk * pgclk = GCLK; + Mclk * pmclk = MCLK; + Tc * ptc4 = TC4; + Tc * ptc0 = TC0; Evsys *pevsys = EVSYS; DBGC(DC_CLK_ENABLE_TIMEBASE_BEGIN); - //gclk2 highspeed time base + // gclk2 highspeed time base CLK_set_gclk_freq(GEN_TC45, FREQ_TC45_DEFAULT); CLK_init_osc(); - //unmask TC4, sourcegclk2 to TC4 - pmclk->APBCMASK.bit.TC4_ = 1; - pgclk->PCHCTRL[TC4_GCLK_ID].bit.GEN = GEN_TC45; + // unmask TC4, sourcegclk2 to TC4 + pmclk->APBCMASK.bit.TC4_ = 1; + pgclk->PCHCTRL[TC4_GCLK_ID].bit.GEN = GEN_TC45; pgclk->PCHCTRL[TC4_GCLK_ID].bit.CHEN = 1; - //configure TC4 + // configure TC4 DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_BEGIN); ptc4->COUNT16.CTRLA.bit.ENABLE = 0; - while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_DISABLE); } + while (ptc4->COUNT16.SYNCBUSY.bit.ENABLE) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_DISABLE); + } ptc4->COUNT16.CTRLA.bit.SWRST = 1; - while (ptc4->COUNT16.SYNCBUSY.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_SWRST_1); } - while (ptc4->COUNT16.CTRLA.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_SWRST_2); } + while (ptc4->COUNT16.SYNCBUSY.bit.SWRST) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_SWRST_1); + } + while (ptc4->COUNT16.CTRLA.bit.SWRST) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_SWRST_2); + } - //CTRLA defaults - //CTRLB as default, counting up + // CTRLA defaults + // CTRLB as default, counting up ptc4->COUNT16.CTRLBCLR.reg = 5; - while (ptc4->COUNT16.SYNCBUSY.bit.CTRLB) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_CLTRB); } + while (ptc4->COUNT16.SYNCBUSY.bit.CTRLB) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_CLTRB); + } ptc4->COUNT16.CC[0].reg = 999; - while (ptc4->COUNT16.SYNCBUSY.bit.CC0) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_CC0); } - //ptc4->COUNT16.DBGCTRL.bit.DBGRUN = 1; + while (ptc4->COUNT16.SYNCBUSY.bit.CC0) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_SYNC_CC0); + } + // ptc4->COUNT16.DBGCTRL.bit.DBGRUN = 1; - //wave mode - ptc4->COUNT16.WAVE.bit.WAVEGEN = 1; //MFRQ match frequency mode, toggle each CC match - //generate event for next stage + // wave mode + ptc4->COUNT16.WAVE.bit.WAVEGEN = 1; // MFRQ match frequency mode, toggle each CC match + // generate event for next stage ptc4->COUNT16.EVCTRL.bit.MCEO0 = 1; NVIC_EnableIRQ(TC4_IRQn); @@ -206,39 +249,45 @@ uint32_t CLK_enable_timebase(void) DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_COMPLETE); - //unmask TC0,1, sourcegclk2 to TC0,1 - pmclk->APBAMASK.bit.TC0_ = 1; - pgclk->PCHCTRL[TC0_GCLK_ID].bit.GEN = GEN_TC45; + // unmask TC0,1, sourcegclk2 to TC0,1 + pmclk->APBAMASK.bit.TC0_ = 1; + pgclk->PCHCTRL[TC0_GCLK_ID].bit.GEN = GEN_TC45; pgclk->PCHCTRL[TC0_GCLK_ID].bit.CHEN = 1; - pmclk->APBAMASK.bit.TC1_ = 1; - pgclk->PCHCTRL[TC1_GCLK_ID].bit.GEN = GEN_TC45; + pmclk->APBAMASK.bit.TC1_ = 1; + pgclk->PCHCTRL[TC1_GCLK_ID].bit.GEN = GEN_TC45; pgclk->PCHCTRL[TC1_GCLK_ID].bit.CHEN = 1; - //configure TC0 + // configure TC0 DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_BEGIN); ptc0->COUNT32.CTRLA.bit.ENABLE = 0; - while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_DISABLE); } + while (ptc0->COUNT32.SYNCBUSY.bit.ENABLE) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_DISABLE); + } ptc0->COUNT32.CTRLA.bit.SWRST = 1; - while (ptc0->COUNT32.SYNCBUSY.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_SWRST_1); } - while (ptc0->COUNT32.CTRLA.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_SWRST_2); } - //CTRLA as default - ptc0->COUNT32.CTRLA.bit.MODE = 2; //32 bit mode - ptc0->COUNT32.EVCTRL.bit.TCEI = 1; //enable incoming events - ptc0->COUNT32.EVCTRL.bit.EVACT = 2 ; //count events + while (ptc0->COUNT32.SYNCBUSY.bit.SWRST) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_SWRST_1); + } + while (ptc0->COUNT32.CTRLA.bit.SWRST) { + DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_SYNC_SWRST_2); + } + // CTRLA as default + ptc0->COUNT32.CTRLA.bit.MODE = 2; // 32 bit mode + ptc0->COUNT32.EVCTRL.bit.TCEI = 1; // enable incoming events + ptc0->COUNT32.EVCTRL.bit.EVACT = 2; // count events DBGC(DC_CLK_ENABLE_TIMEBASE_TC0_COMPLETE); DBGC(DC_CLK_ENABLE_TIMEBASE_EVSYS_BEGIN); - //configure event system - pmclk->APBBMASK.bit.EVSYS_ = 1; - pgclk->PCHCTRL[EVSYS_GCLK_ID_0].bit.GEN = GEN_TC45; + // configure event system + pmclk->APBBMASK.bit.EVSYS_ = 1; + pgclk->PCHCTRL[EVSYS_GCLK_ID_0].bit.GEN = GEN_TC45; pgclk->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN = 1; - pevsys->USER[44].reg = EVSYS_ID_USER_PORT_EV_0; //TC0 will get event channel 0 - pevsys->Channel[0].CHANNEL.bit.EDGSEL = EVSYS_CHANNEL_EDGSEL_RISING_EDGE_Val; //Rising edge - pevsys->Channel[0].CHANNEL.bit.PATH = EVSYS_CHANNEL_PATH_SYNCHRONOUS_Val; //Synchronous - pevsys->Channel[0].CHANNEL.bit.EVGEN = EVSYS_ID_GEN_TC4_MCX_0; //TC4 MC0 + pevsys->USER[44].reg = EVSYS_ID_USER_PORT_EV_0; // TC0 will get event channel 0 + pevsys->Channel[0].CHANNEL.bit.EDGSEL = EVSYS_CHANNEL_EDGSEL_RISING_EDGE_Val; // Rising edge + pevsys->Channel[0].CHANNEL.bit.PATH = EVSYS_CHANNEL_PATH_SYNCHRONOUS_Val; // Synchronous + pevsys->Channel[0].CHANNEL.bit.EVGEN = EVSYS_ID_GEN_TC4_MCX_0; // TC4 MC0 DBGC(DC_CLK_ENABLE_TIMEBASE_EVSYS_COMPLETE); @@ -251,34 +300,29 @@ uint32_t CLK_enable_timebase(void) return 0; } -void CLK_delay_us(uint32_t usec) -{ - asm ( - "CBZ R0, return\n\t" //If usec == 0, branch to return label +void CLK_delay_us(uint32_t usec) { + asm("CBZ R0, return\n\t" // If usec == 0, branch to return label ); - asm ( - "MULS R0, %0\n\t" //Multiply R0(usec) by usec_delay_mult and store in R0 - ".balign 16\n\t" //Ensure loop is aligned for fastest performance - "loop: SUBS R0, #1\n\t" //Subtract 1 from R0 and update flags (1 cycle) - "BNE loop\n\t" //Branch if non-zero to loop label (2 cycles) NOTE: USEC_DELAY_LOOP_CYCLES is the sum of loop cycles - "return:\n\t" //Return label - : //No output registers - : "r" (usec_delay_mult) //For %0 + asm("MULS R0, %0\n\t" // Multiply R0(usec) by usec_delay_mult and store in R0 + ".balign 16\n\t" // Ensure loop is aligned for fastest performance + "loop: SUBS R0, #1\n\t" // Subtract 1 from R0 and update flags (1 cycle) + "BNE loop\n\t" // Branch if non-zero to loop label (2 cycles) NOTE: USEC_DELAY_LOOP_CYCLES is the sum of loop cycles + "return:\n\t" // Return label + : // No output registers + : "r"(usec_delay_mult) // For %0 ); - //Note: BX LR generated + // Note: BX LR generated } -void CLK_delay_ms(uint64_t msec) -{ +void CLK_delay_ms(uint64_t msec) { msec += timer_read64(); - while (msec > timer_read64()) {} + while (msec > timer_read64()) { + } } -void clk_enable_sercom_apbmask(int sercomn) -{ +void clk_enable_sercom_apbmask(int sercomn) { Mclk *pmclk = MCLK; - switch (sercomn) - { + switch (sercomn) { case 0: pmclk->APBAMASK.bit.SERCOM0_ = 1; break; @@ -296,26 +340,27 @@ void clk_enable_sercom_apbmask(int sercomn) } } -//call CLK_oscctrl_init first -//call CLK_set_spi_freq(CHAN_SERCOM_SPI, FREQ_SPI_DEFAULT); -uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq) -{ +// call CLK_oscctrl_init first +// call CLK_set_spi_freq(CHAN_SERCOM_SPI, FREQ_SPI_DEFAULT); +uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq) { DBGC(DC_CLK_SET_SPI_FREQ_BEGIN); - Gclk *pgclk = GCLK; + Gclk * pgclk = GCLK; Sercom *psercom = (Sercom *)sercom_apbbase[sercomn]; clk_enable_sercom_apbmask(sercomn); - //all gclk0 for now - pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; + // all gclk0 for now + pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.CHEN = 1; psercom->I2CM.CTRLA.bit.SWRST = 1; - while (psercom->I2CM.SYNCBUSY.bit.SWRST) {} - while (psercom->I2CM.CTRLA.bit.SWRST) {} + while (psercom->I2CM.SYNCBUSY.bit.SWRST) { + } + while (psercom->I2CM.CTRLA.bit.SWRST) { + } - psercom->SPI.BAUD.reg = (uint8_t) (system_clks.freq_gclk[0]/2/freq-1); - system_clks.freq_spi = system_clks.freq_gclk[0]/2/(psercom->SPI.BAUD.reg+1); + psercom->SPI.BAUD.reg = (uint8_t)(system_clks.freq_gclk[0] / 2 / freq - 1); + system_clks.freq_spi = system_clks.freq_gclk[0] / 2 / (psercom->SPI.BAUD.reg + 1); system_clks.freq_sercom[sercomn] = system_clks.freq_spi; DBGC(DC_CLK_SET_SPI_FREQ_COMPLETE); @@ -323,26 +368,27 @@ uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq) return system_clks.freq_spi; } -//call CLK_oscctrl_init first -//call CLK_set_i2c0_freq(CHAN_SERCOM_I2C0, FREQ_I2C0_DEFAULT); -uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq) -{ +// call CLK_oscctrl_init first +// call CLK_set_i2c0_freq(CHAN_SERCOM_I2C0, FREQ_I2C0_DEFAULT); +uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq) { DBGC(DC_CLK_SET_I2C0_FREQ_BEGIN); - Gclk *pgclk = GCLK; + Gclk * pgclk = GCLK; Sercom *psercom = (Sercom *)sercom_apbbase[sercomn]; clk_enable_sercom_apbmask(sercomn); - //all gclk0 for now - pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; + // all gclk0 for now + pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.CHEN = 1; psercom->I2CM.CTRLA.bit.SWRST = 1; - while (psercom->I2CM.SYNCBUSY.bit.SWRST) {} - while (psercom->I2CM.CTRLA.bit.SWRST) {} + while (psercom->I2CM.SYNCBUSY.bit.SWRST) { + } + while (psercom->I2CM.CTRLA.bit.SWRST) { + } - psercom->I2CM.BAUD.bit.BAUD = (uint8_t) (system_clks.freq_gclk[0]/2/freq-1); - system_clks.freq_i2c0 = system_clks.freq_gclk[0]/2/(psercom->I2CM.BAUD.bit.BAUD+1); + psercom->I2CM.BAUD.bit.BAUD = (uint8_t)(system_clks.freq_gclk[0] / 2 / freq - 1); + system_clks.freq_i2c0 = system_clks.freq_gclk[0] / 2 / (psercom->I2CM.BAUD.bit.BAUD + 1); system_clks.freq_sercom[sercomn] = system_clks.freq_i2c0; DBGC(DC_CLK_SET_I2C0_FREQ_COMPLETE); @@ -350,26 +396,27 @@ uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq) return system_clks.freq_i2c0; } -//call CLK_oscctrl_init first -//call CLK_set_i2c1_freq(CHAN_SERCOM_I2C1, FREQ_I2C1_DEFAULT); -uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq) -{ +// call CLK_oscctrl_init first +// call CLK_set_i2c1_freq(CHAN_SERCOM_I2C1, FREQ_I2C1_DEFAULT); +uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq) { DBGC(DC_CLK_SET_I2C1_FREQ_BEGIN); - Gclk *pgclk = GCLK; + Gclk * pgclk = GCLK; Sercom *psercom = (Sercom *)sercom_apbbase[sercomn]; clk_enable_sercom_apbmask(sercomn); - //all gclk0 for now - pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; + // all gclk0 for now + pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.GEN = 0; pgclk->PCHCTRL[sercom_pchan[sercomn]].bit.CHEN = 1; psercom->I2CM.CTRLA.bit.SWRST = 1; - while (psercom->I2CM.SYNCBUSY.bit.SWRST) {} - while (psercom->I2CM.CTRLA.bit.SWRST) {} + while (psercom->I2CM.SYNCBUSY.bit.SWRST) { + } + while (psercom->I2CM.CTRLA.bit.SWRST) { + } - psercom->I2CM.BAUD.bit.BAUD = (uint8_t) (system_clks.freq_gclk[0]/2/freq-10); - system_clks.freq_i2c1 = system_clks.freq_gclk[0]/2/(psercom->I2CM.BAUD.bit.BAUD+10); + psercom->I2CM.BAUD.bit.BAUD = (uint8_t)(system_clks.freq_gclk[0] / 2 / freq - 10); + system_clks.freq_i2c1 = system_clks.freq_gclk[0] / 2 / (psercom->I2CM.BAUD.bit.BAUD + 10); system_clks.freq_sercom[sercomn] = system_clks.freq_i2c1; DBGC(DC_CLK_SET_I2C1_FREQ_COMPLETE); @@ -377,15 +424,13 @@ uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq) return system_clks.freq_i2c1; } -void CLK_init(void) -{ +void CLK_init(void) { DBGC(DC_CLK_INIT_BEGIN); - memset((void *)&system_clks,0,sizeof(system_clks)); + memset((void *)&system_clks, 0, sizeof(system_clks)); CLK_oscctrl_init(); CLK_enable_timebase(); DBGC(DC_CLK_INIT_COMPLETE); } - diff --git a/tmk_core/protocol/arm_atsam/clks.h b/tmk_core/protocol/arm_atsam/clks.h index 1b01a1764..72df3a8e3 100644 --- a/tmk_core/protocol/arm_atsam/clks.h +++ b/tmk_core/protocol/arm_atsam/clks.h @@ -20,20 +20,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef MD_BOOTLOADER -//From keyboard -#include "config_led.h" -#include "config.h" +// From keyboard +# include "config_led.h" +# include "config.h" -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER -#define PLL_RATIO 47 //mcu frequency ((X+1)MHz) -#define FREQ_DFLL_DEFAULT 48000000 //DFLL frequency / usb clock -#define FREQ_SPI_DEFAULT 1000000 //spi to 595 shift regs -#define FREQ_I2C0_DEFAULT 100000 //i2c to hub -#define FREQ_I2C1_DEFAULT I2C_HZ //i2c to LED drivers -#define FREQ_TC45_DEFAULT 1000000 //1 usec resolution +#define PLL_RATIO 47 // mcu frequency ((X+1)MHz) +#define FREQ_DFLL_DEFAULT 48000000 // DFLL frequency / usb clock +#define FREQ_SPI_DEFAULT 1000000 // spi to 595 shift regs +#define FREQ_I2C0_DEFAULT 100000 // i2c to hub +#define FREQ_I2C1_DEFAULT I2C_HZ // i2c to LED drivers +#define FREQ_TC45_DEFAULT 1000000 // 1 usec resolution -//I2C1 Set ~Result PWM Time (2x Drivers) +// I2C1 Set ~Result PWM Time (2x Drivers) // 1000000 1090000 // 900000 1000000 3.82ms // 800000 860000 @@ -42,20 +42,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. // 580000 615000 6.08ms // 500000 522000 -#define FREQ_XOSC0 16000000 +#define FREQ_XOSC0 16000000 -#define CHAN_SERCOM_SPI 2 //shift regs -#define CHAN_SERCOM_I2C0 0 //hub -#define CHAN_SERCOM_I2C1 1 //led drivers -#define CHAN_SERCOM_UART 3 //debug util +#define CHAN_SERCOM_SPI 2 // shift regs +#define CHAN_SERCOM_I2C0 0 // hub +#define CHAN_SERCOM_I2C1 1 // led drivers +#define CHAN_SERCOM_UART 3 // debug util -//Generator clock channels -#define GEN_DPLL0 0 -#define GEN_OSC0 1 -#define GEN_TC45 2 +// Generator clock channels +#define GEN_DPLL0 0 +#define GEN_OSC0 1 +#define GEN_TC45 2 #define SERCOM_COUNT 5 -#define GCLK_COUNT 12 +#define GCLK_COUNT 12 typedef struct clk_s { uint32_t freq_dfll; @@ -70,20 +70,20 @@ typedef struct clk_s { uint32_t freq_adc0; } clk_t; -extern volatile clk_t system_clks; +extern volatile clk_t system_clks; extern volatile uint64_t ms_clk; -void CLK_oscctrl_init(void); -void CLK_reset_time(void); +void CLK_oscctrl_init(void); +void CLK_reset_time(void); uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq); uint32_t CLK_enable_timebase(void); uint64_t timer_read64(void); -void CLK_delay_us(uint32_t usec); -void CLK_delay_ms(uint64_t msec); +void CLK_delay_us(uint32_t usec); +void CLK_delay_ms(uint64_t msec); uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq); uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq); uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq); -void CLK_init(void); +void CLK_init(void); -#endif // _CLKS_H_ +#endif // _CLKS_H_ diff --git a/tmk_core/protocol/arm_atsam/d51_util.c b/tmk_core/protocol/arm_atsam/d51_util.c index ea4225857..df596f7ba 100644 --- a/tmk_core/protocol/arm_atsam/d51_util.c +++ b/tmk_core/protocol/arm_atsam/d51_util.c @@ -1,46 +1,50 @@ #include "d51_util.h" static volatile uint32_t w; - -//Display unsigned 32-bit number by port toggling DBG_1 (to view on a scope) -//Read as follows: 1230 = | | | | | | || (note zero is fast double toggle) + +// Display unsigned 32-bit number by port toggling DBG_1 (to view on a scope) +// Read as follows: 1230 = | | | | | | || (note zero is fast double toggle) #define DBG_PAUSE 5 -void dbg_print(uint32_t x) -{ - int8_t t; +void dbg_print(uint32_t x) { + int8_t t; uint32_t n; uint32_t p, p2; - if (x < 10) t = 0; - else if (x < 100) t = 1; - else if (x < 1000) t = 2; - else if (x < 10000) t = 3; - else if (x < 100000) t = 4; - else if (x < 1000000) t = 5; - else if (x < 10000000) t = 6; - else if (x < 100000000) t = 7; - else if (x < 1000000000) t = 8; - else t = 9; - - while (t >= 0) - { + if (x < 10) + t = 0; + else if (x < 100) + t = 1; + else if (x < 1000) + t = 2; + else if (x < 10000) + t = 3; + else if (x < 100000) + t = 4; + else if (x < 1000000) + t = 5; + else if (x < 10000000) + t = 6; + else if (x < 100000000) + t = 7; + else if (x < 1000000000) + t = 8; + else + t = 9; + + while (t >= 0) { p2 = t; - p = 1; + p = 1; while (p2--) p *= 10; n = x / p; x -= n * p; - if (!n) - { + if (!n) { DBG_1_ON; DBG_1_OFF; DBG_1_ON; DBG_1_OFF; n--; - } - else - { - while (n > 0) - { + } else { + while (n > 0) { DBG_1_ON; DBG_1_OFF; n--; @@ -50,68 +54,80 @@ void dbg_print(uint32_t x) t--; } - for (w = DBG_PAUSE; w; w--); //Long pause after number is complete + for (w = DBG_PAUSE; w; w--) + ; // Long pause after number is complete } -//Display unsigned 32-bit number through debug led -//Read as follows: 1230 = [*] [* *] [* * *] [**] (note zero is fast double flash) +// Display unsigned 32-bit number through debug led +// Read as follows: 1230 = [*] [* *] [* * *] [**] (note zero is fast double flash) #define DLED_ONTIME 1000000 #define DLED_PAUSE 1500000 -void dled_print(uint32_t x, uint8_t long_pause) -{ - int8_t t; +void dled_print(uint32_t x, uint8_t long_pause) { + int8_t t; uint32_t n; uint32_t p, p2; - if (x < 10) t = 0; - else if (x < 100) t = 1; - else if (x < 1000) t = 2; - else if (x < 10000) t = 3; - else if (x < 100000) t = 4; - else if (x < 1000000) t = 5; - else if (x < 10000000) t = 6; - else if (x < 100000000) t = 7; - else if (x < 1000000000) t = 8; - else t = 9; - - while (t >= 0) - { + if (x < 10) + t = 0; + else if (x < 100) + t = 1; + else if (x < 1000) + t = 2; + else if (x < 10000) + t = 3; + else if (x < 100000) + t = 4; + else if (x < 1000000) + t = 5; + else if (x < 10000000) + t = 6; + else if (x < 100000000) + t = 7; + else if (x < 1000000000) + t = 8; + else + t = 9; + + while (t >= 0) { p2 = t; - p = 1; + p = 1; while (p2--) p *= 10; n = x / p; x -= n * p; - if (!n) - { + if (!n) { DBG_LED_ON; - for (w = DLED_ONTIME / 4; w; w--); + for (w = DLED_ONTIME / 4; w; w--) + ; DBG_LED_OFF; - for (w = DLED_ONTIME / 4; w; w--); + for (w = DLED_ONTIME / 4; w; w--) + ; DBG_LED_ON; - for (w = DLED_ONTIME / 4; w; w--); + for (w = DLED_ONTIME / 4; w; w--) + ; DBG_LED_OFF; - for (w = DLED_ONTIME / 4; w; w--); + for (w = DLED_ONTIME / 4; w; w--) + ; n--; - } - else - { - while (n > 0) - { + } else { + while (n > 0) { DBG_LED_ON; - for (w = DLED_ONTIME; w; w--); + for (w = DLED_ONTIME; w; w--) + ; DBG_LED_OFF; - for (w = DLED_ONTIME / 2; w; w--); + for (w = DLED_ONTIME / 2; w; w--) + ; n--; } } - for (w = DLED_PAUSE; w; w--); + for (w = DLED_PAUSE; w; w--) + ; t--; } - if (long_pause) - { - for (w = DLED_PAUSE * 4; w; w--); + if (long_pause) { + for (w = DLED_PAUSE * 4; w; w--) + ; } } @@ -119,103 +135,102 @@ void dled_print(uint32_t x, uint8_t long_pause) volatile uint32_t debug_code; -//These macros are for compile time substitution -#define DEBUG_BOOT_TRACING_EXTINTn (DEBUG_BOOT_TRACING_PIN % _U_(0x10)) -#define DEBUG_BOOT_TRACING_EXTINTb (_U_(0x1) << DEBUG_BOOT_TRACING_EXTINTn) -#define DEBUG_BOOT_TRACING_CONFIG_INDn (DEBUG_BOOT_TRACING_EXTINTn / _U_(0x8)) -#define DEBUG_BOOT_TRACING_CONFIG_SENSEn (DEBUG_BOOT_TRACING_EXTINTn % _U_(0x8)) -#define DEBUG_BOOT_TRACING_CONFIG_SENSEb (DEBUG_BOOT_TRACING_CONFIG_SENSEn * _U_(0x4)) -#define DEBUG_BOOT_TRACING_IRQn (EIC_0_IRQn + DEBUG_BOOT_TRACING_EXTINTn) - -//These macros perform PORT+PIN definition translation to IRQn in the preprocessor -#define PORTPIN_TO_IRQn_EXPAND(def) def -#define PORTPIN_TO_IRQn_DEF(def) PORTPIN_TO_IRQn_EXPAND(def) -#if DEBUG_BOOT_TRACING_PIN < 10 -#define PORTPIN_TO_IRQn_TODEF(port, pin) PORTPIN_TO_IRQn_DEF(PIN_ ## port ## 0 ## pin ## A_EIC_EXTINT_NUM) -#else -#define PORTPIN_TO_IRQn_TODEF(port, pin) PORTPIN_TO_IRQn_DEF(PIN_ ## port ## pin ## A_EIC_EXTINT_NUM) -#endif -#define PORTPIN_TO_IRQn(port, pin) PORTPIN_TO_IRQn_TODEF(port, pin) - -//These macros perform function name output in the preprocessor -#define DEBUG_BOOT_TRACING_HANDLER_CONCAT(irq) void EIC_ ## irq ## _Handler(void) -#define DEBUG_BOOT_TRACING_HANDLER(irq) DEBUG_BOOT_TRACING_HANDLER_CONCAT(irq) - -//To generate the function name of the IRQ handler catching boot tracing, +// These macros are for compile time substitution +# define DEBUG_BOOT_TRACING_EXTINTn (DEBUG_BOOT_TRACING_PIN % _U_(0x10)) +# define DEBUG_BOOT_TRACING_EXTINTb (_U_(0x1) << DEBUG_BOOT_TRACING_EXTINTn) +# define DEBUG_BOOT_TRACING_CONFIG_INDn (DEBUG_BOOT_TRACING_EXTINTn / _U_(0x8)) +# define DEBUG_BOOT_TRACING_CONFIG_SENSEn (DEBUG_BOOT_TRACING_EXTINTn % _U_(0x8)) +# define DEBUG_BOOT_TRACING_CONFIG_SENSEb (DEBUG_BOOT_TRACING_CONFIG_SENSEn * _U_(0x4)) +# define DEBUG_BOOT_TRACING_IRQn (EIC_0_IRQn + DEBUG_BOOT_TRACING_EXTINTn) + +// These macros perform PORT+PIN definition translation to IRQn in the preprocessor +# define PORTPIN_TO_IRQn_EXPAND(def) def +# define PORTPIN_TO_IRQn_DEF(def) PORTPIN_TO_IRQn_EXPAND(def) +# if DEBUG_BOOT_TRACING_PIN < 10 +# define PORTPIN_TO_IRQn_TODEF(port, pin) PORTPIN_TO_IRQn_DEF(PIN_##port##0##pin##A_EIC_EXTINT_NUM) +# else +# define PORTPIN_TO_IRQn_TODEF(port, pin) PORTPIN_TO_IRQn_DEF(PIN_##port##pin##A_EIC_EXTINT_NUM) +# endif +# define PORTPIN_TO_IRQn(port, pin) PORTPIN_TO_IRQn_TODEF(port, pin) + +// These macros perform function name output in the preprocessor +# define DEBUG_BOOT_TRACING_HANDLER_CONCAT(irq) void EIC_##irq##_Handler(void) +# define DEBUG_BOOT_TRACING_HANDLER(irq) DEBUG_BOOT_TRACING_HANDLER_CONCAT(irq) + +// To generate the function name of the IRQ handler catching boot tracing, // certain macros must be undefined, so save their current values to macro stack -#pragma push_macro("PA") -#pragma push_macro("PB") -#pragma push_macro("_L_") - -//Undefine / redefine pushed macros -#undef PA -#undef PB -#undef _L_ -#define _L_(x) x - -//Perform the work and output -//Ex: PORT PB, PIN 31 = void EIC_15_Handler(void) +# pragma push_macro("PA") +# pragma push_macro("PB") +# pragma push_macro("_L_") + +// Undefine / redefine pushed macros +# undef PA +# undef PB +# undef _L_ +# define _L_(x) x + +// Perform the work and output +// Ex: PORT PB, PIN 31 = void EIC_15_Handler(void) DEBUG_BOOT_TRACING_HANDLER(PORTPIN_TO_IRQn(DEBUG_BOOT_TRACING_PORT, DEBUG_BOOT_TRACING_PIN)) - -//Restore macros -#pragma pop_macro("PA") -#pragma pop_macro("PB") -#pragma pop_macro("_L_") +// Restore macros +# pragma pop_macro("PA") +# pragma pop_macro("PB") +# pragma pop_macro("_L_") { - //This is only for non-functional keyboard troubleshooting and should be disabled after boot - //Intention is to lock up the keyboard here with repeating debug led code - while (1) - { + // This is only for non-functional keyboard troubleshooting and should be disabled after boot + // Intention is to lock up the keyboard here with repeating debug led code + while (1) { dled_print(debug_code, 1); } } -void debug_code_init(void) -{ +void debug_code_init(void) { DBGC(DC_UNSET); - //Configure Ports for EIC - PORT->Group[DEBUG_BOOT_TRACING_PORT].DIRCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; //Input - PORT->Group[DEBUG_BOOT_TRACING_PORT].OUTSET.reg = 1 << DEBUG_BOOT_TRACING_PIN; //High - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.INEN = 1; //Input Enable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PULLEN = 1; //Pull Enable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PMUXEN = 1; //Mux Enable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PMUX[DEBUG_BOOT_TRACING_PIN / 2].bit.PMUXO = 0; //Mux A + // Configure Ports for EIC + PORT->Group[DEBUG_BOOT_TRACING_PORT].DIRCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; // Input + PORT->Group[DEBUG_BOOT_TRACING_PORT].OUTSET.reg = 1 << DEBUG_BOOT_TRACING_PIN; // High + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.INEN = 1; // Input Enable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PULLEN = 1; // Pull Enable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PMUXEN = 1; // Mux Enable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PMUX[DEBUG_BOOT_TRACING_PIN / 2].bit.PMUXO = 0; // Mux A - //Enable CLK_EIC_APB + // Enable CLK_EIC_APB MCLK->APBAMASK.bit.EIC_ = 1; - //Configure EIC + // Configure EIC EIC->CTRLA.bit.SWRST = 1; - while (EIC->SYNCBUSY.bit.SWRST) {} - EIC->ASYNCH.reg = DEBUG_BOOT_TRACING_EXTINTb; + while (EIC->SYNCBUSY.bit.SWRST) { + } + EIC->ASYNCH.reg = DEBUG_BOOT_TRACING_EXTINTb; EIC->INTENSET.reg = DEBUG_BOOT_TRACING_EXTINTb; EIC->CONFIG[DEBUG_BOOT_TRACING_CONFIG_INDn].reg |= (EIC_CONFIG_SENSE0_FALL_Val << DEBUG_BOOT_TRACING_CONFIG_SENSEb); EIC->CTRLA.bit.ENABLE = 1; - while (EIC->SYNCBUSY.bit.ENABLE) {} + while (EIC->SYNCBUSY.bit.ENABLE) { + } - //Enable EIC IRQ + // Enable EIC IRQ NVIC_EnableIRQ(DEBUG_BOOT_TRACING_IRQn); } -void debug_code_disable(void) -{ - //Disable EIC IRQ +void debug_code_disable(void) { + // Disable EIC IRQ NVIC_DisableIRQ(DEBUG_BOOT_TRACING_IRQn); - //Disable EIC + // Disable EIC EIC->CTRLA.bit.ENABLE = 0; - while (EIC->SYNCBUSY.bit.ENABLE) {} + while (EIC->SYNCBUSY.bit.ENABLE) { + } - //Default port configuration - PORT->Group[DEBUG_BOOT_TRACING_PORT].DIRCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; //Input - PORT->Group[DEBUG_BOOT_TRACING_PORT].OUTCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; //Low - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.INEN = 0; //Input Disable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PULLEN = 0; //Pull Disable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PMUXEN = 0; //Mux Disable - PORT->Group[DEBUG_BOOT_TRACING_PORT].PMUX[DEBUG_BOOT_TRACING_PIN / 2].bit.PMUXO = 0; //Mux A + // Default port configuration + PORT->Group[DEBUG_BOOT_TRACING_PORT].DIRCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; // Input + PORT->Group[DEBUG_BOOT_TRACING_PORT].OUTCLR.reg = 1 << DEBUG_BOOT_TRACING_PIN; // Low + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.INEN = 0; // Input Disable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PULLEN = 0; // Pull Disable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PINCFG[DEBUG_BOOT_TRACING_PIN].bit.PMUXEN = 0; // Mux Disable + PORT->Group[DEBUG_BOOT_TRACING_PORT].PMUX[DEBUG_BOOT_TRACING_PIN / 2].bit.PMUXO = 0; // Mux A - //Disable CLK_EIC_APB + // Disable CLK_EIC_APB MCLK->APBAMASK.bit.EIC_ = 0; } @@ -224,4 +239,4 @@ void debug_code_disable(void) void debug_code_init(void) {} void debug_code_disable(void) {} -#endif //DEBUG_BOOT_TRACING_ENABLE +#endif // DEBUG_BOOT_TRACING_ENABLE diff --git a/tmk_core/protocol/arm_atsam/d51_util.h b/tmk_core/protocol/arm_atsam/d51_util.h index 71431942c..89cf338d2 100644 --- a/tmk_core/protocol/arm_atsam/d51_util.h +++ b/tmk_core/protocol/arm_atsam/d51_util.h @@ -22,54 +22,54 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* Debug LED */ #if DEBUG_LED_ENABLE == 1 -#define DBG_LED_ENA PORT->Group[DEBUG_LED_PORT].DIRSET.reg = (1 << DEBUG_LED_PIN) -#define DBG_LED_DIS PORT->Group[DEBUG_LED_PORT].DIRCLR.reg = (1 << DEBUG_LED_PIN) -#define DBG_LED_ON PORT->Group[DEBUG_LED_PORT].OUTSET.reg = (1 << DEBUG_LED_PIN) -#define DBG_LED_OFF PORT->Group[DEBUG_LED_PORT].OUTCLR.reg = (1 << DEBUG_LED_PIN) +# define DBG_LED_ENA PORT->Group[DEBUG_LED_PORT].DIRSET.reg = (1 << DEBUG_LED_PIN) +# define DBG_LED_DIS PORT->Group[DEBUG_LED_PORT].DIRCLR.reg = (1 << DEBUG_LED_PIN) +# define DBG_LED_ON PORT->Group[DEBUG_LED_PORT].OUTSET.reg = (1 << DEBUG_LED_PIN) +# define DBG_LED_OFF PORT->Group[DEBUG_LED_PORT].OUTCLR.reg = (1 << DEBUG_LED_PIN) #else -#define DBG_LED_ENA -#define DBG_LED_DIS -#define DBG_LED_ON -#define DBG_LED_OFF +# define DBG_LED_ENA +# define DBG_LED_DIS +# define DBG_LED_ON +# define DBG_LED_OFF #endif /* Debug Port 1 */ #if DEBUG_PORT1_ENABLE == 1 -#define DBG_1_ENA PORT->Group[DEBUG_PORT1_PORT].DIRSET.reg = (1 << DEBUG_PORT1_PIN) -#define DBG_1_DIS PORT->Group[DEBUG_PORT1_PORT].DIRCLR.reg = (1 << DEBUG_PORT1_PIN) -#define DBG_1_ON PORT->Group[DEBUG_PORT1_PORT].OUTSET.reg = (1 << DEBUG_PORT1_PIN) -#define DBG_1_OFF PORT->Group[DEBUG_PORT1_PORT].OUTCLR.reg = (1 << DEBUG_PORT1_PIN) +# define DBG_1_ENA PORT->Group[DEBUG_PORT1_PORT].DIRSET.reg = (1 << DEBUG_PORT1_PIN) +# define DBG_1_DIS PORT->Group[DEBUG_PORT1_PORT].DIRCLR.reg = (1 << DEBUG_PORT1_PIN) +# define DBG_1_ON PORT->Group[DEBUG_PORT1_PORT].OUTSET.reg = (1 << DEBUG_PORT1_PIN) +# define DBG_1_OFF PORT->Group[DEBUG_PORT1_PORT].OUTCLR.reg = (1 << DEBUG_PORT1_PIN) #else -#define DBG_1_ENA -#define DBG_1_DIS -#define DBG_1_ON -#define DBG_1_OFF +# define DBG_1_ENA +# define DBG_1_DIS +# define DBG_1_ON +# define DBG_1_OFF #endif /* Debug Port 2 */ #if DEBUG_PORT2_ENABLE == 1 -#define DBG_2_ENA PORT->Group[DEBUG_PORT2_PORT].DIRSET.reg = (1 << DEBUG_PORT2_PIN) -#define DBG_2_DIS PORT->Group[DEBUG_PORT2_PORT].DIRCLR.reg = (1 << DEBUG_PORT2_PIN) -#define DBG_2_ON PORT->Group[DEBUG_PORT2_PORT].OUTSET.reg = (1 << DEBUG_PORT2_PIN) -#define DBG_2_OFF PORT->Group[DEBUG_PORT2_PORT].OUTCLR.reg = (1 << DEBUG_PORT2_PIN) +# define DBG_2_ENA PORT->Group[DEBUG_PORT2_PORT].DIRSET.reg = (1 << DEBUG_PORT2_PIN) +# define DBG_2_DIS PORT->Group[DEBUG_PORT2_PORT].DIRCLR.reg = (1 << DEBUG_PORT2_PIN) +# define DBG_2_ON PORT->Group[DEBUG_PORT2_PORT].OUTSET.reg = (1 << DEBUG_PORT2_PIN) +# define DBG_2_OFF PORT->Group[DEBUG_PORT2_PORT].OUTCLR.reg = (1 << DEBUG_PORT2_PIN) #else -#define DBG_2_ENA -#define DBG_2_DIS -#define DBG_2_ON -#define DBG_2_OFF +# define DBG_2_ENA +# define DBG_2_DIS +# define DBG_2_ON +# define DBG_2_OFF #endif /* Debug Port 3 */ #if DEBUG_PORT3_ENABLE == 1 -#define DBG_3_ENA PORT->Group[DEBUG_PORT3_PORT].DIRSET.reg = (1 << DEBUG_PORT3_PIN) -#define DBG_3_DIS PORT->Group[DEBUG_PORT3_PORT].DIRCLR.reg = (1 << DEBUG_PORT3_PIN) -#define DBG_3_ON PORT->Group[DEBUG_PORT3_PORT].OUTSET.reg = (1 << DEBUG_PORT3_PIN) -#define DBG_3_OFF PORT->Group[DEBUG_PORT3_PORT].OUTCLR.reg = (1 << DEBUG_PORT3_PIN) +# define DBG_3_ENA PORT->Group[DEBUG_PORT3_PORT].DIRSET.reg = (1 << DEBUG_PORT3_PIN) +# define DBG_3_DIS PORT->Group[DEBUG_PORT3_PORT].DIRCLR.reg = (1 << DEBUG_PORT3_PIN) +# define DBG_3_ON PORT->Group[DEBUG_PORT3_PORT].OUTSET.reg = (1 << DEBUG_PORT3_PIN) +# define DBG_3_OFF PORT->Group[DEBUG_PORT3_PORT].OUTCLR.reg = (1 << DEBUG_PORT3_PIN) #else -#define DBG_3_ENA -#define DBG_3_DIS -#define DBG_3_ON -#define DBG_3_OFF +# define DBG_3_ENA +# define DBG_3_DIS +# define DBG_3_ON +# define DBG_3_OFF #endif void dbg_print(uint32_t x); @@ -80,7 +80,7 @@ void debug_code_disable(void); #ifdef DEBUG_BOOT_TRACING_ENABLE -#define DBGC(n) debug_code = n +# define DBGC(n) debug_code = n extern volatile uint32_t debug_code; @@ -216,8 +216,9 @@ enum debug_code_list { #else -#define DBGC(n) {} +# define DBGC(n) \ + {} -#endif //DEBUG_BOOT_TRACING_ENABLE +#endif // DEBUG_BOOT_TRACING_ENABLE -#endif //_D51_UTIL_H_ +#endif //_D51_UTIL_H_ diff --git a/tmk_core/protocol/arm_atsam/i2c_master.c b/tmk_core/protocol/arm_atsam/i2c_master.c index 1741d9ac5..d3319ab44 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.c +++ b/tmk_core/protocol/arm_atsam/i2c_master.c @@ -19,77 +19,84 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #if !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) -#include <string.h> +# include <string.h> -//From keyboard -#include "config.h" -#include "config_led.h" -#include "matrix.h" +// From keyboard +# include "config.h" +# include "config_led.h" +# include "matrix.h" -#define I2C_LED_USE_DMA 1 //Set 1 to use background DMA transfers for leds, Set 0 to use inline software transfers +# define I2C_LED_USE_DMA 1 // Set 1 to use background DMA transfers for leds, Set 0 to use inline software transfers -static uint8_t i2c_led_q[I2C_Q_SIZE]; //I2C queue circular buffer -static uint8_t i2c_led_q_s; //Start of circular buffer -static uint8_t i2c_led_q_e; //End of circular buffer -static uint8_t i2c_led_q_full; //Queue full counter for reset +static uint8_t i2c_led_q[I2C_Q_SIZE]; // I2C queue circular buffer +static uint8_t i2c_led_q_s; // Start of circular buffer +static uint8_t i2c_led_q_e; // End of circular buffer +static uint8_t i2c_led_q_full; // Queue full counter for reset -static uint8_t dma_sendbuf[I2C_DMA_MAX_SEND]; //Data being written to I2C +static uint8_t dma_sendbuf[I2C_DMA_MAX_SEND]; // Data being written to I2C volatile uint8_t i2c_led_q_running; -#endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) +#endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) -void i2c0_init(void) -{ +void i2c0_init(void) { DBGC(DC_I2C0_INIT_BEGIN); CLK_set_i2c0_freq(CHAN_SERCOM_I2C0, FREQ_I2C0_DEFAULT); - //MCU - PORT->Group[0].PMUX[4].bit.PMUXE = 2; - PORT->Group[0].PMUX[4].bit.PMUXO = 2; + // MCU + PORT->Group[0].PMUX[4].bit.PMUXE = 2; + PORT->Group[0].PMUX[4].bit.PMUXO = 2; PORT->Group[0].PINCFG[8].bit.PMUXEN = 1; PORT->Group[0].PINCFG[9].bit.PMUXEN = 1; - //I2C - //Note: SW Reset handled in CLK_set_i2c0_freq clks.c + // I2C + // Note: SW Reset handled in CLK_set_i2c0_freq clks.c - SERCOM0->I2CM.CTRLA.bit.MODE = 5; //Set master mode + SERCOM0->I2CM.CTRLA.bit.MODE = 5; // Set master mode - SERCOM0->I2CM.CTRLA.bit.SPEED = 0; //Set to 1 for Fast-mode Plus (FM+) up to 1 MHz - SERCOM0->I2CM.CTRLA.bit.RUNSTDBY = 1; //Enabled + SERCOM0->I2CM.CTRLA.bit.SPEED = 0; // Set to 1 for Fast-mode Plus (FM+) up to 1 MHz + SERCOM0->I2CM.CTRLA.bit.RUNSTDBY = 1; // Enabled - SERCOM0->I2CM.CTRLA.bit.ENABLE = 1; //Enable the device - while (SERCOM0->I2CM.SYNCBUSY.bit.ENABLE) { DBGC(DC_I2C0_INIT_SYNC_ENABLING); } //Wait for SYNCBUSY.ENABLE to clear + SERCOM0->I2CM.CTRLA.bit.ENABLE = 1; // Enable the device + while (SERCOM0->I2CM.SYNCBUSY.bit.ENABLE) { + DBGC(DC_I2C0_INIT_SYNC_ENABLING); + } // Wait for SYNCBUSY.ENABLE to clear - SERCOM0->I2CM.STATUS.bit.BUSSTATE = 1; //Force into IDLE state - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_I2C0_INIT_SYNC_SYSOP); } - while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) { DBGC(DC_I2C0_INIT_WAIT_IDLE); } //Wait while not idle + SERCOM0->I2CM.STATUS.bit.BUSSTATE = 1; // Force into IDLE state + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { + DBGC(DC_I2C0_INIT_SYNC_SYSOP); + } + while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) { + DBGC(DC_I2C0_INIT_WAIT_IDLE); + } // Wait while not idle DBGC(DC_I2C0_INIT_COMPLETE); } -uint8_t i2c0_start(uint8_t address) -{ +uint8_t i2c0_start(uint8_t address) { SERCOM0->I2CM.ADDR.bit.ADDR = address; - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) {} - while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) {} - while (SERCOM0->I2CM.STATUS.bit.RXNACK) {} + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { + } + while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) { + } + while (SERCOM0->I2CM.STATUS.bit.RXNACK) { + } return 1; } -uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) -{ +uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) { if (!length) return 0; i2c0_start(address); - while (length) - { + while (length) { SERCOM0->I2CM.DATA.bit.DATA = *data; - while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) {} - while (SERCOM0->I2CM.STATUS.bit.RXNACK) {} + while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) { + } + while (SERCOM0->I2CM.STATUS.bit.RXNACK) { + } data++; length--; @@ -100,74 +107,83 @@ uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t return 1; } -void i2c0_stop(void) -{ - if (SERCOM0->I2CM.STATUS.bit.CLKHOLD || SERCOM0->I2CM.INTFLAG.bit.MB == 1 || SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) - { +void i2c0_stop(void) { + if (SERCOM0->I2CM.STATUS.bit.CLKHOLD || SERCOM0->I2CM.INTFLAG.bit.MB == 1 || SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) { SERCOM0->I2CM.CTRLB.bit.CMD = 3; - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP); - while (SERCOM0->I2CM.STATUS.bit.CLKHOLD); - while (SERCOM0->I2CM.INTFLAG.bit.MB); - while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1); + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) + ; + while (SERCOM0->I2CM.STATUS.bit.CLKHOLD) + ; + while (SERCOM0->I2CM.INTFLAG.bit.MB) + ; + while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) + ; } } #if !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) -void i2c1_init(void) -{ +void i2c1_init(void) { DBGC(DC_I2C1_INIT_BEGIN); CLK_set_i2c1_freq(CHAN_SERCOM_I2C1, FREQ_I2C1_DEFAULT); /* MCU */ - PORT->Group[0].PMUX[8].bit.PMUXE = 2; - PORT->Group[0].PMUX[8].bit.PMUXO = 2; + PORT->Group[0].PMUX[8].bit.PMUXE = 2; + PORT->Group[0].PMUX[8].bit.PMUXO = 2; PORT->Group[0].PINCFG[16].bit.PMUXEN = 1; PORT->Group[0].PINCFG[17].bit.PMUXEN = 1; /* I2C */ - //Note: SW Reset handled in CLK_set_i2c1_freq clks.c + // Note: SW Reset handled in CLK_set_i2c1_freq clks.c - SERCOM1->I2CM.CTRLA.bit.MODE = 5; //MODE: Set master mode (No sync) - SERCOM1->I2CM.CTRLA.bit.SPEED = 1; //SPEED: Fm+ up to 1MHz (No sync) - SERCOM1->I2CM.CTRLA.bit.RUNSTDBY = 1; //RUNSTBY: Enabled (No sync) + SERCOM1->I2CM.CTRLA.bit.MODE = 5; // MODE: Set master mode (No sync) + SERCOM1->I2CM.CTRLA.bit.SPEED = 1; // SPEED: Fm+ up to 1MHz (No sync) + SERCOM1->I2CM.CTRLA.bit.RUNSTDBY = 1; // RUNSTBY: Enabled (No sync) - SERCOM1->I2CM.CTRLB.bit.SMEN = 1; //SMEN: Smart mode enabled (For DMA)(No sync) + SERCOM1->I2CM.CTRLB.bit.SMEN = 1; // SMEN: Smart mode enabled (For DMA)(No sync) NVIC_EnableIRQ(SERCOM1_0_IRQn); SERCOM1->I2CM.INTENSET.bit.ERROR = 1; - SERCOM1->I2CM.CTRLA.bit.ENABLE = 1; //ENABLE: Enable the device (sync SYNCBUSY.ENABLE) - while (SERCOM1->I2CM.SYNCBUSY.bit.ENABLE) { DBGC(DC_I2C1_INIT_SYNC_ENABLING); } //Wait for SYNCBUSY.ENABLE to clear + SERCOM1->I2CM.CTRLA.bit.ENABLE = 1; // ENABLE: Enable the device (sync SYNCBUSY.ENABLE) + while (SERCOM1->I2CM.SYNCBUSY.bit.ENABLE) { + DBGC(DC_I2C1_INIT_SYNC_ENABLING); + } // Wait for SYNCBUSY.ENABLE to clear - SERCOM1->I2CM.STATUS.bit.BUSSTATE = 1; //BUSSTATE: Force into IDLE state (sync SYNCBUSY.SYSOP) - while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_I2C1_INIT_SYNC_SYSOP); } - while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) { DBGC(DC_I2C1_INIT_WAIT_IDLE); } //Wait while not idle + SERCOM1->I2CM.STATUS.bit.BUSSTATE = 1; // BUSSTATE: Force into IDLE state (sync SYNCBUSY.SYSOP) + while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) { + DBGC(DC_I2C1_INIT_SYNC_SYSOP); + } + while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) { + DBGC(DC_I2C1_INIT_WAIT_IDLE); + } // Wait while not idle DBGC(DC_I2C1_INIT_COMPLETE); } -uint8_t i2c1_start(uint8_t address) -{ +uint8_t i2c1_start(uint8_t address) { SERCOM1->I2CM.ADDR.bit.ADDR = address; - while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) {} - while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) {} - while (SERCOM1->I2CM.STATUS.bit.RXNACK) {} + while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) { + } + while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) { + } + while (SERCOM1->I2CM.STATUS.bit.RXNACK) { + } return 1; } -uint8_t i2c1_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) -{ +uint8_t i2c1_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) { if (!length) return 0; i2c1_start(address); - while (length) - { + while (length) { SERCOM1->I2CM.DATA.bit.DATA = *data; - while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) {} - while (SERCOM1->I2CM.STATUS.bit.RXNACK) {} + while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) { + } + while (SERCOM1->I2CM.STATUS.bit.RXNACK) { + } data++; length--; @@ -178,33 +194,32 @@ uint8_t i2c1_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t return 1; } -void i2c1_stop(void) -{ - if (SERCOM1->I2CM.STATUS.bit.CLKHOLD || SERCOM1->I2CM.INTFLAG.bit.MB == 1 || SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) - { +void i2c1_stop(void) { + if (SERCOM1->I2CM.STATUS.bit.CLKHOLD || SERCOM1->I2CM.INTFLAG.bit.MB == 1 || SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) { SERCOM1->I2CM.CTRLB.bit.CMD = 3; - while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP); - while (SERCOM1->I2CM.STATUS.bit.CLKHOLD); - while (SERCOM1->I2CM.INTFLAG.bit.MB); - while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1); + while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) + ; + while (SERCOM1->I2CM.STATUS.bit.CLKHOLD) + ; + while (SERCOM1->I2CM.INTFLAG.bit.MB) + ; + while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) + ; } } -void i2c_led_send_CRWL(uint8_t drvid) -{ - uint8_t i2cdata[] = { ISSI3733_CMDRWL, ISSI3733_CMDRWL_WRITE_ENABLE_ONCE }; +void i2c_led_send_CRWL(uint8_t drvid) { + uint8_t i2cdata[] = {ISSI3733_CMDRWL, ISSI3733_CMDRWL_WRITE_ENABLE_ONCE}; i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0); } -void i2c_led_select_page(uint8_t drvid, uint8_t pageno) -{ - uint8_t i2cdata[] = { ISSI3733_CMDR, pageno }; +void i2c_led_select_page(uint8_t drvid, uint8_t pageno) { + uint8_t i2cdata[] = {ISSI3733_CMDR, pageno}; i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0); } -void i2c_led_send_GCR(uint8_t drvid) -{ - uint8_t i2cdata[] = { ISSI3733_GCCR, 0x00 }; +void i2c_led_send_GCR(uint8_t drvid) { + uint8_t i2cdata[] = {ISSI3733_GCCR, 0x00}; if (gcr_actual > LED_GCR_MAX) gcr_actual = LED_GCR_MAX; i2cdata[1] = gcr_actual; @@ -212,57 +227,50 @@ void i2c_led_send_GCR(uint8_t drvid) i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0); } -void i2c_led_send_onoff(uint8_t drvid) -{ -#if I2C_LED_USE_DMA != 1 - if (!i2c_led_q_running) - { -#endif +void i2c_led_send_onoff(uint8_t drvid) { +# if I2C_LED_USE_DMA != 1 + if (!i2c_led_q_running) { +# endif i2c_led_send_CRWL(drvid); i2c_led_select_page(drvid, 0); -#if I2C_LED_USE_DMA != 1 +# if I2C_LED_USE_DMA != 1 } -#endif +# endif - *issidrv[drvid].onoff = 0; //Force start location offset to zero + *issidrv[drvid].onoff = 0; // Force start location offset to zero i2c1_transmit(issidrv[drvid].addr, issidrv[drvid].onoff, ISSI3733_PG0_BYTES, 0); } -void i2c_led_send_mode_op_gcr(uint8_t drvid, uint8_t mode, uint8_t operation) -{ - uint8_t i2cdata[] = { ISSI3733_CR, mode | operation, gcr_actual}; +void i2c_led_send_mode_op_gcr(uint8_t drvid, uint8_t mode, uint8_t operation) { + uint8_t i2cdata[] = {ISSI3733_CR, mode | operation, gcr_actual}; i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0); } -void i2c_led_send_pur_pdr(uint8_t drvid, uint8_t pur, uint8_t pdr) -{ - uint8_t i2cdata[] = { ISSI3733_SWYR_PUR, pur, pdr }; +void i2c_led_send_pur_pdr(uint8_t drvid, uint8_t pur, uint8_t pdr) { + uint8_t i2cdata[] = {ISSI3733_SWYR_PUR, pur, pdr}; i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0); } -void i2c_led_send_pwm(uint8_t drvid) -{ -#if I2C_LED_USE_DMA != 1 - if (!i2c_led_q_running) - { -#endif +void i2c_led_send_pwm(uint8_t drvid) { +# if I2C_LED_USE_DMA != 1 + if (!i2c_led_q_running) { +# endif i2c_led_send_CRWL(drvid); i2c_led_select_page(drvid, 0); -#if I2C_LED_USE_DMA != 1 +# if I2C_LED_USE_DMA != 1 } -#endif +# endif - *issidrv[drvid].pwm = 0; //Force start location offset to zero + *issidrv[drvid].pwm = 0; // Force start location offset to zero i2c1_transmit(issidrv[drvid].addr, issidrv[drvid].pwm, ISSI3733_PG1_BYTES, 0); } -uint8_t I2C3733_Init_Control(void) -{ +uint8_t I2C3733_Init_Control(void) { DBGC(DC_I2C3733_INIT_CONTROL_BEGIN); - //Hardware state shutdown on boot - //USB state machine will enable driver when communication is ready + // Hardware state shutdown on boot + // USB state machine will enable driver when communication is ready I2C3733_Control_Set(0); wait_ms(1); @@ -277,25 +285,24 @@ uint8_t I2C3733_Init_Control(void) return 1; } -uint8_t I2C3733_Init_Drivers(void) -{ +uint8_t I2C3733_Init_Drivers(void) { DBGC(DC_I2C3733_INIT_DRIVERS_BEGIN); - gcr_actual = ISSI3733_GCR_DEFAULT; + gcr_actual = ISSI3733_GCR_DEFAULT; gcr_actual_last = gcr_actual; if (gcr_actual > LED_GCR_MAX) gcr_actual = LED_GCR_MAX; gcr_desired = gcr_actual; - //Set up master device + // Set up master device i2c_led_send_CRWL(0); i2c_led_select_page(0, 3); - i2c_led_send_mode_op_gcr(0, 0, ISSI3733_CR_SSD_NORMAL); //No SYNC due to brightness mismatch with second driver + i2c_led_send_mode_op_gcr(0, 0, ISSI3733_CR_SSD_NORMAL); // No SYNC due to brightness mismatch with second driver - //Set up slave device + // Set up slave device i2c_led_send_CRWL(1); i2c_led_select_page(1, 3); - i2c_led_send_mode_op_gcr(1, 0, ISSI3733_CR_SSD_NORMAL); //No SYNC due to brightness mismatch with first driver and slight flicker at rgb values 1,2 + i2c_led_send_mode_op_gcr(1, 0, ISSI3733_CR_SSD_NORMAL); // No SYNC due to brightness mismatch with first driver and slight flicker at rgb values 1,2 i2c_led_send_CRWL(0); i2c_led_select_page(0, 3); @@ -310,51 +317,54 @@ uint8_t I2C3733_Init_Drivers(void) return 1; } -void I2C_DMAC_LED_Init(void) -{ +void I2C_DMAC_LED_Init(void) { Dmac *dmac = DMAC; DBGC(DC_I2C_DMAC_LED_INIT_BEGIN); - //Disable device - dmac->CTRL.bit.DMAENABLE = 0; //Disable DMAC - while (dmac->CTRL.bit.DMAENABLE) {} //Wait for disabled state in case of ongoing transfers - dmac->CTRL.bit.SWRST = 1; //Software Reset DMAC - while (dmac->CTRL.bit.SWRST) {} //Wait for software reset to complete - - //Configure device - dmac->BASEADDR.reg = (uint32_t)&dmac_desc; //Set descriptor base address - dmac->WRBADDR.reg = (uint32_t)&dmac_desc_wb; //Set descriptor write back address - dmac->CTRL.reg |= 0x0f00; //Handle all priorities (LVL0-3) - - //Disable channel - dmac->Channel[0].CHCTRLA.bit.ENABLE = 0; //Disable the channel - while (dmac->Channel[0].CHCTRLA.bit.ENABLE) {} //Wait for disabled state in case of ongoing transfers - dmac->Channel[0].CHCTRLA.bit.SWRST = 1; //Software Reset the channel - while (dmac->Channel[0].CHCTRLA.bit.SWRST) {} //Wait for software reset to complete - - //Configure channel - dmac->Channel[0].CHCTRLA.bit.THRESHOLD = 0; //1BEAT - dmac->Channel[0].CHCTRLA.bit.BURSTLEN = 0; //SINGLE - dmac->Channel[0].CHCTRLA.bit.TRIGACT = 2; //BURST - dmac->Channel[0].CHCTRLA.bit.TRIGSRC = SERCOM1_DMAC_ID_TX; //Trigger source - dmac->Channel[0].CHCTRLA.bit.RUNSTDBY = 1; //Run in standby + // Disable device + dmac->CTRL.bit.DMAENABLE = 0; // Disable DMAC + while (dmac->CTRL.bit.DMAENABLE) { + } // Wait for disabled state in case of ongoing transfers + dmac->CTRL.bit.SWRST = 1; // Software Reset DMAC + while (dmac->CTRL.bit.SWRST) { + } // Wait for software reset to complete + + // Configure device + dmac->BASEADDR.reg = (uint32_t)&dmac_desc; // Set descriptor base address + dmac->WRBADDR.reg = (uint32_t)&dmac_desc_wb; // Set descriptor write back address + dmac->CTRL.reg |= 0x0f00; // Handle all priorities (LVL0-3) + + // Disable channel + dmac->Channel[0].CHCTRLA.bit.ENABLE = 0; // Disable the channel + while (dmac->Channel[0].CHCTRLA.bit.ENABLE) { + } // Wait for disabled state in case of ongoing transfers + dmac->Channel[0].CHCTRLA.bit.SWRST = 1; // Software Reset the channel + while (dmac->Channel[0].CHCTRLA.bit.SWRST) { + } // Wait for software reset to complete + + // Configure channel + dmac->Channel[0].CHCTRLA.bit.THRESHOLD = 0; // 1BEAT + dmac->Channel[0].CHCTRLA.bit.BURSTLEN = 0; // SINGLE + dmac->Channel[0].CHCTRLA.bit.TRIGACT = 2; // BURST + dmac->Channel[0].CHCTRLA.bit.TRIGSRC = SERCOM1_DMAC_ID_TX; // Trigger source + dmac->Channel[0].CHCTRLA.bit.RUNSTDBY = 1; // Run in standby NVIC_EnableIRQ(DMAC_0_IRQn); dmac->Channel[0].CHINTENSET.bit.TCMPL = 1; - dmac->Channel[0].CHINTENSET.bit.TERR = 1; + dmac->Channel[0].CHINTENSET.bit.TERR = 1; - //Enable device - dmac->CTRL.bit.DMAENABLE = 1; //Enable DMAC - while (dmac->CTRL.bit.DMAENABLE == 0) {} //Wait for enable state + // Enable device + dmac->CTRL.bit.DMAENABLE = 1; // Enable DMAC + while (dmac->CTRL.bit.DMAENABLE == 0) { + } // Wait for enable state DBGC(DC_I2C_DMAC_LED_INIT_COMPLETE); } -//state = 1 enable -//state = 0 disable -void I2C3733_Control_Set(uint8_t state) -{ +// state = 1 enable +// state = 0 disable +void I2C3733_Control_Set(uint8_t state) { DBGC(DC_I2C3733_CONTROL_SET_BEGIN); sr_exp_data.bit.SDB_N = (state == 1 ? 1 : 0); @@ -363,131 +373,111 @@ void I2C3733_Control_Set(uint8_t state) DBGC(DC_I2C3733_CONTROL_SET_COMPLETE); } -void i2c_led_desc_defaults(void) -{ - dmac_desc.BTCTRL.bit.STEPSIZE = 0; //SRCINC used in favor for auto 1 inc - dmac_desc.BTCTRL.bit.STEPSEL = 0; //SRCINC used in favor for auto 1 inc - dmac_desc.BTCTRL.bit.DSTINC = 0; //The Destination Address Increment is disabled - dmac_desc.BTCTRL.bit.SRCINC = 1; //The Source Address Increment is enabled (Inc by 1) - dmac_desc.BTCTRL.bit.BEATSIZE = 0; //8-bit bus transfer - dmac_desc.BTCTRL.bit.BLOCKACT = 0; //Channel will be disabled if it is the last block transfer in the transaction - dmac_desc.BTCTRL.bit.EVOSEL = 0; //Event generation disabled - dmac_desc.BTCTRL.bit.VALID = 1; //Set dmac valid +void i2c_led_desc_defaults(void) { + dmac_desc.BTCTRL.bit.STEPSIZE = 0; // SRCINC used in favor for auto 1 inc + dmac_desc.BTCTRL.bit.STEPSEL = 0; // SRCINC used in favor for auto 1 inc + dmac_desc.BTCTRL.bit.DSTINC = 0; // The Destination Address Increment is disabled + dmac_desc.BTCTRL.bit.SRCINC = 1; // The Source Address Increment is enabled (Inc by 1) + dmac_desc.BTCTRL.bit.BEATSIZE = 0; // 8-bit bus transfer + dmac_desc.BTCTRL.bit.BLOCKACT = 0; // Channel will be disabled if it is the last block transfer in the transaction + dmac_desc.BTCTRL.bit.EVOSEL = 0; // Event generation disabled + dmac_desc.BTCTRL.bit.VALID = 1; // Set dmac valid } -void i2c_led_prepare_send_dma(uint8_t *data, uint8_t len) -{ +void i2c_led_prepare_send_dma(uint8_t *data, uint8_t len) { i2c_led_desc_defaults(); - dmac_desc.BTCNT.reg = len; - dmac_desc.SRCADDR.reg = (uint32_t)data + len; - dmac_desc.DSTADDR.reg = (uint32_t)&SERCOM1->I2CM.DATA.reg; + dmac_desc.BTCNT.reg = len; + dmac_desc.SRCADDR.reg = (uint32_t)data + len; + dmac_desc.DSTADDR.reg = (uint32_t)&SERCOM1->I2CM.DATA.reg; dmac_desc.DESCADDR.reg = 0; } -void i2c_led_begin_dma(uint8_t drvid) -{ - DMAC->Channel[0].CHCTRLA.bit.ENABLE = 1; //Enable the channel +void i2c_led_begin_dma(uint8_t drvid) { + DMAC->Channel[0].CHCTRLA.bit.ENABLE = 1; // Enable the channel - SERCOM1->I2CM.ADDR.reg = (dmac_desc.BTCNT.reg << 16) | 0x2000 | issidrv[drvid].addr; //Begin transfer + SERCOM1->I2CM.ADDR.reg = (dmac_desc.BTCNT.reg << 16) | 0x2000 | issidrv[drvid].addr; // Begin transfer } -void i2c_led_send_CRWL_dma(uint8_t drvid) -{ - *(dma_sendbuf+0) = ISSI3733_CMDRWL; - *(dma_sendbuf+1) = ISSI3733_CMDRWL_WRITE_ENABLE_ONCE; +void i2c_led_send_CRWL_dma(uint8_t drvid) { + *(dma_sendbuf + 0) = ISSI3733_CMDRWL; + *(dma_sendbuf + 1) = ISSI3733_CMDRWL_WRITE_ENABLE_ONCE; i2c_led_prepare_send_dma(dma_sendbuf, 2); i2c_led_begin_dma(drvid); } -void i2c_led_select_page_dma(uint8_t drvid, uint8_t pageno) -{ - *(dma_sendbuf+0) = ISSI3733_CMDR; - *(dma_sendbuf+1) = pageno; +void i2c_led_select_page_dma(uint8_t drvid, uint8_t pageno) { + *(dma_sendbuf + 0) = ISSI3733_CMDR; + *(dma_sendbuf + 1) = pageno; i2c_led_prepare_send_dma(dma_sendbuf, 2); i2c_led_begin_dma(drvid); } -void i2c_led_send_GCR_dma(uint8_t drvid) -{ - *(dma_sendbuf+0) = ISSI3733_GCCR; - *(dma_sendbuf+1) = gcr_actual; +void i2c_led_send_GCR_dma(uint8_t drvid) { + *(dma_sendbuf + 0) = ISSI3733_GCCR; + *(dma_sendbuf + 1) = gcr_actual; i2c_led_prepare_send_dma(dma_sendbuf, 2); i2c_led_begin_dma(drvid); } -void i2c_led_send_pwm_dma(uint8_t drvid) -{ - //Note: This copies the CURRENT pwm buffer, which may be getting modified +void i2c_led_send_pwm_dma(uint8_t drvid) { + // Note: This copies the CURRENT pwm buffer, which may be getting modified memcpy(dma_sendbuf, issidrv[drvid].pwm, ISSI3733_PG1_BYTES); - *dma_sendbuf = 0; //Force start location offset to zero + *dma_sendbuf = 0; // Force start location offset to zero i2c_led_prepare_send_dma(dma_sendbuf, ISSI3733_PG1_BYTES); i2c_led_begin_dma(drvid); } -void i2c_led_send_onoff_dma(uint8_t drvid) -{ - //Note: This copies the CURRENT onoff buffer, which may be getting modified +void i2c_led_send_onoff_dma(uint8_t drvid) { + // Note: This copies the CURRENT onoff buffer, which may be getting modified memcpy(dma_sendbuf, issidrv[drvid].onoff, ISSI3733_PG0_BYTES); - *dma_sendbuf = 0; //Force start location offset to zero + *dma_sendbuf = 0; // Force start location offset to zero i2c_led_prepare_send_dma(dma_sendbuf, ISSI3733_PG0_BYTES); i2c_led_begin_dma(drvid); } -void i2c_led_q_init(void) -{ +void i2c_led_q_init(void) { memset(i2c_led_q, 0, I2C_Q_SIZE); - i2c_led_q_s = 0; - i2c_led_q_e = 0; + i2c_led_q_s = 0; + i2c_led_q_e = 0; i2c_led_q_running = 0; - i2c_led_q_full = 0; + i2c_led_q_full = 0; } -uint8_t i2c_led_q_isempty(void) -{ - return i2c_led_q_s == i2c_led_q_e; -} +uint8_t i2c_led_q_isempty(void) { return i2c_led_q_s == i2c_led_q_e; } -uint8_t i2c_led_q_size(void) -{ - return (i2c_led_q_e - i2c_led_q_s) % I2C_Q_SIZE; -} +uint8_t i2c_led_q_size(void) { return (i2c_led_q_e - i2c_led_q_s) % I2C_Q_SIZE; } -uint8_t i2c_led_q_available(void) -{ - return I2C_Q_SIZE - i2c_led_q_size() - 1; //Never allow end to meet start +uint8_t i2c_led_q_available(void) { + return I2C_Q_SIZE - i2c_led_q_size() - 1; // Never allow end to meet start } -void i2c_led_q_add(uint8_t cmd) -{ - //WARNING: Always request room before adding commands! +void i2c_led_q_add(uint8_t cmd) { + // WARNING: Always request room before adding commands! - //Assign command + // Assign command i2c_led_q[i2c_led_q_e] = cmd; - i2c_led_q_e = (i2c_led_q_e + 1) % I2C_Q_SIZE; //Move end up one or wrap + i2c_led_q_e = (i2c_led_q_e + 1) % I2C_Q_SIZE; // Move end up one or wrap } -void i2c_led_q_s_advance(void) -{ - i2c_led_q_s = (i2c_led_q_s + 1) % I2C_Q_SIZE; //Move start up one or wrap +void i2c_led_q_s_advance(void) { + i2c_led_q_s = (i2c_led_q_s + 1) % I2C_Q_SIZE; // Move start up one or wrap } -//Always request room before adding commands -//PS: In case the queue somehow gets filled, it will reset if it can not clear up -//PS: Could only get this to happen through unrealistic timings to overload the I2C bus -uint8_t i2c_led_q_request_room(uint8_t request_size) -{ - if (request_size > i2c_led_q_available()) - { +// Always request room before adding commands +// PS: In case the queue somehow gets filled, it will reset if it can not clear up +// PS: Could only get this to happen through unrealistic timings to overload the I2C bus +uint8_t i2c_led_q_request_room(uint8_t request_size) { + if (request_size > i2c_led_q_available()) { i2c_led_q_full++; - if (i2c_led_q_full >= 100) //Give the queue a chance to clear up + if (i2c_led_q_full >= 100) // Give the queue a chance to clear up { DBG_LED_ON; I2C_DMAC_LED_Init(); @@ -503,10 +493,8 @@ uint8_t i2c_led_q_request_room(uint8_t request_size) return 1; } -uint8_t i2c_led_q_run(void) -{ - if (i2c_led_q_isempty()) - { +uint8_t i2c_led_q_run(void) { + if (i2c_led_q_isempty()) { i2c_led_q_running = 0; return 0; } @@ -515,72 +503,62 @@ uint8_t i2c_led_q_run(void) i2c_led_q_running = 1; -#if I2C_LED_USE_DMA != 1 - while (!i2c_led_q_isempty()) - { -#endif - //run command - if (i2c_led_q[i2c_led_q_s] == I2C_Q_CRWL) - { +# if I2C_LED_USE_DMA != 1 + while (!i2c_led_q_isempty()) { +# endif + // run command + if (i2c_led_q[i2c_led_q_s] == I2C_Q_CRWL) { i2c_led_q_s_advance(); uint8_t drvid = i2c_led_q[i2c_led_q_s]; -#if I2C_LED_USE_DMA == 1 +# if I2C_LED_USE_DMA == 1 i2c_led_send_CRWL_dma(drvid); -#else - i2c_led_send_CRWL(drvid); -#endif - } - else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PAGE_SELECT) - { +# else + i2c_led_send_CRWL(drvid); +# endif + } else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PAGE_SELECT) { i2c_led_q_s_advance(); uint8_t drvid = i2c_led_q[i2c_led_q_s]; i2c_led_q_s_advance(); uint8_t page = i2c_led_q[i2c_led_q_s]; -#if I2C_LED_USE_DMA == 1 +# if I2C_LED_USE_DMA == 1 i2c_led_select_page_dma(drvid, page); -#else - i2c_led_select_page(drvid, page); -#endif - } - else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PWM) - { +# else + i2c_led_select_page(drvid, page); +# endif + } else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PWM) { i2c_led_q_s_advance(); uint8_t drvid = i2c_led_q[i2c_led_q_s]; -#if I2C_LED_USE_DMA == 1 +# if I2C_LED_USE_DMA == 1 i2c_led_send_pwm_dma(drvid); -#else - i2c_led_send_pwm(drvid); -#endif - } - else if (i2c_led_q[i2c_led_q_s] == I2C_Q_GCR) - { +# else + i2c_led_send_pwm(drvid); +# endif + } else if (i2c_led_q[i2c_led_q_s] == I2C_Q_GCR) { i2c_led_q_s_advance(); uint8_t drvid = i2c_led_q[i2c_led_q_s]; -#if I2C_LED_USE_DMA == 1 +# if I2C_LED_USE_DMA == 1 i2c_led_send_GCR_dma(drvid); -#else - i2c_led_send_GCR(drvid); -#endif - } - else if (i2c_led_q[i2c_led_q_s] == I2C_Q_ONOFF) - { +# else + i2c_led_send_GCR(drvid); +# endif + } else if (i2c_led_q[i2c_led_q_s] == I2C_Q_ONOFF) { i2c_led_q_s_advance(); uint8_t drvid = i2c_led_q[i2c_led_q_s]; -#if I2C_LED_USE_DMA == 1 +# if I2C_LED_USE_DMA == 1 i2c_led_send_onoff_dma(drvid); -#else - i2c_led_send_onoff(drvid); -#endif +# else + i2c_led_send_onoff(drvid); +# endif } - i2c_led_q_s_advance(); //Advance last run command or if the command byte was not serviced + i2c_led_q_s_advance(); // Advance last run command or if the command byte was not serviced -#if I2C_LED_USE_DMA != 1 +# if I2C_LED_USE_DMA != 1 } i2c_led_q_running = 0; -#endif +# endif return 1; } -#endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) +#endif // !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE) diff --git a/tmk_core/protocol/arm_atsam/i2c_master.h b/tmk_core/protocol/arm_atsam/i2c_master.h index 99481366a..44dbdfbff 100644 --- a/tmk_core/protocol/arm_atsam/i2c_master.h +++ b/tmk_core/protocol/arm_atsam/i2c_master.h @@ -20,89 +20,85 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef MD_BOOTLOADER -#include "samd51j18a.h" -#include "issi3733_driver.h" -#include "config.h" +# include "samd51j18a.h" +# include "issi3733_driver.h" +# include "config.h" -__attribute__((__aligned__(16))) -DmacDescriptor dmac_desc; -__attribute__((__aligned__(16))) -DmacDescriptor dmac_desc_wb; +__attribute__((__aligned__(16))) DmacDescriptor dmac_desc; +__attribute__((__aligned__(16))) DmacDescriptor dmac_desc_wb; uint8_t I2C3733_Init_Control(void); uint8_t I2C3733_Init_Drivers(void); -void I2C3733_Control_Set(uint8_t state); -void I2C_DMAC_LED_Init(void); +void I2C3733_Control_Set(uint8_t state); +void I2C_DMAC_LED_Init(void); -#define I2C_Q_SIZE 100 +# define I2C_Q_SIZE 100 -#define I2C_Q_NA 100 -#define I2C_Q_CRWL 101 -#define I2C_Q_PAGE_SELECT 102 -#define I2C_Q_PWM 103 -#define I2C_Q_GCR 104 -#define I2C_Q_ONOFF 105 +# define I2C_Q_NA 100 +# define I2C_Q_CRWL 101 +# define I2C_Q_PAGE_SELECT 102 +# define I2C_Q_PWM 103 +# define I2C_Q_GCR 104 +# define I2C_Q_ONOFF 105 -#define I2C_DMA_MAX_SEND 255 +# define I2C_DMA_MAX_SEND 255 extern volatile uint8_t i2c_led_q_running; -#define I2C_LED_Q_PWM(a) { \ - if (i2c_led_q_request_room(7)) \ - { \ - i2c_led_q_add(I2C_Q_CRWL); \ - i2c_led_q_add(a); \ - i2c_led_q_add(I2C_Q_PAGE_SELECT); \ - i2c_led_q_add(a); \ - i2c_led_q_add(ISSI3733_PG_PWM); \ - i2c_led_q_add(I2C_Q_PWM); \ - i2c_led_q_add(a); \ - } \ - } - -#define I2C_LED_Q_GCR(a) { \ - if (i2c_led_q_request_room(7)) \ - { \ - i2c_led_q_add(I2C_Q_CRWL); \ - i2c_led_q_add(a); \ - i2c_led_q_add(I2C_Q_PAGE_SELECT); \ - i2c_led_q_add(a); \ - i2c_led_q_add(ISSI3733_PG_FN); \ - i2c_led_q_add(I2C_Q_GCR); \ - i2c_led_q_add(a); \ - } \ - } - -#define I2C_LED_Q_ONOFF(a) { \ - if (i2c_led_q_request_room(7)) \ - { \ - i2c_led_q_add(I2C_Q_CRWL); \ - i2c_led_q_add(a); \ - i2c_led_q_add(I2C_Q_PAGE_SELECT); \ - i2c_led_q_add(a); \ - i2c_led_q_add(ISSI3733_PG_ONOFF); \ - i2c_led_q_add(I2C_Q_ONOFF); \ - i2c_led_q_add(a); \ - } \ - } - - -void i2c_led_q_init(void); -void i2c_led_q_add(uint8_t cmd); -void i2c_led_q_s_advance(void); +# define I2C_LED_Q_PWM(a) \ + { \ + if (i2c_led_q_request_room(7)) { \ + i2c_led_q_add(I2C_Q_CRWL); \ + i2c_led_q_add(a); \ + i2c_led_q_add(I2C_Q_PAGE_SELECT); \ + i2c_led_q_add(a); \ + i2c_led_q_add(ISSI3733_PG_PWM); \ + i2c_led_q_add(I2C_Q_PWM); \ + i2c_led_q_add(a); \ + } \ + } + +# define I2C_LED_Q_GCR(a) \ + { \ + if (i2c_led_q_request_room(7)) { \ + i2c_led_q_add(I2C_Q_CRWL); \ + i2c_led_q_add(a); \ + i2c_led_q_add(I2C_Q_PAGE_SELECT); \ + i2c_led_q_add(a); \ + i2c_led_q_add(ISSI3733_PG_FN); \ + i2c_led_q_add(I2C_Q_GCR); \ + i2c_led_q_add(a); \ + } \ + } + +# define I2C_LED_Q_ONOFF(a) \ + { \ + if (i2c_led_q_request_room(7)) { \ + i2c_led_q_add(I2C_Q_CRWL); \ + i2c_led_q_add(a); \ + i2c_led_q_add(I2C_Q_PAGE_SELECT); \ + i2c_led_q_add(a); \ + i2c_led_q_add(ISSI3733_PG_ONOFF); \ + i2c_led_q_add(I2C_Q_ONOFF); \ + i2c_led_q_add(a); \ + } \ + } + +void i2c_led_q_init(void); +void i2c_led_q_add(uint8_t cmd); +void i2c_led_q_s_advance(void); uint8_t i2c_led_q_size(void); uint8_t i2c_led_q_request_room(uint8_t request_size); uint8_t i2c_led_q_run(void); -void i2c1_init(void); +void i2c1_init(void); uint8_t i2c1_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout); -void i2c1_stop(void); +void i2c1_stop(void); -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER -void i2c0_init(void); +void i2c0_init(void); uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout); -void i2c0_stop(void); - -#endif // _I2C_MASTER_H_ +void i2c0_stop(void); +#endif // _I2C_MASTER_H_ diff --git a/tmk_core/protocol/arm_atsam/issi3733_driver.h b/tmk_core/protocol/arm_atsam/issi3733_driver.h index a537029f0..bd29fe76b 100644 --- a/tmk_core/protocol/arm_atsam/issi3733_driver.h +++ b/tmk_core/protocol/arm_atsam/issi3733_driver.h @@ -18,31 +18,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef _ISSI3733_DRIVER_H_ #define _ISSI3733_DRIVER_H_ -//ISII3733 Registers +// ISII3733 Registers -#define ISSI3733_CMDR 0xFD //Command Register (Write Only) +#define ISSI3733_CMDR 0xFD // Command Register (Write Only) -#define ISSI3733_CMDRWL 0xFE //Command Register Write Lock (Read/Write) -#define ISSI3733_CMDRWL_WRITE_DISABLE 0x00 //Lock register -#define ISSI3733_CMDRWL_WRITE_ENABLE_ONCE 0xC5 //Enable one write to register then reset to locked +#define ISSI3733_CMDRWL 0xFE // Command Register Write Lock (Read/Write) +#define ISSI3733_CMDRWL_WRITE_DISABLE 0x00 // Lock register +#define ISSI3733_CMDRWL_WRITE_ENABLE_ONCE 0xC5 // Enable one write to register then reset to locked -#define ISSI3733_IMR 0xF0 //Interrupt Mask Register (Write Only) -#define ISSI3733_IMR_IAC_ON 0x08 //Auto Clear Interrupt Bit - Interrupt auto clear when INTB stay low exceeds 8ms -#define ISSI3733_IMR_IAB_ON 0x04 //Auto Breath Interrupt Bit - Enable auto breath loop finish interrupt -#define ISSI3733_IMR_IS_ON 0x02 //Dot Short Interrupt Bit - Enable dot short interrupt -#define ISSI3733_IMR_IO_ON 0x01 //Dot Open Interrupt Bit - Enable dot open interrupt +#define ISSI3733_IMR 0xF0 // Interrupt Mask Register (Write Only) +#define ISSI3733_IMR_IAC_ON 0x08 // Auto Clear Interrupt Bit - Interrupt auto clear when INTB stay low exceeds 8ms +#define ISSI3733_IMR_IAB_ON 0x04 // Auto Breath Interrupt Bit - Enable auto breath loop finish interrupt +#define ISSI3733_IMR_IS_ON 0x02 // Dot Short Interrupt Bit - Enable dot short interrupt +#define ISSI3733_IMR_IO_ON 0x01 // Dot Open Interrupt Bit - Enable dot open interrupt -#define ISSI3733_ISR 0xF1 //Interrupt Status Register (Read Only) -#define ISSI3733_ISR_ABM3_FINISH 0x10 //Auto Breath Mode 3 Finish Bit - ABM3 finished -#define ISSI3733_ISR_ABM2_FINISH 0x08 //Auto Breath Mode 2 Finish Bit - ABM2 finished -#define ISSI3733_ISR_ABM1_FINISH 0x04 //Auto Breath Mode 1 Finish Bit - ABM1 finished -#define ISSI3733_ISR_SB 0x02 //Short Bit - Shorted -#define ISSI3733_ISR_OB 0x01 //Open Bit - Opened +#define ISSI3733_ISR 0xF1 // Interrupt Status Register (Read Only) +#define ISSI3733_ISR_ABM3_FINISH 0x10 // Auto Breath Mode 3 Finish Bit - ABM3 finished +#define ISSI3733_ISR_ABM2_FINISH 0x08 // Auto Breath Mode 2 Finish Bit - ABM2 finished +#define ISSI3733_ISR_ABM1_FINISH 0x04 // Auto Breath Mode 1 Finish Bit - ABM1 finished +#define ISSI3733_ISR_SB 0x02 // Short Bit - Shorted +#define ISSI3733_ISR_OB 0x01 // Open Bit - Opened -#define ISSI3733_PG0 0x00 //LED Control Register -#define ISSI3733_PG1 0x01 //PWM Register -#define ISSI3733_PG2 0x02 //Auto Breath Mode Register -#define ISSI3733_PG3 0x03 //Function Register +#define ISSI3733_PG0 0x00 // LED Control Register +#define ISSI3733_PG1 0x01 // PWM Register +#define ISSI3733_PG2 0x02 // Auto Breath Mode Register +#define ISSI3733_PG3 0x03 // Function Register #define ISSI3733_PG_ONOFF ISSI3733_PG0 #define ISSI3733_PG_OR ISSI3733_PG0 @@ -51,88 +51,88 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define ISSI3733_PG_ABM ISSI3733_PG2 #define ISSI3733_PG_FN ISSI3733_PG3 -#define ISSI3733_CR 0x00 //Configuration Register +#define ISSI3733_CR 0x00 // Configuration Register -//PG3: Configuration Register: Synchronize Configuration -#define ISSI3733_CR_SYNC_MASTER 0x40 //Master -#define ISSI3733_CR_SYNC_SLAVE 0x80 //Slave -#define ISSI3733_CR_SYNC_HIGH_IMP 0xC0 //High Impedance +// PG3: Configuration Register: Synchronize Configuration +#define ISSI3733_CR_SYNC_MASTER 0x40 // Master +#define ISSI3733_CR_SYNC_SLAVE 0x80 // Slave +#define ISSI3733_CR_SYNC_HIGH_IMP 0xC0 // High Impedance -//PG3: Configuration Register: Open/Short Detection Enable Bit +// PG3: Configuration Register: Open/Short Detection Enable Bit //#define ISSI3733_CR_OSD_DISABLE 0x00 //Disable open/short detection -#define ISSI3733_CR_OSD_ENABLE 0x04 //Enable open/short detection +#define ISSI3733_CR_OSD_ENABLE 0x04 // Enable open/short detection -//PG3: Configuration Register: Auto Breath Enable +// PG3: Configuration Register: Auto Breath Enable //#define ISSI3733_CR_B_EN_PWM 0x00 //PWM Mode Enable -#define ISSI3733_CR_B_EN_AUTO 0x02 //Auto Breath Mode Enable +#define ISSI3733_CR_B_EN_AUTO 0x02 // Auto Breath Mode Enable -//PG3: Configuration Register: Software Shutdown Control +// PG3: Configuration Register: Software Shutdown Control //#define ISSI3733_CR_SSD_SHUTDOWN 0x00 //Software shutdown -#define ISSI3733_CR_SSD_NORMAL 0x01 //Normal operation - -#define ISSI3733_GCCR 0x01 //Global Current Control Register - -//1 Byte, Iout = (GCC / 256) * (840 / Rext) -//TODO: Give user define for Rext - -//PG3: Auto Breath Control Register 1 -#define ISSI3733_ABCR1_ABM1 0x02 //Auto Breath Control Register 1 of ABM-1 -#define ISSI3733_ABCR1_ABM2 0x06 //Auto Breath Control Register 1 of ABM-2 -#define ISSI3733_ABCR1_ABM3 0x0A //Auto Breath Control Register 1 of ABM-3 - -//Rise time -#define ISSI3733_ABCR1_T1_0021 0x00 //0.21s -#define ISSI3733_ABCR1_T1_0042 0x20 //0.42s -#define ISSI3733_ABCR1_T1_0084 0x40 //0.84s -#define ISSI3733_ABCR1_T1_0168 0x60 //1.68s -#define ISSI3733_ABCR1_T1_0336 0x80 //3.36s -#define ISSI3733_ABCR1_T1_0672 0xA0 //6.72s -#define ISSI3733_ABCR1_T1_1344 0xC0 //13.44s -#define ISSI3733_ABCR1_T1_2688 0xE0 //26.88s - -//Max value time -#define ISSI3733_ABCR1_T2_0000 0x00 //0s -#define ISSI3733_ABCR1_T2_0021 0x02 //0.21s -#define ISSI3733_ABCR1_T2_0042 0x04 //0.42s -#define ISSI3733_ABCR1_T2_0084 0x06 //0.84s -#define ISSI3733_ABCR1_T2_0168 0x08 //1.68s -#define ISSI3733_ABCR1_T2_0336 0x0A //3.36s -#define ISSI3733_ABCR1_T2_0672 0x0C //6.72s -#define ISSI3733_ABCR1_T2_1344 0x0E //13.44s -#define ISSI3733_ABCR1_T2_2688 0x10 //26.88s - -//PG3: Auto Breath Control Register 2 -#define ISSI3733_ABCR2_ABM1 0x03 //Auto Breath Control Register 2 of ABM-1 -#define ISSI3733_ABCR2_ABM2 0x07 //Auto Breath Control Register 2 of ABM-2 -#define ISSI3733_ABCR2_ABM3 0x0B //Auto Breath Control Register 2 of ABM-3 - -//Fall time -#define ISSI3733_ABCR2_T3_0021 0x00 //0.21s -#define ISSI3733_ABCR2_T3_0042 0x20 //0.42s -#define ISSI3733_ABCR2_T3_0084 0x40 //0.84s -#define ISSI3733_ABCR2_T3_0168 0x60 //1.68s -#define ISSI3733_ABCR2_T3_0336 0x80 //3.36s -#define ISSI3733_ABCR2_T3_0672 0xA0 //6.72s -#define ISSI3733_ABCR2_T3_1344 0xC0 //13.44s -#define ISSI3733_ABCR2_T3_2688 0xE0 //26.88s - -//Min value time -#define ISSI3733_ABCR2_T4_0000 0x00 //0s -#define ISSI3733_ABCR2_T4_0021 0x02 //0.21s -#define ISSI3733_ABCR2_T4_0042 0x04 //0.42s -#define ISSI3733_ABCR2_T4_0084 0x06 //0.84s -#define ISSI3733_ABCR2_T4_0168 0x08 //1.68s -#define ISSI3733_ABCR2_T4_0336 0x0A //3.36s -#define ISSI3733_ABCR2_T4_0672 0x0C //6.72s -#define ISSI3733_ABCR2_T4_1344 0x0E //13.44s -#define ISSI3733_ABCR2_T4_2688 0x10 //26.88s -#define ISSI3733_ABCR2_T4_5376 0x12 //53.76s -#define ISSI3733_ABCR2_T4_10752 0x14 //107.52s - -//PG3: Auto Breath Control Register 3 -#define ISSI3733_ABCR3_ABM1 0x04 //Auto Breath Control Register 3 of ABM-1 -#define ISSI3733_ABCR3_ABM2 0x08 //Auto Breath Control Register 3 of ABM-2 -#define ISSI3733_ABCR3_ABM3 0x0C //Auto Breath Control Register 3 of ABM-3 +#define ISSI3733_CR_SSD_NORMAL 0x01 // Normal operation + +#define ISSI3733_GCCR 0x01 // Global Current Control Register + +// 1 Byte, Iout = (GCC / 256) * (840 / Rext) +// TODO: Give user define for Rext + +// PG3: Auto Breath Control Register 1 +#define ISSI3733_ABCR1_ABM1 0x02 // Auto Breath Control Register 1 of ABM-1 +#define ISSI3733_ABCR1_ABM2 0x06 // Auto Breath Control Register 1 of ABM-2 +#define ISSI3733_ABCR1_ABM3 0x0A // Auto Breath Control Register 1 of ABM-3 + +// Rise time +#define ISSI3733_ABCR1_T1_0021 0x00 // 0.21s +#define ISSI3733_ABCR1_T1_0042 0x20 // 0.42s +#define ISSI3733_ABCR1_T1_0084 0x40 // 0.84s +#define ISSI3733_ABCR1_T1_0168 0x60 // 1.68s +#define ISSI3733_ABCR1_T1_0336 0x80 // 3.36s +#define ISSI3733_ABCR1_T1_0672 0xA0 // 6.72s +#define ISSI3733_ABCR1_T1_1344 0xC0 // 13.44s +#define ISSI3733_ABCR1_T1_2688 0xE0 // 26.88s + +// Max value time +#define ISSI3733_ABCR1_T2_0000 0x00 // 0s +#define ISSI3733_ABCR1_T2_0021 0x02 // 0.21s +#define ISSI3733_ABCR1_T2_0042 0x04 // 0.42s +#define ISSI3733_ABCR1_T2_0084 0x06 // 0.84s +#define ISSI3733_ABCR1_T2_0168 0x08 // 1.68s +#define ISSI3733_ABCR1_T2_0336 0x0A // 3.36s +#define ISSI3733_ABCR1_T2_0672 0x0C // 6.72s +#define ISSI3733_ABCR1_T2_1344 0x0E // 13.44s +#define ISSI3733_ABCR1_T2_2688 0x10 // 26.88s + +// PG3: Auto Breath Control Register 2 +#define ISSI3733_ABCR2_ABM1 0x03 // Auto Breath Control Register 2 of ABM-1 +#define ISSI3733_ABCR2_ABM2 0x07 // Auto Breath Control Register 2 of ABM-2 +#define ISSI3733_ABCR2_ABM3 0x0B // Auto Breath Control Register 2 of ABM-3 + +// Fall time +#define ISSI3733_ABCR2_T3_0021 0x00 // 0.21s +#define ISSI3733_ABCR2_T3_0042 0x20 // 0.42s +#define ISSI3733_ABCR2_T3_0084 0x40 // 0.84s +#define ISSI3733_ABCR2_T3_0168 0x60 // 1.68s +#define ISSI3733_ABCR2_T3_0336 0x80 // 3.36s +#define ISSI3733_ABCR2_T3_0672 0xA0 // 6.72s +#define ISSI3733_ABCR2_T3_1344 0xC0 // 13.44s +#define ISSI3733_ABCR2_T3_2688 0xE0 // 26.88s + +// Min value time +#define ISSI3733_ABCR2_T4_0000 0x00 // 0s +#define ISSI3733_ABCR2_T4_0021 0x02 // 0.21s +#define ISSI3733_ABCR2_T4_0042 0x04 // 0.42s +#define ISSI3733_ABCR2_T4_0084 0x06 // 0.84s +#define ISSI3733_ABCR2_T4_0168 0x08 // 1.68s +#define ISSI3733_ABCR2_T4_0336 0x0A // 3.36s +#define ISSI3733_ABCR2_T4_0672 0x0C // 6.72s +#define ISSI3733_ABCR2_T4_1344 0x0E // 13.44s +#define ISSI3733_ABCR2_T4_2688 0x10 // 26.88s +#define ISSI3733_ABCR2_T4_5376 0x12 // 53.76s +#define ISSI3733_ABCR2_T4_10752 0x14 // 107.52s + +// PG3: Auto Breath Control Register 3 +#define ISSI3733_ABCR3_ABM1 0x04 // Auto Breath Control Register 3 of ABM-1 +#define ISSI3733_ABCR3_ABM2 0x08 // Auto Breath Control Register 3 of ABM-2 +#define ISSI3733_ABCR3_ABM3 0x0C // Auto Breath Control Register 3 of ABM-3 #define ISSI3733_ABCR3_LTA_LOOP_ENDLESS 0x00 #define ISSI3733_ABCR3_LTA_LOOP_1 0x01 @@ -151,51 +151,51 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define ISSI3733_ABCR3_LTA_LOOP_14 0x0E #define ISSI3733_ABCR3_LTA_LOOP_15 0x0F -//Loop Begin +// Loop Begin #define ISSI3733_ABCR3_LB_T1 0x00 #define ISSI3733_ABCR3_LB_T2 0x10 #define ISSI3733_ABCR3_LB_T3 0x20 #define ISSI3733_ABCR3_LB_T4 0x30 -//Loop End -#define ISSI3733_ABCR3_LE_T3 0x00 //End at Off state -#define ISSI3733_ABCR3_LE_T1 0x40 //End at On State +// Loop End +#define ISSI3733_ABCR3_LE_T3 0x00 // End at Off state +#define ISSI3733_ABCR3_LE_T1 0x40 // End at On State -//PG3: Auto Breath Control Register 4 -#define ISSI3733_ABCR4_ABM1 0x05 //Auto Breath Control Register 4 of ABM-1 -#define ISSI3733_ABCR4_ABM2 0x09 //Auto Breath Control Register 4 of ABM-2 -#define ISSI3733_ABCR4_ABM3 0x0D //Auto Breath Control Register 4 of ABM-3 +// PG3: Auto Breath Control Register 4 +#define ISSI3733_ABCR4_ABM1 0x05 // Auto Breath Control Register 4 of ABM-1 +#define ISSI3733_ABCR4_ABM2 0x09 // Auto Breath Control Register 4 of ABM-2 +#define ISSI3733_ABCR4_ABM3 0x0D // Auto Breath Control Register 4 of ABM-3 #define ISSI3733_ABCR4_LTB_LOOP_ENDLESS 0x00 -//Or 8bit loop times +// Or 8bit loop times -//PG3: Time Update Register +// PG3: Time Update Register #define ISSI3733_TUR 0x0E -#define ISSI3733_TUR_UPDATE 0x00 //Write to update 02h~0Dh time registers after configuring +#define ISSI3733_TUR_UPDATE 0x00 // Write to update 02h~0Dh time registers after configuring -//PG3: SWy Pull-Up Resistor Selection Register +// PG3: SWy Pull-Up Resistor Selection Register #define ISSI3733_SWYR_PUR 0x0F -#define ISSI3733_SWYR_PUR_NONE 0x00 //No pull-up resistor -#define ISSI3733_SWYR_PUR_500 0x01 //0.5k Ohm -#define ISSI3733_SWYR_PUR_1000 0x02 //1.0k Ohm -#define ISSI3733_SWYR_PUR_2000 0x03 //2.0k Ohm -#define ISSI3733_SWYR_PUR_4000 0x04 //4.0k Ohm -#define ISSI3733_SWYR_PUR_8000 0x05 //8.0k Ohm -#define ISSI3733_SWYR_PUR_16000 0x06 //16k Ohm -#define ISSI3733_SWYR_PUR_32000 0x07 //32k Ohm - -//PG3: CSx Pull-Down Resistor Selection Register +#define ISSI3733_SWYR_PUR_NONE 0x00 // No pull-up resistor +#define ISSI3733_SWYR_PUR_500 0x01 // 0.5k Ohm +#define ISSI3733_SWYR_PUR_1000 0x02 // 1.0k Ohm +#define ISSI3733_SWYR_PUR_2000 0x03 // 2.0k Ohm +#define ISSI3733_SWYR_PUR_4000 0x04 // 4.0k Ohm +#define ISSI3733_SWYR_PUR_8000 0x05 // 8.0k Ohm +#define ISSI3733_SWYR_PUR_16000 0x06 // 16k Ohm +#define ISSI3733_SWYR_PUR_32000 0x07 // 32k Ohm + +// PG3: CSx Pull-Down Resistor Selection Register #define ISSI3733_CSXR_PDR 0x10 -#define ISSI3733_CSXR_PDR_NONE 0x00 //No pull-down resistor -#define ISSI3733_CSXR_PDR_500 0x01 //0.5k Ohm -#define ISSI3733_CSXR_PDR_1000 0x02 //1.0k Ohm -#define ISSI3733_CSXR_PDR_2000 0x03 //2.0k Ohm -#define ISSI3733_CSXR_PDR_4000 0x04 //4.0k Ohm -#define ISSI3733_CSXR_PDR_8000 0x05 //8.0k Ohm -#define ISSI3733_CSXR_PDR_16000 0x06 //16k Ohm -#define ISSI3733_CSXR_PDR_32000 0x07 //32k Ohm - -//PG3: Reset Register -#define ISSI3733_RR 0x11 //Read to reset all registers to default values - -#endif //_ISSI3733_DRIVER_H_ +#define ISSI3733_CSXR_PDR_NONE 0x00 // No pull-down resistor +#define ISSI3733_CSXR_PDR_500 0x01 // 0.5k Ohm +#define ISSI3733_CSXR_PDR_1000 0x02 // 1.0k Ohm +#define ISSI3733_CSXR_PDR_2000 0x03 // 2.0k Ohm +#define ISSI3733_CSXR_PDR_4000 0x04 // 4.0k Ohm +#define ISSI3733_CSXR_PDR_8000 0x05 // 8.0k Ohm +#define ISSI3733_CSXR_PDR_16000 0x06 // 16k Ohm +#define ISSI3733_CSXR_PDR_32000 0x07 // 32k Ohm + +// PG3: Reset Register +#define ISSI3733_RR 0x11 // Read to reset all registers to default values + +#endif //_ISSI3733_DRIVER_H_ diff --git a/tmk_core/protocol/arm_atsam/led_matrix.c b/tmk_core/protocol/arm_atsam/led_matrix.c index 42dfccbc5..c078980d6 100644 --- a/tmk_core/protocol/arm_atsam/led_matrix.c +++ b/tmk_core/protocol/arm_atsam/led_matrix.c @@ -22,24 +22,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <math.h> #ifdef USE_MASSDROP_CONFIGURATOR -__attribute__((weak)) -led_instruction_t led_instructions[] = { { .end = 1 } }; -static void led_matrix_massdrop_config_override(int i); -#endif // USE_MASSDROP_CONFIGURATOR +__attribute__((weak)) led_instruction_t led_instructions[] = {{.end = 1}}; +static void led_matrix_massdrop_config_override(int i); +#endif // USE_MASSDROP_CONFIGURATOR - -void SERCOM1_0_Handler( void ) -{ - if (SERCOM1->I2CM.INTFLAG.bit.ERROR) - { +void SERCOM1_0_Handler(void) { + if (SERCOM1->I2CM.INTFLAG.bit.ERROR) { SERCOM1->I2CM.INTFLAG.reg = SERCOM_I2CM_INTENCLR_ERROR; } } -void DMAC_0_Handler( void ) -{ - if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL) - { +void DMAC_0_Handler(void) { + if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL) { DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TCMPL; i2c1_stop(); @@ -51,8 +45,7 @@ void DMAC_0_Handler( void ) return; } - if (DMAC->Channel[0].CHINTFLAG.bit.TERR) - { + if (DMAC->Channel[0].CHINTFLAG.bit.TERR) { DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TERR; } } @@ -60,118 +53,109 @@ void DMAC_0_Handler( void ) issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; issi3733_led_t led_map[ISSI3733_LED_COUNT] = ISSI3733_LED_MAP; -RGB led_buffer[ISSI3733_LED_COUNT]; +RGB led_buffer[ISSI3733_LED_COUNT]; uint8_t gcr_desired; uint8_t gcr_actual; uint8_t gcr_actual_last; #ifdef USE_MASSDROP_CONFIGURATOR uint8_t gcr_breathe; -float breathe_mult; -float pomod; +float breathe_mult; +float pomod; #endif -#define ACT_GCR_NONE 0 -#define ACT_GCR_INC 1 -#define ACT_GCR_DEC 2 +#define ACT_GCR_NONE 0 +#define ACT_GCR_INC 1 +#define ACT_GCR_DEC 2 #define LED_GCR_STEP_AUTO 2 static uint8_t gcr_min_counter; static uint8_t v_5v_cat_hit; -//WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading -void gcr_compute(void) -{ - uint8_t action = ACT_GCR_NONE; +// WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading +void gcr_compute(void) { + uint8_t action = ACT_GCR_NONE; uint8_t gcr_use = gcr_desired; #ifdef USE_MASSDROP_CONFIGURATOR - if (led_animation_breathing) - { + if (led_animation_breathing) { gcr_use = gcr_breathe; } #endif - //If the 5v takes a catastrophic hit, disable the LED drivers briefly, assert auto gcr mode, min gcr and let the auto take over - if (v_5v < V5_CAT) - { + // If the 5v takes a catastrophic hit, disable the LED drivers briefly, assert auto gcr mode, min gcr and let the auto take over + if (v_5v < V5_CAT) { I2C3733_Control_Set(0); - //CDC_print("USB: WARNING: 5V catastrophic level reached! Disabling LED drivers!\r\n"); //Blocking print is bad here! - v_5v_cat_hit = 20; //~100ms recover - gcr_actual = 0; //Minimize GCR - usb_gcr_auto = 1; //Force auto mode enabled + // CDC_print("USB: WARNING: 5V catastrophic level reached! Disabling LED drivers!\r\n"); //Blocking print is bad here! + v_5v_cat_hit = 20; //~100ms recover + gcr_actual = 0; // Minimize GCR + usb_gcr_auto = 1; // Force auto mode enabled return; - } - else if (v_5v_cat_hit > 1) - { + } else if (v_5v_cat_hit > 1) { v_5v_cat_hit--; return; - } - else if (v_5v_cat_hit == 1) - { + } else if (v_5v_cat_hit == 1) { I2C3733_Control_Set(1); CDC_print("USB: WARNING: Re-enabling LED drivers\r\n"); v_5v_cat_hit = 0; return; } - if (usb_gcr_auto) - { - if (v_5v_avg < V5_LOW) action = ACT_GCR_DEC; - else if (v_5v_avg > V5_HIGH && gcr_actual < gcr_use) action = ACT_GCR_INC; - else if (gcr_actual > gcr_use) action = ACT_GCR_DEC; - } - else - { - if (gcr_actual < gcr_use) action = ACT_GCR_INC; - else if (gcr_actual > gcr_use) action = ACT_GCR_DEC; + if (usb_gcr_auto) { + if (v_5v_avg < V5_LOW) + action = ACT_GCR_DEC; + else if (v_5v_avg > V5_HIGH && gcr_actual < gcr_use) + action = ACT_GCR_INC; + else if (gcr_actual > gcr_use) + action = ACT_GCR_DEC; + } else { + if (gcr_actual < gcr_use) + action = ACT_GCR_INC; + else if (gcr_actual > gcr_use) + action = ACT_GCR_DEC; } - if (action == ACT_GCR_NONE) - { + if (action == ACT_GCR_NONE) { gcr_min_counter = 0; - } - else if (action == ACT_GCR_INC) - { - if (LED_GCR_STEP_AUTO > LED_GCR_MAX - gcr_actual) gcr_actual = LED_GCR_MAX; //Obey max and prevent wrapping - else gcr_actual += LED_GCR_STEP_AUTO; + } else if (action == ACT_GCR_INC) { + if (LED_GCR_STEP_AUTO > LED_GCR_MAX - gcr_actual) + gcr_actual = LED_GCR_MAX; // Obey max and prevent wrapping + else + gcr_actual += LED_GCR_STEP_AUTO; gcr_min_counter = 0; - } - else if (action == ACT_GCR_DEC) - { - if (LED_GCR_STEP_AUTO > gcr_actual) //Prevent wrapping + } else if (action == ACT_GCR_DEC) { + if (LED_GCR_STEP_AUTO > gcr_actual) // Prevent wrapping { gcr_actual = 0; - //At this point, power can no longer be cut from the LED drivers, so focus on cutting out extra port if active - if (usb_extra_state != USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) //If not in a wait for replug state + // At this point, power can no longer be cut from the LED drivers, so focus on cutting out extra port if active + if (usb_extra_state != USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) // If not in a wait for replug state { - if (usb_extra_state == USB_EXTRA_STATE_ENABLED) //If extra usb is enabled + if (usb_extra_state == USB_EXTRA_STATE_ENABLED) // If extra usb is enabled { gcr_min_counter++; - if (gcr_min_counter > 200) //5ms per check = 1s delay + if (gcr_min_counter > 200) // 5ms per check = 1s delay { USB_ExtraSetState(USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG); - usb_extra_manual = 0; //Force disable manual mode of extra port - if (usb_extra_manual) CDC_print("USB: Disabling extra port until replug and manual mode toggle!\r\n"); - else CDC_print("USB: Disabling extra port until replug!\r\n"); + usb_extra_manual = 0; // Force disable manual mode of extra port + if (usb_extra_manual) + CDC_print("USB: Disabling extra port until replug and manual mode toggle!\r\n"); + else + CDC_print("USB: Disabling extra port until replug!\r\n"); } } } - } - else - { - //Power successfully cut back from LED drivers + } else { + // Power successfully cut back from LED drivers gcr_actual -= LED_GCR_STEP_AUTO; gcr_min_counter = 0; #ifdef USE_MASSDROP_CONFIGURATOR - //If breathe mode is active, the top end can fluctuate if the host can not supply enough current - //So set the breathe GCR to where it becomes stable - if (led_animation_breathing == 1) - { + // If breathe mode is active, the top end can fluctuate if the host can not supply enough current + // So set the breathe GCR to where it becomes stable + if (led_animation_breathing == 1) { gcr_breathe = gcr_actual; - //PS: At this point, setting breathing to exhale makes a noticebly shorter cycle + // PS: At this point, setting breathing to exhale makes a noticebly shorter cycle // and the same would happen maybe one or two more times. Therefore I'm favoring // powering through one full breathe and letting gcr settle completely } @@ -180,47 +164,40 @@ void gcr_compute(void) } } -void issi3733_prepare_arrays(void) -{ - memset(issidrv,0,sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT); +void issi3733_prepare_arrays(void) { + memset(issidrv, 0, sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT); - int i; + int i; uint8_t addrs[ISSI3733_DRIVER_COUNT] = ISSI3773_DRIVER_ADDRESSES; - for (i=0;i<ISSI3733_DRIVER_COUNT;i++) - { + for (i = 0; i < ISSI3733_DRIVER_COUNT; i++) { issidrv[i].addr = addrs[i]; } - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) - { - //BYTE: 1 + (SW-1)*16 + (CS-1) - led_map[i].rgb.g = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swg-1)*16 + (led_map[i].adr.cs-1)); - led_map[i].rgb.r = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swr-1)*16 + (led_map[i].adr.cs-1)); - led_map[i].rgb.b = issidrv[led_map[i].adr.drv-1].pwm + 1 + ((led_map[i].adr.swb-1)*16 + (led_map[i].adr.cs-1)); - - //BYTE: 1 + (SW-1)*2 + (CS-1)/8 - //BIT: (CS-1)%8 - *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swg-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8)); - *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swr-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8)); - *(issidrv[led_map[i].adr.drv-1].onoff + 1 + (led_map[i].adr.swb-1)*2+(led_map[i].adr.cs-1)/8) |= (1<<((led_map[i].adr.cs-1)%8)); + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + // BYTE: 1 + (SW-1)*16 + (CS-1) + led_map[i].rgb.g = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swg - 1) * 16 + (led_map[i].adr.cs - 1)); + led_map[i].rgb.r = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swr - 1) * 16 + (led_map[i].adr.cs - 1)); + led_map[i].rgb.b = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swb - 1) * 16 + (led_map[i].adr.cs - 1)); + + // BYTE: 1 + (SW-1)*2 + (CS-1)/8 + // BIT: (CS-1)%8 + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swg - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swr - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swb - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); } } -void led_matrix_prepare(void) -{ - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) - { +void led_matrix_prepare(void) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { *led_map[i].rgb.r = 0; *led_map[i].rgb.g = 0; *led_map[i].rgb.b = 0; } } -void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b) -{ - if (i < ISSI3733_LED_COUNT) - { +void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b) { + if (i < ISSI3733_LED_COUNT) { #ifdef USE_MASSDROP_CONFIGURATOR led_matrix_massdrop_config_override(i); #else @@ -231,16 +208,13 @@ void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b) } } -void led_set_all(uint8_t r, uint8_t g, uint8_t b) -{ - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) - { - led_set_one(i, r, g, b); - } +void led_set_all(uint8_t r, uint8_t g, uint8_t b) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + led_set_one(i, r, g, b); + } } -void init(void) -{ +void init(void) { DBGC(DC_LED_MATRIX_INIT_BEGIN); issi3733_prepare_arrays(); @@ -248,25 +222,28 @@ void init(void) led_matrix_prepare(); gcr_min_counter = 0; - v_5v_cat_hit = 0; + v_5v_cat_hit = 0; DBGC(DC_LED_MATRIX_INIT_COMPLETE); } -void flush(void) -{ +void flush(void) { #ifdef USE_MASSDROP_CONFIGURATOR - if (!led_enabled) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled + if (!led_enabled) { + return; + } // Prevent calculations and I2C traffic if LED drivers are not enabled #else - if (!sr_exp_data.bit.SDB_N) { return; } //Prevent calculations and I2C traffic if LED drivers are not enabled + if (!sr_exp_data.bit.SDB_N) { + return; + } // Prevent calculations and I2C traffic if LED drivers are not enabled #endif // Wait for previous transfer to complete - while (i2c_led_q_running) {} + while (i2c_led_q_running) { + } // Copy buffer to live DMA region - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) - { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { *led_map[i].rgb.r = led_buffer[i].r; *led_map[i].rgb.g = led_buffer[i].g; *led_map[i].rgb.b = led_buffer[i].b; @@ -275,8 +252,7 @@ void flush(void) #ifdef USE_MASSDROP_CONFIGURATOR breathe_mult = 1; - if (led_animation_breathing) - { + if (led_animation_breathing) { //+60us 119 LED led_animation_breathe_cur += BREATHE_STEP * breathe_dir; @@ -285,76 +261,65 @@ void flush(void) else if (led_animation_breathe_cur <= BREATHE_MIN_STEP) breathe_dir = 1; - //Brightness curve created for 256 steps, 0 - ~98% + // Brightness curve created for 256 steps, 0 - ~98% breathe_mult = 0.000015 * led_animation_breathe_cur * led_animation_breathe_cur; - if (breathe_mult > 1) breathe_mult = 1; - else if (breathe_mult < 0) breathe_mult = 0; + if (breathe_mult > 1) + breathe_mult = 1; + else if (breathe_mult < 0) + breathe_mult = 0; } - //This should only be performed once per frame + // This should only be performed once per frame pomod = (float)((g_rgb_counters.tick / 10) % (uint32_t)(1000.0f / led_animation_speed)) / 10.0f * led_animation_speed; pomod *= 100.0f; pomod = (uint32_t)pomod % 10000; pomod /= 100.0f; -#endif // USE_MASSDROP_CONFIGURATOR +#endif // USE_MASSDROP_CONFIGURATOR uint8_t drvid; - //NOTE: GCR does not need to be timed with LED processing, but there is really no harm - if (gcr_actual != gcr_actual_last) - { - for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++) - I2C_LED_Q_GCR(drvid); //Queue data + // NOTE: GCR does not need to be timed with LED processing, but there is really no harm + if (gcr_actual != gcr_actual_last) { + for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_GCR(drvid); // Queue data gcr_actual_last = gcr_actual; } - for (drvid=0;drvid<ISSI3733_DRIVER_COUNT;drvid++) - I2C_LED_Q_PWM(drvid); //Queue data + for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_PWM(drvid); // Queue data i2c_led_q_run(); } -void led_matrix_indicators(void) -{ +void led_matrix_indicators(void) { uint8_t kbled = keyboard_leds(); - if (kbled && rgb_matrix_config.enable) - { - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) - { + if (kbled && rgb_matrix_config.enable) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { if ( - #if USB_LED_NUM_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1<<USB_LED_NUM_LOCK))) || - #endif //NUM LOCK - #if USB_LED_CAPS_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_CAPS_LOCK_SCANCODE && (kbled & (1<<USB_LED_CAPS_LOCK))) || - #endif //CAPS LOCK - #if USB_LED_SCROLL_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1<<USB_LED_SCROLL_LOCK))) || - #endif //SCROLL LOCK - #if USB_LED_COMPOSE_SCANCODE != 255 - (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1<<USB_LED_COMPOSE))) || - #endif //COMPOSE - #if USB_LED_KANA_SCANCODE != 255 - (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1<<USB_LED_KANA))) || - #endif //KANA - (0)) - { +#if USB_LED_NUM_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1 << USB_LED_NUM_LOCK))) || +#endif // NUM LOCK +#if USB_LED_CAPS_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_CAPS_LOCK_SCANCODE && (kbled & (1 << USB_LED_CAPS_LOCK))) || +#endif // CAPS LOCK +#if USB_LED_SCROLL_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1 << USB_LED_SCROLL_LOCK))) || +#endif // SCROLL LOCK +#if USB_LED_COMPOSE_SCANCODE != 255 + (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1 << USB_LED_COMPOSE))) || +#endif // COMPOSE +#if USB_LED_KANA_SCANCODE != 255 + (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1 << USB_LED_KANA))) || +#endif // KANA + (0)) { led_buffer[i].r = 255 - led_buffer[i].r; led_buffer[i].g = 255 - led_buffer[i].g; led_buffer[i].b = 255 - led_buffer[i].b; } } } - } -const rgb_matrix_driver_t rgb_matrix_driver = { - .init = init, - .flush = flush, - .set_color = led_set_one, - .set_color_all = led_set_all -}; +const rgb_matrix_driver_t rgb_matrix_driver = {.init = init, .flush = flush, .set_color = led_set_one, .set_color_all = led_set_all}; /*============================================================================== = Legacy Lighting Support = @@ -365,103 +330,100 @@ const rgb_matrix_driver_t rgb_matrix_driver = { // TODO?: wire these up to keymap.c uint8_t led_animation_orientation = 0; -uint8_t led_animation_direction = 0; -uint8_t led_animation_breathing = 0; -uint8_t led_animation_id = 0; -float led_animation_speed = 4.0f; -uint8_t led_lighting_mode = LED_MODE_NORMAL; -uint8_t led_enabled = 1; +uint8_t led_animation_direction = 0; +uint8_t led_animation_breathing = 0; +uint8_t led_animation_id = 0; +float led_animation_speed = 4.0f; +uint8_t led_lighting_mode = LED_MODE_NORMAL; +uint8_t led_enabled = 1; uint8_t led_animation_breathe_cur = BREATHE_MIN_STEP; -uint8_t breathe_dir = 1; +uint8_t breathe_dir = 1; -static void led_run_pattern(led_setup_t *f, float* ro, float* go, float* bo, float pos) { +static void led_run_pattern(led_setup_t* f, float* ro, float* go, float* bo, float pos) { float po; - while (f->end != 1) - { - po = pos; //Reset po for new frame + while (f->end != 1) { + po = pos; // Reset po for new frame - //Add in any moving effects - if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L))) - { + // Add in any moving effects + if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L))) { po -= pomod; - if (po > 100) po -= 100; - else if (po < 0) po += 100; - } - else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R))) - { + if (po > 100) + po -= 100; + else if (po < 0) + po += 100; + } else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R))) { po += pomod; - if (po > 100) po -= 100; - else if (po < 0) po += 100; + if (po > 100) + po -= 100; + else if (po < 0) + po += 100; } - //Check if LED's po is in current frame - if (po < f->hs) { f++; continue; } - if (po > f->he) { f++; continue; } - //note: < 0 or > 100 continue + // Check if LED's po is in current frame + if (po < f->hs) { + f++; + continue; + } + if (po > f->he) { + f++; + continue; + } + // note: < 0 or > 100 continue - //Calculate the po within the start-stop percentage for color blending + // Calculate the po within the start-stop percentage for color blending po = (po - f->hs) / (f->he - f->hs); - //Add in any color effects - if (f->ef & EF_OVER) - { - *ro = (po * (f->re - f->rs)) + f->rs;// + 0.5; - *go = (po * (f->ge - f->gs)) + f->gs;// + 0.5; - *bo = (po * (f->be - f->bs)) + f->bs;// + 0.5; - } - else if (f->ef & EF_SUBTRACT) - { - *ro -= (po * (f->re - f->rs)) + f->rs;// + 0.5; - *go -= (po * (f->ge - f->gs)) + f->gs;// + 0.5; - *bo -= (po * (f->be - f->bs)) + f->bs;// + 0.5; - } - else - { - *ro += (po * (f->re - f->rs)) + f->rs;// + 0.5; - *go += (po * (f->ge - f->gs)) + f->gs;// + 0.5; - *bo += (po * (f->be - f->bs)) + f->bs;// + 0.5; + // Add in any color effects + if (f->ef & EF_OVER) { + *ro = (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go = (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo = (po * (f->be - f->bs)) + f->bs; // + 0.5; + } else if (f->ef & EF_SUBTRACT) { + *ro -= (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go -= (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo -= (po * (f->be - f->bs)) + f->bs; // + 0.5; + } else { + *ro += (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go += (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo += (po * (f->be - f->bs)) + f->bs; // + 0.5; } f++; } } -static void led_matrix_massdrop_config_override(int i) -{ +static void led_matrix_massdrop_config_override(int i) { float ro = 0; float go = 0; float bo = 0; - float po = (led_animation_orientation) - ? (float)g_led_config.point[i].y / 64.f * 100 - : (float)g_led_config.point[i].x / 224.f * 100; + float po = (led_animation_orientation) ? (float)g_led_config.point[i].y / 64.f * 100 : (float)g_led_config.point[i].x / 224.f * 100; uint8_t highest_active_layer = biton32(layer_state); if (led_lighting_mode == LED_MODE_KEYS_ONLY && HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { - //Do not act on this LED + // Do not act on this LED } else if (led_lighting_mode == LED_MODE_NON_KEYS_ONLY && !HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { - //Do not act on this LED + // Do not act on this LED } else if (led_lighting_mode == LED_MODE_INDICATORS_ONLY) { - //Do not act on this LED (Only show indicators) + // Do not act on this LED (Only show indicators) } else { led_instruction_t* led_cur_instruction = led_instructions; while (!led_cur_instruction->end) { // Check if this applies to current layer - if ((led_cur_instruction->flags & LED_FLAG_MATCH_LAYER) && - (led_cur_instruction->layer != highest_active_layer)) { + if ((led_cur_instruction->flags & LED_FLAG_MATCH_LAYER) && (led_cur_instruction->layer != highest_active_layer)) { goto next_iter; } // Check if this applies to current index if (led_cur_instruction->flags & LED_FLAG_MATCH_ID) { - uint8_t modid = i / 32; //Calculate which id# contains the led bit - uint32_t modidbit = 1 << (i % 32); //Calculate the bit within the id# - uint32_t *bitfield = &led_cur_instruction->id0 + modid; //Add modid as offset to id0 address. *bitfield is now idX of the led id - if (~(*bitfield) & modidbit) { //Check if led bit is not set in idX + uint8_t modid = i / 32; // Calculate which id# contains the led bit + uint32_t modidbit = 1 << (i % 32); // Calculate the bit within the id# + uint32_t* bitfield = &led_cur_instruction->id0 + modid; // Add modid as offset to id0 address. *bitfield is now idX of the led id + if (~(*bitfield) & modidbit) { // Check if led bit is not set in idX goto next_iter; } } @@ -476,16 +438,24 @@ static void led_matrix_massdrop_config_override(int i) led_run_pattern(led_setups[led_animation_id], &ro, &go, &bo, po); } - next_iter: - led_cur_instruction++; + next_iter: + led_cur_instruction++; } - if (ro > 255) ro = 255; else if (ro < 0) ro = 0; - if (go > 255) go = 255; else if (go < 0) go = 0; - if (bo > 255) bo = 255; else if (bo < 0) bo = 0; - - if (led_animation_breathing) - { + if (ro > 255) + ro = 255; + else if (ro < 0) + ro = 0; + if (go > 255) + go = 255; + else if (go < 0) + go = 0; + if (bo > 255) + bo = 255; + else if (bo < 0) + bo = 0; + + if (led_animation_breathing) { ro *= breathe_mult; go *= breathe_mult; bo *= breathe_mult; @@ -497,4 +467,4 @@ static void led_matrix_massdrop_config_override(int i) led_buffer[i].b = (uint8_t)bo; } -#endif // USE_MASSDROP_CONFIGURATOR +#endif // USE_MASSDROP_CONFIGURATOR diff --git a/tmk_core/protocol/arm_atsam/led_matrix.h b/tmk_core/protocol/arm_atsam/led_matrix.h index 1316efd9a..8eaa5623b 100644 --- a/tmk_core/protocol/arm_atsam/led_matrix.h +++ b/tmk_core/protocol/arm_atsam/led_matrix.h @@ -20,20 +20,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "quantum.h" -//From keyboard +// From keyboard #include "config_led.h" -//CS1-CS16 Current Source "Col" +// CS1-CS16 Current Source "Col" #define ISSI3733_CS_COUNT 16 -//SW1-SW12 Switch "Row" +// SW1-SW12 Switch "Row" #define ISSI3733_SW_COUNT 12 -#define ISSI3733_LED_RGB_COUNT ISSI3733_CS_COUNT * ISSI3733_SW_COUNT -#define ISSI3733_PG0_BYTES ISSI3733_LED_RGB_COUNT / 8 + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG1_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG2_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG3_BYTES 18 + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_LED_RGB_COUNT ISSI3733_CS_COUNT *ISSI3733_SW_COUNT +#define ISSI3733_PG0_BYTES ISSI3733_LED_RGB_COUNT / 8 + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG1_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG2_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG3_BYTES 18 + 1 //+1 for first byte being memory start offset for I2C transfer #define ISSI3733_PG_ONOFF_BYTES ISSI3733_PG0_BYTES #define ISSI3733_PG_OR_BYTES ISSI3733_PG0_BYTES @@ -43,38 +43,38 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define ISSI3733_PG_FN_BYTES ISSI3733_PG3_BYTES typedef struct issi3733_driver_s { - uint8_t addr; //Address of the driver according to wiring "ISSI3733: Table 1 Slave Address" - uint8_t onoff[ISSI3733_PG_ONOFF_BYTES]; //PG0 - LED Control Register - LED On/Off Register - uint8_t open[ISSI3733_PG_OR_BYTES]; //PG0 - LED Control Register - LED Open Register - uint8_t shrt[ISSI3733_PG_SR_BYTES]; //PG0 - LED Control Register - LED Short Register - uint8_t pwm[ISSI3733_PG_PWM_BYTES]; //PG1 - PWM Register - uint8_t abm[ISSI3733_PG_ABM_BYTES]; //PG2 - Auto Breath Mode Register - uint8_t conf[ISSI3733_PG_FN_BYTES]; //PG3 - Function Register + uint8_t addr; // Address of the driver according to wiring "ISSI3733: Table 1 Slave Address" + uint8_t onoff[ISSI3733_PG_ONOFF_BYTES]; // PG0 - LED Control Register - LED On/Off Register + uint8_t open[ISSI3733_PG_OR_BYTES]; // PG0 - LED Control Register - LED Open Register + uint8_t shrt[ISSI3733_PG_SR_BYTES]; // PG0 - LED Control Register - LED Short Register + uint8_t pwm[ISSI3733_PG_PWM_BYTES]; // PG1 - PWM Register + uint8_t abm[ISSI3733_PG_ABM_BYTES]; // PG2 - Auto Breath Mode Register + uint8_t conf[ISSI3733_PG_FN_BYTES]; // PG3 - Function Register } issi3733_driver_t; typedef struct issi3733_rgb_s { - uint8_t *r; //Direct access into PWM data - uint8_t *g; //Direct access into PWM data - uint8_t *b; //Direct access into PWM data + uint8_t *r; // Direct access into PWM data + uint8_t *g; // Direct access into PWM data + uint8_t *b; // Direct access into PWM data } issi3733_rgb_t; typedef struct issi3733_rgb_adr_s { - uint8_t drv; //Driver from given list - uint8_t cs; //CS - uint8_t swr; //SW Red - uint8_t swg; //SW Green - uint8_t swb; //SW Blue + uint8_t drv; // Driver from given list + uint8_t cs; // CS + uint8_t swr; // SW Red + uint8_t swg; // SW Green + uint8_t swb; // SW Blue } issi3733_rgb_adr_t; typedef struct issi3733_led_s { - uint8_t id; //According to PCB ref - issi3733_rgb_t rgb; //PWM settings of R G B - issi3733_rgb_adr_t adr; //Hardware addresses - float x; //Physical position X - float y; //Physical position Y - float px; //Physical position X in percent - float py; //Physical position Y in percent - uint8_t scan; //Key scan code from wiring (set 0xFF if no key) + uint8_t id; // According to PCB ref + issi3733_rgb_t rgb; // PWM settings of R G B + issi3733_rgb_adr_t adr; // Hardware addresses + float x; // Physical position X + float y; // Physical position Y + float px; // Physical position X in percent + float py; // Physical position Y in percent + uint8_t scan; // Key scan code from wiring (set 0xFF if no key) } issi3733_led_t; extern issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; @@ -92,67 +92,67 @@ void led_matrix_indicators(void); #ifdef USE_MASSDROP_CONFIGURATOR -#define EF_NONE 0x00000000 //No effect -#define EF_OVER 0x00000001 //Overwrite any previous color information with new -#define EF_SCR_L 0x00000002 //Scroll left -#define EF_SCR_R 0x00000004 //Scroll right -#define EF_SUBTRACT 0x00000008 //Subtract color values +# define EF_NONE 0x00000000 // No effect +# define EF_OVER 0x00000001 // Overwrite any previous color information with new +# define EF_SCR_L 0x00000002 // Scroll left +# define EF_SCR_R 0x00000004 // Scroll right +# define EF_SUBTRACT 0x00000008 // Subtract color values typedef struct led_setup_s { - float hs; //Band begin at percent - float he; //Band end at percent - uint8_t rs; //Red start value - uint8_t re; //Red end value - uint8_t gs; //Green start value - uint8_t ge; //Green end value - uint8_t bs; //Blue start value - uint8_t be; //Blue end value - uint32_t ef; //Animation and color effects - uint8_t end; //Set to signal end of the setup + float hs; // Band begin at percent + float he; // Band end at percent + uint8_t rs; // Red start value + uint8_t re; // Red end value + uint8_t gs; // Green start value + uint8_t ge; // Green end value + uint8_t bs; // Blue start value + uint8_t be; // Blue end value + uint32_t ef; // Animation and color effects + uint8_t end; // Set to signal end of the setup } led_setup_t; extern const uint8_t led_setups_count; -extern void *led_setups[]; +extern void * led_setups[]; -//LED Extra Instructions -#define LED_FLAG_NULL 0x00 //Matching and coloring not used (default) -#define LED_FLAG_MATCH_ID 0x01 //Match on the ID of the LED (set id#'s to desired bit pattern, first LED is id 1) -#define LED_FLAG_MATCH_LAYER 0x02 //Match on the current active layer (set layer to desired match layer) -#define LED_FLAG_USE_RGB 0x10 //Use a specific RGB value (set r, g, b to desired output color values) -#define LED_FLAG_USE_PATTERN 0x20 //Use a specific pattern ID (set pattern_id to desired output pattern) -#define LED_FLAG_USE_ROTATE_PATTERN 0x40 //Use pattern the user has cycled to manually +// LED Extra Instructions +# define LED_FLAG_NULL 0x00 // Matching and coloring not used (default) +# define LED_FLAG_MATCH_ID 0x01 // Match on the ID of the LED (set id#'s to desired bit pattern, first LED is id 1) +# define LED_FLAG_MATCH_LAYER 0x02 // Match on the current active layer (set layer to desired match layer) +# define LED_FLAG_USE_RGB 0x10 // Use a specific RGB value (set r, g, b to desired output color values) +# define LED_FLAG_USE_PATTERN 0x20 // Use a specific pattern ID (set pattern_id to desired output pattern) +# define LED_FLAG_USE_ROTATE_PATTERN 0x40 // Use pattern the user has cycled to manually typedef struct led_instruction_s { - uint16_t flags; // Bitfield for LED instructions - uint32_t id0; // Bitwise id, IDs 0-31 - uint32_t id1; // Bitwise id, IDs 32-63 - uint32_t id2; // Bitwise id, IDs 64-95 - uint32_t id3; // Bitwise id, IDs 96-127 - uint8_t layer; - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t pattern_id; - uint8_t end; + uint16_t flags; // Bitfield for LED instructions + uint32_t id0; // Bitwise id, IDs 0-31 + uint32_t id1; // Bitwise id, IDs 32-63 + uint32_t id2; // Bitwise id, IDs 64-95 + uint32_t id3; // Bitwise id, IDs 96-127 + uint8_t layer; + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t pattern_id; + uint8_t end; } led_instruction_t; extern led_instruction_t led_instructions[]; extern uint8_t led_animation_breathing; extern uint8_t led_animation_id; -extern float led_animation_speed; +extern float led_animation_speed; extern uint8_t led_lighting_mode; extern uint8_t led_enabled; extern uint8_t led_animation_breathe_cur; extern uint8_t led_animation_direction; extern uint8_t breathe_dir; -#define LED_MODE_NORMAL 0 //Must be 0 -#define LED_MODE_KEYS_ONLY 1 -#define LED_MODE_NON_KEYS_ONLY 2 -#define LED_MODE_INDICATORS_ONLY 3 -#define LED_MODE_MAX_INDEX LED_MODE_INDICATORS_ONLY //Must be highest value +# define LED_MODE_NORMAL 0 // Must be 0 +# define LED_MODE_KEYS_ONLY 1 +# define LED_MODE_NON_KEYS_ONLY 2 +# define LED_MODE_INDICATORS_ONLY 3 +# define LED_MODE_MAX_INDEX LED_MODE_INDICATORS_ONLY // Must be highest value -#endif // USE_MASSDROP_CONFIGURATOR +#endif // USE_MASSDROP_CONFIGURATOR -#endif //_LED_MATRIX_H_ +#endif //_LED_MATRIX_H_ diff --git a/tmk_core/protocol/arm_atsam/led_matrix_programs.c b/tmk_core/protocol/arm_atsam/led_matrix_programs.c index cf7478dc3..360102ba8 100644 --- a/tmk_core/protocol/arm_atsam/led_matrix_programs.c +++ b/tmk_core/protocol/arm_atsam/led_matrix_programs.c @@ -17,106 +17,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifdef USE_MASSDROP_CONFIGURATOR -#include "led_matrix.h" +# include "led_matrix.h" -//Teal <-> Salmon +// Teal <-> Salmon led_setup_t leds_teal_salmon[] = { - { .hs = 0, .he = 33, .rs = 24, .re = 24, .gs = 215, .ge = 215, .bs = 204, .be = 204, .ef = EF_NONE }, - { .hs = 33, .he = 66, .rs = 24, .re = 255, .gs = 215, .ge = 114, .bs = 204, .be = 118, .ef = EF_NONE }, - { .hs = 66, .he = 100, .rs = 255, .re = 255, .gs = 114, .ge = 114, .bs = 118, .be = 118, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 33, .rs = 24, .re = 24, .gs = 215, .ge = 215, .bs = 204, .be = 204, .ef = EF_NONE}, + {.hs = 33, .he = 66, .rs = 24, .re = 255, .gs = 215, .ge = 114, .bs = 204, .be = 118, .ef = EF_NONE}, + {.hs = 66, .he = 100, .rs = 255, .re = 255, .gs = 114, .ge = 114, .bs = 118, .be = 118, .ef = EF_NONE}, + {.end = 1}, }; -//Yellow +// Yellow led_setup_t leds_yellow[] = { - { .hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, }; -//Off +// Off led_setup_t leds_off[] = { - { .hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, }; -//Red +// Red led_setup_t leds_red[] = { - { .hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, }; -//Green +// Green led_setup_t leds_green[] = { - { .hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, }; -//Blue +// Blue led_setup_t leds_blue[] = { - { .hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_NONE}, + {.end = 1}, }; -//White +// White led_setup_t leds_white[] = { - { .hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, + {.end = 1}, }; -//White with moving red stripe +// White with moving red stripe led_setup_t leds_white_with_red_stripe[] = { - { .hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE }, - { .hs = 0, .he = 15, .rs = 0, .re = 0, .gs = 0, .ge = 255, .bs = 0, .be = 255, .ef = EF_SCR_R | EF_SUBTRACT }, - { .hs = 15, .he = 30, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 0, .ef = EF_SCR_R | EF_SUBTRACT }, - { .end = 1 }, + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, + {.hs = 0, .he = 15, .rs = 0, .re = 0, .gs = 0, .ge = 255, .bs = 0, .be = 255, .ef = EF_SCR_R | EF_SUBTRACT}, + {.hs = 15, .he = 30, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 0, .ef = EF_SCR_R | EF_SUBTRACT}, + {.end = 1}, }; -//Black with moving red stripe +// Black with moving red stripe led_setup_t leds_black_with_red_stripe[] = { - { .hs = 0, .he = 15, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R }, - { .hs = 15, .he = 30, .rs = 255, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R }, - { .end = 1 }, + {.hs = 0, .he = 15, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, + {.hs = 15, .he = 30, .rs = 255, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, + {.end = 1}, }; -//Rainbow no scrolling +// Rainbow no scrolling led_setup_t leds_rainbow_ns[] = { - { .hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER }, - { .hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER }, - { .hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER }, - { .hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER }, - { .hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER }, - { .hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER }, - { .end = 1 }, + {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER}, {.end = 1}, }; -//Rainbow scrolling +// Rainbow scrolling led_setup_t leds_rainbow_s[] = { - { .hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R }, - { .hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R }, - { .hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER | EF_SCR_R }, - { .hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R }, - { .hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R }, - { .hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER | EF_SCR_R }, - { .end = 1 }, + {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.end = 1}, }; -//Add new LED animations here using one from above as example -//The last entry must be { .end = 1 } -//Add the new animation name to the list below following its format - -void *led_setups[] = { - leds_rainbow_s, - leds_rainbow_ns, - leds_teal_salmon, - leds_yellow, - leds_red, - leds_green, - leds_blue, - leds_white, - leds_white_with_red_stripe, - leds_black_with_red_stripe, - leds_off -}; +// Add new LED animations here using one from above as example +// The last entry must be { .end = 1 } +// Add the new animation name to the list below following its format + +void *led_setups[] = {leds_rainbow_s, leds_rainbow_ns, leds_teal_salmon, leds_yellow, leds_red, leds_green, leds_blue, leds_white, leds_white_with_red_stripe, leds_black_with_red_stripe, leds_off}; const uint8_t led_setups_count = sizeof(led_setups) / sizeof(led_setups[0]); diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.c b/tmk_core/protocol/arm_atsam/main_arm_atsam.c index 0974a230d..a42d9daf3 100644 --- a/tmk_core/protocol/arm_atsam/main_arm_atsam.c +++ b/tmk_core/protocol/arm_atsam/main_arm_atsam.c @@ -25,50 +25,43 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <string.h> #include "quantum.h" -//From protocol directory +// From protocol directory #include "arm_atsam_protocol.h" -//From keyboard's directory +// From keyboard's directory #include "config_led.h" -uint8_t g_usb_state = USB_FSMSTATUS_FSMSTATE_OFF_Val; //Saved USB state from hardware value to detect changes +uint8_t g_usb_state = USB_FSMSTATUS_FSMSTATE_OFF_Val; // Saved USB state from hardware value to detect changes -void main_subtasks(void); +void main_subtasks(void); uint8_t keyboard_leds(void); -void send_keyboard(report_keyboard_t *report); -void send_mouse(report_mouse_t *report); -void send_system(uint16_t data); -void send_consumer(uint16_t data); - -host_driver_t arm_atsam_driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; +void send_keyboard(report_keyboard_t *report); +void send_mouse(report_mouse_t *report); +void send_system(uint16_t data); +void send_consumer(uint16_t data); + +host_driver_t arm_atsam_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; uint8_t led_states; -uint8_t keyboard_leds(void) -{ +uint8_t keyboard_leds(void) { #ifdef NKRO_ENABLE if (keymap_config.nkro) return udi_hid_nkro_report_set; else -#endif //NKRO_ENABLE +#endif // NKRO_ENABLE return udi_hid_kbd_report_set; } -void send_keyboard(report_keyboard_t *report) -{ +void send_keyboard(report_keyboard_t *report) { uint32_t irqflags; #ifdef NKRO_ENABLE - if (!keymap_config.nkro) - { -#endif //NKRO_ENABLE - while (udi_hid_kbd_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free + if (!keymap_config.nkro) { +#endif // NKRO_ENABLE + while (udi_hid_kbd_b_report_trans_ongoing) { + main_subtasks(); + } // Run other tasks while waiting for USB to be free irqflags = __get_PRIMASK(); __disable_irq(); @@ -81,10 +74,10 @@ void send_keyboard(report_keyboard_t *report) __DMB(); __set_PRIMASK(irqflags); #ifdef NKRO_ENABLE - } - else - { - while (udi_hid_nkro_b_report_trans_ongoing) { main_subtasks(); } //Run other tasks while waiting for USB to be free + } else { + while (udi_hid_nkro_b_report_trans_ongoing) { + main_subtasks(); + } // Run other tasks while waiting for USB to be free irqflags = __get_PRIMASK(); __disable_irq(); @@ -97,11 +90,10 @@ void send_keyboard(report_keyboard_t *report) __DMB(); __set_PRIMASK(irqflags); } -#endif //NKRO_ENABLE +#endif // NKRO_ENABLE } -void send_mouse(report_mouse_t *report) -{ +void send_mouse(report_mouse_t *report) { #ifdef MOUSEKEY_ENABLE uint32_t irqflags; @@ -115,11 +107,10 @@ void send_mouse(report_mouse_t *report) __DMB(); __set_PRIMASK(irqflags); -#endif //MOUSEKEY_ENABLE +#endif // MOUSEKEY_ENABLE } -void send_system(uint16_t data) -{ +void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint32_t irqflags; @@ -130,16 +121,15 @@ void send_system(uint16_t data) udi_hid_exk_report.desc.report_id = REPORT_ID_SYSTEM; if (data != 0) data = data - SYSTEM_POWER_DOWN + 1; udi_hid_exk_report.desc.report_data = data; - udi_hid_exk_b_report_valid = 1; + udi_hid_exk_b_report_valid = 1; udi_hid_exk_send_report(); __DMB(); __set_PRIMASK(irqflags); -#endif //EXTRAKEY_ENABLE +#endif // EXTRAKEY_ENABLE } -void send_consumer(uint16_t data) -{ +void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint32_t irqflags; @@ -147,71 +137,64 @@ void send_consumer(uint16_t data) __disable_irq(); __DMB(); - udi_hid_exk_report.desc.report_id = REPORT_ID_CONSUMER; + udi_hid_exk_report.desc.report_id = REPORT_ID_CONSUMER; udi_hid_exk_report.desc.report_data = data; - udi_hid_exk_b_report_valid = 1; + udi_hid_exk_b_report_valid = 1; udi_hid_exk_send_report(); __DMB(); __set_PRIMASK(irqflags); -#endif //EXTRAKEY_ENABLE +#endif // EXTRAKEY_ENABLE } -void main_subtask_usb_state(void) -{ - static uint64_t fsmstate_on_delay = 0; //Delay timer to be sure USB is actually operating before bringing up hardware - uint8_t fsmstate_now = USB->DEVICE.FSMSTATUS.reg; //Current state from hardware register +void main_subtask_usb_state(void) { + static uint64_t fsmstate_on_delay = 0; // Delay timer to be sure USB is actually operating before bringing up hardware + uint8_t fsmstate_now = USB->DEVICE.FSMSTATUS.reg; // Current state from hardware register - if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val) //If USB SUSPENDED + if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val) // If USB SUSPENDED { - fsmstate_on_delay = 0; //Clear ON delay timer + fsmstate_on_delay = 0; // Clear ON delay timer - if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SUSPEND_Val) //If previously not SUSPENDED + if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SUSPEND_Val) // If previously not SUSPENDED { - suspend_power_down(); //Run suspend routine - g_usb_state = fsmstate_now; //Save current USB state + suspend_power_down(); // Run suspend routine + g_usb_state = fsmstate_now; // Save current USB state } - } - else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SLEEP_Val) //Else if USB SLEEPING + } else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_SLEEP_Val) // Else if USB SLEEPING { - fsmstate_on_delay = 0; //Clear ON delay timer + fsmstate_on_delay = 0; // Clear ON delay timer - if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SLEEP_Val) //If previously not SLEEPING + if (g_usb_state != USB_FSMSTATUS_FSMSTATE_SLEEP_Val) // If previously not SLEEPING { - suspend_power_down(); //Run suspend routine - g_usb_state = fsmstate_now; //Save current USB state + suspend_power_down(); // Run suspend routine + g_usb_state = fsmstate_now; // Save current USB state } - } - else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_ON_Val) //Else if USB ON + } else if (fsmstate_now == USB_FSMSTATUS_FSMSTATE_ON_Val) // Else if USB ON { - if (g_usb_state != USB_FSMSTATUS_FSMSTATE_ON_Val) //If previously not ON + if (g_usb_state != USB_FSMSTATUS_FSMSTATE_ON_Val) // If previously not ON { - if (fsmstate_on_delay == 0) //If ON delay timer is cleared + if (fsmstate_on_delay == 0) // If ON delay timer is cleared { - fsmstate_on_delay = timer_read64() + 250; //Set ON delay timer - } - else if (timer_read64() > fsmstate_on_delay) //Else if ON delay timer is active and timed out + fsmstate_on_delay = timer_read64() + 250; // Set ON delay timer + } else if (timer_read64() > fsmstate_on_delay) // Else if ON delay timer is active and timed out { - suspend_wakeup_init(); //Run wakeup routine - g_usb_state = fsmstate_now; //Save current USB state + suspend_wakeup_init(); // Run wakeup routine + g_usb_state = fsmstate_now; // Save current USB state } } - } - else //Else if USB is in a state not being tracked + } else // Else if USB is in a state not being tracked { - fsmstate_on_delay = 0; //Clear ON delay timer + fsmstate_on_delay = 0; // Clear ON delay timer } } -void main_subtask_power_check(void) -{ +void main_subtask_power_check(void) { static uint64_t next_5v_checkup = 0; - if (timer_read64() > next_5v_checkup) - { + if (timer_read64() > next_5v_checkup) { next_5v_checkup = timer_read64() + 5; - v_5v = adc_get(ADC_5V); + v_5v = adc_get(ADC_5V); v_5v_avg = 0.9 * v_5v_avg + 0.1 * v_5v; #ifdef RGB_MATRIX_ENABLE @@ -220,27 +203,23 @@ void main_subtask_power_check(void) } } -void main_subtask_usb_extra_device(void) -{ +void main_subtask_usb_extra_device(void) { static uint64_t next_usb_checkup = 0; - if (timer_read64() > next_usb_checkup) - { + if (timer_read64() > next_usb_checkup) { next_usb_checkup = timer_read64() + 10; USB_HandleExtraDevice(); } } -void main_subtasks(void) -{ +void main_subtasks(void) { main_subtask_usb_state(); main_subtask_power_check(); main_subtask_usb_extra_device(); } -int main(void) -{ +int main(void) { DBG_LED_ENA; DBG_1_ENA; DBG_1_OFF; @@ -259,7 +238,7 @@ int main(void) #ifdef RGB_MATRIX_ENABLE i2c1_init(); -#endif // RGB_MATRIX_ENABLE +#endif // RGB_MATRIX_ENABLE matrix_init(); @@ -273,21 +252,23 @@ int main(void) CDC_init(); DBGC(DC_MAIN_CDC_INIT_COMPLETE); - while (USB2422_Port_Detect_Init() == 0) {} + while (USB2422_Port_Detect_Init() == 0) { + } DBG_LED_OFF; #ifdef RGB_MATRIX_ENABLE - while (I2C3733_Init_Control() != 1) {} - while (I2C3733_Init_Drivers() != 1) {} + while (I2C3733_Init_Control() != 1) { + } + while (I2C3733_Init_Drivers() != 1) { + } I2C_DMAC_LED_Init(); i2c_led_q_init(); - for (uint8_t drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) - I2C_LED_Q_ONOFF(drvid); //Queue data -#endif // RGB_MATRIX_ENABLE + for (uint8_t drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_ONOFF(drvid); // Queue data +#endif // RGB_MATRIX_ENABLE keyboard_setup(); @@ -297,21 +278,18 @@ int main(void) #ifdef CONSOLE_ENABLE uint64_t next_print = 0; -#endif //CONSOLE_ENABLE +#endif // CONSOLE_ENABLE v_5v_avg = adc_get(ADC_5V); debug_code_disable(); - while (1) - { - main_subtasks(); //Note these tasks will also be run while waiting for USB keyboard polling intervals + while (1) { + main_subtasks(); // Note these tasks will also be run while waiting for USB keyboard polling intervals - if (g_usb_state == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val || g_usb_state == USB_FSMSTATUS_FSMSTATE_SLEEP_Val) - { - if (suspend_wakeup_condition()) - { - udc_remotewakeup(); //Send remote wakeup signal + if (g_usb_state == USB_FSMSTATUS_FSMSTATE_SUSPEND_Val || g_usb_state == USB_FSMSTATUS_FSMSTATE_SLEEP_Val) { + if (suspend_wakeup_condition()) { + udc_remotewakeup(); // Send remote wakeup signal wait_ms(50); } @@ -321,16 +299,13 @@ int main(void) keyboard_task(); #ifdef CONSOLE_ENABLE - if (timer_read64() > next_print) - { + if (timer_read64() > next_print) { next_print = timer_read64() + 250; - //Add any debug information here that you want to see very often - //dprintf("5v=%u 5vu=%u dlow=%u dhi=%u gca=%u gcd=%u\r\n", v_5v, v_5v_avg, v_5v_avg - V5_LOW, v_5v_avg - V5_HIGH, gcr_actual, gcr_desired); + // Add any debug information here that you want to see very often + // dprintf("5v=%u 5vu=%u dlow=%u dhi=%u gca=%u gcd=%u\r\n", v_5v, v_5v_avg, v_5v_avg - V5_LOW, v_5v_avg - V5_HIGH, gcr_actual, gcr_desired); } -#endif //CONSOLE_ENABLE +#endif // CONSOLE_ENABLE } - return 1; } - diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.h b/tmk_core/protocol/arm_atsam/main_arm_atsam.h index 78205e2e1..5b088f0e2 100644 --- a/tmk_core/protocol/arm_atsam/main_arm_atsam.h +++ b/tmk_core/protocol/arm_atsam/main_arm_atsam.h @@ -20,4 +20,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. uint8_t keyboard_leds(void); -#endif //_MAIN_ARM_ATSAM_H_ +#endif //_MAIN_ARM_ATSAM_H_ diff --git a/tmk_core/protocol/arm_atsam/md_bootloader.h b/tmk_core/protocol/arm_atsam/md_bootloader.h index 6b80ef492..362b9bd52 100644 --- a/tmk_core/protocol/arm_atsam/md_bootloader.h +++ b/tmk_core/protocol/arm_atsam/md_bootloader.h @@ -5,20 +5,20 @@ extern uint32_t _srom; extern uint32_t _lrom; extern uint32_t _erom; -#define BOOTLOADER_SERIAL_MAX_SIZE 20 //DO NOT MODIFY! +#define BOOTLOADER_SERIAL_MAX_SIZE 20 // DO NOT MODIFY! #ifdef KEYBOARD_massdrop_ctrl -//WARNING: These are only for CTRL bootloader release "v2.18Jun 22 2018 17:28:08" for bootloader_jump support +// WARNING: These are only for CTRL bootloader release "v2.18Jun 22 2018 17:28:08" for bootloader_jump support extern uint32_t _eram; -#define BOOTLOADER_MAGIC 0x3B9ACA00 -#define MAGIC_ADDR (uint32_t *)((intptr_t)(&_eram) - 4) +# define BOOTLOADER_MAGIC 0x3B9ACA00 +# define MAGIC_ADDR (uint32_t *)((intptr_t)(&_eram) - 4) #endif #ifdef MD_BOOTLOADER -#define MCU_HZ 48000000 -#define I2C_HZ 0 //Not used +# define MCU_HZ 48000000 +# define I2C_HZ 0 // Not used -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER -#endif //_MD_BOOTLOADER_H_ +#endif //_MD_BOOTLOADER_H_ diff --git a/tmk_core/protocol/arm_atsam/spi.c b/tmk_core/protocol/arm_atsam/spi.c index e275ba13f..3b118bc1f 100644 --- a/tmk_core/protocol/arm_atsam/spi.c +++ b/tmk_core/protocol/arm_atsam/spi.c @@ -19,69 +19,74 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. sr_exp_t sr_exp_data; -void SR_EXP_WriteData(void) -{ +void SR_EXP_WriteData(void) { SR_EXP_RCLK_LO; - while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.DRE)) { DBGC(DC_SPI_WRITE_DRE); } + while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.DRE)) { + DBGC(DC_SPI_WRITE_DRE); + } - SR_EXP_SERCOM->SPI.DATA.bit.DATA = sr_exp_data.reg & 0xFF; //Shift in bits 7-0 - while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { DBGC(DC_SPI_WRITE_TXC_1); } + SR_EXP_SERCOM->SPI.DATA.bit.DATA = sr_exp_data.reg & 0xFF; // Shift in bits 7-0 + while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { + DBGC(DC_SPI_WRITE_TXC_1); + } - SR_EXP_SERCOM->SPI.DATA.bit.DATA = (sr_exp_data.reg >> 8) & 0xFF; //Shift in bits 15-8 - while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { DBGC(DC_SPI_WRITE_TXC_2); } + SR_EXP_SERCOM->SPI.DATA.bit.DATA = (sr_exp_data.reg >> 8) & 0xFF; // Shift in bits 15-8 + while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { + DBGC(DC_SPI_WRITE_TXC_2); + } SR_EXP_RCLK_HI; } -void SR_EXP_Init(void) -{ +void SR_EXP_Init(void) { DBGC(DC_SPI_INIT_BEGIN); CLK_set_spi_freq(CHAN_SERCOM_SPI, FREQ_SPI_DEFAULT); - //Set up MCU Shift Register pins + // Set up MCU Shift Register pins PORT->Group[SR_EXP_RCLK_PORT].DIRSET.reg = (1 << SR_EXP_RCLK_PIN); PORT->Group[SR_EXP_OE_N_PORT].DIRSET.reg = (1 << SR_EXP_OE_N_PIN); - - //Set up MCU SPI pins - PORT->Group[SR_EXP_DATAOUT_PORT].PMUX[SR_EXP_DATAOUT_PIN / 2].bit.SR_EXP_DATAOUT_MUX_SEL = SR_EXP_DATAOUT_MUX; //MUX select for sercom - PORT->Group[SR_EXP_SCLK_PORT].PMUX[SR_EXP_SCLK_PIN / 2].bit.SR_EXP_SCLK_MUX_SEL = SR_EXP_SCLK_MUX; //MUX select for sercom - PORT->Group[SR_EXP_DATAOUT_PORT].PINCFG[SR_EXP_DATAOUT_PIN].bit.PMUXEN = 1; //MUX Enable - PORT->Group[SR_EXP_SCLK_PORT].PINCFG[SR_EXP_SCLK_PIN].bit.PMUXEN = 1; //MUX Enable - - //Initialize Shift Register + + // Set up MCU SPI pins + PORT->Group[SR_EXP_DATAOUT_PORT].PMUX[SR_EXP_DATAOUT_PIN / 2].bit.SR_EXP_DATAOUT_MUX_SEL = SR_EXP_DATAOUT_MUX; // MUX select for sercom + PORT->Group[SR_EXP_SCLK_PORT].PMUX[SR_EXP_SCLK_PIN / 2].bit.SR_EXP_SCLK_MUX_SEL = SR_EXP_SCLK_MUX; // MUX select for sercom + PORT->Group[SR_EXP_DATAOUT_PORT].PINCFG[SR_EXP_DATAOUT_PIN].bit.PMUXEN = 1; // MUX Enable + PORT->Group[SR_EXP_SCLK_PORT].PINCFG[SR_EXP_SCLK_PIN].bit.PMUXEN = 1; // MUX Enable + + // Initialize Shift Register SR_EXP_OE_N_DIS; SR_EXP_RCLK_HI; - SR_EXP_SERCOM->SPI.CTRLA.bit.DORD = 1; //Data Order - LSB is transferred first - SR_EXP_SERCOM->SPI.CTRLA.bit.CPOL = 1; //Clock Polarity - SCK high when idle. Leading edge of cycle is falling. Trailing rising. - SR_EXP_SERCOM->SPI.CTRLA.bit.CPHA = 1; //Clock Phase - Leading Edge Falling, change, Trailing Edge - Rising, sample - SR_EXP_SERCOM->SPI.CTRLA.bit.DIPO = 3; //Data In Pinout - SERCOM PAD[3] is used as data input (Configure away from DOPO. Not using input.) - SR_EXP_SERCOM->SPI.CTRLA.bit.DOPO = 0; //Data Output PAD[0], Serial Clock PAD[1] - SR_EXP_SERCOM->SPI.CTRLA.bit.MODE = 3; //Operating Mode - Master operation + SR_EXP_SERCOM->SPI.CTRLA.bit.DORD = 1; // Data Order - LSB is transferred first + SR_EXP_SERCOM->SPI.CTRLA.bit.CPOL = 1; // Clock Polarity - SCK high when idle. Leading edge of cycle is falling. Trailing rising. + SR_EXP_SERCOM->SPI.CTRLA.bit.CPHA = 1; // Clock Phase - Leading Edge Falling, change, Trailing Edge - Rising, sample + SR_EXP_SERCOM->SPI.CTRLA.bit.DIPO = 3; // Data In Pinout - SERCOM PAD[3] is used as data input (Configure away from DOPO. Not using input.) + SR_EXP_SERCOM->SPI.CTRLA.bit.DOPO = 0; // Data Output PAD[0], Serial Clock PAD[1] + SR_EXP_SERCOM->SPI.CTRLA.bit.MODE = 3; // Operating Mode - Master operation - SR_EXP_SERCOM->SPI.CTRLA.bit.ENABLE = 1; //Enable - Peripheral is enabled or being enabled - while (SR_EXP_SERCOM->SPI.SYNCBUSY.bit.ENABLE) { DBGC(DC_SPI_SYNC_ENABLING); } + SR_EXP_SERCOM->SPI.CTRLA.bit.ENABLE = 1; // Enable - Peripheral is enabled or being enabled + while (SR_EXP_SERCOM->SPI.SYNCBUSY.bit.ENABLE) { + DBGC(DC_SPI_SYNC_ENABLING); + } - sr_exp_data.reg = 0; + sr_exp_data.reg = 0; sr_exp_data.bit.HUB_CONNECT = 0; sr_exp_data.bit.HUB_RESET_N = 0; - sr_exp_data.bit.S_UP = 0; - sr_exp_data.bit.E_UP_N = 1; - sr_exp_data.bit.S_DN1 = 1; - sr_exp_data.bit.E_DN1_N = 1; - sr_exp_data.bit.E_VBUS_1 = 0; - sr_exp_data.bit.E_VBUS_2 = 0; - sr_exp_data.bit.SRC_1 = 1; - sr_exp_data.bit.SRC_2 = 1; - sr_exp_data.bit.IRST = 1; - sr_exp_data.bit.SDB_N = 0; + sr_exp_data.bit.S_UP = 0; + sr_exp_data.bit.E_UP_N = 1; + sr_exp_data.bit.S_DN1 = 1; + sr_exp_data.bit.E_DN1_N = 1; + sr_exp_data.bit.E_VBUS_1 = 0; + sr_exp_data.bit.E_VBUS_2 = 0; + sr_exp_data.bit.SRC_1 = 1; + sr_exp_data.bit.SRC_2 = 1; + sr_exp_data.bit.IRST = 1; + sr_exp_data.bit.SDB_N = 0; SR_EXP_WriteData(); - //Enable Shift Register output + // Enable Shift Register output SR_EXP_OE_N_ENA; DBGC(DC_SPI_INIT_COMPLETE); } - diff --git a/tmk_core/protocol/arm_atsam/spi.h b/tmk_core/protocol/arm_atsam/spi.h index 4739b775d..dcd45f31a 100644 --- a/tmk_core/protocol/arm_atsam/spi.h +++ b/tmk_core/protocol/arm_atsam/spi.h @@ -26,40 +26,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. /* Determine bits to set for mux selection */ #if SR_EXP_DATAOUT_PIN % 2 == 0 -#define SR_EXP_DATAOUT_MUX_SEL PMUXE +# define SR_EXP_DATAOUT_MUX_SEL PMUXE #else -#define SR_EXP_DATAOUT_MUX_SEL PMUXO +# define SR_EXP_DATAOUT_MUX_SEL PMUXO #endif /* Determine bits to set for mux selection */ #if SR_EXP_SCLK_PIN % 2 == 0 -#define SR_EXP_SCLK_MUX_SEL PMUXE +# define SR_EXP_SCLK_MUX_SEL PMUXE #else -#define SR_EXP_SCLK_MUX_SEL PMUXO +# define SR_EXP_SCLK_MUX_SEL PMUXO #endif /* Data structure to define Shift Register output expander hardware */ /* This structure gets shifted into registers LSB first */ typedef union { - struct { - uint16_t RSVD4:1; /*!< bit: 0 */ - uint16_t RSVD3:1; /*!< bit: 1 */ - uint16_t RSVD2:1; /*!< bit: 2 */ - uint16_t RSVD1:1; /*!< bit: 3 */ - uint16_t SDB_N:1; /*!< bit: 4 SHUTDOWN THE CHIP WHEN 0, RUN WHEN 1 */ - uint16_t IRST:1; /*!< bit: 5 RESET THE IS3733 I2C WHEN 1, RUN WHEN 0 */ - uint16_t SRC_2:1; /*!< bit: 6 ADVERTISE A SOURCE TO USBC-2 CC */ - uint16_t SRC_1:1; /*!< bit: 7 ADVERTISE A SOURCE TO USBC-1 CC */ - uint16_t E_VBUS_2:1; /*!< bit: 8 ENABLE 5V OUT TO USBC-2 WHEN 1 */ - uint16_t E_VBUS_1:1; /*!< bit: 9 ENABLE 5V OUT TO USBC-1 WHEN 1 */ - uint16_t E_DN1_N:1; /*!< bit: 10 ENABLE DN1 1:2 MUX WHEN 0 */ - uint16_t S_DN1:1; /*!< bit: 11 SELECT DN1 PATH 0:USBC-1, 1:USBC-2 */ - uint16_t E_UP_N:1; /*!< bit: 12 ENABLE SUP 1:2 MUX WHEN 0 */ - uint16_t S_UP:1; /*!< bit: 13 SELECT UP PATH 0:USBC-1, 1:USBC-2 */ - uint16_t HUB_RESET_N:1; /*!< bit: 14 RESET USB HUB WHEN 0, RUN WHEN 1 */ - uint16_t HUB_CONNECT:1; /*!< bit: 15 SIGNAL VBUS CONNECT TO USB HUB WHEN 1 */ - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ + struct { + uint16_t RSVD4 : 1; /*!< bit: 0 */ + uint16_t RSVD3 : 1; /*!< bit: 1 */ + uint16_t RSVD2 : 1; /*!< bit: 2 */ + uint16_t RSVD1 : 1; /*!< bit: 3 */ + uint16_t SDB_N : 1; /*!< bit: 4 SHUTDOWN THE CHIP WHEN 0, RUN WHEN 1 */ + uint16_t IRST : 1; /*!< bit: 5 RESET THE IS3733 I2C WHEN 1, RUN WHEN 0 */ + uint16_t SRC_2 : 1; /*!< bit: 6 ADVERTISE A SOURCE TO USBC-2 CC */ + uint16_t SRC_1 : 1; /*!< bit: 7 ADVERTISE A SOURCE TO USBC-1 CC */ + uint16_t E_VBUS_2 : 1; /*!< bit: 8 ENABLE 5V OUT TO USBC-2 WHEN 1 */ + uint16_t E_VBUS_1 : 1; /*!< bit: 9 ENABLE 5V OUT TO USBC-1 WHEN 1 */ + uint16_t E_DN1_N : 1; /*!< bit: 10 ENABLE DN1 1:2 MUX WHEN 0 */ + uint16_t S_DN1 : 1; /*!< bit: 11 SELECT DN1 PATH 0:USBC-1, 1:USBC-2 */ + uint16_t E_UP_N : 1; /*!< bit: 12 ENABLE SUP 1:2 MUX WHEN 0 */ + uint16_t S_UP : 1; /*!< bit: 13 SELECT UP PATH 0:USBC-1, 1:USBC-2 */ + uint16_t HUB_RESET_N : 1; /*!< bit: 14 RESET USB HUB WHEN 0, RUN WHEN 1 */ + uint16_t HUB_CONNECT : 1; /*!< bit: 15 SIGNAL VBUS CONNECT TO USB HUB WHEN 1 */ + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ } sr_exp_t; extern sr_exp_t sr_exp_data; @@ -67,4 +67,4 @@ extern sr_exp_t sr_exp_data; void SR_EXP_WriteData(void); void SR_EXP_Init(void); -#endif //_SPI_H_ +#endif //_SPI_H_ diff --git a/tmk_core/protocol/arm_atsam/startup.c b/tmk_core/protocol/arm_atsam/startup.c index f29fac179..7a5791ab5 100644 --- a/tmk_core/protocol/arm_atsam/startup.c +++ b/tmk_core/protocol/arm_atsam/startup.c @@ -14,9 +14,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the Licence at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -51,447 +51,448 @@ void __libc_init_array(void); void Dummy_Handler(void); /* Cortex-M4 core handlers */ -void NMI_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void HardFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void MemManage_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void BusFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void UsageFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SVC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void DebugMon_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void PendSV_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SysTick_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void NMI_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void HardFault_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void MemManage_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void BusFault_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void UsageFault_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void SVC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void DebugMon_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void PendSV_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void SysTick_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* Peripherals handlers */ -void PM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void MCLK_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void OSCCTRL_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* OSCCTRL_XOSCFAIL_0, OSCCTRL_XOSCRDY_0 */ -void OSCCTRL_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* OSCCTRL_XOSCFAIL_1, OSCCTRL_XOSCRDY_1 */ -void OSCCTRL_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* OSCCTRL_DFLLLOCKC, OSCCTRL_DFLLLOCKF, OSCCTRL_DFLLOOB, OSCCTRL_DFLLRCS, OSCCTRL_DFLLRDY */ -void OSCCTRL_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* OSCCTRL_DPLLLCKF_0, OSCCTRL_DPLLLCKR_0, OSCCTRL_DPLLLDRTO_0, OSCCTRL_DPLLLTO_0 */ -void OSCCTRL_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* OSCCTRL_DPLLLCKF_1, OSCCTRL_DPLLLCKR_1, OSCCTRL_DPLLLDRTO_1, OSCCTRL_DPLLLTO_1 */ -void OSC32KCTRL_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SUPC_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SUPC_B12SRDY, SUPC_B33SRDY, SUPC_BOD12RDY, SUPC_BOD33RDY, SUPC_VCORERDY, SUPC_VREGRDY */ -void SUPC_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SUPC_BOD12DET, SUPC_BOD33DET */ -void WDT_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void RTC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void EIC_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_0 */ -void EIC_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_1 */ -void EIC_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_2 */ -void EIC_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_3 */ -void EIC_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_4 */ -void EIC_5_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_5 */ -void EIC_6_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_6 */ -void EIC_7_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_7 */ -void EIC_8_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_8 */ -void EIC_9_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_9 */ -void EIC_10_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_10 */ -void EIC_11_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_11 */ -void EIC_12_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_12 */ -void EIC_13_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_13 */ -void EIC_14_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_14 */ -void EIC_15_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_15 */ -void FREQM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void NVMCTRL_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* NVMCTRL_0, NVMCTRL_1, NVMCTRL_2, NVMCTRL_3, NVMCTRL_4, NVMCTRL_5, NVMCTRL_6, NVMCTRL_7 */ -void NVMCTRL_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* NVMCTRL_10, NVMCTRL_8, NVMCTRL_9 */ -void DMAC_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_0, DMAC_TCMPL_0, DMAC_TERR_0 */ -void DMAC_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_1, DMAC_TCMPL_1, DMAC_TERR_1 */ -void DMAC_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_2, DMAC_TCMPL_2, DMAC_TERR_2 */ -void DMAC_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_3, DMAC_TCMPL_3, DMAC_TERR_3 */ -void DMAC_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_10, DMAC_SUSP_11, DMAC_SUSP_12, DMAC_SUSP_13, DMAC_SUSP_14, DMAC_SUSP_15, DMAC_SUSP_16, DMAC_SUSP_17, DMAC_SUSP_18, DMAC_SUSP_19, DMAC_SUSP_20, DMAC_SUSP_21, DMAC_SUSP_22, DMAC_SUSP_23, DMAC_SUSP_24, DMAC_SUSP_25, DMAC_SUSP_26, DMAC_SUSP_27, DMAC_SUSP_28, DMAC_SUSP_29, DMAC_SUSP_30, DMAC_SUSP_31, DMAC_SUSP_4, DMAC_SUSP_5, DMAC_SUSP_6, DMAC_SUSP_7, DMAC_SUSP_8, DMAC_SUSP_9, DMAC_TCMPL_10, DMAC_TCMPL_11, DMAC_TCMPL_12, DMAC_TCMPL_13, DMAC_TCMPL_14, DMAC_TCMPL_15, DMAC_TCMPL_16, DMAC_TCMPL_17, DMAC_TCMPL_18, DMAC_TCMPL_19, DMAC_TCMPL_20, DMAC_TCMPL_21, DMAC_TCMPL_22, DMAC_TCMPL_23, DMAC_TCMPL_24, DMAC_TCMPL_25, DMAC_TCMPL_26, DMAC_TCMPL_27, DMAC_TCMPL_28, DMAC_TCMPL_29, DMAC_TCMPL_30, DMAC_TCMPL_31, DMAC_TCMPL_4, DMAC_TCMPL_5, DMAC_TCMPL_6, DMAC_TCMPL_7, DMAC_TCMPL_8, DMAC_TCMPL_9, DMAC_TERR_10, DMAC_TERR_11, DMAC_TERR_12, DMAC_TERR_13, DMAC_TERR_14, DMAC_TERR_15, DMAC_TERR_16, DMAC_TERR_17, DMAC_TERR_18, DMAC_TERR_19, DMAC_TERR_20, DMAC_TERR_21, DMAC_TERR_22, DMAC_TERR_23, DMAC_TERR_24, DMAC_TERR_25, DMAC_TERR_26, DMAC_TERR_27, DMAC_TERR_28, DMAC_TERR_29, DMAC_TERR_30, DMAC_TERR_31, DMAC_TERR_4, DMAC_TERR_5, DMAC_TERR_6, DMAC_TERR_7, DMAC_TERR_8, DMAC_TERR_9 */ -void EVSYS_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_0, EVSYS_OVR_0 */ -void EVSYS_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_1, EVSYS_OVR_1 */ -void EVSYS_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_2, EVSYS_OVR_2 */ -void EVSYS_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_3, EVSYS_OVR_3 */ -void EVSYS_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_10, EVSYS_EVD_11, EVSYS_EVD_4, EVSYS_EVD_5, EVSYS_EVD_6, EVSYS_EVD_7, EVSYS_EVD_8, EVSYS_EVD_9, EVSYS_OVR_10, EVSYS_OVR_11, EVSYS_OVR_4, EVSYS_OVR_5, EVSYS_OVR_6, EVSYS_OVR_7, EVSYS_OVR_8, EVSYS_OVR_9 */ -void PAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TAL_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TAL_BRK */ -void TAL_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TAL_IPS_0, TAL_IPS_1 */ -void RAMECC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM0_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM0_0 */ -void SERCOM0_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM0_1 */ -void SERCOM0_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM0_2 */ -void SERCOM0_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM0_3, SERCOM0_4, SERCOM0_5, SERCOM0_6 */ -void SERCOM1_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM1_0 */ -void SERCOM1_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM1_1 */ -void SERCOM1_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM1_2 */ -void SERCOM1_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM1_3, SERCOM1_4, SERCOM1_5, SERCOM1_6 */ -void SERCOM2_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM2_0 */ -void SERCOM2_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM2_1 */ -void SERCOM2_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM2_2 */ -void SERCOM2_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM2_3, SERCOM2_4, SERCOM2_5, SERCOM2_6 */ -void SERCOM3_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM3_0 */ -void SERCOM3_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM3_1 */ -void SERCOM3_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM3_2 */ -void SERCOM3_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM3_3, SERCOM3_4, SERCOM3_5, SERCOM3_6 */ +void PM_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void MCLK_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void OSCCTRL_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* OSCCTRL_XOSCFAIL_0, OSCCTRL_XOSCRDY_0 */ +void OSCCTRL_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* OSCCTRL_XOSCFAIL_1, OSCCTRL_XOSCRDY_1 */ +void OSCCTRL_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* OSCCTRL_DFLLLOCKC, OSCCTRL_DFLLLOCKF, OSCCTRL_DFLLOOB, OSCCTRL_DFLLRCS, OSCCTRL_DFLLRDY */ +void OSCCTRL_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* OSCCTRL_DPLLLCKF_0, OSCCTRL_DPLLLCKR_0, OSCCTRL_DPLLLDRTO_0, OSCCTRL_DPLLLTO_0 */ +void OSCCTRL_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* OSCCTRL_DPLLLCKF_1, OSCCTRL_DPLLLCKR_1, OSCCTRL_DPLLLDRTO_1, OSCCTRL_DPLLLTO_1 */ +void OSC32KCTRL_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void SUPC_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SUPC_B12SRDY, SUPC_B33SRDY, SUPC_BOD12RDY, SUPC_BOD33RDY, SUPC_VCORERDY, SUPC_VREGRDY */ +void SUPC_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SUPC_BOD12DET, SUPC_BOD33DET */ +void WDT_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void RTC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void EIC_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_0 */ +void EIC_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_1 */ +void EIC_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_2 */ +void EIC_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_3 */ +void EIC_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_4 */ +void EIC_5_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_5 */ +void EIC_6_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_6 */ +void EIC_7_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_7 */ +void EIC_8_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_8 */ +void EIC_9_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_9 */ +void EIC_10_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_10 */ +void EIC_11_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_11 */ +void EIC_12_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_12 */ +void EIC_13_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_13 */ +void EIC_14_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_14 */ +void EIC_15_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EIC_EXTINT_15 */ +void FREQM_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void NVMCTRL_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* NVMCTRL_0, NVMCTRL_1, NVMCTRL_2, NVMCTRL_3, NVMCTRL_4, NVMCTRL_5, NVMCTRL_6, NVMCTRL_7 */ +void NVMCTRL_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* NVMCTRL_10, NVMCTRL_8, NVMCTRL_9 */ +void DMAC_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_0, DMAC_TCMPL_0, DMAC_TERR_0 */ +void DMAC_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_1, DMAC_TCMPL_1, DMAC_TERR_1 */ +void DMAC_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_2, DMAC_TCMPL_2, DMAC_TERR_2 */ +void DMAC_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_3, DMAC_TCMPL_3, DMAC_TERR_3 */ +void DMAC_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DMAC_SUSP_10, DMAC_SUSP_11, DMAC_SUSP_12, DMAC_SUSP_13, DMAC_SUSP_14, DMAC_SUSP_15, DMAC_SUSP_16, DMAC_SUSP_17, DMAC_SUSP_18, DMAC_SUSP_19, DMAC_SUSP_20, DMAC_SUSP_21, DMAC_SUSP_22, DMAC_SUSP_23, DMAC_SUSP_24, DMAC_SUSP_25, DMAC_SUSP_26, DMAC_SUSP_27, DMAC_SUSP_28, DMAC_SUSP_29, DMAC_SUSP_30, DMAC_SUSP_31, DMAC_SUSP_4, DMAC_SUSP_5, DMAC_SUSP_6, DMAC_SUSP_7, DMAC_SUSP_8, DMAC_SUSP_9, DMAC_TCMPL_10, DMAC_TCMPL_11, DMAC_TCMPL_12, DMAC_TCMPL_13, DMAC_TCMPL_14, DMAC_TCMPL_15, DMAC_TCMPL_16, DMAC_TCMPL_17, DMAC_TCMPL_18, DMAC_TCMPL_19, DMAC_TCMPL_20, DMAC_TCMPL_21, DMAC_TCMPL_22, DMAC_TCMPL_23, DMAC_TCMPL_24, DMAC_TCMPL_25, DMAC_TCMPL_26, DMAC_TCMPL_27, DMAC_TCMPL_28, DMAC_TCMPL_29, DMAC_TCMPL_30, DMAC_TCMPL_31, DMAC_TCMPL_4, DMAC_TCMPL_5, DMAC_TCMPL_6, DMAC_TCMPL_7, DMAC_TCMPL_8, DMAC_TCMPL_9, DMAC_TERR_10, DMAC_TERR_11, DMAC_TERR_12, DMAC_TERR_13, DMAC_TERR_14, DMAC_TERR_15, DMAC_TERR_16, DMAC_TERR_17, + DMAC_TERR_18, DMAC_TERR_19, DMAC_TERR_20, DMAC_TERR_21, DMAC_TERR_22, DMAC_TERR_23, DMAC_TERR_24, DMAC_TERR_25, DMAC_TERR_26, DMAC_TERR_27, DMAC_TERR_28, DMAC_TERR_29, DMAC_TERR_30, DMAC_TERR_31, DMAC_TERR_4, DMAC_TERR_5, DMAC_TERR_6, DMAC_TERR_7, DMAC_TERR_8, DMAC_TERR_9 */ +void EVSYS_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_0, EVSYS_OVR_0 */ +void EVSYS_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_1, EVSYS_OVR_1 */ +void EVSYS_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_2, EVSYS_OVR_2 */ +void EVSYS_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_3, EVSYS_OVR_3 */ +void EVSYS_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* EVSYS_EVD_10, EVSYS_EVD_11, EVSYS_EVD_4, EVSYS_EVD_5, EVSYS_EVD_6, EVSYS_EVD_7, EVSYS_EVD_8, EVSYS_EVD_9, EVSYS_OVR_10, EVSYS_OVR_11, EVSYS_OVR_4, EVSYS_OVR_5, EVSYS_OVR_6, EVSYS_OVR_7, EVSYS_OVR_8, EVSYS_OVR_9 */ +void PAC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void TAL_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TAL_BRK */ +void TAL_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TAL_IPS_0, TAL_IPS_1 */ +void RAMECC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void SERCOM0_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM0_0 */ +void SERCOM0_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM0_1 */ +void SERCOM0_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM0_2 */ +void SERCOM0_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM0_3, SERCOM0_4, SERCOM0_5, SERCOM0_6 */ +void SERCOM1_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM1_0 */ +void SERCOM1_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM1_1 */ +void SERCOM1_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM1_2 */ +void SERCOM1_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM1_3, SERCOM1_4, SERCOM1_5, SERCOM1_6 */ +void SERCOM2_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM2_0 */ +void SERCOM2_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM2_1 */ +void SERCOM2_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM2_2 */ +void SERCOM2_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM2_3, SERCOM2_4, SERCOM2_5, SERCOM2_6 */ +void SERCOM3_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM3_0 */ +void SERCOM3_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM3_1 */ +void SERCOM3_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM3_2 */ +void SERCOM3_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM3_3, SERCOM3_4, SERCOM3_5, SERCOM3_6 */ #ifdef ID_SERCOM4 -void SERCOM4_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM4_0 */ -void SERCOM4_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM4_1 */ -void SERCOM4_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM4_2 */ -void SERCOM4_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM4_3, SERCOM4_4, SERCOM4_5, SERCOM4_6 */ +void SERCOM4_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM4_0 */ +void SERCOM4_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM4_1 */ +void SERCOM4_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM4_2 */ +void SERCOM4_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM4_3, SERCOM4_4, SERCOM4_5, SERCOM4_6 */ #endif #ifdef ID_SERCOM5 -void SERCOM5_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM5_0 */ -void SERCOM5_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM5_1 */ -void SERCOM5_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM5_2 */ -void SERCOM5_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM5_3, SERCOM5_4, SERCOM5_5, SERCOM5_6 */ +void SERCOM5_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM5_0 */ +void SERCOM5_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM5_1 */ +void SERCOM5_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM5_2 */ +void SERCOM5_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM5_3, SERCOM5_4, SERCOM5_5, SERCOM5_6 */ #endif #ifdef ID_SERCOM6 -void SERCOM6_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM6_0 */ -void SERCOM6_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM6_1 */ -void SERCOM6_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM6_2 */ -void SERCOM6_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM6_3, SERCOM6_4, SERCOM6_5, SERCOM6_6 */ +void SERCOM6_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM6_0 */ +void SERCOM6_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM6_1 */ +void SERCOM6_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM6_2 */ +void SERCOM6_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM6_3, SERCOM6_4, SERCOM6_5, SERCOM6_6 */ #endif #ifdef ID_SERCOM7 -void SERCOM7_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM7_0 */ -void SERCOM7_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM7_1 */ -void SERCOM7_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM7_2 */ -void SERCOM7_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* SERCOM7_3, SERCOM7_4, SERCOM7_5, SERCOM7_6 */ +void SERCOM7_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM7_0 */ +void SERCOM7_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM7_1 */ +void SERCOM7_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM7_2 */ +void SERCOM7_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* SERCOM7_3, SERCOM7_4, SERCOM7_5, SERCOM7_6 */ #endif #ifdef ID_CAN0 -void CAN0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void CAN0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_CAN1 -void CAN1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void CAN1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_USB -void USB_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* USB_EORSM_DNRSM, USB_EORST_RST, USB_LPMSUSP_DDISC, USB_LPM_DCONN, USB_MSOF, USB_RAMACER, USB_RXSTP_TXSTP_0, USB_RXSTP_TXSTP_1, USB_RXSTP_TXSTP_2, USB_RXSTP_TXSTP_3, USB_RXSTP_TXSTP_4, USB_RXSTP_TXSTP_5, USB_RXSTP_TXSTP_6, USB_RXSTP_TXSTP_7, USB_STALL0_STALL_0, USB_STALL0_STALL_1, USB_STALL0_STALL_2, USB_STALL0_STALL_3, USB_STALL0_STALL_4, USB_STALL0_STALL_5, USB_STALL0_STALL_6, USB_STALL0_STALL_7, USB_STALL1_0, USB_STALL1_1, USB_STALL1_2, USB_STALL1_3, USB_STALL1_4, USB_STALL1_5, USB_STALL1_6, USB_STALL1_7, USB_SUSPEND, USB_TRFAIL0_TRFAIL_0, USB_TRFAIL0_TRFAIL_1, USB_TRFAIL0_TRFAIL_2, USB_TRFAIL0_TRFAIL_3, USB_TRFAIL0_TRFAIL_4, USB_TRFAIL0_TRFAIL_5, USB_TRFAIL0_TRFAIL_6, USB_TRFAIL0_TRFAIL_7, USB_TRFAIL1_PERR_0, USB_TRFAIL1_PERR_1, USB_TRFAIL1_PERR_2, USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5, USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */ -void USB_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* USB_SOF_HSOF */ -void USB_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* USB_TRCPT0_0, USB_TRCPT0_1, USB_TRCPT0_2, USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5, USB_TRCPT0_6, USB_TRCPT0_7 */ -void USB_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* USB_TRCPT1_0, USB_TRCPT1_1, USB_TRCPT1_2, USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5, USB_TRCPT1_6, USB_TRCPT1_7 */ +void USB_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* USB_EORSM_DNRSM, USB_EORST_RST, USB_LPMSUSP_DDISC, USB_LPM_DCONN, USB_MSOF, USB_RAMACER, USB_RXSTP_TXSTP_0, USB_RXSTP_TXSTP_1, USB_RXSTP_TXSTP_2, USB_RXSTP_TXSTP_3, USB_RXSTP_TXSTP_4, USB_RXSTP_TXSTP_5, USB_RXSTP_TXSTP_6, USB_RXSTP_TXSTP_7, USB_STALL0_STALL_0, USB_STALL0_STALL_1, USB_STALL0_STALL_2, USB_STALL0_STALL_3, USB_STALL0_STALL_4, USB_STALL0_STALL_5, USB_STALL0_STALL_6, USB_STALL0_STALL_7, USB_STALL1_0, USB_STALL1_1, USB_STALL1_2, USB_STALL1_3, USB_STALL1_4, USB_STALL1_5, USB_STALL1_6, USB_STALL1_7, USB_SUSPEND, USB_TRFAIL0_TRFAIL_0, USB_TRFAIL0_TRFAIL_1, USB_TRFAIL0_TRFAIL_2, USB_TRFAIL0_TRFAIL_3, USB_TRFAIL0_TRFAIL_4, USB_TRFAIL0_TRFAIL_5, USB_TRFAIL0_TRFAIL_6, USB_TRFAIL0_TRFAIL_7, USB_TRFAIL1_PERR_0, USB_TRFAIL1_PERR_1, USB_TRFAIL1_PERR_2, USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5, USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */ +void USB_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* USB_SOF_HSOF */ +void USB_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* USB_TRCPT0_0, USB_TRCPT0_1, USB_TRCPT0_2, USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5, USB_TRCPT0_6, USB_TRCPT0_7 */ +void USB_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* USB_TRCPT1_0, USB_TRCPT1_1, USB_TRCPT1_2, USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5, USB_TRCPT1_6, USB_TRCPT1_7 */ #endif #ifdef ID_GMAC -void GMAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -#endif -void TCC0_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_CNT_A, TCC0_DFS_A, TCC0_ERR_A, TCC0_FAULT0_A, TCC0_FAULT1_A, TCC0_FAULTA_A, TCC0_FAULTB_A, TCC0_OVF, TCC0_TRG, TCC0_UFS_A */ -void TCC0_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_0 */ -void TCC0_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_1 */ -void TCC0_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_2 */ -void TCC0_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_3 */ -void TCC0_5_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_4 */ -void TCC0_6_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC0_MC_5 */ -void TCC1_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC1_CNT_A, TCC1_DFS_A, TCC1_ERR_A, TCC1_FAULT0_A, TCC1_FAULT1_A, TCC1_FAULTA_A, TCC1_FAULTB_A, TCC1_OVF, TCC1_TRG, TCC1_UFS_A */ -void TCC1_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC1_MC_0 */ -void TCC1_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC1_MC_1 */ -void TCC1_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC1_MC_2 */ -void TCC1_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC1_MC_3 */ -void TCC2_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC2_CNT_A, TCC2_DFS_A, TCC2_ERR_A, TCC2_FAULT0_A, TCC2_FAULT1_A, TCC2_FAULTA_A, TCC2_FAULTB_A, TCC2_OVF, TCC2_TRG, TCC2_UFS_A */ -void TCC2_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC2_MC_0 */ -void TCC2_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC2_MC_1 */ -void TCC2_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC2_MC_2 */ +void GMAC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +#endif +void TCC0_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_CNT_A, TCC0_DFS_A, TCC0_ERR_A, TCC0_FAULT0_A, TCC0_FAULT1_A, TCC0_FAULTA_A, TCC0_FAULTB_A, TCC0_OVF, TCC0_TRG, TCC0_UFS_A */ +void TCC0_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_0 */ +void TCC0_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_1 */ +void TCC0_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_2 */ +void TCC0_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_3 */ +void TCC0_5_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_4 */ +void TCC0_6_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC0_MC_5 */ +void TCC1_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC1_CNT_A, TCC1_DFS_A, TCC1_ERR_A, TCC1_FAULT0_A, TCC1_FAULT1_A, TCC1_FAULTA_A, TCC1_FAULTB_A, TCC1_OVF, TCC1_TRG, TCC1_UFS_A */ +void TCC1_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC1_MC_0 */ +void TCC1_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC1_MC_1 */ +void TCC1_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC1_MC_2 */ +void TCC1_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC1_MC_3 */ +void TCC2_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC2_CNT_A, TCC2_DFS_A, TCC2_ERR_A, TCC2_FAULT0_A, TCC2_FAULT1_A, TCC2_FAULTA_A, TCC2_FAULTB_A, TCC2_OVF, TCC2_TRG, TCC2_UFS_A */ +void TCC2_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC2_MC_0 */ +void TCC2_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC2_MC_1 */ +void TCC2_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC2_MC_2 */ #ifdef ID_TCC3 -void TCC3_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC3_CNT_A, TCC3_DFS_A, TCC3_ERR_A, TCC3_FAULT0_A, TCC3_FAULT1_A, TCC3_FAULTA_A, TCC3_FAULTB_A, TCC3_OVF, TCC3_TRG, TCC3_UFS_A */ -void TCC3_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC3_MC_0 */ -void TCC3_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC3_MC_1 */ +void TCC3_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC3_CNT_A, TCC3_DFS_A, TCC3_ERR_A, TCC3_FAULT0_A, TCC3_FAULT1_A, TCC3_FAULTA_A, TCC3_FAULTB_A, TCC3_OVF, TCC3_TRG, TCC3_UFS_A */ +void TCC3_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC3_MC_0 */ +void TCC3_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC3_MC_1 */ #endif #ifdef ID_TCC4 -void TCC4_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC4_CNT_A, TCC4_DFS_A, TCC4_ERR_A, TCC4_FAULT0_A, TCC4_FAULT1_A, TCC4_FAULTA_A, TCC4_FAULTB_A, TCC4_OVF, TCC4_TRG, TCC4_UFS_A */ -void TCC4_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC4_MC_0 */ -void TCC4_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* TCC4_MC_1 */ -#endif -void TC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void TCC4_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC4_CNT_A, TCC4_DFS_A, TCC4_ERR_A, TCC4_FAULT0_A, TCC4_FAULT1_A, TCC4_FAULTA_A, TCC4_FAULTB_A, TCC4_OVF, TCC4_TRG, TCC4_UFS_A */ +void TCC4_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC4_MC_0 */ +void TCC4_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* TCC4_MC_1 */ +#endif +void TC0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void TC1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void TC2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void TC3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #ifdef ID_TC4 -void TC4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_TC5 -void TC5_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC5_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_TC6 -void TC6_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC6_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_TC7 -void TC7_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -#endif -void PDEC_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* PDEC_DIR_A, PDEC_ERR_A, PDEC_OVF, PDEC_VLC_A */ -void PDEC_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* PDEC_MC_0 */ -void PDEC_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* PDEC_MC_1 */ -void ADC0_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* ADC0_OVERRUN, ADC0_WINMON */ -void ADC0_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* ADC0_RESRDY */ -void ADC1_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* ADC1_OVERRUN, ADC1_WINMON */ -void ADC1_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* ADC1_RESRDY */ -void AC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void DAC_0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DAC_OVERRUN_A_0, DAC_OVERRUN_A_1, DAC_UNDERRUN_A_0, DAC_UNDERRUN_A_1 */ -void DAC_1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DAC_EMPTY_0 */ -void DAC_2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DAC_EMPTY_1 */ -void DAC_3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DAC_RESRDY_0 */ -void DAC_4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* DAC_RESRDY_1 */ +void TC7_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +#endif +void PDEC_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* PDEC_DIR_A, PDEC_ERR_A, PDEC_OVF, PDEC_VLC_A */ +void PDEC_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* PDEC_MC_0 */ +void PDEC_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* PDEC_MC_1 */ +void ADC0_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* ADC0_OVERRUN, ADC0_WINMON */ +void ADC0_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* ADC0_RESRDY */ +void ADC1_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* ADC1_OVERRUN, ADC1_WINMON */ +void ADC1_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* ADC1_RESRDY */ +void AC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void DAC_0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DAC_OVERRUN_A_0, DAC_OVERRUN_A_1, DAC_UNDERRUN_A_0, DAC_UNDERRUN_A_1 */ +void DAC_1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DAC_EMPTY_0 */ +void DAC_2_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DAC_EMPTY_1 */ +void DAC_3_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DAC_RESRDY_0 */ +void DAC_4_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); /* DAC_RESRDY_1 */ #ifdef ID_I2S -void I2S_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void I2S_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif -void PCC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void AES_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TRNG_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void PCC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void AES_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); +void TRNG_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #ifdef ID_ICM -void ICM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void ICM_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_PUKCC -void PUKCC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void PUKCC_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif -void QSPI_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void QSPI_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #ifdef ID_SDHC0 -void SDHC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void SDHC0_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif #ifdef ID_SDHC1 -void SDHC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); +void SDHC1_Handler(void) __attribute__((weak, alias("Dummy_Handler"))); #endif /* Exception Table */ -__attribute__ ((section(".vectors"))) -const DeviceVectors exception_table = { +__attribute__((section(".vectors"))) const DeviceVectors exception_table = { - /* Configure Initial Stack Pointer, using linker-generated symbols */ - .pvStack = (void*) (&_estack), + /* Configure Initial Stack Pointer, using linker-generated symbols */ + .pvStack = (void *)(&_estack), - .pfnReset_Handler = (void*) Reset_Handler, - .pfnNMI_Handler = (void*) NMI_Handler, - .pfnHardFault_Handler = (void*) HardFault_Handler, - .pfnMemManage_Handler = (void*) MemManage_Handler, - .pfnBusFault_Handler = (void*) BusFault_Handler, - .pfnUsageFault_Handler = (void*) UsageFault_Handler, - .pvReservedM9 = (void*) (0UL), /* Reserved */ - .pvReservedM8 = (void*) (0UL), /* Reserved */ - .pvReservedM7 = (void*) (0UL), /* Reserved */ - .pvReservedM6 = (void*) (0UL), /* Reserved */ - .pfnSVC_Handler = (void*) SVC_Handler, - .pfnDebugMon_Handler = (void*) DebugMon_Handler, - .pvReservedM3 = (void*) (0UL), /* Reserved */ - .pfnPendSV_Handler = (void*) PendSV_Handler, - .pfnSysTick_Handler = (void*) SysTick_Handler, + .pfnReset_Handler = (void *)Reset_Handler, + .pfnNMI_Handler = (void *)NMI_Handler, + .pfnHardFault_Handler = (void *)HardFault_Handler, + .pfnMemManage_Handler = (void *)MemManage_Handler, + .pfnBusFault_Handler = (void *)BusFault_Handler, + .pfnUsageFault_Handler = (void *)UsageFault_Handler, + .pvReservedM9 = (void *)(0UL), /* Reserved */ + .pvReservedM8 = (void *)(0UL), /* Reserved */ + .pvReservedM7 = (void *)(0UL), /* Reserved */ + .pvReservedM6 = (void *)(0UL), /* Reserved */ + .pfnSVC_Handler = (void *)SVC_Handler, + .pfnDebugMon_Handler = (void *)DebugMon_Handler, + .pvReservedM3 = (void *)(0UL), /* Reserved */ + .pfnPendSV_Handler = (void *)PendSV_Handler, + .pfnSysTick_Handler = (void *)SysTick_Handler, - /* Configurable interrupts */ - .pfnPM_Handler = (void*) PM_Handler, /* 0 Power Manager */ - .pfnMCLK_Handler = (void*) MCLK_Handler, /* 1 Main Clock */ - .pfnOSCCTRL_0_Handler = (void*) OSCCTRL_0_Handler, /* 2 OSCCTRL_XOSCFAIL_0, OSCCTRL_XOSCRDY_0 */ - .pfnOSCCTRL_1_Handler = (void*) OSCCTRL_1_Handler, /* 3 OSCCTRL_XOSCFAIL_1, OSCCTRL_XOSCRDY_1 */ - .pfnOSCCTRL_2_Handler = (void*) OSCCTRL_2_Handler, /* 4 OSCCTRL_DFLLLOCKC, OSCCTRL_DFLLLOCKF, OSCCTRL_DFLLOOB, OSCCTRL_DFLLRCS, OSCCTRL_DFLLRDY */ - .pfnOSCCTRL_3_Handler = (void*) OSCCTRL_3_Handler, /* 5 OSCCTRL_DPLLLCKF_0, OSCCTRL_DPLLLCKR_0, OSCCTRL_DPLLLDRTO_0, OSCCTRL_DPLLLTO_0 */ - .pfnOSCCTRL_4_Handler = (void*) OSCCTRL_4_Handler, /* 6 OSCCTRL_DPLLLCKF_1, OSCCTRL_DPLLLCKR_1, OSCCTRL_DPLLLDRTO_1, OSCCTRL_DPLLLTO_1 */ - .pfnOSC32KCTRL_Handler = (void*) OSC32KCTRL_Handler, /* 7 32kHz Oscillators Control */ - .pfnSUPC_0_Handler = (void*) SUPC_0_Handler, /* 8 SUPC_B12SRDY, SUPC_B33SRDY, SUPC_BOD12RDY, SUPC_BOD33RDY, SUPC_VCORERDY, SUPC_VREGRDY */ - .pfnSUPC_1_Handler = (void*) SUPC_1_Handler, /* 9 SUPC_BOD12DET, SUPC_BOD33DET */ - .pfnWDT_Handler = (void*) WDT_Handler, /* 10 Watchdog Timer */ - .pfnRTC_Handler = (void*) RTC_Handler, /* 11 Real-Time Counter */ - .pfnEIC_0_Handler = (void*) EIC_0_Handler, /* 12 EIC_EXTINT_0 */ - .pfnEIC_1_Handler = (void*) EIC_1_Handler, /* 13 EIC_EXTINT_1 */ - .pfnEIC_2_Handler = (void*) EIC_2_Handler, /* 14 EIC_EXTINT_2 */ - .pfnEIC_3_Handler = (void*) EIC_3_Handler, /* 15 EIC_EXTINT_3 */ - .pfnEIC_4_Handler = (void*) EIC_4_Handler, /* 16 EIC_EXTINT_4 */ - .pfnEIC_5_Handler = (void*) EIC_5_Handler, /* 17 EIC_EXTINT_5 */ - .pfnEIC_6_Handler = (void*) EIC_6_Handler, /* 18 EIC_EXTINT_6 */ - .pfnEIC_7_Handler = (void*) EIC_7_Handler, /* 19 EIC_EXTINT_7 */ - .pfnEIC_8_Handler = (void*) EIC_8_Handler, /* 20 EIC_EXTINT_8 */ - .pfnEIC_9_Handler = (void*) EIC_9_Handler, /* 21 EIC_EXTINT_9 */ - .pfnEIC_10_Handler = (void*) EIC_10_Handler, /* 22 EIC_EXTINT_10 */ - .pfnEIC_11_Handler = (void*) EIC_11_Handler, /* 23 EIC_EXTINT_11 */ - .pfnEIC_12_Handler = (void*) EIC_12_Handler, /* 24 EIC_EXTINT_12 */ - .pfnEIC_13_Handler = (void*) EIC_13_Handler, /* 25 EIC_EXTINT_13 */ - .pfnEIC_14_Handler = (void*) EIC_14_Handler, /* 26 EIC_EXTINT_14 */ - .pfnEIC_15_Handler = (void*) EIC_15_Handler, /* 27 EIC_EXTINT_15 */ - .pfnFREQM_Handler = (void*) FREQM_Handler, /* 28 Frequency Meter */ - .pfnNVMCTRL_0_Handler = (void*) NVMCTRL_0_Handler, /* 29 NVMCTRL_0, NVMCTRL_1, NVMCTRL_2, NVMCTRL_3, NVMCTRL_4, NVMCTRL_5, NVMCTRL_6, NVMCTRL_7 */ - .pfnNVMCTRL_1_Handler = (void*) NVMCTRL_1_Handler, /* 30 NVMCTRL_10, NVMCTRL_8, NVMCTRL_9 */ - .pfnDMAC_0_Handler = (void*) DMAC_0_Handler, /* 31 DMAC_SUSP_0, DMAC_TCMPL_0, DMAC_TERR_0 */ - .pfnDMAC_1_Handler = (void*) DMAC_1_Handler, /* 32 DMAC_SUSP_1, DMAC_TCMPL_1, DMAC_TERR_1 */ - .pfnDMAC_2_Handler = (void*) DMAC_2_Handler, /* 33 DMAC_SUSP_2, DMAC_TCMPL_2, DMAC_TERR_2 */ - .pfnDMAC_3_Handler = (void*) DMAC_3_Handler, /* 34 DMAC_SUSP_3, DMAC_TCMPL_3, DMAC_TERR_3 */ - .pfnDMAC_4_Handler = (void*) DMAC_4_Handler, /* 35 DMAC_SUSP_10, DMAC_SUSP_11, DMAC_SUSP_12, DMAC_SUSP_13, DMAC_SUSP_14, DMAC_SUSP_15, DMAC_SUSP_16, DMAC_SUSP_17, DMAC_SUSP_18, DMAC_SUSP_19, DMAC_SUSP_20, DMAC_SUSP_21, DMAC_SUSP_22, DMAC_SUSP_23, DMAC_SUSP_24, DMAC_SUSP_25, DMAC_SUSP_26, DMAC_SUSP_27, DMAC_SUSP_28, DMAC_SUSP_29, DMAC_SUSP_30, DMAC_SUSP_31, DMAC_SUSP_4, DMAC_SUSP_5, DMAC_SUSP_6, DMAC_SUSP_7, DMAC_SUSP_8, DMAC_SUSP_9, DMAC_TCMPL_10, DMAC_TCMPL_11, DMAC_TCMPL_12, DMAC_TCMPL_13, DMAC_TCMPL_14, DMAC_TCMPL_15, DMAC_TCMPL_16, DMAC_TCMPL_17, DMAC_TCMPL_18, DMAC_TCMPL_19, DMAC_TCMPL_20, DMAC_TCMPL_21, DMAC_TCMPL_22, DMAC_TCMPL_23, DMAC_TCMPL_24, DMAC_TCMPL_25, DMAC_TCMPL_26, DMAC_TCMPL_27, DMAC_TCMPL_28, DMAC_TCMPL_29, DMAC_TCMPL_30, DMAC_TCMPL_31, DMAC_TCMPL_4, DMAC_TCMPL_5, DMAC_TCMPL_6, DMAC_TCMPL_7, DMAC_TCMPL_8, DMAC_TCMPL_9, DMAC_TERR_10, DMAC_TERR_11, DMAC_TERR_12, DMAC_TERR_13, DMAC_TERR_14, DMAC_TERR_15, DMAC_TERR_16, DMAC_TERR_17, DMAC_TERR_18, DMAC_TERR_19, DMAC_TERR_20, DMAC_TERR_21, DMAC_TERR_22, DMAC_TERR_23, DMAC_TERR_24, DMAC_TERR_25, DMAC_TERR_26, DMAC_TERR_27, DMAC_TERR_28, DMAC_TERR_29, DMAC_TERR_30, DMAC_TERR_31, DMAC_TERR_4, DMAC_TERR_5, DMAC_TERR_6, DMAC_TERR_7, DMAC_TERR_8, DMAC_TERR_9 */ - .pfnEVSYS_0_Handler = (void*) EVSYS_0_Handler, /* 36 EVSYS_EVD_0, EVSYS_OVR_0 */ - .pfnEVSYS_1_Handler = (void*) EVSYS_1_Handler, /* 37 EVSYS_EVD_1, EVSYS_OVR_1 */ - .pfnEVSYS_2_Handler = (void*) EVSYS_2_Handler, /* 38 EVSYS_EVD_2, EVSYS_OVR_2 */ - .pfnEVSYS_3_Handler = (void*) EVSYS_3_Handler, /* 39 EVSYS_EVD_3, EVSYS_OVR_3 */ - .pfnEVSYS_4_Handler = (void*) EVSYS_4_Handler, /* 40 EVSYS_EVD_10, EVSYS_EVD_11, EVSYS_EVD_4, EVSYS_EVD_5, EVSYS_EVD_6, EVSYS_EVD_7, EVSYS_EVD_8, EVSYS_EVD_9, EVSYS_OVR_10, EVSYS_OVR_11, EVSYS_OVR_4, EVSYS_OVR_5, EVSYS_OVR_6, EVSYS_OVR_7, EVSYS_OVR_8, EVSYS_OVR_9 */ - .pfnPAC_Handler = (void*) PAC_Handler, /* 41 Peripheral Access Controller */ - .pfnTAL_0_Handler = (void*) TAL_0_Handler, /* 42 TAL_BRK */ - .pfnTAL_1_Handler = (void*) TAL_1_Handler, /* 43 TAL_IPS_0, TAL_IPS_1 */ - .pvReserved44 = (void*) (0UL), /* 44 Reserved */ - .pfnRAMECC_Handler = (void*) RAMECC_Handler, /* 45 RAM ECC */ - .pfnSERCOM0_0_Handler = (void*) SERCOM0_0_Handler, /* 46 SERCOM0_0 */ - .pfnSERCOM0_1_Handler = (void*) SERCOM0_1_Handler, /* 47 SERCOM0_1 */ - .pfnSERCOM0_2_Handler = (void*) SERCOM0_2_Handler, /* 48 SERCOM0_2 */ - .pfnSERCOM0_3_Handler = (void*) SERCOM0_3_Handler, /* 49 SERCOM0_3, SERCOM0_4, SERCOM0_5, SERCOM0_6 */ - .pfnSERCOM1_0_Handler = (void*) SERCOM1_0_Handler, /* 50 SERCOM1_0 */ - .pfnSERCOM1_1_Handler = (void*) SERCOM1_1_Handler, /* 51 SERCOM1_1 */ - .pfnSERCOM1_2_Handler = (void*) SERCOM1_2_Handler, /* 52 SERCOM1_2 */ - .pfnSERCOM1_3_Handler = (void*) SERCOM1_3_Handler, /* 53 SERCOM1_3, SERCOM1_4, SERCOM1_5, SERCOM1_6 */ - .pfnSERCOM2_0_Handler = (void*) SERCOM2_0_Handler, /* 54 SERCOM2_0 */ - .pfnSERCOM2_1_Handler = (void*) SERCOM2_1_Handler, /* 55 SERCOM2_1 */ - .pfnSERCOM2_2_Handler = (void*) SERCOM2_2_Handler, /* 56 SERCOM2_2 */ - .pfnSERCOM2_3_Handler = (void*) SERCOM2_3_Handler, /* 57 SERCOM2_3, SERCOM2_4, SERCOM2_5, SERCOM2_6 */ - .pfnSERCOM3_0_Handler = (void*) SERCOM3_0_Handler, /* 58 SERCOM3_0 */ - .pfnSERCOM3_1_Handler = (void*) SERCOM3_1_Handler, /* 59 SERCOM3_1 */ - .pfnSERCOM3_2_Handler = (void*) SERCOM3_2_Handler, /* 60 SERCOM3_2 */ - .pfnSERCOM3_3_Handler = (void*) SERCOM3_3_Handler, /* 61 SERCOM3_3, SERCOM3_4, SERCOM3_5, SERCOM3_6 */ + /* Configurable interrupts */ + .pfnPM_Handler = (void *)PM_Handler, /* 0 Power Manager */ + .pfnMCLK_Handler = (void *)MCLK_Handler, /* 1 Main Clock */ + .pfnOSCCTRL_0_Handler = (void *)OSCCTRL_0_Handler, /* 2 OSCCTRL_XOSCFAIL_0, OSCCTRL_XOSCRDY_0 */ + .pfnOSCCTRL_1_Handler = (void *)OSCCTRL_1_Handler, /* 3 OSCCTRL_XOSCFAIL_1, OSCCTRL_XOSCRDY_1 */ + .pfnOSCCTRL_2_Handler = (void *)OSCCTRL_2_Handler, /* 4 OSCCTRL_DFLLLOCKC, OSCCTRL_DFLLLOCKF, OSCCTRL_DFLLOOB, OSCCTRL_DFLLRCS, OSCCTRL_DFLLRDY */ + .pfnOSCCTRL_3_Handler = (void *)OSCCTRL_3_Handler, /* 5 OSCCTRL_DPLLLCKF_0, OSCCTRL_DPLLLCKR_0, OSCCTRL_DPLLLDRTO_0, OSCCTRL_DPLLLTO_0 */ + .pfnOSCCTRL_4_Handler = (void *)OSCCTRL_4_Handler, /* 6 OSCCTRL_DPLLLCKF_1, OSCCTRL_DPLLLCKR_1, OSCCTRL_DPLLLDRTO_1, OSCCTRL_DPLLLTO_1 */ + .pfnOSC32KCTRL_Handler = (void *)OSC32KCTRL_Handler, /* 7 32kHz Oscillators Control */ + .pfnSUPC_0_Handler = (void *)SUPC_0_Handler, /* 8 SUPC_B12SRDY, SUPC_B33SRDY, SUPC_BOD12RDY, SUPC_BOD33RDY, SUPC_VCORERDY, SUPC_VREGRDY */ + .pfnSUPC_1_Handler = (void *)SUPC_1_Handler, /* 9 SUPC_BOD12DET, SUPC_BOD33DET */ + .pfnWDT_Handler = (void *)WDT_Handler, /* 10 Watchdog Timer */ + .pfnRTC_Handler = (void *)RTC_Handler, /* 11 Real-Time Counter */ + .pfnEIC_0_Handler = (void *)EIC_0_Handler, /* 12 EIC_EXTINT_0 */ + .pfnEIC_1_Handler = (void *)EIC_1_Handler, /* 13 EIC_EXTINT_1 */ + .pfnEIC_2_Handler = (void *)EIC_2_Handler, /* 14 EIC_EXTINT_2 */ + .pfnEIC_3_Handler = (void *)EIC_3_Handler, /* 15 EIC_EXTINT_3 */ + .pfnEIC_4_Handler = (void *)EIC_4_Handler, /* 16 EIC_EXTINT_4 */ + .pfnEIC_5_Handler = (void *)EIC_5_Handler, /* 17 EIC_EXTINT_5 */ + .pfnEIC_6_Handler = (void *)EIC_6_Handler, /* 18 EIC_EXTINT_6 */ + .pfnEIC_7_Handler = (void *)EIC_7_Handler, /* 19 EIC_EXTINT_7 */ + .pfnEIC_8_Handler = (void *)EIC_8_Handler, /* 20 EIC_EXTINT_8 */ + .pfnEIC_9_Handler = (void *)EIC_9_Handler, /* 21 EIC_EXTINT_9 */ + .pfnEIC_10_Handler = (void *)EIC_10_Handler, /* 22 EIC_EXTINT_10 */ + .pfnEIC_11_Handler = (void *)EIC_11_Handler, /* 23 EIC_EXTINT_11 */ + .pfnEIC_12_Handler = (void *)EIC_12_Handler, /* 24 EIC_EXTINT_12 */ + .pfnEIC_13_Handler = (void *)EIC_13_Handler, /* 25 EIC_EXTINT_13 */ + .pfnEIC_14_Handler = (void *)EIC_14_Handler, /* 26 EIC_EXTINT_14 */ + .pfnEIC_15_Handler = (void *)EIC_15_Handler, /* 27 EIC_EXTINT_15 */ + .pfnFREQM_Handler = (void *)FREQM_Handler, /* 28 Frequency Meter */ + .pfnNVMCTRL_0_Handler = (void *)NVMCTRL_0_Handler, /* 29 NVMCTRL_0, NVMCTRL_1, NVMCTRL_2, NVMCTRL_3, NVMCTRL_4, NVMCTRL_5, NVMCTRL_6, NVMCTRL_7 */ + .pfnNVMCTRL_1_Handler = (void *)NVMCTRL_1_Handler, /* 30 NVMCTRL_10, NVMCTRL_8, NVMCTRL_9 */ + .pfnDMAC_0_Handler = (void *)DMAC_0_Handler, /* 31 DMAC_SUSP_0, DMAC_TCMPL_0, DMAC_TERR_0 */ + .pfnDMAC_1_Handler = (void *)DMAC_1_Handler, /* 32 DMAC_SUSP_1, DMAC_TCMPL_1, DMAC_TERR_1 */ + .pfnDMAC_2_Handler = (void *)DMAC_2_Handler, /* 33 DMAC_SUSP_2, DMAC_TCMPL_2, DMAC_TERR_2 */ + .pfnDMAC_3_Handler = (void *)DMAC_3_Handler, /* 34 DMAC_SUSP_3, DMAC_TCMPL_3, DMAC_TERR_3 */ + .pfnDMAC_4_Handler = (void *)DMAC_4_Handler, /* 35 DMAC_SUSP_10, DMAC_SUSP_11, DMAC_SUSP_12, DMAC_SUSP_13, DMAC_SUSP_14, DMAC_SUSP_15, DMAC_SUSP_16, DMAC_SUSP_17, DMAC_SUSP_18, DMAC_SUSP_19, DMAC_SUSP_20, DMAC_SUSP_21, DMAC_SUSP_22, DMAC_SUSP_23, DMAC_SUSP_24, DMAC_SUSP_25, DMAC_SUSP_26, DMAC_SUSP_27, DMAC_SUSP_28, DMAC_SUSP_29, DMAC_SUSP_30, DMAC_SUSP_31, DMAC_SUSP_4, DMAC_SUSP_5, DMAC_SUSP_6, DMAC_SUSP_7, DMAC_SUSP_8, DMAC_SUSP_9, DMAC_TCMPL_10, DMAC_TCMPL_11, DMAC_TCMPL_12, DMAC_TCMPL_13, DMAC_TCMPL_14, DMAC_TCMPL_15, DMAC_TCMPL_16, DMAC_TCMPL_17, DMAC_TCMPL_18, DMAC_TCMPL_19, DMAC_TCMPL_20, DMAC_TCMPL_21, DMAC_TCMPL_22, DMAC_TCMPL_23, DMAC_TCMPL_24, DMAC_TCMPL_25, DMAC_TCMPL_26, DMAC_TCMPL_27, DMAC_TCMPL_28, DMAC_TCMPL_29, DMAC_TCMPL_30, DMAC_TCMPL_31, DMAC_TCMPL_4, DMAC_TCMPL_5, DMAC_TCMPL_6, DMAC_TCMPL_7, DMAC_TCMPL_8, DMAC_TCMPL_9, DMAC_TERR_10, DMAC_TERR_11, DMAC_TERR_12, DMAC_TERR_13, DMAC_TERR_14, DMAC_TERR_15, DMAC_TERR_16, DMAC_TERR_17, DMAC_TERR_18, DMAC_TERR_19, + DMAC_TERR_20, DMAC_TERR_21, DMAC_TERR_22, DMAC_TERR_23, DMAC_TERR_24, DMAC_TERR_25, DMAC_TERR_26, DMAC_TERR_27, DMAC_TERR_28, DMAC_TERR_29, DMAC_TERR_30, DMAC_TERR_31, DMAC_TERR_4, DMAC_TERR_5, DMAC_TERR_6, DMAC_TERR_7, DMAC_TERR_8, DMAC_TERR_9 */ + .pfnEVSYS_0_Handler = (void *)EVSYS_0_Handler, /* 36 EVSYS_EVD_0, EVSYS_OVR_0 */ + .pfnEVSYS_1_Handler = (void *)EVSYS_1_Handler, /* 37 EVSYS_EVD_1, EVSYS_OVR_1 */ + .pfnEVSYS_2_Handler = (void *)EVSYS_2_Handler, /* 38 EVSYS_EVD_2, EVSYS_OVR_2 */ + .pfnEVSYS_3_Handler = (void *)EVSYS_3_Handler, /* 39 EVSYS_EVD_3, EVSYS_OVR_3 */ + .pfnEVSYS_4_Handler = (void *)EVSYS_4_Handler, /* 40 EVSYS_EVD_10, EVSYS_EVD_11, EVSYS_EVD_4, EVSYS_EVD_5, EVSYS_EVD_6, EVSYS_EVD_7, EVSYS_EVD_8, EVSYS_EVD_9, EVSYS_OVR_10, EVSYS_OVR_11, EVSYS_OVR_4, EVSYS_OVR_5, EVSYS_OVR_6, EVSYS_OVR_7, EVSYS_OVR_8, EVSYS_OVR_9 */ + .pfnPAC_Handler = (void *)PAC_Handler, /* 41 Peripheral Access Controller */ + .pfnTAL_0_Handler = (void *)TAL_0_Handler, /* 42 TAL_BRK */ + .pfnTAL_1_Handler = (void *)TAL_1_Handler, /* 43 TAL_IPS_0, TAL_IPS_1 */ + .pvReserved44 = (void *)(0UL), /* 44 Reserved */ + .pfnRAMECC_Handler = (void *)RAMECC_Handler, /* 45 RAM ECC */ + .pfnSERCOM0_0_Handler = (void *)SERCOM0_0_Handler, /* 46 SERCOM0_0 */ + .pfnSERCOM0_1_Handler = (void *)SERCOM0_1_Handler, /* 47 SERCOM0_1 */ + .pfnSERCOM0_2_Handler = (void *)SERCOM0_2_Handler, /* 48 SERCOM0_2 */ + .pfnSERCOM0_3_Handler = (void *)SERCOM0_3_Handler, /* 49 SERCOM0_3, SERCOM0_4, SERCOM0_5, SERCOM0_6 */ + .pfnSERCOM1_0_Handler = (void *)SERCOM1_0_Handler, /* 50 SERCOM1_0 */ + .pfnSERCOM1_1_Handler = (void *)SERCOM1_1_Handler, /* 51 SERCOM1_1 */ + .pfnSERCOM1_2_Handler = (void *)SERCOM1_2_Handler, /* 52 SERCOM1_2 */ + .pfnSERCOM1_3_Handler = (void *)SERCOM1_3_Handler, /* 53 SERCOM1_3, SERCOM1_4, SERCOM1_5, SERCOM1_6 */ + .pfnSERCOM2_0_Handler = (void *)SERCOM2_0_Handler, /* 54 SERCOM2_0 */ + .pfnSERCOM2_1_Handler = (void *)SERCOM2_1_Handler, /* 55 SERCOM2_1 */ + .pfnSERCOM2_2_Handler = (void *)SERCOM2_2_Handler, /* 56 SERCOM2_2 */ + .pfnSERCOM2_3_Handler = (void *)SERCOM2_3_Handler, /* 57 SERCOM2_3, SERCOM2_4, SERCOM2_5, SERCOM2_6 */ + .pfnSERCOM3_0_Handler = (void *)SERCOM3_0_Handler, /* 58 SERCOM3_0 */ + .pfnSERCOM3_1_Handler = (void *)SERCOM3_1_Handler, /* 59 SERCOM3_1 */ + .pfnSERCOM3_2_Handler = (void *)SERCOM3_2_Handler, /* 60 SERCOM3_2 */ + .pfnSERCOM3_3_Handler = (void *)SERCOM3_3_Handler, /* 61 SERCOM3_3, SERCOM3_4, SERCOM3_5, SERCOM3_6 */ #ifdef ID_SERCOM4 - .pfnSERCOM4_0_Handler = (void*) SERCOM4_0_Handler, /* 62 SERCOM4_0 */ - .pfnSERCOM4_1_Handler = (void*) SERCOM4_1_Handler, /* 63 SERCOM4_1 */ - .pfnSERCOM4_2_Handler = (void*) SERCOM4_2_Handler, /* 64 SERCOM4_2 */ - .pfnSERCOM4_3_Handler = (void*) SERCOM4_3_Handler, /* 65 SERCOM4_3, SERCOM4_4, SERCOM4_5, SERCOM4_6 */ + .pfnSERCOM4_0_Handler = (void *)SERCOM4_0_Handler, /* 62 SERCOM4_0 */ + .pfnSERCOM4_1_Handler = (void *)SERCOM4_1_Handler, /* 63 SERCOM4_1 */ + .pfnSERCOM4_2_Handler = (void *)SERCOM4_2_Handler, /* 64 SERCOM4_2 */ + .pfnSERCOM4_3_Handler = (void *)SERCOM4_3_Handler, /* 65 SERCOM4_3, SERCOM4_4, SERCOM4_5, SERCOM4_6 */ #else - .pvReserved62 = (void*) (0UL), /* 62 Reserved */ - .pvReserved63 = (void*) (0UL), /* 63 Reserved */ - .pvReserved64 = (void*) (0UL), /* 64 Reserved */ - .pvReserved65 = (void*) (0UL), /* 65 Reserved */ + .pvReserved62 = (void *)(0UL), /* 62 Reserved */ + .pvReserved63 = (void *)(0UL), /* 63 Reserved */ + .pvReserved64 = (void *)(0UL), /* 64 Reserved */ + .pvReserved65 = (void *)(0UL), /* 65 Reserved */ #endif #ifdef ID_SERCOM5 - .pfnSERCOM5_0_Handler = (void*) SERCOM5_0_Handler, /* 66 SERCOM5_0 */ - .pfnSERCOM5_1_Handler = (void*) SERCOM5_1_Handler, /* 67 SERCOM5_1 */ - .pfnSERCOM5_2_Handler = (void*) SERCOM5_2_Handler, /* 68 SERCOM5_2 */ - .pfnSERCOM5_3_Handler = (void*) SERCOM5_3_Handler, /* 69 SERCOM5_3, SERCOM5_4, SERCOM5_5, SERCOM5_6 */ + .pfnSERCOM5_0_Handler = (void *)SERCOM5_0_Handler, /* 66 SERCOM5_0 */ + .pfnSERCOM5_1_Handler = (void *)SERCOM5_1_Handler, /* 67 SERCOM5_1 */ + .pfnSERCOM5_2_Handler = (void *)SERCOM5_2_Handler, /* 68 SERCOM5_2 */ + .pfnSERCOM5_3_Handler = (void *)SERCOM5_3_Handler, /* 69 SERCOM5_3, SERCOM5_4, SERCOM5_5, SERCOM5_6 */ #else - .pvReserved66 = (void*) (0UL), /* 66 Reserved */ - .pvReserved67 = (void*) (0UL), /* 67 Reserved */ - .pvReserved68 = (void*) (0UL), /* 68 Reserved */ - .pvReserved69 = (void*) (0UL), /* 69 Reserved */ + .pvReserved66 = (void *)(0UL), /* 66 Reserved */ + .pvReserved67 = (void *)(0UL), /* 67 Reserved */ + .pvReserved68 = (void *)(0UL), /* 68 Reserved */ + .pvReserved69 = (void *)(0UL), /* 69 Reserved */ #endif #ifdef ID_SERCOM6 - .pfnSERCOM6_0_Handler = (void*) SERCOM6_0_Handler, /* 70 SERCOM6_0 */ - .pfnSERCOM6_1_Handler = (void*) SERCOM6_1_Handler, /* 71 SERCOM6_1 */ - .pfnSERCOM6_2_Handler = (void*) SERCOM6_2_Handler, /* 72 SERCOM6_2 */ - .pfnSERCOM6_3_Handler = (void*) SERCOM6_3_Handler, /* 73 SERCOM6_3, SERCOM6_4, SERCOM6_5, SERCOM6_6 */ + .pfnSERCOM6_0_Handler = (void *)SERCOM6_0_Handler, /* 70 SERCOM6_0 */ + .pfnSERCOM6_1_Handler = (void *)SERCOM6_1_Handler, /* 71 SERCOM6_1 */ + .pfnSERCOM6_2_Handler = (void *)SERCOM6_2_Handler, /* 72 SERCOM6_2 */ + .pfnSERCOM6_3_Handler = (void *)SERCOM6_3_Handler, /* 73 SERCOM6_3, SERCOM6_4, SERCOM6_5, SERCOM6_6 */ #else - .pvReserved70 = (void*) (0UL), /* 70 Reserved */ - .pvReserved71 = (void*) (0UL), /* 71 Reserved */ - .pvReserved72 = (void*) (0UL), /* 72 Reserved */ - .pvReserved73 = (void*) (0UL), /* 73 Reserved */ + .pvReserved70 = (void *)(0UL), /* 70 Reserved */ + .pvReserved71 = (void *)(0UL), /* 71 Reserved */ + .pvReserved72 = (void *)(0UL), /* 72 Reserved */ + .pvReserved73 = (void *)(0UL), /* 73 Reserved */ #endif #ifdef ID_SERCOM7 - .pfnSERCOM7_0_Handler = (void*) SERCOM7_0_Handler, /* 74 SERCOM7_0 */ - .pfnSERCOM7_1_Handler = (void*) SERCOM7_1_Handler, /* 75 SERCOM7_1 */ - .pfnSERCOM7_2_Handler = (void*) SERCOM7_2_Handler, /* 76 SERCOM7_2 */ - .pfnSERCOM7_3_Handler = (void*) SERCOM7_3_Handler, /* 77 SERCOM7_3, SERCOM7_4, SERCOM7_5, SERCOM7_6 */ + .pfnSERCOM7_0_Handler = (void *)SERCOM7_0_Handler, /* 74 SERCOM7_0 */ + .pfnSERCOM7_1_Handler = (void *)SERCOM7_1_Handler, /* 75 SERCOM7_1 */ + .pfnSERCOM7_2_Handler = (void *)SERCOM7_2_Handler, /* 76 SERCOM7_2 */ + .pfnSERCOM7_3_Handler = (void *)SERCOM7_3_Handler, /* 77 SERCOM7_3, SERCOM7_4, SERCOM7_5, SERCOM7_6 */ #else - .pvReserved74 = (void*) (0UL), /* 74 Reserved */ - .pvReserved75 = (void*) (0UL), /* 75 Reserved */ - .pvReserved76 = (void*) (0UL), /* 76 Reserved */ - .pvReserved77 = (void*) (0UL), /* 77 Reserved */ + .pvReserved74 = (void *)(0UL), /* 74 Reserved */ + .pvReserved75 = (void *)(0UL), /* 75 Reserved */ + .pvReserved76 = (void *)(0UL), /* 76 Reserved */ + .pvReserved77 = (void *)(0UL), /* 77 Reserved */ #endif #ifdef ID_CAN0 - .pfnCAN0_Handler = (void*) CAN0_Handler, /* 78 Control Area Network 0 */ + .pfnCAN0_Handler = (void *)CAN0_Handler, /* 78 Control Area Network 0 */ #else - .pvReserved78 = (void*) (0UL), /* 78 Reserved */ + .pvReserved78 = (void *)(0UL), /* 78 Reserved */ #endif #ifdef ID_CAN1 - .pfnCAN1_Handler = (void*) CAN1_Handler, /* 79 Control Area Network 1 */ + .pfnCAN1_Handler = (void *)CAN1_Handler, /* 79 Control Area Network 1 */ #else - .pvReserved79 = (void*) (0UL), /* 79 Reserved */ + .pvReserved79 = (void *)(0UL), /* 79 Reserved */ #endif #ifdef ID_USB - .pfnUSB_0_Handler = (void*) USB_0_Handler, /* 80 USB_EORSM_DNRSM, USB_EORST_RST, USB_LPMSUSP_DDISC, USB_LPM_DCONN, USB_MSOF, USB_RAMACER, USB_RXSTP_TXSTP_0, USB_RXSTP_TXSTP_1, USB_RXSTP_TXSTP_2, USB_RXSTP_TXSTP_3, USB_RXSTP_TXSTP_4, USB_RXSTP_TXSTP_5, USB_RXSTP_TXSTP_6, USB_RXSTP_TXSTP_7, USB_STALL0_STALL_0, USB_STALL0_STALL_1, USB_STALL0_STALL_2, USB_STALL0_STALL_3, USB_STALL0_STALL_4, USB_STALL0_STALL_5, USB_STALL0_STALL_6, USB_STALL0_STALL_7, USB_STALL1_0, USB_STALL1_1, USB_STALL1_2, USB_STALL1_3, USB_STALL1_4, USB_STALL1_5, USB_STALL1_6, USB_STALL1_7, USB_SUSPEND, USB_TRFAIL0_TRFAIL_0, USB_TRFAIL0_TRFAIL_1, USB_TRFAIL0_TRFAIL_2, USB_TRFAIL0_TRFAIL_3, USB_TRFAIL0_TRFAIL_4, USB_TRFAIL0_TRFAIL_5, USB_TRFAIL0_TRFAIL_6, USB_TRFAIL0_TRFAIL_7, USB_TRFAIL1_PERR_0, USB_TRFAIL1_PERR_1, USB_TRFAIL1_PERR_2, USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5, USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */ - .pfnUSB_1_Handler = (void*) USB_1_Handler, /* 81 USB_SOF_HSOF */ - .pfnUSB_2_Handler = (void*) USB_2_Handler, /* 82 USB_TRCPT0_0, USB_TRCPT0_1, USB_TRCPT0_2, USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5, USB_TRCPT0_6, USB_TRCPT0_7 */ - .pfnUSB_3_Handler = (void*) USB_3_Handler, /* 83 USB_TRCPT1_0, USB_TRCPT1_1, USB_TRCPT1_2, USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5, USB_TRCPT1_6, USB_TRCPT1_7 */ + .pfnUSB_0_Handler = (void *)USB_0_Handler, /* 80 USB_EORSM_DNRSM, USB_EORST_RST, USB_LPMSUSP_DDISC, USB_LPM_DCONN, USB_MSOF, USB_RAMACER, USB_RXSTP_TXSTP_0, USB_RXSTP_TXSTP_1, USB_RXSTP_TXSTP_2, USB_RXSTP_TXSTP_3, USB_RXSTP_TXSTP_4, USB_RXSTP_TXSTP_5, USB_RXSTP_TXSTP_6, USB_RXSTP_TXSTP_7, USB_STALL0_STALL_0, USB_STALL0_STALL_1, USB_STALL0_STALL_2, USB_STALL0_STALL_3, USB_STALL0_STALL_4, USB_STALL0_STALL_5, USB_STALL0_STALL_6, USB_STALL0_STALL_7, USB_STALL1_0, USB_STALL1_1, USB_STALL1_2, USB_STALL1_3, USB_STALL1_4, USB_STALL1_5, USB_STALL1_6, USB_STALL1_7, USB_SUSPEND, USB_TRFAIL0_TRFAIL_0, USB_TRFAIL0_TRFAIL_1, USB_TRFAIL0_TRFAIL_2, USB_TRFAIL0_TRFAIL_3, USB_TRFAIL0_TRFAIL_4, USB_TRFAIL0_TRFAIL_5, USB_TRFAIL0_TRFAIL_6, USB_TRFAIL0_TRFAIL_7, USB_TRFAIL1_PERR_0, USB_TRFAIL1_PERR_1, USB_TRFAIL1_PERR_2, USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5, USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */ + .pfnUSB_1_Handler = (void *)USB_1_Handler, /* 81 USB_SOF_HSOF */ + .pfnUSB_2_Handler = (void *)USB_2_Handler, /* 82 USB_TRCPT0_0, USB_TRCPT0_1, USB_TRCPT0_2, USB_TRCPT0_3, USB_TRCPT0_4, USB_TRCPT0_5, USB_TRCPT0_6, USB_TRCPT0_7 */ + .pfnUSB_3_Handler = (void *)USB_3_Handler, /* 83 USB_TRCPT1_0, USB_TRCPT1_1, USB_TRCPT1_2, USB_TRCPT1_3, USB_TRCPT1_4, USB_TRCPT1_5, USB_TRCPT1_6, USB_TRCPT1_7 */ #else - .pvReserved80 = (void*) (0UL), /* 80 Reserved */ - .pvReserved81 = (void*) (0UL), /* 81 Reserved */ - .pvReserved82 = (void*) (0UL), /* 82 Reserved */ - .pvReserved83 = (void*) (0UL), /* 83 Reserved */ + .pvReserved80 = (void *)(0UL), /* 80 Reserved */ + .pvReserved81 = (void *)(0UL), /* 81 Reserved */ + .pvReserved82 = (void *)(0UL), /* 82 Reserved */ + .pvReserved83 = (void *)(0UL), /* 83 Reserved */ #endif #ifdef ID_GMAC - .pfnGMAC_Handler = (void*) GMAC_Handler, /* 84 Ethernet MAC */ + .pfnGMAC_Handler = (void *)GMAC_Handler, /* 84 Ethernet MAC */ #else - .pvReserved84 = (void*) (0UL), /* 84 Reserved */ -#endif - .pfnTCC0_0_Handler = (void*) TCC0_0_Handler, /* 85 TCC0_CNT_A, TCC0_DFS_A, TCC0_ERR_A, TCC0_FAULT0_A, TCC0_FAULT1_A, TCC0_FAULTA_A, TCC0_FAULTB_A, TCC0_OVF, TCC0_TRG, TCC0_UFS_A */ - .pfnTCC0_1_Handler = (void*) TCC0_1_Handler, /* 86 TCC0_MC_0 */ - .pfnTCC0_2_Handler = (void*) TCC0_2_Handler, /* 87 TCC0_MC_1 */ - .pfnTCC0_3_Handler = (void*) TCC0_3_Handler, /* 88 TCC0_MC_2 */ - .pfnTCC0_4_Handler = (void*) TCC0_4_Handler, /* 89 TCC0_MC_3 */ - .pfnTCC0_5_Handler = (void*) TCC0_5_Handler, /* 90 TCC0_MC_4 */ - .pfnTCC0_6_Handler = (void*) TCC0_6_Handler, /* 91 TCC0_MC_5 */ - .pfnTCC1_0_Handler = (void*) TCC1_0_Handler, /* 92 TCC1_CNT_A, TCC1_DFS_A, TCC1_ERR_A, TCC1_FAULT0_A, TCC1_FAULT1_A, TCC1_FAULTA_A, TCC1_FAULTB_A, TCC1_OVF, TCC1_TRG, TCC1_UFS_A */ - .pfnTCC1_1_Handler = (void*) TCC1_1_Handler, /* 93 TCC1_MC_0 */ - .pfnTCC1_2_Handler = (void*) TCC1_2_Handler, /* 94 TCC1_MC_1 */ - .pfnTCC1_3_Handler = (void*) TCC1_3_Handler, /* 95 TCC1_MC_2 */ - .pfnTCC1_4_Handler = (void*) TCC1_4_Handler, /* 96 TCC1_MC_3 */ - .pfnTCC2_0_Handler = (void*) TCC2_0_Handler, /* 97 TCC2_CNT_A, TCC2_DFS_A, TCC2_ERR_A, TCC2_FAULT0_A, TCC2_FAULT1_A, TCC2_FAULTA_A, TCC2_FAULTB_A, TCC2_OVF, TCC2_TRG, TCC2_UFS_A */ - .pfnTCC2_1_Handler = (void*) TCC2_1_Handler, /* 98 TCC2_MC_0 */ - .pfnTCC2_2_Handler = (void*) TCC2_2_Handler, /* 99 TCC2_MC_1 */ - .pfnTCC2_3_Handler = (void*) TCC2_3_Handler, /* 100 TCC2_MC_2 */ + .pvReserved84 = (void *)(0UL), /* 84 Reserved */ +#endif + .pfnTCC0_0_Handler = (void *)TCC0_0_Handler, /* 85 TCC0_CNT_A, TCC0_DFS_A, TCC0_ERR_A, TCC0_FAULT0_A, TCC0_FAULT1_A, TCC0_FAULTA_A, TCC0_FAULTB_A, TCC0_OVF, TCC0_TRG, TCC0_UFS_A */ + .pfnTCC0_1_Handler = (void *)TCC0_1_Handler, /* 86 TCC0_MC_0 */ + .pfnTCC0_2_Handler = (void *)TCC0_2_Handler, /* 87 TCC0_MC_1 */ + .pfnTCC0_3_Handler = (void *)TCC0_3_Handler, /* 88 TCC0_MC_2 */ + .pfnTCC0_4_Handler = (void *)TCC0_4_Handler, /* 89 TCC0_MC_3 */ + .pfnTCC0_5_Handler = (void *)TCC0_5_Handler, /* 90 TCC0_MC_4 */ + .pfnTCC0_6_Handler = (void *)TCC0_6_Handler, /* 91 TCC0_MC_5 */ + .pfnTCC1_0_Handler = (void *)TCC1_0_Handler, /* 92 TCC1_CNT_A, TCC1_DFS_A, TCC1_ERR_A, TCC1_FAULT0_A, TCC1_FAULT1_A, TCC1_FAULTA_A, TCC1_FAULTB_A, TCC1_OVF, TCC1_TRG, TCC1_UFS_A */ + .pfnTCC1_1_Handler = (void *)TCC1_1_Handler, /* 93 TCC1_MC_0 */ + .pfnTCC1_2_Handler = (void *)TCC1_2_Handler, /* 94 TCC1_MC_1 */ + .pfnTCC1_3_Handler = (void *)TCC1_3_Handler, /* 95 TCC1_MC_2 */ + .pfnTCC1_4_Handler = (void *)TCC1_4_Handler, /* 96 TCC1_MC_3 */ + .pfnTCC2_0_Handler = (void *)TCC2_0_Handler, /* 97 TCC2_CNT_A, TCC2_DFS_A, TCC2_ERR_A, TCC2_FAULT0_A, TCC2_FAULT1_A, TCC2_FAULTA_A, TCC2_FAULTB_A, TCC2_OVF, TCC2_TRG, TCC2_UFS_A */ + .pfnTCC2_1_Handler = (void *)TCC2_1_Handler, /* 98 TCC2_MC_0 */ + .pfnTCC2_2_Handler = (void *)TCC2_2_Handler, /* 99 TCC2_MC_1 */ + .pfnTCC2_3_Handler = (void *)TCC2_3_Handler, /* 100 TCC2_MC_2 */ #ifdef ID_TCC3 - .pfnTCC3_0_Handler = (void*) TCC3_0_Handler, /* 101 TCC3_CNT_A, TCC3_DFS_A, TCC3_ERR_A, TCC3_FAULT0_A, TCC3_FAULT1_A, TCC3_FAULTA_A, TCC3_FAULTB_A, TCC3_OVF, TCC3_TRG, TCC3_UFS_A */ - .pfnTCC3_1_Handler = (void*) TCC3_1_Handler, /* 102 TCC3_MC_0 */ - .pfnTCC3_2_Handler = (void*) TCC3_2_Handler, /* 103 TCC3_MC_1 */ + .pfnTCC3_0_Handler = (void *)TCC3_0_Handler, /* 101 TCC3_CNT_A, TCC3_DFS_A, TCC3_ERR_A, TCC3_FAULT0_A, TCC3_FAULT1_A, TCC3_FAULTA_A, TCC3_FAULTB_A, TCC3_OVF, TCC3_TRG, TCC3_UFS_A */ + .pfnTCC3_1_Handler = (void *)TCC3_1_Handler, /* 102 TCC3_MC_0 */ + .pfnTCC3_2_Handler = (void *)TCC3_2_Handler, /* 103 TCC3_MC_1 */ #else - .pvReserved101 = (void*) (0UL), /* 101 Reserved */ - .pvReserved102 = (void*) (0UL), /* 102 Reserved */ - .pvReserved103 = (void*) (0UL), /* 103 Reserved */ + .pvReserved101 = (void *)(0UL), /* 101 Reserved */ + .pvReserved102 = (void *)(0UL), /* 102 Reserved */ + .pvReserved103 = (void *)(0UL), /* 103 Reserved */ #endif #ifdef ID_TCC4 - .pfnTCC4_0_Handler = (void*) TCC4_0_Handler, /* 104 TCC4_CNT_A, TCC4_DFS_A, TCC4_ERR_A, TCC4_FAULT0_A, TCC4_FAULT1_A, TCC4_FAULTA_A, TCC4_FAULTB_A, TCC4_OVF, TCC4_TRG, TCC4_UFS_A */ - .pfnTCC4_1_Handler = (void*) TCC4_1_Handler, /* 105 TCC4_MC_0 */ - .pfnTCC4_2_Handler = (void*) TCC4_2_Handler, /* 106 TCC4_MC_1 */ + .pfnTCC4_0_Handler = (void *)TCC4_0_Handler, /* 104 TCC4_CNT_A, TCC4_DFS_A, TCC4_ERR_A, TCC4_FAULT0_A, TCC4_FAULT1_A, TCC4_FAULTA_A, TCC4_FAULTB_A, TCC4_OVF, TCC4_TRG, TCC4_UFS_A */ + .pfnTCC4_1_Handler = (void *)TCC4_1_Handler, /* 105 TCC4_MC_0 */ + .pfnTCC4_2_Handler = (void *)TCC4_2_Handler, /* 106 TCC4_MC_1 */ #else - .pvReserved104 = (void*) (0UL), /* 104 Reserved */ - .pvReserved105 = (void*) (0UL), /* 105 Reserved */ - .pvReserved106 = (void*) (0UL), /* 106 Reserved */ -#endif - .pfnTC0_Handler = (void*) TC0_Handler, /* 107 Basic Timer Counter 0 */ - .pfnTC1_Handler = (void*) TC1_Handler, /* 108 Basic Timer Counter 1 */ - .pfnTC2_Handler = (void*) TC2_Handler, /* 109 Basic Timer Counter 2 */ - .pfnTC3_Handler = (void*) TC3_Handler, /* 110 Basic Timer Counter 3 */ + .pvReserved104 = (void *)(0UL), /* 104 Reserved */ + .pvReserved105 = (void *)(0UL), /* 105 Reserved */ + .pvReserved106 = (void *)(0UL), /* 106 Reserved */ +#endif + .pfnTC0_Handler = (void *)TC0_Handler, /* 107 Basic Timer Counter 0 */ + .pfnTC1_Handler = (void *)TC1_Handler, /* 108 Basic Timer Counter 1 */ + .pfnTC2_Handler = (void *)TC2_Handler, /* 109 Basic Timer Counter 2 */ + .pfnTC3_Handler = (void *)TC3_Handler, /* 110 Basic Timer Counter 3 */ #ifdef ID_TC4 - .pfnTC4_Handler = (void*) TC4_Handler, /* 111 Basic Timer Counter 4 */ + .pfnTC4_Handler = (void *)TC4_Handler, /* 111 Basic Timer Counter 4 */ #else - .pvReserved111 = (void*) (0UL), /* 111 Reserved */ + .pvReserved111 = (void *)(0UL), /* 111 Reserved */ #endif #ifdef ID_TC5 - .pfnTC5_Handler = (void*) TC5_Handler, /* 112 Basic Timer Counter 5 */ + .pfnTC5_Handler = (void *)TC5_Handler, /* 112 Basic Timer Counter 5 */ #else - .pvReserved112 = (void*) (0UL), /* 112 Reserved */ + .pvReserved112 = (void *)(0UL), /* 112 Reserved */ #endif #ifdef ID_TC6 - .pfnTC6_Handler = (void*) TC6_Handler, /* 113 Basic Timer Counter 6 */ + .pfnTC6_Handler = (void *)TC6_Handler, /* 113 Basic Timer Counter 6 */ #else - .pvReserved113 = (void*) (0UL), /* 113 Reserved */ + .pvReserved113 = (void *)(0UL), /* 113 Reserved */ #endif #ifdef ID_TC7 - .pfnTC7_Handler = (void*) TC7_Handler, /* 114 Basic Timer Counter 7 */ + .pfnTC7_Handler = (void *)TC7_Handler, /* 114 Basic Timer Counter 7 */ #else - .pvReserved114 = (void*) (0UL), /* 114 Reserved */ -#endif - .pfnPDEC_0_Handler = (void*) PDEC_0_Handler, /* 115 PDEC_DIR_A, PDEC_ERR_A, PDEC_OVF, PDEC_VLC_A */ - .pfnPDEC_1_Handler = (void*) PDEC_1_Handler, /* 116 PDEC_MC_0 */ - .pfnPDEC_2_Handler = (void*) PDEC_2_Handler, /* 117 PDEC_MC_1 */ - .pfnADC0_0_Handler = (void*) ADC0_0_Handler, /* 118 ADC0_OVERRUN, ADC0_WINMON */ - .pfnADC0_1_Handler = (void*) ADC0_1_Handler, /* 119 ADC0_RESRDY */ - .pfnADC1_0_Handler = (void*) ADC1_0_Handler, /* 120 ADC1_OVERRUN, ADC1_WINMON */ - .pfnADC1_1_Handler = (void*) ADC1_1_Handler, /* 121 ADC1_RESRDY */ - .pfnAC_Handler = (void*) AC_Handler, /* 122 Analog Comparators */ - .pfnDAC_0_Handler = (void*) DAC_0_Handler, /* 123 DAC_OVERRUN_A_0, DAC_OVERRUN_A_1, DAC_UNDERRUN_A_0, DAC_UNDERRUN_A_1 */ - .pfnDAC_1_Handler = (void*) DAC_1_Handler, /* 124 DAC_EMPTY_0 */ - .pfnDAC_2_Handler = (void*) DAC_2_Handler, /* 125 DAC_EMPTY_1 */ - .pfnDAC_3_Handler = (void*) DAC_3_Handler, /* 126 DAC_RESRDY_0 */ - .pfnDAC_4_Handler = (void*) DAC_4_Handler, /* 127 DAC_RESRDY_1 */ + .pvReserved114 = (void *)(0UL), /* 114 Reserved */ +#endif + .pfnPDEC_0_Handler = (void *)PDEC_0_Handler, /* 115 PDEC_DIR_A, PDEC_ERR_A, PDEC_OVF, PDEC_VLC_A */ + .pfnPDEC_1_Handler = (void *)PDEC_1_Handler, /* 116 PDEC_MC_0 */ + .pfnPDEC_2_Handler = (void *)PDEC_2_Handler, /* 117 PDEC_MC_1 */ + .pfnADC0_0_Handler = (void *)ADC0_0_Handler, /* 118 ADC0_OVERRUN, ADC0_WINMON */ + .pfnADC0_1_Handler = (void *)ADC0_1_Handler, /* 119 ADC0_RESRDY */ + .pfnADC1_0_Handler = (void *)ADC1_0_Handler, /* 120 ADC1_OVERRUN, ADC1_WINMON */ + .pfnADC1_1_Handler = (void *)ADC1_1_Handler, /* 121 ADC1_RESRDY */ + .pfnAC_Handler = (void *)AC_Handler, /* 122 Analog Comparators */ + .pfnDAC_0_Handler = (void *)DAC_0_Handler, /* 123 DAC_OVERRUN_A_0, DAC_OVERRUN_A_1, DAC_UNDERRUN_A_0, DAC_UNDERRUN_A_1 */ + .pfnDAC_1_Handler = (void *)DAC_1_Handler, /* 124 DAC_EMPTY_0 */ + .pfnDAC_2_Handler = (void *)DAC_2_Handler, /* 125 DAC_EMPTY_1 */ + .pfnDAC_3_Handler = (void *)DAC_3_Handler, /* 126 DAC_RESRDY_0 */ + .pfnDAC_4_Handler = (void *)DAC_4_Handler, /* 127 DAC_RESRDY_1 */ #ifdef ID_I2S - .pfnI2S_Handler = (void*) I2S_Handler, /* 128 Inter-IC Sound Interface */ + .pfnI2S_Handler = (void *)I2S_Handler, /* 128 Inter-IC Sound Interface */ #else - .pvReserved128 = (void*) (0UL), /* 128 Reserved */ + .pvReserved128 = (void *)(0UL), /* 128 Reserved */ #endif - .pfnPCC_Handler = (void*) PCC_Handler, /* 129 Parallel Capture Controller */ - .pfnAES_Handler = (void*) AES_Handler, /* 130 Advanced Encryption Standard */ - .pfnTRNG_Handler = (void*) TRNG_Handler, /* 131 True Random Generator */ + .pfnPCC_Handler = (void *)PCC_Handler, /* 129 Parallel Capture Controller */ + .pfnAES_Handler = (void *)AES_Handler, /* 130 Advanced Encryption Standard */ + .pfnTRNG_Handler = (void *)TRNG_Handler, /* 131 True Random Generator */ #ifdef ID_ICM - .pfnICM_Handler = (void*) ICM_Handler, /* 132 Integrity Check Monitor */ + .pfnICM_Handler = (void *)ICM_Handler, /* 132 Integrity Check Monitor */ #else - .pvReserved132 = (void*) (0UL), /* 132 Reserved */ + .pvReserved132 = (void *)(0UL), /* 132 Reserved */ #endif #ifdef ID_PUKCC - .pfnPUKCC_Handler = (void*) PUKCC_Handler, /* 133 PUblic-Key Cryptography Controller */ + .pfnPUKCC_Handler = (void *)PUKCC_Handler, /* 133 PUblic-Key Cryptography Controller */ #else - .pvReserved133 = (void*) (0UL), /* 133 Reserved */ + .pvReserved133 = (void *)(0UL), /* 133 Reserved */ #endif - .pfnQSPI_Handler = (void*) QSPI_Handler, /* 134 Quad SPI interface */ + .pfnQSPI_Handler = (void *)QSPI_Handler, /* 134 Quad SPI interface */ #ifdef ID_SDHC0 - .pfnSDHC0_Handler = (void*) SDHC0_Handler, /* 135 SD/MMC Host Controller 0 */ + .pfnSDHC0_Handler = (void *)SDHC0_Handler, /* 135 SD/MMC Host Controller 0 */ #else - .pvReserved135 = (void*) (0UL), /* 135 Reserved */ + .pvReserved135 = (void *)(0UL), /* 135 Reserved */ #endif #ifdef ID_SDHC1 - .pfnSDHC1_Handler = (void*) SDHC1_Handler /* 136 SD/MMC Host Controller 1 */ + .pfnSDHC1_Handler = (void *)SDHC1_Handler /* 136 SD/MMC Host Controller 1 */ #else - .pvReserved136 = (void*) (0UL) /* 136 Reserved */ + .pvReserved136 = (void *)(0UL) /* 136 Reserved */ #endif }; @@ -499,61 +500,60 @@ const DeviceVectors exception_table = { * \brief This is the code that gets called on processor reset. * To initialize the device, and call the main() routine. */ -void Reset_Handler(void) -{ +void Reset_Handler(void) { #ifdef KEYBOARD_massdrop_ctrl - /* WARNING: This is only for CTRL bootloader release "v2.18Jun 22 2018 17:28:08" for bootloader_jump support */ - if (*MAGIC_ADDR == BOOTLOADER_MAGIC) { - /* At this point, the bootloader's memory is initialized properly, so undo the jump to here, then jump back */ - *MAGIC_ADDR = 0x00000000; /* Change value to prevent potential bootloader entrance loop */ - __set_MSP(0x20008818); /* MSP according to bootloader */ - SCB->VTOR = 0x00000000; /* Vector table back to bootloader's */ - asm("bx %0"::"r"(0x00001267)); /* Jump past bootloader RCAUSE check using THUMB */ - } -#endif - uint32_t *pSrc, *pDest; + /* WARNING: This is only for CTRL bootloader release "v2.18Jun 22 2018 17:28:08" for bootloader_jump support */ + if (*MAGIC_ADDR == BOOTLOADER_MAGIC) { + /* At this point, the bootloader's memory is initialized properly, so undo the jump to here, then jump back */ + *MAGIC_ADDR = 0x00000000; /* Change value to prevent potential bootloader entrance loop */ + __set_MSP(0x20008818); /* MSP according to bootloader */ + SCB->VTOR = 0x00000000; /* Vector table back to bootloader's */ + asm("bx %0" ::"r"(0x00001267)); /* Jump past bootloader RCAUSE check using THUMB */ + } +#endif + uint32_t *pSrc, *pDest; - /* Initialize the relocate segment */ - pSrc = &_etext; - pDest = &_srelocate; + /* Initialize the relocate segment */ + pSrc = &_etext; + pDest = &_srelocate; - if (pSrc != pDest) { - for (; pDest < &_erelocate;) { - *pDest++ = *pSrc++; - } + if (pSrc != pDest) { + for (; pDest < &_erelocate;) { + *pDest++ = *pSrc++; } + } - /* Clear the zero segment */ - for (pDest = &_szero; pDest < &_ezero;) { - *pDest++ = 0; - } + /* Clear the zero segment */ + for (pDest = &_szero; pDest < &_ezero;) { + *pDest++ = 0; + } - /* Set the vector table base address */ - pSrc = (uint32_t *) & _sfixed; - SCB->VTOR = ((uint32_t) pSrc & SCB_VTOR_TBLOFF_Msk); + /* Set the vector table base address */ + pSrc = (uint32_t *)&_sfixed; + SCB->VTOR = ((uint32_t)pSrc & SCB_VTOR_TBLOFF_Msk); #if __FPU_USED - /* Enable FPU */ - SCB->CPACR |= (0xFu << 20); - __DSB(); - __ISB(); + /* Enable FPU */ + SCB->CPACR |= (0xFu << 20); + __DSB(); + __ISB(); #endif - /* Initialize the C library */ - __libc_init_array(); + /* Initialize the C library */ + __libc_init_array(); - /* Branch to main function */ - main(); + /* Branch to main function */ + main(); - /* Infinite loop */ - while (1); + /* Infinite loop */ + while (1) + ; } /** * \brief Default interrupt handler for unused IRQs. */ -void Dummy_Handler(void) -{ - while (1) { - } +void Dummy_Handler(void) { + while (1) { + } } diff --git a/tmk_core/protocol/arm_atsam/usb/compiler.h b/tmk_core/protocol/arm_atsam/usb/compiler.h index b2ccfd73e..6ba232234 100644 --- a/tmk_core/protocol/arm_atsam/usb/compiler.h +++ b/tmk_core/protocol/arm_atsam/usb/compiler.h @@ -43,7 +43,7 @@ */ #ifndef UTILS_COMPILER_H_INCLUDED -#define UTILS_COMPILER_H_INCLUDED +# define UTILS_COMPILER_H_INCLUDED /** * \defgroup group_sam0_utils Compiler abstraction layer and code utilities @@ -54,38 +54,38 @@ * @{ */ -#if (defined __ICCARM__) -# include <intrinsics.h> -#endif +# if (defined __ICCARM__) +# include <intrinsics.h> +# endif -#include <stddef.h> +# include <stddef.h> //#include <parts.h> //#include <status_codes.h> //#include <preprocessor.h> //#include <io.h> -#ifndef __ASSEMBLY__ +# ifndef __ASSEMBLY__ -#include <stdio.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> +# include <stdio.h> +# include <stdbool.h> +# include <stdint.h> +# include <stdlib.h> /** * \def UNUSED * \brief Marking \a v as a unused parameter or value. */ -#define UNUSED(v) (void)(v) +# define UNUSED(v) (void)(v) /** * \def barrier * \brief Memory barrier */ -#ifdef __GNUC__ -# define barrier() asm volatile("" ::: "memory") -#else -# define barrier() asm ("") -#endif +# ifdef __GNUC__ +# define barrier() asm volatile("" ::: "memory") +# else +# define barrier() asm("") +# endif /** * \brief Emit the compiler pragma \a arg. @@ -93,38 +93,37 @@ * \param[in] arg The pragma directive as it would appear after \e \#pragma * (i.e. not stringified). */ -#define COMPILER_PRAGMA(arg) _Pragma(#arg) +# define COMPILER_PRAGMA(arg) _Pragma(# arg) /** * \def COMPILER_PACK_SET(alignment) * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment. */ -#define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment)) +# define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment)) /** * \def COMPILER_PACK_RESET() * \brief Set default alignment for subsequent struct and union definitions. */ -#define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack()) - +# define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack()) /** * \brief Set aligned boundary. */ -#if (defined __GNUC__) || (defined __CC_ARM) -# define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) -#elif (defined __ICCARM__) -# define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a) -#endif +# if (defined __GNUC__) || (defined __CC_ARM) +# define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) +# elif (defined __ICCARM__) +# define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a) +# endif /** * \brief Set word-aligned boundary. */ -#if (defined __GNUC__) || defined(__CC_ARM) -#define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) -#elif (defined __ICCARM__) -#define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4) -#endif +# if (defined __GNUC__) || defined(__CC_ARM) +# define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) +# elif (defined __ICCARM__) +# define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4) +# endif /** * \def __always_inline @@ -134,15 +133,15 @@ * heuristics and inline the function no matter how big it thinks it * becomes. */ -#if !defined(__always_inline) -#if defined(__CC_ARM) -# define __always_inline __forceinline -#elif (defined __GNUC__) -# define __always_inline __attribute__((__always_inline__)) -#elif (defined __ICCARM__) -# define __always_inline _Pragma("inline=forced") -#endif -#endif +# if !defined(__always_inline) +# if defined(__CC_ARM) +# define __always_inline __forceinline +# elif (defined __GNUC__) +# define __always_inline __attribute__((__always_inline__)) +# elif (defined __ICCARM__) +# define __always_inline _Pragma("inline=forced") +# endif +# endif /** * \def __no_inline @@ -152,14 +151,13 @@ * heuristics and not inline the function no matter how small it thinks it * becomes. */ -#if defined(__CC_ARM) -# define __no_inline __attribute__((noinline)) -#elif (defined __GNUC__) -# define __no_inline __attribute__((noinline)) -#elif (defined __ICCARM__) -# define __no_inline _Pragma("inline=never") -#endif - +# if defined(__CC_ARM) +# define __no_inline __attribute__((noinline)) +# elif (defined __GNUC__) +# define __no_inline __attribute__((noinline)) +# elif (defined __ICCARM__) +# define __no_inline _Pragma("inline=never") +# endif /** \brief This macro is used to test fatal errors. * @@ -170,212 +168,201 @@ * * \param[in] expr Expression to evaluate and supposed to be nonzero. */ -#if defined(_ASSERT_ENABLE_) -# if defined(TEST_SUITE_DEFINE_ASSERT_MACRO) -# include "unit_test/suite.h" -# else -# undef TEST_SUITE_DEFINE_ASSERT_MACRO -# define Assert(expr) \ - {\ - if (!(expr)) asm("BKPT #0");\ - } -# endif -#else -# define Assert(expr) ((void) 0) -#endif +# if defined(_ASSERT_ENABLE_) +# if defined(TEST_SUITE_DEFINE_ASSERT_MACRO) +# include "unit_test/suite.h" +# else +# undef TEST_SUITE_DEFINE_ASSERT_MACRO +# define Assert(expr) \ + { \ + if (!(expr)) asm("BKPT #0"); \ + } +# endif +# else +# define Assert(expr) ((void)0) +# endif /* Define WEAK attribute */ -#if defined ( __CC_ARM ) -# define WEAK __attribute__ ((weak)) -#elif defined ( __ICCARM__ ) -# define WEAK __weak -#elif defined ( __GNUC__ ) -# define WEAK __attribute__ ((weak)) -#endif +# if defined(__CC_ARM) +# define WEAK __attribute__((weak)) +# elif defined(__ICCARM__) +# define WEAK __weak +# elif defined(__GNUC__) +# define WEAK __attribute__((weak)) +# endif /* Define NO_INIT attribute */ -#if defined ( __CC_ARM ) -# define NO_INIT __attribute__((zero_init)) -#elif defined ( __ICCARM__ ) -# define NO_INIT __no_init -#elif defined ( __GNUC__ ) -# define NO_INIT __attribute__((section(".no_init"))) -#endif +# if defined(__CC_ARM) +# define NO_INIT __attribute__((zero_init)) +# elif defined(__ICCARM__) +# define NO_INIT __no_init +# elif defined(__GNUC__) +# define NO_INIT __attribute__((section(".no_init"))) +# endif //#include "interrupt.h" /** \name Usual Types * @{ */ -#ifndef __cplusplus -# if !defined(__bool_true_false_are_defined) -typedef unsigned char bool; -# endif -#endif -typedef uint16_t le16_t; -typedef uint16_t be16_t; -typedef uint32_t le32_t; -typedef uint32_t be32_t; -typedef uint32_t iram_size_t; +# ifndef __cplusplus +# if !defined(__bool_true_false_are_defined) +typedef unsigned char bool; +# endif +# endif +typedef uint16_t le16_t; +typedef uint16_t be16_t; +typedef uint32_t le32_t; +typedef uint32_t be32_t; +typedef uint32_t iram_size_t; /** @} */ /** \name Aliasing Aggregate Types * @{ */ /** 16-bit union. */ -typedef union -{ - int16_t s16; - uint16_t u16; - int8_t s8[2]; - uint8_t u8[2]; +typedef union { + int16_t s16; + uint16_t u16; + int8_t s8[2]; + uint8_t u8[2]; } Union16; /** 32-bit union. */ -typedef union -{ - int32_t s32; - uint32_t u32; - int16_t s16[2]; - uint16_t u16[2]; - int8_t s8[4]; - uint8_t u8[4]; +typedef union { + int32_t s32; + uint32_t u32; + int16_t s16[2]; + uint16_t u16[2]; + int8_t s8[4]; + uint8_t u8[4]; } Union32; /** 64-bit union. */ -typedef union -{ - int64_t s64; - uint64_t u64; - int32_t s32[2]; - uint32_t u32[2]; - int16_t s16[4]; - uint16_t u16[4]; - int8_t s8[8]; - uint8_t u8[8]; +typedef union { + int64_t s64; + uint64_t u64; + int32_t s32[2]; + uint32_t u32[2]; + int16_t s16[4]; + uint16_t u16[4]; + int8_t s8[8]; + uint8_t u8[8]; } Union64; /** Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef union -{ - int64_t *s64ptr; - uint64_t *u64ptr; - int32_t *s32ptr; - uint32_t *u32ptr; - int16_t *s16ptr; - uint16_t *u16ptr; - int8_t *s8ptr; - uint8_t *u8ptr; +typedef union { + int64_t * s64ptr; + uint64_t *u64ptr; + int32_t * s32ptr; + uint32_t *u32ptr; + int16_t * s16ptr; + uint16_t *u16ptr; + int8_t * s8ptr; + uint8_t * u8ptr; } UnionPtr; /** Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef union -{ - volatile int64_t *s64ptr; - volatile uint64_t *u64ptr; - volatile int32_t *s32ptr; - volatile uint32_t *u32ptr; - volatile int16_t *s16ptr; - volatile uint16_t *u16ptr; - volatile int8_t *s8ptr; - volatile uint8_t *u8ptr; +typedef union { + volatile int64_t * s64ptr; + volatile uint64_t *u64ptr; + volatile int32_t * s32ptr; + volatile uint32_t *u32ptr; + volatile int16_t * s16ptr; + volatile uint16_t *u16ptr; + volatile int8_t * s8ptr; + volatile uint8_t * u8ptr; } UnionVPtr; /** Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef union -{ - const int64_t *s64ptr; - const uint64_t *u64ptr; - const int32_t *s32ptr; - const uint32_t *u32ptr; - const int16_t *s16ptr; - const uint16_t *u16ptr; - const int8_t *s8ptr; - const uint8_t *u8ptr; +typedef union { + const int64_t * s64ptr; + const uint64_t *u64ptr; + const int32_t * s32ptr; + const uint32_t *u32ptr; + const int16_t * s16ptr; + const uint16_t *u16ptr; + const int8_t * s8ptr; + const uint8_t * u8ptr; } UnionCPtr; /** Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef union -{ - const volatile int64_t *s64ptr; - const volatile uint64_t *u64ptr; - const volatile int32_t *s32ptr; - const volatile uint32_t *u32ptr; - const volatile int16_t *s16ptr; - const volatile uint16_t *u16ptr; - const volatile int8_t *s8ptr; - const volatile uint8_t *u8ptr; +typedef union { + const volatile int64_t * s64ptr; + const volatile uint64_t *u64ptr; + const volatile int32_t * s32ptr; + const volatile uint32_t *u32ptr; + const volatile int16_t * s16ptr; + const volatile uint16_t *u16ptr; + const volatile int8_t * s8ptr; + const volatile uint8_t * u8ptr; } UnionCVPtr; /** Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef struct -{ - int64_t *s64ptr; - uint64_t *u64ptr; - int32_t *s32ptr; - uint32_t *u32ptr; - int16_t *s16ptr; - uint16_t *u16ptr; - int8_t *s8ptr; - uint8_t *u8ptr; +typedef struct { + int64_t * s64ptr; + uint64_t *u64ptr; + int32_t * s32ptr; + uint32_t *u32ptr; + int16_t * s16ptr; + uint16_t *u16ptr; + int8_t * s8ptr; + uint8_t * u8ptr; } StructPtr; /** Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef struct -{ - volatile int64_t *s64ptr; - volatile uint64_t *u64ptr; - volatile int32_t *s32ptr; - volatile uint32_t *u32ptr; - volatile int16_t *s16ptr; - volatile uint16_t *u16ptr; - volatile int8_t *s8ptr; - volatile uint8_t *u8ptr; +typedef struct { + volatile int64_t * s64ptr; + volatile uint64_t *u64ptr; + volatile int32_t * s32ptr; + volatile uint32_t *u32ptr; + volatile int16_t * s16ptr; + volatile uint16_t *u16ptr; + volatile int8_t * s8ptr; + volatile uint8_t * u8ptr; } StructVPtr; /** Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef struct -{ - const int64_t *s64ptr; - const uint64_t *u64ptr; - const int32_t *s32ptr; - const uint32_t *u32ptr; - const int16_t *s16ptr; - const uint16_t *u16ptr; - const int8_t *s8ptr; - const uint8_t *u8ptr; +typedef struct { + const int64_t * s64ptr; + const uint64_t *u64ptr; + const int32_t * s32ptr; + const uint32_t *u32ptr; + const int16_t * s16ptr; + const uint16_t *u16ptr; + const int8_t * s8ptr; + const uint8_t * u8ptr; } StructCPtr; /** Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. */ -typedef struct -{ - const volatile int64_t *s64ptr; - const volatile uint64_t *u64ptr; - const volatile int32_t *s32ptr; - const volatile uint32_t *u32ptr; - const volatile int16_t *s16ptr; - const volatile uint16_t *u16ptr; - const volatile int8_t *s8ptr; - const volatile uint8_t *u8ptr; +typedef struct { + const volatile int64_t * s64ptr; + const volatile uint64_t *u64ptr; + const volatile int32_t * s32ptr; + const volatile uint32_t *u32ptr; + const volatile int16_t * s16ptr; + const volatile uint16_t *u16ptr; + const volatile int8_t * s8ptr; + const volatile uint8_t * u8ptr; } StructCVPtr; /** @} */ -#endif /* #ifndef __ASSEMBLY__ */ +# endif /* #ifndef __ASSEMBLY__ */ /** \name Usual Constants * @{ */ -//kmod #define DISABLE 0 -//kmod #define ENABLE 1 - -#ifndef __cplusplus -# if !defined(__bool_true_false_are_defined) -# define false 0 -# define true 1 -# endif -#endif +// kmod #define DISABLE 0 +// kmod #define ENABLE 1 + +# ifndef __cplusplus +# if !defined(__bool_true_false_are_defined) +# define false 0 +# define true 1 +# endif +# endif /** @} */ -#ifndef __ASSEMBLY__ +# ifndef __ASSEMBLY__ /** \name Optimization Control * @{ */ @@ -384,17 +371,17 @@ typedef struct * \def likely(exp) * \brief The expression \a exp is likely to be true */ -#if !defined(likely) || defined(__DOXYGEN__) -# define likely(exp) (exp) -#endif +# if !defined(likely) || defined(__DOXYGEN__) +# define likely(exp) (exp) +# endif /** * \def unlikely(exp) * \brief The expression \a exp is unlikely to be true */ -#if !defined(unlikely) || defined(__DOXYGEN__) -# define unlikely(exp) (exp) -#endif +# if !defined(unlikely) || defined(__DOXYGEN__) +# define unlikely(exp) (exp) +# endif /** * \def is_constant(exp) @@ -404,11 +391,11 @@ typedef struct * * \return true if \a exp is constant, false otherwise. */ -#if (defined __GNUC__) || (defined __CC_ARM) -# define is_constant(exp) __builtin_constant_p(exp) -#else -# define is_constant(exp) (0) -#endif +# if (defined __GNUC__) || (defined __CC_ARM) +# define is_constant(exp) __builtin_constant_p(exp) +# else +# define is_constant(exp) (0) +# endif /** @} */ @@ -422,7 +409,7 @@ typedef struct * * \return Read bits. */ -#define Rd_bits( value, mask) ((value) & (mask)) +# define Rd_bits(value, mask) ((value) & (mask)) /** \brief Writes the bits of a C lvalue specified by a given bit-mask. * @@ -432,8 +419,7 @@ typedef struct * * \return Resulting value with written bits. */ -#define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\ - ((bits ) & (mask))) +# define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) | ((bits) & (mask))) /** \brief Tests the bits of a value specified by a given bit-mask. * @@ -442,7 +428,7 @@ typedef struct * * \return \c 1 if at least one of the tested bits is set, else \c 0. */ -#define Tst_bits( value, mask) (Rd_bits(value, mask) != 0) +# define Tst_bits(value, mask) (Rd_bits(value, mask) != 0) /** \brief Clears the bits of a C lvalue specified by a given bit-mask. * @@ -451,7 +437,7 @@ typedef struct * * \return Resulting value with cleared bits. */ -#define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask)) +# define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask)) /** \brief Sets the bits of a C lvalue specified by a given bit-mask. * @@ -460,7 +446,7 @@ typedef struct * * \return Resulting value with set bits. */ -#define Set_bits(lvalue, mask) ((lvalue) |= (mask)) +# define Set_bits(lvalue, mask) ((lvalue) |= (mask)) /** \brief Toggles the bits of a C lvalue specified by a given bit-mask. * @@ -469,7 +455,7 @@ typedef struct * * \return Resulting value with toggled bits. */ -#define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask)) +# define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask)) /** \brief Reads the bit-field of a value specified by a given bit-mask. * @@ -478,7 +464,7 @@ typedef struct * * \return Read bit-field. */ -#define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask)) +# define Rd_bitfield(value, mask) (Rd_bits(value, mask) >> ctz(mask)) /** \brief Writes the bit-field of a C lvalue specified by a given bit-mask. * @@ -488,11 +474,10 @@ typedef struct * * \return Resulting value with written bit-field. */ -#define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask))) +# define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask))) /** @} */ - /** \name Zero-Bit Counting * * Under GCC, __builtin_clz and __builtin_ctz behave like macros when @@ -513,43 +498,11 @@ typedef struct * * \return The count of leading zero bits in \a u. */ -#if (defined __GNUC__) || (defined __CC_ARM) -# define clz(u) ((u) ? __builtin_clz(u) : 32) -#else -# define clz(u) (((u) == 0) ? 32 : \ - ((u) & (1ul << 31)) ? 0 : \ - ((u) & (1ul << 30)) ? 1 : \ - ((u) & (1ul << 29)) ? 2 : \ - ((u) & (1ul << 28)) ? 3 : \ - ((u) & (1ul << 27)) ? 4 : \ - ((u) & (1ul << 26)) ? 5 : \ - ((u) & (1ul << 25)) ? 6 : \ - ((u) & (1ul << 24)) ? 7 : \ - ((u) & (1ul << 23)) ? 8 : \ - ((u) & (1ul << 22)) ? 9 : \ - ((u) & (1ul << 21)) ? 10 : \ - ((u) & (1ul << 20)) ? 11 : \ - ((u) & (1ul << 19)) ? 12 : \ - ((u) & (1ul << 18)) ? 13 : \ - ((u) & (1ul << 17)) ? 14 : \ - ((u) & (1ul << 16)) ? 15 : \ - ((u) & (1ul << 15)) ? 16 : \ - ((u) & (1ul << 14)) ? 17 : \ - ((u) & (1ul << 13)) ? 18 : \ - ((u) & (1ul << 12)) ? 19 : \ - ((u) & (1ul << 11)) ? 20 : \ - ((u) & (1ul << 10)) ? 21 : \ - ((u) & (1ul << 9)) ? 22 : \ - ((u) & (1ul << 8)) ? 23 : \ - ((u) & (1ul << 7)) ? 24 : \ - ((u) & (1ul << 6)) ? 25 : \ - ((u) & (1ul << 5)) ? 26 : \ - ((u) & (1ul << 4)) ? 27 : \ - ((u) & (1ul << 3)) ? 28 : \ - ((u) & (1ul << 2)) ? 29 : \ - ((u) & (1ul << 1)) ? 30 : \ - 31) -#endif +# if (defined __GNUC__) || (defined __CC_ARM) +# define clz(u) ((u) ? __builtin_clz(u) : 32) +# else +# define clz(u) (((u) == 0) ? 32 : ((u) & (1ul << 31)) ? 0 : ((u) & (1ul << 30)) ? 1 : ((u) & (1ul << 29)) ? 2 : ((u) & (1ul << 28)) ? 3 : ((u) & (1ul << 27)) ? 4 : ((u) & (1ul << 26)) ? 5 : ((u) & (1ul << 25)) ? 6 : ((u) & (1ul << 24)) ? 7 : ((u) & (1ul << 23)) ? 8 : ((u) & (1ul << 22)) ? 9 : ((u) & (1ul << 21)) ? 10 : ((u) & (1ul << 20)) ? 11 : ((u) & (1ul << 19)) ? 12 : ((u) & (1ul << 18)) ? 13 : ((u) & (1ul << 17)) ? 14 : ((u) & (1ul << 16)) ? 15 : ((u) & (1ul << 15)) ? 16 : ((u) & (1ul << 14)) ? 17 : ((u) & (1ul << 13)) ? 18 : ((u) & (1ul << 12)) ? 19 : ((u) & (1ul << 11)) ? 20 : ((u) & (1ul << 10)) ? 21 : ((u) & (1ul << 9)) ? 22 : ((u) & (1ul << 8)) ? 23 : ((u) & (1ul << 7)) ? 24 : ((u) & (1ul << 6)) ? 25 : ((u) & (1ul << 5)) ? 26 : ((u) & (1ul << 4)) ? 27 : ((u) & (1ul << 3)) ? 28 : ((u) & (1ul << 2)) ? 29 : ((u) & (1ul << 1)) ? 30 : 31) +# endif /** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer. * @@ -557,47 +510,14 @@ typedef struct * * \return The count of trailing zero bits in \a u. */ -#if (defined __GNUC__) || (defined __CC_ARM) -# define ctz(u) ((u) ? __builtin_ctz(u) : 32) -#else -# define ctz(u) ((u) & (1ul << 0) ? 0 : \ - (u) & (1ul << 1) ? 1 : \ - (u) & (1ul << 2) ? 2 : \ - (u) & (1ul << 3) ? 3 : \ - (u) & (1ul << 4) ? 4 : \ - (u) & (1ul << 5) ? 5 : \ - (u) & (1ul << 6) ? 6 : \ - (u) & (1ul << 7) ? 7 : \ - (u) & (1ul << 8) ? 8 : \ - (u) & (1ul << 9) ? 9 : \ - (u) & (1ul << 10) ? 10 : \ - (u) & (1ul << 11) ? 11 : \ - (u) & (1ul << 12) ? 12 : \ - (u) & (1ul << 13) ? 13 : \ - (u) & (1ul << 14) ? 14 : \ - (u) & (1ul << 15) ? 15 : \ - (u) & (1ul << 16) ? 16 : \ - (u) & (1ul << 17) ? 17 : \ - (u) & (1ul << 18) ? 18 : \ - (u) & (1ul << 19) ? 19 : \ - (u) & (1ul << 20) ? 20 : \ - (u) & (1ul << 21) ? 21 : \ - (u) & (1ul << 22) ? 22 : \ - (u) & (1ul << 23) ? 23 : \ - (u) & (1ul << 24) ? 24 : \ - (u) & (1ul << 25) ? 25 : \ - (u) & (1ul << 26) ? 26 : \ - (u) & (1ul << 27) ? 27 : \ - (u) & (1ul << 28) ? 28 : \ - (u) & (1ul << 29) ? 29 : \ - (u) & (1ul << 30) ? 30 : \ - (u) & (1ul << 31) ? 31 : \ - 32) -#endif +# if (defined __GNUC__) || (defined __CC_ARM) +# define ctz(u) ((u) ? __builtin_ctz(u) : 32) +# else +# define ctz(u) ((u) & (1ul << 0) ? 0 : (u) & (1ul << 1) ? 1 : (u) & (1ul << 2) ? 2 : (u) & (1ul << 3) ? 3 : (u) & (1ul << 4) ? 4 : (u) & (1ul << 5) ? 5 : (u) & (1ul << 6) ? 6 : (u) & (1ul << 7) ? 7 : (u) & (1ul << 8) ? 8 : (u) & (1ul << 9) ? 9 : (u) & (1ul << 10) ? 10 : (u) & (1ul << 11) ? 11 : (u) & (1ul << 12) ? 12 : (u) & (1ul << 13) ? 13 : (u) & (1ul << 14) ? 14 : (u) & (1ul << 15) ? 15 : (u) & (1ul << 16) ? 16 : (u) & (1ul << 17) ? 17 : (u) & (1ul << 18) ? 18 : (u) & (1ul << 19) ? 19 : (u) & (1ul << 20) ? 20 : (u) & (1ul << 21) ? 21 : (u) & (1ul << 22) ? 22 : (u) & (1ul << 23) ? 23 : (u) & (1ul << 24) ? 24 : (u) & (1ul << 25) ? 25 : (u) & (1ul << 26) ? 26 : (u) & (1ul << 27) ? 27 : (u) & (1ul << 28) ? 28 : (u) & (1ul << 29) ? 29 : (u) & (1ul << 30) ? 30 : (u) & (1ul << 31) ? 31 : 32) +# endif /** @} */ - /** \name Bit Reversing * @{ */ @@ -607,7 +527,7 @@ typedef struct * * \return Value resulting from \a u8 with reversed bits. */ -#define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24)) +# define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24)) /** \brief Reverses the bits of \a u16. * @@ -615,7 +535,7 @@ typedef struct * * \return Value resulting from \a u16 with reversed bits. */ -#define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16)) +# define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16)) /** \brief Reverses the bits of \a u32. * @@ -623,7 +543,7 @@ typedef struct * * \return Value resulting from \a u32 with reversed bits. */ -#define bit_reverse32(u32) __RBIT(u32) +# define bit_reverse32(u32) __RBIT(u32) /** \brief Reverses the bits of \a u64. * @@ -631,12 +551,10 @@ typedef struct * * \return Value resulting from \a u64 with reversed bits. */ -#define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) |\ - ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32))) +# define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) | ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32))) /** @} */ - /** \name Alignment * @{ */ @@ -647,7 +565,7 @@ typedef struct * * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0. */ -#define Test_align(val, n) (!Tst_bits( val, (n) - 1 ) ) +# define Test_align(val, n) (!Tst_bits(val, (n)-1)) /** \brief Gets alignment of the number \a val with respect to the \a n boundary. * @@ -656,7 +574,7 @@ typedef struct * * \return Alignment of the number \a val with respect to the \a n boundary. */ -#define Get_align(val, n) ( Rd_bits( val, (n) - 1 ) ) +# define Get_align(val, n) (Rd_bits(val, (n)-1)) /** \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary. * @@ -666,7 +584,7 @@ typedef struct * * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary. */ -#define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) ) +# define Set_align(lval, n, alg) (Wr_bits(lval, (n)-1, alg)) /** \brief Aligns the number \a val with the upper \a n boundary. * @@ -675,7 +593,7 @@ typedef struct * * \return Value resulting from the number \a val aligned with the upper \a n boundary. */ -#define Align_up( val, n) (((val) + ((n) - 1)) & ~((n) - 1)) +# define Align_up(val, n) (((val) + ((n)-1)) & ~((n)-1)) /** \brief Aligns the number \a val with the lower \a n boundary. * @@ -684,11 +602,10 @@ typedef struct * * \return Value resulting from the number \a val aligned with the lower \a n boundary. */ -#define Align_down(val, n) ( (val) & ~((n) - 1)) +# define Align_down(val, n) ((val) & ~((n)-1)) /** @} */ - /** \name Mathematics * * The same considerations as for clz and ctz apply here but GCC does not @@ -710,9 +627,9 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Abs(a) (((a) < 0 ) ? -(a) : (a)) +# define Abs(a) (((a) < 0) ? -(a) : (a)) -#ifndef __cplusplus +# ifndef __cplusplus /** \brief Takes the minimal value of \a a and \a b. * * \param[in] a Input value. @@ -722,7 +639,7 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Min(a, b) (((a) < (b)) ? (a) : (b)) +# define Min(a, b) (((a) < (b)) ? (a) : (b)) /** \brief Takes the maximal value of \a a and \a b. * @@ -733,7 +650,7 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Max(a, b) (((a) > (b)) ? (a) : (b)) +# define Max(a, b) (((a) > (b)) ? (a) : (b)) /** \brief Takes the minimal value of \a a and \a b. * @@ -744,7 +661,7 @@ typedef struct * * \note More optimized if only used with values unknown at compile time. */ -#define min(a, b) Min(a, b) +# define min(a, b) Min(a, b) /** \brief Takes the maximal value of \a a and \a b. * @@ -755,12 +672,11 @@ typedef struct * * \note More optimized if only used with values unknown at compile time. */ -#define max(a, b) Max(a, b) -#endif +# define max(a, b) Max(a, b) +# endif /** @} */ - /** \brief Calls the routine at address \a addr. * * It generates a long call opcode. @@ -772,38 +688,36 @@ typedef struct * * \note It may be used as a long jump opcode in some special cases. */ -#define Long_call(addr) ((*(void (*)(void))(addr))()) - +# define Long_call(addr) ((*(void (*)(void))(addr))()) /** \name MCU Endianism Handling * ARM is MCU little endian. * * @{ */ -#define BE16(x) swap16(x) -#define LE16(x) (x) - -#define le16_to_cpu(x) (x) -#define cpu_to_le16(x) (x) -#define LE16_TO_CPU(x) (x) -#define CPU_TO_LE16(x) (x) - -#define be16_to_cpu(x) swap16(x) -#define cpu_to_be16(x) swap16(x) -#define BE16_TO_CPU(x) swap16(x) -#define CPU_TO_BE16(x) swap16(x) - -#define le32_to_cpu(x) (x) -#define cpu_to_le32(x) (x) -#define LE32_TO_CPU(x) (x) -#define CPU_TO_LE32(x) (x) - -#define be32_to_cpu(x) swap32(x) -#define cpu_to_be32(x) swap32(x) -#define BE32_TO_CPU(x) swap32(x) -#define CPU_TO_BE32(x) swap32(x) +# define BE16(x) swap16(x) +# define LE16(x) (x) + +# define le16_to_cpu(x) (x) +# define cpu_to_le16(x) (x) +# define LE16_TO_CPU(x) (x) +# define CPU_TO_LE16(x) (x) + +# define be16_to_cpu(x) swap16(x) +# define cpu_to_be16(x) swap16(x) +# define BE16_TO_CPU(x) swap16(x) +# define CPU_TO_BE16(x) swap16(x) + +# define le32_to_cpu(x) (x) +# define cpu_to_le32(x) (x) +# define LE32_TO_CPU(x) (x) +# define CPU_TO_LE32(x) (x) + +# define be32_to_cpu(x) swap32(x) +# define cpu_to_be32(x) swap32(x) +# define BE32_TO_CPU(x) swap32(x) +# define CPU_TO_BE32(x) swap32(x) /** @} */ - /** \name Endianism Conversion * * The same considerations as for clz and ctz apply here but GCC's @@ -824,8 +738,7 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\ - ((uint16_t)(u16) << 8))) +# define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) | ((uint16_t)(u16) << 8))) /** \brief Toggles the endianism of \a u32 (by swapping its bytes). * @@ -835,8 +748,7 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\ - ((uint32_t)Swap16((uint32_t)(u32)) << 16))) +# define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) | ((uint32_t)Swap16((uint32_t)(u32)) << 16))) /** \brief Toggles the endianism of \a u64 (by swapping its bytes). * @@ -846,8 +758,7 @@ typedef struct * * \note More optimized if only used with values known at compile time. */ -#define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) |\ - ((uint64_t)Swap32((uint64_t)(u64)) << 32))) +# define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) | ((uint64_t)Swap32((uint64_t)(u64)) << 32))) /** \brief Toggles the endianism of \a u16 (by swapping its bytes). * @@ -857,7 +768,7 @@ typedef struct * * \note More optimized if only used with values unknown at compile time. */ -#define swap16(u16) Swap16(u16) +# define swap16(u16) Swap16(u16) /** \brief Toggles the endianism of \a u32 (by swapping its bytes). * @@ -867,11 +778,11 @@ typedef struct * * \note More optimized if only used with values unknown at compile time. */ -#if (defined __GNUC__) -# define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32))) -#else -# define swap32(u32) Swap32(u32) -#endif +# if (defined __GNUC__) +# define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32))) +# else +# define swap32(u32) Swap32(u32) +# endif /** \brief Toggles the endianism of \a u64 (by swapping its bytes). * @@ -881,30 +792,28 @@ typedef struct * * \note More optimized if only used with values unknown at compile time. */ -#if (defined __GNUC__) -# define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64))) -#else -# define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) |\ - ((uint64_t)swap32((uint64_t)(u64)) << 32))) -#endif +# if (defined __GNUC__) +# define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64))) +# else +# define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) | ((uint64_t)swap32((uint64_t)(u64)) << 32))) +# endif /** @} */ - /** \name Target Abstraction * * @{ */ -#define _GLOBEXT_ extern /**< extern storage-class specifier. */ -#define _CONST_TYPE_ const /**< const type qualifier. */ -#define _MEM_TYPE_SLOW_ /**< Slow memory type. */ -#define _MEM_TYPE_MEDFAST_ /**< Fairly fast memory type. */ -#define _MEM_TYPE_FAST_ /**< Fast memory type. */ +# define _GLOBEXT_ extern /**< extern storage-class specifier. */ +# define _CONST_TYPE_ const /**< const type qualifier. */ +# define _MEM_TYPE_SLOW_ /**< Slow memory type. */ +# define _MEM_TYPE_MEDFAST_ /**< Fairly fast memory type. */ +# define _MEM_TYPE_FAST_ /**< Fast memory type. */ -#define memcmp_ram2ram memcmp /**< Target-specific memcmp of RAM to RAM. */ -#define memcmp_code2ram memcmp /**< Target-specific memcmp of RAM to NVRAM. */ -#define memcpy_ram2ram memcpy /**< Target-specific memcpy from RAM to RAM. */ -#define memcpy_code2ram memcpy /**< Target-specific memcpy from NVRAM to RAM. */ +# define memcmp_ram2ram memcmp /**< Target-specific memcmp of RAM to RAM. */ +# define memcmp_code2ram memcmp /**< Target-specific memcmp of RAM to NVRAM. */ +# define memcpy_ram2ram memcpy /**< Target-specific memcpy from RAM to RAM. */ +# define memcpy_code2ram memcpy /**< Target-specific memcpy from NVRAM to RAM. */ /** @} */ @@ -917,159 +826,162 @@ typedef struct * * \return (\a a / \a b) rounded up to the nearest integer. */ -#define div_ceil(a, b) (((a) + (b) - 1) / (b)) +# define div_ceil(a, b) (((a) + (b)-1) / (b)) -#endif /* #ifndef __ASSEMBLY__ */ -#ifdef __ICCARM__ +# endif /* #ifndef __ASSEMBLY__ */ +# ifdef __ICCARM__ /** \name Compiler Keywords * * Port of some keywords from GCC to IAR Embedded Workbench. * * @{ */ -#define __asm__ asm -#define __inline__ inline -#define __volatile__ +# define __asm__ asm +# define __inline__ inline +# define __volatile__ /** @} */ -#endif +# endif -#define FUNC_PTR void * +# define FUNC_PTR void * /** * \def unused * \brief Marking \a v as a unused parameter or value. */ -#define unused(v) do { (void)(v); } while(0) +# define unused(v) \ + do { \ + (void)(v); \ + } while (0) /* Define RAMFUNC attribute */ -#if defined ( __CC_ARM ) /* Keil uVision 4 */ -# define RAMFUNC __attribute__ ((section(".ramfunc"))) -#elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ -# define RAMFUNC __ramfunc -#elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ -# define RAMFUNC __attribute__ ((section(".ramfunc"))) -#endif +# if defined(__CC_ARM) /* Keil uVision 4 */ +# define RAMFUNC __attribute__((section(".ramfunc"))) +# elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */ +# define RAMFUNC __ramfunc +# elif defined(__GNUC__) /* GCC CS3 2009q3-68 */ +# define RAMFUNC __attribute__((section(".ramfunc"))) +# endif /* Define OPTIMIZE_HIGH attribute */ -#if defined ( __CC_ARM ) /* Keil uVision 4 */ -# define OPTIMIZE_HIGH _Pragma("O3") -#elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ -# define OPTIMIZE_HIGH _Pragma("optimize=high") -#elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ -# define OPTIMIZE_HIGH __attribute__((optimize("s"))) -#endif -//kmod #define PASS 0 -//kmod #define FAIL 1 -//kmod #define LOW 0 -//kmod #define HIGH 1 - -typedef int8_t S8 ; //!< 8-bit signed integer. -typedef uint8_t U8 ; //!< 8-bit unsigned integer. -typedef int16_t S16; //!< 16-bit signed integer. -typedef uint16_t U16; //!< 16-bit unsigned integer. -typedef int32_t S32; //!< 32-bit signed integer. -typedef uint32_t U32; //!< 32-bit unsigned integer. -typedef int64_t S64; //!< 64-bit signed integer. -typedef uint64_t U64; //!< 64-bit unsigned integer. -typedef float F32; //!< 32-bit floating-point number. -typedef double F64; //!< 64-bit floating-point number. - -#define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16. -#define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16. - -#define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32. -#define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32. -#define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32. -#define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32. -#define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32. -#define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32. -#define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32. -#define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32. -#define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32. -#define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32. - -#define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64. -#define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64. -#define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64. -#define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64. -#define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64. -#define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64. -#define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64. -#define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64. -#define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64. -#define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64. -#define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64. -#define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64. -#define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64. -#define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64. -#define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64. -#define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64. -#define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64. -#define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64. -#define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64. -#define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64. -#define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64. -#define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64. -#define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64. -#define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64. -#define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64. -#define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64. - -#define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32. -#define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32. -#define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32. -#define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32. -#define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32. -#define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32. -#define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32. -#define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32. - -#if defined(__ICCARM__) -#define SHORTENUM __packed -#elif defined(__GNUC__) -#define SHORTENUM __attribute__((packed)) -#endif +# if defined(__CC_ARM) /* Keil uVision 4 */ +# define OPTIMIZE_HIGH _Pragma("O3") +# elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */ +# define OPTIMIZE_HIGH _Pragma("optimize=high") +# elif defined(__GNUC__) /* GCC CS3 2009q3-68 */ +# define OPTIMIZE_HIGH __attribute__((optimize("s"))) +# endif +// kmod #define PASS 0 +// kmod #define FAIL 1 +// kmod #define LOW 0 +// kmod #define HIGH 1 + +typedef int8_t S8; //!< 8-bit signed integer. +typedef uint8_t U8; //!< 8-bit unsigned integer. +typedef int16_t S16; //!< 16-bit signed integer. +typedef uint16_t U16; //!< 16-bit unsigned integer. +typedef int32_t S32; //!< 32-bit signed integer. +typedef uint32_t U32; //!< 32-bit unsigned integer. +typedef int64_t S64; //!< 64-bit signed integer. +typedef uint64_t U64; //!< 64-bit unsigned integer. +typedef float F32; //!< 32-bit floating-point number. +typedef double F64; //!< 64-bit floating-point number. + +# define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16. +# define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16. + +# define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32. +# define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32. +# define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32. +# define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32. +# define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32. +# define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32. +# define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32. +# define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32. +# define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32. +# define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32. + +# define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64. +# define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64. +# define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64. +# define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64. +# define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64. +# define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64. +# define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64. +# define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64. +# define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64. +# define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64. +# define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64. +# define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64. +# define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64. +# define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64. +# define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64. +# define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64. +# define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64. +# define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64. +# define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64. +# define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64. +# define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64. +# define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64. +# define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64. +# define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64. +# define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64. +# define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64. + +# define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32. +# define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32. +# define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32. +# define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32. +# define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32. +# define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32. +# define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32. +# define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32. + +# if defined(__ICCARM__) +# define SHORTENUM __packed +# elif defined(__GNUC__) +# define SHORTENUM __attribute__((packed)) +# endif /* No operation */ -#if defined(__ICCARM__) -#define nop() __no_operation() -#elif defined(__GNUC__) -#define nop() (__NOP()) -#endif - -#define FLASH_DECLARE(x) const x -#define FLASH_EXTERN(x) extern const x -#define PGM_READ_BYTE(x) *(x) -#define PGM_READ_WORD(x) *(x) -#define MEMCPY_ENDIAN memcpy -#define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len)) +# if defined(__ICCARM__) +# define nop() __no_operation() +# elif defined(__GNUC__) +# define nop() (__NOP()) +# endif + +# define FLASH_DECLARE(x) const x +# define FLASH_EXTERN(x) extern const x +# define PGM_READ_BYTE(x) *(x) +# define PGM_READ_WORD(x) *(x) +# define MEMCPY_ENDIAN memcpy +# define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len)) /*Defines the Flash Storage for the request and response of MAC*/ -#define CMD_ID_OCTET (0) +# define CMD_ID_OCTET (0) /* Converting of values from CPU endian to little endian. */ -#define CPU_ENDIAN_TO_LE16(x) (x) -#define CPU_ENDIAN_TO_LE32(x) (x) -#define CPU_ENDIAN_TO_LE64(x) (x) +# define CPU_ENDIAN_TO_LE16(x) (x) +# define CPU_ENDIAN_TO_LE32(x) (x) +# define CPU_ENDIAN_TO_LE64(x) (x) /* Converting of values from little endian to CPU endian. */ -#define LE16_TO_CPU_ENDIAN(x) (x) -#define LE32_TO_CPU_ENDIAN(x) (x) -#define LE64_TO_CPU_ENDIAN(x) (x) +# define LE16_TO_CPU_ENDIAN(x) (x) +# define LE32_TO_CPU_ENDIAN(x) (x) +# define LE64_TO_CPU_ENDIAN(x) (x) /* Converting of constants from little endian to CPU endian. */ -#define CLE16_TO_CPU_ENDIAN(x) (x) -#define CLE32_TO_CPU_ENDIAN(x) (x) -#define CLE64_TO_CPU_ENDIAN(x) (x) +# define CLE16_TO_CPU_ENDIAN(x) (x) +# define CLE32_TO_CPU_ENDIAN(x) (x) +# define CLE64_TO_CPU_ENDIAN(x) (x) /* Converting of constants from CPU endian to little endian. */ -#define CCPU_ENDIAN_TO_LE16(x) (x) -#define CCPU_ENDIAN_TO_LE32(x) (x) -#define CCPU_ENDIAN_TO_LE64(x) (x) +# define CCPU_ENDIAN_TO_LE16(x) (x) +# define CCPU_ENDIAN_TO_LE32(x) (x) +# define CCPU_ENDIAN_TO_LE64(x) (x) -#define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src)) -#define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src)) +# define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src)) +# define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src)) /** * @brief Converts a 64-Bit value into a 8 Byte array @@ -1078,14 +990,12 @@ typedef double F64; //!< 64-bit floating-point number. * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value * @ingroup apiPalApi */ -static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data) -{ +static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data) { uint8_t index = 0; - while (index < 8) - { + while (index < 8) { data[index++] = value & 0xFF; - value = value >> 8; + value = value >> 8; } } @@ -1096,22 +1006,19 @@ static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data) * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value * @ingroup apiPalApi */ -static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data) -{ +static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data) { data[0] = value & 0xFF; data[1] = (value >> 8) & 0xFF; } /* Converts a 16-Bit value into a 2 Byte array */ -static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data) -{ +static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data) { data[0] = value & 0xFF; data[1] = (value >> 8) & 0xFF; } /* Converts a 16-Bit value into a 2 Byte array */ -static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data) -{ +static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data) { data[0] = value & 0xFF; data[1] = (value >> 8) & 0xFF; } @@ -1124,24 +1031,18 @@ static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data) * @return 16-Bit value * @ingroup apiPalApi */ -static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data) -{ - return (data[0] | ((uint16_t)data[1] << 8)); -} +static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data) { return (data[0] | ((uint16_t)data[1] << 8)); } /* Converts a 4 Byte array into a 32-Bit value */ -static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data) -{ - union - { +static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data) { + union { uint32_t u32; - uint8_t u8[4]; + uint8_t u8[4]; } long_addr; uint8_t index; - for (index = 0; index < 4; index++) - { + for (index = 0; index < 4; index++) { long_addr.u8[index] = *data++; } @@ -1156,18 +1057,15 @@ static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data) * @return 64-Bit value * @ingroup apiPalApi */ -static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data) -{ - union - { +static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data) { + union { uint64_t u64; - uint8_t u8[8]; + uint8_t u8[8]; } long_addr; uint8_t index; - for (index = 0; index < 8; index++) - { + for (index = 0; index < 8; index++) { long_addr.u8[index] = *data++; } diff --git a/tmk_core/protocol/arm_atsam/usb/conf_usb.h b/tmk_core/protocol/arm_atsam/usb/conf_usb.h index c91caffe0..f23c2a80d 100644 --- a/tmk_core/protocol/arm_atsam/usb/conf_usb.h +++ b/tmk_core/protocol/arm_atsam/usb/conf_usb.h @@ -50,29 +50,29 @@ #include "compiler.h" #include "udi_device_conf.h" -#define UDI_CDC_DEFAULT_RATE 115200 -#define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1 -#define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE -#define UDI_CDC_DEFAULT_DATABITS 8 +#define UDI_CDC_DEFAULT_RATE 115200 +#define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1 +#define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE +#define UDI_CDC_DEFAULT_DATABITS 8 //! Device definition (mandatory) -#define USB_DEVICE_VENDOR_ID VENDOR_ID -#define USB_DEVICE_PRODUCT_ID PRODUCT_ID -#define USB_DEVICE_VERSION DEVICE_VER -#define USB_DEVICE_POWER 500 // Consumption on Vbus line (mA) -#define USB_DEVICE_ATTR (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED) +#define USB_DEVICE_VENDOR_ID VENDOR_ID +#define USB_DEVICE_PRODUCT_ID PRODUCT_ID +#define USB_DEVICE_VERSION DEVICE_VER +#define USB_DEVICE_POWER 500 // Consumption on Vbus line (mA) +#define USB_DEVICE_ATTR (USB_CONFIG_ATTR_REMOTE_WAKEUP | USB_CONFIG_ATTR_BUS_POWERED) // (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED) // (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_SELF_POWERED) // (USB_CONFIG_ATTR_SELF_POWERED) // (USB_CONFIG_ATTR_BUS_POWERED) //! USB Device string definitions (Optional) -#define USB_DEVICE_MANUFACTURE_NAME MANUFACTURER -#define USB_DEVICE_PRODUCT_NAME PRODUCT -#define USB_DEVICE_SERIAL_NAME SERIAL_NUM +#define USB_DEVICE_MANUFACTURE_NAME MANUFACTURER +#define USB_DEVICE_PRODUCT_NAME PRODUCT +#define USB_DEVICE_SERIAL_NAME SERIAL_NUM -//Comment out USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL to prevent ROM lookup of factory programmed serial number -#define USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL +// Comment out USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL to prevent ROM lookup of factory programmed serial number +#define USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL /** * Device speeds support @@ -82,9 +82,9 @@ //#define USB_DEVICE_LOW_SPEED //! To authorize the High speed -#if (UC3A3||UC3A4) +#if (UC3A3 || UC3A4) //#define USB_DEVICE_HS_SUPPORT -#elif (SAM3XA||SAM3U) +#elif (SAM3XA || SAM3U) //#define USB_DEVICE_HS_SUPPORT #endif //@} @@ -93,13 +93,13 @@ * USB Device Callbacks definitions (Optional) * @{ */ -#define UDC_VBUS_EVENT(b_vbus_high) -#define UDC_SOF_EVENT() main_sof_action() -#define UDC_SUSPEND_EVENT() main_suspend_action() -#define UDC_RESUME_EVENT() main_resume_action() +#define UDC_VBUS_EVENT(b_vbus_high) +#define UDC_SOF_EVENT() main_sof_action() +#define UDC_SUSPEND_EVENT() main_suspend_action() +#define UDC_RESUME_EVENT() main_resume_action() //! Mandatory when USB_DEVICE_ATTR authorizes remote wakeup feature -#define UDC_REMOTEWAKEUP_ENABLE() main_remotewakeup_enable() -#define UDC_REMOTEWAKEUP_DISABLE() main_remotewakeup_disable() +#define UDC_REMOTEWAKEUP_ENABLE() main_remotewakeup_enable() +#define UDC_REMOTEWAKEUP_DISABLE() main_remotewakeup_disable() //! When a extra string descriptor must be supported //! other than manufacturer, product and serial string // #define UDC_GET_EXTRA_STRING() @@ -107,7 +107,6 @@ //@} - /** * USB Interface Configuration * @{ @@ -118,42 +117,40 @@ */ //! Interface callback definition #ifdef KBD -#define UDI_HID_KBD_ENABLE_EXT() main_kbd_enable() -#define UDI_HID_KBD_DISABLE_EXT() main_kbd_disable() +# define UDI_HID_KBD_ENABLE_EXT() main_kbd_enable() +# define UDI_HID_KBD_DISABLE_EXT() main_kbd_disable() //#define UDI_HID_KBD_CHANGE_LED(value) ui_kbd_led(value) #endif #ifdef NKRO -#define UDI_HID_NKRO_ENABLE_EXT() main_nkro_enable() -#define UDI_HID_NKRO_DISABLE_EXT() main_nkro_disable() +# define UDI_HID_NKRO_ENABLE_EXT() main_nkro_enable() +# define UDI_HID_NKRO_DISABLE_EXT() main_nkro_disable() //#define UDI_HID_NKRO_CHANGE_LED(value) ui_kbd_led(value) #endif #ifdef EXK -#define UDI_HID_EXK_ENABLE_EXT() main_exk_enable() -#define UDI_HID_EXK_DISABLE_EXT() main_exk_disable() +# define UDI_HID_EXK_ENABLE_EXT() main_exk_enable() +# define UDI_HID_EXK_DISABLE_EXT() main_exk_disable() #endif #ifdef CON -#define UDI_HID_CON_ENABLE_EXT() main_con_enable() -#define UDI_HID_CON_DISABLE_EXT() main_con_disable() +# define UDI_HID_CON_ENABLE_EXT() main_con_enable() +# define UDI_HID_CON_DISABLE_EXT() main_con_disable() #endif #ifdef MOU -#define UDI_HID_MOU_ENABLE_EXT() main_mou_enable() -#define UDI_HID_MOU_DISABLE_EXT() main_mou_disable() +# define UDI_HID_MOU_ENABLE_EXT() main_mou_enable() +# define UDI_HID_MOU_DISABLE_EXT() main_mou_disable() #endif #ifdef RAW -#define UDI_HID_RAW_ENABLE_EXT() main_raw_enable() -#define UDI_HID_RAW_DISABLE_EXT() main_raw_disable() +# define UDI_HID_RAW_ENABLE_EXT() main_raw_enable() +# define UDI_HID_RAW_DISABLE_EXT() main_raw_disable() #endif - //@} //@} - /** * USB Device Driver Configuration * @{ @@ -165,4 +162,4 @@ #include "usb_main.h" #include "ui.h" -#endif // _CONF_USB_H_ +#endif // _CONF_USB_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/main_usb.c b/tmk_core/protocol/arm_atsam/usb/main_usb.c index 0f676ab63..82ab123fd 100644 --- a/tmk_core/protocol/arm_atsam/usb/main_usb.c +++ b/tmk_core/protocol/arm_atsam/usb/main_usb.c @@ -21,112 +21,72 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. uint8_t keyboard_protocol = 1; -void main_suspend_action(void) -{ - ui_powerdown(); -} +void main_suspend_action(void) { ui_powerdown(); } -void main_resume_action(void) -{ - ui_wakeup(); -} +void main_resume_action(void) { ui_wakeup(); } -void main_sof_action(void) -{ - ui_process(udd_get_frame_number()); -} +void main_sof_action(void) { ui_process(udd_get_frame_number()); } -void main_remotewakeup_enable(void) -{ - ui_wakeup_enable(); -} +void main_remotewakeup_enable(void) { ui_wakeup_enable(); } -void main_remotewakeup_disable(void) -{ - ui_wakeup_disable(); -} +void main_remotewakeup_disable(void) { ui_wakeup_disable(); } #ifdef KBD volatile bool main_b_kbd_enable = false; -bool main_kbd_enable(void) -{ +bool main_kbd_enable(void) { main_b_kbd_enable = true; return true; } -void main_kbd_disable(void) -{ - main_b_kbd_enable = false; -} +void main_kbd_disable(void) { main_b_kbd_enable = false; } #endif #ifdef NKRO volatile bool main_b_nkro_enable = false; -bool main_nkro_enable(void) -{ +bool main_nkro_enable(void) { main_b_nkro_enable = true; return true; } -void main_nkro_disable(void) -{ - main_b_nkro_enable = false; -} +void main_nkro_disable(void) { main_b_nkro_enable = false; } #endif #ifdef EXK volatile bool main_b_exk_enable = false; -bool main_exk_enable(void) -{ +bool main_exk_enable(void) { main_b_exk_enable = true; return true; } -void main_exk_disable(void) -{ - main_b_exk_enable = false; -} +void main_exk_disable(void) { main_b_exk_enable = false; } #endif #ifdef CON volatile bool main_b_con_enable = false; -bool main_con_enable(void) -{ +bool main_con_enable(void) { main_b_con_enable = true; return true; } -void main_con_disable(void) -{ - main_b_con_enable = false; -} +void main_con_disable(void) { main_b_con_enable = false; } #endif #ifdef MOU volatile bool main_b_mou_enable = false; -bool main_mou_enable(void) -{ +bool main_mou_enable(void) { main_b_mou_enable = true; return true; } -void main_mou_disable(void) -{ - main_b_mou_enable = false; -} +void main_mou_disable(void) { main_b_mou_enable = false; } #endif #ifdef RAW volatile bool main_b_raw_enable = false; -bool main_raw_enable(void) -{ +bool main_raw_enable(void) { main_b_raw_enable = true; return true; } -void main_raw_disable(void) -{ - main_b_raw_enable = false; -} +void main_raw_disable(void) { main_b_raw_enable = false; } #endif - diff --git a/tmk_core/protocol/arm_atsam/usb/status_codes.h b/tmk_core/protocol/arm_atsam/usb/status_codes.h index f56d2faed..a7a1230be 100644 --- a/tmk_core/protocol/arm_atsam/usb/status_codes.h +++ b/tmk_core/protocol/arm_atsam/usb/status_codes.h @@ -61,18 +61,18 @@ */ /** Mask to retrieve the error category of a status code. */ -#define STATUS_CATEGORY_MASK 0xF0 +#define STATUS_CATEGORY_MASK 0xF0 /** Mask to retrieve the error code within the category of a status code. */ -#define STATUS_ERROR_MASK 0x0F +#define STATUS_ERROR_MASK 0x0F /** Status code error categories. */ enum status_categories { - STATUS_CATEGORY_OK = 0x00, - STATUS_CATEGORY_COMMON = 0x10, - STATUS_CATEGORY_ANALOG = 0x30, - STATUS_CATEGORY_COM = 0x40, - STATUS_CATEGORY_IO = 0x50, + STATUS_CATEGORY_OK = 0x00, + STATUS_CATEGORY_COMMON = 0x10, + STATUS_CATEGORY_ANALOG = 0x30, + STATUS_CATEGORY_COM = 0x40, + STATUS_CATEGORY_IO = 0x50, }; /** @@ -85,37 +85,37 @@ enum status_categories { * at the same time. */ enum status_code { - STATUS_OK = STATUS_CATEGORY_OK | 0x00, - STATUS_VALID_DATA = STATUS_CATEGORY_OK | 0x01, - STATUS_NO_CHANGE = STATUS_CATEGORY_OK | 0x02, - STATUS_ABORTED = STATUS_CATEGORY_OK | 0x04, - STATUS_BUSY = STATUS_CATEGORY_OK | 0x05, - STATUS_SUSPEND = STATUS_CATEGORY_OK | 0x06, - - STATUS_ERR_IO = STATUS_CATEGORY_COMMON | 0x00, - STATUS_ERR_REQ_FLUSHED = STATUS_CATEGORY_COMMON | 0x01, - STATUS_ERR_TIMEOUT = STATUS_CATEGORY_COMMON | 0x02, - STATUS_ERR_BAD_DATA = STATUS_CATEGORY_COMMON | 0x03, - STATUS_ERR_NOT_FOUND = STATUS_CATEGORY_COMMON | 0x04, - STATUS_ERR_UNSUPPORTED_DEV = STATUS_CATEGORY_COMMON | 0x05, - STATUS_ERR_NO_MEMORY = STATUS_CATEGORY_COMMON | 0x06, - STATUS_ERR_INVALID_ARG = STATUS_CATEGORY_COMMON | 0x07, - STATUS_ERR_BAD_ADDRESS = STATUS_CATEGORY_COMMON | 0x08, - STATUS_ERR_BAD_FORMAT = STATUS_CATEGORY_COMMON | 0x0A, - STATUS_ERR_BAD_FRQ = STATUS_CATEGORY_COMMON | 0x0B, - STATUS_ERR_DENIED = STATUS_CATEGORY_COMMON | 0x0c, - STATUS_ERR_ALREADY_INITIALIZED = STATUS_CATEGORY_COMMON | 0x0d, - STATUS_ERR_OVERFLOW = STATUS_CATEGORY_COMMON | 0x0e, - STATUS_ERR_NOT_INITIALIZED = STATUS_CATEGORY_COMMON | 0x0f, + STATUS_OK = STATUS_CATEGORY_OK | 0x00, + STATUS_VALID_DATA = STATUS_CATEGORY_OK | 0x01, + STATUS_NO_CHANGE = STATUS_CATEGORY_OK | 0x02, + STATUS_ABORTED = STATUS_CATEGORY_OK | 0x04, + STATUS_BUSY = STATUS_CATEGORY_OK | 0x05, + STATUS_SUSPEND = STATUS_CATEGORY_OK | 0x06, + + STATUS_ERR_IO = STATUS_CATEGORY_COMMON | 0x00, + STATUS_ERR_REQ_FLUSHED = STATUS_CATEGORY_COMMON | 0x01, + STATUS_ERR_TIMEOUT = STATUS_CATEGORY_COMMON | 0x02, + STATUS_ERR_BAD_DATA = STATUS_CATEGORY_COMMON | 0x03, + STATUS_ERR_NOT_FOUND = STATUS_CATEGORY_COMMON | 0x04, + STATUS_ERR_UNSUPPORTED_DEV = STATUS_CATEGORY_COMMON | 0x05, + STATUS_ERR_NO_MEMORY = STATUS_CATEGORY_COMMON | 0x06, + STATUS_ERR_INVALID_ARG = STATUS_CATEGORY_COMMON | 0x07, + STATUS_ERR_BAD_ADDRESS = STATUS_CATEGORY_COMMON | 0x08, + STATUS_ERR_BAD_FORMAT = STATUS_CATEGORY_COMMON | 0x0A, + STATUS_ERR_BAD_FRQ = STATUS_CATEGORY_COMMON | 0x0B, + STATUS_ERR_DENIED = STATUS_CATEGORY_COMMON | 0x0c, + STATUS_ERR_ALREADY_INITIALIZED = STATUS_CATEGORY_COMMON | 0x0d, + STATUS_ERR_OVERFLOW = STATUS_CATEGORY_COMMON | 0x0e, + STATUS_ERR_NOT_INITIALIZED = STATUS_CATEGORY_COMMON | 0x0f, STATUS_ERR_SAMPLERATE_UNAVAILABLE = STATUS_CATEGORY_ANALOG | 0x00, STATUS_ERR_RESOLUTION_UNAVAILABLE = STATUS_CATEGORY_ANALOG | 0x01, - STATUS_ERR_BAUDRATE_UNAVAILABLE = STATUS_CATEGORY_COM | 0x00, - STATUS_ERR_PACKET_COLLISION = STATUS_CATEGORY_COM | 0x01, - STATUS_ERR_PROTOCOL = STATUS_CATEGORY_COM | 0x02, + STATUS_ERR_BAUDRATE_UNAVAILABLE = STATUS_CATEGORY_COM | 0x00, + STATUS_ERR_PACKET_COLLISION = STATUS_CATEGORY_COM | 0x01, + STATUS_ERR_PROTOCOL = STATUS_CATEGORY_COM | 0x02, - STATUS_ERR_PIN_MUX_INVALID = STATUS_CATEGORY_IO | 0x00, + STATUS_ERR_PIN_MUX_INVALID = STATUS_CATEGORY_IO | 0x00, }; typedef enum status_code status_code_genare_t; @@ -123,21 +123,21 @@ typedef enum status_code status_code_genare_t; Status codes used by MAC stack. */ enum status_code_wireless { - //STATUS_OK = 0, //!< Success - ERR_IO_ERROR = -1, //!< I/O error - ERR_FLUSHED = -2, //!< Request flushed from queue - ERR_TIMEOUT = -3, //!< Operation timed out - ERR_BAD_DATA = -4, //!< Data integrity check failed - ERR_PROTOCOL = -5, //!< Protocol error - ERR_UNSUPPORTED_DEV = -6, //!< Unsupported device - ERR_NO_MEMORY = -7, //!< Insufficient memory - ERR_INVALID_ARG = -8, //!< Invalid argument - ERR_BAD_ADDRESS = -9, //!< Bad address - ERR_BUSY = -10, //!< Resource is busy - ERR_BAD_FORMAT = -11, //!< Data format not recognized - ERR_NO_TIMER = -12, //!< No timer available - ERR_TIMER_ALREADY_RUNNING = -13, //!< Timer already running - ERR_TIMER_NOT_RUNNING = -14, //!< Timer not running + // STATUS_OK = 0, //!< Success + ERR_IO_ERROR = -1, //!< I/O error + ERR_FLUSHED = -2, //!< Request flushed from queue + ERR_TIMEOUT = -3, //!< Operation timed out + ERR_BAD_DATA = -4, //!< Data integrity check failed + ERR_PROTOCOL = -5, //!< Protocol error + ERR_UNSUPPORTED_DEV = -6, //!< Unsupported device + ERR_NO_MEMORY = -7, //!< Insufficient memory + ERR_INVALID_ARG = -8, //!< Invalid argument + ERR_BAD_ADDRESS = -9, //!< Bad address + ERR_BUSY = -10, //!< Resource is busy + ERR_BAD_FORMAT = -11, //!< Data format not recognized + ERR_NO_TIMER = -12, //!< No timer available + ERR_TIMER_ALREADY_RUNNING = -13, //!< Timer already running + ERR_TIMER_NOT_RUNNING = -14, //!< Timer not running /** * \brief Operation in progress @@ -148,7 +148,7 @@ enum status_code_wireless { * \note Drivers should never return this status code to any * callers. It is strictly for internal use. */ - OPERATION_IN_PROGRESS = -128, + OPERATION_IN_PROGRESS = -128, }; typedef enum status_code_wireless status_code_t; diff --git a/tmk_core/protocol/arm_atsam/usb/udc.c b/tmk_core/protocol/arm_atsam/usb/udc.c index 12444d305..d04e9b7b2 100644 --- a/tmk_core/protocol/arm_atsam/usb/udc.c +++ b/tmk_core/protocol/arm_atsam/usb/udc.c @@ -83,7 +83,6 @@ static usb_iface_desc_t UDC_DESC_STORAGE *udc_ptr_iface; //! @} - //! \name Internal structure to store the USB device main strings //! @{ @@ -91,11 +90,7 @@ static usb_iface_desc_t UDC_DESC_STORAGE *udc_ptr_iface; * \brief Language ID of USB device (US ID by default) */ COMPILER_WORD_ALIGNED -static UDC_DESC_STORAGE usb_str_lgid_desc_t udc_string_desc_languageid = { - .desc.bLength = sizeof(usb_str_lgid_desc_t), - .desc.bDescriptorType = USB_DT_STRING, - .string = {LE16(USB_LANGID_EN_US)} -}; +static UDC_DESC_STORAGE usb_str_lgid_desc_t udc_string_desc_languageid = {.desc.bLength = sizeof(usb_str_lgid_desc_t), .desc.bDescriptorType = USB_DT_STRING, .string = {LE16(USB_LANGID_EN_US)}}; /** * \brief USB device manufacture name storage @@ -104,9 +99,9 @@ static UDC_DESC_STORAGE usb_str_lgid_desc_t udc_string_desc_languageid = { */ #ifdef USB_DEVICE_MANUFACTURE_NAME static uint8_t udc_string_manufacturer_name[] = USB_DEVICE_MANUFACTURE_NAME; -#define USB_DEVICE_MANUFACTURE_NAME_SIZE (sizeof(udc_string_manufacturer_name)-1) +# define USB_DEVICE_MANUFACTURE_NAME_SIZE (sizeof(udc_string_manufacturer_name) - 1) #else -#define USB_DEVICE_MANUFACTURE_NAME_SIZE 0 +# define USB_DEVICE_MANUFACTURE_NAME_SIZE 0 #endif /** @@ -116,37 +111,34 @@ static uint8_t udc_string_manufacturer_name[] = USB_DEVICE_MANUFACTURE_NAME; */ #ifdef USB_DEVICE_PRODUCT_NAME static uint8_t udc_string_product_name[] = USB_DEVICE_PRODUCT_NAME; -#define USB_DEVICE_PRODUCT_NAME_SIZE (sizeof(udc_string_product_name)-1) +# define USB_DEVICE_PRODUCT_NAME_SIZE (sizeof(udc_string_product_name) - 1) #else -#define USB_DEVICE_PRODUCT_NAME_SIZE 0 +# define USB_DEVICE_PRODUCT_NAME_SIZE 0 #endif #if defined USB_DEVICE_SERIAL_NAME -#define USB_DEVICE_SERIAL_NAME_SIZE (sizeof(USB_DEVICE_SERIAL_NAME)-1) +# define USB_DEVICE_SERIAL_NAME_SIZE (sizeof(USB_DEVICE_SERIAL_NAME) - 1) #else -#define USB_DEVICE_SERIAL_NAME_SIZE 0 +# define USB_DEVICE_SERIAL_NAME_SIZE 0 #endif -uint8_t usb_device_serial_name_size = 0; +uint8_t usb_device_serial_name_size = 0; #if defined USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL -uint8_t bootloader_serial_number[BOOTLOADER_SERIAL_MAX_SIZE+1]=""; +uint8_t bootloader_serial_number[BOOTLOADER_SERIAL_MAX_SIZE + 1] = ""; #endif -static const uint8_t *udc_get_string_serial_name(void) -{ -#if defined USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL - uint32_t serial_ptrloc = (uint32_t)&_srom - 4; - uint32_t serial_address = *(uint32_t *)serial_ptrloc; //Address of bootloader's serial number if available +static const uint8_t *udc_get_string_serial_name(void) { +#if defined USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL + uint32_t serial_ptrloc = (uint32_t)&_srom - 4; + uint32_t serial_address = *(uint32_t *)serial_ptrloc; // Address of bootloader's serial number if available - if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) //Check for factory programmed serial address + if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) // Check for factory programmed serial address { - if ((serial_address & 0xFF) % 4 == 0) //Check alignment + if ((serial_address & 0xFF) % 4 == 0) // Check alignment { - uint16_t *serial_use = (uint16_t *)(serial_address); //Point to address of string in rom - uint8_t serial_length = 0; + uint16_t *serial_use = (uint16_t *)(serial_address); // Point to address of string in rom + uint8_t serial_length = 0; - while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && - serial_length < BOOTLOADER_SERIAL_MAX_SIZE) - { + while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && serial_length < BOOTLOADER_SERIAL_MAX_SIZE) { bootloader_serial_number[serial_length] = *(serial_use + serial_length) & 0xFF; serial_length++; } @@ -154,7 +146,7 @@ static const uint8_t *udc_get_string_serial_name(void) usb_device_serial_name_size = serial_length; - return bootloader_serial_number; //Use serial programmed into bootloader rom + return bootloader_serial_number; // Use serial programmed into bootloader rom } } #endif @@ -162,9 +154,9 @@ static const uint8_t *udc_get_string_serial_name(void) usb_device_serial_name_size = USB_DEVICE_SERIAL_NAME_SIZE; #if defined USB_DEVICE_SERIAL_NAME - return (const uint8_t *)USB_DEVICE_SERIAL_NAME; //Use serial supplied by keyboard's config.h + return (const uint8_t *)USB_DEVICE_SERIAL_NAME; // Use serial supplied by keyboard's config.h #else - return 0; //No serial supplied + return 0; // No serial supplied #endif } @@ -173,38 +165,26 @@ static const uint8_t *udc_get_string_serial_name(void) * Structure used to transfer ASCII strings to USB String descriptor structure. */ #ifndef BOOTLOADER_SERIAL_MAX_SIZE -#define BOOTLOADER_SERIAL_MAX_SIZE 0 -#endif //BOOTLOADER_SERIAL_MAX_SIZE +# define BOOTLOADER_SERIAL_MAX_SIZE 0 +#endif // BOOTLOADER_SERIAL_MAX_SIZE struct udc_string_desc_t { usb_str_desc_t header; - le16_t string[Max(Max(Max(USB_DEVICE_MANUFACTURE_NAME_SIZE, \ - USB_DEVICE_PRODUCT_NAME_SIZE), USB_DEVICE_SERIAL_NAME_SIZE), \ - BOOTLOADER_SERIAL_MAX_SIZE)]; + le16_t string[Max(Max(Max(USB_DEVICE_MANUFACTURE_NAME_SIZE, USB_DEVICE_PRODUCT_NAME_SIZE), USB_DEVICE_SERIAL_NAME_SIZE), BOOTLOADER_SERIAL_MAX_SIZE)]; }; COMPILER_WORD_ALIGNED -static UDC_DESC_STORAGE struct udc_string_desc_t udc_string_desc = { - .header.bDescriptorType = USB_DT_STRING -}; +static UDC_DESC_STORAGE struct udc_string_desc_t udc_string_desc = {.header.bDescriptorType = USB_DT_STRING}; //! @} -usb_iface_desc_t UDC_DESC_STORAGE *udc_get_interface_desc(void) -{ - return udc_ptr_iface; -} +usb_iface_desc_t UDC_DESC_STORAGE *udc_get_interface_desc(void) { return udc_ptr_iface; } /** * \brief Returns a value to check the end of USB Configuration descriptor * * \return address after the last byte of USB Configuration descriptor */ -static usb_conf_desc_t UDC_DESC_STORAGE *udc_get_eof_conf(void) -{ - return (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *) - udc_ptr_conf->desc + - le16_to_cpu(udc_ptr_conf->desc->wTotalLength)); -} +static usb_conf_desc_t UDC_DESC_STORAGE *udc_get_eof_conf(void) { return (UDC_DESC_STORAGE usb_conf_desc_t *)((uint8_t *)udc_ptr_conf->desc + le16_to_cpu(udc_ptr_conf->desc->wTotalLength)); } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) /** * \brief Search specific descriptor in global interface descriptor * @@ -215,30 +195,26 @@ static usb_conf_desc_t UDC_DESC_STORAGE *udc_get_eof_conf(void) * \return address of specific descriptor found * \return NULL if it is the end of global interface descriptor */ -static usb_conf_desc_t UDC_DESC_STORAGE *udc_next_desc_in_iface(usb_conf_desc_t - UDC_DESC_STORAGE * desc, uint8_t desc_id) -{ +static usb_conf_desc_t UDC_DESC_STORAGE *udc_next_desc_in_iface(usb_conf_desc_t UDC_DESC_STORAGE *desc, uint8_t desc_id) { usb_conf_desc_t UDC_DESC_STORAGE *ptr_eof_desc; ptr_eof_desc = udc_get_eof_conf(); // Go to next descriptor - desc = (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *) desc + - desc->bLength); + desc = (UDC_DESC_STORAGE usb_conf_desc_t *)((uint8_t *)desc + desc->bLength); // Check the end of configuration descriptor while (ptr_eof_desc > desc) { // If new interface descriptor is found, // then it is the end of the current global interface descriptor if (USB_DT_INTERFACE == desc->bDescriptorType) { - break; // End of global interface descriptor + break; // End of global interface descriptor } if (desc_id == desc->bDescriptorType) { - return desc; // Specific descriptor found + return desc; // Specific descriptor found } // Go to next descriptor - desc = (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *) desc + - desc->bLength); + desc = (UDC_DESC_STORAGE usb_conf_desc_t *)((uint8_t *)desc + desc->bLength); } - return NULL; // No specific descriptor found + return NULL; // No specific descriptor found } #endif @@ -251,8 +227,7 @@ static usb_conf_desc_t UDC_DESC_STORAGE *udc_next_desc_in_iface(usb_conf_desc_t * * \return 1 if found or 0 if not found */ -static bool udc_update_iface_desc(uint8_t iface_num, uint8_t setting_num) -{ +static bool udc_update_iface_desc(uint8_t iface_num, uint8_t setting_num) { usb_conf_desc_t UDC_DESC_STORAGE *ptr_end_desc; if (0 == udc_num_configuration) { @@ -264,28 +239,22 @@ static bool udc_update_iface_desc(uint8_t iface_num, uint8_t setting_num) } // Start at the beginning of configuration descriptor - udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *) - udc_ptr_conf->desc; + udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *)udc_ptr_conf->desc; // Check the end of configuration descriptor ptr_end_desc = udc_get_eof_conf(); - while (ptr_end_desc > - (UDC_DESC_STORAGE usb_conf_desc_t *) udc_ptr_iface) { + while (ptr_end_desc > (UDC_DESC_STORAGE usb_conf_desc_t *)udc_ptr_iface) { if (USB_DT_INTERFACE == udc_ptr_iface->bDescriptorType) { // A interface descriptor is found // Check interface and alternate setting number - if ((iface_num == udc_ptr_iface->bInterfaceNumber) && - (setting_num == - udc_ptr_iface->bAlternateSetting)) { - return true; // Interface found + if ((iface_num == udc_ptr_iface->bInterfaceNumber) && (setting_num == udc_ptr_iface->bAlternateSetting)) { + return true; // Interface found } } // Go to next descriptor - udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *) ( - (uint8_t *) udc_ptr_iface + - udc_ptr_iface->bLength); + udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *)((uint8_t *)udc_ptr_iface + udc_ptr_iface->bLength); } - return false; // Interface not found + return false; // Interface not found } /** @@ -296,8 +265,7 @@ static bool udc_update_iface_desc(uint8_t iface_num, uint8_t setting_num) * * \return 1 if it is done or 0 if interface is not found */ -static bool udc_iface_disable(uint8_t iface_num) -{ +static bool udc_iface_disable(uint8_t iface_num) { udi_api_t UDC_DESC_STORAGE *udi_api; // Select first alternate setting of the interface @@ -309,7 +277,7 @@ static bool udc_iface_disable(uint8_t iface_num) // Select the interface with the current alternate setting udi_api = udc_ptr_conf->udi_apis[iface_num]; -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) if (!udc_update_iface_desc(iface_num, udi_api->getsetting())) { return false; } @@ -317,13 +285,10 @@ static bool udc_iface_disable(uint8_t iface_num) // Start at the beginning of interface descriptor { usb_ep_desc_t UDC_DESC_STORAGE *ep_desc; - ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) udc_ptr_iface; + ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)udc_ptr_iface; while (1) { // Search Endpoint descriptor included in global interface descriptor - ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) - udc_next_desc_in_iface((UDC_DESC_STORAGE - usb_conf_desc_t *) - ep_desc, USB_DT_ENDPOINT); + ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)udc_next_desc_in_iface((UDC_DESC_STORAGE usb_conf_desc_t *)ep_desc, USB_DT_ENDPOINT); if (NULL == ep_desc) { break; } @@ -348,31 +313,23 @@ static bool udc_iface_disable(uint8_t iface_num) * * \return 1 if it is done or 0 if interface is not found */ -static bool udc_iface_enable(uint8_t iface_num, uint8_t setting_num) -{ +static bool udc_iface_enable(uint8_t iface_num, uint8_t setting_num) { // Select the interface descriptor if (!udc_update_iface_desc(iface_num, setting_num)) { return false; } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) usb_ep_desc_t UDC_DESC_STORAGE *ep_desc; // Start at the beginning of the global interface descriptor - ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) udc_ptr_iface; + ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)udc_ptr_iface; while (1) { // Search Endpoint descriptor included in the global interface descriptor - ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) - udc_next_desc_in_iface((UDC_DESC_STORAGE - usb_conf_desc_t *) ep_desc, - USB_DT_ENDPOINT); - if (NULL == ep_desc) - break; + ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)udc_next_desc_in_iface((UDC_DESC_STORAGE usb_conf_desc_t *)ep_desc, USB_DT_ENDPOINT); + if (NULL == ep_desc) break; // Alloc the endpoint used by the interface - if (!udd_ep_alloc(ep_desc->bEndpointAddress, - ep_desc->bmAttributes, - le16_to_cpu - (ep_desc->wMaxPacketSize))) { + if (!udd_ep_alloc(ep_desc->bEndpointAddress, ep_desc->bmAttributes, le16_to_cpu(ep_desc->wMaxPacketSize))) { return false; } } @@ -383,15 +340,11 @@ static bool udc_iface_enable(uint8_t iface_num, uint8_t setting_num) /*! \brief Start the USB Device stack */ -void udc_start(void) -{ - udd_enable(); -} +void udc_start(void) { udd_enable(); } /*! \brief Stop the USB Device stack */ -void udc_stop(void) -{ +void udc_stop(void) { udd_disable(); udc_reset(); } @@ -400,20 +353,16 @@ void udc_stop(void) * \brief Reset the current configuration of the USB device, * This routines can be called by UDD when a RESET on the USB line occurs. */ -void udc_reset(void) -{ +void udc_reset(void) { uint8_t iface_num; if (udc_num_configuration) { - for (iface_num = 0; - iface_num < udc_ptr_conf->desc->bNumInterfaces; - iface_num++) { + for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; iface_num++) { udc_iface_disable(iface_num); } } udc_num_configuration = 0; -#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \ - == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) +#if (USB_CONFIG_ATTR_REMOTE_WAKEUP == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) if (CPU_TO_LE16(USB_DEV_STATUS_REMOTEWAKEUP) & udc_device_status) { // Remote wakeup is enabled then disable it UDC_REMOTEWAKEUP_DISABLE(); @@ -421,20 +370,17 @@ void udc_reset(void) #endif udc_device_status = #if (USB_DEVICE_ATTR & USB_CONFIG_ATTR_SELF_POWERED) - CPU_TO_LE16(USB_DEV_STATUS_SELF_POWERED); + CPU_TO_LE16(USB_DEV_STATUS_SELF_POWERED); #else - CPU_TO_LE16(USB_DEV_STATUS_BUS_POWERED); + CPU_TO_LE16(USB_DEV_STATUS_BUS_POWERED); #endif } -void udc_sof_notify(void) -{ +void udc_sof_notify(void) { uint8_t iface_num; if (udc_num_configuration) { - for (iface_num = 0; - iface_num < udc_ptr_conf->desc->bNumInterfaces; - iface_num++) { + for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; iface_num++) { if (udc_ptr_conf->udi_apis[iface_num]->sof_notify != NULL) { udc_ptr_conf->udi_apis[iface_num]->sof_notify(); } @@ -447,36 +393,31 @@ void udc_sof_notify(void) * * \return true if success */ -static bool udc_req_std_dev_get_status(void) -{ +static bool udc_req_std_dev_get_status(void) { if (udd_g_ctrlreq.req.wLength != sizeof(udc_device_status)) { return false; } - udd_set_setup_payload( (uint8_t *) & udc_device_status, - sizeof(udc_device_status)); + udd_set_setup_payload((uint8_t *)&udc_device_status, sizeof(udc_device_status)); return true; } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) /** * \brief Standard endpoint request to get endpoint status * * \return true if success */ -static bool udc_req_std_ep_get_status(void) -{ +static bool udc_req_std_ep_get_status(void) { static le16_t udc_ep_status; if (udd_g_ctrlreq.req.wLength != sizeof(udc_ep_status)) { return false; } - udc_ep_status = udd_ep_is_halted(udd_g_ctrlreq.req. - wIndex & 0xFF) ? CPU_TO_LE16(USB_EP_STATUS_HALTED) : 0; + udc_ep_status = udd_ep_is_halted(udd_g_ctrlreq.req.wIndex & 0xFF) ? CPU_TO_LE16(USB_EP_STATUS_HALTED) : 0; - udd_set_setup_payload( (uint8_t *) & udc_ep_status, - sizeof(udc_ep_status)); + udd_set_setup_payload((uint8_t *)&udc_ep_status, sizeof(udc_ep_status)); return true; } #endif @@ -486,16 +427,14 @@ static bool udc_req_std_ep_get_status(void) * * \return true if success */ -static bool udc_req_std_dev_clear_feature(void) -{ +static bool udc_req_std_dev_clear_feature(void) { if (udd_g_ctrlreq.req.wLength) { return false; } if (udd_g_ctrlreq.req.wValue == USB_DEV_FEATURE_REMOTE_WAKEUP) { udc_device_status &= CPU_TO_LE16(~(uint32_t)USB_DEV_STATUS_REMOTEWAKEUP); -#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \ - == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) +#if (USB_CONFIG_ATTR_REMOTE_WAKEUP == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) UDC_REMOTEWAKEUP_DISABLE(); #endif return true; @@ -503,14 +442,13 @@ static bool udc_req_std_dev_clear_feature(void) return false; } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) /** * \brief Standard endpoint request to clear endpoint feature * * \return true if success */ -static bool udc_req_std_ep_clear_feature(void) -{ +static bool udc_req_std_ep_clear_feature(void) { if (udd_g_ctrlreq.req.wLength) { return false; } @@ -527,59 +465,56 @@ static bool udc_req_std_ep_clear_feature(void) * * \return true if success */ -static bool udc_req_std_dev_set_feature(void) -{ +static bool udc_req_std_dev_set_feature(void) { if (udd_g_ctrlreq.req.wLength) { return false; } switch (udd_g_ctrlreq.req.wValue) { - - case USB_DEV_FEATURE_REMOTE_WAKEUP: -#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \ - == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) - udc_device_status |= CPU_TO_LE16(USB_DEV_STATUS_REMOTEWAKEUP); - UDC_REMOTEWAKEUP_ENABLE(); - return true; + case USB_DEV_FEATURE_REMOTE_WAKEUP: +#if (USB_CONFIG_ATTR_REMOTE_WAKEUP == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP)) + udc_device_status |= CPU_TO_LE16(USB_DEV_STATUS_REMOTEWAKEUP); + UDC_REMOTEWAKEUP_ENABLE(); + return true; #else - return false; + return false; #endif #ifdef USB_DEVICE_HS_SUPPORT - case USB_DEV_FEATURE_TEST_MODE: - if (!udd_is_high_speed()) { - break; - } - if (udd_g_ctrlreq.req.wIndex & 0xff) { + case USB_DEV_FEATURE_TEST_MODE: + if (!udd_is_high_speed()) { + break; + } + if (udd_g_ctrlreq.req.wIndex & 0xff) { + break; + } + // Unconfigure the device, terminating all ongoing requests + udc_reset(); + switch ((udd_g_ctrlreq.req.wIndex >> 8) & 0xFF) { + case USB_DEV_TEST_MODE_J: + udd_g_ctrlreq.callback = udd_test_mode_j; + return true; + + case USB_DEV_TEST_MODE_K: + udd_g_ctrlreq.callback = udd_test_mode_k; + return true; + + case USB_DEV_TEST_MODE_SE0_NAK: + udd_g_ctrlreq.callback = udd_test_mode_se0_nak; + return true; + + case USB_DEV_TEST_MODE_PACKET: + udd_g_ctrlreq.callback = udd_test_mode_packet; + return true; + + case USB_DEV_TEST_MODE_FORCE_ENABLE: // Only for downstream facing hub ports + default: + break; + } break; - } - // Unconfigure the device, terminating all ongoing requests - udc_reset(); - switch ((udd_g_ctrlreq.req.wIndex >> 8) & 0xFF) { - case USB_DEV_TEST_MODE_J: - udd_g_ctrlreq.callback = udd_test_mode_j; - return true; - - case USB_DEV_TEST_MODE_K: - udd_g_ctrlreq.callback = udd_test_mode_k; - return true; - - case USB_DEV_TEST_MODE_SE0_NAK: - udd_g_ctrlreq.callback = udd_test_mode_se0_nak; - return true; - - case USB_DEV_TEST_MODE_PACKET: - udd_g_ctrlreq.callback = udd_test_mode_packet; - return true; - - case USB_DEV_TEST_MODE_FORCE_ENABLE: // Only for downstream facing hub ports +#endif default: break; - } - break; -#endif - default: - break; } return false; } @@ -589,9 +524,8 @@ static bool udc_req_std_dev_set_feature(void) * * \return true if success */ -#if (0!=USB_DEVICE_MAX_EP) -static bool udc_req_std_ep_set_feature(void) -{ +#if (0 != USB_DEVICE_MAX_EP) +static bool udc_req_std_ep_set_feature(void) { if (udd_g_ctrlreq.req.wLength) { return false; } @@ -607,18 +541,14 @@ static bool udc_req_std_ep_set_feature(void) * \brief Change the address of device * Callback called at the end of request set address */ -static void udc_valid_address(void) -{ - udd_set_address(udd_g_ctrlreq.req.wValue & 0x7F); -} +static void udc_valid_address(void) { udd_set_address(udd_g_ctrlreq.req.wValue & 0x7F); } /** * \brief Standard device request to set device address * * \return true if success */ -static bool udc_req_std_dev_set_address(void) -{ +static bool udc_req_std_dev_set_address(void) { if (udd_g_ctrlreq.req.wLength) { return false; } @@ -634,53 +564,49 @@ static bool udc_req_std_dev_set_address(void) * * \return true if success */ -static bool udc_req_std_dev_get_str_desc(void) -{ - uint8_t i; +static bool udc_req_std_dev_get_str_desc(void) { + uint8_t i; const uint8_t *str; - uint8_t str_length = 0; + uint8_t str_length = 0; // Link payload pointer to the string corresponding at request switch (udd_g_ctrlreq.req.wValue & 0xff) { - case 0: - udd_set_setup_payload((uint8_t *) &udc_string_desc_languageid, - sizeof(udc_string_desc_languageid)); - break; + case 0: + udd_set_setup_payload((uint8_t *)&udc_string_desc_languageid, sizeof(udc_string_desc_languageid)); + break; #ifdef USB_DEVICE_MANUFACTURE_NAME - case 1: - str_length = USB_DEVICE_MANUFACTURE_NAME_SIZE; - str = udc_string_manufacturer_name; - break; + case 1: + str_length = USB_DEVICE_MANUFACTURE_NAME_SIZE; + str = udc_string_manufacturer_name; + break; #endif #ifdef USB_DEVICE_PRODUCT_NAME - case 2: - str_length = USB_DEVICE_PRODUCT_NAME_SIZE; - str = udc_string_product_name; - break; + case 2: + str_length = USB_DEVICE_PRODUCT_NAME_SIZE; + str = udc_string_product_name; + break; #endif - case 3: - str = udc_get_string_serial_name(); - str_length = usb_device_serial_name_size; - break; - default: -#ifdef UDC_GET_EXTRA_STRING - if (UDC_GET_EXTRA_STRING()) { + case 3: + str = udc_get_string_serial_name(); + str_length = usb_device_serial_name_size; break; - } + default: +#ifdef UDC_GET_EXTRA_STRING + if (UDC_GET_EXTRA_STRING()) { + break; + } #endif - return false; + return false; } if (str_length) { - for(i = 0; i < str_length; i++) { + for (i = 0; i < str_length; i++) { udc_string_desc.string[i] = cpu_to_le16((le16_t)str[i]); } - udc_string_desc.header.bLength = 2 + (str_length) * 2; - udd_set_setup_payload( - (uint8_t *) &udc_string_desc, - udc_string_desc.header.bLength); + udc_string_desc.header.bLength = 2 + (str_length)*2; + udd_set_setup_payload((uint8_t *)&udc_string_desc, udc_string_desc.header.bLength); } return true; @@ -691,106 +617,89 @@ static bool udc_req_std_dev_get_str_desc(void) * * \return true if success */ -static bool udc_req_std_dev_get_descriptor(void) -{ +static bool udc_req_std_dev_get_descriptor(void) { uint8_t conf_num; conf_num = udd_g_ctrlreq.req.wValue & 0xff; // Check descriptor ID - switch ((uint8_t) (udd_g_ctrlreq.req.wValue >> 8)) { - case USB_DT_DEVICE: - // Device descriptor requested + switch ((uint8_t)(udd_g_ctrlreq.req.wValue >> 8)) { + case USB_DT_DEVICE: + // Device descriptor requested #ifdef USB_DEVICE_HS_SUPPORT - if (!udd_is_high_speed()) { - udd_set_setup_payload( - (uint8_t *) udc_config.confdev_hs, - udc_config.confdev_hs->bLength); - } else + if (!udd_is_high_speed()) { + udd_set_setup_payload((uint8_t *)udc_config.confdev_hs, udc_config.confdev_hs->bLength); + } else #endif - { - udd_set_setup_payload( - (uint8_t *) udc_config.confdev_lsfs, - udc_config.confdev_lsfs->bLength); - } - break; + { + udd_set_setup_payload((uint8_t *)udc_config.confdev_lsfs, udc_config.confdev_lsfs->bLength); + } + break; - case USB_DT_CONFIGURATION: - // Configuration descriptor requested + case USB_DT_CONFIGURATION: + // Configuration descriptor requested #ifdef USB_DEVICE_HS_SUPPORT - if (udd_is_high_speed()) { - // HS descriptor - if (conf_num >= udc_config.confdev_hs->bNumConfigurations) { - return false; - } - udd_set_setup_payload( - (uint8_t *)udc_config.conf_hs[conf_num].desc, - le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength)); - } else + if (udd_is_high_speed()) { + // HS descriptor + if (conf_num >= udc_config.confdev_hs->bNumConfigurations) { + return false; + } + udd_set_setup_payload((uint8_t *)udc_config.conf_hs[conf_num].desc, le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength)); + } else #endif - { - // FS descriptor - if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) { - return false; + { + // FS descriptor + if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) { + return false; + } + udd_set_setup_payload((uint8_t *)udc_config.conf_lsfs[conf_num].desc, le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength)); } - udd_set_setup_payload( - (uint8_t *)udc_config.conf_lsfs[conf_num].desc, - le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength)); - } - ((usb_conf_desc_t *) udd_g_ctrlreq.payload)->bDescriptorType = - USB_DT_CONFIGURATION; - break; + ((usb_conf_desc_t *)udd_g_ctrlreq.payload)->bDescriptorType = USB_DT_CONFIGURATION; + break; #ifdef USB_DEVICE_HS_SUPPORT - case USB_DT_DEVICE_QUALIFIER: - // Device qualifier descriptor requested - udd_set_setup_payload( (uint8_t *) udc_config.qualifier, - udc_config.qualifier->bLength); - break; - - case USB_DT_OTHER_SPEED_CONFIGURATION: - // Other configuration descriptor requested - if (!udd_is_high_speed()) { - // HS descriptor - if (conf_num >= udc_config.confdev_hs->bNumConfigurations) { - return false; + case USB_DT_DEVICE_QUALIFIER: + // Device qualifier descriptor requested + udd_set_setup_payload((uint8_t *)udc_config.qualifier, udc_config.qualifier->bLength); + break; + + case USB_DT_OTHER_SPEED_CONFIGURATION: + // Other configuration descriptor requested + if (!udd_is_high_speed()) { + // HS descriptor + if (conf_num >= udc_config.confdev_hs->bNumConfigurations) { + return false; + } + udd_set_setup_payload((uint8_t *)udc_config.conf_hs[conf_num].desc, le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength)); + } else { + // FS descriptor + if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) { + return false; + } + udd_set_setup_payload((uint8_t *)udc_config.conf_lsfs[conf_num].desc, le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength)); } - udd_set_setup_payload( - (uint8_t *)udc_config.conf_hs[conf_num].desc, - le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength)); - } else { - // FS descriptor - if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) { + ((usb_conf_desc_t *)udd_g_ctrlreq.payload)->bDescriptorType = USB_DT_OTHER_SPEED_CONFIGURATION; + break; +#endif + + case USB_DT_BOS: + // Device BOS descriptor requested + if (udc_config.conf_bos == NULL) { return false; } - udd_set_setup_payload( - (uint8_t *)udc_config.conf_lsfs[conf_num].desc, - le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength)); - } - ((usb_conf_desc_t *) udd_g_ctrlreq.payload)->bDescriptorType = - USB_DT_OTHER_SPEED_CONFIGURATION; - break; -#endif + udd_set_setup_payload((uint8_t *)udc_config.conf_bos, udc_config.conf_bos->wTotalLength); + break; - case USB_DT_BOS: - // Device BOS descriptor requested - if (udc_config.conf_bos == NULL) { - return false; - } - udd_set_setup_payload( (uint8_t *) udc_config.conf_bos, - udc_config.conf_bos->wTotalLength); - break; + case USB_DT_STRING: + // String descriptor requested + if (!udc_req_std_dev_get_str_desc()) { + return false; + } + break; - case USB_DT_STRING: - // String descriptor requested - if (!udc_req_std_dev_get_str_desc()) { + default: + // Unknown descriptor requested return false; - } - break; - - default: - // Unknown descriptor requested - return false; } // if the descriptor is larger than length requested, then reduce it if (udd_g_ctrlreq.req.wLength < udd_g_ctrlreq.payload_size) { @@ -804,13 +713,12 @@ static bool udc_req_std_dev_get_descriptor(void) * * \return true if success */ -static bool udc_req_std_dev_get_configuration(void) -{ +static bool udc_req_std_dev_get_configuration(void) { if (udd_g_ctrlreq.req.wLength != 1) { return false; } - udd_set_setup_payload(&udc_num_configuration,1); + udd_set_setup_payload(&udc_num_configuration, 1); return true; } @@ -819,8 +727,7 @@ static bool udc_req_std_dev_get_configuration(void) * * \return true if success */ -static bool udc_req_std_dev_set_configuration(void) -{ +static bool udc_req_std_dev_set_configuration(void) { uint8_t iface_num; // Check request length @@ -835,16 +742,14 @@ static bool udc_req_std_dev_set_configuration(void) #ifdef USB_DEVICE_HS_SUPPORT if (udd_is_high_speed()) { // HS descriptor - if ((udd_g_ctrlreq.req.wValue & 0xFF) > - udc_config.confdev_hs->bNumConfigurations) { + if ((udd_g_ctrlreq.req.wValue & 0xFF) > udc_config.confdev_hs->bNumConfigurations) { return false; } } else #endif { // FS descriptor - if ((udd_g_ctrlreq.req.wValue & 0xFF) > - udc_config.confdev_lsfs->bNumConfigurations) { + if ((udd_g_ctrlreq.req.wValue & 0xFF) > udc_config.confdev_lsfs->bNumConfigurations) { return false; } } @@ -855,7 +760,7 @@ static bool udc_req_std_dev_set_configuration(void) // Enable new configuration udc_num_configuration = udd_g_ctrlreq.req.wValue & 0xFF; if (udc_num_configuration == 0) { - return true; // Default empty configuration requested + return true; // Default empty configuration requested } // Update pointer of the configuration descriptor #ifdef USB_DEVICE_HS_SUPPORT @@ -869,8 +774,7 @@ static bool udc_req_std_dev_set_configuration(void) udc_ptr_conf = &udc_config.conf_lsfs[udc_num_configuration - 1]; } // Enable all interfaces of the selected configuration - for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; - iface_num++) { + for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; iface_num++) { if (!udc_iface_enable(iface_num, 0)) { return false; } @@ -884,16 +788,15 @@ static bool udc_req_std_dev_set_configuration(void) * * \return true if success */ -static bool udc_req_std_iface_get_setting(void) -{ - uint8_t iface_num; +static bool udc_req_std_iface_get_setting(void) { + uint8_t iface_num; udi_api_t UDC_DESC_STORAGE *udi_api; if (udd_g_ctrlreq.req.wLength != 1) { - return false; // Error in request + return false; // Error in request } if (!udc_num_configuration) { - return false; // The device is not is configured state yet + return false; // The device is not is configured state yet } // Check the interface number included in the request @@ -908,11 +811,11 @@ static bool udc_req_std_iface_get_setting(void) return false; } // Get alternate setting from UDI - udi_api = udc_ptr_conf->udi_apis[iface_num]; + udi_api = udc_ptr_conf->udi_apis[iface_num]; udc_iface_setting = udi_api->getsetting(); // Link value to payload pointer of request - udd_set_setup_payload(&udc_iface_setting,1); + udd_set_setup_payload(&udc_iface_setting, 1); return true; } @@ -922,18 +825,17 @@ static bool udc_req_std_iface_get_setting(void) * * \return true if success */ -static bool udc_req_std_iface_set_setting(void) -{ +static bool udc_req_std_iface_set_setting(void) { uint8_t iface_num, setting_num; if (udd_g_ctrlreq.req.wLength) { - return false; // Error in request + return false; // Error in request } if (!udc_num_configuration) { - return false; // The device is not is configured state yet + return false; // The device is not is configured state yet } - iface_num = udd_g_ctrlreq.req.wIndex & 0xFF; + iface_num = udd_g_ctrlreq.req.wIndex & 0xFF; setting_num = udd_g_ctrlreq.req.wValue & 0xFF; // Disable current setting @@ -950,45 +852,44 @@ static bool udc_req_std_iface_set_setting(void) * * \return true if the request is supported */ -static bool udc_reqstd(void) -{ +static bool udc_reqstd(void) { if (Udd_setup_is_in()) { // GET Standard Requests if (udd_g_ctrlreq.req.wLength == 0) { - return false; // Error for USB host + return false; // Error for USB host } if (USB_REQ_RECIP_DEVICE == Udd_setup_recipient()) { // Standard Get Device request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_GET_STATUS: - return udc_req_std_dev_get_status(); - case USB_REQ_GET_DESCRIPTOR: - return udc_req_std_dev_get_descriptor(); - case USB_REQ_GET_CONFIGURATION: - return udc_req_std_dev_get_configuration(); - default: - break; + case USB_REQ_GET_STATUS: + return udc_req_std_dev_get_status(); + case USB_REQ_GET_DESCRIPTOR: + return udc_req_std_dev_get_descriptor(); + case USB_REQ_GET_CONFIGURATION: + return udc_req_std_dev_get_configuration(); + default: + break; } } if (USB_REQ_RECIP_INTERFACE == Udd_setup_recipient()) { // Standard Get Interface request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_GET_INTERFACE: - return udc_req_std_iface_get_setting(); - default: - break; + case USB_REQ_GET_INTERFACE: + return udc_req_std_iface_get_setting(); + default: + break; } } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) if (USB_REQ_RECIP_ENDPOINT == Udd_setup_recipient()) { // Standard Get Endpoint request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_GET_STATUS: - return udc_req_std_ep_get_status(); - default: - break; + case USB_REQ_GET_STATUS: + return udc_req_std_ep_get_status(); + default: + break; } } #endif @@ -997,41 +898,41 @@ static bool udc_reqstd(void) if (USB_REQ_RECIP_DEVICE == Udd_setup_recipient()) { // Standard Set Device request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_SET_ADDRESS: - return udc_req_std_dev_set_address(); - case USB_REQ_CLEAR_FEATURE: - return udc_req_std_dev_clear_feature(); - case USB_REQ_SET_FEATURE: - return udc_req_std_dev_set_feature(); - case USB_REQ_SET_CONFIGURATION: - return udc_req_std_dev_set_configuration(); - case USB_REQ_SET_DESCRIPTOR: - /* Not supported (defined as optional by the USB 2.0 spec) */ - break; - default: - break; + case USB_REQ_SET_ADDRESS: + return udc_req_std_dev_set_address(); + case USB_REQ_CLEAR_FEATURE: + return udc_req_std_dev_clear_feature(); + case USB_REQ_SET_FEATURE: + return udc_req_std_dev_set_feature(); + case USB_REQ_SET_CONFIGURATION: + return udc_req_std_dev_set_configuration(); + case USB_REQ_SET_DESCRIPTOR: + /* Not supported (defined as optional by the USB 2.0 spec) */ + break; + default: + break; } } if (USB_REQ_RECIP_INTERFACE == Udd_setup_recipient()) { // Standard Set Interface request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_SET_INTERFACE: - return udc_req_std_iface_set_setting(); - default: - break; + case USB_REQ_SET_INTERFACE: + return udc_req_std_iface_set_setting(); + default: + break; } } -#if (0!=USB_DEVICE_MAX_EP) +#if (0 != USB_DEVICE_MAX_EP) if (USB_REQ_RECIP_ENDPOINT == Udd_setup_recipient()) { // Standard Set Endpoint request switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_CLEAR_FEATURE: - return udc_req_std_ep_clear_feature(); - case USB_REQ_SET_FEATURE: - return udc_req_std_ep_set_feature(); - default: - break; + case USB_REQ_CLEAR_FEATURE: + return udc_req_std_ep_clear_feature(); + case USB_REQ_SET_FEATURE: + return udc_req_std_ep_set_feature(); + default: + break; } } #endif @@ -1044,13 +945,12 @@ static bool udc_reqstd(void) * * \return true if the request is supported */ -static bool udc_req_iface(void) -{ - uint8_t iface_num; +static bool udc_req_iface(void) { + uint8_t iface_num; udi_api_t UDC_DESC_STORAGE *udi_api; if (0 == udc_num_configuration) { - return false; // The device is not is configured state yet + return false; // The device is not is configured state yet } // Check interface number iface_num = udd_g_ctrlreq.req.wIndex & 0xFF; @@ -1079,18 +979,16 @@ static bool udc_req_iface(void) * * \return true if the request is supported */ -static bool udc_req_ep(void) -{ - uint8_t iface_num; +static bool udc_req_ep(void) { + uint8_t iface_num; udi_api_t UDC_DESC_STORAGE *udi_api; if (0 == udc_num_configuration) { - return false; // The device is not is configured state yet + return false; // The device is not is configured state yet } // Send this request on all enabled interfaces iface_num = udd_g_ctrlreq.req.wIndex & 0xFF; - for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; - iface_num++) { + for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces; iface_num++) { // Select the interface with the current alternate setting udi_api = udc_ptr_conf->udi_apis[iface_num]; if (!udc_update_iface_desc(iface_num, udi_api->getsetting())) { @@ -1118,16 +1016,15 @@ static bool udc_req_ep(void) * * \return true if the request is supported, else the request is stalled by UDD */ -bool udc_process_setup(void) -{ +bool udc_process_setup(void) { // By default no data (receive/send) and no callbacks registered - udd_g_ctrlreq.payload_size = 0; - udd_g_ctrlreq.callback = NULL; + udd_g_ctrlreq.payload_size = 0; + udd_g_ctrlreq.callback = NULL; udd_g_ctrlreq.over_under_run = NULL; if (Udd_setup_is_in()) { if (udd_g_ctrlreq.req.wLength == 0) { - return false; // Error from USB host + return false; // Error from USB host } } @@ -1155,7 +1052,7 @@ bool udc_process_setup(void) // Here SETUP request unknown by UDC and UDIs #ifdef USB_DEVICE_SPECIFIC_REQUEST // Try to decode it in specific callback - return USB_DEVICE_SPECIFIC_REQUEST(); // Ex: Vendor request,... + return USB_DEVICE_SPECIFIC_REQUEST(); // Ex: Vendor request,... #else return false; #endif diff --git a/tmk_core/protocol/arm_atsam/usb/udc.h b/tmk_core/protocol/arm_atsam/usb/udc.h index 33335d186..8d33307d3 100644 --- a/tmk_core/protocol/arm_atsam/usb/udc.h +++ b/tmk_core/protocol/arm_atsam/usb/udc.h @@ -53,11 +53,11 @@ #include "udd.h" #if USB_DEVICE_VENDOR_ID == 0 -# error USB_DEVICE_VENDOR_ID cannot be equal to 0 +# error USB_DEVICE_VENDOR_ID cannot be equal to 0 #endif #if USB_DEVICE_PRODUCT_ID == 0 -# error USB_DEVICE_PRODUCT_ID cannot be equal to 0 +# error USB_DEVICE_PRODUCT_ID cannot be equal to 0 #endif #ifdef __cplusplus @@ -172,10 +172,7 @@ extern "C" { } \endcode */ -static inline bool udc_include_vbus_monitoring(void) -{ - return udd_include_vbus_monitoring(); -} +static inline bool udc_include_vbus_monitoring(void) { return udd_include_vbus_monitoring(); } /*! \brief Start the USB Device stack */ @@ -192,28 +189,19 @@ void udc_stop(void); * then it will attach device when an acceptable Vbus * level from the host is detected. */ -static inline void udc_attach(void) -{ - udd_attach(); -} +static inline void udc_attach(void) { udd_attach(); } /** * \brief Detaches the device from the bus * * The driver must remove pull-up on USB line D- or D+. */ -static inline void udc_detach(void) -{ - udd_detach(); -} +static inline void udc_detach(void) { udd_detach(); } /*! \brief The USB driver sends a resume signal called \e "Upstream Resume" * This is authorized only when the remote wakeup feature is enabled by host. */ -inline void udc_remotewakeup(void) -{ - udd_send_remotewakeup(); -} +inline void udc_remotewakeup(void) { udd_send_remotewakeup(); } /** * \brief Returns a pointer on the current interface descriptor @@ -257,4 +245,4 @@ usb_iface_desc_t UDC_DESC_STORAGE *udc_get_interface_desc(void); } #endif -#endif // _UDC_H_ +#endif // _UDC_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udc_desc.h b/tmk_core/protocol/arm_atsam/usb/udc_desc.h index 9cab03dcb..39ea153b4 100644 --- a/tmk_core/protocol/arm_atsam/usb/udc_desc.h +++ b/tmk_core/protocol/arm_atsam/usb/udc_desc.h @@ -77,23 +77,21 @@ extern "C" { * * For Mega application used "code". */ -#define UDC_DESC_STORAGE - // Descriptor storage in internal RAM +#define UDC_DESC_STORAGE +// Descriptor storage in internal RAM #if (defined UDC_DATA_USE_HRAM_SUPPORT) -#if defined(__GNUC__) -#define UDC_DATA(x) COMPILER_WORD_ALIGNED __attribute__((__section__(".data_hram0"))) -#define UDC_BSS(x) COMPILER_ALIGNED(x) __attribute__((__section__(".bss_hram0"))) -#elif defined(__ICCAVR32__) -#define UDC_DATA(x) COMPILER_ALIGNED(x) __data32 -#define UDC_BSS(x) COMPILER_ALIGNED(x) __data32 -#endif +# if defined(__GNUC__) +# define UDC_DATA(x) COMPILER_WORD_ALIGNED __attribute__((__section__(".data_hram0"))) +# define UDC_BSS(x) COMPILER_ALIGNED(x) __attribute__((__section__(".bss_hram0"))) +# elif defined(__ICCAVR32__) +# define UDC_DATA(x) COMPILER_ALIGNED(x) __data32 +# define UDC_BSS(x) COMPILER_ALIGNED(x) __data32 +# endif #else -#define UDC_DATA(x) COMPILER_ALIGNED(x) -#define UDC_BSS(x) COMPILER_ALIGNED(x) +# define UDC_DATA(x) COMPILER_ALIGNED(x) +# define UDC_BSS(x) COMPILER_ALIGNED(x) #endif - - /** * \brief Configuration descriptor and UDI link for one USB speed */ @@ -101,10 +99,9 @@ typedef struct { //! USB configuration descriptor usb_conf_desc_t UDC_DESC_STORAGE *desc; //! Array of UDI API pointer - udi_api_t UDC_DESC_STORAGE *UDC_DESC_STORAGE * udi_apis; + udi_api_t UDC_DESC_STORAGE *UDC_DESC_STORAGE *udi_apis; } udc_config_speed_t; - /** * \brief All information about the USB Device */ @@ -132,4 +129,4 @@ extern UDC_DESC_STORAGE udc_config_t udc_config; #ifdef __cplusplus } #endif -#endif // _UDC_DESC_H_ +#endif // _UDC_DESC_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udd.h b/tmk_core/protocol/arm_atsam/usb/udd.h index b580e5847..805b761a7 100644 --- a/tmk_core/protocol/arm_atsam/usb/udd.h +++ b/tmk_core/protocol/arm_atsam/usb/udd.h @@ -71,7 +71,7 @@ typedef uint8_t udd_ep_id_t; //! \brief Endpoint transfer status //! Returned in parameters of callback register via udd_ep_run routine. typedef enum { - UDD_EP_TRANSFER_OK = 0, + UDD_EP_TRANSFER_OK = 0, UDD_EP_TRANSFER_ABORT = 1, } udd_ep_status_t; @@ -94,36 +94,32 @@ typedef struct { uint16_t payload_size; //! Callback called after reception of ZLP from setup request - void (*callback) (void); + void (*callback)(void); //! Callback called when the buffer given (.payload) is full or empty. //! This one return false to abort data transfer, or true with a new buffer in .payload. - bool(*over_under_run) (void); + bool (*over_under_run)(void); } udd_ctrl_request_t; extern udd_ctrl_request_t udd_g_ctrlreq; //! Return true if the setup request \a udd_g_ctrlreq indicates IN data transfer -#define Udd_setup_is_in() \ - (USB_REQ_DIR_IN == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK)) +#define Udd_setup_is_in() (USB_REQ_DIR_IN == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK)) //! Return true if the setup request \a udd_g_ctrlreq indicates OUT data transfer -#define Udd_setup_is_out() \ - (USB_REQ_DIR_OUT == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK)) +#define Udd_setup_is_out() (USB_REQ_DIR_OUT == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK)) //! Return the type of the SETUP request \a udd_g_ctrlreq. \see usb_reqtype. -#define Udd_setup_type() \ - (udd_g_ctrlreq.req.bmRequestType & USB_REQ_TYPE_MASK) +#define Udd_setup_type() (udd_g_ctrlreq.req.bmRequestType & USB_REQ_TYPE_MASK) //! Return the recipient of the SETUP request \a udd_g_ctrlreq. \see usb_recipient -#define Udd_setup_recipient() \ - (udd_g_ctrlreq.req.bmRequestType & USB_REQ_RECIP_MASK) +#define Udd_setup_recipient() (udd_g_ctrlreq.req.bmRequestType & USB_REQ_RECIP_MASK) /** * \brief End of halt callback function type. * Registered by routine udd_ep_wait_stall_clear() * Callback called when endpoint stall is cleared. */ -typedef void (*udd_callback_halt_cleared_t) (void); +typedef void (*udd_callback_halt_cleared_t)(void); /** * \brief End of transfer callback function type. @@ -134,8 +130,7 @@ typedef void (*udd_callback_halt_cleared_t) (void); * \param status UDD_EP_TRANSFER_ABORT, if transfer is aborted * \param n number of data transfered */ -typedef void (*udd_callback_trans_t) (udd_ep_status_t status, - iram_size_t nb_transfered, udd_ep_id_t ep); +typedef void (*udd_callback_trans_t)(udd_ep_status_t status, iram_size_t nb_transfered, udd_ep_id_t ep); /** * \brief Authorizes the VBUS event @@ -216,8 +211,7 @@ void udd_send_remotewakeup(void); * \param payload Pointer on payload * \param payload_size Size of payload */ -void udd_set_setup_payload( uint8_t *payload, uint16_t payload_size ); - +void udd_set_setup_payload(uint8_t *payload, uint16_t payload_size); /** * \name Endpoint Management @@ -239,8 +233,7 @@ void udd_set_setup_payload( uint8_t *payload, uint16_t payload_size ); * * \return \c 1 if the endpoint is enabled, otherwise \c 0. */ -bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes, - uint16_t MaxEndpointSize); +bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize); /** * \brief Disables an endpoint @@ -294,8 +287,7 @@ bool udd_ep_clear_halt(udd_ep_id_t ep); * * \return \c 1 if the register is accepted, otherwise \c 0. */ -bool udd_ep_wait_stall_clear(udd_ep_id_t ep, - udd_callback_halt_cleared_t callback); +bool udd_ep_wait_stall_clear(udd_ep_id_t ep, udd_callback_halt_cleared_t callback); /** * \brief Allows to receive or send data on an endpoint @@ -321,9 +313,7 @@ bool udd_ep_wait_stall_clear(udd_ep_id_t ep, * * \return \c 1 if function was successfully done, otherwise \c 0. */ -bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, - uint8_t *buf, iram_size_t buf_size, - udd_callback_trans_t callback); +bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t buf_size, udd_callback_trans_t callback); /** * \brief Aborts transfer on going on endpoint * @@ -339,7 +329,6 @@ void udd_ep_abort(udd_ep_id_t ep); //@} - /** * \name High speed test mode management * @@ -352,7 +341,6 @@ void udd_test_mode_se0_nak(void); void udd_test_mode_packet(void); //@} - /** * \name UDC callbacks to provide for UDD * @@ -393,4 +381,4 @@ extern void udc_sof_notify(void); #ifdef __cplusplus } #endif -#endif // _UDD_H_ +#endif // _UDD_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi.h b/tmk_core/protocol/arm_atsam/usb/udi.h index 9e4d4baf7..89942cce4 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi.h +++ b/tmk_core/protocol/arm_atsam/usb/udi.h @@ -72,57 +72,57 @@ extern "C" { * selected by UDC. */ typedef struct { - /** - * \brief Enable the interface. - * - * This function is called when the host selects a configuration - * to which this interface belongs through a Set Configuration - * request, and when the host selects an alternate setting of - * this interface through a Set Interface request. - * - * \return \c 1 if function was successfully done, otherwise \c 0. - */ - bool(*enable) (void); + /** + * \brief Enable the interface. + * + * This function is called when the host selects a configuration + * to which this interface belongs through a Set Configuration + * request, and when the host selects an alternate setting of + * this interface through a Set Interface request. + * + * \return \c 1 if function was successfully done, otherwise \c 0. + */ + bool (*enable)(void); - /** - * \brief Disable the interface. - * - * This function is called when this interface is currently - * active, and - * - the host selects any configuration through a Set - * Configuration request, or - * - the host issues a USB reset, or - * - the device is detached from the host (i.e. Vbus is no - * longer present) - */ - void (*disable) (void); + /** + * \brief Disable the interface. + * + * This function is called when this interface is currently + * active, and + * - the host selects any configuration through a Set + * Configuration request, or + * - the host issues a USB reset, or + * - the device is detached from the host (i.e. Vbus is no + * longer present) + */ + void (*disable)(void); - /** - * \brief Handle a control request directed at an interface. - * - * This function is called when this interface is currently - * active and the host sends a SETUP request - * with this interface as the recipient. - * - * Use udd_g_ctrlreq to decode and response to SETUP request. - * - * \return \c 1 if this interface supports the SETUP request, otherwise \c 0. - */ - bool(*setup) (void); + /** + * \brief Handle a control request directed at an interface. + * + * This function is called when this interface is currently + * active and the host sends a SETUP request + * with this interface as the recipient. + * + * Use udd_g_ctrlreq to decode and response to SETUP request. + * + * \return \c 1 if this interface supports the SETUP request, otherwise \c 0. + */ + bool (*setup)(void); - /** - * \brief Returns the current setting of the selected interface. - * - * This function is called when UDC when know alternate setting of selected interface. - * - * \return alternate setting of selected interface - */ - uint8_t(*getsetting) (void); + /** + * \brief Returns the current setting of the selected interface. + * + * This function is called when UDC when know alternate setting of selected interface. + * + * \return alternate setting of selected interface + */ + uint8_t (*getsetting)(void); - /** - * \brief To signal that a SOF is occurred - */ - void(*sof_notify) (void); + /** + * \brief To signal that a SOF is occurred + */ + void (*sof_notify)(void); } udi_api_t; //@} @@ -130,4 +130,4 @@ typedef struct { #ifdef __cplusplus } #endif -#endif // _UDI_H_ +#endif // _UDI_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_cdc.c b/tmk_core/protocol/arm_atsam/usb/udi_cdc.c index ffe3526db..8271f3b97 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_cdc.c +++ b/tmk_core/protocol/arm_atsam/usb/udi_cdc.c @@ -59,27 +59,27 @@ #ifdef CDC -#ifdef UDI_CDC_LOW_RATE -# ifdef USB_DEVICE_HS_SUPPORT -# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) -# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) -# else -# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_FS_SIZE) -# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_FS_SIZE) -# endif -#else -# ifdef USB_DEVICE_HS_SUPPORT -# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) -# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) -# else -# define UDI_CDC_TX_BUFFERS (5*UDI_CDC_DATA_EPS_FS_SIZE) -# define UDI_CDC_RX_BUFFERS (5*UDI_CDC_DATA_EPS_FS_SIZE) -# endif -#endif - -#ifndef UDI_CDC_TX_EMPTY_NOTIFY -# define UDI_CDC_TX_EMPTY_NOTIFY(port) -#endif +# ifdef UDI_CDC_LOW_RATE +# ifdef USB_DEVICE_HS_SUPPORT +# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) +# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) +# else +# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_FS_SIZE) +# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_FS_SIZE) +# endif +# else +# ifdef USB_DEVICE_HS_SUPPORT +# define UDI_CDC_TX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) +# define UDI_CDC_RX_BUFFERS (UDI_CDC_DATA_EPS_HS_SIZE) +# else +# define UDI_CDC_TX_BUFFERS (5 * UDI_CDC_DATA_EPS_FS_SIZE) +# define UDI_CDC_RX_BUFFERS (5 * UDI_CDC_DATA_EPS_FS_SIZE) +# endif +# endif + +# ifndef UDI_CDC_TX_EMPTY_NOTIFY +# define UDI_CDC_TX_EMPTY_NOTIFY(port) +# endif /** * \ingroup udi_cdc_group @@ -89,25 +89,19 @@ * * @{ */ -bool udi_cdc_comm_enable(void); -void udi_cdc_comm_disable(void); -bool udi_cdc_comm_setup(void); -bool udi_cdc_data_enable(void); -void udi_cdc_data_disable(void); -bool udi_cdc_data_setup(void); -uint8_t udi_cdc_getsetting(void); -void udi_cdc_data_sof_notify(void); -UDC_DESC_STORAGE udi_api_t udi_api_cdc_comm = { - .enable = udi_cdc_comm_enable, - .disable = udi_cdc_comm_disable, - .setup = udi_cdc_comm_setup, - .getsetting = udi_cdc_getsetting, - .sof_notify = NULL -}; +bool udi_cdc_comm_enable(void); +void udi_cdc_comm_disable(void); +bool udi_cdc_comm_setup(void); +bool udi_cdc_data_enable(void); +void udi_cdc_data_disable(void); +bool udi_cdc_data_setup(void); +uint8_t udi_cdc_getsetting(void); +void udi_cdc_data_sof_notify(void); +UDC_DESC_STORAGE udi_api_t udi_api_cdc_comm = {.enable = udi_cdc_comm_enable, .disable = udi_cdc_comm_disable, .setup = udi_cdc_comm_setup, .getsetting = udi_cdc_getsetting, .sof_notify = NULL}; UDC_DESC_STORAGE udi_api_t udi_api_cdc_data = { - .enable = udi_cdc_data_enable, - .disable = udi_cdc_data_disable, - .setup = udi_cdc_data_setup, + .enable = udi_cdc_data_enable, + .disable = udi_cdc_data_disable, + .setup = udi_cdc_data_setup, .getsetting = udi_cdc_getsetting, .sof_notify = udi_cdc_data_sof_notify, }; @@ -226,9 +220,9 @@ static void udi_cdc_tx_send(uint8_t port); */ //@{ COMPILER_WORD_ALIGNED -static usb_cdc_line_coding_t udi_cdc_line_coding[UDI_CDC_PORT_NB]; -static bool udi_cdc_serial_state_msg_ongoing[UDI_CDC_PORT_NB]; -static volatile le16_t udi_cdc_state[UDI_CDC_PORT_NB]; +static usb_cdc_line_coding_t udi_cdc_line_coding[UDI_CDC_PORT_NB]; +static bool udi_cdc_serial_state_msg_ongoing[UDI_CDC_PORT_NB]; +static volatile le16_t udi_cdc_state[UDI_CDC_PORT_NB]; COMPILER_WORD_ALIGNED static usb_cdc_notify_serial_state_t uid_cdc_state_msg[UDI_CDC_PORT_NB]; //! Status of CDC COMM interfaces @@ -243,7 +237,7 @@ static volatile uint8_t udi_cdc_nb_comm_enabled = 0; //! Status of CDC DATA interfaces static volatile uint8_t udi_cdc_nb_data_enabled = 0; -static volatile bool udi_cdc_data_running = false; +static volatile bool udi_cdc_data_running = false; //! Buffer to receive data COMPILER_WORD_ALIGNED static uint8_t udi_cdc_rx_buf[UDI_CDC_PORT_NB][2][UDI_CDC_RX_BUFFERS]; //! Data available in RX buffers @@ -256,7 +250,7 @@ static volatile uint16_t udi_cdc_rx_pos[UDI_CDC_PORT_NB]; static volatile bool udi_cdc_rx_trans_ongoing[UDI_CDC_PORT_NB]; //! Define a transfer halted -#define UDI_CDC_TRANS_HALTED 2 +# define UDI_CDC_TRANS_HALTED 2 //! Buffer to send data COMPILER_WORD_ALIGNED static uint8_t udi_cdc_tx_buf[UDI_CDC_PORT_NB][2][UDI_CDC_TX_BUFFERS]; @@ -273,29 +267,26 @@ static volatile bool udi_cdc_tx_both_buf_to_send[UDI_CDC_PORT_NB]; //@} -bool udi_cdc_comm_enable(void) -{ +bool udi_cdc_comm_enable(void) { uint8_t port; uint8_t iface_comm_num; -//#if UDI_CDC_PORT_NB == 1 // To optimize code - port = 0; + //#if UDI_CDC_PORT_NB == 1 // To optimize code + port = 0; udi_cdc_nb_comm_enabled = 0; -//#else -// if (udi_cdc_nb_comm_enabled > UDI_CDC_PORT_NB) { -// udi_cdc_nb_comm_enabled = 0; -// } -// port = udi_cdc_nb_comm_enabled; -//#endif + //#else + // if (udi_cdc_nb_comm_enabled > UDI_CDC_PORT_NB) { + // udi_cdc_nb_comm_enabled = 0; + // } + // port = udi_cdc_nb_comm_enabled; + //#endif // Initialize control signal management udi_cdc_state[port] = CPU_TO_LE16(0); - uid_cdc_state_msg[port].header.bmRequestType = - USB_REQ_DIR_IN | USB_REQ_TYPE_CLASS | - USB_REQ_RECIP_INTERFACE; + uid_cdc_state_msg[port].header.bmRequestType = USB_REQ_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_RECIP_INTERFACE; uid_cdc_state_msg[port].header.bNotification = USB_REQ_CDC_NOTIFY_SERIAL_STATE; - uid_cdc_state_msg[port].header.wValue = LE16(0); + uid_cdc_state_msg[port].header.wValue = LE16(0); /* switch (port) { @@ -312,55 +303,54 @@ bool udi_cdc_comm_enable(void) */ iface_comm_num = UDI_CDC_COMM_IFACE_NUMBER_0; - uid_cdc_state_msg[port].header.wIndex = LE16(iface_comm_num); + uid_cdc_state_msg[port].header.wIndex = LE16(iface_comm_num); uid_cdc_state_msg[port].header.wLength = LE16(2); - uid_cdc_state_msg[port].value = CPU_TO_LE16(0); + uid_cdc_state_msg[port].value = CPU_TO_LE16(0); - udi_cdc_line_coding[port].dwDTERate = CPU_TO_LE32(UDI_CDC_DEFAULT_RATE); + udi_cdc_line_coding[port].dwDTERate = CPU_TO_LE32(UDI_CDC_DEFAULT_RATE); udi_cdc_line_coding[port].bCharFormat = UDI_CDC_DEFAULT_STOPBITS; udi_cdc_line_coding[port].bParityType = UDI_CDC_DEFAULT_PARITY; - udi_cdc_line_coding[port].bDataBits = UDI_CDC_DEFAULT_DATABITS; + udi_cdc_line_coding[port].bDataBits = UDI_CDC_DEFAULT_DATABITS; // Call application callback // to initialize memories or indicate that interface is enabled -#if 0 +# if 0 UDI_CDC_SET_CODING_EXT(port,(&udi_cdc_line_coding[port])); if (!UDI_CDC_ENABLE_EXT(port)) { return false; } -#endif +# endif udi_cdc_nb_comm_enabled++; return true; } -bool udi_cdc_data_enable(void) -{ +bool udi_cdc_data_enable(void) { uint8_t port; -//#if UDI_CDC_PORT_NB == 1 // To optimize code - port = 0; + //#if UDI_CDC_PORT_NB == 1 // To optimize code + port = 0; udi_cdc_nb_data_enabled = 0; -//#else -// if (udi_cdc_nb_data_enabled > UDI_CDC_PORT_NB) { -// udi_cdc_nb_data_enabled = 0; -// } -// port = udi_cdc_nb_data_enabled; -//#endif + //#else + // if (udi_cdc_nb_data_enabled > UDI_CDC_PORT_NB) { + // udi_cdc_nb_data_enabled = 0; + // } + // port = udi_cdc_nb_data_enabled; + //#endif // Initialize TX management - udi_cdc_tx_trans_ongoing[port] = false; + udi_cdc_tx_trans_ongoing[port] = false; udi_cdc_tx_both_buf_to_send[port] = false; - udi_cdc_tx_buf_sel[port] = 0; - udi_cdc_tx_buf_nb[port][0] = 0; - udi_cdc_tx_buf_nb[port][1] = 0; - udi_cdc_tx_sof_num[port] = 0; + udi_cdc_tx_buf_sel[port] = 0; + udi_cdc_tx_buf_nb[port][0] = 0; + udi_cdc_tx_buf_nb[port][1] = 0; + udi_cdc_tx_sof_num[port] = 0; udi_cdc_tx_send(port); // Initialize RX management udi_cdc_rx_trans_ongoing[port] = false; - udi_cdc_rx_buf_sel[port] = 0; - udi_cdc_rx_buf_nb[port][0] = 0; - udi_cdc_rx_buf_nb[port][1] = 0; - udi_cdc_rx_pos[port] = 0; + udi_cdc_rx_buf_sel[port] = 0; + udi_cdc_rx_buf_nb[port][0] = 0; + udi_cdc_rx_buf_nb[port][1] = 0; + udi_cdc_rx_pos[port] = 0; if (!udi_cdc_rx_start(port)) { return false; } @@ -371,25 +361,22 @@ bool udi_cdc_data_enable(void) return true; } -void udi_cdc_comm_disable(void) -{ +void udi_cdc_comm_disable(void) { Assert(udi_cdc_nb_comm_enabled != 0); udi_cdc_nb_comm_enabled--; } -void udi_cdc_data_disable(void) -{ -// uint8_t port; +void udi_cdc_data_disable(void) { + // uint8_t port; Assert(udi_cdc_nb_data_enabled != 0); udi_cdc_nb_data_enabled--; -// port = udi_cdc_nb_data_enabled; -// UDI_CDC_DISABLE_EXT(port); + // port = udi_cdc_nb_data_enabled; + // UDI_CDC_DISABLE_EXT(port); udi_cdc_data_running = false; } -bool udi_cdc_comm_setup(void) -{ +bool udi_cdc_comm_setup(void) { uint8_t port = udi_cdc_setup_to_port(); if (Udd_setup_is_in()) { @@ -397,17 +384,12 @@ bool udi_cdc_comm_setup(void) if (Udd_setup_type() == USB_REQ_TYPE_CLASS) { // Requests Class Interface Get switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_CDC_GET_LINE_CODING: - // Get configuration of CDC line - if (sizeof(usb_cdc_line_coding_t) != - udd_g_ctrlreq.req.wLength) - return false; // Error for USB host - udd_g_ctrlreq.payload = - (uint8_t *) & - udi_cdc_line_coding[port]; - udd_g_ctrlreq.payload_size = - sizeof(usb_cdc_line_coding_t); - return true; + case USB_REQ_CDC_GET_LINE_CODING: + // Get configuration of CDC line + if (sizeof(usb_cdc_line_coding_t) != udd_g_ctrlreq.req.wLength) return false; // Error for USB host + udd_g_ctrlreq.payload = (uint8_t *)&udi_cdc_line_coding[port]; + udd_g_ctrlreq.payload_size = sizeof(usb_cdc_line_coding_t); + return true; } } } @@ -416,46 +398,37 @@ bool udi_cdc_comm_setup(void) if (Udd_setup_type() == USB_REQ_TYPE_CLASS) { // Requests Class Interface Set switch (udd_g_ctrlreq.req.bRequest) { - case USB_REQ_CDC_SET_LINE_CODING: - // Change configuration of CDC line - if (sizeof(usb_cdc_line_coding_t) != - udd_g_ctrlreq.req.wLength) - return false; // Error for USB host - udd_g_ctrlreq.callback = - udi_cdc_line_coding_received; - udd_g_ctrlreq.payload = - (uint8_t *) & - udi_cdc_line_coding[port]; - udd_g_ctrlreq.payload_size = - sizeof(usb_cdc_line_coding_t); - return true; - case USB_REQ_CDC_SET_CONTROL_LINE_STATE: - // According cdc spec 1.1 chapter 6.2.14 -// UDI_CDC_SET_DTR_EXT(port, (0 != -// (udd_g_ctrlreq.req.wValue -// & CDC_CTRL_SIGNAL_DTE_PRESENT))); -// UDI_CDC_SET_RTS_EXT(port, (0 != -// (udd_g_ctrlreq.req.wValue -// & CDC_CTRL_SIGNAL_ACTIVATE_CARRIER))); - return true; + case USB_REQ_CDC_SET_LINE_CODING: + // Change configuration of CDC line + if (sizeof(usb_cdc_line_coding_t) != udd_g_ctrlreq.req.wLength) return false; // Error for USB host + udd_g_ctrlreq.callback = udi_cdc_line_coding_received; + udd_g_ctrlreq.payload = (uint8_t *)&udi_cdc_line_coding[port]; + udd_g_ctrlreq.payload_size = sizeof(usb_cdc_line_coding_t); + return true; + case USB_REQ_CDC_SET_CONTROL_LINE_STATE: + // According cdc spec 1.1 chapter 6.2.14 + // UDI_CDC_SET_DTR_EXT(port, (0 != + // (udd_g_ctrlreq.req.wValue + // & CDC_CTRL_SIGNAL_DTE_PRESENT))); + // UDI_CDC_SET_RTS_EXT(port, (0 != + // (udd_g_ctrlreq.req.wValue + // & CDC_CTRL_SIGNAL_ACTIVATE_CARRIER))); + return true; } } } return false; // request Not supported } -bool udi_cdc_data_setup(void) -{ +bool udi_cdc_data_setup(void) { return false; // request Not supported } -uint8_t udi_cdc_getsetting(void) -{ - return 0; // CDC don't have multiple alternate setting +uint8_t udi_cdc_getsetting(void) { + return 0; // CDC don't have multiple alternate setting } -void udi_cdc_data_sof_notify(void) -{ +void udi_cdc_data_sof_notify(void) { static uint8_t port_notify = 0; // A call of udi_cdc_data_sof_notify() is done for each port @@ -470,12 +443,10 @@ void udi_cdc_data_sof_notify(void) */ } - //------------------------------------------------- //------- Internal routines to control serial line -static uint8_t udi_cdc_setup_to_port(void) -{ +static uint8_t udi_cdc_setup_to_port(void) { uint8_t port; /* @@ -496,35 +467,32 @@ static uint8_t udi_cdc_setup_to_port(void) return port; } -static void udi_cdc_line_coding_received(void) -{ +static void udi_cdc_line_coding_received(void) { uint8_t port = udi_cdc_setup_to_port(); UNUSED(port); -// UDI_CDC_SET_CODING_EXT(port, (&udi_cdc_line_coding[port])); + // UDI_CDC_SET_CODING_EXT(port, (&udi_cdc_line_coding[port])); } -static void udi_cdc_ctrl_state_change(uint8_t port, bool b_set, le16_t bit_mask) -{ +static void udi_cdc_ctrl_state_change(uint8_t port, bool b_set, le16_t bit_mask) { udd_ep_id_t ep_comm; - uint32_t irqflags; //irqflags_t - + uint32_t irqflags; // irqflags_t -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif // Update state - irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); + irqflags = __get_PRIMASK(); + __disable_irq(); + __DMB(); if (b_set) { udi_cdc_state[port] |= bit_mask; } else { udi_cdc_state[port] &= ~(unsigned)bit_mask; } __DMB(); - __set_PRIMASK(irqflags); + __set_PRIMASK(irqflags); /* // Send it if possible and state changed @@ -545,31 +513,21 @@ static void udi_cdc_ctrl_state_change(uint8_t port, bool b_set, le16_t bit_mask) udi_cdc_ctrl_state_notify(port, ep_comm); } - -static void udi_cdc_ctrl_state_notify(uint8_t port, udd_ep_id_t ep) -{ -#if UDI_CDC_PORT_NB == 1 // To optimize code +static void udi_cdc_ctrl_state_notify(uint8_t port, udd_ep_id_t ep) { +# if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -#endif +# endif // Send it if possible and state changed - if ((!udi_cdc_serial_state_msg_ongoing[port]) - && (udi_cdc_state[port] != uid_cdc_state_msg[port].value)) { + if ((!udi_cdc_serial_state_msg_ongoing[port]) && (udi_cdc_state[port] != uid_cdc_state_msg[port].value)) { // Fill notification message uid_cdc_state_msg[port].value = udi_cdc_state[port]; // Send notification message - udi_cdc_serial_state_msg_ongoing[port] = - udd_ep_run(ep, - false, - (uint8_t *) & uid_cdc_state_msg[port], - sizeof(uid_cdc_state_msg[0]), - udi_cdc_serial_state_msg_sent); + udi_cdc_serial_state_msg_ongoing[port] = udd_ep_run(ep, false, (uint8_t *)&uid_cdc_state_msg[port], sizeof(uid_cdc_state_msg[0]), udi_cdc_serial_state_msg_sent); } } - -static void udi_cdc_serial_state_msg_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) -{ +static void udi_cdc_serial_state_msg_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) { uint8_t port; UNUSED(n); UNUSED(status); @@ -594,14 +552,8 @@ static void udi_cdc_serial_state_msg_sent(udd_ep_status_t status, iram_size_t n, // For the irregular signals like break, the incoming ring signal, // or the overrun error state, this will reset their values to zero // and again will not send another notification until their state changes. - udi_cdc_state[port] &= ~(CDC_SERIAL_STATE_BREAK | - CDC_SERIAL_STATE_RING | - CDC_SERIAL_STATE_FRAMING | - CDC_SERIAL_STATE_PARITY | CDC_SERIAL_STATE_OVERRUN); - uid_cdc_state_msg[port].value &= ~(CDC_SERIAL_STATE_BREAK | - CDC_SERIAL_STATE_RING | - CDC_SERIAL_STATE_FRAMING | - CDC_SERIAL_STATE_PARITY | CDC_SERIAL_STATE_OVERRUN); + udi_cdc_state[port] &= ~(CDC_SERIAL_STATE_BREAK | CDC_SERIAL_STATE_RING | CDC_SERIAL_STATE_FRAMING | CDC_SERIAL_STATE_PARITY | CDC_SERIAL_STATE_OVERRUN); + uid_cdc_state_msg[port].value &= ~(CDC_SERIAL_STATE_BREAK | CDC_SERIAL_STATE_RING | CDC_SERIAL_STATE_FRAMING | CDC_SERIAL_STATE_PARITY | CDC_SERIAL_STATE_OVERRUN); // Send it if possible and state changed udi_cdc_ctrl_state_notify(port, ep); } @@ -609,39 +561,37 @@ static void udi_cdc_serial_state_msg_sent(udd_ep_status_t status, iram_size_t n, //------------------------------------------------- //------- Internal routines to process data transfer -static bool udi_cdc_rx_start(uint8_t port) -{ - uint32_t irqflags; //irqflags_t - uint8_t buf_sel_trans; +static bool udi_cdc_rx_start(uint8_t port) { + uint32_t irqflags; // irqflags_t + uint8_t buf_sel_trans; udd_ep_id_t ep; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); + __disable_irq(); + __DMB(); buf_sel_trans = udi_cdc_rx_buf_sel[port]; - if (udi_cdc_rx_trans_ongoing[port] || - (udi_cdc_rx_pos[port] < udi_cdc_rx_buf_nb[port][buf_sel_trans])) { + if (udi_cdc_rx_trans_ongoing[port] || (udi_cdc_rx_pos[port] < udi_cdc_rx_buf_nb[port][buf_sel_trans])) { // Transfer already on-going or current buffer no empty - __DMB(); - __set_PRIMASK(irqflags); + __DMB(); + __set_PRIMASK(irqflags); return false; } // Change current buffer - udi_cdc_rx_pos[port] = 0; - udi_cdc_rx_buf_sel[port] = (buf_sel_trans==0)?1:0; + udi_cdc_rx_pos[port] = 0; + udi_cdc_rx_buf_sel[port] = (buf_sel_trans == 0) ? 1 : 0; // Start transfer on RX udi_cdc_rx_trans_ongoing[port] = true; - __DMB(); - __set_PRIMASK(irqflags); + __DMB(); + __set_PRIMASK(irqflags); if (udi_cdc_multi_is_rx_ready(port)) { -// UDI_CDC_RX_NOTIFY(port); + // UDI_CDC_RX_NOTIFY(port); } /* @@ -660,15 +610,10 @@ static bool udi_cdc_rx_start(uint8_t port) */ ep = UDI_CDC_DATA_EP_OUT_0; - return udd_ep_run(ep, - true, - udi_cdc_rx_buf[port][buf_sel_trans], - UDI_CDC_RX_BUFFERS, - udi_cdc_data_received); + return udd_ep_run(ep, true, udi_cdc_rx_buf[port][buf_sel_trans], UDI_CDC_RX_BUFFERS, udi_cdc_data_received); } -static void udi_cdc_data_received(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) -{ +static void udi_cdc_data_received(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) { uint8_t buf_sel_trans; uint8_t port; @@ -692,24 +637,19 @@ static void udi_cdc_data_received(udd_ep_status_t status, iram_size_t n, udd_ep_ return; } - buf_sel_trans = (udi_cdc_rx_buf_sel[port]==0)?1:0; + buf_sel_trans = (udi_cdc_rx_buf_sel[port] == 0) ? 1 : 0; if (!n) { - udd_ep_run( ep, - true, - udi_cdc_rx_buf[port][buf_sel_trans], - UDI_CDC_RX_BUFFERS, - udi_cdc_data_received); + udd_ep_run(ep, true, udi_cdc_rx_buf[port][buf_sel_trans], UDI_CDC_RX_BUFFERS, udi_cdc_data_received); return; } udi_cdc_rx_buf_nb[port][buf_sel_trans] = n; - udi_cdc_rx_trans_ongoing[port] = false; + udi_cdc_rx_trans_ongoing[port] = false; udi_cdc_rx_start(port); } -static void udi_cdc_data_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) -{ +static void udi_cdc_data_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep) { uint8_t port; UNUSED(n); @@ -733,9 +673,9 @@ static void udi_cdc_data_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t return; } - udi_cdc_tx_buf_nb[port][(udi_cdc_tx_buf_sel[port]==0)?1:0] = 0; - udi_cdc_tx_both_buf_to_send[port] = false; - udi_cdc_tx_trans_ongoing[port] = false; + udi_cdc_tx_buf_nb[port][(udi_cdc_tx_buf_sel[port] == 0) ? 1 : 0] = 0; + udi_cdc_tx_both_buf_to_send[port] = false; + udi_cdc_tx_trans_ongoing[port] = false; if (n != 0) { UDI_CDC_TX_EMPTY_NOTIFY(port); @@ -744,41 +684,39 @@ static void udi_cdc_data_sent(udd_ep_status_t status, iram_size_t n, udd_ep_id_t udi_cdc_tx_send(port); } -static void udi_cdc_tx_send(uint8_t port) -{ - uint32_t irqflags; //irqflags_t - uint8_t buf_sel_trans; - bool b_short_packet; - udd_ep_id_t ep; +static void udi_cdc_tx_send(uint8_t port) { + uint32_t irqflags; // irqflags_t + uint8_t buf_sel_trans; + bool b_short_packet; + udd_ep_id_t ep; static uint16_t sof_zlp_counter = 0; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif if (udi_cdc_tx_trans_ongoing[port]) { - return; // Already on going or wait next SOF to send next data + return; // Already on going or wait next SOF to send next data } if (udd_is_high_speed()) { if (udi_cdc_tx_sof_num[port] == udd_get_micro_frame_number()) { - return; // Wait next SOF to send next data + return; // Wait next SOF to send next data } - }else{ + } else { if (udi_cdc_tx_sof_num[port] == udd_get_frame_number()) { - return; // Wait next SOF to send next data + return; // Wait next SOF to send next data } } irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); + __disable_irq(); + __DMB(); buf_sel_trans = udi_cdc_tx_buf_sel[port]; if (udi_cdc_tx_buf_nb[port][buf_sel_trans] == 0) { sof_zlp_counter++; - if (((!udd_is_high_speed()) && (sof_zlp_counter < 100)) - || (udd_is_high_speed() && (sof_zlp_counter < 800))) { - __DMB(); - __set_PRIMASK(irqflags); + if (((!udd_is_high_speed()) && (sof_zlp_counter < 100)) || (udd_is_high_speed() && (sof_zlp_counter < 800))) { + __DMB(); + __set_PRIMASK(irqflags); return; } } @@ -787,25 +725,25 @@ static void udi_cdc_tx_send(uint8_t port) if (!udi_cdc_tx_both_buf_to_send[port]) { // Send current Buffer // and switch the current buffer - udi_cdc_tx_buf_sel[port] = (buf_sel_trans==0)?1:0; - }else{ + udi_cdc_tx_buf_sel[port] = (buf_sel_trans == 0) ? 1 : 0; + } else { // Send the other Buffer // and no switch the current buffer - buf_sel_trans = (buf_sel_trans==0)?1:0; + buf_sel_trans = (buf_sel_trans == 0) ? 1 : 0; } udi_cdc_tx_trans_ongoing[port] = true; - __DMB(); - __set_PRIMASK(irqflags); + __DMB(); + __set_PRIMASK(irqflags); b_short_packet = (udi_cdc_tx_buf_nb[port][buf_sel_trans] != UDI_CDC_TX_BUFFERS); if (b_short_packet) { if (udd_is_high_speed()) { udi_cdc_tx_sof_num[port] = udd_get_micro_frame_number(); - }else{ + } else { udi_cdc_tx_sof_num[port] = udd_get_frame_number(); } - }else{ - udi_cdc_tx_sof_num[port] = 0; // Force next transfer without wait SOF + } else { + udi_cdc_tx_sof_num[port] = 0; // Force next transfer without wait SOF } /* @@ -824,126 +762,81 @@ static void udi_cdc_tx_send(uint8_t port) */ ep = UDI_CDC_DATA_EP_IN_0; - udd_ep_run( ep, - b_short_packet, - udi_cdc_tx_buf[port][buf_sel_trans], - udi_cdc_tx_buf_nb[port][buf_sel_trans], - udi_cdc_data_sent); + udd_ep_run(ep, b_short_packet, udi_cdc_tx_buf[port][buf_sel_trans], udi_cdc_tx_buf_nb[port][buf_sel_trans], udi_cdc_data_sent); } //--------------------------------------------- //------- Application interface -void udi_cdc_ctrl_signal_dcd(bool b_set) -{ - udi_cdc_ctrl_state_change(0, b_set, CDC_SERIAL_STATE_DCD); -} +void udi_cdc_ctrl_signal_dcd(bool b_set) { udi_cdc_ctrl_state_change(0, b_set, CDC_SERIAL_STATE_DCD); } -void udi_cdc_ctrl_signal_dsr(bool b_set) -{ - udi_cdc_ctrl_state_change(0, b_set, CDC_SERIAL_STATE_DSR); -} +void udi_cdc_ctrl_signal_dsr(bool b_set) { udi_cdc_ctrl_state_change(0, b_set, CDC_SERIAL_STATE_DSR); } -void udi_cdc_signal_framing_error(void) -{ - udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_FRAMING); -} +void udi_cdc_signal_framing_error(void) { udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_FRAMING); } -void udi_cdc_signal_parity_error(void) -{ - udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_PARITY); -} +void udi_cdc_signal_parity_error(void) { udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_PARITY); } -void udi_cdc_signal_overrun(void) -{ - udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_OVERRUN); -} +void udi_cdc_signal_overrun(void) { udi_cdc_ctrl_state_change(0, true, CDC_SERIAL_STATE_OVERRUN); } -void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set) -{ - udi_cdc_ctrl_state_change(port, b_set, CDC_SERIAL_STATE_DCD); -} +void udi_cdc_multi_ctrl_signal_dcd(uint8_t port, bool b_set) { udi_cdc_ctrl_state_change(port, b_set, CDC_SERIAL_STATE_DCD); } -void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set) -{ - udi_cdc_ctrl_state_change(port, b_set, CDC_SERIAL_STATE_DSR); -} +void udi_cdc_multi_ctrl_signal_dsr(uint8_t port, bool b_set) { udi_cdc_ctrl_state_change(port, b_set, CDC_SERIAL_STATE_DSR); } -void udi_cdc_multi_signal_framing_error(uint8_t port) -{ - udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_FRAMING); -} +void udi_cdc_multi_signal_framing_error(uint8_t port) { udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_FRAMING); } -void udi_cdc_multi_signal_parity_error(uint8_t port) -{ - udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_PARITY); -} +void udi_cdc_multi_signal_parity_error(uint8_t port) { udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_PARITY); } -void udi_cdc_multi_signal_overrun(uint8_t port) -{ - udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_OVERRUN); -} +void udi_cdc_multi_signal_overrun(uint8_t port) { udi_cdc_ctrl_state_change(port, true, CDC_SERIAL_STATE_OVERRUN); } -iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port) -{ - uint32_t irqflags; //irqflags_t - uint16_t pos; +iram_size_t udi_cdc_multi_get_nb_received_data(uint8_t port) { + uint32_t irqflags; // irqflags_t + uint16_t pos; iram_size_t nb_received; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); - pos = udi_cdc_rx_pos[port]; + __disable_irq(); + __DMB(); + pos = udi_cdc_rx_pos[port]; nb_received = udi_cdc_rx_buf_nb[port][udi_cdc_rx_buf_sel[port]] - pos; __DMB(); - __set_PRIMASK(irqflags); + __set_PRIMASK(irqflags); return nb_received; } -iram_size_t udi_cdc_get_nb_received_data(void) -{ - return udi_cdc_multi_get_nb_received_data(0); -} +iram_size_t udi_cdc_get_nb_received_data(void) { return udi_cdc_multi_get_nb_received_data(0); } -bool udi_cdc_multi_is_rx_ready(uint8_t port) -{ - return (udi_cdc_multi_get_nb_received_data(port) > 0); -} +bool udi_cdc_multi_is_rx_ready(uint8_t port) { return (udi_cdc_multi_get_nb_received_data(port) > 0); } -bool udi_cdc_is_rx_ready(void) -{ - return udi_cdc_multi_is_rx_ready(0); -} +bool udi_cdc_is_rx_ready(void) { return udi_cdc_multi_is_rx_ready(0); } -int udi_cdc_multi_getc(uint8_t port) -{ - uint32_t irqflags; //irqflags_t - int rx_data = 0; - bool b_databit_9; +int udi_cdc_multi_getc(uint8_t port) { + uint32_t irqflags; // irqflags_t + int rx_data = 0; + bool b_databit_9; uint16_t pos; - uint8_t buf_sel; - bool again; + uint8_t buf_sel; + bool again; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif b_databit_9 = (9 == udi_cdc_line_coding[port].bDataBits); udi_cdc_getc_process_one_byte: // Check available data irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); - pos = udi_cdc_rx_pos[port]; + __disable_irq(); + __DMB(); + pos = udi_cdc_rx_pos[port]; buf_sel = udi_cdc_rx_buf_sel[port]; - again = pos >= udi_cdc_rx_buf_nb[port][buf_sel]; + again = pos >= udi_cdc_rx_buf_nb[port][buf_sel]; __DMB(); - __set_PRIMASK(irqflags); + __set_PRIMASK(irqflags); while (again) { if (!udi_cdc_data_running) { return 0; @@ -953,46 +846,43 @@ udi_cdc_getc_process_one_byte: // Read data rx_data |= udi_cdc_rx_buf[port][buf_sel][pos]; - udi_cdc_rx_pos[port] = pos+1; + udi_cdc_rx_pos[port] = pos + 1; udi_cdc_rx_start(port); if (b_databit_9) { // Receive MSB b_databit_9 = false; - rx_data = rx_data << 8; + rx_data = rx_data << 8; goto udi_cdc_getc_process_one_byte; } return rx_data; } -int udi_cdc_getc(void) -{ - return udi_cdc_multi_getc(0); -} +int udi_cdc_getc(void) { return udi_cdc_multi_getc(0); } -iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size) -{ - uint32_t irqflags; //irqflags_t - uint8_t *ptr_buf = (uint8_t *)buf; +iram_size_t udi_cdc_multi_read_buf(uint8_t port, void *buf, iram_size_t size) { + uint32_t irqflags; // irqflags_t + uint8_t * ptr_buf = (uint8_t *)buf; iram_size_t copy_nb; - uint16_t pos; - uint8_t buf_sel; - bool again; + uint16_t pos; + uint8_t buf_sel; + bool again; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif udi_cdc_read_buf_loop_wait: // Check available data irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); pos = udi_cdc_rx_pos[port]; + __disable_irq(); + __DMB(); + pos = udi_cdc_rx_pos[port]; buf_sel = udi_cdc_rx_buf_sel[port]; - again = pos >= udi_cdc_rx_buf_nb[port][buf_sel]; + again = pos >= udi_cdc_rx_buf_nb[port][buf_sel]; __DMB(); - __set_PRIMASK(irqflags); + __set_PRIMASK(irqflags); while (again) { if (!udi_cdc_data_running) { return size; @@ -1002,7 +892,7 @@ udi_cdc_read_buf_loop_wait: // Read data copy_nb = udi_cdc_rx_buf_nb[port][buf_sel] - pos; - if (copy_nb>size) { + if (copy_nb > size) { copy_nb = size; } memcpy(ptr_buf, &udi_cdc_rx_buf[port][buf_sel][pos], copy_nb); @@ -1017,118 +907,99 @@ udi_cdc_read_buf_loop_wait: return 0; } -static iram_size_t udi_cdc_multi_read_no_polling(uint8_t port, void* buf, iram_size_t size) -{ - uint8_t *ptr_buf = (uint8_t *)buf; +static iram_size_t udi_cdc_multi_read_no_polling(uint8_t port, void *buf, iram_size_t size) { + uint8_t * ptr_buf = (uint8_t *)buf; iram_size_t nb_avail_data; - uint16_t pos; - uint8_t buf_sel; - uint32_t irqflags; //irqflags_t + uint16_t pos; + uint8_t buf_sel; + uint32_t irqflags; // irqflags_t -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif - //Data interface not started... exit + // Data interface not started... exit if (!udi_cdc_data_running) { return 0; } - //Get number of available data + // Get number of available data // Check available data irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); - pos = udi_cdc_rx_pos[port]; - buf_sel = udi_cdc_rx_buf_sel[port]; + __disable_irq(); + __DMB(); + pos = udi_cdc_rx_pos[port]; + buf_sel = udi_cdc_rx_buf_sel[port]; nb_avail_data = udi_cdc_rx_buf_nb[port][buf_sel] - pos; __DMB(); - __set_PRIMASK(irqflags); - //If the buffer contains less than the requested number of data, - //adjust read size - if(nb_avail_data<size) { + __set_PRIMASK(irqflags); + // If the buffer contains less than the requested number of data, + // adjust read size + if (nb_avail_data < size) { size = nb_avail_data; } - if(size>0) { + if (size > 0) { memcpy(ptr_buf, &udi_cdc_rx_buf[port][buf_sel][pos], size); - irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); + irqflags = __get_PRIMASK(); + __disable_irq(); + __DMB(); udi_cdc_rx_pos[port] += size; - __DMB(); - __set_PRIMASK(irqflags); + __DMB(); + __set_PRIMASK(irqflags); ptr_buf += size; udi_cdc_rx_start(port); } - return(nb_avail_data); + return (nb_avail_data); } -iram_size_t udi_cdc_read_no_polling(void* buf, iram_size_t size) -{ - return udi_cdc_multi_read_no_polling(0, buf, size); -} +iram_size_t udi_cdc_read_no_polling(void *buf, iram_size_t size) { return udi_cdc_multi_read_no_polling(0, buf, size); } -iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size) -{ - return udi_cdc_multi_read_buf(0, buf, size); -} +iram_size_t udi_cdc_read_buf(void *buf, iram_size_t size) { return udi_cdc_multi_read_buf(0, buf, size); } -iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port) -{ - uint32_t irqflags; //irqflags_t +iram_size_t udi_cdc_multi_get_free_tx_buffer(uint8_t port) { + uint32_t irqflags; // irqflags_t iram_size_t buf_sel_nb, retval; - uint8_t buf_sel; + uint8_t buf_sel; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); - buf_sel = udi_cdc_tx_buf_sel[port]; + __disable_irq(); + __DMB(); + buf_sel = udi_cdc_tx_buf_sel[port]; buf_sel_nb = udi_cdc_tx_buf_nb[port][buf_sel]; if (buf_sel_nb == UDI_CDC_TX_BUFFERS) { - if ((!udi_cdc_tx_trans_ongoing[port]) - && (!udi_cdc_tx_both_buf_to_send[port])) { + if ((!udi_cdc_tx_trans_ongoing[port]) && (!udi_cdc_tx_both_buf_to_send[port])) { /* One buffer is full, but the other buffer is not used. * (not used = transfer on-going) * then move to the other buffer to store data */ udi_cdc_tx_both_buf_to_send[port] = true; - udi_cdc_tx_buf_sel[port] = (buf_sel == 0)? 1 : 0; - buf_sel_nb = 0; + udi_cdc_tx_buf_sel[port] = (buf_sel == 0) ? 1 : 0; + buf_sel_nb = 0; } } retval = UDI_CDC_TX_BUFFERS - buf_sel_nb; __DMB(); - __set_PRIMASK(irqflags); + __set_PRIMASK(irqflags); return retval; } -iram_size_t udi_cdc_get_free_tx_buffer(void) -{ - return udi_cdc_multi_get_free_tx_buffer(0); -} +iram_size_t udi_cdc_get_free_tx_buffer(void) { return udi_cdc_multi_get_free_tx_buffer(0); } -bool udi_cdc_multi_is_tx_ready(uint8_t port) -{ - return (udi_cdc_multi_get_free_tx_buffer(port) != 0); -} +bool udi_cdc_multi_is_tx_ready(uint8_t port) { return (udi_cdc_multi_get_free_tx_buffer(port) != 0); } -bool udi_cdc_is_tx_ready(void) -{ - return udi_cdc_multi_is_tx_ready(0); -} +bool udi_cdc_is_tx_ready(void) { return udi_cdc_multi_is_tx_ready(0); } -int udi_cdc_multi_putc(uint8_t port, int value) -{ - uint32_t irqflags; //irqflags_t - bool b_databit_9; - uint8_t buf_sel; +int udi_cdc_multi_putc(uint8_t port, int value) { + uint32_t irqflags; // irqflags_t + bool b_databit_9; + uint8_t buf_sel; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif b_databit_9 = (9 == udi_cdc_line_coding[port].bDataBits); @@ -1143,44 +1014,40 @@ udi_cdc_putc_process_one_byte: // Write value irqflags = __get_PRIMASK(); - __disable_irq(); - __DMB(); - buf_sel = udi_cdc_tx_buf_sel[port]; + __disable_irq(); + __DMB(); + buf_sel = udi_cdc_tx_buf_sel[port]; udi_cdc_tx_buf[port][buf_sel][udi_cdc_tx_buf_nb[port][buf_sel]++] = value; - __DMB(); - __set_PRIMASK(irqflags); + __DMB(); + __set_PRIMASK(irqflags); if (b_databit_9) { // Send MSB b_databit_9 = false; - value = value >> 8; + value = value >> 8; goto udi_cdc_putc_process_one_byte; } return true; } -int udi_cdc_putc(int value) -{ - return udi_cdc_multi_putc(0, value); -} +int udi_cdc_putc(int value) { return udi_cdc_multi_putc(0, value); } -iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size) -{ - uint32_t irqflags; //irqflags_t - uint8_t buf_sel; - uint16_t buf_nb; +iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void *buf, iram_size_t size) { + uint32_t irqflags; // irqflags_t + uint8_t buf_sel; + uint16_t buf_nb; iram_size_t copy_nb; - uint8_t *ptr_buf = (uint8_t *)buf; + uint8_t * ptr_buf = (uint8_t *)buf; -//#if UDI_CDC_PORT_NB == 1 // To optimize code + //#if UDI_CDC_PORT_NB == 1 // To optimize code port = 0; -//#endif + //#endif if (9 == udi_cdc_line_coding[port].bDataBits) { - size *=2; + size *= 2; } - udi_cdc_write_buf_loop_wait: +udi_cdc_write_buf_loop_wait: // Check available space if (!udi_cdc_multi_is_tx_ready(port)) { @@ -1195,7 +1062,7 @@ iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t s __disable_irq(); __DMB(); buf_sel = udi_cdc_tx_buf_sel[port]; - buf_nb = udi_cdc_tx_buf_nb[port][buf_sel]; + buf_nb = udi_cdc_tx_buf_nb[port][buf_sel]; copy_nb = UDI_CDC_TX_BUFFERS - buf_nb; if (copy_nb > size) { copy_nb = size; @@ -1216,43 +1083,36 @@ iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t s return 0; } -iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size) -{ - return udi_cdc_multi_write_buf(0, buf, size); -} +iram_size_t udi_cdc_write_buf(const void *buf, iram_size_t size) { return udi_cdc_multi_write_buf(0, buf, size); } -#define MAX_PRINT 256 -#define CDC_SEND_INTERVAL 2 +# define MAX_PRINT 256 +# define CDC_SEND_INTERVAL 2 uint32_t cdc_tx_send_time_next; -void CDC_send(void) -{ - while (timer_read64() < cdc_tx_send_time_next); +void CDC_send(void) { + while (timer_read64() < cdc_tx_send_time_next) + ; udi_cdc_tx_send(0); cdc_tx_send_time_next = timer_read64() + CDC_SEND_INTERVAL; } -uint32_t CDC_print(char *printbuf) -{ - uint32_t count=0; - char *buf = printbuf; - char c; +uint32_t CDC_print(char *printbuf) { + uint32_t count = 0; + char * buf = printbuf; + char c; if (timer_read64() < 5000) return 0; - while ((c = *buf++) != 0 && !(count >= MAX_PRINT)) - { + while ((c = *buf++) != 0 && !(count >= MAX_PRINT)) { count++; if (!udi_cdc_is_tx_ready()) return 0; udi_cdc_putc(c); - if (count >= UDI_CDC_TX_BUFFERS) - { + if (count >= UDI_CDC_TX_BUFFERS) { count = 0; CDC_send(); } } - if (count) - { + if (count) { CDC_send(); } return 1; @@ -1260,12 +1120,11 @@ uint32_t CDC_print(char *printbuf) char printbuf[CDC_PRINTBUF_SIZE]; -int CDC_printf(const char *_Format, ...) -{ - va_list va; //Variable argument list variable - int result; +int CDC_printf(const char *_Format, ...) { + va_list va; // Variable argument list variable + int result; - va_start(va, _Format); //Initialize the variable argument list + va_start(va, _Format); // Initialize the variable argument list result = vsnprintf(printbuf, CDC_PRINTBUF_SIZE, _Format, va); va_end(va); @@ -1274,107 +1133,83 @@ int CDC_printf(const char *_Format, ...) return result; } -//global "inbuf" if desired +// global "inbuf" if desired inbuf_t inbuf; -uint32_t CDC_input_buf(inbuf_t inbuf, uint32_t inbuf_size) -{ +uint32_t CDC_input_buf(inbuf_t inbuf, uint32_t inbuf_size) { int RXChar; int entered = 0; if (!udi_cdc_is_rx_ready()) return 0; udi_cdc_get_nb_received_data(); - RXChar = udi_cdc_getc(); - - if (RXChar) - { - switch (RXChar) - { - case '\t': //tab - repeat last - inbuf.count=inbuf.lastcount; - inbuf.buf[inbuf.count+1] = 0; + RXChar = udi_cdc_getc(); + + if (RXChar) { + switch (RXChar) { + case '\t': // tab - repeat last + inbuf.count = inbuf.lastcount; + inbuf.buf[inbuf.count + 1] = 0; CDC_print(inbuf.buf); break; - case '\r': //enter - inbuf.buf[inbuf.count]=0; - inbuf.lastcount = inbuf.count; - inbuf.count = 0; - entered = 1; + case '\r': // enter + inbuf.buf[inbuf.count] = 0; + inbuf.lastcount = inbuf.count; + inbuf.count = 0; + entered = 1; break; - case '\b': //backspace + case '\b': // backspace if (inbuf.count > 0) { inbuf.count -= 1; CDC_print("\b \b\0"); - } - else + } else CDC_print("\a\0"); break; - default: - if ((RXChar >= 32) && (RXChar <= 126)) - { - if (inbuf.count < inbuf_size-1) - { - inbuf.buf[inbuf.count] = RXChar; - inbuf.buf[inbuf.count+1] = 0; - CDC_print(&inbuf.buf[inbuf.count]); - inbuf.count += 1; + default: + if ((RXChar >= 32) && (RXChar <= 126)) { + if (inbuf.count < inbuf_size - 1) { + inbuf.buf[inbuf.count] = RXChar; + inbuf.buf[inbuf.count + 1] = 0; + CDC_print(&inbuf.buf[inbuf.count]); + inbuf.count += 1; + } else + CDC_print("\a\0"); } - else - CDC_print("\a\0"); - } - break; + break; } RXChar = 0; } return entered; } -uint32_t CDC_input() -{ - return CDC_input_buf(inbuf, CDC_INBUF_SIZE); -} +uint32_t CDC_input() { return CDC_input_buf(inbuf, CDC_INBUF_SIZE); } -void CDC_init(void) -{ - inbuf.count = 0; - inbuf.lastcount = 0; - printbuf[0] = 0; +void CDC_init(void) { + inbuf.count = 0; + inbuf.lastcount = 0; + printbuf[0] = 0; cdc_tx_send_time_next = timer_read64() + CDC_SEND_INTERVAL; } -#else //CDC line 62 +#else // CDC line 62 char printbuf[CDC_PRINTBUF_SIZE]; -void CDC_send(void) -{ - return; -} +void CDC_send(void) { return; } -uint32_t CDC_print(char *printbuf) -{ - return 0; -} +uint32_t CDC_print(char *printbuf) { return 0; } -int CDC_printf(const char *_Format, ...) -{ - return 0; -} +int CDC_printf(const char *_Format, ...) { return 0; } inbuf_t inbuf; -uint32_t CDC_input(void) -{ - return 0; -} +uint32_t CDC_input(void) { return 0; } -void CDC_init(void) -{ - inbuf.count = 0; +void CDC_init(void) { + inbuf.count = 0; inbuf.lastcount = 0; - printbuf[0]=0; + printbuf[0] = 0; } -#endif //CDC line 62 +#endif // CDC line 62 //@} diff --git a/tmk_core/protocol/arm_atsam/usb/udi_cdc.h b/tmk_core/protocol/arm_atsam/usb/udi_cdc.h index 86077ce53..9135bab54 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_cdc.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_cdc.h @@ -49,24 +49,24 @@ #ifdef CDC -#include "conf_usb.h" -#include "usb_protocol.h" -#include "usb_protocol_cdc.h" -#include "udd.h" -#include "udc_desc.h" -#include "udi.h" +# include "conf_usb.h" +# include "usb_protocol.h" +# include "usb_protocol_cdc.h" +# include "udd.h" +# include "udc_desc.h" +# include "udi.h" // Check the number of port -#ifndef UDI_CDC_PORT_NB -# define UDI_CDC_PORT_NB 1 -#endif -#if (UDI_CDC_PORT_NB > 1) -# error UDI_CDC_PORT_NB must be at most 1 -#endif - -#ifdef __cplusplus +# ifndef UDI_CDC_PORT_NB +# define UDI_CDC_PORT_NB 1 +# endif +# if (UDI_CDC_PORT_NB > 1) +# error UDI_CDC_PORT_NB must be at most 1 +# endif + +# ifdef __cplusplus extern "C" { -#endif +# endif /** * \addtogroup udi_cdc_group_udc @@ -82,9 +82,9 @@ extern UDC_DESC_STORAGE udi_api_t udi_api_cdc_data; //#define CDC_RX_SIZE 64 //! CDC communication endpoints size for all speeds -#define UDI_CDC_COMM_EP_SIZE CDC_ACM_SIZE +# define UDI_CDC_COMM_EP_SIZE CDC_ACM_SIZE //! CDC data endpoints size for FS speed (8B, 16B, 32B, 64B) -#define UDI_CDC_DATA_EPS_FS_SIZE CDC_RX_SIZE +# define UDI_CDC_DATA_EPS_FS_SIZE CDC_RX_SIZE //@} @@ -335,42 +335,42 @@ int udi_cdc_multi_putc(uint8_t port, int value); iram_size_t udi_cdc_multi_write_buf(uint8_t port, const void* buf, iram_size_t size); //@} -#define CDC_PRINTBUF_SIZE 256 +# define CDC_PRINTBUF_SIZE 256 extern char printbuf[CDC_PRINTBUF_SIZE]; -#define CDC_INBUF_SIZE 256 +# define CDC_INBUF_SIZE 256 typedef struct { uint32_t count; uint32_t lastcount; - char buf[CDC_INBUF_SIZE]; + char buf[CDC_INBUF_SIZE]; } inbuf_t; -#else //CDC +#else // CDC // keep these to accommodate calls if remaining -#define CDC_PRINTBUF_SIZE 1 +# define CDC_PRINTBUF_SIZE 1 extern char printbuf[CDC_PRINTBUF_SIZE]; -#define CDC_INBUF_SIZE 1 +# define CDC_INBUF_SIZE 1 typedef struct { uint32_t count; uint32_t lastcount; - char buf[CDC_INBUF_SIZE]; + char buf[CDC_INBUF_SIZE]; } inbuf_t; extern inbuf_t inbuf; -#endif //CDC +#endif // CDC -uint32_t CDC_print(char *printbuf); -int CDC_printf(const char *_Format, ...); +uint32_t CDC_print(char* printbuf); +int CDC_printf(const char* _Format, ...); uint32_t CDC_input(void); -void CDC_init(void); +void CDC_init(void); #ifdef __cplusplus } #endif -#endif // _UDI_CDC_H_ +#endif // _UDI_CDC_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_cdc_conf.h b/tmk_core/protocol/arm_atsam/usb/udi_cdc_conf.h index 2db61fab5..e07951249 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_cdc_conf.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_cdc_conf.h @@ -51,22 +51,22 @@ #include "conf_usb.h" #include "udi_device_conf.h" -#ifndef UDI_CDC_PORT_NB -#define UDI_CDC_PORT_NB 1 +#ifndef UDI_CDC_PORT_NB +# define UDI_CDC_PORT_NB 1 #endif #ifdef __cplusplus extern "C" { #endif -#define UDI_CDC_DATA_EP_IN_0 ((CDC_TX_ENDPOINT) | (USB_EP_DIR_IN)) //TX -#define UDI_CDC_DATA_EP_OUT_0 ((CDC_RX_ENDPOINT) | (USB_EP_DIR_OUT)) // RX -#define UDI_CDC_COMM_EP_0 ((CDC_ACM_ENDPOINT) | (USB_EP_DIR_IN)) // Notify endpoint +#define UDI_CDC_DATA_EP_IN_0 ((CDC_TX_ENDPOINT) | (USB_EP_DIR_IN)) // TX +#define UDI_CDC_DATA_EP_OUT_0 ((CDC_RX_ENDPOINT) | (USB_EP_DIR_OUT)) // RX +#define UDI_CDC_COMM_EP_0 ((CDC_ACM_ENDPOINT) | (USB_EP_DIR_IN)) // Notify endpoint -#define UDI_CDC_COMM_IFACE_NUMBER_0 (CDC_STATUS_INTERFACE) -#define UDI_CDC_DATA_IFACE_NUMBER_0 (CDC_DATA_INTERFACE) +#define UDI_CDC_COMM_IFACE_NUMBER_0 (CDC_STATUS_INTERFACE) +#define UDI_CDC_DATA_IFACE_NUMBER_0 (CDC_DATA_INTERFACE) #ifdef __cplusplus } #endif -#endif // _UDI_CDC_CONF_H_ +#endif // _UDI_CDC_CONF_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_device_conf.h b/tmk_core/protocol/arm_atsam/usb/udi_device_conf.h index 1e82b9ecc..f22f5003a 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_device_conf.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_device_conf.h @@ -31,27 +31,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. //#define MOUSE_ENABLE //rules.mk #ifdef MOUSE_ENABLE -#define MOU +# define MOU #endif //#define EXTRAKEY_ENABLE //rules.mk #ifdef EXTRAKEY_ENABLE -#define EXK +# define EXK #endif //#define RAW_ENABLE //rules.mk #ifdef RAW_ENABLE -#define RAW +# define RAW #endif //#define CONSOLE_ENABLE //rules.mk #ifdef CONSOLE_ENABLE -#define CON +# define CON #endif //#define NKRO_ENABLE //rules.mk #ifdef NKRO_ENABLE -#define NKRO +# define NKRO #endif //#define MIDI_ENABLE //deferred implementation @@ -61,229 +61,227 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. //#define VIRTSER_ENABLE //rules.mk #ifdef VIRTSER_ENABLE -#define CDC -//because CDC uses IAD (interface association descriptor -//per USB Interface Association Descriptor Device Class Code and Use Model 7/23/2003 Rev 1.0) -#undef DEVICE_CLASS -#define DEVICE_CLASS 0xEF -#undef DEVICE_SUBCLASS -#define DEVICE_SUBCLASS 0x02 -#undef DEVICE_PROTOCOL -#define DEVICE_PROTOCOL 0x01 +# define CDC +// because CDC uses IAD (interface association descriptor +// per USB Interface Association Descriptor Device Class Code and Use Model 7/23/2003 Rev 1.0) +# undef DEVICE_CLASS +# define DEVICE_CLASS 0xEF +# undef DEVICE_SUBCLASS +# define DEVICE_SUBCLASS 0x02 +# undef DEVICE_PROTOCOL +# define DEVICE_PROTOCOL 0x01 #endif /* number of interfaces */ -#define NEXT_INTERFACE_0 0 +#define NEXT_INTERFACE_0 0 #ifdef KBD -#define KEYBOARD_INTERFACE NEXT_INTERFACE_0 -#define NEXT_INTERFACE_1 (KEYBOARD_INTERFACE + 1) -#define UDI_HID_KBD_IFACE_NUMBER KEYBOARD_INTERFACE +# define KEYBOARD_INTERFACE NEXT_INTERFACE_0 +# define NEXT_INTERFACE_1 (KEYBOARD_INTERFACE + 1) +# define UDI_HID_KBD_IFACE_NUMBER KEYBOARD_INTERFACE #else -#define NEXT_INTERFACE_1 NEXT_INTERFACE_0 +# define NEXT_INTERFACE_1 NEXT_INTERFACE_0 #endif // It is important that the Raw HID interface is at a constant // interface number, to support Linux/OSX platforms and chrome.hid // If Raw HID is enabled, let it be always 1. #ifdef RAW -#define RAW_INTERFACE NEXT_INTERFACE_1 -#define NEXT_INTERFACE_2 (RAW_INTERFACE + 1) +# define RAW_INTERFACE NEXT_INTERFACE_1 +# define NEXT_INTERFACE_2 (RAW_INTERFACE + 1) #else -#define NEXT_INTERFACE_2 NEXT_INTERFACE_1 +# define NEXT_INTERFACE_2 NEXT_INTERFACE_1 #endif #ifdef MOU -#define MOUSE_INTERFACE NEXT_INTERFACE_2 -#define UDI_HID_MOU_IFACE_NUMBER MOUSE_INTERFACE -#define NEXT_INTERFACE_3 (MOUSE_INTERFACE + 1) +# define MOUSE_INTERFACE NEXT_INTERFACE_2 +# define UDI_HID_MOU_IFACE_NUMBER MOUSE_INTERFACE +# define NEXT_INTERFACE_3 (MOUSE_INTERFACE + 1) #else -#define NEXT_INTERFACE_3 NEXT_INTERFACE_2 +# define NEXT_INTERFACE_3 NEXT_INTERFACE_2 #endif #ifdef EXK -#define EXTRAKEY_INTERFACE NEXT_INTERFACE_3 -#define NEXT_INTERFACE_4 (EXTRAKEY_INTERFACE + 1) -#define UDI_HID_EXK_IFACE_NUMBER EXTRAKEY_INTERFACE +# define EXTRAKEY_INTERFACE NEXT_INTERFACE_3 +# define NEXT_INTERFACE_4 (EXTRAKEY_INTERFACE + 1) +# define UDI_HID_EXK_IFACE_NUMBER EXTRAKEY_INTERFACE #else -#define NEXT_INTERFACE_4 NEXT_INTERFACE_3 +# define NEXT_INTERFACE_4 NEXT_INTERFACE_3 #endif #ifdef CON -#define CON_INTERFACE NEXT_INTERFACE_4 -#define NEXT_INTERFACE_5 (CON_INTERFACE + 1) -#define UDI_HID_CON_IFACE_NUMBER CON_INTERFACE +# define CON_INTERFACE NEXT_INTERFACE_4 +# define NEXT_INTERFACE_5 (CON_INTERFACE + 1) +# define UDI_HID_CON_IFACE_NUMBER CON_INTERFACE #else -#define NEXT_INTERFACE_5 NEXT_INTERFACE_4 +# define NEXT_INTERFACE_5 NEXT_INTERFACE_4 #endif #ifdef NKRO -#define NKRO_INTERFACE NEXT_INTERFACE_5 -#define NEXT_INTERFACE_6 (NKRO_INTERFACE + 1) -#define UDI_HID_NKRO_IFACE_NUMBER NKRO_INTERFACE +# define NKRO_INTERFACE NEXT_INTERFACE_5 +# define NEXT_INTERFACE_6 (NKRO_INTERFACE + 1) +# define UDI_HID_NKRO_IFACE_NUMBER NKRO_INTERFACE #else -#define NEXT_INTERFACE_6 NEXT_INTERFACE_5 +# define NEXT_INTERFACE_6 NEXT_INTERFACE_5 #endif #ifdef MIDI -#define AC_INTERFACE NEXT_INTERFACE_6 -#define AS_INTERFACE (AC_INTERFACE + 1) -#define NEXT_INTERFACE_7 (AS_INTERFACE + 1) +# define AC_INTERFACE NEXT_INTERFACE_6 +# define AS_INTERFACE (AC_INTERFACE + 1) +# define NEXT_INTERFACE_7 (AS_INTERFACE + 1) #else -#define NEXT_INTERFACE_7 NEXT_INTERFACE_6 +# define NEXT_INTERFACE_7 NEXT_INTERFACE_6 #endif #ifdef CDC -#define CCI_INTERFACE NEXT_INTERFACE_7 -#define CDI_INTERFACE (CCI_INTERFACE + 1) -#define NEXT_INTERFACE_8 (CDI_INTERFACE + 1) -#define CDC_STATUS_INTERFACE CCI_INTERFACE -#define CDC_DATA_INTERFACE CDI_INTERFACE +# define CCI_INTERFACE NEXT_INTERFACE_7 +# define CDI_INTERFACE (CCI_INTERFACE + 1) +# define NEXT_INTERFACE_8 (CDI_INTERFACE + 1) +# define CDC_STATUS_INTERFACE CCI_INTERFACE +# define CDC_DATA_INTERFACE CDI_INTERFACE #else -#define NEXT_INTERFACE_8 NEXT_INTERFACE_7 +# define NEXT_INTERFACE_8 NEXT_INTERFACE_7 #endif /* nubmer of interfaces */ -#define TOTAL_INTERFACES NEXT_INTERFACE_8 -#define USB_DEVICE_NB_INTERFACE TOTAL_INTERFACES - +#define TOTAL_INTERFACES NEXT_INTERFACE_8 +#define USB_DEVICE_NB_INTERFACE TOTAL_INTERFACES // ********************************************************************** // Endopoint number and size // ********************************************************************** -#define USB_DEVICE_EP_CTRL_SIZE 8 +#define USB_DEVICE_EP_CTRL_SIZE 8 -#define NEXT_IN_EPNUM_0 1 -#define NEXT_OUT_EPNUM_0 1 +#define NEXT_IN_EPNUM_0 1 +#define NEXT_OUT_EPNUM_0 1 #ifdef KBD -#define KEYBOARD_IN_EPNUM NEXT_IN_EPNUM_0 -#define UDI_HID_KBD_EP_IN KEYBOARD_IN_EPNUM -#define NEXT_IN_EPNUM_1 (KEYBOARD_IN_EPNUM + 1) -#define UDI_HID_KBD_EP_SIZE KEYBOARD_EPSIZE -#define KBD_POLLING_INTERVAL 10 -#ifndef UDI_HID_KBD_STRING_ID -#define UDI_HID_KBD_STRING_ID 0 -#endif +# define KEYBOARD_IN_EPNUM NEXT_IN_EPNUM_0 +# define UDI_HID_KBD_EP_IN KEYBOARD_IN_EPNUM +# define NEXT_IN_EPNUM_1 (KEYBOARD_IN_EPNUM + 1) +# define UDI_HID_KBD_EP_SIZE KEYBOARD_EPSIZE +# define KBD_POLLING_INTERVAL 10 +# ifndef UDI_HID_KBD_STRING_ID +# define UDI_HID_KBD_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_1 NEXT_IN_EPNUM_0 +# define NEXT_IN_EPNUM_1 NEXT_IN_EPNUM_0 #endif #ifdef MOU -#define MOUSE_IN_EPNUM NEXT_IN_EPNUM_1 -#define NEXT_IN_EPNUM_2 (MOUSE_IN_EPNUM + 1) -#define UDI_HID_MOU_EP_IN MOUSE_IN_EPNUM -#define UDI_HID_MOU_EP_SIZE MOUSE_EPSIZE -#define MOU_POLLING_INTERVAL 10 -#ifndef UDI_HID_MOU_STRING_ID -#define UDI_HID_MOU_STRING_ID 0 -#endif +# define MOUSE_IN_EPNUM NEXT_IN_EPNUM_1 +# define NEXT_IN_EPNUM_2 (MOUSE_IN_EPNUM + 1) +# define UDI_HID_MOU_EP_IN MOUSE_IN_EPNUM +# define UDI_HID_MOU_EP_SIZE MOUSE_EPSIZE +# define MOU_POLLING_INTERVAL 10 +# ifndef UDI_HID_MOU_STRING_ID +# define UDI_HID_MOU_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_2 NEXT_IN_EPNUM_1 +# define NEXT_IN_EPNUM_2 NEXT_IN_EPNUM_1 #endif #ifdef EXK -#define EXTRAKEY_IN_EPNUM NEXT_IN_EPNUM_2 -#define UDI_HID_EXK_EP_IN EXTRAKEY_IN_EPNUM -#define NEXT_IN_EPNUM_3 (EXTRAKEY_IN_EPNUM + 1) -#define UDI_HID_EXK_EP_SIZE EXTRAKEY_EPSIZE -#define EXTRAKEY_POLLING_INTERVAL 10 -#ifndef UDI_HID_EXK_STRING_ID -#define UDI_HID_EXK_STRING_ID 0 -#endif +# define EXTRAKEY_IN_EPNUM NEXT_IN_EPNUM_2 +# define UDI_HID_EXK_EP_IN EXTRAKEY_IN_EPNUM +# define NEXT_IN_EPNUM_3 (EXTRAKEY_IN_EPNUM + 1) +# define UDI_HID_EXK_EP_SIZE EXTRAKEY_EPSIZE +# define EXTRAKEY_POLLING_INTERVAL 10 +# ifndef UDI_HID_EXK_STRING_ID +# define UDI_HID_EXK_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_3 NEXT_IN_EPNUM_2 +# define NEXT_IN_EPNUM_3 NEXT_IN_EPNUM_2 #endif #ifdef RAW -#define RAW_IN_EPNUM NEXT_IN_EPNUM_3 -#define UDI_HID_RAW_EP_IN RAW_IN_EPNUM -#define NEXT_IN_EPNUM_4 (RAW_IN_EPNUM + 1) -#define RAW_OUT_EPNUM NEXT_OUT_EPNUM_0 -#define UDI_HID_RAW_EP_OUT RAW_OUT_EPNUM -#define NEXT_OUT_EPNUM_1 (RAW_OUT_EPNUM + 1) -#define RAW_POLLING_INTERVAL 1 -#ifndef UDI_HID_RAW_STRING_ID -#define UDI_HID_RAW_STRING_ID 0 -#endif +# define RAW_IN_EPNUM NEXT_IN_EPNUM_3 +# define UDI_HID_RAW_EP_IN RAW_IN_EPNUM +# define NEXT_IN_EPNUM_4 (RAW_IN_EPNUM + 1) +# define RAW_OUT_EPNUM NEXT_OUT_EPNUM_0 +# define UDI_HID_RAW_EP_OUT RAW_OUT_EPNUM +# define NEXT_OUT_EPNUM_1 (RAW_OUT_EPNUM + 1) +# define RAW_POLLING_INTERVAL 1 +# ifndef UDI_HID_RAW_STRING_ID +# define UDI_HID_RAW_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_4 NEXT_IN_EPNUM_3 -#define NEXT_OUT_EPNUM_1 NEXT_OUT_EPNUM_0 +# define NEXT_IN_EPNUM_4 NEXT_IN_EPNUM_3 +# define NEXT_OUT_EPNUM_1 NEXT_OUT_EPNUM_0 #endif #ifdef CON -#define CON_IN_EPNUM NEXT_IN_EPNUM_4 -#define UDI_HID_CON_EP_IN CON_IN_EPNUM -#define NEXT_IN_EPNUM_5 (CON_IN_EPNUM + 1) -#define CON_OUT_EPNUM NEXT_OUT_EPNUM_1 -#define UDI_HID_CON_EP_OUT CON_OUT_EPNUM -#define NEXT_OUT_EPNUM_2 (CON_OUT_EPNUM + 1) -#define CON_POLLING_INTERVAL 1 -#ifndef UDI_HID_CON_STRING_ID -#define UDI_HID_CON_STRING_ID 0 -#endif +# define CON_IN_EPNUM NEXT_IN_EPNUM_4 +# define UDI_HID_CON_EP_IN CON_IN_EPNUM +# define NEXT_IN_EPNUM_5 (CON_IN_EPNUM + 1) +# define CON_OUT_EPNUM NEXT_OUT_EPNUM_1 +# define UDI_HID_CON_EP_OUT CON_OUT_EPNUM +# define NEXT_OUT_EPNUM_2 (CON_OUT_EPNUM + 1) +# define CON_POLLING_INTERVAL 1 +# ifndef UDI_HID_CON_STRING_ID +# define UDI_HID_CON_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_5 NEXT_IN_EPNUM_4 -#define NEXT_OUT_EPNUM_2 NEXT_OUT_EPNUM_1 +# define NEXT_IN_EPNUM_5 NEXT_IN_EPNUM_4 +# define NEXT_OUT_EPNUM_2 NEXT_OUT_EPNUM_1 #endif #ifdef NKRO -#define NKRO_IN_EPNUM NEXT_IN_EPNUM_5 -#define UDI_HID_NKRO_EP_IN NKRO_IN_EPNUM -#define NEXT_IN_EPNUM_6 (NKRO_IN_EPNUM + 1) -#define UDI_HID_NKRO_EP_SIZE NKRO_EPSIZE -#define NKRO_POLLING_INTERVAL 1 -#ifndef UDI_HID_NKRO_STRING_ID -#define UDI_HID_NKRO_STRING_ID 0 -#endif +# define NKRO_IN_EPNUM NEXT_IN_EPNUM_5 +# define UDI_HID_NKRO_EP_IN NKRO_IN_EPNUM +# define NEXT_IN_EPNUM_6 (NKRO_IN_EPNUM + 1) +# define UDI_HID_NKRO_EP_SIZE NKRO_EPSIZE +# define NKRO_POLLING_INTERVAL 1 +# ifndef UDI_HID_NKRO_STRING_ID +# define UDI_HID_NKRO_STRING_ID 0 +# endif #else -#define NEXT_IN_EPNUM_6 NEXT_IN_EPNUM_5 +# define NEXT_IN_EPNUM_6 NEXT_IN_EPNUM_5 #endif #ifdef MIDI -#define MIDI_STREAM_IN_EPNUM NEXT_IN_EPNUM_6 -#define NEXT_IN_EPNUM_7 (MIDI_STREAM_IN_EPNUM + 1) -#define MIDI_STREAM_OUT_EPNUM NEXT_OUT_EPNUM_2 -#define NEXT_OUT_EPNUM_3 (MIDI_STREAM_OUT_EPNUM + 1) -#define MIDI_POLLING_INTERVAL 5 +# define MIDI_STREAM_IN_EPNUM NEXT_IN_EPNUM_6 +# define NEXT_IN_EPNUM_7 (MIDI_STREAM_IN_EPNUM + 1) +# define MIDI_STREAM_OUT_EPNUM NEXT_OUT_EPNUM_2 +# define NEXT_OUT_EPNUM_3 (MIDI_STREAM_OUT_EPNUM + 1) +# define MIDI_POLLING_INTERVAL 5 #else -#define NEXT_IN_EPNUM_7 NEXT_IN_EPNUM_6 -#define NEXT_OUT_EPNUM_3 NEXT_OUT_EPNUM_2 +# define NEXT_IN_EPNUM_7 NEXT_IN_EPNUM_6 +# define NEXT_OUT_EPNUM_3 NEXT_OUT_EPNUM_2 #endif #ifdef CDC -#define CDC_NOTIFICATION_EPNUM NEXT_IN_EPNUM_7 -#define CDC_ACM_ENDPOINT CDC_NOTIFICATION_EPNUM -#define CDC_TX_ENDPOINT (CDC_NOTIFICATION_EPNUM + 1) -#define NEXT_IN_EPNUM_8 (CDC_TX_ENDPOINT + 1) - -#define CDC_OUT_EPNUM NEXT_OUT_EPNUM_3 -#define CDC_RX_ENDPOINT CDC_OUT_EPNUM -#define NEXT_OUT_EPNUM_4 (CDC_OUT_EPNUM + 1) - -#define CDC_ACM_SIZE CDC_NOTIFICATION_EPSIZE -#define CDC_RX_SIZE CDC_EPSIZE //KFSMOD was 64 -#define CDC_TX_SIZE CDC_RX_SIZE -#define CDC_ACM_POLLING_INTERVAL 255 -#define CDC_EP_INTERVAL_STATUS CDC_ACM_POLLING_INTERVAL -#define CDC_DATA_POLLING_INTERVAL 5 -#define CDC_EP_INTERVAL_DATA CDC_DATA_POLLING_INTERVAL -#define CDC_STATUS_NAME L"Virtual Serial Port - Status" -#define CDC_DATA_NAME L"Virtual Serial Port - Data" +# define CDC_NOTIFICATION_EPNUM NEXT_IN_EPNUM_7 +# define CDC_ACM_ENDPOINT CDC_NOTIFICATION_EPNUM +# define CDC_TX_ENDPOINT (CDC_NOTIFICATION_EPNUM + 1) +# define NEXT_IN_EPNUM_8 (CDC_TX_ENDPOINT + 1) + +# define CDC_OUT_EPNUM NEXT_OUT_EPNUM_3 +# define CDC_RX_ENDPOINT CDC_OUT_EPNUM +# define NEXT_OUT_EPNUM_4 (CDC_OUT_EPNUM + 1) + +# define CDC_ACM_SIZE CDC_NOTIFICATION_EPSIZE +# define CDC_RX_SIZE CDC_EPSIZE // KFSMOD was 64 +# define CDC_TX_SIZE CDC_RX_SIZE +# define CDC_ACM_POLLING_INTERVAL 255 +# define CDC_EP_INTERVAL_STATUS CDC_ACM_POLLING_INTERVAL +# define CDC_DATA_POLLING_INTERVAL 5 +# define CDC_EP_INTERVAL_DATA CDC_DATA_POLLING_INTERVAL +# define CDC_STATUS_NAME L"Virtual Serial Port - Status" +# define CDC_DATA_NAME L"Virtual Serial Port - Data" #else -#define NEXT_IN_EPNUM_8 NEXT_IN_EPNUM_7 -#define NEXT_OUT_EPNUM_4 NEXT_OUT_EPNUM_3 +# define NEXT_IN_EPNUM_8 NEXT_IN_EPNUM_7 +# define NEXT_OUT_EPNUM_4 NEXT_OUT_EPNUM_3 #endif -#define TOTAL_OUT_EP NEXT_OUT_EPNUM_4 -#define TOTAL_IN_EP NEXT_IN_EPNUM_8 -#define USB_DEVICE_MAX_EP (max(NEXT_OUT_EPNUM_4, NEXT_IN_EPNUM_8)) +#define TOTAL_OUT_EP NEXT_OUT_EPNUM_4 +#define TOTAL_IN_EP NEXT_IN_EPNUM_8 +#define USB_DEVICE_MAX_EP (max(NEXT_OUT_EPNUM_4, NEXT_IN_EPNUM_8)) #if USB_DEVICE_MAX_EP > 8 -#error "There are not enough available endpoints to support all functions. Remove some in the rules.mk file.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI, VIRTSER)" +# error "There are not enough available endpoints to support all functions. Remove some in the rules.mk file.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI, VIRTSER)" #endif - // ********************************************************************** // KBD Descriptor structure and content // ********************************************************************** @@ -292,50 +290,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep; + usb_ep_desc_t ep; } udi_hid_kbd_desc_t; typedef struct { uint8_t array[59]; } udi_hid_kbd_report_desc_t; -#define UDI_HID_KBD_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = UDI_HID_KBD_IFACE_NUMBER,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 1,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT,\ - .iface.bInterfaceProtocol = HID_PROTOCOL_KEYBOARD,\ - .iface.iInterface = UDI_HID_KBD_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_kbd_report_desc_t)),\ - .ep.bLength = sizeof(usb_ep_desc_t),\ - .ep.bDescriptorType = USB_DT_ENDPOINT,\ - .ep.bEndpointAddress = UDI_HID_KBD_EP_IN | USB_EP_DIR_IN,\ - .ep.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep.wMaxPacketSize = LE16(UDI_HID_KBD_EP_SIZE),\ - .ep.bInterval = KBD_POLLING_INTERVAL,\ -} - -//set report buffer (from host) +# define UDI_HID_KBD_DESC \ + { .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = UDI_HID_KBD_IFACE_NUMBER, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 1, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT, .iface.bInterfaceProtocol = HID_PROTOCOL_KEYBOARD, .iface.iInterface = UDI_HID_KBD_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_kbd_report_desc_t)), .ep.bLength = sizeof(usb_ep_desc_t), .ep.bDescriptorType = USB_DT_ENDPOINT, .ep.bEndpointAddress = UDI_HID_KBD_EP_IN | USB_EP_DIR_IN, .ep.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep.wMaxPacketSize = LE16(UDI_HID_KBD_EP_SIZE), .ep.bInterval = KBD_POLLING_INTERVAL, } + +// set report buffer (from host) extern uint8_t udi_hid_kbd_report_set; -//report buffer (to host) -#define UDI_HID_KBD_REPORT_SIZE 8 +// report buffer (to host) +# define UDI_HID_KBD_REPORT_SIZE 8 extern uint8_t udi_hid_kbd_report[UDI_HID_KBD_REPORT_SIZE]; COMPILER_PACK_RESET() -#endif //KBD +#endif // KBD // ********************************************************************** // EXK Descriptor structure and content @@ -345,59 +321,37 @@ COMPILER_PACK_RESET() COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep; + usb_ep_desc_t ep; } udi_hid_exk_desc_t; typedef struct { uint8_t array[54]; } udi_hid_exk_report_desc_t; -#define UDI_HID_EXK_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = UDI_HID_EXK_IFACE_NUMBER,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 1,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT,\ - .iface.bInterfaceProtocol = HID_PROTOCOL_GENERIC,\ - .iface.iInterface = UDI_HID_EXK_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_exk_report_desc_t)),\ - .ep.bLength = sizeof(usb_ep_desc_t),\ - .ep.bDescriptorType = USB_DT_ENDPOINT,\ - .ep.bEndpointAddress = UDI_HID_EXK_EP_IN | USB_EP_DIR_IN,\ - .ep.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep.wMaxPacketSize = LE16(UDI_HID_EXK_EP_SIZE),\ - .ep.bInterval = EXTRAKEY_POLLING_INTERVAL,\ -} - -//set report buffer (from host) +# define UDI_HID_EXK_DESC \ + { .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = UDI_HID_EXK_IFACE_NUMBER, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 1, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT, .iface.bInterfaceProtocol = HID_PROTOCOL_GENERIC, .iface.iInterface = UDI_HID_EXK_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_exk_report_desc_t)), .ep.bLength = sizeof(usb_ep_desc_t), .ep.bDescriptorType = USB_DT_ENDPOINT, .ep.bEndpointAddress = UDI_HID_EXK_EP_IN | USB_EP_DIR_IN, .ep.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep.wMaxPacketSize = LE16(UDI_HID_EXK_EP_SIZE), .ep.bInterval = EXTRAKEY_POLLING_INTERVAL, } + +// set report buffer (from host) extern uint8_t udi_hid_exk_report_set; -//report buffer -#define UDI_HID_EXK_REPORT_SIZE 3 +// report buffer +# define UDI_HID_EXK_REPORT_SIZE 3 typedef union { - struct { - uint8_t report_id; - uint16_t report_data; - } desc; - uint8_t raw[UDI_HID_EXK_REPORT_SIZE]; + struct { + uint8_t report_id; + uint16_t report_data; + } desc; + uint8_t raw[UDI_HID_EXK_REPORT_SIZE]; } udi_hid_exk_report_t; extern udi_hid_exk_report_t udi_hid_exk_report; COMPILER_PACK_RESET() -#endif //EXK +#endif // EXK // ********************************************************************** // NKRO Descriptor structure and content @@ -407,50 +361,28 @@ COMPILER_PACK_RESET() COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep; + usb_ep_desc_t ep; } udi_hid_nkro_desc_t; typedef struct { uint8_t array[57]; } udi_hid_nkro_report_desc_t; -#define UDI_HID_NKRO_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = UDI_HID_NKRO_IFACE_NUMBER,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 1,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT,\ - .iface.bInterfaceProtocol = HID_PROTOCOL_KEYBOARD,\ - .iface.iInterface = UDI_HID_NKRO_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_nkro_report_desc_t)),\ - .ep.bLength = sizeof(usb_ep_desc_t),\ - .ep.bDescriptorType = USB_DT_ENDPOINT,\ - .ep.bEndpointAddress = UDI_HID_NKRO_EP_IN | USB_EP_DIR_IN,\ - .ep.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep.wMaxPacketSize = LE16(UDI_HID_NKRO_EP_SIZE),\ - .ep.bInterval = NKRO_POLLING_INTERVAL,\ -} - -//set report buffer +# define UDI_HID_NKRO_DESC \ + { .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = UDI_HID_NKRO_IFACE_NUMBER, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 1, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT, .iface.bInterfaceProtocol = HID_PROTOCOL_KEYBOARD, .iface.iInterface = UDI_HID_NKRO_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_nkro_report_desc_t)), .ep.bLength = sizeof(usb_ep_desc_t), .ep.bDescriptorType = USB_DT_ENDPOINT, .ep.bEndpointAddress = UDI_HID_NKRO_EP_IN | USB_EP_DIR_IN, .ep.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep.wMaxPacketSize = LE16(UDI_HID_NKRO_EP_SIZE), .ep.bInterval = NKRO_POLLING_INTERVAL, } + +// set report buffer extern uint8_t udi_hid_nkro_report_set; -//report buffer -#define UDI_HID_NKRO_REPORT_SIZE 32 +// report buffer +# define UDI_HID_NKRO_REPORT_SIZE 32 extern uint8_t udi_hid_nkro_report[UDI_HID_NKRO_REPORT_SIZE]; COMPILER_PACK_RESET() -#endif //NKRO +#endif // NKRO // ********************************************************************** // MOU Descriptor structure and content @@ -460,49 +392,27 @@ COMPILER_PACK_RESET() COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep; + usb_ep_desc_t ep; } udi_hid_mou_desc_t; typedef struct { - uint8_t array[77];//MOU PDS + uint8_t array[77]; // MOU PDS } udi_hid_mou_report_desc_t; -#define UDI_HID_MOU_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = MOUSE_INTERFACE,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 1,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT,\ - .iface.bInterfaceProtocol = HID_PROTOCOL_MOUSE,\ - .iface.iInterface = UDI_HID_MOU_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_mou_report_desc_t)),\ - .ep.bLength = sizeof(usb_ep_desc_t),\ - .ep.bDescriptorType = USB_DT_ENDPOINT,\ - .ep.bEndpointAddress = UDI_HID_MOU_EP_IN | USB_EP_DIR_IN,\ - .ep.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep.wMaxPacketSize = LE16(UDI_HID_MOU_EP_SIZE),\ - .ep.bInterval = MOU_POLLING_INTERVAL,\ -} - -//no set report buffer - -//report buffer -#define UDI_HID_MOU_REPORT_SIZE 5 //MOU PDS +# define UDI_HID_MOU_DESC \ + { .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = MOUSE_INTERFACE, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 1, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_BOOT, .iface.bInterfaceProtocol = HID_PROTOCOL_MOUSE, .iface.iInterface = UDI_HID_MOU_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_mou_report_desc_t)), .ep.bLength = sizeof(usb_ep_desc_t), .ep.bDescriptorType = USB_DT_ENDPOINT, .ep.bEndpointAddress = UDI_HID_MOU_EP_IN | USB_EP_DIR_IN, .ep.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep.wMaxPacketSize = LE16(UDI_HID_MOU_EP_SIZE), .ep.bInterval = MOU_POLLING_INTERVAL, } + +// no set report buffer + +// report buffer +# define UDI_HID_MOU_REPORT_SIZE 5 // MOU PDS extern uint8_t udi_hid_mou_report[UDI_HID_MOU_REPORT_SIZE]; COMPILER_PACK_RESET() -#endif //MOU +#endif // MOU // ********************************************************************** // RAW Descriptor structure and content @@ -512,57 +422,32 @@ COMPILER_PACK_RESET() COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep_out; - usb_ep_desc_t ep_in; + usb_ep_desc_t ep_out; + usb_ep_desc_t ep_in; } udi_hid_raw_desc_t; typedef struct { uint8_t array[27]; } udi_hid_raw_report_desc_t; -#define UDI_HID_RAW_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = RAW_INTERFACE,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 2,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT,\ - .iface.bInterfaceProtocol = HID_SUB_CLASS_NOBOOT,\ - .iface.iInterface = UDI_HID_RAW_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_raw_report_desc_t)),\ - .ep_out.bLength = sizeof(usb_ep_desc_t),\ - .ep_out.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_out.bEndpointAddress = UDI_HID_RAW_EP_OUT | USB_EP_DIR_OUT,\ - .ep_out.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep_out.wMaxPacketSize = LE16(RAW_EPSIZE),\ - .ep_out.bInterval = RAW_POLLING_INTERVAL,\ - .ep_in.bLength = sizeof(usb_ep_desc_t),\ - .ep_in.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_in.bEndpointAddress = UDI_HID_RAW_EP_IN | USB_EP_DIR_IN,\ - .ep_in.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep_in.wMaxPacketSize = LE16(RAW_EPSIZE),\ - .ep_in.bInterval = RAW_POLLING_INTERVAL,\ -} - -#define UDI_HID_RAW_REPORT_SIZE RAW_EPSIZE +# define UDI_HID_RAW_DESC \ + { \ + .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = RAW_INTERFACE, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 2, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT, .iface.bInterfaceProtocol = HID_SUB_CLASS_NOBOOT, .iface.iInterface = UDI_HID_RAW_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_raw_report_desc_t)), .ep_out.bLength = sizeof(usb_ep_desc_t), .ep_out.bDescriptorType = USB_DT_ENDPOINT, .ep_out.bEndpointAddress = UDI_HID_RAW_EP_OUT | USB_EP_DIR_OUT, .ep_out.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep_out.wMaxPacketSize = LE16(RAW_EPSIZE), .ep_out.bInterval = RAW_POLLING_INTERVAL, \ + .ep_in.bLength = sizeof(usb_ep_desc_t), .ep_in.bDescriptorType = USB_DT_ENDPOINT, .ep_in.bEndpointAddress = UDI_HID_RAW_EP_IN | USB_EP_DIR_IN, .ep_in.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep_in.wMaxPacketSize = LE16(RAW_EPSIZE), .ep_in.bInterval = RAW_POLLING_INTERVAL, \ + } + +# define UDI_HID_RAW_REPORT_SIZE RAW_EPSIZE extern uint8_t udi_hid_raw_report_set[UDI_HID_RAW_REPORT_SIZE]; -//report buffer +// report buffer extern uint8_t udi_hid_raw_report[UDI_HID_RAW_REPORT_SIZE]; COMPILER_PACK_RESET() -#endif //RAW +#endif // RAW // ********************************************************************** // CON Descriptor structure and content @@ -572,57 +457,32 @@ COMPILER_PACK_RESET() COMPILER_PACK_SET(1) typedef struct { - usb_iface_desc_t iface; + usb_iface_desc_t iface; usb_hid_descriptor_t hid; - usb_ep_desc_t ep_out; - usb_ep_desc_t ep_in; + usb_ep_desc_t ep_out; + usb_ep_desc_t ep_in; } udi_hid_con_desc_t; typedef struct { uint8_t array[34]; } udi_hid_con_report_desc_t; -#define UDI_HID_CON_DESC {\ - .iface.bLength = sizeof(usb_iface_desc_t),\ - .iface.bDescriptorType = USB_DT_INTERFACE,\ - .iface.bInterfaceNumber = UDI_HID_CON_IFACE_NUMBER,\ - .iface.bAlternateSetting = 0,\ - .iface.bNumEndpoints = 2,\ - .iface.bInterfaceClass = HID_CLASS,\ - .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT,\ - .iface.bInterfaceProtocol = HID_SUB_CLASS_NOBOOT,\ - .iface.iInterface = UDI_HID_CON_STRING_ID,\ - .hid.bLength = sizeof(usb_hid_descriptor_t),\ - .hid.bDescriptorType = USB_DT_HID,\ - .hid.bcdHID = LE16(USB_HID_BDC_V1_11),\ - .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE,\ - .hid.bNumDescriptors = USB_HID_NUM_DESC,\ - .hid.bRDescriptorType = USB_DT_HID_REPORT,\ - .hid.wDescriptorLength = LE16(sizeof(udi_hid_con_report_desc_t)),\ - .ep_out.bLength = sizeof(usb_ep_desc_t),\ - .ep_out.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_out.bEndpointAddress = UDI_HID_CON_EP_OUT | USB_EP_DIR_OUT,\ - .ep_out.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep_out.wMaxPacketSize = LE16(CONSOLE_EPSIZE),\ - .ep_out.bInterval = CON_POLLING_INTERVAL,\ - .ep_in.bLength = sizeof(usb_ep_desc_t),\ - .ep_in.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_in.bEndpointAddress = UDI_HID_CON_EP_IN | USB_EP_DIR_IN,\ - .ep_in.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep_in.wMaxPacketSize = LE16(CONSOLE_EPSIZE),\ - .ep_in.bInterval = CON_POLLING_INTERVAL,\ -} - -#define UDI_HID_CON_REPORT_SIZE CONSOLE_EPSIZE +# define UDI_HID_CON_DESC \ + { \ + .iface.bLength = sizeof(usb_iface_desc_t), .iface.bDescriptorType = USB_DT_INTERFACE, .iface.bInterfaceNumber = UDI_HID_CON_IFACE_NUMBER, .iface.bAlternateSetting = 0, .iface.bNumEndpoints = 2, .iface.bInterfaceClass = HID_CLASS, .iface.bInterfaceSubClass = HID_SUB_CLASS_NOBOOT, .iface.bInterfaceProtocol = HID_SUB_CLASS_NOBOOT, .iface.iInterface = UDI_HID_CON_STRING_ID, .hid.bLength = sizeof(usb_hid_descriptor_t), .hid.bDescriptorType = USB_DT_HID, .hid.bcdHID = LE16(USB_HID_BDC_V1_11), .hid.bCountryCode = USB_HID_NO_COUNTRY_CODE, .hid.bNumDescriptors = USB_HID_NUM_DESC, .hid.bRDescriptorType = USB_DT_HID_REPORT, .hid.wDescriptorLength = LE16(sizeof(udi_hid_con_report_desc_t)), .ep_out.bLength = sizeof(usb_ep_desc_t), .ep_out.bDescriptorType = USB_DT_ENDPOINT, .ep_out.bEndpointAddress = UDI_HID_CON_EP_OUT | USB_EP_DIR_OUT, .ep_out.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep_out.wMaxPacketSize = LE16(CONSOLE_EPSIZE), .ep_out.bInterval = CON_POLLING_INTERVAL, \ + .ep_in.bLength = sizeof(usb_ep_desc_t), .ep_in.bDescriptorType = USB_DT_ENDPOINT, .ep_in.bEndpointAddress = UDI_HID_CON_EP_IN | USB_EP_DIR_IN, .ep_in.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep_in.wMaxPacketSize = LE16(CONSOLE_EPSIZE), .ep_in.bInterval = CON_POLLING_INTERVAL, \ + } + +# define UDI_HID_CON_REPORT_SIZE CONSOLE_EPSIZE extern uint8_t udi_hid_con_report_set[UDI_HID_CON_REPORT_SIZE]; -//report buffer +// report buffer extern uint8_t udi_hid_con_report[UDI_HID_CON_REPORT_SIZE]; COMPILER_PACK_RESET() -#endif //CON +#endif // CON // ********************************************************************** // CDC Descriptor structure and content @@ -662,86 +522,28 @@ typedef struct { } usb_cdc_union_desc_t; typedef struct { - usb_association_desc_t iaface; - usb_iface_desc_t iface_c; - usb_cdc_hdr_desc_t fd; + usb_association_desc_t iaface; + usb_iface_desc_t iface_c; + usb_cdc_hdr_desc_t fd; usb_cdc_call_mgmt_desc_t mfd; - usb_cdc_acm_desc_t acmd; - usb_cdc_union_desc_t ufd; - usb_ep_desc_t ep_c; - usb_iface_desc_t iface_d; - usb_ep_desc_t ep_tx; - usb_ep_desc_t ep_rx; + usb_cdc_acm_desc_t acmd; + usb_cdc_union_desc_t ufd; + usb_ep_desc_t ep_c; + usb_iface_desc_t iface_d; + usb_ep_desc_t ep_tx; + usb_ep_desc_t ep_rx; } udi_cdc_desc_t; -#define CDC_DESCRIPTOR {\ - .iaface.bLength = sizeof(usb_association_desc_t),\ - .iaface.bDescriptorType = USB_DT_IAD,\ - .iaface.bFirstInterface = CDC_STATUS_INTERFACE,\ - .iaface.bInterfaceCount = 2,\ - .iaface.bFunctionClass = CDC_CLASS_DEVICE,\ - .iaface.bFunctionSubClass = CDC_SUBCLASS_ACM,\ - .iaface.bFunctionProtocol = CDC_PROTOCOL_V25TER,\ - .iaface.iFunction = 0,\ - .iface_c.bLength = sizeof(usb_iface_desc_t),\ - .iface_c.bDescriptorType = USB_DT_INTERFACE,\ - .iface_c.bInterfaceNumber = CDC_STATUS_INTERFACE,\ - .iface_c.bAlternateSetting = 0,\ - .iface_c.bNumEndpoints = 1,\ - .iface_c.bInterfaceClass = 0x02,\ - .iface_c.bInterfaceSubClass = 0x02,\ - .iface_c.bInterfaceProtocol = CDC_PROTOCOL_V25TER,\ - .iface_c.iInterface = 0,\ - .fd.bFunctionLength = sizeof(usb_cdc_hdr_desc_t),\ - .fd.bDescriptorType = CDC_CS_INTERFACE,\ - .fd.bDescriptorSubtype = CDC_SCS_HEADER,\ - .fd.bcdCDC = 0x0110,\ - .mfd.bFunctionLength = sizeof(usb_cdc_call_mgmt_desc_t),\ - .mfd.bDescriptorType = CDC_CS_INTERFACE,\ - .mfd.bDescriptorSubtype = CDC_SCS_CALL_MGMT,\ - .mfd.bmCapabilities = CDC_CALL_MGMT_SUPPORTED,\ - .mfd.bDataInterface = CDC_DATA_INTERFACE,\ - .acmd.bFunctionLength = sizeof(usb_cdc_acm_desc_t),\ - .acmd.bDescriptorType = CDC_CS_INTERFACE,\ - .acmd.bDescriptorSubtype = CDC_SCS_ACM,\ - .acmd.bmCapabilities = CDC_ACM_SUPPORT_LINE_REQUESTS,\ - .ufd.bFunctionLength = sizeof(usb_cdc_union_desc_t),\ - .ufd.bDescriptorType = CDC_CS_INTERFACE,\ - .ufd.bDescriptorSubtype = CDC_SCS_UNION,\ - .ufd.bMasterInterface = CDC_STATUS_INTERFACE,\ - .ufd.bSlaveInterface0 = CDC_DATA_INTERFACE,\ - .ep_c.bLength = sizeof(usb_ep_desc_t),\ - .ep_c.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_c.bEndpointAddress = CDC_ACM_ENDPOINT | USB_EP_DIR_IN,\ - .ep_c.bmAttributes = USB_EP_TYPE_INTERRUPT,\ - .ep_c.wMaxPacketSize = LE16(CDC_ACM_SIZE),\ - .ep_c.bInterval = CDC_EP_INTERVAL_STATUS,\ - .iface_d.bLength = sizeof(usb_iface_desc_t),\ - .iface_d.bDescriptorType = USB_DT_INTERFACE,\ - .iface_d.bInterfaceNumber = CDC_DATA_INTERFACE,\ - .iface_d.bAlternateSetting = 0,\ - .iface_d.bNumEndpoints = 2,\ - .iface_d.bInterfaceClass = CDC_CLASS_DATA,\ - .iface_d.bInterfaceSubClass = 0,\ - .iface_d.bInterfaceProtocol = 0,\ - .iface_d.iInterface = 0,\ - .ep_rx.bLength = sizeof(usb_ep_desc_t),\ - .ep_rx.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_rx.bEndpointAddress = CDC_RX_ENDPOINT | USB_EP_DIR_OUT,\ - .ep_rx.bmAttributes = USB_EP_TYPE_BULK,\ - .ep_rx.wMaxPacketSize = LE16(CDC_RX_SIZE),\ - .ep_rx.bInterval = CDC_EP_INTERVAL_DATA,\ - .ep_tx.bLength = sizeof(usb_ep_desc_t),\ - .ep_tx.bDescriptorType = USB_DT_ENDPOINT,\ - .ep_tx.bEndpointAddress = CDC_TX_ENDPOINT | USB_EP_DIR_IN,\ - .ep_tx.bmAttributes = USB_EP_TYPE_BULK,\ - .ep_tx.wMaxPacketSize = LE16(CDC_TX_SIZE),\ - .ep_tx.bInterval = CDC_EP_INTERVAL_DATA,\ -} +# define CDC_DESCRIPTOR \ + { \ + .iaface.bLength = sizeof(usb_association_desc_t), .iaface.bDescriptorType = USB_DT_IAD, .iaface.bFirstInterface = CDC_STATUS_INTERFACE, .iaface.bInterfaceCount = 2, .iaface.bFunctionClass = CDC_CLASS_DEVICE, .iaface.bFunctionSubClass = CDC_SUBCLASS_ACM, .iaface.bFunctionProtocol = CDC_PROTOCOL_V25TER, .iaface.iFunction = 0, .iface_c.bLength = sizeof(usb_iface_desc_t), .iface_c.bDescriptorType = USB_DT_INTERFACE, .iface_c.bInterfaceNumber = CDC_STATUS_INTERFACE, .iface_c.bAlternateSetting = 0, .iface_c.bNumEndpoints = 1, .iface_c.bInterfaceClass = 0x02, .iface_c.bInterfaceSubClass = 0x02, .iface_c.bInterfaceProtocol = CDC_PROTOCOL_V25TER, .iface_c.iInterface = 0, .fd.bFunctionLength = sizeof(usb_cdc_hdr_desc_t), .fd.bDescriptorType = CDC_CS_INTERFACE, .fd.bDescriptorSubtype = CDC_SCS_HEADER, .fd.bcdCDC = 0x0110, .mfd.bFunctionLength = sizeof(usb_cdc_call_mgmt_desc_t), .mfd.bDescriptorType = CDC_CS_INTERFACE, .mfd.bDescriptorSubtype = CDC_SCS_CALL_MGMT, \ + .mfd.bmCapabilities = CDC_CALL_MGMT_SUPPORTED, .mfd.bDataInterface = CDC_DATA_INTERFACE, .acmd.bFunctionLength = sizeof(usb_cdc_acm_desc_t), .acmd.bDescriptorType = CDC_CS_INTERFACE, .acmd.bDescriptorSubtype = CDC_SCS_ACM, .acmd.bmCapabilities = CDC_ACM_SUPPORT_LINE_REQUESTS, .ufd.bFunctionLength = sizeof(usb_cdc_union_desc_t), .ufd.bDescriptorType = CDC_CS_INTERFACE, .ufd.bDescriptorSubtype = CDC_SCS_UNION, .ufd.bMasterInterface = CDC_STATUS_INTERFACE, .ufd.bSlaveInterface0 = CDC_DATA_INTERFACE, .ep_c.bLength = sizeof(usb_ep_desc_t), .ep_c.bDescriptorType = USB_DT_ENDPOINT, .ep_c.bEndpointAddress = CDC_ACM_ENDPOINT | USB_EP_DIR_IN, .ep_c.bmAttributes = USB_EP_TYPE_INTERRUPT, .ep_c.wMaxPacketSize = LE16(CDC_ACM_SIZE), .ep_c.bInterval = CDC_EP_INTERVAL_STATUS, .iface_d.bLength = sizeof(usb_iface_desc_t), .iface_d.bDescriptorType = USB_DT_INTERFACE, .iface_d.bInterfaceNumber = CDC_DATA_INTERFACE, .iface_d.bAlternateSetting = 0, .iface_d.bNumEndpoints = 2, \ + .iface_d.bInterfaceClass = CDC_CLASS_DATA, .iface_d.bInterfaceSubClass = 0, .iface_d.bInterfaceProtocol = 0, .iface_d.iInterface = 0, .ep_rx.bLength = sizeof(usb_ep_desc_t), .ep_rx.bDescriptorType = USB_DT_ENDPOINT, .ep_rx.bEndpointAddress = CDC_RX_ENDPOINT | USB_EP_DIR_OUT, .ep_rx.bmAttributes = USB_EP_TYPE_BULK, .ep_rx.wMaxPacketSize = LE16(CDC_RX_SIZE), .ep_rx.bInterval = CDC_EP_INTERVAL_DATA, .ep_tx.bLength = sizeof(usb_ep_desc_t), .ep_tx.bDescriptorType = USB_DT_ENDPOINT, .ep_tx.bEndpointAddress = CDC_TX_ENDPOINT | USB_EP_DIR_IN, .ep_tx.bmAttributes = USB_EP_TYPE_BULK, .ep_tx.wMaxPacketSize = LE16(CDC_TX_SIZE), .ep_tx.bInterval = CDC_EP_INTERVAL_DATA, \ + } COMPILER_PACK_RESET() -#endif //CDC +#endif // CDC // ********************************************************************** // CONFIGURATION Descriptor structure and content @@ -778,4 +580,4 @@ typedef struct { COMPILER_PACK_RESET() -#endif //_UDI_DEVICE_CONF_H_ +#endif //_UDI_DEVICE_CONF_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_device_epsize.h b/tmk_core/protocol/arm_atsam/usb/udi_device_epsize.h index 96d03c286..65339bffe 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_device_epsize.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_device_epsize.h @@ -18,15 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef _UDI_DEVICE_EPSIZE_H_ #define _UDI_DEVICE_EPSIZE_H_ -#define KEYBOARD_EPSIZE 8 -#define MOUSE_EPSIZE 8 -#define EXTRAKEY_EPSIZE 8 -#define RAW_EPSIZE 64 -#define CONSOLE_EPSIZE 32 -#define NKRO_EPSIZE 32 -#define MIDI_STREAM_EPSIZE 64 -#define CDC_NOTIFICATION_EPSIZE 8 -#define CDC_EPSIZE 16 - -#endif //_UDI_DEVICE_EPSIZE_H_ +#define KEYBOARD_EPSIZE 8 +#define MOUSE_EPSIZE 8 +#define EXTRAKEY_EPSIZE 8 +#define RAW_EPSIZE 64 +#define CONSOLE_EPSIZE 32 +#define NKRO_EPSIZE 32 +#define MIDI_STREAM_EPSIZE 64 +#define CDC_NOTIFICATION_EPSIZE 8 +#define CDC_EPSIZE 16 +#endif //_UDI_DEVICE_EPSIZE_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid.c b/tmk_core/protocol/arm_atsam/usb/udi_hid.c index 131b7a0ec..b0bdcc693 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid.c +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid.c @@ -50,7 +50,6 @@ #include "udc.h" #include "udi_hid.h" - /** * \ingroup udi_hid_group * \defgroup udi_hid_group_internal Implementation of HID common library @@ -64,34 +63,31 @@ */ static bool udi_hid_reqstdifaceget_descriptor(uint8_t *report_desc); -bool udi_hid_setup( uint8_t *rate, uint8_t *protocol, uint8_t *report_desc, bool (*setup_report)(void) ) -{ +bool udi_hid_setup(uint8_t *rate, uint8_t *protocol, uint8_t *report_desc, bool (*setup_report)(void)) { if (Udd_setup_is_in()) { // Requests Interface GET if (Udd_setup_type() == USB_REQ_TYPE_STANDARD) { // Requests Standard Interface Get switch (udd_g_ctrlreq.req.bRequest) { - - case USB_REQ_GET_DESCRIPTOR: - return udi_hid_reqstdifaceget_descriptor(report_desc); + case USB_REQ_GET_DESCRIPTOR: + return udi_hid_reqstdifaceget_descriptor(report_desc); } } if (Udd_setup_type() == USB_REQ_TYPE_CLASS) { // Requests Class Interface Get switch (udd_g_ctrlreq.req.bRequest) { - - case USB_REQ_HID_GET_REPORT: - return setup_report(); - - case USB_REQ_HID_GET_IDLE: - udd_g_ctrlreq.payload = rate; - udd_g_ctrlreq.payload_size = 1; - return true; - - case USB_REQ_HID_GET_PROTOCOL: - udd_g_ctrlreq.payload = protocol; - udd_g_ctrlreq.payload_size = 1; - return true; + case USB_REQ_HID_GET_REPORT: + return setup_report(); + + case USB_REQ_HID_GET_IDLE: + udd_g_ctrlreq.payload = rate; + udd_g_ctrlreq.payload_size = 1; + return true; + + case USB_REQ_HID_GET_PROTOCOL: + udd_g_ctrlreq.payload = protocol; + udd_g_ctrlreq.payload_size = 1; + return true; } } } @@ -100,60 +96,50 @@ bool udi_hid_setup( uint8_t *rate, uint8_t *protocol, uint8_t *report_desc, bool if (Udd_setup_type() == USB_REQ_TYPE_CLASS) { // Requests Class Interface Set switch (udd_g_ctrlreq.req.bRequest) { + case USB_REQ_HID_SET_REPORT: + return setup_report(); - case USB_REQ_HID_SET_REPORT: - return setup_report(); - - case USB_REQ_HID_SET_IDLE: - *rate = udd_g_ctrlreq.req.wValue >> 8; - return true; + case USB_REQ_HID_SET_IDLE: + *rate = udd_g_ctrlreq.req.wValue >> 8; + return true; - case USB_REQ_HID_SET_PROTOCOL: - if (0 != udd_g_ctrlreq.req.wLength) - return false; - *protocol = udd_g_ctrlreq.req.wValue; - return true; + case USB_REQ_HID_SET_PROTOCOL: + if (0 != udd_g_ctrlreq.req.wLength) return false; + *protocol = udd_g_ctrlreq.req.wValue; + return true; } } } - return false; // Request not supported + return false; // Request not supported } //--------------------------------------------- //------- Internal routines -static bool udi_hid_reqstdifaceget_descriptor(uint8_t *report_desc) -{ +static bool udi_hid_reqstdifaceget_descriptor(uint8_t *report_desc) { usb_hid_descriptor_t UDC_DESC_STORAGE *ptr_hid_desc; // Get the USB descriptor which is located after the interface descriptor // This descriptor must be the HID descriptor - ptr_hid_desc = (usb_hid_descriptor_t UDC_DESC_STORAGE *) ((uint8_t *) - udc_get_interface_desc() + sizeof(usb_iface_desc_t)); - if (USB_DT_HID != ptr_hid_desc->bDescriptorType) - return false; + ptr_hid_desc = (usb_hid_descriptor_t UDC_DESC_STORAGE *)((uint8_t *)udc_get_interface_desc() + sizeof(usb_iface_desc_t)); + if (USB_DT_HID != ptr_hid_desc->bDescriptorType) return false; // The SETUP request can ask for: // - an USB_DT_HID descriptor // - or USB_DT_HID_REPORT descriptor // - or USB_DT_HID_PHYSICAL descriptor - if (USB_DT_HID == (uint8_t) (udd_g_ctrlreq.req.wValue >> 8)) { + if (USB_DT_HID == (uint8_t)(udd_g_ctrlreq.req.wValue >> 8)) { // USB_DT_HID descriptor requested then send it - udd_g_ctrlreq.payload = (uint8_t *) ptr_hid_desc; - udd_g_ctrlreq.payload_size = - min(udd_g_ctrlreq.req.wLength, - ptr_hid_desc->bLength); + udd_g_ctrlreq.payload = (uint8_t *)ptr_hid_desc; + udd_g_ctrlreq.payload_size = min(udd_g_ctrlreq.req.wLength, ptr_hid_desc->bLength); return true; } // The HID_X descriptor requested must correspond to report type // included in the HID descriptor - if (ptr_hid_desc->bRDescriptorType == - (uint8_t) (udd_g_ctrlreq.req.wValue >> 8)) { + if (ptr_hid_desc->bRDescriptorType == (uint8_t)(udd_g_ctrlreq.req.wValue >> 8)) { // Send HID Report descriptor given by high level - udd_g_ctrlreq.payload = report_desc; - udd_g_ctrlreq.payload_size = - min(udd_g_ctrlreq.req.wLength, - le16_to_cpu(ptr_hid_desc->wDescriptorLength)); + udd_g_ctrlreq.payload = report_desc; + udd_g_ctrlreq.payload_size = min(udd_g_ctrlreq.req.wLength, le16_to_cpu(ptr_hid_desc->wDescriptorLength)); return true; } return false; diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid.h b/tmk_core/protocol/arm_atsam/usb/udi_hid.h index 0edb09c1c..6fd2cabd4 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid.h @@ -75,11 +75,11 @@ extern "C" { * * \return \c 1 if function was successfully done, otherwise \c 0. */ -bool udi_hid_setup( uint8_t *rate, uint8_t *protocol, uint8_t *report_desc, bool (*setup_report)(void) ); +bool udi_hid_setup(uint8_t *rate, uint8_t *protocol, uint8_t *report_desc, bool (*setup_report)(void)); //@} #ifdef __cplusplus } #endif -#endif // _UDI_HID_H_ +#endif // _UDI_HID_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.c b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.c index b715b7026..c3952d50c 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.c +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.c @@ -61,15 +61,15 @@ //*************************************************************************** #ifdef KBD -bool udi_hid_kbd_enable(void); -void udi_hid_kbd_disable(void); -bool udi_hid_kbd_setup(void); +bool udi_hid_kbd_enable(void); +void udi_hid_kbd_disable(void); +bool udi_hid_kbd_setup(void); uint8_t udi_hid_kbd_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_kbd = { - .enable = (bool(*)(void))udi_hid_kbd_enable, - .disable = (void (*)(void))udi_hid_kbd_disable, - .setup = (bool(*)(void))udi_hid_kbd_setup, + .enable = (bool (*)(void))udi_hid_kbd_enable, + .disable = (void (*)(void))udi_hid_kbd_disable, + .setup = (bool (*)(void))udi_hid_kbd_setup, .getsetting = (uint8_t(*)(void))udi_hid_kbd_getsetting, .sof_notify = NULL, }; @@ -94,40 +94,38 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_kbd_report_trans[UDI_HID_KBD_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_kbd_report_desc_t udi_hid_kbd_report_desc = { - { - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x06, // Usage (Keyboard) - 0xA1, 0x01, // Collection (Application) - 0x05, 0x07, // Usage Page (Keyboard) - 0x19, 0xE0, // Usage Minimum (224) - 0x29, 0xE7, // Usage Maximum (231) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x08, // Report Count (8) - 0x81, 0x02, // Input (Data, Variable, Absolute) - 0x81, 0x01, // Input (Constant) - 0x19, 0x00, // Usage Minimum (0) - 0x29, 0xFF, // Usage Maximum (255) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0xFF, // Logical Maximum (255) - 0x75, 0x08, // Report Size (8) - 0x95, 0x06, // Report Count (6) - 0x81, 0x00, // Input (Data, Array) - 0x05, 0x08, // Usage Page (LED) - 0x19, 0x01, // Usage Minimum (1) - 0x29, 0x05, // Usage Maximum (5) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x05, // Report Count (5) - 0x91, 0x02, // Output (Data, Variable, Absolute) - 0x95, 0x03, // Report Count (3) - 0x91, 0x01, // Output (Constant) - 0xC0 // End Collection - } -}; +UDC_DESC_STORAGE udi_hid_kbd_report_desc_t udi_hid_kbd_report_desc = {{ + 0x05, 0x01, // Usage Page (Generic Desktop) + 0x09, 0x06, // Usage (Keyboard) + 0xA1, 0x01, // Collection (Application) + 0x05, 0x07, // Usage Page (Keyboard) + 0x19, 0xE0, // Usage Minimum (224) + 0x29, 0xE7, // Usage Maximum (231) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x08, // Report Count (8) + 0x81, 0x02, // Input (Data, Variable, Absolute) + 0x81, 0x01, // Input (Constant) + 0x19, 0x00, // Usage Minimum (0) + 0x29, 0xFF, // Usage Maximum (255) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0xFF, // Logical Maximum (255) + 0x75, 0x08, // Report Size (8) + 0x95, 0x06, // Report Count (6) + 0x81, 0x00, // Input (Data, Array) + 0x05, 0x08, // Usage Page (LED) + 0x19, 0x01, // Usage Minimum (1) + 0x29, 0x05, // Usage Maximum (5) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x05, // Report Count (5) + 0x91, 0x02, // Output (Data, Variable, Absolute) + 0x95, 0x03, // Report Count (3) + 0x91, 0x01, // Output (Constant) + 0xC0 // End Collection +}}; static bool udi_hid_kbd_setreport(void); @@ -135,51 +133,34 @@ static void udi_hid_kbd_report_sent(udd_ep_status_t status, iram_size_t nb_sent, static void udi_hid_kbd_setreport_valid(void); -bool udi_hid_kbd_enable(void) -{ +bool udi_hid_kbd_enable(void) { // Initialize internal values - udi_hid_kbd_rate = 0; - udi_hid_kbd_protocol = 0; + udi_hid_kbd_rate = 0; + udi_hid_kbd_protocol = 0; udi_hid_kbd_b_report_trans_ongoing = false; memset(udi_hid_kbd_report, 0, UDI_HID_KBD_REPORT_SIZE); udi_hid_kbd_b_report_valid = false; return UDI_HID_KBD_ENABLE_EXT(); } -void udi_hid_kbd_disable(void) -{ - UDI_HID_KBD_DISABLE_EXT(); -} +void udi_hid_kbd_disable(void) { UDI_HID_KBD_DISABLE_EXT(); } -bool udi_hid_kbd_setup(void) -{ - return udi_hid_setup(&udi_hid_kbd_rate, - &udi_hid_kbd_protocol, - (uint8_t *) &udi_hid_kbd_report_desc, - udi_hid_kbd_setreport); -} +bool udi_hid_kbd_setup(void) { return udi_hid_setup(&udi_hid_kbd_rate, &udi_hid_kbd_protocol, (uint8_t *)&udi_hid_kbd_report_desc, udi_hid_kbd_setreport); } -uint8_t udi_hid_kbd_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_kbd_getsetting(void) { return 0; } -static bool udi_hid_kbd_setreport(void) -{ - if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) - && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) - && (1 == udd_g_ctrlreq.req.wLength)) { +static bool udi_hid_kbd_setreport(void) { + if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) && (1 == udd_g_ctrlreq.req.wLength)) { // Report OUT type on report ID 0 from USB Host - udd_g_ctrlreq.payload = &udi_hid_kbd_report_set; - udd_g_ctrlreq.callback = udi_hid_kbd_setreport_valid; + udd_g_ctrlreq.payload = &udi_hid_kbd_report_set; + udd_g_ctrlreq.callback = udi_hid_kbd_setreport_valid; udd_g_ctrlreq.payload_size = 1; return true; } return false; } -bool udi_hid_kbd_send_report(void) -{ +bool udi_hid_kbd_send_report(void) { if (!main_b_kbd_enable) { return false; } @@ -189,19 +170,13 @@ bool udi_hid_kbd_send_report(void) } memcpy(udi_hid_kbd_report_trans, udi_hid_kbd_report, UDI_HID_KBD_REPORT_SIZE); - udi_hid_kbd_b_report_valid = false; - udi_hid_kbd_b_report_trans_ongoing = - udd_ep_run(UDI_HID_KBD_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_kbd_report_trans, - UDI_HID_KBD_REPORT_SIZE, - udi_hid_kbd_report_sent); + udi_hid_kbd_b_report_valid = false; + udi_hid_kbd_b_report_trans_ongoing = udd_ep_run(UDI_HID_KBD_EP_IN | USB_EP_DIR_IN, false, udi_hid_kbd_report_trans, UDI_HID_KBD_REPORT_SIZE, udi_hid_kbd_report_sent); return udi_hid_kbd_b_report_trans_ongoing; } -static void udi_hid_kbd_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_kbd_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -211,27 +186,26 @@ static void udi_hid_kbd_report_sent(udd_ep_status_t status, iram_size_t nb_sent, } } -static void udi_hid_kbd_setreport_valid(void) -{ - //UDI_HID_KBD_CHANGE_LED(udi_hid_kbd_report_set); +static void udi_hid_kbd_setreport_valid(void) { + // UDI_HID_KBD_CHANGE_LED(udi_hid_kbd_report_set); } -#endif //KBD +#endif // KBD //******************************************************************************************** // NKRO Keyboard //******************************************************************************************** #ifdef NKRO -bool udi_hid_nkro_enable(void); -void udi_hid_nkro_disable(void); -bool udi_hid_nkro_setup(void); +bool udi_hid_nkro_enable(void); +void udi_hid_nkro_disable(void); +bool udi_hid_nkro_setup(void); uint8_t udi_hid_nkro_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_nkro = { - .enable = (bool(*)(void))udi_hid_nkro_enable, - .disable = (void (*)(void))udi_hid_nkro_disable, - .setup = (bool(*)(void))udi_hid_nkro_setup, + .enable = (bool (*)(void))udi_hid_nkro_enable, + .disable = (void (*)(void))udi_hid_nkro_disable, + .setup = (bool (*)(void))udi_hid_nkro_setup, .getsetting = (uint8_t(*)(void))udi_hid_nkro_getsetting, .sof_notify = NULL, }; @@ -256,98 +230,79 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_nkro_report_trans[UDI_HID_NKRO_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_nkro_report_desc_t udi_hid_nkro_report_desc = { - { - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x06, // Usage (Keyboard), - 0xA1, 0x01, // Collection (Application) - Keyboard, - - //Mods - 0x75, 0x01, // Report Size (1), - 0x95, 0x08, // Report Count (8), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0xE0, // Usage Minimum (224), - 0x29, 0xE7, // Usage Maximum (231), - 0x81, 0x02, // Input (Data, Variable, Absolute), - - //LED Report - 0x75, 0x01, // Report Size (1), - 0x95, 0x05, // Report Count (5), - 0x05, 0x08, // Usage Page (LEDs), - 0x19, 0x01, // Usage Minimum (1), - 0x29, 0x05, // Usage Maximum (5), - 0x91, 0x02, // Output (Data, Variable, Absolute), - - //LED Report Padding - 0x75, 0x03, // Report Size (3), - 0x95, 0x01, // Report Count (1), - 0x91, 0x03, // Output (Constant), - - //Main keys - 0x75, 0x01, // Report Size (1), - 0x95, 0xF8, // Report Count (248), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0x00, // Usage Minimum (0), - 0x29, 0xF7, // Usage Maximum (247), - 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield), - 0xc0, // End Collection - Keyboard - } -}; +UDC_DESC_STORAGE udi_hid_nkro_report_desc_t udi_hid_nkro_report_desc = {{ + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x06, // Usage (Keyboard), + 0xA1, 0x01, // Collection (Application) - Keyboard, + + // Mods + 0x75, 0x01, // Report Size (1), + 0x95, 0x08, // Report Count (8), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0xE0, // Usage Minimum (224), + 0x29, 0xE7, // Usage Maximum (231), + 0x81, 0x02, // Input (Data, Variable, Absolute), + + // LED Report + 0x75, 0x01, // Report Size (1), + 0x95, 0x05, // Report Count (5), + 0x05, 0x08, // Usage Page (LEDs), + 0x19, 0x01, // Usage Minimum (1), + 0x29, 0x05, // Usage Maximum (5), + 0x91, 0x02, // Output (Data, Variable, Absolute), + + // LED Report Padding + 0x75, 0x03, // Report Size (3), + 0x95, 0x01, // Report Count (1), + 0x91, 0x03, // Output (Constant), + + // Main keys + 0x75, 0x01, // Report Size (1), + 0x95, 0xF8, // Report Count (248), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0x00, // Usage Minimum (0), + 0x29, 0xF7, // Usage Maximum (247), + 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield), + 0xc0, // End Collection - Keyboard +}}; static bool udi_hid_nkro_setreport(void); static void udi_hid_nkro_setreport_valid(void); static void udi_hid_nkro_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep); -bool udi_hid_nkro_enable(void) -{ +bool udi_hid_nkro_enable(void) { // Initialize internal values - udi_hid_nkro_rate = 0; - udi_hid_nkro_protocol = 0; + udi_hid_nkro_rate = 0; + udi_hid_nkro_protocol = 0; udi_hid_nkro_b_report_trans_ongoing = false; memset(udi_hid_nkro_report, 0, UDI_HID_NKRO_REPORT_SIZE); udi_hid_nkro_b_report_valid = false; return UDI_HID_NKRO_ENABLE_EXT(); } -void udi_hid_nkro_disable(void) -{ - UDI_HID_NKRO_DISABLE_EXT(); -} +void udi_hid_nkro_disable(void) { UDI_HID_NKRO_DISABLE_EXT(); } -bool udi_hid_nkro_setup(void) -{ - return udi_hid_setup(&udi_hid_nkro_rate, - &udi_hid_nkro_protocol, - (uint8_t *) &udi_hid_nkro_report_desc, - udi_hid_nkro_setreport); -} +bool udi_hid_nkro_setup(void) { return udi_hid_setup(&udi_hid_nkro_rate, &udi_hid_nkro_protocol, (uint8_t *)&udi_hid_nkro_report_desc, udi_hid_nkro_setreport); } -uint8_t udi_hid_nkro_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_nkro_getsetting(void) { return 0; } -//keyboard receives LED report here -static bool udi_hid_nkro_setreport(void) -{ - if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) - && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) - && (1 == udd_g_ctrlreq.req.wLength)) { +// keyboard receives LED report here +static bool udi_hid_nkro_setreport(void) { + if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) && (1 == udd_g_ctrlreq.req.wLength)) { // Report OUT type on report ID 0 from USB Host - udd_g_ctrlreq.payload = &udi_hid_nkro_report_set; - udd_g_ctrlreq.callback = udi_hid_nkro_setreport_valid; //must call routine to transform setreport to LED state + udd_g_ctrlreq.payload = &udi_hid_nkro_report_set; + udd_g_ctrlreq.callback = udi_hid_nkro_setreport_valid; // must call routine to transform setreport to LED state udd_g_ctrlreq.payload_size = 1; return true; } return false; } -bool udi_hid_nkro_send_report(void) -{ +bool udi_hid_nkro_send_report(void) { if (!main_b_nkro_enable) { return false; } @@ -357,19 +312,13 @@ bool udi_hid_nkro_send_report(void) } memcpy(udi_hid_nkro_report_trans, udi_hid_nkro_report, UDI_HID_NKRO_REPORT_SIZE); - udi_hid_nkro_b_report_valid = false; - udi_hid_nkro_b_report_trans_ongoing = - udd_ep_run(UDI_HID_NKRO_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_nkro_report_trans, - UDI_HID_NKRO_REPORT_SIZE, - udi_hid_nkro_report_sent); + udi_hid_nkro_b_report_valid = false; + udi_hid_nkro_b_report_trans_ongoing = udd_ep_run(UDI_HID_NKRO_EP_IN | USB_EP_DIR_IN, false, udi_hid_nkro_report_trans, UDI_HID_NKRO_REPORT_SIZE, udi_hid_nkro_report_sent); return udi_hid_nkro_b_report_trans_ongoing; } -static void udi_hid_nkro_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_nkro_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -379,27 +328,26 @@ static void udi_hid_nkro_report_sent(udd_ep_status_t status, iram_size_t nb_sent } } -static void udi_hid_nkro_setreport_valid(void) -{ - //UDI_HID_NKRO_CHANGE_LED(udi_hid_nkro_report_set); +static void udi_hid_nkro_setreport_valid(void) { + // UDI_HID_NKRO_CHANGE_LED(udi_hid_nkro_report_set); } -#endif //NKRO +#endif // NKRO //******************************************************************************************** // EXK (extra-keys) SYS-CTRL Keyboard //******************************************************************************************** #ifdef EXK -bool udi_hid_exk_enable(void); -void udi_hid_exk_disable(void); -bool udi_hid_exk_setup(void); +bool udi_hid_exk_enable(void); +void udi_hid_exk_disable(void); +bool udi_hid_exk_setup(void); uint8_t udi_hid_exk_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_exk = { - .enable = (bool(*)(void))udi_hid_exk_enable, - .disable = (void (*)(void))udi_hid_exk_disable, - .setup = (bool(*)(void))udi_hid_exk_setup, + .enable = (bool (*)(void))udi_hid_exk_enable, + .disable = (void (*)(void))udi_hid_exk_disable, + .setup = (bool (*)(void))udi_hid_exk_setup, .getsetting = (uint8_t(*)(void))udi_hid_exk_getsetting, .sof_notify = NULL, }; @@ -424,39 +372,37 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_exk_report_trans[UDI_HID_EXK_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_exk_report_desc_t udi_hid_exk_report_desc = { - { - // System Control Collection (8 bits) - - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x80, // Usage (System Control), - 0xA1, 0x01, // Collection (Application), - 0x85, REPORT_ID_SYSTEM, // Report ID (2) (System), - 0x16, 0x01, 0x00, // Logical Minimum (1), - 0x26, 0x03, 0x00, // Logical Maximum (3), - 0x1A, 0x81, 0x00, // Usage Minimum (81) (System Power Down), - 0x2A, 0x83, 0x00, // Usage Maximum (83) (System Wake Up), - 0x75, 0x10, // Report Size (16), - 0x95, 0x01, // Report Count (1), - 0x81, 0x00, // Input (Data, Array), - 0xC0, // End Collection - System Control - - // Consumer Control Collection - Media Keys (16 bits) - - 0x05, 0x0C, // Usage Page (Consumer), - 0x09, 0x01, // Usage (Consumer Control), - 0xA1, 0x01, // Collection (Application), - 0x85, REPORT_ID_CONSUMER, // Report ID (3) (Consumer), - 0x16, 0x01, 0x00, // Logical Minimum (1), - 0x26, 0x9C, 0x02, // Logical Maximum (668), - 0x1A, 0x01, 0x00, // Usage Minimum (1), - 0x2A, 0x9C, 0x02, // Usage Maximum (668), - 0x75, 0x10, // Report Size (16), - 0x95, 0x01, // Report Count (1), - 0x81, 0x00, // Input (Data, Array), - 0xC0, // End Collection - Consumer Control - } -}; +UDC_DESC_STORAGE udi_hid_exk_report_desc_t udi_hid_exk_report_desc = {{ + // System Control Collection (8 bits) + + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x80, // Usage (System Control), + 0xA1, 0x01, // Collection (Application), + 0x85, REPORT_ID_SYSTEM, // Report ID (2) (System), + 0x16, 0x01, 0x00, // Logical Minimum (1), + 0x26, 0x03, 0x00, // Logical Maximum (3), + 0x1A, 0x81, 0x00, // Usage Minimum (81) (System Power Down), + 0x2A, 0x83, 0x00, // Usage Maximum (83) (System Wake Up), + 0x75, 0x10, // Report Size (16), + 0x95, 0x01, // Report Count (1), + 0x81, 0x00, // Input (Data, Array), + 0xC0, // End Collection - System Control + + // Consumer Control Collection - Media Keys (16 bits) + + 0x05, 0x0C, // Usage Page (Consumer), + 0x09, 0x01, // Usage (Consumer Control), + 0xA1, 0x01, // Collection (Application), + 0x85, REPORT_ID_CONSUMER, // Report ID (3) (Consumer), + 0x16, 0x01, 0x00, // Logical Minimum (1), + 0x26, 0x9C, 0x02, // Logical Maximum (668), + 0x1A, 0x01, 0x00, // Usage Minimum (1), + 0x2A, 0x9C, 0x02, // Usage Maximum (668), + 0x75, 0x10, // Report Size (16), + 0x95, 0x01, // Report Count (1), + 0x81, 0x00, // Input (Data, Array), + 0xC0, // End Collection - Consumer Control +}}; static bool udi_hid_exk_setreport(void); @@ -464,51 +410,34 @@ static void udi_hid_exk_report_sent(udd_ep_status_t status, iram_size_t nb_sent, static void udi_hid_exk_setreport_valid(void); -bool udi_hid_exk_enable(void) -{ +bool udi_hid_exk_enable(void) { // Initialize internal values - udi_hid_exk_rate = 0; - udi_hid_exk_protocol = 0; + udi_hid_exk_rate = 0; + udi_hid_exk_protocol = 0; udi_hid_exk_b_report_trans_ongoing = false; memset(udi_hid_exk_report.raw, 0, UDI_HID_EXK_REPORT_SIZE); udi_hid_exk_b_report_valid = false; return UDI_HID_EXK_ENABLE_EXT(); } -void udi_hid_exk_disable(void) -{ - UDI_HID_EXK_DISABLE_EXT(); -} +void udi_hid_exk_disable(void) { UDI_HID_EXK_DISABLE_EXT(); } -bool udi_hid_exk_setup(void) -{ - return udi_hid_setup(&udi_hid_exk_rate, - &udi_hid_exk_protocol, - (uint8_t *) &udi_hid_exk_report_desc, - udi_hid_exk_setreport); -} +bool udi_hid_exk_setup(void) { return udi_hid_setup(&udi_hid_exk_rate, &udi_hid_exk_protocol, (uint8_t *)&udi_hid_exk_report_desc, udi_hid_exk_setreport); } -uint8_t udi_hid_exk_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_exk_getsetting(void) { return 0; } -static bool udi_hid_exk_setreport(void) -{ - if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) - && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) - && (1 == udd_g_ctrlreq.req.wLength)) { +static bool udi_hid_exk_setreport(void) { + if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) && (1 == udd_g_ctrlreq.req.wLength)) { // Report OUT type on report ID 0 from USB Host - udd_g_ctrlreq.payload = &udi_hid_exk_report_set; - udd_g_ctrlreq.callback = udi_hid_exk_setreport_valid; + udd_g_ctrlreq.payload = &udi_hid_exk_report_set; + udd_g_ctrlreq.callback = udi_hid_exk_setreport_valid; udd_g_ctrlreq.payload_size = 1; return true; } return false; } -bool udi_hid_exk_send_report(void) -{ +bool udi_hid_exk_send_report(void) { if (!main_b_exk_enable) { return false; } @@ -518,19 +447,13 @@ bool udi_hid_exk_send_report(void) } memcpy(udi_hid_exk_report_trans, udi_hid_exk_report.raw, UDI_HID_EXK_REPORT_SIZE); - udi_hid_exk_b_report_valid = false; - udi_hid_exk_b_report_trans_ongoing = - udd_ep_run(UDI_HID_EXK_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_exk_report_trans, - UDI_HID_EXK_REPORT_SIZE, - udi_hid_exk_report_sent); + udi_hid_exk_b_report_valid = false; + udi_hid_exk_b_report_trans_ongoing = udd_ep_run(UDI_HID_EXK_EP_IN | USB_EP_DIR_IN, false, udi_hid_exk_report_trans, UDI_HID_EXK_REPORT_SIZE, udi_hid_exk_report_sent); return udi_hid_exk_b_report_trans_ongoing; } -static void udi_hid_exk_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_exk_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -540,27 +463,24 @@ static void udi_hid_exk_report_sent(udd_ep_status_t status, iram_size_t nb_sent, } } -static void udi_hid_exk_setreport_valid(void) -{ +static void udi_hid_exk_setreport_valid(void) {} -} - -#endif //EXK +#endif // EXK //******************************************************************************************** // MOU Mouse //******************************************************************************************** #ifdef MOU -bool udi_hid_mou_enable(void); -void udi_hid_mou_disable(void); -bool udi_hid_mou_setup(void); +bool udi_hid_mou_enable(void); +void udi_hid_mou_disable(void); +bool udi_hid_mou_setup(void); uint8_t udi_hid_mou_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_mou = { - .enable = (bool(*)(void))udi_hid_mou_enable, - .disable = (void (*)(void))udi_hid_mou_disable, - .setup = (bool(*)(void))udi_hid_mou_setup, + .enable = (bool (*)(void))udi_hid_mou_enable, + .disable = (void (*)(void))udi_hid_mou_disable, + .setup = (bool (*)(void))udi_hid_mou_setup, .getsetting = (uint8_t(*)(void))udi_hid_mou_getsetting, .sof_notify = NULL, }; @@ -571,8 +491,8 @@ static uint8_t udi_hid_mou_rate; COMPILER_WORD_ALIGNED static uint8_t udi_hid_mou_protocol; -//COMPILER_WORD_ALIGNED -//uint8_t udi_hid_mou_report_set; //No set report +// COMPILER_WORD_ALIGNED +// uint8_t udi_hid_mou_report_set; //No set report bool udi_hid_mou_b_report_valid; @@ -585,87 +505,71 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_mou_report_trans[UDI_HID_MOU_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_mou_report_desc_t udi_hid_mou_report_desc = { - { - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x02, // Usage (Mouse), - 0xA1, 0x01, // Collection (Application), - 0x09, 0x01, // Usage (Pointer), - 0xA1, 0x00, // Collection (Physical), - 0x05, 0x09, // Usage Page (Buttons), - 0x19, 0x01, // Usage Minimum (01), - 0x29, 0x05, // Usage Maximun (05), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x95, 0x05, // Report Count (5), - 0x75, 0x01, // Report Size (1), - 0x81, 0x02, // Input (Data, Variable, Absolute), ;5 button bits - 0x95, 0x01, // Report Count (1), - 0x75, 0x03, // Report Size (3), - 0x81, 0x01, // Input (Constant), ;3 bit padding, - - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x30, // Usage (X), - 0x09, 0x31, // Usage (Y), - 0x15, 0x81, // Logical Minimum (-127), - 0x25, 0x7F, // Logical Maximum (127), - 0x95, 0x02, // Report Count (2), - 0x75, 0x08, // Report Size (8), - 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y), - - 0x09, 0x38, // Usage (Wheel), - 0x15, 0x81, // Logical Minimum (-127), - 0x25, 0x7F, // Logical Maximum (127), - 0x95, 0x01, // Report Count (1), - 0x75, 0x08, // Report Size (8), - 0x81, 0x06, // Input (Data, Variable, Relative), - - 0x05, 0x0C, // Usage Page (Consumer), - 0x0A, 0x38, 0x02, // Usage (AC Pan (Horizontal wheel)), - 0x15, 0x81, // Logical Minimum (-127), - 0x25, 0x7F, // Logical Maximum (127), - 0x95, 0x01, // Report Count (1), - 0x75, 0x08, // Report Size (8), - 0x81, 0x06, // Input (Data, Variable, Relative), - - 0xC0, // End Collection, - 0xC0, // End Collection - } -}; +UDC_DESC_STORAGE udi_hid_mou_report_desc_t udi_hid_mou_report_desc = {{ + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x02, // Usage (Mouse), + 0xA1, 0x01, // Collection (Application), + 0x09, 0x01, // Usage (Pointer), + 0xA1, 0x00, // Collection (Physical), + 0x05, 0x09, // Usage Page (Buttons), + 0x19, 0x01, // Usage Minimum (01), + 0x29, 0x05, // Usage Maximun (05), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x95, 0x05, // Report Count (5), + 0x75, 0x01, // Report Size (1), + 0x81, 0x02, // Input (Data, Variable, Absolute), ;5 button bits + 0x95, 0x01, // Report Count (1), + 0x75, 0x03, // Report Size (3), + 0x81, 0x01, // Input (Constant), ;3 bit padding, + + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x30, // Usage (X), + 0x09, 0x31, // Usage (Y), + 0x15, 0x81, // Logical Minimum (-127), + 0x25, 0x7F, // Logical Maximum (127), + 0x95, 0x02, // Report Count (2), + 0x75, 0x08, // Report Size (8), + 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y), + + 0x09, 0x38, // Usage (Wheel), + 0x15, 0x81, // Logical Minimum (-127), + 0x25, 0x7F, // Logical Maximum (127), + 0x95, 0x01, // Report Count (1), + 0x75, 0x08, // Report Size (8), + 0x81, 0x06, // Input (Data, Variable, Relative), + + 0x05, 0x0C, // Usage Page (Consumer), + 0x0A, 0x38, 0x02, // Usage (AC Pan (Horizontal wheel)), + 0x15, 0x81, // Logical Minimum (-127), + 0x25, 0x7F, // Logical Maximum (127), + 0x95, 0x01, // Report Count (1), + 0x75, 0x08, // Report Size (8), + 0x81, 0x06, // Input (Data, Variable, Relative), + + 0xC0, // End Collection, + 0xC0, // End Collection +}}; static void udi_hid_mou_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep); -bool udi_hid_mou_enable(void) -{ +bool udi_hid_mou_enable(void) { // Initialize internal values - udi_hid_mou_rate = 0; - udi_hid_mou_protocol = 0; + udi_hid_mou_rate = 0; + udi_hid_mou_protocol = 0; udi_hid_mou_b_report_trans_ongoing = false; memset(udi_hid_mou_report, 0, UDI_HID_MOU_REPORT_SIZE); udi_hid_mou_b_report_valid = false; return UDI_HID_MOU_ENABLE_EXT(); } -void udi_hid_mou_disable(void) -{ - UDI_HID_MOU_DISABLE_EXT(); -} +void udi_hid_mou_disable(void) { UDI_HID_MOU_DISABLE_EXT(); } -bool udi_hid_mou_setup(void) -{ - return udi_hid_setup(&udi_hid_mou_rate, - &udi_hid_mou_protocol, - (uint8_t *) &udi_hid_mou_report_desc, - NULL); -} +bool udi_hid_mou_setup(void) { return udi_hid_setup(&udi_hid_mou_rate, &udi_hid_mou_protocol, (uint8_t *)&udi_hid_mou_report_desc, NULL); } -uint8_t udi_hid_mou_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_mou_getsetting(void) { return 0; } -bool udi_hid_mou_send_report(void) -{ +bool udi_hid_mou_send_report(void) { if (!main_b_mou_enable) { return false; } @@ -675,19 +579,13 @@ bool udi_hid_mou_send_report(void) } memcpy(udi_hid_mou_report_trans, udi_hid_mou_report, UDI_HID_MOU_REPORT_SIZE); - udi_hid_mou_b_report_valid = false; - udi_hid_mou_b_report_trans_ongoing = - udd_ep_run(UDI_HID_MOU_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_mou_report_trans, - UDI_HID_MOU_REPORT_SIZE, - udi_hid_mou_report_sent); + udi_hid_mou_b_report_valid = false; + udi_hid_mou_b_report_trans_ongoing = udd_ep_run(UDI_HID_MOU_EP_IN | USB_EP_DIR_IN, false, udi_hid_mou_report_trans, UDI_HID_MOU_REPORT_SIZE, udi_hid_mou_report_sent); return udi_hid_mou_b_report_trans_ongoing; } -static void udi_hid_mou_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_mou_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -697,22 +595,22 @@ static void udi_hid_mou_report_sent(udd_ep_status_t status, iram_size_t nb_sent, } } -#endif //MOU +#endif // MOU //******************************************************************************************** // RAW //******************************************************************************************** #ifdef RAW -bool udi_hid_raw_enable(void); -void udi_hid_raw_disable(void); -bool udi_hid_raw_setup(void); +bool udi_hid_raw_enable(void); +void udi_hid_raw_disable(void); +bool udi_hid_raw_setup(void); uint8_t udi_hid_raw_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_raw = { - .enable = (bool(*)(void))udi_hid_raw_enable, - .disable = (void (*)(void))udi_hid_raw_disable, - .setup = (bool(*)(void))udi_hid_raw_setup, + .enable = (bool (*)(void))udi_hid_raw_enable, + .disable = (void (*)(void))udi_hid_raw_disable, + .setup = (bool (*)(void))udi_hid_raw_setup, .getsetting = (uint8_t(*)(void))udi_hid_raw_getsetting, .sof_notify = NULL, }; @@ -737,76 +635,56 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_raw_report_trans[UDI_HID_RAW_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_raw_report_desc_t udi_hid_raw_report_desc = { - { - 0x06, // Usage Page (Vendor Defined) - 0xFF, 0xFF, - 0x0A, // Usage (Mouse) - 0xFF, 0xFF, - 0xA1, 0x01, // Collection (Application) - 0x75, 0x08, // Report Size (8) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0xFF, // Logical Maximum (255) - 0x95, 0x40, // Report Count - 0x09, 0x01, // Usage (Input) - 0x81, 0x02, // Input (Data - 0x95, 0x40, // Report Count - 0x09, 0x02, // Usage (Output) - 0x91, 0x02, // Output (Data - 0xC0, // End Collection - Consumer Control - } -}; +UDC_DESC_STORAGE udi_hid_raw_report_desc_t udi_hid_raw_report_desc = {{ + 0x06, // Usage Page (Vendor Defined) + 0xFF, 0xFF, + 0x0A, // Usage (Mouse) + 0xFF, 0xFF, 0xA1, 0x01, // Collection (Application) + 0x75, 0x08, // Report Size (8) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0xFF, // Logical Maximum (255) + 0x95, 0x40, // Report Count + 0x09, 0x01, // Usage (Input) + 0x81, 0x02, // Input (Data + 0x95, 0x40, // Report Count + 0x09, 0x02, // Usage (Output) + 0x91, 0x02, // Output (Data + 0xC0, // End Collection - Consumer Control +}}; static bool udi_hid_raw_setreport(void); static void udi_hid_raw_setreport_valid(void); static void udi_hid_raw_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep); -bool udi_hid_raw_enable(void) -{ +bool udi_hid_raw_enable(void) { // Initialize internal values - udi_hid_raw_rate = 0; - udi_hid_raw_protocol = 0; + udi_hid_raw_rate = 0; + udi_hid_raw_protocol = 0; udi_hid_raw_b_report_trans_ongoing = false; memset(udi_hid_raw_report, 0, UDI_HID_RAW_REPORT_SIZE); udi_hid_raw_b_report_valid = false; return UDI_HID_RAW_ENABLE_EXT(); } -void udi_hid_raw_disable(void) -{ - UDI_HID_RAW_DISABLE_EXT(); -} +void udi_hid_raw_disable(void) { UDI_HID_RAW_DISABLE_EXT(); } -bool udi_hid_raw_setup(void) -{ - return udi_hid_setup(&udi_hid_raw_rate, - &udi_hid_raw_protocol, - (uint8_t *) &udi_hid_raw_report_desc, - udi_hid_raw_setreport); -} +bool udi_hid_raw_setup(void) { return udi_hid_setup(&udi_hid_raw_rate, &udi_hid_raw_protocol, (uint8_t *)&udi_hid_raw_report_desc, udi_hid_raw_setreport); } -uint8_t udi_hid_raw_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_raw_getsetting(void) { return 0; } -static bool udi_hid_raw_setreport(void) -{ - if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) - && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) - && (UDI_HID_RAW_REPORT_SIZE == udd_g_ctrlreq.req.wLength)) { +static bool udi_hid_raw_setreport(void) { + if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) && (UDI_HID_RAW_REPORT_SIZE == udd_g_ctrlreq.req.wLength)) { // Report OUT type on report ID 0 from USB Host - udd_g_ctrlreq.payload = udi_hid_raw_report_set; - udd_g_ctrlreq.callback = udi_hid_raw_setreport_valid; //must call routine to transform setreport to LED state + udd_g_ctrlreq.payload = udi_hid_raw_report_set; + udd_g_ctrlreq.callback = udi_hid_raw_setreport_valid; // must call routine to transform setreport to LED state udd_g_ctrlreq.payload_size = UDI_HID_RAW_REPORT_SIZE; return true; } return false; } -bool udi_hid_raw_send_report(void) -{ +bool udi_hid_raw_send_report(void) { if (!main_b_raw_enable) { return false; } @@ -815,20 +693,14 @@ bool udi_hid_raw_send_report(void) return false; } - memcpy(udi_hid_raw_report_trans, udi_hid_raw_report,UDI_HID_RAW_REPORT_SIZE); - udi_hid_raw_b_report_valid = false; - udi_hid_raw_b_report_trans_ongoing = - udd_ep_run(UDI_HID_RAW_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_raw_report_trans, - UDI_HID_RAW_REPORT_SIZE, - udi_hid_raw_report_sent); + memcpy(udi_hid_raw_report_trans, udi_hid_raw_report, UDI_HID_RAW_REPORT_SIZE); + udi_hid_raw_b_report_valid = false; + udi_hid_raw_b_report_trans_ongoing = udd_ep_run(UDI_HID_RAW_EP_IN | USB_EP_DIR_IN, false, udi_hid_raw_report_trans, UDI_HID_RAW_REPORT_SIZE, udi_hid_raw_report_sent); return udi_hid_raw_b_report_trans_ongoing; } -static void udi_hid_raw_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_raw_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -838,27 +710,24 @@ static void udi_hid_raw_report_sent(udd_ep_status_t status, iram_size_t nb_sent, } } -static void udi_hid_raw_setreport_valid(void) -{ +static void udi_hid_raw_setreport_valid(void) {} -} - -#endif //RAW +#endif // RAW //******************************************************************************************** // CON //******************************************************************************************** #ifdef CON -bool udi_hid_con_enable(void); -void udi_hid_con_disable(void); -bool udi_hid_con_setup(void); +bool udi_hid_con_enable(void); +void udi_hid_con_disable(void); +bool udi_hid_con_setup(void); uint8_t udi_hid_con_getsetting(void); UDC_DESC_STORAGE udi_api_t udi_api_hid_con = { - .enable = (bool(*)(void))udi_hid_con_enable, - .disable = (void (*)(void))udi_hid_con_disable, - .setup = (bool(*)(void))udi_hid_con_setup, + .enable = (bool (*)(void))udi_hid_con_enable, + .disable = (void (*)(void))udi_hid_con_disable, + .setup = (bool (*)(void))udi_hid_con_setup, .getsetting = (uint8_t(*)(void))udi_hid_con_getsetting, .sof_notify = NULL, }; @@ -883,76 +752,57 @@ COMPILER_WORD_ALIGNED static uint8_t udi_hid_con_report_trans[UDI_HID_CON_REPORT_SIZE]; COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE udi_hid_con_report_desc_t udi_hid_con_report_desc = { - { - 0x06, 0x31, 0xFF, // Vendor Page (PJRC Teensy compatible) - 0x09, 0x74, // Vendor Usage (PJRC Teensy compatible) - 0xA1, 0x01, // Collection (Application) - 0x09, 0x75, // Usage (Vendor) - 0x15, 0x00, // Logical Minimum (0x00) - 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) - 0x95, CONSOLE_EPSIZE, // Report Count - 0x75, 0x08, // Report Size (8) - 0x81, 0x02, // Input (Data) - 0x09, 0x76, // Usage (Vendor) - 0x15, 0x00, // Logical Minimum (0x00) - 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) - 0x95, CONSOLE_EPSIZE, // Report Count - 0x75, 0x08, // Report Size (8) - 0x91, 0x02, // Output (Data) - 0xC0, // End Collection - } -}; +UDC_DESC_STORAGE udi_hid_con_report_desc_t udi_hid_con_report_desc = {{ + 0x06, 0x31, 0xFF, // Vendor Page (PJRC Teensy compatible) + 0x09, 0x74, // Vendor Usage (PJRC Teensy compatible) + 0xA1, 0x01, // Collection (Application) + 0x09, 0x75, // Usage (Vendor) + 0x15, 0x00, // Logical Minimum (0x00) + 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) + 0x95, CONSOLE_EPSIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x81, 0x02, // Input (Data) + 0x09, 0x76, // Usage (Vendor) + 0x15, 0x00, // Logical Minimum (0x00) + 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF) + 0x95, CONSOLE_EPSIZE, // Report Count + 0x75, 0x08, // Report Size (8) + 0x91, 0x02, // Output (Data) + 0xC0, // End Collection +}}; static bool udi_hid_con_setreport(void); static void udi_hid_con_setreport_valid(void); static void udi_hid_con_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep); -bool udi_hid_con_enable(void) -{ +bool udi_hid_con_enable(void) { // Initialize internal values - udi_hid_con_rate = 0; - udi_hid_con_protocol = 0; + udi_hid_con_rate = 0; + udi_hid_con_protocol = 0; udi_hid_con_b_report_trans_ongoing = false; memset(udi_hid_con_report, 0, UDI_HID_CON_REPORT_SIZE); udi_hid_con_b_report_valid = false; return UDI_HID_CON_ENABLE_EXT(); } -void udi_hid_con_disable(void) -{ - UDI_HID_CON_DISABLE_EXT(); -} +void udi_hid_con_disable(void) { UDI_HID_CON_DISABLE_EXT(); } -bool udi_hid_con_setup(void) -{ - return udi_hid_setup(&udi_hid_con_rate, - &udi_hid_con_protocol, - (uint8_t *) &udi_hid_con_report_desc, - udi_hid_con_setreport); -} +bool udi_hid_con_setup(void) { return udi_hid_setup(&udi_hid_con_rate, &udi_hid_con_protocol, (uint8_t *)&udi_hid_con_report_desc, udi_hid_con_setreport); } -uint8_t udi_hid_con_getsetting(void) -{ - return 0; -} +uint8_t udi_hid_con_getsetting(void) { return 0; } -static bool udi_hid_con_setreport(void) -{ - if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) - && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) - && (UDI_HID_CON_REPORT_SIZE == udd_g_ctrlreq.req.wLength)) { - udd_g_ctrlreq.payload = udi_hid_con_report_set; - udd_g_ctrlreq.callback = udi_hid_con_setreport_valid; +static bool udi_hid_con_setreport(void) { + if ((USB_HID_REPORT_TYPE_OUTPUT == (udd_g_ctrlreq.req.wValue >> 8)) && (0 == (0xFF & udd_g_ctrlreq.req.wValue)) && (UDI_HID_CON_REPORT_SIZE == udd_g_ctrlreq.req.wLength)) { + udd_g_ctrlreq.payload = udi_hid_con_report_set; + udd_g_ctrlreq.callback = udi_hid_con_setreport_valid; udd_g_ctrlreq.payload_size = UDI_HID_CON_REPORT_SIZE; return true; } return false; } -bool udi_hid_con_send_report(void) -{ +bool udi_hid_con_send_report(void) { if (!main_b_con_enable) { return false; } @@ -961,20 +811,14 @@ bool udi_hid_con_send_report(void) return false; } - memcpy(udi_hid_con_report_trans, udi_hid_con_report,UDI_HID_CON_REPORT_SIZE); - udi_hid_con_b_report_valid = false; - udi_hid_con_b_report_trans_ongoing = - udd_ep_run(UDI_HID_CON_EP_IN | USB_EP_DIR_IN, - false, - udi_hid_con_report_trans, - UDI_HID_CON_REPORT_SIZE, - udi_hid_con_report_sent); + memcpy(udi_hid_con_report_trans, udi_hid_con_report, UDI_HID_CON_REPORT_SIZE); + udi_hid_con_b_report_valid = false; + udi_hid_con_b_report_trans_ongoing = udd_ep_run(UDI_HID_CON_EP_IN | USB_EP_DIR_IN, false, udi_hid_con_report_trans, UDI_HID_CON_REPORT_SIZE, udi_hid_con_report_sent); return udi_hid_con_b_report_trans_ongoing; } -static void udi_hid_con_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) -{ +static void udi_hid_con_report_sent(udd_ep_status_t status, iram_size_t nb_sent, udd_ep_id_t ep) { UNUSED(status); UNUSED(nb_sent); UNUSED(ep); @@ -984,9 +828,6 @@ static void udi_hid_con_report_sent(udd_ep_status_t status, iram_size_t nb_sent, } } -static void udi_hid_con_setreport_valid(void) -{ - -} +static void udi_hid_con_setreport_valid(void) {} -#endif //CON +#endif // CON diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.h b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.h index e442919a9..82b1cbfe0 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd.h @@ -59,59 +59,59 @@ extern "C" { //****************************************************************************** #ifdef KBD extern UDC_DESC_STORAGE udi_api_t udi_api_hid_kbd; -extern bool udi_hid_kbd_b_report_valid; -extern volatile bool udi_hid_kbd_b_report_trans_ongoing; -extern uint8_t udi_hid_kbd_report_set; -bool udi_hid_kbd_send_report(void); -#endif //KBD +extern bool udi_hid_kbd_b_report_valid; +extern volatile bool udi_hid_kbd_b_report_trans_ongoing; +extern uint8_t udi_hid_kbd_report_set; +bool udi_hid_kbd_send_report(void); +#endif // KBD //******************************************************************************************** // NKRO Keyboard //******************************************************************************************** #ifdef NKRO extern UDC_DESC_STORAGE udi_api_t udi_api_hid_nkro; -extern bool udi_hid_nkro_b_report_valid; -extern volatile bool udi_hid_nkro_b_report_trans_ongoing; -bool udi_hid_nkro_send_report(void); -#endif //NKRO +extern bool udi_hid_nkro_b_report_valid; +extern volatile bool udi_hid_nkro_b_report_trans_ongoing; +bool udi_hid_nkro_send_report(void); +#endif // NKRO //******************************************************************************************** // SYS-CTRL interface //******************************************************************************************** #ifdef EXK extern UDC_DESC_STORAGE udi_api_t udi_api_hid_exk; -extern bool udi_hid_exk_b_report_valid; -extern uint8_t udi_hid_exk_report_set; -bool udi_hid_exk_send_report(void); -#endif //EXK +extern bool udi_hid_exk_b_report_valid; +extern uint8_t udi_hid_exk_report_set; +bool udi_hid_exk_send_report(void); +#endif // EXK //******************************************************************************************** // CON Console //******************************************************************************************** #ifdef CON extern UDC_DESC_STORAGE udi_api_t udi_api_hid_con; -extern bool udi_hid_con_b_report_valid; -extern uint8_t udi_hid_con_report_set[UDI_HID_CON_REPORT_SIZE]; -extern volatile bool udi_hid_con_b_report_trans_ongoing; -bool udi_hid_con_send_report(void); -#endif //CON +extern bool udi_hid_con_b_report_valid; +extern uint8_t udi_hid_con_report_set[UDI_HID_CON_REPORT_SIZE]; +extern volatile bool udi_hid_con_b_report_trans_ongoing; +bool udi_hid_con_send_report(void); +#endif // CON //******************************************************************************************** // MOU Mouse //******************************************************************************************** #ifdef MOU extern UDC_DESC_STORAGE udi_api_t udi_api_hid_mou; -extern bool udi_hid_mou_b_report_valid; -bool udi_hid_mou_send_report(void); -#endif //MOU +extern bool udi_hid_mou_b_report_valid; +bool udi_hid_mou_send_report(void); +#endif // MOU //******************************************************************************************** // RAW Raw //******************************************************************************************** #ifdef RAW extern UDC_DESC_STORAGE udi_api_t udi_api_hid_raw; -bool udi_hid_raw_send_report(void); -#endif //RAW +bool udi_hid_raw_send_report(void); +#endif // RAW //@} @@ -119,4 +119,4 @@ bool udi_hid_raw_send_report(void); } #endif -#endif // _UDC_HID_KBD_H_ +#endif // _UDC_HID_KBD_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_conf.h b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_conf.h index db5db17ed..ec73252b6 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_conf.h +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_conf.h @@ -57,4 +57,4 @@ #include "udi_hid_kbd.h" -#endif // _UDI_HID_KBD_CONF_H_ +#endif // _UDI_HID_KBD_CONF_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_desc.c b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_desc.c index 2d6e35e25..4e7deaaa3 100644 --- a/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_desc.c +++ b/tmk_core/protocol/arm_atsam/usb/udi_hid_kbd_desc.c @@ -65,37 +65,35 @@ //! USB Device Descriptor COMPILER_WORD_ALIGNED -UDC_DESC_STORAGE usb_dev_desc_t udc_device_desc = { - .bLength = sizeof(usb_dev_desc_t), - .bDescriptorType = USB_DT_DEVICE, - .bcdUSB = LE16(USB_V2_0), - .bDeviceClass = DEVICE_CLASS, - .bDeviceSubClass = DEVICE_SUBCLASS, - .bDeviceProtocol = DEVICE_PROTOCOL, - .bMaxPacketSize0 = USB_DEVICE_EP_CTRL_SIZE, - .idVendor = LE16(USB_DEVICE_VENDOR_ID), - .idProduct = LE16(USB_DEVICE_PRODUCT_ID), - .bcdDevice = LE16(USB_DEVICE_VERSION), +UDC_DESC_STORAGE usb_dev_desc_t udc_device_desc = {.bLength = sizeof(usb_dev_desc_t), + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = LE16(USB_V2_0), + .bDeviceClass = DEVICE_CLASS, + .bDeviceSubClass = DEVICE_SUBCLASS, + .bDeviceProtocol = DEVICE_PROTOCOL, + .bMaxPacketSize0 = USB_DEVICE_EP_CTRL_SIZE, + .idVendor = LE16(USB_DEVICE_VENDOR_ID), + .idProduct = LE16(USB_DEVICE_PRODUCT_ID), + .bcdDevice = LE16(USB_DEVICE_VERSION), #ifdef USB_DEVICE_MANUFACTURE_NAME - .iManufacturer = 1, + .iManufacturer = 1, #else - .iManufacturer = 0, // No manufacture string + .iManufacturer = 0, // No manufacture string #endif #ifdef USB_DEVICE_PRODUCT_NAME - .iProduct = 2, + .iProduct = 2, #else - .iProduct = 0, // No product string + .iProduct = 0, // No product string #endif #if (defined USB_DEVICE_SERIAL_NAME || defined USB_DEVICE_GET_SERIAL_NAME_POINTER) - .iSerialNumber = 3, + .iSerialNumber = 3, #else - .iSerialNumber = 0, // No serial string + .iSerialNumber = 0, // No serial string #endif - .bNumConfigurations = 1 -}; + .bNumConfigurations = 1}; #if 0 -#ifdef USB_DEVICE_HS_SUPPORT +# ifdef USB_DEVICE_HS_SUPPORT //! USB Device Qualifier Descriptor for HS COMPILER_WORD_ALIGNED UDC_DESC_STORAGE usb_dev_qual_desc_t udc_device_qual = { @@ -108,77 +106,77 @@ UDC_DESC_STORAGE usb_dev_qual_desc_t udc_device_qual = { .bMaxPacketSize0 = USB_DEVICE_EP_CTRL_SIZE, .bNumConfigurations = 1 }; -#endif +# endif #endif //! USB Device Configuration Descriptor filled for FS and HS COMPILER_WORD_ALIGNED UDC_DESC_STORAGE udc_desc_t udc_desc = { - .conf.bLength = sizeof(usb_conf_desc_t), - .conf.bDescriptorType = USB_DT_CONFIGURATION, - .conf.wTotalLength = LE16(sizeof(udc_desc_t)), - .conf.bNumInterfaces = USB_DEVICE_NB_INTERFACE, - .conf.bConfigurationValue = 1, - .conf.iConfiguration = 0, - .conf.bmAttributes = /* USB_CONFIG_ATTR_MUST_SET | */ USB_DEVICE_ATTR, - .conf.bMaxPower = USB_CONFIG_MAX_POWER(USB_DEVICE_POWER), + .conf.bLength = sizeof(usb_conf_desc_t), + .conf.bDescriptorType = USB_DT_CONFIGURATION, + .conf.wTotalLength = LE16(sizeof(udc_desc_t)), + .conf.bNumInterfaces = USB_DEVICE_NB_INTERFACE, + .conf.bConfigurationValue = 1, + .conf.iConfiguration = 0, + .conf.bmAttributes = /* USB_CONFIG_ATTR_MUST_SET | */ USB_DEVICE_ATTR, + .conf.bMaxPower = USB_CONFIG_MAX_POWER(USB_DEVICE_POWER), #ifdef KBD - .hid_kbd = UDI_HID_KBD_DESC, + .hid_kbd = UDI_HID_KBD_DESC, #endif #ifdef RAW - .hid_raw = UDI_HID_RAW_DESC, + .hid_raw = UDI_HID_RAW_DESC, #endif #ifdef MOU - .hid_mou = UDI_HID_MOU_DESC, + .hid_mou = UDI_HID_MOU_DESC, #endif #ifdef EXK - .hid_exk = UDI_HID_EXK_DESC, + .hid_exk = UDI_HID_EXK_DESC, #endif #ifdef CON - .hid_con = UDI_HID_CON_DESC, + .hid_con = UDI_HID_CON_DESC, #endif #ifdef NKRO - .hid_nkro = UDI_HID_NKRO_DESC, + .hid_nkro = UDI_HID_NKRO_DESC, #endif #ifdef CDC - .cdc_serial = CDC_DESCRIPTOR, + .cdc_serial = CDC_DESCRIPTOR, #endif }; UDC_DESC_STORAGE udi_api_t *udi_apis[USB_DEVICE_NB_INTERFACE] = { - #ifdef KBD +#ifdef KBD &udi_api_hid_kbd, - #endif - #ifdef RAW +#endif +#ifdef RAW &udi_api_hid_raw, - #endif - #ifdef MOU +#endif +#ifdef MOU &udi_api_hid_mou, - #endif - #ifdef EXK +#endif +#ifdef EXK &udi_api_hid_exk, - #endif - #ifdef CON +#endif +#ifdef CON &udi_api_hid_con, - #endif - #ifdef NKRO +#endif +#ifdef NKRO &udi_api_hid_nkro, - #endif - #ifdef CDC - &udi_api_cdc_comm, &udi_api_cdc_data, - #endif +#endif +#ifdef CDC + &udi_api_cdc_comm, &udi_api_cdc_data, +#endif }; //! Add UDI with USB Descriptors FS & HS -UDC_DESC_STORAGE udc_config_speed_t udc_config_fshs[1] = {{ - .desc = (usb_conf_desc_t UDC_DESC_STORAGE*)&udc_desc, - .udi_apis = udi_apis, +UDC_DESC_STORAGE udc_config_speed_t udc_config_fshs[1] = {{ + .desc = (usb_conf_desc_t UDC_DESC_STORAGE *)&udc_desc, + .udi_apis = udi_apis, }}; //! Add all information about USB Device in global structure for UDC UDC_DESC_STORAGE udc_config_t udc_config = { .confdev_lsfs = &udc_device_desc, - .conf_lsfs = udc_config_fshs, + .conf_lsfs = udc_config_fshs, }; //@} diff --git a/tmk_core/protocol/arm_atsam/usb/ui.c b/tmk_core/protocol/arm_atsam/usb/ui.c index 70a619109..86c7edc94 100644 --- a/tmk_core/protocol/arm_atsam/usb/ui.c +++ b/tmk_core/protocol/arm_atsam/usb/ui.c @@ -45,10 +45,10 @@ */ #ifndef ARM_MATH_CM4 - #define ARM_MATH_CM4 +# define ARM_MATH_CM4 #endif -#undef LITTLE_ENDIAN //redefined in samd51j18a.h +#undef LITTLE_ENDIAN // redefined in samd51j18a.h #include "samd51j18a.h" #include "ui.h" @@ -68,37 +68,16 @@ static void ui_wakeup_handler(void) } #endif -void ui_init(void) -{ +void ui_init(void) {} -} +void ui_powerdown(void) {} -void ui_powerdown(void) -{ +void ui_wakeup_enable(void) {} -} +void ui_wakeup_disable(void) {} -void ui_wakeup_enable(void) -{ +void ui_wakeup(void) {} -} +void ui_process(uint16_t framenumber) {} -void ui_wakeup_disable(void) -{ - -} - -void ui_wakeup(void) -{ - -} - -void ui_process(uint16_t framenumber) -{ - -} - -void ui_kbd_led(uint8_t value) -{ - -} +void ui_kbd_led(uint8_t value) {} diff --git a/tmk_core/protocol/arm_atsam/usb/ui.h b/tmk_core/protocol/arm_atsam/usb/ui.h index d1c767d45..f7bc65236 100644 --- a/tmk_core/protocol/arm_atsam/usb/ui.h +++ b/tmk_core/protocol/arm_atsam/usb/ui.h @@ -73,4 +73,4 @@ void ui_process(uint16_t framenumber); */ void ui_kbd_led(uint8_t value); -#endif // _UI_H_ +#endif // _UI_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb.c b/tmk_core/protocol/arm_atsam/usb/usb.c index d30d76dd1..b7393660b 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb.c +++ b/tmk_core/protocol/arm_atsam/usb/usb.c @@ -48,11 +48,11 @@ #define SAMD11 DEVICE_MODE_ONLY #ifndef ARM_MATH_CM4 - #define ARM_MATH_CM4 +# define ARM_MATH_CM4 #endif #include "compiler.h" -#undef LITTLE_ENDIAN //redefined in samd51j18a.h +#undef LITTLE_ENDIAN // redefined in samd51j18a.h #include "samd51j18a.h" #include <stdbool.h> #include <string.h> @@ -61,28 +61,28 @@ #include "usb.h" /** Fields definition from a LPM TOKEN */ -#define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0) -#define USB_LPM_ATTRIBUT_HIRD_MASK (0xF << 4) -#define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8) -#define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0) -#define USB_LPM_ATTRIBUT_HIRD(value) ((value & 0xF) << 4) -#define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8) -#define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1) +#define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0) +#define USB_LPM_ATTRIBUT_HIRD_MASK (0xF << 4) +#define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8) +#define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0) +#define USB_LPM_ATTRIBUT_HIRD(value) ((value & 0xF) << 4) +#define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8) +#define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1) /** * \brief Mask selecting the index part of an endpoint address */ -#define USB_EP_ADDR_MASK 0x0f +#define USB_EP_ADDR_MASK 0x0f /** * \brief Endpoint transfer direction is IN */ -#define USB_EP_DIR_IN 0x80 +#define USB_EP_DIR_IN 0x80 /** * \brief Endpoint transfer direction is OUT */ -#define USB_EP_DIR_OUT 0x00 +#define USB_EP_DIR_OUT 0x00 /** * \name USB SRAM data containing pipe descriptor table @@ -120,24 +120,13 @@ static struct usb_endpoint_callback_parameter ep_callback_para; * \internal USB Device IRQ Mask Bits Map */ static const uint16_t _usb_device_irq_bits[USB_DEVICE_CALLBACK_N] = { - USB_DEVICE_INTFLAG_SOF, - USB_DEVICE_INTFLAG_EORST, - USB_DEVICE_INTFLAG_WAKEUP | USB_DEVICE_INTFLAG_EORSM | USB_DEVICE_INTFLAG_UPRSM, - USB_DEVICE_INTFLAG_RAMACER, - USB_DEVICE_INTFLAG_SUSPEND, - USB_DEVICE_INTFLAG_LPMNYET, - USB_DEVICE_INTFLAG_LPMSUSP, + USB_DEVICE_INTFLAG_SOF, USB_DEVICE_INTFLAG_EORST, USB_DEVICE_INTFLAG_WAKEUP | USB_DEVICE_INTFLAG_EORSM | USB_DEVICE_INTFLAG_UPRSM, USB_DEVICE_INTFLAG_RAMACER, USB_DEVICE_INTFLAG_SUSPEND, USB_DEVICE_INTFLAG_LPMNYET, USB_DEVICE_INTFLAG_LPMSUSP, }; /** * \internal USB Device IRQ Mask Bits Map */ -static const uint8_t _usb_endpoint_irq_bits[USB_DEVICE_EP_CALLBACK_N] = { - USB_DEVICE_EPINTFLAG_TRCPT_Msk, - USB_DEVICE_EPINTFLAG_TRFAIL_Msk, - USB_DEVICE_EPINTFLAG_RXSTP, - USB_DEVICE_EPINTFLAG_STALL_Msk -}; +static const uint8_t _usb_endpoint_irq_bits[USB_DEVICE_EP_CALLBACK_N] = {USB_DEVICE_EPINTFLAG_TRCPT_Msk, USB_DEVICE_EPINTFLAG_TRFAIL_Msk, USB_DEVICE_EPINTFLAG_RXSTP, USB_DEVICE_EPINTFLAG_STALL_Msk}; /** * \brief Registers a USB device callback @@ -155,10 +144,7 @@ static const uint8_t _usb_endpoint_irq_bits[USB_DEVICE_EP_CALLBACK_N] = { * \return Status of the registration operation. * \retval STATUS_OK The callback was registered successfully. */ -enum status_code usb_device_register_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type, - usb_device_callback_t callback_func) -{ +enum status_code usb_device_register_callback(struct usb_module *module_inst, enum usb_device_callback callback_type, usb_device_callback_t callback_func) { /* Sanity check arguments */ Assert(module_inst); Assert(callback_func); @@ -184,9 +170,7 @@ enum status_code usb_device_register_callback(struct usb_module *module_inst, * \return Status of the de-registration operation. * \retval STATUS_OK The callback was unregistered successfully. */ -enum status_code usb_device_unregister_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type) -{ +enum status_code usb_device_unregister_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); @@ -211,9 +195,7 @@ enum status_code usb_device_unregister_callback(struct usb_module *module_inst, * \return Status of the callback enable operation. * \retval STATUS_OK The callback was enabled successfully. */ -enum status_code usb_device_enable_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type) -{ +enum status_code usb_device_enable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -240,9 +222,7 @@ enum status_code usb_device_enable_callback(struct usb_module *module_inst, * \return Status of the callback disable operation. * \retval STATUS_OK The callback was disabled successfully. */ -enum status_code usb_device_disable_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type) -{ +enum status_code usb_device_disable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -272,11 +252,7 @@ enum status_code usb_device_disable_callback(struct usb_module *module_inst, * \return Status of the registration operation. * \retval STATUS_OK The callback was registered successfully. */ -enum status_code usb_device_endpoint_register_callback( - struct usb_module *module_inst, uint8_t ep_num, - enum usb_device_endpoint_callback callback_type, - usb_device_endpoint_callback_t callback_func) -{ +enum status_code usb_device_endpoint_register_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type, usb_device_endpoint_callback_t callback_func) { /* Sanity check arguments */ Assert(module_inst); Assert(ep_num < USB_EPT_NUM); @@ -304,10 +280,7 @@ enum status_code usb_device_endpoint_register_callback( * \return Status of the de-registration operation. * \retval STATUS_OK The callback was unregistered successfully. */ -enum status_code usb_device_endpoint_unregister_callback( - struct usb_module *module_inst, uint8_t ep_num, - enum usb_device_endpoint_callback callback_type) -{ +enum status_code usb_device_endpoint_unregister_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); Assert(ep_num < USB_EPT_NUM); @@ -334,10 +307,7 @@ enum status_code usb_device_endpoint_unregister_callback( * \return Status of the callback enable operation. * \retval STATUS_OK The callback was enabled successfully. */ -enum status_code usb_device_endpoint_enable_callback( - struct usb_module *module_inst, uint8_t ep, - enum usb_device_endpoint_callback callback_type) -{ +enum status_code usb_device_endpoint_enable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -349,7 +319,7 @@ enum status_code usb_device_endpoint_enable_callback( module_inst->device_endpoint_enabled_callback_mask[ep_num] |= _usb_endpoint_irq_bits[callback_type]; if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRCPT) { - if (ep_num == 0) { // control endpoint + if (ep_num == 0) { // control endpoint module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1; } else if (ep & USB_EP_DIR_IN) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT1; @@ -359,7 +329,7 @@ enum status_code usb_device_endpoint_enable_callback( } if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL) { - if (ep_num == 0) { // control endpoint + if (ep_num == 0) { // control endpoint module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRFAIL0 | USB_DEVICE_EPINTENSET_TRFAIL1; } else if (ep & USB_EP_DIR_IN) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRFAIL1; @@ -395,10 +365,7 @@ enum status_code usb_device_endpoint_enable_callback( * \return Status of the callback disable operation. * \retval STATUS_OK The callback was disabled successfully. */ -enum status_code usb_device_endpoint_disable_callback( - struct usb_module *module_inst, uint8_t ep, - enum usb_device_endpoint_callback callback_type) -{ +enum status_code usb_device_endpoint_disable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -410,17 +377,17 @@ enum status_code usb_device_endpoint_disable_callback( module_inst->device_endpoint_enabled_callback_mask[ep_num] &= ~_usb_endpoint_irq_bits[callback_type]; if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRCPT) { - if (ep_num == 0) { // control endpoint - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0 | USB_DEVICE_EPINTENCLR_TRCPT1; + if (ep_num == 0) { // control endpoint + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0 | USB_DEVICE_EPINTENCLR_TRCPT1; } else if (ep & USB_EP_DIR_IN) { - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT1; + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT1; } else { - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0; + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRCPT0; } } if (callback_type == USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL) { - if (ep_num == 0) { // control endpoint + if (ep_num == 0) { // control endpoint module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRFAIL0 | USB_DEVICE_EPINTENCLR_TRFAIL1; } else if (ep & USB_EP_DIR_IN) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPINTENCLR.reg = USB_DEVICE_EPINTENCLR_TRFAIL1; @@ -460,16 +427,15 @@ enum status_code usb_device_endpoint_disable_callback( * * \param[out] ep_config Configuration structure to initialize to default values */ -void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config) -{ +void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config) { /* Sanity check arguments */ Assert(ep_config); /* Write default config to config struct */ ep_config->ep_address = 0; - ep_config->ep_size = USB_ENDPOINT_8_BYTE; - ep_config->auto_zlp = false; - ep_config->ep_type = USB_DEVICE_ENDPOINT_TYPE_CONTROL; + ep_config->ep_size = USB_ENDPOINT_8_BYTE; + ep_config->auto_zlp = false; + ep_config->ep_type = USB_DEVICE_ENDPOINT_TYPE_CONTROL; } /** @@ -486,25 +452,22 @@ void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config * * \retval STATUS_OK The device endpoint was configured successfully * \retval STATUS_ERR_DENIED The endpoint address is already configured */ -enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, - struct usb_device_endpoint_config *ep_config) -{ +enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, struct usb_device_endpoint_config *ep_config) { /* Sanity check arguments */ Assert(module_inst); Assert(ep_config); - uint8_t ep_num = ep_config->ep_address & USB_EP_ADDR_MASK; + uint8_t ep_num = ep_config->ep_address & USB_EP_ADDR_MASK; uint8_t ep_bank = (ep_config->ep_address & USB_EP_DIR_IN) ? 1 : 0; switch (ep_config->ep_type) { case USB_DEVICE_ENDPOINT_TYPE_DISABLE: - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0) | USB_DEVICE_EPCFG_EPTYPE1(0); + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0) | USB_DEVICE_EPCFG_EPTYPE1(0); return STATUS_OK; case USB_DEVICE_ENDPOINT_TYPE_CONTROL: - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0 && \ - (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) { - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(1) | USB_DEVICE_EPCFG_EPTYPE1(1); + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0 && (module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) { + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(1) | USB_DEVICE_EPCFG_EPTYPE1(1); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY; module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY; } else { @@ -523,14 +486,14 @@ enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, case USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS: if (ep_bank) { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(2); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY; } else { return STATUS_ERR_DENIED; } } else { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(2); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY; } else { @@ -541,14 +504,14 @@ enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, case USB_DEVICE_ENDPOINT_TYPE_BULK: if (ep_bank) { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(3); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY; } else { return STATUS_ERR_DENIED; } } else { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(3); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY; } else { @@ -559,14 +522,14 @@ enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, case USB_DEVICE_ENDPOINT_TYPE_INTERRUPT: if (ep_bank) { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE1_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE1(4); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY; } else { return STATUS_ERR_DENIED; } } else { - if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0){ + if ((module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg & USB_DEVICE_EPCFG_EPTYPE0_Msk) == 0) { module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPCFG.reg |= USB_DEVICE_EPCFG_EPTYPE0(4); module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY; } else { @@ -583,7 +546,7 @@ enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, if (true == ep_config->auto_zlp) { usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[ep_bank].PCKSIZE.reg |= USB_DEVICE_PCKSIZE_AUTO_ZLP; - } else { + } else { usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[ep_bank].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP; } @@ -598,8 +561,7 @@ enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, * * \return \c true if endpoint is configured and ready to use */ -bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep) -{ +bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep) { uint8_t ep_num = ep & USB_EP_ADDR_MASK; uint8_t flag; @@ -611,15 +573,13 @@ bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t e return ((enum usb_device_endpoint_type)(flag) != USB_DEVICE_ENDPOINT_TYPE_DISABLE); } - /** * \brief Abort ongoing job on the endpoint * * \param module_inst Pointer to USB software instance struct * \param ep Endpoint address */ -void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep) -{ +void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep) { uint8_t ep_num; ep_num = ep & USB_EP_ADDR_MASK; @@ -643,8 +603,7 @@ void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep) * * \return \c true if the endpoint is halted */ -bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep) -{ +bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep) { uint8_t ep_num = ep & USB_EP_ADDR_MASK; if (ep & USB_EP_DIR_IN) { @@ -660,8 +619,7 @@ bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep) * \param module_inst Pointer to USB software instance struct * \param ep Endpoint address */ -void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep) -{ +void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep) { uint8_t ep_num = ep & USB_EP_ADDR_MASK; // Stall endpoint @@ -678,8 +636,7 @@ void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep) * \param module_inst Pointer to USB software instance struct * \param ep Endpoint address */ -void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep) -{ +void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep) { uint8_t ep_num = ep & USB_EP_ADDR_MASK; if (ep & USB_EP_DIR_IN) { @@ -717,9 +674,7 @@ void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep) * \retval STATUS_OK Job started successfully * \retval STATUS_ERR_DENIED Endpoint is not ready */ -enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst,uint8_t ep_num, - uint8_t* pbuf, uint32_t buf_size) -{ +enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -732,10 +687,10 @@ enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_ }; /* get endpoint configuration from setting register */ - usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].ADDR.reg = (uint32_t)pbuf; + usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].ADDR.reg = (uint32_t)pbuf; usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.MULTI_PACKET_SIZE = 0; - usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = buf_size; - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY; + usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = buf_size; + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY; return STATUS_OK; } @@ -752,9 +707,7 @@ enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_ * \retval STATUS_OK Job started successfully * \retval STATUS_ERR_DENIED Endpoint is not ready */ -enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst,uint8_t ep_num, - uint8_t* pbuf, uint32_t buf_size) -{ +enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -767,10 +720,10 @@ enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_i }; /* get endpoint configuration from setting register */ - usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf; + usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf; usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = buf_size; - usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0; - module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY; + usb_descriptor_table.usb_endpoint_table[ep_num].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0; + module_inst->hw->DEVICE.DeviceEndpoint[ep_num].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY; return STATUS_OK; } @@ -785,24 +738,21 @@ enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_i * \retval STATUS_OK Job started successfully * \retval STATUS_ERR_DENIED Endpoint is not ready */ -enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst, - uint8_t* pbuf) -{ +enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst, uint8_t *pbuf) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); /* get endpoint configuration from setting register */ - usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf; + usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].ADDR.reg = (uint32_t)pbuf; usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 8; - usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0; - module_inst->hw->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY; + usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0; + module_inst->hw->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY; return STATUS_OK; } -static void _usb_device_interrupt_handler(void) -{ +static void _usb_device_interrupt_handler(void) { uint16_t ep_inst; uint16_t flags, flags_run; ep_inst = _usb_instances->hw->DEVICE.EPINTSMRY.reg; @@ -812,21 +762,16 @@ static void _usb_device_interrupt_handler(void) int i; /* get interrupt flags */ - flags = _usb_instances->hw->DEVICE.INTFLAG.reg; - flags_run = flags & - _usb_instances->device_enabled_callback_mask & - _usb_instances->device_registered_callback_mask; + flags = _usb_instances->hw->DEVICE.INTFLAG.reg; + flags_run = flags & _usb_instances->device_enabled_callback_mask & _usb_instances->device_registered_callback_mask; - for (i = 0; i < USB_DEVICE_CALLBACK_N; i ++) { + for (i = 0; i < USB_DEVICE_CALLBACK_N; i++) { if (flags & _usb_device_irq_bits[i]) { - _usb_instances->hw->DEVICE.INTFLAG.reg = - _usb_device_irq_bits[i]; + _usb_instances->hw->DEVICE.INTFLAG.reg = _usb_device_irq_bits[i]; } if (flags_run & _usb_device_irq_bits[i]) { if (i == USB_DEVICE_CALLBACK_LPMSUSP) { - device_callback_lpm_wakeup_enable = - usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].EXTREG.bit.VARIABLE - & USB_LPM_ATTRIBUT_REMOTEWAKE_MASK; + device_callback_lpm_wakeup_enable = usb_descriptor_table.usb_endpoint_table[0].DeviceDescBank[0].EXTREG.bit.VARIABLE & USB_LPM_ATTRIBUT_REMOTEWAKE_MASK; } (_usb_instances->device_callback[i])(_usb_instances, &device_callback_lpm_wakeup_enable); } @@ -836,25 +781,22 @@ static void _usb_device_interrupt_handler(void) /* endpoint interrupt */ for (uint8_t i = 0; i < USB_EPT_NUM; i++) { - if (ep_inst & (1 << i)) { - flags = _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg; - flags_run = flags & - _usb_instances->device_endpoint_enabled_callback_mask[i] & - _usb_instances->device_endpoint_registered_callback_mask[i]; + flags = _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg; + flags_run = flags & _usb_instances->device_endpoint_enabled_callback_mask[i] & _usb_instances->device_endpoint_registered_callback_mask[i]; // endpoint transfer stall interrupt if (flags & USB_DEVICE_EPINTFLAG_STALL_Msk) { if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL1) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL1; - ep_callback_para.endpoint_address = USB_EP_DIR_IN | i; + ep_callback_para.endpoint_address = USB_EP_DIR_IN | i; } else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_STALL0) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL0; - ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i; + ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i; } if (flags_run & USB_DEVICE_EPINTFLAG_STALL_Msk) { - (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_STALL])(_usb_instances,&ep_callback_para); + (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_STALL])(_usb_instances, &ep_callback_para); } return; } @@ -862,9 +804,9 @@ static void _usb_device_interrupt_handler(void) // endpoint received setup interrupt if (flags & USB_DEVICE_EPINTFLAG_RXSTP) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; - if(_usb_instances->device_endpoint_enabled_callback_mask[i] & _usb_endpoint_irq_bits[USB_DEVICE_ENDPOINT_CALLBACK_RXSTP]) { + if (_usb_instances->device_endpoint_enabled_callback_mask[i] & _usb_endpoint_irq_bits[USB_DEVICE_ENDPOINT_CALLBACK_RXSTP]) { ep_callback_para.received_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT); - (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_RXSTP])(_usb_instances,&ep_callback_para); + (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_RXSTP])(_usb_instances, &ep_callback_para); } return; } @@ -873,17 +815,17 @@ static void _usb_device_interrupt_handler(void) if (flags & USB_DEVICE_EPINTFLAG_TRCPT_Msk) { if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; - ep_callback_para.endpoint_address = USB_EP_DIR_IN | i; - ep_callback_para.sent_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT); + ep_callback_para.endpoint_address = USB_EP_DIR_IN | i; + ep_callback_para.sent_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT); } else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; - ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i; - ep_callback_para.received_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT); - ep_callback_para.out_buffer_size = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE); + ep_callback_para.endpoint_address = USB_EP_DIR_OUT | i; + ep_callback_para.received_bytes = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT); + ep_callback_para.out_buffer_size = (uint16_t)(usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE); } - if(flags_run & USB_DEVICE_EPINTFLAG_TRCPT_Msk) { - (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRCPT])(_usb_instances,&ep_callback_para); + if (flags_run & USB_DEVICE_EPINTFLAG_TRCPT_Msk) { + (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRCPT])(_usb_instances, &ep_callback_para); } return; } @@ -899,7 +841,7 @@ static void _usb_device_interrupt_handler(void) if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1) { return; } - } else if(_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRFAIL0) { + } else if (_usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRFAIL0) { _usb_instances->hw->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRFAIL0; if (usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].STATUS_BK.reg & USB_DEVICE_STATUS_BK_ERRORFLOW) { usb_descriptor_table.usb_endpoint_table[i].DeviceDescBank[0].STATUS_BK.reg &= ~USB_DEVICE_STATUS_BK_ERRORFLOW; @@ -910,8 +852,8 @@ static void _usb_device_interrupt_handler(void) } } - if(flags_run & USB_DEVICE_EPINTFLAG_TRFAIL_Msk) { - (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL])(_usb_instances,&ep_callback_para); + if (flags_run & USB_DEVICE_EPINTFLAG_TRFAIL_Msk) { + (_usb_instances->device_endpoint_callback[i][USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL])(_usb_instances, &ep_callback_para); } return; } @@ -925,13 +867,13 @@ static void _usb_device_interrupt_handler(void) * * \param module_inst pointer to USB module instance */ -void usb_enable(struct usb_module *module_inst) -{ +void usb_enable(struct usb_module *module_inst) { Assert(module_inst); Assert(module_inst->hw); module_inst->hw->DEVICE.CTRLA.reg |= USB_CTRLA_ENABLE; - while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE); + while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE) + ; } /** @@ -939,68 +881,56 @@ void usb_enable(struct usb_module *module_inst) * * \param module_inst pointer to USB module instance */ -void usb_disable(struct usb_module *module_inst) -{ +void usb_disable(struct usb_module *module_inst) { Assert(module_inst); Assert(module_inst->hw); module_inst->hw->DEVICE.INTENCLR.reg = USB_DEVICE_INTENCLR_MASK; - module_inst->hw->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_MASK; + module_inst->hw->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_MASK; module_inst->hw->DEVICE.CTRLA.reg &= ~USB_CTRLA_ENABLE; - while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE); + while (module_inst->hw->DEVICE.SYNCBUSY.reg == USB_SYNCBUSY_ENABLE) + ; } /** * \brief Interrupt handler for the USB module. */ -void USB_0_Handler(void) -{ +void USB_0_Handler(void) { if (_usb_instances->hw->DEVICE.CTRLA.bit.MODE) { - } else { /*device mode ISR */ _usb_device_interrupt_handler(); } } -void USB_1_Handler(void) -{ - _usb_device_interrupt_handler(); -} +void USB_1_Handler(void) { _usb_device_interrupt_handler(); } -void USB_2_Handler(void) -{ - _usb_device_interrupt_handler(); -} +void USB_2_Handler(void) { _usb_device_interrupt_handler(); } -void USB_3_Handler(void) -{ - _usb_device_interrupt_handler(); -} +void USB_3_Handler(void) { _usb_device_interrupt_handler(); } /** * \brief Get the default USB module settings * * \param[out] module_config Configuration structure to initialize to default values */ -void usb_get_config_defaults(struct usb_config *module_config) -{ +void usb_get_config_defaults(struct usb_config *module_config) { Assert(module_config); /* Sanity check arguments */ Assert(module_config); /* Write default configuration to config struct */ module_config->select_host_mode = 0; - module_config->run_in_standby = 1; + module_config->run_in_standby = 1; module_config->source_generator = 0; - module_config->speed_mode = USB_SPEED_FULL; + module_config->speed_mode = USB_SPEED_FULL; } -#define NVM_USB_PAD_TRANSN_POS 45 +#define NVM_USB_PAD_TRANSN_POS 45 #define NVM_USB_PAD_TRANSN_SIZE 5 -#define NVM_USB_PAD_TRANSP_POS 50 +#define NVM_USB_PAD_TRANSP_POS 50 #define NVM_USB_PAD_TRANSP_SIZE 5 -#define NVM_USB_PAD_TRIM_POS 55 +#define NVM_USB_PAD_TRIM_POS 55 #define NVM_USB_PAD_TRIM_SIZE 3 /** @@ -1020,56 +950,61 @@ void usb_get_config_defaults(struct usb_config *module_config) #define GCLK_USB 10 -enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, - struct usb_config *module_config) -{ +enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, struct usb_config *module_config) { /* Sanity check arguments */ Assert(hw); Assert(module_inst); Assert(module_config); - uint32_t i,j; + uint32_t i, j; uint32_t pad_transn, pad_transp, pad_trim; - Gclk *pgclk = GCLK; - Mclk *pmclk = MCLK; - Port *pport = PORT; - Oscctrl *posc = OSCCTRL; + Gclk * pgclk = GCLK; + Mclk * pmclk = MCLK; + Port * pport = PORT; + Oscctrl *posc = OSCCTRL; _usb_instances = module_inst; /* Associate the software module instance with the hardware module */ module_inst->hw = hw; - //setup peripheral and synchronous bus clocks to USB - pmclk->AHBMASK.bit.USB_ = 1; + // setup peripheral and synchronous bus clocks to USB + pmclk->AHBMASK.bit.USB_ = 1; pmclk->APBBMASK.bit.USB_ = 1; /* Set up the USB DP/DN pins */ - pport->Group[0].PMUX[12].reg = 0x77; //PA24, PA25, function column H for USB D-, D+ + pport->Group[0].PMUX[12].reg = 0x77; // PA24, PA25, function column H for USB D-, D+ pport->Group[0].PINCFG[24].bit.PMUXEN = 1; pport->Group[0].PINCFG[25].bit.PMUXEN = 1; - pport->Group[1].PMUX[11].bit.PMUXE = 7; //PB22, function column H for USB SOF_1KHz output + pport->Group[1].PMUX[11].bit.PMUXE = 7; // PB22, function column H for USB SOF_1KHz output pport->Group[1].PINCFG[22].bit.PMUXEN = 1; - //configure and enable DFLL for USB clock recovery mode at 48MHz + // configure and enable DFLL for USB clock recovery mode at 48MHz posc->DFLLCTRLA.bit.ENABLE = 0; - while (posc->DFLLSYNC.bit.ENABLE); - while (posc->DFLLSYNC.bit.DFLLCTRLB); + while (posc->DFLLSYNC.bit.ENABLE) + ; + while (posc->DFLLSYNC.bit.DFLLCTRLB) + ; posc->DFLLCTRLB.bit.USBCRM = 1; - while (posc->DFLLSYNC.bit.DFLLCTRLB); + while (posc->DFLLSYNC.bit.DFLLCTRLB) + ; posc->DFLLCTRLB.bit.MODE = 1; - while (posc->DFLLSYNC.bit.DFLLCTRLB); + while (posc->DFLLSYNC.bit.DFLLCTRLB) + ; posc->DFLLCTRLB.bit.QLDIS = 0; - while (posc->DFLLSYNC.bit.DFLLCTRLB); + while (posc->DFLLSYNC.bit.DFLLCTRLB) + ; posc->DFLLCTRLB.bit.CCDIS = 1; - posc->DFLLMUL.bit.MUL = 0xbb80; //4800 x 1KHz - while (posc->DFLLSYNC.bit.DFLLMUL); + posc->DFLLMUL.bit.MUL = 0xbb80; // 4800 x 1KHz + while (posc->DFLLSYNC.bit.DFLLMUL) + ; posc->DFLLCTRLA.bit.ENABLE = 1; - while (posc->DFLLSYNC.bit.ENABLE); + while (posc->DFLLSYNC.bit.ENABLE) + ; /* Setup clock for module */ - pgclk->PCHCTRL[GCLK_USB].bit.GEN = 0; + pgclk->PCHCTRL[GCLK_USB].bit.GEN = 0; pgclk->PCHCTRL[GCLK_USB].bit.CHEN = 1; /* Reset */ @@ -1084,21 +1019,21 @@ enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, /* Load Pad Calibration */ - pad_transn = (USB_FUSES_TRANSN_ADDR >> USB_FUSES_TRANSN_Pos) & USB_FUSES_TRANSN_Msk; + pad_transn = (USB_FUSES_TRANSN_ADDR >> USB_FUSES_TRANSN_Pos) & USB_FUSES_TRANSN_Msk; if (pad_transn == 0x1F) { pad_transn = 5; } hw->DEVICE.PADCAL.bit.TRANSN = pad_transn; - pad_transp = (USB_FUSES_TRANSP_ADDR >> USB_FUSES_TRANSP_Pos) & USB_FUSES_TRANSP_Msk; + pad_transp = (USB_FUSES_TRANSP_ADDR >> USB_FUSES_TRANSP_Pos) & USB_FUSES_TRANSP_Msk; if (pad_transp == 0x1F) { pad_transp = 29; } hw->DEVICE.PADCAL.bit.TRANSP = pad_transp; - pad_trim = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk; + pad_trim = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk; if (pad_trim == 0x07) { pad_trim = 3; } @@ -1106,32 +1041,31 @@ enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, hw->DEVICE.PADCAL.bit.TRIM = pad_trim; /* Set the configuration */ - hw->DEVICE.CTRLA.bit.MODE = module_config->select_host_mode; + hw->DEVICE.CTRLA.bit.MODE = module_config->select_host_mode; hw->DEVICE.CTRLA.bit.RUNSTDBY = module_config->run_in_standby; - hw->DEVICE.DESCADD.reg = (uint32_t)(&usb_descriptor_table.usb_endpoint_table[0]); + hw->DEVICE.DESCADD.reg = (uint32_t)(&usb_descriptor_table.usb_endpoint_table[0]); if (USB_SPEED_FULL == module_config->speed_mode) { module_inst->hw->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_FS_Val; - } else if(USB_SPEED_LOW == module_config->speed_mode) { + } else if (USB_SPEED_LOW == module_config->speed_mode) { module_inst->hw->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_LS_Val; } - memset((uint8_t *)(&usb_descriptor_table.usb_endpoint_table[0]), 0, - sizeof(usb_descriptor_table.usb_endpoint_table)); + memset((uint8_t *)(&usb_descriptor_table.usb_endpoint_table[0]), 0, sizeof(usb_descriptor_table.usb_endpoint_table)); /* device callback related */ for (i = 0; i < USB_DEVICE_CALLBACK_N; i++) { module_inst->device_callback[i] = NULL; } for (i = 0; i < USB_EPT_NUM; i++) { - for(j = 0; j < USB_DEVICE_EP_CALLBACK_N; j++) { + for (j = 0; j < USB_DEVICE_EP_CALLBACK_N; j++) { module_inst->device_endpoint_callback[i][j] = NULL; } } module_inst->device_registered_callback_mask = 0; - module_inst->device_enabled_callback_mask = 0; + module_inst->device_enabled_callback_mask = 0; for (j = 0; j < USB_EPT_NUM; j++) { module_inst->device_endpoint_registered_callback_mask[j] = 0; - module_inst->device_endpoint_enabled_callback_mask[j] = 0; + module_inst->device_endpoint_enabled_callback_mask[j] = 0; } /* Enable interrupts for this USB module */ @@ -1141,4 +1075,3 @@ enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, return STATUS_OK; } - diff --git a/tmk_core/protocol/arm_atsam/usb/usb.h b/tmk_core/protocol/arm_atsam/usb/usb.h index 9a452881a..4c937ecdc 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb.h +++ b/tmk_core/protocol/arm_atsam/usb/usb.h @@ -233,8 +233,8 @@ typedef void (*usb_host_pipe_callback_t)(struct usb_module *module_inst, void *) * \name Device Callback Functions Types * @{ */ -typedef void (*usb_device_callback_t)(struct usb_module *module_inst, void* pointer); -typedef void (*usb_device_endpoint_callback_t)(struct usb_module *module_inst, void* pointer); +typedef void (*usb_device_callback_t)(struct usb_module *module_inst, void *pointer); +typedef void (*usb_device_endpoint_callback_t)(struct usb_module *module_inst, void *pointer); /** @} */ /** USB configurations */ @@ -247,7 +247,7 @@ struct usb_config { // enum gclk_generator source_generator; uint8_t source_generator; /** Speed mode */ - //enum usb_speed speed_mode; + // enum usb_speed speed_mode; uint8_t speed_mode; }; @@ -263,7 +263,7 @@ struct usb_module { Usb *hw; /** Array to store device related callback functions */ - usb_device_callback_t device_callback[USB_DEVICE_CALLBACK_N]; + usb_device_callback_t device_callback[USB_DEVICE_CALLBACK_N]; usb_device_endpoint_callback_t device_endpoint_callback[USB_EPT_NUM][USB_DEVICE_EP_CALLBACK_N]; /** Bit mask for device callbacks registered */ uint16_t device_registered_callback_mask; @@ -292,7 +292,7 @@ struct usb_endpoint_callback_parameter { uint16_t received_bytes; uint16_t sent_bytes; uint16_t out_buffer_size; - uint8_t endpoint_address; + uint8_t endpoint_address; }; void usb_enable(struct usb_module *module_inst); @@ -303,8 +303,7 @@ void usb_disable(struct usb_module *module_inst); * * \param module_inst Pointer to USB module instance */ -static inline uint8_t usb_get_state_machine_status(struct usb_module *module_inst) -{ +static inline uint8_t usb_get_state_machine_status(struct usb_module *module_inst) { /* Sanity check arguments */ Assert(module_inst); Assert(module_inst->hw); @@ -312,29 +311,22 @@ static inline uint8_t usb_get_state_machine_status(struct usb_module *module_ins return module_inst->hw->DEVICE.FSMSTATUS.reg; } -void usb_get_config_defaults(struct usb_config *module_config); -enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, - struct usb_config *module_config); +void usb_get_config_defaults(struct usb_config *module_config); +enum status_code usb_init(struct usb_module *module_inst, Usb *const hw, struct usb_config *module_config); /** * \brief Attach USB device to the bus * * \param module_inst Pointer to USB device module instance */ -static inline void usb_device_attach(struct usb_module *module_inst) -{ - module_inst->hw->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_DETACH; -} +static inline void usb_device_attach(struct usb_module *module_inst) { module_inst->hw->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_DETACH; } /** * \brief Detach USB device from the bus * * \param module_inst Pointer to USB device module instance */ -static inline void usb_device_detach(struct usb_module *module_inst) -{ - module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_DETACH; -} +static inline void usb_device_detach(struct usb_module *module_inst) { module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_DETACH; } /** * \brief Get the speed mode of USB device @@ -342,8 +334,7 @@ static inline void usb_device_detach(struct usb_module *module_inst) * \param module_inst Pointer to USB device module instance * \return USB Speed mode (\ref usb_speed). */ -static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst) -{ +static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst) { if (!(module_inst->hw->DEVICE.STATUS.reg & USB_DEVICE_STATUS_SPEED_Msk)) { return USB_SPEED_FULL; } else { @@ -357,10 +348,7 @@ static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst * \param module_inst Pointer to USB device module instance * \return USB device address value. */ -static inline uint8_t usb_device_get_address(struct usb_module *module_inst) -{ - return ((uint8_t)(module_inst->hw->DEVICE.DADD.bit.DADD)); -} +static inline uint8_t usb_device_get_address(struct usb_module *module_inst) { return ((uint8_t)(module_inst->hw->DEVICE.DADD.bit.DADD)); } /** * \brief Set the speed mode of USB device @@ -368,10 +356,7 @@ static inline uint8_t usb_device_get_address(struct usb_module *module_inst) * \param module_inst Pointer to USB device module instance * \param address USB device address value */ -static inline void usb_device_set_address(struct usb_module *module_inst, uint8_t address) -{ - module_inst->hw->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address; -} +static inline void usb_device_set_address(struct usb_module *module_inst, uint8_t address) { module_inst->hw->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address; } /** * \brief Get the frame number of USB device @@ -379,10 +364,7 @@ static inline void usb_device_set_address(struct usb_module *module_inst, uint8_ * \param module_inst Pointer to USB device module instance * \return USB device frame number value. */ -static inline uint16_t usb_device_get_frame_number(struct usb_module *module_inst) -{ - return ((uint16_t)(module_inst->hw->DEVICE.FNUM.bit.FNUM)); -} +static inline uint16_t usb_device_get_frame_number(struct usb_module *module_inst) { return ((uint16_t)(module_inst->hw->DEVICE.FNUM.bit.FNUM)); } /** * \brief Get the micro-frame number of USB device @@ -390,20 +372,14 @@ static inline uint16_t usb_device_get_frame_number(struct usb_module *module_ins * \param module_inst Pointer to USB device module instance * \return USB device micro-frame number value. */ -static inline uint16_t usb_device_get_micro_frame_number(struct usb_module *module_inst) -{ - return ((uint16_t)(module_inst->hw->DEVICE.FNUM.reg)); -} +static inline uint16_t usb_device_get_micro_frame_number(struct usb_module *module_inst) { return ((uint16_t)(module_inst->hw->DEVICE.FNUM.reg)); } /** * \brief USB device send the resume wakeup * * \param module_inst Pointer to USB device module instance */ -static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst) -{ - module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_UPRSM; -} +static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst) { module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_UPRSM; } /** * \brief USB device set the LPM mode @@ -411,67 +387,45 @@ static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst * \param module_inst Pointer to USB device module instance * \param lpm_mode LPM mode */ -static inline void usb_device_set_lpm_mode(struct usb_module *module_inst, - enum usb_device_lpm_mode lpm_mode) -{ - module_inst->hw->DEVICE.CTRLB.bit.LPMHDSK = lpm_mode; -} +static inline void usb_device_set_lpm_mode(struct usb_module *module_inst, enum usb_device_lpm_mode lpm_mode) { module_inst->hw->DEVICE.CTRLB.bit.LPMHDSK = lpm_mode; } /** * \name USB Device Callback Management * @{ */ -enum status_code usb_device_register_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type, - usb_device_callback_t callback_func); -enum status_code usb_device_unregister_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type); -enum status_code usb_device_enable_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type); -enum status_code usb_device_disable_callback(struct usb_module *module_inst, - enum usb_device_callback callback_type); +enum status_code usb_device_register_callback(struct usb_module *module_inst, enum usb_device_callback callback_type, usb_device_callback_t callback_func); +enum status_code usb_device_unregister_callback(struct usb_module *module_inst, enum usb_device_callback callback_type); +enum status_code usb_device_enable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type); +enum status_code usb_device_disable_callback(struct usb_module *module_inst, enum usb_device_callback callback_type); /** @} */ /** * \name USB Device Endpoint Configuration * @{ */ -void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config); -enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, - struct usb_device_endpoint_config *ep_config); -bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep); +void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config); +enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst, struct usb_device_endpoint_config *ep_config); +bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep); /** @} */ /** * \name USB Device Endpoint Callback Management * @{ */ -enum status_code usb_device_endpoint_register_callback( - struct usb_module *module_inst, uint8_t ep_num, - enum usb_device_endpoint_callback callback_type, - usb_device_endpoint_callback_t callback_func); -enum status_code usb_device_endpoint_unregister_callback( - struct usb_module *module_inst, uint8_t ep_num, - enum usb_device_endpoint_callback callback_type); -enum status_code usb_device_endpoint_enable_callback( - struct usb_module *module_inst, uint8_t ep, - enum usb_device_endpoint_callback callback_type); -enum status_code usb_device_endpoint_disable_callback( - struct usb_module *module_inst, uint8_t ep, - enum usb_device_endpoint_callback callback_type); +enum status_code usb_device_endpoint_register_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type, usb_device_endpoint_callback_t callback_func); +enum status_code usb_device_endpoint_unregister_callback(struct usb_module *module_inst, uint8_t ep_num, enum usb_device_endpoint_callback callback_type); +enum status_code usb_device_endpoint_enable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type); +enum status_code usb_device_endpoint_disable_callback(struct usb_module *module_inst, uint8_t ep, enum usb_device_endpoint_callback callback_type); /** @} */ /** * \name USB Device Endpoint Job Management * @{ */ -enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst,uint8_t ep_num, - uint8_t* pbuf, uint32_t buf_size); -enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst,uint8_t ep_num, - uint8_t* pbuf, uint32_t buf_size); -enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst, - uint8_t* pbuf); -void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep); +enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size); +enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst, uint8_t ep_num, uint8_t *pbuf, uint32_t buf_size); +enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst, uint8_t *pbuf); +void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep); /** @} */ /** diff --git a/tmk_core/protocol/arm_atsam/usb/usb2422.c b/tmk_core/protocol/arm_atsam/usb/usb2422.c index 76ec3aaaa..a878cb6b7 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb2422.c +++ b/tmk_core/protocol/arm_atsam/usb/usb2422.c @@ -18,23 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "arm_atsam_protocol.h" #include <string.h> -Usb2422 USB2422_shadow; +Usb2422 USB2422_shadow; unsigned char i2c0_buf[34]; -const uint16_t MFRNAME[] = { 'M','a','s','s','d','r','o','p',' ','I','n','c','.' }; //Massdrop Inc. -const uint16_t PRDNAME[] = { 'M','a','s','s','d','r','o','p',' ','H','u','b' }; //Massdrop Hub +const uint16_t MFRNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'I', 'n', 'c', '.'}; // Massdrop Inc. +const uint16_t PRDNAME[] = {'M', 'a', 's', 's', 'd', 'r', 'o', 'p', ' ', 'H', 'u', 'b'}; // Massdrop Hub #ifndef MD_BOOTLOADER -//Serial number reported stops before first found space character or at last found character -const uint16_t SERNAME[] = { 'U','n','a','v','a','i','l','a','b','l','e' }; //Unavailable +// Serial number reported stops before first found space character or at last found character +const uint16_t SERNAME[] = {'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e'}; // Unavailable #else -//In production, this field is found, modified, and offset noted as the last 32-bit word in the bootloader space -//The offset allows the application to use the factory programmed serial (which may differ from the physical serial label) -//Serial number reported stops before first found space character or when max size is reached -__attribute__((__aligned__(4))) -const uint16_t SERNAME[BOOTLOADER_SERIAL_MAX_SIZE] = { 'M','D','H','U','B','B','O','O','T','L','0','0','0','0','0','0','0','0','0','0' }; -//NOTE: Serial replacer will not write a string longer than given here as a precaution, so give enough +// In production, this field is found, modified, and offset noted as the last 32-bit word in the bootloader space +// The offset allows the application to use the factory programmed serial (which may differ from the physical serial label) +// Serial number reported stops before first found space character or when max size is reached +__attribute__((__aligned__(4))) const uint16_t SERNAME[BOOTLOADER_SERIAL_MAX_SIZE] = {'M', 'D', 'H', 'U', 'B', 'B', 'O', 'O', 'T', 'L', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'}; +// NOTE: Serial replacer will not write a string longer than given here as a precaution, so give enough // space as needed and adjust BOOTLOADER_SERIAL_MAX_SIZE to match amount given -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER uint8_t usb_host_port; @@ -44,95 +43,116 @@ uint8_t usb_extra_state; uint8_t usb_extra_manual; uint8_t usb_gcr_auto; -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER uint16_t adc_extra; -void USB_write2422_block(void) -{ +void USB_write2422_block(void) { unsigned char *dest = i2c0_buf; unsigned char *src; unsigned char *base = (unsigned char *)&USB2422_shadow; DBGC(DC_USB_WRITE2422_BLOCK_BEGIN); - for (src = base; src < base + 256; src += 32) - { + for (src = base; src < base + 256; src += 32) { dest[0] = src - base; dest[1] = 32; memcpy(&dest[2], src, 32); i2c0_transmit(USB2422_ADDR, dest, 34, 50000); SERCOM0->I2CM.CTRLB.bit.CMD = 0x03; - while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); } + while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) { + DBGC(DC_USB_WRITE2422_BLOCK_SYNC_SYSOP); + } wait_us(100); } DBGC(DC_USB_WRITE2422_BLOCK_COMPLETE); } -void USB2422_init(void) -{ - Gclk *pgclk = GCLK; - Mclk *pmclk = MCLK; - Port *pport = PORT; - Oscctrl *posc = OSCCTRL; - Usb *pusb = USB; +void USB2422_init(void) { + Gclk * pgclk = GCLK; + Mclk * pmclk = MCLK; + Port * pport = PORT; + Oscctrl *posc = OSCCTRL; + Usb * pusb = USB; DBGC(DC_USB2422_INIT_BEGIN); - while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { DBGC(DC_USB2422_INIT_WAIT_5V_LOW); } + while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { + DBGC(DC_USB2422_INIT_WAIT_5V_LOW); + } - //setup peripheral and synchronous bus clocks to USB - pgclk->PCHCTRL[10].bit.GEN = 0; + // setup peripheral and synchronous bus clocks to USB + pgclk->PCHCTRL[10].bit.GEN = 0; pgclk->PCHCTRL[10].bit.CHEN = 1; - pmclk->AHBMASK.bit.USB_ = 1; - pmclk->APBBMASK.bit.USB_ = 1; + pmclk->AHBMASK.bit.USB_ = 1; + pmclk->APBBMASK.bit.USB_ = 1; - //setup port pins for D-, D+, and SOF_1KHZ - pport->Group[0].PMUX[12].reg = 0x77; //PA24, PA25, function column H for USB D-, D+ + // setup port pins for D-, D+, and SOF_1KHZ + pport->Group[0].PMUX[12].reg = 0x77; // PA24, PA25, function column H for USB D-, D+ pport->Group[0].PINCFG[24].bit.PMUXEN = 1; pport->Group[0].PINCFG[25].bit.PMUXEN = 1; - pport->Group[1].PMUX[11].bit.PMUXE = 7; //PB22, function column H for USB SOF_1KHz output + pport->Group[1].PMUX[11].bit.PMUXE = 7; // PB22, function column H for USB SOF_1KHz output pport->Group[1].PINCFG[22].bit.PMUXEN = 1; - //configure and enable DFLL for USB clock recovery mode at 48MHz + // configure and enable DFLL for USB clock recovery mode at 48MHz posc->DFLLCTRLA.bit.ENABLE = 0; - while (posc->DFLLSYNC.bit.ENABLE) { DBGC(DC_USB2422_INIT_OSC_SYNC_DISABLING); } - while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_1); } + while (posc->DFLLSYNC.bit.ENABLE) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DISABLING); + } + while (posc->DFLLSYNC.bit.DFLLCTRLB) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_1); + } posc->DFLLCTRLB.bit.USBCRM = 1; - while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_2); } + while (posc->DFLLSYNC.bit.DFLLCTRLB) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_2); + } posc->DFLLCTRLB.bit.MODE = 1; - while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_3); } + while (posc->DFLLSYNC.bit.DFLLCTRLB) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_3); + } posc->DFLLCTRLB.bit.QLDIS = 0; - while (posc->DFLLSYNC.bit.DFLLCTRLB) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_4); } + while (posc->DFLLSYNC.bit.DFLLCTRLB) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLCTRLB_4); + } posc->DFLLCTRLB.bit.CCDIS = 1; - posc->DFLLMUL.bit.MUL = 0xBB80; //4800 x 1KHz - while (posc->DFLLSYNC.bit.DFLLMUL) { DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLMUL); } + posc->DFLLMUL.bit.MUL = 0xBB80; // 4800 x 1KHz + while (posc->DFLLSYNC.bit.DFLLMUL) { + DBGC(DC_USB2422_INIT_OSC_SYNC_DFLLMUL); + } posc->DFLLCTRLA.bit.ENABLE = 1; - while (posc->DFLLSYNC.bit.ENABLE) { DBGC(DC_USB2422_INIT_OSC_SYNC_ENABLING); } + while (posc->DFLLSYNC.bit.ENABLE) { + DBGC(DC_USB2422_INIT_OSC_SYNC_ENABLING); + } pusb->DEVICE.CTRLA.bit.SWRST = 1; - while (pusb->DEVICE.SYNCBUSY.bit.SWRST) { DBGC(DC_USB2422_INIT_USB_SYNC_SWRST); } - while (pusb->DEVICE.CTRLA.bit.SWRST) { DBGC(DC_USB2422_INIT_USB_WAIT_SWRST); } - //calibration from factory presets + while (pusb->DEVICE.SYNCBUSY.bit.SWRST) { + DBGC(DC_USB2422_INIT_USB_SYNC_SWRST); + } + while (pusb->DEVICE.CTRLA.bit.SWRST) { + DBGC(DC_USB2422_INIT_USB_WAIT_SWRST); + } + // calibration from factory presets pusb->DEVICE.PADCAL.bit.TRANSN = (USB_FUSES_TRANSN_ADDR >> USB_FUSES_TRANSN_Pos) & USB_FUSES_TRANSN_Msk; pusb->DEVICE.PADCAL.bit.TRANSP = (USB_FUSES_TRANSP_ADDR >> USB_FUSES_TRANSP_Pos) & USB_FUSES_TRANSP_Msk; - pusb->DEVICE.PADCAL.bit.TRIM = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk; - //device mode, enabled - pusb->DEVICE.CTRLB.bit.SPDCONF = 0; //full speed - pusb->DEVICE.CTRLA.bit.MODE = 0; - pusb->DEVICE.CTRLA.bit.ENABLE = 1; - while (pusb->DEVICE.SYNCBUSY.bit.ENABLE) { DBGC(DC_USB2422_INIT_USB_SYNC_ENABLING); } + pusb->DEVICE.PADCAL.bit.TRIM = (USB_FUSES_TRIM_ADDR >> USB_FUSES_TRIM_Pos) & USB_FUSES_TRIM_Msk; + // device mode, enabled + pusb->DEVICE.CTRLB.bit.SPDCONF = 0; // full speed + pusb->DEVICE.CTRLA.bit.MODE = 0; + pusb->DEVICE.CTRLA.bit.ENABLE = 1; + while (pusb->DEVICE.SYNCBUSY.bit.ENABLE) { + DBGC(DC_USB2422_INIT_USB_SYNC_ENABLING); + } pusb->DEVICE.QOSCTRL.bit.DQOS = 2; pusb->DEVICE.QOSCTRL.bit.CQOS = 2; pport->Group[USB2422_HUB_ACTIVE_GROUP].PINCFG[USB2422_HUB_ACTIVE_PIN].bit.INEN = 1; - i2c0_init(); //IC2 clk must be high at USB2422 reset release time to signal SMB configuration + i2c0_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration - sr_exp_data.bit.HUB_CONNECT = 1; //connect signal - sr_exp_data.bit.HUB_RESET_N = 1; //reset high + sr_exp_data.bit.HUB_CONNECT = 1; // connect signal + sr_exp_data.bit.HUB_RESET_N = 1; // reset high SR_EXP_WriteData(); wait_us(100); @@ -140,78 +160,74 @@ void USB2422_init(void) #ifndef MD_BOOTLOADER usb_extra_manual = 0; - usb_gcr_auto = 1; + usb_gcr_auto = 1; -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER DBGC(DC_USB2422_INIT_COMPLETE); } -void USB_reset(void) -{ +void USB_reset(void) { DBGC(DC_USB_RESET_BEGIN); - //pulse reset for at least 1 usec - sr_exp_data.bit.HUB_RESET_N = 0; //reset low + // pulse reset for at least 1 usec + sr_exp_data.bit.HUB_RESET_N = 0; // reset low SR_EXP_WriteData(); wait_us(2); - sr_exp_data.bit.HUB_RESET_N = 1; //reset high to run + sr_exp_data.bit.HUB_RESET_N = 1; // reset high to run SR_EXP_WriteData(); DBGC(DC_USB_RESET_COMPLETE); } -void USB_configure(void) -{ +void USB_configure(void) { Usb2422 *pusb2422 = &USB2422_shadow; memset(pusb2422, 0, sizeof(Usb2422)); - uint16_t *serial_use = (uint16_t *)SERNAME; //Default to use SERNAME from this file - uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); //Default to use SERNAME from this file + uint16_t *serial_use = (uint16_t *)SERNAME; // Default to use SERNAME from this file + uint8_t serial_length = sizeof(SERNAME) / sizeof(uint16_t); // Default to use SERNAME from this file #ifndef MD_BOOTLOADER uint32_t serial_ptrloc = (uint32_t)&_srom - 4; -#else //MD_BOOTLOADER +#else // MD_BOOTLOADER uint32_t serial_ptrloc = (uint32_t)&_erom - 4; -#endif //MD_BOOTLOADER - uint32_t serial_address = *(uint32_t *)serial_ptrloc; //Address of bootloader's serial number if available +#endif // MD_BOOTLOADER + uint32_t serial_address = *(uint32_t *)serial_ptrloc; // Address of bootloader's serial number if available DBGC(DC_USB_CONFIGURE_BEGIN); - if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) //Check for factory programmed serial address + if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) // Check for factory programmed serial address { - if ((serial_address & 0xFF) % 4 == 0) //Check alignment + if ((serial_address & 0xFF) % 4 == 0) // Check alignment { - serial_use = (uint16_t *)(serial_address); + serial_use = (uint16_t *)(serial_address); serial_length = 0; - while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && - serial_length < BOOTLOADER_SERIAL_MAX_SIZE) - { + while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) && serial_length < BOOTLOADER_SERIAL_MAX_SIZE) { serial_length++; DBGC(DC_USB_CONFIGURE_GET_SERIAL); } } } - //configure Usb2422 registers - pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018 - pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub - pusb2422->DID.reg = 0x0101; // BCD 01.01 - pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now - pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed - //pusb2422->CFG2.bit.COMPOUND = 0; // compound device - pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled - //pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable - pusb2422->MAXPB.reg = 20; // 0mA - pusb2422->HCMCB.reg = 20; // 0mA + // configure Usb2422 registers + pusb2422->VID.reg = 0x04D8; // from Microchip 4/19/2018 + pusb2422->PID.reg = 0xEEC5; // from Microchip 4/19/2018 = Massdrop, Inc. USB Hub + pusb2422->DID.reg = 0x0101; // BCD 01.01 + pusb2422->CFG1.bit.SELF_BUS_PWR = 1; // self powered for now + pusb2422->CFG1.bit.HS_DISABLE = 1; // full or high speed + // pusb2422->CFG2.bit.COMPOUND = 0; // compound device + pusb2422->CFG3.bit.STRING_EN = 1; // strings enabled + // pusb2422->NRD.bit.PORT2_NR = 0; // MCU is non-removable + pusb2422->MAXPB.reg = 20; // 0mA + pusb2422->HCMCB.reg = 20; // 0mA pusb2422->MFRSL.reg = sizeof(MFRNAME) / sizeof(uint16_t); pusb2422->PRDSL.reg = sizeof(PRDNAME) / sizeof(uint16_t); pusb2422->SERSL.reg = serial_length; memcpy(pusb2422->MFRSTR, MFRNAME, sizeof(MFRNAME)); memcpy(pusb2422->PRDSTR, PRDNAME, sizeof(PRDNAME)); memcpy(pusb2422->SERSTR, serial_use, serial_length * sizeof(uint16_t)); - //pusb2422->BOOSTUP.bit.BOOST=3; //upstream port - //pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port - //pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close + // pusb2422->BOOSTUP.bit.BOOST=3; //upstream port + // pusb2422->BOOSTDOWN.bit.BOOST1=0; // extra port + // pusb2422->BOOSTDOWN.bit.BOOST2=2; //MCU is close pusb2422->STCD.bit.USB_ATTACH = 1; USB_write2422_block(); @@ -220,35 +236,33 @@ void USB_configure(void) DBGC(DC_USB_CONFIGURE_COMPLETE); } -uint16_t USB_active(void) -{ - return (PORT->Group[USB2422_HUB_ACTIVE_GROUP].IN.reg & (1 << USB2422_HUB_ACTIVE_PIN)) != 0; -} +uint16_t USB_active(void) { return (PORT->Group[USB2422_HUB_ACTIVE_GROUP].IN.reg & (1 << USB2422_HUB_ACTIVE_PIN)) != 0; } -void USB_set_host_by_voltage(void) -{ - //UP is upstream device (HOST) - //DN1 is downstream device (EXTRA) - //DN2 is keyboard (KEYB) +void USB_set_host_by_voltage(void) { + // UP is upstream device (HOST) + // DN1 is downstream device (EXTRA) + // DN2 is keyboard (KEYB) DBGC(DC_USB_SET_HOST_BY_VOLTAGE_BEGIN); usb_host_port = USB_HOST_PORT_UNKNOWN; #ifndef MD_BOOTLOADER usb_extra_state = USB_EXTRA_STATE_UNKNOWN; -#endif //MD_BOOTLOADER - sr_exp_data.bit.SRC_1 = 1; //USBC-1 available for test - sr_exp_data.bit.SRC_2 = 1; //USBC-2 available for test - sr_exp_data.bit.E_UP_N = 1; //HOST disable - sr_exp_data.bit.E_DN1_N = 1; //EXTRA disable - sr_exp_data.bit.E_VBUS_1 = 0; //USBC-1 disable full power I/O - sr_exp_data.bit.E_VBUS_2 = 0; //USBC-2 disable full power I/O +#endif // MD_BOOTLOADER + sr_exp_data.bit.SRC_1 = 1; // USBC-1 available for test + sr_exp_data.bit.SRC_2 = 1; // USBC-2 available for test + sr_exp_data.bit.E_UP_N = 1; // HOST disable + sr_exp_data.bit.E_DN1_N = 1; // EXTRA disable + sr_exp_data.bit.E_VBUS_1 = 0; // USBC-1 disable full power I/O + sr_exp_data.bit.E_VBUS_2 = 0; // USBC-2 disable full power I/O SR_EXP_WriteData(); wait_ms(250); - while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { DBGC(DC_USB_SET_HOST_5V_LOW_WAITING); } + while ((v_5v = adc_get(ADC_5V)) < ADC_5V_START_LEVEL) { + DBGC(DC_USB_SET_HOST_5V_LOW_WAITING); + } v_con_1 = adc_get(ADC_CON1); v_con_2 = adc_get(ADC_CON2); @@ -256,37 +270,34 @@ void USB_set_host_by_voltage(void) v_con_1_boot = v_con_1; v_con_2_boot = v_con_2; - if (v_con_1 > v_con_2) - { - sr_exp_data.bit.S_UP = 0; //HOST to USBC-1 - sr_exp_data.bit.S_DN1 = 1; //EXTRA to USBC-2 - sr_exp_data.bit.SRC_1 = 1; //HOST on USBC-1 - sr_exp_data.bit.SRC_2 = 0; //EXTRA available on USBC-2 + if (v_con_1 > v_con_2) { + sr_exp_data.bit.S_UP = 0; // HOST to USBC-1 + sr_exp_data.bit.S_DN1 = 1; // EXTRA to USBC-2 + sr_exp_data.bit.SRC_1 = 1; // HOST on USBC-1 + sr_exp_data.bit.SRC_2 = 0; // EXTRA available on USBC-2 - sr_exp_data.bit.E_VBUS_1 = 1; //USBC-1 enable full power I/O - sr_exp_data.bit.E_VBUS_2 = 0; //USBC-2 disable full power I/O + sr_exp_data.bit.E_VBUS_1 = 1; // USBC-1 enable full power I/O + sr_exp_data.bit.E_VBUS_2 = 0; // USBC-2 disable full power I/O SR_EXP_WriteData(); - sr_exp_data.bit.E_UP_N = 0; //HOST enable + sr_exp_data.bit.E_UP_N = 0; // HOST enable SR_EXP_WriteData(); usb_host_port = USB_HOST_PORT_1; - } - else - { - sr_exp_data.bit.S_UP = 1; //EXTRA to USBC-1 - sr_exp_data.bit.S_DN1 = 0; //HOST to USBC-2 - sr_exp_data.bit.SRC_1 = 0; //EXTRA available on USBC-1 - sr_exp_data.bit.SRC_2 = 1; //HOST on USBC-2 + } else { + sr_exp_data.bit.S_UP = 1; // EXTRA to USBC-1 + sr_exp_data.bit.S_DN1 = 0; // HOST to USBC-2 + sr_exp_data.bit.SRC_1 = 0; // EXTRA available on USBC-1 + sr_exp_data.bit.SRC_2 = 1; // HOST on USBC-2 - sr_exp_data.bit.E_VBUS_1 = 0; //USBC-1 disable full power I/O - sr_exp_data.bit.E_VBUS_2 = 1; //USBC-2 enable full power I/O + sr_exp_data.bit.E_VBUS_1 = 0; // USBC-1 disable full power I/O + sr_exp_data.bit.E_VBUS_2 = 1; // USBC-2 enable full power I/O SR_EXP_WriteData(); - sr_exp_data.bit.E_UP_N = 0; //HOST enable + sr_exp_data.bit.E_UP_N = 0; // HOST enable SR_EXP_WriteData(); @@ -295,7 +306,7 @@ void USB_set_host_by_voltage(void) #ifndef MD_BOOTLOADER usb_extra_state = USB_EXTRA_STATE_DISABLED; -#endif //MD_BOOTLOADER +#endif // MD_BOOTLOADER USB_reset(); USB_configure(); @@ -303,8 +314,7 @@ void USB_set_host_by_voltage(void) DBGC(DC_USB_SET_HOST_BY_VOLTAGE_COMPLETE); } -uint8_t USB2422_Port_Detect_Init(void) -{ +uint8_t USB2422_Port_Detect_Init(void) { uint32_t port_detect_retry_ms; uint32_t tmod; @@ -314,26 +324,30 @@ uint8_t USB2422_Port_Detect_Init(void) port_detect_retry_ms = timer_read64() + PORT_DETECT_RETRY_INTERVAL; - while (!USB_active()) - { + while (!USB_active()) { tmod = timer_read64() % PORT_DETECT_RETRY_INTERVAL; - if (v_con_1 > v_con_2) //Values updated from USB_set_host_by_voltage(); + if (v_con_1 > v_con_2) // Values updated from USB_set_host_by_voltage(); { - //1 flash for port 1 detected - if (tmod > 500 && tmod < 600) { DBG_LED_ON; } - else { DBG_LED_OFF; } - } - else if (v_con_2 > v_con_1) //Values updated from USB_set_host_by_voltage(); + // 1 flash for port 1 detected + if (tmod > 500 && tmod < 600) { + DBG_LED_ON; + } else { + DBG_LED_OFF; + } + } else if (v_con_2 > v_con_1) // Values updated from USB_set_host_by_voltage(); { - //2 flash for port 2 detected - if (tmod > 500 && tmod < 600) { DBG_LED_ON; } - else if (tmod > 700 && tmod < 800) { DBG_LED_ON; } - else { DBG_LED_OFF; } + // 2 flash for port 2 detected + if (tmod > 500 && tmod < 600) { + DBG_LED_ON; + } else if (tmod > 700 && tmod < 800) { + DBG_LED_ON; + } else { + DBG_LED_OFF; + } } - if (timer_read64() > port_detect_retry_ms) - { + if (timer_read64() > port_detect_retry_ms) { DBGC(DC_PORT_DETECT_INIT_FAILED); return 0; } @@ -346,65 +360,67 @@ uint8_t USB2422_Port_Detect_Init(void) #ifndef MD_BOOTLOADER -void USB_ExtraSetState(uint8_t state) -{ +void USB_ExtraSetState(uint8_t state) { uint8_t state_save = state; - if (state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) - state = USB_EXTRA_STATE_DISABLED; + if (state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) state = USB_EXTRA_STATE_DISABLED; - if (usb_host_port == USB_HOST_PORT_1) sr_exp_data.bit.E_VBUS_2 = state; - else if (usb_host_port == USB_HOST_PORT_2) sr_exp_data.bit.E_VBUS_1 = state; - else return; + if (usb_host_port == USB_HOST_PORT_1) + sr_exp_data.bit.E_VBUS_2 = state; + else if (usb_host_port == USB_HOST_PORT_2) + sr_exp_data.bit.E_VBUS_1 = state; + else + return; sr_exp_data.bit.E_DN1_N = !state; SR_EXP_WriteData(); usb_extra_state = state_save; - if (usb_extra_state == USB_EXTRA_STATE_ENABLED) CDC_print("USB: Extra enabled\r\n"); - else if (usb_extra_state == USB_EXTRA_STATE_DISABLED) - { + if (usb_extra_state == USB_EXTRA_STATE_ENABLED) + CDC_print("USB: Extra enabled\r\n"); + else if (usb_extra_state == USB_EXTRA_STATE_DISABLED) { CDC_print("USB: Extra disabled\r\n"); -#ifdef USE_MASSDROP_CONFIGURATOR +# ifdef USE_MASSDROP_CONFIGURATOR if (led_animation_breathing) gcr_breathe = gcr_desired; -#endif - } - else if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) CDC_print("USB: Extra disabled until replug\r\n"); - else CDC_print("USB: Extra state unknown\r\n"); +# endif + } else if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) + CDC_print("USB: Extra disabled until replug\r\n"); + else + CDC_print("USB: Extra state unknown\r\n"); } -void USB_HandleExtraDevice(void) -{ +void USB_HandleExtraDevice(void) { uint16_t adcval; - if (usb_host_port == USB_HOST_PORT_1) adcval = adc_get(ADC_CON2); - else if (usb_host_port == USB_HOST_PORT_2) adcval = adc_get(ADC_CON1); - else return; + if (usb_host_port == USB_HOST_PORT_1) + adcval = adc_get(ADC_CON2); + else if (usb_host_port == USB_HOST_PORT_2) + adcval = adc_get(ADC_CON1); + else + return; adc_extra = adc_extra * 0.9 + adcval * 0.1; - //Check for a forced disable state (such as overload prevention) - if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) - { - //Detect unplug and reset state to disabled + // Check for a forced disable state (such as overload prevention) + if (usb_extra_state == USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) { + // Detect unplug and reset state to disabled if (adc_extra > USB_EXTRA_ADC_THRESHOLD) usb_extra_state = USB_EXTRA_STATE_DISABLED; - return; //Return even if unplug detected + return; // Return even if unplug detected } - if (usb_extra_manual) - { - if (usb_extra_state == USB_EXTRA_STATE_DISABLED) - USB_ExtraSetState(USB_EXTRA_STATE_ENABLED); + if (usb_extra_manual) { + if (usb_extra_state == USB_EXTRA_STATE_DISABLED) USB_ExtraSetState(USB_EXTRA_STATE_ENABLED); return; } - //dpf("a %i %i\r\n",adcval, adc_extra); - if (usb_extra_state == USB_EXTRA_STATE_DISABLED && adc_extra < USB_EXTRA_ADC_THRESHOLD) USB_ExtraSetState(USB_EXTRA_STATE_ENABLED); - else if (usb_extra_state == USB_EXTRA_STATE_ENABLED && adc_extra > USB_EXTRA_ADC_THRESHOLD) USB_ExtraSetState(USB_EXTRA_STATE_DISABLED); + // dpf("a %i %i\r\n",adcval, adc_extra); + if (usb_extra_state == USB_EXTRA_STATE_DISABLED && adc_extra < USB_EXTRA_ADC_THRESHOLD) + USB_ExtraSetState(USB_EXTRA_STATE_ENABLED); + else if (usb_extra_state == USB_EXTRA_STATE_ENABLED && adc_extra > USB_EXTRA_ADC_THRESHOLD) + USB_ExtraSetState(USB_EXTRA_STATE_DISABLED); } -#endif //MD_BOOTLOADER - +#endif // MD_BOOTLOADER diff --git a/tmk_core/protocol/arm_atsam/usb/usb2422.h b/tmk_core/protocol/arm_atsam/usb/usb2422.h index 85ad596a6..b4830b5bc 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb2422.h +++ b/tmk_core/protocol/arm_atsam/usb/usb2422.h @@ -18,226 +18,225 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #ifndef _USB2422_H_ #define _USB2422_H_ -#define REV_USB2422 0x100 +#define REV_USB2422 0x100 -#define USB2422_ADDR 0x58 //I2C device address, one instance +#define USB2422_ADDR 0x58 // I2C device address, one instance -#define USB2422_HUB_ACTIVE_GROUP 0 //PA -#define USB2422_HUB_ACTIVE_PIN 18 //18 +#define USB2422_HUB_ACTIVE_GROUP 0 // PA +#define USB2422_HUB_ACTIVE_PIN 18 // 18 /* -------- USB2422_VID : (USB2422L Offset: 0x00) (R/W 16) Vendor ID -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint16_t VID_LSB : 8; - uint16_t VID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ + struct { + uint16_t VID_LSB : 8; + uint16_t VID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ } USB2422_VID_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PID : (USB2422L Offset: 0x02) (R/W 16) Product ID -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint16_t PID_LSB : 8; - uint16_t PID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ + struct { + uint16_t PID_LSB : 8; + uint16_t PID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ } USB2422_PID_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_DID : (USB2422L Offset: 0x04) (R/W 16) Device ID -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint16_t DID_LSB : 8; - uint16_t DID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint16_t reg; /*!< Type used for register access */ + struct { + uint16_t DID_LSB : 8; + uint16_t DID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint16_t reg; /*!< Type used for register access */ } USB2422_DID_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_CFG1 : (USB2422L Offset: 0x06) (R/W 8) Configuration Data Byte 1-------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t PORT_PWR : 1; - uint8_t CURRENT_SNS : 2; - uint8_t EOP_DISABLE : 1; - uint8_t MTT_ENABLE : 1; - uint8_t HS_DISABLE :1; - uint8_t :1; - uint8_t SELF_BUS_PWR : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t PORT_PWR : 1; + uint8_t CURRENT_SNS : 2; + uint8_t EOP_DISABLE : 1; + uint8_t MTT_ENABLE : 1; + uint8_t HS_DISABLE : 1; + uint8_t : 1; + uint8_t SELF_BUS_PWR : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_CFG1_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_CFG2 : (USB2422L Offset: 0x07) (R/W 8) Configuration Data Byte 2-------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 3; - uint8_t COMPOUND : 1; - uint8_t OC_TIMER :2; - uint8_t :1; - uint8_t DYNAMIC : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 3; + uint8_t COMPOUND : 1; + uint8_t OC_TIMER : 2; + uint8_t : 1; + uint8_t DYNAMIC : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_CFG2_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_CFG3 : (USB2422L Offset: 0x08) (R/W 16) Configuration Data Byte 3-------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t STRING_EN : 1; - uint8_t :2; - uint8_t PRTMAP_EN :1; - uint8_t : 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t STRING_EN : 1; + uint8_t : 2; + uint8_t PRTMAP_EN : 1; + uint8_t : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_CFG3_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_NRD : (USB2422L Offset: 0x09) (R/W 8) Non Removable Device -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 5; - uint8_t PORT2_NR :1; - uint8_t PORT1_NR :1; - uint8_t : 1; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 5; + uint8_t PORT2_NR : 1; + uint8_t PORT1_NR : 1; + uint8_t : 1; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_NRD_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PDS : (USB2422L Offset: 0x0A) (R/W 8) Port Diable for Self-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_DIS :1; - uint8_t PORT2_DIS :1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 1; + uint8_t PORT1_DIS : 1; + uint8_t PORT2_DIS : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PDS_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PDB : (USB2422L Offset: 0x0B) (R/W 8) Port Diable for Bus-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_DIS :1; - uint8_t PORT2_DIS :1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 1; + uint8_t PORT1_DIS : 1; + uint8_t PORT2_DIS : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PDB_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_MAXPS : (USB2422L Offset: 0x0C) (R/W 8) Max Power for Self-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t MAX_PWR_SP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t MAX_PWR_SP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_MAXPS_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_MAXPB : (USB2422L Offset: 0x0D) (R/W 8) Max Power for Bus-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t MAX_PWR_BP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t MAX_PWR_BP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_MAXPB_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_HCMCS : (USB2422L Offset: 0x0E) (R/W 8) Hub Controller Max Current for Self-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t HC_MAX_C_SP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t HC_MAX_C_SP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_HCMCS_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_HCMCB : (USB2422L Offset: 0x0F) (R/W 8) Hub Controller Max Current for Bus-Powered Operation -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t HC_MAX_C_BP : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t HC_MAX_C_BP : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_HCMCB_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PWRT : (USB2422L Offset: 0x10) (R/W 8) Power On Time -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t POWER_ON_TIME : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t POWER_ON_TIME : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PWRT_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_LANGID LSB : (USB2422L Offset: 0x11) (R/W 16) Language ID -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t LANGID_LSB : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t LANGID_LSB : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_LANGID_LSB_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_LANGID MSB : (USB2422L Offset: 0x12) (R/W 16) Language ID -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t LANGID_MSB : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t LANGID_MSB : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_LANGID_MSB_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ - /* -------- USB2422_MFRSL : (USB2422L Offset: 0x13) (R/W 8) Manufacturer String Length -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t MFR_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t MFR_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_MFRSL_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PRDSL : (USB2422L Offset: 0x14) (R/W 8) Product String Length -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t PRD_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t PRD_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PRDSL_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_SERSL : (USB2422L Offset: 0x15) (R/W 8) Serial String Length -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t SER_STR_LEN : 8; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t SER_STR_LEN : 8; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_SERSL_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ @@ -259,60 +258,60 @@ typedef uint16_t USB2422_SERSTR_Type; /* -------- USB2422_BCEN : (USB2422L Offset: 0xD0) (R/W 8) Battery Charging Enable -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_BCE :1; - uint8_t PORT2_BCE :1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 1; + uint8_t PORT1_BCE : 1; + uint8_t PORT2_BCE : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_BCEN_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_BOOSTUP : (USB2422L Offset: 0xF6) (R/W 8) Boost Upstream -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t BOOST :2; - uint8_t : 6; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t BOOST : 2; + uint8_t : 6; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_BOOSTUP_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_BOOSTDOWN : (USB2422L Offset: 0xF8) (R/W 8) Boost Downstream -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t BOOST1 :2; - uint8_t BOOST2 :2; - uint8_t : 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t BOOST1 : 2; + uint8_t BOOST2 : 2; + uint8_t : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_BOOSTDOWN_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PRTSP : (USB2422L Offset: 0xFA) (R/W 8) Port Swap -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t : 1; - uint8_t PORT1_SP :1; - uint8_t PORT2_SP :1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t : 1; + uint8_t PORT1_SP : 1; + uint8_t PORT2_SP : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PRTSP_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /* -------- USB2422_PRTR12 : (USB2422L Offset: 0xFB) (R/W 8) Port 1/2 Remap -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t PORT1_REMAP: 4; - uint8_t PORT2_REMAP: 4; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t PORT1_REMAP : 4; + uint8_t PORT2_REMAP : 4; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_PRTR12_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ #define USB2422_PRTR12_DISABLE 0 @@ -324,81 +323,80 @@ typedef union { /* -------- USB2422_STCD : (USB2422L Offset: 0xFF) (R/W 8) Status Command -------- */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef union { - struct { - uint8_t USB_ATTACH: 1; - uint8_t RESET: 1; - uint8_t INTF_PWRDN: 1; - uint8_t : 5; - } bit; /*!< Structure used for bit access */ - uint8_t reg; /*!< Type used for register access */ + struct { + uint8_t USB_ATTACH : 1; + uint8_t RESET : 1; + uint8_t INTF_PWRDN : 1; + uint8_t : 5; + } bit; /*!< Structure used for bit access */ + uint8_t reg; /*!< Type used for register access */ } USB2422_STCD_Type; #endif /* !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) */ /** \brief USB2422 device hardware registers */ #if !(defined(__ASSEMBLY__) || defined(__IAR_SYSTEMS_ASM__)) typedef struct { - USB2422_VID_Type VID; /**< \brief Offset: 0x00*/ - USB2422_PID_Type PID; /**< \brief Offset: 0x02*/ - USB2422_DID_Type DID; /**< \brief Offset: 0x04*/ - USB2422_CFG1_Type CFG1; /**< \brief Offset: 0x06*/ - USB2422_CFG2_Type CFG2; /**< \brief Offset: 0x07*/ - USB2422_CFG3_Type CFG3; /**< \brief Offset: 0x08*/ - USB2422_NRD_Type NRD; /**< \brief Offset: 0x09*/ - USB2422_PDS_Type PDS; /**< \brief Offset: 0x0A*/ - USB2422_PDB_Type PDB; /**< \brief Offset: 0x0B*/ - USB2422_MAXPS_Type MAXPS; /**< \brief Offset: 0x0C*/ - USB2422_MAXPB_Type MAXPB; /**< \brief Offset: 0x0D*/ - USB2422_HCMCS_Type HCMCS; /**< \brief Offset: 0x0E*/ - USB2422_HCMCB_Type HCMCB; /**< \brief Offset: 0x0F*/ - USB2422_PWRT_Type PWRT; /**< \brief Offset: 0x10*/ - USB2422_LANGID_LSB_Type LANGID_LSB; /**< \brief Offset: 0x11*/ - USB2422_LANGID_MSB_Type LANGID_MSB; /**< \brief Offset: 0x12*/ - USB2422_MFRSL_Type MFRSL; /**< \brief Offset: 0x13*/ - USB2422_PRDSL_Type PRDSL; /**< \brief Offset: 0x14*/ - USB2422_SERSL_Type SERSL; /**< \brief Offset: 0x15*/ - USB2422_MFRSTR_Type MFRSTR[31]; /**< \brief Offset: 0x16*/ - USB2422_PRDSTR_Type PRDSTR[31]; /**< \brief Offset: 0x54*/ - USB2422_SERSTR_Type SERSTR[31]; /**< \brief Offset: 0x92*/ - USB2422_BCEN_Type BCEN; /**< \brief Offset: 0xD0*/ - uint8_t Reserved1[0x25]; - USB2422_BOOSTUP_Type BOOSTUP; /**< \brief Offset: 0xF6*/ - uint8_t Reserved2[0x1]; - USB2422_BOOSTDOWN_Type BOOSTDOWN; /**< \brief Offset: 0xF8*/ - uint8_t Reserved3[0x1]; - USB2422_PRTSP_Type PRTSP; /**< \brief Offset: 0xFA*/ - USB2422_PRTR12_Type PRTR12; /**< \brief Offset: 0xFB*/ - uint8_t Reserved4[0x3]; - USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ + USB2422_VID_Type VID; /**< \brief Offset: 0x00*/ + USB2422_PID_Type PID; /**< \brief Offset: 0x02*/ + USB2422_DID_Type DID; /**< \brief Offset: 0x04*/ + USB2422_CFG1_Type CFG1; /**< \brief Offset: 0x06*/ + USB2422_CFG2_Type CFG2; /**< \brief Offset: 0x07*/ + USB2422_CFG3_Type CFG3; /**< \brief Offset: 0x08*/ + USB2422_NRD_Type NRD; /**< \brief Offset: 0x09*/ + USB2422_PDS_Type PDS; /**< \brief Offset: 0x0A*/ + USB2422_PDB_Type PDB; /**< \brief Offset: 0x0B*/ + USB2422_MAXPS_Type MAXPS; /**< \brief Offset: 0x0C*/ + USB2422_MAXPB_Type MAXPB; /**< \brief Offset: 0x0D*/ + USB2422_HCMCS_Type HCMCS; /**< \brief Offset: 0x0E*/ + USB2422_HCMCB_Type HCMCB; /**< \brief Offset: 0x0F*/ + USB2422_PWRT_Type PWRT; /**< \brief Offset: 0x10*/ + USB2422_LANGID_LSB_Type LANGID_LSB; /**< \brief Offset: 0x11*/ + USB2422_LANGID_MSB_Type LANGID_MSB; /**< \brief Offset: 0x12*/ + USB2422_MFRSL_Type MFRSL; /**< \brief Offset: 0x13*/ + USB2422_PRDSL_Type PRDSL; /**< \brief Offset: 0x14*/ + USB2422_SERSL_Type SERSL; /**< \brief Offset: 0x15*/ + USB2422_MFRSTR_Type MFRSTR[31]; /**< \brief Offset: 0x16*/ + USB2422_PRDSTR_Type PRDSTR[31]; /**< \brief Offset: 0x54*/ + USB2422_SERSTR_Type SERSTR[31]; /**< \brief Offset: 0x92*/ + USB2422_BCEN_Type BCEN; /**< \brief Offset: 0xD0*/ + uint8_t Reserved1[0x25]; + USB2422_BOOSTUP_Type BOOSTUP; /**< \brief Offset: 0xF6*/ + uint8_t Reserved2[0x1]; + USB2422_BOOSTDOWN_Type BOOSTDOWN; /**< \brief Offset: 0xF8*/ + uint8_t Reserved3[0x1]; + USB2422_PRTSP_Type PRTSP; /**< \brief Offset: 0xFA*/ + USB2422_PRTR12_Type PRTR12; /**< \brief Offset: 0xFB*/ + uint8_t Reserved4[0x3]; + USB2422_STCD_Type STCD; /**< \brief Offset: 0xFF*/ } Usb2422; #endif -#define PORT_DETECT_RETRY_INTERVAL 2000 +#define PORT_DETECT_RETRY_INTERVAL 2000 -#define USB_EXTRA_ADC_THRESHOLD 900 +#define USB_EXTRA_ADC_THRESHOLD 900 -#define USB_EXTRA_STATE_DISABLED 0 -#define USB_EXTRA_STATE_ENABLED 1 -#define USB_EXTRA_STATE_UNKNOWN 2 -#define USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG 3 +#define USB_EXTRA_STATE_DISABLED 0 +#define USB_EXTRA_STATE_ENABLED 1 +#define USB_EXTRA_STATE_UNKNOWN 2 +#define USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG 3 -#define USB_HOST_PORT_1 0 -#define USB_HOST_PORT_2 1 -#define USB_HOST_PORT_UNKNOWN 2 +#define USB_HOST_PORT_1 0 +#define USB_HOST_PORT_2 1 +#define USB_HOST_PORT_UNKNOWN 2 extern uint8_t usb_host_port; extern uint8_t usb_extra_state; extern uint8_t usb_extra_manual; extern uint8_t usb_gcr_auto; -void USB2422_init(void); -void USB_reset(void); -void USB_configure(void); +void USB2422_init(void); +void USB_reset(void); +void USB_configure(void); uint16_t USB_active(void); -void USB_set_host_by_voltage(void); +void USB_set_host_by_voltage(void); uint16_t adc_get(uint8_t muxpos); -uint8_t USB2422_Port_Detect_Init(void); -void USB_HandleExtraDevice(void); -void USB_ExtraSetState(uint8_t state); - -#endif //_USB2422_H_ +uint8_t USB2422_Port_Detect_Init(void); +void USB_HandleExtraDevice(void); +void USB_ExtraSetState(uint8_t state); +#endif //_USB2422_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb_atmel.h b/tmk_core/protocol/arm_atsam/usb/usb_atmel.h index 7febdc9ec..1cef70378 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_atmel.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_atmel.h @@ -67,118 +67,118 @@ */ //! \name Vendor Identifier assigned by USB org to ATMEL -#define USB_VID_ATMEL 0x03EB +#define USB_VID_ATMEL 0x03EB //! \name Product Identifier assigned by ATMEL to AVR applications //! @{ //! \name The range from 2000h to 20FFh is reserved to the old PID for C51, MEGA, and others. //! @{ -#define USB_PID_ATMEL_MEGA_HIDGENERIC 0x2013 -#define USB_PID_ATMEL_MEGA_HIDKEYBOARD 0x2017 -#define USB_PID_ATMEL_MEGA_CDC 0x2018 -#define USB_PID_ATMEL_MEGA_AUDIO_IN 0x2019 -#define USB_PID_ATMEL_MEGA_MS 0x201A -#define USB_PID_ATMEL_MEGA_AUDIO_IN_OUT 0x201B -#define USB_PID_ATMEL_MEGA_HIDMOUSE 0x201C -#define USB_PID_ATMEL_MEGA_HIDMOUSE_CERTIF_U4 0x201D -#define USB_PID_ATMEL_MEGA_CDC_MULTI 0x201E -#define USB_PID_ATMEL_MEGA_MS_HIDMS_HID_USBKEY 0x2022 -#define USB_PID_ATMEL_MEGA_MS_HIDMS_HID_STK525 0x2023 -#define USB_PID_ATMEL_MEGA_MS_2 0x2029 -#define USB_PID_ATMEL_MEGA_MS_HIDMS 0x202A -#define USB_PID_ATMEL_MEGA_MS_3 0x2032 -#define USB_PID_ATMEL_MEGA_LIBUSB 0x2050 +#define USB_PID_ATMEL_MEGA_HIDGENERIC 0x2013 +#define USB_PID_ATMEL_MEGA_HIDKEYBOARD 0x2017 +#define USB_PID_ATMEL_MEGA_CDC 0x2018 +#define USB_PID_ATMEL_MEGA_AUDIO_IN 0x2019 +#define USB_PID_ATMEL_MEGA_MS 0x201A +#define USB_PID_ATMEL_MEGA_AUDIO_IN_OUT 0x201B +#define USB_PID_ATMEL_MEGA_HIDMOUSE 0x201C +#define USB_PID_ATMEL_MEGA_HIDMOUSE_CERTIF_U4 0x201D +#define USB_PID_ATMEL_MEGA_CDC_MULTI 0x201E +#define USB_PID_ATMEL_MEGA_MS_HIDMS_HID_USBKEY 0x2022 +#define USB_PID_ATMEL_MEGA_MS_HIDMS_HID_STK525 0x2023 +#define USB_PID_ATMEL_MEGA_MS_2 0x2029 +#define USB_PID_ATMEL_MEGA_MS_HIDMS 0x202A +#define USB_PID_ATMEL_MEGA_MS_3 0x2032 +#define USB_PID_ATMEL_MEGA_LIBUSB 0x2050 //! @} //! \name The range 2100h to 21FFh is reserved to PIDs for AVR Tools. //! @{ -#define USB_PID_ATMEL_XPLAINED 0x2122 -#define USB_PID_ATMEL_XMEGA_USB_ZIGBIT_2_4GHZ 0x214A -#define USB_PID_ATMEL_XMEGA_USB_ZIGBIT_SUBGHZ 0x214B +#define USB_PID_ATMEL_XPLAINED 0x2122 +#define USB_PID_ATMEL_XMEGA_USB_ZIGBIT_2_4GHZ 0x214A +#define USB_PID_ATMEL_XMEGA_USB_ZIGBIT_SUBGHZ 0x214B //! @} //! \name The range 2300h to 23FFh is reserved to PIDs for demo from ASF1.7=> //! @{ -#define USB_PID_ATMEL_UC3_ENUM 0x2300 -#define USB_PID_ATMEL_UC3_MS 0x2301 -#define USB_PID_ATMEL_UC3_MS_SDRAM_LOADER 0x2302 -#define USB_PID_ATMEL_UC3_EVK1100_CTRLPANEL 0x2303 -#define USB_PID_ATMEL_UC3_HID 0x2304 -#define USB_PID_ATMEL_UC3_EVK1101_CTRLPANEL_HID 0x2305 -#define USB_PID_ATMEL_UC3_EVK1101_CTRLPANEL_HID_MS 0x2306 -#define USB_PID_ATMEL_UC3_CDC 0x2307 -#define USB_PID_ATMEL_UC3_AUDIO_MICRO 0x2308 -#define USB_PID_ATMEL_UC3_CDC_DEBUG 0x2310 // Virtual Com (debug interface) on EVK11xx -#define USB_PID_ATMEL_UC3_AUDIO_SPEAKER_MICRO 0x2311 -#define USB_PID_ATMEL_UC3_CDC_MSC 0x2312 +#define USB_PID_ATMEL_UC3_ENUM 0x2300 +#define USB_PID_ATMEL_UC3_MS 0x2301 +#define USB_PID_ATMEL_UC3_MS_SDRAM_LOADER 0x2302 +#define USB_PID_ATMEL_UC3_EVK1100_CTRLPANEL 0x2303 +#define USB_PID_ATMEL_UC3_HID 0x2304 +#define USB_PID_ATMEL_UC3_EVK1101_CTRLPANEL_HID 0x2305 +#define USB_PID_ATMEL_UC3_EVK1101_CTRLPANEL_HID_MS 0x2306 +#define USB_PID_ATMEL_UC3_CDC 0x2307 +#define USB_PID_ATMEL_UC3_AUDIO_MICRO 0x2308 +#define USB_PID_ATMEL_UC3_CDC_DEBUG 0x2310 // Virtual Com (debug interface) on EVK11xx +#define USB_PID_ATMEL_UC3_AUDIO_SPEAKER_MICRO 0x2311 +#define USB_PID_ATMEL_UC3_CDC_MSC 0x2312 //! @} //! \name The range 2400h to 24FFh is reserved to PIDs for ASF applications //! @{ -#define USB_PID_ATMEL_ASF_HIDMOUSE 0x2400 -#define USB_PID_ATMEL_ASF_HIDKEYBOARD 0x2401 -#define USB_PID_ATMEL_ASF_HIDGENERIC 0x2402 -#define USB_PID_ATMEL_ASF_MSC 0x2403 -#define USB_PID_ATMEL_ASF_CDC 0x2404 -#define USB_PID_ATMEL_ASF_PHDC 0x2405 -#define USB_PID_ATMEL_ASF_HIDMTOUCH 0x2406 -#define USB_PID_ATMEL_ASF_MSC_HIDMOUSE 0x2420 -#define USB_PID_ATMEL_ASF_MSC_HIDS_CDC 0x2421 -#define USB_PID_ATMEL_ASF_MSC_HIDKEYBOARD 0x2422 -#define USB_PID_ATMEL_ASF_VENDOR_CLASS 0x2423 -#define USB_PID_ATMEL_ASF_MSC_CDC 0x2424 -#define USB_PID_ATMEL_ASF_TWO_CDC 0x2425 -#define USB_PID_ATMEL_ASF_SEVEN_CDC 0x2426 -#define USB_PID_ATMEL_ASF_XPLAIN_BC_POWERONLY 0x2430 -#define USB_PID_ATMEL_ASF_XPLAIN_BC_TERMINAL 0x2431 -#define USB_PID_ATMEL_ASF_XPLAIN_BC_TOUCH 0x2432 -#define USB_PID_ATMEL_ASF_AUDIO_SPEAKER 0x2433 -#define USB_PID_ATMEL_ASF_XMEGA_B1_XPLAINED 0x2434 +#define USB_PID_ATMEL_ASF_HIDMOUSE 0x2400 +#define USB_PID_ATMEL_ASF_HIDKEYBOARD 0x2401 +#define USB_PID_ATMEL_ASF_HIDGENERIC 0x2402 +#define USB_PID_ATMEL_ASF_MSC 0x2403 +#define USB_PID_ATMEL_ASF_CDC 0x2404 +#define USB_PID_ATMEL_ASF_PHDC 0x2405 +#define USB_PID_ATMEL_ASF_HIDMTOUCH 0x2406 +#define USB_PID_ATMEL_ASF_MSC_HIDMOUSE 0x2420 +#define USB_PID_ATMEL_ASF_MSC_HIDS_CDC 0x2421 +#define USB_PID_ATMEL_ASF_MSC_HIDKEYBOARD 0x2422 +#define USB_PID_ATMEL_ASF_VENDOR_CLASS 0x2423 +#define USB_PID_ATMEL_ASF_MSC_CDC 0x2424 +#define USB_PID_ATMEL_ASF_TWO_CDC 0x2425 +#define USB_PID_ATMEL_ASF_SEVEN_CDC 0x2426 +#define USB_PID_ATMEL_ASF_XPLAIN_BC_POWERONLY 0x2430 +#define USB_PID_ATMEL_ASF_XPLAIN_BC_TERMINAL 0x2431 +#define USB_PID_ATMEL_ASF_XPLAIN_BC_TOUCH 0x2432 +#define USB_PID_ATMEL_ASF_AUDIO_SPEAKER 0x2433 +#define USB_PID_ATMEL_ASF_XMEGA_B1_XPLAINED 0x2434 //! @} //! \name The range 2F00h to 2FFFh is reserved to official PIDs for AVR bootloaders //! Note, !!!! don't use this range for demos or examples !!!! //! @{ -#define USB_PID_ATMEL_DFU_ATXMEGA64C3 0x2FD6 -#define USB_PID_ATMEL_DFU_ATXMEGA128C3 0x2FD7 -#define USB_PID_ATMEL_DFU_ATXMEGA16C4 0x2FD8 -#define USB_PID_ATMEL_DFU_ATXMEGA32C4 0x2FD9 -#define USB_PID_ATMEL_DFU_ATXMEGA256C3 0x2FDA -#define USB_PID_ATMEL_DFU_ATXMEGA384C3 0x2FDB -#define USB_PID_ATMEL_DFU_ATUCL3_L4 0x2FDC -#define USB_PID_ATMEL_DFU_ATXMEGA64A4U 0x2FDD -#define USB_PID_ATMEL_DFU_ATXMEGA128A4U 0x2FDE - -#define USB_PID_ATMEL_DFU_ATXMEGA64B3 0x2FDF -#define USB_PID_ATMEL_DFU_ATXMEGA128B3 0x2FE0 -#define USB_PID_ATMEL_DFU_ATXMEGA64B1 0x2FE1 -#define USB_PID_ATMEL_DFU_ATXMEGA256A3BU 0x2FE2 -#define USB_PID_ATMEL_DFU_ATXMEGA16A4U 0x2FE3 -#define USB_PID_ATMEL_DFU_ATXMEGA32A4U 0x2FE4 -#define USB_PID_ATMEL_DFU_ATXMEGA64A3U 0x2FE5 -#define USB_PID_ATMEL_DFU_ATXMEGA128A3U 0x2FE6 -#define USB_PID_ATMEL_DFU_ATXMEGA192A3U 0x2FE7 -#define USB_PID_ATMEL_DFU_ATXMEGA64A1U 0x2FE8 -#define USB_PID_ATMEL_DFU_ATUC3D 0x2FE9 -#define USB_PID_ATMEL_DFU_ATXMEGA128B1 0x2FEA -#define USB_PID_ATMEL_DFU_AT32UC3C 0x2FEB -#define USB_PID_ATMEL_DFU_ATXMEGA256A3U 0x2FEC -#define USB_PID_ATMEL_DFU_ATXMEGA128A1U 0x2FED -#define USB_PID_ATMEL_DFU_ATMEGA8U2 0x2FEE -#define USB_PID_ATMEL_DFU_ATMEGA16U2 0x2FEF -#define USB_PID_ATMEL_DFU_ATMEGA32U2 0x2FF0 -#define USB_PID_ATMEL_DFU_AT32UC3A3 0x2FF1 -#define USB_PID_ATMEL_DFU_ATMEGA32U6 0x2FF2 -#define USB_PID_ATMEL_DFU_ATMEGA16U4 0x2FF3 -#define USB_PID_ATMEL_DFU_ATMEGA32U4 0x2FF4 -#define USB_PID_ATMEL_DFU_AT32AP7200 0x2FF5 -#define USB_PID_ATMEL_DFU_AT32UC3B 0x2FF6 -#define USB_PID_ATMEL_DFU_AT90USB82 0x2FF7 -#define USB_PID_ATMEL_DFU_AT32UC3A 0x2FF8 -#define USB_PID_ATMEL_DFU_AT90USB64 0x2FF9 -#define USB_PID_ATMEL_DFU_AT90USB162 0x2FFA -#define USB_PID_ATMEL_DFU_AT90USB128 0x2FFB +#define USB_PID_ATMEL_DFU_ATXMEGA64C3 0x2FD6 +#define USB_PID_ATMEL_DFU_ATXMEGA128C3 0x2FD7 +#define USB_PID_ATMEL_DFU_ATXMEGA16C4 0x2FD8 +#define USB_PID_ATMEL_DFU_ATXMEGA32C4 0x2FD9 +#define USB_PID_ATMEL_DFU_ATXMEGA256C3 0x2FDA +#define USB_PID_ATMEL_DFU_ATXMEGA384C3 0x2FDB +#define USB_PID_ATMEL_DFU_ATUCL3_L4 0x2FDC +#define USB_PID_ATMEL_DFU_ATXMEGA64A4U 0x2FDD +#define USB_PID_ATMEL_DFU_ATXMEGA128A4U 0x2FDE + +#define USB_PID_ATMEL_DFU_ATXMEGA64B3 0x2FDF +#define USB_PID_ATMEL_DFU_ATXMEGA128B3 0x2FE0 +#define USB_PID_ATMEL_DFU_ATXMEGA64B1 0x2FE1 +#define USB_PID_ATMEL_DFU_ATXMEGA256A3BU 0x2FE2 +#define USB_PID_ATMEL_DFU_ATXMEGA16A4U 0x2FE3 +#define USB_PID_ATMEL_DFU_ATXMEGA32A4U 0x2FE4 +#define USB_PID_ATMEL_DFU_ATXMEGA64A3U 0x2FE5 +#define USB_PID_ATMEL_DFU_ATXMEGA128A3U 0x2FE6 +#define USB_PID_ATMEL_DFU_ATXMEGA192A3U 0x2FE7 +#define USB_PID_ATMEL_DFU_ATXMEGA64A1U 0x2FE8 +#define USB_PID_ATMEL_DFU_ATUC3D 0x2FE9 +#define USB_PID_ATMEL_DFU_ATXMEGA128B1 0x2FEA +#define USB_PID_ATMEL_DFU_AT32UC3C 0x2FEB +#define USB_PID_ATMEL_DFU_ATXMEGA256A3U 0x2FEC +#define USB_PID_ATMEL_DFU_ATXMEGA128A1U 0x2FED +#define USB_PID_ATMEL_DFU_ATMEGA8U2 0x2FEE +#define USB_PID_ATMEL_DFU_ATMEGA16U2 0x2FEF +#define USB_PID_ATMEL_DFU_ATMEGA32U2 0x2FF0 +#define USB_PID_ATMEL_DFU_AT32UC3A3 0x2FF1 +#define USB_PID_ATMEL_DFU_ATMEGA32U6 0x2FF2 +#define USB_PID_ATMEL_DFU_ATMEGA16U4 0x2FF3 +#define USB_PID_ATMEL_DFU_ATMEGA32U4 0x2FF4 +#define USB_PID_ATMEL_DFU_AT32AP7200 0x2FF5 +#define USB_PID_ATMEL_DFU_AT32UC3B 0x2FF6 +#define USB_PID_ATMEL_DFU_AT90USB82 0x2FF7 +#define USB_PID_ATMEL_DFU_AT32UC3A 0x2FF8 +#define USB_PID_ATMEL_DFU_AT90USB64 0x2FF9 +#define USB_PID_ATMEL_DFU_AT90USB162 0x2FFA +#define USB_PID_ATMEL_DFU_AT90USB128 0x2FFB // 2FFCh to 2FFFh used by C51 family products //! @} @@ -186,5 +186,4 @@ //! @} - -#endif // _USB_ATMEL_H_ +#endif // _USB_ATMEL_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb_device_udd.c b/tmk_core/protocol/arm_atsam/usb/usb_device_udd.c index b31256df7..f138b4278 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_device_udd.c +++ b/tmk_core/protocol/arm_atsam/usb/usb_device_udd.c @@ -61,30 +61,30 @@ */ // Check USB device configuration #ifdef USB_DEVICE_HS_SUPPORT -# error The High speed mode is not supported on this part, please remove USB_DEVICE_HS_SUPPORT in conf_usb.h +# error The High speed mode is not supported on this part, please remove USB_DEVICE_HS_SUPPORT in conf_usb.h #endif -//Note: This driver is adapted for SAMD51 +// Note: This driver is adapted for SAMD51 #ifndef UDC_REMOTEWAKEUP_LPM_ENABLE -#define UDC_REMOTEWAKEUP_LPM_ENABLE() +# define UDC_REMOTEWAKEUP_LPM_ENABLE() #endif #ifndef UDC_REMOTEWAKEUP_LPM_DISABLE -#define UDC_REMOTEWAKEUP_LPM_DISABLE() +# define UDC_REMOTEWAKEUP_LPM_DISABLE() #endif #ifndef UDC_SUSPEND_LPM_EVENT -#define UDC_SUSPEND_LPM_EVENT() +# define UDC_SUSPEND_LPM_EVENT() #endif /* for debug text */ #ifdef USB_DEBUG -# define dbg_print printf +# define dbg_print printf #else -# define dbg_print(...) +# define dbg_print(...) #endif /** Maximum size of a transfer in multi-packet mode */ -#define UDD_ENDPOINT_MAX_TRANS ((8*1024)-1) +#define UDD_ENDPOINT_MAX_TRANS ((8 * 1024) - 1) /** USB software device instance structure */ struct usb_module usb_device; @@ -97,10 +97,7 @@ struct usb_module usb_device; #define UDD_CLOCK_GEN 0 -static inline void udd_wait_clock_ready(void) -{ - -} +static inline void udd_wait_clock_ready(void) {} /** * \name Power management @@ -130,12 +127,12 @@ uint8_t udd_ctrl_buffer[USB_DEVICE_EP_CTRL_SIZE]; /** Bit definitions about endpoint control state machine for udd_ep_control_state */ typedef enum { - UDD_EPCTRL_SETUP = 0, //!< Wait a SETUP packet - UDD_EPCTRL_DATA_OUT = 1, //!< Wait a OUT data packet - UDD_EPCTRL_DATA_IN = 2, //!< Wait a IN data packet - UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP = 3, //!< Wait a IN ZLP packet - UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP = 4, //!< Wait a OUT ZLP packet - UDD_EPCTRL_STALL_REQ = 5, //!< STALL enabled on IN & OUT packet + UDD_EPCTRL_SETUP = 0, //!< Wait a SETUP packet + UDD_EPCTRL_DATA_OUT = 1, //!< Wait a OUT data packet + UDD_EPCTRL_DATA_IN = 2, //!< Wait a IN data packet + UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP = 3, //!< Wait a IN ZLP packet + UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP = 4, //!< Wait a OUT ZLP packet + UDD_EPCTRL_STALL_REQ = 5, //!< STALL enabled on IN & OUT packet } udd_ctrl_ep_state_t; /** Global variable to give and record information of the set up request management */ @@ -200,11 +197,11 @@ typedef struct { //! Endpoint size uint16_t ep_size; //! A job is registered on this endpoint - uint8_t busy:1; + uint8_t busy : 1; //! A short packet is requested for this job on endpoint IN - uint8_t b_shortpacket:1; + uint8_t b_shortpacket : 1; //! The cache buffer is currently used on endpoint OUT - uint8_t b_use_out_cache_buffer:1; + uint8_t b_use_out_cache_buffer : 1; } udd_ep_job_t; /** Array to register a job on bulk/interrupt/isochronous endpoint */ @@ -217,8 +214,7 @@ static udd_ep_job_t udd_ep_job[2 * USB_DEVICE_MAX_EP]; * \param[in] ep Endpoint Address * \retval pointer to an udd_ep_job_t structure instance */ -static udd_ep_job_t* udd_ep_get_job(udd_ep_id_t ep) -{ +static udd_ep_job_t *udd_ep_get_job(udd_ep_id_t ep) { if ((ep == 0) || (ep == 0x80)) { return NULL; } else { @@ -230,17 +226,16 @@ static udd_ep_job_t* udd_ep_get_job(udd_ep_id_t ep) * \brief Endpoint IN process, continue to send packets or zero length packet * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ep_trans_in_next(void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; - udd_ep_id_t ep = ep_callback_para->endpoint_address; - uint16_t ep_size, nb_trans; - uint16_t next_trans; - udd_ep_id_t ep_num; - udd_ep_job_t *ptr_job; +static void udd_ep_trans_in_next(void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; + udd_ep_id_t ep = ep_callback_para->endpoint_address; + uint16_t ep_size, nb_trans; + uint16_t next_trans; + udd_ep_id_t ep_num; + udd_ep_job_t * ptr_job; ptr_job = udd_ep_get_job(ep); - ep_num = ep & USB_EP_ADDR_MASK; + ep_num = ep & USB_EP_ADDR_MASK; ep_size = ptr_job->ep_size; /* Update number of data transferred */ @@ -251,13 +246,13 @@ static void udd_ep_trans_in_next(void* pointer) if (ptr_job->nb_trans != ptr_job->buf_size) { next_trans = ptr_job->buf_size - ptr_job->nb_trans; if (UDD_ENDPOINT_MAX_TRANS < next_trans) { - /* The USB hardware support a maximum - * transfer size of UDD_ENDPOINT_MAX_TRANS Bytes */ - next_trans = UDD_ENDPOINT_MAX_TRANS -(UDD_ENDPOINT_MAX_TRANS % ep_size); + /* The USB hardware support a maximum + * transfer size of UDD_ENDPOINT_MAX_TRANS Bytes */ + next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ep_size); } /* Need ZLP, if requested and last packet is not a short packet */ ptr_job->b_shortpacket = ptr_job->b_shortpacket && (0 == (next_trans % ep_size)); - usb_device_endpoint_write_buffer_job(&usb_device,ep_num,&ptr_job->buf[ptr_job->nb_trans],next_trans); + usb_device_endpoint_write_buffer_job(&usb_device, ep_num, &ptr_job->buf[ptr_job->nb_trans], next_trans); return; } @@ -265,7 +260,7 @@ static void udd_ep_trans_in_next(void* pointer) if (ptr_job->b_shortpacket) { ptr_job->b_shortpacket = false; /* Start new transfer */ - usb_device_endpoint_write_buffer_job(&usb_device,ep_num,&ptr_job->buf[ptr_job->nb_trans],0); + usb_device_endpoint_write_buffer_job(&usb_device, ep_num, &ptr_job->buf[ptr_job->nb_trans], 0); return; } @@ -280,17 +275,16 @@ static void udd_ep_trans_in_next(void* pointer) * \brief Endpoint OUT process, continue to receive packets or zero length packet * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ep_trans_out_next(void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; - udd_ep_id_t ep = ep_callback_para->endpoint_address; - uint16_t ep_size, nb_trans; - uint16_t next_trans; - udd_ep_id_t ep_num; - udd_ep_job_t *ptr_job; +static void udd_ep_trans_out_next(void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; + udd_ep_id_t ep = ep_callback_para->endpoint_address; + uint16_t ep_size, nb_trans; + uint16_t next_trans; + udd_ep_id_t ep_num; + udd_ep_job_t * ptr_job; ptr_job = udd_ep_get_job(ep); - ep_num = ep & USB_EP_ADDR_MASK; + ep_num = ep & USB_EP_ADDR_MASK; ep_size = ptr_job->ep_size; /* Update number of data transferred */ @@ -312,9 +306,9 @@ static void udd_ep_trans_out_next(void* pointer) if ((nb_trans == ep_callback_para->out_buffer_size) && (ptr_job->nb_trans != ptr_job->buf_size)) { next_trans = ptr_job->buf_size - ptr_job->nb_trans; if (UDD_ENDPOINT_MAX_TRANS < next_trans) { - /* The USB hardware support a maximum transfer size - * of UDD_ENDPOINT_MAX_TRANS Bytes */ - next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ep_size); + /* The USB hardware support a maximum transfer size + * of UDD_ENDPOINT_MAX_TRANS Bytes */ + next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ep_size); } else { next_trans -= next_trans % ep_size; } @@ -322,9 +316,9 @@ static void udd_ep_trans_out_next(void* pointer) if (next_trans < ep_size) { /* Use the cache buffer for Bulk or Interrupt size endpoint */ ptr_job->b_use_out_cache_buffer = true; - usb_device_endpoint_read_buffer_job(&usb_device,ep_num,udd_ep_out_cache_buffer[ep_num - 1],ep_size); + usb_device_endpoint_read_buffer_job(&usb_device, ep_num, udd_ep_out_cache_buffer[ep_num - 1], ep_size); } else { - usb_device_endpoint_read_buffer_job(&usb_device,ep_num,&ptr_job->buf[ptr_job->nb_trans],next_trans); + usb_device_endpoint_read_buffer_job(&usb_device, ep_num, &ptr_job->buf[ptr_job->nb_trans], next_trans); } return; } @@ -341,10 +335,9 @@ static void udd_ep_trans_out_next(void* pointer) * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ep_transfer_process(struct usb_module *module_inst, void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; - udd_ep_id_t ep = ep_callback_para->endpoint_address; +static void udd_ep_transfer_process(struct usb_module *module_inst, void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; + udd_ep_id_t ep = ep_callback_para->endpoint_address; if (ep & USB_EP_DIR_IN) { udd_ep_trans_in_next(pointer); @@ -353,8 +346,7 @@ static void udd_ep_transfer_process(struct usb_module *module_inst, void* pointe } } -void udd_ep_abort(udd_ep_id_t ep) -{ +void udd_ep_abort(udd_ep_id_t ep) { udd_ep_job_t *ptr_job; usb_device_endpoint_abort_job(&usb_device, ep); @@ -371,23 +363,13 @@ void udd_ep_abort(udd_ep_id_t ep) } } -bool udd_is_high_speed(void) -{ - return false; -} +bool udd_is_high_speed(void) { return false; } -uint16_t udd_get_frame_number(void) -{ - return usb_device_get_frame_number(&usb_device); -} +uint16_t udd_get_frame_number(void) { return usb_device_get_frame_number(&usb_device); } -uint16_t udd_get_micro_frame_number(void) -{ - return usb_device_get_micro_frame_number(&usb_device); -} +uint16_t udd_get_micro_frame_number(void) { return usb_device_get_micro_frame_number(&usb_device); } -void udd_ep_free(udd_ep_id_t ep) -{ +void udd_ep_free(udd_ep_id_t ep) { struct usb_device_endpoint_config config_ep; usb_device_endpoint_get_config_defaults(&config_ep); @@ -395,45 +377,44 @@ void udd_ep_free(udd_ep_id_t ep) udd_ep_abort(ep); config_ep.ep_address = ep; - config_ep.ep_type = USB_DEVICE_ENDPOINT_TYPE_DISABLE; + config_ep.ep_type = USB_DEVICE_ENDPOINT_TYPE_DISABLE; usb_device_endpoint_set_config(&usb_device, &config_ep); - usb_device_endpoint_unregister_callback(&usb_device,ep_num,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); - usb_device_endpoint_disable_callback(&usb_device,ep,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); + usb_device_endpoint_unregister_callback(&usb_device, ep_num, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); + usb_device_endpoint_disable_callback(&usb_device, ep, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); } -bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize) -{ +bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize) { struct usb_device_endpoint_config config_ep; usb_device_endpoint_get_config_defaults(&config_ep); config_ep.ep_address = ep; - if(MaxEndpointSize <= 8) { + if (MaxEndpointSize <= 8) { config_ep.ep_size = USB_ENDPOINT_8_BYTE; - } else if(MaxEndpointSize <= 16) { + } else if (MaxEndpointSize <= 16) { config_ep.ep_size = USB_ENDPOINT_16_BYTE; - } else if(MaxEndpointSize <= 32) { + } else if (MaxEndpointSize <= 32) { config_ep.ep_size = USB_ENDPOINT_32_BYTE; - } else if(MaxEndpointSize <= 64) { + } else if (MaxEndpointSize <= 64) { config_ep.ep_size = USB_ENDPOINT_64_BYTE; - } else if(MaxEndpointSize <= 128) { + } else if (MaxEndpointSize <= 128) { config_ep.ep_size = USB_ENDPOINT_128_BYTE; - } else if(MaxEndpointSize <= 256) { + } else if (MaxEndpointSize <= 256) { config_ep.ep_size = USB_ENDPOINT_256_BYTE; - } else if(MaxEndpointSize <= 512) { + } else if (MaxEndpointSize <= 512) { config_ep.ep_size = USB_ENDPOINT_512_BYTE; - } else if(MaxEndpointSize <= 1023) { + } else if (MaxEndpointSize <= 1023) { config_ep.ep_size = USB_ENDPOINT_1023_BYTE; } else { return false; } udd_ep_job_t *ptr_job = udd_ep_get_job(ep); - ptr_job->ep_size = MaxEndpointSize; + ptr_job->ep_size = MaxEndpointSize; bmAttributes = bmAttributes & USB_EP_TYPE_MASK; /* Check endpoint type */ - if(USB_EP_TYPE_ISOCHRONOUS == bmAttributes) { + if (USB_EP_TYPE_ISOCHRONOUS == bmAttributes) { config_ep.ep_type = USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS; } else if (USB_EP_TYPE_BULK == bmAttributes) { config_ep.ep_type = USB_DEVICE_ENDPOINT_TYPE_BULK; @@ -448,20 +429,16 @@ bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes, uint16_t MaxEndpointSize if (STATUS_OK != usb_device_endpoint_set_config(&usb_device, &config_ep)) { return false; } - usb_device_endpoint_register_callback(&usb_device,ep_num,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT,udd_ep_transfer_process); - usb_device_endpoint_enable_callback(&usb_device,ep,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); - usb_device_endpoint_enable_callback(&usb_device,ep,USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL); + usb_device_endpoint_register_callback(&usb_device, ep_num, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT, udd_ep_transfer_process); + usb_device_endpoint_enable_callback(&usb_device, ep, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); + usb_device_endpoint_enable_callback(&usb_device, ep, USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL); return true; } -bool udd_ep_is_halted(udd_ep_id_t ep) -{ - return usb_device_endpoint_is_halted(&usb_device, ep); -} +bool udd_ep_is_halted(udd_ep_id_t ep) { return usb_device_endpoint_is_halted(&usb_device, ep); } -bool udd_ep_set_halt(udd_ep_id_t ep) -{ +bool udd_ep_set_halt(udd_ep_id_t ep) { uint8_t ep_num = ep & USB_EP_ADDR_MASK; if (USB_DEVICE_MAX_EP < ep_num) { @@ -474,10 +451,9 @@ bool udd_ep_set_halt(udd_ep_id_t ep) return true; } -bool udd_ep_clear_halt(udd_ep_id_t ep) -{ +bool udd_ep_clear_halt(udd_ep_id_t ep) { udd_ep_job_t *ptr_job; - uint8_t ep_num = ep & USB_EP_ADDR_MASK; + uint8_t ep_num = ep & USB_EP_ADDR_MASK; if (USB_DEVICE_MAX_EP < ep_num) { return false; @@ -495,9 +471,8 @@ bool udd_ep_clear_halt(udd_ep_id_t ep) return true; } -bool udd_ep_wait_stall_clear(udd_ep_id_t ep, udd_callback_halt_cleared_t callback) -{ - udd_ep_id_t ep_num; +bool udd_ep_wait_stall_clear(udd_ep_id_t ep, udd_callback_halt_cleared_t callback) { + udd_ep_id_t ep_num; udd_ep_job_t *ptr_job; ep_num = ep & USB_EP_ADDR_MASK; @@ -513,7 +488,7 @@ bool udd_ep_wait_stall_clear(udd_ep_id_t ep, udd_callback_halt_cleared_t callbac /* Wait clear halt endpoint */ if (usb_device_endpoint_is_halted(&usb_device, ep)) { /* Endpoint halted then registers the callback */ - ptr_job->busy = true; + ptr_job->busy = true; ptr_job->call_nohalt = callback; return true; } else if (usb_device_endpoint_is_configured(&usb_device, ep)) { @@ -527,19 +502,17 @@ bool udd_ep_wait_stall_clear(udd_ep_id_t ep, udd_callback_halt_cleared_t callbac /** * \brief Control Endpoint stall sending data */ -static void udd_ctrl_stall_data(void) -{ +static void udd_ctrl_stall_data(void) { udd_ep_control_state = UDD_EPCTRL_STALL_REQ; usb_device_endpoint_set_halt(&usb_device, USB_EP_DIR_IN); usb_device_endpoint_clear_halt(&usb_device, USB_EP_DIR_OUT); } -bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t buf_size, udd_callback_trans_t callback) -{ - udd_ep_id_t ep_num; +bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t buf_size, udd_callback_trans_t callback) { + udd_ep_id_t ep_num; udd_ep_job_t *ptr_job; - uint32_t irqflags; + uint32_t irqflags; ep_num = ep & USB_EP_ADDR_MASK; @@ -564,11 +537,11 @@ bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t bu __set_PRIMASK(irqflags); /* No job running, set up a new one */ - ptr_job->buf = buf; - ptr_job->buf_size = buf_size; - ptr_job->nb_trans = 0; - ptr_job->call_trans = callback; - ptr_job->b_shortpacket = b_shortpacket; + ptr_job->buf = buf; + ptr_job->buf_size = buf_size; + ptr_job->nb_trans = 0; + ptr_job->call_trans = callback; + ptr_job->b_shortpacket = b_shortpacket; ptr_job->b_use_out_cache_buffer = false; /* Initialize value to simulate a empty transfer */ @@ -580,10 +553,10 @@ bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t bu if (UDD_ENDPOINT_MAX_TRANS < next_trans) { next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ptr_job->ep_size); } - ptr_job->b_shortpacket = ptr_job->b_shortpacket && (0 == (next_trans % ptr_job->ep_size)); + ptr_job->b_shortpacket = ptr_job->b_shortpacket && (0 == (next_trans % ptr_job->ep_size)); } else if (true == ptr_job->b_shortpacket) { ptr_job->b_shortpacket = false; /* avoid to send zero length packet again */ - next_trans = 0; + next_trans = 0; } else { ptr_job->busy = false; if (NULL != ptr_job->call_trans) { @@ -591,30 +564,22 @@ bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t bu } return true; } - return (STATUS_OK == - usb_device_endpoint_write_buffer_job(&usb_device, - ep_num,&ptr_job->buf[0],next_trans)); + return (STATUS_OK == usb_device_endpoint_write_buffer_job(&usb_device, ep_num, &ptr_job->buf[0], next_trans)); } else { if (0 != ptr_job->buf_size) { next_trans = ptr_job->buf_size; if (UDD_ENDPOINT_MAX_TRANS < next_trans) { /* The USB hardware support a maximum transfer size * of UDD_ENDPOINT_MAX_TRANS Bytes */ - next_trans = UDD_ENDPOINT_MAX_TRANS - - (UDD_ENDPOINT_MAX_TRANS % ptr_job->ep_size); + next_trans = UDD_ENDPOINT_MAX_TRANS - (UDD_ENDPOINT_MAX_TRANS % ptr_job->ep_size); } else { next_trans -= next_trans % ptr_job->ep_size; } if (next_trans < ptr_job->ep_size) { ptr_job->b_use_out_cache_buffer = true; - return (STATUS_OK == - usb_device_endpoint_read_buffer_job(&usb_device, ep_num, - udd_ep_out_cache_buffer[ep_num - 1], - ptr_job->ep_size)); + return (STATUS_OK == usb_device_endpoint_read_buffer_job(&usb_device, ep_num, udd_ep_out_cache_buffer[ep_num - 1], ptr_job->ep_size)); } else { - return (STATUS_OK == - usb_device_endpoint_read_buffer_job(&usb_device, ep_num, - &ptr_job->buf[0],next_trans)); + return (STATUS_OK == usb_device_endpoint_read_buffer_job(&usb_device, ep_num, &ptr_job->buf[0], next_trans)); } } else { ptr_job->busy = false; @@ -626,61 +591,51 @@ bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket, uint8_t *buf, iram_size_t bu } } -void udd_set_address(uint8_t address) -{ - usb_device_set_address(&usb_device,address); -} +void udd_set_address(uint8_t address) { usb_device_set_address(&usb_device, address); } -uint8_t udd_getaddress(void) -{ - return usb_device_get_address(&usb_device); -} +uint8_t udd_getaddress(void) { return usb_device_get_address(&usb_device); } -void udd_send_remotewakeup(void) -{ - uint32_t try = 5; +void udd_send_remotewakeup(void) { + uint32_t try + = 5; udd_wait_clock_ready(); udd_sleep_mode(UDD_STATE_IDLE); - while(2 != usb_get_state_machine_status(&usb_device) && try --) { + while (2 != usb_get_state_machine_status(&usb_device) && try --) { usb_device_send_remote_wake_up(&usb_device); } } -void udd_set_setup_payload( uint8_t *payload, uint16_t payload_size ) -{ - udd_g_ctrlreq.payload = payload; +void udd_set_setup_payload(uint8_t *payload, uint16_t payload_size) { + udd_g_ctrlreq.payload = payload; udd_g_ctrlreq.payload_size = payload_size; } /** * \brief Control Endpoint translate the data in buffer into Device Request Struct */ -static void udd_ctrl_fetch_ram(void) -{ +static void udd_ctrl_fetch_ram(void) { udd_g_ctrlreq.req.bmRequestType = udd_ctrl_buffer[0]; - udd_g_ctrlreq.req.bRequest = udd_ctrl_buffer[1]; - udd_g_ctrlreq.req.wValue = ((uint16_t)(udd_ctrl_buffer[3]) << 8) + udd_ctrl_buffer[2]; - udd_g_ctrlreq.req.wIndex = ((uint16_t)(udd_ctrl_buffer[5]) << 8) + udd_ctrl_buffer[4]; - udd_g_ctrlreq.req.wLength = ((uint16_t)(udd_ctrl_buffer[7]) << 8) + udd_ctrl_buffer[6]; + udd_g_ctrlreq.req.bRequest = udd_ctrl_buffer[1]; + udd_g_ctrlreq.req.wValue = ((uint16_t)(udd_ctrl_buffer[3]) << 8) + udd_ctrl_buffer[2]; + udd_g_ctrlreq.req.wIndex = ((uint16_t)(udd_ctrl_buffer[5]) << 8) + udd_ctrl_buffer[4]; + udd_g_ctrlreq.req.wLength = ((uint16_t)(udd_ctrl_buffer[7]) << 8) + udd_ctrl_buffer[6]; } /** * \brief Control Endpoint send out zero length packet */ -static void udd_ctrl_send_zlp_in(void) -{ +static void udd_ctrl_send_zlp_in(void) { udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP; - usb_device_endpoint_setup_buffer_job(&usb_device,udd_ctrl_buffer); - usb_device_endpoint_write_buffer_job(&usb_device,0,udd_g_ctrlreq.payload,0); + usb_device_endpoint_setup_buffer_job(&usb_device, udd_ctrl_buffer); + usb_device_endpoint_write_buffer_job(&usb_device, 0, udd_g_ctrlreq.payload, 0); } /** * \brief Process control endpoint IN transaction */ -static void udd_ctrl_in_sent(void) -{ +static void udd_ctrl_in_sent(void) { static bool b_shortpacket = false; - uint16_t nb_remain; + uint16_t nb_remain; nb_remain = udd_g_ctrlreq.payload_size - udd_ctrl_payload_nb_trans; @@ -691,7 +646,7 @@ static void udd_ctrl_in_sent(void) /* All data requested are transferred or a short packet has been sent, then it is the end of data phase. * Generate an OUT ZLP for handshake phase */ udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP; - usb_device_endpoint_setup_buffer_job(&usb_device,udd_ctrl_buffer); + usb_device_endpoint_setup_buffer_job(&usb_device, udd_ctrl_buffer); return; } /* Need of new buffer because the data phase is not complete */ @@ -701,20 +656,20 @@ static void udd_ctrl_in_sent(void) } else { /* A new payload buffer is given */ udd_ctrl_payload_nb_trans = 0; - nb_remain = udd_g_ctrlreq.payload_size; + nb_remain = udd_g_ctrlreq.payload_size; } } /* Continue transfer and send next data */ if (nb_remain >= USB_DEVICE_EP_CTRL_SIZE) { - nb_remain = USB_DEVICE_EP_CTRL_SIZE; + nb_remain = USB_DEVICE_EP_CTRL_SIZE; b_shortpacket = false; } else { b_shortpacket = true; } /* Link payload buffer directly on USB hardware */ - usb_device_endpoint_write_buffer_job(&usb_device,0,udd_g_ctrlreq.payload + udd_ctrl_payload_nb_trans,nb_remain); + usb_device_endpoint_write_buffer_job(&usb_device, 0, udd_g_ctrlreq.payload + udd_ctrl_payload_nb_trans, nb_remain); udd_ctrl_payload_nb_trans += nb_remain; } @@ -723,9 +678,8 @@ static void udd_ctrl_in_sent(void) * \brief Process control endpoint OUT transaction * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ctrl_out_received(void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; +static void udd_ctrl_out_received(void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; uint16_t nb_data; nb_data = ep_callback_para->received_bytes; /* Read data received during OUT phase */ @@ -735,11 +689,10 @@ static void udd_ctrl_out_received(void* pointer) nb_data = udd_g_ctrlreq.payload_size - udd_ctrl_payload_nb_trans; } - memcpy((uint8_t *) (udd_g_ctrlreq.payload + udd_ctrl_payload_nb_trans), udd_ctrl_buffer, nb_data); + memcpy((uint8_t *)(udd_g_ctrlreq.payload + udd_ctrl_payload_nb_trans), udd_ctrl_buffer, nb_data); udd_ctrl_payload_nb_trans += nb_data; - if ((USB_DEVICE_EP_CTRL_SIZE != nb_data) || \ - (udd_g_ctrlreq.req.wLength <= (udd_ctrl_prev_payload_nb_trans + udd_ctrl_payload_nb_trans))) { + if ((USB_DEVICE_EP_CTRL_SIZE != nb_data) || (udd_g_ctrlreq.req.wLength <= (udd_ctrl_prev_payload_nb_trans + udd_ctrl_payload_nb_trans))) { /* End of reception because it is a short packet * or all data are transferred */ @@ -786,7 +739,7 @@ static void udd_ctrl_out_received(void* pointer) /* Reinitialize reception on payload buffer */ udd_ctrl_payload_nb_trans = 0; } - usb_device_endpoint_read_buffer_job(&usb_device,0,udd_ctrl_buffer,USB_DEVICE_EP_CTRL_SIZE); + usb_device_endpoint_read_buffer_job(&usb_device, 0, udd_ctrl_buffer, USB_DEVICE_EP_CTRL_SIZE); } /** @@ -795,9 +748,8 @@ static void udd_ctrl_out_received(void* pointer) * \param[in] module_inst pointer to USB module instance * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void _usb_ep0_on_setup(struct usb_module *module_inst, void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; +static void _usb_ep0_on_setup(struct usb_module *module_inst, void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; if (UDD_EPCTRL_SETUP != udd_ep_control_state) { if (NULL != udd_g_ctrlreq.callback) { @@ -805,7 +757,7 @@ static void _usb_ep0_on_setup(struct usb_module *module_inst, void* pointer) } udd_ep_control_state = UDD_EPCTRL_SETUP; } - if ( 8 != ep_callback_para->received_bytes) { + if (8 != ep_callback_para->received_bytes) { udd_ctrl_stall_data(); return; } else { @@ -815,20 +767,20 @@ static void _usb_ep0_on_setup(struct usb_module *module_inst, void* pointer) return; } else if (Udd_setup_is_in()) { udd_ctrl_prev_payload_nb_trans = 0; - udd_ctrl_payload_nb_trans = 0; - udd_ep_control_state = UDD_EPCTRL_DATA_IN; - usb_device_endpoint_read_buffer_job(&usb_device,0,udd_ctrl_buffer,USB_DEVICE_EP_CTRL_SIZE); + udd_ctrl_payload_nb_trans = 0; + udd_ep_control_state = UDD_EPCTRL_DATA_IN; + usb_device_endpoint_read_buffer_job(&usb_device, 0, udd_ctrl_buffer, USB_DEVICE_EP_CTRL_SIZE); udd_ctrl_in_sent(); } else { - if(0 == udd_g_ctrlreq.req.wLength) { + if (0 == udd_g_ctrlreq.req.wLength) { udd_ctrl_send_zlp_in(); return; } else { udd_ctrl_prev_payload_nb_trans = 0; - udd_ctrl_payload_nb_trans = 0; - udd_ep_control_state = UDD_EPCTRL_DATA_OUT; + udd_ctrl_payload_nb_trans = 0; + udd_ep_control_state = UDD_EPCTRL_DATA_OUT; /* Initialize buffer size and enable OUT bank */ - usb_device_endpoint_read_buffer_job(&usb_device,0,udd_ctrl_buffer,USB_DEVICE_EP_CTRL_SIZE); + usb_device_endpoint_read_buffer_job(&usb_device, 0, udd_ctrl_buffer, USB_DEVICE_EP_CTRL_SIZE); } } } @@ -838,9 +790,8 @@ static void _usb_ep0_on_setup(struct usb_module *module_inst, void* pointer) * \brief Control Endpoint Process when underflow condition has occurred * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ctrl_underflow(void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; +static void udd_ctrl_underflow(void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; if (UDD_EPCTRL_DATA_OUT == udd_ep_control_state) { /* Host want to stop OUT transaction @@ -857,9 +808,8 @@ static void udd_ctrl_underflow(void* pointer) * \brief Control Endpoint Process when overflow condition has occurred * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void udd_ctrl_overflow(void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; +static void udd_ctrl_overflow(void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; if (UDD_EPCTRL_DATA_IN == udd_ep_control_state) { /* Host want to stop IN transaction @@ -878,11 +828,10 @@ static void udd_ctrl_overflow(void* pointer) * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void _usb_ep0_on_tansfer_fail(struct usb_module *module_inst, void* pointer) -{ - struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter*)pointer; +static void _usb_ep0_on_tansfer_fail(struct usb_module *module_inst, void *pointer) { + struct usb_endpoint_callback_parameter *ep_callback_para = (struct usb_endpoint_callback_parameter *)pointer; - if(ep_callback_para->endpoint_address & USB_EP_DIR_IN) { + if (ep_callback_para->endpoint_address & USB_EP_DIR_IN) { udd_ctrl_underflow(pointer); } else { udd_ctrl_overflow(pointer); @@ -895,9 +844,8 @@ static void _usb_ep0_on_tansfer_fail(struct usb_module *module_inst, void* point * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the endpoint transfer status parameter struct from driver layer. */ -static void _usb_ep0_on_tansfer_ok(struct usb_module *module_inst, void *pointer) -{ - if (UDD_EPCTRL_DATA_OUT == udd_ep_control_state) { /* handshake Out for status stage */ +static void _usb_ep0_on_tansfer_ok(struct usb_module *module_inst, void *pointer) { + if (UDD_EPCTRL_DATA_OUT == udd_ep_control_state) { /* handshake Out for status stage */ udd_ctrl_out_received(pointer); } else if (UDD_EPCTRL_DATA_IN == udd_ep_control_state) { /* handshake In for status stage */ udd_ctrl_in_sent(); @@ -913,25 +861,24 @@ static void _usb_ep0_on_tansfer_ok(struct usb_module *module_inst, void *pointer * \brief Enable Control Endpoint * \param[in] module_inst Pointer to USB module instance */ -static void udd_ctrl_ep_enable(struct usb_module *module_inst) -{ +static void udd_ctrl_ep_enable(struct usb_module *module_inst) { /* USB Device Endpoint0 Configuration */ struct usb_device_endpoint_config config_ep0; usb_device_endpoint_get_config_defaults(&config_ep0); config_ep0.ep_size = (enum usb_endpoint_size)(32 - clz(((uint32_t)Min(Max(USB_DEVICE_EP_CTRL_SIZE, 8), 1024) << 1) - 1) - 1 - 3); - usb_device_endpoint_set_config(module_inst,&config_ep0); + usb_device_endpoint_set_config(module_inst, &config_ep0); - usb_device_endpoint_setup_buffer_job(module_inst,udd_ctrl_buffer); + usb_device_endpoint_setup_buffer_job(module_inst, udd_ctrl_buffer); - usb_device_endpoint_register_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_RXSTP, _usb_ep0_on_setup ); - usb_device_endpoint_register_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT,_usb_ep0_on_tansfer_ok ); - usb_device_endpoint_register_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL,_usb_ep0_on_tansfer_fail ); - usb_device_endpoint_enable_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_RXSTP); - usb_device_endpoint_enable_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); - usb_device_endpoint_enable_callback(module_inst,0,USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL); + usb_device_endpoint_register_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_RXSTP, _usb_ep0_on_setup); + usb_device_endpoint_register_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT, _usb_ep0_on_tansfer_ok); + usb_device_endpoint_register_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL, _usb_ep0_on_tansfer_fail); + usb_device_endpoint_enable_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_RXSTP); + usb_device_endpoint_enable_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_TRCPT); + usb_device_endpoint_enable_callback(module_inst, 0, USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL); -#ifdef USB_DEVICE_LPM_SUPPORT +#ifdef USB_DEVICE_LPM_SUPPORT // Enable LPM feature usb_device_set_lpm_mode(module_inst, USB_DEVICE_LPM_ACK); #endif @@ -945,8 +892,7 @@ static void udd_ctrl_ep_enable(struct usb_module *module_inst) * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the callback parameter from driver layer. */ -static void _usb_on_suspend(struct usb_module *module_inst, void *pointer) -{ +static void _usb_on_suspend(struct usb_module *module_inst, void *pointer) { usb_device_disable_callback(&usb_device, USB_DEVICE_CALLBACK_SUSPEND); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_WAKEUP); udd_sleep_mode(UDD_STATE_SUSPEND); @@ -955,9 +901,8 @@ static void _usb_on_suspend(struct usb_module *module_inst, void *pointer) #endif } -#ifdef USB_DEVICE_LPM_SUPPORT -static void _usb_device_lpm_suspend(struct usb_module *module_inst, void *pointer) -{ +#ifdef USB_DEVICE_LPM_SUPPORT +static void _usb_device_lpm_suspend(struct usb_module *module_inst, void *pointer) { dbg_print("LPM_SUSP\n"); uint32_t *lpm_wakeup_enable; @@ -967,7 +912,7 @@ static void _usb_device_lpm_suspend(struct usb_module *module_inst, void *pointe usb_device_disable_callback(&usb_device, USB_DEVICE_CALLBACK_SUSPEND); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_WAKEUP); -//#warning Here the sleep mode must be choose to have a DFLL startup time < bmAttribut.HIRD + //#warning Here the sleep mode must be choose to have a DFLL startup time < bmAttribut.HIRD udd_sleep_mode(UDD_STATE_SUSPEND_LPM); // Enter in LPM SUSPEND mode if ((*lpm_wakeup_enable)) { UDC_REMOTEWAKEUP_LPM_ENABLE(); @@ -985,8 +930,7 @@ static void _usb_device_lpm_suspend(struct usb_module *module_inst, void *pointe * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the callback parameter from driver layer. */ -static void _usb_on_sof_notify(struct usb_module *module_inst, void *pointer) -{ +static void _usb_on_sof_notify(struct usb_module *module_inst, void *pointer) { udc_sof_notify(); #ifdef UDC_SOF_EVENT UDC_SOF_EVENT(); @@ -999,11 +943,10 @@ static void _usb_on_sof_notify(struct usb_module *module_inst, void *pointer) * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the callback parameter from driver layer. */ -static void _usb_on_bus_reset(struct usb_module *module_inst, void *pointer) -{ +static void _usb_on_bus_reset(struct usb_module *module_inst, void *pointer) { // Reset USB Device Stack Core udc_reset(); - usb_device_set_address(module_inst,0); + usb_device_set_address(module_inst, 0); udd_ctrl_ep_enable(module_inst); } @@ -1013,13 +956,12 @@ static void _usb_on_bus_reset(struct usb_module *module_inst, void *pointer) * \param[in] module_inst Pointer to USB module instance * \param[in] pointer Pointer to the callback parameter from driver layer. */ -static void _usb_on_wakeup(struct usb_module *module_inst, void *pointer) -{ +static void _usb_on_wakeup(struct usb_module *module_inst, void *pointer) { udd_wait_clock_ready(); usb_device_disable_callback(&usb_device, USB_DEVICE_CALLBACK_WAKEUP); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_SUSPEND); -#ifdef USB_DEVICE_LPM_SUPPORT +#ifdef USB_DEVICE_LPM_SUPPORT usb_device_register_callback(&usb_device, USB_DEVICE_CALLBACK_LPMSUSP, _usb_device_lpm_suspend); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_LPMSUSP); #endif @@ -1029,14 +971,12 @@ static void _usb_on_wakeup(struct usb_module *module_inst, void *pointer) #endif } -void udd_detach(void) -{ +void udd_detach(void) { usb_device_detach(&usb_device); udd_sleep_mode(UDD_STATE_SUSPEND); } -void udd_attach(void) -{ +void udd_attach(void) { udd_sleep_mode(UDD_STATE_IDLE); usb_device_attach(&usb_device); @@ -1049,14 +989,13 @@ void udd_attach(void) usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_SOF); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_RESET); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_WAKEUP); -#ifdef USB_DEVICE_LPM_SUPPORT +#ifdef USB_DEVICE_LPM_SUPPORT usb_device_register_callback(&usb_device, USB_DEVICE_CALLBACK_LPMSUSP, _usb_device_lpm_suspend); usb_device_enable_callback(&usb_device, USB_DEVICE_CALLBACK_LPMSUSP); #endif } -void udd_enable(void) -{ +void udd_enable(void) { uint32_t irqflags; /* To avoid USB interrupt before end of initialization */ @@ -1088,8 +1027,7 @@ void udd_enable(void) __set_PRIMASK(irqflags); } -void udd_disable(void) -{ +void udd_disable(void) { udd_detach(); udd_sleep_mode(UDD_STATE_OFF); diff --git a/tmk_core/protocol/arm_atsam/usb/usb_main.h b/tmk_core/protocol/arm_atsam/usb/usb_main.h index 76ced474d..e1ffa3e18 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_main.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_main.h @@ -47,57 +47,56 @@ #ifndef _MAIN_H_ #define _MAIN_H_ -//Enters the application in low power mode -//Callback called when USB host sets USB line in suspend state +// Enters the application in low power mode +// Callback called when USB host sets USB line in suspend state void main_suspend_action(void); -//Called by UDD when the USB line exit of suspend state +// Called by UDD when the USB line exit of suspend state void main_resume_action(void); -//Called when a start of frame is received on USB line +// Called when a start of frame is received on USB line void main_sof_action(void); -//Called by UDC when USB Host request to enable remote wakeup +// Called by UDC when USB Host request to enable remote wakeup void main_remotewakeup_enable(void); -//Called by UDC when USB Host request to disable remote wakeup +// Called by UDC when USB Host request to disable remote wakeup void main_remotewakeup_disable(void); - #ifdef KBD extern volatile bool main_b_kbd_enable; -bool main_kbd_enable(void); -void main_kbd_disable(void); -#endif //KBD +bool main_kbd_enable(void); +void main_kbd_disable(void); +#endif // KBD #ifdef NKRO extern volatile bool main_b_nkro_enable; -bool main_nkro_enable(void); -void main_nkro_disable(void); -#endif //NKRO +bool main_nkro_enable(void); +void main_nkro_disable(void); +#endif // NKRO #ifdef EXK extern volatile bool main_b_exk_enable; -bool main_exk_enable(void); -void main_exk_disable(void); -#endif //EXK +bool main_exk_enable(void); +void main_exk_disable(void); +#endif // EXK #ifdef CON extern volatile bool main_b_con_enable; -bool main_con_enable(void); -void main_con_disable(void); -#endif //CON +bool main_con_enable(void); +void main_con_disable(void); +#endif // CON #ifdef MOU extern volatile bool main_b_mou_enable; -bool main_mou_enable(void); -void main_mou_disable(void); -#endif //MOU +bool main_mou_enable(void); +void main_mou_disable(void); +#endif // MOU #ifdef RAW extern volatile bool main_b_raw_enable; -bool main_raw_enable(void); -void main_raw_disable(void); -#endif //RAW +bool main_raw_enable(void); +void main_raw_disable(void); +#endif // RAW -#endif // _MAIN_H_ +#endif // _MAIN_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb_protocol.h b/tmk_core/protocol/arm_atsam/usb/usb_protocol.h index 892a7d3a5..54d023a6e 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_protocol.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_protocol.h @@ -63,83 +63,77 @@ */ //! Value for field bcdUSB -#define USB_V2_0 0x0200 //!< USB Specification version 2.00 -#define USB_V2_1 0x0201 //!< USB Specification version 2.01 +#define USB_V2_0 0x0200 //!< USB Specification version 2.00 +#define USB_V2_1 0x0201 //!< USB Specification version 2.01 /*! \name Generic definitions (Class, subclass and protocol) */ //! @{ -#define NO_CLASS 0x00 -#define CLASS_VENDOR_SPECIFIC 0xFF -#define NO_SUBCLASS 0x00 -#define NO_PROTOCOL 0x00 +#define NO_CLASS 0x00 +#define CLASS_VENDOR_SPECIFIC 0xFF +#define NO_SUBCLASS 0x00 +#define NO_PROTOCOL 0x00 //! @} //! \name IAD (Interface Association Descriptor) constants //! @{ -#define CLASS_IAD 0xEF -#define SUB_CLASS_IAD 0x02 -#define PROTOCOL_IAD 0x01 +#define CLASS_IAD 0xEF +#define SUB_CLASS_IAD 0x02 +#define PROTOCOL_IAD 0x01 //! @} /** * \brief USB request data transfer direction (bmRequestType) */ -#define USB_REQ_DIR_OUT (0<<7) //!< Host to device -#define USB_REQ_DIR_IN (1<<7) //!< Device to host -#define USB_REQ_DIR_MASK (1<<7) //!< Mask +#define USB_REQ_DIR_OUT (0 << 7) //!< Host to device +#define USB_REQ_DIR_IN (1 << 7) //!< Device to host +#define USB_REQ_DIR_MASK (1 << 7) //!< Mask /** * \brief USB request types (bmRequestType) */ -#define USB_REQ_TYPE_STANDARD (0<<5) //!< Standard request -#define USB_REQ_TYPE_CLASS (1<<5) //!< Class-specific request -#define USB_REQ_TYPE_VENDOR (2<<5) //!< Vendor-specific request -#define USB_REQ_TYPE_MASK (3<<5) //!< Mask +#define USB_REQ_TYPE_STANDARD (0 << 5) //!< Standard request +#define USB_REQ_TYPE_CLASS (1 << 5) //!< Class-specific request +#define USB_REQ_TYPE_VENDOR (2 << 5) //!< Vendor-specific request +#define USB_REQ_TYPE_MASK (3 << 5) //!< Mask /** * \brief USB recipient codes (bmRequestType) */ -#define USB_REQ_RECIP_DEVICE (0<<0) //!< Recipient device -#define USB_REQ_RECIP_INTERFACE (1<<0) //!< Recipient interface -#define USB_REQ_RECIP_ENDPOINT (2<<0) //!< Recipient endpoint -#define USB_REQ_RECIP_OTHER (3<<0) //!< Recipient other -#define USB_REQ_RECIP_MASK (0x1F) //!< Mask +#define USB_REQ_RECIP_DEVICE (0 << 0) //!< Recipient device +#define USB_REQ_RECIP_INTERFACE (1 << 0) //!< Recipient interface +#define USB_REQ_RECIP_ENDPOINT (2 << 0) //!< Recipient endpoint +#define USB_REQ_RECIP_OTHER (3 << 0) //!< Recipient other +#define USB_REQ_RECIP_MASK (0x1F) //!< Mask /** * \brief Standard USB requests (bRequest) */ enum usb_reqid { - USB_REQ_GET_STATUS = 0, - USB_REQ_CLEAR_FEATURE = 1, - USB_REQ_SET_FEATURE = 3, - USB_REQ_SET_ADDRESS = 5, - USB_REQ_GET_DESCRIPTOR = 6, - USB_REQ_SET_DESCRIPTOR = 7, + USB_REQ_GET_STATUS = 0, + USB_REQ_CLEAR_FEATURE = 1, + USB_REQ_SET_FEATURE = 3, + USB_REQ_SET_ADDRESS = 5, + USB_REQ_GET_DESCRIPTOR = 6, + USB_REQ_SET_DESCRIPTOR = 7, USB_REQ_GET_CONFIGURATION = 8, USB_REQ_SET_CONFIGURATION = 9, - USB_REQ_GET_INTERFACE = 10, - USB_REQ_SET_INTERFACE = 11, - USB_REQ_SYNCH_FRAME = 12, + USB_REQ_GET_INTERFACE = 10, + USB_REQ_SET_INTERFACE = 11, + USB_REQ_SYNCH_FRAME = 12, }; /** * \brief Standard USB device status flags * */ -enum usb_device_status { - USB_DEV_STATUS_BUS_POWERED = 0, - USB_DEV_STATUS_SELF_POWERED = 1, - USB_DEV_STATUS_REMOTEWAKEUP = 2 -}; +enum usb_device_status { USB_DEV_STATUS_BUS_POWERED = 0, USB_DEV_STATUS_SELF_POWERED = 1, USB_DEV_STATUS_REMOTEWAKEUP = 2 }; /** * \brief Standard USB Interface status flags * */ -enum usb_interface_status { - USB_IFACE_STATUS_RESERVED = 0 -}; +enum usb_interface_status { USB_IFACE_STATUS_RESERVED = 0 }; /** * \brief Standard USB endpoint status flags @@ -155,10 +149,10 @@ enum usb_endpoint_status { * \note valid for SetFeature request. */ enum usb_device_feature { - USB_DEV_FEATURE_REMOTE_WAKEUP = 1, //!< Remote wakeup enabled - USB_DEV_FEATURE_TEST_MODE = 2, //!< USB test mode - USB_DEV_FEATURE_OTG_B_HNP_ENABLE = 3, - USB_DEV_FEATURE_OTG_A_HNP_SUPPORT = 4, + USB_DEV_FEATURE_REMOTE_WAKEUP = 1, //!< Remote wakeup enabled + USB_DEV_FEATURE_TEST_MODE = 2, //!< USB test mode + USB_DEV_FEATURE_OTG_B_HNP_ENABLE = 3, + USB_DEV_FEATURE_OTG_A_HNP_SUPPORT = 4, USB_DEV_FEATURE_OTG_A_ALT_HNP_SUPPORT = 5 }; @@ -168,10 +162,10 @@ enum usb_device_feature { * \note valid for USB_DEV_FEATURE_TEST_MODE request. */ enum usb_device_hs_test_mode { - USB_DEV_TEST_MODE_J = 1, - USB_DEV_TEST_MODE_K = 2, - USB_DEV_TEST_MODE_SE0_NAK = 3, - USB_DEV_TEST_MODE_PACKET = 4, + USB_DEV_TEST_MODE_J = 1, + USB_DEV_TEST_MODE_K = 2, + USB_DEV_TEST_MODE_SE0_NAK = 3, + USB_DEV_TEST_MODE_PACKET = 4, USB_DEV_TEST_MODE_FORCE_ENABLE = 5, }; @@ -186,10 +180,10 @@ enum usb_endpoint_feature { * \brief Standard USB Test Mode Selectors */ enum usb_test_mode_selector { - USB_TEST_J = 0x01, - USB_TEST_K = 0x02, - USB_TEST_SE0_NAK = 0x03, - USB_TEST_PACKET = 0x04, + USB_TEST_J = 0x01, + USB_TEST_K = 0x02, + USB_TEST_SE0_NAK = 0x03, + USB_TEST_PACKET = 0x04, USB_TEST_FORCE_ENABLE = 0x05, }; @@ -197,18 +191,18 @@ enum usb_test_mode_selector { * \brief Standard USB descriptor types */ enum usb_descriptor_type { - USB_DT_DEVICE = 1, - USB_DT_CONFIGURATION = 2, - USB_DT_STRING = 3, - USB_DT_INTERFACE = 4, - USB_DT_ENDPOINT = 5, - USB_DT_DEVICE_QUALIFIER = 6, + USB_DT_DEVICE = 1, + USB_DT_CONFIGURATION = 2, + USB_DT_STRING = 3, + USB_DT_INTERFACE = 4, + USB_DT_ENDPOINT = 5, + USB_DT_DEVICE_QUALIFIER = 6, USB_DT_OTHER_SPEED_CONFIGURATION = 7, - USB_DT_INTERFACE_POWER = 8, - USB_DT_OTG = 9, - USB_DT_IAD = 0x0B, - USB_DT_BOS = 0x0F, - USB_DT_DEVICE_CAPABILITY = 0x10, + USB_DT_INTERFACE_POWER = 8, + USB_DT_OTG = 9, + USB_DT_IAD = 0x0B, + USB_DT_BOS = 0x0F, + USB_DT_DEVICE_CAPABILITY = 0x10, }; /** @@ -223,57 +217,57 @@ enum usb_capability_type { * To fill bmAttributes field of usb_capa_ext_desc_t structure. */ enum usb_capability_extension_attr { - USB_DC_EXT_LPM = 0x00000002, + USB_DC_EXT_LPM = 0x00000002, }; -#define HIRD_50_US 0 -#define HIRD_125_US 1 -#define HIRD_200_US 2 -#define HIRD_275_US 3 -#define HIRD_350_US 4 -#define HIRD_425_US 5 -#define HIRD_500_US 6 -#define HIRD_575_US 7 -#define HIRD_650_US 8 -#define HIRD_725_US 9 -#define HIRD_800_US 10 -#define HIRD_875_US 11 -#define HIRD_950_US 12 -#define HIRD_1025_US 13 -#define HIRD_1100_US 14 -#define HIRD_1175_US 15 +#define HIRD_50_US 0 +#define HIRD_125_US 1 +#define HIRD_200_US 2 +#define HIRD_275_US 3 +#define HIRD_350_US 4 +#define HIRD_425_US 5 +#define HIRD_500_US 6 +#define HIRD_575_US 7 +#define HIRD_650_US 8 +#define HIRD_725_US 9 +#define HIRD_800_US 10 +#define HIRD_875_US 11 +#define HIRD_950_US 12 +#define HIRD_1025_US 13 +#define HIRD_1100_US 14 +#define HIRD_1175_US 15 /** Fields definition from a LPM TOKEN */ -#define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0) -#define USB_LPM_ATTRIBUT_FIRD_MASK (0xF << 4) -#define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8) -#define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0) -#define USB_LPM_ATTRIBUT_FIRD(value) ((value & 0xF) << 4) -#define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8) -#define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1) +#define USB_LPM_ATTRIBUT_BLINKSTATE_MASK (0xF << 0) +#define USB_LPM_ATTRIBUT_FIRD_MASK (0xF << 4) +#define USB_LPM_ATTRIBUT_REMOTEWAKE_MASK (1 << 8) +#define USB_LPM_ATTRIBUT_BLINKSTATE(value) ((value & 0xF) << 0) +#define USB_LPM_ATTRIBUT_FIRD(value) ((value & 0xF) << 4) +#define USB_LPM_ATTRIBUT_REMOTEWAKE(value) ((value & 1) << 8) +#define USB_LPM_ATTRIBUT_BLINKSTATE_L1 USB_LPM_ATTRIBUT_BLINKSTATE(1) /** * \brief Standard USB endpoint transfer types */ enum usb_ep_type { - USB_EP_TYPE_CONTROL = 0x00, + USB_EP_TYPE_CONTROL = 0x00, USB_EP_TYPE_ISOCHRONOUS = 0x01, - USB_EP_TYPE_BULK = 0x02, - USB_EP_TYPE_INTERRUPT = 0x03, - USB_EP_TYPE_MASK = 0x03, + USB_EP_TYPE_BULK = 0x02, + USB_EP_TYPE_INTERRUPT = 0x03, + USB_EP_TYPE_MASK = 0x03, }; /** * \brief Standard USB language IDs for string descriptors */ enum usb_langid { - USB_LANGID_EN_US = 0x0409, //!< English (United States) + USB_LANGID_EN_US = 0x0409, //!< English (United States) }; /** * \brief Mask selecting the index part of an endpoint address */ -#define USB_EP_ADDR_MASK 0x0f +#define USB_EP_ADDR_MASK 0x0f //! \brief USB address identifier typedef uint8_t usb_add_t; @@ -281,12 +275,12 @@ typedef uint8_t usb_add_t; /** * \brief Endpoint transfer direction is IN */ -#define USB_EP_DIR_IN 0x80 +#define USB_EP_DIR_IN 0x80 /** * \brief Endpoint transfer direction is OUT */ -#define USB_EP_DIR_OUT 0x00 +#define USB_EP_DIR_OUT 0x00 //! \brief Endpoint identifier typedef uint8_t usb_ep_t; @@ -297,7 +291,7 @@ typedef uint8_t usb_ep_t; * The maximum length of a USB descriptor is limited by the 8-bit * bLength field. */ -#define USB_MAX_DESC_LEN 255 +#define USB_MAX_DESC_LEN 255 /* * 2-byte alignment requested for all USB structures. @@ -312,9 +306,9 @@ COMPILER_PACK_SET(1) typedef struct { uint8_t bmRequestType; uint8_t bRequest; - le16_t wValue; - le16_t wIndex; - le16_t wLength; + le16_t wValue; + le16_t wIndex; + le16_t wLength; } usb_setup_req_t; /** @@ -323,14 +317,14 @@ typedef struct { typedef struct { uint8_t bLength; uint8_t bDescriptorType; - le16_t bcdUSB; + le16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; - le16_t idVendor; - le16_t idProduct; - le16_t bcdDevice; + le16_t idVendor; + le16_t idProduct; + le16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; @@ -348,7 +342,7 @@ typedef struct { typedef struct { uint8_t bLength; uint8_t bDescriptorType; - le16_t bcdUSB; + le16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; @@ -376,7 +370,6 @@ typedef struct { uint8_t bNumDeviceCaps; } usb_dev_bos_desc_t; - /** * \brief USB Device Capabilities - USB 2.0 Extension Descriptor structure * @@ -395,7 +388,7 @@ typedef struct { * The BOS descriptor and capabilities descriptors for LPM. */ typedef struct { - usb_dev_bos_desc_t bos; + usb_dev_bos_desc_t bos; usb_dev_capa_ext_desc_t capa_ext; } usb_dev_lpm_desc_t; @@ -403,24 +396,23 @@ typedef struct { * \brief Standard USB Interface Association Descriptor structure */ typedef struct { - uint8_t bLength; //!< size of this descriptor in bytes - uint8_t bDescriptorType; //!< INTERFACE descriptor type - uint8_t bFirstInterface; //!< Number of interface - uint8_t bInterfaceCount; //!< value to select alternate setting - uint8_t bFunctionClass; //!< Class code assigned by the USB - uint8_t bFunctionSubClass;//!< Sub-class code assigned by the USB - uint8_t bFunctionProtocol;//!< Protocol code assigned by the USB - uint8_t iFunction; //!< Index of string descriptor + uint8_t bLength; //!< size of this descriptor in bytes + uint8_t bDescriptorType; //!< INTERFACE descriptor type + uint8_t bFirstInterface; //!< Number of interface + uint8_t bInterfaceCount; //!< value to select alternate setting + uint8_t bFunctionClass; //!< Class code assigned by the USB + uint8_t bFunctionSubClass; //!< Sub-class code assigned by the USB + uint8_t bFunctionProtocol; //!< Protocol code assigned by the USB + uint8_t iFunction; //!< Index of string descriptor } usb_association_desc_t; - /** * \brief Standard USB configuration descriptor structure */ typedef struct { uint8_t bLength; uint8_t bDescriptorType; - le16_t wTotalLength; + le16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; @@ -428,26 +420,25 @@ typedef struct { uint8_t bMaxPower; } usb_conf_desc_t; +#define USB_CONFIG_ATTR_MUST_SET (1 << 7) //!< Must always be set +#define USB_CONFIG_ATTR_BUS_POWERED (0 << 6) //!< Bus-powered +#define USB_CONFIG_ATTR_SELF_POWERED (1 << 6) //!< Self-powered +#define USB_CONFIG_ATTR_REMOTE_WAKEUP (1 << 5) //!< remote wakeup supported -#define USB_CONFIG_ATTR_MUST_SET (1 << 7) //!< Must always be set -#define USB_CONFIG_ATTR_BUS_POWERED (0 << 6) //!< Bus-powered -#define USB_CONFIG_ATTR_SELF_POWERED (1 << 6) //!< Self-powered -#define USB_CONFIG_ATTR_REMOTE_WAKEUP (1 << 5) //!< remote wakeup supported - -#define USB_CONFIG_MAX_POWER(ma) (((ma) + 1) / 2) //!< Max power in mA +#define USB_CONFIG_MAX_POWER(ma) (((ma) + 1) / 2) //!< Max power in mA /** * \brief Standard USB association descriptor structure */ typedef struct { - uint8_t bLength; //!< Size of this descriptor in bytes - uint8_t bDescriptorType; //!< Interface descriptor type - uint8_t bFirstInterface; //!< Number of interface - uint8_t bInterfaceCount; //!< value to select alternate setting - uint8_t bFunctionClass; //!< Class code assigned by the USB - uint8_t bFunctionSubClass; //!< Sub-class code assigned by the USB - uint8_t bFunctionProtocol; //!< Protocol code assigned by the USB - uint8_t iFunction; //!< Index of string descriptor + uint8_t bLength; //!< Size of this descriptor in bytes + uint8_t bDescriptorType; //!< Interface descriptor type + uint8_t bFirstInterface; //!< Number of interface + uint8_t bInterfaceCount; //!< value to select alternate setting + uint8_t bFunctionClass; //!< Class code assigned by the USB + uint8_t bFunctionSubClass; //!< Sub-class code assigned by the USB + uint8_t bFunctionProtocol; //!< Protocol code assigned by the USB + uint8_t iFunction; //!< Index of string descriptor } usb_iad_desc_t; /** @@ -473,11 +464,10 @@ typedef struct { uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; - le16_t wMaxPacketSize; + le16_t wMaxPacketSize; uint8_t bInterval; } usb_ep_desc_t; - /** * \brief A standard USB string descriptor structure */ @@ -488,7 +478,7 @@ typedef struct { typedef struct { usb_str_desc_t desc; - le16_t string[1]; + le16_t string[1]; } usb_str_lgid_desc_t; COMPILER_PACK_RESET() diff --git a/tmk_core/protocol/arm_atsam/usb/usb_protocol_cdc.h b/tmk_core/protocol/arm_atsam/usb/usb_protocol_cdc.h index 479f25d4e..aa639a6e5 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_protocol_cdc.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_protocol_cdc.h @@ -50,144 +50,141 @@ #ifdef CDC -#define CDC_CLASS_DEVICE 0x02 //!< USB Communication Device Class -#define CDC_CLASS_COMM 0x02 //!< CDC Communication Class Interface -#define CDC_CLASS_DATA 0x0A //!< CDC Data Class Interface - -#define CDC_SUBCLASS_DLCM 0x01 //!< Direct Line Control Model -#define CDC_SUBCLASS_ACM 0x02 //!< Abstract Control Model -#define CDC_SUBCLASS_TCM 0x03 //!< Telephone Control Model -#define CDC_SUBCLASS_MCCM 0x04 //!< Multi-Channel Control Model -#define CDC_SUBCLASS_CCM 0x05 //!< CAPI Control Model -#define CDC_SUBCLASS_ETH 0x06 //!< Ethernet Networking Control Model -#define CDC_SUBCLASS_ATM 0x07 //!< ATM Networking Control Model - -#define CDC_PROTOCOL_V25TER 0x01 //!< Common AT commands - -#define CDC_PROTOCOL_I430 0x30 //!< ISDN BRI -#define CDC_PROTOCOL_HDLC 0x31 //!< HDLC -#define CDC_PROTOCOL_TRANS 0x32 //!< Transparent -#define CDC_PROTOCOL_Q921M 0x50 //!< Q.921 management protocol -#define CDC_PROTOCOL_Q921 0x51 //!< Q.931 [sic] Data link protocol -#define CDC_PROTOCOL_Q921TM 0x52 //!< Q.921 TEI-multiplexor -#define CDC_PROTOCOL_V42BIS 0x90 //!< Data compression procedures -#define CDC_PROTOCOL_Q931 0x91 //!< Euro-ISDN protocol control -#define CDC_PROTOCOL_V120 0x92 //!< V.24 rate adaption to ISDN -#define CDC_PROTOCOL_CAPI20 0x93 //!< CAPI Commands -#define CDC_PROTOCOL_HOST 0xFD //!< Host based driver - -#define CDC_PROTOCOL_PUFD 0xFE - -#define CDC_CS_INTERFACE 0x24 //!< Interface Functional Descriptor -#define CDC_CS_ENDPOINT 0x25 //!< Endpoint Functional Descriptor - -#define CDC_SCS_HEADER 0x00 //!< Header Functional Descriptor -#define CDC_SCS_CALL_MGMT 0x01 //!< Call Management -#define CDC_SCS_ACM 0x02 //!< Abstract Control Management -#define CDC_SCS_UNION 0x06 //!< Union Functional Descriptor - -#define USB_REQ_CDC_SEND_ENCAPSULATED_COMMAND 0x00 -#define USB_REQ_CDC_GET_ENCAPSULATED_RESPONSE 0x01 -#define USB_REQ_CDC_SET_COMM_FEATURE 0x02 -#define USB_REQ_CDC_GET_COMM_FEATURE 0x03 -#define USB_REQ_CDC_CLEAR_COMM_FEATURE 0x04 -#define USB_REQ_CDC_SET_AUX_LINE_STATE 0x10 -#define USB_REQ_CDC_SET_HOOK_STATE 0x11 -#define USB_REQ_CDC_PULSE_SETUP 0x12 -#define USB_REQ_CDC_SEND_PULSE 0x13 -#define USB_REQ_CDC_SET_PULSE_TIME 0x14 -#define USB_REQ_CDC_RING_AUX_JACK 0x15 -#define USB_REQ_CDC_SET_LINE_CODING 0x20 -#define USB_REQ_CDC_GET_LINE_CODING 0x21 -#define USB_REQ_CDC_SET_CONTROL_LINE_STATE 0x22 -#define USB_REQ_CDC_SEND_BREAK 0x23 -#define USB_REQ_CDC_SET_RINGER_PARMS 0x30 -#define USB_REQ_CDC_GET_RINGER_PARMS 0x31 -#define USB_REQ_CDC_SET_OPERATION_PARMS 0x32 -#define USB_REQ_CDC_GET_OPERATION_PARMS 0x33 -#define USB_REQ_CDC_SET_LINE_PARMS 0x34 -#define USB_REQ_CDC_GET_LINE_PARMS 0x35 -#define USB_REQ_CDC_DIAL_DIGITS 0x36 -#define USB_REQ_CDC_SET_UNIT_PARAMETER 0x37 -#define USB_REQ_CDC_GET_UNIT_PARAMETER 0x38 -#define USB_REQ_CDC_CLEAR_UNIT_PARAMETER 0x39 -#define USB_REQ_CDC_GET_PROFILE 0x3A -#define USB_REQ_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 -#define USB_REQ_CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x41 -#define USB_REQ_CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x42 -#define USB_REQ_CDC_SET_ETHERNET_PACKET_FILTER 0x43 -#define USB_REQ_CDC_GET_ETHERNET_STATISTIC 0x44 -#define USB_REQ_CDC_SET_ATM_DATA_FORMAT 0x50 -#define USB_REQ_CDC_GET_ATM_DEVICE_STATISTICS 0x51 -#define USB_REQ_CDC_SET_ATM_DEFAULT_VC 0x52 -#define USB_REQ_CDC_GET_ATM_VC_STATISTICS 0x53 +# define CDC_CLASS_DEVICE 0x02 //!< USB Communication Device Class +# define CDC_CLASS_COMM 0x02 //!< CDC Communication Class Interface +# define CDC_CLASS_DATA 0x0A //!< CDC Data Class Interface + +# define CDC_SUBCLASS_DLCM 0x01 //!< Direct Line Control Model +# define CDC_SUBCLASS_ACM 0x02 //!< Abstract Control Model +# define CDC_SUBCLASS_TCM 0x03 //!< Telephone Control Model +# define CDC_SUBCLASS_MCCM 0x04 //!< Multi-Channel Control Model +# define CDC_SUBCLASS_CCM 0x05 //!< CAPI Control Model +# define CDC_SUBCLASS_ETH 0x06 //!< Ethernet Networking Control Model +# define CDC_SUBCLASS_ATM 0x07 //!< ATM Networking Control Model + +# define CDC_PROTOCOL_V25TER 0x01 //!< Common AT commands + +# define CDC_PROTOCOL_I430 0x30 //!< ISDN BRI +# define CDC_PROTOCOL_HDLC 0x31 //!< HDLC +# define CDC_PROTOCOL_TRANS 0x32 //!< Transparent +# define CDC_PROTOCOL_Q921M 0x50 //!< Q.921 management protocol +# define CDC_PROTOCOL_Q921 0x51 //!< Q.931 [sic] Data link protocol +# define CDC_PROTOCOL_Q921TM 0x52 //!< Q.921 TEI-multiplexor +# define CDC_PROTOCOL_V42BIS 0x90 //!< Data compression procedures +# define CDC_PROTOCOL_Q931 0x91 //!< Euro-ISDN protocol control +# define CDC_PROTOCOL_V120 0x92 //!< V.24 rate adaption to ISDN +# define CDC_PROTOCOL_CAPI20 0x93 //!< CAPI Commands +# define CDC_PROTOCOL_HOST 0xFD //!< Host based driver + +# define CDC_PROTOCOL_PUFD 0xFE + +# define CDC_CS_INTERFACE 0x24 //!< Interface Functional Descriptor +# define CDC_CS_ENDPOINT 0x25 //!< Endpoint Functional Descriptor + +# define CDC_SCS_HEADER 0x00 //!< Header Functional Descriptor +# define CDC_SCS_CALL_MGMT 0x01 //!< Call Management +# define CDC_SCS_ACM 0x02 //!< Abstract Control Management +# define CDC_SCS_UNION 0x06 //!< Union Functional Descriptor + +# define USB_REQ_CDC_SEND_ENCAPSULATED_COMMAND 0x00 +# define USB_REQ_CDC_GET_ENCAPSULATED_RESPONSE 0x01 +# define USB_REQ_CDC_SET_COMM_FEATURE 0x02 +# define USB_REQ_CDC_GET_COMM_FEATURE 0x03 +# define USB_REQ_CDC_CLEAR_COMM_FEATURE 0x04 +# define USB_REQ_CDC_SET_AUX_LINE_STATE 0x10 +# define USB_REQ_CDC_SET_HOOK_STATE 0x11 +# define USB_REQ_CDC_PULSE_SETUP 0x12 +# define USB_REQ_CDC_SEND_PULSE 0x13 +# define USB_REQ_CDC_SET_PULSE_TIME 0x14 +# define USB_REQ_CDC_RING_AUX_JACK 0x15 +# define USB_REQ_CDC_SET_LINE_CODING 0x20 +# define USB_REQ_CDC_GET_LINE_CODING 0x21 +# define USB_REQ_CDC_SET_CONTROL_LINE_STATE 0x22 +# define USB_REQ_CDC_SEND_BREAK 0x23 +# define USB_REQ_CDC_SET_RINGER_PARMS 0x30 +# define USB_REQ_CDC_GET_RINGER_PARMS 0x31 +# define USB_REQ_CDC_SET_OPERATION_PARMS 0x32 +# define USB_REQ_CDC_GET_OPERATION_PARMS 0x33 +# define USB_REQ_CDC_SET_LINE_PARMS 0x34 +# define USB_REQ_CDC_GET_LINE_PARMS 0x35 +# define USB_REQ_CDC_DIAL_DIGITS 0x36 +# define USB_REQ_CDC_SET_UNIT_PARAMETER 0x37 +# define USB_REQ_CDC_GET_UNIT_PARAMETER 0x38 +# define USB_REQ_CDC_CLEAR_UNIT_PARAMETER 0x39 +# define USB_REQ_CDC_GET_PROFILE 0x3A +# define USB_REQ_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 +# define USB_REQ_CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x41 +# define USB_REQ_CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERNFILTER 0x42 +# define USB_REQ_CDC_SET_ETHERNET_PACKET_FILTER 0x43 +# define USB_REQ_CDC_GET_ETHERNET_STATISTIC 0x44 +# define USB_REQ_CDC_SET_ATM_DATA_FORMAT 0x50 +# define USB_REQ_CDC_GET_ATM_DEVICE_STATISTICS 0x51 +# define USB_REQ_CDC_SET_ATM_DEFAULT_VC 0x52 +# define USB_REQ_CDC_GET_ATM_VC_STATISTICS 0x53 // Added bNotification codes according cdc spec 1.1 chapter 6.3 -#define USB_REQ_CDC_NOTIFY_RING_DETECT 0x09 -#define USB_REQ_CDC_NOTIFY_SERIAL_STATE 0x20 -#define USB_REQ_CDC_NOTIFY_CALL_STATE_CHANGE 0x28 -#define USB_REQ_CDC_NOTIFY_LINE_STATE_CHANGE 0x29 - - -#define CDC_CALL_MGMT_SUPPORTED (1 << 0) -#define CDC_CALL_MGMT_OVER_DCI (1 << 1) -#define CDC_ACM_SUPPORT_FEATURE_REQUESTS (1 << 0) -#define CDC_ACM_SUPPORT_LINE_REQUESTS (1 << 1) -#define CDC_ACM_SUPPORT_SENDBREAK_REQUESTS (1 << 2) -#define CDC_ACM_SUPPORT_NOTIFY_REQUESTS (1 << 3) - -#pragma pack(push,1) +# define USB_REQ_CDC_NOTIFY_RING_DETECT 0x09 +# define USB_REQ_CDC_NOTIFY_SERIAL_STATE 0x20 +# define USB_REQ_CDC_NOTIFY_CALL_STATE_CHANGE 0x28 +# define USB_REQ_CDC_NOTIFY_LINE_STATE_CHANGE 0x29 + +# define CDC_CALL_MGMT_SUPPORTED (1 << 0) +# define CDC_CALL_MGMT_OVER_DCI (1 << 1) +# define CDC_ACM_SUPPORT_FEATURE_REQUESTS (1 << 0) +# define CDC_ACM_SUPPORT_LINE_REQUESTS (1 << 1) +# define CDC_ACM_SUPPORT_SENDBREAK_REQUESTS (1 << 2) +# define CDC_ACM_SUPPORT_NOTIFY_REQUESTS (1 << 3) + +# pragma pack(push, 1) typedef struct { - le32_t dwDTERate; + le32_t dwDTERate; uint8_t bCharFormat; uint8_t bParityType; uint8_t bDataBits; } usb_cdc_line_coding_t; -#pragma pack(pop) +# pragma pack(pop) enum cdc_char_format { - CDC_STOP_BITS_1 = 0, //!< 1 stop bit + CDC_STOP_BITS_1 = 0, //!< 1 stop bit CDC_STOP_BITS_1_5 = 1, //!< 1.5 stop bits - CDC_STOP_BITS_2 = 2, //!< 2 stop bits + CDC_STOP_BITS_2 = 2, //!< 2 stop bits }; enum cdc_parity { - CDC_PAR_NONE = 0, //!< No parity - CDC_PAR_ODD = 1, //!< Odd parity - CDC_PAR_EVEN = 2, //!< Even parity - CDC_PAR_MARK = 3, //!< Parity forced to 0 (space) + CDC_PAR_NONE = 0, //!< No parity + CDC_PAR_ODD = 1, //!< Odd parity + CDC_PAR_EVEN = 2, //!< Even parity + CDC_PAR_MARK = 3, //!< Parity forced to 0 (space) CDC_PAR_SPACE = 4, //!< Parity forced to 1 (mark) }; - typedef struct { uint16_t value; } usb_cdc_control_signal_t; -#define CDC_CTRL_SIGNAL_ACTIVATE_CARRIER (1 << 1) -#define CDC_CTRL_SIGNAL_DTE_PRESENT (1 << 0) - +# define CDC_CTRL_SIGNAL_ACTIVATE_CARRIER (1 << 1) +# define CDC_CTRL_SIGNAL_DTE_PRESENT (1 << 0) typedef struct { uint8_t bmRequestType; uint8_t bNotification; - le16_t wValue; - le16_t wIndex; - le16_t wLength; + le16_t wValue; + le16_t wIndex; + le16_t wLength; } usb_cdc_notify_msg_t; typedef struct { usb_cdc_notify_msg_t header; - le16_t value; + le16_t value; } usb_cdc_notify_serial_state_t; -#define CDC_SERIAL_STATE_DCD CPU_TO_LE16((1<<0)) -#define CDC_SERIAL_STATE_DSR CPU_TO_LE16((1<<1)) -#define CDC_SERIAL_STATE_BREAK CPU_TO_LE16((1<<2)) -#define CDC_SERIAL_STATE_RING CPU_TO_LE16((1<<3)) -#define CDC_SERIAL_STATE_FRAMING CPU_TO_LE16((1<<4)) -#define CDC_SERIAL_STATE_PARITY CPU_TO_LE16((1<<5)) -#define CDC_SERIAL_STATE_OVERRUN CPU_TO_LE16((1<<6)) +# define CDC_SERIAL_STATE_DCD CPU_TO_LE16((1 << 0)) +# define CDC_SERIAL_STATE_DSR CPU_TO_LE16((1 << 1)) +# define CDC_SERIAL_STATE_BREAK CPU_TO_LE16((1 << 2)) +# define CDC_SERIAL_STATE_RING CPU_TO_LE16((1 << 3)) +# define CDC_SERIAL_STATE_FRAMING CPU_TO_LE16((1 << 4)) +# define CDC_SERIAL_STATE_PARITY CPU_TO_LE16((1 << 5)) +# define CDC_SERIAL_STATE_OVERRUN CPU_TO_LE16((1 << 6)) #endif -#endif // _USB_PROTOCOL_CDC_H_ +#endif // _USB_PROTOCOL_CDC_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb_protocol_hid.h b/tmk_core/protocol/arm_atsam/usb/usb_protocol_hid.h index c482e9c06..2f8a39bdd 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_protocol_hid.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_protocol_hid.h @@ -58,64 +58,62 @@ //! \name Possible Class value //@{ -#define HID_CLASS 0x03 +#define HID_CLASS 0x03 //@} //! \name Possible SubClass value //@{ //! Interface subclass NO support BOOT protocol -#define HID_SUB_CLASS_NOBOOT 0x00 +#define HID_SUB_CLASS_NOBOOT 0x00 //! Interface subclass support BOOT protocol -#define HID_SUB_CLASS_BOOT 0x01 +#define HID_SUB_CLASS_BOOT 0x01 //@} //! \name Possible protocol value //@{ //! Protocol generic standard -#define HID_PROTOCOL_GENERIC 0x00 +#define HID_PROTOCOL_GENERIC 0x00 //! Protocol keyboard standard -#define HID_PROTOCOL_KEYBOARD 0x01 +#define HID_PROTOCOL_KEYBOARD 0x01 //! Protocol mouse standard -#define HID_PROTOCOL_MOUSE 0x02 +#define HID_PROTOCOL_MOUSE 0x02 //@} - //! \brief Hid USB requests (bRequest) enum usb_reqid_hid { - USB_REQ_HID_GET_REPORT = 0x01, - USB_REQ_HID_GET_IDLE = 0x02, + USB_REQ_HID_GET_REPORT = 0x01, + USB_REQ_HID_GET_IDLE = 0x02, USB_REQ_HID_GET_PROTOCOL = 0x03, - USB_REQ_HID_SET_REPORT = 0x09, - USB_REQ_HID_SET_IDLE = 0x0A, + USB_REQ_HID_SET_REPORT = 0x09, + USB_REQ_HID_SET_IDLE = 0x0A, USB_REQ_HID_SET_PROTOCOL = 0x0B, }; //! \brief HID USB descriptor types enum usb_descriptor_type_hid { - USB_DT_HID = 0x21, - USB_DT_HID_REPORT = 0x22, + USB_DT_HID = 0x21, + USB_DT_HID_REPORT = 0x22, USB_DT_HID_PHYSICAL = 0x23, }; //! \brief HID Type for report descriptor enum usb_hid_item_report_type { - USB_HID_ITEM_REPORT_TYPE_MAIN = 0, + USB_HID_ITEM_REPORT_TYPE_MAIN = 0, USB_HID_ITEM_REPORT_TYPE_GLOBAL = 1, - USB_HID_ITEM_REPORT_TYPE_LOCAL = 2, - USB_HID_ITEM_REPORT_TYPE_LONG = 3, + USB_HID_ITEM_REPORT_TYPE_LOCAL = 2, + USB_HID_ITEM_REPORT_TYPE_LONG = 3, }; //! \brief HID report type enum usb_hid_report_type { - USB_HID_REPORT_TYPE_INPUT = 1, - USB_HID_REPORT_TYPE_OUTPUT = 2, + USB_HID_REPORT_TYPE_INPUT = 1, + USB_HID_REPORT_TYPE_OUTPUT = 2, USB_HID_REPORT_TYPE_FEATURE = 3, }; - //! \brief HID protocol enum usb_hid_protocol { - USB_HID_PROCOTOL_BOOT = 0, + USB_HID_PROCOTOL_BOOT = 0, USB_HID_PROCOTOL_REPORT = 1, }; @@ -123,197 +121,196 @@ COMPILER_PACK_SET(1) //! \brief HID Descriptor typedef struct { - uint8_t bLength; //!< Size of this descriptor in bytes - uint8_t bDescriptorType; //!< HID descriptor type - le16_t bcdHID; //!< Binary Coded Decimal Spec. release - uint8_t bCountryCode; //!< Hardware target country - uint8_t bNumDescriptors; //!< Number of HID class descriptors to follow - uint8_t bRDescriptorType; //!< Report descriptor type - le16_t wDescriptorLength; //!< Total length of Report descriptor + uint8_t bLength; //!< Size of this descriptor in bytes + uint8_t bDescriptorType; //!< HID descriptor type + le16_t bcdHID; //!< Binary Coded Decimal Spec. release + uint8_t bCountryCode; //!< Hardware target country + uint8_t bNumDescriptors; //!< Number of HID class descriptors to follow + uint8_t bRDescriptorType; //!< Report descriptor type + le16_t wDescriptorLength; //!< Total length of Report descriptor } usb_hid_descriptor_t; COMPILER_PACK_RESET() - //! \name HID Report type - //! Used by SETUP_HID_GET_REPORT & SETUP_HID_SET_REPORT - //! @{ -#define REPORT_TYPE_INPUT 0x01 -#define REPORT_TYPE_OUTPUT 0x02 -#define REPORT_TYPE_FEATURE 0x03 - //! @} +//! \name HID Report type +//! Used by SETUP_HID_GET_REPORT & SETUP_HID_SET_REPORT +//! @{ +#define REPORT_TYPE_INPUT 0x01 +#define REPORT_TYPE_OUTPUT 0x02 +#define REPORT_TYPE_FEATURE 0x03 +//! @} - //! \name Constants of field DESCRIPTOR_HID - //! @{ +//! \name Constants of field DESCRIPTOR_HID +//! @{ //! Numeric expression identifying the HID Class //! Specification release (here V1.11) -#define USB_HID_BDC_V1_11 0x0111 +#define USB_HID_BDC_V1_11 0x0111 //! Numeric expression specifying the number of class descriptors //! Note: Always at least one i.e. Report descriptor. -#define USB_HID_NUM_DESC 0x01 +#define USB_HID_NUM_DESC 0x01 - //! \name Country code - //! @{ -#define USB_HID_NO_COUNTRY_CODE 0 // Not Supported -#define USB_HID_COUNTRY_ARABIC 1 // Arabic -#define USB_HID_COUNTRY_BELGIAN 2 // Belgian -#define USB_HID_COUNTRY_CANADIAN_BILINGUAL 3 // Canadian-Bilingual -#define USB_HID_COUNTRY_CANADIAN_FRENCH 4 // Canadian-French -#define USB_HID_COUNTRY_CZECH_REPUBLIC 5 // Czech Republic -#define USB_HID_COUNTRY_DANISH 6 // Danish -#define USB_HID_COUNTRY_FINNISH 7 // Finnish -#define USB_HID_COUNTRY_FRENCH 8 // French -#define USB_HID_COUNTRY_GERMAN 9 // German -#define USB_HID_COUNTRY_GREEK 10 // Greek -#define USB_HID_COUNTRY_HEBREW 11 // Hebrew -#define USB_HID_COUNTRY_HUNGARY 12 // Hungary -#define USB_HID_COUNTRY_INTERNATIONAL_ISO 13 // International (ISO) -#define USB_HID_COUNTRY_ITALIAN 14 // Italian -#define USB_HID_COUNTRY_JAPAN_KATAKANA 15 // Japan (Katakana) -#define USB_HID_COUNTRY_KOREAN 16 // Korean -#define USB_HID_COUNTRY_LATIN_AMERICAN 17 // Latin American -#define USB_HID_COUNTRY_NETHERLANDS_DUTCH 18 // Netherlands/Dutch -#define USB_HID_COUNTRY_NORWEGIAN 19 // Norwegian -#define USB_HID_COUNTRY_PERSIAN_FARSI 20 // Persian (Farsi) -#define USB_HID_COUNTRY_POLAND 21 // Poland -#define USB_HID_COUNTRY_PORTUGUESE 22 // Portuguese -#define USB_HID_COUNTRY_RUSSIA 23 // Russia -#define USB_HID_COUNTRY_SLOVAKIA 24 // Slovakia -#define USB_HID_COUNTRY_SPANISH 25 // Spanish -#define USB_HID_COUNTRY_SWEDISH 26 // Swedish -#define USB_HID_COUNTRY_SWISS_FRENCH 27 // Swiss/French -#define USB_HID_COUNTRY_SWISS_GERMAN 28 // Swiss/German -#define USB_HID_COUNTRY_SWITZERLAND 29 // Switzerland -#define USB_HID_COUNTRY_TAIWAN 30 // Taiwan -#define USB_HID_COUNTRY_TURKISH_Q 31 // Turkish-Q -#define USB_HID_COUNTRY_UK 32 // UK -#define USB_HID_COUNTRY_US 33 // US -#define USB_HID_COUNTRY_YUGOSLAVIA 34 // Yugoslavia -#define USB_HID_COUNTRY_TURKISH_F 35 // Turkish-F - //! @} - //! @} +//! \name Country code +//! @{ +#define USB_HID_NO_COUNTRY_CODE 0 // Not Supported +#define USB_HID_COUNTRY_ARABIC 1 // Arabic +#define USB_HID_COUNTRY_BELGIAN 2 // Belgian +#define USB_HID_COUNTRY_CANADIAN_BILINGUAL 3 // Canadian-Bilingual +#define USB_HID_COUNTRY_CANADIAN_FRENCH 4 // Canadian-French +#define USB_HID_COUNTRY_CZECH_REPUBLIC 5 // Czech Republic +#define USB_HID_COUNTRY_DANISH 6 // Danish +#define USB_HID_COUNTRY_FINNISH 7 // Finnish +#define USB_HID_COUNTRY_FRENCH 8 // French +#define USB_HID_COUNTRY_GERMAN 9 // German +#define USB_HID_COUNTRY_GREEK 10 // Greek +#define USB_HID_COUNTRY_HEBREW 11 // Hebrew +#define USB_HID_COUNTRY_HUNGARY 12 // Hungary +#define USB_HID_COUNTRY_INTERNATIONAL_ISO 13 // International (ISO) +#define USB_HID_COUNTRY_ITALIAN 14 // Italian +#define USB_HID_COUNTRY_JAPAN_KATAKANA 15 // Japan (Katakana) +#define USB_HID_COUNTRY_KOREAN 16 // Korean +#define USB_HID_COUNTRY_LATIN_AMERICAN 17 // Latin American +#define USB_HID_COUNTRY_NETHERLANDS_DUTCH 18 // Netherlands/Dutch +#define USB_HID_COUNTRY_NORWEGIAN 19 // Norwegian +#define USB_HID_COUNTRY_PERSIAN_FARSI 20 // Persian (Farsi) +#define USB_HID_COUNTRY_POLAND 21 // Poland +#define USB_HID_COUNTRY_PORTUGUESE 22 // Portuguese +#define USB_HID_COUNTRY_RUSSIA 23 // Russia +#define USB_HID_COUNTRY_SLOVAKIA 24 // Slovakia +#define USB_HID_COUNTRY_SPANISH 25 // Spanish +#define USB_HID_COUNTRY_SWEDISH 26 // Swedish +#define USB_HID_COUNTRY_SWISS_FRENCH 27 // Swiss/French +#define USB_HID_COUNTRY_SWISS_GERMAN 28 // Swiss/German +#define USB_HID_COUNTRY_SWITZERLAND 29 // Switzerland +#define USB_HID_COUNTRY_TAIWAN 30 // Taiwan +#define USB_HID_COUNTRY_TURKISH_Q 31 // Turkish-Q +#define USB_HID_COUNTRY_UK 32 // UK +#define USB_HID_COUNTRY_US 33 // US +#define USB_HID_COUNTRY_YUGOSLAVIA 34 // Yugoslavia +#define USB_HID_COUNTRY_TURKISH_F 35 // Turkish-F + //! @} + //! @} //! @} - //! \name HID KEYS values //! @{ -#define HID_A 0x04 -#define HID_B 0x05 -#define HID_C 0x06 -#define HID_D 0x07 -#define HID_E 0x08 -#define HID_F 0x09 -#define HID_G 0x0A -#define HID_H 0x0B -#define HID_I 0x0C -#define HID_J 0x0D -#define HID_K 0x0E -#define HID_L 0x0F -#define HID_M 0x10 -#define HID_N 0x11 -#define HID_O 0x12 -#define HID_P 0x13 -#define HID_Q 0x14 -#define HID_R 0x15 -#define HID_S 0x16 -#define HID_T 0x17 -#define HID_U 0x18 -#define HID_V 0x19 -#define HID_W 0x1A -#define HID_X 0x1B -#define HID_Y 0x1C -#define HID_Z 0x1D -#define HID_1 30 -#define HID_2 31 -#define HID_3 32 -#define HID_4 33 -#define HID_5 34 -#define HID_6 35 -#define HID_7 36 -#define HID_8 37 -#define HID_9 38 -#define HID_0 39 -#define HID_ENTER 40 -#define HID_ESCAPE 41 -#define HID_BACKSPACE 42 -#define HID_TAB 43 -#define HID_SPACEBAR 44 -#define HID_UNDERSCORE 45 -#define HID_PLUS 46 -#define HID_OPEN_BRACKET 47 // { -#define HID_CLOSE_BRACKET 48 // } -#define HID_BACKSLASH 49 -#define HID_ASH 50 // # ~ -#define HID_COLON 51 // ; : -#define HID_QUOTE 52 // ' " -#define HID_TILDE 53 -#define HID_COMMA 54 -#define HID_DOT 55 -#define HID_SLASH 56 -#define HID_CAPS_LOCK 57 -#define HID_F1 58 -#define HID_F2 59 -#define HID_F3 60 -#define HID_F4 61 -#define HID_F5 62 -#define HID_F6 63 -#define HID_F7 64 -#define HID_F8 65 -#define HID_F9 66 -#define HID_F10 67 -#define HID_F11 68 -#define HID_F12 69 -#define HID_PRINTSCREEN 70 -#define HID_SCROLL_LOCK 71 -#define HID_PAUSE 72 -#define HID_INSERT 73 -#define HID_HOME 74 -#define HID_PAGEUP 75 -#define HID_DELETE 76 -#define HID_END 77 -#define HID_PAGEDOWN 78 -#define HID_RIGHT 79 -#define HID_LEFT 80 -#define HID_DOWN 81 -#define HID_UP 82 -#define HID_KEYPAD_NUM_LOCK 83 -#define HID_KEYPAD_DIVIDE 84 -#define HID_KEYPAD_AT 85 -#define HID_KEYPAD_MULTIPLY 85 -#define HID_KEYPAD_MINUS 86 -#define HID_KEYPAD_PLUS 87 -#define HID_KEYPAD_ENTER 88 -#define HID_KEYPAD_1 89 -#define HID_KEYPAD_2 90 -#define HID_KEYPAD_3 91 -#define HID_KEYPAD_4 92 -#define HID_KEYPAD_5 93 -#define HID_KEYPAD_6 94 -#define HID_KEYPAD_7 95 -#define HID_KEYPAD_8 96 -#define HID_KEYPAD_9 97 -#define HID_KEYPAD_0 98 +#define HID_A 0x04 +#define HID_B 0x05 +#define HID_C 0x06 +#define HID_D 0x07 +#define HID_E 0x08 +#define HID_F 0x09 +#define HID_G 0x0A +#define HID_H 0x0B +#define HID_I 0x0C +#define HID_J 0x0D +#define HID_K 0x0E +#define HID_L 0x0F +#define HID_M 0x10 +#define HID_N 0x11 +#define HID_O 0x12 +#define HID_P 0x13 +#define HID_Q 0x14 +#define HID_R 0x15 +#define HID_S 0x16 +#define HID_T 0x17 +#define HID_U 0x18 +#define HID_V 0x19 +#define HID_W 0x1A +#define HID_X 0x1B +#define HID_Y 0x1C +#define HID_Z 0x1D +#define HID_1 30 +#define HID_2 31 +#define HID_3 32 +#define HID_4 33 +#define HID_5 34 +#define HID_6 35 +#define HID_7 36 +#define HID_8 37 +#define HID_9 38 +#define HID_0 39 +#define HID_ENTER 40 +#define HID_ESCAPE 41 +#define HID_BACKSPACE 42 +#define HID_TAB 43 +#define HID_SPACEBAR 44 +#define HID_UNDERSCORE 45 +#define HID_PLUS 46 +#define HID_OPEN_BRACKET 47 // { +#define HID_CLOSE_BRACKET 48 // } +#define HID_BACKSLASH 49 +#define HID_ASH 50 // # ~ +#define HID_COLON 51 // ; : +#define HID_QUOTE 52 // ' " +#define HID_TILDE 53 +#define HID_COMMA 54 +#define HID_DOT 55 +#define HID_SLASH 56 +#define HID_CAPS_LOCK 57 +#define HID_F1 58 +#define HID_F2 59 +#define HID_F3 60 +#define HID_F4 61 +#define HID_F5 62 +#define HID_F6 63 +#define HID_F7 64 +#define HID_F8 65 +#define HID_F9 66 +#define HID_F10 67 +#define HID_F11 68 +#define HID_F12 69 +#define HID_PRINTSCREEN 70 +#define HID_SCROLL_LOCK 71 +#define HID_PAUSE 72 +#define HID_INSERT 73 +#define HID_HOME 74 +#define HID_PAGEUP 75 +#define HID_DELETE 76 +#define HID_END 77 +#define HID_PAGEDOWN 78 +#define HID_RIGHT 79 +#define HID_LEFT 80 +#define HID_DOWN 81 +#define HID_UP 82 +#define HID_KEYPAD_NUM_LOCK 83 +#define HID_KEYPAD_DIVIDE 84 +#define HID_KEYPAD_AT 85 +#define HID_KEYPAD_MULTIPLY 85 +#define HID_KEYPAD_MINUS 86 +#define HID_KEYPAD_PLUS 87 +#define HID_KEYPAD_ENTER 88 +#define HID_KEYPAD_1 89 +#define HID_KEYPAD_2 90 +#define HID_KEYPAD_3 91 +#define HID_KEYPAD_4 92 +#define HID_KEYPAD_5 93 +#define HID_KEYPAD_6 94 +#define HID_KEYPAD_7 95 +#define HID_KEYPAD_8 96 +#define HID_KEYPAD_9 97 +#define HID_KEYPAD_0 98 - //! \name HID modifier values - //! @{ -#define HID_MODIFIER_NONE 0x00 -#define HID_MODIFIER_LEFT_CTRL 0x01 -#define HID_MODIFIER_LEFT_SHIFT 0x02 -#define HID_MODIFIER_LEFT_ALT 0x04 -#define HID_MODIFIER_LEFT_UI 0x08 -#define HID_MODIFIER_RIGHT_CTRL 0x10 -#define HID_MODIFIER_RIGHT_SHIFT 0x20 -#define HID_MODIFIER_RIGHT_ALT 0x40 -#define HID_MODIFIER_RIGHT_UI 0x80 - //! @} +//! \name HID modifier values +//! @{ +#define HID_MODIFIER_NONE 0x00 +#define HID_MODIFIER_LEFT_CTRL 0x01 +#define HID_MODIFIER_LEFT_SHIFT 0x02 +#define HID_MODIFIER_LEFT_ALT 0x04 +#define HID_MODIFIER_LEFT_UI 0x08 +#define HID_MODIFIER_RIGHT_CTRL 0x10 +#define HID_MODIFIER_RIGHT_SHIFT 0x20 +#define HID_MODIFIER_RIGHT_ALT 0x40 +#define HID_MODIFIER_RIGHT_UI 0x80 +//! @} //! @} //! \name HID KEYS values //! @{ -#define HID_LED_NUM_LOCK (1<<0) -#define HID_LED_CAPS_LOCK (1<<1) -#define HID_LED_SCROLL_LOCK (1<<2) -#define HID_LED_COMPOSE (1<<3) -#define HID_LED_KANA (1<<4) +#define HID_LED_NUM_LOCK (1 << 0) +#define HID_LED_CAPS_LOCK (1 << 1) +#define HID_LED_SCROLL_LOCK (1 << 2) +#define HID_LED_COMPOSE (1 << 3) +#define HID_LED_KANA (1 << 4) //! @} -#endif // _USB_PROTOCOL_HID_H_ +#endif // _USB_PROTOCOL_HID_H_ diff --git a/tmk_core/protocol/arm_atsam/usb/usb_util.c b/tmk_core/protocol/arm_atsam/usb/usb_util.c index 58b349362..a682c00b5 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_util.c +++ b/tmk_core/protocol/arm_atsam/usb/usb_util.c @@ -2,58 +2,42 @@ #include "string.h" #include "usb_util.h" -char digit(int d, int radix) -{ - if (d < 10) - { +char digit(int d, int radix) { + if (d < 10) { return d + '0'; - } - else - { + } else { return d - 10 + 'A'; } } -int UTIL_ltoa_radix(int64_t value, char *dest, int radix) -{ - int64_t original = value; //save original value - char buf[25] = ""; - int c = sizeof(buf)-1; - int last = c; - int d; - int size; +int UTIL_ltoa_radix(int64_t value, char *dest, int radix) { + int64_t original = value; // save original value + char buf[25] = ""; + int c = sizeof(buf) - 1; + int last = c; + int d; + int size; - if (value < 0) //if it's negative, take the absolute value + if (value < 0) // if it's negative, take the absolute value value = -value; - do //write least significant digit of value that's left + do // write least significant digit of value that's left { - d = (value % radix); + d = (value % radix); buf[--c] = digit(d, radix); value /= radix; } while (value); - if (original < 0) - buf[--c] = '-'; + if (original < 0) buf[--c] = '-'; - size = last - c + 1; //includes null at end + size = last - c + 1; // includes null at end memcpy(dest, &buf[c], last - c + 1); - return (size - 1); //without null termination + return (size - 1); // without null termination } -int UTIL_ltoa(int64_t value, char *dest) -{ - return UTIL_ltoa_radix(value, dest, 10); -} +int UTIL_ltoa(int64_t value, char *dest) { return UTIL_ltoa_radix(value, dest, 10); } -int UTIL_itoa(int value, char *dest) -{ - return UTIL_ltoa_radix((int64_t)value, dest, 10); -} - -int UTIL_utoa(uint32_t value, char *dest) -{ - return UTIL_ltoa_radix((int64_t)value, dest, 10); -} +int UTIL_itoa(int value, char *dest) { return UTIL_ltoa_radix((int64_t)value, dest, 10); } +int UTIL_utoa(uint32_t value, char *dest) { return UTIL_ltoa_radix((int64_t)value, dest, 10); } diff --git a/tmk_core/protocol/arm_atsam/usb/usb_util.h b/tmk_core/protocol/arm_atsam/usb/usb_util.h index 2134d5d27..3bad2c26f 100644 --- a/tmk_core/protocol/arm_atsam/usb/usb_util.h +++ b/tmk_core/protocol/arm_atsam/usb/usb_util.h @@ -6,5 +6,4 @@ int UTIL_ltoa(int64_t value, char *dest); int UTIL_itoa(int value, char *dest); int UTIL_utoa(uint32_t value, char *dest); -#endif //_USB_UTIL_H_ - +#endif //_USB_UTIL_H_ diff --git a/tmk_core/protocol/arm_atsam/wait_api.h b/tmk_core/protocol/arm_atsam/wait_api.h index 424fbb53b..b3918e534 100644 --- a/tmk_core/protocol/arm_atsam/wait_api.h +++ b/tmk_core/protocol/arm_atsam/wait_api.h @@ -5,4 +5,3 @@ void wait_ms(uint64_t msec); void wait_us(uint16_t usec); #endif - diff --git a/tmk_core/protocol/bluefruit/bluefruit.c b/tmk_core/protocol/bluefruit/bluefruit.c index 47c63555c..fb001855e 100644 --- a/tmk_core/protocol/bluefruit/bluefruit.c +++ b/tmk_core/protocol/bluefruit/bluefruit.c @@ -29,31 +29,31 @@ static uint8_t bluefruit_keyboard_leds = 0; static void bluefruit_serial_send(uint8_t); -void bluefruit_keyboard_print_report(report_keyboard_t *report) -{ +void bluefruit_keyboard_print_report(report_keyboard_t *report) { if (!debug_keyboard) return; - dprintf("keys: "); for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { debug_hex8(report->keys[i]); dprintf(" "); } - dprintf(" mods: "); debug_hex8(report->mods); - dprintf(" reserved: "); debug_hex8(report->reserved); + dprintf("keys: "); + for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + debug_hex8(report->keys[i]); + dprintf(" "); + } + dprintf(" mods: "); + debug_hex8(report->mods); + dprintf(" reserved: "); + debug_hex8(report->reserved); dprintf("\n"); } #ifdef BLUEFRUIT_TRACE_SERIAL -static void bluefruit_trace_header() -{ +static void bluefruit_trace_header() { dprintf("+------------------------------------+\n"); dprintf("| HID report to Bluefruit via serial |\n"); dprintf("+------------------------------------+\n|"); } -static void bluefruit_trace_footer() -{ - dprintf("|\n+------------------------------------+\n\n"); -} +static void bluefruit_trace_footer() { dprintf("|\n+------------------------------------+\n\n"); } #endif -static void bluefruit_serial_send(uint8_t data) -{ +static void bluefruit_serial_send(uint8_t data) { #ifdef BLUEFRUIT_TRACE_SERIAL dprintf(" "); debug_hex8(data); @@ -67,53 +67,38 @@ static void bluefruit_serial_send(uint8_t data) *------------------------------------------------------------------*/ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); - +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); void sendString(char string[], int length) { - for(int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) { serial_send(string[i]); } } -static host_driver_t driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; - -host_driver_t *bluefruit_driver(void) -{ - return &driver; -} +static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -static uint8_t keyboard_leds(void) { - return bluefruit_keyboard_leds; -} +host_driver_t *bluefruit_driver(void) { return &driver; } + +static uint8_t keyboard_leds(void) { return bluefruit_keyboard_leds; } -static void send_keyboard(report_keyboard_t *report) -{ -#ifdef BLUEFRUIT_TRACE_SERIAL +static void send_keyboard(report_keyboard_t *report) { +#ifdef BLUEFRUIT_TRACE_SERIAL bluefruit_trace_header(); #endif bluefruit_serial_send(0xFD); for (uint8_t i = 0; i < KEYBOARD_REPORT_SIZE; i++) { - bluefruit_serial_send(report->raw[i]); } -#ifdef BLUEFRUIT_TRACE_SERIAL - bluefruit_trace_footer(); +#ifdef BLUEFRUIT_TRACE_SERIAL + bluefruit_trace_footer(); #endif } -static void send_mouse(report_mouse_t *report) -{ -#ifdef BLUEFRUIT_TRACE_SERIAL +static void send_mouse(report_mouse_t *report) { +#ifdef BLUEFRUIT_TRACE_SERIAL bluefruit_trace_header(); #endif bluefruit_serial_send(0xFD); @@ -122,17 +107,15 @@ static void send_mouse(report_mouse_t *report) bluefruit_serial_send(report->buttons); bluefruit_serial_send(report->x); bluefruit_serial_send(report->y); - bluefruit_serial_send(report->v); // should try sending the wheel v here - bluefruit_serial_send(report->h); // should try sending the wheel h here + bluefruit_serial_send(report->v); // should try sending the wheel v here + bluefruit_serial_send(report->h); // should try sending the wheel h here bluefruit_serial_send(0x00); #ifdef BLUEFRUIT_TRACE_SERIAL bluefruit_trace_footer(); #endif } -static void send_system(uint16_t data) -{ -} +static void send_system(uint16_t data) {} /* +-----------------+-------------------+-------+ @@ -153,48 +136,28 @@ static void send_system(uint16_t data) | Stop | 00000000 00010000 | 00 10 | +-------------------------------------+-------+ */ -#define CONSUMER2BLUEFRUIT(usage) \ - (usage == AUDIO_MUTE ? 0x0000 : \ - (usage == AUDIO_VOL_UP ? 0x1000 : \ - (usage == AUDIO_VOL_DOWN ? 0x2000 : \ - (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \ - (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \ - (usage == TRANSPORT_STOP ? 0x0010 : \ - (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \ - (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \ - (usage == AL_CC_CONFIG ? 0x0000 : \ - (usage == AL_EMAIL ? 0x0000 : \ - (usage == AL_CALCULATOR ? 0x0000 : \ - (usage == AL_LOCAL_BROWSER ? 0x0000 : \ - (usage == AC_SEARCH ? 0x0400 : \ - (usage == AC_HOME ? 0x0100 : \ - (usage == AC_BACK ? 0x0000 : \ - (usage == AC_FORWARD ? 0x0000 : \ - (usage == AC_STOP ? 0x0000 : \ - (usage == AC_REFRESH ? 0x0000 : \ - (usage == AC_BOOKMARKS ? 0x0000 : 0))))))))))))))))))) - -static void send_consumer(uint16_t data) -{ +#define CONSUMER2BLUEFRUIT(usage) (usage == AUDIO_MUTE ? 0x0000 : (usage == AUDIO_VOL_UP ? 0x1000 : (usage == AUDIO_VOL_DOWN ? 0x2000 : (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : (usage == TRANSPORT_PREV_TRACK ? 0x0004 : (usage == TRANSPORT_STOP ? 0x0010 : (usage == TRANSPORT_STOP_EJECT ? 0x0000 : (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : (usage == AL_CC_CONFIG ? 0x0000 : (usage == AL_EMAIL ? 0x0000 : (usage == AL_CALCULATOR ? 0x0000 : (usage == AL_LOCAL_BROWSER ? 0x0000 : (usage == AC_SEARCH ? 0x0400 : (usage == AC_HOME ? 0x0100 : (usage == AC_BACK ? 0x0000 : (usage == AC_FORWARD ? 0x0000 : (usage == AC_STOP ? 0x0000 : (usage == AC_REFRESH ? 0x0000 : (usage == AC_BOOKMARKS ? 0x0000 : 0))))))))))))))))))) + +static void send_consumer(uint16_t data) { static uint16_t last_data = 0; if (data == last_data) return; last_data = data; - + uint16_t bitmap = CONSUMER2BLUEFRUIT(data); - -#ifdef BLUEFRUIT_TRACE_SERIAL - dprintf("\nData: "); - debug_hex16(data); - dprintf("; bitmap: "); - debug_hex16(bitmap); + +#ifdef BLUEFRUIT_TRACE_SERIAL + dprintf("\nData: "); + debug_hex16(data); + dprintf("; bitmap: "); + debug_hex16(bitmap); dprintf("\n"); bluefruit_trace_header(); #endif bluefruit_serial_send(0xFD); bluefruit_serial_send(0x00); bluefruit_serial_send(0x02); - bluefruit_serial_send((bitmap>>8)&0xFF); - bluefruit_serial_send(bitmap&0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); + bluefruit_serial_send(bitmap & 0xFF); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); diff --git a/tmk_core/protocol/bluefruit/bluefruit.h b/tmk_core/protocol/bluefruit/bluefruit.h index ceacc4a36..7b636abb9 100644 --- a/tmk_core/protocol/bluefruit/bluefruit.h +++ b/tmk_core/protocol/bluefruit/bluefruit.h @@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "host_driver.h" - host_driver_t *bluefruit_driver(void); #endif
\ No newline at end of file diff --git a/tmk_core/protocol/bluefruit/main.c b/tmk_core/protocol/bluefruit/main.c index 8a6386b4e..3adcab4f4 100644 --- a/tmk_core/protocol/bluefruit/main.c +++ b/tmk_core/protocol/bluefruit/main.c @@ -34,16 +34,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "bluefruit.h" #include "pjrc.h" -#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) +#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) -#define HOST_DRIVER_NOT_SET 0 -#define BLUEFRUIT_HOST_DRIVER 1 -#define PJRC_HOST_DRIVER 2 - - -int main(void) -{ +#define HOST_DRIVER_NOT_SET 0 +#define BLUEFRUIT_HOST_DRIVER 1 +#define PJRC_HOST_DRIVER 2 +int main(void) { CPU_PRESCALE(0); // DDRD = _BV(PD5); @@ -58,7 +55,6 @@ int main(void) // _delay_ms(2000); // while (!usb_configured()) /* wait */ - keyboard_setup(); dprintf("Initializing keyboard...\n"); @@ -76,11 +72,11 @@ int main(void) // DDRB = _BV(PB6); // PORTB |= _BV(PB6); - dprintf("Setting host driver to bluefruit...\n"); - host_set_driver(bluefruit_driver()); + dprintf("Setting host driver to bluefruit...\n"); + host_set_driver(bluefruit_driver()); - dprintf("Initializing serial...\n"); - serial_init(); + dprintf("Initializing serial...\n"); + serial_init(); // char swpa[] = "+++\r\n"; // for (int i = 0; i < 5; i++) { @@ -101,39 +97,38 @@ int main(void) // serial_send(swpa[i]); // } - // wait an extra second for the PC's operating system - // to load drivers and do whatever it does to actually - // be ready for input - _delay_ms(1000); - // PORTD = ~_BV(PD5); - dprintf("Starting main loop"); - while (1) { - keyboard_task(); - } - -// } else { - -// // I'm not smart enough to get this done with LUFA - BCG -// dprintf("Setting host driver to PJRC...\n"); -// host_set_driver(pjrc_driver()); -// #ifdef SLEEP_LED_ENABLE -// sleep_led_init(); -// #endif -// // wait an extra second for the PC's operating system -// // to load drivers and do whatever it does to actually -// // be ready for input -// _delay_ms(1000); -// PORTB = ~_BV(PB0); -// dprintf("Starting main loop"); -// while (1) { -// while (suspend) { -// suspend_power_down(); -// if (remote_wakeup && suspend_wakeup_condition()) { -// usb_remote_wakeup(); -// } -// } -// keyboard_task(); -// } -// } - + // wait an extra second for the PC's operating system + // to load drivers and do whatever it does to actually + // be ready for input + _delay_ms(1000); + // PORTD = ~_BV(PD5); + dprintf("Starting main loop"); + while (1) { + keyboard_task(); + } + + // } else { + + // // I'm not smart enough to get this done with LUFA - BCG + // dprintf("Setting host driver to PJRC...\n"); + // host_set_driver(pjrc_driver()); + // #ifdef SLEEP_LED_ENABLE + // sleep_led_init(); + // #endif + // // wait an extra second for the PC's operating system + // // to load drivers and do whatever it does to actually + // // be ready for input + // _delay_ms(1000); + // PORTB = ~_BV(PB0); + // dprintf("Starting main loop"); + // while (1) { + // while (suspend) { + // suspend_power_down(); + // if (remote_wakeup && suspend_wakeup_condition()) { + // usb_remote_wakeup(); + // } + // } + // keyboard_task(); + // } + // } } diff --git a/tmk_core/protocol/chibios/lufa_utils/LUFA/Drivers/USB/USB.h b/tmk_core/protocol/chibios/lufa_utils/LUFA/Drivers/USB/USB.h index a5374d820..f59c8d1dd 100644 --- a/tmk_core/protocol/chibios/lufa_utils/LUFA/Drivers/USB/USB.h +++ b/tmk_core/protocol/chibios/lufa_utils/LUFA/Drivers/USB/USB.h @@ -2,7 +2,7 @@ #include "stddef.h" #include "inttypes.h" -#define ATTR_PACKED __attribute__ ((packed)) +#define ATTR_PACKED __attribute__((packed)) /** Concatenates the given input into a single token, via the C Preprocessor. * * \param[in] x First item to concatenate. @@ -10,7 +10,7 @@ * * \return Concatenated version of the input. */ -#define CONCAT(x, y) x ## y +#define CONCAT(x, y) x##y /** CConcatenates the given input into a single token after macro expansion, via the C Preprocessor. * @@ -19,8 +19,8 @@ * * \return Concatenated version of the expanded input. */ -#define CONCAT_EXPANDED(x, y) CONCAT(x, y) -#define CPU_TO_LE16(x) (x) +#define CONCAT_EXPANDED(x, y) CONCAT(x, y) +#define CPU_TO_LE16(x) (x) // We don't need anything from the following files, or we have defined it already #define __LUFA_COMMON_H__ diff --git a/tmk_core/protocol/chibios/main.c b/tmk_core/protocol/chibios/main.c index 8de55bfe3..de2b493b8 100644 --- a/tmk_core/protocol/chibios/main.c +++ b/tmk_core/protocol/chibios/main.c @@ -33,19 +33,19 @@ #include "debug.h" #include "printf.h" #ifdef SLEEP_LED_ENABLE -#include "sleep_led.h" +# include "sleep_led.h" #endif #ifdef SERIAL_LINK_ENABLE -#include "serial_link/system/serial_link.h" +# include "serial_link/system/serial_link.h" #endif #ifdef VISUALIZER_ENABLE -#include "visualizer/visualizer.h" +# include "visualizer/visualizer.h" #endif #ifdef MIDI_ENABLE -#include "qmk_midi.h" +# include "qmk_midi.h" #endif #ifdef STM32_EEPROM_ENABLE -#include "eeprom_stm32.h" +# include "eeprom_stm32.h" #endif #include "suspend.h" #include "wait.h" @@ -57,19 +57,13 @@ /* declarations */ uint8_t keyboard_leds(void); -void send_keyboard(report_keyboard_t *report); -void send_mouse(report_mouse_t *report); -void send_system(uint16_t data); -void send_consumer(uint16_t data); +void send_keyboard(report_keyboard_t *report); +void send_mouse(report_mouse_t *report); +void send_system(uint16_t data); +void send_consumer(uint16_t data); /* host struct */ -host_driver_t chibios_driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; +host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; #ifdef VIRTSER_ENABLE void virtser_task(void); @@ -103,127 +97,123 @@ void console_task(void); // } // } - - /* Main thread */ int main(void) { - /* ChibiOS/RT init */ - halInit(); - chSysInit(); + /* ChibiOS/RT init */ + halInit(); + chSysInit(); #ifdef STM32_EEPROM_ENABLE - EEPROM_Init(); + EEPROM_Init(); #endif - // TESTING - // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); + // TESTING + // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); - keyboard_setup(); + keyboard_setup(); - /* Init USB */ - init_usb_driver(&USB_DRIVER); + /* Init USB */ + init_usb_driver(&USB_DRIVER); - /* init printf */ - init_printf(NULL,sendchar_pf); + /* init printf */ + init_printf(NULL, sendchar_pf); #ifdef MIDI_ENABLE - setup_midi(); + setup_midi(); #endif #ifdef SERIAL_LINK_ENABLE - init_serial_link(); + init_serial_link(); #endif #ifdef VISUALIZER_ENABLE - visualizer_init(); + visualizer_init(); #endif + host_driver_t *driver = NULL; - host_driver_t* driver = NULL; - - /* Wait until the USB or serial link is active */ - while (true) { + /* Wait until the USB or serial link is active */ + while (true) { #if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE) - if(USB_DRIVER.state == USB_ACTIVE) { - driver = &chibios_driver; - break; - } + if (USB_DRIVER.state == USB_ACTIVE) { + driver = &chibios_driver; + break; + } #else - driver = &chibios_driver; - break; + driver = &chibios_driver; + break; #endif #ifdef SERIAL_LINK_ENABLE - if(is_serial_link_connected()) { - driver = get_serial_link_driver(); - break; - } - serial_link_update(); + if (is_serial_link_connected()) { + driver = get_serial_link_driver(); + break; + } + serial_link_update(); #endif - wait_ms(50); - } + wait_ms(50); + } - /* Do need to wait here! - * Otherwise the next print might start a transfer on console EP - * before the USB is completely ready, which sometimes causes - * HardFaults. - */ - wait_ms(50); + /* Do need to wait here! + * Otherwise the next print might start a transfer on console EP + * before the USB is completely ready, which sometimes causes + * HardFaults. + */ + wait_ms(50); - print("USB configured.\n"); + print("USB configured.\n"); - /* init TMK modules */ - keyboard_init(); - host_set_driver(driver); + /* init TMK modules */ + keyboard_init(); + host_set_driver(driver); #ifdef SLEEP_LED_ENABLE - sleep_led_init(); + sleep_led_init(); #endif - print("Keyboard start.\n"); - - /* Main loop */ - while(true) { + print("Keyboard start.\n"); + /* Main loop */ + while (true) { #if !defined(NO_USB_STARTUP_CHECK) - if(USB_DRIVER.state == USB_SUSPENDED) { - print("[s]"); -#ifdef VISUALIZER_ENABLE - visualizer_suspend(); -#endif - while(USB_DRIVER.state == USB_SUSPENDED) { - /* Do this in the suspended state */ -#ifdef SERIAL_LINK_ENABLE - serial_link_update(); -#endif - suspend_power_down(); // on AVR this deep sleeps for 15ms - /* Remote wakeup */ - if(suspend_wakeup_condition()) { - usbWakeupHost(&USB_DRIVER); + if (USB_DRIVER.state == USB_SUSPENDED) { + print("[s]"); +# ifdef VISUALIZER_ENABLE + visualizer_suspend(); +# endif + while (USB_DRIVER.state == USB_SUSPENDED) { + /* Do this in the suspended state */ +# ifdef SERIAL_LINK_ENABLE + serial_link_update(); +# endif + suspend_power_down(); // on AVR this deep sleeps for 15ms + /* Remote wakeup */ + if (suspend_wakeup_condition()) { + usbWakeupHost(&USB_DRIVER); + } + } + /* Woken up */ + // variables has been already cleared by the wakeup hook + send_keyboard_report(); +# ifdef MOUSEKEY_ENABLE + mousekey_send(); +# endif /* MOUSEKEY_ENABLE */ + +# ifdef VISUALIZER_ENABLE + visualizer_resume(); +# endif } - } - /* Woken up */ - // variables has been already cleared by the wakeup hook - send_keyboard_report(); -#ifdef MOUSEKEY_ENABLE - mousekey_send(); -#endif /* MOUSEKEY_ENABLE */ - -#ifdef VISUALIZER_ENABLE - visualizer_resume(); -#endif - } #endif - keyboard_task(); + keyboard_task(); #ifdef CONSOLE_ENABLE - console_task(); + console_task(); #endif #ifdef VIRTSER_ENABLE - virtser_task(); + virtser_task(); #endif #ifdef RAW_ENABLE - raw_hid_task(); + raw_hid_task(); #endif - } + } } diff --git a/tmk_core/protocol/chibios/usb_driver.c b/tmk_core/protocol/chibios/usb_driver.c index fe535eeb3..28a8c6663 100644 --- a/tmk_core/protocol/chibios/usb_driver.c +++ b/tmk_core/protocol/chibios/usb_driver.c @@ -41,93 +41,62 @@ /* * Current Line Coding. */ -static cdc_linecoding_t linecoding = { - {0x00, 0x96, 0x00, 0x00}, /* 38400. */ - LC_STOP_1, LC_PARITY_NONE, 8 -}; +static cdc_linecoding_t linecoding = {{0x00, 0x96, 0x00, 0x00}, /* 38400. */ + LC_STOP_1, + LC_PARITY_NONE, + 8}; /*===========================================================================*/ /* Driver local functions. */ /*===========================================================================*/ static bool qmkusb_start_receive(QMKUSBDriver *qmkusbp) { - uint8_t *buf; - - /* If the USB driver is not in the appropriate state then transactions - must not be started.*/ - if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || - (qmkusbp->state != QMKUSB_READY)) { - return true; - } - - /* Checking if there is already a transaction ongoing on the endpoint.*/ - if (usbGetReceiveStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { - return true; - } - - /* Checking if there is a buffer ready for incoming data.*/ - buf = ibqGetEmptyBufferI(&qmkusbp->ibqueue); - if (buf == NULL) { - return true; - } - - /* Buffer found, starting a new transaction.*/ - usbStartReceiveI(qmkusbp->config->usbp, qmkusbp->config->bulk_out, - buf, qmkusbp->ibqueue.bsize - sizeof(size_t)); - - return false; -} - -/* - * Interface implementation. - */ - -static size_t _write(void *ip, const uint8_t *bp, size_t n) { + uint8_t *buf; - return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, - n, TIME_INFINITE); -} + /* If the USB driver is not in the appropriate state then transactions + must not be started.*/ + if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) { + return true; + } -static size_t _read(void *ip, uint8_t *bp, size_t n) { + /* Checking if there is already a transaction ongoing on the endpoint.*/ + if (usbGetReceiveStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { + return true; + } - return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, - n, TIME_INFINITE); -} + /* Checking if there is a buffer ready for incoming data.*/ + buf = ibqGetEmptyBufferI(&qmkusbp->ibqueue); + if (buf == NULL) { + return true; + } -static msg_t _put(void *ip, uint8_t b) { + /* Buffer found, starting a new transaction.*/ + usbStartReceiveI(qmkusbp->config->usbp, qmkusbp->config->bulk_out, buf, qmkusbp->ibqueue.bsize - sizeof(size_t)); - return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, TIME_INFINITE); + return false; } -static msg_t _get(void *ip) { - - return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, TIME_INFINITE); -} +/* + * Interface implementation. + */ -static msg_t _putt(void *ip, uint8_t b, systime_t timeout) { +static size_t _write(void *ip, const uint8_t *bp, size_t n) { return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, TIME_INFINITE); } - return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, timeout); -} +static size_t _read(void *ip, uint8_t *bp, size_t n) { return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, TIME_INFINITE); } -static msg_t _gett(void *ip, systime_t timeout) { +static msg_t _put(void *ip, uint8_t b) { return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, TIME_INFINITE); } - return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, timeout); -} +static msg_t _get(void *ip) { return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, TIME_INFINITE); } -static size_t _writet(void *ip, const uint8_t *bp, size_t n, systime_t timeout) { +static msg_t _putt(void *ip, uint8_t b, systime_t timeout) { return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, timeout); } - return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, timeout); -} +static msg_t _gett(void *ip, systime_t timeout) { return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, timeout); } -static size_t _readt(void *ip, uint8_t *bp, size_t n, systime_t timeout) { +static size_t _writet(void *ip, const uint8_t *bp, size_t n, systime_t timeout) { return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, timeout); } - return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, timeout); -} +static size_t _readt(void *ip, uint8_t *bp, size_t n, systime_t timeout) { return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, timeout); } -static const struct QMKUSBDriverVMT vmt = { - _write, _read, _put, _get, - _putt, _gett, _writet, _readt -}; +static const struct QMKUSBDriverVMT vmt = {_write, _read, _put, _get, _putt, _gett, _writet, _readt}; /** * @brief Notification of empty buffer released into the input buffers queue. @@ -135,8 +104,8 @@ static const struct QMKUSBDriverVMT vmt = { * @param[in] bqp the buffers queue pointer. */ static void ibnotify(io_buffers_queue_t *bqp) { - QMKUSBDriver *qmkusbp = bqGetLinkX(bqp); - (void) qmkusb_start_receive(qmkusbp); + QMKUSBDriver *qmkusbp = bqGetLinkX(bqp); + (void)qmkusb_start_receive(qmkusbp); } /** @@ -145,25 +114,24 @@ static void ibnotify(io_buffers_queue_t *bqp) { * @param[in] bqp the buffers queue pointer. */ static void obnotify(io_buffers_queue_t *bqp) { - size_t n; - QMKUSBDriver *qmkusbp = bqGetLinkX(bqp); - - /* If the USB driver is not in the appropriate state then transactions - must not be started.*/ - if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || - (qmkusbp->state != QMKUSB_READY)) { - return; - } - - /* Checking if there is already a transaction ongoing on the endpoint.*/ - if (!usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { - /* Trying to get a full buffer.*/ - uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); - if (buf != NULL) { - /* Buffer found, starting a new transaction.*/ - usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n); + size_t n; + QMKUSBDriver *qmkusbp = bqGetLinkX(bqp); + + /* If the USB driver is not in the appropriate state then transactions + must not be started.*/ + if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) { + return; + } + + /* Checking if there is already a transaction ongoing on the endpoint.*/ + if (!usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { + /* Trying to get a full buffer.*/ + uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); + if (buf != NULL) { + /* Buffer found, starting a new transaction.*/ + usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n); + } } - } } /*===========================================================================*/ @@ -177,8 +145,7 @@ static void obnotify(io_buffers_queue_t *bqp) { * * @init */ -void qmkusbInit(void) { -} +void qmkusbInit(void) {} /** * @brief Initializes a generic full duplex driver object. @@ -190,17 +157,12 @@ void qmkusbInit(void) { * @init */ void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) { - - qmkusbp->vmt = &vmt; - osalEventObjectInit(&qmkusbp->event); - qmkusbp->state = QMKUSB_STOP; - // Note that the config uses the USB direction naming - ibqObjectInit(&qmkusbp->ibqueue, true, config->ob, - config->out_size, config->out_buffers, - ibnotify, qmkusbp); - obqObjectInit(&qmkusbp->obqueue, true, config->ib, - config->in_size, config->in_buffers, - obnotify, qmkusbp); + qmkusbp->vmt = &vmt; + osalEventObjectInit(&qmkusbp->event); + qmkusbp->state = QMKUSB_STOP; + // Note that the config uses the USB direction naming + ibqObjectInit(&qmkusbp->ibqueue, true, config->ob, config->out_size, config->out_buffers, ibnotify, qmkusbp); + obqObjectInit(&qmkusbp->obqueue, true, config->ib, config->in_size, config->in_buffers, obnotify, qmkusbp); } /** @@ -212,21 +174,20 @@ void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) { * @api */ void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) { - USBDriver *usbp = config->usbp; - - osalDbgCheck(qmkusbp != NULL); - - osalSysLock(); - osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), - "invalid state"); - usbp->in_params[config->bulk_in - 1U] = qmkusbp; - usbp->out_params[config->bulk_out - 1U] = qmkusbp; - if (config->int_in > 0U) { - usbp->in_params[config->int_in - 1U] = qmkusbp; - } - qmkusbp->config = config; - qmkusbp->state = QMKUSB_READY; - osalSysUnlock(); + USBDriver *usbp = config->usbp; + + osalDbgCheck(qmkusbp != NULL); + + osalSysLock(); + osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), "invalid state"); + usbp->in_params[config->bulk_in - 1U] = qmkusbp; + usbp->out_params[config->bulk_out - 1U] = qmkusbp; + if (config->int_in > 0U) { + usbp->in_params[config->int_in - 1U] = qmkusbp; + } + qmkusbp->config = config; + qmkusbp->state = QMKUSB_READY; + osalSysUnlock(); } /** @@ -239,31 +200,30 @@ void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) { * @api */ void qmkusbStop(QMKUSBDriver *qmkusbp) { - USBDriver *usbp = qmkusbp->config->usbp; + USBDriver *usbp = qmkusbp->config->usbp; - osalDbgCheck(qmkusbp != NULL); + osalDbgCheck(qmkusbp != NULL); - osalSysLock(); + osalSysLock(); - osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), - "invalid state"); + osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), "invalid state"); - /* Driver in stopped state.*/ - usbp->in_params[qmkusbp->config->bulk_in - 1U] = NULL; - usbp->out_params[qmkusbp->config->bulk_out - 1U] = NULL; - if (qmkusbp->config->int_in > 0U) { - usbp->in_params[qmkusbp->config->int_in - 1U] = NULL; - } - qmkusbp->config = NULL; - qmkusbp->state = QMKUSB_STOP; + /* Driver in stopped state.*/ + usbp->in_params[qmkusbp->config->bulk_in - 1U] = NULL; + usbp->out_params[qmkusbp->config->bulk_out - 1U] = NULL; + if (qmkusbp->config->int_in > 0U) { + usbp->in_params[qmkusbp->config->int_in - 1U] = NULL; + } + qmkusbp->config = NULL; + qmkusbp->state = QMKUSB_STOP; - /* Enforces a disconnection.*/ - chnAddFlagsI(qmkusbp, CHN_DISCONNECTED); - ibqResetI(&qmkusbp->ibqueue); - obqResetI(&qmkusbp->obqueue); - osalOsRescheduleS(); + /* Enforces a disconnection.*/ + chnAddFlagsI(qmkusbp, CHN_DISCONNECTED); + ibqResetI(&qmkusbp->ibqueue); + obqResetI(&qmkusbp->obqueue); + osalOsRescheduleS(); - osalSysUnlock(); + osalSysUnlock(); } /** @@ -279,10 +239,9 @@ void qmkusbStop(QMKUSBDriver *qmkusbp) { * @iclass */ void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp) { - - chnAddFlagsI(qmkusbp, CHN_DISCONNECTED); - bqSuspendI(&qmkusbp->ibqueue); - bqSuspendI(&qmkusbp->obqueue); + chnAddFlagsI(qmkusbp, CHN_DISCONNECTED); + bqSuspendI(&qmkusbp->ibqueue); + bqSuspendI(&qmkusbp->obqueue); } /** @@ -298,10 +257,9 @@ void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp) { * @iclass */ void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp) { - - chnAddFlagsI(qmkusbp, CHN_CONNECTED); - bqResumeX(&qmkusbp->ibqueue); - bqResumeX(&qmkusbp->obqueue); + chnAddFlagsI(qmkusbp, CHN_CONNECTED); + bqResumeX(&qmkusbp->ibqueue); + bqResumeX(&qmkusbp->obqueue); } /** @@ -312,13 +270,12 @@ void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp) { * @iclass */ void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp) { - - ibqResetI(&qmkusbp->ibqueue); - bqResumeX(&qmkusbp->ibqueue); - obqResetI(&qmkusbp->obqueue); - bqResumeX(&qmkusbp->obqueue); - chnAddFlagsI(qmkusbp, CHN_CONNECTED); - (void) qmkusb_start_receive(qmkusbp); + ibqResetI(&qmkusbp->ibqueue); + bqResumeX(&qmkusbp->ibqueue); + obqResetI(&qmkusbp->obqueue); + bqResumeX(&qmkusbp->obqueue); + chnAddFlagsI(qmkusbp, CHN_CONNECTED); + (void)qmkusb_start_receive(qmkusbp); } /** @@ -337,24 +294,23 @@ void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp) { * @retval false Message not handled. */ bool qmkusbRequestsHook(USBDriver *usbp) { - - if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { - switch (usbp->setup[1]) { - case CDC_GET_LINE_CODING: - usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL); - return true; - case CDC_SET_LINE_CODING: - usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL); - return true; - case CDC_SET_CONTROL_LINE_STATE: - /* Nothing to do, there are no control lines.*/ - usbSetupTransfer(usbp, NULL, 0, NULL); - return true; - default: - return false; + if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { + switch (usbp->setup[1]) { + case CDC_GET_LINE_CODING: + usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL); + return true; + case CDC_SET_LINE_CODING: + usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL); + return true; + case CDC_SET_CONTROL_LINE_STATE: + /* Nothing to do, there are no control lines.*/ + usbSetupTransfer(usbp, NULL, 0, NULL); + return true; + default: + return false; + } } - } - return false; + return false; } /** @@ -367,36 +323,34 @@ bool qmkusbRequestsHook(USBDriver *usbp) { * @iclass */ void qmkusbSOFHookI(QMKUSBDriver *qmkusbp) { + /* If the USB driver is not in the appropriate state then transactions + must not be started.*/ + if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) { + return; + } - /* If the USB driver is not in the appropriate state then transactions - must not be started.*/ - if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || - (qmkusbp->state != QMKUSB_READY)) { - return; - } - - /* If there is already a transaction ongoing then another one cannot be - started.*/ - if (usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { - return; - } - - /* Checking if there only a buffer partially filled, if so then it is - enforced in the queue and transmitted.*/ - if (obqTryFlushI(&qmkusbp->obqueue)) { - size_t n; - uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); - - /* For fixed size drivers, fill the end with zeros */ - if (qmkusbp->config->fixed_size) { - memset(buf + n, 0, qmkusbp->config->in_size - n); - n = qmkusbp->config->in_size; + /* If there is already a transaction ongoing then another one cannot be + started.*/ + if (usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) { + return; } - osalDbgAssert(buf != NULL, "queue is empty"); + /* Checking if there only a buffer partially filled, if so then it is + enforced in the queue and transmitted.*/ + if (obqTryFlushI(&qmkusbp->obqueue)) { + size_t n; + uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); - usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n); - } + /* For fixed size drivers, fill the end with zeros */ + if (qmkusbp->config->fixed_size) { + memset(buf + n, 0, qmkusbp->config->in_size - n); + n = qmkusbp->config->in_size; + } + + osalDbgAssert(buf != NULL, "queue is empty"); + + usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n); + } } /** @@ -408,49 +362,45 @@ void qmkusbSOFHookI(QMKUSBDriver *qmkusbp) { * @param[in] ep IN endpoint number */ void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep) { - uint8_t *buf; - size_t n; - QMKUSBDriver *qmkusbp = usbp->in_params[ep - 1U]; + uint8_t * buf; + size_t n; + QMKUSBDriver *qmkusbp = usbp->in_params[ep - 1U]; - if (qmkusbp == NULL) { - return; - } + if (qmkusbp == NULL) { + return; + } - osalSysLockFromISR(); + osalSysLockFromISR(); - /* Signaling that space is available in the output queue.*/ - chnAddFlagsI(qmkusbp, CHN_OUTPUT_EMPTY); + /* Signaling that space is available in the output queue.*/ + chnAddFlagsI(qmkusbp, CHN_OUTPUT_EMPTY); - /* Freeing the buffer just transmitted, if it was not a zero size packet.*/ - if (usbp->epc[ep]->in_state->txsize > 0U) { - obqReleaseEmptyBufferI(&qmkusbp->obqueue); - } + /* Freeing the buffer just transmitted, if it was not a zero size packet.*/ + if (usbp->epc[ep]->in_state->txsize > 0U) { + obqReleaseEmptyBufferI(&qmkusbp->obqueue); + } - /* Checking if there is a buffer ready for transmission.*/ - buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); + /* Checking if there is a buffer ready for transmission.*/ + buf = obqGetFullBufferI(&qmkusbp->obqueue, &n); - if (buf != NULL) { - /* The endpoint cannot be busy, we are in the context of the callback, - so it is safe to transmit without a check.*/ - usbStartTransmitI(usbp, ep, buf, n); - } - else if ((usbp->epc[ep]->in_state->txsize > 0U) && - ((usbp->epc[ep]->in_state->txsize & - ((size_t)usbp->epc[ep]->in_maxsize - 1U)) == 0U)) { - /* Transmit zero sized packet in case the last one has maximum allowed - size. Otherwise the recipient may expect more data coming soon and - not return buffered data to app. See section 5.8.3 Bulk Transfer - Packet Size Constraints of the USB Specification document.*/ - if (!qmkusbp->config->fixed_size) { - usbStartTransmitI(usbp, ep, usbp->setup, 0); + if (buf != NULL) { + /* The endpoint cannot be busy, we are in the context of the callback, + so it is safe to transmit without a check.*/ + usbStartTransmitI(usbp, ep, buf, n); + } else if ((usbp->epc[ep]->in_state->txsize > 0U) && ((usbp->epc[ep]->in_state->txsize & ((size_t)usbp->epc[ep]->in_maxsize - 1U)) == 0U)) { + /* Transmit zero sized packet in case the last one has maximum allowed + size. Otherwise the recipient may expect more data coming soon and + not return buffered data to app. See section 5.8.3 Bulk Transfer + Packet Size Constraints of the USB Specification document.*/ + if (!qmkusbp->config->fixed_size) { + usbStartTransmitI(usbp, ep, usbp->setup, 0); + } + + } else { + /* Nothing to transmit.*/ } - } - else { - /* Nothing to transmit.*/ - } - - osalSysUnlockFromISR(); + osalSysUnlockFromISR(); } /** @@ -462,27 +412,25 @@ void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep) { * @param[in] ep OUT endpoint number */ void qmkusbDataReceived(USBDriver *usbp, usbep_t ep) { - QMKUSBDriver *qmkusbp = usbp->out_params[ep - 1U]; - if (qmkusbp == NULL) { - return; - } + QMKUSBDriver *qmkusbp = usbp->out_params[ep - 1U]; + if (qmkusbp == NULL) { + return; + } - osalSysLockFromISR(); + osalSysLockFromISR(); - /* Signaling that data is available in the input queue.*/ - chnAddFlagsI(qmkusbp, CHN_INPUT_AVAILABLE); + /* Signaling that data is available in the input queue.*/ + chnAddFlagsI(qmkusbp, CHN_INPUT_AVAILABLE); - /* Posting the filled buffer in the queue.*/ - ibqPostFullBufferI(&qmkusbp->ibqueue, - usbGetReceiveTransactionSizeX(qmkusbp->config->usbp, - qmkusbp->config->bulk_out)); + /* Posting the filled buffer in the queue.*/ + ibqPostFullBufferI(&qmkusbp->ibqueue, usbGetReceiveTransactionSizeX(qmkusbp->config->usbp, qmkusbp->config->bulk_out)); - /* The endpoint cannot be busy, we are in the context of the callback, - so a packet is in the buffer for sure. Trying to get a free buffer - for the next transaction.*/ - (void) qmkusb_start_receive(qmkusbp); + /* The endpoint cannot be busy, we are in the context of the callback, + so a packet is in the buffer for sure. Trying to get a free buffer + for the next transaction.*/ + (void)qmkusb_start_receive(qmkusbp); - osalSysUnlockFromISR(); + osalSysUnlockFromISR(); } /** @@ -494,9 +442,8 @@ void qmkusbDataReceived(USBDriver *usbp, usbep_t ep) { * @param[in] ep endpoint number */ void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep) { - - (void)usbp; - (void)ep; + (void)usbp; + (void)ep; } /** @} */ diff --git a/tmk_core/protocol/chibios/usb_driver.h b/tmk_core/protocol/chibios/usb_driver.h index 558479e19..77dff8066 100644 --- a/tmk_core/protocol/chibios/usb_driver.h +++ b/tmk_core/protocol/chibios/usb_driver.h @@ -23,9 +23,9 @@ */ #ifndef USB_DRIVER_H -#define USB_DRIVER_H +# define USB_DRIVER_H -#include "hal_usb_cdc.h" +# include "hal_usb_cdc.h" /*===========================================================================*/ /* Driver constants. */ @@ -35,9 +35,9 @@ /* Derived constants and error checks. */ /*===========================================================================*/ -#if HAL_USE_USB == FALSE -#error "The USB Driver requires HAL_USE_USB" -#endif +# if HAL_USE_USB == FALSE +# error "The USB Driver requires HAL_USE_USB" +# endif /*===========================================================================*/ /* Driver data structures and types. */ @@ -47,9 +47,9 @@ * @brief Driver state machine possible states. */ typedef enum { - QMKUSB_UNINIT = 0, /**< Not initialized. */ - QMKUSB_STOP = 1, /**< Stopped. */ - QMKUSB_READY = 2 /**< Ready. */ + QMKUSB_UNINIT = 0, /**< Not initialized. */ + QMKUSB_STOP = 1, /**< Stopped. */ + QMKUSB_READY = 2 /**< Ready. */ } qmkusbstate_t; /** @@ -63,72 +63,70 @@ typedef struct QMKUSBDriver QMKUSBDriver; * in order to configure and start the driver operations. */ typedef struct { - /** - * @brief USB driver to use. - */ - USBDriver *usbp; - /** - * @brief Bulk IN endpoint used for outgoing data transfer. - */ - usbep_t bulk_in; - /** - * @brief Bulk OUT endpoint used for incoming data transfer. - */ - usbep_t bulk_out; - /** - * @brief Interrupt IN endpoint used for notifications. - * @note If set to zero then the INT endpoint is assumed to be not - * present, USB descriptors must be changed accordingly. - */ - usbep_t int_in; - - /** - * @brief The number of buffers in the queues - */ - size_t in_buffers; - size_t out_buffers; - - /** - * @brief The size of each buffer in the queue, typically the same as the endpoint size - */ - size_t in_size; - size_t out_size; - - /** - * @brief Always send full buffers in_size (the rest is filled with zeroes) - */ - bool fixed_size; - - /* Input buffer - * @note needs to be initialized with a memory buffer of the right size - */ - uint8_t* ib; - /* Output buffer - * @note needs to be initialized with a memory buffer of the right size - */ - uint8_t* ob; + /** + * @brief USB driver to use. + */ + USBDriver *usbp; + /** + * @brief Bulk IN endpoint used for outgoing data transfer. + */ + usbep_t bulk_in; + /** + * @brief Bulk OUT endpoint used for incoming data transfer. + */ + usbep_t bulk_out; + /** + * @brief Interrupt IN endpoint used for notifications. + * @note If set to zero then the INT endpoint is assumed to be not + * present, USB descriptors must be changed accordingly. + */ + usbep_t int_in; + + /** + * @brief The number of buffers in the queues + */ + size_t in_buffers; + size_t out_buffers; + + /** + * @brief The size of each buffer in the queue, typically the same as the endpoint size + */ + size_t in_size; + size_t out_size; + + /** + * @brief Always send full buffers in_size (the rest is filled with zeroes) + */ + bool fixed_size; + + /* Input buffer + * @note needs to be initialized with a memory buffer of the right size + */ + uint8_t *ib; + /* Output buffer + * @note needs to be initialized with a memory buffer of the right size + */ + uint8_t *ob; } QMKUSBConfig; /** * @brief @p SerialDriver specific data. */ -#define _qmk_usb_driver_data \ - _base_asynchronous_channel_data \ - /* Driver state.*/ \ - qmkusbstate_t state; \ - /* Input buffers queue.*/ \ - input_buffers_queue_t ibqueue; \ - /* Output queue.*/ \ - output_buffers_queue_t obqueue; \ - /* End of the mandatory fields.*/ \ - /* Current configuration data.*/ \ - const QMKUSBConfig *config; +# define _qmk_usb_driver_data \ + _base_asynchronous_channel_data /* Driver state.*/ \ + qmkusbstate_t state; \ + /* Input buffers queue.*/ \ + input_buffers_queue_t ibqueue; \ + /* Output queue.*/ \ + output_buffers_queue_t obqueue; \ + /* End of the mandatory fields.*/ \ + /* Current configuration data.*/ \ + const QMKUSBConfig *config; /** * @brief @p SerialUSBDriver specific methods. */ -#define _qmk_usb_driver_methods \ - _base_asynchronous_channel_methods +# define _qmk_usb_driver_methods _base_asynchronous_channel_methods /** * @extends BaseAsynchronousChannelVMT @@ -136,7 +134,7 @@ typedef struct { * @brief @p SerialDriver virtual methods table. */ struct QMKUSBDriverVMT { - _qmk_usb_driver_methods + _qmk_usb_driver_methods }; /** @@ -147,9 +145,9 @@ struct QMKUSBDriverVMT { * I/O queues. */ struct QMKUSBDriver { - /** @brief Virtual Methods Table.*/ - const struct QMKUSBDriverVMT *vmt; - _qmk_usb_driver_data + /** @brief Virtual Methods Table.*/ + const struct QMKUSBDriverVMT *vmt; + _qmk_usb_driver_data }; /*===========================================================================*/ @@ -160,24 +158,24 @@ struct QMKUSBDriver { /* External declarations. */ /*===========================================================================*/ -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif - void qmkusbInit(void); - void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig * config); - void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config); - void qmkusbStop(QMKUSBDriver *qmkusbp); - void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp); - void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp); - void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp); - bool qmkusbRequestsHook(USBDriver *usbp); - void qmkusbSOFHookI(QMKUSBDriver *qmkusbp); - void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep); - void qmkusbDataReceived(USBDriver *usbp, usbep_t ep); - void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep); -#ifdef __cplusplus +# endif +void qmkusbInit(void); +void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config); +void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config); +void qmkusbStop(QMKUSBDriver *qmkusbp); +void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp); +void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp); +void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp); +bool qmkusbRequestsHook(USBDriver *usbp); +void qmkusbSOFHookI(QMKUSBDriver *qmkusbp); +void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep); +void qmkusbDataReceived(USBDriver *usbp, usbep_t ep); +void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep); +# ifdef __cplusplus } -#endif +# endif #endif /* USB_DRIVER_H */ diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 8223d9722..c8a6bbb43 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -24,17 +24,17 @@ #include "debug.h" #include "suspend.h" #ifdef SLEEP_LED_ENABLE -#include "sleep_led.h" -#include "led.h" +# include "sleep_led.h" +# include "led.h" #endif #include "wait.h" #include "usb_descriptor.h" #include "usb_driver.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" +# include "keycode_config.h" - extern keymap_config_t keymap_config; +extern keymap_config_t keymap_config; #endif /* --------------------------------------------------------- @@ -43,19 +43,19 @@ */ #ifndef usb_lld_connect_bus - #define usb_lld_connect_bus(usbp) +# define usb_lld_connect_bus(usbp) #endif #ifndef usb_lld_disconnect_bus - #define usb_lld_disconnect_bus(usbp) +# define usb_lld_disconnect_bus(usbp) #endif -uint8_t keyboard_idle __attribute__((aligned(2))) = 0; -uint8_t keyboard_protocol __attribute__((aligned(2))) = 1; -uint16_t keyboard_led_stats __attribute__((aligned(2))) = 0; -volatile uint16_t keyboard_idle_count = 0; +uint8_t keyboard_idle __attribute__((aligned(2))) = 0; +uint8_t keyboard_protocol __attribute__((aligned(2))) = 1; +uint16_t keyboard_led_stats __attribute__((aligned(2))) = 0; +volatile uint16_t keyboard_idle_count = 0; static virtual_timer_t keyboard_idle_timer; -static void keyboard_idle_timer_cb(void *arg); +static void keyboard_idle_timer_cb(void *arg); report_keyboard_t keyboard_report_sent = {{0}}; #ifdef MOUSE_ENABLE @@ -84,15 +84,15 @@ uint8_t extra_report_blank[3] = {0}; * Returns the proper descriptor */ static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t wIndex) { - (void)usbp; - static USBDescriptor desc; - uint16_t wValue = ((uint16_t)dtype << 8) | dindex; - desc.ud_string = NULL; - desc.ud_size = get_usb_descriptor(wValue, wIndex, (const void** const)&desc.ud_string); - if (desc.ud_string == NULL) - return NULL; - else - return &desc; + (void)usbp; + static USBDescriptor desc; + uint16_t wValue = ((uint16_t)dtype << 8) | dindex; + desc.ud_string = NULL; + desc.ud_size = get_usb_descriptor(wValue, wIndex, (const void **const) & desc.ud_string); + if (desc.ud_string == NULL) + return NULL; + else + return &desc; } #ifndef KEYBOARD_SHARED_EP @@ -100,16 +100,16 @@ static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype static USBInEndpointState kbd_ep_state; /* keyboard endpoint initialization structure (IN) */ static const USBEndpointConfig kbd_ep_config = { - USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ - NULL, /* SETUP packet notification callback */ - kbd_in_cb, /* IN notification callback */ - NULL, /* OUT notification callback */ - KEYBOARD_EPSIZE, /* IN maximum packet size */ - 0, /* OUT maximum packet size */ - &kbd_ep_state, /* IN Endpoint state */ - NULL, /* OUT endpoint state */ - 2, /* IN multiplier */ - NULL /* SETUP buffer (not a SETUP endpoint) */ + USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ + NULL, /* SETUP packet notification callback */ + kbd_in_cb, /* IN notification callback */ + NULL, /* OUT notification callback */ + KEYBOARD_EPSIZE, /* IN maximum packet size */ + 0, /* OUT maximum packet size */ + &kbd_ep_state, /* IN Endpoint state */ + NULL, /* OUT endpoint state */ + 2, /* IN multiplier */ + NULL /* SETUP buffer (not a SETUP endpoint) */ }; #endif @@ -119,16 +119,16 @@ static USBInEndpointState mouse_ep_state; /* mouse endpoint initialization structure (IN) */ static const USBEndpointConfig mouse_ep_config = { - USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ - NULL, /* SETUP packet notification callback */ - mouse_in_cb, /* IN notification callback */ - NULL, /* OUT notification callback */ - MOUSE_EPSIZE, /* IN maximum packet size */ - 0, /* OUT maximum packet size */ - &mouse_ep_state, /* IN Endpoint state */ - NULL, /* OUT endpoint state */ - 2, /* IN multiplier */ - NULL /* SETUP buffer (not a SETUP endpoint) */ + USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ + NULL, /* SETUP packet notification callback */ + mouse_in_cb, /* IN notification callback */ + NULL, /* OUT notification callback */ + MOUSE_EPSIZE, /* IN maximum packet size */ + 0, /* OUT maximum packet size */ + &mouse_ep_state, /* IN Endpoint state */ + NULL, /* OUT endpoint state */ + 2, /* IN multiplier */ + NULL /* SETUP buffer (not a SETUP endpoint) */ }; #endif @@ -138,145 +138,141 @@ static USBInEndpointState shared_ep_state; /* shared endpoint initialization structure (IN) */ static const USBEndpointConfig shared_ep_config = { - USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ - NULL, /* SETUP packet notification callback */ - shared_in_cb, /* IN notification callback */ - NULL, /* OUT notification callback */ - SHARED_EPSIZE, /* IN maximum packet size */ - 0, /* OUT maximum packet size */ - &shared_ep_state, /* IN Endpoint state */ - NULL, /* OUT endpoint state */ - 2, /* IN multiplier */ - NULL /* SETUP buffer (not a SETUP endpoint) */ + USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ + NULL, /* SETUP packet notification callback */ + shared_in_cb, /* IN notification callback */ + NULL, /* OUT notification callback */ + SHARED_EPSIZE, /* IN maximum packet size */ + 0, /* OUT maximum packet size */ + &shared_ep_state, /* IN Endpoint state */ + NULL, /* OUT endpoint state */ + 2, /* IN multiplier */ + NULL /* SETUP buffer (not a SETUP endpoint) */ }; #endif typedef struct { - size_t queue_capacity_in; - size_t queue_capacity_out; - USBInEndpointState in_ep_state; - USBOutEndpointState out_ep_state; - USBInEndpointState int_ep_state; - USBEndpointConfig in_ep_config; - USBEndpointConfig out_ep_config; - USBEndpointConfig int_ep_config; - const QMKUSBConfig config; - QMKUSBDriver driver; + size_t queue_capacity_in; + size_t queue_capacity_out; + USBInEndpointState in_ep_state; + USBOutEndpointState out_ep_state; + USBInEndpointState int_ep_state; + USBEndpointConfig in_ep_config; + USBEndpointConfig out_ep_config; + USBEndpointConfig int_ep_config; + const QMKUSBConfig config; + QMKUSBDriver driver; } usb_driver_config_t; -#define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) { \ - .queue_capacity_in = stream##_IN_CAPACITY, \ - .queue_capacity_out = stream##_OUT_CAPACITY, \ - .in_ep_config = { \ - .ep_mode = stream##_IN_MODE, \ - .setup_cb = NULL, \ - .in_cb = qmkusbDataTransmitted, \ - .out_cb = NULL, \ - .in_maxsize = stream##_EPSIZE, \ - .out_maxsize = 0, \ - /* The pointer to the states will be filled during initialization */ \ - .in_state = NULL, \ - .out_state = NULL, \ - .ep_buffers = 2, \ - .setup_buf = NULL \ - }, \ - .out_ep_config = { \ - .ep_mode = stream##_OUT_MODE, \ - .setup_cb = NULL, \ - .in_cb = NULL, \ - .out_cb = qmkusbDataReceived, \ - .in_maxsize = 0, \ - .out_maxsize = stream##_EPSIZE, \ - /* The pointer to the states will be filled during initialization */ \ - .in_state = NULL, \ - .out_state = NULL, \ - .ep_buffers = 2, \ - .setup_buf = NULL, \ - }, \ - .int_ep_config = { \ - .ep_mode = USB_EP_MODE_TYPE_INTR, \ - .setup_cb = NULL, \ - .in_cb = qmkusbInterruptTransmitted, \ - .out_cb = NULL, \ - .in_maxsize = CDC_NOTIFICATION_EPSIZE, \ - .out_maxsize = 0, \ - /* The pointer to the states will be filled during initialization */ \ - .in_state = NULL, \ - .out_state = NULL, \ - .ep_buffers = 2, \ - .setup_buf = NULL, \ - }, \ - .config = { \ - .usbp = &USB_DRIVER, \ - .bulk_in = stream##_IN_EPNUM, \ - .bulk_out = stream##_OUT_EPNUM, \ - .int_in = notification, \ - .in_buffers = stream##_IN_CAPACITY, \ - .out_buffers = stream##_OUT_CAPACITY, \ - .in_size = stream##_EPSIZE, \ - .out_size = stream##_EPSIZE, \ - .fixed_size = fixedsize, \ - .ib = (uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]) {}, \ - .ob = (uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY,stream##_EPSIZE)]) {}, \ - } \ -} +#define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \ + { \ + .queue_capacity_in = stream##_IN_CAPACITY, .queue_capacity_out = stream##_OUT_CAPACITY, \ + .in_ep_config = {.ep_mode = stream##_IN_MODE, \ + .setup_cb = NULL, \ + .in_cb = qmkusbDataTransmitted, \ + .out_cb = NULL, \ + .in_maxsize = stream##_EPSIZE, \ + .out_maxsize = 0, /* The pointer to the states will be filled during initialization */ \ + .in_state = NULL, \ + .out_state = NULL, \ + .ep_buffers = 2, \ + .setup_buf = NULL}, \ + .out_ep_config = \ + { \ + .ep_mode = stream##_OUT_MODE, \ + .setup_cb = NULL, \ + .in_cb = NULL, \ + .out_cb = qmkusbDataReceived, \ + .in_maxsize = 0, \ + .out_maxsize = stream##_EPSIZE, /* The pointer to the states will be filled during initialization */ \ + .in_state = NULL, \ + .out_state = NULL, \ + .ep_buffers = 2, \ + .setup_buf = NULL, \ + }, \ + .int_ep_config = \ + { \ + .ep_mode = USB_EP_MODE_TYPE_INTR, \ + .setup_cb = NULL, \ + .in_cb = qmkusbInterruptTransmitted, \ + .out_cb = NULL, \ + .in_maxsize = CDC_NOTIFICATION_EPSIZE, \ + .out_maxsize = 0, /* The pointer to the states will be filled during initialization */ \ + .in_state = NULL, \ + .out_state = NULL, \ + .ep_buffers = 2, \ + .setup_buf = NULL, \ + }, \ + .config = { \ + .usbp = &USB_DRIVER, \ + .bulk_in = stream##_IN_EPNUM, \ + .bulk_out = stream##_OUT_EPNUM, \ + .int_in = notification, \ + .in_buffers = stream##_IN_CAPACITY, \ + .out_buffers = stream##_OUT_CAPACITY, \ + .in_size = stream##_EPSIZE, \ + .out_size = stream##_EPSIZE, \ + .fixed_size = fixedsize, \ + .ib = (uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]){}, \ + .ob = (uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \ + } \ + } typedef struct { - union { - struct { + union { + struct { #ifdef CONSOLE_ENABLE - usb_driver_config_t console_driver; + usb_driver_config_t console_driver; #endif #ifdef RAW_ENABLE - usb_driver_config_t raw_driver; + usb_driver_config_t raw_driver; #endif #ifdef MIDI_ENABLE - usb_driver_config_t midi_driver; + usb_driver_config_t midi_driver; #endif #ifdef VIRTSER_ENABLE - usb_driver_config_t serial_driver; + usb_driver_config_t serial_driver; #endif + }; + usb_driver_config_t array[0]; }; - usb_driver_config_t array[0]; - }; } usb_driver_configs_t; static usb_driver_configs_t drivers = { #ifdef CONSOLE_ENABLE - #define CONSOLE_IN_CAPACITY 4 - #define CONSOLE_OUT_CAPACITY 4 - #define CONSOLE_IN_MODE USB_EP_MODE_TYPE_INTR - #define CONSOLE_OUT_MODE USB_EP_MODE_TYPE_INTR - .console_driver = QMK_USB_DRIVER_CONFIG(CONSOLE, 0, true), +# define CONSOLE_IN_CAPACITY 4 +# define CONSOLE_OUT_CAPACITY 4 +# define CONSOLE_IN_MODE USB_EP_MODE_TYPE_INTR +# define CONSOLE_OUT_MODE USB_EP_MODE_TYPE_INTR + .console_driver = QMK_USB_DRIVER_CONFIG(CONSOLE, 0, true), #endif #ifdef RAW_ENABLE - #define RAW_IN_CAPACITY 4 - #define RAW_OUT_CAPACITY 4 - #define RAW_IN_MODE USB_EP_MODE_TYPE_INTR - #define RAW_OUT_MODE USB_EP_MODE_TYPE_INTR - .raw_driver = QMK_USB_DRIVER_CONFIG(RAW, 0, false), +# define RAW_IN_CAPACITY 4 +# define RAW_OUT_CAPACITY 4 +# define RAW_IN_MODE USB_EP_MODE_TYPE_INTR +# define RAW_OUT_MODE USB_EP_MODE_TYPE_INTR + .raw_driver = QMK_USB_DRIVER_CONFIG(RAW, 0, false), #endif #ifdef MIDI_ENABLE - #define MIDI_STREAM_IN_CAPACITY 4 - #define MIDI_STREAM_OUT_CAPACITY 4 - #define MIDI_STREAM_IN_MODE USB_EP_MODE_TYPE_BULK - #define MIDI_STREAM_OUT_MODE USB_EP_MODE_TYPE_BULK - .midi_driver = QMK_USB_DRIVER_CONFIG(MIDI_STREAM, 0, false), +# define MIDI_STREAM_IN_CAPACITY 4 +# define MIDI_STREAM_OUT_CAPACITY 4 +# define MIDI_STREAM_IN_MODE USB_EP_MODE_TYPE_BULK +# define MIDI_STREAM_OUT_MODE USB_EP_MODE_TYPE_BULK + .midi_driver = QMK_USB_DRIVER_CONFIG(MIDI_STREAM, 0, false), #endif #ifdef VIRTSER_ENABLE - #define CDC_IN_CAPACITY 4 - #define CDC_OUT_CAPACITY 4 - #define CDC_IN_MODE USB_EP_MODE_TYPE_BULK - #define CDC_OUT_MODE USB_EP_MODE_TYPE_BULK - .serial_driver = QMK_USB_DRIVER_CONFIG(CDC, CDC_NOTIFICATION_EPNUM, false), +# define CDC_IN_CAPACITY 4 +# define CDC_OUT_CAPACITY 4 +# define CDC_IN_MODE USB_EP_MODE_TYPE_BULK +# define CDC_OUT_MODE USB_EP_MODE_TYPE_BULK + .serial_driver = QMK_USB_DRIVER_CONFIG(CDC, CDC_NOTIFICATION_EPNUM, false), #endif }; #define NUM_USB_DRIVERS (sizeof(drivers) / sizeof(usb_driver_config_t)) - /* --------------------------------------------------------- * USB driver functions * --------------------------------------------------------- @@ -285,77 +281,77 @@ static usb_driver_configs_t drivers = { /* Handles the USB driver global events * TODO: maybe disable some things when connection is lost? */ static void usb_event_cb(USBDriver *usbp, usbevent_t event) { - switch(event) { - case USB_EVENT_ADDRESS: - return; + switch (event) { + case USB_EVENT_ADDRESS: + return; - case USB_EVENT_CONFIGURED: - osalSysLockFromISR(); - /* Enable the endpoints specified into the configuration. */ + case USB_EVENT_CONFIGURED: + osalSysLockFromISR(); + /* Enable the endpoints specified into the configuration. */ #ifndef KEYBOARD_SHARED_EP - usbInitEndpointI(usbp, KEYBOARD_IN_EPNUM, &kbd_ep_config); + usbInitEndpointI(usbp, KEYBOARD_IN_EPNUM, &kbd_ep_config); #endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) - usbInitEndpointI(usbp, MOUSE_IN_EPNUM, &mouse_ep_config); + usbInitEndpointI(usbp, MOUSE_IN_EPNUM, &mouse_ep_config); #endif #ifdef SHARED_EP_ENABLE - usbInitEndpointI(usbp, SHARED_IN_EPNUM, &shared_ep_config); + usbInitEndpointI(usbp, SHARED_IN_EPNUM, &shared_ep_config); #endif - for (int i=0;i<NUM_USB_DRIVERS;i++) { - usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].in_ep_config); - usbInitEndpointI(usbp, drivers.array[i].config.bulk_out, &drivers.array[i].out_ep_config); - if (drivers.array[i].config.int_in) { - usbInitEndpointI(usbp, drivers.array[i].config.int_in, &drivers.array[i].int_ep_config); - } - qmkusbConfigureHookI(&drivers.array[i].driver); - } - osalSysUnlockFromISR(); - return; - case USB_EVENT_SUSPEND: + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].in_ep_config); + usbInitEndpointI(usbp, drivers.array[i].config.bulk_out, &drivers.array[i].out_ep_config); + if (drivers.array[i].config.int_in) { + usbInitEndpointI(usbp, drivers.array[i].config.int_in, &drivers.array[i].int_ep_config); + } + qmkusbConfigureHookI(&drivers.array[i].driver); + } + osalSysUnlockFromISR(); + return; + case USB_EVENT_SUSPEND: #ifdef SLEEP_LED_ENABLE - sleep_led_enable(); -#endif /* SLEEP_LED_ENABLE */ - /* Falls into.*/ - case USB_EVENT_UNCONFIGURED: - /* Falls into.*/ - case USB_EVENT_RESET: - for (int i=0;i<NUM_USB_DRIVERS;i++) { - chSysLockFromISR(); - /* Disconnection event on suspend.*/ - qmkusbSuspendHookI(&drivers.array[i].driver); - chSysUnlockFromISR(); - } - return; - - case USB_EVENT_WAKEUP: - //TODO: from ISR! print("[W]"); - for (int i=0;i<NUM_USB_DRIVERS;i++) { - chSysLockFromISR(); - /* Disconnection event on suspend.*/ - qmkusbWakeupHookI(&drivers.array[i].driver); - chSysUnlockFromISR(); - } - suspend_wakeup_init(); + sleep_led_enable(); +#endif /* SLEEP_LED_ENABLE */ + /* Falls into.*/ + case USB_EVENT_UNCONFIGURED: + /* Falls into.*/ + case USB_EVENT_RESET: + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + chSysLockFromISR(); + /* Disconnection event on suspend.*/ + qmkusbSuspendHookI(&drivers.array[i].driver); + chSysUnlockFromISR(); + } + return; + + case USB_EVENT_WAKEUP: + // TODO: from ISR! print("[W]"); + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + chSysLockFromISR(); + /* Disconnection event on suspend.*/ + qmkusbWakeupHookI(&drivers.array[i].driver); + chSysUnlockFromISR(); + } + suspend_wakeup_init(); #ifdef SLEEP_LED_ENABLE - sleep_led_disable(); - // NOTE: converters may not accept this - led_set(host_keyboard_leds()); + sleep_led_disable(); + // NOTE: converters may not accept this + led_set(host_keyboard_leds()); #endif /* SLEEP_LED_ENABLE */ - return; + return; - case USB_EVENT_STALLED: - return; - } + case USB_EVENT_STALLED: + return; + } } /* Function used locally in os/hal/src/usb.c for getting descriptors * need it here for HID descriptor */ static uint16_t get_hword(uint8_t *p) { - uint16_t hw; + uint16_t hw; - hw = (uint16_t)*p++; - hw |= (uint16_t)*p << 8U; - return hw; + hw = (uint16_t)*p++; + hw |= (uint16_t)*p << 8U; + return hw; } /* @@ -373,197 +369,193 @@ static uint16_t get_hword(uint8_t *p) { #ifdef SHARED_EP_ENABLE static uint8_t set_report_buf[2] __attribute__((aligned(2))); -static void set_led_transfer_cb(USBDriver *usbp) { - if ((set_report_buf[0] == REPORT_ID_KEYBOARD) || - (set_report_buf[0] == REPORT_ID_NKRO)) { - keyboard_led_stats = set_report_buf[1]; - } +static void set_led_transfer_cb(USBDriver *usbp) { + if ((set_report_buf[0] == REPORT_ID_KEYBOARD) || (set_report_buf[0] == REPORT_ID_NKRO)) { + keyboard_led_stats = set_report_buf[1]; + } } #endif /* Callback for SETUP request on the endpoint 0 (control) */ static bool usb_request_hook_cb(USBDriver *usbp) { - const USBDescriptor *dp; - int has_report_id; - - /* usbp->setup fields: - * 0: bmRequestType (bitmask) - * 1: bRequest - * 2,3: (LSB,MSB) wValue - * 4,5: (LSB,MSB) wIndex - * 6,7: (LSB,MSB) wLength (number of bytes to transfer if there is a data phase) */ - - /* Handle HID class specific requests */ - if(((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) && - ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) { - switch(usbp->setup[0] & USB_RTYPE_DIR_MASK) { - case USB_RTYPE_DIR_DEV2HOST: - switch(usbp->setup[1]) { /* bRequest */ - case HID_GET_REPORT: - switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */ - case KEYBOARD_INTERFACE: - usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL); - return TRUE; - break; + const USBDescriptor *dp; + int has_report_id; + + /* usbp->setup fields: + * 0: bmRequestType (bitmask) + * 1: bRequest + * 2,3: (LSB,MSB) wValue + * 4,5: (LSB,MSB) wIndex + * 6,7: (LSB,MSB) wLength (number of bytes to transfer if there is a data phase) */ + + /* Handle HID class specific requests */ + if (((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) && ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) { + switch (usbp->setup[0] & USB_RTYPE_DIR_MASK) { + case USB_RTYPE_DIR_DEV2HOST: + switch (usbp->setup[1]) { /* bRequest */ + case HID_GET_REPORT: + switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */ + case KEYBOARD_INTERFACE: + usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL); + return TRUE; + break; #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) - case MOUSE_INTERFACE: - usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL); - return TRUE; - break; + case MOUSE_INTERFACE: + usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL); + return TRUE; + break; #endif - default: - usbSetupTransfer(usbp, NULL, 0, NULL); - return TRUE; - break; - } - break; - - case HID_GET_PROTOCOL: - if((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ - usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL); - return TRUE; - } - break; - - case HID_GET_IDLE: - usbSetupTransfer(usbp, &keyboard_idle, 1, NULL); - return TRUE; - break; - } - break; - - case USB_RTYPE_DIR_HOST2DEV: - switch(usbp->setup[1]) { /* bRequest */ - case HID_SET_REPORT: - switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0 and wLength==1?) */ - case KEYBOARD_INTERFACE: + default: + usbSetupTransfer(usbp, NULL, 0, NULL); + return TRUE; + break; + } + break; + + case HID_GET_PROTOCOL: + if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ + usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL); + return TRUE; + } + break; + + case HID_GET_IDLE: + usbSetupTransfer(usbp, &keyboard_idle, 1, NULL); + return TRUE; + break; + } + break; + + case USB_RTYPE_DIR_HOST2DEV: + switch (usbp->setup[1]) { /* bRequest */ + case HID_SET_REPORT: + switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0 and wLength==1?) */ + case KEYBOARD_INTERFACE: #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP) - case SHARED_INTERFACE: + case SHARED_INTERFACE: #endif - /* keyboard_led_stats = <read byte from next OUT report> - * keyboard_led_stats needs be word (or dword), otherwise we get an exception on F0 */ - has_report_id = 0; + /* keyboard_led_stats = <read byte from next OUT report> + * keyboard_led_stats needs be word (or dword), otherwise we get an exception on F0 */ + has_report_id = 0; #if defined(SHARED_EP_ENABLE) - if (usbp->setup[4] == SHARED_INTERFACE) { - has_report_id = 1; - } + if (usbp->setup[4] == SHARED_INTERFACE) { + has_report_id = 1; + } #endif - if (usbp->setup[4] == KEYBOARD_INTERFACE && !keyboard_protocol) { - has_report_id = 0; - } - if (has_report_id) { - usbSetupTransfer(usbp, set_report_buf, sizeof(set_report_buf), set_led_transfer_cb); - } else { - usbSetupTransfer(usbp, (uint8_t *)&keyboard_led_stats, 1, NULL); - } - return TRUE; - break; - } - break; - - case HID_SET_PROTOCOL: - if((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ - keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */ + if (usbp->setup[4] == KEYBOARD_INTERFACE && !keyboard_protocol) { + has_report_id = 0; + } + if (has_report_id) { + usbSetupTransfer(usbp, set_report_buf, sizeof(set_report_buf), set_led_transfer_cb); + } else { + usbSetupTransfer(usbp, (uint8_t *)&keyboard_led_stats, 1, NULL); + } + return TRUE; + break; + } + break; + + case HID_SET_PROTOCOL: + if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ + keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */ #ifdef NKRO_ENABLE - keymap_config.nkro = !!keyboard_protocol; - if(!keymap_config.nkro && keyboard_idle) { -#else /* NKRO_ENABLE */ - if(keyboard_idle) { -#endif /* NKRO_ENABLE */ - /* arm the idle timer if boot protocol & idle */ - osalSysLockFromISR(); - chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); - osalSysUnlockFromISR(); - } - } - usbSetupTransfer(usbp, NULL, 0, NULL); - return TRUE; - break; - - case HID_SET_IDLE: - keyboard_idle = usbp->setup[3]; /* MSB(wValue) */ - /* arm the timer */ + keymap_config.nkro = !!keyboard_protocol; + if (!keymap_config.nkro && keyboard_idle) { +#else /* NKRO_ENABLE */ + if (keyboard_idle) { +#endif /* NKRO_ENABLE */ + /* arm the idle timer if boot protocol & idle */ + osalSysLockFromISR(); + chVTSetI(&keyboard_idle_timer, 4 * MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); + osalSysUnlockFromISR(); + } + } + usbSetupTransfer(usbp, NULL, 0, NULL); + return TRUE; + break; + + case HID_SET_IDLE: + keyboard_idle = usbp->setup[3]; /* MSB(wValue) */ + /* arm the timer */ #ifdef NKRO_ENABLE - if(!keymap_config.nkro && keyboard_idle) { -#else /* NKRO_ENABLE */ - if(keyboard_idle) { + if (!keymap_config.nkro && keyboard_idle) { +#else /* NKRO_ENABLE */ + if (keyboard_idle) { #endif /* NKRO_ENABLE */ - osalSysLockFromISR(); - chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); - osalSysUnlockFromISR(); + osalSysLockFromISR(); + chVTSetI(&keyboard_idle_timer, 4 * MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); + osalSysUnlockFromISR(); + } + usbSetupTransfer(usbp, NULL, 0, NULL); + return TRUE; + break; + } + break; } - usbSetupTransfer(usbp, NULL, 0, NULL); + } + + /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */ + if ((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) { + dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4])); + if (dp == NULL) return FALSE; + usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL); return TRUE; - break; - } - break; } - } - - /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */ - if((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) { - dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4])); - if(dp == NULL) - return FALSE; - usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL); - return TRUE; - } - - for (int i=0;i<NUM_USB_DRIVERS;i++) { - if (drivers.array[i].config.int_in) { - // NOTE: Assumes that we only have one serial driver - return qmkusbRequestsHook(usbp); + + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + if (drivers.array[i].config.int_in) { + // NOTE: Assumes that we only have one serial driver + return qmkusbRequestsHook(usbp); + } } - } - return FALSE; + return FALSE; } /* Start-of-frame callback */ static void usb_sof_cb(USBDriver *usbp) { - kbd_sof_cb(usbp); - osalSysLockFromISR(); - for (int i=0; i<NUM_USB_DRIVERS;i++) { - qmkusbSOFHookI(&drivers.array[i].driver); - } - osalSysUnlockFromISR(); + kbd_sof_cb(usbp); + osalSysLockFromISR(); + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + qmkusbSOFHookI(&drivers.array[i].driver); + } + osalSysUnlockFromISR(); } - /* USB driver configuration */ static const USBConfig usbcfg = { - usb_event_cb, /* USB events callback */ - usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */ - usb_request_hook_cb, /* Requests hook callback */ - usb_sof_cb /* Start Of Frame callback */ + usb_event_cb, /* USB events callback */ + usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */ + usb_request_hook_cb, /* Requests hook callback */ + usb_sof_cb /* Start Of Frame callback */ }; /* * Initialize the USB driver */ void init_usb_driver(USBDriver *usbp) { - for (int i=0; i<NUM_USB_DRIVERS;i++) { - QMKUSBDriver* driver = &drivers.array[i].driver; - drivers.array[i].in_ep_config.in_state = &drivers.array[i].in_ep_state; - drivers.array[i].out_ep_config.out_state = &drivers.array[i].out_ep_state; - drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state; - qmkusbObjectInit(driver, &drivers.array[i].config); - qmkusbStart(driver, &drivers.array[i].config); - } - - /* - * Activates the USB driver and then the USB bus pull-up on D+. - * Note, a delay is inserted in order to not have to disconnect the cable - * after a reset. - */ - usbDisconnectBus(usbp); - wait_ms(1500); - usbStart(usbp, &usbcfg); - usbConnectBus(usbp); - - chVTObjectInit(&keyboard_idle_timer); + for (int i = 0; i < NUM_USB_DRIVERS; i++) { + QMKUSBDriver *driver = &drivers.array[i].driver; + drivers.array[i].in_ep_config.in_state = &drivers.array[i].in_ep_state; + drivers.array[i].out_ep_config.out_state = &drivers.array[i].out_ep_state; + drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state; + qmkusbObjectInit(driver, &drivers.array[i].config); + qmkusbStart(driver, &drivers.array[i].config); + } + + /* + * Activates the USB driver and then the USB bus pull-up on D+. + * Note, a delay is inserted in order to not have to disconnect the cable + * after a reset. + */ + usbDisconnectBus(usbp); + wait_ms(1500); + usbStart(usbp, &usbcfg); + usbConnectBus(usbp); + + chVTObjectInit(&keyboard_idle_timer); } /* --------------------------------------------------------- @@ -573,108 +565,104 @@ void init_usb_driver(USBDriver *usbp) { /* keyboard IN callback hander (a kbd report has made it IN) */ #ifndef KEYBOARD_SHARED_EP void kbd_in_cb(USBDriver *usbp, usbep_t ep) { - /* STUB */ - (void)usbp; - (void)ep; + /* STUB */ + (void)usbp; + (void)ep; } #endif /* start-of-frame handler * TODO: i guess it would be better to re-implement using timers, * so that this is not going to have to be checked every 1ms */ -void kbd_sof_cb(USBDriver *usbp) { - (void)usbp; -} +void kbd_sof_cb(USBDriver *usbp) { (void)usbp; } /* Idle requests timer code * callback (called from ISR, unlocked state) */ static void keyboard_idle_timer_cb(void *arg) { - USBDriver *usbp = (USBDriver *)arg; + USBDriver *usbp = (USBDriver *)arg; - osalSysLockFromISR(); + osalSysLockFromISR(); - /* check that the states of things are as they're supposed to */ - if(usbGetDriverStateI(usbp) != USB_ACTIVE) { - /* do not rearm the timer, should be enabled on IDLE request */ - osalSysUnlockFromISR(); - return; - } + /* check that the states of things are as they're supposed to */ + if (usbGetDriverStateI(usbp) != USB_ACTIVE) { + /* do not rearm the timer, should be enabled on IDLE request */ + osalSysUnlockFromISR(); + return; + } #ifdef NKRO_ENABLE - if(!keymap_config.nkro && keyboard_idle && keyboard_protocol) { -#else /* NKRO_ENABLE */ - if(keyboard_idle && keyboard_protocol) { + if (!keymap_config.nkro && keyboard_idle && keyboard_protocol) { +#else /* NKRO_ENABLE */ + if (keyboard_idle && keyboard_protocol) { #endif /* NKRO_ENABLE */ - /* TODO: are we sure we want the KBD_ENDPOINT? */ - if(!usbGetTransmitStatusI(usbp, KEYBOARD_IN_EPNUM)) { - usbStartTransmitI(usbp, KEYBOARD_IN_EPNUM, (uint8_t *)&keyboard_report_sent, KEYBOARD_EPSIZE); + /* TODO: are we sure we want the KBD_ENDPOINT? */ + if (!usbGetTransmitStatusI(usbp, KEYBOARD_IN_EPNUM)) { + usbStartTransmitI(usbp, KEYBOARD_IN_EPNUM, (uint8_t *)&keyboard_report_sent, KEYBOARD_EPSIZE); + } + /* rearm the timer */ + chVTSetI(&keyboard_idle_timer, 4 * MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); } - /* rearm the timer */ - chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); - } - /* do not rearm the timer if the condition above fails - * it should be enabled again on either IDLE or SET_PROTOCOL requests */ - osalSysUnlockFromISR(); + /* do not rearm the timer if the condition above fails + * it should be enabled again on either IDLE or SET_PROTOCOL requests */ + osalSysUnlockFromISR(); } /* LED status */ -uint8_t keyboard_leds(void) { - return (uint8_t)(keyboard_led_stats & 0xFF); -} +uint8_t keyboard_leds(void) { return (uint8_t)(keyboard_led_stats & 0xFF); } /* prepare and start sending a report IN * not callable from ISR or locked state */ void send_keyboard(report_keyboard_t *report) { - osalSysLock(); - if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - osalSysUnlock(); - -#ifdef NKRO_ENABLE - if(keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ - /* need to wait until the previous packet has made it through */ - /* can rewrite this using the synchronous API, then would wait - * until *after* the packet has been transmitted. I think - * this is more efficient */ - /* busy wait, should be short and not very common */ osalSysLock(); - if(usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); + if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { + osalSysUnlock(); + return; } - usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); osalSysUnlock(); - } else + +#ifdef NKRO_ENABLE + if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ + /* need to wait until the previous packet has made it through */ + /* can rewrite this using the synchronous API, then would wait + * until *after* the packet has been transmitted. I think + * this is more efficient */ + /* busy wait, should be short and not very common */ + osalSysLock(); + if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { + /* Need to either suspend, or loop and call unlock/lock during + * every iteration - otherwise the system will remain locked, + * no interrupts served, so USB not going through as well. + * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ + osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); + } + usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); + osalSysUnlock(); + } else #endif /* NKRO_ENABLE */ - { /* regular protocol */ - /* need to wait until the previous packet has made it through */ - /* busy wait, should be short and not very common */ - osalSysLock(); - if(usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); - } - uint8_t *data, size; - if (keyboard_protocol) { - data = (uint8_t*)report; - size = KEYBOARD_REPORT_SIZE; - } else { /* boot protocol */ - data = &report->mods; - size = 8; + { /* regular protocol */ + /* need to wait until the previous packet has made it through */ + /* busy wait, should be short and not very common */ + osalSysLock(); + if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { + /* Need to either suspend, or loop and call unlock/lock during + * every iteration - otherwise the system will remain locked, + * no interrupts served, so USB not going through as well. + * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ + osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); + } + uint8_t *data, size; + if (keyboard_protocol) { + data = (uint8_t *)report; + size = KEYBOARD_REPORT_SIZE; + } else { /* boot protocol */ + data = &report->mods; + size = 8; + } + usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); + osalSysUnlock(); } - usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); - osalSysUnlock(); - } - keyboard_report_sent = *report; + keyboard_report_sent = *report; } /* --------------------------------------------------------- @@ -684,39 +672,37 @@ void send_keyboard(report_keyboard_t *report) { #ifdef MOUSE_ENABLE -#ifndef MOUSE_SHARED_EP +# ifndef MOUSE_SHARED_EP /* mouse IN callback hander (a mouse report has made it IN) */ void mouse_in_cb(USBDriver *usbp, usbep_t ep) { - (void)usbp; - (void)ep; + (void)usbp; + (void)ep; } -#endif +# endif void send_mouse(report_mouse_t *report) { - osalSysLock(); - if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - - if(usbGetTransmitStatusI(&USB_DRIVER, MOUSE_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[MOUSE_IN_EPNUM]->in_state->thread, MS2ST(10))==MSG_TIMEOUT) { - osalSysUnlock(); - return; + osalSysLock(); + if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { + osalSysUnlock(); + return; } - } - usbStartTransmitI(&USB_DRIVER, MOUSE_IN_EPNUM, (uint8_t *)report, sizeof(report_mouse_t)); - osalSysUnlock(); -} -#else /* MOUSE_ENABLE */ -void send_mouse(report_mouse_t *report) { - (void)report; + if (usbGetTransmitStatusI(&USB_DRIVER, MOUSE_IN_EPNUM)) { + /* Need to either suspend, or loop and call unlock/lock during + * every iteration - otherwise the system will remain locked, + * no interrupts served, so USB not going through as well. + * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ + if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[MOUSE_IN_EPNUM]->in_state->thread, MS2ST(10)) == MSG_TIMEOUT) { + osalSysUnlock(); + return; + } + } + usbStartTransmitI(&USB_DRIVER, MOUSE_IN_EPNUM, (uint8_t *)report, sizeof(report_mouse_t)); + osalSysUnlock(); } + +#else /* MOUSE_ENABLE */ +void send_mouse(report_mouse_t *report) { (void)report; } #endif /* MOUSE_ENABLE */ /* --------------------------------------------------------- @@ -726,9 +712,9 @@ void send_mouse(report_mouse_t *report) { #ifdef SHARED_EP_ENABLE /* shared IN callback hander */ void shared_in_cb(USBDriver *usbp, usbep_t ep) { - /* STUB */ - (void)usbp; - (void)ep; + /* STUB */ + (void)usbp; + (void)ep; } #endif @@ -739,36 +725,25 @@ void shared_in_cb(USBDriver *usbp, usbep_t ep) { #ifdef EXTRAKEY_ENABLE static void send_extra_report(uint8_t report_id, uint16_t data) { - osalSysLock(); - if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } + osalSysLock(); + if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { + osalSysUnlock(); + return; + } - report_extra_t report = { - .report_id = report_id, - .usage = data - }; + report_extra_t report = {.report_id = report_id, .usage = data}; - usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)&report, sizeof(report_extra_t)); - osalSysUnlock(); + usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)&report, sizeof(report_extra_t)); + osalSysUnlock(); } -void send_system(uint16_t data) { - send_extra_report(REPORT_ID_SYSTEM, data); -} +void send_system(uint16_t data) { send_extra_report(REPORT_ID_SYSTEM, data); } -void send_consumer(uint16_t data) { - send_extra_report(REPORT_ID_CONSUMER, data); -} +void send_consumer(uint16_t data) { send_extra_report(REPORT_ID_CONSUMER, data); } -#else /* EXTRAKEY_ENABLE */ -void send_system(uint16_t data) { - (void)data; -} -void send_consumer(uint16_t data) { - (void)data; -} +#else /* EXTRAKEY_ENABLE */ +void send_system(uint16_t data) { (void)data; } +void send_consumer(uint16_t data) { (void)data; } #endif /* EXTRAKEY_ENABLE */ /* --------------------------------------------------------- @@ -779,106 +754,97 @@ void send_consumer(uint16_t data) { #ifdef CONSOLE_ENABLE int8_t sendchar(uint8_t c) { - // The previous implmentation had timeouts, but I think it's better to just slow down - // and make sure that everything is transferred, rather than dropping stuff - return chnWrite(&drivers.console_driver.driver, &c, 1); + // The previous implmentation had timeouts, but I think it's better to just slow down + // and make sure that everything is transferred, rather than dropping stuff + return chnWrite(&drivers.console_driver.driver, &c, 1); } // Just a dummy function for now, this could be exposed as a weak function // Or connected to the actual QMK console -static void console_receive( uint8_t *data, uint8_t length ) { - (void)data; - (void)length; +static void console_receive(uint8_t *data, uint8_t length) { + (void)data; + (void)length; } void console_task(void) { - uint8_t buffer[CONSOLE_EPSIZE]; - size_t size = 0; - do { - size_t size = chnReadTimeout(&drivers.console_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); - if (size > 0) { - console_receive(buffer, size); - } - } while(size > 0); + uint8_t buffer[CONSOLE_EPSIZE]; + size_t size = 0; + do { + size_t size = chnReadTimeout(&drivers.console_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); + if (size > 0) { + console_receive(buffer, size); + } + } while (size > 0); } -#else /* CONSOLE_ENABLE */ +#else /* CONSOLE_ENABLE */ int8_t sendchar(uint8_t c) { - (void)c; - return 0; + (void)c; + return 0; } #endif /* CONSOLE_ENABLE */ void sendchar_pf(void *p, char c) { - (void)p; - sendchar((uint8_t)c); + (void)p; + sendchar((uint8_t)c); } #ifdef RAW_ENABLE -void raw_hid_send( uint8_t *data, uint8_t length ) { - // TODO: implement variable size packet - if ( length != RAW_EPSIZE ) - { - return; - - } - chnWrite(&drivers.raw_driver.driver, data, length); +void raw_hid_send(uint8_t *data, uint8_t length) { + // TODO: implement variable size packet + if (length != RAW_EPSIZE) { + return; + } + chnWrite(&drivers.raw_driver.driver, data, length); } -__attribute__ ((weak)) -void raw_hid_receive( uint8_t *data, uint8_t length ) { - // Users should #include "raw_hid.h" in their own code - // and implement this function there. Leave this as weak linkage - // so users can opt to not handle data coming in. +__attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { + // Users should #include "raw_hid.h" in their own code + // and implement this function there. Leave this as weak linkage + // so users can opt to not handle data coming in. } void raw_hid_task(void) { - uint8_t buffer[RAW_EPSIZE]; - size_t size = 0; - do { - size_t size = chnReadTimeout(&drivers.raw_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); - if (size > 0) { - raw_hid_receive(buffer, size); - } - } while(size > 0); + uint8_t buffer[RAW_EPSIZE]; + size_t size = 0; + do { + size_t size = chnReadTimeout(&drivers.raw_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); + if (size > 0) { + raw_hid_receive(buffer, size); + } + } while (size > 0); } #endif #ifdef MIDI_ENABLE -void send_midi_packet(MIDI_EventPacket_t* event) { - chnWrite(&drivers.midi_driver.driver, (uint8_t*)event, sizeof(MIDI_EventPacket_t)); -} +void send_midi_packet(MIDI_EventPacket_t *event) { chnWrite(&drivers.midi_driver.driver, (uint8_t *)event, sizeof(MIDI_EventPacket_t)); } -bool recv_midi_packet(MIDI_EventPacket_t* const event) { - size_t size = chnReadTimeout(&drivers.midi_driver.driver, (uint8_t*)event, sizeof(MIDI_EventPacket_t), TIME_IMMEDIATE); - return size == sizeof(MIDI_EventPacket_t); +bool recv_midi_packet(MIDI_EventPacket_t *const event) { + size_t size = chnReadTimeout(&drivers.midi_driver.driver, (uint8_t *)event, sizeof(MIDI_EventPacket_t), TIME_IMMEDIATE); + return size == sizeof(MIDI_EventPacket_t); } #endif #ifdef VIRTSER_ENABLE -void virtser_send(const uint8_t byte) { - chnWrite(&drivers.serial_driver.driver, &byte, 1); -} +void virtser_send(const uint8_t byte) { chnWrite(&drivers.serial_driver.driver, &byte, 1); } -__attribute__ ((weak)) -void virtser_recv(uint8_t c) -{ - // Ignore by default +__attribute__((weak)) void virtser_recv(uint8_t c) { + // Ignore by default } void virtser_task(void) { - uint8_t numBytesReceived = 0; - uint8_t buffer[16]; - do { - numBytesReceived = chnReadTimeout(&drivers.serial_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); - for (int i=0;i<numBytesReceived;i++) { - virtser_recv(buffer[i]); - } - } while (numBytesReceived > 0); + uint8_t numBytesReceived = 0; + uint8_t buffer[16]; + do { + numBytesReceived = chnReadTimeout(&drivers.serial_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); + for (int i = 0; i < numBytesReceived; i++) { + virtser_recv(buffer[i]); + } + } while (numBytesReceived > 0); } #endif diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h index 55e8882cc..cd2bb695b 100644 --- a/tmk_core/protocol/chibios/usb_main.h +++ b/tmk_core/protocol/chibios/usb_main.h @@ -15,7 +15,6 @@ * GPL v2 or later. */ - #ifndef _USB_MAIN_H_ #define _USB_MAIN_H_ @@ -82,9 +81,9 @@ void shared_in_cb(USBDriver *usbp, usbep_t ep); /* extra report structure */ typedef struct { - uint8_t report_id; - uint16_t usage; -} __attribute__ ((packed)) report_extra_t; + uint8_t report_id; + uint16_t usage; +} __attribute__((packed)) report_extra_t; #endif /* EXTRAKEY_ENABLE */ /* -------------- diff --git a/tmk_core/protocol/ibm4704.c b/tmk_core/protocol/ibm4704.c index 6a03cd441..fd8fc4dbd 100644 --- a/tmk_core/protocol/ibm4704.c +++ b/tmk_core/protocol/ibm4704.c @@ -7,24 +7,21 @@ Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com> #include "ring_buffer.h" #include "ibm4704.h" - -#define WAIT(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - ibm4704_error = err; \ - goto ERROR; \ - } \ -} while (0) - +#define WAIT(stat, us, err) \ + do { \ + if (!wait_##stat(us)) { \ + ibm4704_error = err; \ + goto ERROR; \ + } \ + } while (0) uint8_t ibm4704_error = 0; - -void ibm4704_init(void) -{ +void ibm4704_init(void) { inhibit(); // keep keyboard from sending IBM4704_INT_INIT(); IBM4704_INT_ON(); - idle(); // allow keyboard sending + idle(); // allow keyboard sending } /* @@ -46,9 +43,8 @@ Timing: After Request keyboard pull up Data and down Clock line to low for s Host writes a bit while Clock is hi and Keyboard reads while low. Stop bit: Host releases or pulls up Data line to hi after 9th clock and waits for keyboard pull down the line to lo. */ -uint8_t ibm4704_send(uint8_t data) -{ - bool parity = true; // odd parity +uint8_t ibm4704_send(uint8_t data) { + bool parity = true; // odd parity ibm4704_error = 0; IBM4704_INT_OFF(); @@ -62,19 +58,23 @@ uint8_t ibm4704_send(uint8_t data) /* Data bit */ for (uint8_t i = 0; i < 8; i++) { - WAIT(clock_hi, 100, 0x40+i); - if (data&(1<<i)) { + WAIT(clock_hi, 100, 0x40 + i); + if (data & (1 << i)) { parity = !parity; data_hi(); } else { data_lo(); } - WAIT(clock_lo, 100, 0x48+i); + WAIT(clock_lo, 100, 0x48 + i); } /* Parity bit */ WAIT(clock_hi, 100, 0x34); - if (parity) { data_hi(); } else { data_lo(); } + if (parity) { + data_hi(); + } else { + data_lo(); + } WAIT(clock_lo, 100, 0x35); /* Stop bit */ @@ -97,16 +97,14 @@ ERROR: } /* wait forever to receive data */ -uint8_t ibm4704_recv_response(void) -{ +uint8_t ibm4704_recv_response(void) { while (!rbuf_has_data()) { _delay_ms(1); } return rbuf_dequeue(); } -uint8_t ibm4704_recv(void) -{ +uint8_t ibm4704_recv(void) { if (rbuf_has_data()) { return rbuf_dequeue(); } else { @@ -121,7 +119,7 @@ Data bits are LSB first and Parity is odd. Clock has around 60us high and 30us l ____ __ __ __ __ __ __ __ __ __ _______ Clock \_____/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ - ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ + ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ Data ____/ X____X____X____X____X____X____X____X____X____X________ Start 0 1 2 3 4 5 6 7 P Stop @@ -130,11 +128,8 @@ Inhibit: Pull Data line down to inhibit keyboard to send. Timing: Host reads bit while Clock is hi.(rising edge) Stop bit: Keyboard pulls down Data line to lo after 9th clock. */ -ISR(IBM4704_INT_VECT) -{ - static enum { - BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP - } state = BIT0; +ISR(IBM4704_INT_VECT) { + static enum { BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, PARITY, STOP } state = BIT0; // LSB first static uint8_t data = 0; // Odd parity @@ -161,8 +156,7 @@ ISR(IBM4704_INT_VECT) if (data_in()) { parity = !parity; } - if (!parity) - goto ERROR; + if (!parity) goto ERROR; break; case STOP: // Data:Low @@ -178,11 +172,11 @@ ISR(IBM4704_INT_VECT) goto RETURN; ERROR: ibm4704_error = state; - while (ibm4704_send(0xFE)) _delay_ms(1); // resend + while (ibm4704_send(0xFE)) _delay_ms(1); // resend xprintf("R:%02X%02X\n", state, data); DONE: - state = BIT0; - data = 0; + state = BIT0; + data = 0; parity = false; RETURN: return; diff --git a/tmk_core/protocol/ibm4704.h b/tmk_core/protocol/ibm4704.h index 618cce6be..cc2418ee6 100644 --- a/tmk_core/protocol/ibm4704.h +++ b/tmk_core/protocol/ibm4704.h @@ -4,105 +4,100 @@ Copyright 2014 Jun WAKO <wakojun@gmail.com> #ifndef IBM4704_H #define IBM4704_H -#define IBM4704_ERR_NONE 0 -#define IBM4704_ERR_PARITY 0x70 +#define IBM4704_ERR_NONE 0 +#define IBM4704_ERR_PARITY 0x70 - -void ibm4704_init(void); +void ibm4704_init(void); uint8_t ibm4704_send(uint8_t data); uint8_t ibm4704_recv_response(void); uint8_t ibm4704_recv(void); - /* Check pin configuration */ -#if !(defined(IBM4704_CLOCK_PORT) && \ - defined(IBM4704_CLOCK_PIN) && \ - defined(IBM4704_CLOCK_DDR) && \ - defined(IBM4704_CLOCK_BIT)) -# error "ibm4704 clock pin configuration is required in config.h" +#if !(defined(IBM4704_CLOCK_PORT) && defined(IBM4704_CLOCK_PIN) && defined(IBM4704_CLOCK_DDR) && defined(IBM4704_CLOCK_BIT)) +# error "ibm4704 clock pin configuration is required in config.h" #endif -#if !(defined(IBM4704_DATA_PORT) && \ - defined(IBM4704_DATA_PIN) && \ - defined(IBM4704_DATA_DDR) && \ - defined(IBM4704_DATA_BIT)) -# error "ibm4704 data pin configuration is required in config.h" +#if !(defined(IBM4704_DATA_PORT) && defined(IBM4704_DATA_PIN) && defined(IBM4704_DATA_DDR) && defined(IBM4704_DATA_BIT)) +# error "ibm4704 data pin configuration is required in config.h" #endif - /*-------------------------------------------------------------------- * static functions *------------------------------------------------------------------*/ -static inline void clock_lo(void) -{ - IBM4704_CLOCK_PORT &= ~(1<<IBM4704_CLOCK_BIT); - IBM4704_CLOCK_DDR |= (1<<IBM4704_CLOCK_BIT); +static inline void clock_lo(void) { + IBM4704_CLOCK_PORT &= ~(1 << IBM4704_CLOCK_BIT); + IBM4704_CLOCK_DDR |= (1 << IBM4704_CLOCK_BIT); } -static inline void clock_hi(void) -{ +static inline void clock_hi(void) { /* input with pull up */ - IBM4704_CLOCK_DDR &= ~(1<<IBM4704_CLOCK_BIT); - IBM4704_CLOCK_PORT |= (1<<IBM4704_CLOCK_BIT); + IBM4704_CLOCK_DDR &= ~(1 << IBM4704_CLOCK_BIT); + IBM4704_CLOCK_PORT |= (1 << IBM4704_CLOCK_BIT); } -static inline bool clock_in(void) -{ - IBM4704_CLOCK_DDR &= ~(1<<IBM4704_CLOCK_BIT); - IBM4704_CLOCK_PORT |= (1<<IBM4704_CLOCK_BIT); +static inline bool clock_in(void) { + IBM4704_CLOCK_DDR &= ~(1 << IBM4704_CLOCK_BIT); + IBM4704_CLOCK_PORT |= (1 << IBM4704_CLOCK_BIT); _delay_us(1); - return IBM4704_CLOCK_PIN&(1<<IBM4704_CLOCK_BIT); + return IBM4704_CLOCK_PIN & (1 << IBM4704_CLOCK_BIT); } -static inline void data_lo(void) -{ - IBM4704_DATA_PORT &= ~(1<<IBM4704_DATA_BIT); - IBM4704_DATA_DDR |= (1<<IBM4704_DATA_BIT); +static inline void data_lo(void) { + IBM4704_DATA_PORT &= ~(1 << IBM4704_DATA_BIT); + IBM4704_DATA_DDR |= (1 << IBM4704_DATA_BIT); } -static inline void data_hi(void) -{ +static inline void data_hi(void) { /* input with pull up */ - IBM4704_DATA_DDR &= ~(1<<IBM4704_DATA_BIT); - IBM4704_DATA_PORT |= (1<<IBM4704_DATA_BIT); + IBM4704_DATA_DDR &= ~(1 << IBM4704_DATA_BIT); + IBM4704_DATA_PORT |= (1 << IBM4704_DATA_BIT); } -static inline bool data_in(void) -{ - IBM4704_DATA_DDR &= ~(1<<IBM4704_DATA_BIT); - IBM4704_DATA_PORT |= (1<<IBM4704_DATA_BIT); +static inline bool data_in(void) { + IBM4704_DATA_DDR &= ~(1 << IBM4704_DATA_BIT); + IBM4704_DATA_PORT |= (1 << IBM4704_DATA_BIT); _delay_us(1); - return IBM4704_DATA_PIN&(1<<IBM4704_DATA_BIT); + return IBM4704_DATA_PIN & (1 << IBM4704_DATA_BIT); } -static inline uint16_t wait_clock_lo(uint16_t us) -{ - while (clock_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_clock_lo(uint16_t us) { + while (clock_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_clock_hi(uint16_t us) -{ - while (!clock_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_clock_hi(uint16_t us) { + while (!clock_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_data_lo(uint16_t us) -{ - while (data_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_data_lo(uint16_t us) { + while (data_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_data_hi(uint16_t us) -{ - while (!data_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_data_hi(uint16_t us) { + while (!data_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } /* idle state that device can send */ -static inline void idle(void) -{ +static inline void idle(void) { clock_hi(); data_hi(); } -/* inhibit device to send +/* inhibit device to send * keyboard checks Data line on start bit(Data:hi) and it stops sending if Data line is low. */ -static inline void inhibit(void) -{ +static inline void inhibit(void) { clock_hi(); data_lo(); } diff --git a/tmk_core/protocol/iwrap/iwrap.c b/tmk_core/protocol/iwrap/iwrap.c index 71ccc493e..05e632da3 100644 --- a/tmk_core/protocol/iwrap/iwrap.c +++ b/tmk_core/protocol/iwrap/iwrap.c @@ -37,43 +37,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "iwrap.h" #include "print.h" - /* iWRAP MUX mode utils. 3.10 HID raw mode(iWRAP_HID_Application_Note.pdf) */ -#define MUX_HEADER(LINK, LENGTH) do { \ - xmit(0xbf); /* SOF */ \ - xmit(LINK); /* Link */ \ - xmit(0x00); /* Flags */ \ - xmit(LENGTH); /* Length */ \ -} while (0) -#define MUX_FOOTER(LINK) xmit(LINK^0xff) - +#define MUX_HEADER(LINK, LENGTH) \ + do { \ + xmit(0xbf); /* SOF */ \ + xmit(LINK); /* Link */ \ + xmit(0x00); /* Flags */ \ + xmit(LENGTH); /* Length */ \ + } while (0) +#define MUX_FOOTER(LINK) xmit(LINK ^ 0xff) static uint8_t connected = 0; -//static uint8_t channel = 1; +// static uint8_t channel = 1; /* iWRAP buffer */ #define MUX_BUF_SIZE 64 -static char buf[MUX_BUF_SIZE]; +static char buf[MUX_BUF_SIZE]; static uint8_t snd_pos = 0; #define MUX_RCV_BUF_SIZE 256 -static char rcv_buf[MUX_RCV_BUF_SIZE]; +static char rcv_buf[MUX_RCV_BUF_SIZE]; static uint8_t rcv_head = 0; static uint8_t rcv_tail = 0; - /* receive buffer */ -static void rcv_enq(char c) -{ +static void rcv_enq(char c) { uint8_t next = (rcv_head + 1) % MUX_RCV_BUF_SIZE; if (next != rcv_tail) { rcv_buf[rcv_head] = c; - rcv_head = next; + rcv_head = next; } } -static char rcv_deq(void) -{ +static char rcv_deq(void) { char c = 0; if (rcv_head != rcv_tail) { c = rcv_buf[rcv_tail++]; @@ -91,38 +87,33 @@ static char rcv_peek(void) } */ -static void rcv_clear(void) -{ - rcv_tail = rcv_head = 0; -} +static void rcv_clear(void) { rcv_tail = rcv_head = 0; } /* iWRAP response */ -ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK +ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK { - if ((SUART_IN_PIN & (1<<SUART_IN_BIT))) - return; + if ((SUART_IN_PIN & (1 << SUART_IN_BIT))) return; static volatile uint8_t mux_state = 0xff; - static volatile uint8_t mux_link = 0xff; - uint8_t c = recv(); + static volatile uint8_t mux_link = 0xff; + uint8_t c = recv(); switch (mux_state) { - case 0xff: // SOF - if (c == 0xbf) - mux_state--; + case 0xff: // SOF + if (c == 0xbf) mux_state--; break; - case 0xfe: // Link + case 0xfe: // Link mux_state--; mux_link = c; break; - case 0xfd: // Flags + case 0xfd: // Flags mux_state--; break; - case 0xfc: // Length + case 0xfc: // Length mux_state = c; break; case 0x00: mux_state = 0xff; - mux_link = 0xff; + mux_link = 0xff; break; default: if (mux_state--) { @@ -132,12 +123,10 @@ ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK } } - /*------------------------------------------------------------------* * iWRAP communication *------------------------------------------------------------------*/ -void iwrap_init(void) -{ +void iwrap_init(void) { // reset iWRAP if in already MUX mode after AVR software-reset iwrap_send("RESET"); iwrap_mux_send("RESET"); @@ -147,43 +136,34 @@ void iwrap_init(void) iwrap_check_connection(); } -void iwrap_mux_send(const char *s) -{ +void iwrap_mux_send(const char *s) { rcv_clear(); MUX_HEADER(0xff, strlen((char *)s)); iwrap_send(s); MUX_FOOTER(0xff); } -void iwrap_send(const char *s) -{ - while (*s) - xmit(*s++); +void iwrap_send(const char *s) { + while (*s) xmit(*s++); } /* send buffer */ -void iwrap_buf_add(uint8_t c) -{ +void iwrap_buf_add(uint8_t c) { // need space for '\0' - if (snd_pos < MUX_BUF_SIZE-1) - buf[snd_pos++] = c; + if (snd_pos < MUX_BUF_SIZE - 1) buf[snd_pos++] = c; } -void iwrap_buf_del(void) -{ - if (snd_pos) - snd_pos--; +void iwrap_buf_del(void) { + if (snd_pos) snd_pos--; } -void iwrap_buf_send(void) -{ +void iwrap_buf_send(void) { buf[snd_pos] = '\0'; - snd_pos = 0; + snd_pos = 0; iwrap_mux_send(buf); } -void iwrap_call(void) -{ +void iwrap_call(void) { char *p; iwrap_mux_send("SET BT PAIR"); @@ -193,7 +173,7 @@ void iwrap_call(void) while (!strncmp(p, "SET BT PAIR", 11)) { p += 7; strncpy(p, "CALL", 4); - strncpy(p+22, " 11 HID\n\0", 9); + strncpy(p + 22, " 11 HID\n\0", 9); print_S(p); iwrap_mux_send(p); // TODO: skip to next line @@ -224,20 +204,21 @@ void iwrap_call(void) iwrap_check_connection(); } -void iwrap_kill(void) -{ +void iwrap_kill(void) { char c; iwrap_mux_send("LIST"); _delay_ms(500); - while ((c = rcv_deq()) && c != '\n') ; + while ((c = rcv_deq()) && c != '\n') + ; if (strncmp(rcv_buf + rcv_tail, "LIST ", 5)) { print("no connection to kill.\n"); return; } // skip 10 'space' chars for (uint8_t i = 10; i; i--) - while ((c = rcv_deq()) && c != ' ') ; + while ((c = rcv_deq()) && c != ' ') + ; char *p = rcv_buf + rcv_tail - 5; strncpy(p, "KILL ", 5); @@ -249,47 +230,34 @@ void iwrap_kill(void) iwrap_check_connection(); } -void iwrap_unpair(void) -{ +void iwrap_unpair(void) { iwrap_mux_send("SET BT PAIR"); _delay_ms(500); char *p = rcv_buf + rcv_tail; if (!strncmp(p, "SET BT PAIR", 11)) { - strncpy(p+29, "\n\0", 2); + strncpy(p + 29, "\n\0", 2); print_S(p); iwrap_mux_send(p); } } -void iwrap_sleep(void) -{ - iwrap_mux_send("SLEEP"); -} +void iwrap_sleep(void) { iwrap_mux_send("SLEEP"); } -void iwrap_sniff(void) -{ -} +void iwrap_sniff(void) {} -void iwrap_subrate(void) -{ -} +void iwrap_subrate(void) {} -bool iwrap_failed(void) -{ +bool iwrap_failed(void) { if (strncmp(rcv_buf, "SYNTAX ERROR", 12)) return true; else return false; } -uint8_t iwrap_connected(void) -{ - return connected; -} +uint8_t iwrap_connected(void) { return connected; } -uint8_t iwrap_check_connection(void) -{ +uint8_t iwrap_check_connection(void) { iwrap_mux_send("LIST"); _delay_ms(100); @@ -300,44 +268,31 @@ uint8_t iwrap_check_connection(void) return connected; } - /*------------------------------------------------------------------* * Host driver *------------------------------------------------------------------*/ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); - -static host_driver_t driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; - -host_driver_t *iwrap_driver(void) -{ - return &driver; -} +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); -static uint8_t keyboard_leds(void) { - return 0; -} +static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -static void send_keyboard(report_keyboard_t *report) -{ +host_driver_t *iwrap_driver(void) { return &driver; } + +static uint8_t keyboard_leds(void) { return 0; } + +static void send_keyboard(report_keyboard_t *report) { if (!iwrap_connected() && !iwrap_check_connection()) return; MUX_HEADER(0x01, 0x0c); // HID raw mode header xmit(0x9f); - xmit(0x0a); // Length - xmit(0xa1); // DATA(Input) - xmit(0x01); // Report ID + xmit(0x0a); // Length + xmit(0xa1); // DATA(Input) + xmit(0x01); // Report ID xmit(report->mods); - xmit(0x00); // reserved byte(always 0) + xmit(0x00); // reserved byte(always 0) xmit(report->keys[0]); xmit(report->keys[1]); xmit(report->keys[2]); @@ -347,16 +302,15 @@ static void send_keyboard(report_keyboard_t *report) MUX_FOOTER(0x01); } -static void send_mouse(report_mouse_t *report) -{ +static void send_mouse(report_mouse_t *report) { #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE) if (!iwrap_connected() && !iwrap_check_connection()) return; MUX_HEADER(0x01, 0x09); // HID raw mode header xmit(0x9f); - xmit(0x07); // Length - xmit(0xa1); // DATA(Input) - xmit(0x02); // Report ID + xmit(0x07); // Length + xmit(0xa1); // DATA(Input) + xmit(0x02); // Report ID xmit(report->buttons); xmit(report->x); xmit(report->y); @@ -366,18 +320,14 @@ static void send_mouse(report_mouse_t *report) #endif } -static void send_system(uint16_t data) -{ - /* not supported */ -} +static void send_system(uint16_t data) { /* not supported */ } -static void send_consumer(uint16_t data) -{ +static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE static uint16_t last_data = 0; - uint8_t bits1 = 0; - uint8_t bits2 = 0; - uint8_t bits3 = 0; + uint8_t bits1 = 0; + uint8_t bits2 = 0; + uint8_t bits3 = 0; if (!iwrap_connected() && !iwrap_check_connection()) return; if (data == last_data) return; @@ -458,9 +408,9 @@ static void send_consumer(uint16_t data) MUX_HEADER(0x01, 0x07); xmit(0x9f); - xmit(0x05); // Length - xmit(0xa1); // DATA(Input) - xmit(0x03); // Report ID + xmit(0x05); // Length + xmit(0xa1); // DATA(Input) + xmit(0x03); // Report ID xmit(bits1); xmit(bits2); xmit(bits3); diff --git a/tmk_core/protocol/iwrap/iwrap.h b/tmk_core/protocol/iwrap/iwrap.h index ffaad9395..51f2b5670 100644 --- a/tmk_core/protocol/iwrap/iwrap.h +++ b/tmk_core/protocol/iwrap/iwrap.h @@ -22,11 +22,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdbool.h> #include "host_driver.h" - /* enable iWRAP MUX mode */ #define MUX_MODE - host_driver_t *iwrap_driver(void); void iwrap_init(void); @@ -36,13 +34,13 @@ void iwrap_buf_send(void); void iwrap_buf_add(uint8_t c); void iwrap_buf_del(void); -void iwrap_call(void); -void iwrap_kill(void); -void iwrap_unpair(void); -void iwrap_sleep(void); -void iwrap_sniff(void); -void iwrap_subrate(void); -bool iwrap_failed(void); +void iwrap_call(void); +void iwrap_kill(void); +void iwrap_unpair(void); +void iwrap_sleep(void); +void iwrap_sniff(void); +void iwrap_subrate(void); +bool iwrap_failed(void); uint8_t iwrap_connected(void); uint8_t iwrap_check_connection(void); diff --git a/tmk_core/protocol/iwrap/main.c b/tmk_core/protocol/iwrap/main.c index 3abdce8df..7ba780ede 100644 --- a/tmk_core/protocol/iwrap/main.c +++ b/tmk_core/protocol/iwrap/main.c @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <avr/interrupt.h> #include <avr/io.h> //#include <avr/wdt.h> -#include "wd.h" // in order to use watchdog in interrupt mode +#include "wd.h" // in order to use watchdog in interrupt mode #include <avr/sleep.h> #include <util/delay.h> #include <avr/power.h> @@ -28,8 +28,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "action.h" #include "iwrap.h" #ifdef PROTOCOL_VUSB -# include "vusb.h" -# include "usbdrv.h" +# include "vusb.h" +# include "usbdrv.h" #endif #include "uart.h" #include "suart.h" @@ -38,13 +38,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "keycode.h" #include "command.h" - -static void sleep(uint8_t term); -static bool console(void); -static bool console_command(uint8_t c); +static void sleep(uint8_t term); +static bool console(void); +static bool console_command(uint8_t c); static uint8_t key2asc(uint8_t key); - /* static void set_prr(void) { @@ -78,37 +76,32 @@ static void pullup_pins(void) } */ - #ifdef PROTOCOL_VUSB -static void disable_vusb(void) -{ +static void disable_vusb(void) { // disable interrupt & disconnect to prevent host from enumerating USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT); usbDeviceDisconnect(); } -static void enable_vusb(void) -{ +static void enable_vusb(void) { USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT); usbDeviceConnect(); } -static void init_vusb(void) -{ +static void init_vusb(void) { uint8_t i = 0; usbInit(); disable_vusb(); /* fake USB disconnect for > 250 ms */ - while(--i){ + while (--i) { _delay_ms(1); } enable_vusb(); } #endif -void change_driver(host_driver_t *driver) -{ +void change_driver(host_driver_t *driver) { /* host_clear_keyboard_report(); host_swap_keyboard_report(); @@ -120,20 +113,18 @@ void change_driver(host_driver_t *driver) host_set_driver(driver); } - -static bool sleeping = false; -static bool insomniac = false; // TODO: should be false for power saving +static bool sleeping = false; +static bool insomniac = false; // TODO: should be false for power saving static uint16_t last_timer = 0; -int main(void) -{ +int main(void) { MCUSR = 0; clock_prescale_set(clock_div_1); WD_SET(WD_OFF); // power saving: the result is worse than nothing... why? - //pullup_pins(); - //set_prr(); + // pullup_pins(); + // set_prr(); #ifdef PROTOCOL_VUSB disable_vusb(); @@ -146,11 +137,11 @@ int main(void) print("suart init\n"); // suart init // PC4: Tx Output IDLE(Hi) - PORTC |= (1<<4); - DDRC |= (1<<4); + PORTC |= (1 << 4); + DDRC |= (1 << 4); // PC5: Rx Input(pull-up) - PORTC |= (1<<5); - DDRC &= ~(1<<5); + PORTC |= (1 << 5); + DDRC &= ~(1 << 5); // suart receive interrut(PC5/PCINT13) PCMSK1 = 0b00100000; PCICR = 0b00000010; @@ -164,18 +155,16 @@ int main(void) last_timer = timer_read(); while (true) { #ifdef PROTOCOL_VUSB - if (host_get_driver() == vusb_driver()) - usbPoll(); + if (host_get_driver() == vusb_driver()) usbPoll(); #endif keyboard_task(); #ifdef PROTOCOL_VUSB - if (host_get_driver() == vusb_driver()) - vusb_transfer_keyboard(); + if (host_get_driver() == vusb_driver()) vusb_transfer_keyboard(); #endif // TODO: depricated if (matrix_is_modified() || console()) { last_timer = timer_read(); - sleeping = false; + sleeping = false; } else if (!sleeping && timer_elapsed(last_timer) > 4000) { sleeping = true; iwrap_check_connection(); @@ -184,7 +173,7 @@ int main(void) // TODO: suspend.h if (host_get_driver() == iwrap_driver()) { if (sleeping && !insomniac) { - _delay_ms(1); // wait for UART to send + _delay_ms(1); // wait for UART to send iwrap_sleep(); sleep(WDTO_60MS); } @@ -192,8 +181,7 @@ int main(void) } } -static void sleep(uint8_t term) -{ +static void sleep(uint8_t term) { WD_SET(WD_IRQ, term); cli(); @@ -207,51 +195,46 @@ static void sleep(uint8_t term) WD_SET(WD_OFF); } -static bool console(void) -{ - // Send to Bluetoot module WT12 - static bool breaked = false; - if (!uart_available()) - return false; - else { - uint8_t c; - c = uart_getchar(); - uart_putchar(c); - switch (c) { - case 0x00: // BREAK signal - if (!breaked) { - print("break(? for help): "); - breaked = true; - } - break; - case '\r': - uart_putchar('\n'); - iwrap_buf_send(); - break; - case '\b': - iwrap_buf_del(); - break; - default: - if (breaked) { - print("\n"); - console_command(c); - breaked = false; - } else { - iwrap_buf_add(c); - } - break; - } - return true; +static bool console(void) { + // Send to Bluetoot module WT12 + static bool breaked = false; + if (!uart_available()) + return false; + else { + uint8_t c; + c = uart_getchar(); + uart_putchar(c); + switch (c) { + case 0x00: // BREAK signal + if (!breaked) { + print("break(? for help): "); + breaked = true; + } + break; + case '\r': + uart_putchar('\n'); + iwrap_buf_send(); + break; + case '\b': + iwrap_buf_del(); + break; + default: + if (breaked) { + print("\n"); + console_command(c); + breaked = false; + } else { + iwrap_buf_add(c); + } + break; } + return true; + } } -bool command_extra(uint8_t code) -{ - return console_command(key2asc(code)); -} +bool command_extra(uint8_t code) { return console_command(key2asc(code)); } -static bool console_command(uint8_t c) -{ +static bool console_command(uint8_t c) { switch (c) { case 'h': case '?': @@ -287,11 +270,11 @@ static bool console_command(uint8_t c) print("USB mode\n"); init_vusb(); change_driver(vusb_driver()); - //iwrap_kill(); - //iwrap_sleep(); + // iwrap_kill(); + // iwrap_sleep(); // disable suart receive interrut(PC5/PCINT13) PCMSK1 &= ~(0b00100000); - PCICR &= ~(0b00000010); + PCICR &= ~(0b00000010); return 1; case 'w': print("iWRAP mode\n"); @@ -299,7 +282,7 @@ static bool console_command(uint8_t c) disable_vusb(); // enable suart receive interrut(PC5/PCINT13) PCMSK1 |= 0b00100000; - PCICR |= 0b00000010; + PCICR |= 0b00000010; return 1; #endif case 'k': @@ -315,62 +298,115 @@ static bool console_command(uint8_t c) } // convert keycode into ascii charactor -static uint8_t key2asc(uint8_t key) -{ +static uint8_t key2asc(uint8_t key) { switch (key) { - case KC_A: return 'a'; - case KC_B: return 'b'; - case KC_C: return 'c'; - case KC_D: return 'd'; - case KC_E: return 'e'; - case KC_F: return 'f'; - case KC_G: return 'g'; - case KC_H: return 'h'; - case KC_I: return 'i'; - case KC_J: return 'j'; - case KC_K: return 'k'; - case KC_L: return 'l'; - case KC_M: return 'm'; - case KC_N: return 'n'; - case KC_O: return 'o'; - case KC_P: return 'p'; - case KC_Q: return 'q'; - case KC_R: return 'r'; - case KC_S: return 's'; - case KC_T: return 't'; - case KC_U: return 'u'; - case KC_V: return 'v'; - case KC_W: return 'w'; - case KC_X: return 'x'; - case KC_Y: return 'y'; - case KC_Z: return 'z'; - case KC_1: return '1'; - case KC_2: return '2'; - case KC_3: return '3'; - case KC_4: return '4'; - case KC_5: return '5'; - case KC_6: return '6'; - case KC_7: return '7'; - case KC_8: return '8'; - case KC_9: return '9'; - case KC_0: return '0'; - case KC_ENTER: return '\n'; - case KC_ESCAPE: return 0x1B; - case KC_BSPACE: return '\b'; - case KC_TAB: return '\t'; - case KC_SPACE: return ' '; - case KC_MINUS: return '-'; - case KC_EQUAL: return '='; - case KC_LBRACKET: return '['; - case KC_RBRACKET: return ']'; - case KC_BSLASH: return '\\'; - case KC_NONUS_HASH: return '\\'; - case KC_SCOLON: return ';'; - case KC_QUOTE: return '\''; - case KC_GRAVE: return '`'; - case KC_COMMA: return ','; - case KC_DOT: return '.'; - case KC_SLASH: return '/'; - default: return 0x00; + case KC_A: + return 'a'; + case KC_B: + return 'b'; + case KC_C: + return 'c'; + case KC_D: + return 'd'; + case KC_E: + return 'e'; + case KC_F: + return 'f'; + case KC_G: + return 'g'; + case KC_H: + return 'h'; + case KC_I: + return 'i'; + case KC_J: + return 'j'; + case KC_K: + return 'k'; + case KC_L: + return 'l'; + case KC_M: + return 'm'; + case KC_N: + return 'n'; + case KC_O: + return 'o'; + case KC_P: + return 'p'; + case KC_Q: + return 'q'; + case KC_R: + return 'r'; + case KC_S: + return 's'; + case KC_T: + return 't'; + case KC_U: + return 'u'; + case KC_V: + return 'v'; + case KC_W: + return 'w'; + case KC_X: + return 'x'; + case KC_Y: + return 'y'; + case KC_Z: + return 'z'; + case KC_1: + return '1'; + case KC_2: + return '2'; + case KC_3: + return '3'; + case KC_4: + return '4'; + case KC_5: + return '5'; + case KC_6: + return '6'; + case KC_7: + return '7'; + case KC_8: + return '8'; + case KC_9: + return '9'; + case KC_0: + return '0'; + case KC_ENTER: + return '\n'; + case KC_ESCAPE: + return 0x1B; + case KC_BSPACE: + return '\b'; + case KC_TAB: + return '\t'; + case KC_SPACE: + return ' '; + case KC_MINUS: + return '-'; + case KC_EQUAL: + return '='; + case KC_LBRACKET: + return '['; + case KC_RBRACKET: + return ']'; + case KC_BSLASH: + return '\\'; + case KC_NONUS_HASH: + return '\\'; + case KC_SCOLON: + return ';'; + case KC_QUOTE: + return '\''; + case KC_GRAVE: + return '`'; + case KC_COMMA: + return ','; + case KC_DOT: + return '.'; + case KC_SLASH: + return '/'; + default: + return 0x00; } } diff --git a/tmk_core/protocol/iwrap/suart.h b/tmk_core/protocol/iwrap/suart.h index 7d92be069..c634bbc2f 100644 --- a/tmk_core/protocol/iwrap/suart.h +++ b/tmk_core/protocol/iwrap/suart.h @@ -1,8 +1,8 @@ #ifndef SUART #define SUART -void xmit(uint8_t); +void xmit(uint8_t); uint8_t rcvr(void); uint8_t recv(void); -#endif /* SUART */ +#endif /* SUART */ diff --git a/tmk_core/protocol/iwrap/wd.h b/tmk_core/protocol/iwrap/wd.h index 12395bf69..083d6d44d 100644 --- a/tmk_core/protocol/iwrap/wd.h +++ b/tmk_core/protocol/iwrap/wd.h @@ -61,55 +61,58 @@ cleared on every power up or reset, along with disabling the watchdog- */ -//reset registers to the same name (MCUCSR) +// reset registers to the same name (MCUCSR) #if !defined(MCUCSR) -#define MCUCSR MCUSR +# define MCUCSR MCUSR #endif -//watchdog registers to the same name (WDTCSR) +// watchdog registers to the same name (WDTCSR) #if !defined(WDTCSR) -#define WDTCSR WDTCR +# define WDTCSR WDTCR #endif -//if enhanced watchdog, define irq values, create disable macro +// if enhanced watchdog, define irq values, create disable macro #if defined(WDIF) -#define WD_IRQ 0xC0 -#define WD_RST_IRQ 0xC8 -#define WD_DISABLE() do{ \ - MCUCSR &= ~(1<<WDRF); \ - WD_SET(WD_OFF); \ - }while(0) +# define WD_IRQ 0xC0 +# define WD_RST_IRQ 0xC8 +# define WD_DISABLE() \ + do { \ + MCUCSR &= ~(1 << WDRF); \ + WD_SET(WD_OFF); \ + } while (0) #endif -//all watchdogs -#define WD_RST 8 -#define WD_OFF 0 - -//prescale values -#define WDTO_15MS 0 -#define WDTO_30MS 1 -#define WDTO_60MS 2 -#define WDTO_120MS 3 -#define WDTO_250MS 4 -#define WDTO_500MS 5 -#define WDTO_1S 6 -#define WDTO_2S 7 - -//prescale values for avrs with WDP3 +// all watchdogs +#define WD_RST 8 +#define WD_OFF 0 + +// prescale values +#define WDTO_15MS 0 +#define WDTO_30MS 1 +#define WDTO_60MS 2 +#define WDTO_120MS 3 +#define WDTO_250MS 4 +#define WDTO_500MS 5 +#define WDTO_1S 6 +#define WDTO_2S 7 + +// prescale values for avrs with WDP3 #if defined(WDP3) -#define WDTO_4S 0x20 -#define WDTO_8S 0x21 +# define WDTO_4S 0x20 +# define WDTO_8S 0x21 #endif -//watchdog reset -#define WDR() __asm__ __volatile__("wdr") +// watchdog reset +#define WDR() __asm__ __volatile__("wdr") -//avr reset using watchdog -#define WD_AVR_RESET() do{ \ - __asm__ __volatile__("cli"); \ - WD_SET_UNSAFE(WD_RST); \ - while(1); \ - }while(0) +// avr reset using watchdog +#define WD_AVR_RESET() \ + do { \ + __asm__ __volatile__("cli"); \ + WD_SET_UNSAFE(WD_RST); \ + while (1) \ + ; \ + } while (0) /*set the watchdog- 1. save SREG @@ -119,41 +122,40 @@ cleared on every power up or reset, along with disabling the watchdog- 5. write watchdog value 6. restore SREG (restoring irq status) */ -#define WD_SET(val,...) \ - __asm__ __volatile__( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %[wdreg],%[wden]" "\n\t" \ - "sts %[wdreg],%[wdval]" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - : \ - : [wdreg] "M" (&WDTCSR), \ - [wden] "r" ((uint8_t)(0x18)), \ - [wdval] "r" ((uint8_t)(val|(__VA_ARGS__+0))) \ - : "r0" \ -) +#define WD_SET(val, ...) \ + __asm__ __volatile__("in __tmp_reg__,__SREG__" \ + "\n\t" \ + "cli" \ + "\n\t" \ + "wdr" \ + "\n\t" \ + "sts %[wdreg],%[wden]" \ + "\n\t" \ + "sts %[wdreg],%[wdval]" \ + "\n\t" \ + "out __SREG__,__tmp_reg__" \ + "\n\t" \ + : \ + : [ wdreg ] "M"(&WDTCSR), [ wden ] "r"((uint8_t)(0x18)), [ wdval ] "r"((uint8_t)(val | (__VA_ARGS__ + 0))) \ + : "r0") /*set the watchdog when I bit in SREG known to be clear- 1. reset watchdog timer 2. enable watchdog change 5. write watchdog value */ -#define WD_SET_UNSAFE(val,...) \ - __asm__ __volatile__( \ - "wdr" "\n\t" \ - "sts %[wdreg],%[wden]" "\n\t" \ - "sts %[wdreg],%[wdval]" "\n\t" \ - : \ - : [wdreg] "M" (&WDTCSR), \ - [wden] "r" ((uint8_t)(0x18)), \ - [wdval] "r" ((uint8_t)(val|(__VA_ARGS__+0))) \ -) - - -//for compatibility with avr/wdt.h -#define wdt_enable(val) WD_SET(WD_RST,val) -#define wdt_disable() WD_SET(WD_OFF) - +#define WD_SET_UNSAFE(val, ...) \ + __asm__ __volatile__("wdr" \ + "\n\t" \ + "sts %[wdreg],%[wden]" \ + "\n\t" \ + "sts %[wdreg],%[wdval]" \ + "\n\t" \ + : \ + : [ wdreg ] "M"(&WDTCSR), [ wden ] "r"((uint8_t)(0x18)), [ wdval ] "r"((uint8_t)(val | (__VA_ARGS__ + 0)))) + +// for compatibility with avr/wdt.h +#define wdt_enable(val) WD_SET(WD_RST, val) +#define wdt_disable() WD_SET(WD_OFF) #endif /* _AVR_WD_H_ */ diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 80839731f..71d0936f8 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -15,35 +15,34 @@ // You may define them to something else in your config.h // if yours is wired up differently. #ifndef AdafruitBleResetPin -#define AdafruitBleResetPin D4 +# define AdafruitBleResetPin D4 #endif #ifndef AdafruitBleCSPin -#define AdafruitBleCSPin B4 +# define AdafruitBleCSPin B4 #endif #ifndef AdafruitBleIRQPin -#define AdafruitBleIRQPin E6 +# define AdafruitBleIRQPin E6 #endif - #define SAMPLE_BATTERY #define ConnectionUpdateInterval 1000 /* milliseconds */ static struct { - bool is_connected; - bool initialized; - bool configured; + bool is_connected; + bool initialized; + bool configured; #define ProbedEvents 1 #define UsingEvents 2 - bool event_flags; + bool event_flags; #ifdef SAMPLE_BATTERY - uint16_t last_battery_update; - uint32_t vbat; + uint16_t last_battery_update; + uint32_t vbat; #endif - uint16_t last_connection_update; + uint16_t last_connection_update; } state; // Commands are encoded using SDEP and sent via SPI @@ -51,14 +50,14 @@ static struct { #define SdepMaxPayload 16 struct sdep_msg { - uint8_t type; - uint8_t cmd_low; - uint8_t cmd_high; - struct __attribute__((packed)) { - uint8_t len:7; - uint8_t more:1; - }; - uint8_t payload[SdepMaxPayload]; + uint8_t type; + uint8_t cmd_low; + uint8_t cmd_high; + struct __attribute__((packed)) { + uint8_t len : 7; + uint8_t more : 1; + }; + uint8_t payload[SdepMaxPayload]; } __attribute__((packed)); // The recv latency is relatively high, so when we're hammering keys quickly, @@ -68,28 +67,28 @@ struct sdep_msg { // information here. enum queue_type { - QTKeyReport, // 1-byte modifier + 6-byte key report - QTConsumer, // 16-bit key code + QTKeyReport, // 1-byte modifier + 6-byte key report + QTConsumer, // 16-bit key code #ifdef MOUSE_ENABLE - QTMouseMove, // 4-byte mouse report + QTMouseMove, // 4-byte mouse report #endif }; struct queue_item { - enum queue_type queue_type; - uint16_t added; - union __attribute__((packed)) { - struct __attribute__((packed)) { - uint8_t modifier; - uint8_t keys[6]; - } key; - - uint16_t consumer; - struct __attribute__((packed)) { - int8_t x, y, scroll, pan; - uint8_t buttons; - } mousemove; - }; + enum queue_type queue_type; + uint16_t added; + union __attribute__((packed)) { + struct __attribute__((packed)) { + uint8_t modifier; + uint8_t keys[6]; + } key; + + uint16_t consumer; + struct __attribute__((packed)) { + int8_t x, y, scroll, pan; + uint8_t buttons; + } mousemove; + }; }; // Items that we wish to send @@ -102,108 +101,104 @@ static RingBuffer<uint16_t, 2> resp_buf; static bool process_queue_item(struct queue_item *item, uint16_t timeout); enum sdep_type { - SdepCommand = 0x10, - SdepResponse = 0x20, - SdepAlert = 0x40, - SdepError = 0x80, - SdepSlaveNotReady = 0xfe, // Try again later - SdepSlaveOverflow = 0xff, // You read more data than is available + SdepCommand = 0x10, + SdepResponse = 0x20, + SdepAlert = 0x40, + SdepError = 0x80, + SdepSlaveNotReady = 0xfe, // Try again later + SdepSlaveOverflow = 0xff, // You read more data than is available }; enum ble_cmd { - BleInitialize = 0xbeef, - BleAtWrapper = 0x0a00, - BleUartTx = 0x0a01, - BleUartRx = 0x0a02, + BleInitialize = 0xbeef, + BleAtWrapper = 0x0a00, + BleUartTx = 0x0a01, + BleUartRx = 0x0a02, }; enum ble_system_event_bits { - BleSystemConnected = 0, - BleSystemDisconnected = 1, - BleSystemUartRx = 8, - BleSystemMidiRx = 10, + BleSystemConnected = 0, + BleSystemDisconnected = 1, + BleSystemUartRx = 8, + BleSystemMidiRx = 10, }; // The SDEP.md file says 2MHz but the web page and the sample driver // both use 4MHz #define SpiBusSpeed 4000000 -#define SdepTimeout 150 /* milliseconds */ -#define SdepShortTimeout 10 /* milliseconds */ -#define SdepBackOff 25 /* microseconds */ +#define SdepTimeout 150 /* milliseconds */ +#define SdepShortTimeout 10 /* milliseconds */ +#define SdepBackOff 25 /* microseconds */ #define BatteryUpdateInterval 10000 /* milliseconds */ -static bool at_command(const char *cmd, char *resp, uint16_t resplen, - bool verbose, uint16_t timeout = SdepTimeout); -static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, - bool verbose = false); +static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout = SdepTimeout); +static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose = false); struct SPI_Settings { - uint8_t spcr, spsr; + uint8_t spcr, spsr; }; static struct SPI_Settings spi; // Initialize 4Mhz MSBFIRST MODE0 void SPI_init(struct SPI_Settings *spi) { - spi->spcr = _BV(SPE) | _BV(MSTR); - spi->spsr = _BV(SPI2X); + spi->spcr = _BV(SPE) | _BV(MSTR); + spi->spsr = _BV(SPI2X); - static_assert(SpiBusSpeed == F_CPU / 2, "hard coded at 4Mhz"); + static_assert(SpiBusSpeed == F_CPU / 2, "hard coded at 4Mhz"); - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - // Ensure that SS is OUTPUT High - digitalWrite(B0, PinLevelHigh); - pinMode(B0, PinDirectionOutput); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + // Ensure that SS is OUTPUT High + digitalWrite(B0, PinLevelHigh); + pinMode(B0, PinDirectionOutput); - SPCR |= _BV(MSTR); - SPCR |= _BV(SPE); - pinMode(B1 /* SCK */, PinDirectionOutput); - pinMode(B2 /* MOSI */, PinDirectionOutput); - } + SPCR |= _BV(MSTR); + SPCR |= _BV(SPE); + pinMode(B1 /* SCK */, PinDirectionOutput); + pinMode(B2 /* MOSI */, PinDirectionOutput); + } } -static inline void SPI_begin(struct SPI_Settings*spi) { - SPCR = spi->spcr; - SPSR = spi->spsr; +static inline void SPI_begin(struct SPI_Settings *spi) { + SPCR = spi->spcr; + SPSR = spi->spsr; } static inline uint8_t SPI_TransferByte(uint8_t data) { - SPDR = data; - asm volatile("nop"); - while (!(SPSR & _BV(SPIF))) { - ; // wait - } - return SPDR; + SPDR = data; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + return SPDR; } static inline void spi_send_bytes(const uint8_t *buf, uint8_t len) { - if (len == 0) return; - const uint8_t *end = buf + len; - while (buf < end) { - SPDR = *buf; - while (!(SPSR & _BV(SPIF))) { - ; // wait + if (len == 0) return; + const uint8_t *end = buf + len; + while (buf < end) { + SPDR = *buf; + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + ++buf; } - ++buf; - } } -static inline uint16_t spi_read_byte(void) { - return SPI_TransferByte(0x00 /* dummy */); -} +static inline uint16_t spi_read_byte(void) { return SPI_TransferByte(0x00 /* dummy */); } static inline void spi_recv_bytes(uint8_t *buf, uint8_t len) { - const uint8_t *end = buf + len; - if (len == 0) return; - while (buf < end) { - SPDR = 0; // write a dummy to initiate read - while (!(SPSR & _BV(SPIF))) { - ; // wait + const uint8_t *end = buf + len; + if (len == 0) return; + while (buf < end) { + SPDR = 0; // write a dummy to initiate read + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + *buf = SPDR; + ++buf; } - *buf = SPDR; - ++buf; - } } #if 0 @@ -223,600 +218,572 @@ static void dump_pkt(const struct sdep_msg *msg) { // Send a single SDEP packet static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { - SPI_begin(&spi); + SPI_begin(&spi); + + digitalWrite(AdafruitBleCSPin, PinLevelLow); + uint16_t timerStart = timer_read(); + bool success = false; + bool ready = false; + + do { + ready = SPI_TransferByte(msg->type) != SdepSlaveNotReady; + if (ready) { + break; + } - digitalWrite(AdafruitBleCSPin, PinLevelLow); - uint16_t timerStart = timer_read(); - bool success = false; - bool ready = false; + // Release it and let it initialize + digitalWrite(AdafruitBleCSPin, PinLevelHigh); + _delay_us(SdepBackOff); + digitalWrite(AdafruitBleCSPin, PinLevelLow); + } while (timer_elapsed(timerStart) < timeout); - do { - ready = SPI_TransferByte(msg->type) != SdepSlaveNotReady; if (ready) { - break; + // Slave is ready; send the rest of the packet + spi_send_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); + success = true; } - // Release it and let it initialize digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); - } while (timer_elapsed(timerStart) < timeout); - - if (ready) { - // Slave is ready; send the rest of the packet - spi_send_bytes(&msg->cmd_low, - sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); - success = true; - } - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - - return success; + return success; } -static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, - const uint8_t *payload, uint8_t len, - bool moredata) { - msg->type = SdepCommand; - msg->cmd_low = command & 0xff; - msg->cmd_high = command >> 8; - msg->len = len; - msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0; +static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, const uint8_t *payload, uint8_t len, bool moredata) { + msg->type = SdepCommand; + msg->cmd_low = command & 0xff; + msg->cmd_high = command >> 8; + msg->len = len; + msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0; - static_assert(sizeof(*msg) == 20, "msg is correctly packed"); + static_assert(sizeof(*msg) == 20, "msg is correctly packed"); - memcpy(msg->payload, payload, len); + memcpy(msg->payload, payload, len); } // Read a single SDEP packet static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) { - bool success = false; - uint16_t timerStart = timer_read(); - bool ready = false; + bool success = false; + uint16_t timerStart = timer_read(); + bool ready = false; + + do { + ready = digitalRead(AdafruitBleIRQPin); + if (ready) { + break; + } + _delay_us(1); + } while (timer_elapsed(timerStart) < timeout); - do { - ready = digitalRead(AdafruitBleIRQPin); if (ready) { - break; - } - _delay_us(1); - } while (timer_elapsed(timerStart) < timeout); + SPI_begin(&spi); - if (ready) { - SPI_begin(&spi); + digitalWrite(AdafruitBleCSPin, PinLevelLow); - digitalWrite(AdafruitBleCSPin, PinLevelLow); + do { + // Read the command type, waiting for the data to be ready + msg->type = spi_read_byte(); + if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { + // Release it and let it initialize + digitalWrite(AdafruitBleCSPin, PinLevelHigh); + _delay_us(SdepBackOff); + digitalWrite(AdafruitBleCSPin, PinLevelLow); + continue; + } + + // Read the rest of the header + spi_recv_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); + + // and get the payload if there is any + if (msg->len <= SdepMaxPayload) { + spi_recv_bytes(msg->payload, msg->len); + } + success = true; + break; + } while (timer_elapsed(timerStart) < timeout); - do { - // Read the command type, waiting for the data to be ready - msg->type = spi_read_byte(); - if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { - // Release it and let it initialize digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); - continue; - } - - // Read the rest of the header - spi_recv_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); - - // and get the payload if there is any - if (msg->len <= SdepMaxPayload) { - spi_recv_bytes(msg->payload, msg->len); - } - success = true; - break; - } while (timer_elapsed(timerStart) < timeout); - - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - } - return success; + } + return success; } static void resp_buf_read_one(bool greedy) { - uint16_t last_send; - if (!resp_buf.peek(last_send)) { - return; - } + uint16_t last_send; + if (!resp_buf.peek(last_send)) { + return; + } - if (digitalRead(AdafruitBleIRQPin)) { - struct sdep_msg msg; + if (digitalRead(AdafruitBleIRQPin)) { + struct sdep_msg msg; -again: - if (sdep_recv_pkt(&msg, SdepTimeout)) { - if (!msg.more) { - // We got it; consume this entry - resp_buf.get(last_send); - dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send)); - } + again: + if (sdep_recv_pkt(&msg, SdepTimeout)) { + if (!msg.more) { + // We got it; consume this entry + resp_buf.get(last_send); + dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send)); + } - if (greedy && resp_buf.peek(last_send) && digitalRead(AdafruitBleIRQPin)) { - goto again; - } - } + if (greedy && resp_buf.peek(last_send) && digitalRead(AdafruitBleIRQPin)) { + goto again; + } + } - } else if (timer_elapsed(last_send) > SdepTimeout * 2) { - dprintf("waiting_for_result: timeout, resp_buf size %d\n", - (int)resp_buf.size()); + } else if (timer_elapsed(last_send) > SdepTimeout * 2) { + dprintf("waiting_for_result: timeout, resp_buf size %d\n", (int)resp_buf.size()); - // Timed out: consume this entry - resp_buf.get(last_send); - } + // Timed out: consume this entry + resp_buf.get(last_send); + } } static void send_buf_send_one(uint16_t timeout = SdepTimeout) { - struct queue_item item; - - // Don't send anything more until we get an ACK - if (!resp_buf.empty()) { - return; - } - - if (!send_buf.peek(item)) { - return; - } - if (process_queue_item(&item, timeout)) { - // commit that peek - send_buf.get(item); - dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size()); - } else { - dprint("failed to send, will retry\n"); - _delay_ms(SdepTimeout); - resp_buf_read_one(true); - } + struct queue_item item; + + // Don't send anything more until we get an ACK + if (!resp_buf.empty()) { + return; + } + + if (!send_buf.peek(item)) { + return; + } + if (process_queue_item(&item, timeout)) { + // commit that peek + send_buf.get(item); + dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size()); + } else { + dprint("failed to send, will retry\n"); + _delay_ms(SdepTimeout); + resp_buf_read_one(true); + } } static void resp_buf_wait(const char *cmd) { - bool didPrint = false; - while (!resp_buf.empty()) { - if (!didPrint) { - dprintf("wait on buf for %s\n", cmd); - didPrint = true; + bool didPrint = false; + while (!resp_buf.empty()) { + if (!didPrint) { + dprintf("wait on buf for %s\n", cmd); + didPrint = true; + } + resp_buf_read_one(true); } - resp_buf_read_one(true); - } } static bool ble_init(void) { - state.initialized = false; - state.configured = false; - state.is_connected = false; + state.initialized = false; + state.configured = false; + state.is_connected = false; - pinMode(AdafruitBleIRQPin, PinDirectionInput); - pinMode(AdafruitBleCSPin, PinDirectionOutput); - digitalWrite(AdafruitBleCSPin, PinLevelHigh); + pinMode(AdafruitBleIRQPin, PinDirectionInput); + pinMode(AdafruitBleCSPin, PinDirectionOutput); + digitalWrite(AdafruitBleCSPin, PinLevelHigh); - SPI_init(&spi); + SPI_init(&spi); - // Perform a hardware reset - pinMode(AdafruitBleResetPin, PinDirectionOutput); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); - digitalWrite(AdafruitBleResetPin, PinLevelLow); - _delay_ms(10); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); + // Perform a hardware reset + pinMode(AdafruitBleResetPin, PinDirectionOutput); + digitalWrite(AdafruitBleResetPin, PinLevelHigh); + digitalWrite(AdafruitBleResetPin, PinLevelLow); + _delay_ms(10); + digitalWrite(AdafruitBleResetPin, PinLevelHigh); - _delay_ms(1000); // Give it a second to initialize + _delay_ms(1000); // Give it a second to initialize - state.initialized = true; - return state.initialized; + state.initialized = true; + return state.initialized; } -static inline uint8_t min(uint8_t a, uint8_t b) { - return a < b ? a : b; -} +static inline uint8_t min(uint8_t a, uint8_t b) { return a < b ? a : b; } static bool read_response(char *resp, uint16_t resplen, bool verbose) { - char *dest = resp; - char *end = dest + resplen; - - while (true) { - struct sdep_msg msg; - - if (!sdep_recv_pkt(&msg, 2 * SdepTimeout)) { - dprint("sdep_recv_pkt failed\n"); - return false; + char *dest = resp; + char *end = dest + resplen; + + while (true) { + struct sdep_msg msg; + + if (!sdep_recv_pkt(&msg, 2 * SdepTimeout)) { + dprint("sdep_recv_pkt failed\n"); + return false; + } + + if (msg.type != SdepResponse) { + *resp = 0; + return false; + } + + uint8_t len = min(msg.len, end - dest); + if (len > 0) { + memcpy(dest, msg.payload, len); + dest += len; + } + + if (!msg.more) { + // No more data is expected! + break; + } } - if (msg.type != SdepResponse) { - *resp = 0; - return false; - } + // Ensure the response is NUL terminated + *dest = 0; - uint8_t len = min(msg.len, end - dest); - if (len > 0) { - memcpy(dest, msg.payload, len); - dest += len; + // "Parse" the result text; we want to snip off the trailing OK or ERROR line + // Rewind past the possible trailing CRLF so that we can strip it + --dest; + while (dest > resp && (dest[0] == '\n' || dest[0] == '\r')) { + *dest = 0; + --dest; } - if (!msg.more) { - // No more data is expected! - break; + // Look back for start of preceeding line + char *last_line = strrchr(resp, '\n'); + if (last_line) { + ++last_line; + } else { + last_line = resp; } - } - - // Ensure the response is NUL terminated - *dest = 0; - // "Parse" the result text; we want to snip off the trailing OK or ERROR line - // Rewind past the possible trailing CRLF so that we can strip it - --dest; - while (dest > resp && (dest[0] == '\n' || dest[0] == '\r')) { - *dest = 0; - --dest; - } + bool success = false; + static const char kOK[] PROGMEM = "OK"; - // Look back for start of preceeding line - char *last_line = strrchr(resp, '\n'); - if (last_line) { - ++last_line; - } else { - last_line = resp; - } + success = !strcmp_P(last_line, kOK); - bool success = false; - static const char kOK[] PROGMEM = "OK"; + if (verbose || !success) { + dprintf("result: %s\n", resp); + } + return success; +} - success = !strcmp_P(last_line, kOK ); +static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout) { + const char * end = cmd + strlen(cmd); + struct sdep_msg msg; - if (verbose || !success) { - dprintf("result: %s\n", resp); - } - return success; -} + if (verbose) { + dprintf("ble send: %s\n", cmd); + } -static bool at_command(const char *cmd, char *resp, uint16_t resplen, - bool verbose, uint16_t timeout) { - const char *end = cmd + strlen(cmd); - struct sdep_msg msg; - - if (verbose) { - dprintf("ble send: %s\n", cmd); - } - - if (resp) { - // They want to decode the response, so we need to flush and wait - // for all pending I/O to finish before we start this one, so - // that we don't confuse the results - resp_buf_wait(cmd); - *resp = 0; - } - - // Fragment the command into a series of SDEP packets - while (end - cmd > SdepMaxPayload) { - sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, SdepMaxPayload, true); - if (!sdep_send_pkt(&msg, timeout)) { - return false; + if (resp) { + // They want to decode the response, so we need to flush and wait + // for all pending I/O to finish before we start this one, so + // that we don't confuse the results + resp_buf_wait(cmd); + *resp = 0; } - cmd += SdepMaxPayload; - } - sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, end - cmd, false); - if (!sdep_send_pkt(&msg, timeout)) { - return false; - } + // Fragment the command into a series of SDEP packets + while (end - cmd > SdepMaxPayload) { + sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, SdepMaxPayload, true); + if (!sdep_send_pkt(&msg, timeout)) { + return false; + } + cmd += SdepMaxPayload; + } - if (resp == NULL) { - auto now = timer_read(); - while (!resp_buf.enqueue(now)) { - resp_buf_read_one(false); + sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, end - cmd, false); + if (!sdep_send_pkt(&msg, timeout)) { + return false; } - auto later = timer_read(); - if (TIMER_DIFF_16(later, now) > 0) { - dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now)); + + if (resp == NULL) { + auto now = timer_read(); + while (!resp_buf.enqueue(now)) { + resp_buf_read_one(false); + } + auto later = timer_read(); + if (TIMER_DIFF_16(later, now) > 0) { + dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now)); + } + return true; } - return true; - } - return read_response(resp, resplen, verbose); + return read_response(resp, resplen, verbose); } bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose) { - auto cmdbuf = (char *)alloca(strlen_P(cmd) + 1); - strcpy_P(cmdbuf, cmd); - return at_command(cmdbuf, resp, resplen, verbose); + auto cmdbuf = (char *)alloca(strlen_P(cmd) + 1); + strcpy_P(cmdbuf, cmd); + return at_command(cmdbuf, resp, resplen, verbose); } -bool adafruit_ble_is_connected(void) { - return state.is_connected; -} +bool adafruit_ble_is_connected(void) { return state.is_connected; } bool adafruit_ble_enable_keyboard(void) { - char resbuf[128]; - - if (!state.initialized && !ble_init()) { - return false; - } - - state.configured = false; - - // Disable command echo - static const char kEcho[] PROGMEM = "ATE=0"; - // Make the advertised name match the keyboard - static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); - // Turn on keyboard support - static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; - - // Adjust intervals to improve latency. This causes the "central" - // system (computer/tablet) to poll us every 10-30 ms. We can't - // set a smaller value than 10ms, and 30ms seems to be the natural - // processing time on my macbook. Keeping it constrained to that - // feels reasonable to type to. - static const char kGapIntervals[] PROGMEM = "AT+GAPINTERVALS=10,30,,"; - - // Reset the device so that it picks up the above changes - static const char kATZ[] PROGMEM = "ATZ"; - - // Turn down the power level a bit - static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12"; - static PGM_P const configure_commands[] PROGMEM = { - kEcho, - kGapIntervals, - kGapDevName, - kHidEnOn, - kPower, - kATZ, - }; - - uint8_t i; - for (i = 0; i < sizeof(configure_commands) / sizeof(configure_commands[0]); - ++i) { - PGM_P cmd; - memcpy_P(&cmd, configure_commands + i, sizeof(cmd)); - - if (!at_command_P(cmd, resbuf, sizeof(resbuf))) { - dprintf("failed BLE command: %S: %s\n", cmd, resbuf); - goto fail; - } - } - - state.configured = true; - - // Check connection status in a little while; allow the ATZ time - // to kick in. - state.last_connection_update = timer_read(); + char resbuf[128]; + + if (!state.initialized && !ble_init()) { + return false; + } + + state.configured = false; + + // Disable command echo + static const char kEcho[] PROGMEM = "ATE=0"; + // Make the advertised name match the keyboard + static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); + // Turn on keyboard support + static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; + + // Adjust intervals to improve latency. This causes the "central" + // system (computer/tablet) to poll us every 10-30 ms. We can't + // set a smaller value than 10ms, and 30ms seems to be the natural + // processing time on my macbook. Keeping it constrained to that + // feels reasonable to type to. + static const char kGapIntervals[] PROGMEM = "AT+GAPINTERVALS=10,30,,"; + + // Reset the device so that it picks up the above changes + static const char kATZ[] PROGMEM = "ATZ"; + + // Turn down the power level a bit + static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12"; + static PGM_P const configure_commands[] PROGMEM = { + kEcho, kGapIntervals, kGapDevName, kHidEnOn, kPower, kATZ, + }; + + uint8_t i; + for (i = 0; i < sizeof(configure_commands) / sizeof(configure_commands[0]); ++i) { + PGM_P cmd; + memcpy_P(&cmd, configure_commands + i, sizeof(cmd)); + + if (!at_command_P(cmd, resbuf, sizeof(resbuf))) { + dprintf("failed BLE command: %S: %s\n", cmd, resbuf); + goto fail; + } + } + + state.configured = true; + + // Check connection status in a little while; allow the ATZ time + // to kick in. + state.last_connection_update = timer_read(); fail: - return state.configured; + return state.configured; } static void set_connected(bool connected) { - if (connected != state.is_connected) { - if (connected) { - print("****** BLE CONNECT!!!!\n"); - } else { - print("****** BLE DISCONNECT!!!!\n"); - } - state.is_connected = connected; - - // TODO: if modifiers are down on the USB interface and - // we cut over to BLE or vice versa, they will remain stuck. - // This feels like a good point to do something like clearing - // the keyboard and/or generating a fake all keys up message. - // However, I've noticed that it takes a couple of seconds - // for macOS to to start recognizing key presses after BLE - // is in the connected state, so I worry that doing that - // here may not be good enough. - } + if (connected != state.is_connected) { + if (connected) { + print("****** BLE CONNECT!!!!\n"); + } else { + print("****** BLE DISCONNECT!!!!\n"); + } + state.is_connected = connected; + + // TODO: if modifiers are down on the USB interface and + // we cut over to BLE or vice versa, they will remain stuck. + // This feels like a good point to do something like clearing + // the keyboard and/or generating a fake all keys up message. + // However, I've noticed that it takes a couple of seconds + // for macOS to to start recognizing key presses after BLE + // is in the connected state, so I worry that doing that + // here may not be good enough. + } } void adafruit_ble_task(void) { - char resbuf[48]; - - if (!state.configured && !adafruit_ble_enable_keyboard()) { - return; - } - resp_buf_read_one(true); - send_buf_send_one(SdepShortTimeout); - - if (resp_buf.empty() && (state.event_flags & UsingEvents) && - digitalRead(AdafruitBleIRQPin)) { - // Must be an event update - if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) { - uint32_t mask = strtoul(resbuf, NULL, 16); - - if (mask & BleSystemConnected) { - set_connected(true); - } else if (mask & BleSystemDisconnected) { - set_connected(false); - } - } - } - - if (timer_elapsed(state.last_connection_update) > ConnectionUpdateInterval) { - bool shouldPoll = true; - if (!(state.event_flags & ProbedEvents)) { - // Request notifications about connection status changes. - // This only works in SPIFRIEND firmware > 0.6.7, which is why - // we check for this conditionally here. - // Note that at the time of writing, HID reports only work correctly - // with Apple products on firmware version 0.6.7! - // https://forums.adafruit.com/viewtopic.php?f=8&t=104052 - if (at_command_P(PSTR("AT+EVENTENABLE=0x1"), resbuf, sizeof(resbuf))) { - at_command_P(PSTR("AT+EVENTENABLE=0x2"), resbuf, sizeof(resbuf)); - state.event_flags |= UsingEvents; - } - state.event_flags |= ProbedEvents; - - // leave shouldPoll == true so that we check at least once - // before relying solely on events - } else { - shouldPoll = false; - } + char resbuf[48]; - static const char kGetConn[] PROGMEM = "AT+GAPGETCONN"; - state.last_connection_update = timer_read(); + if (!state.configured && !adafruit_ble_enable_keyboard()) { + return; + } + resp_buf_read_one(true); + send_buf_send_one(SdepShortTimeout); + + if (resp_buf.empty() && (state.event_flags & UsingEvents) && digitalRead(AdafruitBleIRQPin)) { + // Must be an event update + if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) { + uint32_t mask = strtoul(resbuf, NULL, 16); + + if (mask & BleSystemConnected) { + set_connected(true); + } else if (mask & BleSystemDisconnected) { + set_connected(false); + } + } + } - if (at_command_P(kGetConn, resbuf, sizeof(resbuf))) { - set_connected(atoi(resbuf)); + if (timer_elapsed(state.last_connection_update) > ConnectionUpdateInterval) { + bool shouldPoll = true; + if (!(state.event_flags & ProbedEvents)) { + // Request notifications about connection status changes. + // This only works in SPIFRIEND firmware > 0.6.7, which is why + // we check for this conditionally here. + // Note that at the time of writing, HID reports only work correctly + // with Apple products on firmware version 0.6.7! + // https://forums.adafruit.com/viewtopic.php?f=8&t=104052 + if (at_command_P(PSTR("AT+EVENTENABLE=0x1"), resbuf, sizeof(resbuf))) { + at_command_P(PSTR("AT+EVENTENABLE=0x2"), resbuf, sizeof(resbuf)); + state.event_flags |= UsingEvents; + } + state.event_flags |= ProbedEvents; + + // leave shouldPoll == true so that we check at least once + // before relying solely on events + } else { + shouldPoll = false; + } + + static const char kGetConn[] PROGMEM = "AT+GAPGETCONN"; + state.last_connection_update = timer_read(); + + if (at_command_P(kGetConn, resbuf, sizeof(resbuf))) { + set_connected(atoi(resbuf)); + } } - } #ifdef SAMPLE_BATTERY - // I don't know if this really does anything useful yet; the reported - // voltage level always seems to be around 3200mV. We may want to just rip - // this code out. - if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && - resp_buf.empty()) { - state.last_battery_update = timer_read(); - - if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) { - state.vbat = atoi(resbuf); + // I don't know if this really does anything useful yet; the reported + // voltage level always seems to be around 3200mV. We may want to just rip + // this code out. + if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) { + state.last_battery_update = timer_read(); + + if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) { + state.vbat = atoi(resbuf); + } } - } #endif } static bool process_queue_item(struct queue_item *item, uint16_t timeout) { - char cmdbuf[48]; - char fmtbuf[64]; + char cmdbuf[48]; + char fmtbuf[64]; - // Arrange to re-check connection after keys have settled - state.last_connection_update = timer_read(); + // Arrange to re-check connection after keys have settled + state.last_connection_update = timer_read(); #if 1 - if (TIMER_DIFF_16(state.last_connection_update, item->added) > 0) { - dprintf("send latency %dms\n", - TIMER_DIFF_16(state.last_connection_update, item->added)); - } + if (TIMER_DIFF_16(state.last_connection_update, item->added) > 0) { + dprintf("send latency %dms\n", TIMER_DIFF_16(state.last_connection_update, item->added)); + } #endif - switch (item->queue_type) { - case QTKeyReport: - strcpy_P(fmtbuf, - PSTR("AT+BLEKEYBOARDCODE=%02x-00-%02x-%02x-%02x-%02x-%02x-%02x")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, - item->key.keys[0], item->key.keys[1], item->key.keys[2], - item->key.keys[3], item->key.keys[4], item->key.keys[5]); - return at_command(cmdbuf, NULL, 0, true, timeout); + switch (item->queue_type) { + case QTKeyReport: + strcpy_P(fmtbuf, PSTR("AT+BLEKEYBOARDCODE=%02x-00-%02x-%02x-%02x-%02x-%02x-%02x")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, item->key.keys[0], item->key.keys[1], item->key.keys[2], item->key.keys[3], item->key.keys[4], item->key.keys[5]); + return at_command(cmdbuf, NULL, 0, true, timeout); - case QTConsumer: - strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer); - return at_command(cmdbuf, NULL, 0, true, timeout); + case QTConsumer: + strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer); + return at_command(cmdbuf, NULL, 0, true, timeout); #ifdef MOUSE_ENABLE - case QTMouseMove: - strcpy_P(fmtbuf, PSTR("AT+BLEHIDMOUSEMOVE=%d,%d,%d,%d")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->mousemove.x, - item->mousemove.y, item->mousemove.scroll, item->mousemove.pan); - if (!at_command(cmdbuf, NULL, 0, true, timeout)) { - return false; - } - strcpy_P(cmdbuf, PSTR("AT+BLEHIDMOUSEBUTTON=")); - if (item->mousemove.buttons & MOUSE_BTN1) { - strcat(cmdbuf, "L"); - } - if (item->mousemove.buttons & MOUSE_BTN2) { - strcat(cmdbuf, "R"); - } - if (item->mousemove.buttons & MOUSE_BTN3) { - strcat(cmdbuf, "M"); - } - if (item->mousemove.buttons == 0) { - strcat(cmdbuf, "0"); - } - return at_command(cmdbuf, NULL, 0, true, timeout); + case QTMouseMove: + strcpy_P(fmtbuf, PSTR("AT+BLEHIDMOUSEMOVE=%d,%d,%d,%d")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->mousemove.x, item->mousemove.y, item->mousemove.scroll, item->mousemove.pan); + if (!at_command(cmdbuf, NULL, 0, true, timeout)) { + return false; + } + strcpy_P(cmdbuf, PSTR("AT+BLEHIDMOUSEBUTTON=")); + if (item->mousemove.buttons & MOUSE_BTN1) { + strcat(cmdbuf, "L"); + } + if (item->mousemove.buttons & MOUSE_BTN2) { + strcat(cmdbuf, "R"); + } + if (item->mousemove.buttons & MOUSE_BTN3) { + strcat(cmdbuf, "M"); + } + if (item->mousemove.buttons == 0) { + strcat(cmdbuf, "0"); + } + return at_command(cmdbuf, NULL, 0, true, timeout); #endif - default: - return true; - } -} - -bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, - uint8_t nkeys) { - struct queue_item item; - bool didWait = false; - - item.queue_type = QTKeyReport; - item.key.modifier = hid_modifier_mask; - item.added = timer_read(); - - while (nkeys >= 0) { - item.key.keys[0] = keys[0]; - item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; - item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; - item.key.keys[3] = nkeys >= 3 ? keys[3] : 0; - item.key.keys[4] = nkeys >= 4 ? keys[4] : 0; - item.key.keys[5] = nkeys >= 5 ? keys[5] : 0; - - if (!send_buf.enqueue(item)) { - if (!didWait) { - dprint("wait for buf space\n"); - didWait = true; - } - send_buf_send_one(); - continue; + default: + return true; } +} - if (nkeys <= 6) { - return true; +bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { + struct queue_item item; + bool didWait = false; + + item.queue_type = QTKeyReport; + item.key.modifier = hid_modifier_mask; + item.added = timer_read(); + + while (nkeys >= 0) { + item.key.keys[0] = keys[0]; + item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; + item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; + item.key.keys[3] = nkeys >= 3 ? keys[3] : 0; + item.key.keys[4] = nkeys >= 4 ? keys[4] : 0; + item.key.keys[5] = nkeys >= 5 ? keys[5] : 0; + + if (!send_buf.enqueue(item)) { + if (!didWait) { + dprint("wait for buf space\n"); + didWait = true; + } + send_buf_send_one(); + continue; + } + + if (nkeys <= 6) { + return true; + } + + nkeys -= 6; + keys += 6; } - nkeys -= 6; - keys += 6; - } - - return true; + return true; } bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration) { - struct queue_item item; + struct queue_item item; - item.queue_type = QTConsumer; - item.consumer = keycode; + item.queue_type = QTConsumer; + item.consumer = keycode; - while (!send_buf.enqueue(item)) { - send_buf_send_one(); - } - return true; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } + return true; } #ifdef MOUSE_ENABLE -bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, - int8_t pan, uint8_t buttons) { - struct queue_item item; - - item.queue_type = QTMouseMove; - item.mousemove.x = x; - item.mousemove.y = y; - item.mousemove.scroll = scroll; - item.mousemove.pan = pan; - item.mousemove.buttons = buttons; - - while (!send_buf.enqueue(item)) { - send_buf_send_one(); - } - return true; +bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { + struct queue_item item; + + item.queue_type = QTMouseMove; + item.mousemove.x = x; + item.mousemove.y = y; + item.mousemove.scroll = scroll; + item.mousemove.pan = pan; + item.mousemove.buttons = buttons; + + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } + return true; } #endif -uint32_t adafruit_ble_read_battery_voltage(void) { - return state.vbat; -} +uint32_t adafruit_ble_read_battery_voltage(void) { return state.vbat; } bool adafruit_ble_set_mode_leds(bool on) { - if (!state.configured) { - return false; - } - - // The "mode" led is the red blinky one - at_command_P(on ? PSTR("AT+HWMODELED=1") : PSTR("AT+HWMODELED=0"), NULL, 0); - - // Pin 19 is the blue "connected" LED; turn that off too. - // When turning LEDs back on, don't turn that LED on if we're - // not connected, as that would be confusing. - at_command_P(on && state.is_connected ? PSTR("AT+HWGPIO=19,1") - : PSTR("AT+HWGPIO=19,0"), - NULL, 0); - return true; + if (!state.configured) { + return false; + } + + // The "mode" led is the red blinky one + at_command_P(on ? PSTR("AT+HWMODELED=1") : PSTR("AT+HWMODELED=0"), NULL, 0); + + // Pin 19 is the blue "connected" LED; turn that off too. + // When turning LEDs back on, don't turn that LED on if we're + // not connected, as that would be confusing. + at_command_P(on && state.is_connected ? PSTR("AT+HWGPIO=19,1") : PSTR("AT+HWGPIO=19,0"), NULL, 0); + return true; } // https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le/ble-generic#at-plus-blepowerlevel bool adafruit_ble_set_power_level(int8_t level) { - char cmd[46]; - if (!state.configured) { - return false; - } - snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level); - return at_command(cmd, NULL, 0, false); + char cmd[46]; + if (!state.configured) { + return false; + } + snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level); + return at_command(cmd, NULL, 0, false); } diff --git a/tmk_core/protocol/lufa/adafruit_ble.h b/tmk_core/protocol/lufa/adafruit_ble.h index 5d26a9d5a..cef46fe9f 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.h +++ b/tmk_core/protocol/lufa/adafruit_ble.h @@ -4,16 +4,16 @@ */ #pragma once #ifdef MODULE_ADAFRUIT_BLE -#include <stdbool.h> -#include <stdint.h> -#include <string.h> +# include <stdbool.h> +# include <stdint.h> +# include <string.h> -#include "config_common.h" -#include "progmem.h" +# include "config_common.h" +# include "progmem.h" -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif /* Instruct the module to enable HID keyboard support and reset */ extern bool adafruit_ble_enable_keyboard(void); @@ -34,20 +34,18 @@ extern void adafruit_ble_task(void); * this set of keys. * Also sends a key release indicator, so that the keys do not remain * held down. */ -extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, - uint8_t nkeys); +extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); /* Send a consumer keycode, holding it down for the specified duration * (milliseconds) */ extern bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration); -#ifdef MOUSE_ENABLE +# ifdef MOUSE_ENABLE /* Send a mouse/wheel movement report. * The parameters are signed and indicate positive of negative direction * change. */ -extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, - int8_t pan, uint8_t buttons); -#endif +extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); +# endif /* Compute battery voltage by reading an analog pin. * Returns the integer number of millivolts */ @@ -56,8 +54,8 @@ extern uint32_t adafruit_ble_read_battery_voltage(void); extern bool adafruit_ble_set_mode_leds(bool on); extern bool adafruit_ble_set_power_level(int8_t level); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif -#endif // MODULE_ADAFRUIT_BLE +#endif // MODULE_ADAFRUIT_BLE diff --git a/tmk_core/protocol/lufa/bluetooth.c b/tmk_core/protocol/lufa/bluetooth.c index 549606162..5eb52860b 100644 --- a/tmk_core/protocol/lufa/bluetooth.c +++ b/tmk_core/protocol/lufa/bluetooth.c @@ -21,16 +21,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "debug.h" #include "bluetooth.h" -void bluefruit_keyboard_print_report(report_keyboard_t *report) -{ +void bluefruit_keyboard_print_report(report_keyboard_t *report) { if (!debug_keyboard) return; - dprintf("keys: "); for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { debug_hex8(report->keys[i]); dprintf(" "); } - dprintf(" mods: "); debug_hex8(report->mods); - dprintf(" reserved: "); debug_hex8(report->reserved); + dprintf("keys: "); + for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + debug_hex8(report->keys[i]); + dprintf(" "); + } + dprintf(" mods: "); + debug_hex8(report->mods); + dprintf(" reserved: "); + debug_hex8(report->reserved); dprintf("\n"); } -void bluefruit_serial_send(uint8_t data) -{ - serial_send(data); -}
\ No newline at end of file +void bluefruit_serial_send(uint8_t data) { serial_send(data); }
\ No newline at end of file diff --git a/tmk_core/protocol/lufa/bluetooth.h b/tmk_core/protocol/lufa/bluetooth.h index f4b2f6f8b..081271a4e 100644 --- a/tmk_core/protocol/lufa/bluetooth.h +++ b/tmk_core/protocol/lufa/bluetooth.h @@ -41,39 +41,8 @@ void bluefruit_serial_send(uint8_t data); | Stop | 00000000 00010000 | 00 10 | +-------------------------------------+-------+ */ -#define CONSUMER2BLUEFRUIT(usage) \ - (usage == AUDIO_MUTE ? 0x0000 : \ - (usage == AUDIO_VOL_UP ? 0x1000 : \ - (usage == AUDIO_VOL_DOWN ? 0x2000 : \ - (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \ - (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \ - (usage == TRANSPORT_STOP ? 0x0010 : \ - (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \ - (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \ - (usage == AL_CC_CONFIG ? 0x0000 : \ - (usage == AL_EMAIL ? 0x0000 : \ - (usage == AL_CALCULATOR ? 0x0000 : \ - (usage == AL_LOCAL_BROWSER ? 0x0000 : \ - (usage == AC_SEARCH ? 0x0400 : \ - (usage == AC_HOME ? 0x0100 : \ - (usage == AC_BACK ? 0x0000 : \ - (usage == AC_FORWARD ? 0x0000 : \ - (usage == AC_STOP ? 0x0000 : \ - (usage == AC_REFRESH ? 0x0000 : \ - (usage == AC_BOOKMARKS ? 0x0000 : 0))))))))))))))))))) +#define CONSUMER2BLUEFRUIT(usage) (usage == AUDIO_MUTE ? 0x0000 : (usage == AUDIO_VOL_UP ? 0x1000 : (usage == AUDIO_VOL_DOWN ? 0x2000 : (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : (usage == TRANSPORT_PREV_TRACK ? 0x0004 : (usage == TRANSPORT_STOP ? 0x0010 : (usage == TRANSPORT_STOP_EJECT ? 0x0000 : (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : (usage == AL_CC_CONFIG ? 0x0000 : (usage == AL_EMAIL ? 0x0000 : (usage == AL_CALCULATOR ? 0x0000 : (usage == AL_LOCAL_BROWSER ? 0x0000 : (usage == AC_SEARCH ? 0x0400 : (usage == AC_HOME ? 0x0100 : (usage == AC_BACK ? 0x0000 : (usage == AC_FORWARD ? 0x0000 : (usage == AC_STOP ? 0x0000 : (usage == AC_REFRESH ? 0x0000 : (usage == AC_BOOKMARKS ? 0x0000 : 0))))))))))))))))))) -#define CONSUMER2RN42(usage) \ - (usage == AUDIO_MUTE ? 0x0040 : \ - (usage == AUDIO_VOL_UP ? 0x0010 : \ - (usage == AUDIO_VOL_DOWN ? 0x0020 : \ - (usage == TRANSPORT_NEXT_TRACK ? 0x0100 : \ - (usage == TRANSPORT_PREV_TRACK ? 0x0200 : \ - (usage == TRANSPORT_STOP ? 0x0400 : \ - (usage == TRANSPORT_STOP_EJECT ? 0x0800 : \ - (usage == TRANSPORT_PLAY_PAUSE ? 0x0080 : \ - (usage == AL_EMAIL ? 0x0200 : \ - (usage == AL_LOCAL_BROWSER ? 0x8000 : \ - (usage == AC_SEARCH ? 0x0400 : \ - (usage == AC_HOME ? 0x0100 : 0)))))))))))) +#define CONSUMER2RN42(usage) (usage == AUDIO_MUTE ? 0x0040 : (usage == AUDIO_VOL_UP ? 0x0010 : (usage == AUDIO_VOL_DOWN ? 0x0020 : (usage == TRANSPORT_NEXT_TRACK ? 0x0100 : (usage == TRANSPORT_PREV_TRACK ? 0x0200 : (usage == TRANSPORT_STOP ? 0x0400 : (usage == TRANSPORT_STOP_EJECT ? 0x0800 : (usage == TRANSPORT_PLAY_PAUSE ? 0x0080 : (usage == AL_EMAIL ? 0x0200 : (usage == AL_LOCAL_BROWSER ? 0x8000 : (usage == AC_SEARCH ? 0x0400 : (usage == AC_HOME ? 0x0100 : 0)))))))))))) - #endif +#endif diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index f2ecf2465..12a6924fd 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -45,7 +45,7 @@ #include "sendchar.h" #include "debug.h" #ifdef SLEEP_LED_ENABLE -#include "sleep_led.h" +# include "sleep_led.h" #endif #include "suspend.h" @@ -57,86 +57,80 @@ #include "rgblight_reconfig.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" +# include "keycode_config.h" - extern keymap_config_t keymap_config; +extern keymap_config_t keymap_config; #endif - #ifdef AUDIO_ENABLE - #include <audio.h> +# include <audio.h> #endif #ifdef BLUETOOTH_ENABLE - #ifdef MODULE_ADAFRUIT_BLE - #include "adafruit_ble.h" - #else - #include "bluetooth.h" - #endif +# ifdef MODULE_ADAFRUIT_BLE +# include "adafruit_ble.h" +# else +# include "bluetooth.h" +# endif #endif #ifdef VIRTSER_ENABLE - #include "virtser.h" +# include "virtser.h" #endif #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE) - #include "rgblight.h" +# include "rgblight.h" #endif #ifdef MIDI_ENABLE - #include "qmk_midi.h" +# include "qmk_midi.h" #endif #ifdef RAW_ENABLE - #include "raw_hid.h" +# include "raw_hid.h" #endif uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ -uint8_t keyboard_protocol = 1; +uint8_t keyboard_protocol = 1; static uint8_t keyboard_led_stats = 0; static report_keyboard_t keyboard_report_sent; /* Host driver */ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); -host_driver_t lufa_driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer, +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); +host_driver_t lufa_driver = { + keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer, }; #ifdef VIRTSER_ENABLE -USB_ClassInfo_CDC_Device_t cdc_device = -{ - .Config = - { - .ControlInterfaceNumber = CCI_INTERFACE, - .DataINEndpoint = - { - .Address = CDC_IN_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = CDC_OUT_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .NotificationEndpoint = - { - .Address = CDC_NOTIFICATION_EPADDR, - .Size = CDC_NOTIFICATION_EPSIZE, - .Banks = 1, - }, - }, +USB_ClassInfo_CDC_Device_t cdc_device = { + .Config = + { + .ControlInterfaceNumber = CCI_INTERFACE, + .DataINEndpoint = + { + .Address = CDC_IN_EPADDR, + .Size = CDC_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = CDC_OUT_EPADDR, + .Size = CDC_EPSIZE, + .Banks = 1, + }, + .NotificationEndpoint = + { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Banks = 1, + }, + }, }; #endif @@ -146,84 +140,73 @@ USB_ClassInfo_CDC_Device_t cdc_device = * * FIXME: Needs doc */ -void raw_hid_send( uint8_t *data, uint8_t length ) -{ - // TODO: implement variable size packet - if ( length != RAW_EPSIZE ) - { - return; - } - - if (USB_DeviceState != DEVICE_STATE_Configured) - { - return; - } - - // TODO: decide if we allow calls to raw_hid_send() in the middle - // of other endpoint usage. - uint8_t ep = Endpoint_GetCurrentEndpoint(); - - Endpoint_SelectEndpoint(RAW_IN_EPNUM); - - // Check to see if the host is ready to accept another packet - if (Endpoint_IsINReady()) - { - // Write data - Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL); - // Finalize the stream transfer to send the last packet - Endpoint_ClearIN(); - } - - Endpoint_SelectEndpoint(ep); +void raw_hid_send(uint8_t *data, uint8_t length) { + // TODO: implement variable size packet + if (length != RAW_EPSIZE) { + return; + } + + if (USB_DeviceState != DEVICE_STATE_Configured) { + return; + } + + // TODO: decide if we allow calls to raw_hid_send() in the middle + // of other endpoint usage. + uint8_t ep = Endpoint_GetCurrentEndpoint(); + + Endpoint_SelectEndpoint(RAW_IN_EPNUM); + + // Check to see if the host is ready to accept another packet + if (Endpoint_IsINReady()) { + // Write data + Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL); + // Finalize the stream transfer to send the last packet + Endpoint_ClearIN(); + } + + Endpoint_SelectEndpoint(ep); } /** \brief Raw HID Receive * * FIXME: Needs doc */ -__attribute__ ((weak)) -void raw_hid_receive( uint8_t *data, uint8_t length ) -{ - // Users should #include "raw_hid.h" in their own code - // and implement this function there. Leave this as weak linkage - // so users can opt to not handle data coming in. +__attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { + // Users should #include "raw_hid.h" in their own code + // and implement this function there. Leave this as weak linkage + // so users can opt to not handle data coming in. } /** \brief Raw HID Task * * FIXME: Needs doc */ -static void raw_hid_task(void) -{ - // Create a temporary buffer to hold the read in data from the host - uint8_t data[RAW_EPSIZE]; - bool data_read = false; - - // Device must be connected and configured for the task to run - if (USB_DeviceState != DEVICE_STATE_Configured) - return; - - Endpoint_SelectEndpoint(RAW_OUT_EPNUM); - - // Check to see if a packet has been sent from the host - if (Endpoint_IsOUTReceived()) - { - // Check to see if the packet contains data - if (Endpoint_IsReadWriteAllowed()) - { - /* Read data */ - Endpoint_Read_Stream_LE(data, sizeof(data), NULL); - data_read = true; - } - - // Finalize the stream transfer to receive the last packet - Endpoint_ClearOUT(); - - if ( data_read ) - { - raw_hid_receive( data, sizeof(data) ); - } - } +static void raw_hid_task(void) { + // Create a temporary buffer to hold the read in data from the host + uint8_t data[RAW_EPSIZE]; + bool data_read = false; + + // Device must be connected and configured for the task to run + if (USB_DeviceState != DEVICE_STATE_Configured) return; + + Endpoint_SelectEndpoint(RAW_OUT_EPNUM); + + // Check to see if a packet has been sent from the host + if (Endpoint_IsOUTReceived()) { + // Check to see if the packet contains data + if (Endpoint_IsReadWriteAllowed()) { + /* Read data */ + Endpoint_Read_Stream_LE(data, sizeof(data), NULL); + data_read = true; + } + + // Finalize the stream transfer to receive the last packet + Endpoint_ClearOUT(); + + if (data_read) { + raw_hid_receive(data, sizeof(data)); + } + } } #endif @@ -235,15 +218,13 @@ static void raw_hid_task(void) * * FIXME: Needs doc */ -static void Console_Task(void) -{ +static void Console_Task(void) { /* Device must be connected and configured for the task to run */ - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if (USB_DeviceState != DEVICE_STATE_Configured) return; uint8_t ep = Endpoint_GetCurrentEndpoint(); -#if 0 +# if 0 // TODO: impl receivechar()/recvchar() Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM); @@ -266,7 +247,7 @@ static void Console_Task(void) /* Finalize the stream transfer to send the last packet */ Endpoint_ClearOUT(); } -#endif +# endif /* IN packet */ Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); @@ -276,8 +257,7 @@ static void Console_Task(void) } // fill empty bank - while (Endpoint_IsReadWriteAllowed()) - Endpoint_Write_8(0); + while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0); // flash senchar packet if (Endpoint_IsINReady()) { @@ -288,7 +268,6 @@ static void Console_Task(void) } #endif - /******************************************************************************* * USB Events ******************************************************************************/ @@ -298,13 +277,12 @@ static void Console_Task(void) * 1) EVENT_USB_Device_Suspend * 2) EVENT_USB_Device_Reset * 3) EVENT_USB_Device_Wake -*/ + */ /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Connect(void) -{ +void EVENT_USB_Device_Connect(void) { print("[C]"); /* For battery powered device */ if (!USB_IsInitialized) { @@ -318,35 +296,30 @@ void EVENT_USB_Device_Connect(void) * * FIXME: Needs doc */ -void EVENT_USB_Device_Disconnect(void) -{ +void EVENT_USB_Device_Disconnect(void) { print("[D]"); /* For battery powered device */ USB_IsInitialized = false; -/* TODO: This doesn't work. After several plug in/outs can not be enumerated. - if (USB_IsInitialized) { - USB_Disable(); // Disable all interrupts - USB_Controller_Enable(); - USB_INT_Enable(USB_INT_VBUSTI); - } -*/ + /* TODO: This doesn't work. After several plug in/outs can not be enumerated. + if (USB_IsInitialized) { + USB_Disable(); // Disable all interrupts + USB_Controller_Enable(); + USB_INT_Enable(USB_INT_VBUSTI); + } + */ } /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Reset(void) -{ - print("[R]"); -} +void EVENT_USB_Device_Reset(void) { print("[R]"); } /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Suspend() -{ +void EVENT_USB_Device_Suspend() { print("[S]"); #ifdef SLEEP_LED_ENABLE sleep_led_enable(); @@ -357,8 +330,7 @@ void EVENT_USB_Device_Suspend() * * FIXME: Needs doc */ -void EVENT_USB_Device_WakeUp() -{ +void EVENT_USB_Device_WakeUp() { print("[W]"); suspend_wakeup_init(); @@ -369,23 +341,19 @@ void EVENT_USB_Device_WakeUp() #endif } - - #ifdef CONSOLE_ENABLE static bool console_flush = false; -#define CONSOLE_FLUSH_SET(b) do { \ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\ - console_flush = b; \ - } \ -} while (0) +# define CONSOLE_FLUSH_SET(b) \ + do { \ + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { console_flush = b; } \ + } while (0) /** \brief Event USB Device Start Of Frame * * FIXME: Needs doc * called every 1ms */ -void EVENT_USB_Device_StartOfFrame(void) -{ +void EVENT_USB_Device_StartOfFrame(void) { static uint8_t count; if (++count % 50) return; count = 0; @@ -404,44 +372,37 @@ void EVENT_USB_Device_StartOfFrame(void) * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4, * it is safe to use single bank for all endpoints. */ -void EVENT_USB_Device_ConfigurationChanged(void) -{ +void EVENT_USB_Device_ConfigurationChanged(void) { bool ConfigSuccess = true; /* Setup Keyboard HID Report Endpoints */ #ifndef KEYBOARD_SHARED_EP - ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) /* Setup Mouse HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef SHARED_EP_ENABLE /* Setup Shared HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - SHARED_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef RAW_ENABLE /* Setup Raw HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - RAW_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, - RAW_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef CONSOLE_ENABLE /* Setup Console HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); -#if 0 + ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); +# if 0 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); -#endif +# endif #endif #ifdef MIDI_ENABLE @@ -473,26 +434,23 @@ Other Device Required Optional Optional Optional Optional Opti * * This is fired before passing along unhandled control requests to the library for processing internally. */ -void EVENT_USB_Device_ControlRequest(void) -{ - uint8_t* ReportData = NULL; +void EVENT_USB_Device_ControlRequest(void) { + uint8_t *ReportData = NULL; uint8_t ReportSize = 0; /* Handle HID Class specific requests */ - switch (USB_ControlRequest.bRequest) - { + switch (USB_ControlRequest.bRequest) { case HID_REQ_GetReport: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); // Interface switch (USB_ControlRequest.wIndex) { - case KEYBOARD_INTERFACE: - // TODO: test/check - ReportData = (uint8_t*)&keyboard_report_sent; - ReportSize = sizeof(keyboard_report_sent); - break; + case KEYBOARD_INTERFACE: + // TODO: test/check + ReportData = (uint8_t *)&keyboard_report_sent; + ReportSize = sizeof(keyboard_report_sent); + break; } /* Write the report data to the control endpoint */ @@ -502,47 +460,43 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetReport: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { - + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { // Interface switch (USB_ControlRequest.wIndex) { - case KEYBOARD_INTERFACE: + case KEYBOARD_INTERFACE: #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP) - case SHARED_INTERFACE: + case SHARED_INTERFACE: #endif - Endpoint_ClearSETUP(); + Endpoint_ClearSETUP(); - while (!(Endpoint_IsOUTReceived())) { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + while (!(Endpoint_IsOUTReceived())) { + if (USB_DeviceState == DEVICE_STATE_Unattached) return; + } - if (Endpoint_BytesInEndpoint() == 2) { - uint8_t report_id = Endpoint_Read_8(); + if (Endpoint_BytesInEndpoint() == 2) { + uint8_t report_id = Endpoint_Read_8(); - if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { - keyboard_led_stats = Endpoint_Read_8(); - } - } else { - keyboard_led_stats = Endpoint_Read_8(); - } + if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { + keyboard_led_stats = Endpoint_Read_8(); + } + } else { + keyboard_led_stats = Endpoint_Read_8(); + } - Endpoint_ClearOUT(); - Endpoint_ClearStatusStage(); - break; + Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); + break; } - } break; case HID_REQ_GetProtocol: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { Endpoint_ClearSETUP(); - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_Write_8(keyboard_protocol); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); @@ -551,8 +505,7 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetProtocol: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { Endpoint_ClearSETUP(); Endpoint_ClearStatusStage(); @@ -564,8 +517,7 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetIdle: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); Endpoint_ClearStatusStage(); @@ -574,10 +526,10 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_GetIdle: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_Write_8(keyboard_idle); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); @@ -598,54 +550,50 @@ void EVENT_USB_Device_ControlRequest(void) * * FIXME: Needs doc */ -static uint8_t keyboard_leds(void) -{ - return keyboard_led_stats; -} +static uint8_t keyboard_leds(void) { return keyboard_led_stats; } /** \brief Send Keyboard * * FIXME: Needs doc */ -static void send_keyboard(report_keyboard_t *report) -{ +static void send_keyboard(report_keyboard_t *report) { uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); #ifdef BLUETOOTH_ENABLE - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE - adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); - #elif MODULE_RN42 - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x09); - bluefruit_serial_send(0x01); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); - } - #else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); - } - #endif - } + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { +# ifdef MODULE_ADAFRUIT_BLE + adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); +# elif MODULE_RN42 + bluefruit_serial_send(0xFD); + bluefruit_serial_send(0x09); + bluefruit_serial_send(0x01); + bluefruit_serial_send(report->mods); + bluefruit_serial_send(report->reserved); + for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + bluefruit_serial_send(report->keys[i]); + } +# else + bluefruit_serial_send(0xFD); + bluefruit_serial_send(report->mods); + bluefruit_serial_send(report->reserved); + for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + bluefruit_serial_send(report->keys[i]); + } +# endif + } #endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } /* Select the Keyboard Report Endpoint */ - uint8_t ep = KEYBOARD_IN_EPNUM; + uint8_t ep = KEYBOARD_IN_EPNUM; uint8_t size = KEYBOARD_REPORT_SIZE; #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { - ep = SHARED_IN_EPNUM; + ep = SHARED_IN_EPNUM; size = sizeof(struct nkro_report); } #endif @@ -666,38 +614,37 @@ static void send_keyboard(report_keyboard_t *report) keyboard_report_sent = *report; } - + /** \brief Send Mouse * * FIXME: Needs doc */ -static void send_mouse(report_mouse_t *report) -{ +static void send_mouse(report_mouse_t *report) { #ifdef MOUSE_ENABLE uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); -#ifdef BLUETOOTH_ENABLE - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE - // FIXME: mouse buttons - adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); - #else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x03); - bluefruit_serial_send(report->buttons); - bluefruit_serial_send(report->x); - bluefruit_serial_send(report->y); - bluefruit_serial_send(report->v); // should try sending the wheel v here - bluefruit_serial_send(report->h); // should try sending the wheel h here - bluefruit_serial_send(0x00); - #endif - } -#endif +# ifdef BLUETOOTH_ENABLE + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { +# ifdef MODULE_ADAFRUIT_BLE + // FIXME: mouse buttons + adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); +# else + bluefruit_serial_send(0xFD); + bluefruit_serial_send(0x00); + bluefruit_serial_send(0x03); + bluefruit_serial_send(report->buttons); + bluefruit_serial_send(report->x); + bluefruit_serial_send(report->y); + bluefruit_serial_send(report->v); // should try sending the wheel v here + bluefruit_serial_send(report->h); // should try sending the wheel h here + bluefruit_serial_send(0x00); +# endif + } +# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } /* Select the Mouse Report Endpoint */ @@ -719,18 +666,13 @@ static void send_mouse(report_mouse_t *report) * * FIXME: Needs doc */ -static void send_system(uint16_t data) -{ +static void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if (USB_DeviceState != DEVICE_STATE_Configured) return; - report_extra_t r = { - .report_id = REPORT_ID_SYSTEM, - .usage = data - SYSTEM_POWER_DOWN + 1 - }; + report_extra_t r = {.report_id = REPORT_ID_SYSTEM, .usage = data - SYSTEM_POWER_DOWN + 1}; Endpoint_SelectEndpoint(SHARED_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ @@ -746,52 +688,48 @@ static void send_system(uint16_t data) * * FIXME: Needs doc */ -static void send_consumer(uint16_t data) -{ +static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); -#ifdef BLUETOOTH_ENABLE +# ifdef BLUETOOTH_ENABLE if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE +# ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_consumer_key(data, 0); - #elif MODULE_RN42 +# elif MODULE_RN42 static uint16_t last_data = 0; if (data == last_data) return; - last_data = data; + last_data = data; uint16_t bitmap = CONSUMER2RN42(data); bluefruit_serial_send(0xFD); bluefruit_serial_send(0x03); bluefruit_serial_send(0x03); - bluefruit_serial_send(bitmap&0xFF); - bluefruit_serial_send((bitmap>>8)&0xFF); - #else + bluefruit_serial_send(bitmap & 0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); +# else static uint16_t last_data = 0; if (data == last_data) return; - last_data = data; + last_data = data; uint16_t bitmap = CONSUMER2BLUEFRUIT(data); bluefruit_serial_send(0xFD); bluefruit_serial_send(0x00); bluefruit_serial_send(0x02); - bluefruit_serial_send((bitmap>>8)&0xFF); - bluefruit_serial_send(bitmap&0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); + bluefruit_serial_send(bitmap & 0xFF); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); - #endif +# endif } -#endif +# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } - report_extra_t r = { - .report_id = REPORT_ID_CONSUMER, - .usage = data - }; + report_extra_t r = {.report_id = REPORT_ID_CONSUMER, .usage = data}; Endpoint_SelectEndpoint(SHARED_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ @@ -803,18 +741,16 @@ static void send_consumer(uint16_t data) #endif } - /******************************************************************************* * sendchar ******************************************************************************/ #ifdef CONSOLE_ENABLE -#define SEND_TIMEOUT 5 +# define SEND_TIMEOUT 5 /** \brief Send Char * * FIXME: Needs doc */ -int8_t sendchar(uint8_t c) -{ +int8_t sendchar(uint8_t c) { // Not wait once timeouted. // Because sendchar() is called so many times, waiting each call causes big lag. static bool timeouted = false; @@ -823,8 +759,7 @@ int8_t sendchar(uint8_t c) // or char will be lost. These two function is mutually exclusive. CONSOLE_FLUSH_SET(false); - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; + if (USB_DeviceState != DEVICE_STATE_Configured) return -1; uint8_t ep = Endpoint_GetCurrentEndpoint(); Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); @@ -857,7 +792,8 @@ int8_t sendchar(uint8_t c) // send when bank is full if (!Endpoint_IsReadWriteAllowed()) { - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_ClearIN(); } else { CONSOLE_FLUSH_SET(true); @@ -870,10 +806,7 @@ ERROR_EXIT: return -1; } #else -int8_t sendchar(uint8_t c) -{ - return 0; -} +int8_t sendchar(uint8_t c) { return 0; } #endif /******************************************************************************* @@ -881,33 +814,28 @@ int8_t sendchar(uint8_t c) ******************************************************************************/ #ifdef MIDI_ENABLE -USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = -{ - .Config = - { - .StreamingInterfaceNumber = AS_INTERFACE, - .DataINEndpoint = - { - .Address = MIDI_STREAM_IN_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = MIDI_STREAM_OUT_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, - }, +USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = { + .Config = + { + .StreamingInterfaceNumber = AS_INTERFACE, + .DataINEndpoint = + { + .Address = MIDI_STREAM_IN_EPADDR, + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = MIDI_STREAM_OUT_EPADDR, + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1, + }, + }, }; -void send_midi_packet(MIDI_EventPacket_t* event) { - MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); -} +void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); } -bool recv_midi_packet(MIDI_EventPacket_t* const event) { - return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); -} +bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); } #endif @@ -920,66 +848,60 @@ bool recv_midi_packet(MIDI_EventPacket_t* const event) { * * FIXME: Needs doc */ -void virtser_init(void) -{ - cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ; - CDC_Device_SendControlLineStateChange(&cdc_device); +void virtser_init(void) { + cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR; + CDC_Device_SendControlLineStateChange(&cdc_device); } /** \brief Virtual Serial Receive * * FIXME: Needs doc */ -void virtser_recv(uint8_t c) __attribute__ ((weak)); -void virtser_recv(uint8_t c) -{ - // Ignore by default +void virtser_recv(uint8_t c) __attribute__((weak)); +void virtser_recv(uint8_t c) { + // Ignore by default } /** \brief Virtual Serial Task * * FIXME: Needs doc */ -void virtser_task(void) -{ - uint16_t count = CDC_Device_BytesReceived(&cdc_device); - uint8_t ch; - if (count) - { - ch = CDC_Device_ReceiveByte(&cdc_device); - virtser_recv(ch); - } +void virtser_task(void) { + uint16_t count = CDC_Device_BytesReceived(&cdc_device); + uint8_t ch; + if (count) { + ch = CDC_Device_ReceiveByte(&cdc_device); + virtser_recv(ch); + } } /** \brief Virtual Serial Send * * FIXME: Needs doc */ -void virtser_send(const uint8_t byte) -{ - uint8_t timeout = 255; - uint8_t ep = Endpoint_GetCurrentEndpoint(); +void virtser_send(const uint8_t byte) { + uint8_t timeout = 255; + uint8_t ep = Endpoint_GetCurrentEndpoint(); - if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) - { - /* IN packet */ - Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address); + if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) { + /* IN packet */ + Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address); - if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { - Endpoint_SelectEndpoint(ep); - return; - } + if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { + Endpoint_SelectEndpoint(ep); + return; + } - while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); + while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); - Endpoint_Write_8(byte); - CDC_Device_Flush(&cdc_device); + Endpoint_Write_8(byte); + CDC_Device_Flush(&cdc_device); - if (Endpoint_IsINReady()) { - Endpoint_ClearIN(); - } + if (Endpoint_IsINReady()) { + Endpoint_ClearIN(); + } - Endpoint_SelectEndpoint(ep); - } + Endpoint_SelectEndpoint(ep); + } } #endif @@ -990,8 +912,7 @@ void virtser_send(const uint8_t byte) * * FIXME: Needs doc */ -static void setup_mcu(void) -{ +static void setup_mcu(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); @@ -1007,8 +928,7 @@ static void setup_mcu(void) * * FIXME: Needs doc */ -static void setup_usb(void) -{ +static void setup_usb(void) { // Leonardo needs. Without this USB device is not recognized. USB_Disable(); @@ -1023,9 +943,8 @@ static void setup_usb(void) * * FIXME: Needs doc */ -int main(void) __attribute__ ((weak)); -int main(void) -{ +int main(void) __attribute__((weak)); +int main(void) { #ifdef MIDI_ENABLE setup_midi(); #endif @@ -1043,11 +962,11 @@ int main(void) #ifdef WAIT_FOR_USB while (USB_DeviceState != DEVICE_STATE_Configured) { - #if defined(INTERRUPT_CONTROL_ENDPOINT) - ; - #else - USB_USBTask(); - #endif +# if defined(INTERRUPT_CONTROL_ENDPOINT) + ; +# else + USB_USBTask(); +# endif } print("USB configured.\n"); #else @@ -1066,15 +985,15 @@ int main(void) print("Keyboard start.\n"); while (1) { - #if !defined(NO_USB_STARTUP_CHECK) +#if !defined(NO_USB_STARTUP_CHECK) while (USB_DeviceState == DEVICE_STATE_Suspended) { print("[s]"); suspend_power_down(); if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { - USB_Device_SendRemoteWakeup(); + USB_Device_SendRemoteWakeup(); } } - #endif +#endif keyboard_task(); @@ -1102,14 +1021,7 @@ int main(void) #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif - } } -uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, - const uint16_t wIndex, - const void** const DescriptorAddress) -{ - return get_usb_descriptor(wValue, wIndex, DescriptorAddress); -} - +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, const void **const DescriptorAddress) { return get_usb_descriptor(wValue, wIndex, DescriptorAddress); } diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 7364cdf7c..652e4e79b 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -62,27 +62,27 @@ extern host_driver_t lufa_driver; typedef struct { uint8_t report_id; uint16_t usage; -} __attribute__ ((packed)) report_extra_t; +} __attribute__((packed)) report_extra_t; #ifdef API_ENABLE - #include "api.h" +# include "api.h" #endif #ifdef API_SYSEX_ENABLE - #include "api_sysex.h" - // Allocate space for encoding overhead. - //The header and terminator are not stored to save a few bytes of precious ram - #define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) +# include "api_sysex.h" +// Allocate space for encoding overhead. +// The header and terminator are not stored to save a few bytes of precious ram +# define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) #endif // #if LUFA_VERSION_INTEGER < 0x120730 // /* old API 120219 */ // #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank) // #else - /* new API >= 120730 */ - #define ENDPOINT_BANK_SINGLE 1 - #define ENDPOINT_BANK_DOUBLE 2 - #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank) +/* new API >= 120730 */ +#define ENDPOINT_BANK_SINGLE 1 +#define ENDPOINT_BANK_DOUBLE 2 +#define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank) // #endif #endif diff --git a/tmk_core/protocol/lufa/outputselect.c b/tmk_core/protocol/lufa/outputselect.c index 42de80612..b115ea969 100644 --- a/tmk_core/protocol/lufa/outputselect.c +++ b/tmk_core/protocol/lufa/outputselect.c @@ -15,7 +15,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "lufa.h" #include "outputselect.h" #ifdef MODULE_ADAFRUIT_BLE - #include "adafruit_ble.h" +# include "adafruit_ble.h" #endif uint8_t desired_output = OUTPUT_DEFAULT; @@ -33,9 +33,7 @@ void set_output(uint8_t output) { * * FIXME: Needs doc */ -__attribute__((weak)) -void set_output_user(uint8_t output) { -} +__attribute__((weak)) void set_output_user(uint8_t output) {} /** \brief Auto Detect Output * @@ -53,7 +51,7 @@ uint8_t auto_detect_output(void) { #endif #ifdef BLUETOOTH_ENABLE - return OUTPUT_BLUETOOTH; // should check if BT is connected here + return OUTPUT_BLUETOOTH; // should check if BT is connected here #endif return OUTPUT_NONE; @@ -69,4 +67,3 @@ uint8_t where_to_send(void) { } return desired_output; } - diff --git a/tmk_core/protocol/lufa/outputselect.h b/tmk_core/protocol/lufa/outputselect.h index 28cc3298e..24fe4daa2 100644 --- a/tmk_core/protocol/lufa/outputselect.h +++ b/tmk_core/protocol/lufa/outputselect.h @@ -27,14 +27,14 @@ enum outputs { * backward compatibility for BLUETOOTH_ENABLE, send to BT and USB by default */ #ifndef OUTPUT_DEFAULT - #ifdef BLUETOOTH_ENABLE - #define OUTPUT_DEFAULT OUTPUT_USB_AND_BT - #else - #define OUTPUT_DEFAULT OUTPUT_AUTO - #endif +# ifdef BLUETOOTH_ENABLE +# define OUTPUT_DEFAULT OUTPUT_USB_AND_BT +# else +# define OUTPUT_DEFAULT OUTPUT_AUTO +# endif #endif -void set_output(uint8_t output); -void set_output_user(uint8_t output); +void set_output(uint8_t output); +void set_output_user(uint8_t output); uint8_t auto_detect_output(void); uint8_t where_to_send(void);
\ No newline at end of file diff --git a/tmk_core/protocol/m0110.c b/tmk_core/protocol/m0110.c index 0d3a5aaa4..b02a6933d 100644 --- a/tmk_core/protocol/m0110.c +++ b/tmk_core/protocol/m0110.c @@ -43,98 +43,95 @@ POSSIBILITY OF SUCH DAMAGE. #include "m0110.h" #include "debug.h" - -static inline uint8_t raw2scan(uint8_t raw); -static inline uint8_t inquiry(void); -static inline uint8_t instant(void); -static inline void clock_lo(void); -static inline void clock_hi(void); -static inline bool clock_in(void); -static inline void data_lo(void); -static inline void data_hi(void); -static inline bool data_in(void); +static inline uint8_t raw2scan(uint8_t raw); +static inline uint8_t inquiry(void); +static inline uint8_t instant(void); +static inline void clock_lo(void); +static inline void clock_hi(void); +static inline bool clock_in(void); +static inline void data_lo(void); +static inline void data_hi(void); +static inline bool data_in(void); static inline uint16_t wait_clock_lo(uint16_t us); static inline uint16_t wait_clock_hi(uint16_t us); static inline uint16_t wait_data_lo(uint16_t us); static inline uint16_t wait_data_hi(uint16_t us); -static inline void idle(void); -static inline void request(void); - - -#define WAIT_US(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - m0110_error = err; \ - goto ERROR; \ - } \ -} while (0) - -#define WAIT_MS(stat, ms, err) do { \ - uint16_t _ms = ms; \ - while (_ms) { \ - if (wait_##stat(1000)) { \ - break; \ - } \ - _ms--; \ - } \ - if (_ms == 0) { \ - m0110_error = err; \ - goto ERROR; \ - } \ -} while (0) - -#define KEY(raw) ((raw) & 0x7f) -#define IS_BREAK(raw) (((raw) & 0x80) == 0x80) - +static inline void idle(void); +static inline void request(void); + +#define WAIT_US(stat, us, err) \ + do { \ + if (!wait_##stat(us)) { \ + m0110_error = err; \ + goto ERROR; \ + } \ + } while (0) + +#define WAIT_MS(stat, ms, err) \ + do { \ + uint16_t _ms = ms; \ + while (_ms) { \ + if (wait_##stat(1000)) { \ + break; \ + } \ + _ms--; \ + } \ + if (_ms == 0) { \ + m0110_error = err; \ + goto ERROR; \ + } \ + } while (0) + +#define KEY(raw) ((raw)&0x7f) +#define IS_BREAK(raw) (((raw)&0x80) == 0x80) uint8_t m0110_error = 0; - -void m0110_init(void) -{ +void m0110_init(void) { idle(); _delay_ms(1000); -/* Not needed to initialize in fact. - uint8_t data; - m0110_send(M0110_MODEL); - data = m0110_recv(); - print("m0110_init model: "); phex(data); print("\n"); + /* Not needed to initialize in fact. + uint8_t data; + m0110_send(M0110_MODEL); + data = m0110_recv(); + print("m0110_init model: "); phex(data); print("\n"); - m0110_send(M0110_TEST); - data = m0110_recv(); - print("m0110_init test: "); phex(data); print("\n"); -*/ + m0110_send(M0110_TEST); + data = m0110_recv(); + print("m0110_init test: "); phex(data); print("\n"); + */ } -uint8_t m0110_send(uint8_t data) -{ +uint8_t m0110_send(uint8_t data) { m0110_error = 0; request(); WAIT_MS(clock_lo, 250, 1); // keyboard may block long time for (uint8_t bit = 0x80; bit; bit >>= 1) { WAIT_US(clock_lo, 250, 3); - if (data&bit) { + if (data & bit) { data_hi(); } else { data_lo(); } WAIT_US(clock_hi, 200, 4); } - _delay_us(100); // hold last bit for 80us + _delay_us(100); // hold last bit for 80us idle(); return 1; ERROR: - print("m0110_send err: "); phex(m0110_error); print("\n"); + print("m0110_send err: "); + phex(m0110_error); + print("\n"); _delay_ms(500); idle(); return 0; } -uint8_t m0110_recv(void) -{ +uint8_t m0110_recv(void) { uint8_t data = 0; - m0110_error = 0; + m0110_error = 0; WAIT_MS(clock_lo, 250, 1); // keyboard may block long time for (uint8_t i = 0; i < 8; i++) { @@ -148,7 +145,9 @@ uint8_t m0110_recv(void) idle(); return data; ERROR: - print("m0110_recv err: "); phex(m0110_error); print("\n"); + print("m0110_recv err: "); + phex(m0110_error); + print("\n"); _delay_ms(500); idle(); return 0xFF; @@ -199,26 +198,25 @@ During Calc key is hold: *b: Shift(d) event is ignored. *c: Arrow/Calc(d) event is ignored. */ -uint8_t m0110_recv_key(void) -{ - static uint8_t keybuf = 0x00; +uint8_t m0110_recv_key(void) { + static uint8_t keybuf = 0x00; static uint8_t keybuf2 = 0x00; - static uint8_t rawbuf = 0x00; - uint8_t raw, raw2, raw3; + static uint8_t rawbuf = 0x00; + uint8_t raw, raw2, raw3; if (keybuf) { - raw = keybuf; + raw = keybuf; keybuf = 0x00; return raw; } if (keybuf2) { - raw = keybuf2; + raw = keybuf2; keybuf2 = 0x00; return raw; } if (rawbuf) { - raw = rawbuf; + raw = rawbuf; rawbuf = 0x00; } else { raw = instant(); // Use INSTANT for better response. Should be INQUIRY ? @@ -233,8 +231,8 @@ uint8_t m0110_recv_key(void) case M0110_ARROW_RIGHT: if (IS_BREAK(raw2)) { // Case B,F,N: - keybuf = (raw2scan(raw2) | M0110_CALC_OFFSET); // Calc(u) - return (raw2scan(raw2) | M0110_KEYPAD_OFFSET); // Arrow(u) + keybuf = (raw2scan(raw2) | M0110_CALC_OFFSET); // Calc(u) + return (raw2scan(raw2) | M0110_KEYPAD_OFFSET); // Arrow(u) } break; } @@ -247,7 +245,7 @@ uint8_t m0110_recv_key(void) case M0110_SHIFT: // Case: 5-8,C,G,H rawbuf = raw2; - return raw2scan(raw); // Shift(d/u) + return raw2scan(raw); // Shift(d/u) break; case M0110_KEYPAD: // Shift + Arrow, Calc, or etc. @@ -261,38 +259,38 @@ uint8_t m0110_recv_key(void) if (IS_BREAK(raw3)) { // Case 4: print("(4)\n"); - keybuf2 = raw2scan(raw); // Shift(u) - keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(u) - return (raw2scan(raw3) | M0110_KEYPAD_OFFSET); // Arrow(u) + keybuf2 = raw2scan(raw); // Shift(u) + keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(u) + return (raw2scan(raw3) | M0110_KEYPAD_OFFSET); // Arrow(u) } else { // Case 3: print("(3)\n"); - return (raw2scan(raw)); // Shift(u) + return (raw2scan(raw)); // Shift(u) } } else { if (IS_BREAK(raw3)) { // Case 2: print("(2)\n"); - keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(u) + keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(u) return (raw2scan(raw3) | M0110_KEYPAD_OFFSET); // Arrow(u) } else { // Case 1: print("(1)\n"); - return (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(d) + return (raw2scan(raw3) | M0110_CALC_OFFSET); // Calc(d) } } break; default: // Shift + Keypad keybuf = (raw2scan(raw3) | M0110_KEYPAD_OFFSET); - return raw2scan(raw); // Shift(d/u) + return raw2scan(raw); // Shift(d/u) break; } break; default: // Shift + Normal keys keybuf = raw2scan(raw2); - return raw2scan(raw); // Shift(d/u) + return raw2scan(raw); // Shift(d/u) break; } break; @@ -303,103 +301,97 @@ uint8_t m0110_recv_key(void) } } +static inline uint8_t raw2scan(uint8_t raw) { return (raw == M0110_NULL) ? M0110_NULL : ((raw == M0110_ERROR) ? M0110_ERROR : (((raw & 0x80) | ((raw & 0x7F) >> 1)))); } -static inline uint8_t raw2scan(uint8_t raw) { - return (raw == M0110_NULL) ? M0110_NULL : ( - (raw == M0110_ERROR) ? M0110_ERROR : ( - ((raw&0x80) | ((raw&0x7F)>>1)) - ) - ); -} - -static inline uint8_t inquiry(void) -{ +static inline uint8_t inquiry(void) { m0110_send(M0110_INQUIRY); return m0110_recv(); } -static inline uint8_t instant(void) -{ +static inline uint8_t instant(void) { m0110_send(M0110_INSTANT); uint8_t data = m0110_recv(); if (data != M0110_NULL) { - debug_hex(data); debug(" "); + debug_hex(data); + debug(" "); } return data; } -static inline void clock_lo() -{ - M0110_CLOCK_PORT &= ~(1<<M0110_CLOCK_BIT); - M0110_CLOCK_DDR |= (1<<M0110_CLOCK_BIT); +static inline void clock_lo() { + M0110_CLOCK_PORT &= ~(1 << M0110_CLOCK_BIT); + M0110_CLOCK_DDR |= (1 << M0110_CLOCK_BIT); } -static inline void clock_hi() -{ +static inline void clock_hi() { /* input with pull up */ - M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT); - M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT); + M0110_CLOCK_DDR &= ~(1 << M0110_CLOCK_BIT); + M0110_CLOCK_PORT |= (1 << M0110_CLOCK_BIT); } -static inline bool clock_in() -{ - M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT); - M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT); +static inline bool clock_in() { + M0110_CLOCK_DDR &= ~(1 << M0110_CLOCK_BIT); + M0110_CLOCK_PORT |= (1 << M0110_CLOCK_BIT); _delay_us(1); - return M0110_CLOCK_PIN&(1<<M0110_CLOCK_BIT); + return M0110_CLOCK_PIN & (1 << M0110_CLOCK_BIT); } -static inline void data_lo() -{ - M0110_DATA_PORT &= ~(1<<M0110_DATA_BIT); - M0110_DATA_DDR |= (1<<M0110_DATA_BIT); +static inline void data_lo() { + M0110_DATA_PORT &= ~(1 << M0110_DATA_BIT); + M0110_DATA_DDR |= (1 << M0110_DATA_BIT); } -static inline void data_hi() -{ +static inline void data_hi() { /* input with pull up */ - M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT); - M0110_DATA_PORT |= (1<<M0110_DATA_BIT); + M0110_DATA_DDR &= ~(1 << M0110_DATA_BIT); + M0110_DATA_PORT |= (1 << M0110_DATA_BIT); } -static inline bool data_in() -{ - M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT); - M0110_DATA_PORT |= (1<<M0110_DATA_BIT); +static inline bool data_in() { + M0110_DATA_DDR &= ~(1 << M0110_DATA_BIT); + M0110_DATA_PORT |= (1 << M0110_DATA_BIT); _delay_us(1); - return M0110_DATA_PIN&(1<<M0110_DATA_BIT); + return M0110_DATA_PIN & (1 << M0110_DATA_BIT); } -static inline uint16_t wait_clock_lo(uint16_t us) -{ - while (clock_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_clock_lo(uint16_t us) { + while (clock_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_clock_hi(uint16_t us) -{ - while (!clock_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_clock_hi(uint16_t us) { + while (!clock_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_data_lo(uint16_t us) -{ - while (data_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_data_lo(uint16_t us) { + while (data_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline uint16_t wait_data_hi(uint16_t us) -{ - while (!data_in() && us) { asm(""); _delay_us(1); us--; } +static inline uint16_t wait_data_hi(uint16_t us) { + while (!data_in() && us) { + asm(""); + _delay_us(1); + us--; + } return us; } -static inline void idle(void) -{ +static inline void idle(void) { clock_hi(); data_hi(); } -static inline void request(void) -{ +static inline void request(void) { clock_hi(); data_lo(); } - - /* Primitive M0110 Library for AVR ============================== diff --git a/tmk_core/protocol/m0110.h b/tmk_core/protocol/m0110.h index 2b95ed34d..3f9686b0d 100644 --- a/tmk_core/protocol/m0110.h +++ b/tmk_core/protocol/m0110.h @@ -38,51 +38,43 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef M0110_H #define M0110_H - /* port settings for clock and data line */ -#if !(defined(M0110_CLOCK_PORT) && \ - defined(M0110_CLOCK_PIN) && \ - defined(M0110_CLOCK_DDR) && \ - defined(M0110_CLOCK_BIT)) -# error "M0110 clock port setting is required in config.h" +#if !(defined(M0110_CLOCK_PORT) && defined(M0110_CLOCK_PIN) && defined(M0110_CLOCK_DDR) && defined(M0110_CLOCK_BIT)) +# error "M0110 clock port setting is required in config.h" #endif -#if !(defined(M0110_DATA_PORT) && \ - defined(M0110_DATA_PIN) && \ - defined(M0110_DATA_DDR) && \ - defined(M0110_DATA_BIT)) -# error "M0110 data port setting is required in config.h" +#if !(defined(M0110_DATA_PORT) && defined(M0110_DATA_PIN) && defined(M0110_DATA_DDR) && defined(M0110_DATA_BIT)) +# error "M0110 data port setting is required in config.h" #endif /* Commands */ -#define M0110_INQUIRY 0x10 -#define M0110_INSTANT 0x14 -#define M0110_MODEL 0x16 -#define M0110_TEST 0x36 +#define M0110_INQUIRY 0x10 +#define M0110_INSTANT 0x14 +#define M0110_MODEL 0x16 +#define M0110_TEST 0x36 /* Response(raw byte from M0110) */ -#define M0110_NULL 0x7B -#define M0110_KEYPAD 0x79 -#define M0110_TEST_ACK 0x7D -#define M0110_TEST_NAK 0x77 -#define M0110_SHIFT 0x71 -#define M0110_ARROW_UP 0x1B -#define M0110_ARROW_DOWN 0x11 -#define M0110_ARROW_LEFT 0x0D -#define M0110_ARROW_RIGHT 0x05 +#define M0110_NULL 0x7B +#define M0110_KEYPAD 0x79 +#define M0110_TEST_ACK 0x7D +#define M0110_TEST_NAK 0x77 +#define M0110_SHIFT 0x71 +#define M0110_ARROW_UP 0x1B +#define M0110_ARROW_DOWN 0x11 +#define M0110_ARROW_LEFT 0x0D +#define M0110_ARROW_RIGHT 0x05 /* This inidcates no response. */ -#define M0110_ERROR 0xFF +#define M0110_ERROR 0xFF /* scan code offset for keypad and arrow keys */ #define M0110_KEYPAD_OFFSET 0x40 -#define M0110_CALC_OFFSET 0x60 - +#define M0110_CALC_OFFSET 0x60 extern uint8_t m0110_error; /* host role */ -void m0110_init(void); +void m0110_init(void); uint8_t m0110_send(uint8_t data); uint8_t m0110_recv(void); uint8_t m0110_recv_key(void); diff --git a/tmk_core/protocol/mbed/HIDKeyboard.cpp b/tmk_core/protocol/mbed/HIDKeyboard.cpp index 947077cd2..dbaf108fa 100644 --- a/tmk_core/protocol/mbed/HIDKeyboard.cpp +++ b/tmk_core/protocol/mbed/HIDKeyboard.cpp @@ -6,20 +6,14 @@ #define DEFAULT_CONFIGURATION (1) - -HIDKeyboard::HIDKeyboard(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) -{ - USBDevice::connect(); -} +HIDKeyboard::HIDKeyboard(uint16_t vendor_id, uint16_t product_id, uint16_t product_release) : USBDevice(vendor_id, product_id, product_release) { USBDevice::connect(); } bool HIDKeyboard::sendReport(report_keyboard_t report) { USBDevice::write(EP1IN, report.raw, sizeof(report), MAX_PACKET_SIZE_EP1); return true; } -uint8_t HIDKeyboard::leds() { - return led_state; -} +uint8_t HIDKeyboard::leds() { return led_state; } bool HIDKeyboard::USBCallback_setConfiguration(uint8_t configuration) { if (configuration != DEFAULT_CONFIGURATION) { @@ -28,80 +22,86 @@ bool HIDKeyboard::USBCallback_setConfiguration(uint8_t configuration) { // Configure endpoints > 0 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); - //addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); + // addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); // We activate the endpoint to be able to recceive data - //readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT); + // readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT); return true; } - -uint8_t * HIDKeyboard::stringImanufacturerDesc() { +uint8_t *HIDKeyboard::stringImanufacturerDesc() { static uint8_t stringImanufacturerDescriptor[] = { - 0x18, /*bLength*/ - STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ - 't',0,'m',0,'k',0,'-',0,'k',0,'b',0,'d',0,'.',0,'c',0,'o',0,'m',0 /*bString iManufacturer*/ + 0x18, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + 't', + 0, + 'm', + 0, + 'k', + 0, + '-', + 0, + 'k', + 0, + 'b', + 0, + 'd', + 0, + '.', + 0, + 'c', + 0, + 'o', + 0, + 'm', + 0 /*bString iManufacturer*/ }; return stringImanufacturerDescriptor; } -uint8_t * HIDKeyboard::stringIproductDesc() { +uint8_t *HIDKeyboard::stringIproductDesc() { static uint8_t stringIproductDescriptor[] = { - 0x0a, /*bLength*/ - STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ - 'm',0,'b',0,'e',0,'d',0 /*bString iProduct*/ + 0x0a, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + 'm', + 0, + 'b', + 0, + 'e', + 0, + 'd', + 0 /*bString iProduct*/ }; return stringIproductDescriptor; } -uint8_t * HIDKeyboard::stringIserialDesc() { +uint8_t *HIDKeyboard::stringIserialDesc() { static uint8_t stringIserialDescriptor[] = { - 0x04, /*bLength*/ - STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ - '0',0 /*bString iSerial*/ + 0x04, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + '0', 0 /*bString iSerial*/ }; return stringIserialDescriptor; } -uint8_t * HIDKeyboard::reportDesc() { +uint8_t *HIDKeyboard::reportDesc() { static uint8_t reportDescriptor[] = { - USAGE_PAGE(1), 0x01, // Generic Desktop - USAGE(1), 0x06, // Keyboard - COLLECTION(1), 0x01, // Application - - USAGE_PAGE(1), 0x07, // Key Codes - USAGE_MINIMUM(1), 0xE0, - USAGE_MAXIMUM(1), 0xE7, - LOGICAL_MINIMUM(1), 0x00, - LOGICAL_MAXIMUM(1), 0x01, - REPORT_SIZE(1), 0x01, - REPORT_COUNT(1), 0x08, - INPUT(1), 0x02, // Data, Variable, Absolute + USAGE_PAGE(1), 0x01, // Generic Desktop + USAGE(1), 0x06, // Keyboard + COLLECTION(1), 0x01, // Application - REPORT_COUNT(1), 0x01, - REPORT_SIZE(1), 0x08, - INPUT(1), 0x01, // Constant + USAGE_PAGE(1), 0x07, // Key Codes + USAGE_MINIMUM(1), 0xE0, USAGE_MAXIMUM(1), 0xE7, LOGICAL_MINIMUM(1), 0x00, LOGICAL_MAXIMUM(1), 0x01, REPORT_SIZE(1), 0x01, REPORT_COUNT(1), 0x08, INPUT(1), 0x02, // Data, Variable, Absolute - REPORT_COUNT(1), 0x05, - REPORT_SIZE(1), 0x01, - USAGE_PAGE(1), 0x08, // LEDs - USAGE_MINIMUM(1), 0x01, - USAGE_MAXIMUM(1), 0x05, - OUTPUT(1), 0x02, // Data, Variable, Absolute + REPORT_COUNT(1), 0x01, REPORT_SIZE(1), 0x08, INPUT(1), 0x01, // Constant - REPORT_COUNT(1), 0x01, - REPORT_SIZE(1), 0x03, - OUTPUT(1), 0x01, // Constant + REPORT_COUNT(1), 0x05, REPORT_SIZE(1), 0x01, USAGE_PAGE(1), 0x08, // LEDs + USAGE_MINIMUM(1), 0x01, USAGE_MAXIMUM(1), 0x05, OUTPUT(1), 0x02, // Data, Variable, Absolute + REPORT_COUNT(1), 0x01, REPORT_SIZE(1), 0x03, OUTPUT(1), 0x01, // Constant - REPORT_COUNT(1), 0x06, - REPORT_SIZE(1), 0x08, - LOGICAL_MINIMUM(1), 0x00, - LOGICAL_MAXIMUM(1), 0xFF, - USAGE_PAGE(1), 0x07, // Key Codes - USAGE_MINIMUM(1), 0x00, - USAGE_MAXIMUM(1), 0xFF, - INPUT(1), 0x00, // Data, Array + REPORT_COUNT(1), 0x06, REPORT_SIZE(1), 0x08, LOGICAL_MINIMUM(1), 0x00, LOGICAL_MAXIMUM(1), 0xFF, USAGE_PAGE(1), 0x07, // Key Codes + USAGE_MINIMUM(1), 0x00, USAGE_MAXIMUM(1), 0xFF, INPUT(1), 0x00, // Data, Array END_COLLECTION(0), }; reportLength = sizeof(reportDescriptor); @@ -113,48 +113,45 @@ uint16_t HIDKeyboard::reportDescLength() { return reportLength; } -#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ - + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ - + (1 * HID_DESCRIPTOR_LENGTH) \ - + (1 * ENDPOINT_DESCRIPTOR_LENGTH)) -uint8_t * HIDKeyboard::configurationDesc() { +#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) + (1 * INTERFACE_DESCRIPTOR_LENGTH) + (1 * HID_DESCRIPTOR_LENGTH) + (1 * ENDPOINT_DESCRIPTOR_LENGTH)) +uint8_t *HIDKeyboard::configurationDesc() { static uint8_t configurationDescriptor[] = { - CONFIGURATION_DESCRIPTOR_LENGTH,// bLength - CONFIGURATION_DESCRIPTOR, // bDescriptorType - LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) - MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) - 0x01, // bNumInterfaces - DEFAULT_CONFIGURATION, // bConfigurationValue - 0x00, // iConfiguration - C_RESERVED | C_REMOTE_WAKEUP, // bmAttributes - C_POWER(100), // bMaxPowerHello World from Mbed - - INTERFACE_DESCRIPTOR_LENGTH, // bLength - INTERFACE_DESCRIPTOR, // bDescriptorType - 0x00, // bInterfaceNumber - 0x00, // bAlternateSetting - 0x01, // bNumEndpoints - HID_CLASS, // bInterfaceClass - 1, // bInterfaceSubClass (boot) - 1, // bInterfaceProtocol (keyboard) - 0x00, // iInterface - - HID_DESCRIPTOR_LENGTH, // bLength - HID_DESCRIPTOR, // bDescriptorType - LSB(HID_VERSION_1_11), // bcdHID (LSB) - MSB(HID_VERSION_1_11), // bcdHID (MSB) - 0x00, // bCountryCode - 0x01, // bNumDescriptors - REPORT_DESCRIPTOR, // bDescriptorType + CONFIGURATION_DESCRIPTOR_LENGTH, // bLength + CONFIGURATION_DESCRIPTOR, // bDescriptorType + LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) + MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) + 0x01, // bNumInterfaces + DEFAULT_CONFIGURATION, // bConfigurationValue + 0x00, // iConfiguration + C_RESERVED | C_REMOTE_WAKEUP, // bmAttributes + C_POWER(100), // bMaxPowerHello World from Mbed + + INTERFACE_DESCRIPTOR_LENGTH, // bLength + INTERFACE_DESCRIPTOR, // bDescriptorType + 0x00, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x01, // bNumEndpoints + HID_CLASS, // bInterfaceClass + 1, // bInterfaceSubClass (boot) + 1, // bInterfaceProtocol (keyboard) + 0x00, // iInterface + + HID_DESCRIPTOR_LENGTH, // bLength + HID_DESCRIPTOR, // bDescriptorType + LSB(HID_VERSION_1_11), // bcdHID (LSB) + MSB(HID_VERSION_1_11), // bcdHID (MSB) + 0x00, // bCountryCode + 0x01, // bNumDescriptors + REPORT_DESCRIPTOR, // bDescriptorType (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB) (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB) - ENDPOINT_DESCRIPTOR_LENGTH, // bLength - ENDPOINT_DESCRIPTOR, // bDescriptorType - PHY_TO_DESC(EP1IN), // bEndpointAddress - E_INTERRUPT, // bmAttributes - LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) - MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) + ENDPOINT_DESCRIPTOR_LENGTH, // bLength + ENDPOINT_DESCRIPTOR, // bDescriptorType + PHY_TO_DESC(EP1IN), // bEndpointAddress + E_INTERRUPT, // bmAttributes + LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) + MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) 1, // bInterval (milliseconds) }; return configurationDescriptor; @@ -187,41 +184,35 @@ uint8_t * HIDKeyboard::deviceDesc() { #endif bool HIDKeyboard::USBCallback_request() { - bool success = false; - CONTROL_TRANSFER * transfer = getTransferPtr(); - uint8_t *hidDescriptor; + bool success = false; + CONTROL_TRANSFER *transfer = getTransferPtr(); + uint8_t * hidDescriptor; // Process additional standard requests - if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) - { - switch (transfer->setup.bRequest) - { + if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) { + switch (transfer->setup.bRequest) { case GET_DESCRIPTOR: - switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) - { + switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) { case REPORT_DESCRIPTOR: - if ((reportDesc() != NULL) \ - && (reportDescLength() != 0)) - { + if ((reportDesc() != NULL) && (reportDescLength() != 0)) { transfer->remaining = reportDescLength(); - transfer->ptr = reportDesc(); + transfer->ptr = reportDesc(); transfer->direction = DEVICE_TO_HOST; - success = true; + success = true; } break; case HID_DESCRIPTOR: - // Find the HID descriptor, after the configuration descriptor - hidDescriptor = findDescriptor(HID_DESCRIPTOR); - if (hidDescriptor != NULL) - { - transfer->remaining = HID_DESCRIPTOR_LENGTH; - transfer->ptr = hidDescriptor; - transfer->direction = DEVICE_TO_HOST; - success = true; - } - break; - + // Find the HID descriptor, after the configuration descriptor + hidDescriptor = findDescriptor(HID_DESCRIPTOR); + if (hidDescriptor != NULL) { + transfer->remaining = HID_DESCRIPTOR_LENGTH; + transfer->ptr = hidDescriptor; + transfer->direction = DEVICE_TO_HOST; + success = true; + } + break; + default: break; } @@ -232,8 +223,7 @@ bool HIDKeyboard::USBCallback_request() { } // Process class-specific requests - if (transfer->setup.bmRequestType.Type == CLASS_TYPE) - { + if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { switch (transfer->setup.bRequest) { case SET_REPORT: // LED indicator @@ -242,10 +232,10 @@ bool HIDKeyboard::USBCallback_request() { // if (transfer->setup.wLength == 1) transfer->remaining = 1; - //transfer->ptr = ?? what ptr should be set when OUT(not used?) + // transfer->ptr = ?? what ptr should be set when OUT(not used?) transfer->direction = HOST_TO_DEVICE; - transfer->notify = true; /* notify with USBCallback_requestCompleted */ - success = true; + transfer->notify = true; /* notify with USBCallback_requestCompleted */ + success = true; default: break; } @@ -254,8 +244,7 @@ bool HIDKeyboard::USBCallback_request() { return success; } -void HIDKeyboard::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) -{ +void HIDKeyboard::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) { if (length > 0) { CONTROL_TRANSFER *transfer = getTransferPtr(); if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { diff --git a/tmk_core/protocol/mbed/HIDKeyboard.h b/tmk_core/protocol/mbed/HIDKeyboard.h index c537e5ece..e8ff10869 100644 --- a/tmk_core/protocol/mbed/HIDKeyboard.h +++ b/tmk_core/protocol/mbed/HIDKeyboard.h @@ -1,30 +1,31 @@ #ifndef HIDKEYBOARD_H -#include "stdint.h" -#include "stdbool.h" -#include "USBHID.h" -#include "report.h" - +# include "stdint.h" +# include "stdbool.h" +# include "USBHID.h" +# include "report.h" class HIDKeyboard : public USBDevice { -public: + public: HIDKeyboard(uint16_t vendor_id = 0xFEED, uint16_t product_id = 0xabed, uint16_t product_release = 0x0001); - bool sendReport(report_keyboard_t report); + bool sendReport(report_keyboard_t report); uint8_t leds(void); -protected: - uint16_t reportLength; - virtual bool USBCallback_setConfiguration(uint8_t configuration); - virtual uint8_t * stringImanufacturerDesc(); - virtual uint8_t * stringIproductDesc(); - virtual uint8_t * stringIserialDesc(); + + protected: + uint16_t reportLength; + virtual bool USBCallback_setConfiguration(uint8_t configuration); + virtual uint8_t* stringImanufacturerDesc(); + virtual uint8_t* stringIproductDesc(); + virtual uint8_t* stringIserialDesc(); virtual uint16_t reportDescLength(); - virtual uint8_t * reportDesc(); - virtual uint8_t * configurationDesc(); - //virtual uint8_t * deviceDesc(); + virtual uint8_t* reportDesc(); + virtual uint8_t* configurationDesc(); + // virtual uint8_t * deviceDesc(); virtual bool USBCallback_request(); - virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); -private: + virtual void USBCallback_requestCompleted(uint8_t* buf, uint32_t length); + + private: uint8_t led_state; }; diff --git a/tmk_core/protocol/mbed/mbed_driver.cpp b/tmk_core/protocol/mbed/mbed_driver.cpp index 6c7b16e23..83086499c 100644 --- a/tmk_core/protocol/mbed/mbed_driver.cpp +++ b/tmk_core/protocol/mbed/mbed_driver.cpp @@ -5,37 +5,17 @@ HIDKeyboard keyboard; - /* Host driver */ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); - -host_driver_t mbed_driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); +host_driver_t mbed_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -static uint8_t keyboard_leds(void) -{ - return keyboard.leds(); -} -static void send_keyboard(report_keyboard_t *report) -{ - keyboard.sendReport(*report); -} -static void send_mouse(report_mouse_t *report) -{ -} -static void send_system(uint16_t data) -{ -} -static void send_consumer(uint16_t data) -{ -} +static uint8_t keyboard_leds(void) { return keyboard.leds(); } +static void send_keyboard(report_keyboard_t *report) { keyboard.sendReport(*report); } +static void send_mouse(report_mouse_t *report) {} +static void send_system(uint16_t data) {} +static void send_consumer(uint16_t data) {} diff --git a/tmk_core/protocol/midi/Config/LUFAConfig.h b/tmk_core/protocol/midi/Config/LUFAConfig.h index a1d748267..b346c05bd 100755..100644 --- a/tmk_core/protocol/midi/Config/LUFAConfig.h +++ b/tmk_core/protocol/midi/Config/LUFAConfig.h @@ -42,12 +42,12 @@ #ifndef _LUFA_CONFIG_H_ #define _LUFA_CONFIG_H_ - #if (ARCH == ARCH_AVR8) +#if (ARCH == ARCH_AVR8) - /* Non-USB Related Configuration Tokens: */ +/* Non-USB Related Configuration Tokens: */ // #define DISABLE_TERMINAL_CODES - /* USB Class Driver Related Tokens: */ +/* USB Class Driver Related Tokens: */ // #define HID_HOST_BOOT_PROTOCOL_ONLY // #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} // #define HID_USAGE_STACK_DEPTH {Insert Value Here} @@ -56,38 +56,38 @@ // #define HID_MAX_REPORT_IDS {Insert Value Here} // #define NO_CLASS_DRIVER_AUTOFLUSH - /* General USB Driver Related Tokens: */ +/* General USB Driver Related Tokens: */ // #define ORDERED_EP_CONFIG - #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) - #define USB_DEVICE_ONLY +# define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) +# define USB_DEVICE_ONLY // #define USB_HOST_ONLY // #define USB_STREAM_TIMEOUT_MS {Insert Value Here} // #define NO_LIMITED_CONTROLLER_CONNECT // #define NO_SOF_EVENTS - /* USB Device Mode Driver Related Tokens: */ +/* USB Device Mode Driver Related Tokens: */ // #define USE_RAM_DESCRIPTORS - #define USE_FLASH_DESCRIPTORS +# define USE_FLASH_DESCRIPTORS // #define USE_EEPROM_DESCRIPTORS // #define NO_INTERNAL_SERIAL - #define FIXED_CONTROL_ENDPOINT_SIZE 8 +# define FIXED_CONTROL_ENDPOINT_SIZE 8 // #define DEVICE_STATE_AS_GPIOR {Insert Value Here} - #define FIXED_NUM_CONFIGURATIONS 1 +# define FIXED_NUM_CONFIGURATIONS 1 // #define CONTROL_ONLY_DEVICE // #define INTERRUPT_CONTROL_ENDPOINT // #define NO_DEVICE_REMOTE_WAKEUP // #define NO_DEVICE_SELF_POWER - /* USB Host Mode Driver Related Tokens: */ +/* USB Host Mode Driver Related Tokens: */ // #define HOST_STATE_AS_GPIOR {Insert Value Here} // #define USB_HOST_TIMEOUT_MS {Insert Value Here} // #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} // #define NO_AUTO_VBUS_MANAGEMENT // #define INVERTED_VBUS_ENABLE_LINE - #else +#else - #error Unsupported architecture for this LUFA configuration file. +# error Unsupported architecture for this LUFA configuration file. - #endif +#endif #endif diff --git a/tmk_core/protocol/midi/bytequeue/bytequeue.c b/tmk_core/protocol/midi/bytequeue/bytequeue.c index e43495632..e36a9e66b 100755..100644 --- a/tmk_core/protocol/midi/bytequeue/bytequeue.c +++ b/tmk_core/protocol/midi/bytequeue/bytequeue.c @@ -1,65 +1,62 @@ -//this is a single reader [maybe multiple writer?] byte queue -//Copyright 2008 Alex Norman -//writen by Alex Norman +// this is a single reader [maybe multiple writer?] byte queue +// Copyright 2008 Alex Norman +// writen by Alex Norman // -//This file is part of avr-bytequeue. +// This file is part of avr-bytequeue. // -//avr-bytequeue 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 +// avr-bytequeue 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. // -//avr-bytequeue 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. +// avr-bytequeue 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 avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. #include "bytequeue.h" #include "interrupt_setting.h" -void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen){ - queue->length = arrayLen; - queue->data = dataArray; - queue->start = queue->end = 0; +void bytequeue_init(byteQueue_t* queue, uint8_t* dataArray, byteQueueIndex_t arrayLen) { + queue->length = arrayLen; + queue->data = dataArray; + queue->start = queue->end = 0; } -bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item){ - interrupt_setting_t setting = store_and_clear_interrupt(); - //full - if(((queue->end + 1) % queue->length) == queue->start){ - restore_interrupt_setting(setting); - return false; - } else { - queue->data[queue->end] = item; - queue->end = (queue->end + 1) % queue->length; - restore_interrupt_setting(setting); - return true; - } +bool bytequeue_enqueue(byteQueue_t* queue, uint8_t item) { + interrupt_setting_t setting = store_and_clear_interrupt(); + // full + if (((queue->end + 1) % queue->length) == queue->start) { + restore_interrupt_setting(setting); + return false; + } else { + queue->data[queue->end] = item; + queue->end = (queue->end + 1) % queue->length; + restore_interrupt_setting(setting); + return true; + } } -byteQueueIndex_t bytequeue_length(byteQueue_t * queue){ - byteQueueIndex_t len; - interrupt_setting_t setting = store_and_clear_interrupt(); - if(queue->end >= queue->start) - len = queue->end - queue->start; - else - len = (queue->length - queue->start) + queue->end; - restore_interrupt_setting(setting); - return len; +byteQueueIndex_t bytequeue_length(byteQueue_t* queue) { + byteQueueIndex_t len; + interrupt_setting_t setting = store_and_clear_interrupt(); + if (queue->end >= queue->start) + len = queue->end - queue->start; + else + len = (queue->length - queue->start) + queue->end; + restore_interrupt_setting(setting); + return len; } -//we don't need to avoid interrupts if there is only one reader -uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index){ - return queue->data[(queue->start + index) % queue->length]; -} +// we don't need to avoid interrupts if there is only one reader +uint8_t bytequeue_get(byteQueue_t* queue, byteQueueIndex_t index) { return queue->data[(queue->start + index) % queue->length]; } -//we just update the start index to remove elements -void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove){ - interrupt_setting_t setting = store_and_clear_interrupt(); - queue->start = (queue->start + numToRemove) % queue->length; - restore_interrupt_setting(setting); +// we just update the start index to remove elements +void bytequeue_remove(byteQueue_t* queue, byteQueueIndex_t numToRemove) { + interrupt_setting_t setting = store_and_clear_interrupt(); + queue->start = (queue->start + numToRemove) % queue->length; + restore_interrupt_setting(setting); } - diff --git a/tmk_core/protocol/midi/bytequeue/bytequeue.h b/tmk_core/protocol/midi/bytequeue/bytequeue.h index e4a286134..33fb63a8c 100755..100644 --- a/tmk_core/protocol/midi/bytequeue/bytequeue.h +++ b/tmk_core/protocol/midi/bytequeue/bytequeue.h @@ -1,28 +1,28 @@ -//this is a single reader [maybe multiple writer?] byte queue -//Copyright 2008 Alex Norman -//writen by Alex Norman +// this is a single reader [maybe multiple writer?] byte queue +// Copyright 2008 Alex Norman +// writen by Alex Norman // -//This file is part of avr-bytequeue. +// This file is part of avr-bytequeue. // -//avr-bytequeue 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 +// avr-bytequeue 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. // -//avr-bytequeue 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. +// avr-bytequeue 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 avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. #ifndef BYTEQUEUE_H #define BYTEQUEUE_H #ifdef __cplusplus extern "C" { -#endif +#endif #include <inttypes.h> #include <stdbool.h> @@ -30,30 +30,29 @@ extern "C" { typedef uint8_t byteQueueIndex_t; typedef struct { - byteQueueIndex_t start; - byteQueueIndex_t end; - byteQueueIndex_t length; - uint8_t * data; + byteQueueIndex_t start; + byteQueueIndex_t end; + byteQueueIndex_t length; + uint8_t* data; } byteQueue_t; -//you must have a queue, an array of data which the queue will use, and the length of that array -void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen); +// you must have a queue, an array of data which the queue will use, and the length of that array +void bytequeue_init(byteQueue_t* queue, uint8_t* dataArray, byteQueueIndex_t arrayLen); -//add an item to the queue, returns false if the queue is full -bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item); +// add an item to the queue, returns false if the queue is full +bool bytequeue_enqueue(byteQueue_t* queue, uint8_t item); -//get the length of the queue -byteQueueIndex_t bytequeue_length(byteQueue_t * queue); +// get the length of the queue +byteQueueIndex_t bytequeue_length(byteQueue_t* queue); -//this grabs data at the index given [starting at queue->start] -uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index); +// this grabs data at the index given [starting at queue->start] +uint8_t bytequeue_get(byteQueue_t* queue, byteQueueIndex_t index); -//update the index in the queue to reflect data that has been dealt with -void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove); +// update the index in the queue to reflect data that has been dealt with +void bytequeue_remove(byteQueue_t* queue, byteQueueIndex_t numToRemove); #ifdef __cplusplus } -#endif - #endif +#endif diff --git a/tmk_core/protocol/midi/bytequeue/interrupt_setting.c b/tmk_core/protocol/midi/bytequeue/interrupt_setting.c index 0ab8b5462..1be1fee97 100755..100644 --- a/tmk_core/protocol/midi/bytequeue/interrupt_setting.c +++ b/tmk_core/protocol/midi/bytequeue/interrupt_setting.c @@ -1,49 +1,43 @@ -//Copyright 20010 Alex Norman -//writen by Alex Norman +// Copyright 20010 Alex Norman +// writen by Alex Norman // -//This file is part of avr-bytequeue. +// This file is part of avr-bytequeue. // -//avr-bytequeue 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 +// avr-bytequeue 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. // -//avr-bytequeue 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. +// avr-bytequeue 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 avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. - -//AVR specific code -//should be able to port to other systems by simply providing chip specific -//implementations of the typedef and these functions +// AVR specific code +// should be able to port to other systems by simply providing chip specific +// implementations of the typedef and these functions #include "interrupt_setting.h" #if defined(__AVR__) -#include <avr/interrupt.h> +# include <avr/interrupt.h> interrupt_setting_t store_and_clear_interrupt(void) { - uint8_t sreg = SREG; - cli(); - return sreg; + uint8_t sreg = SREG; + cli(); + return sreg; } -void restore_interrupt_setting(interrupt_setting_t setting) { - SREG = setting; -} +void restore_interrupt_setting(interrupt_setting_t setting) { SREG = setting; } #elif defined(__arm__) -#include "ch.h" +# include "ch.h" interrupt_setting_t store_and_clear_interrupt(void) { - chSysLock(); - return 0; + chSysLock(); + return 0; } -void restore_interrupt_setting(interrupt_setting_t setting) { - chSysUnlock(); -} +void restore_interrupt_setting(interrupt_setting_t setting) { chSysUnlock(); } #endif - diff --git a/tmk_core/protocol/midi/bytequeue/interrupt_setting.h b/tmk_core/protocol/midi/bytequeue/interrupt_setting.h index 053d02c9d..788f75d79 100755..100644 --- a/tmk_core/protocol/midi/bytequeue/interrupt_setting.h +++ b/tmk_core/protocol/midi/bytequeue/interrupt_setting.h @@ -1,39 +1,38 @@ -//Copyright 20010 Alex Norman -//writen by Alex Norman +// Copyright 20010 Alex Norman +// writen by Alex Norman // -//This file is part of avr-bytequeue. +// This file is part of avr-bytequeue. // -//avr-bytequeue 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 +// avr-bytequeue 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. // -//avr-bytequeue 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. +// avr-bytequeue 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 avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. #ifndef INTERRUPT_SETTING_H #define INTERRUPT_SETTING_H #ifdef __cplusplus extern "C" { -#endif +#endif #include <inttypes.h> -//AVR specific typedef +// AVR specific typedef typedef uint8_t interrupt_setting_t; interrupt_setting_t store_and_clear_interrupt(void); -void restore_interrupt_setting(interrupt_setting_t setting); +void restore_interrupt_setting(interrupt_setting_t setting); #ifdef __cplusplus } -#endif - #endif +#endif diff --git a/tmk_core/protocol/midi/midi.c b/tmk_core/protocol/midi/midi.c index 11a589078..643c43722 100755..100644 --- a/tmk_core/protocol/midi/midi.c +++ b/tmk_core/protocol/midi/midi.c @@ -1,277 +1,181 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. #include "midi.h" -#include <string.h> //for memcpy +#include <string.h> //for memcpy -#define MIN(x,y) (((x) < (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) #ifndef NULL -#define NULL 0 +# define NULL 0 #endif -bool midi_is_statusbyte(uint8_t theByte){ - return (bool)(theByte & MIDI_STATUSMASK); -} +bool midi_is_statusbyte(uint8_t theByte) { return (bool)(theByte & MIDI_STATUSMASK); } -bool midi_is_realtime(uint8_t theByte){ - return (theByte >= MIDI_CLOCK); -} +bool midi_is_realtime(uint8_t theByte) { return (theByte >= MIDI_CLOCK); } -midi_packet_length_t midi_packet_length(uint8_t status){ - switch(status & 0xF0){ - case MIDI_CC: - case MIDI_NOTEON: - case MIDI_NOTEOFF: - case MIDI_AFTERTOUCH: - case MIDI_PITCHBEND: - return THREE; - case MIDI_PROGCHANGE: - case MIDI_CHANPRESSURE: - case MIDI_SONGSELECT: - return TWO; - case 0xF0: - switch(status) { - case MIDI_CLOCK: - case MIDI_TICK: - case MIDI_START: - case MIDI_CONTINUE: - case MIDI_STOP: - case MIDI_ACTIVESENSE: - case MIDI_RESET: - case MIDI_TUNEREQUEST: - return ONE; - case MIDI_SONGPOSITION: - return THREE; - case MIDI_TC_QUARTERFRAME: - case MIDI_SONGSELECT: - return TWO; - case SYSEX_END: - case SYSEX_BEGIN: - default: - return UNDEFINED; - } - default: - return UNDEFINED; - } +midi_packet_length_t midi_packet_length(uint8_t status) { + switch (status & 0xF0) { + case MIDI_CC: + case MIDI_NOTEON: + case MIDI_NOTEOFF: + case MIDI_AFTERTOUCH: + case MIDI_PITCHBEND: + return THREE; + case MIDI_PROGCHANGE: + case MIDI_CHANPRESSURE: + case MIDI_SONGSELECT: + return TWO; + case 0xF0: + switch (status) { + case MIDI_CLOCK: + case MIDI_TICK: + case MIDI_START: + case MIDI_CONTINUE: + case MIDI_STOP: + case MIDI_ACTIVESENSE: + case MIDI_RESET: + case MIDI_TUNEREQUEST: + return ONE; + case MIDI_SONGPOSITION: + return THREE; + case MIDI_TC_QUARTERFRAME: + case MIDI_SONGSELECT: + return TWO; + case SYSEX_END: + case SYSEX_BEGIN: + default: + return UNDEFINED; + } + default: + return UNDEFINED; + } } -void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val){ - //CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel. - //CC Data: Controller Num, Controller Val - device->send_func(device, 3, - MIDI_CC | (chan & MIDI_CHANMASK), - num & 0x7F, - val & 0x7F); +void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) { + // CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel. + // CC Data: Controller Num, Controller Val + device->send_func(device, 3, MIDI_CC | (chan & MIDI_CHANMASK), num & 0x7F, val & 0x7F); } -void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){ - //Note Data: Note Num, Note Velocity - device->send_func(device, 3, - MIDI_NOTEON | (chan & MIDI_CHANMASK), - num & 0x7F, - vel & 0x7F); +void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) { + // Note Data: Note Num, Note Velocity + device->send_func(device, 3, MIDI_NOTEON | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F); } -void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){ - //Note Data: Note Num, Note Velocity - device->send_func(device, 3, - MIDI_NOTEOFF | (chan & MIDI_CHANMASK), - num & 0x7F, - vel & 0x7F); +void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) { + // Note Data: Note Num, Note Velocity + device->send_func(device, 3, MIDI_NOTEOFF | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F); } -void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt){ - device->send_func(device, 3, - MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), - note_num & 0x7F, - amt & 0x7F); -} +void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt) { device->send_func(device, 3, MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), note_num & 0x7F, amt & 0x7F); } -//XXX does this work right? -//amt in range -0x2000, 0x1fff -//uAmt should be in range.. -//0x0000 to 0x3FFF -void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt){ - uint16_t uAmt; - //check range - if(amt > 0x1fff){ - uAmt = 0x3FFF; - } else if(amt < -0x2000){ - uAmt = 0; - } else { - uAmt = amt + 0x2000; - } - device->send_func(device, 3, - MIDI_PITCHBEND | (chan & MIDI_CHANMASK), - uAmt & 0x7F, - (uAmt >> 7) & 0x7F); +// XXX does this work right? +// amt in range -0x2000, 0x1fff +// uAmt should be in range.. +// 0x0000 to 0x3FFF +void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt) { + uint16_t uAmt; + // check range + if (amt > 0x1fff) { + uAmt = 0x3FFF; + } else if (amt < -0x2000) { + uAmt = 0; + } else { + uAmt = amt + 0x2000; + } + device->send_func(device, 3, MIDI_PITCHBEND | (chan & MIDI_CHANMASK), uAmt & 0x7F, (uAmt >> 7) & 0x7F); } -void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num){ - device->send_func(device, 2, - MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), - num & 0x7F, - 0); -} +void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num) { device->send_func(device, 2, MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), num & 0x7F, 0); } -void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt){ - device->send_func(device, 2, - MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), - amt & 0x7F, - 0); -} +void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt) { device->send_func(device, 2, MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), amt & 0x7F, 0); } -void midi_send_clock(MidiDevice * device){ - device->send_func(device, 1, MIDI_CLOCK, 0, 0); -} +void midi_send_clock(MidiDevice* device) { device->send_func(device, 1, MIDI_CLOCK, 0, 0); } -void midi_send_tick(MidiDevice * device){ - device->send_func(device, 1, MIDI_TICK, 0, 0); -} +void midi_send_tick(MidiDevice* device) { device->send_func(device, 1, MIDI_TICK, 0, 0); } -void midi_send_start(MidiDevice * device){ - device->send_func(device, 1, MIDI_START, 0, 0); -} +void midi_send_start(MidiDevice* device) { device->send_func(device, 1, MIDI_START, 0, 0); } -void midi_send_continue(MidiDevice * device){ - device->send_func(device, 1, MIDI_CONTINUE, 0, 0); -} +void midi_send_continue(MidiDevice* device) { device->send_func(device, 1, MIDI_CONTINUE, 0, 0); } -void midi_send_stop(MidiDevice * device){ - device->send_func(device, 1, MIDI_STOP, 0, 0); -} - -void midi_send_activesense(MidiDevice * device){ - device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0); -} +void midi_send_stop(MidiDevice* device) { device->send_func(device, 1, MIDI_STOP, 0, 0); } -void midi_send_reset(MidiDevice * device){ - device->send_func(device, 1, MIDI_RESET, 0, 0); -} +void midi_send_activesense(MidiDevice* device) { device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0); } -void midi_send_tcquarterframe(MidiDevice * device, uint8_t time){ - device->send_func(device, 2, - MIDI_TC_QUARTERFRAME, - time & 0x7F, - 0); -} +void midi_send_reset(MidiDevice* device) { device->send_func(device, 1, MIDI_RESET, 0, 0); } -//XXX is this right? -void midi_send_songposition(MidiDevice * device, uint16_t pos){ - device->send_func(device, 3, - MIDI_SONGPOSITION, - pos & 0x7F, - (pos >> 7) & 0x7F); -} +void midi_send_tcquarterframe(MidiDevice* device, uint8_t time) { device->send_func(device, 2, MIDI_TC_QUARTERFRAME, time & 0x7F, 0); } -void midi_send_songselect(MidiDevice * device, uint8_t song){ - device->send_func(device, 2, - MIDI_SONGSELECT, - song & 0x7F, - 0); -} +// XXX is this right? +void midi_send_songposition(MidiDevice* device, uint16_t pos) { device->send_func(device, 3, MIDI_SONGPOSITION, pos & 0x7F, (pos >> 7) & 0x7F); } -void midi_send_tunerequest(MidiDevice * device){ - device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0); -} +void midi_send_songselect(MidiDevice* device, uint8_t song) { device->send_func(device, 2, MIDI_SONGSELECT, song & 0x7F, 0); } -void midi_send_byte(MidiDevice * device, uint8_t b){ - device->send_func(device, 1, b, 0, 0); -} +void midi_send_tunerequest(MidiDevice* device) { device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0); } -void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2){ - //ensure that the count passed along is always 3 or lower - if (count > 3) { - //TODO how to do this correctly? - } - device->send_func(device, count, byte0, byte1, byte2); -} +void midi_send_byte(MidiDevice* device, uint8_t b) { device->send_func(device, 1, b, 0, 0); } -void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array) { - uint16_t i; - for (i = 0; i < count; i += 3) { - uint8_t b[3] = { 0, 0, 0 }; - uint16_t to_send = count - i; - to_send = (to_send > 3) ? 3 : to_send; - memcpy(b, array + i, to_send); - midi_send_data(device, to_send, b[0], b[1], b[2]); - } +void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2) { + // ensure that the count passed along is always 3 or lower + if (count > 3) { + // TODO how to do this correctly? + } + device->send_func(device, count, byte0, byte1, byte2); } - -void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_cc_callback = func; +void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array) { + uint16_t i; + for (i = 0; i < count; i += 3) { + uint8_t b[3] = {0, 0, 0}; + uint16_t to_send = count - i; + to_send = (to_send > 3) ? 3 : to_send; + memcpy(b, array + i, to_send); + midi_send_data(device, to_send, b[0], b[1], b[2]); + } } -void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_noteon_callback = func; -} +void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_cc_callback = func; } -void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_noteoff_callback = func; -} +void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteon_callback = func; } -void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_aftertouch_callback = func; -} +void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteoff_callback = func; } -void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_pitchbend_callback = func; -} +void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_aftertouch_callback = func; } -void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func){ - device->input_songposition_callback = func; -} +void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_pitchbend_callback = func; } -void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func) { - device->input_progchange_callback = func; -} +void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_songposition_callback = func; } -void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func) { - device->input_chanpressure_callback = func; -} +void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_progchange_callback = func; } -void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func) { - device->input_songselect_callback = func; -} +void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_chanpressure_callback = func; } -void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func) { - device->input_tc_quarterframe_callback = func; -} +void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_songselect_callback = func; } -void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func){ - device->input_realtime_callback = func; -} +void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_tc_quarterframe_callback = func; } -void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func){ - device->input_tunerequest_callback = func; -} +void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_realtime_callback = func; } -void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func) { - device->input_sysex_callback = func; -} +void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_tunerequest_callback = func; } -void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func){ - device->input_fallthrough_callback = func; -} +void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func) { device->input_sysex_callback = func; } -void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func){ - device->input_catchall_callback = func; -} +void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_fallthrough_callback = func; } +void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_catchall_callback = func; } diff --git a/tmk_core/protocol/midi/midi.h b/tmk_core/protocol/midi/midi.h index 1a36737df..07d8cebc1 100755..100644 --- a/tmk_core/protocol/midi/midi.h +++ b/tmk_core/protocol/midi/midi.h @@ -1,20 +1,20 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. /** * @file @@ -30,7 +30,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif #include "midi_device.h" #include "midi_function_types.h" @@ -48,8 +48,8 @@ extern "C" { * You must call this before using the device in question. * * @param device the device to initialize -*/ -void midi_device_init(MidiDevice * device); // [implementation in midi_device.c] + */ +void midi_device_init(MidiDevice* device); // [implementation in midi_device.c] /** * @brief Process input data @@ -58,8 +58,8 @@ void midi_device_init(MidiDevice * device); // [implementation in midi_device.c] * if you expect to have your input callbacks called. * * @param device the device to process -*/ -void midi_device_process(MidiDevice * device); // [implementation in midi_device.c] + */ +void midi_device_process(MidiDevice* device); // [implementation in midi_device.c] /**@}*/ @@ -76,8 +76,8 @@ void midi_device_process(MidiDevice * device); // [implementation in midi_device * @param chan the channel to send on, 0-15 * @param num the cc num * @param val the value of that cc num -*/ -void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val); + */ +void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val); /** * @brief Send a note on message via the given device. @@ -86,8 +86,8 @@ void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val); * @param chan the channel to send on, 0-15 * @param num the note number * @param vel the note velocity -*/ -void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel); + */ +void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel); /** * @brief Send a note off message via the given device. @@ -96,8 +96,8 @@ void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t ve * @param chan the channel to send on, 0-15 * @param num the note number * @param vel the note velocity -*/ -void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel); + */ +void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel); /** * @brief Send an after touch message via the given device. @@ -106,8 +106,8 @@ void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t v * @param chan the channel to send on, 0-15 * @param note_num the note number * @param amt the after touch amount -*/ -void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt); + */ +void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt); /** * @brief Send a pitch bend message via the given device. @@ -115,8 +115,8 @@ void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, u * @param device the device to use for sending * @param chan the channel to send on, 0-15 * @param amt the bend amount range: -8192..8191, 0 means no bend -*/ -void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt); //range -8192, 8191 + */ +void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt); // range -8192, 8191 /** * @brief Send a program change message via the given device. @@ -124,8 +124,8 @@ void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt); //rang * @param device the device to use for sending * @param chan the channel to send on, 0-15 * @param num the program to change to -*/ -void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num); + */ +void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num); /** * @brief Send a channel pressure message via the given device. @@ -133,58 +133,57 @@ void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num); * @param device the device to use for sending * @param chan the channel to send on, 0-15 * @param amt the amount of channel pressure -*/ -void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt); + */ +void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt); /** * @brief Send a clock message via the given device. * * @param device the device to use for sending */ -void midi_send_clock(MidiDevice * device); +void midi_send_clock(MidiDevice* device); /** * @brief Send a tick message via the given device. * * @param device the device to use for sending */ -void midi_send_tick(MidiDevice * device); +void midi_send_tick(MidiDevice* device); /** * @brief Send a start message via the given device. * * @param device the device to use for sending */ -void midi_send_start(MidiDevice * device); +void midi_send_start(MidiDevice* device); /** * @brief Send a continue message via the given device. * * @param device the device to use for sending */ -void midi_send_continue(MidiDevice * device); +void midi_send_continue(MidiDevice* device); /** * @brief Send a stop message via the given device. * * @param device the device to use for sending */ -void midi_send_stop(MidiDevice * device); +void midi_send_stop(MidiDevice* device); /** * @brief Send an active sense message via the given device. * * @param device the device to use for sending */ -void midi_send_activesense(MidiDevice * device); +void midi_send_activesense(MidiDevice* device); /** * @brief Send a reset message via the given device. * * @param device the device to use for sending */ -void midi_send_reset(MidiDevice * device); - +void midi_send_reset(MidiDevice* device); /** * @brief Send a tc quarter frame message via the given device. @@ -192,7 +191,7 @@ void midi_send_reset(MidiDevice * device); * @param device the device to use for sending * @param time the time of this quarter frame, range 0..16383 */ -void midi_send_tcquarterframe(MidiDevice * device, uint8_t time); +void midi_send_tcquarterframe(MidiDevice* device, uint8_t time); /** * @brief Send a song position message via the given device. @@ -200,7 +199,7 @@ void midi_send_tcquarterframe(MidiDevice * device, uint8_t time); * @param device the device to use for sending * @param pos the song position */ -void midi_send_songposition(MidiDevice * device, uint16_t pos); +void midi_send_songposition(MidiDevice* device, uint16_t pos); /** * @brief Send a song select message via the given device. @@ -208,14 +207,14 @@ void midi_send_songposition(MidiDevice * device, uint16_t pos); * @param device the device to use for sending * @param song the song to select */ -void midi_send_songselect(MidiDevice * device, uint8_t song); +void midi_send_songselect(MidiDevice* device, uint8_t song); /** * @brief Send a tune request message via the given device. * * @param device the device to use for sending */ -void midi_send_tunerequest(MidiDevice * device); +void midi_send_tunerequest(MidiDevice* device); /** * @brief Send a byte via the given device. @@ -228,7 +227,7 @@ void midi_send_tunerequest(MidiDevice * device); * @param device the device to use for sending * @param b the byte to send */ -void midi_send_byte(MidiDevice * device, uint8_t b); +void midi_send_byte(MidiDevice* device, uint8_t b); /** * @brief Send up to 3 bytes of data @@ -241,7 +240,7 @@ void midi_send_byte(MidiDevice * device, uint8_t b); * @param byte1 the second byte, ignored if cnt % 4 != 2 * @param byte2 the third byte, ignored if cnt % 4 != 3 */ -void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); +void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); /** * @brief Send an array of formatted midi data. @@ -252,14 +251,13 @@ void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t * @param count the count of bytes to send * @param array the array of bytes */ -void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array); +void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array); /**@}*/ - /** * @defgroup input_callback_reg Input callback registration functions - * + * * @brief These are the functions you use to register your input callbacks. * * The functions are called when the appropriate midi message is matched on the @@ -268,7 +266,7 @@ void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array); * @{ */ -//three byte funcs +// three byte funcs /** * @brief Register a control change message (cc) callback. @@ -276,7 +274,7 @@ void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array); * @param device the device associate with * @param func the callback function to register */ -void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func); /** * @brief Register a note on callback. @@ -284,7 +282,7 @@ void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func) * @param device the device associate with * @param func the callback function to register */ -void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func); /** * @brief Register a note off callback. @@ -292,7 +290,7 @@ void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t f * @param device the device associate with * @param func the callback function to register */ -void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func); /** * @brief Register an after touch callback. @@ -301,7 +299,7 @@ void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t * @param func the callback function to register */ -void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func); /** * @brief Register a pitch bend callback. @@ -309,7 +307,7 @@ void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func * @param device the device associate with * @param func the callback function to register */ -void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func); /** * @brief Register a song position callback. @@ -317,9 +315,9 @@ void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_ * @param device the device associate with * @param func the callback function to register */ -void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func); +void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func); -//two byte funcs +// two byte funcs /** * @brief Register a program change callback. @@ -327,7 +325,7 @@ void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_fu * @param device the device associate with * @param func the callback function to register */ -void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func); +void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func); /** * @brief Register a channel pressure callback. @@ -335,7 +333,7 @@ void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t * @param device the device associate with * @param func the callback function to register */ -void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func); +void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func); /** * @brief Register a song select callback. @@ -343,7 +341,7 @@ void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func * @param device the device associate with * @param func the callback function to register */ -void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func); +void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func); /** * @brief Register a tc quarter frame callback. @@ -351,9 +349,9 @@ void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t * @param device the device associate with * @param func the callback function to register */ -void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func); +void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func); -//one byte funcs +// one byte funcs /** * @brief Register a realtime callback. @@ -363,7 +361,7 @@ void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_f * @param device the device associate with * @param func the callback function to register */ -void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func); +void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func); /** * @brief Register a tune request callback. @@ -371,7 +369,7 @@ void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t f * @param device the device associate with * @param func the callback function to register */ -void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func); +void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func); /** * @brief Register a sysex callback. @@ -379,7 +377,7 @@ void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_ * @param device the device associate with * @param func the callback function to register */ -void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func); +void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func); /** * @brief Register fall through callback. @@ -391,8 +389,7 @@ void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func); * @param device the device associate with * @param func the callback function to register */ -void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func); - +void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func); /** * @brief Register a catch all callback. @@ -403,7 +400,7 @@ void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_ * @param device the device associate with * @param func the callback function to register */ -void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func); +void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func); /**@}*/ @@ -417,11 +414,7 @@ void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t f * * An enumeration of the possible packet length values. */ -typedef enum { - UNDEFINED = 0, - ONE = 1, - TWO = 2, - THREE = 3} midi_packet_length_t; +typedef enum { UNDEFINED = 0, ONE = 1, TWO = 2, THREE = 3 } midi_packet_length_t; /** * @brief Test to see if the byte given is a status byte @@ -456,11 +449,11 @@ midi_packet_length_t midi_packet_length(uint8_t status); #define SYSEX_BEGIN 0xF0 #define SYSEX_END 0xF7 -//if you and this with a byte and you get anything non-zero -//it is a status message +// if you and this with a byte and you get anything non-zero +// it is a status message #define MIDI_STATUSMASK 0x80 -//if you and this with a status message that contains channel info, -//you'll get the channel +// if you and this with a status message that contains channel info, +// you'll get the channel #define MIDI_CHANMASK 0x0F #define MIDI_CC 0xB0 @@ -471,7 +464,7 @@ midi_packet_length_t midi_packet_length(uint8_t status); #define MIDI_PROGCHANGE 0xC0 #define MIDI_CHANPRESSURE 0xD0 -//midi realtime +// midi realtime #define MIDI_CLOCK 0xF8 #define MIDI_TICK 0xF9 #define MIDI_START 0xFA @@ -485,14 +478,13 @@ midi_packet_length_t midi_packet_length(uint8_t status); #define MIDI_SONGSELECT 0xF3 #define MIDI_TUNEREQUEST 0xF6 -//This ID is for educational or development use only +// This ID is for educational or development use only #define SYSEX_EDUMANUFID 0x7D /**@}*/ #ifdef __cplusplus } -#endif - #endif +#endif diff --git a/tmk_core/protocol/midi/midi_device.c b/tmk_core/protocol/midi/midi_device.c index 3215a007d..58fe685e2 100755..100644 --- a/tmk_core/protocol/midi/midi_device.c +++ b/tmk_core/protocol/midi/midi_device.c @@ -1,291 +1,272 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. #include "midi_device.h" #include "midi.h" #ifndef NULL -#define NULL 0 +# define NULL 0 #endif -//forward declarations, internally used to call the callbacks -void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); -void midi_process_byte(MidiDevice * device, uint8_t input); +// forward declarations, internally used to call the callbacks +void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); +void midi_process_byte(MidiDevice* device, uint8_t input); -void midi_device_init(MidiDevice * device){ - device->input_state = IDLE; - device->input_count = 0; - bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH); +void midi_device_init(MidiDevice* device) { + device->input_state = IDLE; + device->input_count = 0; + bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH); - //three byte funcs - device->input_cc_callback = NULL; - device->input_noteon_callback = NULL; - device->input_noteoff_callback = NULL; - device->input_aftertouch_callback = NULL; - device->input_pitchbend_callback = NULL; - device->input_songposition_callback = NULL; + // three byte funcs + device->input_cc_callback = NULL; + device->input_noteon_callback = NULL; + device->input_noteoff_callback = NULL; + device->input_aftertouch_callback = NULL; + device->input_pitchbend_callback = NULL; + device->input_songposition_callback = NULL; - //two byte funcs - device->input_progchange_callback = NULL; - device->input_chanpressure_callback = NULL; - device->input_songselect_callback = NULL; - device->input_tc_quarterframe_callback = NULL; + // two byte funcs + device->input_progchange_callback = NULL; + device->input_chanpressure_callback = NULL; + device->input_songselect_callback = NULL; + device->input_tc_quarterframe_callback = NULL; - //one byte funcs - device->input_realtime_callback = NULL; - device->input_tunerequest_callback = NULL; + // one byte funcs + device->input_realtime_callback = NULL; + device->input_tunerequest_callback = NULL; - //var byte functions - device->input_sysex_callback = NULL; - device->input_fallthrough_callback = NULL; - device->input_catchall_callback = NULL; + // var byte functions + device->input_sysex_callback = NULL; + device->input_fallthrough_callback = NULL; + device->input_catchall_callback = NULL; - device->pre_input_process_callback = NULL; + device->pre_input_process_callback = NULL; } -void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input) { - uint8_t i; - for (i = 0; i < cnt; i++) - bytequeue_enqueue(&device->input_queue, input[i]); +void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input) { + uint8_t i; + for (i = 0; i < cnt; i++) bytequeue_enqueue(&device->input_queue, input[i]); } -void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func){ - device->send_func = send_func; -} +void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func) { device->send_func = send_func; } -void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func){ - device->pre_input_process_callback = pre_process_func; -} +void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func) { device->pre_input_process_callback = pre_process_func; } -void midi_device_process(MidiDevice * device) { - //call the pre_input_process_callback if there is one - if(device->pre_input_process_callback) - device->pre_input_process_callback(device); +void midi_device_process(MidiDevice* device) { + // call the pre_input_process_callback if there is one + if (device->pre_input_process_callback) device->pre_input_process_callback(device); - //pull stuff off the queue and process - byteQueueIndex_t len = bytequeue_length(&device->input_queue); - uint16_t i; - //TODO limit number of bytes processed? - for(i = 0; i < len; i++) { - uint8_t val = bytequeue_get(&device->input_queue, 0); - midi_process_byte(device, val); - bytequeue_remove(&device->input_queue, 1); - } + // pull stuff off the queue and process + byteQueueIndex_t len = bytequeue_length(&device->input_queue); + uint16_t i; + // TODO limit number of bytes processed? + for (i = 0; i < len; i++) { + uint8_t val = bytequeue_get(&device->input_queue, 0); + midi_process_byte(device, val); + bytequeue_remove(&device->input_queue, 1); + } } -void midi_process_byte(MidiDevice * device, uint8_t input) { - if (midi_is_realtime(input)) { - //call callback, store and restore state - input_state_t state = device->input_state; - device->input_state = ONE_BYTE_MESSAGE; - midi_input_callbacks(device, 1, input, 0, 0); - device->input_state = state; - } else if (midi_is_statusbyte(input)) { - //store the byte - if (device->input_state != SYSEX_MESSAGE) { - device->input_buffer[0] = input; - device->input_count = 1; - } - switch (midi_packet_length(input)) { - case ONE: - device->input_state = ONE_BYTE_MESSAGE;; +void midi_process_byte(MidiDevice* device, uint8_t input) { + if (midi_is_realtime(input)) { + // call callback, store and restore state + input_state_t state = device->input_state; + device->input_state = ONE_BYTE_MESSAGE; midi_input_callbacks(device, 1, input, 0, 0); - device->input_state = IDLE; - break; - case TWO: - device->input_state = TWO_BYTE_MESSAGE; - break; - case THREE: - device->input_state = THREE_BYTE_MESSAGE; - break; - case UNDEFINED: - switch(input) { - case SYSEX_BEGIN: - device->input_state = SYSEX_MESSAGE; + device->input_state = state; + } else if (midi_is_statusbyte(input)) { + // store the byte + if (device->input_state != SYSEX_MESSAGE) { device->input_buffer[0] = input; - device->input_count = 1; - break; - case SYSEX_END: - //send what is left in the input buffer, set idle - device->input_buffer[device->input_count % 3] = input; - device->input_count += 1; - //call the callback - midi_input_callbacks(device, device->input_count, - device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); - device->input_state = IDLE; - break; - default: - device->input_state = IDLE; - device->input_count = 0; + device->input_count = 1; } + switch (midi_packet_length(input)) { + case ONE: + device->input_state = ONE_BYTE_MESSAGE; + ; + midi_input_callbacks(device, 1, input, 0, 0); + device->input_state = IDLE; + break; + case TWO: + device->input_state = TWO_BYTE_MESSAGE; + break; + case THREE: + device->input_state = THREE_BYTE_MESSAGE; + break; + case UNDEFINED: + switch (input) { + case SYSEX_BEGIN: + device->input_state = SYSEX_MESSAGE; + device->input_buffer[0] = input; + device->input_count = 1; + break; + case SYSEX_END: + // send what is left in the input buffer, set idle + device->input_buffer[device->input_count % 3] = input; + device->input_count += 1; + // call the callback + midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); + device->input_state = IDLE; + break; + default: + device->input_state = IDLE; + device->input_count = 0; + } - break; - default: - device->input_state = IDLE; - device->input_count = 0; - break; - } - } else { - if (device->input_state != IDLE) { - //store the byte - device->input_buffer[device->input_count % 3] = input; - //increment count - uint16_t prev = device->input_count; - device->input_count += 1; + break; + default: + device->input_state = IDLE; + device->input_count = 0; + break; + } + } else { + if (device->input_state != IDLE) { + // store the byte + device->input_buffer[device->input_count % 3] = input; + // increment count + uint16_t prev = device->input_count; + device->input_count += 1; - switch(prev % 3) { - case 2: - //call callback - midi_input_callbacks(device, device->input_count, - device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); - if (device->input_state != SYSEX_MESSAGE) { - //set to 1, keeping status byte, allowing for running status - device->input_count = 1; - } - break; - case 1: - if (device->input_state == TWO_BYTE_MESSAGE) { - //call callback - midi_input_callbacks(device, device->input_count, - device->input_buffer[0], device->input_buffer[1], 0); - if (device->input_state != SYSEX_MESSAGE) { - //set to 1, keeping status byte, allowing for running status - device->input_count = 1; + switch (prev % 3) { + case 2: + // call callback + midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); + if (device->input_state != SYSEX_MESSAGE) { + // set to 1, keeping status byte, allowing for running status + device->input_count = 1; + } + break; + case 1: + if (device->input_state == TWO_BYTE_MESSAGE) { + // call callback + midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], 0); + if (device->input_state != SYSEX_MESSAGE) { + // set to 1, keeping status byte, allowing for running status + device->input_count = 1; + } + } + break; + case 0: + default: + // one byte messages are dealt with directly + break; } - } - break; - case 0: - default: - //one byte messages are dealt with directly - break; - } + } } - } } -void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { - //did we end up calling a callback? - bool called = false; - if (device->input_state == SYSEX_MESSAGE) { - if (device->input_sysex_callback) { - const uint16_t start = ((cnt - 1) / 3) * 3; - const uint8_t length = (cnt - start); - uint8_t data[3]; - data[0] = byte0; - data[1] = byte1; - data[2] = byte2; - device->input_sysex_callback(device, start, length, data); - called = true; - } - } else { - switch (cnt) { - case 3: - { - midi_three_byte_func_t func = NULL; - switch (byte0 & 0xF0) { - case MIDI_CC: - func = device->input_cc_callback; - break; - case MIDI_NOTEON: - func = device->input_noteon_callback; - break; - case MIDI_NOTEOFF: - func = device->input_noteoff_callback; - break; - case MIDI_AFTERTOUCH: - func = device->input_aftertouch_callback; - break; - case MIDI_PITCHBEND: - func = device->input_pitchbend_callback; - break; - case 0xF0: - if (byte0 == MIDI_SONGPOSITION) - func = device->input_songposition_callback; - break; - default: - break; - } - if(func) { - //mask off the channel for non song position functions - if (byte0 == MIDI_SONGPOSITION) - func(device, byte0, byte1, byte2); - else - func(device, byte0 & 0x0F, byte1, byte2); +void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { + // did we end up calling a callback? + bool called = false; + if (device->input_state == SYSEX_MESSAGE) { + if (device->input_sysex_callback) { + const uint16_t start = ((cnt - 1) / 3) * 3; + const uint8_t length = (cnt - start); + uint8_t data[3]; + data[0] = byte0; + data[1] = byte1; + data[2] = byte2; + device->input_sysex_callback(device, start, length, data); called = true; - } } - break; - case 2: - { - midi_two_byte_func_t func = NULL; - switch (byte0 & 0xF0) { - case MIDI_PROGCHANGE: - func = device->input_progchange_callback; - break; - case MIDI_CHANPRESSURE: - func = device->input_chanpressure_callback; - break; - case 0xF0: - if (byte0 == MIDI_SONGSELECT) - func = device->input_songselect_callback; - else if (byte0 == MIDI_TC_QUARTERFRAME) - func = device->input_tc_quarterframe_callback; - break; + } else { + switch (cnt) { + case 3: { + midi_three_byte_func_t func = NULL; + switch (byte0 & 0xF0) { + case MIDI_CC: + func = device->input_cc_callback; + break; + case MIDI_NOTEON: + func = device->input_noteon_callback; + break; + case MIDI_NOTEOFF: + func = device->input_noteoff_callback; + break; + case MIDI_AFTERTOUCH: + func = device->input_aftertouch_callback; + break; + case MIDI_PITCHBEND: + func = device->input_pitchbend_callback; + break; + case 0xF0: + if (byte0 == MIDI_SONGPOSITION) func = device->input_songposition_callback; + break; + default: + break; + } + if (func) { + // mask off the channel for non song position functions + if (byte0 == MIDI_SONGPOSITION) + func(device, byte0, byte1, byte2); + else + func(device, byte0 & 0x0F, byte1, byte2); + called = true; + } + } break; + case 2: { + midi_two_byte_func_t func = NULL; + switch (byte0 & 0xF0) { + case MIDI_PROGCHANGE: + func = device->input_progchange_callback; + break; + case MIDI_CHANPRESSURE: + func = device->input_chanpressure_callback; + break; + case 0xF0: + if (byte0 == MIDI_SONGSELECT) + func = device->input_songselect_callback; + else if (byte0 == MIDI_TC_QUARTERFRAME) + func = device->input_tc_quarterframe_callback; + break; + default: + break; + } + if (func) { + // mask off the channel + if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME) + func(device, byte0, byte1); + else + func(device, byte0 & 0x0F, byte1); + called = true; + } + } break; + case 1: { + midi_one_byte_func_t func = NULL; + if (midi_is_realtime(byte0)) + func = device->input_realtime_callback; + else if (byte0 == MIDI_TUNEREQUEST) + func = device->input_tunerequest_callback; + if (func) { + func(device, byte0); + called = true; + } + } break; default: - break; - } - if(func) { - //mask off the channel - if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME) - func(device, byte0, byte1); - else - func(device, byte0 & 0x0F, byte1); - called = true; - } - } - break; - case 1: - { - midi_one_byte_func_t func = NULL; - if (midi_is_realtime(byte0)) - func = device->input_realtime_callback; - else if (byte0 == MIDI_TUNEREQUEST) - func = device->input_tunerequest_callback; - if (func) { - func(device, byte0); - called = true; - } + // just in case + if (cnt > 3) cnt = 0; + break; } - break; - default: - //just in case - if (cnt > 3) - cnt = 0; - break; } - } - //if there is fallthrough default callback and we haven't called a more specific one, - //call the fallthrough - if (!called && device->input_fallthrough_callback) - device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2); - //always call the catch all if it exists - if (device->input_catchall_callback) - device->input_catchall_callback(device, cnt, byte0, byte1, byte2); + // if there is fallthrough default callback and we haven't called a more specific one, + // call the fallthrough + if (!called && device->input_fallthrough_callback) device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2); + // always call the catch all if it exists + if (device->input_catchall_callback) device->input_catchall_callback(device, cnt, byte0, byte1, byte2); } - diff --git a/tmk_core/protocol/midi/midi_device.h b/tmk_core/protocol/midi/midi_device.h index 088995286..693d81a8e 100755..100644 --- a/tmk_core/protocol/midi/midi_device.h +++ b/tmk_core/protocol/midi/midi_device.h @@ -1,20 +1,20 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. /** * @file @@ -26,7 +26,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif /** * @defgroup midi_device Functions used when implementing your own midi device. @@ -51,14 +51,9 @@ extern "C" { #include "bytequeue/bytequeue.h" #define MIDI_INPUT_QUEUE_LENGTH 192 -typedef enum { - IDLE, - ONE_BYTE_MESSAGE = 1, - TWO_BYTE_MESSAGE = 2, - THREE_BYTE_MESSAGE = 3, - SYSEX_MESSAGE} input_state_t; +typedef enum { IDLE, ONE_BYTE_MESSAGE = 1, TWO_BYTE_MESSAGE = 2, THREE_BYTE_MESSAGE = 3, SYSEX_MESSAGE } input_state_t; -typedef void (* midi_no_byte_func_t)(MidiDevice * device); +typedef void (*midi_no_byte_func_t)(MidiDevice* device); /** * \struct _midi_device @@ -71,45 +66,45 @@ typedef void (* midi_no_byte_func_t)(MidiDevice * device); * You should not need to modify this structure directly. */ struct _midi_device { - //output send function - midi_var_byte_func_t send_func; - - //********input callbacks - //three byte funcs - midi_three_byte_func_t input_cc_callback; - midi_three_byte_func_t input_noteon_callback; - midi_three_byte_func_t input_noteoff_callback; - midi_three_byte_func_t input_aftertouch_callback; - midi_three_byte_func_t input_pitchbend_callback; - midi_three_byte_func_t input_songposition_callback; - //two byte funcs - midi_two_byte_func_t input_progchange_callback; - midi_two_byte_func_t input_chanpressure_callback; - midi_two_byte_func_t input_songselect_callback; - midi_two_byte_func_t input_tc_quarterframe_callback; - //one byte funcs - midi_one_byte_func_t input_realtime_callback; - midi_one_byte_func_t input_tunerequest_callback; - - //sysex - midi_sysex_func_t input_sysex_callback; - - //only called if more specific callback is not matched - midi_var_byte_func_t input_fallthrough_callback; - //called if registered, independent of other callbacks - midi_var_byte_func_t input_catchall_callback; - - //pre input processing function - midi_no_byte_func_t pre_input_process_callback; - - //for internal input processing - uint8_t input_buffer[3]; - input_state_t input_state; - uint16_t input_count; - - //for queueing data between the input and the processing functions - uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH]; - byteQueue_t input_queue; + // output send function + midi_var_byte_func_t send_func; + + //********input callbacks + // three byte funcs + midi_three_byte_func_t input_cc_callback; + midi_three_byte_func_t input_noteon_callback; + midi_three_byte_func_t input_noteoff_callback; + midi_three_byte_func_t input_aftertouch_callback; + midi_three_byte_func_t input_pitchbend_callback; + midi_three_byte_func_t input_songposition_callback; + // two byte funcs + midi_two_byte_func_t input_progchange_callback; + midi_two_byte_func_t input_chanpressure_callback; + midi_two_byte_func_t input_songselect_callback; + midi_two_byte_func_t input_tc_quarterframe_callback; + // one byte funcs + midi_one_byte_func_t input_realtime_callback; + midi_one_byte_func_t input_tunerequest_callback; + + // sysex + midi_sysex_func_t input_sysex_callback; + + // only called if more specific callback is not matched + midi_var_byte_func_t input_fallthrough_callback; + // called if registered, independent of other callbacks + midi_var_byte_func_t input_catchall_callback; + + // pre input processing function + midi_no_byte_func_t pre_input_process_callback; + + // for internal input processing + uint8_t input_buffer[3]; + input_state_t input_state; + uint16_t input_count; + + // for queueing data between the input and the processing functions + uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH]; + byteQueue_t input_queue; }; /** @@ -122,7 +117,7 @@ struct _midi_device { * @param cnt the number of bytes you are processing * @param input the bytes to process */ -void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input); +void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input); /** * @brief Set the callback function that will be used for sending output @@ -134,7 +129,7 @@ void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input); * \param device the midi device to associate this callback with * \param send_func the callback function that will do the sending */ -void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func); +void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func); /** * @brief Set a callback which is called at the beginning of the @@ -145,12 +140,12 @@ void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_fu * \param device the midi device to associate this callback with * \param midi_no_byte_func_t the actual callback function */ -void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func); +void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func); /**@}*/ #ifdef __cplusplus } -#endif +#endif #endif diff --git a/tmk_core/protocol/midi/midi_function_types.h b/tmk_core/protocol/midi/midi_function_types.h index 35c4601b2..761e88117 100755..100644 --- a/tmk_core/protocol/midi/midi_function_types.h +++ b/tmk_core/protocol/midi/midi_function_types.h @@ -1,20 +1,20 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. /** * @file @@ -26,25 +26,25 @@ #ifdef __cplusplus extern "C" { -#endif +#endif #include <inttypes.h> #include <stdbool.h> -//forward declaration +// forward declaration typedef struct _midi_device MidiDevice; -typedef void (* midi_one_byte_func_t)(MidiDevice * device, uint8_t byte); -typedef void (* midi_two_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1); -typedef void (* midi_three_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1, uint8_t byte2); -//all bytes after count bytes should be ignored -typedef void (* midi_var_byte_func_t)(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); +typedef void (*midi_one_byte_func_t)(MidiDevice *device, uint8_t byte); +typedef void (*midi_two_byte_func_t)(MidiDevice *device, uint8_t byte0, uint8_t byte1); +typedef void (*midi_three_byte_func_t)(MidiDevice *device, uint8_t byte0, uint8_t byte1, uint8_t byte2); +// all bytes after count bytes should be ignored +typedef void (*midi_var_byte_func_t)(MidiDevice *device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); -//the start byte tells you how far into the sysex message you are, the data_length tells you how many bytes data is -typedef void (* midi_sysex_func_t)(MidiDevice * device, uint16_t start_byte, uint8_t data_length, uint8_t *data); +// the start byte tells you how far into the sysex message you are, the data_length tells you how many bytes data is +typedef void (*midi_sysex_func_t)(MidiDevice *device, uint16_t start_byte, uint8_t data_length, uint8_t *data); #ifdef __cplusplus } -#endif +#endif #endif diff --git a/tmk_core/protocol/midi/qmk_midi.c b/tmk_core/protocol/midi/qmk_midi.c index d4de6caa7..6f6aced72 100644 --- a/tmk_core/protocol/midi/qmk_midi.c +++ b/tmk_core/protocol/midi/qmk_midi.c @@ -5,7 +5,7 @@ #include "usb_descriptor.h" #include "process_midi.h" #if API_SYSEX_ENABLE -#include "api.h" +# include "api.h" #endif /******************************************************************************* @@ -23,162 +23,154 @@ MidiDevice midi_device; #define SYS_COMMON_2 0x20 #define SYS_COMMON_3 0x30 -static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { - MIDI_EventPacket_t event; - event.Data1 = byte0; - event.Data2 = byte1; - event.Data3 = byte2; - - uint8_t cable = 0; - - //if the length is undefined we assume it is a SYSEX message - if (midi_packet_length(byte0) == UNDEFINED) { - switch(cnt) { - case 3: - if (byte2 == SYSEX_END) - event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3); - else - event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); - break; - case 2: - if (byte1 == SYSEX_END) - event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2); - else - event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); - break; - case 1: - if (byte0 == SYSEX_END) - event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1); - else - event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); - break; - default: - return; //invalid cnt +static void usb_send_func(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { + MIDI_EventPacket_t event; + event.Data1 = byte0; + event.Data2 = byte1; + event.Data3 = byte2; + + uint8_t cable = 0; + + // if the length is undefined we assume it is a SYSEX message + if (midi_packet_length(byte0) == UNDEFINED) { + switch (cnt) { + case 3: + if (byte2 == SYSEX_END) + event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3); + else + event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); + break; + case 2: + if (byte1 == SYSEX_END) + event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2); + else + event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); + break; + case 1: + if (byte0 == SYSEX_END) + event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1); + else + event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT); + break; + default: + return; // invalid cnt + } + } else { + // deal with 'system common' messages + // TODO are there any more? + switch (byte0 & 0xF0) { + case MIDI_SONGPOSITION: + event.Event = MIDI_EVENT(cable, SYS_COMMON_3); + break; + case MIDI_SONGSELECT: + case MIDI_TC_QUARTERFRAME: + event.Event = MIDI_EVENT(cable, SYS_COMMON_2); + break; + default: + event.Event = MIDI_EVENT(cable, byte0); + break; + } } - } else { - //deal with 'system common' messages - //TODO are there any more? - switch(byte0 & 0xF0){ - case MIDI_SONGPOSITION: - event.Event = MIDI_EVENT(cable, SYS_COMMON_3); - break; - case MIDI_SONGSELECT: - case MIDI_TC_QUARTERFRAME: - event.Event = MIDI_EVENT(cable, SYS_COMMON_2); - break; - default: - event.Event = MIDI_EVENT(cable, byte0); - break; - } - } - send_midi_packet(&event); + send_midi_packet(&event); } -static void usb_get_midi(MidiDevice * device) { - MIDI_EventPacket_t event; - while (recv_midi_packet(&event)) { - - midi_packet_length_t length = midi_packet_length(event.Data1); - uint8_t input[3]; - input[0] = event.Data1; - input[1] = event.Data2; - input[2] = event.Data3; - if (length == UNDEFINED) { - //sysex - if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) { - length = 3; - } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) { - length = 2; - } else if(event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) { - length = 1; - } else { - //XXX what to do? - } +static void usb_get_midi(MidiDevice* device) { + MIDI_EventPacket_t event; + while (recv_midi_packet(&event)) { + midi_packet_length_t length = midi_packet_length(event.Data1); + uint8_t input[3]; + input[0] = event.Data1; + input[1] = event.Data2; + input[2] = event.Data3; + if (length == UNDEFINED) { + // sysex + if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) { + length = 3; + } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) { + length = 2; + } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) { + length = 1; + } else { + // XXX what to do? + } + } + + // pass the data to the device input function + if (length != UNDEFINED) midi_device_input(device, length, input); } - - //pass the data to the device input function - if (length != UNDEFINED) - midi_device_input(device, length, input); - } } -static void fallthrough_callback(MidiDevice * device, - uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2){ - +static void fallthrough_callback(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { #ifdef AUDIO_ENABLE - if (cnt == 3) { - switch (byte0 & 0xF0) { - case MIDI_NOTEON: - play_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0), (byte2 & 0x7F) / 8); - break; - case MIDI_NOTEOFF: - stop_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0)); - break; + if (cnt == 3) { + switch (byte0 & 0xF0) { + case MIDI_NOTEON: + play_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0), (byte2 & 0x7F) / 8); + break; + case MIDI_NOTEOFF: + stop_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0)); + break; + } + } + if (byte0 == MIDI_STOP) { + stop_all_notes(); } - } - if (byte0 == MIDI_STOP) { - stop_all_notes(); - } #endif } - -static void cc_callback(MidiDevice * device, - uint8_t chan, uint8_t num, uint8_t val) { - //sending it back on the next channel - // midi_send_cc(device, (chan + 1) % 16, num, val); +static void cc_callback(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) { + // sending it back on the next channel + // midi_send_cc(device, (chan + 1) % 16, num, val); } #ifdef API_SYSEX_ENABLE uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0}; -static void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) { - // SEND_STRING("\n"); - // send_word(start); - // SEND_STRING(": "); - // Don't store the header - int16_t pos = start - 4; - for (uint8_t place = 0; place < length; place++) { - // send_byte(*data); - if (pos >= 0) { - if (*data == 0xF7) { - // SEND_STRING("\nRD: "); - // for (uint8_t i = 0; i < start + place + 1; i++){ - // send_byte(midi_buffer[i]); - // SEND_STRING(" "); - // } - const unsigned decoded_length = sysex_decoded_length(pos); - uint8_t decoded[API_SYSEX_MAX_SIZE]; - sysex_decode(decoded, midi_buffer, pos); - process_api(decoded_length, decoded); - return; - } - else if (pos >= MIDI_SYSEX_BUFFER) { - return; - } - midi_buffer[pos] = *data; - } - // SEND_STRING(" "); - data++; - pos++; - } +static void sysex_callback(MidiDevice* device, uint16_t start, uint8_t length, uint8_t* data) { + // SEND_STRING("\n"); + // send_word(start); + // SEND_STRING(": "); + // Don't store the header + int16_t pos = start - 4; + for (uint8_t place = 0; place < length; place++) { + // send_byte(*data); + if (pos >= 0) { + if (*data == 0xF7) { + // SEND_STRING("\nRD: "); + // for (uint8_t i = 0; i < start + place + 1; i++){ + // send_byte(midi_buffer[i]); + // SEND_STRING(" "); + // } + const unsigned decoded_length = sysex_decoded_length(pos); + uint8_t decoded[API_SYSEX_MAX_SIZE]; + sysex_decode(decoded, midi_buffer, pos); + process_api(decoded_length, decoded); + return; + } else if (pos >= MIDI_SYSEX_BUFFER) { + return; + } + midi_buffer[pos] = *data; + } + // SEND_STRING(" "); + data++; + pos++; + } } #endif void midi_init(void); -void setup_midi(void) -{ +void setup_midi(void) { #ifdef MIDI_ADVANCED - midi_init(); + midi_init(); #endif - midi_device_init(&midi_device); - midi_device_set_send_func(&midi_device, usb_send_func); - midi_device_set_pre_input_process_func(&midi_device, usb_get_midi); - midi_register_fallthrough_callback(&midi_device, fallthrough_callback); - midi_register_cc_callback(&midi_device, cc_callback); + midi_device_init(&midi_device); + midi_device_set_send_func(&midi_device, usb_send_func); + midi_device_set_pre_input_process_func(&midi_device, usb_get_midi); + midi_register_fallthrough_callback(&midi_device, fallthrough_callback); + midi_register_cc_callback(&midi_device, cc_callback); #ifdef API_SYSEX_ENABLE - midi_register_sysex_callback(&midi_device, sysex_callback); + midi_register_sysex_callback(&midi_device, sysex_callback); #endif } diff --git a/tmk_core/protocol/midi/qmk_midi.h b/tmk_core/protocol/midi/qmk_midi.h index 7282a19d4..0a348d5fd 100644 --- a/tmk_core/protocol/midi/qmk_midi.h +++ b/tmk_core/protocol/midi/qmk_midi.h @@ -1,9 +1,9 @@ #pragma once #ifdef MIDI_ENABLE - #include "midi.h" - extern MidiDevice midi_device; - void setup_midi(void); - void send_midi_packet(MIDI_EventPacket_t* event); - bool recv_midi_packet(MIDI_EventPacket_t* const event); +# include "midi.h" +extern MidiDevice midi_device; +void setup_midi(void); +void send_midi_packet(MIDI_EventPacket_t* event); +bool recv_midi_packet(MIDI_EventPacket_t* const event); #endif diff --git a/tmk_core/protocol/midi/sysex_tools.c b/tmk_core/protocol/midi/sysex_tools.c index 7563a3e2a..84bca7215 100755..100644 --- a/tmk_core/protocol/midi/sysex_tools.c +++ b/tmk_core/protocol/midi/sysex_tools.c @@ -1,99 +1,97 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. #include "sysex_tools.h" -uint16_t sysex_encoded_length(uint16_t decoded_length){ - uint8_t remainder = decoded_length % 7; - if (remainder) - return (decoded_length / 7) * 8 + remainder + 1; - else - return (decoded_length / 7) * 8; +uint16_t sysex_encoded_length(uint16_t decoded_length) { + uint8_t remainder = decoded_length % 7; + if (remainder) + return (decoded_length / 7) * 8 + remainder + 1; + else + return (decoded_length / 7) * 8; } -uint16_t sysex_decoded_length(uint16_t encoded_length){ - uint8_t remainder = encoded_length % 8; - if (remainder) - return (encoded_length / 8) * 7 + remainder - 1; - else - return (encoded_length / 8) * 7; +uint16_t sysex_decoded_length(uint16_t encoded_length) { + uint8_t remainder = encoded_length % 8; + if (remainder) + return (encoded_length / 8) * 7 + remainder - 1; + else + return (encoded_length / 8) * 7; } -uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length){ - uint16_t encoded_full = length / 7; //number of full 8 byte sections from 7 bytes of input - uint16_t i,j; +uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length) { + uint16_t encoded_full = length / 7; // number of full 8 byte sections from 7 bytes of input + uint16_t i, j; - //fill out the fully encoded sections - for(i = 0; i < encoded_full; i++) { - uint16_t encoded_msb_idx = i * 8; - uint16_t input_start_idx = i * 7; - encoded[encoded_msb_idx] = 0; - for(j = 0; j < 7; j++){ - uint8_t current = source[input_start_idx + j]; - encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); - encoded[encoded_msb_idx + 1 + j] = 0x7F & current; - } - } + // fill out the fully encoded sections + for (i = 0; i < encoded_full; i++) { + uint16_t encoded_msb_idx = i * 8; + uint16_t input_start_idx = i * 7; + encoded[encoded_msb_idx] = 0; + for (j = 0; j < 7; j++) { + uint8_t current = source[input_start_idx + j]; + encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); + encoded[encoded_msb_idx + 1 + j] = 0x7F & current; + } + } - //fill out the rest if there is any more - uint8_t remainder = length % 7; - if (remainder) { - uint16_t encoded_msb_idx = encoded_full * 8; - uint16_t input_start_idx = encoded_full * 7; - encoded[encoded_msb_idx] = 0; - for(j = 0; j < remainder; j++){ - uint8_t current = source[input_start_idx + j]; - encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); - encoded[encoded_msb_idx + 1 + j] = 0x7F & current; - } - return encoded_msb_idx + remainder + 1; - } else { - return encoded_full * 8; - } + // fill out the rest if there is any more + uint8_t remainder = length % 7; + if (remainder) { + uint16_t encoded_msb_idx = encoded_full * 8; + uint16_t input_start_idx = encoded_full * 7; + encoded[encoded_msb_idx] = 0; + for (j = 0; j < remainder; j++) { + uint8_t current = source[input_start_idx + j]; + encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); + encoded[encoded_msb_idx + 1 + j] = 0x7F & current; + } + return encoded_msb_idx + remainder + 1; + } else { + return encoded_full * 8; + } } -uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length){ - uint16_t decoded_full = length / 8; - uint16_t i,j; +uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length) { + uint16_t decoded_full = length / 8; + uint16_t i, j; - if (length < 2) - return 0; + if (length < 2) return 0; - //fill out the fully encoded sections - for(i = 0; i < decoded_full; i++) { - uint16_t encoded_msb_idx = i * 8; - uint16_t output_start_index = i * 7; - for(j = 0; j < 7; j++){ - decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; - decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); - } - } - uint8_t remainder = length % 8; - if (remainder) { - uint16_t encoded_msb_idx = decoded_full * 8; - uint16_t output_start_index = decoded_full * 7; - for(j = 0; j < (remainder - 1); j++) { - decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; - decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); - } - return decoded_full * 7 + remainder - 1; - } else { - return decoded_full * 7; - } + // fill out the fully encoded sections + for (i = 0; i < decoded_full; i++) { + uint16_t encoded_msb_idx = i * 8; + uint16_t output_start_index = i * 7; + for (j = 0; j < 7; j++) { + decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; + decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); + } + } + uint8_t remainder = length % 8; + if (remainder) { + uint16_t encoded_msb_idx = decoded_full * 8; + uint16_t output_start_index = decoded_full * 7; + for (j = 0; j < (remainder - 1); j++) { + decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; + decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); + } + return decoded_full * 7 + remainder - 1; + } else { + return decoded_full * 7; + } } - diff --git a/tmk_core/protocol/midi/sysex_tools.h b/tmk_core/protocol/midi/sysex_tools.h index 3654d0114..454a92ea5 100755..100644 --- a/tmk_core/protocol/midi/sysex_tools.h +++ b/tmk_core/protocol/midi/sysex_tools.h @@ -1,27 +1,27 @@ -//midi for embedded chips, -//Copyright 2010 Alex Norman +// midi for embedded chips, +// Copyright 2010 Alex Norman // -//This file is part of avr-midi. +// This file is part of avr-midi. // -//avr-midi 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 +// avr-midi 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. // -//avr-midi 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. +// avr-midi 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 avr-midi. If not, see <http://www.gnu.org/licenses/>. +// You should have received a copy of the GNU General Public License +// along with avr-midi. If not, see <http://www.gnu.org/licenses/>. #ifndef SYSEX_TOOLS_H #define SYSEX_TOOLS_H #ifdef __cplusplus extern "C" { -#endif +#endif #include <inttypes.h> @@ -31,7 +31,7 @@ extern "C" { * * These functions are for converting data to and from a "midi-safe" format, * which can be use to send data with sysex messages. Sysex messages may only - * contain data where the to bit is not set. + * contain data where the to bit is not set. * * An "encoded" midi message is one that contains all of the data from its * original state, but does not have any of the top bits set. @@ -70,7 +70,7 @@ uint16_t sysex_decoded_length(uint16_t encoded_length); * @param encoded The output data buffer, must be at least sysex_encoded_length(length) bytes long. * @param source The input buffer of data to be encoded. * @param length The number of bytes from the input buffer to encode. - * + * * @return number of bytes encoded. */ uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length); @@ -81,7 +81,7 @@ uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length); * @param decoded The output data buffer, must be at least sysex_decoded_length(length) bytes long. * @param source The input buffer of data to be decoded. * @param length The number of bytes from the input buffer to decode. - * + * * @return number of bytes decoded. */ uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length); @@ -90,6 +90,6 @@ uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length); #ifdef __cplusplus } -#endif +#endif #endif diff --git a/tmk_core/protocol/news.c b/tmk_core/protocol/news.c index 00755a5e2..4463e8dd4 100644 --- a/tmk_core/protocol/news.c +++ b/tmk_core/protocol/news.c @@ -40,41 +40,34 @@ POSSIBILITY OF SUCH DAMAGE. #include <avr/interrupt.h> #include "news.h" - -void news_init(void) -{ - NEWS_KBD_RX_INIT(); -} +void news_init(void) { NEWS_KBD_RX_INIT(); } // RX ring buffer -#define RBUF_SIZE 8 +#define RBUF_SIZE 8 static uint8_t rbuf[RBUF_SIZE]; static uint8_t rbuf_head = 0; static uint8_t rbuf_tail = 0; -uint8_t news_recv(void) -{ +uint8_t news_recv(void) { uint8_t data = 0; if (rbuf_head == rbuf_tail) { return 0; } - data = rbuf[rbuf_tail]; + data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; return data; } // USART RX complete interrupt -ISR(NEWS_KBD_RX_VECT) -{ +ISR(NEWS_KBD_RX_VECT) { uint8_t next = (rbuf_head + 1) % RBUF_SIZE; if (next != rbuf_tail) { rbuf[rbuf_head] = NEWS_KBD_RX_DATA; - rbuf_head = next; + rbuf_head = next; } } - /* SONY NEWS Keyboard Protocol =========================== diff --git a/tmk_core/protocol/news.h b/tmk_core/protocol/news.h index 35e09c4d2..d0c4bc6a4 100644 --- a/tmk_core/protocol/news.h +++ b/tmk_core/protocol/news.h @@ -41,9 +41,8 @@ POSSIBILITY OF SUCH DAMAGE. * Primitive PS/2 Library for AVR */ - /* host role */ -void news_init(void); +void news_init(void); uint8_t news_recv(void); /* device role */ diff --git a/tmk_core/protocol/next_kbd.c b/tmk_core/protocol/next_kbd.c index fa3034b3f..6f118e617 100644 --- a/tmk_core/protocol/next_kbd.c +++ b/tmk_core/protocol/next_kbd.c @@ -1,7 +1,7 @@ /* NeXT non-ADB Keyboard Protocol - + Copyright 2013, Benjamin Gould (bgould@github.com) Based on: @@ -53,10 +53,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "next_kbd.h" #include "debug.h" -static inline void out_lo(void); -static inline void out_hi(void); -static inline void query(void); -static inline void reset(void); +static inline void out_lo(void); +static inline void out_hi(void); +static inline void query(void); +static inline void reset(void); static inline uint32_t response(void); /* The keyboard sends signal with 50us pulse width on OUT line @@ -65,53 +65,65 @@ static inline uint32_t response(void); * but it works well with 51us(+1us) on TMK converter(ATMeaga32u2) at least. * TODO: test on Teensy and Pro Micro configuration */ -#define out_hi_delay(intervals) do { out_hi(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); -#define out_lo_delay(intervals) do { out_lo(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); -#define query_delay(intervals) do { query(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); -#define reset_delay(intervals) do { reset(); _delay_us((NEXT_KBD_TIMING+1) * intervals); } while (0); - -void next_kbd_init(void) -{ +#define out_hi_delay(intervals) \ + do { \ + out_hi(); \ + _delay_us((NEXT_KBD_TIMING + 1) * intervals); \ + } while (0); +#define out_lo_delay(intervals) \ + do { \ + out_lo(); \ + _delay_us((NEXT_KBD_TIMING + 1) * intervals); \ + } while (0); +#define query_delay(intervals) \ + do { \ + query(); \ + _delay_us((NEXT_KBD_TIMING + 1) * intervals); \ + } while (0); +#define reset_delay(intervals) \ + do { \ + reset(); \ + _delay_us((NEXT_KBD_TIMING + 1) * intervals); \ + } while (0); + +void next_kbd_init(void) { out_hi(); - NEXT_KBD_IN_DDR &= ~(1<<NEXT_KBD_IN_BIT); // KBD_IN to input - NEXT_KBD_IN_PORT |= (1<<NEXT_KBD_IN_BIT); // KBD_IN pull up - + NEXT_KBD_IN_DDR &= ~(1 << NEXT_KBD_IN_BIT); // KBD_IN to input + NEXT_KBD_IN_PORT |= (1 << NEXT_KBD_IN_BIT); // KBD_IN pull up + query_delay(5); reset_delay(8); - + query_delay(5); reset_delay(8); } -void next_kbd_set_leds(bool left, bool right) -{ +void next_kbd_set_leds(bool left, bool right) { cli(); out_lo_delay(9); - + out_hi_delay(3); out_lo_delay(1); - + if (left) { out_hi_delay(1); } else { out_lo_delay(1); } - + if (right) { out_hi_delay(1); } else { out_lo_delay(1); } - + out_lo_delay(7); out_hi(); sei(); } -#define NEXT_KBD_READ (NEXT_KBD_IN_PIN&(1<<NEXT_KBD_IN_BIT)) -uint32_t next_kbd_recv(void) -{ - +#define NEXT_KBD_READ (NEXT_KBD_IN_PIN & (1 << NEXT_KBD_IN_BIT)) +uint32_t next_kbd_recv(void) { // First check to make sure that the keyboard is actually connected; // if not, just return // TODO: reflect the status of the keyboard in a return code @@ -122,22 +134,23 @@ uint32_t next_kbd_recv(void) query(); uint32_t resp = response(); - + return resp; } -static inline uint32_t response(void) -{ +static inline uint32_t response(void) { cli(); - + // try a 5ms read; this should be called after the query method has // been run so if a key is pressed we should get a response within // 5ms; if not then send a reset and exit - uint8_t i = 0; - uint32_t data = 0; + uint8_t i = 0; + uint32_t data = 0; uint16_t reset_timeout = 50000; - while (NEXT_KBD_READ && reset_timeout) { - asm(""); _delay_us(1); reset_timeout--; + while (NEXT_KBD_READ && reset_timeout) { + asm(""); + _delay_us(1); + reset_timeout--; } if (!reset_timeout) { reset(); @@ -145,64 +158,58 @@ static inline uint32_t response(void) return 0; } _delay_us(NEXT_KBD_TIMING / 2); - for (; i < 22; i++) - { - if (NEXT_KBD_READ) - { - data |= ((uint32_t) 1 << i); + for (; i < 22; i++) { + if (NEXT_KBD_READ) { + data |= ((uint32_t)1 << i); /* Note: * My testing with the ATmega32u4 showed that there might * something wrong with the timing here; by the end of the * second data byte some of the modifiers can get bumped out * to the next bit over if we just cycle through the data - * based on the expected interval. There is a bit (i = 10) - * in the middle of the data that is always on followed by - * one that is always off - so we'll use that to reset our + * based on the expected interval. There is a bit (i = 10) + * in the middle of the data that is always on followed by + * one that is always off - so we'll use that to reset our * timing in case we've gotten ahead of the keyboard; */ - if (i == 10) - { + if (i == 10) { i++; - while (NEXT_KBD_READ) ; + while (NEXT_KBD_READ) + ; _delay_us(NEXT_KBD_TIMING / 2); } } else { /* redundant - but I don't want to remove if it might screw * up the timing */ - data |= ((uint32_t) 0 << i); + data |= ((uint32_t)0 << i); } _delay_us(NEXT_KBD_TIMING); } - + sei(); - + return data; } -static inline void out_lo(void) -{ - NEXT_KBD_OUT_PORT &= ~(1<<NEXT_KBD_OUT_BIT); - NEXT_KBD_OUT_DDR |= (1<<NEXT_KBD_OUT_BIT); +static inline void out_lo(void) { + NEXT_KBD_OUT_PORT &= ~(1 << NEXT_KBD_OUT_BIT); + NEXT_KBD_OUT_DDR |= (1 << NEXT_KBD_OUT_BIT); } -static inline void out_hi(void) -{ +static inline void out_hi(void) { /* input with pull up */ - NEXT_KBD_OUT_DDR &= ~(1<<NEXT_KBD_OUT_BIT); - NEXT_KBD_OUT_PORT |= (1<<NEXT_KBD_OUT_BIT); + NEXT_KBD_OUT_DDR &= ~(1 << NEXT_KBD_OUT_BIT); + NEXT_KBD_OUT_PORT |= (1 << NEXT_KBD_OUT_BIT); } -static inline void query(void) -{ +static inline void query(void) { out_lo_delay(5); out_hi_delay(1); out_lo_delay(3); out_hi(); } -static inline void reset(void) -{ +static inline void reset(void) { out_lo_delay(1); out_hi_delay(4); out_lo_delay(1); diff --git a/tmk_core/protocol/next_kbd.h b/tmk_core/protocol/next_kbd.h index 6d455d4fa..bff7a474d 100644 --- a/tmk_core/protocol/next_kbd.h +++ b/tmk_core/protocol/next_kbd.h @@ -1,6 +1,6 @@ /* NeXT non-ADB Keyboard Protocol - + Copyright 2013, Benjamin Gould (bgould@github.com) Based on: @@ -48,16 +48,16 @@ POSSIBILITY OF SUCH DAMAGE. #include <stdbool.h> #ifndef NEXT_KBD_H -#define NEXT_KBD_H +# define NEXT_KBD_H -#define NEXT_KBD_KMBUS_IDLE 0x300600 -#define NEXT_KBD_TIMING 50 +# define NEXT_KBD_KMBUS_IDLE 0x300600 +# define NEXT_KBD_TIMING 50 extern uint8_t next_kbd_error; /* host role */ -void next_kbd_init(void); -void next_kbd_set_leds(bool left, bool right); +void next_kbd_init(void); +void next_kbd_set_leds(bool left, bool right); uint32_t next_kbd_recv(void); #endif diff --git a/tmk_core/protocol/pjrc/main.c b/tmk_core/protocol/pjrc/main.c index 45eb17d4c..d16051c5f 100644 --- a/tmk_core/protocol/pjrc/main.c +++ b/tmk_core/protocol/pjrc/main.c @@ -1,17 +1,17 @@ /* Keyboard example with debug channel, for Teensy USB Development Board * http://www.pjrc.com/teensy/usb_keyboard.html * Copyright (c) 2008 PJRC.COM, LLC - * + * * 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 @@ -37,12 +37,9 @@ #include "host.h" #include "pjrc.h" +#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) -#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) - - -int main(void) -{ +int main(void) { // set for 16 MHz clock CPU_PRESCALE(0); @@ -52,7 +49,8 @@ int main(void) // If the Teensy is powered without a PC connected to the USB port, // this will wait forever. usb_init(); - while (!usb_configured()) /* wait */ ; + while (!usb_configured()) /* wait */ + ; print_set_sendchar(sendchar); @@ -69,6 +67,6 @@ int main(void) } } - keyboard_task(); + keyboard_task(); } } diff --git a/tmk_core/protocol/pjrc/pjrc.c b/tmk_core/protocol/pjrc/pjrc.c index 0562a12ff..60932f59e 100644 --- a/tmk_core/protocol/pjrc/pjrc.c +++ b/tmk_core/protocol/pjrc/pjrc.c @@ -22,54 +22,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "host_driver.h" #include "pjrc.h" - /*------------------------------------------------------------------* * Host driver *------------------------------------------------------------------*/ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); -static host_driver_t driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; +static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -host_driver_t *pjrc_driver(void) -{ - return &driver; -} +host_driver_t *pjrc_driver(void) { return &driver; } -static uint8_t keyboard_leds(void) { - return usb_keyboard_leds; -} +static uint8_t keyboard_leds(void) { return usb_keyboard_leds; } -static void send_keyboard(report_keyboard_t *report) -{ - usb_keyboard_send_report(report); -} +static void send_keyboard(report_keyboard_t *report) { usb_keyboard_send_report(report); } -static void send_mouse(report_mouse_t *report) -{ +static void send_mouse(report_mouse_t *report) { #ifdef MOUSE_ENABLE usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons); #endif } -static void send_system(uint16_t data) -{ +static void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE usb_extra_system_send(data); #endif } -static void send_consumer(uint16_t data) -{ +static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE usb_extra_consumer_send(data); #endif diff --git a/tmk_core/protocol/pjrc/pjrc.h b/tmk_core/protocol/pjrc/pjrc.h index 06e79626f..08a1ede18 100644 --- a/tmk_core/protocol/pjrc/pjrc.h +++ b/tmk_core/protocol/pjrc/pjrc.h @@ -20,7 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "host_driver.h" - host_driver_t *pjrc_driver(void); #endif diff --git a/tmk_core/protocol/pjrc/usb.c b/tmk_core/protocol/pjrc/usb.c index 09efbe076..22fbcfdcd 100644 --- a/tmk_core/protocol/pjrc/usb.c +++ b/tmk_core/protocol/pjrc/usb.c @@ -35,19 +35,18 @@ #include "print.h" #include "util.h" #ifdef SLEEP_LED_ENABLE -#include "sleep_led.h" +# include "sleep_led.h" #endif #include "suspend.h" #include "action.h" #include "action_util.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" +# include "keycode_config.h" - extern keymap_config_t keymap_config; +extern keymap_config_t keymap_config; #endif - /************************************************************************** * * Configurable Options @@ -56,34 +55,32 @@ // You can change these to give your code its own name. #ifndef MANUFACTURER -# define STR_MANUFACTURER L"t.m.k." +# define STR_MANUFACTURER L"t.m.k." #else -# define STR_MANUFACTURER LSTR(MANUFACTURER) +# define STR_MANUFACTURER LSTR(MANUFACTURER) #endif #ifndef PRODUCT -# define STR_PRODUCT L"t.m.k. keyboard" +# define STR_PRODUCT L"t.m.k. keyboard" #else -# define STR_PRODUCT LSTR(PRODUCT) +# define STR_PRODUCT LSTR(PRODUCT) #endif - // Mac OS-X and Linux automatically load the correct drivers. On // Windows, even though the driver is supplied by Microsoft, an // INF file is needed to load the driver. These numbers need to // match the INF file. #ifndef VENDOR_ID -# define VENDOR_ID 0xFEED +# define VENDOR_ID 0xFEED #endif #ifndef PRODUCT_ID -# define PRODUCT_ID 0xBABE +# define PRODUCT_ID 0xBABE #endif #ifndef DEVICE_VER -# define DEVICE_VER 0x0100 +# define DEVICE_VER 0x0100 #endif - // USB devices are supposed to implment a halt feature, which is // rarely (if ever) used. If you comment this line out, the halt // code will be removed, saving 102 bytes of space (gcc 4.3.0). @@ -91,47 +88,44 @@ // operating systems. #define SUPPORT_ENDPOINT_HALT - - /************************************************************************** * * Endpoint Buffer Configuration * **************************************************************************/ -#define ENDPOINT0_SIZE 32 +#define ENDPOINT0_SIZE 32 bool remote_wakeup = false; -bool suspend = false; +bool suspend = false; // 0:control endpoint is enabled automatically by controller. static const uint8_t PROGMEM endpoint_config_table[] = { - // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation) - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD_SIZE) | KBD_BUFFER, // 1 + // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation) + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD_SIZE) | KBD_BUFFER, // 1 #ifdef MOUSE_ENABLE - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2 #else - 0, // 2 + 0, // 2 #endif #ifdef CONSOLE_ENABLE - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3 #else - 0, + 0, #endif #ifdef EXTRAKEY_ENABLE - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4 #else - 0, // 4 + 0, // 4 #endif #ifdef NKRO_ENABLE - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD2_SIZE) | KBD2_BUFFER, // 5 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD2_SIZE) | KBD2_BUFFER, // 5 #else - 0, // 5 + 0, // 5 #endif - 0, // 6 + 0, // 6 }; - /************************************************************************** * * Descriptor Data @@ -144,93 +138,96 @@ static const uint8_t PROGMEM endpoint_config_table[] = { // in here should only be done by those who've read chapter 9 of the USB // spec and relevant portions of any USB class specifications! - static const uint8_t PROGMEM device_descriptor[] = { - 18, // bLength - 1, // bDescriptorType - 0x00, 0x02, // bcdUSB - 0, // bDeviceClass - 0, // bDeviceSubClass - 0, // bDeviceProtocol - ENDPOINT0_SIZE, // bMaxPacketSize0 - LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor - LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct - LSB(DEVICE_VER), MSB(DEVICE_VER), // bcdDevice - 1, // iManufacturer - 2, // iProduct - 0, // iSerialNumber - 1 // bNumConfigurations + 18, // bLength + 1, // bDescriptorType + 0x00, + 0x02, // bcdUSB + 0, // bDeviceClass + 0, // bDeviceSubClass + 0, // bDeviceProtocol + ENDPOINT0_SIZE, // bMaxPacketSize0 + LSB(VENDOR_ID), + MSB(VENDOR_ID), // idVendor + LSB(PRODUCT_ID), + MSB(PRODUCT_ID), // idProduct + LSB(DEVICE_VER), + MSB(DEVICE_VER), // bcdDevice + 1, // iManufacturer + 2, // iProduct + 0, // iSerialNumber + 1 // bNumConfigurations }; // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60 static const uint8_t PROGMEM keyboard_hid_report_desc[] = { - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x06, // Usage (Keyboard), - 0xA1, 0x01, // Collection (Application), - 0x75, 0x01, // Report Size (1), - 0x95, 0x08, // Report Count (8), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0xE0, // Usage Minimum (224), - 0x29, 0xE7, // Usage Maximum (231), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte - 0x95, 0x01, // Report Count (1), - 0x75, 0x08, // Report Size (8), - 0x81, 0x03, // Input (Constant), ;Reserved byte - 0x95, 0x05, // Report Count (5), - 0x75, 0x01, // Report Size (1), - 0x05, 0x08, // Usage Page (LEDs), - 0x19, 0x01, // Usage Minimum (1), - 0x29, 0x05, // Usage Maximum (5), - 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report - 0x95, 0x01, // Report Count (1), - 0x75, 0x03, // Report Size (3), - 0x91, 0x03, // Output (Constant), ;LED report padding - 0x95, KBD_REPORT_KEYS, // Report Count (), - 0x75, 0x08, // Report Size (8), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0xFF, // Logical Maximum(255), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0x00, // Usage Minimum (0), - 0x29, 0xFF, // Usage Maximum (255), - 0x81, 0x00, // Input (Data, Array), - 0xc0 // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x06, // Usage (Keyboard), + 0xA1, 0x01, // Collection (Application), + 0x75, 0x01, // Report Size (1), + 0x95, 0x08, // Report Count (8), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0xE0, // Usage Minimum (224), + 0x29, 0xE7, // Usage Maximum (231), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte + 0x95, 0x01, // Report Count (1), + 0x75, 0x08, // Report Size (8), + 0x81, 0x03, // Input (Constant), ;Reserved byte + 0x95, 0x05, // Report Count (5), + 0x75, 0x01, // Report Size (1), + 0x05, 0x08, // Usage Page (LEDs), + 0x19, 0x01, // Usage Minimum (1), + 0x29, 0x05, // Usage Maximum (5), + 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report + 0x95, 0x01, // Report Count (1), + 0x75, 0x03, // Report Size (3), + 0x91, 0x03, // Output (Constant), ;LED report padding + 0x95, KBD_REPORT_KEYS, // Report Count (), + 0x75, 0x08, // Report Size (8), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0xFF, // Logical Maximum(255), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0x00, // Usage Minimum (0), + 0x29, 0xFF, // Usage Maximum (255), + 0x81, 0x00, // Input (Data, Array), + 0xc0 // End Collection }; #ifdef NKRO_ENABLE static const uint8_t PROGMEM keyboard2_hid_report_desc[] = { - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x06, // Usage (Keyboard), - 0xA1, 0x01, // Collection (Application), - // bitmap of modifiers - 0x75, 0x01, // Report Size (1), - 0x95, 0x08, // Report Count (8), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0xE0, // Usage Minimum (224), - 0x29, 0xE7, // Usage Maximum (231), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte - // LED output report - 0x95, 0x05, // Report Count (5), - 0x75, 0x01, // Report Size (1), - 0x05, 0x08, // Usage Page (LEDs), - 0x19, 0x01, // Usage Minimum (1), - 0x29, 0x05, // Usage Maximum (5), - 0x91, 0x02, // Output (Data, Variable, Absolute), - 0x95, 0x01, // Report Count (1), - 0x75, 0x03, // Report Size (3), - 0x91, 0x03, // Output (Constant), - // bitmap of keys - 0x95, KBD2_REPORT_KEYS*8, // Report Count (), - 0x75, 0x01, // Report Size (1), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum(1), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0x00, // Usage Minimum (0), - 0x29, KBD2_REPORT_KEYS*8-1, // Usage Maximum (), - 0x81, 0x02, // Input (Data, Variable, Absolute), - 0xc0 // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x06, // Usage (Keyboard), + 0xA1, 0x01, // Collection (Application), + // bitmap of modifiers + 0x75, 0x01, // Report Size (1), + 0x95, 0x08, // Report Count (8), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0xE0, // Usage Minimum (224), + 0x29, 0xE7, // Usage Maximum (231), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte + // LED output report + 0x95, 0x05, // Report Count (5), + 0x75, 0x01, // Report Size (1), + 0x05, 0x08, // Usage Page (LEDs), + 0x19, 0x01, // Usage Minimum (1), + 0x29, 0x05, // Usage Maximum (5), + 0x91, 0x02, // Output (Data, Variable, Absolute), + 0x95, 0x01, // Report Count (1), + 0x75, 0x03, // Report Size (3), + 0x91, 0x03, // Output (Constant), + // bitmap of keys + 0x95, KBD2_REPORT_KEYS * 8, // Report Count (), + 0x75, 0x01, // Report Size (1), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum(1), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0x00, // Usage Minimum (0), + 0x29, KBD2_REPORT_KEYS * 8 - 1, // Usage Maximum (), + 0x81, 0x02, // Input (Data, Variable, Absolute), + 0xc0 // End Collection }; #endif @@ -241,66 +238,66 @@ static const uint8_t PROGMEM keyboard2_hid_report_desc[] = { // http://www.microsoft.com/whdc/device/input/wheel.mspx static const uint8_t PROGMEM mouse_hid_report_desc[] = { /* mouse */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - //0x85, REPORT_ID_MOUSE, // REPORT_ID (1) - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - // ---------------------------- Buttons - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x05, // USAGE_MAXIMUM (Button 5) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x05, // REPORT_COUNT (5) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x75, 0x03, // REPORT_SIZE (3) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x03, // INPUT (Cnst,Var,Abs) - // ---------------------------- X,Y position - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x02, // REPORT_COUNT (2) - 0x81, 0x06, // INPUT (Data,Var,Rel) - // ---------------------------- Vertical wheel - 0x09, 0x38, // USAGE (Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical - 0x45, 0x00, // PHYSICAL_MAXIMUM (0) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - // ---------------------------- Horizontal wheel - 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) - 0x0a, 0x38, 0x02, // USAGE (AC Pan) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xc0, // END_COLLECTION - 0xc0, // END_COLLECTION + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xa1, 0x01, // COLLECTION (Application) + // 0x85, REPORT_ID_MOUSE, // REPORT_ID (1) + 0x09, 0x01, // USAGE (Pointer) + 0xa1, 0x00, // COLLECTION (Physical) + // ---------------------------- Buttons + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x05, // USAGE_MAXIMUM (Button 5) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x75, 0x01, // REPORT_SIZE (1) + 0x95, 0x05, // REPORT_COUNT (5) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x75, 0x03, // REPORT_SIZE (3) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x03, // INPUT (Cnst,Var,Abs) + // ---------------------------- X,Y position + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x02, // REPORT_COUNT (2) + 0x81, 0x06, // INPUT (Data,Var,Rel) + // ---------------------------- Vertical wheel + 0x09, 0x38, // USAGE (Wheel) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical + 0x45, 0x00, // PHYSICAL_MAXIMUM (0) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + // ---------------------------- Horizontal wheel + 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) + 0x0a, 0x38, 0x02, // USAGE (AC Pan) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + 0xc0, // END_COLLECTION + 0xc0, // END_COLLECTION }; #endif static const uint8_t PROGMEM debug_hid_report_desc[] = { - 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined) - 0x09, 0x74, // Usage 0x74 - 0xA1, 0x53, // Collection 0x53 - 0x75, 0x08, // report size = 8 bits - 0x15, 0x00, // logical minimum = 0 - 0x26, 0xFF, 0x00, // logical maximum = 255 - 0x95, DEBUG_TX_SIZE, // report count - 0x09, 0x75, // usage - 0x81, 0x02, // Input (array) - 0xC0 // end collection + 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined) + 0x09, 0x74, // Usage 0x74 + 0xA1, 0x53, // Collection 0x53 + 0x75, 0x08, // report size = 8 bits + 0x15, 0x00, // logical minimum = 0 + 0x26, 0xFF, 0x00, // logical maximum = 255 + 0x95, DEBUG_TX_SIZE, // report count + 0x09, 0x75, // usage + 0x81, 0x02, // Input (array) + 0xC0 // end collection }; #ifdef EXTRAKEY_ENABLE @@ -308,225 +305,225 @@ static const uint8_t PROGMEM debug_hid_report_desc[] = { // http://www.microsoft.com/whdc/archive/w2kbd.mspx static const uint8_t PROGMEM extra_hid_report_desc[] = { /* system control */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x80, // USAGE (System Control) - 0xa1, 0x01, // COLLECTION (Application) - 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2) - 0x15, 0x01, // LOGICAL_MINIMUM (0x1) - 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7) - 0x19, 0x01, // USAGE_MINIMUM (0x1) - 0x29, 0xb7, // USAGE_MAXIMUM (0xb7) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x00, // INPUT (Data,Array,Abs) - 0xc0, // END_COLLECTION + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x80, // USAGE (System Control) + 0xa1, 0x01, // COLLECTION (Application) + 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2) + 0x15, 0x01, // LOGICAL_MINIMUM (0x1) + 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7) + 0x19, 0x01, // USAGE_MINIMUM (0x1) + 0x29, 0xb7, // USAGE_MAXIMUM (0xb7) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x00, // INPUT (Data,Array,Abs) + 0xc0, // END_COLLECTION /* consumer */ - 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) - 0x09, 0x01, // USAGE (Consumer Control) - 0xa1, 0x01, // COLLECTION (Application) - 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3) - 0x15, 0x01, // LOGICAL_MINIMUM (0x1) - 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c) - 0x19, 0x01, // USAGE_MINIMUM (0x1) - 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x00, // INPUT (Data,Array,Abs) - 0xc0, // END_COLLECTION + 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) + 0x09, 0x01, // USAGE (Consumer Control) + 0xa1, 0x01, // COLLECTION (Application) + 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3) + 0x15, 0x01, // LOGICAL_MINIMUM (0x1) + 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c) + 0x19, 0x01, // USAGE_MINIMUM (0x1) + 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x00, // INPUT (Data,Array,Abs) + 0xc0, // END_COLLECTION }; #endif -#define KBD_HID_DESC_NUM 0 -#define KBD_HID_DESC_OFFSET (9+(9+9+7)*KBD_HID_DESC_NUM+9) +#define KBD_HID_DESC_NUM 0 +#define KBD_HID_DESC_OFFSET (9 + (9 + 9 + 7) * KBD_HID_DESC_NUM + 9) #ifdef MOUSE_ENABLE -# define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1) -# define MOUSE_HID_DESC_OFFSET (9+(9+9+7)*MOUSE_HID_DESC_NUM+9) +# define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1) +# define MOUSE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * MOUSE_HID_DESC_NUM + 9) #else -# define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0) +# define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0) #endif #ifdef CONSOLE_ENABLE -#define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1) -#define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9) +# define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1) +# define DEBUG_HID_DESC_OFFSET (9 + (9 + 9 + 7) * DEBUG_HID_DESC_NUM + 9) #else -# define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0) +# define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0) #endif #ifdef EXTRAKEY_ENABLE -# define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 1) -# define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9) +# define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 1) +# define EXTRA_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9) #else -# define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0) +# define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0) #endif #ifdef NKRO_ENABLE -# define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1) -# define KBD2_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9) +# define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1) +# define KBD2_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9) #else -# define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0) +# define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0) #endif -#define NUM_INTERFACES (KBD2_HID_DESC_NUM + 1) -#define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES) +#define NUM_INTERFACES (KBD2_HID_DESC_NUM + 1) +#define CONFIG1_DESC_SIZE (9 + (9 + 9 + 7) * NUM_INTERFACES) static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { - // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10 - 9, // bLength; - 2, // bDescriptorType; - LSB(CONFIG1_DESC_SIZE), // wTotalLength - MSB(CONFIG1_DESC_SIZE), - NUM_INTERFACES, // bNumInterfaces - 1, // bConfigurationValue - 0, // iConfiguration - 0xA0, // bmAttributes - 50, // bMaxPower - - // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - KBD_INTERFACE, // bInterfaceNumber - 0, // bAlternateSetting - 1, // bNumEndpoints - 0x03, // bInterfaceClass (0x03 = HID) - 0x01, // bInterfaceSubClass (0x01 = Boot) - 0x01, // bInterfaceProtocol (0x01 = Keyboard) - 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 - 9, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0, // bCountryCode - 1, // bNumDescriptors - 0x22, // bDescriptorType - sizeof(keyboard_hid_report_desc), // wDescriptorLength - 0, - // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - KBD_ENDPOINT | 0x80, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - KBD_SIZE, 0, // wMaxPacketSize - 10, // bInterval + // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10 + 9, // bLength; + 2, // bDescriptorType; + LSB(CONFIG1_DESC_SIZE), // wTotalLength + MSB(CONFIG1_DESC_SIZE), + NUM_INTERFACES, // bNumInterfaces + 1, // bConfigurationValue + 0, // iConfiguration + 0xA0, // bmAttributes + 50, // bMaxPower + + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + KBD_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + 0x01, // bInterfaceSubClass (0x01 = Boot) + 0x01, // bInterfaceProtocol (0x01 = Keyboard) + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(keyboard_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + KBD_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + KBD_SIZE, 0, // wMaxPacketSize + 10, // bInterval #ifdef MOUSE_ENABLE - // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - MOUSE_INTERFACE, // bInterfaceNumber - 0, // bAlternateSetting - 1, // bNumEndpoints - 0x03, // bInterfaceClass (0x03 = HID) - // ThinkPad T23 BIOS doesn't work with boot mouse. - 0x00, // bInterfaceSubClass (0x01 = Boot) - 0x00, // bInterfaceProtocol (0x02 = Mouse) -/* - 0x01, // bInterfaceSubClass (0x01 = Boot) - 0x02, // bInterfaceProtocol (0x02 = Mouse) -*/ - 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 - 9, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0, // bCountryCode - 1, // bNumDescriptors - 0x22, // bDescriptorType - sizeof(mouse_hid_report_desc), // wDescriptorLength - 0, - // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - MOUSE_ENDPOINT | 0x80, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - MOUSE_SIZE, 0, // wMaxPacketSize - 1, // bInterval + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + MOUSE_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + // ThinkPad T23 BIOS doesn't work with boot mouse. + 0x00, // bInterfaceSubClass (0x01 = Boot) + 0x00, // bInterfaceProtocol (0x02 = Mouse) + /* + 0x01, // bInterfaceSubClass (0x01 = Boot) + 0x02, // bInterfaceProtocol (0x02 = Mouse) + */ + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(mouse_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + MOUSE_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + MOUSE_SIZE, 0, // wMaxPacketSize + 1, // bInterval #endif #ifdef CONSOLE_ENABLE - // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - DEBUG_INTERFACE, // bInterfaceNumber - 0, // bAlternateSetting - 1, // bNumEndpoints - 0x03, // bInterfaceClass (0x03 = HID) - 0x00, // bInterfaceSubClass - 0x00, // bInterfaceProtocol - 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 - 9, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0, // bCountryCode - 1, // bNumDescriptors - 0x22, // bDescriptorType - sizeof(debug_hid_report_desc), // wDescriptorLength - 0, - // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - DEBUG_TX_SIZE, 0, // wMaxPacketSize - 1, // bInterval + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + DEBUG_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + 0x00, // bInterfaceSubClass + 0x00, // bInterfaceProtocol + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(debug_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + DEBUG_TX_SIZE, 0, // wMaxPacketSize + 1, // bInterval #endif #ifdef EXTRAKEY_ENABLE - // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - EXTRA_INTERFACE, // bInterfaceNumber - 0, // bAlternateSetting - 1, // bNumEndpoints - 0x03, // bInterfaceClass (0x03 = HID) - 0x00, // bInterfaceSubClass - 0x00, // bInterfaceProtocol - 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 - 9, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0, // bCountryCode - 1, // bNumDescriptors - 0x22, // bDescriptorType - sizeof(extra_hid_report_desc), // wDescriptorLength - 0, - // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - EXTRA_ENDPOINT | 0x80, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - EXTRA_SIZE, 0, // wMaxPacketSize - 10, // bInterval + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + EXTRA_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + 0x00, // bInterfaceSubClass + 0x00, // bInterfaceProtocol + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(extra_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + EXTRA_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + EXTRA_SIZE, 0, // wMaxPacketSize + 10, // bInterval #endif #ifdef NKRO_ENABLE - // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - KBD2_INTERFACE, // bInterfaceNumber - 0, // bAlternateSetting - 1, // bNumEndpoints - 0x03, // bInterfaceClass (0x03 = HID) - 0x00, // bInterfaceSubClass (0x01 = Boot) - 0x00, // bInterfaceProtocol (0x01 = Keyboard) - 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 - 9, // bLength - 0x21, // bDescriptorType - 0x11, 0x01, // bcdHID - 0, // bCountryCode - 1, // bNumDescriptors - 0x22, // bDescriptorType - sizeof(keyboard2_hid_report_desc), // wDescriptorLength - 0, - // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - KBD2_ENDPOINT | 0x80, // bEndpointAddress - 0x03, // bmAttributes (0x03=intr) - KBD2_SIZE, 0, // wMaxPacketSize - 1, // bInterval + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + KBD2_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + 0x00, // bInterfaceSubClass (0x01 = Boot) + 0x00, // bInterfaceProtocol (0x01 = Keyboard) + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(keyboard2_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + KBD2_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + KBD2_SIZE, 0, // wMaxPacketSize + 1, // bInterval #endif }; @@ -534,64 +531,50 @@ static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { // can be completely removed if iManufacturer, iProduct, iSerialNumber // in the device desciptor are changed to zeros. struct usb_string_descriptor_struct { - uint8_t bLength; - uint8_t bDescriptorType; - int16_t wString[]; -}; -static const struct usb_string_descriptor_struct PROGMEM string0 = { - 4, - 3, - {0x0409} -}; -static const struct usb_string_descriptor_struct PROGMEM string1 = { - sizeof(STR_MANUFACTURER), - 3, - STR_MANUFACTURER -}; -static const struct usb_string_descriptor_struct PROGMEM string2 = { - sizeof(STR_PRODUCT), - 3, - STR_PRODUCT + uint8_t bLength; + uint8_t bDescriptorType; + int16_t wString[]; }; +static const struct usb_string_descriptor_struct PROGMEM string0 = {4, 3, {0x0409}}; +static const struct usb_string_descriptor_struct PROGMEM string1 = {sizeof(STR_MANUFACTURER), 3, STR_MANUFACTURER}; +static const struct usb_string_descriptor_struct PROGMEM string2 = {sizeof(STR_PRODUCT), 3, STR_PRODUCT}; // This table defines which descriptor data is sent for each specific // request from the host (in wValue and wIndex). static const struct descriptor_list_struct { - uint16_t wValue; // descriptor type - uint16_t wIndex; - const uint8_t *addr; - uint8_t length; + uint16_t wValue; // descriptor type + uint16_t wIndex; + const uint8_t *addr; + uint8_t length; } PROGMEM descriptor_list[] = { - // DEVICE descriptor - {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)}, - // CONFIGURATION descriptor - {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)}, - // HID/REPORT descriptors - {0x2100, KBD_INTERFACE, config1_descriptor+KBD_HID_DESC_OFFSET, 9}, - {0x2200, KBD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)}, + // DEVICE descriptor + {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)}, + // CONFIGURATION descriptor + {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)}, + // HID/REPORT descriptors + {0x2100, KBD_INTERFACE, config1_descriptor + KBD_HID_DESC_OFFSET, 9}, + {0x2200, KBD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)}, #ifdef MOUSE_ENABLE - {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9}, - {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)}, + {0x2100, MOUSE_INTERFACE, config1_descriptor + MOUSE_HID_DESC_OFFSET, 9}, + {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)}, #endif #ifdef CONSOLE_ENABLE - {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9}, - {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)}, + {0x2100, DEBUG_INTERFACE, config1_descriptor + DEBUG_HID_DESC_OFFSET, 9}, + {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)}, #endif #ifdef EXTRAKEY_ENABLE - {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9}, - {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)}, + {0x2100, EXTRA_INTERFACE, config1_descriptor + EXTRA_HID_DESC_OFFSET, 9}, + {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)}, #endif #ifdef NKRO_ENABLE - {0x2100, KBD2_INTERFACE, config1_descriptor+KBD2_HID_DESC_OFFSET, 9}, - {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)}, + {0x2100, KBD2_INTERFACE, config1_descriptor + KBD2_HID_DESC_OFFSET, 9}, + {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)}, #endif - // STRING descriptors - {0x0300, 0x0000, (const uint8_t *)&string0, 4}, - {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)}, - {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)} -}; -#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct)) - + // STRING descriptors + {0x0300, 0x0000, (const uint8_t *)&string0, 4}, + {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)}, + {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}}; +#define NUM_DESC_LIST (sizeof(descriptor_list) / sizeof(struct descriptor_list_struct)) /************************************************************************** * @@ -600,8 +583,7 @@ static const struct descriptor_list_struct { **************************************************************************/ // zero when we are not configured, non-zero when enumerated -static volatile uint8_t usb_configuration=0; - +static volatile uint8_t usb_configuration = 0; /************************************************************************** * @@ -609,400 +591,380 @@ static volatile uint8_t usb_configuration=0; * **************************************************************************/ - // initialize USB -void usb_init(void) -{ - HW_CONFIG(); - USB_FREEZE(); // enable USB - PLL_CONFIG(); // config PLL - while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock - USB_CONFIG(); // start USB clock - UDCON = 0; // enable attach resistor - usb_configuration = 0; - suspend = false; - UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE)|(1<<WAKEUPE); - sei(); +void usb_init(void) { + HW_CONFIG(); + USB_FREEZE(); // enable USB + PLL_CONFIG(); // config PLL + while (!(PLLCSR & (1 << PLOCK))) + ; // wait for PLL lock + USB_CONFIG(); // start USB clock + UDCON = 0; // enable attach resistor + usb_configuration = 0; + suspend = false; + UDIEN = (1 << EORSTE) | (1 << SOFE) | (1 << SUSPE) | (1 << WAKEUPE); + sei(); } // return 0 if the USB is not configured, or the configuration // number selected by the HOST -uint8_t usb_configured(void) -{ - return usb_configuration && !suspend; -} +uint8_t usb_configured(void) { return usb_configuration && !suspend; } -void usb_remote_wakeup(void) -{ - UDCON |= (1<<RMWKUP); - while (UDCON & (1<<RMWKUP)); +void usb_remote_wakeup(void) { + UDCON |= (1 << RMWKUP); + while (UDCON & (1 << RMWKUP)) + ; } - - /************************************************************************** * * Private Functions - not intended for general user consumption.... * **************************************************************************/ - - // USB Device Interrupt - handle all device-level events // the transmit buffer flushing is triggered by the start of frame // -ISR(USB_GEN_vect) -{ - uint8_t intbits, t; - static uint8_t div4=0; - - intbits = UDINT; - UDINT = 0; - if ((intbits & (1<<SUSPI)) && (UDIEN & (1<<SUSPE)) && usb_configuration) { +ISR(USB_GEN_vect) { + uint8_t intbits, t; + static uint8_t div4 = 0; + + intbits = UDINT; + UDINT = 0; + if ((intbits & (1 << SUSPI)) && (UDIEN & (1 << SUSPE)) && usb_configuration) { #ifdef SLEEP_LED_ENABLE - sleep_led_enable(); -#endif - UDIEN &= ~(1<<SUSPE); - UDIEN |= (1<<WAKEUPE); - suspend = true; - } - if ((intbits & (1<<WAKEUPI)) && (UDIEN & (1<<WAKEUPE)) && usb_configuration) { - suspend_wakeup_init(); + sleep_led_enable(); +#endif + UDIEN &= ~(1 << SUSPE); + UDIEN |= (1 << WAKEUPE); + suspend = true; + } + if ((intbits & (1 << WAKEUPI)) && (UDIEN & (1 << WAKEUPE)) && usb_configuration) { + suspend_wakeup_init(); #ifdef SLEEP_LED_ENABLE - sleep_led_disable(); - // NOTE: converters may not accept this - led_set(host_keyboard_leds()); + sleep_led_disable(); + // NOTE: converters may not accept this + led_set(host_keyboard_leds()); #endif - UDIEN |= (1<<SUSPE); - UDIEN &= ~(1<<WAKEUPE); - suspend = false; - } - if (intbits & (1<<EORSTI)) { - UENUM = 0; - UECONX = 1; - UECFG0X = EP_TYPE_CONTROL; - UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER; - UEIENX = (1<<RXSTPE); - usb_configuration = 0; + UDIEN |= (1 << SUSPE); + UDIEN &= ~(1 << WAKEUPE); + suspend = false; + } + if (intbits & (1 << EORSTI)) { + UENUM = 0; + UECONX = 1; + UECFG0X = EP_TYPE_CONTROL; + UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER; + UEIENX = (1 << RXSTPE); + usb_configuration = 0; + } + if ((intbits & (1 << SOFI)) && usb_configuration) { + t = debug_flush_timer; + if (t) { + debug_flush_timer = --t; + if (!t) { + UENUM = DEBUG_TX_ENDPOINT; + while ((UEINTX & (1 << RWAL))) { + UEDATX = 0; + } + UEINTX = 0x3A; + } } - if ((intbits & (1<<SOFI)) && usb_configuration) { - t = debug_flush_timer; - if (t) { - debug_flush_timer = -- t; - if (!t) { - UENUM = DEBUG_TX_ENDPOINT; - while ((UEINTX & (1<<RWAL))) { - UEDATX = 0; - } - UEINTX = 0x3A; - } - } - /* TODO: should keep IDLE rate on each keyboard interface */ + /* TODO: should keep IDLE rate on each keyboard interface */ #ifdef NKRO_ENABLE - if (!keymap_config.nkro && keyboard_idle && (++div4 & 3) == 0) { + if (!keymap_config.nkro && keyboard_idle && (++div4 & 3) == 0) { #else - if (keyboard_idle && (++div4 & 3) == 0) { -#endif - UENUM = KBD_ENDPOINT; - if (UEINTX & (1<<RWAL)) { - usb_keyboard_idle_count++; - if (usb_keyboard_idle_count == keyboard_idle) { - usb_keyboard_idle_count = 0; - /* TODO: fix keyboard_report inconsistency */ -/* To avoid Mac SET_IDLE behaviour. - UEDATX = keyboard_report_prev->mods; - UEDATX = 0; - uint8_t keys = keyboard_protocol ? KBD_REPORT_KEYS : 6; - for (uint8_t i=0; i<keys; i++) { - UEDATX = keyboard_report_prev->keys[i]; - } - UEINTX = 0x3A; -*/ - } - } - } - } + if (keyboard_idle && (++div4 & 3) == 0) { +#endif + UENUM = KBD_ENDPOINT; + if (UEINTX & (1 << RWAL)) { + usb_keyboard_idle_count++; + if (usb_keyboard_idle_count == keyboard_idle) { + usb_keyboard_idle_count = 0; + /* TODO: fix keyboard_report inconsistency */ + /* To avoid Mac SET_IDLE behaviour. + UEDATX = keyboard_report_prev->mods; + UEDATX = 0; + uint8_t keys = keyboard_protocol ? KBD_REPORT_KEYS : 6; + for (uint8_t i=0; i<keys; i++) { + UEDATX = keyboard_report_prev->keys[i]; + } + UEINTX = 0x3A; + */ + } + } + } + } } - - // Misc functions to wait for ready and send/receive packets -static inline void usb_wait_in_ready(void) -{ - while (!(UEINTX & (1<<TXINI))) ; -} -static inline void usb_send_in(void) -{ - UEINTX = ~(1<<TXINI); -} -static inline void usb_wait_receive_out(void) -{ - while (!(UEINTX & (1<<RXOUTI))) ; +static inline void usb_wait_in_ready(void) { + while (!(UEINTX & (1 << TXINI))) + ; } -static inline void usb_ack_out(void) -{ - UEINTX = ~(1<<RXOUTI); +static inline void usb_send_in(void) { UEINTX = ~(1 << TXINI); } +static inline void usb_wait_receive_out(void) { + while (!(UEINTX & (1 << RXOUTI))) + ; } - - +static inline void usb_ack_out(void) { UEINTX = ~(1 << RXOUTI); } // USB Endpoint Interrupt - endpoint 0 is handled here. The // other endpoints are manipulated by the user-callable // functions, and the start-of-frame interrupt. // -ISR(USB_COM_vect) -{ - uint8_t intbits; - const uint8_t *list; - const uint8_t *cfg; - uint8_t i, n, len, en; - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; - uint16_t desc_val; - const uint8_t *desc_addr; - uint8_t desc_length; - - UENUM = 0; - intbits = UEINTX; - if (intbits & (1<<RXSTPI)) { - bmRequestType = UEDATX; - bRequest = UEDATX; - wValue = UEDATX; - wValue |= (UEDATX << 8); - wIndex = UEDATX; - wIndex |= (UEDATX << 8); - wLength = UEDATX; - wLength |= (UEDATX << 8); - UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI)); - if (bRequest == GET_DESCRIPTOR) { - list = (const uint8_t *)descriptor_list; - for (i=0; ; i++) { - if (i >= NUM_DESC_LIST) { - UECONX = (1<<STALLRQ)|(1<<EPEN); //stall - return; - } - desc_val = pgm_read_word(list); - if (desc_val != wValue) { - list += sizeof(struct descriptor_list_struct); - continue; - } - list += 2; - desc_val = pgm_read_word(list); - if (desc_val != wIndex) { - list += sizeof(struct descriptor_list_struct)-2; - continue; - } - list += 2; - desc_addr = (const uint8_t *)pgm_read_word(list); - list += 2; - desc_length = pgm_read_byte(list); - break; - } - len = (wLength < 256) ? wLength : 255; - if (len > desc_length) len = desc_length; - do { - // wait for host ready for IN packet - do { - i = UEINTX; - } while (!(i & ((1<<TXINI)|(1<<RXOUTI)))); - if (i & (1<<RXOUTI)) return; // abort - // send IN packet - n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE; - for (i = n; i; i--) { - UEDATX = pgm_read_byte(desc_addr++); - } - len -= n; - usb_send_in(); - } while (len || n == ENDPOINT0_SIZE); - return; +ISR(USB_COM_vect) { + uint8_t intbits; + const uint8_t *list; + const uint8_t *cfg; + uint8_t i, n, len, en; + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; + uint16_t desc_val; + const uint8_t *desc_addr; + uint8_t desc_length; + + UENUM = 0; + intbits = UEINTX; + if (intbits & (1 << RXSTPI)) { + bmRequestType = UEDATX; + bRequest = UEDATX; + wValue = UEDATX; + wValue |= (UEDATX << 8); + wIndex = UEDATX; + wIndex |= (UEDATX << 8); + wLength = UEDATX; + wLength |= (UEDATX << 8); + UEINTX = ~((1 << RXSTPI) | (1 << RXOUTI) | (1 << TXINI)); + if (bRequest == GET_DESCRIPTOR) { + list = (const uint8_t *)descriptor_list; + for (i = 0;; i++) { + if (i >= NUM_DESC_LIST) { + UECONX = (1 << STALLRQ) | (1 << EPEN); // stall + return; + } + desc_val = pgm_read_word(list); + if (desc_val != wValue) { + list += sizeof(struct descriptor_list_struct); + continue; + } + list += 2; + desc_val = pgm_read_word(list); + if (desc_val != wIndex) { + list += sizeof(struct descriptor_list_struct) - 2; + continue; } - if (bRequest == SET_ADDRESS) { - usb_send_in(); - usb_wait_in_ready(); - UDADDR = wValue | (1<<ADDEN); - return; - } - if (bRequest == SET_CONFIGURATION && bmRequestType == 0) { - usb_configuration = wValue; - usb_send_in(); - cfg = endpoint_config_table; - for (i=1; i<=MAX_ENDPOINT; i++) { - UENUM = i; - en = pgm_read_byte(cfg++); - if (en) { - UECONX = (1<<EPEN); - UECFG0X = pgm_read_byte(cfg++); - UECFG1X = pgm_read_byte(cfg++); - } else { - UECONX = 0; - } - } - UERST = UERST_MASK; - UERST = 0; - return; - } - if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) { - usb_wait_in_ready(); - UEDATX = usb_configuration; - usb_send_in(); - return; - } - - if (bRequest == GET_STATUS) { - usb_wait_in_ready(); - i = 0; - #ifdef SUPPORT_ENDPOINT_HALT - if (bmRequestType == 0x82) { - UENUM = wIndex; - if (UECONX & (1<<STALLRQ)) i = 1; - UENUM = 0; - } - #endif - UEDATX = i; - UEDATX = 0; - usb_send_in(); - return; - } - if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) { + list += 2; + desc_addr = (const uint8_t *)pgm_read_word(list); + list += 2; + desc_length = pgm_read_byte(list); + break; + } + len = (wLength < 256) ? wLength : 255; + if (len > desc_length) len = desc_length; + do { + // wait for host ready for IN packet + do { + i = UEINTX; + } while (!(i & ((1 << TXINI) | (1 << RXOUTI)))); + if (i & (1 << RXOUTI)) return; // abort + // send IN packet + n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE; + for (i = n; i; i--) { + UEDATX = pgm_read_byte(desc_addr++); + } + len -= n; + usb_send_in(); + } while (len || n == ENDPOINT0_SIZE); + return; + } + if (bRequest == SET_ADDRESS) { + usb_send_in(); + usb_wait_in_ready(); + UDADDR = wValue | (1 << ADDEN); + return; + } + if (bRequest == SET_CONFIGURATION && bmRequestType == 0) { + usb_configuration = wValue; + usb_send_in(); + cfg = endpoint_config_table; + for (i = 1; i <= MAX_ENDPOINT; i++) { + UENUM = i; + en = pgm_read_byte(cfg++); + if (en) { + UECONX = (1 << EPEN); + UECFG0X = pgm_read_byte(cfg++); + UECFG1X = pgm_read_byte(cfg++); + } else { + UECONX = 0; + } + } + UERST = UERST_MASK; + UERST = 0; + return; + } + if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) { + usb_wait_in_ready(); + UEDATX = usb_configuration; + usb_send_in(); + return; + } + + if (bRequest == GET_STATUS) { + usb_wait_in_ready(); + i = 0; +#ifdef SUPPORT_ENDPOINT_HALT + if (bmRequestType == 0x82) { + UENUM = wIndex; + if (UECONX & (1 << STALLRQ)) i = 1; + UENUM = 0; + } +#endif + UEDATX = i; + UEDATX = 0; + usb_send_in(); + return; + } + if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) { #ifdef SUPPORT_ENDPOINT_HALT - if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) { - i = wIndex & 0x7F; - if (i >= 1 && i <= MAX_ENDPOINT) { - usb_send_in(); - UENUM = i; - if (bRequest == SET_FEATURE) { - UECONX = (1<<STALLRQ)|(1<<EPEN); - } else { - UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN); - UERST = (1 << i); - UERST = 0; - } - return; - } + if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) { + i = wIndex & 0x7F; + if (i >= 1 && i <= MAX_ENDPOINT) { + usb_send_in(); + UENUM = i; + if (bRequest == SET_FEATURE) { + UECONX = (1 << STALLRQ) | (1 << EPEN); + } else { + UECONX = (1 << STALLRQC) | (1 << RSTDT) | (1 << EPEN); + UERST = (1 << i); + UERST = 0; } + return; + } + } #endif - if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) { - if (bRequest == SET_FEATURE) { - remote_wakeup = true; - } else { - remote_wakeup = false; - } - usb_send_in(); - return; + if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) { + if (bRequest == SET_FEATURE) { + remote_wakeup = true; + } else { + remote_wakeup = false; + } + usb_send_in(); + return; + } + } + if (wIndex == KBD_INTERFACE) { + if (bmRequestType == 0xA1) { + if (bRequest == HID_GET_REPORT) { + usb_wait_in_ready(); + UEDATX = keyboard_report->mods; + UEDATX = 0; + for (i = 0; i < 6; i++) { + UEDATX = keyboard_report->keys[i]; } - } - if (wIndex == KBD_INTERFACE) { - if (bmRequestType == 0xA1) { - if (bRequest == HID_GET_REPORT) { - usb_wait_in_ready(); - UEDATX = keyboard_report->mods; - UEDATX = 0; - for (i=0; i<6; i++) { - UEDATX = keyboard_report->keys[i]; - } - usb_send_in(); - return; - } - if (bRequest == HID_GET_IDLE) { - usb_wait_in_ready(); - UEDATX = keyboard_idle; - usb_send_in(); - return; - } - if (bRequest == HID_GET_PROTOCOL) { - usb_wait_in_ready(); - UEDATX = keyboard_protocol; - usb_send_in(); - return; - } - } - if (bmRequestType == 0x21) { - if (bRequest == HID_SET_REPORT) { - usb_wait_receive_out(); - usb_keyboard_leds = UEDATX; - usb_ack_out(); - usb_send_in(); - return; - } - if (bRequest == HID_SET_IDLE) { - keyboard_idle = (wValue >> 8); - usb_keyboard_idle_count = 0; - //usb_wait_in_ready(); - usb_send_in(); - return; - } - if (bRequest == HID_SET_PROTOCOL) { - keyboard_protocol = wValue; + usb_send_in(); + return; + } + if (bRequest == HID_GET_IDLE) { + usb_wait_in_ready(); + UEDATX = keyboard_idle; + usb_send_in(); + return; + } + if (bRequest == HID_GET_PROTOCOL) { + usb_wait_in_ready(); + UEDATX = keyboard_protocol; + usb_send_in(); + return; + } + } + if (bmRequestType == 0x21) { + if (bRequest == HID_SET_REPORT) { + usb_wait_receive_out(); + usb_keyboard_leds = UEDATX; + usb_ack_out(); + usb_send_in(); + return; + } + if (bRequest == HID_SET_IDLE) { + keyboard_idle = (wValue >> 8); + usb_keyboard_idle_count = 0; + // usb_wait_in_ready(); + usb_send_in(); + return; + } + if (bRequest == HID_SET_PROTOCOL) { + keyboard_protocol = wValue; #ifdef NKRO_ENABLE - keymap_config.nkro = !!keyboard_protocol; -#endif - clear_keyboard(); - //usb_wait_in_ready(); - usb_send_in(); - return; - } - } - } + keymap_config.nkro = !!keyboard_protocol; +#endif + clear_keyboard(); + // usb_wait_in_ready(); + usb_send_in(); + return; + } + } + } #ifdef MOUSE_ENABLE - if (wIndex == MOUSE_INTERFACE) { - if (bmRequestType == 0xA1) { - if (bRequest == HID_GET_REPORT) { - if (wValue == HID_REPORT_INPUT) { - usb_wait_in_ready(); - UEDATX = 0; - UEDATX = 0; - UEDATX = 0; - UEDATX = 0; - usb_send_in(); - return; - } - if (wValue == HID_REPORT_FEATURE) { - usb_wait_in_ready(); - UEDATX = 0x05; - usb_send_in(); - return; - } - } - if (bRequest == HID_GET_PROTOCOL) { - usb_wait_in_ready(); - UEDATX = usb_mouse_protocol; - usb_send_in(); - return; - } - } - if (bmRequestType == 0x21) { - if (bRequest == HID_SET_PROTOCOL) { - usb_mouse_protocol = wValue; - usb_send_in(); - return; - } - } - } -#endif - if (wIndex == DEBUG_INTERFACE) { - if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) { - len = wLength; - do { - // wait for host ready for IN packet - do { - i = UEINTX; - } while (!(i & ((1<<TXINI)|(1<<RXOUTI)))); - if (i & (1<<RXOUTI)) return; // abort - // send IN packet - n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE; - for (i = n; i; i--) { - UEDATX = 0; - } - len -= n; - usb_send_in(); - } while (len || n == ENDPOINT0_SIZE); - return; - } - } - } - UECONX = (1<<STALLRQ) | (1<<EPEN); // stall + if (wIndex == MOUSE_INTERFACE) { + if (bmRequestType == 0xA1) { + if (bRequest == HID_GET_REPORT) { + if (wValue == HID_REPORT_INPUT) { + usb_wait_in_ready(); + UEDATX = 0; + UEDATX = 0; + UEDATX = 0; + UEDATX = 0; + usb_send_in(); + return; + } + if (wValue == HID_REPORT_FEATURE) { + usb_wait_in_ready(); + UEDATX = 0x05; + usb_send_in(); + return; + } + } + if (bRequest == HID_GET_PROTOCOL) { + usb_wait_in_ready(); + UEDATX = usb_mouse_protocol; + usb_send_in(); + return; + } + } + if (bmRequestType == 0x21) { + if (bRequest == HID_SET_PROTOCOL) { + usb_mouse_protocol = wValue; + usb_send_in(); + return; + } + } + } +#endif + if (wIndex == DEBUG_INTERFACE) { + if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) { + len = wLength; + do { + // wait for host ready for IN packet + do { + i = UEINTX; + } while (!(i & ((1 << TXINI) | (1 << RXOUTI)))); + if (i & (1 << RXOUTI)) return; // abort + // send IN packet + n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE; + for (i = n; i; i--) { + UEDATX = 0; + } + len -= n; + usb_send_in(); + } while (len || n == ENDPOINT0_SIZE); + return; + } + } + } + UECONX = (1 << STALLRQ) | (1 << EPEN); // stall } diff --git a/tmk_core/protocol/pjrc/usb.h b/tmk_core/protocol/pjrc/usb.h index a195b671d..50654dccf 100644 --- a/tmk_core/protocol/pjrc/usb.h +++ b/tmk_core/protocol/pjrc/usb.h @@ -22,116 +22,110 @@ */ #ifndef USB_H -#define USB_H 1 +#define USB_H 1 #include <stdint.h> #include <stdbool.h> #include <avr/io.h> - extern bool remote_wakeup; extern bool suspend; -void usb_init(void); // initialize everything -uint8_t usb_configured(void); // is the USB port configured -void usb_remote_wakeup(void); - +void usb_init(void); // initialize everything +uint8_t usb_configured(void); // is the USB port configured +void usb_remote_wakeup(void); -#define EP_TYPE_CONTROL 0x00 -#define EP_TYPE_BULK_IN 0x81 -#define EP_TYPE_BULK_OUT 0x80 -#define EP_TYPE_INTERRUPT_IN 0xC1 -#define EP_TYPE_INTERRUPT_OUT 0xC0 -#define EP_TYPE_ISOCHRONOUS_IN 0x41 -#define EP_TYPE_ISOCHRONOUS_OUT 0x40 +#define EP_TYPE_CONTROL 0x00 +#define EP_TYPE_BULK_IN 0x81 +#define EP_TYPE_BULK_OUT 0x80 +#define EP_TYPE_INTERRUPT_IN 0xC1 +#define EP_TYPE_INTERRUPT_OUT 0xC0 +#define EP_TYPE_ISOCHRONOUS_IN 0x41 +#define EP_TYPE_ISOCHRONOUS_OUT 0x40 -#define EP_SINGLE_BUFFER 0x02 -#define EP_DOUBLE_BUFFER 0x06 +#define EP_SINGLE_BUFFER 0x02 +#define EP_DOUBLE_BUFFER 0x06 -#define EP_SIZE(s) ((s) == 64 ? 0x30 : \ - ((s) == 32 ? 0x20 : \ - ((s) == 16 ? 0x10 : \ - 0x00))) +#define EP_SIZE(s) ((s) == 64 ? 0x30 : ((s) == 32 ? 0x20 : ((s) == 16 ? 0x10 : 0x00))) -#if defined (__AVR_AT90USB162__) || defined (__AVR_AT90USB82__) -# define MAX_ENDPOINT 4 -# define UERST_MASK 0x1E +#if defined(__AVR_AT90USB162__) || defined(__AVR_AT90USB82__) +# define MAX_ENDPOINT 4 +# define UERST_MASK 0x1E #else -# define MAX_ENDPOINT 6 -# define UERST_MASK 0x7E +# define MAX_ENDPOINT 6 +# define UERST_MASK 0x7E #endif #define LSB(n) (n & 255) #define MSB(n) ((n >> 8) & 255) #if defined(__AVR_AT90USB162__) -#define HW_CONFIG() -#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0))) -#define USB_CONFIG() (USBCON = (1<<USBE)) -#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK))) +# define HW_CONFIG() +# define PLL_CONFIG() (PLLCSR = ((1 << PLLE) | (1 << PLLP0))) +# define USB_CONFIG() (USBCON = (1 << USBE)) +# define USB_FREEZE() (USBCON = ((1 << USBE) | (1 << FRZCLK))) #elif defined(__AVR_ATmega32U4__) -#define HW_CONFIG() (UHWCON = 0x01) -#define PLL_CONFIG() (PLLCSR = 0x12) -#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE))) -#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK))) +# define HW_CONFIG() (UHWCON = 0x01) +# define PLL_CONFIG() (PLLCSR = 0x12) +# define USB_CONFIG() (USBCON = ((1 << USBE) | (1 << OTGPADE))) +# define USB_FREEZE() (USBCON = ((1 << USBE) | (1 << FRZCLK))) #elif defined(__AVR_AT90USB646__) -#define HW_CONFIG() (UHWCON = 0x81) -#define PLL_CONFIG() (PLLCSR = 0x1A) -#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE))) -#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK))) +# define HW_CONFIG() (UHWCON = 0x81) +# define PLL_CONFIG() (PLLCSR = 0x1A) +# define USB_CONFIG() (USBCON = ((1 << USBE) | (1 << OTGPADE))) +# define USB_FREEZE() (USBCON = ((1 << USBE) | (1 << FRZCLK))) #elif defined(__AVR_AT90USB1286__) -#define HW_CONFIG() (UHWCON = 0x81) -#define PLL_CONFIG() (PLLCSR = 0x16) -#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE))) -#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK))) +# define HW_CONFIG() (UHWCON = 0x81) +# define PLL_CONFIG() (PLLCSR = 0x16) +# define USB_CONFIG() (USBCON = ((1 << USBE) | (1 << OTGPADE))) +# define USB_FREEZE() (USBCON = ((1 << USBE) | (1 << FRZCLK))) #endif // standard control endpoint request types -#define GET_STATUS 0 -#define CLEAR_FEATURE 1 -#define SET_FEATURE 3 -#define SET_ADDRESS 5 -#define GET_DESCRIPTOR 6 -#define GET_CONFIGURATION 8 -#define SET_CONFIGURATION 9 -#define GET_INTERFACE 10 -#define SET_INTERFACE 11 +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 // HID (human interface device) -#define HID_GET_REPORT 1 -#define HID_GET_IDLE 2 -#define HID_GET_PROTOCOL 3 -#define HID_SET_REPORT 9 -#define HID_SET_IDLE 10 -#define HID_SET_PROTOCOL 11 -#define HID_REPORT_INPUT 1 -#define HID_REPORT_OUTPUT 2 -#define HID_REPORT_FEATURE 3 +#define HID_GET_REPORT 1 +#define HID_GET_IDLE 2 +#define HID_GET_PROTOCOL 3 +#define HID_SET_REPORT 9 +#define HID_SET_IDLE 10 +#define HID_SET_PROTOCOL 11 +#define HID_REPORT_INPUT 1 +#define HID_REPORT_OUTPUT 2 +#define HID_REPORT_FEATURE 3 // CDC (communication class device) -#define CDC_SET_LINE_CODING 0x20 -#define CDC_GET_LINE_CODING 0x21 -#define CDC_SET_CONTROL_LINE_STATE 0x22 +#define CDC_SET_LINE_CODING 0x20 +#define CDC_GET_LINE_CODING 0x21 +#define CDC_SET_CONTROL_LINE_STATE 0x22 // HID feature selectors -#define DEVICE_REMOTE_WAKEUP 1 -#define ENDPOINT_HALT 0 -#define TEST_MODE 2 - +#define DEVICE_REMOTE_WAKEUP 1 +#define ENDPOINT_HALT 0 +#define TEST_MODE 2 /*------------------------------------------------------------------* * Keyboard descriptor setting *------------------------------------------------------------------*/ -#define KBD_INTERFACE 0 -#define KBD_ENDPOINT 1 -#define KBD_SIZE 8 -#define KBD_BUFFER EP_SINGLE_BUFFER -#define KBD_REPORT_KEYS (KBD_SIZE - 2) +#define KBD_INTERFACE 0 +#define KBD_ENDPOINT 1 +#define KBD_SIZE 8 +#define KBD_BUFFER EP_SINGLE_BUFFER +#define KBD_REPORT_KEYS (KBD_SIZE - 2) // secondary keyboard #ifdef NKRO_ENABLE -#define KBD2_INTERFACE 4 -#define KBD2_ENDPOINT 5 -#define KBD2_SIZE 16 -#define KBD2_BUFFER EP_DOUBLE_BUFFER -#define KBD2_REPORT_KEYS (KBD2_SIZE - 1) +# define KBD2_INTERFACE 4 +# define KBD2_ENDPOINT 5 +# define KBD2_SIZE 16 +# define KBD2_BUFFER EP_DOUBLE_BUFFER +# define KBD2_REPORT_KEYS (KBD2_SIZE - 1) #endif #endif diff --git a/tmk_core/protocol/pjrc/usb_debug.c b/tmk_core/protocol/pjrc/usb_debug.c index c1e6f6544..7e78d96c3 100644 --- a/tmk_core/protocol/pjrc/usb_debug.c +++ b/tmk_core/protocol/pjrc/usb_debug.c @@ -25,78 +25,74 @@ #include "sendchar.h" #include "usb_debug.h" - // the time remaining before we transmit any partially full // packet, or send a zero length packet. -volatile uint8_t debug_flush_timer=0; - +volatile uint8_t debug_flush_timer = 0; -int8_t sendchar(uint8_t c) -{ - static uint8_t previous_timeout=0; - uint8_t timeout, intr_state; +int8_t sendchar(uint8_t c) { + static uint8_t previous_timeout = 0; + uint8_t timeout, intr_state; - // if we're not online (enumerated and configured), error - if (!usb_configured()) return -1; - // interrupts are disabled so these functions can be - // used from the main program or interrupt context, - // even both in the same program! - intr_state = SREG; - cli(); - UENUM = DEBUG_TX_ENDPOINT; - // if we gave up due to timeout before, don't wait again - if (previous_timeout) { - if (!(UEINTX & (1<<RWAL))) { - SREG = intr_state; - return -1; - } - previous_timeout = 0; - } - // wait for the FIFO to be ready to accept data - timeout = UDFNUML + 4; - while (1) { - // are we ready to transmit? - if (UEINTX & (1<<RWAL)) break; - SREG = intr_state; - // have we waited too long? - if (UDFNUML == timeout) { - previous_timeout = 1; - return -1; - } - // has the USB gone offline? - if (!usb_configured()) return -1; - // get ready to try checking again - intr_state = SREG; - cli(); - UENUM = DEBUG_TX_ENDPOINT; - } - // actually write the byte into the FIFO - UEDATX = c; - // if this completed a packet, transmit it now! - if (!(UEINTX & (1<<RWAL))) { - UEINTX = 0x3A; - debug_flush_timer = 0; - } else { - debug_flush_timer = 2; - } - SREG = intr_state; - return 0; + // if we're not online (enumerated and configured), error + if (!usb_configured()) return -1; + // interrupts are disabled so these functions can be + // used from the main program or interrupt context, + // even both in the same program! + intr_state = SREG; + cli(); + UENUM = DEBUG_TX_ENDPOINT; + // if we gave up due to timeout before, don't wait again + if (previous_timeout) { + if (!(UEINTX & (1 << RWAL))) { + SREG = intr_state; + return -1; + } + previous_timeout = 0; + } + // wait for the FIFO to be ready to accept data + timeout = UDFNUML + 4; + while (1) { + // are we ready to transmit? + if (UEINTX & (1 << RWAL)) break; + SREG = intr_state; + // have we waited too long? + if (UDFNUML == timeout) { + previous_timeout = 1; + return -1; + } + // has the USB gone offline? + if (!usb_configured()) return -1; + // get ready to try checking again + intr_state = SREG; + cli(); + UENUM = DEBUG_TX_ENDPOINT; + } + // actually write the byte into the FIFO + UEDATX = c; + // if this completed a packet, transmit it now! + if (!(UEINTX & (1 << RWAL))) { + UEINTX = 0x3A; + debug_flush_timer = 0; + } else { + debug_flush_timer = 2; + } + SREG = intr_state; + return 0; } // immediately transmit any buffered output. -void usb_debug_flush_output(void) -{ - uint8_t intr_state; +void usb_debug_flush_output(void) { + uint8_t intr_state; - intr_state = SREG; - cli(); - if (debug_flush_timer) { - UENUM = DEBUG_TX_ENDPOINT; - while ((UEINTX & (1<<RWAL))) { - UEDATX = 0; - } - UEINTX = 0x3A; - debug_flush_timer = 0; - } - SREG = intr_state; + intr_state = SREG; + cli(); + if (debug_flush_timer) { + UENUM = DEBUG_TX_ENDPOINT; + while ((UEINTX & (1 << RWAL))) { + UEDATX = 0; + } + UEINTX = 0x3A; + debug_flush_timer = 0; + } + SREG = intr_state; } diff --git a/tmk_core/protocol/pjrc/usb_debug.h b/tmk_core/protocol/pjrc/usb_debug.h index e70f4ca3a..efe075fa9 100644 --- a/tmk_core/protocol/pjrc/usb_debug.h +++ b/tmk_core/protocol/pjrc/usb_debug.h @@ -22,21 +22,18 @@ */ #ifndef USB_DEBUG_H -#define USB_DEBUG_H 1 +#define USB_DEBUG_H 1 #include <stdint.h> #include "usb.h" - -#define DEBUG_INTERFACE 2 -#define DEBUG_TX_ENDPOINT 3 -#define DEBUG_TX_SIZE 32 -#define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER - +#define DEBUG_INTERFACE 2 +#define DEBUG_TX_ENDPOINT 3 +#define DEBUG_TX_SIZE 32 +#define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER extern volatile uint8_t debug_flush_timer; - -void usb_debug_flush_output(void); // immediately transmit any buffered output +void usb_debug_flush_output(void); // immediately transmit any buffered output #endif diff --git a/tmk_core/protocol/pjrc/usb_extra.c b/tmk_core/protocol/pjrc/usb_extra.c index fe1f422c0..1745e651c 100644 --- a/tmk_core/protocol/pjrc/usb_extra.c +++ b/tmk_core/protocol/pjrc/usb_extra.c @@ -26,45 +26,37 @@ #include "host.h" #include "usb_extra.h" - -int8_t usb_extra_send(uint8_t report_id, uint16_t data) -{ - uint8_t intr_state, timeout; - - if (!usb_configured()) return -1; - intr_state = SREG; - cli(); - UENUM = EXTRA_ENDPOINT; - timeout = UDFNUML + 50; - while (1) { - // are we ready to transmit? - if (UEINTX & (1<<RWAL)) break; - SREG = intr_state; - // has the USB gone offline? - if (!usb_configured()) return -1; - // have we waited too long? - if (UDFNUML == timeout) return -1; - // get ready to try checking again - intr_state = SREG; - cli(); - UENUM = EXTRA_ENDPOINT; - } - - UEDATX = report_id; - UEDATX = data&0xFF; - UEDATX = (data>>8)&0xFF; - - UEINTX = 0x3A; - SREG = intr_state; - return 0; +int8_t usb_extra_send(uint8_t report_id, uint16_t data) { + uint8_t intr_state, timeout; + + if (!usb_configured()) return -1; + intr_state = SREG; + cli(); + UENUM = EXTRA_ENDPOINT; + timeout = UDFNUML + 50; + while (1) { + // are we ready to transmit? + if (UEINTX & (1 << RWAL)) break; + SREG = intr_state; + // has the USB gone offline? + if (!usb_configured()) return -1; + // have we waited too long? + if (UDFNUML == timeout) return -1; + // get ready to try checking again + intr_state = SREG; + cli(); + UENUM = EXTRA_ENDPOINT; + } + + UEDATX = report_id; + UEDATX = data & 0xFF; + UEDATX = (data >> 8) & 0xFF; + + UEINTX = 0x3A; + SREG = intr_state; + return 0; } -int8_t usb_extra_consumer_send(uint16_t bits) -{ - return usb_extra_send(REPORT_ID_CONSUMER, bits); -} +int8_t usb_extra_consumer_send(uint16_t bits) { return usb_extra_send(REPORT_ID_CONSUMER, bits); } -int8_t usb_extra_system_send(uint16_t bits) -{ - return usb_extra_send(REPORT_ID_SYSTEM, bits); -} +int8_t usb_extra_system_send(uint16_t bits) { return usb_extra_send(REPORT_ID_SYSTEM, bits); } diff --git a/tmk_core/protocol/pjrc/usb_extra.h b/tmk_core/protocol/pjrc/usb_extra.h index 042ac4837..674d582ac 100644 --- a/tmk_core/protocol/pjrc/usb_extra.h +++ b/tmk_core/protocol/pjrc/usb_extra.h @@ -22,7 +22,7 @@ */ #ifndef USB_EXTRA_H -#define USB_EXTRA_H 1 +#define USB_EXTRA_H 1 /* * Enhanced keyboard features for Windows: * Audio control and System control @@ -33,12 +33,10 @@ #include <stdint.h> #include "usb.h" - -#define EXTRA_INTERFACE 3 -#define EXTRA_ENDPOINT 4 -#define EXTRA_SIZE 8 -#define EXTRA_BUFFER EP_DOUBLE_BUFFER - +#define EXTRA_INTERFACE 3 +#define EXTRA_ENDPOINT 4 +#define EXTRA_SIZE 8 +#define EXTRA_BUFFER EP_DOUBLE_BUFFER int8_t usb_extra_consumer_send(uint16_t bits); int8_t usb_extra_system_send(uint16_t bits); diff --git a/tmk_core/protocol/pjrc/usb_keyboard.c b/tmk_core/protocol/pjrc/usb_keyboard.c index 05f479734..31856d0fd 100644 --- a/tmk_core/protocol/pjrc/usb_keyboard.c +++ b/tmk_core/protocol/pjrc/usb_keyboard.c @@ -31,34 +31,30 @@ #include "host.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" +# include "keycode_config.h" - extern keymap_config_t keymap_config; +extern keymap_config_t keymap_config; #endif - // protocol setting from the host. We use exactly the same report // either way, so this variable only stores the setting since we // are required to be able to report which setting is in use. -uint8_t keyboard_protocol=1; +uint8_t keyboard_protocol = 1; // the idle configuration, how often we send the report to the // host (ms * 4) even when it hasn't changed // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request. -uint8_t keyboard_idle=125; +uint8_t keyboard_idle = 125; // count until idle timeout -uint8_t usb_keyboard_idle_count=0; +uint8_t usb_keyboard_idle_count = 0; // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana -volatile uint8_t usb_keyboard_leds=0; - +volatile uint8_t usb_keyboard_leds = 0; static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end); - -int8_t usb_keyboard_send_report(report_keyboard_t *report) -{ +int8_t usb_keyboard_send_report(report_keyboard_t *report) { int8_t result = 0; #ifdef NKRO_ENABLE @@ -76,41 +72,43 @@ int8_t usb_keyboard_send_report(report_keyboard_t *report) return 0; } -void usb_keyboard_print_report(report_keyboard_t *report) -{ +void usb_keyboard_print_report(report_keyboard_t *report) { if (!debug_keyboard) return; print("keys: "); - for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); } - print(" mods: "); phex(report->mods); print("\n"); + for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + phex(report->keys[i]); + print(" "); + } + print(" mods: "); + phex(report->mods); + print("\n"); } - -static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end) -{ +static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end) { uint8_t intr_state, timeout; if (!usb_configured()) return -1; intr_state = SREG; cli(); - UENUM = endpoint; + UENUM = endpoint; timeout = UDFNUML + 50; while (1) { - // are we ready to transmit? - if (UEINTX & (1<<RWAL)) break; - SREG = intr_state; - // has the USB gone offline? - if (!usb_configured()) return -1; - // have we waited too long? - if (UDFNUML == timeout) return -1; - // get ready to try checking again - intr_state = SREG; - cli(); - UENUM = endpoint; + // are we ready to transmit? + if (UEINTX & (1 << RWAL)) break; + SREG = intr_state; + // has the USB gone offline? + if (!usb_configured()) return -1; + // have we waited too long? + if (UDFNUML == timeout) return -1; + // get ready to try checking again + intr_state = SREG; + cli(); + UENUM = endpoint; } for (uint8_t i = keys_start; i < keys_end; i++) { - UEDATX = report->raw[i]; + UEDATX = report->raw[i]; } UEINTX = 0x3A; - SREG = intr_state; + SREG = intr_state; return 0; } diff --git a/tmk_core/protocol/pjrc/usb_keyboard.h b/tmk_core/protocol/pjrc/usb_keyboard.h index 9b798e9a8..919685fde 100644 --- a/tmk_core/protocol/pjrc/usb_keyboard.h +++ b/tmk_core/protocol/pjrc/usb_keyboard.h @@ -22,19 +22,17 @@ */ #ifndef USB_KEYBOARD_H -#define USB_KEYBOARD_H 1 +#define USB_KEYBOARD_H 1 #include <stdint.h> #include <stdbool.h> #include "usb.h" #include "host.h" - -extern uint8_t usb_keyboard_idle_count; +extern uint8_t usb_keyboard_idle_count; extern volatile uint8_t usb_keyboard_leds; - int8_t usb_keyboard_send_report(report_keyboard_t *report); -void usb_keyboard_print_report(report_keyboard_t *report); +void usb_keyboard_print_report(report_keyboard_t *report); #endif diff --git a/tmk_core/protocol/pjrc/usb_mouse.c b/tmk_core/protocol/pjrc/usb_mouse.c index d81db756f..9e3639571 100644 --- a/tmk_core/protocol/pjrc/usb_mouse.c +++ b/tmk_core/protocol/pjrc/usb_mouse.c @@ -27,55 +27,57 @@ #include "print.h" #include "debug.h" +uint8_t usb_mouse_protocol = 1; -uint8_t usb_mouse_protocol=1; +int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) { + uint8_t intr_state, timeout; + if (!usb_configured()) return -1; + if (x == -128) x = -127; + if (y == -128) y = -127; + if (wheel_v == -128) wheel_v = -127; + if (wheel_h == -128) wheel_h = -127; + intr_state = SREG; + cli(); + UENUM = MOUSE_ENDPOINT; + timeout = UDFNUML + 50; + while (1) { + // are we ready to transmit? + if (UEINTX & (1 << RWAL)) break; + SREG = intr_state; + // has the USB gone offline? + if (!usb_configured()) return -1; + // have we waited too long? + if (UDFNUML == timeout) return -1; + // get ready to try checking again + intr_state = SREG; + cli(); + UENUM = MOUSE_ENDPOINT; + } + UEDATX = buttons; + UEDATX = x; + UEDATX = y; + if (usb_mouse_protocol) { + UEDATX = wheel_v; + UEDATX = wheel_h; + } -int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) -{ - uint8_t intr_state, timeout; - - if (!usb_configured()) return -1; - if (x == -128) x = -127; - if (y == -128) y = -127; - if (wheel_v == -128) wheel_v = -127; - if (wheel_h == -128) wheel_h = -127; - intr_state = SREG; - cli(); - UENUM = MOUSE_ENDPOINT; - timeout = UDFNUML + 50; - while (1) { - // are we ready to transmit? - if (UEINTX & (1<<RWAL)) break; - SREG = intr_state; - // has the USB gone offline? - if (!usb_configured()) return -1; - // have we waited too long? - if (UDFNUML == timeout) return -1; - // get ready to try checking again - intr_state = SREG; - cli(); - UENUM = MOUSE_ENDPOINT; - } - UEDATX = buttons; - UEDATX = x; - UEDATX = y; - if (usb_mouse_protocol) { - UEDATX = wheel_v; - UEDATX = wheel_h; - } - - UEINTX = 0x3A; - SREG = intr_state; - return 0; + UEINTX = 0x3A; + SREG = intr_state; + return 0; } void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons) { if (!debug_mouse) return; print("usb_mouse[btn|x y v h]: "); - phex(buttons); print("|"); - phex(x); print(" "); - phex(y); print(" "); - phex(wheel_v); print(" "); - phex(wheel_h); print("\n"); + phex(buttons); + print("|"); + phex(x); + print(" "); + phex(y); + print(" "); + phex(wheel_v); + print(" "); + phex(wheel_h); + print("\n"); } diff --git a/tmk_core/protocol/pjrc/usb_mouse.h b/tmk_core/protocol/pjrc/usb_mouse.h index ce26887c9..ba7df230d 100644 --- a/tmk_core/protocol/pjrc/usb_mouse.h +++ b/tmk_core/protocol/pjrc/usb_mouse.h @@ -22,29 +22,26 @@ */ #ifndef USB_MOUSE_H -#define USB_MOUSE_H 1 +#define USB_MOUSE_H 1 #include <stdint.h> #include <stdbool.h> #include "usb.h" +#define MOUSE_INTERFACE 1 +#define MOUSE_ENDPOINT 2 +#define MOUSE_SIZE 8 +#define MOUSE_BUFFER EP_SINGLE_BUFFER -#define MOUSE_INTERFACE 1 -#define MOUSE_ENDPOINT 2 -#define MOUSE_SIZE 8 -#define MOUSE_BUFFER EP_SINGLE_BUFFER - -#define MOUSE_BTN1 (1<<0) -#define MOUSE_BTN2 (1<<1) -#define MOUSE_BTN3 (1<<2) -#define MOUSE_BTN4 (1<<3) -#define MOUSE_BTN5 (1<<4) - +#define MOUSE_BTN1 (1 << 0) +#define MOUSE_BTN2 (1 << 1) +#define MOUSE_BTN3 (1 << 2) +#define MOUSE_BTN4 (1 << 3) +#define MOUSE_BTN5 (1 << 4) extern uint8_t usb_mouse_protocol; - int8_t usb_mouse_send(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons); -void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons); +void usb_mouse_print(int8_t x, int8_t y, int8_t wheel_v, int8_t wheel_h, uint8_t buttons); #endif diff --git a/tmk_core/protocol/ps2.h b/tmk_core/protocol/ps2.h index acde679cf..e32cc9603 100644 --- a/tmk_core/protocol/ps2.h +++ b/tmk_core/protocol/ps2.h @@ -67,66 +67,74 @@ POSSIBILITY OF SUCH DAMAGE. * [5] TrackPoint Engineering Specifications for version 3E * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html */ -#define PS2_ACK 0xFA -#define PS2_RESEND 0xFE -#define PS2_SET_LED 0xED +#define PS2_ACK 0xFA +#define PS2_RESEND 0xFE +#define PS2_SET_LED 0xED // TODO: error numbers -#define PS2_ERR_NONE 0 -#define PS2_ERR_STARTBIT1 1 -#define PS2_ERR_STARTBIT2 2 -#define PS2_ERR_STARTBIT3 3 -#define PS2_ERR_PARITY 0x10 -#define PS2_ERR_NODATA 0x20 +#define PS2_ERR_NONE 0 +#define PS2_ERR_STARTBIT1 1 +#define PS2_ERR_STARTBIT2 2 +#define PS2_ERR_STARTBIT3 3 +#define PS2_ERR_PARITY 0x10 +#define PS2_ERR_NODATA 0x20 #define PS2_LED_SCROLL_LOCK 0 -#define PS2_LED_NUM_LOCK 1 -#define PS2_LED_CAPS_LOCK 2 - +#define PS2_LED_NUM_LOCK 1 +#define PS2_LED_CAPS_LOCK 2 extern uint8_t ps2_error; -void ps2_host_init(void); +void ps2_host_init(void); uint8_t ps2_host_send(uint8_t data); uint8_t ps2_host_recv_response(void); uint8_t ps2_host_recv(void); -void ps2_host_set_led(uint8_t usb_led); - +void ps2_host_set_led(uint8_t usb_led); /*-------------------------------------------------------------------- * static functions *------------------------------------------------------------------*/ -static inline uint16_t wait_clock_lo(uint16_t us) -{ - while (clock_in() && us) { asm(""); wait_us(1); us--; } +static inline uint16_t wait_clock_lo(uint16_t us) { + while (clock_in() && us) { + asm(""); + wait_us(1); + us--; + } return us; } -static inline uint16_t wait_clock_hi(uint16_t us) -{ - while (!clock_in() && us) { asm(""); wait_us(1); us--; } +static inline uint16_t wait_clock_hi(uint16_t us) { + while (!clock_in() && us) { + asm(""); + wait_us(1); + us--; + } return us; } -static inline uint16_t wait_data_lo(uint16_t us) -{ - while (data_in() && us) { asm(""); wait_us(1); us--; } +static inline uint16_t wait_data_lo(uint16_t us) { + while (data_in() && us) { + asm(""); + wait_us(1); + us--; + } return us; } -static inline uint16_t wait_data_hi(uint16_t us) -{ - while (!data_in() && us) { asm(""); wait_us(1); us--; } +static inline uint16_t wait_data_hi(uint16_t us) { + while (!data_in() && us) { + asm(""); + wait_us(1); + us--; + } return us; } /* idle state that device can send */ -static inline void idle(void) -{ +static inline void idle(void) { clock_hi(); data_hi(); } /* inhibit device to send */ -static inline void inhibit(void) -{ +static inline void inhibit(void) { clock_lo(); data_hi(); } diff --git a/tmk_core/protocol/ps2_busywait.c b/tmk_core/protocol/ps2_busywait.c index a64933219..983194eea 100644 --- a/tmk_core/protocol/ps2_busywait.c +++ b/tmk_core/protocol/ps2_busywait.c @@ -45,20 +45,17 @@ POSSIBILITY OF SUCH DAMAGE. #include "ps2_io.h" #include "debug.h" - -#define WAIT(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - ps2_error = err; \ - goto ERROR; \ - } \ -} while (0) - +#define WAIT(stat, us, err) \ + do { \ + if (!wait_##stat(us)) { \ + ps2_error = err; \ + goto ERROR; \ + } \ + } while (0) uint8_t ps2_error = PS2_ERR_NONE; - -void ps2_host_init(void) -{ +void ps2_host_init(void) { clock_init(); data_init(); @@ -68,24 +65,23 @@ void ps2_host_init(void) inhibit(); } -uint8_t ps2_host_send(uint8_t data) -{ +uint8_t ps2_host_send(uint8_t data) { bool parity = true; - ps2_error = PS2_ERR_NONE; + ps2_error = PS2_ERR_NONE; /* terminate a transmission if we have */ inhibit(); - wait_us(100); // 100us [4]p.13, [5]p.50 + wait_us(100); // 100us [4]p.13, [5]p.50 /* 'Request to Send' and Start bit */ data_lo(); clock_hi(); - WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 + WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 /* Data bit */ for (uint8_t i = 0; i < 8; i++) { wait_us(15); - if (data&(1<<i)) { + if (data & (1 << i)) { parity = !parity; data_hi(); } else { @@ -97,7 +93,11 @@ uint8_t ps2_host_send(uint8_t data) /* Parity bit */ wait_us(15); - if (parity) { data_hi(); } else { data_lo(); } + if (parity) { + data_hi(); + } else { + data_lo(); + } WAIT(clock_hi, 50, 4); WAIT(clock_lo, 50, 5); @@ -121,30 +121,29 @@ ERROR: } /* receive data when host want else inhibit communication */ -uint8_t ps2_host_recv_response(void) -{ +uint8_t ps2_host_recv_response(void) { // Command may take 25ms/20ms at most([5]p.46, [3]p.21) // 250 * 100us(wait for start bit in ps2_host_recv) uint8_t data = 0; - uint8_t try = 250; + uint8_t try + = 250; do { data = ps2_host_recv(); - } while (try-- && ps2_error); + } while (try --&&ps2_error); return data; } /* called after start bit comes */ -uint8_t ps2_host_recv(void) -{ - uint8_t data = 0; - bool parity = true; - ps2_error = PS2_ERR_NONE; +uint8_t ps2_host_recv(void) { + uint8_t data = 0; + bool parity = true; + ps2_error = PS2_ERR_NONE; /* release lines(idle state) */ idle(); /* start bit [1] */ - WAIT(clock_lo, 100, 1); // TODO: this is enough? + WAIT(clock_lo, 100, 1); // TODO: this is enough? WAIT(data_lo, 1, 2); WAIT(clock_hi, 50, 3); @@ -153,7 +152,7 @@ uint8_t ps2_host_recv(void) WAIT(clock_lo, 50, 4); if (data_in()) { parity = !parity; - data |= (1<<i); + data |= (1 << i); } WAIT(clock_hi, 50, 5); } @@ -182,8 +181,7 @@ ERROR: } /* send LED state to keyboard */ -void ps2_host_set_led(uint8_t led) -{ +void ps2_host_set_led(uint8_t led) { ps2_host_send(0xED); ps2_host_send(led); } diff --git a/tmk_core/protocol/ps2_interrupt.c b/tmk_core/protocol/ps2_interrupt.c index 8114442ba..5afc8a82e 100644 --- a/tmk_core/protocol/ps2_interrupt.c +++ b/tmk_core/protocol/ps2_interrupt.c @@ -46,26 +46,22 @@ POSSIBILITY OF SUCH DAMAGE. #include "ps2_io.h" #include "print.h" - -#define WAIT(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - ps2_error = err; \ - goto ERROR; \ - } \ -} while (0) - +#define WAIT(stat, us, err) \ + do { \ + if (!wait_##stat(us)) { \ + ps2_error = err; \ + goto ERROR; \ + } \ + } while (0) uint8_t ps2_error = PS2_ERR_NONE; - static inline uint8_t pbuf_dequeue(void); -static inline void pbuf_enqueue(uint8_t data); -static inline bool pbuf_has_data(void); -static inline void pbuf_clear(void); +static inline void pbuf_enqueue(uint8_t data); +static inline bool pbuf_has_data(void); +static inline void pbuf_clear(void); - -void ps2_host_init(void) -{ +void ps2_host_init(void) { idle(); PS2_INT_INIT(); PS2_INT_ON(); @@ -73,26 +69,25 @@ void ps2_host_init(void) //_delay_ms(2500); } -uint8_t ps2_host_send(uint8_t data) -{ +uint8_t ps2_host_send(uint8_t data) { bool parity = true; - ps2_error = PS2_ERR_NONE; + ps2_error = PS2_ERR_NONE; PS2_INT_OFF(); /* terminate a transmission if we have */ inhibit(); - _delay_us(100); // 100us [4]p.13, [5]p.50 + _delay_us(100); // 100us [4]p.13, [5]p.50 /* 'Request to Send' and Start bit */ data_lo(); clock_hi(); - WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 + WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 /* Data bit[2-9] */ for (uint8_t i = 0; i < 8; i++) { _delay_us(15); - if (data&(1<<i)) { + if (data & (1 << i)) { parity = !parity; data_hi(); } else { @@ -104,7 +99,11 @@ uint8_t ps2_host_send(uint8_t data) /* Parity bit */ _delay_us(15); - if (parity) { data_hi(); } else { data_lo(); } + if (parity) { + data_hi(); + } else { + data_lo(); + } WAIT(clock_hi, 50, 4); WAIT(clock_lo, 50, 5); @@ -129,8 +128,7 @@ ERROR: return 0; } -uint8_t ps2_host_recv_response(void) -{ +uint8_t ps2_host_recv_response(void) { // Command may take 25ms/20ms at most([5]p.46, [3]p.21) uint8_t retry = 25; while (retry-- && !pbuf_has_data()) { @@ -140,8 +138,7 @@ uint8_t ps2_host_recv_response(void) } /* get data received by interrupt */ -uint8_t ps2_host_recv(void) -{ +uint8_t ps2_host_recv(void) { if (pbuf_has_data()) { ps2_error = PS2_ERR_NONE; return pbuf_dequeue(); @@ -151,16 +148,22 @@ uint8_t ps2_host_recv(void) } } -ISR(PS2_INT_VECT) -{ +ISR(PS2_INT_VECT) { static enum { INIT, START, - BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7, + BIT0, + BIT1, + BIT2, + BIT3, + BIT4, + BIT5, + BIT6, + BIT7, PARITY, STOP, - } state = INIT; - static uint8_t data = 0; + } state = INIT; + static uint8_t data = 0; static uint8_t parity = 1; // TODO: abort if elapse 100us from previous interrupt @@ -173,8 +176,7 @@ ISR(PS2_INT_VECT) state++; switch (state) { case START: - if (data_in()) - goto ERROR; + if (data_in()) goto ERROR; break; case BIT0: case BIT1: @@ -192,16 +194,13 @@ ISR(PS2_INT_VECT) break; case PARITY: if (data_in()) { - if (!(parity & 0x01)) - goto ERROR; + if (!(parity & 0x01)) goto ERROR; } else { - if (parity & 0x01) - goto ERROR; + if (parity & 0x01) goto ERROR; } break; case STOP: - if (!data_in()) - goto ERROR; + if (!data_in()) goto ERROR; pbuf_enqueue(data); goto DONE; break; @@ -212,68 +211,61 @@ ISR(PS2_INT_VECT) ERROR: ps2_error = state; DONE: - state = INIT; - data = 0; + state = INIT; + data = 0; parity = 1; RETURN: return; } /* send LED state to keyboard */ -void ps2_host_set_led(uint8_t led) -{ +void ps2_host_set_led(uint8_t led) { ps2_host_send(0xED); ps2_host_send(led); } - /*-------------------------------------------------------------------- * Ring buffer to store scan codes from keyboard *------------------------------------------------------------------*/ #define PBUF_SIZE 32 -static uint8_t pbuf[PBUF_SIZE]; -static uint8_t pbuf_head = 0; -static uint8_t pbuf_tail = 0; -static inline void pbuf_enqueue(uint8_t data) -{ +static uint8_t pbuf[PBUF_SIZE]; +static uint8_t pbuf_head = 0; +static uint8_t pbuf_tail = 0; +static inline void pbuf_enqueue(uint8_t data) { uint8_t sreg = SREG; cli(); uint8_t next = (pbuf_head + 1) % PBUF_SIZE; if (next != pbuf_tail) { pbuf[pbuf_head] = data; - pbuf_head = next; + pbuf_head = next; } else { print("pbuf: full\n"); } SREG = sreg; } -static inline uint8_t pbuf_dequeue(void) -{ +static inline uint8_t pbuf_dequeue(void) { uint8_t val = 0; uint8_t sreg = SREG; cli(); if (pbuf_head != pbuf_tail) { - val = pbuf[pbuf_tail]; + val = pbuf[pbuf_tail]; pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; } SREG = sreg; return val; } -static inline bool pbuf_has_data(void) -{ +static inline bool pbuf_has_data(void) { uint8_t sreg = SREG; cli(); bool has_data = (pbuf_head != pbuf_tail); - SREG = sreg; + SREG = sreg; return has_data; } -static inline void pbuf_clear(void) -{ +static inline void pbuf_clear(void) { uint8_t sreg = SREG; cli(); pbuf_head = pbuf_tail = 0; - SREG = sreg; + SREG = sreg; } - diff --git a/tmk_core/protocol/ps2_io.h b/tmk_core/protocol/ps2_io.h index a46a358e7..35e26561d 100644 --- a/tmk_core/protocol/ps2_io.h +++ b/tmk_core/protocol/ps2_io.h @@ -1,7 +1,6 @@ #ifndef PS2_IO_H #define PS2_IO_H - void clock_init(void); void clock_lo(void); void clock_hi(void); diff --git a/tmk_core/protocol/ps2_io_avr.c b/tmk_core/protocol/ps2_io_avr.c index ed462345b..a9ac5d338 100644 --- a/tmk_core/protocol/ps2_io_avr.c +++ b/tmk_core/protocol/ps2_io_avr.c @@ -3,73 +3,56 @@ #include <util/delay.h> /* Check port settings for clock and data line */ -#if !(defined(PS2_CLOCK_PORT) && \ - defined(PS2_CLOCK_PIN) && \ - defined(PS2_CLOCK_DDR) && \ - defined(PS2_CLOCK_BIT)) -# error "PS/2 clock port setting is required in config.h" +#if !(defined(PS2_CLOCK_PORT) && defined(PS2_CLOCK_PIN) && defined(PS2_CLOCK_DDR) && defined(PS2_CLOCK_BIT)) +# error "PS/2 clock port setting is required in config.h" #endif -#if !(defined(PS2_DATA_PORT) && \ - defined(PS2_DATA_PIN) && \ - defined(PS2_DATA_DDR) && \ - defined(PS2_DATA_BIT)) -# error "PS/2 data port setting is required in config.h" +#if !(defined(PS2_DATA_PORT) && defined(PS2_DATA_PIN) && defined(PS2_DATA_DDR) && defined(PS2_DATA_BIT)) +# error "PS/2 data port setting is required in config.h" #endif - /* * Clock */ -void clock_init(void) -{ -} +void clock_init(void) {} -void clock_lo(void) -{ - PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT); - PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT); +void clock_lo(void) { + PS2_CLOCK_PORT &= ~(1 << PS2_CLOCK_BIT); + PS2_CLOCK_DDR |= (1 << PS2_CLOCK_BIT); } -void clock_hi(void) -{ +void clock_hi(void) { /* input with pull up */ - PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); - PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); + PS2_CLOCK_DDR &= ~(1 << PS2_CLOCK_BIT); + PS2_CLOCK_PORT |= (1 << PS2_CLOCK_BIT); } -bool clock_in(void) -{ - PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); - PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); +bool clock_in(void) { + PS2_CLOCK_DDR &= ~(1 << PS2_CLOCK_BIT); + PS2_CLOCK_PORT |= (1 << PS2_CLOCK_BIT); _delay_us(1); - return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT); + return PS2_CLOCK_PIN & (1 << PS2_CLOCK_BIT); } /* * Data */ -void data_init(void) -{ -} +void data_init(void) {} -void data_lo(void) -{ - PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT); - PS2_DATA_DDR |= (1<<PS2_DATA_BIT); +void data_lo(void) { + PS2_DATA_PORT &= ~(1 << PS2_DATA_BIT); + PS2_DATA_DDR |= (1 << PS2_DATA_BIT); } -void data_hi(void) -{ +void data_hi(void) { /* input with pull up */ - PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); - PS2_DATA_PORT |= (1<<PS2_DATA_BIT); + PS2_DATA_DDR &= ~(1 << PS2_DATA_BIT); + PS2_DATA_PORT |= (1 << PS2_DATA_BIT); } -bool data_in(void) -{ - PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); - PS2_DATA_PORT |= (1<<PS2_DATA_BIT); +bool data_in(void) { + PS2_DATA_DDR &= ~(1 << PS2_DATA_BIT); + PS2_DATA_PORT |= (1 << PS2_DATA_BIT); _delay_us(1); - return PS2_DATA_PIN&(1<<PS2_DATA_BIT); + return PS2_DATA_PIN & (1 << PS2_DATA_BIT); } diff --git a/tmk_core/protocol/ps2_io_mbed.c b/tmk_core/protocol/ps2_io_mbed.c index 83bdcef7f..9cec3dfbb 100644 --- a/tmk_core/protocol/ps2_io_mbed.c +++ b/tmk_core/protocol/ps2_io_mbed.c @@ -2,32 +2,27 @@ #include "ps2_io.h" #include "gpio_api.h" - static gpio_t clock; static gpio_t data; /* * Clock */ -void clock_init(void) -{ +void clock_init(void) { gpio_init(&clock, P0_9); - gpio_mode(&clock, OpenDrain|PullNone); + gpio_mode(&clock, OpenDrain | PullNone); } -void clock_lo(void) -{ +void clock_lo(void) { gpio_dir(&clock, PIN_OUTPUT); gpio_write(&clock, 0); } -void clock_hi(void) -{ +void clock_hi(void) { gpio_dir(&clock, PIN_OUTPUT); gpio_write(&clock, 1); } -bool clock_in(void) -{ +bool clock_in(void) { gpio_dir(&clock, PIN_INPUT); return gpio_read(&clock); } @@ -35,26 +30,22 @@ bool clock_in(void) /* * Data */ -void data_init(void) -{ +void data_init(void) { gpio_init(&data, P0_8); - gpio_mode(&data, OpenDrain|PullNone); + gpio_mode(&data, OpenDrain | PullNone); } -void data_lo(void) -{ +void data_lo(void) { gpio_dir(&data, PIN_OUTPUT); gpio_write(&data, 0); } -void data_hi(void) -{ +void data_hi(void) { gpio_dir(&data, PIN_OUTPUT); gpio_write(&data, 1); } -bool data_in(void) -{ +bool data_in(void) { gpio_dir(&data, PIN_INPUT); return gpio_read(&data); } diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index cf1055eb8..aa3a307eb 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -16,8 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <stdbool.h> -#include<avr/io.h> -#include<util/delay.h> +#include <avr/io.h> +#include <util/delay.h> #include "ps2_mouse.h" #include "host.h" #include "timer.h" @@ -42,7 +42,7 @@ static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report); void ps2_mouse_init(void) { ps2_host_init(); - _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up + _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset"); @@ -66,21 +66,19 @@ void ps2_mouse_init(void) { ps2_mouse_init_user(); } -__attribute__((weak)) -void ps2_mouse_init_user(void) { -} +__attribute__((weak)) void ps2_mouse_init_user(void) {} void ps2_mouse_task(void) { static uint8_t buttons_prev = 0; - extern int tp_buttons; + extern int tp_buttons; /* receives packet from mouse */ uint8_t rcv; rcv = ps2_host_send(PS2_MOUSE_READ_DATA); if (rcv == PS2_ACK) { mouse_report.buttons = ps2_host_recv_response() | tp_buttons; - mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER; - mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER; + mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER; + mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER; #ifdef PS2_MOUSE_ENABLE_SCROLLING mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER; #endif @@ -90,8 +88,7 @@ void ps2_mouse_task(void) { } /* if mouse moves or buttons state changes */ - if (mouse_report.x || mouse_report.y || mouse_report.v || - ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) { + if (mouse_report.x || mouse_report.y || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) { #ifdef PS2_MOUSE_DEBUG_RAW // Used to debug raw ps2 bytes from mouse ps2_mouse_print_report(&mouse_report); @@ -111,13 +108,9 @@ void ps2_mouse_task(void) { ps2_mouse_clear_report(&mouse_report); } -void ps2_mouse_disable_data_reporting(void) { - PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting"); -} +void ps2_mouse_disable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting"); } -void ps2_mouse_enable_data_reporting(void) { - PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting"); -} +void ps2_mouse_enable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting"); } void ps2_mouse_set_remote_mode(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode"); @@ -129,28 +122,20 @@ void ps2_mouse_set_stream_mode(void) { ps2_mouse_mode = PS2_MOUSE_STREAM_MODE; } -void ps2_mouse_set_scaling_2_1(void) { - PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1"); -} +void ps2_mouse_set_scaling_2_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1"); } -void ps2_mouse_set_scaling_1_1(void) { - PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1"); -} +void ps2_mouse_set_scaling_1_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1"); } -void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) { - PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution"); -} +void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution"); } -void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) { - PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate"); -} +void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate"); } /* ============================= HELPERS ============================ */ -#define X_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_X_SIGN)) -#define Y_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_Y_SIGN)) -#define X_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_X_OVFLW)) -#define Y_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_Y_OVFLW)) +#define X_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_X_SIGN)) +#define Y_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_Y_SIGN)) +#define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW)) +#define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW)) static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) { // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value. // bit: 8 7 ... 0 @@ -159,12 +144,8 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used. // // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit. - mouse_report->x = X_IS_NEG ? - ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : - ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); - mouse_report->y = Y_IS_NEG ? - ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : - ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); + mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); + mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; @@ -172,29 +153,33 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) #ifdef PS2_MOUSE_INVERT_X mouse_report->x = -mouse_report->x; #endif -#ifndef PS2_MOUSE_INVERT_Y // NOTE if not! +#ifndef PS2_MOUSE_INVERT_Y // NOTE if not! // invert coordinate of y to conform to USB HID mouse mouse_report->y = -mouse_report->y; #endif - } static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) { - mouse_report->x = 0; - mouse_report->y = 0; - mouse_report->v = 0; - mouse_report->h = 0; + mouse_report->x = 0; + mouse_report->y = 0; + mouse_report->v = 0; + mouse_report->h = 0; mouse_report->buttons = 0; } static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) { if (!debug_mouse) return; print("ps2_mouse: ["); - phex(mouse_report->buttons); print("|"); - print_hex8((uint8_t)mouse_report->x); print(" "); - print_hex8((uint8_t)mouse_report->y); print(" "); - print_hex8((uint8_t)mouse_report->v); print(" "); - print_hex8((uint8_t)mouse_report->h); print("]\n"); + phex(mouse_report->buttons); + print("|"); + print_hex8((uint8_t)mouse_report->x); + print(" "); + print_hex8((uint8_t)mouse_report->y); + print(" "); + print_hex8((uint8_t)mouse_report->v); + print(" "); + print_hex8((uint8_t)mouse_report->h); + print("]\n"); } static inline void ps2_mouse_enable_scrolling(void) { @@ -208,14 +193,14 @@ static inline void ps2_mouse_enable_scrolling(void) { _delay_ms(20); } -#define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK) -#define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK) +#define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK) +#define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK) static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) { static enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT, - } scroll_state = SCROLL_NONE; + } scroll_state = SCROLL_NONE; static uint16_t scroll_button_time = 0; if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) { @@ -223,14 +208,14 @@ static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) { if (scroll_state == SCROLL_NONE) { scroll_button_time = timer_read(); - scroll_state = SCROLL_BTN; + scroll_state = SCROLL_BTN; } // If the mouse has moved, update the report to scroll instead of move the mouse if (mouse_report->x || mouse_report->y) { - scroll_state = SCROLL_SENT; - mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V); - mouse_report->h = mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H); + scroll_state = SCROLL_SENT; + mouse_report->v = -mouse_report->y / (PS2_MOUSE_SCROLL_DIVISOR_V); + mouse_report->h = mouse_report->x / (PS2_MOUSE_SCROLL_DIVISOR_H); mouse_report->x = 0; mouse_report->y = 0; #ifdef PS2_MOUSE_INVERT_H @@ -244,8 +229,7 @@ static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) { // None of the scroll buttons are pressed #if PS2_MOUSE_SCROLL_BTN_SEND - if (scroll_state == SCROLL_BTN - && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) { + if (scroll_state == SCROLL_BTN && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) { PRESS_SCROLL_BUTTONS; host_mouse_send(mouse_report); _delay_ms(100); diff --git a/tmk_core/protocol/ps2_mouse.h b/tmk_core/protocol/ps2_mouse.h index eeeffe4d8..30053ef18 100644 --- a/tmk_core/protocol/ps2_mouse.h +++ b/tmk_core/protocol/ps2_mouse.h @@ -16,54 +16,53 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef PS2_MOUSE_H -#define PS2_MOUSE_H +#define PS2_MOUSE_H #include <stdbool.h> #include "debug.h" -#define PS2_MOUSE_SEND(command, message) \ -do { \ - __attribute__ ((unused)) uint8_t rcv = ps2_host_send(command); \ - if (debug_mouse) { \ - print((message)); \ - xprintf(" command: %X, result: %X, error: %X \n", command, rcv, ps2_error); \ - } \ -} while(0) - -#define PS2_MOUSE_SEND_SAFE(command, message) \ -do { \ - if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ - ps2_mouse_disable_data_reporting(); \ - } \ - PS2_MOUSE_SEND(command, message); \ - if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ - ps2_mouse_enable_data_reporting(); \ - } \ -} while(0) - -#define PS2_MOUSE_SET_SAFE(command, value, message) \ -do { \ - if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ - ps2_mouse_disable_data_reporting(); \ - } \ - PS2_MOUSE_SEND(command, message); \ - PS2_MOUSE_SEND(value, "Sending value"); \ - if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ - ps2_mouse_enable_data_reporting(); \ - } \ -} while(0) - -#define PS2_MOUSE_RECEIVE(message) \ -do { \ - __attribute__ ((unused)) uint8_t rcv = ps2_host_recv_response(); \ - if (debug_mouse) { \ - print((message)); \ - xprintf(" result: %X, error: %X \n", rcv, ps2_error); \ - } \ -} while(0) - -__attribute__ ((unused)) -static enum ps2_mouse_mode_e { +#define PS2_MOUSE_SEND(command, message) \ + do { \ + __attribute__((unused)) uint8_t rcv = ps2_host_send(command); \ + if (debug_mouse) { \ + print((message)); \ + xprintf(" command: %X, result: %X, error: %X \n", command, rcv, ps2_error); \ + } \ + } while (0) + +#define PS2_MOUSE_SEND_SAFE(command, message) \ + do { \ + if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ + ps2_mouse_disable_data_reporting(); \ + } \ + PS2_MOUSE_SEND(command, message); \ + if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ + ps2_mouse_enable_data_reporting(); \ + } \ + } while (0) + +#define PS2_MOUSE_SET_SAFE(command, value, message) \ + do { \ + if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ + ps2_mouse_disable_data_reporting(); \ + } \ + PS2_MOUSE_SEND(command, message); \ + PS2_MOUSE_SEND(value, "Sending value"); \ + if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \ + ps2_mouse_enable_data_reporting(); \ + } \ + } while (0) + +#define PS2_MOUSE_RECEIVE(message) \ + do { \ + __attribute__((unused)) uint8_t rcv = ps2_host_recv_response(); \ + if (debug_mouse) { \ + print((message)); \ + xprintf(" result: %X, error: %X \n", rcv, ps2_error); \ + } \ + } while (0) + +__attribute__((unused)) static enum ps2_mouse_mode_e { PS2_MOUSE_STREAM_MODE, PS2_MOUSE_REMOTE_MODE, } ps2_mouse_mode = PS2_MOUSE_STREAM_MODE; @@ -76,79 +75,79 @@ static enum ps2_mouse_mode_e { * 1|[ X movement(0-255) ] * 2|[ Y movement(0-255) ] */ -#define PS2_MOUSE_BTN_MASK 0x07 -#define PS2_MOUSE_BTN_LEFT 0 -#define PS2_MOUSE_BTN_RIGHT 1 -#define PS2_MOUSE_BTN_MIDDLE 2 -#define PS2_MOUSE_X_SIGN 4 -#define PS2_MOUSE_Y_SIGN 5 -#define PS2_MOUSE_X_OVFLW 6 -#define PS2_MOUSE_Y_OVFLW 7 +#define PS2_MOUSE_BTN_MASK 0x07 +#define PS2_MOUSE_BTN_LEFT 0 +#define PS2_MOUSE_BTN_RIGHT 1 +#define PS2_MOUSE_BTN_MIDDLE 2 +#define PS2_MOUSE_X_SIGN 4 +#define PS2_MOUSE_Y_SIGN 5 +#define PS2_MOUSE_X_OVFLW 6 +#define PS2_MOUSE_Y_OVFLW 7 /* mouse button to start scrolling; set 0 to disable scroll */ #ifndef PS2_MOUSE_SCROLL_BTN_MASK -#define PS2_MOUSE_SCROLL_BTN_MASK (1<<PS2_MOUSE_BTN_MIDDLE) +# define PS2_MOUSE_SCROLL_BTN_MASK (1 << PS2_MOUSE_BTN_MIDDLE) #endif /* send button event when button is released within this value(ms); set 0 to disable */ #ifndef PS2_MOUSE_SCROLL_BTN_SEND -#define PS2_MOUSE_SCROLL_BTN_SEND 300 +# define PS2_MOUSE_SCROLL_BTN_SEND 300 #endif /* divide virtical and horizontal mouse move by this to convert to scroll move */ #ifndef PS2_MOUSE_SCROLL_DIVISOR_V -#define PS2_MOUSE_SCROLL_DIVISOR_V 2 +# define PS2_MOUSE_SCROLL_DIVISOR_V 2 #endif #ifndef PS2_MOUSE_SCROLL_DIVISOR_H -#define PS2_MOUSE_SCROLL_DIVISOR_H 2 +# define PS2_MOUSE_SCROLL_DIVISOR_H 2 #endif /* multiply reported mouse values by these */ #ifndef PS2_MOUSE_X_MULTIPLIER -#define PS2_MOUSE_X_MULTIPLIER 1 +# define PS2_MOUSE_X_MULTIPLIER 1 #endif #ifndef PS2_MOUSE_Y_MULTIPLIER -#define PS2_MOUSE_Y_MULTIPLIER 1 +# define PS2_MOUSE_Y_MULTIPLIER 1 #endif #ifndef PS2_MOUSE_V_MULTIPLIER -#define PS2_MOUSE_V_MULTIPLIER 1 +# define PS2_MOUSE_V_MULTIPLIER 1 #endif /* For some mice this will need to be 0x0F */ -#ifndef PS2_MOUSE_SCROLL_MASK -#define PS2_MOUSE_SCROLL_MASK 0xFF +#ifndef PS2_MOUSE_SCROLL_MASK +# define PS2_MOUSE_SCROLL_MASK 0xFF #endif #ifndef PS2_MOUSE_INIT_DELAY -#define PS2_MOUSE_INIT_DELAY 1000 +# define PS2_MOUSE_INIT_DELAY 1000 #endif enum ps2_mouse_command_e { - PS2_MOUSE_RESET = 0xFF, - PS2_MOUSE_RESEND = 0xFE, - PS2_MOSUE_SET_DEFAULTS = 0xF6, + PS2_MOUSE_RESET = 0xFF, + PS2_MOUSE_RESEND = 0xFE, + PS2_MOSUE_SET_DEFAULTS = 0xF6, PS2_MOUSE_DISABLE_DATA_REPORTING = 0xF5, - PS2_MOUSE_ENABLE_DATA_REPORTING = 0xF4, - PS2_MOUSE_SET_SAMPLE_RATE = 0xF3, - PS2_MOUSE_GET_DEVICE_ID = 0xF2, - PS2_MOUSE_SET_REMOTE_MODE = 0xF0, - PS2_MOUSE_SET_WRAP_MODE = 0xEC, - PS2_MOUSE_READ_DATA = 0xEB, - PS2_MOUSE_SET_STREAM_MODE = 0xEA, - PS2_MOUSE_STATUS_REQUEST = 0xE9, - PS2_MOUSE_SET_RESOLUTION = 0xE8, - PS2_MOUSE_SET_SCALING_2_1 = 0xE7, - PS2_MOUSE_SET_SCALING_1_1 = 0xE6, + PS2_MOUSE_ENABLE_DATA_REPORTING = 0xF4, + PS2_MOUSE_SET_SAMPLE_RATE = 0xF3, + PS2_MOUSE_GET_DEVICE_ID = 0xF2, + PS2_MOUSE_SET_REMOTE_MODE = 0xF0, + PS2_MOUSE_SET_WRAP_MODE = 0xEC, + PS2_MOUSE_READ_DATA = 0xEB, + PS2_MOUSE_SET_STREAM_MODE = 0xEA, + PS2_MOUSE_STATUS_REQUEST = 0xE9, + PS2_MOUSE_SET_RESOLUTION = 0xE8, + PS2_MOUSE_SET_SCALING_2_1 = 0xE7, + PS2_MOUSE_SET_SCALING_1_1 = 0xE6, }; typedef enum ps2_mouse_resolution_e { PS2_MOUSE_1_COUNT_MM, PS2_MOUSE_2_COUNT_MM, PS2_MOUSE_4_COUNT_MM, - PS2_MOUSE_8_COUNT_MM, + PS2_MOUSE_8_COUNT_MM, } ps2_mouse_resolution_t; typedef enum ps2_mouse_sample_rate_e { - PS2_MOUSE_10_SAMPLES_SEC = 10, - PS2_MOUSE_20_SAMPLES_SEC = 20, - PS2_MOUSE_40_SAMPLES_SEC = 40, - PS2_MOUSE_60_SAMPLES_SEC = 60, - PS2_MOUSE_80_SAMPLES_SEC = 80, + PS2_MOUSE_10_SAMPLES_SEC = 10, + PS2_MOUSE_20_SAMPLES_SEC = 20, + PS2_MOUSE_40_SAMPLES_SEC = 40, + PS2_MOUSE_60_SAMPLES_SEC = 60, + PS2_MOUSE_80_SAMPLES_SEC = 80, PS2_MOUSE_100_SAMPLES_SEC = 100, PS2_MOUSE_200_SAMPLES_SEC = 200, } ps2_mouse_sample_rate_t; diff --git a/tmk_core/protocol/ps2_usart.c b/tmk_core/protocol/ps2_usart.c index 6936ca7b8..6a66dc4a1 100644 --- a/tmk_core/protocol/ps2_usart.c +++ b/tmk_core/protocol/ps2_usart.c @@ -46,53 +46,48 @@ POSSIBILITY OF SUCH DAMAGE. #include "ps2_io.h" #include "print.h" - -#define WAIT(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - ps2_error = err; \ - goto ERROR; \ - } \ -} while (0) - +#define WAIT(stat, us, err) \ + do { \ + if (!wait_##stat(us)) { \ + ps2_error = err; \ + goto ERROR; \ + } \ + } while (0) uint8_t ps2_error = PS2_ERR_NONE; - static inline uint8_t pbuf_dequeue(void); -static inline void pbuf_enqueue(uint8_t data); -static inline bool pbuf_has_data(void); -static inline void pbuf_clear(void); +static inline void pbuf_enqueue(uint8_t data); +static inline bool pbuf_has_data(void); +static inline void pbuf_clear(void); - -void ps2_host_init(void) -{ - idle(); // without this many USART errors occur when cable is disconnected +void ps2_host_init(void) { + idle(); // without this many USART errors occur when cable is disconnected PS2_USART_INIT(); PS2_USART_RX_INT_ON(); // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) //_delay_ms(2500); } -uint8_t ps2_host_send(uint8_t data) -{ +uint8_t ps2_host_send(uint8_t data) { bool parity = true; - ps2_error = PS2_ERR_NONE; + ps2_error = PS2_ERR_NONE; PS2_USART_OFF(); /* terminate a transmission if we have */ inhibit(); - _delay_us(100); // [4]p.13 + _delay_us(100); // [4]p.13 /* 'Request to Send' and Start bit */ data_lo(); clock_hi(); - WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 + WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 /* Data bit[2-9] */ for (uint8_t i = 0; i < 8; i++) { _delay_us(15); - if (data&(1<<i)) { + if (data & (1 << i)) { parity = !parity; data_hi(); } else { @@ -104,7 +99,11 @@ uint8_t ps2_host_send(uint8_t data) /* Parity bit */ _delay_us(15); - if (parity) { data_hi(); } else { data_lo(); } + if (parity) { + data_hi(); + } else { + data_lo(); + } WAIT(clock_hi, 50, 4); WAIT(clock_lo, 50, 5); @@ -131,8 +130,7 @@ ERROR: return 0; } -uint8_t ps2_host_recv_response(void) -{ +uint8_t ps2_host_recv_response(void) { // Command may take 25ms/20ms at most([5]p.46, [3]p.21) uint8_t retry = 25; while (retry-- && !pbuf_has_data()) { @@ -141,8 +139,7 @@ uint8_t ps2_host_recv_response(void) return pbuf_dequeue(); } -uint8_t ps2_host_recv(void) -{ +uint8_t ps2_host_recv(void) { if (pbuf_has_data()) { ps2_error = PS2_ERR_NONE; return pbuf_dequeue(); @@ -152,11 +149,10 @@ uint8_t ps2_host_recv(void) } } -ISR(PS2_USART_RX_VECT) -{ +ISR(PS2_USART_RX_VECT) { // TODO: request RESEND when error occurs? - uint8_t error = PS2_USART_ERROR; // USART error should be read before data - uint8_t data = PS2_USART_RX_DATA; + uint8_t error = PS2_USART_ERROR; // USART error should be read before data + uint8_t data = PS2_USART_RX_DATA; if (!error) { pbuf_enqueue(data); } else { @@ -165,59 +161,53 @@ ISR(PS2_USART_RX_VECT) } /* send LED state to keyboard */ -void ps2_host_set_led(uint8_t led) -{ +void ps2_host_set_led(uint8_t led) { ps2_host_send(0xED); ps2_host_send(led); } - /*-------------------------------------------------------------------- * Ring buffer to store scan codes from keyboard *------------------------------------------------------------------*/ #define PBUF_SIZE 32 -static uint8_t pbuf[PBUF_SIZE]; -static uint8_t pbuf_head = 0; -static uint8_t pbuf_tail = 0; -static inline void pbuf_enqueue(uint8_t data) -{ +static uint8_t pbuf[PBUF_SIZE]; +static uint8_t pbuf_head = 0; +static uint8_t pbuf_tail = 0; +static inline void pbuf_enqueue(uint8_t data) { uint8_t sreg = SREG; cli(); uint8_t next = (pbuf_head + 1) % PBUF_SIZE; if (next != pbuf_tail) { pbuf[pbuf_head] = data; - pbuf_head = next; + pbuf_head = next; } else { print("pbuf: full\n"); } SREG = sreg; } -static inline uint8_t pbuf_dequeue(void) -{ +static inline uint8_t pbuf_dequeue(void) { uint8_t val = 0; uint8_t sreg = SREG; cli(); if (pbuf_head != pbuf_tail) { - val = pbuf[pbuf_tail]; + val = pbuf[pbuf_tail]; pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; } SREG = sreg; return val; } -static inline bool pbuf_has_data(void) -{ +static inline bool pbuf_has_data(void) { uint8_t sreg = SREG; cli(); bool has_data = (pbuf_head != pbuf_tail); - SREG = sreg; + SREG = sreg; return has_data; } -static inline void pbuf_clear(void) -{ +static inline void pbuf_clear(void) { uint8_t sreg = SREG; cli(); pbuf_head = pbuf_tail = 0; - SREG = sreg; + SREG = sreg; } diff --git a/tmk_core/protocol/serial.h b/tmk_core/protocol/serial.h index 96913c867..93ac99898 100644 --- a/tmk_core/protocol/serial.h +++ b/tmk_core/protocol/serial.h @@ -39,9 +39,9 @@ POSSIBILITY OF SUCH DAMAGE. #define SERIAL_H /* host role */ -void serial_init(void); +void serial_init(void); uint8_t serial_recv(void); int16_t serial_recv2(void); -void serial_send(uint8_t data); +void serial_send(uint8_t data); #endif diff --git a/tmk_core/protocol/serial_mouse.h b/tmk_core/protocol/serial_mouse.h index 226314fc0..b10315f36 100644 --- a/tmk_core/protocol/serial_mouse.h +++ b/tmk_core/protocol/serial_mouse.h @@ -22,8 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "serial.h" -static inline uint8_t serial_mouse_init(void) -{ +static inline uint8_t serial_mouse_init(void) { serial_init(); return 0; } diff --git a/tmk_core/protocol/serial_mouse_microsoft.c b/tmk_core/protocol/serial_mouse_microsoft.c index ab74b7cdd..eff0bf6e4 100644 --- a/tmk_core/protocol/serial_mouse_microsoft.c +++ b/tmk_core/protocol/serial_mouse_microsoft.c @@ -28,36 +28,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "debug.h" #ifdef MAX -#undef MAX +# undef MAX #endif #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) static void print_usb_data(const report_mouse_t *report); -void serial_mouse_task(void) -{ +void serial_mouse_task(void) { /* 3 byte ring buffer */ static uint8_t buffer[3]; - static int buffer_cur = 0; + static int buffer_cur = 0; static report_mouse_t report = {}; int16_t rcv; rcv = serial_recv2(); - if (rcv < 0) - /* no new data */ + if (rcv < 0) /* no new data */ return; - if (debug_mouse) - xprintf("serial_mouse: byte: %04X\n", rcv); + if (debug_mouse) xprintf("serial_mouse: byte: %04X\n", rcv); /* * If bit 6 is one, this signals the beginning * of a 3 byte sequence/packet. */ - if (rcv & (1 << 6)) - buffer_cur = 0; + if (rcv & (1 << 6)) buffer_cur = 0; buffer[buffer_cur] = (uint8_t)rcv; @@ -76,8 +72,7 @@ void serial_mouse_task(void) buffer_cur++; - if (buffer_cur < 3) - return; + if (buffer_cur < 3) return; buffer_cur = 0; /* @@ -87,10 +82,8 @@ void serial_mouse_task(void) * change. */ report.buttons = 0; - if (buffer[0] & (1 << 5)) - report.buttons |= MOUSE_BTN1; - if (buffer[0] & (1 << 4)) - report.buttons |= MOUSE_BTN2; + if (buffer[0] & (1 << 5)) report.buttons |= MOUSE_BTN1; + if (buffer[0] & (1 << 4)) report.buttons |= MOUSE_BTN2; report.x = (buffer[0] << 6) | buffer[1]; report.y = ((buffer[0] << 4) & 0xC0) | buffer[2]; @@ -113,12 +106,8 @@ void serial_mouse_task(void) host_mouse_send(&report); } -static void print_usb_data(const report_mouse_t *report) -{ - if (!debug_mouse) - return; +static void print_usb_data(const report_mouse_t *report) { + if (!debug_mouse) return; - xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", - report->buttons, report->x, report->y, - report->v, report->h); + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", report->buttons, report->x, report->y, report->v, report->h); } diff --git a/tmk_core/protocol/serial_mouse_mousesystems.c b/tmk_core/protocol/serial_mouse_mousesystems.c index cfe899621..0ec2b0399 100644 --- a/tmk_core/protocol/serial_mouse_mousesystems.c +++ b/tmk_core/protocol/serial_mouse_mousesystems.c @@ -28,7 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "debug.h" #ifdef MAX -#undef MAX +# undef MAX #endif #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) @@ -36,23 +36,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. static void print_usb_data(const report_mouse_t *report); -void serial_mouse_task(void) -{ +void serial_mouse_task(void) { /* 5 byte ring buffer */ static uint8_t buffer[5]; - static int buffer_cur = 0; + static int buffer_cur = 0; int16_t rcv; report_mouse_t report = {0, 0, 0, 0, 0}; rcv = serial_recv2(); - if (rcv < 0) - /* no new data */ + if (rcv < 0) /* no new data */ return; - if (debug_mouse) - xprintf("serial_mouse: byte: %04X\n", rcv); + if (debug_mouse) xprintf("serial_mouse: byte: %04X\n", rcv); /* * Synchronization: mouse(4) says that all @@ -61,13 +58,11 @@ void serial_mouse_task(void) * Therefore we discard all bytes up to the * first one with the characteristic bit pattern. */ - if (buffer_cur == 0 && (rcv >> 3) != 0x10) - return; + if (buffer_cur == 0 && (rcv >> 3) != 0x10) return; buffer[buffer_cur++] = (uint8_t)rcv; - if (buffer_cur < 5) - return; + if (buffer_cur < 5) return; buffer_cur = 0; #ifdef SERIAL_MOUSE_CENTER_SCROLL @@ -97,12 +92,9 @@ void serial_mouse_task(void) * if the mouse moved or the button states * change. */ - if (!(buffer[0] & (1 << 2))) - report.buttons |= MOUSE_BTN1; - if (!(buffer[0] & (1 << 1))) - report.buttons |= MOUSE_BTN3; - if (!(buffer[0] & (1 << 0))) - report.buttons |= MOUSE_BTN2; + if (!(buffer[0] & (1 << 2))) report.buttons |= MOUSE_BTN1; + if (!(buffer[0] & (1 << 1))) report.buttons |= MOUSE_BTN3; + if (!(buffer[0] & (1 << 0))) report.buttons |= MOUSE_BTN2; /* USB HID uses only values from -127 to 127 */ report.x = MAX((int8_t)buffer[1], -127); @@ -120,12 +112,8 @@ void serial_mouse_task(void) } } -static void print_usb_data(const report_mouse_t *report) -{ - if (!debug_mouse) - return; +static void print_usb_data(const report_mouse_t *report) { + if (!debug_mouse) return; - xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", - report->buttons, report->x, report->y, - report->v, report->h); + xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", report->buttons, report->x, report->y, report->v, report->h); } diff --git a/tmk_core/protocol/serial_soft.c b/tmk_core/protocol/serial_soft.c index 44822b7e4..b40907995 100644 --- a/tmk_core/protocol/serial_soft.c +++ b/tmk_core/protocol/serial_soft.c @@ -49,37 +49,35 @@ POSSIBILITY OF SUCH DAMAGE. * TODO: delay is not accurate enough. Instruction cycle should be counted and inline assemby is needed. */ -#define WAIT_US (1000000L/SERIAL_SOFT_BAUD) +#define WAIT_US (1000000L / SERIAL_SOFT_BAUD) #ifdef SERIAL_SOFT_LOGIC_NEGATIVE - #define SERIAL_SOFT_RXD_IN() !(SERIAL_SOFT_RXD_READ()) - #define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_LO() - #define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_HI() +# define SERIAL_SOFT_RXD_IN() !(SERIAL_SOFT_RXD_READ()) +# define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_LO() +# define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_HI() #else - #define SERIAL_SOFT_RXD_IN() !!(SERIAL_SOFT_RXD_READ()) - #define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_HI() - #define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_LO() +# define SERIAL_SOFT_RXD_IN() !!(SERIAL_SOFT_RXD_READ()) +# define SERIAL_SOFT_TXD_ON() SERIAL_SOFT_TXD_HI() +# define SERIAL_SOFT_TXD_OFF() SERIAL_SOFT_TXD_LO() #endif #ifdef SERIAL_SOFT_PARITY_EVEN - #define SERIAL_SOFT_PARITY_VAL 0 +# define SERIAL_SOFT_PARITY_VAL 0 #elif defined(SERIAL_SOFT_PARITY_ODD) - #define SERIAL_SOFT_PARITY_VAL 1 +# define SERIAL_SOFT_PARITY_VAL 1 #endif /* debug for signal timing, see debug pin with oscilloscope */ #define SERIAL_SOFT_DEBUG #ifdef SERIAL_SOFT_DEBUG - #define SERIAL_SOFT_DEBUG_INIT() (DDRD |= 1<<7) - #define SERIAL_SOFT_DEBUG_TGL() (PORTD ^= 1<<7) +# define SERIAL_SOFT_DEBUG_INIT() (DDRD |= 1 << 7) +# define SERIAL_SOFT_DEBUG_TGL() (PORTD ^= 1 << 7) #else - #define SERIAL_SOFT_DEBUG_INIT() - #define SERIAL_SOFT_DEBUG_TGL() +# define SERIAL_SOFT_DEBUG_INIT() +# define SERIAL_SOFT_DEBUG_TGL() #endif - -void serial_init(void) -{ +void serial_init(void) { SERIAL_SOFT_DEBUG_INIT(); SERIAL_SOFT_RXD_INIT(); @@ -87,46 +85,42 @@ void serial_init(void) } /* RX ring buffer */ -#define RBUF_SIZE 8 +#define RBUF_SIZE 8 static uint8_t rbuf[RBUF_SIZE]; static uint8_t rbuf_head = 0; static uint8_t rbuf_tail = 0; - -uint8_t serial_recv(void) -{ +uint8_t serial_recv(void) { uint8_t data = 0; if (rbuf_head == rbuf_tail) { return 0; } - data = rbuf[rbuf_tail]; + data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; return data; } -int16_t serial_recv2(void) -{ +int16_t serial_recv2(void) { uint8_t data = 0; if (rbuf_head == rbuf_tail) { return -1; } - data = rbuf[rbuf_tail]; + data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; return data; } -void serial_send(uint8_t data) -{ +void serial_send(uint8_t data) { /* signal state: IDLE: ON, START: OFF, STOP: ON, DATA0: OFF, DATA1: ON */ #ifdef SERIAL_SOFT_BIT_ORDER_MSB - #ifdef SERIAL_SOFT_DATA_7BIT +# ifdef SERIAL_SOFT_DATA_7BIT uint8_t mask = 0x40; - #else +# else uint8_t mask = 0x80; - #endif +# endif #else uint8_t mask = 0x01; #endif @@ -138,11 +132,11 @@ void serial_send(uint8_t data) _delay_us(WAIT_US); #ifdef SERIAL_SOFT_DATA_7BIT - while (mask&0x7F) { + while (mask & 0x7F) { #else - while (mask&0xFF) { + while (mask & 0xFF) { #endif - if (data&mask) { + if (data & mask) { SERIAL_SOFT_TXD_ON(); parity ^= 1; } else { @@ -173,19 +167,18 @@ void serial_send(uint8_t data) } /* detect edge of start bit */ -ISR(SERIAL_SOFT_RXD_VECT) -{ +ISR(SERIAL_SOFT_RXD_VECT) { SERIAL_SOFT_DEBUG_TGL(); SERIAL_SOFT_RXD_INT_ENTER() uint8_t data = 0; #ifdef SERIAL_SOFT_BIT_ORDER_MSB - #ifdef SERIAL_SOFT_DATA_7BIT +# ifdef SERIAL_SOFT_DATA_7BIT uint8_t mask = 0x40; - #else +# else uint8_t mask = 0x80; - #endif +# endif #else uint8_t mask = 0x01; #endif @@ -193,13 +186,13 @@ ISR(SERIAL_SOFT_RXD_VECT) uint8_t parity = 0; /* to center of start bit */ - _delay_us(WAIT_US/2); + _delay_us(WAIT_US / 2); SERIAL_SOFT_DEBUG_TGL(); do { /* to center of next bit */ _delay_us(WAIT_US); - SERIAL_SOFT_DEBUG_TGL(); + SERIAL_SOFT_DEBUG_TGL(); if (SERIAL_SOFT_RXD_IN()) { data |= mask; parity ^= 1; @@ -210,15 +203,17 @@ ISR(SERIAL_SOFT_RXD_VECT) mask <<= 1; #endif #ifdef SERIAL_SOFT_DATA_7BIT - } while (mask&0x7F); + } while (mask & 0x7F); #else - } while (mask&0xFF); + } while (mask & 0xFF); #endif #if defined(SERIAL_SOFT_PARITY_EVEN) || defined(SERIAL_SOFT_PARITY_ODD) /* to center of parity bit */ _delay_us(WAIT_US); - if (SERIAL_SOFT_RXD_IN()) { parity ^= 1; } + if (SERIAL_SOFT_RXD_IN()) { + parity ^= 1; + } SERIAL_SOFT_DEBUG_TGL(); #endif @@ -232,7 +227,7 @@ ISR(SERIAL_SOFT_RXD_VECT) if (next != rbuf_tail) { #endif rbuf[rbuf_head] = data; - rbuf_head = next; + rbuf_head = next; } SERIAL_SOFT_RXD_INT_EXIT(); diff --git a/tmk_core/protocol/serial_uart.c b/tmk_core/protocol/serial_uart.c index 35df27fd6..a15124193 100644 --- a/tmk_core/protocol/serial_uart.c +++ b/tmk_core/protocol/serial_uart.c @@ -40,73 +40,71 @@ POSSIBILITY OF SUCH DAMAGE. #include <avr/interrupt.h> #include "serial.h" - #if defined(SERIAL_UART_RTS_LO) && defined(SERIAL_UART_RTS_HI) - // Buffer state - // Empty: RBUF_SPACE == RBUF_SIZE(head==tail) - // Last 1 space: RBUF_SPACE == 2 - // Full: RBUF_SPACE == 1(last cell of rbuf be never used.) - #define RBUF_SPACE() (rbuf_head < rbuf_tail ? (rbuf_tail - rbuf_head) : (RBUF_SIZE - rbuf_head + rbuf_tail)) - // allow to send - #define rbuf_check_rts_lo() do { if (RBUF_SPACE() > 2) SERIAL_UART_RTS_LO(); } while (0) - // prohibit to send - #define rbuf_check_rts_hi() do { if (RBUF_SPACE() <= 2) SERIAL_UART_RTS_HI(); } while (0) +// Buffer state +// Empty: RBUF_SPACE == RBUF_SIZE(head==tail) +// Last 1 space: RBUF_SPACE == 2 +// Full: RBUF_SPACE == 1(last cell of rbuf be never used.) +# define RBUF_SPACE() (rbuf_head < rbuf_tail ? (rbuf_tail - rbuf_head) : (RBUF_SIZE - rbuf_head + rbuf_tail)) +// allow to send +# define rbuf_check_rts_lo() \ + do { \ + if (RBUF_SPACE() > 2) SERIAL_UART_RTS_LO(); \ + } while (0) +// prohibit to send +# define rbuf_check_rts_hi() \ + do { \ + if (RBUF_SPACE() <= 2) SERIAL_UART_RTS_HI(); \ + } while (0) #else - #define rbuf_check_rts_lo() - #define rbuf_check_rts_hi() +# define rbuf_check_rts_lo() +# define rbuf_check_rts_hi() #endif - -void serial_init(void) -{ - SERIAL_UART_INIT(); -} +void serial_init(void) { SERIAL_UART_INIT(); } // RX ring buffer -#define RBUF_SIZE 256 +#define RBUF_SIZE 256 static uint8_t rbuf[RBUF_SIZE]; static uint8_t rbuf_head = 0; static uint8_t rbuf_tail = 0; -uint8_t serial_recv(void) -{ +uint8_t serial_recv(void) { uint8_t data = 0; if (rbuf_head == rbuf_tail) { return 0; } - data = rbuf[rbuf_tail]; + data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; rbuf_check_rts_lo(); return data; } -int16_t serial_recv2(void) -{ +int16_t serial_recv2(void) { uint8_t data = 0; if (rbuf_head == rbuf_tail) { return -1; } - data = rbuf[rbuf_tail]; + data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; rbuf_check_rts_lo(); return data; } -void serial_send(uint8_t data) -{ - while (!SERIAL_UART_TXD_READY) ; +void serial_send(uint8_t data) { + while (!SERIAL_UART_TXD_READY) + ; SERIAL_UART_DATA = data; } // USART RX complete interrupt -ISR(SERIAL_UART_RXD_VECT) -{ +ISR(SERIAL_UART_RXD_VECT) { uint8_t next = (rbuf_head + 1) % RBUF_SIZE; if (next != rbuf_tail) { rbuf[rbuf_head] = SERIAL_UART_DATA; - rbuf_head = next; + rbuf_head = next; } rbuf_check_rts_hi(); } diff --git a/tmk_core/protocol/usb_descriptor.c b/tmk_core/protocol/usb_descriptor.c index af49eeec3..5349553b7 100644 --- a/tmk_core/protocol/usb_descriptor.c +++ b/tmk_core/protocol/usb_descriptor.c @@ -45,114 +45,114 @@ */ #ifdef KEYBOARD_SHARED_EP const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = { - #define SHARED_REPORT_STARTED +# define SHARED_REPORT_STARTED #else const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] = { #endif - HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop - HID_RI_USAGE(8, 0x06), // Keyboard - HID_RI_COLLECTION(8, 0x01), // Application + HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop + HID_RI_USAGE(8, 0x06), // Keyboard + HID_RI_COLLECTION(8, 0x01), // Application #ifdef KEYBOARD_SHARED_EP - HID_RI_REPORT_ID(8, REPORT_ID_KEYBOARD), + HID_RI_REPORT_ID(8, REPORT_ID_KEYBOARD), #endif - // Modifiers (8 bits) - HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad - HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control - HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(8, 0x01), - HID_RI_REPORT_COUNT(8, 0x08), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - // Reserved (1 byte) - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_CONSTANT), - // Keycodes (6 bytes) - HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad - HID_RI_USAGE_MINIMUM(8, 0x00), - HID_RI_USAGE_MAXIMUM(8, 0xFF), - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), - HID_RI_REPORT_COUNT(8, 0x06), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), - - // Status LEDs (5 bits) - HID_RI_USAGE_PAGE(8, 0x08), // LED - HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock - HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana - HID_RI_REPORT_COUNT(8, 0x05), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), - // LED padding (3 bits) - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x03), - HID_RI_OUTPUT(8, HID_IOF_CONSTANT), + // Modifiers (8 bits) + HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad + HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control + HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(8, 0x01), + HID_RI_REPORT_COUNT(8, 0x08), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + // Reserved (1 byte) + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_CONSTANT), + // Keycodes (6 bytes) + HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad + HID_RI_USAGE_MINIMUM(8, 0x00), + HID_RI_USAGE_MAXIMUM(8, 0xFF), + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), + HID_RI_REPORT_COUNT(8, 0x06), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), + + // Status LEDs (5 bits) + HID_RI_USAGE_PAGE(8, 0x08), // LED + HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock + HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana + HID_RI_REPORT_COUNT(8, 0x05), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), + // LED padding (3 bits) + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x03), + HID_RI_OUTPUT(8, HID_IOF_CONSTANT), HID_RI_END_COLLECTION(0), #ifndef KEYBOARD_SHARED_EP }; #endif #ifdef MOUSE_ENABLE - #ifndef MOUSE_SHARED_EP +# ifndef MOUSE_SHARED_EP const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] = { - #elif !defined(SHARED_REPORT_STARTED) +# elif !defined(SHARED_REPORT_STARTED) const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = { - #define SHARED_REPORT_STARTED - #endif - HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop - HID_RI_USAGE(8, 0x02), // Mouse - HID_RI_COLLECTION(8, 0x01), // Application - #ifdef MOUSE_SHARED_EP - HID_RI_REPORT_ID(8, REPORT_ID_MOUSE), - #endif - HID_RI_USAGE(8, 0x01), // Pointer - HID_RI_COLLECTION(8, 0x00), // Physical - // Buttons (5 bits) - HID_RI_USAGE_PAGE(8, 0x09), // Button - HID_RI_USAGE_MINIMUM(8, 0x01), // Button 1 - HID_RI_USAGE_MAXIMUM(8, 0x05), // Button 5 - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(8, 0x01), - HID_RI_REPORT_COUNT(8, 0x05), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - // Button padding (3 bits) - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x03), - HID_RI_INPUT(8, HID_IOF_CONSTANT), - - // X/Y position (2 bytes) - HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop - HID_RI_USAGE(8, 0x30), // X - HID_RI_USAGE(8, 0x31), // Y - HID_RI_LOGICAL_MINIMUM(8, -127), - HID_RI_LOGICAL_MAXIMUM(8, 127), - HID_RI_REPORT_COUNT(8, 0x02), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), - - // Vertical wheel (1 byte) - HID_RI_USAGE(8, 0x38), // Wheel - HID_RI_LOGICAL_MINIMUM(8, -127), - HID_RI_LOGICAL_MAXIMUM(8, 127), - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), - // Horizontal wheel (1 byte) - HID_RI_USAGE_PAGE(8, 0x0C), // Consumer - HID_RI_USAGE(16, 0x0238), // AC Pan - HID_RI_LOGICAL_MINIMUM(8, -127), - HID_RI_LOGICAL_MAXIMUM(8, 127), - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), - HID_RI_END_COLLECTION(0), +# define SHARED_REPORT_STARTED +# endif + HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop + HID_RI_USAGE(8, 0x02), // Mouse + HID_RI_COLLECTION(8, 0x01), // Application +# ifdef MOUSE_SHARED_EP + HID_RI_REPORT_ID(8, REPORT_ID_MOUSE), +# endif + HID_RI_USAGE(8, 0x01), // Pointer + HID_RI_COLLECTION(8, 0x00), // Physical + // Buttons (5 bits) + HID_RI_USAGE_PAGE(8, 0x09), // Button + HID_RI_USAGE_MINIMUM(8, 0x01), // Button 1 + HID_RI_USAGE_MAXIMUM(8, 0x05), // Button 5 + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(8, 0x01), + HID_RI_REPORT_COUNT(8, 0x05), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + // Button padding (3 bits) + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x03), + HID_RI_INPUT(8, HID_IOF_CONSTANT), + + // X/Y position (2 bytes) + HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop + HID_RI_USAGE(8, 0x30), // X + HID_RI_USAGE(8, 0x31), // Y + HID_RI_LOGICAL_MINIMUM(8, -127), + HID_RI_LOGICAL_MAXIMUM(8, 127), + HID_RI_REPORT_COUNT(8, 0x02), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), + + // Vertical wheel (1 byte) + HID_RI_USAGE(8, 0x38), // Wheel + HID_RI_LOGICAL_MINIMUM(8, -127), + HID_RI_LOGICAL_MAXIMUM(8, 127), + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), + // Horizontal wheel (1 byte) + HID_RI_USAGE_PAGE(8, 0x0C), // Consumer + HID_RI_USAGE(16, 0x0238), // AC Pan + HID_RI_LOGICAL_MINIMUM(8, -127), + HID_RI_LOGICAL_MAXIMUM(8, 127), + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE), HID_RI_END_COLLECTION(0), - #ifndef MOUSE_SHARED_EP + HID_RI_END_COLLECTION(0), +# ifndef MOUSE_SHARED_EP }; - #endif +# endif #endif #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED) @@ -160,68 +160,68 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = { #endif #ifdef EXTRAKEY_ENABLE - HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop - HID_RI_USAGE(8, 0x80), // System Control - HID_RI_COLLECTION(8, 0x01), // Application - HID_RI_REPORT_ID(8, REPORT_ID_SYSTEM), - HID_RI_USAGE_MINIMUM(16, 0x0081), // System Power Down - HID_RI_USAGE_MAXIMUM(16, 0x0083), // System Wake Up - HID_RI_LOGICAL_MINIMUM(16, 0x0001), - HID_RI_LOGICAL_MAXIMUM(16, 0x0003), - HID_RI_REPORT_COUNT(8, 1), - HID_RI_REPORT_SIZE(8, 16), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), + HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop + HID_RI_USAGE(8, 0x80), // System Control + HID_RI_COLLECTION(8, 0x01), // Application + HID_RI_REPORT_ID(8, REPORT_ID_SYSTEM), + HID_RI_USAGE_MINIMUM(16, 0x0081), // System Power Down + HID_RI_USAGE_MAXIMUM(16, 0x0083), // System Wake Up + HID_RI_LOGICAL_MINIMUM(16, 0x0001), + HID_RI_LOGICAL_MAXIMUM(16, 0x0003), + HID_RI_REPORT_COUNT(8, 1), + HID_RI_REPORT_SIZE(8, 16), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), HID_RI_END_COLLECTION(0), - HID_RI_USAGE_PAGE(8, 0x0C), // Consumer - HID_RI_USAGE(8, 0x01), // Consumer Control - HID_RI_COLLECTION(8, 0x01), // Application - HID_RI_REPORT_ID(8, REPORT_ID_CONSUMER), - HID_RI_USAGE_MINIMUM(16, 0x0001), // Consumer Control - HID_RI_USAGE_MAXIMUM(16, 0x029C), // AC Distribute Vertically - HID_RI_LOGICAL_MINIMUM(16, 0x0001), - HID_RI_LOGICAL_MAXIMUM(16, 0x029C), - HID_RI_REPORT_COUNT(8, 1), - HID_RI_REPORT_SIZE(8, 16), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), + HID_RI_USAGE_PAGE(8, 0x0C), // Consumer + HID_RI_USAGE(8, 0x01), // Consumer Control + HID_RI_COLLECTION(8, 0x01), // Application + HID_RI_REPORT_ID(8, REPORT_ID_CONSUMER), + HID_RI_USAGE_MINIMUM(16, 0x0001), // Consumer Control + HID_RI_USAGE_MAXIMUM(16, 0x029C), // AC Distribute Vertically + HID_RI_LOGICAL_MINIMUM(16, 0x0001), + HID_RI_LOGICAL_MAXIMUM(16, 0x029C), + HID_RI_REPORT_COUNT(8, 1), + HID_RI_REPORT_SIZE(8, 16), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE), HID_RI_END_COLLECTION(0), #endif #ifdef NKRO_ENABLE - HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop - HID_RI_USAGE(8, 0x06), // Keyboard - HID_RI_COLLECTION(8, 0x01), // Application - HID_RI_REPORT_ID(8, REPORT_ID_NKRO), - // Modifiers (8 bits) - HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad - HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control - HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(8, 0x01), - HID_RI_REPORT_COUNT(8, 0x08), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - // Keycodes - HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad - HID_RI_USAGE_MINIMUM(8, 0x00), - HID_RI_USAGE_MAXIMUM(8, KEYBOARD_REPORT_BITS * 8 - 1), - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(8, 0x01), - HID_RI_REPORT_COUNT(8, KEYBOARD_REPORT_BITS * 8), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - - // Status LEDs (5 bits) - HID_RI_USAGE_PAGE(8, 0x08), // LED - HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock - HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana - HID_RI_REPORT_COUNT(8, 0x05), - HID_RI_REPORT_SIZE(8, 0x01), - HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), - // LED padding (3 bits) - HID_RI_REPORT_COUNT(8, 0x01), - HID_RI_REPORT_SIZE(8, 0x03), - HID_RI_OUTPUT(8, HID_IOF_CONSTANT), + HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop + HID_RI_USAGE(8, 0x06), // Keyboard + HID_RI_COLLECTION(8, 0x01), // Application + HID_RI_REPORT_ID(8, REPORT_ID_NKRO), + // Modifiers (8 bits) + HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad + HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control + HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(8, 0x01), + HID_RI_REPORT_COUNT(8, 0x08), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + // Keycodes + HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad + HID_RI_USAGE_MINIMUM(8, 0x00), + HID_RI_USAGE_MAXIMUM(8, KEYBOARD_REPORT_BITS * 8 - 1), + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(8, 0x01), + HID_RI_REPORT_COUNT(8, KEYBOARD_REPORT_BITS * 8), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + + // Status LEDs (5 bits) + HID_RI_USAGE_PAGE(8, 0x08), // LED + HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock + HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana + HID_RI_REPORT_COUNT(8, 0x05), + HID_RI_REPORT_SIZE(8, 0x01), + HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), + // LED padding (3 bits) + HID_RI_REPORT_COUNT(8, 0x01), + HID_RI_REPORT_SIZE(8, 0x03), + HID_RI_OUTPUT(8, HID_IOF_CONSTANT), HID_RI_END_COLLECTION(0), #endif #ifdef SHARED_EP_ENABLE @@ -230,46 +230,46 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = { #ifdef RAW_ENABLE const USB_Descriptor_HIDReport_Datatype_t PROGMEM RawReport[] = { - HID_RI_USAGE_PAGE(16, 0xFF60), // Vendor Defined - HID_RI_USAGE(8, 0x61), // Vendor Defined - HID_RI_COLLECTION(8, 0x01), // Application - // Data to host - HID_RI_USAGE(8, 0x62), // Vendor Defined - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), - HID_RI_REPORT_COUNT(8, RAW_EPSIZE), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - // Data from host - HID_RI_USAGE(8, 0x63), // Vendor Defined - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), - HID_RI_REPORT_COUNT(8, RAW_EPSIZE), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), + HID_RI_USAGE_PAGE(16, 0xFF60), // Vendor Defined + HID_RI_USAGE(8, 0x61), // Vendor Defined + HID_RI_COLLECTION(8, 0x01), // Application + // Data to host + HID_RI_USAGE(8, 0x62), // Vendor Defined + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), + HID_RI_REPORT_COUNT(8, RAW_EPSIZE), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + // Data from host + HID_RI_USAGE(8, 0x63), // Vendor Defined + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), + HID_RI_REPORT_COUNT(8, RAW_EPSIZE), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), HID_RI_END_COLLECTION(0), }; #endif #ifdef CONSOLE_ENABLE const USB_Descriptor_HIDReport_Datatype_t PROGMEM ConsoleReport[] = { - HID_RI_USAGE_PAGE(16, 0xFF31), // Vendor Defined (PJRC Teensy compatible) - HID_RI_USAGE(8, 0x74), // Vendor Defined (PJRC Teensy compatible) - HID_RI_COLLECTION(8, 0x01), // Application - // Data to host - HID_RI_USAGE(8, 0x75), // Vendor Defined - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), - HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), - // Data from host - HID_RI_USAGE(8, 0x76), // Vendor Defined - HID_RI_LOGICAL_MINIMUM(8, 0x00), - HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), - HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE), - HID_RI_REPORT_SIZE(8, 0x08), - HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), + HID_RI_USAGE_PAGE(16, 0xFF31), // Vendor Defined (PJRC Teensy compatible) + HID_RI_USAGE(8, 0x74), // Vendor Defined (PJRC Teensy compatible) + HID_RI_COLLECTION(8, 0x01), // Application + // Data to host + HID_RI_USAGE(8, 0x75), // Vendor Defined + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), + HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), + // Data from host + HID_RI_USAGE(8, 0x76), // Vendor Defined + HID_RI_LOGICAL_MINIMUM(8, 0x00), + HID_RI_LOGICAL_MAXIMUM(16, 0x00FF), + HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE), + HID_RI_REPORT_SIZE(8, 0x08), + HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE), HID_RI_END_COLLECTION(0), }; #endif @@ -277,577 +277,225 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM ConsoleReport[] = { /* * Device descriptor */ -const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = { - .Header = { - .Size = sizeof(USB_Descriptor_Device_t), - .Type = DTYPE_Device - }, - .USBSpecification = VERSION_BCD(1, 1, 0), +const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = {.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, + .USBSpecification = VERSION_BCD(1, 1, 0), #if VIRTSER_ENABLE - .Class = USB_CSCP_IADDeviceClass, - .SubClass = USB_CSCP_IADDeviceSubclass, - .Protocol = USB_CSCP_IADDeviceProtocol, + .Class = USB_CSCP_IADDeviceClass, + .SubClass = USB_CSCP_IADDeviceSubclass, + .Protocol = USB_CSCP_IADDeviceProtocol, #else - .Class = USB_CSCP_NoDeviceClass, - .SubClass = USB_CSCP_NoDeviceSubclass, - .Protocol = USB_CSCP_NoDeviceProtocol, + .Class = USB_CSCP_NoDeviceClass, + .SubClass = USB_CSCP_NoDeviceSubclass, + .Protocol = USB_CSCP_NoDeviceProtocol, #endif - .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, - // Specified in config.h - .VendorID = VENDOR_ID, - .ProductID = PRODUCT_ID, - .ReleaseNumber = DEVICE_VER, - .ManufacturerStrIndex = 0x01, - .ProductStrIndex = 0x02, - .SerialNumStrIndex = 0x03, - .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS -}; + .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, + // Specified in config.h + .VendorID = VENDOR_ID, + .ProductID = PRODUCT_ID, + .ReleaseNumber = DEVICE_VER, + .ManufacturerStrIndex = 0x01, + .ProductStrIndex = 0x02, + .SerialNumStrIndex = 0x03, + .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS}; #ifndef USB_MAX_POWER_CONSUMPTION - #define USB_MAX_POWER_CONSUMPTION 500 +# define USB_MAX_POWER_CONSUMPTION 500 #endif /* * Configuration descriptors */ -const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = { - .Config = { - .Header = { - .Size = sizeof(USB_Descriptor_Configuration_Header_t), - .Type = DTYPE_Configuration - }, - .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), - .TotalInterfaces = TOTAL_INTERFACES, - .ConfigurationNumber = 1, - .ConfigurationStrIndex = NO_DESCRIPTOR, - .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP), - .MaxPowerConsumption = USB_CONFIG_POWER_MA(USB_MAX_POWER_CONSUMPTION) - }, +const USB_Descriptor_Configuration_t PROGMEM + ConfigurationDescriptor = + { + .Config = {.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), .TotalInterfaces = TOTAL_INTERFACES, .ConfigurationNumber = 1, .ConfigurationStrIndex = NO_DESCRIPTOR, .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP), .MaxPowerConsumption = USB_CONFIG_POWER_MA(USB_MAX_POWER_CONSUMPTION)}, #ifndef KEYBOARD_SHARED_EP - /* - * Keyboard - */ - .Keyboard_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = KEYBOARD_INTERFACE, - .AlternateSetting = 0x00, - .TotalEndpoints = 1, - .Class = HID_CSCP_HIDClass, - .SubClass = HID_CSCP_BootSubclass, - .Protocol = HID_CSCP_KeyboardBootProtocol, - - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Keyboard_HID = { - .Header = { - .Size = sizeof(USB_HID_Descriptor_HID_t), - .Type = HID_DTYPE_HID - }, - .HIDSpec = VERSION_BCD(1, 1, 1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(KeyboardReport) - }, - .Keyboard_INEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_IN | KEYBOARD_IN_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = KEYBOARD_EPSIZE, - .PollingIntervalMS = 0x0A - }, + /* + * Keyboard + */ + .Keyboard_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + .InterfaceNumber = KEYBOARD_INTERFACE, + .AlternateSetting = 0x00, + .TotalEndpoints = 1, + .Class = HID_CSCP_HIDClass, + .SubClass = HID_CSCP_BootSubclass, + .Protocol = HID_CSCP_KeyboardBootProtocol, + + .InterfaceStrIndex = NO_DESCRIPTOR}, + .Keyboard_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(KeyboardReport)}, + .Keyboard_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | KEYBOARD_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = KEYBOARD_EPSIZE, .PollingIntervalMS = 0x0A}, #endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) - /* - * Mouse - */ - .Mouse_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = MOUSE_INTERFACE, - .AlternateSetting = 0x00, - .TotalEndpoints = 1, - .Class = HID_CSCP_HIDClass, - .SubClass = HID_CSCP_BootSubclass, - .Protocol = HID_CSCP_MouseBootProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Mouse_HID = { - .Header = { - .Size = sizeof(USB_HID_Descriptor_HID_t), - .Type = HID_DTYPE_HID - }, - .HIDSpec = VERSION_BCD(1, 1, 1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(MouseReport) - }, - .Mouse_INEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_IN | MOUSE_IN_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = MOUSE_EPSIZE, - .PollingIntervalMS = 0x0A - }, + /* + * Mouse + */ + .Mouse_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = MOUSE_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 1, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_BootSubclass, .Protocol = HID_CSCP_MouseBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR}, + .Mouse_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(MouseReport)}, + .Mouse_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | MOUSE_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MOUSE_EPSIZE, .PollingIntervalMS = 0x0A}, #endif #ifdef SHARED_EP_ENABLE - /* - * Shared - */ - .Shared_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = SHARED_INTERFACE, - .AlternateSetting = 0x00, - .TotalEndpoints = 1, - .Class = HID_CSCP_HIDClass, -#ifdef KEYBOARD_SHARED_EP - .SubClass = HID_CSCP_BootSubclass, - .Protocol = HID_CSCP_KeyboardBootProtocol, -#else - .SubClass = HID_CSCP_NonBootSubclass, - .Protocol = HID_CSCP_NonBootProtocol, -#endif - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Shared_HID = { - .Header = { - .Size = sizeof(USB_HID_Descriptor_HID_t), - .Type = HID_DTYPE_HID - }, - .HIDSpec = VERSION_BCD(1, 1, 1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(SharedReport) - }, - .Shared_INEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_IN | SHARED_IN_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = SHARED_EPSIZE, - .PollingIntervalMS = 0x0A - }, + /* + * Shared + */ + .Shared_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + .InterfaceNumber = SHARED_INTERFACE, + .AlternateSetting = 0x00, + .TotalEndpoints = 1, + .Class = HID_CSCP_HIDClass, +# ifdef KEYBOARD_SHARED_EP + .SubClass = HID_CSCP_BootSubclass, + .Protocol = HID_CSCP_KeyboardBootProtocol, +# else + .SubClass = HID_CSCP_NonBootSubclass, + .Protocol = HID_CSCP_NonBootProtocol, +# endif + .InterfaceStrIndex = NO_DESCRIPTOR}, + .Shared_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(SharedReport)}, + .Shared_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | SHARED_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = SHARED_EPSIZE, .PollingIntervalMS = 0x0A}, #endif #ifdef RAW_ENABLE - /* - * Raw HID - */ - .Raw_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = RAW_INTERFACE, - .AlternateSetting = 0x00, - .TotalEndpoints = 2, - .Class = HID_CSCP_HIDClass, - .SubClass = HID_CSCP_NonBootSubclass, - .Protocol = HID_CSCP_NonBootProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Raw_HID = { - .Header = { - .Size = sizeof(USB_HID_Descriptor_HID_t), - .Type = HID_DTYPE_HID - }, - .HIDSpec = VERSION_BCD(1, 1, 1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(RawReport) - }, - .Raw_INEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_IN | RAW_IN_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = RAW_EPSIZE, - .PollingIntervalMS = 0x01 - }, - .Raw_OUTEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_OUT | RAW_OUT_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = RAW_EPSIZE, - .PollingIntervalMS = 0x01 - }, + /* + * Raw HID + */ + .Raw_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = RAW_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 2, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_NonBootSubclass, .Protocol = HID_CSCP_NonBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR}, + .Raw_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(RawReport)}, + .Raw_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | RAW_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = RAW_EPSIZE, .PollingIntervalMS = 0x01}, + .Raw_OUTEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_OUT | RAW_OUT_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = RAW_EPSIZE, .PollingIntervalMS = 0x01}, #endif #ifdef CONSOLE_ENABLE - /* - * Console - */ - .Console_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = CONSOLE_INTERFACE, - .AlternateSetting = 0x00, - .TotalEndpoints = 2, - .Class = HID_CSCP_HIDClass, - .SubClass = HID_CSCP_NonBootSubclass, - .Protocol = HID_CSCP_NonBootProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Console_HID = { - .Header = { - .Size = sizeof(USB_HID_Descriptor_HID_t), - .Type = HID_DTYPE_HID - }, - .HIDSpec = VERSION_BCD(1, 1, 1), - .CountryCode = 0x00, - .TotalReportDescriptors = 1, - .HIDReportType = HID_DTYPE_Report, - .HIDReportLength = sizeof(ConsoleReport) - }, - .Console_INEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_IN | CONSOLE_IN_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CONSOLE_EPSIZE, - .PollingIntervalMS = 0x01 - }, - .Console_OUTEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = (ENDPOINT_DIR_OUT | CONSOLE_OUT_EPNUM), - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CONSOLE_EPSIZE, - .PollingIntervalMS = 0x01 - }, + /* + * Console + */ + .Console_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CONSOLE_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 2, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_NonBootSubclass, .Protocol = HID_CSCP_NonBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR}, + .Console_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(ConsoleReport)}, + .Console_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | CONSOLE_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CONSOLE_EPSIZE, .PollingIntervalMS = 0x01}, + .Console_OUTEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_OUT | CONSOLE_OUT_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CONSOLE_EPSIZE, .PollingIntervalMS = 0x01}, #endif #ifdef MIDI_ENABLE - /* - * MIDI - */ - .Audio_Interface_Association = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_Association_t), - .Type = DTYPE_InterfaceAssociation - }, - .FirstInterfaceIndex = AC_INTERFACE, - .TotalInterfaces = 2, - .Class = AUDIO_CSCP_AudioClass, - .SubClass = AUDIO_CSCP_ControlSubclass, - .Protocol = AUDIO_CSCP_ControlProtocol, - .IADStrIndex = NO_DESCRIPTOR, - }, - .Audio_ControlInterface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - - .InterfaceNumber = AC_INTERFACE, - .AlternateSetting = 0, - .TotalEndpoints = 0, - .Class = AUDIO_CSCP_AudioClass, - .SubClass = AUDIO_CSCP_ControlSubclass, - .Protocol = AUDIO_CSCP_ControlProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Audio_ControlInterface_SPC = { - .Header = { - .Size = sizeof(USB_Audio_Descriptor_Interface_AC_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_Header, - .ACSpecification = VERSION_BCD(1, 0, 0), - .TotalLength = sizeof(USB_Audio_Descriptor_Interface_AC_t), - .InCollection = 1, - .InterfaceNumber = AS_INTERFACE, - }, - .Audio_StreamInterface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - - .InterfaceNumber = AS_INTERFACE, - .AlternateSetting = 0, - .TotalEndpoints = 2, - .Class = AUDIO_CSCP_AudioClass, - .SubClass = AUDIO_CSCP_MIDIStreamingSubclass, - .Protocol = AUDIO_CSCP_StreamingProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .Audio_StreamInterface_SPC = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_AudioInterface_AS_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_General, - .AudioSpecification = VERSION_BCD(1, 0, 0), - .TotalLength = offsetof(USB_Descriptor_Configuration_t, MIDI_Out_Jack_Endpoint_SPC) + sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t) - offsetof(USB_Descriptor_Configuration_t, Audio_StreamInterface_SPC) - }, - .MIDI_In_Jack_Emb = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_InputJack_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, - .JackType = MIDI_JACKTYPE_Embedded, - .JackID = 0x01, - .JackStrIndex = NO_DESCRIPTOR - }, - .MIDI_In_Jack_Ext = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_InputJack_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, - .JackType = MIDI_JACKTYPE_External, - .JackID = 0x02, - .JackStrIndex = NO_DESCRIPTOR - }, - .MIDI_Out_Jack_Emb = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, - .JackType = MIDI_JACKTYPE_Embedded, - .JackID = 0x03, - .NumberOfPins = 1, - .SourceJackID = {0x02}, - .SourcePinID = {0x01}, - .JackStrIndex = NO_DESCRIPTOR - }, - .MIDI_Out_Jack_Ext = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), - .Type = AUDIO_DTYPE_CSInterface - }, - .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, - .JackType = MIDI_JACKTYPE_External, - .JackID = 0x04, - .NumberOfPins = 1, - .SourceJackID = {0x01}, - .SourcePinID = {0x01}, - .JackStrIndex = NO_DESCRIPTOR - }, - .MIDI_In_Jack_Endpoint = { - .Endpoint = { - .Header = { - .Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = MIDI_STREAM_OUT_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = MIDI_STREAM_EPSIZE, - .PollingIntervalMS = 0x05 - }, - - .Refresh = 0, - .SyncEndpointNumber = 0 - }, - .MIDI_In_Jack_Endpoint_SPC = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), - .Type = AUDIO_DSUBTYPE_CSEndpoint_General - }, - .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, - .TotalEmbeddedJacks = 0x01, - .AssociatedJackID = {0x01} - }, - .MIDI_Out_Jack_Endpoint = { - .Endpoint = { - .Header = { - .Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = MIDI_STREAM_IN_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = MIDI_STREAM_EPSIZE, - .PollingIntervalMS = 0x05 - }, - - .Refresh = 0, - .SyncEndpointNumber = 0 - }, - .MIDI_Out_Jack_Endpoint_SPC = { - .Header = { - .Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), - .Type = AUDIO_DTYPE_CSEndpoint - }, - .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, - .TotalEmbeddedJacks = 0x01, - .AssociatedJackID = {0x03} - }, + /* + * MIDI + */ + .Audio_Interface_Association = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation}, + .FirstInterfaceIndex = AC_INTERFACE, + .TotalInterfaces = 2, + .Class = AUDIO_CSCP_AudioClass, + .SubClass = AUDIO_CSCP_ControlSubclass, + .Protocol = AUDIO_CSCP_ControlProtocol, + .IADStrIndex = NO_DESCRIPTOR, + }, + .Audio_ControlInterface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = AC_INTERFACE, + .AlternateSetting = 0, + .TotalEndpoints = 0, + .Class = AUDIO_CSCP_AudioClass, + .SubClass = AUDIO_CSCP_ControlSubclass, + .Protocol = AUDIO_CSCP_ControlProtocol, + .InterfaceStrIndex = NO_DESCRIPTOR}, + .Audio_ControlInterface_SPC = + { + .Header = {.Size = sizeof(USB_Audio_Descriptor_Interface_AC_t), .Type = AUDIO_DTYPE_CSInterface}, + .Subtype = AUDIO_DSUBTYPE_CSInterface_Header, + .ACSpecification = VERSION_BCD(1, 0, 0), + .TotalLength = sizeof(USB_Audio_Descriptor_Interface_AC_t), + .InCollection = 1, + .InterfaceNumber = AS_INTERFACE, + }, + .Audio_StreamInterface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = AS_INTERFACE, + .AlternateSetting = 0, + .TotalEndpoints = 2, + .Class = AUDIO_CSCP_AudioClass, + .SubClass = AUDIO_CSCP_MIDIStreamingSubclass, + .Protocol = AUDIO_CSCP_StreamingProtocol, + .InterfaceStrIndex = NO_DESCRIPTOR}, + .Audio_StreamInterface_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_AudioInterface_AS_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_General, .AudioSpecification = VERSION_BCD(1, 0, 0), .TotalLength = offsetof(USB_Descriptor_Configuration_t, MIDI_Out_Jack_Endpoint_SPC) + sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t) - offsetof(USB_Descriptor_Configuration_t, Audio_StreamInterface_SPC)}, + .MIDI_In_Jack_Emb = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, .JackType = MIDI_JACKTYPE_Embedded, .JackID = 0x01, .JackStrIndex = NO_DESCRIPTOR}, + .MIDI_In_Jack_Ext = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, .JackType = MIDI_JACKTYPE_External, .JackID = 0x02, .JackStrIndex = NO_DESCRIPTOR}, + .MIDI_Out_Jack_Emb = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, .JackType = MIDI_JACKTYPE_Embedded, .JackID = 0x03, .NumberOfPins = 1, .SourceJackID = {0x02}, .SourcePinID = {0x01}, .JackStrIndex = NO_DESCRIPTOR}, + .MIDI_Out_Jack_Ext = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, .JackType = MIDI_JACKTYPE_External, .JackID = 0x04, .NumberOfPins = 1, .SourceJackID = {0x01}, .SourcePinID = {0x01}, .JackStrIndex = NO_DESCRIPTOR}, + .MIDI_In_Jack_Endpoint = {.Endpoint = {.Header = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint}, .EndpointAddress = MIDI_STREAM_OUT_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MIDI_STREAM_EPSIZE, .PollingIntervalMS = 0x05}, + + .Refresh = 0, + .SyncEndpointNumber = 0}, + .MIDI_In_Jack_Endpoint_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = AUDIO_DSUBTYPE_CSEndpoint_General}, .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, .TotalEmbeddedJacks = 0x01, .AssociatedJackID = {0x01}}, + .MIDI_Out_Jack_Endpoint = {.Endpoint = {.Header = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint}, .EndpointAddress = MIDI_STREAM_IN_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MIDI_STREAM_EPSIZE, .PollingIntervalMS = 0x05}, + + .Refresh = 0, + .SyncEndpointNumber = 0}, + .MIDI_Out_Jack_Endpoint_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = AUDIO_DTYPE_CSEndpoint}, .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, .TotalEmbeddedJacks = 0x01, .AssociatedJackID = {0x03}}, #endif #ifdef VIRTSER_ENABLE - /* - * Virtual Serial - */ - .CDC_Interface_Association = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_Association_t), - .Type = DTYPE_InterfaceAssociation - }, - .FirstInterfaceIndex = CCI_INTERFACE, - .TotalInterfaces = 2, - .Class = CDC_CSCP_CDCClass, - .SubClass = CDC_CSCP_ACMSubclass, - .Protocol = CDC_CSCP_ATCommandProtocol, - .IADStrIndex = NO_DESCRIPTOR, - }, - .CDC_CCI_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = CCI_INTERFACE, - .AlternateSetting = 0, - .TotalEndpoints = 1, - .Class = CDC_CSCP_CDCClass, - .SubClass = CDC_CSCP_ACMSubclass, - .Protocol = CDC_CSCP_ATCommandProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .CDC_Functional_Header = { - .Header = { - .Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), - .Type = CDC_DTYPE_CSInterface - }, - .Subtype = 0x00, - .CDCSpecification = VERSION_BCD(1, 1, 0), - }, - .CDC_Functional_ACM = { - .Header = { - .Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), - .Type = CDC_DTYPE_CSInterface - }, - .Subtype = 0x02, - .Capabilities = 0x02, - }, - .CDC_Functional_Union = { - .Header = { - .Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), - .Type = CDC_DTYPE_CSInterface - }, - .Subtype = 0x06, - .MasterInterfaceNumber = CCI_INTERFACE, - .SlaveInterfaceNumber = CDI_INTERFACE, - }, - .CDC_NotificationEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = CDC_NOTIFICATION_EPADDR, - .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_NOTIFICATION_EPSIZE, - .PollingIntervalMS = 0xFF - }, - .CDC_DCI_Interface = { - .Header = { - .Size = sizeof(USB_Descriptor_Interface_t), - .Type = DTYPE_Interface - }, - .InterfaceNumber = CDI_INTERFACE, - .AlternateSetting = 0, - .TotalEndpoints = 2, - .Class = CDC_CSCP_CDCDataClass, - .SubClass = CDC_CSCP_NoDataSubclass, - .Protocol = CDC_CSCP_NoDataProtocol, - .InterfaceStrIndex = NO_DESCRIPTOR - }, - .CDC_DataOutEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = CDC_OUT_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_EPSIZE, - .PollingIntervalMS = 0x05 - }, - .CDC_DataInEndpoint = { - .Header = { - .Size = sizeof(USB_Descriptor_Endpoint_t), - .Type = DTYPE_Endpoint - }, - .EndpointAddress = CDC_IN_EPADDR, - .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), - .EndpointSize = CDC_EPSIZE, - .PollingIntervalMS = 0x05 - }, + /* + * Virtual Serial + */ + .CDC_Interface_Association = + { + .Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation}, + .FirstInterfaceIndex = CCI_INTERFACE, + .TotalInterfaces = 2, + .Class = CDC_CSCP_CDCClass, + .SubClass = CDC_CSCP_ACMSubclass, + .Protocol = CDC_CSCP_ATCommandProtocol, + .IADStrIndex = NO_DESCRIPTOR, + }, + .CDC_CCI_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CCI_INTERFACE, .AlternateSetting = 0, .TotalEndpoints = 1, .Class = CDC_CSCP_CDCClass, .SubClass = CDC_CSCP_ACMSubclass, .Protocol = CDC_CSCP_ATCommandProtocol, .InterfaceStrIndex = NO_DESCRIPTOR}, + .CDC_Functional_Header = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = 0x00, + .CDCSpecification = VERSION_BCD(1, 1, 0), + }, + .CDC_Functional_ACM = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = 0x02, + .Capabilities = 0x02, + }, + .CDC_Functional_Union = + { + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface}, + .Subtype = 0x06, + .MasterInterfaceNumber = CCI_INTERFACE, + .SlaveInterfaceNumber = CDI_INTERFACE, + }, + .CDC_NotificationEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_NOTIFICATION_EPADDR, .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_NOTIFICATION_EPSIZE, .PollingIntervalMS = 0xFF}, + .CDC_DCI_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CDI_INTERFACE, .AlternateSetting = 0, .TotalEndpoints = 2, .Class = CDC_CSCP_CDCDataClass, .SubClass = CDC_CSCP_NoDataSubclass, .Protocol = CDC_CSCP_NoDataProtocol, .InterfaceStrIndex = NO_DESCRIPTOR}, + .CDC_DataOutEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_OUT_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_EPSIZE, .PollingIntervalMS = 0x05}, + .CDC_DataInEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_IN_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_EPSIZE, .PollingIntervalMS = 0x05}, #endif }; /* * String descriptors */ -const USB_Descriptor_String_t PROGMEM LanguageString = { - .Header = { - .Size = USB_STRING_LEN(1), - .Type = DTYPE_String - }, - .UnicodeString = {LANGUAGE_ID_ENG} -}; +const USB_Descriptor_String_t PROGMEM LanguageString = {.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String}, .UnicodeString = {LANGUAGE_ID_ENG}}; -const USB_Descriptor_String_t PROGMEM ManufacturerString = { - .Header = { - .Size = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1), // Subtract 1 for null terminator - .Type = DTYPE_String - }, - .UnicodeString = LSTR(MANUFACTURER) -}; +const USB_Descriptor_String_t PROGMEM ManufacturerString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1), // Subtract 1 for null terminator + .Type = DTYPE_String}, + .UnicodeString = LSTR(MANUFACTURER)}; -const USB_Descriptor_String_t PROGMEM ProductString = { - .Header = { - .Size = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1), // Subtract 1 for null terminator - .Type = DTYPE_String - }, - .UnicodeString = LSTR(PRODUCT) -}; +const USB_Descriptor_String_t PROGMEM ProductString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1), // Subtract 1 for null terminator + .Type = DTYPE_String}, + .UnicodeString = LSTR(PRODUCT)}; #ifndef SERIAL_NUMBER - #define SERIAL_NUMBER 0 +# define SERIAL_NUMBER 0 #endif -const USB_Descriptor_String_t PROGMEM SerialNumberString = { - .Header = { - .Size = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1), // Subtract 1 for null terminator - .Type = DTYPE_String - }, - .UnicodeString = LSTR(SERIAL_NUMBER) -}; +const USB_Descriptor_String_t PROGMEM SerialNumberString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1), // Subtract 1 for null terminator + .Type = DTYPE_String}, + .UnicodeString = LSTR(SERIAL_NUMBER)}; /** * This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" diff --git a/tmk_core/protocol/usb_descriptor.h b/tmk_core/protocol/usb_descriptor.h index d41956803..e922edc45 100644 --- a/tmk_core/protocol/usb_descriptor.h +++ b/tmk_core/protocol/usb_descriptor.h @@ -41,62 +41,62 @@ * Header file for Descriptors.c. */ #ifndef _DESCRIPTORS_H_ - #define _DESCRIPTORS_H_ +#define _DESCRIPTORS_H_ - #include <LUFA/Drivers/USB/USB.h> +#include <LUFA/Drivers/USB/USB.h> - #ifdef PROTOCOL_CHIBIOS - #include "hal.h" - #endif +#ifdef PROTOCOL_CHIBIOS +# include "hal.h" +#endif /* * USB descriptor structure */ typedef struct { - USB_Descriptor_Configuration_Header_t Config; + USB_Descriptor_Configuration_Header_t Config; - #ifndef KEYBOARD_SHARED_EP +#ifndef KEYBOARD_SHARED_EP // Keyboard HID Interface - USB_Descriptor_Interface_t Keyboard_Interface; - USB_HID_Descriptor_HID_t Keyboard_HID; - USB_Descriptor_Endpoint_t Keyboard_INEndpoint; - #endif + USB_Descriptor_Interface_t Keyboard_Interface; + USB_HID_Descriptor_HID_t Keyboard_HID; + USB_Descriptor_Endpoint_t Keyboard_INEndpoint; +#endif - #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) +#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) // Mouse HID Interface - USB_Descriptor_Interface_t Mouse_Interface; - USB_HID_Descriptor_HID_t Mouse_HID; - USB_Descriptor_Endpoint_t Mouse_INEndpoint; - #endif + USB_Descriptor_Interface_t Mouse_Interface; + USB_HID_Descriptor_HID_t Mouse_HID; + USB_Descriptor_Endpoint_t Mouse_INEndpoint; +#endif - #ifdef SHARED_EP_ENABLE +#ifdef SHARED_EP_ENABLE // Shared Interface - USB_Descriptor_Interface_t Shared_Interface; - USB_HID_Descriptor_HID_t Shared_HID; - USB_Descriptor_Endpoint_t Shared_INEndpoint; - #endif + USB_Descriptor_Interface_t Shared_Interface; + USB_HID_Descriptor_HID_t Shared_HID; + USB_Descriptor_Endpoint_t Shared_INEndpoint; +#endif - #ifdef RAW_ENABLE +#ifdef RAW_ENABLE // Raw HID Interface - USB_Descriptor_Interface_t Raw_Interface; - USB_HID_Descriptor_HID_t Raw_HID; - USB_Descriptor_Endpoint_t Raw_INEndpoint; - USB_Descriptor_Endpoint_t Raw_OUTEndpoint; - #endif + USB_Descriptor_Interface_t Raw_Interface; + USB_HID_Descriptor_HID_t Raw_HID; + USB_Descriptor_Endpoint_t Raw_INEndpoint; + USB_Descriptor_Endpoint_t Raw_OUTEndpoint; +#endif - #ifdef CONSOLE_ENABLE +#ifdef CONSOLE_ENABLE // Console HID Interface - USB_Descriptor_Interface_t Console_Interface; - USB_HID_Descriptor_HID_t Console_HID; - USB_Descriptor_Endpoint_t Console_INEndpoint; - USB_Descriptor_Endpoint_t Console_OUTEndpoint; - #endif - - #ifdef MIDI_ENABLE - USB_Descriptor_Interface_Association_t Audio_Interface_Association; + USB_Descriptor_Interface_t Console_Interface; + USB_HID_Descriptor_HID_t Console_HID; + USB_Descriptor_Endpoint_t Console_INEndpoint; + USB_Descriptor_Endpoint_t Console_OUTEndpoint; +#endif + +#ifdef MIDI_ENABLE + USB_Descriptor_Interface_Association_t Audio_Interface_Association; // MIDI Audio Control Interface - USB_Descriptor_Interface_t Audio_ControlInterface; - USB_Audio_Descriptor_Interface_AC_t Audio_ControlInterface_SPC; + USB_Descriptor_Interface_t Audio_ControlInterface; + USB_Audio_Descriptor_Interface_AC_t Audio_ControlInterface_SPC; // MIDI Audio Streaming Interface USB_Descriptor_Interface_t Audio_StreamInterface; USB_MIDI_Descriptor_AudioInterface_AS_t Audio_StreamInterface_SPC; @@ -108,146 +108,146 @@ typedef struct { USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_In_Jack_Endpoint_SPC; USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_Out_Jack_Endpoint; USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_Out_Jack_Endpoint_SPC; - #endif +#endif - #ifdef VIRTSER_ENABLE - USB_Descriptor_Interface_Association_t CDC_Interface_Association; +#ifdef VIRTSER_ENABLE + USB_Descriptor_Interface_Association_t CDC_Interface_Association; // CDC Control Interface - USB_Descriptor_Interface_t CDC_CCI_Interface; - USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header; - USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM; - USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union; - USB_Descriptor_Endpoint_t CDC_NotificationEndpoint; + USB_Descriptor_Interface_t CDC_CCI_Interface; + USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header; + USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM; + USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union; + USB_Descriptor_Endpoint_t CDC_NotificationEndpoint; // CDC Data Interface - USB_Descriptor_Interface_t CDC_DCI_Interface; - USB_Descriptor_Endpoint_t CDC_DataOutEndpoint; - USB_Descriptor_Endpoint_t CDC_DataInEndpoint; - #endif + USB_Descriptor_Interface_t CDC_DCI_Interface; + USB_Descriptor_Endpoint_t CDC_DataOutEndpoint; + USB_Descriptor_Endpoint_t CDC_DataInEndpoint; +#endif } USB_Descriptor_Configuration_t; /* * Interface indexes */ enum usb_interfaces { - #ifndef KEYBOARD_SHARED_EP +#ifndef KEYBOARD_SHARED_EP KEYBOARD_INTERFACE, - #else - #define KEYBOARD_INTERFACE SHARED_INTERFACE - #endif - - // It is important that the Raw HID interface is at a constant - // interface number, to support Linux/OSX platforms and chrome.hid - // If Raw HID is enabled, let it be always 1. - #ifdef RAW_ENABLE +#else +# define KEYBOARD_INTERFACE SHARED_INTERFACE +#endif + +// It is important that the Raw HID interface is at a constant +// interface number, to support Linux/OSX platforms and chrome.hid +// If Raw HID is enabled, let it be always 1. +#ifdef RAW_ENABLE RAW_INTERFACE, - #endif +#endif - #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) +#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) MOUSE_INTERFACE, - #endif +#endif - #ifdef SHARED_EP_ENABLE +#ifdef SHARED_EP_ENABLE SHARED_INTERFACE, - #endif +#endif - #ifdef CONSOLE_ENABLE +#ifdef CONSOLE_ENABLE CONSOLE_INTERFACE, - #endif +#endif - #ifdef MIDI_ENABLE +#ifdef MIDI_ENABLE AC_INTERFACE, AS_INTERFACE, - #endif +#endif - #ifdef VIRTSER_ENABLE +#ifdef VIRTSER_ENABLE CCI_INTERFACE, CDI_INTERFACE, - #endif +#endif TOTAL_INTERFACES }; - #define NEXT_EPNUM __COUNTER__ +#define NEXT_EPNUM __COUNTER__ /* * Endpoint numbers */ enum usb_endpoints { - __unused_epnum__ = NEXT_EPNUM, // Endpoint numbering starts at 1 + __unused_epnum__ = NEXT_EPNUM, // Endpoint numbering starts at 1 - #ifndef KEYBOARD_SHARED_EP +#ifndef KEYBOARD_SHARED_EP KEYBOARD_IN_EPNUM = NEXT_EPNUM, - #else - #define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM - #endif +#else +# define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM +#endif - #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) +#if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) MOUSE_IN_EPNUM = NEXT_EPNUM, - #else - #define MOUSE_IN_EPNUM SHARED_IN_EPNUM - #endif +#else +# define MOUSE_IN_EPNUM SHARED_IN_EPNUM +#endif - #ifdef RAW_ENABLE - RAW_IN_EPNUM = NEXT_EPNUM, +#ifdef RAW_ENABLE + RAW_IN_EPNUM = NEXT_EPNUM, RAW_OUT_EPNUM = NEXT_EPNUM, - #endif +#endif - #ifdef SHARED_EP_ENABLE +#ifdef SHARED_EP_ENABLE SHARED_IN_EPNUM = NEXT_EPNUM, - #endif +#endif - #ifdef CONSOLE_ENABLE +#ifdef CONSOLE_ENABLE CONSOLE_IN_EPNUM = NEXT_EPNUM, - #ifdef PROTOCOL_CHIBIOS +# ifdef PROTOCOL_CHIBIOS // ChibiOS has enough memory and descriptor to actually enable the endpoint // It could use the same endpoint numbers, as that's supported by ChibiOS // But the QMK code currently assumes that the endpoint numbers are different CONSOLE_OUT_EPNUM = NEXT_EPNUM, - #else - #define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM - #endif - #endif +# else +# define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM +# endif +#endif - #ifdef MIDI_ENABLE - MIDI_STREAM_IN_EPNUM = NEXT_EPNUM, +#ifdef MIDI_ENABLE + MIDI_STREAM_IN_EPNUM = NEXT_EPNUM, MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM, - #define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM) - #define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM) - #endif +# define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM) +# define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM) +#endif - #ifdef VIRTSER_ENABLE +#ifdef VIRTSER_ENABLE CDC_NOTIFICATION_EPNUM = NEXT_EPNUM, - CDC_IN_EPNUM = NEXT_EPNUM, - CDC_OUT_EPNUM = NEXT_EPNUM, - #define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM) - #define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM) - #define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM) - #endif + CDC_IN_EPNUM = NEXT_EPNUM, + CDC_OUT_EPNUM = NEXT_EPNUM, +# define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM) +# define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM) +# define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM) +#endif }; - #ifdef PROTOCOL_LUFA - // LUFA tells us total endpoints including control - #define MAX_ENDPOINTS (ENDPOINT_TOTAL_ENDPOINTS - 1) - #elif defined(PROTOCOL_CHIBIOS) - // ChibiOS gives us number of available user endpoints, not control - #define MAX_ENDPOINTS USB_MAX_ENDPOINTS - #endif - - // TODO - ARM_ATSAM - - #if (NEXT_EPNUM - 1) > MAX_ENDPOINTS - #error There are not enough available endpoints to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Console, NKRO, MIDI, Serial, Steno - #endif - - #define KEYBOARD_EPSIZE 8 - #define SHARED_EPSIZE 32 - #define MOUSE_EPSIZE 8 - #define RAW_EPSIZE 32 - #define CONSOLE_EPSIZE 32 - #define MIDI_STREAM_EPSIZE 64 - #define CDC_NOTIFICATION_EPSIZE 8 - #define CDC_EPSIZE 16 +#ifdef PROTOCOL_LUFA +// LUFA tells us total endpoints including control +# define MAX_ENDPOINTS (ENDPOINT_TOTAL_ENDPOINTS - 1) +#elif defined(PROTOCOL_CHIBIOS) +// ChibiOS gives us number of available user endpoints, not control +# define MAX_ENDPOINTS USB_MAX_ENDPOINTS +#endif + +// TODO - ARM_ATSAM + +#if (NEXT_EPNUM - 1) > MAX_ENDPOINTS +# error There are not enough available endpoints to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Console, NKRO, MIDI, Serial, Steno +#endif + +#define KEYBOARD_EPSIZE 8 +#define SHARED_EPSIZE 32 +#define MOUSE_EPSIZE 8 +#define RAW_EPSIZE 32 +#define CONSOLE_EPSIZE 32 +#define MIDI_STREAM_EPSIZE 64 +#define CDC_NOTIFICATION_EPSIZE 8 +#define CDC_EPSIZE 16 uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const void** const DescriptorAddress); #endif diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index 86c2188c8..f8322d94a 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c @@ -21,19 +21,16 @@ #include "uart.h" #include "debug.h" - #define UART_BAUD_RATE 115200 - /* This is from main.c of USBaspLoader */ -static void initForUsbConnectivity(void) -{ +static void initForUsbConnectivity(void) { uint8_t i = 0; usbInit(); /* enforce USB re-enumerate: */ - usbDeviceDisconnect(); /* do this while interrupts are disabled */ - while(--i){ /* fake USB disconnect for > 250 ms */ + usbDeviceDisconnect(); /* do this while interrupts are disabled */ + while (--i) { /* fake USB disconnect for > 250 ms */ wdt_reset(); _delay_ms(1); } @@ -41,8 +38,7 @@ static void initForUsbConnectivity(void) sei(); } -int main(void) -{ +int main(void) { bool suspended = false; #if USB_COUNT_SOF uint16_t last_timer = timer_read(); @@ -68,26 +64,26 @@ int main(void) while (1) { #if USB_COUNT_SOF if (usbSofCount != 0) { - suspended = false; + suspended = false; usbSofCount = 0; - last_timer = timer_read(); + last_timer = timer_read(); } else { // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) if (timer_elapsed(last_timer) > 5) { suspended = true; -/* - uart_putchar('S'); - _delay_ms(1); - cli(); - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - sleep_enable(); - sleep_bod_disable(); - sei(); - sleep_cpu(); - sleep_disable(); - _delay_ms(10); - uart_putchar('W'); -*/ + /* + uart_putchar('S'); + _delay_ms(1); + cli(); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + sleep_bod_disable(); + sei(); + sleep_cpu(); + sleep_disable(); + _delay_ms(10); + uart_putchar('W'); + */ } } #endif diff --git a/tmk_core/protocol/vusb/sendchar_usart.c b/tmk_core/protocol/vusb/sendchar_usart.c index 8d24f87d1..42bd9ee36 100644 --- a/tmk_core/protocol/vusb/sendchar_usart.c +++ b/tmk_core/protocol/vusb/sendchar_usart.c @@ -6,18 +6,14 @@ #include "oddebug.h" #include "sendchar.h" - #if DEBUG_LEVEL > 0 /* from oddebug.c */ -int8_t sendchar(uint8_t c) -{ - while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */ +int8_t sendchar(uint8_t c) { + while (!(ODDBG_USR & (1 << ODDBG_UDRE))) + ; /* wait for data register empty */ ODDBG_UDR = c; return 1; } #else -int8_t sendchar(uint8_t c) -{ - return 1; -} +int8_t sendchar(uint8_t c) { return 1; } #endif diff --git a/tmk_core/protocol/vusb/usbdrv/oddebug.c b/tmk_core/protocol/vusb/usbdrv/oddebug.c index 945457c1f..bcd28ff01 100644 --- a/tmk_core/protocol/vusb/usbdrv/oddebug.c +++ b/tmk_core/protocol/vusb/usbdrv/oddebug.c @@ -12,34 +12,30 @@ #if DEBUG_LEVEL > 0 -#warning "Never compile production devices with debugging enabled" +# warning "Never compile production devices with debugging enabled" -static void uartPutc(char c) -{ - while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */ +static void uartPutc(char c) { + while (!(ODDBG_USR & (1 << ODDBG_UDRE))) + ; /* wait for data register empty */ ODDBG_UDR = c; } -static uchar hexAscii(uchar h) -{ +static uchar hexAscii(uchar h) { h &= 0xf; - if(h >= 10) - h += 'a' - (uchar)10 - '0'; + if (h >= 10) h += 'a' - (uchar)10 - '0'; h += '0'; return h; } -static void printHex(uchar c) -{ +static void printHex(uchar c) { uartPutc(hexAscii(c >> 4)); uartPutc(hexAscii(c)); } -void odDebug(uchar prefix, uchar *data, uchar len) -{ +void odDebug(uchar prefix, uchar *data, uchar len) { printHex(prefix); uartPutc(':'); - while(len--){ + while (len--) { uartPutc(' '); printHex(*data++); } diff --git a/tmk_core/protocol/vusb/usbdrv/oddebug.h b/tmk_core/protocol/vusb/usbdrv/oddebug.h index d61309daa..f93f33879 100644 --- a/tmk_core/protocol/vusb/usbdrv/oddebug.h +++ b/tmk_core/protocol/vusb/usbdrv/oddebug.h @@ -23,39 +23,38 @@ A debug log consists of a label ('prefix') to indicate which debug log created the output and a memory block to dump in hex ('data' and 'len'). */ - #ifndef F_CPU -# define F_CPU 12000000 /* 12 MHz */ +# define F_CPU 12000000 /* 12 MHz */ #endif /* make sure we have the UART defines: */ #include "usbportability.h" #ifndef uchar -# define uchar unsigned char +# define uchar unsigned char #endif #if DEBUG_LEVEL > 0 && !(defined TXEN || defined TXEN0) /* no UART in device */ -# warning "Debugging disabled because device has no UART" -# undef DEBUG_LEVEL +# warning "Debugging disabled because device has no UART" +# undef DEBUG_LEVEL #endif #ifndef DEBUG_LEVEL -# define DEBUG_LEVEL 0 +# define DEBUG_LEVEL 0 #endif /* ------------------------------------------------------------------------- */ #if DEBUG_LEVEL > 0 -# define DBG1(prefix, data, len) odDebug(prefix, data, len) +# define DBG1(prefix, data, len) odDebug(prefix, data, len) #else -# define DBG1(prefix, data, len) +# define DBG1(prefix, data, len) #endif #if DEBUG_LEVEL > 1 -# define DBG2(prefix, data, len) odDebug(prefix, data, len) +# define DBG2(prefix, data, len) odDebug(prefix, data, len) #else -# define DBG2(prefix, data, len) +# define DBG2(prefix, data, len) #endif /* ------------------------------------------------------------------------- */ @@ -65,57 +64,56 @@ extern void odDebug(uchar prefix, uchar *data, uchar len); /* Try to find our control registers; ATMEL likes to rename these */ -#if defined UBRR -# define ODDBG_UBRR UBRR -#elif defined UBRRL -# define ODDBG_UBRR UBRRL -#elif defined UBRR0 -# define ODDBG_UBRR UBRR0 -#elif defined UBRR0L -# define ODDBG_UBRR UBRR0L -#endif - -#if defined UCR -# define ODDBG_UCR UCR -#elif defined UCSRB -# define ODDBG_UCR UCSRB -#elif defined UCSR0B -# define ODDBG_UCR UCSR0B -#endif - -#if defined TXEN -# define ODDBG_TXEN TXEN -#else -# define ODDBG_TXEN TXEN0 -#endif - -#if defined USR -# define ODDBG_USR USR -#elif defined UCSRA -# define ODDBG_USR UCSRA -#elif defined UCSR0A -# define ODDBG_USR UCSR0A -#endif - -#if defined UDRE -# define ODDBG_UDRE UDRE -#else -# define ODDBG_UDRE UDRE0 -#endif - -#if defined UDR -# define ODDBG_UDR UDR -#elif defined UDR0 -# define ODDBG_UDR UDR0 -#endif - -static inline void odDebugInit(void) -{ - ODDBG_UCR |= (1<<ODDBG_TXEN); +# if defined UBRR +# define ODDBG_UBRR UBRR +# elif defined UBRRL +# define ODDBG_UBRR UBRRL +# elif defined UBRR0 +# define ODDBG_UBRR UBRR0 +# elif defined UBRR0L +# define ODDBG_UBRR UBRR0L +# endif + +# if defined UCR +# define ODDBG_UCR UCR +# elif defined UCSRB +# define ODDBG_UCR UCSRB +# elif defined UCSR0B +# define ODDBG_UCR UCSR0B +# endif + +# if defined TXEN +# define ODDBG_TXEN TXEN +# else +# define ODDBG_TXEN TXEN0 +# endif + +# if defined USR +# define ODDBG_USR USR +# elif defined UCSRA +# define ODDBG_USR UCSRA +# elif defined UCSR0A +# define ODDBG_USR UCSR0A +# endif + +# if defined UDRE +# define ODDBG_UDRE UDRE +# else +# define ODDBG_UDRE UDRE0 +# endif + +# if defined UDR +# define ODDBG_UDR UDR +# elif defined UDR0 +# define ODDBG_UDR UDR0 +# endif + +static inline void odDebugInit(void) { + ODDBG_UCR |= (1 << ODDBG_TXEN); ODDBG_UBRR = F_CPU / (19200 * 16L) - 1; } #else -# define odDebugInit() +# define odDebugInit() #endif /* ------------------------------------------------------------------------- */ diff --git a/tmk_core/protocol/vusb/usbdrv/usbconfig-prototype.h b/tmk_core/protocol/vusb/usbdrv/usbconfig-prototype.h index 847710e2a..020ea5147 100644 --- a/tmk_core/protocol/vusb/usbdrv/usbconfig-prototype.h +++ b/tmk_core/protocol/vusb/usbdrv/usbconfig-prototype.h @@ -26,15 +26,15 @@ section at the end of this file). /* ---------------------------- Hardware Config ---------------------------- */ -#define USB_CFG_IOPORTNAME D +#define USB_CFG_IOPORTNAME D /* This is the port where the USB bus is connected. When you configure it to * "B", the registers PORTB, PINB and DDRB will be used. */ -#define USB_CFG_DMINUS_BIT 4 +#define USB_CFG_DMINUS_BIT 4 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. * This may be any bit in the port. */ -#define USB_CFG_DPLUS_BIT 2 +#define USB_CFG_DPLUS_BIT 2 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected. * This may be any bit in the port. Please note that D+ must also be connected * to interrupt pin INT0! [You can also use other interrupts, see section @@ -43,7 +43,7 @@ section at the end of this file). * interrupt, the USB interrupt will also be triggered at Start-Of-Frame * markers every millisecond.] */ -#define USB_CFG_CLOCK_KHZ (F_CPU/1000) +#define USB_CFG_CLOCK_KHZ (F_CPU / 1000) /* Clock rate of the AVR in kHz. Legal values are 12000, 12800, 15000, 16000, * 16500, 18000 and 20000. The 12.8 MHz and 16.5 MHz versions of the code * require no crystal, they tolerate +/- 1% deviation from the nominal @@ -52,7 +52,7 @@ section at the end of this file). * Since F_CPU should be defined to your actual clock rate anyway, you should * not need to modify this setting. */ -#define USB_CFG_CHECK_CRC 0 +#define USB_CFG_CHECK_CRC 0 /* Define this to 1 if you want that the driver checks integrity of incoming * data packets (CRC checks). CRC checks cost quite a bit of code size and are * currently only available for 18 MHz crystal clock. You must choose @@ -75,18 +75,18 @@ section at the end of this file). /* --------------------------- Functional Range ---------------------------- */ -#define USB_CFG_HAVE_INTRIN_ENDPOINT 0 +#define USB_CFG_HAVE_INTRIN_ENDPOINT 0 /* Define this to 1 if you want to compile a version with two endpoints: The * default control endpoint 0 and an interrupt-in endpoint (any other endpoint * number). */ -#define USB_CFG_HAVE_INTRIN_ENDPOINT3 0 +#define USB_CFG_HAVE_INTRIN_ENDPOINT3 0 /* Define this to 1 if you want to compile a version with three endpoints: The * default control endpoint 0, an interrupt-in endpoint 3 (or the number * configured below) and a catch-all default interrupt-in endpoint as above. * You must also define USB_CFG_HAVE_INTRIN_ENDPOINT to 1 for this feature. */ -#define USB_CFG_EP3_NUMBER 3 +#define USB_CFG_EP3_NUMBER 3 /* If the so-called endpoint 3 is used, it can now be configured to any other * endpoint number (except 0) with this macro. Default if undefined is 3. */ @@ -96,13 +96,13 @@ section at the end of this file). * Since the token is toggled BEFORE sending any data, the first packet is * sent with the oposite value of this configuration! */ -#define USB_CFG_IMPLEMENT_HALT 0 +#define USB_CFG_IMPLEMENT_HALT 0 /* Define this to 1 if you also want to implement the ENDPOINT_HALT feature * for endpoint 1 (interrupt endpoint). Although you may not need this feature, * it is required by the standard. We have made it a config option because it * bloats the code considerably. */ -#define USB_CFG_SUPPRESS_INTR_CODE 0 +#define USB_CFG_SUPPRESS_INTR_CODE 0 /* Define this to 1 if you want to declare interrupt-in endpoints, but don't * want to send any data over them. If this macro is defined to 1, functions * usbSetInterrupt() and usbSetInterrupt3() are omitted. This is useful if @@ -110,48 +110,48 @@ section at the end of this file). * (e.g. HID), but never want to send any data. This option saves a couple * of bytes in flash memory and the transmit buffers in RAM. */ -#define USB_CFG_INTR_POLL_INTERVAL 10 +#define USB_CFG_INTR_POLL_INTERVAL 10 /* If you compile a version with endpoint 1 (interrupt-in), this is the poll * interval. The value is in milliseconds and must not be less than 10 ms for * low speed devices. */ -#define USB_CFG_IS_SELF_POWERED 0 +#define USB_CFG_IS_SELF_POWERED 0 /* Define this to 1 if the device has its own power supply. Set it to 0 if the * device is powered from the USB bus. */ -#define USB_CFG_MAX_BUS_POWER 100 +#define USB_CFG_MAX_BUS_POWER 100 /* Set this variable to the maximum USB bus power consumption of your device. * The value is in milliamperes. [It will be divided by two since USB * communicates power requirements in units of 2 mA.] */ -#define USB_CFG_IMPLEMENT_FN_WRITE 0 +#define USB_CFG_IMPLEMENT_FN_WRITE 0 /* Set this to 1 if you want usbFunctionWrite() to be called for control-out * transfers. Set it to 0 if you don't need it and want to save a couple of * bytes. */ -#define USB_CFG_IMPLEMENT_FN_READ 0 +#define USB_CFG_IMPLEMENT_FN_READ 0 /* Set this to 1 if you need to send control replies which are generated * "on the fly" when usbFunctionRead() is called. If you only want to send * data from a static buffer, set it to 0 and return the data from * usbFunctionSetup(). This saves a couple of bytes. */ -#define USB_CFG_IMPLEMENT_FN_WRITEOUT 0 +#define USB_CFG_IMPLEMENT_FN_WRITEOUT 0 /* Define this to 1 if you want to use interrupt-out (or bulk out) endpoints. * You must implement the function usbFunctionWriteOut() which receives all * interrupt/bulk data sent to any endpoint other than 0. The endpoint number * can be found in 'usbRxToken'. */ -#define USB_CFG_HAVE_FLOWCONTROL 0 +#define USB_CFG_HAVE_FLOWCONTROL 0 /* Define this to 1 if you want flowcontrol over USB data. See the definition * of the macros usbDisableAllRequests() and usbEnableAllRequests() in * usbdrv.h. */ -#define USB_CFG_DRIVER_FLASH_PAGE 0 +#define USB_CFG_DRIVER_FLASH_PAGE 0 /* If the device has more than 64 kBytes of flash, define this to the 64 k page * where the driver's constants (descriptors) are located. Or in other words: * Define this to 1 for boot loaders on the ATMega128. */ -#define USB_CFG_LONG_TRANSFERS 0 +#define USB_CFG_LONG_TRANSFERS 0 /* Define this to 1 if you want to send/receive blocks of more than 254 bytes * in a single control-in or control-out transfer. Note that the capability * for long transfers increases the driver size. @@ -172,7 +172,7 @@ section at the end of this file). /* This macro (if defined) is executed when a USB SET_ADDRESS request was * received. */ -#define USB_COUNT_SOF 0 +#define USB_COUNT_SOF 0 /* define this macro to 1 if you need the global variable "usbSofCount" which * counts SOF packets. This feature requires that the hardware interrupt is * connected to D- instead of D+. @@ -196,7 +196,7 @@ section at the end of this file). * Please note that Start Of Frame detection works only if D- is wired to the * interrupt, not D+. THIS IS DIFFERENT THAN MOST EXAMPLES! */ -#define USB_CFG_CHECK_DATA_TOGGLING 0 +#define USB_CFG_CHECK_DATA_TOGGLING 0 /* define this macro to 1 if you want to filter out duplicate data packets * sent by the host. Duplicates occur only as a consequence of communication * errors, when the host does not receive an ACK. Please note that you need to @@ -204,11 +204,11 @@ section at the end of this file). * usbFunctionWrite(). Use the global usbCurrentDataToken and a static variable * for each control- and out-endpoint to check for duplicate packets. */ -#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH 0 +#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH 0 /* define this macro to 1 if you want the function usbMeasureFrameLength() * compiled in. This function can be used to calibrate the AVR's RC oscillator. */ -#define USB_USE_FAST_CRC 0 +#define USB_USE_FAST_CRC 0 /* The assembler module has two implementations for the CRC algorithm. One is * faster, the other is smaller. This CRC routine is only used for transmitted * messages where timing is not critical. The faster routine needs 31 cycles @@ -219,7 +219,7 @@ section at the end of this file). /* -------------------------- Device Description --------------------------- */ -#define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ +#define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ /* USB vendor ID for the device, low byte first. If you have registered your * own Vendor ID, define it here. Otherwise you may use one of obdev's free * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules! @@ -228,7 +228,7 @@ section at the end of this file). * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand * the implications! */ -#define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x05dc = 1500 */ +#define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x05dc = 1500 */ /* This is the ID of the product, low byte first. It is interpreted in the * scope of the vendor ID. If you have registered your own VID with usb.org * or if you have licensed a PID from somebody else, define it here. Otherwise @@ -239,10 +239,10 @@ section at the end of this file). * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand * the implications! */ -#define USB_CFG_DEVICE_VERSION 0x00, 0x01 +#define USB_CFG_DEVICE_VERSION 0x00, 0x01 /* Version number of the device: Minor number first, then major number. */ -#define USB_CFG_VENDOR_NAME 'o', 'b', 'd', 'e', 'v', '.', 'a', 't' +#define USB_CFG_VENDOR_NAME 'o', 'b', 'd', 'e', 'v', '.', 'a', 't' #define USB_CFG_VENDOR_NAME_LEN 8 /* These two values define the vendor name returned by the USB device. The name * must be given as a list of characters under single quotes. The characters @@ -252,7 +252,7 @@ section at the end of this file). * obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for * details. */ -#define USB_CFG_DEVICE_NAME 'T', 'e', 'm', 'p', 'l', 'a', 't', 'e' +#define USB_CFG_DEVICE_NAME 'T', 'e', 'm', 'p', 'l', 'a', 't', 'e' #define USB_CFG_DEVICE_NAME_LEN 8 /* Same as above for the device name. If you don't want a device name, undefine * the macros. See the file USB-IDs-for-free.txt before you assign a name if @@ -267,14 +267,14 @@ section at the end of this file). * to fine tune control over USB descriptors such as the string descriptor * for the serial number. */ -#define USB_CFG_DEVICE_CLASS 0xff /* set to 0 if deferred to interface */ -#define USB_CFG_DEVICE_SUBCLASS 0 +#define USB_CFG_DEVICE_CLASS 0xff /* set to 0 if deferred to interface */ +#define USB_CFG_DEVICE_SUBCLASS 0 /* See USB specification if you want to conform to an existing device class. * Class 0xff is "vendor specific". */ -#define USB_CFG_INTERFACE_CLASS 0 /* define class here if not at device level */ -#define USB_CFG_INTERFACE_SUBCLASS 0 -#define USB_CFG_INTERFACE_PROTOCOL 0 +#define USB_CFG_INTERFACE_CLASS 0 /* define class here if not at device level */ +#define USB_CFG_INTERFACE_SUBCLASS 0 +#define USB_CFG_INTERFACE_PROTOCOL 0 /* See USB specification if you want to conform to an existing device class or * protocol. The following classes must be set at interface level: * HID class is 3, no subclass and protocol required (but may be useful!) @@ -345,16 +345,16 @@ section at the end of this file). * }; */ -#define USB_CFG_DESCR_PROPS_DEVICE 0 -#define USB_CFG_DESCR_PROPS_CONFIGURATION 0 -#define USB_CFG_DESCR_PROPS_STRINGS 0 -#define USB_CFG_DESCR_PROPS_STRING_0 0 -#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 -#define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 -#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 -#define USB_CFG_DESCR_PROPS_HID 0 -#define USB_CFG_DESCR_PROPS_HID_REPORT 0 -#define USB_CFG_DESCR_PROPS_UNKNOWN 0 +#define USB_CFG_DESCR_PROPS_DEVICE 0 +#define USB_CFG_DESCR_PROPS_CONFIGURATION 0 +#define USB_CFG_DESCR_PROPS_STRINGS 0 +#define USB_CFG_DESCR_PROPS_STRING_0 0 +#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 +#define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 +#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 +#define USB_CFG_DESCR_PROPS_HID 0 +#define USB_CFG_DESCR_PROPS_HID_REPORT 0 +#define USB_CFG_DESCR_PROPS_UNKNOWN 0 /* ----------------------- Optional MCU Description ------------------------ */ diff --git a/tmk_core/protocol/vusb/usbdrv/usbdrv.c b/tmk_core/protocol/vusb/usbdrv/usbdrv.c index 30cdc9dcf..f69198b1b 100644 --- a/tmk_core/protocol/vusb/usbdrv/usbdrv.c +++ b/tmk_core/protocol/vusb/usbdrv/usbdrv.c @@ -21,36 +21,36 @@ documentation of the entire driver. /* ------------------------------------------------------------------------- */ /* raw USB registers / interface to assembler code: */ -uchar usbRxBuf[2*USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */ -uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */ -uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */ -uchar usbNewDeviceAddr; /* device ID which should be set after status phase */ -uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */ -volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */ -uchar usbCurrentTok; /* last token received or endpoint number for last OUT token if != 0 */ -uchar usbRxToken; /* token for data we received; or endpont number for last OUT */ -volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */ -uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */ +uchar usbRxBuf[2 * USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */ +uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */ +uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */ +uchar usbNewDeviceAddr; /* device ID which should be set after status phase */ +uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */ +volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */ +uchar usbCurrentTok; /* last token received or endpoint number for last OUT token if != 0 */ +uchar usbRxToken; /* token for data we received; or endpont number for last OUT */ +volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */ +uchar usbTxBuf[USB_BUFSIZE]; /* data to transmit with next IN, free if usbTxLen contains handshake token */ #if USB_COUNT_SOF -volatile uchar usbSofCount; /* incremented by assembler module every SOF */ +volatile uchar usbSofCount; /* incremented by assembler module every SOF */ #endif #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE -usbTxStatus_t usbTxStatus1; -# if USB_CFG_HAVE_INTRIN_ENDPOINT3 -usbTxStatus_t usbTxStatus3; -# endif +usbTxStatus_t usbTxStatus1; +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 +usbTxStatus_t usbTxStatus3; +# endif #endif #if USB_CFG_CHECK_DATA_TOGGLING -uchar usbCurrentDataToken;/* when we check data toggling to ignore duplicate packets */ +uchar usbCurrentDataToken; /* when we check data toggling to ignore duplicate packets */ #endif /* USB status registers / not shared with asm code */ -uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */ -static usbMsgLen_t usbMsgLen = USB_NO_MSG; /* remaining number of bytes */ -static uchar usbMsgFlags; /* flag values see below */ +uchar * usbMsgPtr; /* data to transmit next -- ROM or RAM address */ +static usbMsgLen_t usbMsgLen = USB_NO_MSG; /* remaining number of bytes */ +static uchar usbMsgFlags; /* flag values see below */ -#define USB_FLG_MSGPTR_IS_ROM (1<<6) -#define USB_FLG_USE_USER_RW (1<<7) +#define USB_FLG_MSGPTR_IS_ROM (1 << 6) +#define USB_FLG_USE_USER_RW (1 << 7) /* optimizing hints: @@ -64,196 +64,174 @@ optimizing hints: #if USB_CFG_DESCR_PROPS_STRINGS == 0 -#if USB_CFG_DESCR_PROPS_STRING_0 == 0 -#undef USB_CFG_DESCR_PROPS_STRING_0 -#define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0) -const PROGMEM char usbDescriptorString0[] = { /* language descriptor */ +# if USB_CFG_DESCR_PROPS_STRING_0 == 0 +# undef USB_CFG_DESCR_PROPS_STRING_0 +# define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0) +const PROGMEM char usbDescriptorString0[] = { + /* language descriptor */ 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */ 3, /* descriptor type */ 0x09, 0x04, /* language index (0x0409 = US-English) */ }; -#endif +# endif -#if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN -#undef USB_CFG_DESCR_PROPS_STRING_VENDOR -#define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor) -const PROGMEM int usbDescriptorStringVendor[] = { - USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN), - USB_CFG_VENDOR_NAME -}; -#endif +# if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN +# undef USB_CFG_DESCR_PROPS_STRING_VENDOR +# define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor) +const PROGMEM int usbDescriptorStringVendor[] = {USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN), USB_CFG_VENDOR_NAME}; +# endif -#if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN -#undef USB_CFG_DESCR_PROPS_STRING_PRODUCT -#define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice) -const PROGMEM int usbDescriptorStringDevice[] = { - USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN), - USB_CFG_DEVICE_NAME -}; -#endif +# if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN +# undef USB_CFG_DESCR_PROPS_STRING_PRODUCT +# define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice) +const PROGMEM int usbDescriptorStringDevice[] = {USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN), USB_CFG_DEVICE_NAME}; +# endif -#if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN -#undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER -#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber) -const PROGMEM int usbDescriptorStringSerialNumber[] = { - USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN), - USB_CFG_SERIAL_NUMBER -}; -#endif +# if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN +# undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER +# define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber) +const PROGMEM int usbDescriptorStringSerialNumber[] = {USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN), USB_CFG_SERIAL_NUMBER}; +# endif -#endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */ +#endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */ /* --------------------------- Device Descriptor --------------------------- */ #if USB_CFG_DESCR_PROPS_DEVICE == 0 -#undef USB_CFG_DESCR_PROPS_DEVICE -#define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice) -const PROGMEM char usbDescriptorDevice[] = { /* USB device descriptor */ - 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ - USBDESCR_DEVICE, /* descriptor type */ - 0x10, 0x01, /* USB version supported */ - USB_CFG_DEVICE_CLASS, - USB_CFG_DEVICE_SUBCLASS, - 0, /* protocol */ - 8, /* max packet size */ +# undef USB_CFG_DESCR_PROPS_DEVICE +# define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice) +const PROGMEM char usbDescriptorDevice[] = { + /* USB device descriptor */ + 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ + USBDESCR_DEVICE, /* descriptor type */ + 0x10, 0x01, /* USB version supported */ + USB_CFG_DEVICE_CLASS, USB_CFG_DEVICE_SUBCLASS, 0, /* protocol */ + 8, /* max packet size */ /* the following two casts affect the first byte of the constant only, but * that's sufficient to avoid a warning with the default values. */ - (char)USB_CFG_VENDOR_ID,/* 2 bytes */ - (char)USB_CFG_DEVICE_ID,/* 2 bytes */ - USB_CFG_DEVICE_VERSION, /* 2 bytes */ - USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */ - USB_CFG_DESCR_PROPS_STRING_PRODUCT != 0 ? 2 : 0, /* product string index */ - USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */ - 1, /* number of configurations */ + (char)USB_CFG_VENDOR_ID, /* 2 bytes */ + (char)USB_CFG_DEVICE_ID, /* 2 bytes */ + USB_CFG_DEVICE_VERSION, /* 2 bytes */ + USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */ + USB_CFG_DESCR_PROPS_STRING_PRODUCT != 0 ? 2 : 0, /* product string index */ + USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */ + 1, /* number of configurations */ }; #endif /* ----------------------- Configuration Descriptor ------------------------ */ #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0 -#undef USB_CFG_DESCR_PROPS_HID -#define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */ +# undef USB_CFG_DESCR_PROPS_HID +# define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */ #endif #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0 -#undef USB_CFG_DESCR_PROPS_CONFIGURATION -#define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration) -PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */ - 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ - USBDESCR_CONFIG, /* descriptor type */ - 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + - (USB_CFG_DESCR_PROPS_HID & 0xff), 0, - /* total length of data returned (including inlined descriptors) */ - 1, /* number of interfaces in this configuration */ - 1, /* index of this configuration */ - 0, /* configuration name string index */ -#if USB_CFG_IS_SELF_POWERED - (1 << 7) | USBATTR_SELFPOWER, /* attributes */ -#else - (1 << 7), /* attributes */ -#endif - USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */ -/* interface descriptor follows inline: */ - 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ - USBDESCR_INTERFACE, /* descriptor type */ - 0, /* index of this interface */ - 0, /* alternate setting for this interface */ - USB_CFG_HAVE_INTRIN_ENDPOINT + USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */ - USB_CFG_INTERFACE_CLASS, - USB_CFG_INTERFACE_SUBCLASS, - USB_CFG_INTERFACE_PROTOCOL, - 0, /* string index for interface */ -#if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */ - 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ - USBDESCR_HID, /* descriptor type: HID */ - 0x01, 0x01, /* BCD representation of HID version */ - 0x00, /* target country code */ - 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ - 0x22, /* descriptor type: report */ - USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */ -#endif -#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ - 7, /* sizeof(usbDescrEndpoint) */ - USBDESCR_ENDPOINT, /* descriptor type = endpoint */ - (char)0x81, /* IN endpoint number 1 */ - 0x03, /* attrib: Interrupt endpoint */ - 8, 0, /* maximum packet size */ - USB_CFG_INTR_POLL_INTERVAL, /* in ms */ -#endif -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */ - 7, /* sizeof(usbDescrEndpoint) */ - USBDESCR_ENDPOINT, /* descriptor type = endpoint */ +# undef USB_CFG_DESCR_PROPS_CONFIGURATION +# define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration) +PROGMEM char usbDescriptorConfiguration[] = { + /* USB configuration descriptor */ + 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ + USBDESCR_CONFIG, /* descriptor type */ + 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + (USB_CFG_DESCR_PROPS_HID & 0xff), 0, + /* total length of data returned (including inlined descriptors) */ + 1, /* number of interfaces in this configuration */ + 1, /* index of this configuration */ + 0, /* configuration name string index */ +# if USB_CFG_IS_SELF_POWERED + (1 << 7) | USBATTR_SELFPOWER, /* attributes */ +# else + (1 << 7), /* attributes */ +# endif + USB_CFG_MAX_BUS_POWER / 2, /* max USB current in 2mA units */ + /* interface descriptor follows inline: */ + 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ + USBDESCR_INTERFACE, /* descriptor type */ + 0, /* index of this interface */ + 0, /* alternate setting for this interface */ + USB_CFG_HAVE_INTRIN_ENDPOINT + USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */ + USB_CFG_INTERFACE_CLASS, USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */ +# if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */ + 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ + USBDESCR_HID, /* descriptor type: HID */ + 0x01, 0x01, /* BCD representation of HID version */ + 0x00, /* target country code */ + 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ + 0x22, /* descriptor type: report */ + USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */ +# endif +# if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ + 7, /* sizeof(usbDescrEndpoint) */ + USBDESCR_ENDPOINT, /* descriptor type = endpoint */ + (char)0x81, /* IN endpoint number 1 */ + 0x03, /* attrib: Interrupt endpoint */ + 8, 0, /* maximum packet size */ + USB_CFG_INTR_POLL_INTERVAL, /* in ms */ +# endif +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */ + 7, /* sizeof(usbDescrEndpoint) */ + USBDESCR_ENDPOINT, /* descriptor type = endpoint */ (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */ - 0x03, /* attrib: Interrupt endpoint */ - 8, 0, /* maximum packet size */ - USB_CFG_INTR_POLL_INTERVAL, /* in ms */ -#endif + 0x03, /* attrib: Interrupt endpoint */ + 8, 0, /* maximum packet size */ + USB_CFG_INTR_POLL_INTERVAL, /* in ms */ +# endif }; #endif /* ------------------------------------------------------------------------- */ -static inline void usbResetDataToggling(void) -{ +static inline void usbResetDataToggling(void) { #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE - USB_SET_DATATOKEN1(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */ -# if USB_CFG_HAVE_INTRIN_ENDPOINT3 - USB_SET_DATATOKEN3(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */ -# endif + USB_SET_DATATOKEN1(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */ +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 + USB_SET_DATATOKEN3(USB_INITIAL_DATATOKEN); /* reset data toggling for interrupt endpoint */ +# endif #endif } -static inline void usbResetStall(void) -{ +static inline void usbResetStall(void) { #if USB_CFG_IMPLEMENT_HALT && USB_CFG_HAVE_INTRIN_ENDPOINT - usbTxLen1 = USBPID_NAK; -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 - usbTxLen3 = USBPID_NAK; -#endif + usbTxLen1 = USBPID_NAK; +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 + usbTxLen3 = USBPID_NAK; +# endif #endif } /* ------------------------------------------------------------------------- */ #if !USB_CFG_SUPPRESS_INTR_CODE -#if USB_CFG_HAVE_INTRIN_ENDPOINT -static void usbGenericSetInterrupt(uchar *data, uchar len, usbTxStatus_t *txStatus) -{ -uchar *p; -char i; - -#if USB_CFG_IMPLEMENT_HALT - if(usbTxLen1 == USBPID_STALL) - return; -#endif - if(txStatus->len & 0x10){ /* packet buffer was empty */ +# if USB_CFG_HAVE_INTRIN_ENDPOINT +static void usbGenericSetInterrupt(uchar *data, uchar len, usbTxStatus_t *txStatus) { + uchar *p; + char i; + +# if USB_CFG_IMPLEMENT_HALT + if (usbTxLen1 == USBPID_STALL) return; +# endif + if (txStatus->len & 0x10) { /* packet buffer was empty */ txStatus->buffer[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ - }else{ + } else { txStatus->len = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ } p = txStatus->buffer + 1; i = len; - do{ /* if len == 0, we still copy 1 byte, but that's no problem */ + do { /* if len == 0, we still copy 1 byte, but that's no problem */ *p++ = *data++; - }while(--i > 0); /* loop control at the end is 2 bytes shorter than at beginning */ + } while (--i > 0); /* loop control at the end is 2 bytes shorter than at beginning */ usbCrc16Append(&txStatus->buffer[1], len); - txStatus->len = len + 4; /* len must be given including sync byte */ + txStatus->len = len + 4; /* len must be given including sync byte */ DBG2(0x21 + (((int)txStatus >> 3) & 3), txStatus->buffer, len + 3); } -USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len) -{ - usbGenericSetInterrupt(data, len, &usbTxStatus1); -} -#endif +USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len) { usbGenericSetInterrupt(data, len, &usbTxStatus1); } +# endif -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 -USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) -{ - usbGenericSetInterrupt(data, len, &usbTxStatus3); -} -#endif +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 +USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) { usbGenericSetInterrupt(data, len, &usbTxStatus3); } +# endif #endif /* USB_CFG_SUPPRESS_INTR_CODE */ /* ------------------ utilities for code following below ------------------- */ @@ -264,26 +242,58 @@ USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) * cases. */ #if USB_CFG_USE_SWITCH_STATEMENT -# define SWITCH_START(cmd) switch(cmd){{ -# define SWITCH_CASE(value) }break; case (value):{ -# define SWITCH_CASE2(v1,v2) }break; case (v1): case(v2):{ -# define SWITCH_CASE3(v1,v2,v3) }break; case (v1): case(v2): case(v3):{ -# define SWITCH_DEFAULT }break; default:{ -# define SWITCH_END }} +# define SWITCH_START(cmd) \ + switch (cmd) { \ + { +# define SWITCH_CASE(value) \ + } \ + break; \ + case (value): { +# define SWITCH_CASE2(v1, v2) \ + } \ + break; \ + case (v1): \ + case (v2): { +# define SWITCH_CASE3(v1, v2, v3) \ + } \ + break; \ + case (v1): \ + case (v2): \ + case (v3): { +# define SWITCH_DEFAULT \ + } \ + break; \ + default: { +# define SWITCH_END \ + } \ + } #else -# define SWITCH_START(cmd) {uchar _cmd = cmd; if(0){ -# define SWITCH_CASE(value) }else if(_cmd == (value)){ -# define SWITCH_CASE2(v1,v2) }else if(_cmd == (v1) || _cmd == (v2)){ -# define SWITCH_CASE3(v1,v2,v3) }else if(_cmd == (v1) || _cmd == (v2) || (_cmd == v3)){ -# define SWITCH_DEFAULT }else{ -# define SWITCH_END }} +# define SWITCH_START(cmd) \ + { \ + uchar _cmd = cmd; \ + if (0) { +# define SWITCH_CASE(value) \ + } \ + else if (_cmd == (value)) { +# define SWITCH_CASE2(v1, v2) \ + } \ + else if (_cmd == (v1) || _cmd == (v2)) { +# define SWITCH_CASE3(v1, v2, v3) \ + } \ + else if (_cmd == (v1) || _cmd == (v2) || (_cmd == v3)) { +# define SWITCH_DEFAULT \ + } \ + else { +# define SWITCH_END \ + } \ + } #endif #ifndef USB_RX_USER_HOOK -#define USB_RX_USER_HOOK(data, len) +# define USB_RX_USER_HOOK(data, len) #endif #ifndef USB_SET_ADDRESS_HOOK -#define USB_SET_ADDRESS_HOOK() +# define USB_SET_ADDRESS_HOOK() #endif /* ------------------------------------------------------------------------- */ @@ -293,62 +303,59 @@ USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) * This may cause problems with undefined symbols if compiled without * optimizing! */ -#define GET_DESCRIPTOR(cfgProp, staticName) \ - if(cfgProp){ \ - if((cfgProp) & USB_PROP_IS_RAM) \ - flags = 0; \ - if((cfgProp) & USB_PROP_IS_DYNAMIC){ \ - len = usbFunctionDescriptor(rq); \ - }else{ \ - len = USB_PROP_LENGTH(cfgProp); \ - usbMsgPtr = (uchar *)(staticName); \ - } \ +#define GET_DESCRIPTOR(cfgProp, staticName) \ + if (cfgProp) { \ + if ((cfgProp)&USB_PROP_IS_RAM) flags = 0; \ + if ((cfgProp)&USB_PROP_IS_DYNAMIC) { \ + len = usbFunctionDescriptor(rq); \ + } else { \ + len = USB_PROP_LENGTH(cfgProp); \ + usbMsgPtr = (uchar *)(staticName); \ + } \ } /* usbDriverDescriptor() is similar to usbFunctionDescriptor(), but used * internally for all types of descriptors. */ -static inline usbMsgLen_t usbDriverDescriptor(usbRequest_t *rq) -{ -usbMsgLen_t len = 0; -uchar flags = USB_FLG_MSGPTR_IS_ROM; +static inline usbMsgLen_t usbDriverDescriptor(usbRequest_t *rq) { + usbMsgLen_t len = 0; + uchar flags = USB_FLG_MSGPTR_IS_ROM; SWITCH_START(rq->wValue.bytes[1]) - SWITCH_CASE(USBDESCR_DEVICE) /* 1 */ - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice) - SWITCH_CASE(USBDESCR_CONFIG) /* 2 */ - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration) - SWITCH_CASE(USBDESCR_STRING) /* 3 */ + SWITCH_CASE(USBDESCR_DEVICE) /* 1 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice) + SWITCH_CASE(USBDESCR_CONFIG) /* 2 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration) + SWITCH_CASE(USBDESCR_STRING) /* 3 */ #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC - if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM) - flags = 0; + if (USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM) flags = 0; + len = usbFunctionDescriptor(rq); +#else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ + SWITCH_START(rq->wValue.bytes[0]) + SWITCH_CASE(0) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0) + SWITCH_CASE(1) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor) + SWITCH_CASE(2) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice) + SWITCH_CASE(3) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber) + SWITCH_DEFAULT + if (USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC) { len = usbFunctionDescriptor(rq); -#else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ - SWITCH_START(rq->wValue.bytes[0]) - SWITCH_CASE(0) - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0) - SWITCH_CASE(1) - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor) - SWITCH_CASE(2) - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice) - SWITCH_CASE(3) - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber) - SWITCH_DEFAULT - if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ - len = usbFunctionDescriptor(rq); - } - SWITCH_END -#endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ -#if USB_CFG_DESCR_PROPS_HID_REPORT /* only support HID descriptors if enabled */ - SWITCH_CASE(USBDESCR_HID) /* 0x21 */ - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18) - SWITCH_CASE(USBDESCR_HID_REPORT)/* 0x22 */ - GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport) + } + SWITCH_END +#endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ +#if USB_CFG_DESCR_PROPS_HID_REPORT /* only support HID descriptors if enabled */ + SWITCH_CASE(USBDESCR_HID) /* 0x21 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18) + SWITCH_CASE(USBDESCR_HID_REPORT) /* 0x22 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport) #endif SWITCH_DEFAULT - if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ - len = usbFunctionDescriptor(rq); - } + if (USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC) { + len = usbFunctionDescriptor(rq); + } SWITCH_END usbMsgFlags = flags; return len; @@ -359,56 +366,53 @@ uchar flags = USB_FLG_MSGPTR_IS_ROM; /* usbDriverSetup() is similar to usbFunctionSetup(), but it's used for * standard requests instead of class and custom requests. */ -static inline usbMsgLen_t usbDriverSetup(usbRequest_t *rq) -{ -uchar len = 0, *dataPtr = usbTxBuf + 9; /* there are 2 bytes free space at the end of the buffer */ -uchar value = rq->wValue.bytes[0]; +static inline usbMsgLen_t usbDriverSetup(usbRequest_t *rq) { + uchar len = 0, *dataPtr = usbTxBuf + 9; /* there are 2 bytes free space at the end of the buffer */ + uchar value = rq->wValue.bytes[0]; #if USB_CFG_IMPLEMENT_HALT -uchar index = rq->wIndex.bytes[0]; + uchar index = rq->wIndex.bytes[0]; #endif dataPtr[0] = 0; /* default reply common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */ SWITCH_START(rq->bRequest) - SWITCH_CASE(USBRQ_GET_STATUS) /* 0 */ - uchar recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */ - if(USB_CFG_IS_SELF_POWERED && recipient == USBRQ_RCPT_DEVICE) - dataPtr[0] = USB_CFG_IS_SELF_POWERED; + SWITCH_CASE(USBRQ_GET_STATUS) /* 0 */ + uchar recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */ + if (USB_CFG_IS_SELF_POWERED && recipient == USBRQ_RCPT_DEVICE) dataPtr[0] = USB_CFG_IS_SELF_POWERED; #if USB_CFG_IMPLEMENT_HALT - if(recipient == USBRQ_RCPT_ENDPOINT && index == 0x81) /* request status for endpoint 1 */ - dataPtr[0] = usbTxLen1 == USBPID_STALL; + if (recipient == USBRQ_RCPT_ENDPOINT && index == 0x81) /* request status for endpoint 1 */ + dataPtr[0] = usbTxLen1 == USBPID_STALL; #endif - dataPtr[1] = 0; - len = 2; + dataPtr[1] = 0; + len = 2; #if USB_CFG_IMPLEMENT_HALT - SWITCH_CASE2(USBRQ_CLEAR_FEATURE, USBRQ_SET_FEATURE) /* 1, 3 */ - if(value == 0 && index == 0x81){ /* feature 0 == HALT for endpoint == 1 */ - usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL; - usbResetDataToggling(); - } + SWITCH_CASE2(USBRQ_CLEAR_FEATURE, USBRQ_SET_FEATURE) /* 1, 3 */ + if (value == 0 && index == 0x81) { /* feature 0 == HALT for endpoint == 1 */ + usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL; + usbResetDataToggling(); + } #endif - SWITCH_CASE(USBRQ_SET_ADDRESS) /* 5 */ - usbNewDeviceAddr = value; - USB_SET_ADDRESS_HOOK(); - SWITCH_CASE(USBRQ_GET_DESCRIPTOR) /* 6 */ - len = usbDriverDescriptor(rq); - goto skipMsgPtrAssignment; - SWITCH_CASE(USBRQ_GET_CONFIGURATION) /* 8 */ - dataPtr = &usbConfiguration; /* send current configuration value */ - len = 1; - SWITCH_CASE(USBRQ_SET_CONFIGURATION) /* 9 */ - usbConfiguration = value; - usbResetStall(); - SWITCH_CASE(USBRQ_GET_INTERFACE) /* 10 */ - len = 1; + SWITCH_CASE(USBRQ_SET_ADDRESS) /* 5 */ + usbNewDeviceAddr = value; + USB_SET_ADDRESS_HOOK(); + SWITCH_CASE(USBRQ_GET_DESCRIPTOR) /* 6 */ + len = usbDriverDescriptor(rq); + goto skipMsgPtrAssignment; + SWITCH_CASE(USBRQ_GET_CONFIGURATION) /* 8 */ + dataPtr = &usbConfiguration; /* send current configuration value */ + len = 1; + SWITCH_CASE(USBRQ_SET_CONFIGURATION) /* 9 */ + usbConfiguration = value; + usbResetStall(); + SWITCH_CASE(USBRQ_GET_INTERFACE) /* 10 */ + len = 1; #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE - SWITCH_CASE(USBRQ_SET_INTERFACE) /* 11 */ - usbResetDataToggling(); - usbResetStall(); + SWITCH_CASE(USBRQ_SET_INTERFACE) /* 11 */ + usbResetDataToggling(); + usbResetStall(); #endif - SWITCH_DEFAULT /* 7=SET_DESCRIPTOR, 12=SYNC_FRAME */ + SWITCH_DEFAULT /* 7=SET_DESCRIPTOR, 12=SYNC_FRAME */ /* Should we add an optional hook here? */ - SWITCH_END - usbMsgPtr = dataPtr; + SWITCH_END usbMsgPtr = dataPtr; skipMsgPtrAssignment: return len; } @@ -419,65 +423,64 @@ skipMsgPtrAssignment: * routine. It distinguishes between SETUP and DATA packets and processes * them accordingly. */ -static inline void usbProcessRx(uchar *data, uchar len) -{ -usbRequest_t *rq = (void *)data; - -/* usbRxToken can be: - * 0x2d 00101101 (USBPID_SETUP for setup data) - * 0xe1 11100001 (USBPID_OUT: data phase of setup transfer) - * 0...0x0f for OUT on endpoint X - */ +static inline void usbProcessRx(uchar *data, uchar len) { + usbRequest_t *rq = (void *)data; + + /* usbRxToken can be: + * 0x2d 00101101 (USBPID_SETUP for setup data) + * 0xe1 11100001 (USBPID_OUT: data phase of setup transfer) + * 0...0x0f for OUT on endpoint X + */ DBG2(0x10 + (usbRxToken & 0xf), data, len + 2); /* SETUP=1d, SETUP-DATA=11, OUTx=1x */ USB_RX_USER_HOOK(data, len) #if USB_CFG_IMPLEMENT_FN_WRITEOUT - if(usbRxToken < 0x10){ /* OUT to endpoint != 0: endpoint number in usbRxToken */ + if (usbRxToken < 0x10) { /* OUT to endpoint != 0: endpoint number in usbRxToken */ usbFunctionWriteOut(data, len); return; } #endif - if(usbRxToken == (uchar)USBPID_SETUP){ - if(len != 8) /* Setup size must be always 8 bytes. Ignore otherwise. */ + if (usbRxToken == (uchar)USBPID_SETUP) { + if (len != 8) /* Setup size must be always 8 bytes. Ignore otherwise. */ return; usbMsgLen_t replyLen; - usbTxBuf[0] = USBPID_DATA0; /* initialize data toggling */ - usbTxLen = USBPID_NAK; /* abort pending transmit */ + usbTxBuf[0] = USBPID_DATA0; /* initialize data toggling */ + usbTxLen = USBPID_NAK; /* abort pending transmit */ usbMsgFlags = 0; - uchar type = rq->bmRequestType & USBRQ_TYPE_MASK; - if(type != USBRQ_TYPE_STANDARD){ /* standard requests are handled by driver */ + uchar type = rq->bmRequestType & USBRQ_TYPE_MASK; + if (type != USBRQ_TYPE_STANDARD) { /* standard requests are handled by driver */ replyLen = usbFunctionSetup(data); - }else{ + } else { replyLen = usbDriverSetup(rq); } #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE - if(replyLen == USB_NO_MSG){ /* use user-supplied read/write function */ + if (replyLen == USB_NO_MSG) { /* use user-supplied read/write function */ /* do some conditioning on replyLen, but on IN transfers only */ - if((rq->bmRequestType & USBRQ_DIR_MASK) != USBRQ_DIR_HOST_TO_DEVICE){ - if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */ + if ((rq->bmRequestType & USBRQ_DIR_MASK) != USBRQ_DIR_HOST_TO_DEVICE) { + if (sizeof(replyLen) < sizeof(rq->wLength.word)) { /* help compiler with optimizing */ replyLen = rq->wLength.bytes[0]; - }else{ + } else { replyLen = rq->wLength.word; } } usbMsgFlags = USB_FLG_USE_USER_RW; - }else /* The 'else' prevents that we limit a replyLen of USB_NO_MSG to the maximum transfer len. */ + } else /* The 'else' prevents that we limit a replyLen of USB_NO_MSG to the maximum transfer len. */ #endif - if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */ - if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */ + if (sizeof(replyLen) < sizeof(rq->wLength.word)) { /* help compiler with optimizing */ + if (!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */ replyLen = rq->wLength.bytes[0]; - }else{ - if(replyLen > rq->wLength.word) /* limit length to max */ + } else { + if (replyLen > rq->wLength.word) /* limit length to max */ replyLen = rq->wLength.word; } usbMsgLen = replyLen; - }else{ /* usbRxToken must be USBPID_OUT, which means data phase of setup (control-out) */ + } else { /* usbRxToken must be USBPID_OUT, which means data phase of setup (control-out) */ #if USB_CFG_IMPLEMENT_FN_WRITE - if(usbMsgFlags & USB_FLG_USE_USER_RW){ + if (usbMsgFlags & USB_FLG_USE_USER_RW) { uchar rval = usbFunctionWrite(data, len); - if(rval == 0xff){ /* an error occurred */ + if (rval == 0xff) { /* an error occurred */ usbTxLen = USBPID_STALL; - }else if(rval != 0){ /* This was the final package */ - usbMsgLen = 0; /* answer with a zero-sized data packet */ + } else if (rval != 0) { /* This was the final package */ + usbMsgLen = 0; /* answer with a zero-sized data packet */ } } #endif @@ -489,26 +492,25 @@ usbRequest_t *rq = (void *)data; /* This function is similar to usbFunctionRead(), but it's also called for * data handled automatically by the driver (e.g. descriptor reads). */ -static uchar usbDeviceRead(uchar *data, uchar len) -{ - if(len > 0){ /* don't bother app with 0 sized reads */ +static uchar usbDeviceRead(uchar *data, uchar len) { + if (len > 0) { /* don't bother app with 0 sized reads */ #if USB_CFG_IMPLEMENT_FN_READ - if(usbMsgFlags & USB_FLG_USE_USER_RW){ + if (usbMsgFlags & USB_FLG_USE_USER_RW) { len = usbFunctionRead(data, len); - }else + } else #endif { uchar i = len, *r = usbMsgPtr; - if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */ - do{ - uchar c = USB_READ_FLASH(r); /* assign to char size variable to enforce byte ops */ + if (usbMsgFlags & USB_FLG_MSGPTR_IS_ROM) { /* ROM data */ + do { + uchar c = USB_READ_FLASH(r); /* assign to char size variable to enforce byte ops */ *data++ = c; r++; - }while(--i); - }else{ /* RAM data */ - do{ + } while (--i); + } else { /* RAM data */ + do { *data++ = *r++; - }while(--i); + } while (--i); } usbMsgPtr = r; } @@ -521,39 +523,36 @@ static uchar usbDeviceRead(uchar *data, uchar len) /* usbBuildTxBlock() is called when we have data to transmit and the * interrupt routine's transmit buffer is empty. */ -static inline void usbBuildTxBlock(void) -{ -usbMsgLen_t wantLen; -uchar len; +static inline void usbBuildTxBlock(void) { + usbMsgLen_t wantLen; + uchar len; wantLen = usbMsgLen; - if(wantLen > 8) - wantLen = 8; + if (wantLen > 8) wantLen = 8; usbMsgLen -= wantLen; usbTxBuf[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* DATA toggling */ len = usbDeviceRead(usbTxBuf + 1, wantLen); - if(len <= 8){ /* valid data packet */ + if (len <= 8) { /* valid data packet */ usbCrc16Append(&usbTxBuf[1], len); - len += 4; /* length including sync byte */ - if(len < 12) /* a partial package identifies end of message */ + len += 4; /* length including sync byte */ + if (len < 12) /* a partial package identifies end of message */ usbMsgLen = USB_NO_MSG; - }else{ - len = USBPID_STALL; /* stall the endpoint */ + } else { + len = USBPID_STALL; /* stall the endpoint */ usbMsgLen = USB_NO_MSG; } usbTxLen = len; - DBG2(0x20, usbTxBuf, len-1); + DBG2(0x20, usbTxBuf, len - 1); } /* ------------------------------------------------------------------------- */ -static inline void usbHandleResetHook(uchar notResetState) -{ +static inline void usbHandleResetHook(uchar notResetState) { #ifdef USB_RESET_HOOK -static uchar wasReset; -uchar isReset = !notResetState; + static uchar wasReset; + uchar isReset = !notResetState; - if(wasReset != isReset){ + if (wasReset != isReset) { USB_RESET_HOOK(isReset); wasReset = isReset; } @@ -562,40 +561,39 @@ uchar isReset = !notResetState; /* ------------------------------------------------------------------------- */ -USB_PUBLIC void usbPoll(void) -{ -schar len; -uchar i; +USB_PUBLIC void usbPoll(void) { + schar len; + uchar i; len = usbRxLen - 3; - if(len >= 0){ -/* We could check CRC16 here -- but ACK has already been sent anyway. If you - * need data integrity checks with this driver, check the CRC in your app - * code and report errors back to the host. Since the ACK was already sent, - * retries must be handled on application level. - * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3); - */ + if (len >= 0) { + /* We could check CRC16 here -- but ACK has already been sent anyway. If you + * need data integrity checks with this driver, check the CRC in your app + * code and report errors back to the host. Since the ACK was already sent, + * retries must be handled on application level. + * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3); + */ usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len); #if USB_CFG_HAVE_FLOWCONTROL - if(usbRxLen > 0) /* only mark as available if not inactivated */ + if (usbRxLen > 0) /* only mark as available if not inactivated */ usbRxLen = 0; #else - usbRxLen = 0; /* mark rx buffer as available */ + usbRxLen = 0; /* mark rx buffer as available */ #endif } - if(usbTxLen & 0x10){ /* transmit system idle */ - if(usbMsgLen != USB_NO_MSG){ /* transmit data pending? */ + if (usbTxLen & 0x10) { /* transmit system idle */ + if (usbMsgLen != USB_NO_MSG) { /* transmit data pending? */ usbBuildTxBlock(); } } - for(i = 20; i > 0; i--){ + for (i = 20; i > 0; i--) { uchar usbLineStatus = USBIN & USBMASK; - if(usbLineStatus != 0) /* SE0 has ended */ + if (usbLineStatus != 0) /* SE0 has ended */ goto isNotReset; } /* RESET condition, called multiple times during reset */ usbNewDeviceAddr = 0; - usbDeviceAddr = 0; + usbDeviceAddr = 0; usbResetStall(); DBG1(0xff, 0, 0); isNotReset: @@ -604,8 +602,7 @@ isNotReset: /* ------------------------------------------------------------------------- */ -USB_PUBLIC void usbInit(void) -{ +USB_PUBLIC void usbInit(void) { #if USB_INTR_CFG_SET != 0 USB_INTR_CFG |= USB_INTR_CFG_SET; #endif @@ -616,9 +613,9 @@ USB_PUBLIC void usbInit(void) usbResetDataToggling(); #if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE usbTxLen1 = USBPID_NAK; -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 usbTxLen3 = USBPID_NAK; -#endif +# endif #endif } diff --git a/tmk_core/protocol/vusb/usbdrv/usbdrv.h b/tmk_core/protocol/vusb/usbdrv/usbdrv.h index 42fe16372..88a1bce76 100644 --- a/tmk_core/protocol/vusb/usbdrv/usbdrv.h +++ b/tmk_core/protocol/vusb/usbdrv/usbdrv.h @@ -122,7 +122,7 @@ USB messages, even if they address another (low-speed) device on the same bus. /* --------------------------- Module Interface ---------------------------- */ /* ------------------------------------------------------------------------- */ -#define USBDRV_VERSION 20100715 +#define USBDRV_VERSION 20100715 /* This define uniquely identifies a driver version. It is a decimal number * constructed from the driver's release date in the form YYYYMMDD. If the * driver's behavior or interface changes, you can use this constant to @@ -130,9 +130,8 @@ USB messages, even if they address another (low-speed) device on the same bus. * older than 2006-01-25. */ - #ifndef USB_PUBLIC -#define USB_PUBLIC +# define USB_PUBLIC #endif /* USB_PUBLIC is used as declaration attribute for all functions exported by * the USB driver. The default is no attribute (see above). You may define it @@ -142,28 +141,28 @@ USB messages, even if they address another (low-speed) device on the same bus. */ #ifndef __ASSEMBLER__ -#ifndef uchar -#define uchar unsigned char -#endif -#ifndef schar -#define schar signed char -#endif +# ifndef uchar +# define uchar unsigned char +# endif +# ifndef schar +# define schar signed char +# endif /* shortcuts for well defined 8 bit integer types */ -#if USB_CFG_LONG_TRANSFERS /* if more than 254 bytes transfer size required */ -# define usbMsgLen_t unsigned -#else -# define usbMsgLen_t uchar -#endif +# if USB_CFG_LONG_TRANSFERS /* if more than 254 bytes transfer size required */ +# define usbMsgLen_t unsigned +# else +# define usbMsgLen_t uchar +# endif /* usbMsgLen_t is the data type used for transfer lengths. By default, it is * defined to uchar, allowing a maximum of 254 bytes (255 is reserved for * USB_NO_MSG below). If the usbconfig.h defines USB_CFG_LONG_TRANSFERS to 1, * a 16 bit data type is used, allowing up to 16384 bytes (the rest is used * for flags in the descriptor configuration). */ -#define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */ +# define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */ -struct usbRequest; /* forward declaration */ +struct usbRequest; /* forward declaration */ USB_PUBLIC void usbInit(void); /* This function must be called before interrupts are enabled and the main @@ -216,7 +215,7 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq); * usbFunctionSetup() above, but it is called only to request USB descriptor * data. See the documentation of usbFunctionSetup() above for more info. */ -#if USB_CFG_HAVE_INTRIN_ENDPOINT +# if USB_CFG_HAVE_INTRIN_ENDPOINT USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len); /* This function sets the message which will be sent during the next interrupt * IN transfer. The message is copied to an internal buffer and must not exceed @@ -224,19 +223,19 @@ USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len); * interrupt status to the host. * If you need to transfer more bytes, use a control read after the interrupt. */ -#define usbInterruptIsReady() (usbTxLen1 & 0x10) +# define usbInterruptIsReady() (usbTxLen1 & 0x10) /* This macro indicates whether the last interrupt message has already been * sent. If you set a new interrupt message before the old was sent, the * message already buffered will be lost. */ -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len); -#define usbInterruptIsReady3() (usbTxLen3 & 0x10) +# define usbInterruptIsReady3() (usbTxLen3 & 0x10) /* Same as above for endpoint 3 */ -#endif -#endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */ -#if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */ -#define usbHidReportDescriptor usbDescriptorHidReport +# endif +# endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */ +# if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */ +# define usbHidReportDescriptor usbDescriptorHidReport /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */ /* If you implement an HID device, you need to provide a report descriptor. * The HID report descriptor syntax is a bit complex. If you understand how @@ -244,8 +243,8 @@ USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len); * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/. * Otherwise you should probably start with a working example. */ -#endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */ -#if USB_CFG_IMPLEMENT_FN_WRITE +# endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */ +# if USB_CFG_IMPLEMENT_FN_WRITE USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len); /* This function is called by the driver to provide a control transfer's * payload data (control-out). It is called in chunks of up to 8 bytes. The @@ -262,8 +261,8 @@ USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len); * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. */ -#endif /* USB_CFG_IMPLEMENT_FN_WRITE */ -#if USB_CFG_IMPLEMENT_FN_READ +# endif /* USB_CFG_IMPLEMENT_FN_WRITE */ +# if USB_CFG_IMPLEMENT_FN_READ USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len); /* This function is called by the driver to ask the application for a control * transfer's payload data (control-in). It is called in chunks of up to 8 @@ -274,26 +273,24 @@ USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len); * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. */ -#endif /* USB_CFG_IMPLEMENT_FN_READ */ +# endif /* USB_CFG_IMPLEMENT_FN_READ */ -extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */ -#if USB_CFG_IMPLEMENT_FN_WRITEOUT +extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */ +# if USB_CFG_IMPLEMENT_FN_WRITEOUT USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len); /* This function is called by the driver when data is received on an interrupt- * or bulk-out endpoint. The endpoint number can be found in the global * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in * usbconfig.h to get this function called. */ -#endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */ -#ifdef USB_CFG_PULLUP_IOPORTNAME -#define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \ - (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT))) -#define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \ - (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT))) -#else /* USB_CFG_PULLUP_IOPORTNAME */ -#define usbDeviceConnect() (USBDDR &= ~(1<<USBMINUS)) -#define usbDeviceDisconnect() (USBDDR |= (1<<USBMINUS)) -#endif /* USB_CFG_PULLUP_IOPORTNAME */ +# endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */ +# ifdef USB_CFG_PULLUP_IOPORTNAME +# define usbDeviceConnect() ((USB_PULLUP_DDR |= (1 << USB_CFG_PULLUP_BIT)), (USB_PULLUP_OUT |= (1 << USB_CFG_PULLUP_BIT))) +# define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1 << USB_CFG_PULLUP_BIT)), (USB_PULLUP_OUT &= ~(1 << USB_CFG_PULLUP_BIT))) +# else /* USB_CFG_PULLUP_IOPORTNAME */ +# define usbDeviceConnect() (USBDDR &= ~(1 << USBMINUS)) +# define usbDeviceDisconnect() (USBDDR |= (1 << USBMINUS)) +# endif /* USB_CFG_PULLUP_IOPORTNAME */ /* The macros usbDeviceConnect() and usbDeviceDisconnect() (intended to look * like a function) connect resp. disconnect the device from the host's USB. * If the constants USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT are defined @@ -307,7 +304,7 @@ USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len); * or use cli() to disable interrupts globally. */ extern unsigned usbCrc16(unsigned data, uchar len); -#define usbCrc16(data, len) usbCrc16((unsigned)(data), len) +# define usbCrc16(data, len) usbCrc16((unsigned)(data), len) /* This function calculates the binary complement of the data CRC used in * USB data packets. The value is used to build raw transmit packets. * You may want to use this function for data checksums or to verify received @@ -315,12 +312,12 @@ extern unsigned usbCrc16(unsigned data, uchar len); * tiny memory model. */ extern unsigned usbCrc16Append(unsigned data, uchar len); -#define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len) +# define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len) /* This function is equivalent to usbCrc16() above, except that it appends * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len' * bytes. */ -#if USB_CFG_HAVE_MEASURE_FRAME_LENGTH +# if USB_CFG_HAVE_MEASURE_FRAME_LENGTH extern unsigned usbMeasureFrameLength(void); /* This function MUST be called IMMEDIATELY AFTER USB reset and measures 1/7 of * the number of CPU cycles during one USB frame minus one low speed bit @@ -329,59 +326,58 @@ extern unsigned usbMeasureFrameLength(void); * calling this function. * This can be used to calibrate the AVR's RC oscillator. */ -#endif -extern uchar usbConfiguration; +# endif +extern uchar usbConfiguration; /* This value contains the current configuration set by the host. The driver * allows setting and querying of this variable with the USB SET_CONFIGURATION * and GET_CONFIGURATION requests, but does not use it otherwise. * You may want to reflect the "configured" status with a LED on the device or * switch on high power parts of the circuit only if the device is configured. */ -#if USB_COUNT_SOF -extern volatile uchar usbSofCount; +# if USB_COUNT_SOF +extern volatile uchar usbSofCount; /* This variable is incremented on every SOF packet. It is only available if * the macro USB_COUNT_SOF is defined to a value != 0. */ -#endif -#if USB_CFG_CHECK_DATA_TOGGLING -extern uchar usbCurrentDataToken; +# endif +# if USB_CFG_CHECK_DATA_TOGGLING +extern uchar usbCurrentDataToken; /* This variable can be checked in usbFunctionWrite() and usbFunctionWriteOut() * to ignore duplicate packets. */ -#endif +# endif -#define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8)) +# define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2 * (stringLength) + 2) | (3 << 8)) /* This macro builds a descriptor header for a string descriptor given the * string's length. See usbdrv.c for an example how to use it. */ -#if USB_CFG_HAVE_FLOWCONTROL -extern volatile schar usbRxLen; -#define usbDisableAllRequests() usbRxLen = -1 +# if USB_CFG_HAVE_FLOWCONTROL +extern volatile schar usbRxLen; +# define usbDisableAllRequests() usbRxLen = -1 /* Must be called from usbFunctionWrite(). This macro disables all data input * from the USB interface. Requests from the host are answered with a NAK * while they are disabled. */ -#define usbEnableAllRequests() usbRxLen = 0 +# define usbEnableAllRequests() usbRxLen = 0 /* May only be called if requests are disabled. This macro enables input from * the USB interface after it has been disabled with usbDisableAllRequests(). */ -#define usbAllRequestsAreDisabled() (usbRxLen < 0) +# define usbAllRequestsAreDisabled() (usbRxLen < 0) /* Use this macro to find out whether requests are disabled. It may be needed * to ensure that usbEnableAllRequests() is never called when requests are * enabled. */ -#endif +# endif -#define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token -#define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token +# define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token +# define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token /* These two macros can be used by application software to reset data toggling * for interrupt-in endpoints 1 and 3. Since the token is toggled BEFORE * sending data, you must set the opposite value of the token which should come * first. */ -#endif /* __ASSEMBLER__ */ - +#endif /* __ASSEMBLER__ */ /* ------------------------------------------------------------------------- */ /* ----------------- Definitions for Descriptor Properties ----------------- */ @@ -390,57 +386,57 @@ extern volatile schar usbRxLen; * about the various methods to define USB descriptors. If you do nothing, * the default descriptors will be used. */ -#define USB_PROP_IS_DYNAMIC (1 << 14) +#define USB_PROP_IS_DYNAMIC (1 << 14) /* If this property is set for a descriptor, usbFunctionDescriptor() will be * used to obtain the particular descriptor. Data directly returned via * usbMsgPtr are FLASH data by default, combine (OR) with USB_PROP_IS_RAM to * return RAM data. */ -#define USB_PROP_IS_RAM (1 << 15) +#define USB_PROP_IS_RAM (1 << 15) /* If this property is set for a descriptor, the data is read from RAM * memory instead of Flash. The property is used for all methods to provide * external descriptors. */ -#define USB_PROP_LENGTH(len) ((len) & 0x3fff) +#define USB_PROP_LENGTH(len) ((len)&0x3fff) /* If a static external descriptor is used, this is the total length of the * descriptor in bytes. */ /* all descriptors which may have properties: */ #ifndef USB_CFG_DESCR_PROPS_DEVICE -#define USB_CFG_DESCR_PROPS_DEVICE 0 +# define USB_CFG_DESCR_PROPS_DEVICE 0 #endif #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION -#define USB_CFG_DESCR_PROPS_CONFIGURATION 0 +# define USB_CFG_DESCR_PROPS_CONFIGURATION 0 #endif #ifndef USB_CFG_DESCR_PROPS_STRINGS -#define USB_CFG_DESCR_PROPS_STRINGS 0 +# define USB_CFG_DESCR_PROPS_STRINGS 0 #endif #ifndef USB_CFG_DESCR_PROPS_STRING_0 -#define USB_CFG_DESCR_PROPS_STRING_0 0 +# define USB_CFG_DESCR_PROPS_STRING_0 0 #endif #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR -#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 +# define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 #endif #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT -#define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 +# define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 #endif #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER -#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 +# define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 #endif #ifndef USB_CFG_DESCR_PROPS_HID -#define USB_CFG_DESCR_PROPS_HID 0 +# define USB_CFG_DESCR_PROPS_HID 0 #endif #if !(USB_CFG_DESCR_PROPS_HID_REPORT) -# undef USB_CFG_DESCR_PROPS_HID_REPORT -# if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */ -# define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH -# else -# define USB_CFG_DESCR_PROPS_HID_REPORT 0 -# endif +# undef USB_CFG_DESCR_PROPS_HID_REPORT +# if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */ +# define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH +# else +# define USB_CFG_DESCR_PROPS_HID_REPORT 0 +# endif #endif #ifndef USB_CFG_DESCR_PROPS_UNKNOWN -#define USB_CFG_DESCR_PROPS_UNKNOWN 0 +# define USB_CFG_DESCR_PROPS_UNKNOWN 0 #endif /* ------------------ forward declaration of descriptors ------------------- */ @@ -449,46 +445,46 @@ extern volatile schar usbRxLen; */ #ifndef __ASSEMBLER__ extern -#if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM) -PROGMEM -#endif -const char usbDescriptorDevice[]; +# if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM) + PROGMEM +# endif + const char usbDescriptorDevice[]; extern -#if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM) -PROGMEM -#endif -const char usbDescriptorConfiguration[]; +# if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM) + PROGMEM +# endif + const char usbDescriptorConfiguration[]; extern -#if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM) -PROGMEM -#endif -const char usbDescriptorHidReport[]; +# if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM) + PROGMEM +# endif + const char usbDescriptorHidReport[]; extern -#if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM) -PROGMEM -#endif -const char usbDescriptorString0[]; +# if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM) + PROGMEM +# endif + const char usbDescriptorString0[]; extern -#if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM) -PROGMEM -#endif -const int usbDescriptorStringVendor[]; +# if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM) + PROGMEM +# endif + const int usbDescriptorStringVendor[]; extern -#if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM) -PROGMEM -#endif -const int usbDescriptorStringDevice[]; +# if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM) + PROGMEM +# endif + const int usbDescriptorStringDevice[]; extern -#if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM) -PROGMEM -#endif -const int usbDescriptorStringSerialNumber[]; +# if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM) + PROGMEM +# endif + const int usbDescriptorStringSerialNumber[]; #endif /* __ASSEMBLER__ */ @@ -496,12 +492,12 @@ const int usbDescriptorStringSerialNumber[]; /* ------------------------ General Purpose Macros ------------------------- */ /* ------------------------------------------------------------------------- */ -#define USB_CONCAT(a, b) a ## b -#define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b) +#define USB_CONCAT(a, b) a##b +#define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b) -#define USB_OUTPORT(name) USB_CONCAT(PORT, name) -#define USB_INPORT(name) USB_CONCAT(PIN, name) -#define USB_DDRPORT(name) USB_CONCAT(DDR, name) +#define USB_OUTPORT(name) USB_CONCAT(PORT, name) +#define USB_INPORT(name) USB_CONCAT(PIN, name) +#define USB_DDRPORT(name) USB_CONCAT(DDR, name) /* The double-define trick above lets us concatenate strings which are * defined by macros. */ @@ -511,7 +507,7 @@ const int usbDescriptorStringSerialNumber[]; /* ------------------------------------------------------------------------- */ #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID) -#warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h" +# warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h" /* If the user has not defined IDs, we default to obdev's free IDs. * See USB-IDs-for-free.txt for details. */ @@ -519,96 +515,96 @@ const int usbDescriptorStringSerialNumber[]; /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */ #ifndef USB_CFG_VENDOR_ID -# define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ +# define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ #endif #ifndef USB_CFG_DEVICE_ID -# if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH -# define USB_CFG_DEVICE_ID 0xdf, 0x05 /* = 0x5df = 1503, shared PID for HIDs */ -# elif USB_CFG_INTERFACE_CLASS == 2 -# define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x5e1 = 1505, shared PID for CDC Modems */ -# else -# define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x5dc = 1500, obdev's free PID */ -# endif +# if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH +# define USB_CFG_DEVICE_ID 0xdf, 0x05 /* = 0x5df = 1503, shared PID for HIDs */ +# elif USB_CFG_INTERFACE_CLASS == 2 +# define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x5e1 = 1505, shared PID for CDC Modems */ +# else +# define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x5dc = 1500, obdev's free PID */ +# endif #endif /* Derive Output, Input and DataDirection ports from port names */ #ifndef USB_CFG_IOPORTNAME -#error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h" +# error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h" #endif -#define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME) -#define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) -#define USBIN USB_INPORT(USB_CFG_IOPORTNAME) -#define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME) -#define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME) +#define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME) +#define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) +#define USBIN USB_INPORT(USB_CFG_IOPORTNAME) +#define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME) +#define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME) -#define USBMINUS USB_CFG_DMINUS_BIT -#define USBPLUS USB_CFG_DPLUS_BIT -#define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */ -#define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */ +#define USBMINUS USB_CFG_DMINUS_BIT +#define USBPLUS USB_CFG_DPLUS_BIT +#define USBIDLE (1 << USB_CFG_DMINUS_BIT) /* value representing J state */ +#define USBMASK ((1 << USB_CFG_DPLUS_BIT) | (1 << USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */ /* defines for backward compatibility with older driver versions: */ -#define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME) +#define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME) #ifdef USB_CFG_PULLUP_IOPORTNAME -#define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) +# define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) #endif -#ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */ -#define USB_CFG_EP3_NUMBER 3 +#ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */ +# define USB_CFG_EP3_NUMBER 3 #endif #ifndef USB_CFG_HAVE_INTRIN_ENDPOINT3 -#define USB_CFG_HAVE_INTRIN_ENDPOINT3 0 +# define USB_CFG_HAVE_INTRIN_ENDPOINT3 0 #endif -#define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */ +#define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */ /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */ -#ifndef USB_INTR_CFG /* allow user to override our default */ -# if defined EICRA -# define USB_INTR_CFG EICRA -# else -# define USB_INTR_CFG MCUCR -# endif -#endif -#ifndef USB_INTR_CFG_SET /* allow user to override our default */ -# if defined(USB_COUNT_SOF) || defined(USB_SOF_HOOK) -# define USB_INTR_CFG_SET (1 << ISC01) /* cfg for falling edge */ - /* If any SOF logic is used, the interrupt must be wired to D- where - * we better trigger on falling edge - */ -# else -# define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */ -# endif -#endif -#ifndef USB_INTR_CFG_CLR /* allow user to override our default */ -# define USB_INTR_CFG_CLR 0 /* no bits to clear */ -#endif - -#ifndef USB_INTR_ENABLE /* allow user to override our default */ -# if defined GIMSK -# define USB_INTR_ENABLE GIMSK -# elif defined EIMSK -# define USB_INTR_ENABLE EIMSK -# else -# define USB_INTR_ENABLE GICR -# endif +#ifndef USB_INTR_CFG /* allow user to override our default */ +# if defined EICRA +# define USB_INTR_CFG EICRA +# else +# define USB_INTR_CFG MCUCR +# endif +#endif +#ifndef USB_INTR_CFG_SET /* allow user to override our default */ +# if defined(USB_COUNT_SOF) || defined(USB_SOF_HOOK) +# define USB_INTR_CFG_SET (1 << ISC01) /* cfg for falling edge */ + /* If any SOF logic is used, the interrupt must be wired to D- where + * we better trigger on falling edge + */ +# else +# define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */ +# endif +#endif +#ifndef USB_INTR_CFG_CLR /* allow user to override our default */ +# define USB_INTR_CFG_CLR 0 /* no bits to clear */ +#endif + +#ifndef USB_INTR_ENABLE /* allow user to override our default */ +# if defined GIMSK +# define USB_INTR_ENABLE GIMSK +# elif defined EIMSK +# define USB_INTR_ENABLE EIMSK +# else +# define USB_INTR_ENABLE GICR +# endif #endif #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */ -# define USB_INTR_ENABLE_BIT INT0 +# define USB_INTR_ENABLE_BIT INT0 #endif -#ifndef USB_INTR_PENDING /* allow user to override our default */ -# if defined EIFR -# define USB_INTR_PENDING EIFR -# else -# define USB_INTR_PENDING GIFR -# endif +#ifndef USB_INTR_PENDING /* allow user to override our default */ +# if defined EIFR +# define USB_INTR_PENDING EIFR +# else +# define USB_INTR_PENDING GIFR +# endif #endif -#ifndef USB_INTR_PENDING_BIT /* allow user to override our default */ -# define USB_INTR_PENDING_BIT INTF0 +#ifndef USB_INTR_PENDING_BIT /* allow user to override our default */ +# define USB_INTR_PENDING_BIT INTF0 #endif /* @@ -628,46 +624,45 @@ at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM /* ------------------------------------------------------------------------- */ /* USB Token values */ -#define USBPID_SETUP 0x2d -#define USBPID_OUT 0xe1 -#define USBPID_IN 0x69 -#define USBPID_DATA0 0xc3 -#define USBPID_DATA1 0x4b +#define USBPID_SETUP 0x2d +#define USBPID_OUT 0xe1 +#define USBPID_IN 0x69 +#define USBPID_DATA0 0xc3 +#define USBPID_DATA1 0x4b -#define USBPID_ACK 0xd2 -#define USBPID_NAK 0x5a -#define USBPID_STALL 0x1e +#define USBPID_ACK 0xd2 +#define USBPID_NAK 0x5a +#define USBPID_STALL 0x1e #ifndef USB_INITIAL_DATATOKEN -#define USB_INITIAL_DATATOKEN USBPID_DATA1 +# define USB_INITIAL_DATATOKEN USBPID_DATA1 #endif #ifndef __ASSEMBLER__ -typedef struct usbTxStatus{ - volatile uchar len; - uchar buffer[USB_BUFSIZE]; -}usbTxStatus_t; - -extern usbTxStatus_t usbTxStatus1, usbTxStatus3; -#define usbTxLen1 usbTxStatus1.len -#define usbTxBuf1 usbTxStatus1.buffer -#define usbTxLen3 usbTxStatus3.len -#define usbTxBuf3 usbTxStatus3.buffer - - -typedef union usbWord{ - unsigned word; - uchar bytes[2]; -}usbWord_t; - -typedef struct usbRequest{ - uchar bmRequestType; - uchar bRequest; - usbWord_t wValue; - usbWord_t wIndex; - usbWord_t wLength; -}usbRequest_t; +typedef struct usbTxStatus { + volatile uchar len; + uchar buffer[USB_BUFSIZE]; +} usbTxStatus_t; + +extern usbTxStatus_t usbTxStatus1, usbTxStatus3; +# define usbTxLen1 usbTxStatus1.len +# define usbTxBuf1 usbTxStatus1.buffer +# define usbTxLen3 usbTxStatus3.len +# define usbTxBuf3 usbTxStatus3.buffer + +typedef union usbWord { + unsigned word; + uchar bytes[2]; +} usbWord_t; + +typedef struct usbRequest { + uchar bmRequestType; + uchar bRequest; + usbWord_t wValue; + usbWord_t wIndex; + usbWord_t wLength; +} usbRequest_t; /* This structure matches the 8 byte setup request */ #endif @@ -679,56 +674,56 @@ typedef struct usbRequest{ */ /* USB setup recipient values */ -#define USBRQ_RCPT_MASK 0x1f -#define USBRQ_RCPT_DEVICE 0 -#define USBRQ_RCPT_INTERFACE 1 -#define USBRQ_RCPT_ENDPOINT 2 +#define USBRQ_RCPT_MASK 0x1f +#define USBRQ_RCPT_DEVICE 0 +#define USBRQ_RCPT_INTERFACE 1 +#define USBRQ_RCPT_ENDPOINT 2 /* USB request type values */ -#define USBRQ_TYPE_MASK 0x60 -#define USBRQ_TYPE_STANDARD (0<<5) -#define USBRQ_TYPE_CLASS (1<<5) -#define USBRQ_TYPE_VENDOR (2<<5) +#define USBRQ_TYPE_MASK 0x60 +#define USBRQ_TYPE_STANDARD (0 << 5) +#define USBRQ_TYPE_CLASS (1 << 5) +#define USBRQ_TYPE_VENDOR (2 << 5) /* USB direction values: */ -#define USBRQ_DIR_MASK 0x80 -#define USBRQ_DIR_HOST_TO_DEVICE (0<<7) -#define USBRQ_DIR_DEVICE_TO_HOST (1<<7) +#define USBRQ_DIR_MASK 0x80 +#define USBRQ_DIR_HOST_TO_DEVICE (0 << 7) +#define USBRQ_DIR_DEVICE_TO_HOST (1 << 7) /* USB Standard Requests */ -#define USBRQ_GET_STATUS 0 -#define USBRQ_CLEAR_FEATURE 1 -#define USBRQ_SET_FEATURE 3 -#define USBRQ_SET_ADDRESS 5 -#define USBRQ_GET_DESCRIPTOR 6 -#define USBRQ_SET_DESCRIPTOR 7 +#define USBRQ_GET_STATUS 0 +#define USBRQ_CLEAR_FEATURE 1 +#define USBRQ_SET_FEATURE 3 +#define USBRQ_SET_ADDRESS 5 +#define USBRQ_GET_DESCRIPTOR 6 +#define USBRQ_SET_DESCRIPTOR 7 #define USBRQ_GET_CONFIGURATION 8 #define USBRQ_SET_CONFIGURATION 9 -#define USBRQ_GET_INTERFACE 10 -#define USBRQ_SET_INTERFACE 11 -#define USBRQ_SYNCH_FRAME 12 +#define USBRQ_GET_INTERFACE 10 +#define USBRQ_SET_INTERFACE 11 +#define USBRQ_SYNCH_FRAME 12 /* USB descriptor constants */ -#define USBDESCR_DEVICE 1 -#define USBDESCR_CONFIG 2 -#define USBDESCR_STRING 3 -#define USBDESCR_INTERFACE 4 -#define USBDESCR_ENDPOINT 5 -#define USBDESCR_HID 0x21 -#define USBDESCR_HID_REPORT 0x22 -#define USBDESCR_HID_PHYS 0x23 +#define USBDESCR_DEVICE 1 +#define USBDESCR_CONFIG 2 +#define USBDESCR_STRING 3 +#define USBDESCR_INTERFACE 4 +#define USBDESCR_ENDPOINT 5 +#define USBDESCR_HID 0x21 +#define USBDESCR_HID_REPORT 0x22 +#define USBDESCR_HID_PHYS 0x23 //#define USBATTR_BUSPOWER 0x80 // USB 1.1 does not define this value any more -#define USBATTR_SELFPOWER 0x40 -#define USBATTR_REMOTEWAKE 0x20 +#define USBATTR_SELFPOWER 0x40 +#define USBATTR_REMOTEWAKE 0x20 /* USB HID Requests */ -#define USBRQ_HID_GET_REPORT 0x01 -#define USBRQ_HID_GET_IDLE 0x02 -#define USBRQ_HID_GET_PROTOCOL 0x03 -#define USBRQ_HID_SET_REPORT 0x09 -#define USBRQ_HID_SET_IDLE 0x0a -#define USBRQ_HID_SET_PROTOCOL 0x0b +#define USBRQ_HID_GET_REPORT 0x01 +#define USBRQ_HID_GET_IDLE 0x02 +#define USBRQ_HID_GET_PROTOCOL 0x03 +#define USBRQ_HID_SET_REPORT 0x09 +#define USBRQ_HID_SET_IDLE 0x0a +#define USBRQ_HID_SET_PROTOCOL 0x0b /* ------------------------------------------------------------------------- */ diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index 60e48c3a9..3719b7aa0 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -29,36 +29,38 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "bootloader.h" #include <util/delay.h> - static uint8_t vusb_keyboard_leds = 0; -static uint8_t vusb_idle_rate = 0; +static uint8_t vusb_idle_rate = 0; /* Keyboard report send buffer */ #define KBUF_SIZE 16 static report_keyboard_t kbuf[KBUF_SIZE]; -static uint8_t kbuf_head = 0; -static uint8_t kbuf_tail = 0; +static uint8_t kbuf_head = 0; +static uint8_t kbuf_tail = 0; typedef struct { - uint8_t modifier; - uint8_t reserved; - uint8_t keycode[6]; + uint8_t modifier; + uint8_t reserved; + uint8_t keycode[6]; } keyboard_report_t; -static keyboard_report_t keyboard_report; // sent to PC +static keyboard_report_t keyboard_report; // sent to PC #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10 /* transfer keyboard report from buffer */ -void vusb_transfer_keyboard(void) -{ +void vusb_transfer_keyboard(void) { for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) { if (usbInterruptIsReady()) { if (kbuf_head != kbuf_tail) { usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t)); kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE; if (debug_keyboard) { - print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("]("); + print("V-USB: kbuf["); + pdec(kbuf_tail); + print("->"); + pdec(kbuf_head); + print("]("); phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail)); print(")\n"); } @@ -70,39 +72,26 @@ void vusb_transfer_keyboard(void) } } - /*------------------------------------------------------------------* * Host driver *------------------------------------------------------------------*/ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); - -static host_driver_t driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer -}; +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); -host_driver_t *vusb_driver(void) -{ - return &driver; -} +static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -static uint8_t keyboard_leds(void) { - return vusb_keyboard_leds; -} +host_driver_t *vusb_driver(void) { return &driver; } + +static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; } -static void send_keyboard(report_keyboard_t *report) -{ +static void send_keyboard(report_keyboard_t *report) { uint8_t next = (kbuf_head + 1) % KBUF_SIZE; if (next != kbuf_tail) { kbuf[kbuf_head] = *report; - kbuf_head = next; + kbuf_head = next; } else { debug("kbuf: full\n"); } @@ -112,119 +101,97 @@ static void send_keyboard(report_keyboard_t *report) vusb_transfer_keyboard(); } - typedef struct { - uint8_t report_id; + uint8_t report_id; report_mouse_t report; -} __attribute__ ((packed)) vusb_mouse_report_t; - -static void send_mouse(report_mouse_t *report) -{ - vusb_mouse_report_t r = { - .report_id = REPORT_ID_MOUSE, - .report = *report - }; +} __attribute__((packed)) vusb_mouse_report_t; + +static void send_mouse(report_mouse_t *report) { + vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report}; if (usbInterruptIsReady3()) { usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t)); } } - typedef struct { uint8_t report_id; uint16_t usage; -} __attribute__ ((packed)) report_extra_t; +} __attribute__((packed)) report_extra_t; -static void send_system(uint16_t data) -{ +static void send_system(uint16_t data) { static uint16_t last_data = 0; if (data == last_data) return; last_data = data; - report_extra_t report = { - .report_id = REPORT_ID_SYSTEM, - .usage = data - }; + report_extra_t report = {.report_id = REPORT_ID_SYSTEM, .usage = data}; if (usbInterruptIsReady3()) { usbSetInterrupt3((void *)&report, sizeof(report)); } } -static void send_consumer(uint16_t data) -{ +static void send_consumer(uint16_t data) { static uint16_t last_data = 0; if (data == last_data) return; last_data = data; - report_extra_t report = { - .report_id = REPORT_ID_CONSUMER, - .usage = data - }; + report_extra_t report = {.report_id = REPORT_ID_CONSUMER, .usage = data}; if (usbInterruptIsReady3()) { usbSetInterrupt3((void *)&report, sizeof(report)); } } - - /*------------------------------------------------------------------* * Request from host * *------------------------------------------------------------------*/ static struct { - uint16_t len; - enum { - NONE, - BOOTLOADER, - SET_LED - } kind; + uint16_t len; + enum { NONE, BOOTLOADER, SET_LED } kind; } last_req; -usbMsgLen_t usbFunctionSetup(uchar data[8]) -{ -usbRequest_t *rq = (void *)data; +usbMsgLen_t usbFunctionSetup(uchar data[8]) { + usbRequest_t *rq = (void *)data; - if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */ - if(rq->bRequest == USBRQ_HID_GET_REPORT){ + if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */ + if (rq->bRequest == USBRQ_HID_GET_REPORT) { debug("GET_REPORT:"); /* we only have one report type, so don't look at wValue */ usbMsgPtr = (void *)&keyboard_report; return sizeof(keyboard_report); - }else if(rq->bRequest == USBRQ_HID_GET_IDLE){ + } else if (rq->bRequest == USBRQ_HID_GET_IDLE) { debug("GET_IDLE: "); - //debug_hex(vusb_idle_rate); + // debug_hex(vusb_idle_rate); usbMsgPtr = &vusb_idle_rate; return 1; - }else if(rq->bRequest == USBRQ_HID_SET_IDLE){ + } else if (rq->bRequest == USBRQ_HID_SET_IDLE) { vusb_idle_rate = rq->wValue.bytes[1]; debug("SET_IDLE: "); debug_hex(vusb_idle_rate); - }else if(rq->bRequest == USBRQ_HID_SET_REPORT){ + } else if (rq->bRequest == USBRQ_HID_SET_REPORT) { debug("SET_REPORT: "); // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard) if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) { debug("SET_LED: "); last_req.kind = SET_LED; - last_req.len = rq->wLength.word; + last_req.len = rq->wLength.word; #ifdef BOOTLOADER_SIZE - } else if(rq->wValue.word == 0x0301) { + } else if (rq->wValue.word == 0x0301) { last_req.kind = BOOTLOADER; - last_req.len = rq->wLength.word; + last_req.len = rq->wLength.word; #endif } - return USB_NO_MSG; // to get data in usbFunctionWrite + return USB_NO_MSG; // to get data in usbFunctionWrite } else { debug("UNKNOWN:"); } - }else{ + } else { debug("VENDOR:"); /* no vendor specific requests implemented */ } debug("\n"); - return 0; /* default for not implemented requests: return no data back to host */ + return 0; /* default for not implemented requests: return no data back to host */ } -uchar usbFunctionWrite(uchar *data, uchar len) -{ +uchar usbFunctionWrite(uchar *data, uchar len) { if (last_req.len == 0) { return -1; } @@ -234,7 +201,7 @@ uchar usbFunctionWrite(uchar *data, uchar len) debug_hex(data[0]); debug("\n"); vusb_keyboard_leds = data[0]; - last_req.len = 0; + last_req.len = 0; return 1; break; case BOOTLOADER: @@ -250,8 +217,6 @@ uchar usbFunctionWrite(uchar *data, uchar len) return 1; } - - /*------------------------------------------------------------------* * Descriptors * *------------------------------------------------------------------*/ @@ -262,38 +227,38 @@ uchar usbFunctionWrite(uchar *data, uchar len) * from an example in HID spec appendix */ const PROGMEM uchar keyboard_hid_report[] = { - 0x05, 0x01, // Usage Page (Generic Desktop), - 0x09, 0x06, // Usage (Keyboard), - 0xA1, 0x01, // Collection (Application), - 0x75, 0x01, // Report Size (1), - 0x95, 0x08, // Report Count (8), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0xE0, // Usage Minimum (224), - 0x29, 0xE7, // Usage Maximum (231), - 0x15, 0x00, // Logical Minimum (0), - 0x25, 0x01, // Logical Maximum (1), - 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte - 0x95, 0x01, // Report Count (1), - 0x75, 0x08, // Report Size (8), - 0x81, 0x03, // Input (Constant), ;Reserved byte - 0x95, 0x05, // Report Count (5), - 0x75, 0x01, // Report Size (1), - 0x05, 0x08, // Usage Page (LEDs), - 0x19, 0x01, // Usage Minimum (1), - 0x29, 0x05, // Usage Maximum (5), - 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report - 0x95, 0x01, // Report Count (1), - 0x75, 0x03, // Report Size (3), - 0x91, 0x03, // Output (Constant), ;LED report padding - 0x95, 0x06, // Report Count (6), - 0x75, 0x08, // Report Size (8), - 0x15, 0x00, // Logical Minimum (0), - 0x26, 0xFF, 0x00, // Logical Maximum(255), - 0x05, 0x07, // Usage Page (Key Codes), - 0x19, 0x00, // Usage Minimum (0), - 0x29, 0xFF, // Usage Maximum (255), - 0x81, 0x00, // Input (Data, Array), - 0xc0 // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop), + 0x09, 0x06, // Usage (Keyboard), + 0xA1, 0x01, // Collection (Application), + 0x75, 0x01, // Report Size (1), + 0x95, 0x08, // Report Count (8), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0xE0, // Usage Minimum (224), + 0x29, 0xE7, // Usage Maximum (231), + 0x15, 0x00, // Logical Minimum (0), + 0x25, 0x01, // Logical Maximum (1), + 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte + 0x95, 0x01, // Report Count (1), + 0x75, 0x08, // Report Size (8), + 0x81, 0x03, // Input (Constant), ;Reserved byte + 0x95, 0x05, // Report Count (5), + 0x75, 0x01, // Report Size (1), + 0x05, 0x08, // Usage Page (LEDs), + 0x19, 0x01, // Usage Minimum (1), + 0x29, 0x05, // Usage Maximum (5), + 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report + 0x95, 0x01, // Report Count (1), + 0x75, 0x03, // Report Size (3), + 0x91, 0x03, // Output (Constant), ;LED report padding + 0x95, 0x06, // Report Count (6), + 0x75, 0x08, // Report Size (8), + 0x15, 0x00, // Logical Minimum (0), + 0x26, 0xFF, 0x00, // Logical Maximum(255), + 0x05, 0x07, // Usage Page (Key Codes), + 0x19, 0x00, // Usage Minimum (0), + 0x29, 0xFF, // Usage Maximum (255), + 0x81, 0x00, // Input (Data, Array), + 0xc0 // End Collection }; /* @@ -306,196 +271,191 @@ const PROGMEM uchar keyboard_hid_report[] = { */ const PROGMEM uchar mouse_hid_report[] = { /* mouse */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - 0x85, REPORT_ID_MOUSE, // REPORT_ID (1) - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - // ---------------------------- Buttons - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x05, // USAGE_MAXIMUM (Button 5) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x05, // REPORT_COUNT (5) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x75, 0x03, // REPORT_SIZE (3) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x03, // INPUT (Cnst,Var,Abs) - // ---------------------------- X,Y position - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x02, // REPORT_COUNT (2) - 0x81, 0x06, // INPUT (Data,Var,Rel) - // ---------------------------- Vertical wheel - 0x09, 0x38, // USAGE (Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical - 0x45, 0x00, // PHYSICAL_MAXIMUM (0) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - // ---------------------------- Horizontal wheel - 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) - 0x0a, 0x38, 0x02, // USAGE (AC Pan) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0xc0, // END_COLLECTION - 0xc0, // END_COLLECTION + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xa1, 0x01, // COLLECTION (Application) + 0x85, REPORT_ID_MOUSE, // REPORT_ID (1) + 0x09, 0x01, // USAGE (Pointer) + 0xa1, 0x00, // COLLECTION (Physical) + // ---------------------------- Buttons + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x05, // USAGE_MAXIMUM (Button 5) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x75, 0x01, // REPORT_SIZE (1) + 0x95, 0x05, // REPORT_COUNT (5) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x75, 0x03, // REPORT_SIZE (3) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x03, // INPUT (Cnst,Var,Abs) + // ---------------------------- X,Y position + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x02, // REPORT_COUNT (2) + 0x81, 0x06, // INPUT (Data,Var,Rel) + // ---------------------------- Vertical wheel + 0x09, 0x38, // USAGE (Wheel) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical + 0x45, 0x00, // PHYSICAL_MAXIMUM (0) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + // ---------------------------- Horizontal wheel + 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) + 0x0a, 0x38, 0x02, // USAGE (AC Pan) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + 0xc0, // END_COLLECTION + 0xc0, // END_COLLECTION /* system control */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x80, // USAGE (System Control) - 0xa1, 0x01, // COLLECTION (Application) - 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2) - 0x15, 0x01, // LOGICAL_MINIMUM (0x1) - 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7) - 0x19, 0x01, // USAGE_MINIMUM (0x1) - 0x29, 0xb7, // USAGE_MAXIMUM (0xb7) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x00, // INPUT (Data,Array,Abs) - 0xc0, // END_COLLECTION + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x80, // USAGE (System Control) + 0xa1, 0x01, // COLLECTION (Application) + 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2) + 0x15, 0x01, // LOGICAL_MINIMUM (0x1) + 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7) + 0x19, 0x01, // USAGE_MINIMUM (0x1) + 0x29, 0xb7, // USAGE_MAXIMUM (0xb7) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x00, // INPUT (Data,Array,Abs) + 0xc0, // END_COLLECTION /* consumer */ - 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) - 0x09, 0x01, // USAGE (Consumer Control) - 0xa1, 0x01, // COLLECTION (Application) - 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3) - 0x15, 0x01, // LOGICAL_MINIMUM (0x1) - 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c) - 0x19, 0x01, // USAGE_MINIMUM (0x1) - 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x00, // INPUT (Data,Array,Abs) - 0xc0, // END_COLLECTION + 0x05, 0x0c, // USAGE_PAGE (Consumer Devices) + 0x09, 0x01, // USAGE (Consumer Control) + 0xa1, 0x01, // COLLECTION (Application) + 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3) + 0x15, 0x01, // LOGICAL_MINIMUM (0x1) + 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c) + 0x19, 0x01, // USAGE_MINIMUM (0x1) + 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x00, // INPUT (Data,Array,Abs) + 0xc0, // END_COLLECTION }; - -/* +/* * Descriptor for compite device: Keyboard + Mouse - * + * * contains: device, interface, HID and endpoint descriptors */ #if USB_CFG_DESCR_PROPS_CONFIGURATION -const PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */ - 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ - USBDESCR_CONFIG, /* descriptor type */ +const PROGMEM char usbDescriptorConfiguration[] = { + /* USB configuration descriptor */ + 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ + USBDESCR_CONFIG, /* descriptor type */ 9 + (9 + 9 + 7) + (9 + 9 + 7), 0, - //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0, - /* total length of data returned (including inlined descriptors) */ - 2, /* number of interfaces in this configuration */ - 1, /* index of this configuration */ - 0, /* configuration name string index */ -#if USB_CFG_IS_SELF_POWERED - (1 << 7) | USBATTR_SELFPOWER, /* attributes */ -#else - (1 << 7), /* attributes */ -#endif - USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */ + // 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0, + /* total length of data returned (including inlined descriptors) */ + 2, /* number of interfaces in this configuration */ + 1, /* index of this configuration */ + 0, /* configuration name string index */ +# if USB_CFG_IS_SELF_POWERED + (1 << 7) | USBATTR_SELFPOWER, /* attributes */ +# else + (1 << 7), /* attributes */ +# endif + USB_CFG_MAX_BUS_POWER / 2, /* max USB current in 2mA units */ /* * Keyboard interface */ /* Interface descriptor */ - 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ - USBDESCR_INTERFACE, /* descriptor type */ - 0, /* index of this interface */ - 0, /* alternate setting for this interface */ - USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */ - USB_CFG_INTERFACE_CLASS, - USB_CFG_INTERFACE_SUBCLASS, - USB_CFG_INTERFACE_PROTOCOL, - 0, /* string index for interface */ + 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ + USBDESCR_INTERFACE, /* descriptor type */ + 0, /* index of this interface */ + 0, /* alternate setting for this interface */ + USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */ + USB_CFG_INTERFACE_CLASS, USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */ /* HID descriptor */ - 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ - USBDESCR_HID, /* descriptor type: HID */ - 0x01, 0x01, /* BCD representation of HID version */ - 0x00, /* target country code */ - 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ - 0x22, /* descriptor type: report */ - sizeof(keyboard_hid_report), 0, /* total length of report descriptor */ - /* Endpoint descriptor */ -#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ - 7, /* sizeof(usbDescrEndpoint) */ - USBDESCR_ENDPOINT, /* descriptor type = endpoint */ - (char)0x81, /* IN endpoint number 1 */ - 0x03, /* attrib: Interrupt endpoint */ - 8, 0, /* maximum packet size */ - USB_CFG_INTR_POLL_INTERVAL, /* in ms */ -#endif + 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ + USBDESCR_HID, /* descriptor type: HID */ + 0x01, 0x01, /* BCD representation of HID version */ + 0x00, /* target country code */ + 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ + 0x22, /* descriptor type: report */ + sizeof(keyboard_hid_report), 0, /* total length of report descriptor */ +/* Endpoint descriptor */ +# if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ + 7, /* sizeof(usbDescrEndpoint) */ + USBDESCR_ENDPOINT, /* descriptor type = endpoint */ + (char)0x81, /* IN endpoint number 1 */ + 0x03, /* attrib: Interrupt endpoint */ + 8, 0, /* maximum packet size */ + USB_CFG_INTR_POLL_INTERVAL, /* in ms */ +# endif /* * Mouse interface */ /* Interface descriptor */ - 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ - USBDESCR_INTERFACE, /* descriptor type */ - 1, /* index of this interface */ - 0, /* alternate setting for this interface */ + 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ + USBDESCR_INTERFACE, /* descriptor type */ + 1, /* index of this interface */ + 0, /* alternate setting for this interface */ USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */ - 0x03, /* CLASS: HID */ - 0, /* SUBCLASS: none */ - 0, /* PROTOCOL: none */ - 0, /* string index for interface */ + 0x03, /* CLASS: HID */ + 0, /* SUBCLASS: none */ + 0, /* PROTOCOL: none */ + 0, /* string index for interface */ /* HID descriptor */ - 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ - USBDESCR_HID, /* descriptor type: HID */ - 0x01, 0x01, /* BCD representation of HID version */ - 0x00, /* target country code */ - 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ - 0x22, /* descriptor type: report */ - sizeof(mouse_hid_report), 0, /* total length of report descriptor */ -#if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */ + 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ + USBDESCR_HID, /* descriptor type: HID */ + 0x01, 0x01, /* BCD representation of HID version */ + 0x00, /* target country code */ + 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ + 0x22, /* descriptor type: report */ + sizeof(mouse_hid_report), 0, /* total length of report descriptor */ +# if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */ /* Endpoint descriptor */ - 7, /* sizeof(usbDescrEndpoint) */ - USBDESCR_ENDPOINT, /* descriptor type = endpoint */ + 7, /* sizeof(usbDescrEndpoint) */ + USBDESCR_ENDPOINT, /* descriptor type = endpoint */ (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */ - 0x03, /* attrib: Interrupt endpoint */ - 8, 0, /* maximum packet size */ - USB_CFG_INTR_POLL_INTERVAL, /* in ms */ -#endif + 0x03, /* attrib: Interrupt endpoint */ + 8, 0, /* maximum packet size */ + USB_CFG_INTR_POLL_INTERVAL, /* in ms */ +# endif }; #endif - -USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) -{ +USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) { usbMsgLen_t len = 0; -/* - debug("usbFunctionDescriptor: "); - debug_hex(rq->bmRequestType); debug(" "); - debug_hex(rq->bRequest); debug(" "); - debug_hex16(rq->wValue.word); debug(" "); - debug_hex16(rq->wIndex.word); debug(" "); - debug_hex16(rq->wLength.word); debug("\n"); -*/ + /* + debug("usbFunctionDescriptor: "); + debug_hex(rq->bmRequestType); debug(" "); + debug_hex(rq->bRequest); debug(" "); + debug_hex16(rq->wValue.word); debug(" "); + debug_hex16(rq->wIndex.word); debug(" "); + debug_hex16(rq->wLength.word); debug("\n"); + */ switch (rq->wValue.bytes[1]) { #if USB_CFG_DESCR_PROPS_CONFIGURATION case USBDESCR_CONFIG: usbMsgPtr = (unsigned char *)usbDescriptorConfiguration; - len = sizeof(usbDescriptorConfiguration); + len = sizeof(usbDescriptorConfiguration); break; #endif case USBDESCR_HID: switch (rq->wValue.bytes[0]) { case 0: usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9); - len = 9; + len = 9; break; case 1: usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9); - len = 9; + len = 9; break; } break; @@ -504,15 +464,15 @@ USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) switch (rq->wIndex.word) { case 0: usbMsgPtr = (unsigned char *)keyboard_hid_report; - len = sizeof(keyboard_hid_report); + len = sizeof(keyboard_hid_report); break; case 1: usbMsgPtr = (unsigned char *)mouse_hid_report; - len = sizeof(mouse_hid_report); + len = sizeof(mouse_hid_report); break; } break; } - //debug("desc len: "); debug_hex(len); debug("\n"); + // debug("desc len: "); debug_hex(len); debug("\n"); return len; } diff --git a/tmk_core/protocol/vusb/vusb.h b/tmk_core/protocol/vusb/vusb.h index 5accf233b..7e3f8c394 100644 --- a/tmk_core/protocol/vusb/vusb.h +++ b/tmk_core/protocol/vusb/vusb.h @@ -20,8 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "host_driver.h" - host_driver_t *vusb_driver(void); -void vusb_transfer_keyboard(void); +void vusb_transfer_keyboard(void); #endif diff --git a/tmk_core/protocol/xt.h b/tmk_core/protocol/xt.h index 93bc5daf8..e7e1a13f4 100644 --- a/tmk_core/protocol/xt.h +++ b/tmk_core/protocol/xt.h @@ -39,33 +39,35 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef XT_H #define XT_H -#define XT_DATA_IN() do { \ - XT_DATA_DDR &= ~(1<<XT_DATA_BIT); \ - XT_DATA_PORT |= (1<<XT_DATA_BIT); \ -} while (0) - -#define XT_DATA_READ() (XT_DATA_PIN&(1<<XT_DATA_BIT)) - -#define XT_DATA_LO() do { \ - XT_DATA_PORT &= ~(1<<XT_DATA_BIT); \ - XT_DATA_DDR |= (1<<XT_DATA_BIT); \ -} while (0) - - -#define XT_CLOCK_IN() do { \ - XT_CLOCK_DDR &= ~(1<<XT_CLOCK_BIT); \ - XT_CLOCK_PORT |= (1<<XT_CLOCK_BIT); \ -} while (0) - -#define XT_CLOCK_READ() (XT_CLOCK_PIN&(1<<XT_CLOCK_BIT)) - -#define XT_CLOCK_LO() do { \ - XT_CLOCK_PORT &= ~(1<<XT_CLOCK_BIT); \ - XT_CLOCK_DDR |= (1<<XT_CLOCK_BIT); \ -} while (0) - - -void xt_host_init(void); +#define XT_DATA_IN() \ + do { \ + XT_DATA_DDR &= ~(1 << XT_DATA_BIT); \ + XT_DATA_PORT |= (1 << XT_DATA_BIT); \ + } while (0) + +#define XT_DATA_READ() (XT_DATA_PIN & (1 << XT_DATA_BIT)) + +#define XT_DATA_LO() \ + do { \ + XT_DATA_PORT &= ~(1 << XT_DATA_BIT); \ + XT_DATA_DDR |= (1 << XT_DATA_BIT); \ + } while (0) + +#define XT_CLOCK_IN() \ + do { \ + XT_CLOCK_DDR &= ~(1 << XT_CLOCK_BIT); \ + XT_CLOCK_PORT |= (1 << XT_CLOCK_BIT); \ + } while (0) + +#define XT_CLOCK_READ() (XT_CLOCK_PIN & (1 << XT_CLOCK_BIT)) + +#define XT_CLOCK_LO() \ + do { \ + XT_CLOCK_PORT &= ~(1 << XT_CLOCK_BIT); \ + XT_CLOCK_DDR |= (1 << XT_CLOCK_BIT); \ + } while (0) + +void xt_host_init(void); uint8_t xt_host_recv(void); #endif diff --git a/tmk_core/protocol/xt_interrupt.c b/tmk_core/protocol/xt_interrupt.c index 8276f96cd..51e52243f 100644 --- a/tmk_core/protocol/xt_interrupt.c +++ b/tmk_core/protocol/xt_interrupt.c @@ -44,12 +44,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "debug.h" static inline uint8_t pbuf_dequeue(void); -static inline void pbuf_enqueue(uint8_t data); -static inline bool pbuf_has_data(void); -static inline void pbuf_clear(void); +static inline void pbuf_enqueue(uint8_t data); +static inline bool pbuf_has_data(void); +static inline void pbuf_clear(void); -void xt_host_init(void) -{ +void xt_host_init(void) { XT_INT_INIT(); XT_INT_OFF(); @@ -71,8 +70,7 @@ void xt_host_init(void) } /* get data received by interrupt */ -uint8_t xt_host_recv(void) -{ +uint8_t xt_host_recv(void) { if (pbuf_has_data()) { return pbuf_dequeue(); } else { @@ -80,8 +78,7 @@ uint8_t xt_host_recv(void) } } -ISR(XT_INT_VECT) -{ +ISR(XT_INT_VECT) { /* * XT signal format consits of 10 or 9 clocks and sends start bits and 8-bit data, * which should be read on falling edge of clock. @@ -93,15 +90,13 @@ ISR(XT_INT_VECT) * * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol */ - static enum { - START, BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7 - } state = START; - static uint8_t data = 0; + static enum { START, BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7 } state = START; + static uint8_t data = 0; uint8_t dbit = XT_DATA_READ(); // This is needed if using PCINT which can be called on both falling and rising edge - //if (XT_CLOCK_READ()) return; + // if (XT_CLOCK_READ()) return; switch (state) { case START: @@ -110,14 +105,13 @@ ISR(XT_INT_VECT) break; case BIT0 ... BIT7: data >>= 1; - if (dbit) - data |= 0x80; + if (dbit) data |= 0x80; break; } if (state++ == BIT7) { pbuf_enqueue(data); state = START; - data = 0; + data = 0; } return; } @@ -126,48 +120,44 @@ ISR(XT_INT_VECT) * Ring buffer to store scan codes from keyboard *------------------------------------------------------------------*/ #define PBUF_SIZE 32 -static uint8_t pbuf[PBUF_SIZE]; -static uint8_t pbuf_head = 0; -static uint8_t pbuf_tail = 0; -static inline void pbuf_enqueue(uint8_t data) -{ +static uint8_t pbuf[PBUF_SIZE]; +static uint8_t pbuf_head = 0; +static uint8_t pbuf_tail = 0; +static inline void pbuf_enqueue(uint8_t data) { uint8_t sreg = SREG; cli(); uint8_t next = (pbuf_head + 1) % PBUF_SIZE; if (next != pbuf_tail) { pbuf[pbuf_head] = data; - pbuf_head = next; + pbuf_head = next; } else { dprintf("pbuf: full\n"); } SREG = sreg; } -static inline uint8_t pbuf_dequeue(void) -{ +static inline uint8_t pbuf_dequeue(void) { uint8_t val = 0; uint8_t sreg = SREG; cli(); if (pbuf_head != pbuf_tail) { - val = pbuf[pbuf_tail]; + val = pbuf[pbuf_tail]; pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; } SREG = sreg; return val; } -static inline bool pbuf_has_data(void) -{ +static inline bool pbuf_has_data(void) { uint8_t sreg = SREG; cli(); bool has_data = (pbuf_head != pbuf_tail); - SREG = sreg; + SREG = sreg; return has_data; } -static inline void pbuf_clear(void) -{ +static inline void pbuf_clear(void) { uint8_t sreg = SREG; cli(); pbuf_head = pbuf_tail = 0; - SREG = sreg; + SREG = sreg; } diff --git a/tmk_core/ring_buffer.h b/tmk_core/ring_buffer.h index 005d1be61..6bea3509b 100644 --- a/tmk_core/ring_buffer.h +++ b/tmk_core/ring_buffer.h @@ -5,47 +5,38 @@ *------------------------------------------------------------------*/ #define RBUF_SIZE 32 #include <util/atomic.h> -static uint8_t rbuf[RBUF_SIZE]; -static uint8_t rbuf_head = 0; -static uint8_t rbuf_tail = 0; -static inline void rbuf_enqueue(uint8_t data) -{ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - uint8_t next = (rbuf_head + 1) % RBUF_SIZE; - if (next != rbuf_tail) { - rbuf[rbuf_head] = data; - rbuf_head = next; - } else { - print("rbuf: full\n"); +static uint8_t rbuf[RBUF_SIZE]; +static uint8_t rbuf_head = 0; +static uint8_t rbuf_tail = 0; +static inline void rbuf_enqueue(uint8_t data) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + uint8_t next = (rbuf_head + 1) % RBUF_SIZE; + if (next != rbuf_tail) { + rbuf[rbuf_head] = data; + rbuf_head = next; + } else { + print("rbuf: full\n"); + } } - } } -static inline uint8_t rbuf_dequeue(void) -{ +static inline uint8_t rbuf_dequeue(void) { uint8_t val = 0; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - - if (rbuf_head != rbuf_tail) { - val = rbuf[rbuf_tail]; - rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + if (rbuf_head != rbuf_tail) { + val = rbuf[rbuf_tail]; + rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + } } - } return val; } -static inline bool rbuf_has_data(void) -{ - bool has_data; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - has_data = (rbuf_head != rbuf_tail); - } - return has_data; +static inline bool rbuf_has_data(void) { + bool has_data; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { has_data = (rbuf_head != rbuf_tail); } + return has_data; } -static inline void rbuf_clear(void) -{ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - rbuf_head = rbuf_tail = 0; - } +static inline void rbuf_clear(void) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { rbuf_head = rbuf_tail = 0; } } -#endif /* RING_BUFFER_H */ +#endif /* RING_BUFFER_H */ |