1
0

Fix rn42.h API

This commit is contained in:
tmk 2014-07-17 13:06:48 +09:00
parent 862f519e24
commit fa545c87f7
3 changed files with 60 additions and 59 deletions

View File

@ -45,9 +45,6 @@ static void SetupHardware(void)
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;
@ -75,7 +72,7 @@ int main(void)
/* 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);
@ -112,9 +109,9 @@ int main(void)
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) {
@ -148,10 +145,10 @@ int main(void)
/* 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);
} }
@ -159,7 +156,10 @@ int main(void)
} }
} }
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;
@ -167,46 +167,37 @@ bool command_extra(uint8_t code)
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("Del: auto_connect/disconnect(enter/exit config mode)\n");
print("l: toggle print module output(local echo)\n"); print("i: RN-42 info\n");
print("a: Bluetooth auto connect\n");
print("del: Bluetooth disconnect\n");
print("i: 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: case KC_DELETE:
if (!config_mode) { if (rn42_autoconnecting()) {
print("\nEnter RN-42 config mode\n"); rn42_disconnect();
print("type $$$ to enter RN-42 command mode\n"); print("\nRN-42: disconnect\n");
print("type Delete to disconnect Bluetooth connection\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) {
@ -219,20 +210,12 @@ bool command_extra(uint8_t code)
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:
@ -266,7 +249,6 @@ bool command_console_extra(uint8_t code)
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;

View File

@ -24,17 +24,17 @@ host_driver_t rn42_driver = {
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);
@ -46,22 +46,43 @@ void rn42_putc(uint8_t c)
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); // hi to auto connect
PORTF &= ~(1<<7); 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)
{ {
// RTS low // low when RN-42 is powered and ready to receive
return PINF&(1<<1) ? false : true; return PINF&(1<<1);
}
void rn42_cts_hi(void)
{
// not allow to send
PORTD |= (1<<5);
}
void rn42_cts_lo(void)
{
// allow to send
PORTD &= ~(1<<5);
} }

View File

@ -3,18 +3,16 @@
#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