我当时正在使用带有Arduino IDE的ESP8266。从这一经验中,我了解到所有项目都应包含两个主要功能-void setup()和void loop()。
后来我使用Platformio迁移到Visual Studio Code,并且选择的第一个名称为Blink的示例具有完全不同的结构:
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;
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;
}
void blinky(void *arg)
{
static uint8_t state = 0;
if (state) {
GPIO_OUTPUT_SET(2, 1);
} else {
GPIO_OUTPUT_SET(2, 0);
}
state ^= 1;
}
void ICACHE_FLASH_ATTR user_init(void)
{
gpio_init();
uart_init(115200, 115200);
os_printf("SDK version:%s\n", system_get_sdk_version());
// Disable WiFi
wifi_set_opmode(NULL_MODE);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
os_timer_disarm(&ptimer);
os_timer_setfn(&ptimer, (os_timer_func_t *)blinky, NULL);
os_timer_arm(&ptimer, 2000, 1);
}
代码包含3个功能,但没有一个是设置或循环的。为ESP8266创建程序的基本原理是什么?入口是哪个功能?程序应保留什么功能?