Showing posts with label BLINKY. Show all posts
Showing posts with label BLINKY. Show all posts

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

Saturday, 28 April 2018

A Very Simple ESP8266 Blinky Source Code Using the Non-OS SDK

There are examples come with the official Espressif SDK, for the latest version 2.2.0, the examples are those:

As I said before, Espressif has the worst documentation comparing to other chip suppliers. These examples don't have any detailed information, the only thing you can find on their web site is an article about the Iot_Demo, which tells you how to use the program, not explaining how the code works.

I created a very simple blinky program with the Non-OS SDK, it has only one souce file in c, and it does nothing more than blink a LED on the ESP Launcher board. Not using any WiFi or communication APIs, anyway, the SDK forces you to include an initialization code in the user code to tell the chip where is the default parameters for the wireless circuits inside, and it seems the chip goes into Access Point mode though you don't have any code to let it do so.

There is a blue LED connected to MTDI/GPIO12 pin on the ESP Launcher board, what out code does, is to setup a software system timer, then toggle the status of the GPIO12 pin in the callback function. Plus a "Hello World" printed to the serial port. Here is the complete source code:

#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "user_interface.h"

uint32 priv_param_start_sec;
/******************************************************************************
 * 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 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
    enum 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;
            priv_param_start_sec = 0x3C;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            priv_param_start_sec = 0x7C;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
            rf_cal_sec = 512 - 5;
            priv_param_start_sec = 0x7C;
            break;
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            priv_param_start_sec = 0xFC;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
            rf_cal_sec = 1024 - 5;
            priv_param_start_sec = 0x7C;
            break;
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            priv_param_start_sec = 0xFC;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            priv_param_start_sec = 0xFC;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            priv_param_start_sec = 0xFC;
            break;
        default:
            rf_cal_sec = 0;
            priv_param_start_sec = 0;
            break;
    }

    return rf_cal_sec;
}

/*******  toggle LED  **************/
void ICACHE_FLASH_ATTR toggle(void)
{
if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT12) // test if GPIO12 is 1
{
gpio_output_set(0, BIT12, 0, 0); // output 0
}
else
{
gpio_output_set(BIT12, 0, 0, 0); // output 1
}
}

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
LOCAL os_timer_t blink_timer;
os_printf("Hello World from MCU Labs ESP8266\n");
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //set the pin as GPIO
gpio_output_set(0, 0, BIT12, 0); //enable output on GPIO12
os_timer_disarm(&blink_timer);
os_timer_setfn(&blink_timer, (os_timer_func_t*)toggle, NULL); //setup callback
os_timer_arm(&blink_timer, 300, 1); //start timer at an interval of 300ms
}

Here is the video for compiling and running of the code, and the serial port output.
For how to download the code to ESP8266 in Ubuntu, see this article : https://www.mculabs.net/2018/04/downloading-code-to-esp8266-in-ubuntu.html 
For how to set Ubuntu to ESP8266's 74880 baudrate, see this: https://www.mculabs.net/2018/04/getting-odd-74880-baudrate-for-esp8266.html