Merge remote-tracking branch 'upstream/master'

This commit is contained in:
BruceHsu 2017-10-03 01:35:25 +08:00
commit 56ba21c081
299 changed files with 16208 additions and 13185 deletions

View file

@ -48,7 +48,7 @@ void user_init(void)
};
sdk_wifi_softap_set_config(&ap_config);
ip_addr_t first_client_ip;
ip4_addr_t first_client_ip;
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
dhcpserver_start(&first_client_ip, 4);
@ -65,14 +65,14 @@ static void telnetTask(void *pvParameters)
printf("Status monitor: Failed to allocate socket.\r\n");
return;
}
netconn_bind(nc, IP_ADDR_ANY, TELNET_PORT);
netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT);
netconn_listen(nc);
while(1) {
struct netconn *client = NULL;
err_t err = netconn_accept(nc, &client);
if ( err != ERR_OK ) {
if (err != ERR_OK) {
if(client)
netconn_delete(client);
continue;
@ -88,9 +88,8 @@ static void telnetTask(void *pvParameters)
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int)xPortGetFreeHeapSize());
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
snprintf(buf, sizeof(buf), "Your address is %d.%d.%d.%d\r\n\r\n",
ip4_addr1(&client_addr), ip4_addr2(&client_addr),
ip4_addr3(&client_addr), ip4_addr4(&client_addr));
char abuf[40];
snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf)));
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
netconn_delete(client);
}

4
examples/ad770x/Makefile Normal file
View file

@ -0,0 +1,4 @@
PROGRAM = ad770x
EXTRA_COMPONENTS = extras/ad770x
#ESPBAUD = 460800
include ../../common.mk

49
examples/ad770x/main.c Normal file
View file

@ -0,0 +1,49 @@
/*
* Example of using AD7705/AD7706 driver
*
* Part of esp-open-rtos
* Copyright (C) 2017 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#include <esp/uart.h>
#include <espressif/esp_common.h>
#include <stdio.h>
#include <ad770x/ad770x.h>
#include <FreeRTOS.h>
#include <task.h>
#define CS_PIN 2
#define AIN_CHANNEL 0 // AIN1+,AIN1- for AD7705
static const ad770x_params_t dev = {
.cs_pin = CS_PIN,
.master_clock = AD770X_MCLK_4_9152MHz, // 4.9152 MHz
.bipolar = false, // Unipolar mode
.gain = AD770X_GAIN_1, // No gain
.update_rate = AD770X_RATE_50 // 50 Hz output update rate
};
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
while (ad770x_init(&dev, AIN_CHANNEL) != 0)
{
printf("Cannot initialize AD7705\n");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
while (true)
{
// wait for data
while (!ad770x_data_ready(&dev, AIN_CHANNEL)) {}
// Read result
uint16_t raw = ad770x_raw_adc_value(&dev, AIN_CHANNEL);
printf("Raw ADC value: %d\n", raw);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}

View file

@ -17,6 +17,7 @@
// Connect ADDR pin to GND
#define ADDR ADS111X_ADDR_GND
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -28,23 +29,27 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
ads111x_set_mode(ADDR, ADS111X_MODE_CONTUNOUS);
ads111x_set_data_rate(ADDR, ADS111X_DATA_RATE_32);
ads111x_set_mode(&dev, ADS111X_MODE_CONTUNOUS);
ads111x_set_data_rate(&dev, ADS111X_DATA_RATE_32);
ads111x_set_input_mux(ADDR, ADS111X_MUX_0_GND);
ads111x_set_gain(ADDR, GAIN);
ads111x_set_input_mux(&dev, ADS111X_MUX_0_GND);
ads111x_set_gain(&dev, GAIN);
float gain_val = ads111x_gain_values[GAIN];
while (true)
{
// wait for conversion end
while (ads111x_busy(ADDR)) {}
while (ads111x_busy(&dev)) {}
// Read result
int16_t raw = ads111x_get_value(ADDR);
int16_t raw = ads111x_get_value(&dev);
float voltage = gain_val / ADS111X_MAX_VALUE * raw;

View file

@ -4,7 +4,7 @@
// this must be ahead of any mbedtls header files so the local mbedtls/config.h can be properly referenced
#include "mbedtls/config.h"
#include "mbedtls/net.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"

3
examples/bh1750/Makefile Normal file
View file

@ -0,0 +1,3 @@
PROGRAM=bh1750_example
EXTRA_COMPONENTS = extras/i2c extras/bh1750
include ../../common.mk

View file

@ -0,0 +1,44 @@
#include <stdio.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "i2c/i2c.h"
#include "bh1750/bh1750.h"
#define SCL_PIN 5
#define SDA_PIN 4
#define I2C_BUS 0
static void measure(void *pvParameters)
{
i2c_dev_t dev = {
.addr = BH1750_ADDR_LO,
.bus = I2C_BUS,
};
bh1750_configure(&dev, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE);
while (1) {
while(1) {
vTaskDelay(200 / portTICK_PERIOD_MS);
printf("Lux: %d\n", bh1750_read(&dev));
}
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
// Just some information
printf("\n");
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL);
}

View file

@ -17,13 +17,13 @@ const int freq_frc2 = 10;
static volatile uint32_t frc1_count;
static volatile uint32_t frc2_count;
void frc1_interrupt_handler(void)
void frc1_interrupt_handler(void *arg)
{
frc1_count++;
gpio_toggle(gpio_frc1);
}
void frc2_interrupt_handler(void)
void frc2_interrupt_handler(void *arg)
{
/* FRC2 needs the match register updated on each timer interrupt */
timer_set_frequency(FRC2, freq_frc2);
@ -47,8 +47,8 @@ void user_init(void)
timer_set_run(FRC2, false);
/* set up ISRs */
_xt_isr_attach(INUM_TIMER_FRC1, frc1_interrupt_handler);
_xt_isr_attach(INUM_TIMER_FRC2, frc2_interrupt_handler);
_xt_isr_attach(INUM_TIMER_FRC1, frc1_interrupt_handler, NULL);
_xt_isr_attach(INUM_TIMER_FRC2, frc2_interrupt_handler, NULL);
/* configure timer frequencies */
timer_set_frequency(FRC1, freq_frc1);

View file

@ -13,18 +13,25 @@
// BMP180 driver
#include "bmp180/bmp180.h"
#define MY_EVT_TIMER 0x01
#define MY_EVT_BMP180 0x02
#define I2C_BUS 0
#define SCL_PIN GPIO_ID_PIN((0))
#define SDA_PIN GPIO_ID_PIN((2))
#define MY_EVT_TIMER 0x01
#define MY_EVT_BMP180 0x02
typedef struct
{
uint8_t event_type;
bmp180_result_t bmp180_data;
} my_event_t;
//device descriptor
i2c_dev_t dev = {
.addr = BMP180_DEVICE_ADDRESS,
.bus = I2C_BUS,
};
// Communication Queue
static QueueHandle_t mainqueue;
static TimerHandle_t timerHandle;
@ -70,7 +77,7 @@ void bmp180_task(void *pvParameters)
case MY_EVT_TIMER:
printf("%s: Received Timer Event\n", __FUNCTION__);
bmp180_trigger_measurement(com_queue);
bmp180_trigger_measurement(&dev, com_queue);
break;
case MY_EVT_BMP180:
printf("%s: Received BMP180 Event temp:=%d.%dC press=%d.%02dhPa\n", __FUNCTION__, \
@ -91,6 +98,9 @@ void user_setup(void)
// Give the UART some time to settle
sdk_os_delay_us(500);
// Init I2C bus Interface
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
}
void user_init(void)
@ -107,7 +117,7 @@ void user_init(void)
bmp180_informUser = bmp180_i2c_informUser;
// Init BMP180 Interface
bmp180_init(SCL_PIN, SDA_PIN);
bmp180_init(&dev);
// Create Main Communication Queue
mainqueue = xQueueCreate(10, sizeof(my_event_t));

View file

@ -10,9 +10,9 @@
#include "bmp280/bmp280.h"
// In forced mode user initiate measurement each time.
// In normal mode measurement is done continuously with specified standby time.
// In normal mode measurement is done continuously with specified standby time.
// #define MODE_FORCED
const uint8_t i2c_bus = 0;
const uint8_t scl_pin = 0;
const uint8_t sda_pin = 2;
@ -26,7 +26,8 @@ static void bmp280_task_forced(void *pvParameters)
params.mode = BMP280_MODE_FORCED;
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
bmp280_dev.i2c_dev.bus = i2c_bus;
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
@ -67,7 +68,8 @@ static void bmp280_task_normal(void *pvParameters)
bmp280_init_default_params(&params);
bmp280_t bmp280_dev;
bmp280_dev.i2c_addr = BMP280_I2C_ADDRESS_0;
bmp280_dev.i2c_dev.bus = i2c_bus;
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
while (1) {
while (!bmp280_init(&bmp280_dev, &params)) {
@ -103,7 +105,7 @@ void user_init(void)
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(scl_pin, sda_pin);
i2c_init(i2c_bus, scl_pin, sda_pin, I2C_FREQ_400K);
#ifdef MODE_FORCED
xTaskCreate(bmp280_task_forced, "bmp280_task", 256, NULL, 2, NULL);

View file

@ -11,6 +11,7 @@
#include <ds1307/ds1307.h>
#include <stdio.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -19,8 +20,12 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
ds1307_start(true);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_start(&dev, true);
// setup datetime: 2016-10-09 13:50:10
struct tm time = {
@ -31,11 +36,11 @@ void user_init(void)
.tm_min = 50,
.tm_sec = 10
};
ds1307_set_time(&time);
ds1307_set_time(&dev, &time);
while (true)
{
ds1307_get_time(&time);
ds1307_get_time(&dev, &time);
printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon + 1,
time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);

View file

@ -1,4 +1,4 @@
/* Test code for DS3231 high precision RTC module
/* Test code for DS3231 high precision RTC module
*
* Part of esp-open-rtos
* Copyright (C) 2016 Bhuvanchandra DV <bhuvanchandra.dv@gmail.com>
@ -12,15 +12,21 @@
#include "ds3231/ds3231.h"
#define ADDR DS3231_ADDR
#define I2C_BUS 0
void task1(void *pvParameters)
{
struct tm time;
float tempFloat;
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
while(1) {
vTaskDelay(100);
ds3231_getTime(&time);
ds3231_getTempFloat(&tempFloat);
ds3231_getTime(&dev, &time);
ds3231_getTempFloat(&dev, &tempFloat);
printf("TIME:%d:%d:%d, TEMPERATURE:%.2f DegC\r\n", time.tm_hour, time.tm_min, time.tm_sec, tempFloat);
}
}
@ -35,7 +41,7 @@ void user_init(void)
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
ds3231_Init(scl, sda);
i2c_init(0,scl,sda,I2C_FREQ_400K);
xTaskCreate(task1, "tsk1", 256, NULL, 2, NULL);
}

View file

@ -97,7 +97,7 @@ void timerRegTask(void *pvParameters)
}
}
IRAM void frc1_handler(void)
IRAM void frc1_handler(void *arg)
{
frc1_handler_call_count++;
frc1_last_count_val = TIMER(0).COUNT;
@ -106,7 +106,7 @@ IRAM void frc1_handler(void)
//TIMER_FRC1_MATCH_REG = frc1_last_count_val + 0x100000;
}
void frc2_handler(void)
void frc2_handler(void *arg)
{
frc2_handler_call_count++;
frc2_last_count_val = TIMER(1).COUNT;
@ -127,9 +127,9 @@ void user_init(void)
TIMER(1).LOAD = VAL2FIELD(TIMER_CTRL_CLKDIV, TIMER_CLKDIV_256);
DPORT.INT_ENABLE |= DPORT_INT_ENABLE_TIMER0 | DPORT_INT_ENABLE_TIMER1;
_xt_isr_attach(INUM_TIMER_FRC1, frc1_handler);
_xt_isr_attach(INUM_TIMER_FRC1, frc1_handler, NULL);
_xt_isr_unmask(1<<INUM_TIMER_FRC1);
_xt_isr_attach(INUM_TIMER_FRC2, frc2_handler);
_xt_isr_attach(INUM_TIMER_FRC2, frc2_handler, NULL);
_xt_isr_unmask(1<<INUM_TIMER_FRC2);
TIMER(0).CTRL |= TIMER_CTRL_RUN;

View file

@ -237,7 +237,7 @@ static volatile bool frc1_ran;
static volatile bool frc1_finished;
static volatile char frc1_buf[80];
static void frc1_interrupt_handler(void)
static void frc1_interrupt_handler(void *arg)
{
frc1_ran = true;
timer_set_run(FRC1, false);
@ -250,7 +250,7 @@ static void test_isr()
printf("Testing behaviour inside ISRs...\r\n");
timer_set_interrupts(FRC1, false);
timer_set_run(FRC1, false);
_xt_isr_attach(INUM_TIMER_FRC1, frc1_interrupt_handler);
_xt_isr_attach(INUM_TIMER_FRC1, frc1_interrupt_handler, NULL);
timer_set_frequency(FRC1, 1000);
timer_set_interrupts(FRC1, true);
timer_set_run(FRC1, true);

View file

@ -18,6 +18,7 @@
#define CS_GPIO_PIN 2
// ds1307
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -29,7 +30,11 @@
uint32_t get_fattime()
{
struct tm time;
ds1307_get_time(&time);
i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_get_time(&dev, &time);
return ((uint32_t)(time.tm_year - 1980) << 25)
| ((uint32_t)time.tm_mon << 21)
@ -127,7 +132,7 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
i2c_init (SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
xTaskCreate(rewrite_file_task, "task1", 512, NULL, 2, NULL);
}

View file

@ -11,6 +11,7 @@
#include <i2c/i2c.h>
#include <hmc5883l/hmc5883l.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -19,20 +20,24 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = HMC5883L_ADDR,
.bus = I2C_BUS,
};
while (!hmc5883l_init())
while (!hmc5883l_init(&dev))
printf("Device not found\n");
hmc5883l_set_operating_mode(HMC5883L_MODE_CONTINUOUS);
hmc5883l_set_samples_averaged(HMC5883L_SAMPLES_8);
hmc5883l_set_data_rate(HMC5883L_DATA_RATE_07_50);
hmc5883l_set_gain(HMC5883L_GAIN_1090);
hmc5883l_set_operating_mode(&dev, HMC5883L_MODE_CONTINUOUS);
hmc5883l_set_samples_averaged(&dev, HMC5883L_SAMPLES_8);
hmc5883l_set_data_rate(&dev, HMC5883L_DATA_RATE_07_50);
hmc5883l_set_gain(&dev, HMC5883L_GAIN_1090);
while (true)
{
hmc5883l_data_t data;
hmc5883l_get_data(&data);
hmc5883l_get_data(&dev, &data);
printf("Magnetic data: X:%.2f mG, Y:%.2f mG, Z:%.2f mG\n", data.x, data.y, data.z);
for (uint32_t i = 0; i < 1000; i++)

View file

@ -20,9 +20,9 @@
#include "ssid_config.h"
#define WEB_SERVER "chainxor.org"
#define WEB_SERVER "ipv6.google.com"
#define WEB_PORT 80
#define WEB_URL "http://chainxor.org/"
#define WEB_PATH "/"
void http_get_task(void *pvParameters)
{
@ -31,7 +31,7 @@ void http_get_task(void *pvParameters)
while(1) {
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
@ -39,7 +39,7 @@ void http_get_task(void *pvParameters)
printf("Running DNS lookup for %s...\r\n", WEB_SERVER);
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);
if(err != 0 || res == NULL) {
if (err != 0 || res == NULL) {
printf("DNS lookup failed err=%d res=%p\r\n", err, res);
if(res)
freeaddrinfo(res);
@ -47,9 +47,28 @@ void http_get_task(void *pvParameters)
failures++;
continue;
}
/* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr));
#if LWIP_IPV6
{
struct netif *netif = sdk_system_get_netif(0);
int i;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
printf(" ip6 %d state %x\n", i, netif_ip6_addr_state(netif, i));
if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)))
printf(" ip6 addr %d = %s\n", i, ip6addr_ntoa(netif_ip6_addr(netif, i)));
}
}
#endif
struct sockaddr *sa = res->ai_addr;
if (sa->sa_family == AF_INET) {
printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr));
}
#if LWIP_IPV6
if (sa->sa_family == AF_INET6) {
printf("DNS lookup succeeded. IP=%s\r\n", inet6_ntoa(((struct sockaddr_in6 *)sa)->sin6_addr));
}
#endif
int s = socket(res->ai_family, res->ai_socktype, 0);
if(s < 0) {
@ -75,8 +94,10 @@ void http_get_task(void *pvParameters)
freeaddrinfo(res);
const char *req =
"GET "WEB_URL"\r\n"
"GET "WEB_PATH" HTTP/1.1\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-open-rtos/0.1 esp8266\r\n"
"Connection: close\r\n"
"\r\n";
if (write(s, req, strlen(req)) < 0) {
printf("... socket send failed\r\n");
@ -126,6 +147,6 @@ void user_init(void)
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(&http_get_task, "get_task", 256, NULL, 2, NULL);
xTaskCreate(&http_get_task, "get_task", 384, NULL, 2, NULL);
}

View file

@ -33,7 +33,7 @@
errors at link time if functions don't exist.) */
#include "mbedtls/config.h"
#include "mbedtls/net.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"

View file

@ -19,7 +19,7 @@
<h1>About</h1>
<p>This server is based on httpd from LwIP.</p>
<p>To enable debugging compile with flags -DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON.</p>
<p>For more info see <a href="http://www.nongnu.org/lwip/2_0_0/group__httpd.html">HTTP Server documentation</a>.</p>
<p>For more info see <a href="http://www.nongnu.org/lwip/2_0_x/group__httpd.html">HTTP Server documentation</a>.</p>
</div>
</body>
</html>

View file

@ -13,6 +13,7 @@
#include <hd44780/hd44780.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
#define ADDR 0x27
@ -27,10 +28,11 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
hd44780_t lcd = {
.addr = ADDR,
.i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
.font = HD44780_FONT_5X8,
.lines = 2,
.pins = {

View file

@ -88,7 +88,7 @@ static inline void init_descriptors_list()
}
// DMA interrupt handler. It is called each time a DMA block is finished processing.
static void dma_isr_handler(void)
static void dma_isr_handler(void *args)
{
portBASE_TYPE task_awoken = pdFALSE;
@ -168,7 +168,7 @@ void play_task(void *pvParameters)
i2s_pins_t i2s_pins = {.data = true, .clock = true, .ws = true};
i2s_dma_init(dma_isr_handler, clock_div, i2s_pins);
i2s_dma_init(dma_isr_handler, NULL, clock_div, i2s_pins);
while (1) {
init_descriptors_list();

View file

@ -13,6 +13,7 @@
#include <stdbool.h>
#include "ina3221/ina3221.h"
#define I2C_BUS 0
#define PIN_SCL 5
#define PIN_SDA 2
#define ADDR INA3221_ADDR_0
@ -33,7 +34,8 @@ void ina_measure(void *pvParameters)
// Create ina3221 device
ina3221_t dev = {
.addr = ADDR,
.i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
.shunt = { 100 ,100 ,100 }, // shunt values are 100 mOhm for each channel
.mask.mask_register = INA3221_DEFAULT_MASK, // Init
.config.config_register = INA3221_DEFAULT_CONFIG, // Init
@ -120,7 +122,7 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(PIN_SCL,PIN_SDA);
i2c_init(I2C_BUS, PIN_SCL, PIN_SDA, I2C_FREQ_400K);
xTaskCreate(ina_measure, "Measurements_task", 512, NULL, 2, NULL);
}

View file

@ -14,14 +14,15 @@
#include <FreeRTOS.h>
#include <task.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
#define ADDR MCP4725A0_ADDR0
#define VDD 3.3
inline static void wait_for_eeprom()
inline static void wait_for_eeprom(i2c_dev_t* dev)
{
while (mcp4725_eeprom_busy(ADDR))
while (mcp4725_eeprom_busy(dev))
{
printf("...DAC is busy, waiting...\n");
vTaskDelay(1);
@ -33,21 +34,25 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
// setup EEPROM values
if (mcp4725_get_power_mode(ADDR, true) != MCP4725_PM_NORMAL)
if (mcp4725_get_power_mode(&dev, true) != MCP4725_PM_NORMAL)
{
printf("DAC was sleeping... Wake up Neo!\n");
mcp4725_set_power_mode(ADDR, MCP4725_PM_NORMAL, true);
wait_for_eeprom();
mcp4725_set_power_mode(&dev, MCP4725_PM_NORMAL, true);
wait_for_eeprom(&dev);
}
printf("Set default DAC ouptut value to MAX...\n");
mcp4725_set_raw_output(ADDR, MCP4725_MAX_VALUE, true);
wait_for_eeprom();
mcp4725_set_raw_output(&dev, MCP4725_MAX_VALUE, true);
wait_for_eeprom(&dev);
printf("And now default DAC output value is 0x%03x\n", mcp4725_get_raw_output(ADDR, true));
printf("And now default DAC output value is 0x%03x\n", mcp4725_get_raw_output(&dev, true));
printf("Now let's generate the sawtooth wave in slow manner\n");
@ -58,7 +63,7 @@ void user_init(void)
if (vout > VDD) vout = 0;
printf("Vout: %.02f\n", vout);
mcp4725_set_voltage(ADDR, VDD, vout, false);
mcp4725_set_voltage(&dev, VDD, vout, false);
// It will be very low freq wave
vTaskDelay(100 / portTICK_PERIOD_MS);

View file

@ -13,18 +13,20 @@
#include <i2c/i2c.h>
#include <ms561101ba03/ms561101ba03.h>
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
void user_init(void)
{
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
ms561101ba03_t device = {
.addr = MS561101BA03_ADDR_CSB_LOW,
.i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = MS561101BA03_ADDR_CSB_LOW,
.osr = MS561101BA03_OSR_4096,
};

View file

@ -11,8 +11,9 @@
#include <pca9685/pca9685.h>
#include <stdio.h>
#define ADDR 0x40
#define ADDR PCA9685_ADDR_BASE
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
@ -23,19 +24,23 @@ void user_init(void)
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
pca9685_init(ADDR);
pca9685_init(&dev);
pca9685_set_pwm_frequency(ADDR, 1000);
printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(ADDR));
pca9685_set_pwm_frequency(&dev, 1000);
printf("Freq 1000Hz, real %d\n", pca9685_get_pwm_frequency(&dev));
uint16_t val = 0;
while (true)
{
printf("Set ch0 to %d, ch4 to %d\n", val, 4096 - val);
pca9685_set_pwm_value(ADDR, 0, val);
pca9685_set_pwm_value(ADDR, 4, 4096 - val);
pca9685_set_pwm_value(&dev, 0, val);
pca9685_set_pwm_value(&dev, 4, 4096 - val);
if (val++ == 4096)
val = 0;

View file

@ -0,0 +1,3 @@
PROGRAM = pcf8591
EXTRA_COMPONENTS = extras/i2c extras/pcf8591
include ../../common.mk

42
examples/pcf8591/main.c Normal file
View file

@ -0,0 +1,42 @@
#include <stdio.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "i2c/i2c.h"
#include "pcf8591/pcf8591.h"
#define ADDR PCF8591_DEFAULT_ADDRESS
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
static void measure(void *pvParameters)
{
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
while (1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("Value: %d\n", pcf8591_read(&dev, 0x03));
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
// Just some information
printf("\n");
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL);
}

23
examples/softuart/LICENSE Normal file
View file

@ -0,0 +1,23 @@
The MIT License (MIT)
Copyright (C) 2016 Bernhard Guillon <Bernhard.Guillon@web.de>
Copyright (c) 2015 plieningerweb
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,4 @@
PROGRAM = softuart
EXTRA_COMPONENTS = extras/softuart
ESPBAUD = 460800
include ../../common.mk

40
examples/softuart/main.c Normal file
View file

@ -0,0 +1,40 @@
/*
* Softuart example
*
* Copyright (C) 2017 Ruslan V. Uss <unclerus@gmail.com>
* Copyright (C) 2016 Bernhard Guillon <Bernhard.Guillon@web.de>
* Copyright (c) 2015 plieningerweb
*
* MIT Licensed as described in the file LICENSE
*/
#include <esp/gpio.h>
#include <esp/uart.h>
#include <espressif/esp_common.h>
#include <stdio.h>
//#include <FreeRTOS.h>
//#include <task.h>
#include <softuart/softuart.h>
#define RX_PIN 5
#define TX_PIN 4
void user_init(void)
{
// setup real UART for now
uart_set_baud(0, 115200);
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
// setup software uart to 9600 8n1
softuart_open(0, 9600, RX_PIN, TX_PIN);
while (true)
{
if (!softuart_available(0))
continue;
char c = softuart_read(0);
printf("input: %c, 0x%02x\n", c, c);
softuart_puts(0, "start\r\n");
}
}

View file

@ -23,6 +23,7 @@
#ifdef I2C_CONNECTION
#define PROTOCOL SSD1306_PROTO_I2C
#define ADDR SSD1306_I2C_ADDR_0
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
#else
@ -35,7 +36,8 @@
static const ssd1306_t dev = {
.protocol = PROTOCOL,
#ifdef I2C_CONNECTION
.addr = ADDR,
.i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
#else
.cs_pin = CS_PIN,
.dc_pin = DC_PIN,
@ -97,7 +99,7 @@ void user_init(void)
printf("SDK version:%s\n", sdk_system_get_sdk_version());
#ifdef I2C_CONNECTION
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
#endif
while (ssd1306_init(&dev) != 0)

View file

@ -29,6 +29,7 @@
#ifdef I2C_CONNECTION
#define PROTOCOL SSD1306_PROTO_I2C
#define ADDR SSD1306_I2C_ADDR_0
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
#else
@ -43,7 +44,8 @@
static const ssd1306_t dev = {
.protocol = PROTOCOL,
#ifdef I2C_CONNECTION
.addr = ADDR,
.i2c_dev.bus = I2C_BUS,
.i2c_dev.addr = ADDR,
#else
.cs_pin = CS_PIN,
.dc_pin = DC_PIN,
@ -162,7 +164,7 @@ void user_init(void)
printf("SDK version:%s\n", sdk_system_get_sdk_version());
#ifdef I2C_CONNECTION
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
#endif
while (ssd1306_init(&dev) != 0) {

View file

@ -8,7 +8,7 @@ include ../../common.mk
# `make dump-flash` can be used to view the current contents of the sysparam
# regions in flash.
dump-flash:
esptool.py read_flash 0x1f8000 8192 r1.bin
esptool.py read_flash 0x1f7000 8192 r1.bin
hexdump -C r1.bin
esptool.py read_flash 0x1fa000 8192 r2.bin
esptool.py read_flash 0x1f9000 8192 r2.bin
hexdump -C r2.bin

View file

@ -45,7 +45,7 @@ size_t tty_readline(char *buffer, size_t buf_size, bool echo) {
while (true) {
c = getchar();
if (c == '\r') {
if (c == '\r' || c == '\n') {
if (echo) putchar('\n');
break;
} else if (c == '\b' || c == 0x7f) {
@ -173,7 +173,7 @@ void sysparam_editor_task(void *pvParameters) {
// stuff, so if the user uses this utility to reformat it, it will put
// it somewhere the system will find it later
num_sectors = DEFAULT_SYSPARAM_SECTORS;
base_addr = sdk_flashchip.chip_size - (4 + num_sectors) * sdk_flashchip.sector_size;
base_addr = sdk_flashchip.chip_size - (5 + num_sectors) * sdk_flashchip.sector_size;
}
while (true) {
printf("==> ");
@ -246,5 +246,7 @@ void user_init(void)
{
uart_set_baud(0, 115200);
sdk_wifi_set_opmode(NULL_MODE);
xTaskCreate(sysparam_editor_task, "sysparam_editor_task", 512, NULL, 2, NULL);
}

View file

@ -0,0 +1,10 @@
/**
Configuration overrides for FreeRTOS.
**/
#define configUSE_IDLE_HOOK 1
#define configUSE_TICK_HOOK 1
#include_next "FreeRTOSConfig.h"

View file

@ -0,0 +1,3 @@
# Simple makefile for simple example
PROGRAM=simple
include ../../common.mk

View file

@ -0,0 +1,56 @@
/* Very basic example that just demonstrates we can run at all!
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
void vApplicationIdleHook(void)
{
// Go to sleep; either deeply (waiting for an event to wake)
// or light, allowing FreeRTOS ticks to wake
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
}
void vApplicationTickHook(void)
{
// Called every tick
}
void task1(void *pvParameters)
{
QueueHandle_t *queue = (QueueHandle_t *)pvParameters;
printf("Hello from task1!\r\n");
uint32_t count = 0;
while(1) {
vTaskDelay(100);
xQueueSend(*queue, &count, 0);
count++;
}
}
void task2(void *pvParameters)
{
printf("Hello from task 2!\r\n");
QueueHandle_t *queue = (QueueHandle_t *)pvParameters;
while(1) {
uint32_t count;
if(xQueueReceive(*queue, &count, 1000)) {
printf("Got %u\n", count);
} else {
printf("No msg :(\n");
}
}
}
static QueueHandle_t mainqueue;
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
mainqueue = xQueueCreate(10, sizeof(uint32_t));
xTaskCreate(task1, "tsk1", 256, &mainqueue, 2, NULL);
xTaskCreate(task2, "tsk2", 256, &mainqueue, 2, NULL);
}

View file

@ -43,7 +43,7 @@ extern const char *server_key;
errors at link time if functions don't exist.) */
#include "mbedtls/config.h"
#include "mbedtls/net.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
@ -202,9 +202,8 @@ void tls_server_task(void *pvParameters)
socklen_t peer_addr_len = sizeof(struct sockaddr_in);
getpeername(client_ctx.fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
unsigned char buf[256];
int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\nFree heap size is %d bytes\n",
ip4_addr1(&peer_addr.sin_addr), ip4_addr2(&peer_addr.sin_addr),
ip4_addr3(&peer_addr.sin_addr), ip4_addr4(&peer_addr.sin_addr),
int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\nFree heap size is %d bytes\n",
IP2STR((ip4_addr_t *)&peer_addr.sin_addr.s_addr),
peer_addr.sin_port, xPortGetFreeHeapSize());
while((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0)
{
@ -216,6 +215,7 @@ void tls_server_task(void *pvParameters)
}
len = ret;
ret = 0;
printf(" %d bytes written. Closing socket on client.\n\n%s", len, (char *) buf);
mbedtls_ssl_close_notify(&ssl);

View file

@ -146,9 +146,8 @@ void tls_server_task(void *pvParameters)
/* Prepare a message to the client */
unsigned char buf[100];
int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\r\nFree heap size is %d bytes\r\n",
ip4_addr1(&sa.sin_addr), ip4_addr2(&sa.sin_addr),
ip4_addr3(&sa.sin_addr), ip4_addr4(&sa.sin_addr),
int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\r\nFree heap size is %d bytes\r\n",
IP2STR((ip4_addr_t *)&sa.sin_addr.s_addr),
ntohs(sa.sin_port), xPortGetFreeHeapSize());
/* Send the message and close the connection */

View file

@ -16,6 +16,7 @@
* Connect 3.3v from the ESP to Vin and GND to GND
*/
#define I2C_BUS (0)
#define SCL_PIN (2)
#define SDA_PIN (0)
@ -27,7 +28,8 @@ void tsl2561MeasurementTask(void *pvParameters)
// TSL2561_I2C_ADDR_VCC (0x49)
// TSL2561_I2C_ADDR_GND (0x29)
// TSL2561_I2C_ADDR_FLOAT (0x39) Default
lightSensor.i2c_addr = TSL2561_I2C_ADDR_FLOAT;
lightSensor.i2c_dev.bus = I2C_BUS;
lightSensor.i2c_dev.addr = TSL2561_I2C_ADDR_FLOAT;
tsl2561_init(&lightSensor);
@ -63,7 +65,7 @@ void tsl2561MeasurementTask(void *pvParameters)
void user_init(void)
{
uart_set_baud(0, 115200);
i2c_init(SCL_PIN, SDA_PIN);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(tsl2561MeasurementTask, "tsl2561MeasurementTask", 256, NULL, 2, NULL);
}

View file

@ -0,0 +1,3 @@
PROGRAM=tsl4531_example
EXTRA_COMPONENTS = extras/i2c extras/tsl4531
include ../../common.mk

View file

@ -0,0 +1,57 @@
/*
* This sample code is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "i2c/i2c.h"
#include "task.h"
#include "tsl4531/tsl4531.h"
/* An example using the TSL4531 light sensor
* to read and print lux values from a sensor
* attached to GPIO pin 2 (SCL) and GPIO pin 0 (SDA)
* Connect 3.3v from the ESP to Vin and GND to GND
*/
#define I2C_BUS (0)
#define SCL_PIN (2)
#define SDA_PIN (0)
void tsl4531MeasurementTask(void *pvParameters)
{
tsl4531_t lightSensor;
lightSensor.i2c_dev.bus= I2C_BUS;
lightSensor.i2c_dev.addr= TSL4531_I2C_ADDR;
tsl4531_init(&lightSensor);
tsl4531_set_integration_time(&lightSensor, TSL4531_INTEGRATION_400MS);
tsl4531_set_power_save_skip(&lightSensor, true);
uint16_t lux = 0;
while (1)
{
if (tsl4531_read_lux(&lightSensor, &lux))
{
printf("Lux: %u\n", lux);
}
else
{
printf("Could not read data from TSL4531\n");
}
// 0.05 second delay
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
xTaskCreate(tsl4531MeasurementTask, "tsl4531MeasurementTask", 256, NULL, 2, NULL);
}

5
examples/upnp/Makefile Normal file
View file

@ -0,0 +1,5 @@
PROGRAM=upnp_test
OTA=1
EXTRA_COMPONENTS=extras/rboot-ota
include ../../common.mk

3
examples/upnp/README.md Normal file
View file

@ -0,0 +1,3 @@
# upnp Example
This is an example to generate an upnp server and emulate a WeMo switch recognizable by Amazon echo Dot.

53
examples/upnp/httpd.c Normal file
View file

@ -0,0 +1,53 @@
#include <lwip/api.h>
#include <string.h>
#include <espressif/esp_common.h>
void httpd_task(void *pvParameters)
{
struct netconn *client = NULL;
struct netconn *nc = netconn_new(NETCONN_TCP);
if (nc == NULL) {
printf("Failed to allocate socket\n");
vTaskDelete(NULL);
}
netconn_bind(nc, IP_ADDR_ANY, 80);
netconn_listen(nc);
while (1) {
err_t err = netconn_accept(nc, &client);
if (err == ERR_OK) {
struct netbuf *nb;
if ((err = netconn_recv(client, &nb)) == ERR_OK) {
struct sdk_station_config config;
sdk_wifi_station_get_config(&config);
char * buf =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<root>\
<device>\
<deviceType>urn:Belkin:device:controllee:1</deviceType>\
<friendlyName>hello</friendlyName>\
<manufacturer>Belkin International Inc.</manufacturer>\
<modelName>Emulated Socket</modelName>\
<modelNumber>3.1415</modelNumber>\
<UDN>uuid:Socket-1_0-38323636-4558-4dda-9188-cda0e6cc3dc0</UDN>\
<serialNumber>221517K0101769</serialNumber>\
<binaryState>0</binaryState>\
<serviceList>\
<service>\
<serviceType>urn:Belkin:service:basicevent:1</serviceType>\
<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>\
<controlURL>/upnp/control/basicevent1</controlURL>\
<eventSubURL>/upnp/event/basicevent1</eventSubURL>\
<SCPDURL>/eventservice.xml</SCPDURL>\
</service>\
</serviceList>\
</device>\
</root>";
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
}
netbuf_delete(nb);
}
printf("Closing connection\n");
netconn_close(client);
netconn_delete(client);
}
}

3
examples/upnp/httpd.h Normal file
View file

@ -0,0 +1,3 @@
#include <lwip/api.h>
void httpd_task(void *pvParameters);

5
examples/upnp/lwipopts.h Normal file
View file

@ -0,0 +1,5 @@
#define LWIP_IGMP 1
/* Use the defaults for everything else */
#include_next <lwipopts.h>

135
examples/upnp/upnp.c Normal file
View file

@ -0,0 +1,135 @@
#include <string.h>
#include <lwip/udp.h>
#include <lwip/igmp.h>
#include <lwip/ip_addr.h>
#include <espressif/esp_common.h>
#include "upnp.h"
#define UPNP_MCAST_GRP ("239.255.255.250")
#define UPNP_MCAST_PORT (1900)
static const char* get_my_ip(void)
{
static char ip[16] = "0.0.0.0";
ip[0] = 0;
struct ip_info ipinfo;
(void) sdk_wifi_get_ip_info(STATION_IF, &ipinfo);
snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ipinfo.ip));
return (char*) ip;
}
/**
* @brief This function joins a multicast group with the specified ip/port
* @param group_ip the specified multicast group ip
* @param group_port the specified multicast port number
* @param recv the lwip UDP callback
* @retval udp_pcb* or NULL if joining failed
*/
static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port))
{
bool status = false;
struct udp_pcb *upcb;
printf("Joining mcast group %s:%d\n", group_ip, group_port);
do {
upcb = udp_new();
if (!upcb) {
printf("Error, udp_new failed");
break;
}
udp_bind(upcb, IP4_ADDR_ANY, group_port);
struct netif* netif = sdk_system_get_netif(STATION_IF);
if (!netif) {
printf("Error, netif is null");
break;
}
if (!(netif->flags & NETIF_FLAG_IGMP)) {
netif->flags |= NETIF_FLAG_IGMP;
igmp_start(netif);
}
ip4_addr_t ipgroup;
ip4addr_aton(group_ip, &ipgroup);
err_t err = igmp_joingroup_netif(netif, &ipgroup);
if (ERR_OK != err) {
printf("Failed to join multicast group: %d", err);
break;
}
status = true;
} while(0);
if (status) {
printf("Join successs\n");
udp_recv(upcb, recv, upcb);
} else {
if (upcb) {
udp_remove(upcb);
}
upcb = NULL;
}
return upcb;
}
static void send_udp(struct udp_pcb *upcb, const ip_addr_t *addr, u16_t port)
{
struct pbuf *p;
char msg[500];
snprintf(msg, sizeof(msg),
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=86400\r\n"
"DATE: Fri, 15 Apr 2016 04:56:29 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://%s:80/setup.xml\r\n"
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
"01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"ST: urn:Belkin:device:**\r\n"
"USN: uuid:Socket-1_0-38323636-4558-4dda-9188-cda0e6cc3dc0::urn:Belkin:device:**\r\n"
"X-User-Agent: redsonic\r\n\r\n", get_my_ip());
p = pbuf_alloc(PBUF_TRANSPORT, strlen(msg)+1, PBUF_RAM);
if (!p) {
printf("Failed to allocate transport buffer\n");
} else {
memcpy(p->payload, msg, strlen(msg)+1);
err_t err = udp_sendto(upcb, p, addr, port);
if (err < 0) {
printf("Error sending message: %s (%d)\n", lwip_strerr(err), err);
} else {
printf("Sent message '%s'\n", msg);
}
pbuf_free(p);
}
}
/**
* @brief This function is called when an UDP datagrm has been received on the port UDP_PORT.
* @param arg user supplied argument (udp_pcb.recv_arg)
* @param pcb the udp_pcb which received data
* @param p the packet buffer that was received
* @param addr the remote IP address from which the packet was received
* @param port the remote port from which the packet was received
* @retval None
*/
static void receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
if (p) {
printf("Msg received port:%d len:%d\n", port, p->len);
uint8_t *buf = (uint8_t*) p->payload;
printf("Msg received port:%d len:%d\nbuf: %s\n", port, p->len, buf);
send_udp(upcb, addr, port);
pbuf_free(p);
}
}
/**
* @brief Initialize the upnp server
* @retval true if init was succcessful
*/
bool upnp_server_init(void)
{
struct udp_pcb *upcb = mcast_join_group(UPNP_MCAST_GRP, UPNP_MCAST_PORT, receive_callback);
return (upcb != NULL);
}

2
examples/upnp/upnp.h Normal file
View file

@ -0,0 +1,2 @@
bool upnp_server_init(void);

114
examples/upnp/upnp_test.c Normal file
View file

@ -0,0 +1,114 @@
#include <esp8266.h>
#include <espressif/esp_common.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <esp8266.h>
#include <esp/uart.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <semphr.h>
#include <task.h>
#include <timers.h>
#include <queue.h>
#include <ota-tftp.h>
#include <rboot-api.h>
#include <lwip/pbuf.h>
#include <lwip/udp.h>
#include <lwip/tcp.h>
#include <lwip/ip_addr.h>
#include <lwip/api.h>
#include <lwip/netbuf.h>
#include <lwip/igmp.h>
#include <ssid_config.h>
#include <espressif/esp_wifi.h>
#include "lwipopts.h"
#include "upnp.h"
#include "httpd.h"
/** User friendly FreeRTOS delay macro */
#define delay_ms(ms) vTaskDelay(ms / portTICK_PERIOD_MS)
/** Semaphore to signal wifi availability */
static SemaphoreHandle_t wifi_alive;
/**
* @brief This is the multicast task
* @param arg user supplied argument from xTaskCreate
* @retval None
*/
static void mcast_task(void *arg)
{
xSemaphoreTake(wifi_alive, portMAX_DELAY);
xSemaphoreGive(wifi_alive);
(void) upnp_server_init();
while(1) {
delay_ms(2000);
}
}
/**
* @brief This is the wifi connection task
* @param arg user supplied argument from xTaskCreate
* @retval None
*/
static void wifi_task(void *pvParameters)
{
uint8_t status = 0;
uint8_t retries = 30;
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
xSemaphoreTake(wifi_alive, portMAX_DELAY);
printf("WiFi: connecting to WiFi\n");
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
while(1) {
while (status != STATION_GOT_IP && retries) {
status = sdk_wifi_station_get_connect_status();
if(status == STATION_WRONG_PASSWORD) {
printf("WiFi: wrong password\n");
break;
} else if(status == STATION_NO_AP_FOUND) {
printf("WiFi: AP not found\n");
break;
} else if(status == STATION_CONNECT_FAIL) {
printf("WiFi: connection failed\n");
break;
}
delay_ms(1000);
retries--;
}
if (status == STATION_GOT_IP) {
printf("WiFi: connected\n");
xSemaphoreGive(wifi_alive);
taskYIELD();
}
while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) {
xSemaphoreGive(wifi_alive);
taskYIELD();
}
printf("WiFi: disconnected\n");
sdk_wifi_station_disconnect();
delay_ms(1000);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
vSemaphoreCreateBinary(wifi_alive);
ota_tftp_init_server(TFTP_PORT);
xTaskCreate(&wifi_task, "wifi_task", 256, NULL, 2, NULL);
delay_ms(250);
xTaskCreate(&httpd_task, "http_server", 1024, NULL, 4, NULL);
xTaskCreate(&mcast_task, "mcast_task", 1024, NULL, 4, NULL);
}

1
examples/wificfg/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
!local.mk

View file

@ -0,0 +1,7 @@
#define configUSE_TRACE_FACILITY 1
#define configGENERATE_RUN_TIME_STATS 1
#define portGET_RUN_TIME_COUNTER_VALUE() (RTC.COUNTER)
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() {}
/* Use the defaults for everything else */
#include_next<FreeRTOSConfig.h>

View file

@ -0,0 +1,5 @@
# Makefile for wificfg example
PROGRAM=wificfg
EXTRA_COMPONENTS=extras/dhcpserver extras/wificfg
include ../../common.mk

View file

@ -0,0 +1,18 @@
"<!DOCTYPE html><html lang=\"en\">"
"<head>"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">"
"<script src=\"/script.js\"></script>"
"<title>",
"</title>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
"</head>"
"<body>"
"<ul class=\"topnav\" id=\"myTopnav\">"
"<li class=\"active\"><a href=\"/\">Home</a></li>"
"<li><a href=\"/wificfg/\">WiFi Config</a></li>"
"<li><a href=\"/tasks.html\">Tasks</a></li>"
"<li class=\"icon\">"
"<a href=\"javascript:void(0);\" onclick=\"myFunction()\">&#9776;</a>"
"</li>"
"</ul>",
"</body></html>"

View file

@ -0,0 +1 @@
FLASH_SIZE ?= 32

View file

@ -0,0 +1,94 @@
/*
* Example Wifi configuration via an access point.
*
* Copyright (C) 2016 OurAirQuality.org
*
* Licensed under the Apache License, Version 2.0, January 2004 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.apache.org/licenses/
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <string.h>
#include <ctype.h>
#include <espressif/esp_common.h>
#include <espressif/user_interface.h>
#include <esp/uart.h>
#include <FreeRTOS.h>
#include <task.h>
#include "lwip/sockets.h"
#include "wificfg/wificfg.h"
#include "sysparam.h"
static const char http_success_header[] = "HTTP/1.1 200 \r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Cache-Control: no-store\r\n"
"Transfer-Encoding: chunked\r\n"
"Connection: close\r\n"
"\r\n";
static const char *http_index_content[] = {
#include "content/index.html"
};
static int handle_index(int s, wificfg_method method,
uint32_t content_length,
wificfg_content_type content_type,
char *buf, size_t len)
{
if (wificfg_write_string(s, http_success_header) < 0) return -1;
if (method != HTTP_METHOD_HEAD) {
if (wificfg_write_string_chunk(s, http_index_content[0], buf, len) < 0) return -1;
if (wificfg_write_html_title(s, buf, len, "Home") < 0) return -1;
if (wificfg_write_string_chunk(s, http_index_content[1], buf, len) < 0) return -1;
socklen_t addr_len;
struct sockaddr addr;
addr_len = sizeof(addr);
getpeername(s, (struct sockaddr*)&addr, &addr_len);
if (wificfg_write_string_chunk(s, "<dl class=\"dlh\">", buf, len) < 0) return -1;
if (addr.sa_family == AF_INET) {
struct sockaddr_in *sa = (struct sockaddr_in *)&addr;
if (wificfg_write_string_chunk(s, "<dt>Peer address</dt>", buf, len) < 0) return -1;
snprintf(buf, len, "<dd>" IPSTR " : %d</dd>",
IP2STR((ip4_addr_t *)&sa->sin_addr.s_addr), ntohs(sa->sin_port));
if (wificfg_write_string_chunk(s, buf, buf, len) < 0) return -1;
}
if (wificfg_write_string_chunk(s, "</dl>", buf, len) < 0) return -1;
if (wificfg_write_string_chunk(s, http_index_content[2], buf, len) < 0) return -1;
if (wificfg_write_chunk_end(s) < 0) return -1;
}
return 0;
}
static const wificfg_dispatch dispatch_list[] = {
{"/", HTTP_METHOD_GET, handle_index, false},
{"/index.html", HTTP_METHOD_GET, handle_index, false},
{NULL, HTTP_METHOD_ANY, NULL}
};
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
wificfg_init(80, dispatch_list);
}