crashtest-r0ket/firmware/funk/rftransfer.c

116 lines
3.6 KiB
C
Raw Normal View History

2011-07-13 21:03:40 +00:00
#include "rftransfer.h"
#include "nrf24l01p.h"
#include <basic/basic.h>
#include <core/systick/systick.h>
#include <lcd/print.h>
2011-07-13 21:03:40 +00:00
2011-07-17 10:46:33 +00:00
#define MAXPACKET 32
2011-07-13 21:03:40 +00:00
void rftransfer_send(uint16_t size, uint8_t *data)
{
2011-07-13 22:13:16 +00:00
uint8_t buf[MAXPACKET];
2011-07-13 21:03:40 +00:00
buf[0] = 'L';
buf[1] = size >> 8;
buf[2] = size & 0xFF;
uint16_t rand = 5; //random_rand16();
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
2011-07-16 17:12:35 +00:00
//nrf_snd_pkt_crc(5,buf); //setup packet
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
2011-07-13 21:03:40 +00:00
uint16_t index = 0;
uint8_t i;
2011-07-13 22:13:16 +00:00
uint16_t crc = crc16(data,size);
2011-07-13 21:03:40 +00:00
while(size){
buf[0] = 'D';
buf[1] = index >> 8;
buf[2] = index & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
2011-07-23 13:35:00 +00:00
for(i=5; i<MAXPACKET-2 && size>0; i++,size--){
2011-07-13 21:03:40 +00:00
buf[i] = *data++;
}
index++;
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32,buf); //data packet
delayms(20);
2011-07-13 21:03:40 +00:00
}
buf[0] = 'C';
buf[1] = crc >> 8;
buf[2] = crc & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
2011-07-16 17:12:35 +00:00
//nrf_snd_pkt_crc(5,buf); //crc packet
2011-07-17 10:46:33 +00:00
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
2011-07-23 13:35:00 +00:00
lcdPrint("crc="); lcdPrintIntHex(crc);lcdPrintln("");lcdRefresh();
2011-07-13 21:03:40 +00:00
}
uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
{
2011-07-13 22:13:16 +00:00
uint8_t buf[MAXPACKET];
2011-07-13 21:03:40 +00:00
uint8_t state = 0;
uint16_t pos = 0, seq = 0, size = 0, rand = 0, crc = 0;
int n,i;
unsigned int currentTick = systickGetTicks();
unsigned int startTick = currentTick;
while(currentTick < (startTick+timeout) ){//this fails if either overflows
2011-07-17 10:46:33 +00:00
n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
2011-07-13 21:03:40 +00:00
switch(state){
case 0:
2011-07-17 10:46:33 +00:00
if( n == 32 && buf[0] == 'L' ){
2011-07-13 21:03:40 +00:00
size = (buf[1] << 8) | buf[2];
rand = (buf[3] << 8) | buf[4];
seq = 0;
pos = 0;
2011-07-17 10:46:33 +00:00
if( size <= maxlen ){
lcdClear();
2011-07-23 13:35:00 +00:00
lcdPrint("got l="); lcdPrintInt(size);
lcdPrintln(""); lcdRefresh();
2011-07-13 21:03:40 +00:00
state = 1;
2011-07-17 10:46:33 +00:00
}
2011-07-13 21:03:40 +00:00
}
break;
case 1:
2011-07-17 10:46:33 +00:00
if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
lcdPrint("got d"); lcdRefresh();
2011-07-13 21:03:40 +00:00
if( seq == ((buf[1]<<8)|buf[2]) ){
2011-07-17 10:46:33 +00:00
lcdPrintln(" in seq"); lcdRefresh();
//if( (pos + n - 5)<maxlen ){
//for(i=5; i<n; i++,pos++){
2011-07-23 13:35:00 +00:00
for(i=5; i<n-2 && pos<size; i++,pos++){
2011-07-13 21:03:40 +00:00
buffer[pos] = buf[i];
}
2011-07-17 10:46:33 +00:00
seq++;
//}
2011-07-13 21:03:40 +00:00
}
}
if( pos == size ){
2011-07-17 10:46:33 +00:00
lcdPrintln("got all"); lcdRefresh();
2011-07-13 21:03:40 +00:00
crc = crc16(buffer, size);
2011-07-23 13:35:00 +00:00
lcdPrint("crc="); lcdPrintIntHex(crc);
lcdPrintln("");lcdRefresh();
2011-07-13 21:03:40 +00:00
state = 2;
}
break;
case 2:
2011-07-17 10:46:33 +00:00
if( n == 32 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
lcdPrint("got crc"); lcdRefresh();
2011-07-13 21:03:40 +00:00
if( crc == ((buf[1]<<8)|buf[2]) ){
2011-07-17 10:46:33 +00:00
lcdPrintln(" ok"); lcdRefresh();
2011-07-13 21:03:40 +00:00
return size;
}else{
2011-07-23 13:35:00 +00:00
lcdPrintln(" nok"); lcdRefresh();
2011-07-13 21:03:40 +00:00
state = 0;
}
}
break;
};
}
return 0;
}