Last active
July 2, 2022 21:57
-
-
Save SpComb/909d20847645aa22361cf4d3e8bc3700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <esp_log.h> | |
#include <esp_err.h> | |
#include <driver/gpio.h> | |
#include <esp32/rom/ets_sys.h> | |
#include <freertos/FreeRTOS.h> | |
#include <freertos/event_groups.h> | |
#include <freertos/semphr.h> | |
#include <freertos/task.h> | |
#define RANDOM_SEED 0x1337 | |
#define WAIT_TICKS 1 | |
#define BLINK_BIT 0x1 | |
#define SYNC_BIT 0x2 | |
#define BLINK_GPIO GPIO_NUM_2 | |
#define BLINK_ON 0 | |
#define BLINK_OFF 1 | |
static const char* TAG = "GH8336"; | |
TaskHandle_t task0, task1; | |
EventGroupHandle_t event_group; | |
SemaphoreHandle_t sync_semphr; | |
void init_gpio() | |
{ | |
gpio_config_t config = { | |
.pin_bit_mask = (1 << BLINK_GPIO), | |
.mode = GPIO_MODE_OUTPUT, | |
.pull_up_en = GPIO_PULLUP_ENABLE, | |
}; | |
ESP_ERROR_CHECK(gpio_config(&config)); | |
} | |
void task0_main() | |
{ | |
for (;;) { | |
EventBits_t bits = xEventGroupWaitBits(event_group, BLINK_BIT | SYNC_BIT, pdTRUE, pdFALSE, WAIT_TICKS); | |
if (bits & BLINK_BIT) { | |
gpio_set_level(BLINK_GPIO, BLINK_ON); | |
} else { | |
gpio_set_level(BLINK_GPIO, BLINK_OFF); | |
} | |
if (bits & SYNC_BIT) { | |
if (!xSemaphoreGive(sync_semphr)) { | |
ESP_LOGE(TAG, "xSemaphoreGive"); | |
abort(); | |
} | |
} | |
} | |
} | |
void task1_main() | |
{ | |
int count = 0, total_sleep = 0, total_delay = 0, sync_count = 0; | |
for (;;) { | |
if (++count % 1000 == 0) { | |
ESP_LOGI(TAG, "[%04d] sleep=%fms delay=%fus sync=%d", | |
count, | |
(float)total_sleep / (float)portTICK_RATE_MS, | |
(float)total_delay, | |
sync_count | |
); | |
} | |
if (rand() % 100) { | |
int delay = rand() % 1000; | |
ets_delay_us(delay); | |
total_delay += delay; | |
} else { | |
int sleep = rand() % 10; | |
vTaskDelay(sleep); | |
total_sleep += sleep; | |
} | |
EventBits_t bits = BLINK_BIT; | |
if (xSemaphoreTake(sync_semphr, 0)) { | |
sync_count++; | |
bits |= SYNC_BIT; | |
} | |
xEventGroupSetBits(event_group, bits); | |
} | |
} | |
void app_main(void) | |
{ | |
srand(RANDOM_SEED); | |
init_gpio(); | |
if (!(event_group = xEventGroupCreate())) { | |
ESP_LOGE(TAG, "xEventGroupCreate"); | |
abort(); | |
} | |
if (!(sync_semphr = xSemaphoreCreateBinary())) { | |
ESP_LOGE(TAG, "xSemaphoreCreateBinary"); | |
abort(); | |
} | |
xSemaphoreGive(sync_semphr); | |
if (!xTaskCreatePinnedToCore(task0_main, "task0", 2048, NULL, 10, &task0, 0)) { | |
ESP_LOGE(TAG, "xTaskCreatePinnedToCore"); | |
abort(); | |
} | |
if (!xTaskCreatePinnedToCore(task1_main, "task1", 2048, NULL, 10, &task1, 1)) { | |
ESP_LOGE(TAG, "xTaskCreatePinnedToCore"); | |
abort(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment