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

This commit is contained in:
lilafisch 2011-12-11 18:45:31 +01:00
commit af28f49b03
10 changed files with 290 additions and 285 deletions

18
firmware/BRIDGE-PROTOCOL Normal file
View File

@ -0,0 +1,18 @@
Receiving:
r->h: \1<packet data>\0
Sending:
h->r: \1<packet data>\0
r->h: \2\0
Settings:
h->r:\3<TXMAC>\0
r->h: \2\0
h->r:\4<RXMAX>\0
r->h: \2\0
h->r:\5<CHANNEL>\0
r->h: \2\0
h->r:\6<MACLEN>\0
r->h: \2\0

View File

@ -6,52 +6,186 @@
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/allfonts.h"
#include "basic/config.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 "core/ssp/ssp.h"
#include "ecc.c"
void backlightInit(void);
#if CFG_USBMSC
#error "MSC is defined"
#endif
#if !CFG_USBCDC
#error "CDC is not defined"
#endif
#define CHANNEL 81
#define MAC "\x1\x2\x3\x2\x1"
#define UB_NONE 0
#define UB_ESCAPE '\\'
#define UB_STOP '0'
#define UB_PACKETLEN 128
struct NRF_CFG config = {
.channel= CHANNEL,
.txmac= MAC,
.nrmacs=1,
.mac0= MAC,
.maclen ="\x10",
};
uint8_t serialmsg_message[UB_PACKETLEN];
uint8_t serialmsg_len = 0;
void serialmsg_init(void);
uint8_t serialmsg_put(uint8_t data);
char snd_pkt_no_crc(int size, uint8_t * pkt);
#define INPUTLEN 99
/**************************************************************************/
void main_schneider(void) {
//int yctr=8;
int dx=0;
char key;
int ctr = 1;
backlightInit();
font_direction = FONT_DIR_LTR; // LeftToRight is the default
//yctr=18;
bitstr_parse(poly, "800000000000000000000000000000000000000c9");
bitstr_parse(coeff_b, "20a601907b8c953ca1481eb10512f78744a3205fd");
bitstr_parse(base_x, "3f0eba16286a2d57ea0991168d4994637e8343e36");
bitstr_parse(base_y, "0d51fbc6c71a0094fa2cdd545b11c5c0c797324f1");
bitstr_parse(base_order, "40000000000000000000292fe77e70c12a4234c33");
void main_schneider(void)
{
GLOBAL(daytrig)=10;
GLOBAL(lcdbacklight)=10;
char input[INPUTLEN+1];
ECIES_generate_key_pair(); // generate a public/private key pair
while (1) {
key= getInput();
font=&Font_7x8;
// Easy flashing
if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!");
lcdDisplay();
ISPandReset();
};
// Display nickname
//font = &Font_Ubuntu36pt;
dx=DoString(0,0,"Test");
dx=DoInt(dx,0,ctr++);
lcdDisplay();
encryption_decryption_demo("This is encrypted",
"1c56d302cf642a8e1ba4b48cc4fbe2845ee32dce7",
"45f46eb303edf2e62f74bd68368d979e265ee3c03",
"0e10e787036941e6c78daf8a0e8e1dbfac68e26d2");
usbCDCInit();
delayms(500);
nrf_init();
nrf_config_set(&config);
nrf_rcv_pkt_start();
while(1){
int l=INPUTLEN, i, status;
CDC_OutBufAvailChar (&l);
if(l>0){
CDC_RdOutBuf (input, &l);
for(i=0; i<l; i++){
uint8_t cmd = serialmsg_put(input[i]);
if( cmd != UB_NONE ){
switch( cmd ){
case '1':
// can we loose packets here?
nrf_rcv_pkt_end();
status=snd_pkt_no_crc(serialmsg_len, serialmsg_message);
nrf_rcv_pkt_start();
break;
case '3':
memcpy(config.txmac, serialmsg_message, 5);
nrf_config_set(&config);
break;
case '4':
memcpy(config.mac0, serialmsg_message, 5);
nrf_config_set(&config);
break;
case '5':
config.channel=serialmsg_message[0];
nrf_config_set(&config);
break;
case '6':
config.maclen[0]=serialmsg_message[0];
nrf_config_set(&config);
break;
};
puts("\\2\\0");
}
}
}
int len;
uint8_t buf[32];
len=nrf_rcv_pkt_poll(sizeof(buf),buf);
if( len > 0 ){
puts("\\1");
CDC_WrInBuf(buf, &len);
puts("\\0");
}
}
return;
}
void tick_schneider(void){
return;
};
inline void xmit_spi(uint8_t dat) {
sspSend(0, (uint8_t*) &dat, 1);
}
inline void rcv_spi(uint8_t *dat) {
sspReceive(0, dat, 1);
}
#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)
char snd_pkt_no_crc(int size, uint8_t * pkt)
{
if(size > MAX_PKT)
size=MAX_PKT;
nrf_write_reg(R_CONFIG,
R_CONFIG_PWR_UP| // Power on
R_CONFIG_EN_CRC // CRC on, single byte
);
CS_LOW();
xmit_spi(C_W_TX_PAYLOAD);
sspSend(0,pkt,size);
CS_HIGH();
CE_HIGH();
delayms(1); // Send it. (only needs >10ys, i think)
CE_LOW();
return nrf_cmd_status(C_NOP);
};
void serialmsg_init(void)
{
serialmsg_len = 0;
}
//returns the message type or UB_NONE
uint8_t serialmsg_put(uint8_t data)
{
static uint8_t escaped = 0;
static uint8_t msgtype = UB_NONE;
if( data == UB_ESCAPE && escaped == 0 ){
//a control code will follow
escaped = 1;
return UB_NONE;
}else if( escaped ){
escaped = 0;
if( data != UB_ESCAPE ){
if( data == UB_STOP ){
uint8_t tmp = msgtype;
msgtype = UB_NONE;
return tmp;
}
msgtype = data;
serialmsg_len=0;
return UB_NONE;
}
}
serialmsg_message[serialmsg_len++] = data;
//prevent a buffer overflow
if( serialmsg_len == UB_PACKETLEN )
serialmsg_len--;
return UB_NONE;
}

View File

@ -28,7 +28,7 @@
#define CRP_VALUE 0x0 // ANY non-magic value disables CRP
#endif
__attribute__ ((used, section("crp"))) const uint32_t the_crp=CRP_VALUE;
//__attribute__ ((used, section("crp"))) const uint32_t the_crp=CRP_VALUE;
/**************************************************************************/

View File

@ -4,7 +4,6 @@
OBJS =
OBJS += cdcuser.o
OBJS += cdc_buf.o
OBJS += usbcore.o
OBJS += usbdesc.o
OBJS += usbhw.o

View File

@ -1,161 +0,0 @@
/*******************************************************************
Copyright (C) 2009 FreakLabs
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Originally written by Christopher Wang aka Akiba.
Please post support questions to the FreakLabs forum.
*******************************************************************/
/**************************************************************************/
/*!
@file cdc_buf.c
@author Christopher Wang (Freaklabs)
Modified by: K. Townsend (microBuilder.eu)
@date 19 May 2010
Original code taken from the FreakUSB Open Source USB Device Stack
http://freaklabs.org/index.php/FreakUSB-Open-Source-USB-Device-Stack.html
If it works well, you can thank Akiba at Freaklabs. If it fails
miserably, you can blame me (since parts of it it were rather
ungraciously modified). :-)
*/
/**************************************************************************/
#include "cdc_buf.h"
static cdc_buffer_t cdcfifo;
/**************************************************************************/
/*!
Gets a pointer to the fifo buffer
*/
/**************************************************************************/
cdc_buffer_t *cdcGetBuffer()
{
return &cdcfifo;
}
/**************************************************************************/
/*!
Initialises the RX FIFO buffer
*/
/**************************************************************************/
void cdcBufferInit()
{
cdcfifo.len = 0;
}
/**************************************************************************/
/*!
Read one byte out of the RX buffer. This function will return the byte
located at the array index of the read pointer, and then increment the
read pointer index. If the read pointer exceeds the maximum buffer
size, it will roll over to zero.
*/
/**************************************************************************/
uint8_t cdcBufferRead()
{
uint8_t data;
data = cdcfifo.buf[cdcfifo.rd_ptr];
cdcfifo.rd_ptr = (cdcfifo.rd_ptr + 1) % CFG_USBCDC_BUFFERSIZE;
cdcfifo.len--;
return data;
}
/**************************************************************************/
/*!
Reads x bytes from cdc buffer
*/
/**************************************************************************/
uint32_t cdcBufferReadLen(uint8_t* buf, uint32_t len)
{
uint32_t counter, actual;
counter = actual = 0;
while(counter != len)
{
// Make sure we don't exceed buffer limits
if (cdcfifo.len > 0)
{
buf[counter] = cdcBufferRead();
actual++;
counter++;
}
else
{
return actual;
}
}
return actual;
}
/**************************************************************************/
/*!
Write one byte into the RX buffer. This function will write one
byte into the array index specified by the write pointer and increment
the write index. If the write index exceeds the max buffer size, then it
will roll over to zero.
*/
/**************************************************************************/
void cdcBufferWrite(uint8_t data)
{
cdcfifo.buf[cdcfifo.wr_ptr] = data;
cdcfifo.wr_ptr = (cdcfifo.wr_ptr + 1) % CFG_USBCDC_BUFFERSIZE;
cdcfifo.len++;
}
/**************************************************************************/
/*!
Clear the fifo read and write pointers and set the length to zero.
*/
/**************************************************************************/
void cdcBufferClearFIFO()
{
cdcfifo.rd_ptr = 0;
cdcfifo.wr_ptr = 0;
cdcfifo.len = 0;
}
/**************************************************************************/
/*!
Check whether there is any data pending on the RX buffer.
*/
/**************************************************************************/
uint8_t cdcBufferDataPending()
{
if (cdcfifo.len != 0)
{
return 1;
}
return 0;
}

View File

@ -1,29 +0,0 @@
/*----------------------------------------------------------------------------
* Name: cdc_buf.h
* Purpose: usb cdc buffer handling
* Version: V1.00
*---------------------------------------------------------------------------*/
#ifndef __CDC_BUF_H__
#define __CDC_BUF_H__
#include "projectconfig.h"
// Buffer used for circular fifo
typedef struct _cdc_buffer_t
{
volatile uint8_t len;
volatile uint8_t wr_ptr;
volatile uint8_t rd_ptr;
uint8_t buf[CFG_USBCDC_BUFFERSIZE];
} cdc_buffer_t;
cdc_buffer_t * cdcGetBuffer();
void cdcBufferInit();
uint8_t cdcBufferRead();
uint32_t cdcBufferReadLen(uint8_t* buf, uint32_t len);
void cdcBufferWrite(uint8_t data);
void cdcBufferClearFIFO();
uint8_t cdcBufferDataPending();
#endif

View File

@ -1,4 +1,4 @@
/*----------------------------------------------------------------------------
/*----------/BulkBufOut------------------------------------------------------------------
* U S B - K e r n e l
*----------------------------------------------------------------------------
* Name: cdcuser.c
@ -17,6 +17,7 @@
*---------------------------------------------------------------------------*/
#include "projectconfig.h"
#include "basic/basic.h"
#include "usb.h"
#include "usbhw.h"
@ -24,15 +25,16 @@
#include "usbcore.h"
#include "cdc.h"
#include "cdcuser.h"
#include "cdc_buf.h"
#include "usbreg.h"
// unsigned char BulkBufIn [64]; // Buffer to store USB IN packet
unsigned char BulkBufIn [64]; // Buffer to store USB IN packet
unsigned char BulkBufOut [64]; // Buffer to store USB OUT packet
unsigned char NotificationBuf [10];
CDC_LINE_CODING CDC_LineCoding = {CFG_USBCDC_BAUDRATE, 0, 0, 8};
unsigned short CDC_SerialState = 0x0000;
unsigned short CDC_DepInEmpty = 1; // Data IN EP is empty
volatile unsigned char CDC_DepInEmpty = 1; // Data IN EP is empty
/*----------------------------------------------------------------------------
We need a buffer for incoming data on USB port because USB receives
@ -61,6 +63,7 @@ typedef struct __CDC_BUF_T
} CDC_BUF_T;
CDC_BUF_T CDC_OutBuf; // buffer for all CDC Out data
CDC_BUF_T CDC_InBuf; // buffer for all CDC Out data
/*----------------------------------------------------------------------------
read data from CDC_OutBuf
@ -116,6 +119,71 @@ int CDC_OutBufAvailChar (int *availChar)
}
/* end Buffer handling */
/*----------------------------------------------------------------------------
read data from CDC_InBuf
*---------------------------------------------------------------------------*/
int CDC_RdInBuf (char *buffer, const int *length)
{
int bytesToRead, bytesRead;
/* Read *length bytes, block if *bytes are not avaialable */
bytesToRead = *length;
//bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length);
bytesRead = bytesToRead;
// ... add code to check for underrun
while (bytesToRead--) {
*buffer++ = CDC_BUF_RD(CDC_InBuf);
}
return (bytesRead);
}
/*----------------------------------------------------------------------------
write data to CDC_InBuf
*---------------------------------------------------------------------------*/
int CDC_WrInBuf (const char *buffer, int *length)
{
int bytesToWrite, bytesWritten;
// Write *length bytes
bytesToWrite = *length;
bytesWritten = bytesToWrite;
// Just block if we can't write all at once
// +1 to prevent an overflow of the ring buffer
while( CDC_BUF_SIZE - CDC_BUF_COUNT(CDC_InBuf) < bytesToWrite+1 );
//uint8_t flush = CDC_DepInEmpty;
USB_DEVINTEN = 0;
while (bytesToWrite--) {
CDC_BUF_WR(CDC_InBuf, *buffer++); // Copy Data to buffer
}
//if( flush == 1 ){
//if( CDC_DepInEmpty && CDC_BUF_COUNT(CDC_InBuf) ){
if( CDC_DepInEmpty ){
CDC_DepInEmpty = 0;
CDC_BulkIn();
}
USB_DEVINTEN = DEV_STAT_INT | (0xFF<<1) | (USB_SOF_EVENT ? FRAME_INT : 0);
return (bytesWritten);
}
/*----------------------------------------------------------------------------
check if character(s) are available at CDC_OutBuf
*---------------------------------------------------------------------------*/
int CDC_InBufAvailChar (int *availChar)
{
*availChar = CDC_BUF_COUNT(CDC_InBuf);
return (0);
}
/* end Buffer handling */
/*----------------------------------------------------------------------------
CDC Initialisation
@ -129,12 +197,8 @@ void CDC_Init (void)
CDC_SerialState = CDC_GetSerialState();
CDC_BUF_RESET(CDC_OutBuf);
CDC_BUF_RESET(CDC_InBuf);
// Initialise the CDC buffer. This is required to buffer outgoing
// data (MCU to PC) since data can only be sent 64 bytes per frame
// with at least 1ms between frames. To see how the buffer is used,
// see 'puts' in systeminit.c
cdcBufferInit();
}
@ -269,7 +333,6 @@ uint32_t CDC_SendBreak (unsigned short wDurationOfBreak) {
return (TRUE);
}
/*----------------------------------------------------------------------------
CDC_BulkIn call on DataIn Request
Parameters: none
@ -277,25 +340,19 @@ uint32_t CDC_SendBreak (unsigned short wDurationOfBreak) {
*---------------------------------------------------------------------------*/
void CDC_BulkIn(void)
{
// int numBytesRead, numBytesAvail;
//
// // ToDo: Modify BulkIn to send incoming data to USB
//
// ser_AvailChar (&numBytesAvail);
//
// // ... add code to check for overwrite
//
// numBytesRead = ser_Read ((char *)&BulkBufIn[0], &numBytesAvail);
//
// // send over USB
// if (numBytesRead > 0) {
// USB_WriteEP (CDC_DEP_IN, &BulkBufIn[0], numBytesRead);
// }
// else {
// CDC_DepInEmpty = 1;
// }
//
//
int numBytesRead, numBytesAvail;
CDC_InBufAvailChar(&numBytesAvail);
numBytesRead = CDC_RdInBuf(&BulkBufIn[0], &numBytesAvail);
// send over USB
if (numBytesRead > 0) {
//gpioSetValue (RB_LED0, 1);
USB_WriteEP (CDC_DEP_IN, &BulkBufIn[0], numBytesRead);
//gpioSetValue (RB_LED0, 0);
} else {
//USB_WriteEP (CDC_DEP_IN, "test\r\n", 6);
CDC_DepInEmpty = 1;
//gpioSetValue (RB_LED2, 1);
}
}

View File

@ -57,7 +57,7 @@ extern void CDC_Init (void);
extern unsigned short CDC_GetSerialState (void);
/* flow control */
extern unsigned short CDC_DepInEmpty; // DataEndPoint IN empty
extern volatile unsigned char CDC_DepInEmpty; // DataEndPoint IN empty
#endif /* __CDCUSER_H__ */

View File

@ -26,6 +26,7 @@
#include "usbhw.h"
#include "usbcore.h"
#include "usbuser.h"
#include "basic/basic.h"
#include "usb/usbmsc.h"
@ -470,6 +471,10 @@ uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData)
uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt)
{
uint32_t n;
//this seems rather brutal...
//disable all usb related interrupts or WrCmd might block
USB_DEVINTEN = 0;
USB_CTRL = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
/* 3 clock cycles to fetch the packet length from RAM. */
@ -486,6 +491,9 @@ uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt)
WrCmdEP(EPNum, CMD_VALID_BUF);
//enable interrupts again
USB_DEVINTEN = DEV_STAT_INT | (0xFF<<1) | (USB_SOF_EVENT ? FRAME_INT : 0);
return (cnt);
}

View File

@ -16,30 +16,9 @@ volatile unsigned int lastTick;
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;
char repeat=0;
while (cdcBufferDataPending()){
// Read up to 64 bytes as long as possible
bytesRead = cdcBufferReadLen(frame, 64);
USB_WriteEP (CDC_DEP_IN, frame, bytesRead);
if(repeat)
systickDelay(1);
else
repeat=1;
}
lastTick = currentTick;
}
int len = strlen(str);
CDC_WrInBuf(str, &len);
return 0;
}
@ -47,8 +26,8 @@ int puts_plus(const char * str){
if(!USB_Configuration)
return -1;
while(*str)
cdcBufferWrite(*str++);
int len = strlen(str);
CDC_WrInBuf(str, &len);
return 0;
}