diff --git a/firmware/Makefile b/firmware/Makefile index 65dbcf0..2f5e009 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -8,7 +8,7 @@ OBJS = main.o VPATH += OBJS += OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o basic/voltage.o -OBJS += basic/keyin.o basic/uuid.o +OBJS += basic/keyin.o basic/uuid.o basic/crc.o LIBS += core/libcore.a lcd/liblcd.a applications/libapp.a filesystem/libfat.a usb/libusb.a funk/libfunk.a ########################################################################## @@ -104,5 +104,5 @@ $(OUTFILE).elf: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX) $(LD_TEMP) -@echo "" $(LPCFIX) -c $@ -.PHONY: $(LD_TEMP) lcd/liblcd.a applications/libapp.a filesystem/libfat.a usb/libusb.a +.PHONY: $(LD_TEMP) lcd/liblcd.a applications/libapp.a filesystem/libfat.a usb/libusb.a funk/libfunk.a diff --git a/firmware/applications/funk.c b/firmware/applications/funk.c index ccd4d88..ea19a3c 100644 --- a/firmware/applications/funk.c +++ b/firmware/applications/funk.c @@ -23,65 +23,78 @@ void f_status(void){ int dy=8; uint8_t buf[4]; - // Enable SPI correctly - sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge); - - // Enable CS & CE pins - gpioSetDir(RB_SPI_NRF_CS, gpioDirection_Output); - gpioSetPullup(&RB_SPI_NRF_CS_IO, gpioPullupMode_Inactive); - gpioSetDir(3,2, gpioDirection_Output); - gpioSetPullup(&IOCON_PIO3_2, gpioPullupMode_Inactive); - CS_HIGH(); - - gpioSetDir(RB_NRF_CE, gpioDirection_Output); - gpioSetPullup(&RB_NRF_CE_IO, gpioPullupMode_Inactive); - gpioSetValue(RB_NRF_CE, 0); - -delayms(10); - - buf[0]=C_W_REGISTER | R_CONFIG; - buf[1]=R_CONFIG_PRIM_RX| R_CONFIG_PWR_UP| R_CONFIG_CRCO; -// buf[0]=C_R_REGISTER | 5; buf[1]=10; -// buf[0]=C_W_REGISTER | R_EN_AA; buf[1]=0x21; - buf[2]=0; - buf[3]=0; - - dx=DoString(0,dy,"Snd:"); DoIntX(dx,dy,*(int*)buf);dy+=8; - - CS_LOW(); -delayms(10); - sspSendReceive(0, buf, 2); - //sspReceive(0, buf, 2); - CS_HIGH(); -delayms(10); - - dx=DoString(0,dy,"Rcv:"); DoIntX(dx,dy,*(int*)buf);dy+=8; - - /* - CS_LOW(); - status=nrf_cmd_status(C_NOP); - CS_HIGH(); - dx=DoString(0,dy,"St:"); DoIntX(dx,dy,status); dy+=8; - */ - buf[0]=C_R_REGISTER | R_CONFIG; -// buf[0]=C_R_REGISTER | R_EN_AA; buf[1]=0; buf[2]=0; buf[3]=0; - dx=DoString(0,dy,"S2:"); DoIntX(dx,dy,*(int*)buf);dy+=8; + dx=DoString(0,dy,"S:"); DoIntX(dx,dy,*(int*)buf);dy+=8; + nrf_cmd_rw_long(buf,2); + dx=DoString(0,dy,"R:"); DoIntX(dx,dy,*(int*)buf);dy+=8; - CS_LOW(); -delayms(10); - sspSendReceive(0, buf, 2); - //sspReceive(0, buf, 2); - CS_HIGH(); -delayms(10); - - dx=DoString(0,dy,"R2:"); DoIntX(dx,dy,*(int*)buf);dy+=8; + int status=nrf_cmd_status(C_NOP); + dx=DoString(0,dy,"St:"); DoIntX(dx,dy,status);dy+=8; }; void f_recv(void){ + int dx=0; + int dy=8; + uint8_t buf[32]; + int len; + + len=nrf_rcv_pkt_time(500,sizeof(buf),buf); + + if(len==0){ + dx=DoString(0,dy,"No pkt"); dy+=8; + return; + }; + if(len<0){ + dx=DoString(0,dy,"Pkt too lg"); dy+=8; + return; + }; + dx=DoString(0,dy,"Size:"); DoInt(dx,dy,len); dy+=8; + dx=DoString(0,dy,"1:"); DoIntX(dx,dy,*(int*)(buf));dy+=8; + dx=DoString(0,dy,"2:"); DoIntX(dx,dy,*(int*)(buf+4));dy+=8; + dx=DoString(0,dy,"3:"); DoIntX(dx,dy,*(int*)(buf+8));dy+=8; + dx=DoString(0,dy,"4:"); DoIntX(dx,dy,*(int*)(buf+12));dy+=8; + + len=crc16(buf,14); + dx=DoString(0,dy,"c:"); DoIntX(dx,dy,len);dy+=8; + +}; + +void f_send(void){ + static char ctr=1; + int dx=0; + int dy=8; + uint8_t buf[32]; + int status; + int crc; + + buf[0]=0x05; // ID + buf[1]=0xEC; // ID + buf[2]=0xff; + buf[3]=0xff; // Send intensity + + buf[4]=0x00; // ctr + buf[5]=0x00; // ctr + buf[6]=0x00; // ctr + buf[7]=ctr++; // ctr + + buf[8]=0xff; + buf[9]=0xff; + buf[10]=0xff; + buf[11]=0xff; + buf[12]=0xff; + buf[13]=0xff; + + crc=crc16(buf,14); + buf[14]=crc & 0xff; // CRC + buf[15]=(crc >>8) & 0xff; // CRC + + status=nrf_snd_pkt_crc(16,buf); + + dx=DoString(0,dy,"St:"); DoInt(dx,dy,status); dy+=8; + }; void gotoISP(void) { @@ -108,12 +121,14 @@ const struct MENU_DEF menu_ISP = {"Invoke ISP", &gotoISP}; const struct MENU_DEF menu_init = {"F Init", &f_init}; const struct MENU_DEF menu_status = {"F Status", &f_status}; const struct MENU_DEF menu_rcv = {"F Recv", &f_recv}; +const struct MENU_DEF menu_snd = {"F Send", &f_send}; const struct MENU_DEF menu_nop = {"---", NULL}; static menuentry menu[] = { - &menu_status, &menu_init, + &menu_status, &menu_rcv, + &menu_snd, &menu_nop, &menu_ISP, NULL, @@ -236,3 +251,4 @@ void tick_funk(void){ return; }; + diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index 4bd93ea..ba942af 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -152,4 +152,8 @@ uint16_t GetUUID16(void); // for core/iap/iap.c (no official definition) void iap_entry(uint32_t param_tab[], uint32_t result_tab[]); +// crc.c +uint16_t crc16(char * buf, int len); + + #endif diff --git a/firmware/basic/crc.c b/firmware/basic/crc.c new file mode 100644 index 0000000..9846888 --- /dev/null +++ b/firmware/basic/crc.c @@ -0,0 +1,24 @@ +#include "basic.h" + +// Calculate the CRC for transmitted and received data using +// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1). + +uint16_t crc16(char * buf, int len){ + unsigned int crc=0xffff; + + for(int i=0;i> 8) | (crc << 8); + crc ^= buf[i]; + crc ^= (unsigned char)(crc & 0xff) >> 4; + crc ^= (crc << 8) << 4; + crc ^= ((crc & 0xff) << 4) << 1; + }; + return crc; +}; + +/* Note: + It is best not to alter this code. For example, (crc<<8)<<4 does + not generate the same code as crc<<12. Although the result of the + computation is the same, the latter generates much more code and + executes slower. + */ diff --git a/firmware/funk/Makefile b/firmware/funk/Makefile index 4fdde2b..88dbfc0 100644 --- a/firmware/funk/Makefile +++ b/firmware/funk/Makefile @@ -32,5 +32,5 @@ $(LIBFILE): $(OBJS) clean: rm -f $(OBJS) $(LIBFILE) -nrf24l01p.o: nrf24l01p.h +nrf24l01p.o: nrf24l01p.c nrf24l01p.h diff --git a/firmware/funk/nrf24l01p.c b/firmware/funk/nrf24l01p.c index 921b9e2..e3a0c69 100644 --- a/firmware/funk/nrf24l01p.c +++ b/firmware/funk/nrf24l01p.c @@ -7,71 +7,63 @@ #define MAC_BEACON "BEACO" /*-----------------------------------------------------------------------*/ -/* Transmit a byte via SPI (Platform dependent) */ +/* Transmit a byte via SPI */ /*-----------------------------------------------------------------------*/ -void xmit_spi(uint8_t dat) { +inline void xmit_spi(uint8_t dat) { sspSend(0, (uint8_t*) &dat, 1); } -/*-----------------------------------------------------------------------*/ -/* Receive a byte from MMC via SPI (Platform dependent) */ -/*-----------------------------------------------------------------------*/ -uint8_t rcvr_spi (void) { - uint8_t data = 0; - - sspReceive(0, &data, 1); - - return data; -} - -#define rcvr_spi_m(dst) \ - do { \ - sspReceive(0, (uint8_t*)(dst), 1); \ - } while(0) - #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) void nrf_cmd(uint8_t cmd){ + CS_LOW(); xmit_spi(cmd); + CS_HIGH(); }; uint8_t nrf_cmd_status(uint8_t cmd){ + CS_LOW(); sspSendReceive(0, &cmd, 1); return cmd; + CS_HIGH(); }; +void nrf_cmd_rw_long(uint8_t* data, int len){ + CS_LOW(); + sspSendReceive(0,data,len); + CS_HIGH(); +}; + + void nrf_write_reg(const uint8_t reg, const uint8_t val){ + CS_LOW(); xmit_spi(C_W_REGISTER | reg); xmit_spi(val); + CS_HIGH(); }; -/* -uint8_t nrf_read_reg(const uint8_t reg, uint8_t val){ - xmit_spi(C_R_REGISTER | reg); - // do i need to read the status byte here? - xmit_spi(val); - return rcvr_spi(); -}; -*/ - -void nrf_write_reg_long(const uint8_t reg, int len, char* data){ - xmit_spi(C_W_REGISTER | reg); - for(int i=0;i32 || len==0){ return 0; // no packet @@ -138,7 +128,23 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt){ if(len>maxsize){ return -1; // packet too large }; - nrf_cmd_read_long(C_R_RX_PAYLOAD,&len,pkt); + nrf_read_long(C_R_RX_PAYLOAD,len,pkt); CS_HIGH(); return len; }; + +char nrf_snd_pkt_crc(int size, uint8_t * pkt){ + + nrf_write_reg(R_CONFIG, + R_CONFIG_PWR_UP| // Power on + R_CONFIG_CRCO // 2-byte CRC + ); + + nrf_write_long(C_W_TX_PAYLOAD,size,pkt); + + CE_HIGH(); + delayms(10); // Send it. (only needs >10ys, i think) + CE_LOW(); + + return nrf_cmd_status(C_NOP); +}; diff --git a/firmware/funk/nrf24l01p.h b/firmware/funk/nrf24l01p.h index a19b6ab..6996bd0 100644 --- a/firmware/funk/nrf24l01p.h +++ b/firmware/funk/nrf24l01p.h @@ -93,13 +93,15 @@ #define R_RF_SETUP_DR_250K 0x20 /* exported functions */ -int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt); +int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt); void nrf_init() ; -void nrf_cmd_read_long(const uint8_t cmd, int *len, char* data); -void nrf_write_reg_long(const uint8_t reg, int len, char* data); -void nrf_write_reg(const uint8_t reg, const uint8_t val); -uint8_t nrf_cmd_status(uint8_t cmd); + void nrf_cmd(uint8_t cmd); +uint8_t nrf_cmd_status(uint8_t cmd); +void nrf_cmd_rw_long(uint8_t* data, int len); +void nrf_read_long(const uint8_t reg, int len, uint8_t* data); +void nrf_write_reg(const uint8_t reg, const uint8_t val); +void nrf_write_reg_long(const uint8_t reg, int len, uint8_t* data); /* END */