close

 記錄如何安裝 ESP-IDF 開發環境

ESP 應用程式開發之元件

  • PC loaded with either Windows, Linux or Mac operating system
  • Toolchain to build the Application for ESP32
  • ESP-IDF that essentially contains API for ESP32 and scripts to operate the Toolchain
  • A text editor to write programs (Projects) in C, e.g. Eclipse
  • ESP32 board itself

ESP 應用程式開發之環境準備

  • 1. Setup of Toolchain
  • 2. Getting of ESP-IDF from GitHub
  • 3. Installation and configuration of Eclipse
    You may skip the last step, if you prefer to use different editor 
     

ESP 應用程式開發之步驟

  • 1. Configuration of a Project and writing the code
  • 2. Compilation of the Project and linking it to build an Application
  • 3. Flashing (uploading) of the Application to ESP32
  • 4. Monitoring / debugging of the Application

實例

  • Download and Install VirtualBox 5.2 & Ubuntu 16.04   (參考http://blog.xuite.net/yh96301/blog/432341564)   
  • Setup Toolchain
  • Verify Toolchain Installation
  • Download ESP32 ESP-IDF from GitHub
  • Compile Blinking LED Example

#---------------------------

ESP-IDF 有二種方式可以得到,一種是從source file 得到,一種是從bin檔得到,我們使用第二種方式安裝,

需要第一種方式使用者請自行參考 Compile the toolchain from source using crosstool-NG

第二種方式如下:

Set up of Toolchain for Linux

Step 0: Prerequisites

Install some packages

To compile with ESP-IDF you need to get the following packages:

Ubuntu and Debian:

             sudo apt-get install git wget make libncurses-dev flex bison gperf python python-serial

Step 1: Download binary toolchain for the ESP32

ESP32 toolchain for Linux is available for download from Espressif website:

for 64-bit Linux:
       https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz

Download this file, then extract it to the location you prefer, for example:

    mkdir -p ~/esp
    cd ~/esp
    tar -xzf ~/Downloads/xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz

The toolchain will be extracted into ~/esp/xtensa-esp32-elf/ directory.

update your PATH environment variable to ~/.profile file. 
    export PATH=$PATH:$HOME/esp/xtensa-esp32-elf/bin

Step 2: Getting ESP-IDF from github

Open terminal, navigate to the directory you want to clone ESP-IDF and clone it using git clone command:

    cd ~/esp
    git clone --recursive https://github.com/espressif/esp-idf.git

ESP-IDF will be downloaded into ~/esp/esp-idf.

Note the --recursive option! If you have already cloned ESP-IDF without this option, run another command to get all the submodules:

    cd ~/esp/esp-idf
    git submodule update --init

Step 3: Starting a project

ESP-IDF by itself does not build a binary to run on the ESP32. The binary “app” comes from a project in a different directory. Multiple projects can share the same ESP-IDF directory.

The easiest way to start a project is to download the template project from GitHub:

    cd ~/esp
    git clone https://github.com/espressif/esp-idf-template.git myapp

This will download esp-idf-template project into ~/esp/myapp directory.

Step 4: Building and flashing the application

Now you are ready to prepare your application for ESP32. To start off quickly, we will use get-started/hello_world project from examples directory in IDF.

Copy get-started/hello_world to ~/esp directory:

    cd ~/esp
    cp -r $IDF_PATH/examples/get-started/hello_world .

Connect

You are almost there. To be able to proceed further, connect ESP32 board to PC, check under what serial port the board is visible and verify if serial communication works. If you are not sure how to do it, check instructions in section Establish Serial Connection with ESP32. Note the port number, as it will be required in the next step.

Configure

Being in terminal window, go to directory of hello_world application by typing cd ~/esp/hello_world. Then start project configuration utility menuconfig:

    cd ~/esp/hello_world
    make menuconfig

If previous steps have been done correctly, the following menu will be displayed:

Project configuration - Home window

Project configuration - Home window

In the menu, navigate to Serial flasher config > Default serial port to configure the serial port, where project will be loaded to. Confirm selection by pressing enter, save configuration by selecting < Save > and then exit application by selecting < Exit >.

Step 5: Build and Flash

Now you can build and flash the application. Run:

make flash

This will compile the application and all the ESP-IDF components, generate bootloader, partition table, and application binaries, and flash these binaries to your ESP32 board.

esptool.py v2.0-beta2
Flashing binaries to serial port /dev/ttyUSB0 (app at offset 0x10000)...
esptool.py v2.0-beta2
Connecting........___
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Attaching SPI flash...
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0220
Compressed 11616 bytes to 6695...
Wrote 11616 bytes (6695 compressed) at 0x00001000 in 0.1 seconds (effective 920.5 kbit/s)...
Hash of data verified.
Compressed 408096 bytes to 171625...
Wrote 408096 bytes (171625 compressed) at 0x00010000 in 3.9 seconds (effective 847.3 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 82...
Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 8297.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting...

If there are no issues, at the end of build process, you should see messages describing progress of loading process. Finally, the end module will be reset and “hello_world” application will start.

If you’d like to use the Eclipse IDE instead of running make, check out the Eclipse guide.

Step 6: Monitor

To see if “hello_world” application is indeed running, type make monitor. This command is launching IDF Monitor application:

$ make monitor
MONITOR
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57
...

Several lines below, after start up and diagnostic log, you should see “Hello world!” printed out by the application.

...
Hello world!
Restarting in 10 seconds...
I (211) cpu_start: Starting scheduler on APP CPU.
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...

To exit monitor use shortcut Ctrl+].

To execute make flash and make monitor in one shoot type make flash monitor

#------END ----------------------------------------- 

ESP32 – IDF Programming Getting Started

Hello World

Esp-IDF useses FreeRTOS as an operating system. If we want to work in esp-idf, we should have knowledge on How to write a program using FreeRTOS’s API. For FreeRTOS, I will give some separate tutorial later. Whenever i’m using some FreeRTOS API that time itself i will explain that API. So don’t worry guys. If you want to learn basic concepts of RTOS Part 1 please click Here. For RTOS Part 2 please click Here.

Now we are going to see the first example “hello world”. Hello world is the first program for programming, who wants to learn any programming language. So we are also going to see that.

Before that, we have to look in ESP directory. So the directory contains below Sub directory.

+–esp-idf

            |

            + – – components

            |

            + – – docs

            |

            + – – examples

            |

            + – – make

            |

            + – – tools

The components directory holds all the ‘C’ code for the ESP32. It contains all the‘components’ that make up the ESP32.
It includes Drivers for numerous peripherals, the bootloader, BT (bluetooth), freeRTOS etc.

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"

void app_main()
{
  printf("Hello world....\n");
  vTaskDelay(1000 / portTICK_PERIOD_MS);
  for(int i = 10; i>0; i--) {
    printf("Module restarts in %d seconds...\n", i);
    vTaskDelay(1000 / portTICK_PERIOD_MS); 
  }
printf("Restarting....\n");
fflush(stdout);
esp_restart();
}

freertos/FreeRTOS.h : Inclusion of this sets configuration required to run freeRTOS on ESP32.
freertos/task.h: This file includes task related things such as multitasking, task creating, deleting.
esp_system.h: This inclusion configures the peripherals in the ESP system. Think of it as system initialization.

esp_restart():

This function restart the esp. So again app_main will prints. Its like while(1).

 

MULTITASK : LED BLINKING WITH HELLO WORLD 

It is a RTOS. So we can create multiple tasks. Using that method, i am going to create the two tasks.

  1. LED task (It will blink the LED)
  2. Hello world task (It prints in serial console)

Before that, FreeRTOS has many API. Here we are going to use API for Task creating.

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
 
#define BLINK_GPIO 13
 
void blink_task(void *pvParameter)
{
 gpio_pad_select_gpio(BLINK_GPIO);               /* Set the GPIO as a push/pull output */
 gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
 while(1) {
   gpio_set_level(BLINK_GPIO, 0);              /* Blink off (output low) */
 vTaskDelay(1000 / portTICK_PERIOD_MS);
  gpio_set_level(BLINK_GPIO, 1);             /* Blink on (output high) */
 vTaskDelay(1000 / portTICK_PERIOD_MS);
  }

void print_task(void *pvParameter)
{
 while(1) {
 printf("Hello world.... Welcome to www.embetronicx.com\n");
 vTaskDelay(1000 / portTICK_PERIOD_MS);
 }
}
 
void app_main()
{
 xTaskCreate(&blink_task, "blink_task", 512, NULL, 5, NULL);
 xTaskCreate(&print_task, "print_task", 2048, NULL, 5, NULL);
}

 

xTaskCreate()  API:

            This FreeRTOS API is used to create a task. Using this API we can create more number of tasks.

portBASE_TYPE xTaskCreate  (      pdTASK_CODE pvTaskCode,

                                  const signed portCHAR * const pcName,

                                  unsigned portSHORT usStackDepth,

                                  void *pvParameters,

                                  unsigned portBASE_TYPE uxPriority,

                                  xTaskHandle *pxCreatedTask );

 

  • pvTaskCode: a pointer to the function where the task is implemented. (Address of the fuction)
  • pcName: given name to the task. This is useless to FreeRTOS but is intented to debugging purpose only.
  • usStackDepth: length of the stack for this task in words. The actual size of the stack depends on the micro controller.
  • pvParameters: a pointer to arguments given to the task.
  • uxPriority: priority given to the task, a number between 0 and MAX_PRIORITIES – 1.
  • pxCreatedTask: a pointer to an identifier that allows to handle the task. If the task does not have to be handled in the future, this can be leaved NULL.

 

ESP32 IDF: Setting a soft AP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "esp_wifi.h"
 
#define SSID "ESP32AP"
 
void app_main()
{  
 
    wifi_init_config_t wifiInitializationConfig = WIFI_INIT_CONFIG_DEFAULT();
 
    esp_wifi_init(&wifiInitializationConfig);
 
    esp_wifi_set_storage(WIFI_STORAGE_RAM);
 
    esp_wifi_set_mode(WIFI_MODE_AP);
 
    wifi_config_t ap_config = {
          .ap = {
            .ssid = SSID,
            .channel = 0,
            .authmode = WIFI_AUTH_OPEN,
            .ssid_hidden = 0,
            .max_connection = 1,
            .beacon_interval = 100
          }
        };
 
    esp_wifi_set_config(WIFI_IF_AP, &ap_config);
 
    esp_wifi_start();
 
}

 

 

  •  
arrow
arrow
    全站熱搜

    stanley 發表在 痞客邦 留言(0) 人氣()