Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created August 30, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Micrified/ec855eaec5e80064dfb0e908e5b92766 to your computer and use it in GitHub Desktop.
Save Micrified/ec855eaec5e80064dfb0e908e5b92766 to your computer and use it in GitHub Desktop.
Main File
#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