Wednesday 9 May 2018

ESP8266 Blinky with official RTOS SDK

Last time I created a blinky program with the Espressif official Non-OS SDK, though it seems Espressif pays more attention to the NON-OS SDK (all of their major demos are non-OS), I'd like to use the RTOS because it will make things easier.

It is almost the same as the Non-OS, except instead of using the system timer of the Non-OS SDK, it uses the FreeRTOS software timer for the delay of the blink.

There is a project template in the SDK's ./example/ folder, we'll just use it as our base of this small demo, copy the "project_template" folder to the SDK's root and rename it to "blinky".

I'm using the official ESP Launcher board for this test, it has a blue LED connected to GPIO12, so in the "blinky/user/" folder, edit or create a user_main.c as this:

#include "esp_common.h"
#include "gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"

xTimerHandle    blink_timer;


/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABCCC
 *                A : rf cal
 *                B : rf init data
 *                C : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 user_rf_cal_sector_set(void)
{
    flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;
        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }

    return rf_cal_sec;
}


/****** led blink **********/
void led_toggle(void)
{
    if(GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT12)
    {
          gpio_output_set(0,BIT12, 0,0);
    }
    else
    {
        gpio_output_set(BIT12,0,0,0);
    }
}



/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
    gpio_output_set(BIT12, 0, BIT12, 0);
    blink_timer = xTimerCreate("blinkTMR", 300/portTICK_RATE_MS, pdTRUE, NULL, (tmrTIMER_CALLBACK)led_toggle);
    xTimerStart(blink_timer, portMAX_DELAY);
}

The only user created contents are the last 2 functions, the led_toggle() and user_init().

Reminder, don't forget to change the SDK_PATH and BIN_PATH settings in "gen_misc.sh", I feel it convenient to set the BIN_PATH to the SDK's root /bin/ folder, as the  blink.bin and esp_init_data_default.bin files are in there too. You'll need to download these files at least once when you use the ESP8266 for the first time, otherwise the chip will keep rebooting.

You are reading this probably because you are new to the ESP8266 as myself too. You may find it helpful by reading these articles too in my blog:

For building the SDK in Ubuntu:
https://www.mculabs.net/2018/04/building-esp8266-sdk-in-ubuntu.html

To connect a ESP8266 in Ubuntu:
https://www.mculabs.net/2017/07/connect-esp8266-in-ubuntu.html

Downloading(programming) the ESP8266 in Ubuntu:
https://www.mculabs.net/2018/04/downloading-code-to-esp8266-in-ubuntu.html