Skip to content

Commit

Permalink
added new baud and port commands to dynamic config
Browse files Browse the repository at this point in the history
  • Loading branch information
dacb committed Dec 18, 2014
1 parent a4068b5 commit e0a9c8c
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 37 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Telnet into the module and issue commands prefixed by +++AT to escape each comma
+++AT AP # print the current soft ap settings
+++AT AP <ssid> # set the AP as open with specified ssid
+++AT AP <ssid> <password> # set the AP as WPA with password
+++AT BAUD # print current UART baud rate
+++AT BAUD <baud> # set currrent UART baud rate
+++AT PORT # print current incoming TCP socket port
+++AT PORT <port> # set current incoming TCP socket port (restarts ESP)
+++AT RESET # software reset the unit
```
Upon success, all commands send back "OK" as their final output. Note that passwords may not contain spaces. For the softAP, the mode is fixed to AUTH_WPA_PSK.
Expand Down
65 changes: 65 additions & 0 deletions user/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "espconn.h"
#include "mem.h"
#include "osapi.h"
#include "driver/uart.h"

#include "flash_param.h"

#else

Expand Down Expand Up @@ -170,6 +173,66 @@ void config_cmd_reset(struct espconn *conn, uint8_t argc, char *argv[]) {
system_restart();
}

void config_cmd_baud(struct espconn *conn, uint8_t argc, char *argv[]) {
flash_param_t *flash_param = flash_param_get();

if (argc == 0) {
char buf[MSG_BUF_LEN];
uint8_t len;
len = os_sprintf(buf, "BAUD=%d\n", flash_param->baud);
espconn_sent(conn, buf, len);
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
} else if (argc != 1) {
espconn_sent(conn, MSG_ERROR, strlen(MSG_ERROR));
} else {
uint32_t baud = atoi(argv[1]);
if ((baud > (UART_CLK_FREQ / 16)) || baud == 0) {
espconn_sent(conn, MSG_ERROR, strlen(MSG_ERROR));
} else {
// pump and dump fifo
while(TRUE) {
uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) {
break;
}
}
os_delay_us(10000);
uart_div_modify(0, UART_CLK_FREQ / baud);
flash_param->baud = baud;
flash_param_set();
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
}
}
}

void config_cmd_port(struct espconn *conn, uint8_t argc, char *argv[]) {
flash_param_t *flash_param = flash_param_get();

if (argc == 0) {
char buf[MSG_BUF_LEN];
uint8_t len;
len = os_sprintf(buf, "PORT=%d\n", flash_param->port);
espconn_sent(conn, buf, len);
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
} else if (argc != 1) {
espconn_sent(conn, MSG_ERROR, strlen(MSG_ERROR));
} else {
uint32_t port = atoi(argv[1]);
if (port == 0) {
espconn_sent(conn, MSG_ERROR, strlen(MSG_ERROR));
} else {
if (port != flash_param->port) {
flash_param->port = port;
flash_param_set();
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
system_restart();
} else {
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
}
}
}
}

void config_cmd_mode(struct espconn *conn, uint8_t argc, char *argv[]) {
uint8_t mode;

Expand Down Expand Up @@ -266,6 +329,8 @@ void config_cmd_ap(struct espconn *conn, uint8_t argc, char *argv[]) {

const config_commands_t config_commands[] = {
{ "RESET", &config_cmd_reset },
{ "BAUD", &config_cmd_baud },
{ "PORT", &config_cmd_port },
{ "MODE", &config_cmd_mode },
{ "STA", &config_cmd_sta },
{ "AP", &config_cmd_ap },
Expand Down
57 changes: 57 additions & 0 deletions user/flash_param.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "user_interface.h"
#include "spi_flash.h"
#include "ets_sys.h"
#include "c_types.h"
#include "flash_param.h"

#define FLASH_PARAM_START_SECTOR 0x3C
#define FLASH_PARAM_ADDR (SPI_FLASH_SEC_SIZE * FLASH_PARAM_START_SECTOR)

static int flash_param_loaded = 0;
static flash_param_t flash_param;

void ICACHE_FLASH_ATTR flash_param_read(flash_param_t *flash_param) {
spi_flash_read(FLASH_PARAM_ADDR, (uint32 *)flash_param, sizeof(flash_param_t));
}

void ICACHE_FLASH_ATTR flash_param_write(flash_param_t *flash_param) {
ETS_UART_INTR_DISABLE();
spi_flash_erase_sector(FLASH_PARAM_START_SECTOR);
spi_flash_write(FLASH_PARAM_ADDR, (uint32 *) flash_param, sizeof(flash_param_t));
ETS_UART_INTR_ENABLE();
}

flash_param_t *ICACHE_FLASH_ATTR flash_param_get(void) {
if (!flash_param_loaded) {
flash_param_read(&flash_param);
flash_param_loaded = 1;
}
return &flash_param;
}

void ICACHE_FLASH_ATTR flash_param_set(void) {
flash_param_write(&flash_param);
flash_param_t tmp;
flash_param_read(&tmp);
if (memcmp(&tmp, &flash_param, sizeof(flash_param_t)) != 0) {
DCE_FAIL("flash_param verify failed");
}
}

void ICACHE_FLASH_ATTR flash_param_init_defaults(void) {
flash_param_t *flash_param = flash_param_get();
flash_param->magic = FLASH_PARAM_MAGIC;
flash_param->version = FLASH_PARAM_VERSION;
flash_param->baud = 115200;
flash_param->port = 23;
flash_param_set();
}

flash_param_t* ICACHE_FLASH_ATTR flash_param_init(void) {
flash_param_t *flash_param = flash_param_get();
if (flash_param->magic != FLASH_PARAM_MAGIC || flash_param->version != FLASH_PARAM_VERSION) {
flash_param_init_defaults();
}
return flash_param;
}

16 changes: 16 additions & 0 deletions user/flash_param.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __FLASH_PARAM_H__
#define __FLASH_PARAM_H__

#define FLASH_PARAM_MAGIC 8255
#define FLASH_PARAM_VERSION 3

typedef struct flash_param {
uint32_t magic;
uint32_t version;
uint32_t baud;
uint32_t port;
} flash_param_t;

flash_param_t *flash_param_get(void);

#endif /* __FLASH_PARAM_H__ */
84 changes: 47 additions & 37 deletions user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ets_sys.h"
#include "os_type.h"
Expand All @@ -22,57 +22,67 @@
#include "task.h"

#include "server.h"
#include "config.h"
#include "flash_param.h"

os_event_t recvTaskQueue[recvTaskQueueLen];
os_event_t recvTaskQueue[recvTaskQueueLen];
extern serverConnData connData[MAX_CONN];

static void ICACHE_FLASH_ATTR recvTask(os_event_t *events)
{
uint8_t c, i;
uint8_t c, i;

//uart0_sendStr("\r\nrecTask called\r\n");
//uart0_sendStr("\r\nrecTask called\r\n");

while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S))
{
WRITE_PERI_REG(0X60000914, 0x73); //WTD
c = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S))
{
WRITE_PERI_REG(0X60000914, 0x73); //WTD
c = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;

for (i = 0; i < MAX_CONN; ++i) {
if (connData[i].conn) {
espconn_sent(connData[i].conn, &c, 1);
}
}
// echo
// uart_tx_one_char(c);
}
for (i = 0; i < MAX_CONN; ++i) {
if (connData[i].conn) {
espconn_sent(connData[i].conn, &c, 1);
}
}
// echo
// uart_tx_one_char(c);
}

if(UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST))
{
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
}
else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST))
{
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
}
ETS_UART_INTR_ENABLE();
if(UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST))
{
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
}
else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST))
{
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
}
ETS_UART_INTR_ENABLE();
}

void user_init(void)
{
uint8_t i;
uint8_t i;

uart_init(BIT_RATE_115200, BIT_RATE_115200);

#ifdef CONFIG_STATIC
// refresh wifi config
config_execute();
#endif
#ifdef CONFIG_DYNAMIC
flash_param_t *flash_param = flash_param_get();
uart_init(flash_param->baud, BIT_RATE_115200);
#else
uart_init(BIT_RATE_115200, BIT_RATE_115200);
#endif

serverInit(23);
#ifdef CONFIG_STATIC
// refresh wifi config
config_execute();
#endif

for (i = 0; i < 16; ++i)
uart0_sendStr("\r\n");
#ifdef CONFIG_DYNAMIC
serverInit(flash_param->port);
#else
serverInit(23);
#endif

system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen);
for (i = 0; i < 16; ++i)
uart0_sendStr("\r\n");

system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen);
}

0 comments on commit e0a9c8c

Please sign in to comment.