Merge git://github.com/r0ket/r0ket

This commit is contained in:
bernd 2011-07-29 04:38:12 +02:00
commit 3f7ac1c777
14 changed files with 690 additions and 76 deletions

View File

@ -41,6 +41,7 @@ LDLIBS += -Lbasic -lbasic
LDLIBS += -Llcd -llcd LDLIBS += -Llcd -llcd
LDLIBS += -Lcore -lcore LDLIBS += -Lcore -lcore
LDLIBS += -Lusb -lusb LDLIBS += -Lusb -lusb
LDLIBS += -lbasic
OCFLAGS = --strip-unneeded OCFLAGS = --strip-unneeded
SUBDIRS?= $(foreach lib,$(LIBS),$(dir $(lib))) SUBDIRS?= $(foreach lib,$(LIBS),$(dir $(lib)))

View File

@ -1 +0,0 @@
../tester/config.c

View File

@ -0,0 +1,38 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/print.h"
#include "lcd/display.h"
#include "filesystem/ff.h"
#include <string.h>
/**************************************************************************/
void readcfg(void) {
readConfig();
};
void savecfg(void){
saveConfig();
};
void applycfg(void){
applyConfig();
};
void show(void){
lcdClear();
lcdPrint("time:"); lcdPrintInt(globalconfig.time); lcdNl();
lcdPrint("btrig:"); lcdPrintInt(globalconfig.backlighttrigger); lcdNl();
lcdPrint("bval:"); lcdPrintInt(globalconfig.backlightvalue); lcdNl();
lcdPrint("lcd:"); lcdPrintInt(globalconfig.lcdstate); lcdNl();
lcdPrint("priv:"); lcdPrintInt(globalconfig.privacy); lcdNl();
lcdRefresh();
};
void lcdmirror(void){
lcdToggleFlag(LCD_MIRRORX);
};

View File

@ -0,0 +1,274 @@
#include <sysinit.h>
#include "basic/basic.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 <string.h>
#define REMOTE_CHANNEL 91
#define REMOTE_MAC "REM0T"
#if CFG_USBMSC
#error "MSC is defined"
#endif
#if !CFG_USBCDC
#error "CDC is not defined"
#endif
/**************************************************************************/
uint32_t const remotekey[4] = {
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
};
void r_init(void){
nrf_init();
struct NRF_CFG config = {
.channel= REMOTE_CHANNEL,
.txmac= REMOTE_MAC,
.nrmacs=1,
.mac0= REMOTE_MAC,
.maclen ="\x10",
};
nrf_config_set(&config);
};
void s_init(void){
usbCDCInit();
nrf_init();
struct NRF_CFG config = {
.channel= REMOTE_CHANNEL,
.txmac= REMOTE_MAC,
.nrmacs=1,
.mac0= REMOTE_MAC,
.maclen ="\x10",
};
nrf_config_set(&config);
};
void process(uint8_t * input){
__attribute__ ((aligned (4))) uint8_t buf[32];
puts("process: ");
puts(input);
puts("\r\n");
if(input[0]=='M'){
buf[0]=0x10; // Length: 16 bytes
buf[1]='M'; // Proto
buf[2]=0x01;
buf[3]=0x01; // Unused
uint32touint8p(0,buf+4);
uint32touint8p(0x41424344,buf+8);
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
nrf_snd_pkt_crc_encr(16,buf,remotekey);
nrf_rcv_pkt_start();
};
};
#define INPUTLEN 99
void r_recv(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
uint8_t input[INPUTLEN+1];
int inputptr=0;
nrf_rcv_pkt_start();
puts("D start");
getInputWaitRelease();
while(!getInputRaw()){
delayms(100);
// Input
int l=INPUTLEN-inputptr;
CDC_OutBufAvailChar (&l);
if(l>0){
CDC_RdOutBuf (input+inputptr, &l);
input[inputptr+l+1]=0;
for(int i=0;i<l;i++){
if(input[inputptr+i] =='\r'){
input[inputptr+i]=0;
process(input);
if(i<l)
memmove(input,input+inputptr+i+1,l-i);
inputptr=-i-1;
break;
};
};
};
inputptr+=l;
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,remotekey);
// Receive
if(len<=0){
delayms(10);
continue;
};
if(buf[1]=='C'){ // Cursor
puts("C ");
puts(IntToStrX( buf[2],2 ));
puts(" ");
puts(IntToStrX( uint8ptouint32(buf+4), 8 ));
puts(" ");
puts(IntToStrX( uint8ptouint32(buf+8), 8 ));
}else{
puts("U ");
// puts("[");puts(IntToStrX(len,2));puts("] ");
puts(IntToStrX( *(int*)(buf+ 0),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 1),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 2),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 3),2 ));
puts(" ");
puts(IntToStrX( uint8ptouint32(buf+4),8 ));
puts(".");
puts(IntToStrX( uint8ptouint32(buf+8),8 ));
puts(" ");
puts(IntToStrX( uint8ptouint32(buf+10),4 ));
};
puts("\r\n");
};
nrf_rcv_pkt_end();
puts("D exit");
}
void r_s1(void){
static int ctr=1;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
buf[0]=0x10; // Length: 16 bytes
buf[1]='1'; // Proto
buf[2]=0x00;
buf[3]=0x00; // Unused
uint32touint8p(ctr++,buf+4);
uint32touint8p(0x5ec,buf+8);
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc_encr(16,buf,remotekey);
lcdClear();
lcdPrint("F-St:"); lcdPrintInt(status);
lcdDisplay();
};
void r_s2(void){
static int ctr=1;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
buf[0]=0x10; // Length: 16 bytes
buf[1]='C'; // Proto
buf[2]=0x00;
buf[3]=0x00; // Unused
uint32touint8p(ctr++,buf+4);
uint32touint8p(0x5ec,buf+8);
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc_encr(16,buf,remotekey);
buf[0]=0x10; // Length: 16 bytes
buf[1]='I'; // Proto
buf[2]=0x00;
buf[3]=0x00; // Unused
uint32touint8p(ctr++,buf+4);
uint32touint8p(0x5ec,buf+8);
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc_encr(16,buf,remotekey);
lcdClear();
lcdPrint("F-St:"); lcdPrintInt(status);
lcdDisplay();
};
void r_send(void){
int ctr=1;
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
int status;
while(1){
buf[0]=0x10; // Length: 16 bytes
buf[1]='C'; // Proto
buf[2]=getInputRaw();
buf[3]=0x00; // Unused
ctr++;
*(int*)(buf+4)=ctr;
/*
buf[4]=0x00; // ctr
buf[5]=0x00; // ctr
buf[6]=0x00; // ctr
buf[7]=ctr++; // ctr
*/
buf[8]=0x0; // Object id
buf[9]=0x0;
buf[10]=0x05;
buf[11]=0xec;
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
lcdClear();
lcdPrint("Key:"); lcdPrintInt(buf[2]); lcdNl();
if(buf[2]==BTN_ENTER)
break;
status=nrf_snd_pkt_crc_encr(16,buf,remotekey);
lcdPrint("F-St:"); lcdPrintInt(status);
lcdDisplay();
len=nrf_rcv_pkt_time_encr(100,sizeof(buf),buf,remotekey);
if(len>0){
lcdPrint("Got!");
lcdDisplay();
break;
};
delayms(10);
};
};

View File

@ -7,79 +7,45 @@
#include "funk/nrf24l01p.h" #include "funk/nrf24l01p.h"
#include "core/usbcdc/usb.h" #include "usbcdc/usb.h"
#include "core/usbcdc/usbcore.h" #include "usbcdc/usbcore.h"
#include "core/usbcdc/usbhw.h" #include "usbcdc/usbhw.h"
#include "core/usbcdc/cdcuser.h" #include "usbcdc/cdcuser.h"
#include "core/usbcdc/cdc_buf.h" #include "usbcdc/cdc_buf.h"
#include "usbcdc/util.h"
#include <string.h> #include <string.h>
#define BEACON_CHANNEL 81
#define BEACON_MAC "\x1\x2\x3\x2\1"
uint32_t const testkey[4] = {
0xB4595344,0xD3E119B6,0xA814D0EC,0xEFF5A24E
};
#if CFG_USBMSC #if CFG_USBMSC
#error "MSC is defined #error "MSC is defined"
#endif #endif
#if !CFG_USBCDC #if !CFG_USBCDC
#error "CDC is not defined #error "CDC is not defined"
#endif #endif
/**************************************************************************/ /**************************************************************************/
volatile unsigned int lastTick;
int puts(const char * str)
{
// There must be at least 1ms between USB frames (of up to 64 bytes)
// This buffers all data and writes it out from the buffer one frame
// and one millisecond at a time
if (USB_Configuration)
{
while(*str)
cdcBufferWrite(*str++);
// Check if we can flush the buffer now or if we need to wait
unsigned int currentTick = systickGetTicks();
if (currentTick != lastTick)
{
uint8_t frame[64];
uint32_t bytesRead = 0;
while (cdcBufferDataPending())
{
// Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
systickDelay(1);
}
lastTick = currentTick;
}
}
return 0;
}
void ser_enable(void) {
void f_ser(void) { usbCDCInit();
//lastTick = systickGetTicks(); // Used to control output/printf timing
CDC_Init(); // Initialise VCOM
USB_Init(); // USB Initialization
lcdPrintln("preconnect");
USB_Connect(TRUE); // USB Connect
lcdPrintln("postconnect");
// Wait until USB is configured or timeout occurs
uint32_t usbTimeout = 0;
// while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) {
// if (USB_Configuration) break;
// delayms(10); // Wait 10ms
// usbTimeout++;
// }
lcdPrintln("fini");
}; };
void f_disconnect(void) { void ser_disable(void) {
USB_Connect(FALSE); usbCDCOff();
}; };
#define LEN 10 #define myLEN 10
void f_sread(){ void ser_read(){
uint8_t buf[LEN+1]; uint8_t buf[myLEN+1];
int l=LEN; int l=myLEN;
lcdPrint("Bytes:"); lcdPrint("Bytes:");
CDC_OutBufAvailChar (&l); CDC_OutBufAvailChar (&l);
@ -95,22 +61,79 @@ void f_sread(){
lcdPrintln(buf); lcdPrintln(buf);
}; };
void f_echo(){ void ser_say(){
uint8_t buf[2] = {0,0};
int l;
while(1){
CDC_OutBufAvailChar(&l);
if( l ){
l = 1;
CDC_RdOutBuf (buf, &l);
puts(buf);
}
//puts("hello world\r\n");
//delayms(1);
}
};
void f_say(){
puts("hello world\r\n"); puts("hello world\r\n");
}; };
void f_init(){
nrf_init();
struct NRF_CFG config = {
.channel= BEACON_CHANNEL,
.txmac= BEACON_MAC,
.nrmacs=1,
.mac0= BEACON_MAC,
.maclen ="\x10",
};
nrf_config_set(&config);
};
void f_beacon(void){
struct NRF_CFG config = {
.channel= BEACON_CHANNEL,
.txmac= BEACON_MAC,
.nrmacs=1,
.mac0= BEACON_MAC,
.maclen ="\x10",
};
nrf_config_set(&config);
};
int enctoggle=0;
void f_enctog(){
enctoggle=1-enctoggle;
lcdClear();
lcdPrint("Encryption:");
if(enctoggle)
lcdPrintln("ON");
else
lcdPrintln("Off");
};
void f_recser(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
getInputWaitRelease();
do{
len=nrf_rcv_pkt_time_encr(1000,sizeof(buf),buf,enctoggle?testkey:NULL);
if(len==0){
puts("(Timeout)\r\n");
};
puts("pkt: ");
puts("[");puts(IntToStrX(len,2));puts("] ");
puts(IntToStrX( *(int*)(buf+ 0),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 1),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 2),2 ));
puts(" ");
puts(IntToStrX( *(int*)(buf+ 3),2 ));
puts(".");
puts(IntToStrX( *(int*)(buf+ 4),8 ));
puts(".");
puts(IntToStrX( *(int*)(buf+ 8),8 ));
puts(".");
puts(IntToStrX( *(int*)(buf+ 12),4 ));
puts(" [");
len=crc16(buf,14);
puts(IntToStrX(len,4)); puts("]\r\n");
delayms(10);
}while ((getInputRaw())==BTN_NONE);
};

View File

@ -1 +0,0 @@
../tester/util.c

View File

@ -0,0 +1,83 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "lcd/allfonts.h"
#include "filesystem/ff.h"
#include "filesystem/select.h"
#include "funk/nrf24l01p.h"
#include "usb/usbmsc.h"
#include <string.h>
/**************************************************************************/
void show_ticks(void) {
int dx=0;
int dy=8;
lcdClear();
dx=DoString(0,dy,"Ticks:");
while ((getInputRaw())==BTN_NONE){
DoInt(0,dy+8,_timectr);
lcdDisplay();
};
dy+=16;
dx=DoString(0,dy,"Done.");
};
void adc_light(void) {
int dx=0;
int dy=8;
dx=DoString(0,dy,"Light:");
DoString(0,dy+16,"Night:");
while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetLight());
DoInt(dx,dy+16,isNight());
DoInt(dx,dy+8,globalconfig.backlighttrigger);
lcdDisplay();
};
dy+=8;
dx=DoString(0,dy,"Done.");
};
void gotoISP(void) {
DoString(0,0,"Enter ISP!");
lcdDisplay();
ISPandReset();
}
void lcd_mirror(void) {
lcdToggleFlag(LCD_MIRRORX);
};
void lcd_invert(void) {
lcdToggleFlag(LCD_INVERTED);
};
void adc_check(void) {
int dx=0;
int dy=8;
// Print Voltage
dx=DoString(0,dy,"Voltage:");
while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetVoltage());
lcdDisplay();
};
dy+=8;
dx=DoString(0,dy,"Done.");
};
void msc_menu(void){
DoString(0,8,"MSC Enabled.");
lcdDisplay();
usbMSCInit();
getInputWaitRelease();
getInputWait();
DoString(0,16,"MSC Disabled.");
usbMSCOff();
};

View File

@ -1 +0,0 @@
../tester/uuid.c

View File

@ -0,0 +1,26 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "funk/nrf24l01p.h"
#include <string.h>
#include "funk/rftransfer.h"
#include "funk/openbeacon.h"
#include "core/iap/iap.h"
/**************************************************************************/
void f_uuid(void) {
IAP_return_t iap_return;
iap_return = iapReadSerialNumber();
lcdPrintIntHex(iap_return.Result[0]); lcdNl();
lcdPrintIntHex(iap_return.Result[1]); lcdNl();
lcdPrintIntHex(iap_return.Result[2]); lcdNl();
lcdPrintIntHex(iap_return.Result[3]); lcdNl();
}

View File

@ -19,6 +19,7 @@ OBJS += byteorder.o
OBJS += random.o OBJS += random.o
OBJS += idle.o OBJS += idle.o
OBJS += config.o OBJS += config.o
OBJS += itoa.o
LIBNAME=basic LIBNAME=basic

View File

@ -1,6 +1,7 @@
#ifndef __BASIC_H_ #ifndef __BASIC_H_
#define __BASIC_H_ #define __BASIC_H_
#include <time.h>
#include "core/gpio/gpio.h" #include "core/gpio/gpio.h"
#include "core/adc/adc.h" #include "core/adc/adc.h"
@ -202,4 +203,7 @@ int applyConfig(void);
#define SYSTICKSPEED 10 #define SYSTICKSPEED 10
// itoa.c
const char* IntToStrX(unsigned int num, unsigned int mxlen);
#endif #endif

15
firmware/basic/itoa.c Normal file
View File

@ -0,0 +1,15 @@
#define LEN 32
const char* IntToStrX(unsigned int num, unsigned int mxlen){
static char s[LEN+1];
char * o=s;
int len;
s[LEN]=0;
for (len=(LEN-1);len>=(LEN-mxlen);len--){
s[len]=(num%16)+'0';
if(s[len]>'9')
s[len]+='A'-'9'-1;
num/=16;
};
return &s[len+1];
};

View File

@ -91,6 +91,85 @@ void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data){
#define nrf_write_reg_long(reg, len, data) \ #define nrf_write_reg_long(reg, len, data) \
nrf_write_long(C_W_REGISTER|(reg), len, data) nrf_write_long(C_W_REGISTER|(reg), len, data)
// High-Level:
void nrf_rcv_pkt_start(void){
nrf_write_reg(R_CONFIG,
R_CONFIG_PRIM_RX| // Receive mode
R_CONFIG_PWR_UP| // Power on
R_CONFIG_EN_CRC // CRC on, single byte
);
nrf_cmd(C_FLUSH_RX);
nrf_write_reg(R_STATUS,0);
CE_HIGH();
};
int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt){
uint8_t len;
uint8_t status=0;
for(int i=0;i<maxsize;i++) pkt[i] = 0x00; // Sanity: clear packet buffer
status =nrf_cmd_status(C_NOP);
if((status & R_STATUS_RX_P_NO) == R_STATUS_RX_FIFO_EMPTY){
if( (status & R_STATUS_RX_DR) == R_STATUS_RX_DR){
#ifdef USB_CDC
puts("FIFO empty, but RX?\r\n");
#endif
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
};
return 0;
};
nrf_read_long(C_R_RX_PL_WID,1,&len);
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
if(len>32 || len==0){
return -2; // no packet error
};
if(len>maxsize){
return -1; // packet too large
};
nrf_read_pkt(len,pkt);
return len;
};
int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]){
int len;
uint16_t cmpcrc;
len=nrf_rcv_pkt_poll(maxsize,pkt);
if(len <=0)
return len;
if(key==NULL)
return len;
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
xxtea_decode_words((uint32_t*)pkt,len/4,key);
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
return -3; // CRC failed
};
};
return len;
};
void nrf_rcv_pkt_end(void){
CE_LOW();
nrf_cmd(C_FLUSH_RX);
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
};
// High-Level: // High-Level:
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
uint8_t len; uint8_t len;
@ -156,6 +235,7 @@ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t cons
return len; return len;
}; };
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
if(size > MAX_PKT) if(size > MAX_PKT)

View File

@ -157,6 +157,12 @@ void nrf_config_get(nrfconfig config);
void nrf_set_strength(unsigned char strength); void nrf_set_strength(unsigned char strength);
// new receive IF
void nrf_rcv_pkt_start(void);
int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt);
int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]);
void nrf_rcv_pkt_end(void);
/* END */ /* END */
#endif /* _NRF24L01P_H */ #endif /* _NRF24L01P_H */

View File

@ -9,6 +9,7 @@ OBJS += usbcore.o
OBJS += usbdesc.o OBJS += usbdesc.o
OBJS += usbhw.o OBJS += usbhw.o
OBJS += usbuser.o OBJS += usbuser.o
OBJS += util.o
LIBNAME=usbcdc LIBNAME=usbcdc

62
firmware/usbcdc/util.c Normal file
View File

@ -0,0 +1,62 @@
#include <sysinit.h>
#include "usbcdc/usb.h"
#include "usbcdc/usbcore.h"
#include "usbcdc/cdc_buf.h"
#include "usbcdc/usbhw.h"
#include "usbcdc/cdcuser.h"
#include "basic/basic.h"
volatile unsigned int lastTick;
// There must be at least 1ms between USB frames (of up to 64 bytes)
// This buffers all data and writes it out from the buffer one frame
// and one millisecond at a time
int puts(const char * str){
if(!USB_Configuration)
return -1;
while(*str)
cdcBufferWrite(*str++);
//XXX: This assumes systick is 1ms, which it isn't for us.
// this makes usbserial unnecessary slow. Ah well....
// Check if we can flush the buffer now or if we need to wait
unsigned int currentTick = systickGetTicks();
if (currentTick != lastTick){
uint8_t frame[64];
uint32_t bytesRead = 0;
while (cdcBufferDataPending()){
// Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
systickDelay(1);
}
lastTick = currentTick;
}
return 0;
}
void usbCDCInit(){
lastTick = systickGetTicks(); // Used to control output/printf timing
CDC_Init(); // Initialise VCOM
USB_Init(); // USB Initialization
USB_Connect(TRUE); // USB Connect
/* You could wait until serial is connected. */
#if 0 // We don't
uint32_t usbTimeout = 0;
while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) {
if (USB_Configuration) break;
delayms(10); // Wait 10ms
usbTimeout++;
}
#endif
}
usbCDCOff(void){
USB_Connect(FALSE);
}

3
firmware/usbcdc/util.h Normal file
View File

@ -0,0 +1,3 @@
int puts(const char * str);
void usbCDCInit(void);
void usbCDCOff(void);