diff --git a/firmware/Makefile b/firmware/Makefile index 0f47a3d..0f54761 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -41,6 +41,7 @@ LDLIBS += -Lbasic -lbasic LDLIBS += -Llcd -llcd LDLIBS += -Lcore -lcore LDLIBS += -Lusb -lusb +LDLIBS += -lbasic OCFLAGS = --strip-unneeded SUBDIRS?= $(foreach lib,$(LIBS),$(dir $(lib))) diff --git a/firmware/applications/serial/config.c b/firmware/applications/serial/config.c deleted file mode 120000 index 0dc83e2..0000000 --- a/firmware/applications/serial/config.c +++ /dev/null @@ -1 +0,0 @@ -../tester/config.c \ No newline at end of file diff --git a/firmware/applications/serial/config.c b/firmware/applications/serial/config.c new file mode 100644 index 0000000..0d8df7c --- /dev/null +++ b/firmware/applications/serial/config.c @@ -0,0 +1,38 @@ +#include + +#include "basic/basic.h" + +#include "lcd/print.h" +#include "lcd/display.h" + +#include "filesystem/ff.h" + +#include + +/**************************************************************************/ + +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); +}; diff --git a/firmware/applications/serial/remote.c b/firmware/applications/serial/remote.c new file mode 100644 index 0000000..be96720 --- /dev/null +++ b/firmware/applications/serial/remote.c @@ -0,0 +1,274 @@ +#include + +#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 + +#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;i0){ + lcdPrint("Got!"); + lcdDisplay(); + break; + }; + delayms(10); + }; +}; + diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index d8e1788..94d41b3 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -7,79 +7,45 @@ #include "funk/nrf24l01p.h" -#include "core/usbcdc/usb.h" -#include "core/usbcdc/usbcore.h" -#include "core/usbcdc/usbhw.h" -#include "core/usbcdc/cdcuser.h" -#include "core/usbcdc/cdc_buf.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 +#define BEACON_CHANNEL 81 +#define BEACON_MAC "\x1\x2\x3\x2\1" + +uint32_t const testkey[4] = { + 0xB4595344,0xD3E119B6,0xA814D0EC,0xEFF5A24E +}; + #if CFG_USBMSC -#error "MSC is defined +#error "MSC is defined" #endif #if !CFG_USBCDC -#error "CDC is not defined +#error "CDC is not defined" #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 f_ser(void) { - //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 ser_enable(void) { + usbCDCInit(); }; -void f_disconnect(void) { - USB_Connect(FALSE); +void ser_disable(void) { + usbCDCOff(); }; -#define LEN 10 -void f_sread(){ - uint8_t buf[LEN+1]; - int l=LEN; +#define myLEN 10 +void ser_read(){ + uint8_t buf[myLEN+1]; + int l=myLEN; lcdPrint("Bytes:"); CDC_OutBufAvailChar (&l); @@ -95,22 +61,79 @@ void f_sread(){ lcdPrintln(buf); }; -void f_echo(){ - 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(){ +void ser_say(){ 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); + +}; diff --git a/firmware/applications/serial/util.c b/firmware/applications/serial/util.c deleted file mode 120000 index 2a06902..0000000 --- a/firmware/applications/serial/util.c +++ /dev/null @@ -1 +0,0 @@ -../tester/util.c \ No newline at end of file diff --git a/firmware/applications/serial/util.c b/firmware/applications/serial/util.c new file mode 100644 index 0000000..8747264 --- /dev/null +++ b/firmware/applications/serial/util.c @@ -0,0 +1,83 @@ +#include + +#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 + +/**************************************************************************/ + +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(); +}; + diff --git a/firmware/applications/serial/uuid.c b/firmware/applications/serial/uuid.c deleted file mode 120000 index a3edae7..0000000 --- a/firmware/applications/serial/uuid.c +++ /dev/null @@ -1 +0,0 @@ -../tester/uuid.c \ No newline at end of file diff --git a/firmware/applications/serial/uuid.c b/firmware/applications/serial/uuid.c new file mode 100644 index 0000000..601a5f7 --- /dev/null +++ b/firmware/applications/serial/uuid.c @@ -0,0 +1,26 @@ +#include + +#include "basic/basic.h" + +#include "lcd/lcd.h" +#include "lcd/print.h" + +#include "funk/nrf24l01p.h" + +#include + +#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(); +} diff --git a/firmware/basic/Makefile b/firmware/basic/Makefile index e831d6d..eb3b501 100644 --- a/firmware/basic/Makefile +++ b/firmware/basic/Makefile @@ -19,6 +19,7 @@ OBJS += byteorder.o OBJS += random.o OBJS += idle.o OBJS += config.o +OBJS += itoa.o LIBNAME=basic diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index b311f81..d218cd6 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -1,6 +1,7 @@ #ifndef __BASIC_H_ #define __BASIC_H_ +#include #include "core/gpio/gpio.h" #include "core/adc/adc.h" @@ -202,4 +203,7 @@ int applyConfig(void); #define SYSTICKSPEED 10 +// itoa.c +const char* IntToStrX(unsigned int num, unsigned int mxlen); + #endif diff --git a/firmware/basic/itoa.c b/firmware/basic/itoa.c new file mode 100644 index 0000000..9cc8de5 --- /dev/null +++ b/firmware/basic/itoa.c @@ -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]; +}; diff --git a/firmware/funk/nrf24l01p.c b/firmware/funk/nrf24l01p.c index 63ee4cf..bffaa34 100644 --- a/firmware/funk/nrf24l01p.c +++ b/firmware/funk/nrf24l01p.c @@ -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) \ 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;i32 || 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: int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){ 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; }; + char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ if(size > MAX_PKT) diff --git a/firmware/funk/nrf24l01p.h b/firmware/funk/nrf24l01p.h index 4e84106..ace6122 100644 --- a/firmware/funk/nrf24l01p.h +++ b/firmware/funk/nrf24l01p.h @@ -157,6 +157,12 @@ void nrf_config_get(nrfconfig config); 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 */ #endif /* _NRF24L01P_H */ diff --git a/firmware/usbcdc/Makefile b/firmware/usbcdc/Makefile index a319efc..3a9ea98 100644 --- a/firmware/usbcdc/Makefile +++ b/firmware/usbcdc/Makefile @@ -9,6 +9,7 @@ OBJS += usbcore.o OBJS += usbdesc.o OBJS += usbhw.o OBJS += usbuser.o +OBJS += util.o LIBNAME=usbcdc diff --git a/firmware/usbcdc/util.c b/firmware/usbcdc/util.c new file mode 100644 index 0000000..d1d8753 --- /dev/null +++ b/firmware/usbcdc/util.c @@ -0,0 +1,62 @@ +#include + +#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); +} diff --git a/firmware/usbcdc/util.h b/firmware/usbcdc/util.h new file mode 100644 index 0000000..d743b2f --- /dev/null +++ b/firmware/usbcdc/util.h @@ -0,0 +1,3 @@ +int puts(const char * str); +void usbCDCInit(void); +void usbCDCOff(void);