Created
August 30, 2019 12:34
-
-
Save Micrified/ec855eaec5e80064dfb0e908e5b92766 to your computer and use it in GitHub Desktop.
Main File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "freertos/event_groups.h" | |
#include "esp_system.h" | |
#include "esp_wifi.h" | |
#include "esp_event.h" | |
#include "esp_spi_flash.h" | |
#include "lwip/err.h" | |
#include "lwip/sys.h" | |
#include "nvs_flash.h" | |
#include "wifi.h" | |
#include "ble.h" | |
/* | |
******************************************************************************* | |
* Global Variables * | |
******************************************************************************* | |
*/ | |
// Network configuration details | |
char g_wifi_ssid[32] = "Foo"; | |
char g_wifi_pswd[64] = "bar"; | |
unsigned int g_wifi_retc = 3; | |
/* FreeRTOS Event Group */ | |
EventGroupHandle_t g_wifi_event_group; | |
/* | |
******************************************************************************* | |
* Function Definitions * | |
******************************************************************************* | |
*/ | |
// Aborts with the given message and error. Suspends current task forever | |
void panic (const char *msg, esp_err_t err) { | |
if (err != ESP_OK) { | |
fprintf(stderr, "Panic: %s | \"%s\"\n", msg, esp_err_to_name(err)); | |
} else { | |
fprintf(stderr, "Panic: %s\n", msg); | |
} | |
vTaskSuspend(NULL); | |
} | |
// Shutdown handler | |
void shutdown_handler (void) { | |
printf(" -*- Shutdown -*-\n"); | |
fflush(stdout); | |
} | |
// Initializes flash-memory | |
esp_err_t init_flash (void) { | |
esp_err_t err; | |
// Attempt initialization | |
err = nvs_flash_init(); | |
// Attempt to handle some errors | |
if (err == ESP_ERR_NVS_NO_FREE_PAGES || | |
err == ESP_ERR_NVS_NEW_VERSION_FOUND) { | |
err = nvs_flash_erase(); | |
if (err == ESP_OK) { | |
err = nvs_flash_init(); | |
} | |
} | |
return err; | |
} | |
// Starting point for the application | |
void app_main (void) { | |
esp_err_t err; | |
// Obtain and display flash memory size | |
size_t flash_size_bytes = spi_flash_get_chip_size(); | |
printf("Flash (MB): %d\n", flash_size_bytes / (1024 * 1024)); | |
// Register a shutdown handler | |
if ((err = esp_register_shutdown_handler(shutdown_handler)) != ESP_OK) { | |
panic("Failed to register shutdown handler!", err); | |
} | |
// Flash MUST be initialized before WiFi now. So we do that first | |
if ((err = init_flash()) != ESP_OK) { | |
panic("Failed to initialize flash memory!", err); | |
} | |
// Start BLE | |
if ((err = ble_init()) != ESP_OK) { | |
fprintf(stderr, "BLE couldn't be started - sorry :(\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment