Skip to content

Commit

Permalink
select background task core during init
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Jan 11, 2023
1 parent 49e5f2e commit 9fd8eac
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
5 changes: 0 additions & 5 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ config ESP_MQTT_TASK_PRIORITY
depends on ESP_MQTT_ENABLED
default 5

config ESP_MQTT_TASK_CORE
int "MQTT background process task core"
depends on ESP_MQTT_ENABLED
default 1

config ESP_MQTT_EVENT_QUEUE_SIZE
int "MQTT event queue size"
depends on ESP_MQTT_ENABLED
Expand Down
8 changes: 6 additions & 2 deletions esp_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static esp_lwmqtt_timer_t esp_mqtt_timer1, esp_mqtt_timer2;

static void *esp_mqtt_write_buffer;
static void *esp_mqtt_read_buffer;
static int esp_mqtt_core;

static QueueHandle_t esp_mqtt_event_queue = NULL;

Expand All @@ -80,12 +81,15 @@ typedef struct {
} esp_mqtt_event_t;

void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t mcb, size_t buffer_size,
int command_timeout) {
int command_timeout, int core) {
// set callbacks
esp_mqtt_status_callback = scb;
esp_mqtt_message_callback = mcb;

// set settings
esp_mqtt_buffer_size = buffer_size;
esp_mqtt_command_timeout = (uint32_t)command_timeout;
esp_mqtt_core = core;

// allocate buffers
esp_mqtt_write_buffer = malloc((size_t)buffer_size);
Expand Down Expand Up @@ -553,7 +557,7 @@ bool esp_mqtt_start(const char *host, const char *port, const char *client_id, c
// create mqtt thread
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_start: create task");
BaseType_t ret = xTaskCreatePinnedToCore(esp_mqtt_run, "esp_mqtt", CONFIG_ESP_MQTT_TASK_STACK, NULL,
CONFIG_ESP_MQTT_TASK_PRIORITY, &esp_mqtt_task, CONFIG_ESP_MQTT_TASK_CORE);
CONFIG_ESP_MQTT_TASK_PRIORITY, &esp_mqtt_task, esp_mqtt_core);
if (ret != pdPASS) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_start: failed to create task");
ESP_MQTT_UNLOCK_MAIN();
Expand Down
3 changes: 2 additions & 1 deletion esp_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ typedef void (*esp_mqtt_message_callback_t)(const char *topic, const uint8_t *pa
* @param mcb - The message callback.
* @param buffer_size - The read and write buffer size.
* @param command_timeout - The command timeout.
* @param core - The core to run the background task on.
*/
void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t mcb, size_t buffer_size,
int command_timeout);
int command_timeout, int core);

/**
* Configure TLS connection.
Expand Down
2 changes: 1 addition & 1 deletion test/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void app_main() {
ESP_ERROR_CHECK(esp_wifi_start());

// initialize mqtt
esp_mqtt_init(status_callback, message_callback, 256, 2000);
esp_mqtt_init(status_callback, message_callback, 256, 2000, 1);

// create tasks
xTaskCreatePinnedToCore(process, "process", 2048, NULL, 10, NULL, 1);
Expand Down
1 change: 0 additions & 1 deletion test/sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,6 @@ CONFIG_WPA_MBEDTLS_CRYPTO=y
CONFIG_ESP_MQTT_ENABLED=y
CONFIG_ESP_MQTT_TASK_STACK=9216
CONFIG_ESP_MQTT_TASK_PRIORITY=5
CONFIG_ESP_MQTT_TASK_CORE=1
CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE=64
CONFIG_ESP_MQTT_TLS_ENABLE=y
# end of esp-mqtt
Expand Down

0 comments on commit 9fd8eac

Please sign in to comment.