2016-03-22 03:05:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014-2016 Cesanta Software Limited
|
|
|
|
* All rights reserved
|
|
|
|
*/
|
|
|
|
|
2016-03-24 20:58:26 +08:00
|
|
|
#include <stdbool.h>
|
2016-03-22 03:05:28 +08:00
|
|
|
#include <stdio.h>
|
2016-03-30 23:52:56 +08:00
|
|
|
#include <stdlib.h>
|
2016-03-22 03:05:28 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-04-15 21:10:15 +08:00
|
|
|
#include <inc/hw_types.h>
|
|
|
|
#include <inc/hw_ints.h>
|
|
|
|
#include <inc/hw_memmap.h>
|
|
|
|
|
|
|
|
#include <driverlib/gpio.h>
|
|
|
|
#include <driverlib/interrupt.h>
|
|
|
|
#include <driverlib/pin.h>
|
|
|
|
#include <driverlib/prcm.h>
|
|
|
|
#include <driverlib/rom.h>
|
|
|
|
#include <driverlib/rom_map.h>
|
|
|
|
#include <driverlib/uart.h>
|
|
|
|
#include <driverlib/utils.h>
|
|
|
|
|
|
|
|
#include <example/common/gpio_if.h>
|
|
|
|
#include <example/common/i2c_if.h>
|
|
|
|
|
|
|
|
/* Mongoose.h brings in SimpleLink support. Do not include simplelink.h. */
|
|
|
|
#include <mongoose.h>
|
|
|
|
|
|
|
|
#include <simplelink/include/device.h>
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
#include "data.h"
|
|
|
|
#include "wifi.h"
|
2016-03-24 20:58:26 +08:00
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
/* Set up an AP or connect to existing WiFi network. */
|
2016-04-06 02:44:19 +08:00
|
|
|
#define WIFI_AP_SSID "Mongoose"
|
|
|
|
#define WIFI_AP_PASS ""
|
|
|
|
#define WIFI_AP_CHAN 6
|
|
|
|
// #define WIFI_STA_SSID "YourWiFi"
|
|
|
|
// #define WIFI_STA_PASS "YourPass"
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
#define DATA_COLLECTION_INTERVAL_MS 20
|
2016-03-24 20:58:26 +08:00
|
|
|
|
2016-03-22 03:05:28 +08:00
|
|
|
#define CONSOLE_BAUD_RATE 115200
|
|
|
|
#define CONSOLE_UART UARTA0_BASE
|
|
|
|
#define CONSOLE_UART_PERIPH PRCM_UARTA0
|
2016-04-06 02:44:19 +08:00
|
|
|
#define MG_TASK_PRIORITY 3
|
2016-03-30 23:52:56 +08:00
|
|
|
#define MG_TASK_STACK_SIZE 8192
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-03-24 20:58:26 +08:00
|
|
|
#define BM222_ADDR 0x18
|
2016-03-24 03:17:11 +08:00
|
|
|
#define TMP006_ADDR 0x41
|
|
|
|
|
2016-04-16 00:23:14 +08:00
|
|
|
static const char *upload_form =
|
|
|
|
"\
|
2016-04-15 21:10:15 +08:00
|
|
|
<h1>Upload file</h1> \
|
|
|
|
<form action='/upload' method='POST' enctype='multipart/form-data'> \
|
|
|
|
<input type='file' name='file'> \
|
|
|
|
<input type='submit' value='Upload'> \
|
|
|
|
</form>";
|
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
static struct mg_str upload_fname(struct mg_connection *nc,
|
|
|
|
struct mg_str fname) {
|
2016-04-01 00:27:26 +08:00
|
|
|
struct mg_str lfn;
|
|
|
|
lfn.len = fname.len + 3;
|
|
|
|
lfn.p = malloc(lfn.len);
|
|
|
|
memcpy((char *) lfn.p, "SL:", 3);
|
|
|
|
memcpy((char *) lfn.p + 3, fname.p, fname.len);
|
|
|
|
return lfn;
|
|
|
|
}
|
|
|
|
|
2016-04-06 02:44:19 +08:00
|
|
|
static void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
2016-03-24 03:17:11 +08:00
|
|
|
switch (ev) {
|
|
|
|
case MG_EV_ACCEPT: {
|
|
|
|
char addr[32];
|
2016-04-01 05:02:07 +08:00
|
|
|
mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE |
|
|
|
|
MG_SOCK_STRINGIFY_IP |
|
|
|
|
MG_SOCK_STRINGIFY_PORT);
|
2016-03-24 03:17:11 +08:00
|
|
|
LOG(LL_INFO, ("%p conn from %s", nc, addr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MG_EV_HTTP_REQUEST: {
|
|
|
|
char addr[32];
|
2016-04-06 02:44:19 +08:00
|
|
|
struct http_message *hm = (struct http_message *) ev_data;
|
2016-04-15 21:10:15 +08:00
|
|
|
cs_stat_t st;
|
2016-04-01 05:02:07 +08:00
|
|
|
mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE |
|
|
|
|
MG_SOCK_STRINGIFY_IP |
|
|
|
|
MG_SOCK_STRINGIFY_PORT);
|
2016-03-24 03:17:11 +08:00
|
|
|
LOG(LL_INFO,
|
|
|
|
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
|
|
|
|
hm->method.p, (int) hm->uri.len, hm->uri.p));
|
2016-04-15 21:10:15 +08:00
|
|
|
if (mg_vcmp(&hm->uri, "/upload") == 0 ||
|
2016-04-16 00:23:14 +08:00
|
|
|
(mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) {
|
2016-04-15 21:10:15 +08:00
|
|
|
mg_send(nc, upload_form, strlen(upload_form));
|
|
|
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-24 03:17:11 +08:00
|
|
|
struct mg_serve_http_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2016-03-31 23:49:33 +08:00
|
|
|
opts.document_root = "SL:";
|
2016-04-06 02:44:19 +08:00
|
|
|
mg_serve_http(nc, hm, opts);
|
2016-03-24 03:17:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MG_EV_CLOSE: {
|
|
|
|
LOG(LL_INFO, ("%p closed", nc));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
|
|
|
LOG(LL_INFO, ("%p switching to data mode", nc));
|
2016-04-01 05:02:07 +08:00
|
|
|
nc->handler = data_conn_handler;
|
2016-03-24 20:58:26 +08:00
|
|
|
nc->ev_timer_time = mg_time(); /* Immediately */
|
2016-03-24 03:17:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MG_EV_TIMER: {
|
2016-04-01 05:02:07 +08:00
|
|
|
data_collect();
|
|
|
|
nc->ev_timer_time = mg_time() + (DATA_COLLECTION_INTERVAL_MS * 0.001);
|
2016-03-24 03:17:11 +08:00
|
|
|
}
|
2016-04-01 00:27:26 +08:00
|
|
|
case MG_EV_HTTP_PART_BEGIN:
|
|
|
|
case MG_EV_HTTP_PART_DATA:
|
|
|
|
case MG_EV_HTTP_PART_END: {
|
2016-04-06 02:44:19 +08:00
|
|
|
struct mg_http_multipart_part *mp =
|
|
|
|
(struct mg_http_multipart_part *) ev_data;
|
|
|
|
if (ev == MG_EV_HTTP_PART_BEGIN) {
|
|
|
|
LOG(LL_INFO, ("Begin file upload: %s", mp->file_name));
|
|
|
|
} else if (ev == MG_EV_HTTP_PART_END) {
|
|
|
|
LOG(LL_INFO, ("End file upload: %s", mp->file_name));
|
|
|
|
}
|
|
|
|
mg_file_upload_handler(nc, ev, ev_data, upload_fname);
|
2016-04-01 00:27:26 +08:00
|
|
|
}
|
2016-03-24 03:17:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 02:44:19 +08:00
|
|
|
static void mg_init(struct mg_mgr *mgr) {
|
2016-03-30 23:52:56 +08:00
|
|
|
LOG(LL_INFO, ("MG task running"));
|
2016-04-16 00:23:14 +08:00
|
|
|
|
|
|
|
stop_nwp(); /* See function description in wifi.c */
|
|
|
|
int role = sl_Start(0, 0, 0);
|
|
|
|
if (role < 0) {
|
|
|
|
LOG(LL_ERROR, ("Failed to start NWP"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(LL_INFO, ("NWP started"));
|
2016-03-30 23:52:56 +08:00
|
|
|
GPIO_IF_LedToggle(MCU_RED_LED_GPIO);
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
data_init_sensors(TMP006_ADDR, BM222_ADDR);
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-04-12 01:46:31 +08:00
|
|
|
sl_fs_init();
|
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
#if defined(WIFI_STA_SSID)
|
|
|
|
if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) {
|
|
|
|
LOG(LL_ERROR, ("Error setting up WiFi station"));
|
|
|
|
}
|
|
|
|
#elif defined(WIFI_AP_SSID)
|
|
|
|
if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) {
|
|
|
|
LOG(LL_ERROR, ("Error setting up WiFi AP"));
|
2016-03-22 03:05:28 +08:00
|
|
|
}
|
2016-04-01 05:02:07 +08:00
|
|
|
#else
|
|
|
|
#error WiFi not configured
|
|
|
|
#endif
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-04-01 05:02:07 +08:00
|
|
|
/* We don't need SimpleLink's web server. */
|
2016-03-22 03:05:28 +08:00
|
|
|
sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
|
|
|
|
|
|
|
|
const char *err = "";
|
|
|
|
struct mg_bind_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
opts.error_string = &err;
|
|
|
|
|
2016-04-06 02:44:19 +08:00
|
|
|
struct mg_connection *nc = mg_bind(mgr, "80", mg_ev_handler);
|
2016-03-22 03:05:28 +08:00
|
|
|
if (nc != NULL) {
|
|
|
|
mg_set_protocol_http_websocket(nc);
|
2016-03-24 20:58:26 +08:00
|
|
|
nc->ev_timer_time = mg_time(); /* Start data collection */
|
2016-03-22 03:05:28 +08:00
|
|
|
} else {
|
|
|
|
LOG(LL_ERROR, ("Failed to create listener: %s", err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:52:56 +08:00
|
|
|
#ifndef USE_TIRTOS
|
2016-03-22 03:05:28 +08:00
|
|
|
/* Int vector table, defined in startup_gcc.c */
|
|
|
|
extern void (*const g_pfnVectors[])(void);
|
2016-03-30 23:52:56 +08:00
|
|
|
#endif
|
2016-03-22 03:05:28 +08:00
|
|
|
|
|
|
|
int main() {
|
2016-03-30 23:52:56 +08:00
|
|
|
#ifndef USE_TIRTOS
|
2016-03-22 03:05:28 +08:00
|
|
|
MAP_IntVTableBaseSet((unsigned long) &g_pfnVectors[0]);
|
2016-03-30 23:52:56 +08:00
|
|
|
#endif
|
2016-03-22 03:05:28 +08:00
|
|
|
MAP_IntEnable(FAULT_SYSTICK);
|
|
|
|
MAP_IntMasterEnable();
|
|
|
|
PRCMCC3200MCUInit();
|
|
|
|
|
|
|
|
/* Console UART init. */
|
|
|
|
MAP_PRCMPeripheralClkEnable(CONSOLE_UART_PERIPH, PRCM_RUN_MODE_CLK);
|
|
|
|
MAP_PinTypeUART(PIN_55, PIN_MODE_3); /* PIN_55 -> UART0_TX */
|
|
|
|
MAP_PinTypeUART(PIN_57, PIN_MODE_3); /* PIN_57 -> UART0_RX */
|
|
|
|
MAP_UARTConfigSetExpClk(
|
|
|
|
CONSOLE_UART, MAP_PRCMPeripheralClockGet(CONSOLE_UART_PERIPH),
|
|
|
|
CONSOLE_BAUD_RATE,
|
|
|
|
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
|
|
|
|
MAP_UARTFIFOLevelSet(CONSOLE_UART, UART_FIFO_TX1_8, UART_FIFO_RX4_8);
|
|
|
|
MAP_UARTFIFOEnable(CONSOLE_UART);
|
|
|
|
|
2016-03-30 23:52:56 +08:00
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
2016-03-23 16:16:59 +08:00
|
|
|
cs_log_set_level(LL_INFO);
|
2016-03-30 23:52:56 +08:00
|
|
|
cs_log_set_file(stdout);
|
2016-03-31 23:49:33 +08:00
|
|
|
|
2016-03-30 23:52:56 +08:00
|
|
|
LOG(LL_INFO, ("Hello, world!"));
|
2016-03-22 03:05:28 +08:00
|
|
|
|
2016-03-24 03:17:11 +08:00
|
|
|
MAP_PinTypeI2C(PIN_01, PIN_MODE_1); /* SDA */
|
|
|
|
MAP_PinTypeI2C(PIN_02, PIN_MODE_1); /* SCL */
|
|
|
|
I2C_IF_Open(I2C_MASTER_MODE_FST);
|
|
|
|
|
2016-03-24 20:58:26 +08:00
|
|
|
/* Set up the red LED. Note that amber and green cannot be used as they share
|
|
|
|
* pins with I2C. */
|
|
|
|
MAP_PRCMPeripheralClkEnable(PRCM_GPIOA1, PRCM_RUN_MODE_CLK);
|
|
|
|
MAP_PinTypeGPIO(PIN_64, PIN_MODE_0, false);
|
|
|
|
MAP_GPIODirModeSet(GPIOA1_BASE, 0x2, GPIO_DIR_MODE_OUT);
|
|
|
|
GPIO_IF_LedConfigure(LED1);
|
2016-04-15 21:10:15 +08:00
|
|
|
GPIO_IF_LedOn(MCU_RED_LED_GPIO);
|
2016-03-30 23:52:56 +08:00
|
|
|
|
|
|
|
if (VStartSimpleLinkSpawnTask(8) != 0) {
|
2016-03-31 23:49:33 +08:00
|
|
|
LOG(LL_ERROR, ("Failed to create SL task"));
|
2016-03-30 23:52:56 +08:00
|
|
|
}
|
2016-04-06 02:44:19 +08:00
|
|
|
|
|
|
|
if (!mg_start_task(MG_TASK_PRIORITY, MG_TASK_STACK_SIZE, mg_init)) {
|
2016-03-31 23:49:33 +08:00
|
|
|
LOG(LL_ERROR, ("Failed to create MG task"));
|
2016-03-30 23:52:56 +08:00
|
|
|
}
|
2016-03-24 20:58:26 +08:00
|
|
|
|
2016-03-22 03:05:28 +08:00
|
|
|
osi_start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These are FreeRTOS hooks for various life situations. */
|
|
|
|
void vApplicationMallocFailedHook() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void vApplicationIdleHook() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void vApplicationStackOverflowHook(OsiTaskHandle *th, signed char *tn) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *e,
|
|
|
|
SlHttpServerResponse_t *resp) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleLinkSockEventHandler(SlSockEvent_t *e) {
|
|
|
|
}
|
2016-04-06 15:22:45 +08:00
|
|
|
|
|
|
|
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *e) {
|
2016-04-12 14:40:07 +08:00
|
|
|
LOG(LL_ERROR, ("status %d sender %d", e->EventData.deviceEvent.status,
|
|
|
|
e->EventData.deviceEvent.sender));
|
2016-04-06 15:22:45 +08:00
|
|
|
}
|