Compare commits

..

No commits in common. "master" and "acb5f6dc0fd13d341c40ae4f4c38f9d20cda3d95" have entirely different histories.

8 changed files with 160 additions and 155 deletions

View File

@ -1,12 +0,0 @@
BasedOnStyle: Google
IndentWidth: 4
AlignConsecutiveMacros: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveAssignments: Consecutive
AllowShortFunctionsOnASingleLine: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
ColumnLimit: 120

4
.gitignore vendored
View File

@ -1,4 +0,0 @@
/build
/cmake-build-*
/sdkconfig
/sdkconfig.old

View File

@ -1,7 +1,7 @@
idf_component_register(SRCS idf_component_register(SRCS
"main.c" "main.c"
"app_wifi.c" "app_wifi.c"
"app_led.c" "app_task_led.c"
INCLUDE_DIRS INCLUDE_DIRS
"." "."

View File

@ -1,20 +1,20 @@
menu "Application Specific Configuration" menu "WiFi Station Configuration"
config APP_WIFI_SSID config ESP_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
default "myssid" default "myssid"
help help
SSID (network name) for the example to connect to. SSID (network name) for the example to connect to.
config APP_WIFI_PASS config ESP_WIFI_PASSWORD
string "WiFi Password" string "WiFi Password"
default "mypassword" default "mypassword"
help help
WiFi password (WPA or WPA2) for the example to use. WiFi password (WPA or WPA2) for the example to use.
config APP_WIFI_RETR config ESP_MAXIMUM_RETRY
int "Maximum retry" int "Maximum retry"
default 5 default 5
help help
Set the Maximum retry to avoid station reconnecting to the AP. Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
endmenu endmenu

View File

@ -1,62 +0,0 @@
#include <stdint.h>
#include <stdio.h>
#include "sdkconfig.h"
/* FreeRTOS */
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
/* IDF */
#include "driver/rmt_tx.h"
#include "esp_log.h"
#define LOG_TAG "LED"
#define LED_NP_PIN (8)
static rmt_encoder_handle_t s_led_enc;
static rmt_channel_handle_t s_led_chan;
int app_led_init(void) {
esp_err_t ret = ESP_OK;
rmt_tx_channel_config_t led_chan_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.gpio_num = LED_NP_PIN,
.mem_block_symbols = 64,
.resolution_hz = 10000000UL,
.trans_queue_depth = 4,
};
ret = rmt_new_tx_channel(&led_chan_config, &s_led_chan);
if(ret != ESP_OK) {
return -1;
}
rmt_bytes_encoder_config_t led_enc_config = {
.bit0 =
{
.level0 = 1,
.duration0 = 3,
.level1 = 0,
.duration1 = 9,
},
.bit1 =
{
.level0 = 1,
.duration0 = 9,
.level1 = 0,
.duration1 = 3,
},
};
ret = rmt_new_bytes_encoder(&led_enc_config, &s_led_enc);
if (ret != ESP_OK) {
ESP_LOGE(LOG_TAG, "Failed to create LED encoder.");
return -2;
}
return 0;
}

92
main/app_task_led.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdint.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "driver/ledc.h"
#define LED_R_PIN (4)
#define LED_G_PIN (3)
#define LED_B_PIN (5)
#define LED_R_CHANNEL LEDC_CHANNEL_0
#define LED_G_CHANNEL LEDC_CHANNEL_1
#define LED_B_CHANNEL LEDC_CHANNEL_2
#define FADE_MS 250
TaskHandle_t xTaskDiscoLEDHandle = NULL;
void vTaskDiscoLED(void *pvParameters) {
ledc_timer_config_t ledc_timer = {
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 7500,
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channels[3] = {
{
.channel = LED_R_CHANNEL,
.duty = 0,
.gpio_num = LED_R_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.flags.output_invert = 0
},
{
.channel = LED_G_CHANNEL,
.duty = 0,
.gpio_num = LED_G_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.flags.output_invert = 0
},
{
.channel = LED_B_CHANNEL,
.duty = 0,
.gpio_num = LED_B_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
.flags.output_invert = 0
}
};
for(uint8_t i = 0; i < 3; i++) {
ledc_channel_config(&ledc_channels[i]);
}
ledc_fade_func_install(0);
uint32_t pwm_data[3];
for(;;) {
for(uint8_t i = 0; i < 3; i++) {
uint32_t pwm_new;
uint32_t delta;
do {
pwm_new = esp_random() / 0x80000; // 0-8191
if(pwm_new > pwm_data[i]) delta = pwm_new - pwm_data[i];
else delta = pwm_data[i] - pwm_new;
} while(delta < 2 * FADE_MS);
pwm_data[i] = pwm_new;
}
for(uint8_t i = 0; i < 3; i++) {
ledc_set_fade_with_time(ledc_channels[i].speed_mode,
ledc_channels[i].channel, pwm_data[i], FADE_MS);
ledc_fade_start(ledc_channels[i].speed_mode,
ledc_channels[i].channel, LEDC_FADE_NO_WAIT);
}
vTaskDelay(pdMS_TO_TICKS(FADE_MS));
}
}

View File

@ -1,80 +1,66 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include "sdkconfig.h" #include "sdkconfig.h"
/* FreeRTOS */
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h"
/* IDF */ #include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_wifi.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "nvs_flash.h"
#include "esp_wifi.h"
/* LwIP */
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sys.h" #include "lwip/sys.h"
static const char *LOG_TAG = "APP_WIFI"; static const char *TAG = "APP_WIFI";
#define WIFI_SSID CONFIG_APP_WIFI_SSID #define WIFI_SSID CONFIG_ESP_WIFI_SSID
#define WIFI_PSK CONFIG_APP_WIFI_PASS #define WIFI_PSK CONFIG_ESP_WIFI_PASSWORD
#define WIFI_RETR CONFIG_APP_WIFI_RETR #define WIFI_RETR CONFIG_ESP_MAXIMUM_RETRY
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1 #define WIFI_FAIL_BIT BIT1
static uint8_t s_retry_num = 0; static uint8_t s_retry_num = 0;
static EventGroupHandle_t s_wifi_event_group; EventGroupHandle_t g_wifi_event_group;
static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect(); esp_wifi_connect();
} }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); if(s_retry_num < WIFI_RETR) {
#if WIFI_RETR != 0
if (s_retry_num < WIFI_RETR) {
esp_wifi_connect(); esp_wifi_connect();
s_retry_num++; s_retry_num ++;
ESP_LOGI(LOG_TAG, "connection lost, reconnecting..."); ESP_LOGI(TAG, "connection lost, reconnecting...");
} else { } else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); xEventGroupSetBits(g_wifi_event_group, WIFI_FAIL_BIT);
ESP_LOGE(LOG_TAG, "connection failed, no more retries scheduled."); ESP_LOGE(TAG, "connection failed, no more retries scheduled.");
} }
#else
esp_wifi_connect();
ESP_LOGI(LOG_TAG, "connection lost, reconnecting...");
#endif
} }
else if (event_base == IP_EVENT) { else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
if (event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); ESP_LOGI(TAG, "got IPv4 address: "IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(g_wifi_event_group, WIFI_CONNECTED_BIT);
}
ip_event_got_ip_t *event = event_data; else if(event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
ESP_LOGI(LOG_TAG, "got IPv4 address: " IPSTR, IP2STR(&event->ip_info.ip)); ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
s_retry_num = 0; ESP_LOGI(TAG, "got IPv6 address: "IPV6STR, IPV62STR(event->ip6_info.ip));
} s_retry_num = 0;
xEventGroupSetBits(g_wifi_event_group, WIFI_CONNECTED_BIT);
if (event_id == IP_EVENT_GOT_IP6) {
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
ip_event_got_ip6_t *event = event_data;
ESP_LOGI(LOG_TAG, "got IPv6 address: " IPV6STR, IPV62STR(event->ip6_info.ip));
s_retry_num = 0;
}
} }
} }
void app_wifi_init(void) { void app_wifi_init(void) {
s_wifi_event_group = xEventGroupCreate(); g_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
@ -85,27 +71,23 @@ void app_wifi_init(void) {
esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip; esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL, ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL, &instance_any_id));
&instance_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL, &instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL, ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_GOT_IP6, &_wifi_event_handler, NULL, &instance_got_ip));
&instance_got_ip));
ESP_ERROR_CHECK(
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_GOT_IP6, &_wifi_event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = .sta = {
{ .ssid = WIFI_SSID,
.ssid = WIFI_SSID, .password = WIFI_PSK,
.password = WIFI_PSK, .threshold.authmode = WIFI_AUTH_WPA2_PSK,
.threshold.authmode = WIFI_AUTH_WPA2_PSK, .pmf_cfg = {
.pmf_cfg = {.capable = true, .required = false}, .capable = true,
.required = false
}, },
},
}; };
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
}
void app_wifi_wait_connected(uint32_t timeout_ticks) {
} }

View File

@ -1,37 +1,46 @@
#include <stdio.h> #include <stdio.h>
#include "sdkconfig.h" #include "sdkconfig.h"
/* FreeRTOS */
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/event_groups.h"
/* IDF */
#include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_spi_flash.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "spi_flash_mmap.h" #include "esp_log.h"
static const char *LOG_TAG = "APP_MAIN"; static const char *TAG = "APP_MAIN";
extern EventGroupHandle_t s_wifi_event_group; extern EventGroupHandle_t g_wifi_event_group;
void app_wifi_init(void); void app_wifi_init(void);
// Tasks
extern TaskHandle_t xTaskDiscoLEDHandle;
void vTaskDiscoLED(void *pvParameters);
void app_main(void) { void app_main(void) {
esp_err_t ret = nvs_flash_init(); esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init(); ret = nvs_flash_init();
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
ESP_LOGI(LOG_TAG, "Playground started.\r\n"); printf("Playground started.\r\n");
app_wifi_init(); app_wifi_init();
EventBits_t bits = xEventGroupWaitBits(g_wifi_event_group, 0x03, pdFALSE, pdFALSE, portMAX_DELAY);
if(bits & 0x01) {
ESP_LOGI(TAG, "WiFi connected.");
} else {
ESP_LOGE(TAG, "WiFi failed.");
}
for (;;) { xTaskCreate(vTaskDiscoLED, "vTaskDisco", 1024, NULL, 3, &xTaskDiscoLEDHandle);
for(;;) {
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
} }
} }