Skip to content

Commit

Permalink
add LAN config - new commands for read and write ethernet config
Browse files Browse the repository at this point in the history
- es wird nun aus der Seriennummer des STM32 (UID) eine mac Adresse erzeugt (Adressbereich des VEB Kombinat Robotron)
- es gibt einen neuen Befehl "ri" zum Anzeigen der ethernet config
- mit "Wi..." kann die ethernet config geändert werden, wird erst nach einem Reset wirksam
 Wia - address
 Wig - gateway
 Win - network mask
z.B. Wia192.168.0.100

Die Default Werte sind
IP = 192.168.0.244
Gateway = 192.168.0.1
Netmask = 255.255.255.0
  • Loading branch information
Ralf9 committed May 3, 2020
1 parent aec52b7 commit d1ff85c
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 13 deletions.
126 changes: 114 additions & 12 deletions SIGNALDuino.ino
Expand Up @@ -40,16 +40,18 @@

#define MAPLE_SDUINO 1
//#define MAPLE_CUL 1
//#define LAN_WIZ 1
#define LAN_WIZ 1
//#define ARDUINO_ATMEGA328P_MINICUL 1
//#define OTHER_BOARD_WITH_CC1101 1
//#define CMP_MEMDBG 1

// bitte auch das "#define LAN_WIZ 1" in der SignalDecoder.h beachten
// *** bitte auch das "#define LAN_WIZ 1" in der SignalDecoder.h beachten ***
// *** bitte auch das "#define LAN_WIZ 1" in der USBD_reenumerate.c beachten ***

// bitte auch das "#define CMP_CC1101" in der SignalDecoder.h beachten

#define PROGNAME "RF_RECEIVER"
#define PROGVERS "4.1.0-dev200501"
#define PROGVERS "4.1.1-dev200503"
#define VERSION_1 0x41
#define VERSION_2 0x0d

Expand Down Expand Up @@ -144,11 +146,6 @@ SignalDetectorClass musterDec;
#ifdef LAN_WIZ
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAE, 0xBE, 0xEF, 0xF9, 0xE9 };
byte ip[] = { 192, 168, 0, 85 };
byte gateway[] = { 192, 168, 0, 191 };
byte subnet[] = { 255, 255, 255, 0 };

EthernetServer server = EthernetServer(23);
EthernetClient client;
Expand Down Expand Up @@ -181,6 +178,16 @@ SignalDetectorClass musterDec;
#define addr_selRadio 0xEF
//#define addr_bank 0xFD
#define addr_features 0xFF
// Ethernet EEProm Address
#define EE_MAC_ADDR 0xC0
#define EE_IP4_ADDR 0xC8
#define EE_IP4_GATEWAY 0xCC
#define EE_IP4_NETMASK 0xD0

const uint8_t mac_def[] = { 0x00, 0x80, 0x41 };
const uint8_t ip_def[] = { 192, 168, 0, 244 };
const uint8_t gateway_def[] = { 192, 168, 0, 1 };
const uint8_t netmask_def[] = { 255, 255, 255, 0 };

volatile bool blinkLED = false;
String cmdstring = "";
Expand All @@ -200,6 +207,11 @@ uint8_t ccmode = 0; // cc1101 Mode: 0 - normal, 1 - FIFO, 2 - FIFO ohne dup, 3
uint8_t radionr = defSelRadio;
uint8_t radio_bank[4];
uint8_t ccBuf[4][ccMaxBuf];
// Ethernet
uint8_t mac[6];
uint8_t ip[4];
uint8_t gateway[4];
uint8_t netmask[4];

void cmd_help_S();
void cmd_help();
Expand Down Expand Up @@ -347,11 +359,13 @@ void setup() {
digitalWrite(PIN_WIZ_RST, HIGH);
#endif
#endif
tools::EEbufferFill();
getEthernetConfig();
#ifdef LAN_WIZ
digitalWrite(PIN_WIZ_RST, LOW); // RESET should be heldlowat least 500 us for W5500
delayMicroseconds(500);
digitalWrite(PIN_WIZ_RST, HIGH);
Ethernet.begin(mac, ip, gateway, subnet);
Ethernet.begin(mac, ip, gateway, netmask);
server.begin(); // start listening for clients
#else
Serial.begin(BAUDRATE);
Expand Down Expand Up @@ -1495,6 +1509,26 @@ void print_radio_sum() // br - Bankinfo fuer alle cc1101 denen eine Bank zugeord
MSG_PRINTLN("");
}

void print_mac(uint8_t mac[])
{
for (uint8_t i = 0; i < 6; i++) {
printHex2(mac[i]);
if (i < 5) {
MSG_PRINT(F(":"));
}
}
}

void print_ip(uint8_t ip[])
{
for (uint8_t i = 0; i < 4; i++) {
MSG_PRINT(ip[i]);
if (i < 3) {
MSG_PRINT(F("."));
}
}
}

void cmd_Version() // V: Version
{
MSG_PRINT(F("V " PROGVERS " SIGNALduino "));
Expand Down Expand Up @@ -1704,9 +1738,27 @@ void cmd_writeEEPROM() // write EEPROM und CC11001 register
{
uint8_t val;
uint8_t reg;
bool ret = false;

if (cmdstring.charAt(1) == 'S' && cmdstring.charAt(2) == '3' && hasCC1101) { // WS<reg> Command Strobes
cc1101::commandStrobes();
} else if (cmdstring.charAt(1) == 'i') { // write ip
if (cmdstring.charAt(2) == 'a') { // adress
ret = tools::cmdstringPos2ip(ip, 3, EE_IP4_ADDR);
}
else if (cmdstring.charAt(2) == 'g') { // gateway
ret = tools::cmdstringPos2ip(gateway, 3, EE_IP4_GATEWAY);
}
else if (cmdstring.charAt(2) == 'n') { // netmask
ret = tools::cmdstringPos2ip(netmask, 3, EE_IP4_NETMASK);
}
else {
unsuppCmd = true;
return;
}
if (ret == false) {
MSG_PRINTLN(F("incorrect"));
}
} else if (isHexadecimalDigit(cmdstring.charAt(1)) && isHexadecimalDigit(cmdstring.charAt(2)) && isHexadecimalDigit(cmdstring.charAt(3)) && isHexadecimalDigit(cmdstring.charAt(4))) {
reg = tools::cmdstringPos2int(1);
val = tools::cmdstringPos2int(3);
Expand Down Expand Up @@ -1751,8 +1803,21 @@ void cmd_readEEPROM() // R<adr> read EEPROM
MSG_PRINT(F(" "));
}
MSG_PRINTLN("");
return;
}
else if (isHexadecimalDigit(cmdstring.charAt(1)) && isHexadecimalDigit(cmdstring.charAt(2))) { // r<adr> read EEPROM
if (cmdstring.charAt(1) == 'i') { // print ethernet config
MSG_PRINT(F("mac = "));
print_mac(mac);
MSG_PRINT(F(" ip = "));
print_ip(ip);
MSG_PRINT(F(" gw = "));
print_ip(gateway);
MSG_PRINT(F(" nm = "));
print_ip(netmask);
MSG_PRINTLN("");
return;
}
if (isHexadecimalDigit(cmdstring.charAt(1)) && isHexadecimalDigit(cmdstring.charAt(2))) { // r<adr> read EEPROM
uint8_t reg;
uint16_t reg16;

Expand All @@ -1774,9 +1839,9 @@ void cmd_readEEPROM() // R<adr> read EEPROM
printHex2(tools::EEread(reg16));
}
MSG_PRINTLN("");
} else {
unsuppCmd = true;
return;
}
unsuppCmd = true;
}

void cmd_writePatable()
Expand Down Expand Up @@ -2441,6 +2506,43 @@ void getSelRadioBank(void)
ccmode = ccmode & 0x0F;
}


void getEthernetConfig(void)
{
uint8_t i;

if (tools::EEread(EE_MAC_ADDR) != mac_def[0] || tools::EEread(EE_MAC_ADDR+1) != mac_def[1] || tools::EEread(EE_MAC_ADDR+2) != mac_def[2]) {
initEthernetConfig();
}

for (i = 0; i < 6; i++) {
mac[i] = tools::EEread(EE_MAC_ADDR+i);
}
for (i = 0; i < 4; i++) {
ip[i] = tools::EEread(EE_IP4_ADDR+i);
gateway[i] = tools::EEread(EE_IP4_GATEWAY+i);
netmask[i] = tools::EEread(EE_IP4_NETMASK+i);
}
}

void initEthernetConfig(void)
{
for (uint8_t i = 0; i < 4; i++) {
tools::EEwrite(EE_IP4_ADDR+i,ip_def[i]);
tools::EEwrite(EE_IP4_GATEWAY+i,gateway_def[i]);
tools::EEwrite(EE_IP4_NETMASK+i,netmask_def[i]);
}

tools::EEwrite(EE_MAC_ADDR, mac_def[0]);
tools::EEwrite(EE_MAC_ADDR+1, mac_def[1]);
tools::EEwrite(EE_MAC_ADDR+2, mac_def[2]);
uint32_t fserial = tools::flash_serial();
tools::EEwrite(EE_MAC_ADDR+3, (uint8_t)(fserial>>16 & 0xff));
tools::EEwrite(EE_MAC_ADDR+4, (uint8_t)(fserial>>8 & 0xff));
tools::EEwrite(EE_MAC_ADDR+5, (uint8_t)(fserial & 0xff));
tools::EEstore();
}

void initEEPROMconfig(void)
{
tools::EEwrite(addr_features, 0x37); // Init EEPROM with all flags enabled, except red, nn and toggleBank
Expand Down
4 changes: 4 additions & 0 deletions USBD_reenumerate.c
@@ -1,3 +1,6 @@
#define LAN_WIZ 1

#ifndef LAN_WIZ
#include "usbd_if.h"
#include "usbd_cdc_if.h"

Expand Down Expand Up @@ -27,6 +30,7 @@ void USBD_reenumerate(void)
/*delay(USBD_ENUM_DELAY);*/
#endif /* USB_DISC_PIN */
#endif /* USBD_REENUM_DISABLED */
#endif /* LAN_WIZ */
}

#endif
Expand Up @@ -41,7 +41,7 @@
#define CMP_CC1101
#define DEBUG 1

//#define LAN_WIZ 1 // die Ausgabe ueber Ethernet funktioniert nur, wenn dies hier nochmals definiert wird
#define LAN_WIZ 1 // die Ausgabe ueber Ethernet funktioniert nur, wenn dies hier nochmals definiert wird
#include "output.h"
#include "bitstore.h"
#include "FastDelegate.h"
Expand Down
109 changes: 109 additions & 0 deletions tools.h
Expand Up @@ -12,6 +12,9 @@ extern uint16_t bankOffset;
extern String cmdstring;

namespace tools {

void EEwrite(uint16_t, uint8_t);
void EEstore();

uint8_t hex2int(uint8_t hex) { // convert a hexdigit to int // Todo: printf oder scanf nutzen
if (hex >= '0' && hex <= '9') hex = hex - '0';
Expand All @@ -31,6 +34,46 @@ namespace tools {
val = hex2int(hex) + val;
return val;
}

bool cmdstringPos2ip(uint8_t* ip, uint8_t pos, uint8_t EE_pos) // eine ip im cmdstring im EEPROM merken und als array zurueckgeben
{
int8_t end;
uint8_t i;
uint32_t nr;
uint8_t tmpIp[4];

for (i = 0; i < 4; i++) {
end = cmdstring.indexOf('.',pos); // nach dem naechsten Punkt suchen
if (end > 0) {
nr = cmdstring.substring(pos, end).toInt();
}
else {
nr = cmdstring.substring(pos).toInt();
}
if (nr < 256) {
tmpIp[i] = (uint8_t)nr;
pos = end + 1;
}
else {
i = 4;
break;
}
if (end <= 0) { // letzter Wert
break;
}
}
if (i != 3) {
return false; // keine 4 Werte
}

for (i = 0; i < 4; i++) {
//MSG_PRINTLN(tmpIp[i]);
ip[i] = tmpIp[i];
EEwrite(EE_pos+i,tmpIp[i]);
}
EEstore();
return true;
}

void EEwrite(uint16_t adr, uint8_t val)
{
Expand Down Expand Up @@ -81,5 +124,71 @@ namespace tools {
{
return EEread(bankOffset + reg);
}


/*
* The width of the CRC calculation and result.
* Modify the typedef for a 16 or 32-bit CRC standard.
*/
typedef uint32_t crc;
#define CRC_POLYNOMIAL 0x8005
#define CRC_WIDTH (8 * sizeof(crc))
#define CRC_TOPBIT (1 << (CRC_WIDTH - 1))

crc CRCs(unsigned char* message, uint16_t count)
{
crc remainder = 0;
uint16_t byte;
unsigned char bit;

/*
* Perform modulo-2 division, a byte at a time.
*/
for (byte = 0; byte < count; ++byte)
{
/*
* Bring the next byte into the remainder.
*/
remainder ^= (message[byte] << (CRC_WIDTH - 8));

/*
* Perform modulo-2 division, a bit at a time.
*/
for (bit = 8; bit > 0; --bit)
{
/*
* Try to divide the current data bit.
*/
if (remainder & CRC_TOPBIT)
{
remainder = (remainder << 1) ^ CRC_POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
}

/*
* The final remainder is the CRC result.
*/
return (remainder);

} /* crcSlow() */


#define STM32_UUID ((uint32_t *)UID_BASE)

uint32_t flash_serial(void) // Seriennummer des STM32 (UID) auslesen
{
uint32_t v[3];

v[0] = STM32_UUID[0];
v[1] = STM32_UUID[1];
v[2] = STM32_UUID[2];

return CRCs((unsigned char*)v,9);
}
}
#endif

0 comments on commit d1ff85c

Please sign in to comment.