Browse Source

Fix rn42.h API

tags/v1.9
tmk 10 years ago
parent
commit
fa545c87f7
3 changed files with 60 additions and 59 deletions
  1. 25
    43
      keyboard/hhkb_rn42/main.c
  2. 31
    10
      keyboard/hhkb_rn42/rn42.c
  3. 4
    6
      keyboard/hhkb_rn42/rn42.h

+ 25
- 43
keyboard/hhkb_rn42/main.c View File

PORTD |= (1<<0); PORTD |= (1<<0);
DDRD &= ~(1<<1); DDRD &= ~(1<<1);
PORTD |= (1<<1); PORTD |= (1<<1);

// CTS control
CTS_INIT();
} }


static bool force_usb = false; static bool force_usb = false;
/* init modules */ /* init modules */
keyboard_init(); keyboard_init();


if (rn42_ready()) {
if (!rn42_rts()) {
host_set_driver(&rn42_driver); host_set_driver(&rn42_driver);
} else { } else {
host_set_driver(&lufa_driver); host_set_driver(&lufa_driver);
if (config_mode) { if (config_mode) {
while ((c = serial_recv2()) != -1) { while ((c = serial_recv2()) != -1) {
// without flow control it'll fail to receive data when flooded // without flow control it'll fail to receive data when flooded
CTS_HI();
rn42_cts_hi();
xprintf("%c", c); xprintf("%c", c);
CTS_LO();
rn42_cts_lo();
} }
} else { } else {
while ((c = serial_recv2()) != -1) { while ((c = serial_recv2()) != -1) {


/* Bluetooth mode when ready */ /* Bluetooth mode when ready */
if (!config_mode && !force_usb) { if (!config_mode && !force_usb) {
if (rn42_ready() && host_get_driver() != &rn42_driver) {
if (!rn42_rts() && host_get_driver() != &rn42_driver) {
clear_keyboard(); clear_keyboard();
host_set_driver(&rn42_driver); host_set_driver(&rn42_driver);
} else if (!rn42_ready() && host_get_driver() != &lufa_driver) {
} else if (rn42_rts() && host_get_driver() != &lufa_driver) {
clear_keyboard(); clear_keyboard();
host_set_driver(&lufa_driver); host_set_driver(&lufa_driver);
} }
} }
} }


static bool local_echo = false;

/******************************************************************************
* Command
******************************************************************************/
bool command_extra(uint8_t code) bool command_extra(uint8_t code)
{ {
static host_driver_t *prev_driver = &rn42_driver; static host_driver_t *prev_driver = &rn42_driver;
case KC_H: case KC_H:
case KC_SLASH: /* ? */ case KC_SLASH: /* ? */
print("\n\n----- Bluetooth RN-42 Help -----\n"); print("\n\n----- Bluetooth RN-42 Help -----\n");
print("w: toggle RN-42 config mode(enter/exit)\n");
print("l: toggle print module output(local echo)\n");
print("a: Bluetooth auto connect\n");
print("del: Bluetooth disconnect\n");
print("i: info\n");
print("Del: auto_connect/disconnect(enter/exit config mode)\n");
print("i: RN-42 info\n");
print("b: battery voltage\n"); print("b: battery voltage\n");


if (config_mode) { if (config_mode) {
return true; return true;
} else { } else {
print("u: force USB mode\n");
print("u: Force USB mode\n");
return false; // to display default command help return false; // to display default command help
} }
case KC_W:
if (!config_mode) {
print("\nEnter RN-42 config mode\n");
print("type $$$ to enter RN-42 command mode\n");
print("type Delete to disconnect Bluetooth connection\n");
case KC_DELETE:
if (rn42_autoconnecting()) {
rn42_disconnect();
print("\nRN-42: disconnect\n");
print("Enter config mode\n");
print("type $$$ to start and + for local echo\n");
command_state = CONSOLE; command_state = CONSOLE;
config_mode = true; config_mode = true;
prev_driver = host_get_driver(); prev_driver = host_get_driver();
clear_keyboard(); clear_keyboard();
host_set_driver(&rn42_config_driver);
host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
} else { } else {
print("\nExit RN-42 config mode\n");
rn42_autoconnect();
print("\nRN-42: auto_connect\n");
print("Exit config mode\n");
command_state = ONESHOT; command_state = ONESHOT;
config_mode = false; config_mode = false;
clear_keyboard(); clear_keyboard();
host_set_driver(prev_driver); host_set_driver(prev_driver);
} }
return true; return true;
case KC_L:
if (local_echo) {
print("local echo off\n");
local_echo = false;
} else {
print("local echo on\n");
local_echo = true;
}
return true;
case KC_U: case KC_U:
if (config_mode) return false; if (config_mode) return false;
if (force_usb) { if (force_usb) {
host_set_driver(&lufa_driver); host_set_driver(&lufa_driver);
} }
return true; return true;
case KC_A:
print("auto connect\n");
rn42_autoconnect();
return true;
case KC_DELETE:
print("disconnect\n");
rn42_disconnect();
//rn42_putc('\0'); // see 5.3.4.4 DISCONNECT KEY of User's Guide
return true;
case KC_I: case KC_I:
print("\nRN-42 info\n");
print("\n----- RN-42 info -----\n");
xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA"); xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
xprintf("force_usb: %X\n", force_usb); xprintf("force_usb: %X\n", force_usb);
xprintf("rn42_ready(): %X\n", rn42_ready());
xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
xprintf("rn42_rts(): %X\n", rn42_rts());
xprintf("config_mode: %X\n", config_mode); xprintf("config_mode: %X\n", config_mode);
return true; return true;
case KC_B: case KC_B:
switch (code) { switch (code) {
default: default:
rn42_putc(code2asc(code)); rn42_putc(code2asc(code));
if (local_echo) xprintf("%c", code2asc(code));
return true; return true;
} }
return false; return false;

+ 31
- 10
keyboard/hhkb_rn42/rn42.c View File



void rn42_init(void) void rn42_init(void)
{ {
// PF1: check RTS(active low)
DDRF &= ~(1<<1);
PORTF &= ~(1<<1);

// PF7: BT connection control(HiZ: connect, low: disconnect) // PF7: BT connection control(HiZ: connect, low: disconnect)
// JTAG disable for PORT F. write JTD bit twice within four cycles. // JTAG disable for PORT F. write JTD bit twice within four cycles.
MCUCR |= (1<<JTD); MCUCR |= (1<<JTD);
MCUCR |= (1<<JTD); MCUCR |= (1<<JTD);
rn42_autoconnect(); rn42_autoconnect();


// PD5: CTS (low: allow to send, high:not allowed)
// PF1: RTS(low: allowed to send, high: not allowed)
DDRF &= ~(1<<1);
PORTF &= ~(1<<1);

// PD5: CTS(low: allow to send, high:not allow)
DDRD |= (1<<5); DDRD |= (1<<5);
PORTD &= ~(1<<5); PORTD &= ~(1<<5);


serial_send(c); serial_send(c);
} }


bool rn42_autoconnecting(void)
{
// GPIO6 for control connection(high: auto connect, low: disconnect)
// Note that this needs config: SM,4(Auto-Connect DTR Mode)
return (PORTF & (1<<7) ? true : false);
}

void rn42_autoconnect(void) void rn42_autoconnect(void)
{ {
DDRF &= ~(1<<7);
PORTF &= ~(1<<7);
// hi to auto connect
DDRF |= (1<<7);
PORTF |= (1<<7);
} }


void rn42_disconnect(void) void rn42_disconnect(void)
{ {
// low to disconnect
DDRF |= (1<<7); DDRF |= (1<<7);
PORTF &= ~(1<<7); PORTF &= ~(1<<7);
} }


bool rn42_ready(void)
bool rn42_rts(void)
{
// low when RN-42 is powered and ready to receive
return PINF&(1<<1);
}

void rn42_cts_hi(void)
{ {
// RTS low
return PINF&(1<<1) ? false : true;
// not allow to send
PORTD |= (1<<5);
}

void rn42_cts_lo(void)
{
// allow to send
PORTD &= ~(1<<5);
} }





+ 4
- 6
keyboard/hhkb_rn42/rn42.h View File



#include <stdbool.h> #include <stdbool.h>


// RN-42 CTS pin
#define CTS_INIT() (DDRD |= (1<<5))
#define CTS_HI() (PORTD |= (1<<5))
#define CTS_LO() (PORTD &= ~(1<<5))

host_driver_t rn42_driver; host_driver_t rn42_driver;
host_driver_t rn42_config_driver; host_driver_t rn42_config_driver;


void rn42_init(void); void rn42_init(void);
void rn42_putc(uint8_t c); void rn42_putc(uint8_t c);
bool rn42_autoconnecting(void);
void rn42_autoconnect(void); void rn42_autoconnect(void);
void rn42_disconnect(void); void rn42_disconnect(void);
bool rn42_ready(void);
bool rn42_rts(void);
void rn42_cts_hi(void);
void rn42_cts_lo(void);


#endif #endif

Loading…
Cancel
Save