Skip to content

Commit

Permalink
Merge branch 'rn42' into merge_rn42
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
	common.mk
	common/debug_config.h
	common/print.h
  • Loading branch information
yashikno committed Nov 24, 2014
2 parents eb90ed6 + 60096e1 commit 3639509
Show file tree
Hide file tree
Showing 92 changed files with 4,462 additions and 402 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
*.sym
tags
*~
build/
*.bak
12 changes: 6 additions & 6 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ SRC += $(COMMON_DIR)/host.c \
$(COMMON_DIR)/action_layer.c \
$(COMMON_DIR)/action_util.c \
$(COMMON_DIR)/keymap.c \
$(COMMON_DIR)/timer.c \
$(COMMON_DIR)/print.c \
$(COMMON_DIR)/debug.c \
$(COMMON_DIR)/bootloader.c \
$(COMMON_DIR)/suspend.c \
$(COMMON_DIR)/xprintf.S \
$(COMMON_DIR)/util.c
$(COMMON_DIR)/util.c \
$(COMMON_DIR)/avr/suspend.c \
$(COMMON_DIR)/avr/xprintf.S \
$(COMMON_DIR)/avr/timer.c \
$(COMMON_DIR)/avr/bootloader.c


# Option modules
ifdef BOOTMAGIC_ENABLE
SRC += $(COMMON_DIR)/bootmagic.c
SRC += $(COMMON_DIR)/eeconfig.c
SRC += $(COMMON_DIR)/avr/eeconfig.c
OPT_DEFS += -DBOOTMAGIC_ENABLE
endif

Expand Down
2 changes: 1 addition & 1 deletion common/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ void clear_keyboard_but_mods(void)
#endif
}

bool is_tap_key(key_t key)
bool is_tap_key(keypos_t key)
{
action_t action = layer_switch_get_action(key);

Expand Down
13 changes: 10 additions & 3 deletions common/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ along with this program. If not, see <http:https://www.gnu.org/licenses/>.
#include "action_macro.h"


#ifdef __cplusplus
extern "C" {
#endif

/* tapping count and state */
typedef struct {
bool interrupted :1;
Expand All @@ -42,12 +46,11 @@ typedef struct {
#endif
} keyrecord_t;


/* Execute action per keyevent */
void action_exec(keyevent_t event);

/* action for key */
action_t action_for_key(uint8_t layer, key_t key);
action_t action_for_key(uint8_t layer, keypos_t key);

/* macro */
const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
Expand All @@ -65,11 +68,15 @@ void unregister_mods(uint8_t mods);
void clear_keyboard(void);
void clear_keyboard_but_mods(void);
void layer_switch(uint8_t new_layer);
bool is_tap_key(key_t key);
bool is_tap_key(keypos_t key);

/* debug */
void debug_event(keyevent_t event);
void debug_record(keyrecord_t record);
void debug_action(action_t action);

#ifdef __cplusplus
}
#endif

#endif /* ACTION_H */
2 changes: 1 addition & 1 deletion common/action_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void layer_debug(void)



action_t layer_switch_get_action(key_t key)
action_t layer_switch_get_action(keypos_t key)
{
action_t action;
action.code = ACTION_TRANSPARENT;
Expand Down
2 changes: 1 addition & 1 deletion common/action_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ void layer_xor(uint32_t state);


/* return action depending on current layer status */
action_t layer_switch_get_action(key_t key);
action_t layer_switch_get_action(keypos_t key);

#endif
8 changes: 4 additions & 4 deletions common/action_macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
*/
#include <util/delay.h>
#include "action.h"
#include "action_util.h"
#include "action_macro.h"
#include "wait.h"

#ifdef DEBUG_ACTION
#include "debug.h"
Expand All @@ -28,7 +28,7 @@ along with this program. If not, see <http:https://www.gnu.org/licenses/>.

#ifndef NO_ACTION_MACRO

#define MACRO_READ() (macro = pgm_read_byte(macro_p++))
#define MACRO_READ() (macro = MACRO_GET(macro_p++))
void action_macro_play(const macro_t *macro_p)
{
macro_t macro = END;
Expand Down Expand Up @@ -58,7 +58,7 @@ void action_macro_play(const macro_t *macro_p)
case WAIT:
MACRO_READ();
dprintf("WAIT(%u)\n", macro);
{ uint8_t ms = macro; while (ms--) _delay_ms(1); }
{ uint8_t ms = macro; while (ms--) wait_ms(1); }
break;
case INTERVAL:
interval = MACRO_READ();
Expand All @@ -77,7 +77,7 @@ void action_macro_play(const macro_t *macro_p)
return;
}
// interval
{ uint8_t ms = interval; while (ms--) _delay_ms(1); }
{ uint8_t ms = interval; while (ms--) wait_ms(1); }
}
}
#endif
8 changes: 4 additions & 4 deletions common/action_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ along with this program. If not, see <http:https://www.gnu.org/licenses/>.
#ifndef ACTION_MACRO_H
#define ACTION_MACRO_H
#include <stdint.h>
#include <avr/pgmspace.h>
#include "progmem.h"


#define MACRO_NONE 0
#define MACRO(...) ({ static const macro_t __m[] PROGMEM = { __VA_ARGS__ }; &__m[0]; })

#define MACRO_NONE 0
#define MACRO(...) ({ static const macro_t __m[] PROGMEM = { __VA_ARGS__ }; &__m[0]; })
#define MACRO_GET(p) pgm_read_byte(p)

typedef uint8_t macro_t;

Expand Down
20 changes: 10 additions & 10 deletions common/action_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static uint8_t real_mods = 0;
static uint8_t weak_mods = 0;

#ifdef USB_6KRO_ENABLE
#define RO_ADD(a, b) ((a + b) % REPORT_KEYS)
#define RO_SUB(a, b) ((a - b + REPORT_KEYS) % REPORT_KEYS)
#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;
Expand Down Expand Up @@ -98,7 +98,7 @@ void del_key(uint8_t key)
void clear_keys(void)
{
// not clear mods
for (int8_t i = 1; i < REPORT_SIZE; i++) {
for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
keyboard_report->raw[i] = 0;
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ void clear_oneshot_mods(void)
uint8_t has_anykey(void)
{
uint8_t cnt = 0;
for (uint8_t i = 1; i < REPORT_SIZE; i++) {
for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
if (keyboard_report->raw[i])
cnt++;
}
Expand All @@ -162,7 +162,7 @@ uint8_t get_first_key(void)
#ifdef NKRO_ENABLE
if (keyboard_nkro) {
uint8_t i = 0;
for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
;
return i<<3 | biton(keyboard_report->nkro.bits[i]);
}
Expand Down Expand Up @@ -234,15 +234,15 @@ static inline void add_key_byte(uint8_t code)
#else
int8_t i = 0;
int8_t empty = -1;
for (; i < REPORT_KEYS; i++) {
for (; i < KEYBOARD_REPORT_KEYS; i++) {
if (keyboard_report->keys[i] == code) {
break;
}
if (empty == -1 && keyboard_report->keys[i] == 0) {
empty = i;
}
}
if (i == REPORT_KEYS) {
if (i == KEYBOARD_REPORT_KEYS) {
if (empty != -1) {
keyboard_report->keys[empty] = code;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ static inline void del_key_byte(uint8_t code)
} while (i != cb_tail);
}
#else
for (uint8_t i = 0; i < REPORT_KEYS; i++) {
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
if (keyboard_report->keys[i] == code) {
keyboard_report->keys[i] = 0;
}
Expand All @@ -289,7 +289,7 @@ static inline void del_key_byte(uint8_t code)
#ifdef NKRO_ENABLE
static inline void add_key_bit(uint8_t code)
{
if ((code>>3) < REPORT_BITS) {
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);
Expand All @@ -298,7 +298,7 @@ static inline void add_key_bit(uint8_t code)

static inline void del_key_bit(uint8_t code)
{
if ((code>>3) < REPORT_BITS) {
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);
Expand Down
9 changes: 9 additions & 0 deletions common/action_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ along with this program. If not, see <http:https://www.gnu.org/licenses/>.
#include <stdint.h>
#include "report.h"

#ifdef __cplusplus
extern "C" {
#endif

extern report_keyboard_t *keyboard_report;

void send_keyboard_report(void);
Expand Down Expand Up @@ -54,4 +58,9 @@ void oneshot_disable(void);
uint8_t has_anykey(void);
uint8_t has_anymod(void);
uint8_t get_first_key(void);

#ifdef __cplusplus
}
#endif

#endif
File renamed without changes.
File renamed without changes.
117 changes: 117 additions & 0 deletions common/avr/suspend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <stdbool.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include "matrix.h"
#include "action.h"
#include "backlight.h"
#include "suspend_avr.h"
#include "suspend.h"
#ifdef PROTOCOL_LUFA
#include "lufa.h"
#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" \
)


void suspend_idle(uint8_t time)
{
cli();
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}

/* Power down MCU with watchdog timer
* wdto: watchdog timer timeout defined in <avr/wdt.h>
* WDTO_15MS
* WDTO_30MS
* WDTO_60MS
* WDTO_120MS
* WDTO_250MS
* WDTO_500MS
* WDTO_1S
* WDTO_2S
* WDTO_4S
* WDTO_8S
*/
void suspend_power_down(uint8_t wdto)
{
#ifdef PROTOCOL_LUFA
if (USB_DeviceState == DEVICE_STATE_Configured) return;
#endif

// Watchdog Interrupt Mode
wdt_intr_enable(wdto);

// TODO: more power saving
// See PicoPower application note
// - I/O port input with pullup
// - prescale clock
// - BOD disable
// - Power Reduction Register PRR

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sei();
sleep_cpu();
sleep_disable();

// Disable watchdog after sleep
wdt_disable();
}

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;
}

// run immediately after wakeup
void suspend_wakeup_init(void)
{
// clear keyboard state
clear_keyboard();
#ifdef BACKLIGHT_ENABLE
backlight_init();
#endif
}

#ifndef NO_SUSPEND_POWER_DOWN
/* watchdog timeout */
ISR(WDT_vect)
{
/* wakeup from MCU sleep mode */
/*
// blink LED
static uint8_t led_state = 0;
static uint8_t led_count = 0;
led_count++;
if ((led_count & 0x07) == 0) {
led_set((led_state ^= (1<<USB_LED_CAPS_LOCK)));
}
*/
}
#endif
Loading

0 comments on commit 3639509

Please sign in to comment.