crashtest-r0ket/firmware/applications/bridge.c

220 lines
5.5 KiB
C
Raw Permalink Normal View History

2011-07-03 20:55:56 +00:00
#include <sysinit.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/allfonts.h"
2011-12-11 15:13:46 +00:00
#include "basic/config.h"
#include "basic/byteorder.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "funk/nrf24l01p.h"
#include "usbcdc/usb.h"
#include "usbcdc/usbcore.h"
#include "usbcdc/usbhw.h"
#include "usbcdc/cdcuser.h"
#include "usbcdc/cdc_buf.h"
#include "usbcdc/util.h"
#include "core/ssp/ssp.h"
2011-12-11 15:13:46 +00:00
#if CFG_USBMSC
#error "MSC is defined"
#endif
2011-07-03 20:55:56 +00:00
2011-12-11 15:13:46 +00:00
#if !CFG_USBCDC
#error "CDC is not defined"
#endif
#define CHANNEL 81
#define MAC "\x1\x2\x3\x2\x1"
2011-12-11 17:24:11 +00:00
#define SERIAL_NONE 0
#define SERIAL_ESCAPE '\\'
#define SERIAL_STOP '0'
#define SERIAL_PACKETLEN 128
2011-12-11 15:13:46 +00:00
struct NRF_CFG config = {
.channel= CHANNEL,
.txmac= MAC,
.nrmacs=1,
.mac0= MAC,
2011-12-12 17:01:01 +00:00
.maclen ="\x20",
2011-12-11 15:13:46 +00:00
};
2011-12-11 17:24:11 +00:00
uint8_t serialmsg_message[SERIAL_PACKETLEN];
2011-12-11 15:13:46 +00:00
uint8_t serialmsg_len = 0;
void serialmsg_init(void);
uint8_t serialmsg_put(uint8_t data);
char snd_pkt_no_crc(int size, uint8_t * pkt);
2011-12-11 17:24:11 +00:00
void dump_encoded(int len, uint8_t *data);
2011-12-11 15:13:46 +00:00
#define INPUTLEN 99
2011-07-03 20:55:56 +00:00
/**************************************************************************/
2011-12-17 00:59:54 +00:00
void main_bridge(void)
2011-12-11 15:13:46 +00:00
{
GLOBAL(daytrig)=10;
GLOBAL(lcdbacklight)=10;
2011-12-17 00:59:54 +00:00
GLOBAL(privacy) = 3;
2011-12-11 17:24:11 +00:00
char input[64];
char led1=0;
char led2=0;
2011-12-11 15:13:46 +00:00
usbCDCInit();
delayms(500);
nrf_init();
nrf_config_set(&config);
nrf_rcv_pkt_start();
while(1){
2011-12-11 17:24:11 +00:00
int l, i, status;
2011-12-11 15:13:46 +00:00
CDC_OutBufAvailChar (&l);
if(l>0){
gpioSetValue (RB_LED0, led1);led1=1-led1;
2011-12-11 15:13:46 +00:00
CDC_RdOutBuf (input, &l);
for(i=0; i<l; i++){
uint8_t cmd = serialmsg_put(input[i]);
2011-12-11 17:24:11 +00:00
if( cmd != SERIAL_NONE ){
2011-12-11 15:13:46 +00:00
switch( cmd ){
case '1':
// can we loose packets here?
nrf_rcv_pkt_end();
status=snd_pkt_no_crc(serialmsg_len, serialmsg_message);
2011-12-12 17:01:01 +00:00
//status=nrf_snd_pkt_crc(serialmsg_len, serialmsg_message);
2011-12-11 15:13:46 +00:00
nrf_rcv_pkt_start();
break;
case '3':
memcpy(config.txmac, serialmsg_message, 5);
nrf_config_set(&config);
break;
case '4':
memcpy(config.mac0, serialmsg_message, 5);
nrf_config_set(&config);
break;
case '5':
config.channel=serialmsg_message[0];
nrf_config_set(&config);
break;
case '6':
config.maclen[0]=serialmsg_message[0];
nrf_config_set(&config);
break;
case '7':
puts("\\7");
char s[sizeof(uint32_t)+1];
*((uint32_t*)s) =GetUUID32();
s[sizeof(uint32_t)]=0;
puts(s);
puts("\\0");
break;
2011-12-11 15:13:46 +00:00
};
puts("\\2\\0");
}
}
}
int len;
uint8_t buf[32];
len=nrf_rcv_pkt_poll(sizeof(buf),buf);
if( len > 0 ){
gpioSetValue (RB_LED2, led2);led2=1-led2;
2011-12-11 15:13:46 +00:00
puts("\\1");
2011-12-11 17:24:11 +00:00
dump_encoded(len, buf);
2011-12-11 15:13:46 +00:00
puts("\\0");
}
2011-07-03 20:55:56 +00:00
}
}
2011-12-11 17:24:11 +00:00
void dump_encoded(int len, uint8_t *data)
{
int i=0,j=0;
uint8_t buf[SERIAL_PACKETLEN*2];
for(i=0; i<len; i++){
if( data[i] == SERIAL_ESCAPE ){
buf[j++] = SERIAL_ESCAPE;
}
buf[j++] = data[i];
}
2011-12-11 17:38:40 +00:00
CDC_WrInBuf((char*)buf, &j);
2011-12-11 17:24:11 +00:00
}
2011-12-17 00:59:54 +00:00
void tick_bridge(void){
2011-07-03 20:55:56 +00:00
return;
};
2011-12-11 15:13:46 +00:00
inline void xmit_spi(uint8_t dat) {
sspSend(0, (uint8_t*) &dat, 1);
}
inline void rcv_spi(uint8_t *dat) {
sspReceive(0, dat, 1);
}
#define CS_LOW() gpioSetValue(RB_SPI_NRF_CS, 0)
#define CS_HIGH() gpioSetValue(RB_SPI_NRF_CS, 1)
#define CE_LOW() gpioSetValue(RB_NRF_CE, 0)
#define CE_HIGH() gpioSetValue(RB_NRF_CE, 1)
char snd_pkt_no_crc(int size, uint8_t * pkt)
{
if(size > MAX_PKT)
size=MAX_PKT;
nrf_write_reg(R_CONFIG,
R_CONFIG_PWR_UP| // Power on
R_CONFIG_EN_CRC // CRC on, single byte
);
CS_LOW();
xmit_spi(C_W_TX_PAYLOAD);
sspSend(0,pkt,size);
CS_HIGH();
CE_HIGH();
delayms(1); // Send it. (only needs >10ys, i think)
CE_LOW();
return nrf_cmd_status(C_NOP);
};
void serialmsg_init(void)
{
serialmsg_len = 0;
}
2011-12-11 17:24:11 +00:00
//returns the message type or SERIAL_NONE
2011-12-11 15:13:46 +00:00
uint8_t serialmsg_put(uint8_t data)
{
static uint8_t escaped = 0;
2011-12-11 17:24:11 +00:00
static uint8_t msgtype = SERIAL_NONE;
2011-12-11 15:13:46 +00:00
2011-12-11 17:24:11 +00:00
if( data == SERIAL_ESCAPE && escaped == 0 ){
2011-12-11 15:13:46 +00:00
//a control code will follow
escaped = 1;
2011-12-11 17:24:11 +00:00
return SERIAL_NONE;
2011-12-11 15:13:46 +00:00
}else if( escaped ){
escaped = 0;
2011-12-11 17:24:11 +00:00
if( data != SERIAL_ESCAPE ){
if( data == SERIAL_STOP ){
2011-12-11 15:13:46 +00:00
uint8_t tmp = msgtype;
2011-12-11 17:24:11 +00:00
msgtype = SERIAL_NONE;
2011-12-11 15:13:46 +00:00
return tmp;
}
msgtype = data;
serialmsg_len=0;
2011-12-11 17:24:11 +00:00
return SERIAL_NONE;
2011-12-11 15:13:46 +00:00
}
}
serialmsg_message[serialmsg_len++] = data;
//prevent a buffer overflow
2011-12-11 17:24:11 +00:00
if( serialmsg_len == SERIAL_PACKETLEN )
2011-12-11 15:13:46 +00:00
serialmsg_len--;
2011-12-11 17:24:11 +00:00
return SERIAL_NONE;
2011-12-11 15:13:46 +00:00
}