Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
Stefan `Sec` Zehl 2011-08-03 18:20:25 +02:00
commit 3028583e28
5 changed files with 347 additions and 169 deletions

View File

@ -5,120 +5,34 @@
#include "lcd/render.h" #include "lcd/render.h"
#include "lcd/allfonts.h" #include "lcd/allfonts.h"
void ReinvokeISP(void); void backlightInit(void);
void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms);
/**************************************************************************/ /**************************************************************************/
void main_s(void) { void main_s(void) {
//Make PIO1_11 an analog input uint8_t key;
gpioSetDir(RB_LED3, gpioDirection_Input); //lcdClear();
IOCON_PIO1_11 = 0x41;
backlightInit();
uint32_t j=0;
//disable the JTAG on PIO3_3
IOCON_PIO3_3 = 0x10;
int yctr=8;
int dx=0;
font_direction = FONT_DIR_LTR; // LeftToRight is the default
font = &Font_8x8;
static FONT fonts[] = {
&Font_7x8,
&Font_Ubuntu18pt, // 3 byte-font
&Font_8x8,
};
int fontctr=0;
yctr=18;
uint8_t trigger;
#define SEND
#ifdef SEND
trigger=200;
gpioSetDir(RB_LED0, gpioDirection_Output);
IOCON_JTAG_TDI_PIO0_11 = 0x11;
#else
trigger=380;
gpioSetDir(RB_LED0, gpioDirection_Input);
IOCON_JTAG_TDI_PIO0_11 = 0x42;
#endif
uint32_t ctr=0;
while (1) { while (1) {
ctr++; uint8_t contrast = lcdRead(0xDF);
uint32_t results; lcdInit(); // display
lcdDisplay(); lcdPrint("reg=");
delayms(10); lcdPrintCharHex(contrast);
lcdNl();
lcdDisplay();
while(1);
delayms(500);
font=fonts[fontctr]; //key= getInputWait();
if(gpioGetValue(RB_BTN3)==0){ // Easy flashing
while(gpioGetValue(RB_BTN3)==0); if(key==BTN_LEFT){
trigger +=10; DoString(0,8,"Enter ISP!");
}; lcdDisplay();
if(gpioGetValue(RB_BTN2)==0){ ISPandReset();
while(gpioGetValue(RB_BTN2)==0); }
trigger -=10;
};
//dx=DoString(0,0,"Trig:");
//dx=DoInt(dx,0,trigger);
//DoString(dx,0," ");
if(gpioGetValue(RB_BTN0)==0){
while(gpioGetValue(RB_BTN0)==0);
DoString(0,8,"Enter ISP!");
lcdDisplay();
EnableWatchdog(1000*5);
ReinvokeISP();
};
font = &Font_Ubuntu36pt;
static uint8_t ctrx=0, ctry=0, dirx=1, diry=1;
dx=DoString(ctrx,ctry,"S");
if( dirx ){
if(ctrx++ == 60)
dirx = 0;
}else{
if(ctrx-- == 0)
dirx=1;
} }
if( diry ){
if(ctry++ == 12)
diry = 0;
}else{
if(ctry-- == 0)
diry=1;
}
font = &Font_7x8;
results = adcRead(1);
//dx=DoString(0,yctr+28,"Voltage:");
results *= 10560;
results /= 1024;
//DoInt(dx,yctr+28,results);
if( results < 3500 ){
DoString(0,yctr+30,"Shutdown");
gpioSetValue (RB_PWR_GOOD, 0);
gpioSetValue (RB_LCD_BL, 0);
SCB_SCR |= SCB_SCR_SLEEPDEEP;
PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN;
__asm volatile ("WFI");
}else{
//DoString(0,yctr+30,"OK ");
;
}
}
return;
} }
void tick_minimal(void){
return;
};

View File

@ -35,3 +35,15 @@ push_queue
the_config the_config
the_queue the_queue
work_queue work_queue
selectFile
nrf_snd_pkt_crc_encr
nrf_rcv_pkt_time_encr
getInput
ECIES_encyptkeygen
f_open
f_read
strlen
strcpy
xxtea_encode_words
getRandom
crc16

View File

@ -1,5 +1,5 @@
MEMORY MEMORY
{ {
sram(rwx): ORIGIN = 0x10002000 - 2048, LENGTH = 2048 sram(rwx): ORIGIN = 0x10002000 - 2548, LENGTH = 2548
} }
INCLUDE ram.ld INCLUDE ram.ld

284
firmware/l0dable/sendcard.c Normal file
View File

@ -0,0 +1,284 @@
#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"
#include "basic/ecc.h"
#include "funk/nrf24l01p.h"
#include "filesystem/ff.h"
#include "filesystem/diskio.h"
#include "funk/filetransfer.h"
#include "lcd/print.h"
#include <string.h>
#include "funk/nrf24l01p.h"
#include "funk/filetransfer.h"
#include "funk/rftransfer.h"
#include "basic/basic.h"
#include "basic/xxtea.h"
#include "filesystem/ff.h"
#include "funk/rftransfer.h"
#include "funk/nrf24l01p.h"
#include <basic/basic.h>
#include <basic/random.h>
#include <core/systick/systick.h>
//#include <lcd/print.h>
#include "usetable.h"
//#include "lcd/print.h"
uint8_t mac[5] = {1,2,3,2,1};
void ram(void)
{
char file[13];
selectFile(file,"TXT");
sendFile(file);
}
void sendR(uint8_t *rx, uint8_t *ry)
{
uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i];
exp[1] = 'X';
nrf_snd_pkt_crc(32, exp);
delayms(10);
exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i];
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(1000, 32, buf);
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2];
return 0;
}
}
return -1;
}
int receivePublicKey(uint8_t *px, uint8_t *py)
{
return receiveKey('P',px,py);
}
void sendMac(void)
{
uint8_t buf[32];
buf[0] = 'M';
buf[1] = 'C';
buf[2] = mac[0];
buf[3] = mac[1];
buf[4] = mac[2];
buf[5] = mac[3];
buf[6] = mac[4];
nrf_snd_pkt_crc(32, buf);
delayms(10);
}
int receiveMac(uint8_t *mac)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
for(int i=0; i<5; i++)
mac[i] = buf[i+2];
return 0;
}
return -1;
}
int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
{
uint8_t done = 0;
char key;
while( !done ){
lcdClear();
lcdPrintln("Recv. PUBKEY");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receivePublicKey(px,py) )
continue;
if( receiveMac(mac) )
continue;
lcdClear();
lcdPrintln("Got PUBKEY"); lcdRefresh();
lcdPrintln("Hash:");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
return 0;
}
void sendFile(char *filename)
{
uint8_t px[4*NUMWORDS];
uint8_t py[4*NUMWORDS];
uint8_t mac[5];
if( receiveKeys(px, py, mac) )
return;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
lcdClear();
lcdPrintln("Creating key"); lcdRefresh();
ECIES_encyptkeygen(px, py, k1, k2, rx, ry);
while( !done ){
lcdPrintln("Sending file");lcdRefresh();
sendR(rx,ry);
delayms(3000);
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1);
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}
}
lcdClear();
}
}
//TODO: use a proper MAC to sign the message
int filetransfer_send(uint8_t *filename, uint16_t size,
uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE];
FIL file;
FRESULT res;
UINT readbytes;
if( size > MAXSIZE )
return 1; //File to big
res=f_open(&file, (const char*)filename, FA_OPEN_EXISTING|FA_READ);
if( res )
return res;
//res = f_read(&file, (char *)buf, size, &readbytes);
for(uint16_t i=0; i<MAXSIZE; i++)
buf[i] = 0;
res = f_read(&file, (char *)buf, MAXSIZE, &readbytes);
size = readbytes;
if( res )
return res;
if( size != readbytes)
return 1; //Error while reading
uint16_t wordcount = (size+3)/4;
//uint8_t macbuf[5];
uint8_t metadata[32];
if( strlen((char*)filename) < 20 )
strcpy((char*)metadata, (char*)filename);
else
return 1; //File name too long
metadata[20] = size >> 8;
metadata[21] = size & 0xFF;
//nrf_get_tx_max(5,macbuf);
//nrf_set_tx_mac(5, mac);
nrf_snd_pkt_crc_encr(32, metadata, k);
delayms(20);
xxtea_encode_words((uint32_t *)buf, wordcount, k);
rftransfer_send(wordcount*4, buf);
//nrf_set_tx_mac(5, macbuf);
return 0;
}
#define MAXPACKET 32
void rftransfer_send(uint16_t size, uint8_t *data)
{
uint8_t buf[MAXPACKET];
buf[0] = 'L';
buf[1] = size >> 8;
buf[2] = size & 0xFF;
uint16_t rand = getRandom() & 0xFFFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
uint16_t index = 0;
uint8_t i;
uint16_t crc = crc16(data,size);
while(size){
buf[0] = 'D';
buf[1] = index >> 8;
buf[2] = index & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
for(i=5; i<MAXPACKET-2 && size>0; i++,size--){
buf[i] = *data++;
}
index++;
nrf_snd_pkt_crc(32,buf); //data packet
delayms(20);
}
buf[0] = 'C';
buf[1] = crc >> 8;
buf[2] = crc & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
}

View File

@ -1,15 +1,13 @@
#include <sysinit.h> #include <sysinit.h>
#include <string.h>
#include "basic/basic.h" #include "basic/basic.h"
#include "basic/random.h"
#include "lcd/render.h" #include "lcd/render.h"
#include "lcd/display.h" #include "lcd/display.h"
#include "lcd/allfonts.h" #include "lcd/allfonts.h"
void ReinvokeISP(void);
void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms);
/**************************************************************************/ /**************************************************************************/
#define POS_PLAYER_Y 60 #define POS_PLAYER_Y 60
#define POS_PLAYER_X RESX/2-3 #define POS_PLAYER_X RESX/2-3
@ -20,11 +18,11 @@ void delayms(uint32_t ms);
#define UFO_PROB 1024 #define UFO_PROB 1024
#define TYPE_PLAYER 1 #define TYPE_PLAYER 1
#define TYPE_ENEMY_A 3 #define TYPE_ENEMY_A 3
#define TYPE_ENEMY_B 2 #define TYPE_ENEMY_B 2
#define TYPE_ENEMY_C 4 #define TYPE_ENEMY_C 4
#define TYPE_UFO 5 #define TYPE_UFO 5
#define BUNKERS 3 #define BUNKERS 3
#define BUNKER_WIDTH 10 #define BUNKER_WIDTH 10
@ -52,16 +50,19 @@ struct gamestate {
char key; char key;
void init_game(); void init_game();
void init_enemy();
void check_end(); void check_end();
void move_ufo(); void move_ufo();
void move_shot();
void move_shots(); void move_shots();
void move_player(); void move_player();
void move_enemay(); void move_enemy();
void draw_score(); void draw_score();
void draw_bunker(); void draw_bunker();
void draw_player(); void draw_player();
void draw_enemy(); void draw_enemy();
void draw_shots(); void draw_shots();
void draw_sprite(char type, char x, char y);
void draw_ufo(); void draw_ufo();
void screen_intro(); void screen_intro();
void screen_gameover(); void screen_gameover();
@ -150,14 +151,20 @@ void init_game(void) {
game.shot_y = 0; game.shot_y = 0;
game.alive = ENEMY_ROWS*ENEMY_COLUMNS; game.alive = ENEMY_ROWS*ENEMY_COLUMNS;
game.move = 0; game.move = 0;
game.direction = -1; if (getRandom()%2 == 0) {
game.lastcol = ENEMY_COLUMNS-1; game.direction = -1;
game.lastcol = ENEMY_COLUMNS-1;
} else {
game.direction = 1;
game.lastcol = 0;
}
game.killed = 0; game.killed = 0;
game.step = false; game.step = false;
game.ufo = DISABLED; game.ufo = DISABLED;
game.score = 0;
init_enemy(); init_enemy();
for (char col=0; col<ENEMY_COLUMNS; col++){ for (int col=0; col<ENEMY_COLUMNS; col++){
game.shots_x[col] = DISABLED; game.shots_x[col] = DISABLED;
} }
@ -218,20 +225,7 @@ void move_shot() {
return; return;
} }
//check for collision with bunker if (check_bunker(game.shot_x,game.shot_y-5,1 ))
// for (int b=0; b<BUNKERS; b++) {
// if (game.shot_x>BUNKER_X[BUNKERS-1-b] &&
// game.shot_x<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.shot_y<RESY-8 &&
// game.shot_y>RESY-16) {
// int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]);
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]<<1;
// game.shot_x=DISABLED;
// }
// }
// }
if (check_bunker(game.shot_x,game.shot_y-5,1 ))
game.shot_x=DISABLED; game.shot_x=DISABLED;
//check for collision with enemy, kill enemy if //check for collision with enemy, kill enemy if
@ -242,12 +236,13 @@ void move_shot() {
game.enemy_x[row][col]=DISABLED; game.enemy_x[row][col]=DISABLED;
game.shot_x = DISABLED; game.shot_x = DISABLED;
game.alive--; game.alive--;
game.score++; game.score+=(3-row)*10;
return; return;
} }
} }
} }
} }
//check for collision with ufo //check for collision with ufo
if (game.ufo != DISABLED && if (game.ufo != DISABLED &&
game.shot_x>game.ufo && game.shot_x>game.ufo &&
@ -255,7 +250,7 @@ void move_shot() {
game.shot_y<8) { game.shot_y<8) {
game.ufo = DISABLED; game.ufo = DISABLED;
game.score += 5; game.score += 50;
} }
game.shot_y -= 2; game.shot_y -= 2;
@ -265,10 +260,10 @@ void move_shot() {
void move_shots() { void move_shots() {
for (char col = 0; col<ENEMY_COLUMNS; col++){ for (int col = 0; col<ENEMY_COLUMNS; col++){
//No shot, maybe generate //No shot, maybe generate
if (game.shots_x[col] == DISABLED) { if (game.shots_x[col] == DISABLED) {
for (char row = 0; row<ENEMY_ROWS; row++) { for (int row = 0; row<ENEMY_ROWS; row++) {
if (game.enemy_x[row][col] != DISABLED) { if (game.enemy_x[row][col] != DISABLED) {
if(getRandom()%(game.alive*20/((game.level/3)+1))==0) { if(getRandom()%(game.alive*20/((game.level/3)+1))==0) {
game.shots_x[col] = game.enemy_x[row][col]+5; game.shots_x[col] = game.enemy_x[row][col]+5;
@ -288,20 +283,7 @@ void move_shots() {
if (check_bunker(game.shots_x[col],game.shots_y[col],-1)) if (check_bunker(game.shots_x[col],game.shots_y[col],-1))
game.shots_x[col]=DISABLED; game.shots_x[col]=DISABLED;
// for (int b=0; b<BUNKERS; b++) {
// if (game.shots_x[col]>BUNKER_X[BUNKERS-1-b] &&
// game.shots_x[col]<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.shots_y[col]<RESY-8 &&
// game.shots_y[col]>RESY-16) {
// int offset = BUNKER_WIDTH - (game.shots_x[col]-BUNKER_X[BUNKERS-1-b])-1;
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]>>1;
// game.shots_x[col]=DISABLED;
// }
// }
// }
//check for collision with player //check for collision with player
if (game.shots_y[col] >= RESY-13 && if (game.shots_y[col] >= RESY-13 &&
game.shots_x[col] > game.player+1 && game.shots_x[col] > game.player+1 &&
game.shots_x[col] < game.player+6) { game.shots_x[col] < game.player+6) {
@ -363,25 +345,12 @@ void move_enemy() {
game.killed = true; game.killed = true;
} }
check_bunker(pos,game.enemy_row_y[row]+8,-2); check_bunker(pos,game.enemy_row_y[row]+8,-2);
//check for collision with bunker, dirty
// for (int b=0; b<BUNKERS; b++) {
// if (pos>=BUNKER_X[BUNKERS-1-b] &&
// pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.enemy_row_y[row]+8<RESY-8 &&
// game.enemy_row_y[row]+8>RESY-16) {
// int offset = BUNKER_WIDTH - (pos-BUNKER_X[BUNKERS-1-b]);
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]>>2;
// }
// }
// }
//Are we at the beginning or end? Direction change //Are we at the beginning or end? Direction change
if((pos <=0 && game.direction != 1) || if((pos <=0 && game.direction != 1) ||
(pos >=RESX-10 && game.direction == 1)){ (pos >=RESX-10 && game.direction == 1)){
game.direction = (game.direction==1)?-1:1; game.direction = (game.direction==1)?-1:1;
for (char r = 0; r<ENEMY_ROWS; r++) { for (int r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=game.level>=23?4:2; game.enemy_row_y[r]+=game.level>=23?4:2;
} }
return; return;
@ -395,7 +364,6 @@ void move_enemy() {
} }
void draw_player() { void draw_player() {
//draw_sprite(50, 20);
draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y); draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y);
} }
@ -427,7 +395,7 @@ void draw_shots() {
} }
} }
for (char col = 0; col < ENEMY_COLUMNS; col++) { for (int col = 0; col < ENEMY_COLUMNS; col++) {
if (game.shots_x[col] != DISABLED) { if (game.shots_x[col] != DISABLED) {
for (int length=0; length<=5; length++) { for (int length=0; length<=5; length++) {
lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true); lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true);