ESP32修改BootLoader:在boot中添加GPIO和IIC驱动方式

1. ESP Bootloader简介

ESP32有着强大的引导加载程序(Bootloader)功能:
主要执行以下任务:
内部模块的最小化初始配置;
根据分区表和 ota_data(如果存在)选择需要引导的应用程序(app)分区;
将此应用程序映像加载到 RAM(IRAM 和 DRAM)中,最后把控制权转交给应用程序。
引导加载程序位于 Flash 的 0x1000 偏移地址处。

2. Bootloader修改方式

这里引用C站一个作者的文章,写的不错:点这里 文章分为上下两篇,下篇。

当涉及到用户有特殊BootLoader功能需求时,需要用户自行修改,修改方式主要有:
通过钩子函数。扩展原来的bootloader流程,打个补丁。
通过覆盖bootloader。重写一个bootloader,覆盖原来bootloader的逻辑来运行。
可以发现,这两种方式一个针对小改动需求,一个针对大改动的需要。
怎么添加自己的功能
如果在应用层开发,会做些什么?

嗯,你会在component目录下新建自己功能名的文件夹
之后呢?不知道了… 那就看看component其他组件都放了啥吧。哦,有CMakeLists.txt,component.mk,还有代码目录。那咱们依葫芦画瓢,也放上这些东西。
然后?既然有Cmake的东西,那就看看里面都说了啥。哦,原来里面就是设置好了要编译哪些源文件,头文件目录在哪里,还有配置好编译选项啊,库路径什么的。

是的,这些东西在bootloader里面都有,方法类似。就我的开发方式而言,mycode放在bootloader_support目录下之后,里面的源文件、头文件、库这些的,都跟应用层开发一样。该在CMakeLists.txt加什么就加。
这跟在boot还是在app其实就没啥关系,是CMake这种跨平台编译方式的基本操作。

3. 我的修改方式

本文主要讲解其中一种修改方式:mycode放在bootloader_support目录下之后,里面的源文件、头文件、库这些的,都跟应用层开发一样。该在CMakeLists.txt加什么就加。

下图是本人在bootloader_support文件夹中添加的个人代码“tc2x”

esp32怎么拓展外部ram esp32外扩ram_#define

并在CmakeLists.txt中添加需要编译的源文件和头文件目录

esp32怎么拓展外部ram esp32外扩ram_#define_02

此时,完成了在bootloader_support添加自己的代码。

4. 修改bootloader_utility.c文件

如图,我的boot修改内容主要是基于boot跳转APP失败时,跑一段自己的驱动和应用。

esp32怎么拓展外部ram esp32外扩ram_驱动开发_03

5. 在bootloader中实现GPIO控制

实现代码:

#include <string.h>
#include <stdint.h>
#include <limits.h>
#include "esp_attr.h"
#include "esp_log.h"
#include "esp_err.h"
#include "esp_rom_sys.h"
#include "esp_rom_uart.h"
#include "sdkconfig.h"
#include "esp_rom_crc.h"
#include "esp_rom_gpio.h"
#include "esp_rom_efuse.h"
#include "soc/gpio_periph.h"
#include "soc/soc_caps.h"
#include "hal/gpio_ll.h"
#include "hal/gpio_types.h"

#define CFG_I2C_SCL_PORT   GPIO_PORT_0
#define CFG_I2C_SCL_PIN    18
#define CFG_I2C_SDA_PORT   GPIO_PORT_0
#define CFG_I2C_SDA_PIN    17

#define PIN_OUTPUT_ENABLE(PIN_NAME)               SET_PERI_REG_MASK(PIN_NAME,FUN_IE)
#define PIN_OUTPUT_DISABLE(PIN_NAME)              CLEAR_PERI_REG_MASK(PIN_NAME,FUN_IE)
void HAL_GPIO_WritePin(int gpio_port, int gpio_pin, int level)
{
    gpio_ll_set_level(&GPIO, gpio_pin, level);
}
uint8_t HAL_GPIO_ReadPin(int gpio_port, int gpio_pin)
{
    return gpio_ll_get_level(&GPIO, gpio_pin);
}
void HAL_GPIO_SetOut(int gpio_port, int gpio_pin)
{
    esp_rom_gpio_pad_select_gpio(gpio_pin);
    // if (GPIO_PIN_MUX_REG[gpio_pin]) {
    //     PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_pin]);
    //     PIN_SLP_OUTPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_pin]);
    // }
    esp_rom_gpio_pad_pullup_only(gpio_pin);
    gpio_ll_output_enable(&GPIO, gpio_pin);
    //gpio_ll_sleep_output_enable(&GPIO, gpio_pin);
}
void HAL_GPIO_SetIn(int gpio_port, int gpio_pin)
{
    esp_rom_gpio_pad_select_gpio(gpio_pin);
    // if (GPIO_PIN_MUX_REG[gpio_pin]) {
    //     PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_pin]);
    //     PIN_SLP_OUTPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_pin]);
    // }
    esp_rom_gpio_pad_pullup_only(gpio_pin);
    gpio_ll_input_enable(&GPIO, gpio_pin);
   // gpio_ll_sleep_input_enable(&GPIO, gpio_pin);
}

6. 在bootloader中实现IIC控制

代码实现:

void I2C_DELAY(uint32_t us)
{
    esp_rom_delay_us(us);
}
static const char* TAG = "[GPIOIIC]";

#define IIC1 0
#define IIC2 1
#define IICX 0XFF 
#define GPIO_PIN_SET   1 
#define GPIO_PIN_RESET 0  
static int I2C_SCL_PORT = 0;
static int I2C_SCL_PIN  = 18;
static int I2C_SDA_PORT = 0;
static int I2C_SDA_PIN  = 17;
#define us_num 2
#define I2C_READ_BIT                 (0x01)        //!< If this bit is set in the address field, transfer direction is from slave to master.
#define I2C_WRITE_BIT                (0xFE)

static void I2C_SEL(uint8_t I2Cn, uint8_t scl, uint8_t sda)
{
    switch(I2Cn)
    {
    case IIC1:
        I2C_SCL_PORT = CFG_I2C_SCL_PORT;
        I2C_SCL_PIN  = CFG_I2C_SCL_PIN;
        I2C_SDA_PORT = CFG_I2C_SDA_PORT;
        I2C_SDA_PIN  = CFG_I2C_SDA_PIN;
        break;
    case IIC2:
        I2C_SCL_PORT = CFG_I2C_SCL_PORT;
        I2C_SCL_PIN  = CFG_I2C_SCL_PIN;
        I2C_SDA_PORT = CFG_I2C_SDA_PORT;
        I2C_SDA_PIN  = CFG_I2C_SDA_PIN;
        break;
    default: 
        I2C_SCL_PORT = CFG_I2C_SCL_PORT;
        I2C_SCL_PIN  = scl;
        I2C_SDA_PORT = CFG_I2C_SDA_PORT;
        I2C_SDA_PIN  = sda;
        break;
    }
}
static void I2C_SCL_OUTPUT(void)
{
    HAL_GPIO_SetOut(I2C_SCL_PORT, I2C_SCL_PIN);
}
static void I2C_SCL_INPUT(void)
{
    HAL_GPIO_SetIn(I2C_SCL_PORT, I2C_SCL_PIN);
}
static void I2C_SCL_HIGH(void)
{
    HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, GPIO_PIN_SET);
}
static void I2C_SCL_LOW(void)
{
    HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, GPIO_PIN_RESET);
}
static void I2C_SDA_OUTPUT(void)
{
    HAL_GPIO_SetOut(I2C_SDA_PORT, I2C_SDA_PIN);
}
static void I2C_SDA_INPUT(void)
{
    HAL_GPIO_SetIn(I2C_SDA_PORT, I2C_SDA_PIN);
}
static void I2C_SDA_HIGH(void)
{
    HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, GPIO_PIN_SET);
}
static void I2C_SDA_LOW(void)
{
    HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, GPIO_PIN_RESET);
}
static uint8_t I2C_SDA_READ(void)
{
    return HAL_GPIO_ReadPin(I2C_SDA_PORT, I2C_SDA_PIN);
}
static uint8_t I2C_SCL_READ(void)
{
    return HAL_GPIO_ReadPin(I2C_SCL_PORT, I2C_SCL_PIN);
}
void I2C_SCL_SDA_TEST(void)
{
    I2C_SCL_OUTPUT();
    I2C_SDA_OUTPUT();
    I2C_SDA_HIGH();
    I2C_SDA_LOW();
}
static void I2C_Init(int scl, int sda)
{
    I2C_SEL(IICX, scl, sda);
    I2C_SCL_INPUT();
    I2C_SDA_INPUT();
    I2C_SCL_OUTPUT();
    I2C_SCL_HIGH();
    I2C_SDA_OUTPUT();
    I2C_SDA_HIGH();
}
static bool i2c_master_clear_bus(void);
static bool i2c_master_issue_startcondition(void);
static bool i2c_master_issue_stopcondition(void);
static bool i2c_master_clock_byte(uint_fast8_t databyte);
static bool i2c_master_clock_byte_in(uint8_t *databyte, bool ack);
static bool i2c_master_wait_while_scl_low(void);
bool gpioi2c_master_init(uint8_t sclpin, uint8_t sdapin)
{
    I2C_Init(sclpin, sdapin);
    return i2c_master_clear_bus();
}
static bool i2c_master_clear_bus(void)
{
    bool bus_clear;
    I2C_SDA_HIGH();
    I2C_SCL_HIGH();
    I2C_DELAY(1);
    if (I2C_SDA_READ() == 1 && I2C_SCL_READ() == 1)
    {
        bus_clear = true;
    }
    else if (I2C_SCL_READ() == 1)
    {
        bus_clear = false;
        ESP_LOGE(TAG, "i2c_master_clear_bus 1 fail");
        for (uint_fast8_t i = 18; i--;)
        {
            I2C_SCL_LOW();
            I2C_DELAY(1);
            I2C_SCL_HIGH();
            I2C_DELAY(1);
            if (I2C_SDA_READ() == 1)
            {
                bus_clear = true;
                break;
            }
        }
    }
    else
    {
        bus_clear = false;
         ESP_LOGE(TAG, "i2c_master_clear_bus 2 fail");
    }
    return bus_clear;
}
static bool i2c_master_issue_startcondition(void)
{
    I2C_SCL_OUTPUT();
    I2C_SDA_OUTPUT();
    I2C_SDA_HIGH();
    I2C_DELAY(1);
    if (!i2c_master_wait_while_scl_low())
    {
        ESP_LOGE(TAG, "i2c_master_wait_while_scl_low fail");
        return false;
    }
    I2C_SDA_LOW();
    I2C_DELAY(1);
    // Other module function expect SCL to be low
    I2C_SCL_LOW();
    I2C_DELAY(1);
    return true;
}
static bool i2c_master_issue_stopcondition(void)
{
    I2C_SDA_LOW();
    I2C_DELAY(1);
    if (!i2c_master_wait_while_scl_low())
    {
        ESP_LOGE(TAG, "i2c_master_issue_stopcondition fail");
        return false;
    }
    I2C_SDA_HIGH();
    I2C_DELAY(1);
    return true;
}
static bool i2c_master_clock_byte(uint_fast8_t databyte)
{
    bool transfer_succeeded = true;
    I2C_SDA_OUTPUT();
    // MSB first
    for (uint_fast8_t i = 0x80; i != 0; i >>= 1)
    {
        I2C_SCL_LOW();
        I2C_DELAY(1);
        if (databyte & i)
        {
            I2C_SDA_HIGH();
        }
        else
        {
            I2C_SDA_LOW();
        }
        if (!i2c_master_wait_while_scl_low())
        {
            ESP_LOGE(TAG, "i2c_master_clock_byte fail");
            transfer_succeeded = false; // Timeout
            break;
        }
    }
    I2C_SCL_LOW();
    I2C_DELAY(1);
    I2C_SDA_INPUT();
    I2C_DELAY(1);
    transfer_succeeded &= i2c_master_wait_while_scl_low();
    transfer_succeeded &= !(I2C_SDA_READ());
    I2C_SCL_LOW();
    I2C_DELAY(1);
    I2C_SDA_OUTPUT();
    return transfer_succeeded;
}
static bool i2c_master_clock_byte_in(uint8_t *databyte, bool ack)
{
    uint_fast8_t byte_read          = 0;
    bool         transfer_succeeded = true;
    I2C_SDA_INPUT();
    for (uint_fast8_t i = 0x80; i != 0; i >>= 1)
    {
        if (!i2c_master_wait_while_scl_low())
        {
            ESP_LOGE(TAG, "i2c_master_clock_byte_in 1 fail");
            transfer_succeeded = false;
            break;
        }
        if (I2C_SDA_READ())
        {
            byte_read |= i;
        }
        else
        {
            // No need to do anything
        }
        I2C_SCL_LOW();
        I2C_DELAY(1);
    }
    I2C_SDA_OUTPUT();
    *databyte = (uint8_t)byte_read;
    if (ack)
    {
        I2C_SDA_LOW();
    }
    else
    {
        I2C_SDA_HIGH();
    }
    I2C_DELAY(1);
    if (!i2c_master_wait_while_scl_low())
    {
        ESP_LOGE(TAG, "i2c_master_clock_byte_in 2 fail");
        transfer_succeeded = false; // Timeout
    }
    I2C_SCL_LOW();
    I2C_DELAY(1);
    return transfer_succeeded;
}
static bool i2c_master_wait_while_scl_low()
{
    uint8_t timeout_counter = 30;
    I2C_SCL_HIGH();
    I2C_DELAY(1);
    while (I2C_SCL_READ() == 0)
    {
        if (timeout_counter-- == 0)
        {
            ESP_LOGE(TAG, "i2c_master_wait_while_scl_low fail");
            return false;
        }
        else
        {
            I2C_DELAY(3);
        }
    }
    return true;
}
bool gpioi2c_master_read(uint8_t address, uint8_t reg_addr, uint8_t * data, uint8_t data_length, bool issue_stop_condition)
{
    bool transfer_succeeded = true;
    transfer_succeeded &= i2c_master_issue_startcondition();
    transfer_succeeded &= i2c_master_clock_byte(address&I2C_WRITE_BIT);
    /** send the address of register which you want to read*/
    transfer_succeeded &= i2c_master_clock_byte(reg_addr);
    /** must send 2 times start condition */
    transfer_succeeded &= i2c_master_issue_startcondition();
    transfer_succeeded &= i2c_master_clock_byte(address|I2C_READ_BIT);
    /* Transfer direction is from Slave to Master */
    while (data_length-- && transfer_succeeded)
    {
        // To indicate to slave that we've finished transferring last data byte
        // we need to NACK the last transfer.
        if (data_length == 0)
        {
            transfer_succeeded &= i2c_master_clock_byte_in(data, (bool)false);
        }
        else
        {
            transfer_succeeded &= i2c_master_clock_byte_in(data, (bool)true);
        }
        data++;
    }
    if (issue_stop_condition || !transfer_succeeded)
    {
        transfer_succeeded &= i2c_master_issue_stopcondition();
    }
    return transfer_succeeded;
}


bool gpioi2c_master_write(uint8_t address, uint8_t reg_addr, uint8_t * data, uint8_t data_length, bool issue_stop_condition)
{
    bool transfer_succeeded = true;
    
    transfer_succeeded &= i2c_master_issue_startcondition();
ESP_LOGD(TAG, "gpioi2c_master_write transfer_succeeded1=%d",transfer_succeeded);
    transfer_succeeded &= i2c_master_clock_byte(address&I2C_WRITE_BIT);
ESP_LOGD(TAG, "gpioi2c_master_write transfer_succeeded2=%d",transfer_succeeded);
    /** send the address of register which you want to write */
    transfer_succeeded &= i2c_master_clock_byte(reg_addr);
ESP_LOGD(TAG, "gpioi2c_master_write transfer_succeeded3=%d",transfer_succeeded);
    /* Transfer direction is from Master to Slave */
    while (data_length-- && transfer_succeeded)
    {
        transfer_succeeded &= i2c_master_clock_byte(*data);
        data++;
    }
    if (issue_stop_condition || !transfer_succeeded)
    {
        transfer_succeeded &= i2c_master_issue_stopcondition();
ESP_LOGD(TAG, "gpioi2c_master_write transfer_succeeded4=%d",transfer_succeeded);
    }
    return transfer_succeeded;
}


7. 总结

通过以上方式,可以实现在esp32修改BootLoader代码,并实现GPIO和IIC驱动,从而实现自己的修改需求。