Commit 40d0c4a9 authored by Hermann Mayer's avatar Hermann Mayer

Added rfm70 lib. Upgraded arduino lib. Refactored backend lib. Rewritten…

Added rfm70 lib. Upgraded arduino lib. Refactored backend lib. Rewritten organization binaries. Added slave-rfm test.
parent 45b71964
......@@ -9,7 +9,7 @@ SET(CSTANDARD "-std=gnu99")
SET(CWARN "-Wall -Wstrict-prototypes")
SET(CTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums")
SET(COPT "-Os")
SET(CINCS "-I${CMAKE_SOURCE_DIR}/src/lib/arduino")
SET(CINCS "-I${CMAKE_SOURCE_DIR}/src/vendor/arduino")
SET(CMCU "-mmcu=atmega328p")
SET(CDEFS "-DF_CPU=16000000")
SET(CFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${CINCS} ${COPT} ${CWARN} ${CSTANDARD} ${CEXTRA} ${CTUNING}")
......@@ -20,7 +20,7 @@ SET(CXXSTANDARD "")
SET(CXXWARN "-Wall")
SET(CXXTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -Wl,--gc-sections -fno-exceptions --pedantic")
SET(CXXOPT "-Os")
SET(CXXINCS "-I${CMAKE_SOURCE_DIR}/src/lib/arduino")
SET(CXXINCS "-I${CMAKE_SOURCE_DIR}/src/vendor/arduino")
SET(CXXMCU "-mmcu=atmega328p")
SET(CXXDEFS "-DF_CPU=16000000")
SET(CXXFLAGS "${CXXMCU} ${CXXDEBUG} ${CXXDEFS} ${CXXINCS} ${CXXOPT} ${CXXWARN} ${CXXSTANDARD} ${CXXEXTRA} ${CXXTUNING}")
......
......@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.6)
Project(Slave-Stack)
add_subdirectory(src/lib/arduino)
add_subdirectory(src/vendor/arduino)
add_subdirectory(src/vendor/rfm70)
add_subdirectory(src/lib)
add_subdirectory(src)
#!/bin/bash
doFlash=0
while getopts ":--help:h:f" opt; do
case $opt in
--help|h)
echo "Usage of $0:"
echo
echo " --help -h Print this help"
echo " -f Flash builded firmware adhoc to the device"
exit
;;
f)
doFlash=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
# Get current location of this script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Configure current build path
buildPath="$DIR/../build"
# Rebuild tags file if we find ctags binary
type -P ctags &>/dev/null && cd "$DIR/../" && ctags -R .
# Prepare build path
rm -rf "$buildPath" && mkdir "$buildPath" && cd "$buildPath"
# Do the build
touch .gitkeep
cmake ..
make
make || exit 1
if [ $doFlash = 1 ]; then
cd "$DIR" && ./flash
fi
......@@ -7,5 +7,5 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
buildPath="$DIR/../build"
# Goto build path and flash firmware on the device
cd "$buildPath" && avrdude -p m328p -c arduino -P /dev/ttyACM0 -U flash:w:Slave-Stack.hex
cd "$buildPath" && avrdude -e -p m328p -c arduino -P /dev/ttyACM0 -U flash:w:Slave-Stack.hex
#!/bin/sh
#!/bin/bash
COMMAND="$1"
FLAGS="$2"
PWD="$3"
FlashLog="${PWD}/bin/helper/.flash.log"
FirmwareLog="${PWD}/bin/helper/.firmware.log"
FileLog="${PWD}/bin/helper/.firmware.log"
if [ ! -f "$FlashLog" ]; then
touch "$FlashLog";
fi
if [ ! -f "$FirmwareLog" ]; then
touch "$FirmwareLog";
if [ ! -f "$FileLog" ]; then
touch "$FileLog";
fi
NColor="\E[0m";
Color0="\E[0;34m";
Color1="\E[1;37m";
Color2="\E[0;37m";
Color3="\E[0;35m";
Color4="\E[1;33m";
Color5="\E[1;31m";
j=0; k=0;
cd "${PWD}/build";
echo
echo "+-------------------------------------------------------------------------------";
for i in $(${COMMAND} ${FLAGS}); do
if [ $j -eq 0 ]; then
echo -n "|";
if [ $k -eq 0 ]; then
echo -en "${Color5}";
fi
fi
echo -n " $i "
j=$((j+1)); k=$((k+1))
if [ $j -eq 6 ]; then
echo -e "$NColor";
j=0;
fi
done
echo "+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
FlashSize=`${COMMAND} ${FLAGS} |grep "(TOTALS)"|sed -e "s/\s\{3,\}/+/g"|cut -d "+" -f2-3|bc`;
OldFlashSize=$( cat "$FlashLog" )
FileSize=`ls -1lisa|grep hex|cut -d ' ' -f7;`;
OldFileSize=$( cat "$FirmwareLog" )
FlashRatio=`echo "scale=8;100-(($FlashSize/$OldFlashSize)*100)"|bc|awk '{printf "%0f\n", $0}'|cut -c1-5`;
FlashDiff=`echo "scale=8;$OldFlashSize-$FlashSize"|bc|cut -c1-5`;
if [ $FlashDiff -lt 0 ]; then
MsgColor=$Color1;
SL="gewonnen";
FlashDiff=`echo $FlashDiffDiff|cut -c2-32`;
elif [ $FlashDiff -eq 0 ]; then
MsgColor=$Color0;
SL="Differenz";
else
MsgColor=$Color0;
SL="verloren";
fi
NColor="\033[0m";
Color0="\033[1;34m";
Color1="\033[1;37m";
Color2="\033[0;37m";
Color3="\033[0;35m";
Color4="\033[1;33m";
Color5="\033[1;31m";
Color6="\033[1;32m";
Color7="\033[0;36m";
FileRatio=`echo "scale=8;100-(($FileSize/$OldFileSize)*100)"|bc|awk '{printf "%0f\n", $0}'|cut -c1-5`;
FileDiff=`echo "scale=8;$OldFileSize-$FileSize"|bc|cut -c1-5`;
if [ $FileDiff -lt 0 ]; then
MsgColor2=$Color1;
SL2="gewonnen";
FileDiff=`echo $FileDiff|cut -c2-32`;
elif [ $FileDiff -eq 0 ]; then
MsgColor2=$Color0;
SL2="Differenz";
else
MsgColor2=$Color0;
SL2="verloren";
fi
flashSize=0
fileSize=$(ls -l | grep hex | awk '{print $5}')
function printSeperator()
{
awk 'BEGIN {printf "+"; for(i=70;i--;) printf "-"; print "+"}' < /dev/null
}
function printHeadingTable()
{
echo && printSeperator
i=0
while read line; do
rColor=''
if [ $i -eq 0 ]; then
rColor=$Color1
fi
if [ $i -eq 1 ]; then
flashSize=$(echo $line | awk '{printf $4}');
fi
echo $line | awk \
-v color="$rColor" \
-v ncolor="$NColor" \
'{printf("|%s %6s %6s %6s %6s %6s %s%s\n", color, $1, $2, $3, $4, $5, $6, ncolor)}'
i=$((i+1))
done < <($COMMAND $FLAGS)
printSeperator
}
function printDiff()
{
diff=$(echo "$1 - $2" | bc)
new=$1; old=$2;
rColor=''
if [ $diff -eq 0 ]; then
rColor="$Color1"
text="${rColor}keine Veränderung${NColor}"
elif [ $diff -gt 0 ]; then
rColor="$Color5"
text="${rColor}$diff byte verloren${NColor}"
t=$new; new=$old; old=$t;
elif [ $diff -lt 0 ]; then
rColor="$Color6"
text="${rColor}$(($diff*-1)) byte gewonnen${NColor}"
t=$new; new=$old; old=$t;
fi
echo -en "$4$rColor"
echo "$new $old" | awk "{printf(\" %f%% \", 100-((\$1/\$2)*100))}"
echo -e "$NColor($text)"
}
function printStats()
{
# Print Flash-Stats
allSize=32768
awk "BEGIN {printf(\"| Current Flash-Size: $Color7%d byte$NColor ($Color4%f%% used$NColor) / $Color0%d byte$NColor\n\", "\
"$flashSize, ($flashSize / $allSize) * 100, $allSize)}"
# Calc Flash Ratio
oldFlashSize=$(cat "$FlashLog")
printDiff "$flashSize" "$oldFlashSize" "$allSize" "| Flash-Size-Ratio:"
# Print newline
echo "|"
# Print File-Stats
awk "BEGIN {printf(\"| Current File-Size: $Color7%d byte$NColor\n\", $fileSize)}"
# Print File-Ratio
oldFileSize=$(cat "$FileLog")
# echo "$fileSize $oldFileSize" | awk '{printf("| File-Size-Ratio: %f ", 100-(($1/$2)*100))}'
printDiff "$fileSize" "$oldFileSize" "$allSize" "| File-Size-Ratio:"
printSeperator && echo
}
# Do the outputs
printHeadingTable
printStats
echo -en "| ${Color5}Current Flash-Size:$NColor ${Color3}${FlashSize} byte$NColor ";
echo -en "(${Color4}`echo "scale=8;((${FlashSize})/32768)*100"|bc|cut -c1-5`% used$NColor) / ${Color3}32768 byte$NColor";echo ;
echo -e "| ${Color5}Flash-Size-Ratio:$NColor ${MsgColor}$FlashRatio%$NColor (${MsgColor}$FlashDiff byte $SL$NColor)"
echo -e "|";
echo -e "| ${Color5}Current File-Size:$NColor ${Color2}$FileSize Byte$NColor";
echo -e "| ${Color5}File-Size-Ratio:$NColor ${MsgColor2}$FileRatio%$NColor (${MsgColor2}$FileDiff byte $SL2$NColor)"
echo "+-------------------------------------------------------------------------------";
echo "$FlashSize" > "$FlashLog"
echo "$FileSize" > "$FirmwareLog"
echo
# Do all file updates
echo "$flashSize" > "$FlashLog"
echo "$fileSize" > "$FileLog"
#!/bin/bash
# Get current location of this script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
screen /dev/ttyACM0
......@@ -31,7 +31,8 @@
#ifndef IOChip_H
#define IOChip_H
#include "WProgram.h"
// #include "WProgram.h"
#include "Arduino.h"
struct BoardPin
{
......
......@@ -30,7 +30,8 @@
#define ComBufferWidth 64
//#define ComDebug
#include "WProgram.h"
// #include "WProgram.h"
#include "Arduino.h"
//| Message-Codes
const char infoFirmwareIsReady[6] = "0x79A";
......
......@@ -29,7 +29,8 @@
#include "libMCP2301X.h"
#include "WProgram.h"
// #include "WProgram.h"
#include "Arduino.h"
//| Constructor
//+----------------------------------------
......@@ -46,9 +47,9 @@ IOChip_MCP2301X::~IOChip_MCP2301X(){}
bool IOChip_MCP2301X::sendData(uint8_t Register, uint8_t Data0, uint8_t Data1)
{
Wire.beginTransmission(I2CAddress);
Wire.send(Register); // Select register
Wire.send(Data0); // Write to given Register
Wire.send(Data1); // Write to given Register
Wire.write(Register); // Select register
Wire.write(Data0); // Write to given Register
Wire.write(Data1); // Write to given Register
if(Wire.endTransmission() == 0)
return true;
else
......@@ -61,7 +62,7 @@ bool IOChip_MCP2301X::sendData(uint8_t Register, uint8_t Data0, uint8_t Data1)
bool IOChip_MCP2301X::refresh()
{
Wire.beginTransmission(I2CAddress);
Wire.send(GP0); // Select Register
Wire.write(GP0); // Select Register
if(Wire.endTransmission() > 0)
return false;
......@@ -69,8 +70,8 @@ bool IOChip_MCP2301X::refresh()
if(Wire.available() == 2)
{
Port0 = Wire.receive();
Port1 = Wire.receive();
Port0 = Wire.read();
Port1 = Wire.read();
return true;
}
else
......@@ -299,14 +300,14 @@ void IOChip_MCP2301X::runPinTest()
{
tPinConf++;
Wire.beginTransmission(I2CAddress);
Wire.send(0x00);
Wire.send(tPinConf);
Wire.write(0x00);
Wire.write(tPinConf);
Wire.endTransmission();
delay(150);
Wire.beginTransmission(I2CAddress);
Wire.send(0x01);
Wire.send(tPinConf);
Wire.write(0x01);
Wire.write(tPinConf);
Wire.endTransmission();
delay(150);
......
......@@ -31,7 +31,8 @@
#define IOChip_MCP2301X_h
#include <inttypes.h>
#include "WProgram.h"
// #include "WProgram.h"
#include "Arduino.h"
#include "libChip.h"
#include "Wire.h"
......
/* FREEWARE
Original: Wojciech M. Zabolotny wzab<at>ise.pw.edu.pl)
kner 2011
*/
#include "rfm70.h"
#include "SPI.h"
#ifdef PROGMEM
#undef PROGMEM
#define PROGMEM __attribute__((section(".progmem.data")))
#endif
static PROGMEM uint8_t cmd_activate[]={0x50,0x73};
static PROGMEM uint8_t cmd_tog1[]={0xd9 | 0x06, 0x9e, 0x86, 0x0b}; //assosciated with set1[4]!
static PROGMEM uint8_t cmd_tog2[]={0xd9 & ~0x06, 0x9e, 0x86, 0x0b};
static PROGMEM uint8_t cmd_flush_rx[]={0xe2,0x00};
static PROGMEM uint8_t cmd_flush_tx[]={0xe1,0x00};
static PROGMEM uint8_t cmd_switch_cfg[]={0x50,0x53};
static PROGMEM uint8_t adr0[]={0x21,'K','N'}; //
//static PROGMEM uint8_t adr1[]={SET_NUMBER,'W','G'};
static PROGMEM uint8_t set1[][4]={
{0x40, 0x4b, 0x01, 0xe2},//0
{0xc0, 0x4b, 0x00, 0x00},
{0xd0, 0xfc, 0x8c, 0x02},//2
{0x99, 0x00, 0x39, 0x41},
{0xd9, 0x9e, 0x86, 0x0b},//4 #?? d9 oder f9? POWER
{0x24, 0x06, 0x7f, 0xa6},
{0x00, 0x00, 0x00, 0x00},//6
{0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00},//8
{0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00},//10
{0x00, 0x00, 0x00, 0x00},
{0x00, 0x12, 0x73, 0x00},//12
{0x36, 0xB4, 0x80, 0x00},
};
static PROGMEM uint8_t set1_14[]={0x41,0x20,0x08,0x04,0x81,0x20,0xCF,0xF7,0xFE,0xFF,0xFF};
static PROGMEM uint8_t set0[][2]={
{0,0x3F}, //mask RX_DR\TX_DS\MAX_RT (we do not use IRQ!),Enable CRC ,2byte,POWER UP,PRX
{1,0x01}, //Enable AACK only in pipe 0! Was:{1,0x3F}, //Enable auto acknowledgement data pipe5\4\3\2\1\0
{2,0x01}, //Enable only RX pipe 0 //Enable RX Addresses pipe5\4\3\2\1\0
{3,0x01}, //RX/TX address field width 3 bytes
{4,0x0f}, //up to 15 retransmissions 250#,A5#(Bs delay! Was: {4,0xff}, //auto retransmission dalay (4000us),auto retransmission count(15)
{5,0x17}, // ?????? (5,0x17), #32 channel
{6,0x3f}, //6,0x17), #air data rate-1M,out power 0dbm,setup LNA gain
{7,0x07}, //
{8,0x00}, //
{9,0x00}, //
{12,0xc3},// only LSB Receive address data pipe 2, MSB bytes is equal to RX_ADDR_P1[39:8]
{13,0xc4},// only LSB Receive address data pipe 3, MSB bytes is equal to RX_ADDR_P1[39:8]
{14,0xc5},// only LSB Receive address data pipe 4, MSB bytes is equal to RX_ADDR_P1[39:8]
{15,0xc6},// only LSB Receive address data pipe 5, MSB bytes is equal to RX_ADDR_P1[39:8]
{17,0x20},// Number of bytes in RX payload in data pipe0(32 byte)
{18,0x20},// Number of bytes in RX payload in data pipe1(32 byte)
{19,0x20},// Number of bytes in RX payload in data pipe2(32 byte)
{20,0x20},// Number of bytes in RX payload in data pipe3(32 byte)
{21,0x20},// Number of bytes in RX payload in data pipe4(32 byte)
{22,0x20},// Number of bytes in RX payload in data pipe5(32 byte)
{23,0x00},// fifo status
{28,0x00},// No dynamic payload length! {28,0x3F},// Enable dynamic payload length data pipe5\4\3\2\1\0
{29,0x07},// Enables Dynamic Payload Length,Enables Payload with ACK,Enables the W_TX_PAYLOAD_NOACK command
};
void write_pcmd(const uint8_t * cmd, uint8_t len)
{
SPI.setSS(1);
SPI.setSS(0);
while(len--) {
SPI.transfer(pgm_read_byte(cmd++));
};
SPI.setSS(1);
}
void write_reg(uint8_t reg, uint8_t val)
{
SPI.setSS(1);
SPI.setSS(0);
SPI.transfer(reg | 0x20);
SPI.transfer(val);
SPI.setSS(1);
}
__attribute__ ((always_inline))
uint8_t read_reg(uint8_t reg)
{
uint8_t res;
SPI.setSS(0);
SPI.transfer(reg);
res=SPI.transfer(0);
SPI.setSS(1);
return res;
}
void switch_cfg(uint8_t cnum)
{
uint8_t tmp = read_reg(0x07) & 0x80;
if(cnum) {
if(!tmp)
write_pcmd(cmd_switch_cfg,sizeof(cmd_switch_cfg));
} else {
if(tmp)
write_pcmd(cmd_switch_cfg,sizeof(cmd_switch_cfg));
}
}
void switch_to_rx_mode(void)
{
uint8_t val;
write_pcmd(cmd_flush_rx,sizeof(cmd_flush_rx));
val = read_reg(0x07);
write_reg(0x07,val);
set_ce(0);
val=read_reg(0x00);
val |= 0x01;
write_reg(0x00,val);
set_ce(1);
}
void switch_to_tx_mode(void)
{
uint8_t val;
write_pcmd(cmd_flush_tx,sizeof(cmd_flush_tx));
set_ce(0);
val=read_reg(0x00);
val &= ~0x01;
write_reg(0x00,val);
set_ce(1);
}
void set_channel(uint8_t cnum)
{
write_reg(5, cnum);
}
void write_reg_pbuf(uint8_t reg, uint8_t * buf, uint8_t len)
{
SPI.setSS(1);
SPI.setSS(0);
SPI.transfer(reg | 0x20);
while(len--)
SPI.transfer(pgm_read_byte(buf++));
SPI.setSS(1);
}
void read_reg_pbuf(uint8_t reg, uint8_t * buf, uint8_t len)
//len Byte von Register reg lesen
{
SPI.setSS(1);
SPI.setSS(0);
SPI.transfer(reg);
while(len--){
*buf=SPI.transfer(0);
buf++;
}
SPI.setSS(1);
}
uint8_t send_packet(uint8_t * data, uint8_t len)
{
uint8_t status;
switch_to_tx_mode();
status = read_reg(0x17); //FIFO_STATUS
if (status & 0x20) return 0xff; //Error?
SPI.setSS(0);
SPI.transfer(0xb0);
while(len--) {
SPI.transfer(*(data++));
}
SPI.setSS(1);
return 0;
}
void rfm70_init(uint8_t channel)
{
uint8_t i;
switch_cfg(0);
for(i=0;i<20;i++) {
write_reg(pgm_read_byte(&set0[i][0]),pgm_read_byte(&set0[i][1]));
}
write_reg_pbuf(10,adr0,sizeof(adr0)); //PIPE 0 read address
//write_reg_pbuf(11,adr1,sizeof(adr1)); // PIPE 1 not used!
write_reg_pbuf(16,adr0,sizeof(adr0));
set_channel(channel);
if(!read_reg(29))
write_pcmd(cmd_activate,sizeof(cmd_activate));
write_reg(pgm_read_byte(&set0[22][0]),pgm_read_byte(&set0[22][1]));
write_reg(pgm_read_byte(&set0[21][0]),pgm_read_byte(&set0[21][1]));
switch_cfg(1);
for(i=0;i<14;i++) {
write_reg_pbuf(i,set1[i],sizeof(set1[i]));
}
write_reg_pbuf(14,set1_14,sizeof(set1_14));
write_reg_pbuf(4,cmd_tog1,sizeof(cmd_tog1));
write_reg_pbuf(4,cmd_tog2,sizeof(cmd_tog2));
//delay 50 ms
_delay_ms(50);
switch_cfg(0);
switch_to_rx_mode();
}
#ifndef _RFM70_H_
#define _RFM70_H_
/* Includes: */
#include <ctype.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "SPI.h"
#define PORT_CE PORTD
#define DDR_CE DDRD
#define PIN_CE 3
static inline void set_ce(uint8_t val)
{
if(val)
PORT_CE |= (1<<PIN_CE);
else
PORT_CE &= ~(1<<PIN_CE);
}
static inline void rfm70_hw_setup(void)
{
DDR_CE |= (1<<PIN_CE);
}
void switch_cfg(uint8_t cnum); // switch to Register Bank cnum
void write_reg(uint8_t reg, uint8_t val);
uint8_t read_reg(uint8_t reg);
void write_pcmd(const uint8_t * cmd, uint8_t len); //Kommando und Parameter schreiben
void write_reg_pbuf(uint8_t reg, uint8_t * buf, uint8_t len); // auf Register reg die Daten in buf schreiben
void read_reg_pbuf(uint8_t reg, uint8_t * buf, uint8_t len);
void rfm70_init(uint8_t channel);
void switch_to_tx_mode(void);
void switch_to_rx_mode(void);
void show_error(uint8_t msg);
void set_channel(uint8_t cnum);
#endif
......@@ -2,10 +2,14 @@ cmake_minimum_required(VERSION 2.6.0)
include(${CMAKE_SOURCE_DIR}/CMakeConfig.cmake)
include_directories(${CMAKE_SOURCE_DIR}/src/lib/arduino)