Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F1820931
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Size
24 KB
Subscribers
None
View Options
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,47 +1,48 @@
PROJECT(BlindControl)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_VERBOSE_MAKEFILE ON)
OPTION(BLINDCONTROL_USE_MEGA2560 "Use ATMega2560 instead of ATMega328p" OFF)
OPTION(BLINDCONTROL_USE_ETH_INTERRUPT "Use Ethernet Interrupt" OFF)
SET(CMAKE_C_COMPILER avr-gcc)
#SET(CMAKE_CXX_COMPILER avr-g++)
SET(CSTANDARD "-std=gnu99")
SET(CDEBUG "-gstabs")
SET(CWARN "-Wall -Wstrict-prototypes")
SET(CTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums")
SET(COPT "-Os")
IF(BLINDCONTROL_USE_MEGA2560)
SET(CMCU "-mmcu=atmega2560")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/linker_script.x")
ADD_DEFINITIONS(-DUSE_MEGA2560)
ELSE()
SET(CMCU "-mmcu=atmega328p")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
ENDIF()
SET(CDEFS "-DF_CPU=16000000")
IF(BLINDCONTROL_USE_ETH_INTERRUPT)
ADD_DEFINITIONS(-DUSE_ETHERNET_INTERRUPT)
ENDIF()
SET(CFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${COPT} ${CWARN} ${CSTANDARD} ${CEXTRA}")
#SET(CXXFLAGS "${CMCU} ${CDEFS} ${CINCS} ${COPT}")
SET(CMAKE_C_FLAGS ${CFLAGS})
#SET(CMAKE_CXX_FLAGS ${CXXFLAGS})
-ADD_EXECUTABLE(BlindControl main.c w5100.c ethernet.c spi.c)
+ADD_EXECUTABLE(BlindControl main.c w5100.c ethernet.c spi.c neteeprom.c)
ADD_CUSTOM_COMMAND(TARGET BlindControl POST_BUILD COMMAND avr-objcopy -O ihex -R .eeprom ${CMAKE_CURRENT_BINARY_DIR}/BlindControl ${CMAKE_CURRENT_BINARY_DIR}/BlindControl.hex)
+ADD_CUSTOM_COMMAND(TARGET BlindControl POST_BUILD COMMAND avr-objcopy -I ihex ${CMAKE_CURRENT_BINARY_DIR}/BlindControl.hex -O binary ${CMAKE_CURRENT_BINARY_DIR}/BlindControl.bin)
ADD_CUSTOM_TARGET(upload ${CMAKE_COMMAND} ${PROJECT_SOURCE_DIR})
IF(BLINDCONTROL_USE_MEGA2560)
ADD_CUSTOM_COMMAND(TARGET upload POST_BUILD COMMAND avrdude -V -c stk500v2 -p m2560 -b 115200 -P /dev/ttyACM0 -U flash:w:${CMAKE_CURRENT_BINARY_DIR}/BlindControl.hex)
ELSE()
ADD_CUSTOM_COMMAND(TARGET upload POST_BUILD COMMAND avrdude -V -c usbtiny -p m328p -U flash:w:${CMAKE_CURRENT_BINARY_DIR}/BlindControl.hex)
ENDIF()
SET_TARGET_PROPERTIES(BlindControl PROPERTIES COMPILE_FLAGS -D__AVR_LIBC_DEPRECATED_ENABLE__)
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -1,491 +1,611 @@
/*
* This is COSMICrtOS based on FreeRTOS port to ATmega2560.
* (c) 2011 Andreas Boehler <andreas@aboehler.at>
*
* ATTENTION: Software licenses apply! GPL is probably incompatible
* with our NDA! Can we link to a binary blob instead?
*/
// FIXME: Get rid of FreeRTOS
#include <stdlib.h>
#include <string.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
+#include <avr/wdt.h>
#include <util/delay.h>
#include "iomacro.h"
#include "main.h"
-
+#include "neteeprom.h"
#include "ethernet.h"
volatile int gEthFired = FALSE;
SOCKET gSock = MAX_SOCK_NUM;
#define TOGGLE_DELAY 300
#define INCR_DELAY 200
#define CMD_STOP 5
#define CMD_SELECT 6
#define CMD_UP 7
#define CMD_DOWN 8
#ifdef USE_MEGA2560 // ATMega2560 (Arduino Mega2560) defines
#define AI_PIN0 F,0
#define AI_PIN1 F,1
#define AI_PIN2 F,2
#define AI_PIN3 F,3
#define DO_PIN_STOP E,3
#define DO_PIN_SELECT H,3
#define DO_PIN_UP H,4
#define DO_PIN_DOWN H,5
#else // ATMega328p (Arduino Ethernet) defines
#define AI_PIN0 C,0
#define AI_PIN1 C,1
#define AI_PIN2 C,2
#define AI_PIN3 C,3
#define DO_PIN_STOP D,5
#define DO_PIN_SELECT D,6
#define DO_PIN_UP D,7
#define DO_PIN_DOWN B,0
#endif
#ifdef USE_ETHERNET_INTERRUPT
/***************************************
* Helper function to setup interrupts *
***************************************/
void setupExternalInterrupts(void)
{
EICRB |= (1 << ISC41); // Enable falling edge interrupt
EIMSK |= (1 << INT4); // enable external interrupt 4
}
/****************************************************
* Setup the interrupt handler for ethernet traffic *
****************************************************/
ISR(INT4_vect)
{
gEthFired = TRUE;
}
#endif
/************************************************
* Setup and initialize the ethernet controller *
************************************************/
void setupEthernet(void)
{
- uint8_t mac[] = { 0x00, 0x01, 0x02, 0x0D, 0x74, 0x47 };
- uint8_t ip[] = { 192, 168, 1, 20 };
- uint8_t gateway[] = { 192, 168, 1, 1 };
- uint8_t subnet[] = { 255, 255, 255, 0 };
+ //uint8_t mac[] = { 0x00, 0x01, 0x02, 0x0D, 0x74, 0x49 };
+ //uint8_t ip[] = { 192, 168, 1, 30 };
+ //uint8_t gateway[] = { 192, 168, 1, 1 };
+ //uint8_t subnet[] = { 255, 255, 255, 0 };
+ uint8_t mac[6];
+ uint8_t ip[4];
+ uint8_t gateway[4];
+ uint8_t subnet[4];
+
+ eepromReadMAC(mac);
+ eepromReadIP(ip);
+ eepromReadGW(gateway);
+ eepromReadSN(subnet);
- if(ethernet_init(mac, ip, gateway, subnet))
+ if(!eepromNetSigIsSet())
+ {
+ eepromWriteIP(ip);
+ eepromWriteMAC(mac);
+ eepromWriteGW(gateway);
+ eepromWriteSN(subnet);
+ eepromWriteNetSig();
+ }
+
+ if(ethernet_init(mac, ip, gateway, subnet))
+ {
+ if((gSock = socket_server_init(SOCK_PORT)))
{
- if((gSock = socket_server_init(SOCK_PORT)))
- {
- gSock -= 1;
- }
+ gSock -= 1;
}
+ }
}
/*****************
* Main function *
*****************/
int main( void )
{
- // Setup registers for input and output
- setupIOPorts();
+ // Setup registers for input and output
+ setupIOPorts();
#ifdef USE_ETHERNET_INTERRUPT
setupExternalInterrupts();
#endif
if(spi_init())
setupEthernet();
#ifdef USE_ETHERNET_INTERRUPT
sei();
#endif
-/*
- selectBlindChannel(5);
- _delay_ms(5000);
- blindUp(1);
- blindDown(1);
- blindDown(2);
- blindDown(3);
- blindDown(4);
- blindDown(5);
-*/
+ uint8_t new_connect[MAX_SOCK_NUM];
+ uint8_t connected[MAX_SOCK_NUM];
+ uint8_t i;
+ uint16_t len;
+ SOCKET sock;
+
+ unsigned char clientline[BUF_SIZE];
- uint8_t new_connect[MAX_SOCK_NUM];
- uint8_t connected[MAX_SOCK_NUM];
- uint8_t i;
- uint16_t len;
- SOCKET sock;
+ for(i=0; i < MAX_SOCK_NUM; i++)
+ {
+ new_connect[i] = TRUE;
+ connected[i] = FALSE;
+ }
- unsigned char clientline[BUF_SIZE];
-
- for(i=0; i < MAX_SOCK_NUM; i++)
+ for( ;; )
+ {
+#ifndef USE_ETHERNET_INTERRUPT
+ for(i=0; i<MAX_SOCK_NUM; i++)
{
- new_connect[i] = TRUE;
- connected[i] = FALSE;
- }
-
- for( ;; )
- {
-#ifndef USE_ETHERNET_INTERRUPT
- for(i=0; i<MAX_SOCK_NUM; i++)
+ if(socket_server_socket_connected(i))
{
- if(socket_server_socket_connected(i))
+ if((new_connect[i]) || (socket_server_data_available(i)))
{
- if((new_connect[i]) || (socket_server_data_available(i)))
- {
- gEthFired = TRUE;
- sock = i;
- break;
- }
- }
- }
-#endif
- if(gEthFired)
- {
- gEthFired = FALSE;
-#ifdef USE_ETHERNET_INTERRUPT
- sock = socket_server_handle_interrupt();
-#endif
- connected[sock] = socket_server_socket_connected(sock);
- if(connected[sock])
- {
- if(new_connect[sock])
- {
- socket_server_write(sock, (unsigned char*)"BlindControl ready\n", (uint16_t)19);
- new_connect[sock] = FALSE;
- }
- }
- else
- {
- if (!new_connect[sock])
- new_connect[sock] = TRUE;
- if(socket_server_socket_closed(sock))
- {
- socket_server_close_socket(gSock);
- setupEthernet();
- }
-
- }
- if(connected[sock] && (len = socket_server_data_available(sock)))
- {
- if(len > BUF_SIZE)
- len = BUF_SIZE;
- socket_server_receive(sock, clientline, len);
- if(!handle_req((signed char*)clientline))
- {
- socket_server_close_socket(gSock);
- new_connect[sock] = TRUE;
- setupEthernet();
- }
+ gEthFired = TRUE;
+ sock = i;
+ break;
}
}
}
+#endif
+ if(gEthFired)
+ {
+ gEthFired = FALSE;
+#ifdef USE_ETHERNET_INTERRUPT
+ sock = socket_server_handle_interrupt();
+#endif
+ connected[sock] = socket_server_socket_connected(sock);
+ if(connected[sock])
+ {
+ if(new_connect[sock])
+ {
+ socket_server_write(sock, (unsigned char*)"BlindControl ready\n", (uint16_t)19);
+ new_connect[sock] = FALSE;
+ }
+ }
+ else
+ {
+ if (!new_connect[sock])
+ new_connect[sock] = TRUE;
+ if(socket_server_socket_closed(sock))
+ {
+ socket_server_close_socket(gSock);
+ setupEthernet();
+ }
- return 0;
+ }
+ if(connected[sock] && (len = socket_server_data_available(sock)))
+ {
+ if(len >= BUF_SIZE)
+ len = BUF_SIZE-1;
+ socket_server_receive(sock, clientline, len);
+ clientline[len] = '\0';
+ if(!handle_req((signed char*)clientline))
+ {
+ socket_server_close_socket(gSock);
+ new_connect[sock] = TRUE;
+ setupEthernet();
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+void wdtReset(void)
+{
+ wdt_disable();
+ wdt_enable(WDTO_2S);
}
int handle_req(signed char *clientline)
{
int blindNum;
+ uint8_t buff[6];
char helpStr[] = "Available Commands: SelectBlind N, BlindUp[ N], BlindDown[ N], "
- "BlindStop[ N], BlindIncr[ N], BlindDecr[ N], AllUp, AllDown, AllStop, help, quit\n\n";
+ "BlindStop[ N], BlindIncr[ N], BlindDecr[ N], AllUp, AllDown, AllStop, SetIP, "
+ "GetIP, SetSN, SetMAC, SetGW, help, reset, reprogram, quit\n\n";
if(strncmp(clientline, "SelectBlind ", 12) == 0)
{
blindNum = atoi(&clientline[12]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindUp ", 8) == 0)
{
blindNum = atoi(&clientline[8]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindUp(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindDown ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindDown(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindStop ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindStop(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindIncr ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
sendCommand(CMD_UP);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindDecr ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
sendCommand(CMD_DOWN);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP); socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindUp", 7) == 0)
{
sendCommand(CMD_UP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindDown", 9) == 0)
{
sendCommand(CMD_DOWN);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindStop", 9) == 0)
{
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindIncr", 9) == 0)
{
sendCommand(CMD_UP);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindDecr", 9) == 0)
{
sendCommand(CMD_DOWN);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "AllUp", 5) == 0)
{
for(blindNum=1; blindNum<6; blindNum++)
{
blindUp(blindNum);
}
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "AllDown", 7) == 0)
{
for(blindNum=1; blindNum<6; blindNum++)
{
blindDown(blindNum);
}
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "AllStop", 7) == 0)
{
for(blindNum = 1; blindNum < 6; blindNum++)
{
blindStop(blindNum);
}
socket_server_write(gSock, "OK\n", 3);
}
+ else if(strncmp(clientline, "reset", 5) == 0)
+ {
+ wdtReset();
+ socket_server_write(gSock, "OK\n", 3);
+ return FALSE;
+ }
+ else if(strncmp(clientline, "reprogram", 9) == 0)
+ {
+ eepromWriteImgBad();
+ wdtReset();
+ socket_server_write(gSock, "OK\n", 3);
+ return FALSE;
+ }
+ else if(strncmp(clientline, "SetIP", 5) == 0)
+ {
+ if(strlen(clientline) > 13)
+ {
+ parseAddr(clientline+6, buff, ".");
+ eepromWriteIP(buff);
+ if(!eepromNetSigIsSet())
+ eepromWriteNetSig();
+ socket_server_write(gSock, "OK\n", 3);
+ }
+ else
+ {
+ socket_server_write(gSock, "ERR\n", 4);
+ }
+ }
+ else if(strncmp(clientline, "SetGW", 5) == 0)
+ {
+ if(strlen(clientline) > 13)
+ {
+ parseAddr(clientline+6, buff, ".");
+ eepromWriteGW(buff);
+ if(!eepromNetSigIsSet())
+ eepromWriteNetSig();
+ socket_server_write(gSock, "OK\n", 3);
+ }
+ else
+ {
+ socket_server_write(gSock, "ERR\n", 4);
+ }
+ }
+ else if(strncmp(clientline, "SetSN", 5) == 0)
+ {
+ if(strlen(clientline) > 13)
+ {
+ parseAddr(clientline+6, buff, ".");
+ eepromWriteSN(buff);
+ if(!eepromNetSigIsSet())
+ eepromWriteNetSig();
+ socket_server_write(gSock, "OK\n", 3);
+ }
+ else
+ {
+ socket_server_write(gSock, "ERR\n", 4);
+ }
+ }
+ else if(strncmp(clientline, "SetMAC", 6) == 0)
+ {
+ if(strlen(clientline) > 13) // FIXME: Must be bigger!
+ {
+ parseAddr(clientline+7, buff, ":");
+ eepromWriteMAC(buff);
+ if(!eepromNetSigIsSet())
+ eepromWriteNetSig();
+ socket_server_write(gSock, "OK\n", 3);
+ }
+ else
+ {
+ socket_server_write(gSock, "ERR\n", 4);
+ }
+ }
+ else if(strncmp(clientline, "GetIP", 5) == 0)
+ {
+ uint8_t buff2[10];
+ eepromReadIP(buff);
+ for(int i=0; i<4; i++)
+ {
+ itoa(buff[i], buff2, 10);
+ socket_server_write(gSock, buff2, strlen(buff2));
+ if(i<3)
+ socket_server_write(gSock, ".", 1);
+ else
+ socket_server_write(gSock, "\n", 1);
+ }
+ socket_server_write(gSock, "OK\n", 3);
+ }
else if(strncmp(clientline, "help", 4) == 0)
{
socket_server_write(gSock, helpStr, strlen(helpStr));
}
else if(strncmp(clientline, "quit", 4) == 0)
{
return FALSE;
}
else
{
socket_server_write(gSock, "ERR\n", 4);
}
return TRUE;
}
int getCurrentBlindChannel(void)
{
int currentChannel = 0;
int readValues;
+ readValues = readFromADC();
+ if(readValues == 0)
+ {
+ sendCommand(CMD_SELECT);
readValues = readFromADC();
- if(readValues == 0)
- {
- sendCommand(CMD_SELECT);
- readValues = readFromADC();
- }
- switch(readValues)
- {
- case 1:
- currentChannel = 1;
- break;
- case 2:
- currentChannel = 2;
- break;
- case 4:
- currentChannel = 3;
- break;
- case 8:
- currentChannel = 4;
- break;
- case 15:
- currentChannel = 5;
- break;
- default:
- currentChannel = 0;
- }
+ }
+ switch(readValues)
+ {
+ case 1:
+ currentChannel = 1;
+ break;
+ case 2:
+ currentChannel = 2;
+ break;
+ case 4:
+ currentChannel = 3;
+ break;
+ case 8:
+ currentChannel = 4;
+ break;
+ case 15:
+ currentChannel = 5;
+ break;
+ default:
+ currentChannel = 0;
+ }
return currentChannel;
}
int readFromADC(void)
{
int j;
int channel = 0;
for(j=0;j<200;j++)
{
if(!IS_SET(AI_PIN0))
channel |= 0x01;
if(!IS_SET(AI_PIN1))
channel |= 0x02;
if(!IS_SET(AI_PIN2))
channel |= 0x04;
if(!IS_SET(AI_PIN3))
channel |= 0x08;
}
return channel;
}
int selectBlindChannel(int telisChannel)
{
while(getCurrentBlindChannel() != telisChannel)
sendCommand(CMD_SELECT);
return TRUE;
}
int blindUp(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_UP);
return TRUE;
}
int blindDown(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_DOWN);
return TRUE;
}
int blindStop(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_STOP);
return TRUE;
}
int sendCommand(int digitalChannel)
{
switch(digitalChannel)
{
- case CMD_STOP:
+ case CMD_STOP:
SET(DO_PIN_STOP);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_STOP);
break;
case CMD_SELECT:
SET(DO_PIN_SELECT);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_SELECT);
break;
case CMD_UP:
SET(DO_PIN_UP);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_UP);
break;
case CMD_DOWN:
SET(DO_PIN_DOWN);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_DOWN);
break;
}
_delay_ms(TOGGLE_DELAY);
return TRUE;
}
/****************************************
* Setup all required I/O Ports for use *
****************************************/
uint8_t setupIOPorts( void )
{
SET_INPUT(AI_PIN0);
SET_INPUT(AI_PIN1);
SET_INPUT(AI_PIN2);
SET_INPUT(AI_PIN3);
RESET(DO_PIN_STOP);
RESET(DO_PIN_SELECT);
RESET(DO_PIN_UP);
RESET(DO_PIN_DOWN);
SET_OUTPUT(DO_PIN_STOP);
SET_OUTPUT(DO_PIN_SELECT);
SET_OUTPUT(DO_PIN_UP);
SET_OUTPUT(DO_PIN_DOWN);
- return TRUE;
+ return TRUE;
+}
+
+void parseAddr(signed char *str, uint8_t *ip, char *delim)
+{
+ int i = 0;
+
+ char* buff = (char*)malloc(10);
+ buff = strtok(str, delim);
+ while (buff != NULL)
+ {
+ ip[i] = (uint8_t)atoi(buff);
+ buff = strtok(NULL, delim);
+ i++;
+ }
+ free(buff);
}
//EOF
diff --git a/main.h b/main.h
--- a/main.h
+++ b/main.h
@@ -1,44 +1,46 @@
#ifndef MAIN_H
#define MAIN_H
#define TRUE 1
#define FALSE 0
#ifdef USE_MEGA2560 // Arduino Mega2560 defines
#define P_MOSI B,2
#define P_MISO B,3
#define P_SCK B,1
#define P_SS B,0
#define W5100_CS B,4
#define W5100_INT D,2
#else // Arduino Ethernet defines
#define P_MOSI B,3 //B,2
#define P_MISO B,4 //B,3
#define P_SCK B,5 //B,1
#define P_SS B,2 //B,0
#define W5100_CS B,2 //B,4
#define W5100_INT E,4 // D,2
#endif
#define BUF_SIZE 255
#define SOCK_PORT 63543
int readADC(int digitalChannel);
int getCurrentBlindChannel(void);
int selectBlindChannel(int telisChannel);
int blindUp(int telisChannel);
int blindDown(int telisChannel);
int blindStop(int telisChannel);
int sendCommand(int digitalChannel);
int readFromADC(void);
+void wdtReset(void);
+void parseAddr(signed char *str, uint8_t *ip, char *delim);
int main(void);
#ifdef USE_ETHERNET_INTERRUPT
void setupExternalInterrupts(void);
#endif
int handle_req(signed char* clientline);
uint8_t setupIOPorts(void);
#endif
diff --git a/neteeprom.c b/neteeprom.c
new file mode 100644
--- /dev/null
+++ b/neteeprom.c
@@ -0,0 +1,131 @@
+#include "neteeprom.h"
+#include <avr/pgmspace.h>
+#include <avr/eeprom.h>
+#include "main.h"
+#include <string.h>
+
+void eepromWriteImgBad(void)
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_IMG_STAT, NETEEPROM_IMG_BAD_VALUE); // Image status set to invalid
+}
+
+void eepromWriteImgGood(void)
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_IMG_STAT, NETEEPROM_IMG_GOOD_VALUE);
+}
+
+void eepromWriteNetSig(void)
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_1, NETEEPROM_SIG_1_VALUE); // Set signature 1 to load eeprom settings
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_2, NETEEPROM_SIG_2_VALUE); // Set signature 2
+}
+void eepromWriteAddr(uint8_t* addr, uint8_t start)
+{
+ for(uint8_t i = 0; i < 4; i++)
+ eeprom_write_byte((uint8_t*)(i + start), addr[i]);
+}
+
+void eepromWriteMAC(uint8_t *mac)
+{
+ for(uint8_t i = 0; i < 6; i++)
+ eeprom_write_byte((uint8_t*)(i + NETEEPROM_MAC), mac[i]);
+}
+
+
+void eepromWriteIP(uint8_t *ip)
+{
+ eepromWriteAddr(ip, NETEEPROM_IP);
+}
+
+void eepromWriteGW(uint8_t *gw)
+{
+ eepromWriteAddr(gw, NETEEPROM_GW);
+}
+
+void eepromWriteSN(uint8_t *sn)
+{
+ eepromWriteAddr(sn, NETEEPROM_SN);
+}
+
+void eepromReadAddr(uint8_t* octet, uint8_t start)
+{
+ for(uint8_t i=0; i<4; i++)
+ octet[i] = eeprom_read_byte((uint8_t*)(start+i));
+}
+
+void eepromWritePortSig(void)
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_3, NETEEPROM_SIG_3_VALUE);
+}
+
+void eepromEraseNetSig()
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_1, 0); // Unset signature 1 to load built-in settings
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_2, 0); // Unset signature 2
+}
+
+
+void eepromErasePortSig()
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_SIG_3, 0);
+}
+
+
+void eepromWritePort(int port)
+{
+ eeprom_write_byte((uint8_t*)NETEEPROM_PORT, (port & 0xFF));
+ eeprom_write_byte((uint8_t*)NETEEPROM_PORT + 1, (port >> 8));
+
+ eepromWritePortSig();
+}
+
+
+void eepromReadMAC(uint8_t *mac)
+{
+ uint8_t default_mac[6] = {DEFAULT_MAC_ADDR};
+ if(eepromNetSigIsSet())
+ for(uint8_t i=0;i<6;i++)
+ mac[i] = eeprom_read_byte((uint8_t*)(NETEEPROM_MAC + i));
+ else
+ memcpy((void*)mac, (void*)default_mac, 6);
+}
+
+
+void eepromReadIP(uint8_t *ip)
+{
+ uint8_t default_ip[4] = {DEFAULT_IP_ADDR};
+ if(eepromNetSigIsSet())
+ eepromReadAddr(ip, NETEEPROM_IP);
+ else
+ memcpy((void*)ip, (void*)default_ip, 4);
+}
+
+
+void eepromReadGW(uint8_t *gw)
+{
+ uint8_t default_gw[4] = {DEFAULT_GW_ADDR};
+ if(eepromNetSigIsSet())
+ eepromReadAddr(gw, NETEEPROM_GW);
+ else
+ memcpy((void*)gw, (void*)default_gw, 4);
+}
+
+
+void eepromReadSN(uint8_t *sn)
+{
+ uint8_t default_sn[4] = {DEFAULT_SUB_MASK};
+ if(eepromNetSigIsSet())
+ eepromReadAddr(sn, NETEEPROM_SN);
+ else
+ memcpy((void*)sn, (void*)default_sn, 4);
+}
+
+
+int eepromNetSigIsSet()
+{
+ if((eeprom_read_byte((uint8_t*)NETEEPROM_SIG_1) == NETEEPROM_SIG_1_VALUE)
+ && (eeprom_read_byte((uint8_t*)NETEEPROM_SIG_2) == NETEEPROM_SIG_2_VALUE))
+ return(TRUE);
+ else
+ return(FALSE);
+}
diff --git a/neteeprom.h b/neteeprom.h
new file mode 100644
--- /dev/null
+++ b/neteeprom.h
@@ -0,0 +1,66 @@
+#ifndef NETEEPROM_H
+#define NETEEPROM_H
+
+#include <stdint.h>
+
+/* EEPROM partitioning */
+#define NETEEPROM_MAJVER 0
+#define NETEEPROM_MINVER 1
+#define NETEEPROM_IMG_STAT 2
+#define NETEEPROM_SIG_1 3
+#define NETEEPROM_SIG_2 4
+#define NETEEPROM_DATA 5
+#define NETEEPROM_GW 5
+#define NETEEPROM_SN 9
+#define NETEEPROM_MAC 13
+#define NETEEPROM_IP 19
+#define NETEEPROM_SIG_3 23
+#define NETEEPROM_PORT 24
+#define NETEEPROM_SIG_4 26
+#define NETEEPROM_PASS 27
+#define NETEEPROM_END 63
+
+#define EEPROM_SETTINGS_SIZE 18
+#define REGISTER_BLOCK_SIZE 28
+
+/* EEPROM values */
+#define ARIADNE_MAJVER 0
+#define ARIADNE_MINVER 4
+
+#define NETEEPROM_IMG_GOOD_VALUE (0xEE)
+#define NETEEPROM_IMG_BAD_VALUE (0xFF)
+
+#define NETEEPROM_SIG_1_VALUE (0x55)
+#define NETEEPROM_SIG_2_VALUE (0xAA)
+#define NETEEPROM_SIG_3_VALUE (0xBB)
+#define NETEEPROM_SIG_4_VALUE (0xCC)
+
+#define DEFAULT_MAC_ADDR 0xDE,0xAD,0xBE,0xEF,0xFE,0xED
+#define DEFAULT_IP_ADDR 192,168,1,128
+#define DEFAULT_SUB_MASK 255,255,255,0
+#define DEFAULT_GW_ADDR 192,168,1,1
+
+
+void eepromWriteImgBad(void);
+void eepromWriteImgGood(void);
+void eepromWriteNetSig(void);
+void eepromEraseNetSig(void);
+void eepromErasePortSig(void);
+void eepromWriteAddr(uint8_t* addr, uint8_t start);
+void eepromWriteMAC(uint8_t* mac);
+void eepromWriteIP(uint8_t* ip);
+void eepromWriteGW(uint8_t* gw);
+void eepromWriteSN(uint8_t* sn);
+void eepromWritePortSig(void);
+void eepromWritePort(int port);
+void eepromReadAddr(uint8_t *octet, uint8_t start);
+void eepromReadMAC(uint8_t* mac);
+void eepromReadIP(uint8_t* ip);
+void eepromReadGW(uint8_t* gw);
+void eepromReadSN(uint8_t* sn);
+int eepromNetSigIsSet(void);
+
+
+
+
+#endif //NETEEPROM_H
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Dec 22, 9:52 PM (2 d, 21 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
533956
Default Alt Text
(24 KB)
Attached To
rBC BlindControl
Event Timeline
Log In to Comment