Serial monitoring over freertos task and Messagebuffer

PollysCracker
Posts: 16
Joined: Fri Jul 19, 2024 11:08 pm

Serial monitoring over freertos task and Messagebuffer

Postby PollysCracker » Mon Feb 03, 2025 4:07 am

Who can correct me and clarify some things related to freertos?

1.) I want to make a freertos task to monitor incoming serial input, preferably the task should only run when a user is starting to enter input into the serial monitor, how can i realise this, for now i just added a vtaskdelay to keep checking for input, is there a more direct or better approach?

2.) I have a second task that requests some user input through that initial freertos serial monitor task, then the serial monitor task needs to forward the input to the User_Cmd1 task, there will be multiple tasks that will request this type of info from the serial terminal monitoring task. Is my approach(Messagebuffer) and execution(use of) correct and desirable as is?

3.) How do i detect wether the user pressed the backspace button? right now the backspace is counted as a char input, wich is not desirable.

4.) What is the correct way to call forth a task to execute only once when called upon? fe see the User_Cmd1 task.
- By including and enabling a bool to indicate the task may run else vtaskdelay it untill the bool is true, after execution set it back to false?
- Or by deleting the task and recalling xTaskCreatePinnedToCore for the task and delete it again after it ran?
- or by a different method?

5.) After we recieved our user input in the User_Cmd1 task, I delete the buffer but the handle to the buffer is not set to NULL so i manually set it to NULL but after that the rest of the function does not execute, i assume that is because i call vTaskDelete before any other message is printed, i guess vTaskDelete removes the entire task without first completing it, vTaskDelete is also not setting my handle to 0 so that i'm not able to restart the task for another go(in the app_main infinite loop), how can i delete a task and set its handle to NULL to indicate the task is not running and i can check for that in my code to see wether or not i should start that task again?

The code:

Code: Select all

#include "Arduino.h"
#include "HardwareSerial.h"
#include "esp32-hal.h"
#include "esp_log.h"
#include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "nvs_flash.h"
#include "portmacro.h"
#include "freertos/message_buffer.h"


#include <cstddef>
#include <cstring>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>


#define Max_Msg_Len 	256

static const char tag[] = "Testertje";

MessageBufferHandle_t xMsg1Buffer;
MessageBufferHandle_t xMsg2Buffer;

static TaskHandle_t hUser_Cmd1 = NULL;


extern "C" 
{
	void app_main(void);
}

static void User_Cmd1(void *arg)
{
	/* 	What is the correct way to call forth a task to execute only once when called upon? 
		By including and enabling a bool to indicate the task may run else vtaskdelay it untill the bool is true, after execution set it back to false
		Or by deleting the task and recalling xTaskCreatePinnedToCore for the task and delete it again after it ran?
	*/
	char *Msg = (char*)calloc(1, sizeof(char)*Max_Msg_Len);
	uint16_t rx_Length = 0;
	ESP_LOGI(tag, "User_Cmd1 task succesfully created");
	
	printf("\t\tGetting ready to write some stuff.. ");
    xMsg1Buffer = xMessageBufferCreate(Max_Msg_Len);
   	if( xMsg1Buffer == NULL )
	{
		ESP_LOGE(tag, "There was not enough heap memory space available to create the message buffer. (user_cmd1)");
	} else {
		ESP_LOGI(tag, "Successfully created our MSG Buffer");
		rx_Length = xMessageBufferReceive(xMsg1Buffer,Msg, Max_Msg_Len, portMAX_DELAY);
		if( rx_Length > 0 )
		{
			ESP_LOGI(tag, "User_Cmd1 successfully recieved a message from the buffer, we recieved %i bytes\n ", rx_Length);
			printf("\tThe recieved message:\n\t");
			printf("%s\n", Msg);
		} else ESP_LOGE(tag, "Did not recieve any bytes from our message buffer!");
		vMessageBufferDelete(xMsg1Buffer);
		ESP_LOGI(tag, "deleted the messagebuffer");
		if(xMsg1Buffer == NULL)	ESP_LOGI(tag, "Successfully deleted our message buffer");
		else {
			ESP_LOGE(tag, "Deleted MessageBuffer but handle is not NULL..");
			xMsg1Buffer = NULL;
			ESP_LOGI(tag, "Buffer handle is now: %p", xMsg1Buffer);
		}
	}
	free(Msg);
	vTaskDelete(hUser_Cmd1);
	hUser_Cmd1 = NULL;
	ESP_LOGI(tag, "Task should be deleted");
	ESP_LOGI(tag, "handle is set to %s (%p)", hUser_Cmd1==NULL?"NULL":"Not NULL", hUser_Cmd1);
}

static void Monitor_serial(void *arg)
{
	ESP_LOGI(tag, "Monitor serial task succesfully created");
	char Input;
	static uint16_t Length = 0;
	static uint16_t LengthSent = 0;
	static char Cmd[Max_Msg_Len];
	//bool Quit = false;
	// Whats the proper way to ignore this task and only come here as soon as something is being entered in the serial terminal? So that this task runs on the best efficiency?
	while(true) {
		if(Serial.available())
		{
			Input = Serial.read();
			/*
				How do i detect when the user used the backspace to delete a char? right now, when we press backspace this action is counted as a char input
			*/
			if(Input == '\n' || Input == '\r' || Length == Max_Msg_Len)	// end of message
			{
				if(Input == '\n') ESP_LOGI(tag, "Cmd end was init by char: /n");
				if(Input == '\r') ESP_LOGI(tag, "Cmd end was init by char: /r");
				if(Input != '\n' && Input != '\r') ESP_LOGI(tag, "Cmd end was init by Full Buffer(255)");
				Cmd[Length] = '\0';
				printf("\n");
				//Cmd[Length+1] = '\0';
				ESP_LOGI(tag, "total length of the cmd: %i, Entered Command:\t%s", Length, Cmd);
				if( strcmp(Cmd, "quit") == 0)
				{
					// Results in a reboot; console log: "abort() was called at PC 0x400877d3 on core 0"
					// break; results in the task not responding anymore but does not continue after breaking out the while loop so the message: Leaving infite loop never shows up
					ESP_LOGI(tag, "Entered quit, trying to break free out of the task..");
					return;
				}	
				// Check if we have a pending request for a message
				if( xMsg1Buffer != NULL )
				{
					if( xMessageBufferIsEmpty(xMsg1Buffer) == pdFALSE )
					{
						// For some reason the buffer is not empty, lets clear it first
						if( xMessageBufferReset(xMsg1Buffer) == pdFAIL )ESP_LOGE(tag, "Could not empty MessageBuffer (user_cmd1)");
					}
					if( xMessageBufferSpaceAvailable(xMsg1Buffer) < Length )	ESP_LOGE(tag, "Not enough space in Message Buffer to fit our message");
					else {
						LengthSent = xMessageBufferSend(xMsg1Buffer, &Cmd, Length, 0);
						if(LengthSent != Length)	ESP_LOGE(tag, "Did not send the full message for some reason, msg length: %i, length sent: %i", Length, LengthSent);
						else ESP_LOGI(tag, "Successfully sent the complete Cmd to User_Cmd1 task, Msg Length: %i", Length);
					}
				} else if( xMsg2Buffer != NULL) {}
				Length = 0;
				LengthSent = 0;
				Cmd[Length] = '\0';
				
			} else  {			// fill buffer, msg ongoing, add null terminator for string print
				printf("%c", Input);
				Cmd[Length] = Input;
				Length++;
			}
			//vTaskDelay(50/portTICK_PERIOD_MS);
		} else vTaskDelay(500 / portTICK_PERIOD_MS);
	}
	printf("Leaving infite loop for created task Monitor_serial, deleting..");
	vTaskDelete(NULL);
}

void app_main(void)
{
	esp_err_t err = nvs_flash_init();
	if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
	{
		ESP_ERROR_CHECK(nvs_flash_erase());
		err = nvs_flash_init();
	}
	if(err != ESP_OK)
	{
		ESP_LOGD(tag, "Error (%s) while initializing memory, can not proceed", esp_err_to_name(err));
	}
	err = esp_timer_early_init();
	if(err != ESP_OK) ESP_LOGE(tag, "Could not early initiate timer.h library (%i).", err);
	
	initArduino();
	Serial.begin(115200);
	
	BaseType_t result = pdFAIL;
	result = xTaskCreatePinnedToCore(Monitor_serial, "Comm-Check", 4096, NULL, 10, NULL, tskNO_AFFINITY);
	if(result != pdPASS)	ESP_LOGE(tag, "Failed to create the serial monitor task");
	result = xTaskCreatePinnedToCore(User_Cmd1, "usrinput1", 4096, NULL, 10, &hUser_Cmd1, tskNO_AFFINITY);
	if(result != pdPASS)	ESP_LOGE(tag, "Failed to create the sUser_Cmd1 task");
	printf("Hello from app_main!\nEnter something:");
	while (true) {
        if( hUser_Cmd1 == NULL )
        { 
			ESP_LOGI(tag, "User_Cmd1 task finished, recalling in 2 seconds..");
			vTaskDelay(2000/portTICK_PERIOD_MS);
			result = xTaskCreatePinnedToCore(User_Cmd1, "User input menu1", 4096, NULL, 10, &hUser_Cmd1, tskNO_AFFINITY);
			if(result != pdPASS)	ESP_LOGE(tag, "Failed to create the sUser_Cmd1 task");
		}
		
        vTaskDelay(2000/portTICK_PERIOD_MS);
    }
}

Code: Select all

 //console output:
 I (276) heap_init: Initializing. RAM available for dynamic allocation:
I (283) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (289) heap_init: At 3FFB3310 len 0002CCF0 (179 KiB): DRAM
I (295) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (301) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (308) heap_init: At 4008EB24 len 000114DC (69 KiB): IRAM
I (316) spi_flash: detected chip: generic
I (319) spi_flash: flash io: dio
W (323) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (337) main_task: Started on CPU0
I (340) main_task: Calling app_main()
W (354) �ysI (357) Testertje: Monitor serial task succesfully created
I (358) Testertje: User_Cmd1 task succesfully created
                Getting ready to write some stuff.. I (358) Testertje: Successfully created our MSG Buffer
Hello from app_main!
Enter something:qsdI (49858) Testertje: Cmd end was init by char: /r

I (49858) Testertje: total length of the cmd: 3, Entered Command:       qsd
I (49861) Testertje: Successfully sent the complete Cmd to User_Cmd1 task, Msg Length: 3
I (49861) Testertje: User_Cmd1 successfully recieved a message from the buffer, we recieved 3 bytes
 
        The recieved message:
        qsd
I (49882) Testertje: deleted the messagebuffer
E (49887) Testertje: Deleted MessageBuffer but handle is not NULL..
I (3894) Testertje: Buffer handle is now: 0x0
brokeI (60870) Testertje: Cmd end was init by char: /r

I (60870) Testertje: total length of the cmd: 5, Entered Command:       broke
10 backspacesI (82372) Testertje: Cmd end was init by char: /r

I (82372) Testertje: total length of the cmd: 24, Entered Com10 backspaces
5backspacesI (121877) Testertje: Cmd end was init by char: /r

I (121877) Testertje: total length of the cmd: 16, Entered Command:5backspaces
10backspacesI (138381) Testertje: Cmd end was init by char: /r

I (138381) Testertje: total length of the cmd: 22, Entered Com10backspaces
10 BackspacesI (155886) Testertje: Cmd end was init by char: /r

I (155886) Testertje: total length of the cmd: 23, Entered Com10 Backspaces
What is happening here??!I (175891) Testertje: Cmd end was init by char: /r

I (175891) Testertje: total length of the cmd: 25, Entered Command:     What is happening here??!
// entering 256 backspaces..
I (220896) Testertje: Cmd end was init by Full Buffer(255)

I (220902) Testertje: total length of the cmd: 256, Entered Command: 



ESP_Sprite
Posts: 9985
Joined: Thu Nov 26, 2015 4:08 am

Re: Serial monitoring over freertos task and Messagebuffer

Postby ESP_Sprite » Tue Feb 04, 2025 3:15 am

PollysCracker wrote:
Mon Feb 03, 2025 4:07 am
Who can correct me and clarify some things related to freertos?

1.) I want to make a freertos task to monitor incoming serial input, preferably the task should only run when a user is starting to enter input into the serial monitor, how can i realise this, for now i just added a vtaskdelay to keep checking for input, is there a more direct or better approach?
You'd use a blocking call to read from the uart; e.g. uart_read_bytes(). Generally, any call that takes a timeout is a blocking call; blocking calls stop the calling task from running until the event they wait for (or a timeout) happens.
2.) I have a second task that requests some user input through that initial freertos serial monitor task, then the serial monitor task needs to forward the input to the User_Cmd1 task, there will be multiple tasks that will request this type of info from the serial terminal monitoring task. Is my approach(Messagebuffer) and execution(use of) correct and desirable as is?
It depends on the structure of your application, but using a messagebuffer certainly does not sound like a bad approach.
3.) How do i detect wether the user pressed the backspace button? right now the backspace is counted as a char input, wich is not desirable.
Backspace is sent as a normal keypress; either ascii 8 or 127 (it depends on the terminal). You can detect that.
4.) What is the correct way to call forth a task to execute only once when called upon? fe see the User_Cmd1 task.
- By including and enabling a bool to indicate the task may run else vtaskdelay it untill the bool is true, after execution set it back to false?
- Or by deleting the task and recalling xTaskCreatePinnedToCore for the task and delete it again after it ran?
- or by a different method?
vTaskDelay + bool will work, but is suboptimal as it still uses some CPU time and adds latency (the time between the other task setting the bool and the vTaskDelay timing out). Deleting and creating tasks is fairly expensive, CPU-time wise, so you'd only want to do it if you're sure that task is only needed once every blue moon, e.g. if you need a task to factory-reset your device. (Also, you'd need to make sure 1 of these tasks is running at any time.)

I'd simply create a task and have it wait on a FreeRTOS semaphore (or queue, or messagebuffer; any of those work and you probably want to pick the one that fits your problem best). That's similar to your vTaskDelay+book approach, but doesn't have the downsides.
5.) After we recieved our user input in the User_Cmd1 task, I delete the buffer but the handle to the buffer is not set to NULL so i manually set it to NULL but after that the rest of the function does not execute, i assume that is because i call vTaskDelete before any other message is printed, i guess vTaskDelete removes the entire task without first completing it, vTaskDelete is also not setting my handle to 0 so that i'm not able to restart the task for another go(in the app_main infinite loop), how can i delete a task and set its handle to NULL to indicate the task is not running and i can check for that in my code to see wether or not i should start that task again?
vTaskDelete simply stops the task from running and cleans up the stuff intrinsic to a FreeRTOS task (so the stack and task control block). Any stack-allocated variables will disappear (and pointers to that will point to random space), anything else you need to clean up yourself before you delete the task.


In general, this is what I consider good practice with FreeRTOS tasks (but other peoples opinions may differ):

1. Create all the things that are used for inter-task communication (queues, message buffers, semaphores, ...) before any tasks are created. This way you get around the possibility that a task tries to use one of those before it's created.
2. Create all tasks your program needs in day-to-day use directly after. Doing 1+2 up front means the inter-task stuff at least is created in the same way every time, and tells you about out-of-memory issues early on.
3. Tasks should have an infinite loop where they wait on some sort of blocking call to do some work.

PollysCracker
Posts: 16
Joined: Fri Jul 19, 2024 11:08 pm

Re: Serial monitoring over freertos task and Messagebuffer

Postby PollysCracker » Tue Feb 04, 2025 4:47 am

Thank you very much for you insights and elaboration, very much appreciated.

I'll also be checking out the uart library soon to see if i can skip the arduino core, I'm only using it for now for their serial wrap around.
Backspace is sent as a normal keypress; either ascii 8 or 127 (it depends on the terminal). You can detect that.

How would that translate into capturing/recognizing that keypress. going of this info: https://en.wikipedia.org/wiki/Backspace ... 20alphabet.
could i do a check like this: | Input == '^H' | or could i do also a hex check: | Input == '0x08' | ?


for the last part, is it common practice to have a freertos task running that checks the details of each task, such as fe handling handles and setting them to NULL after they have already been freed by calling vTaskDelete or is it more desirable to ask the kernel for this type of info more efficiently in some way?

ESP_Sprite
Posts: 9985
Joined: Thu Nov 26, 2015 4:08 am

Re: Serial monitoring over freertos task and Messagebuffer

Postby ESP_Sprite » Tue Feb 04, 2025 1:39 pm

PollysCracker wrote:
Tue Feb 04, 2025 4:47 am
How would that translate into capturing/recognizing that keypress. going of this info: https://en.wikipedia.org/wiki/Backspace ... 20alphabet.
could i do a check like this: | Input == '^H' | or could i do also a hex check: | Input == '0x08' | ?
Almost correct: input == 0x08 (without the quotes).

for the last part, is it common practice to have a freertos task running that checks the details of each task, such as fe handling handles and setting them to NULL after they have already been freed by calling vTaskDelete or is it more desirable to ask the kernel for this type of info more efficiently in some way?
The kernel does not have that info, as the 'ownership' of some kind of object is more of an architectural concept than one you can actually pin down in code or data. Generally, tasks should only vTaskDelete() themselves (you can in theory delete a task from another task, but that's rarely advisable) and as such they 'know' when they are going to end. Just uninitialize/free any stuff owned by the task before that.

PollysCracker
Posts: 16
Joined: Fri Jul 19, 2024 11:08 pm

Re: Serial monitoring over freertos task and Messagebuffer

Postby PollysCracker » Thu Feb 13, 2025 1:14 pm

Thanks alot for the valuable knowledge Sprite
I'm having trouble with sending the message when the buffer is close to being full and i have no clue why that is, i have been trying all possible things and i just can't figure out why, best i can come up with is that the messagebuffer has 2 or 3 bytes of overhead but i can't find any info on that.

Can some1 see what i'm doing wrong?? Did i find a bug in the system?
The code:

Code: Select all

#include "Arduino.h"
#include "HardwareSerial.h"
#include "esp32-hal.h"
#include "esp_log.h"
#include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "nvs_flash.h"
#include "portmacro.h"
#include "freertos/message_buffer.h"


#include <cstddef>
#include <cstring>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>


#define Max_Msg_Len 	4

static const char tag[] = "Testertje";

MessageBufferHandle_t xMsg1Buffer;
MessageBufferHandle_t xMsg2Buffer;

static TaskHandle_t hUser_Cmd1 = NULL;


extern "C" 
{
	void app_main(void);
}

static void User_Cmd1(void *arg)
{
	printf("\n\nCreating task: User_Cmd1\n\n");
	char *Msg = (char*)calloc(1, sizeof(char)*Max_Msg_Len+1);
	uint16_t rx_Length = 0;
	ESP_LOGI(tag, "User_Cmd1 task succesfully created\n");
	printf("\t\tGetting ready to write some stuff..\n ");
	xMsg1Buffer = xMessageBufferCreate(Max_Msg_Len+1);
	if( xMsg1Buffer == NULL )
	{
		ESP_LOGE(tag, "There was not enough heap memory space available to create the message buffer. (user_cmd1)");
	}
	for(;;)
	{
		/*if( ulTaskNotifyTake(pdTRUE, portMAX_DELAY) > 0 )
		{
	  		if(!xMessageBufferIsEmpty(xMsg1Buffer))
	  		{
				  xMessageBufferReceive(xMsg1Buffer,Msg, Max_Msg_Len, 0);
				  ESP_LOGE(tag, "Messagebuffer was already in use somehow(not empty), has content: '%s'", Msg);
			}*/
	
		rx_Length = xMessageBufferReceive(xMsg1Buffer,Msg, Max_Msg_Len+1, portMAX_DELAY);
		if( rx_Length > 0 )
		{
			ESP_LOGI(tag, "User_Cmd1 successfully recieved a message from the buffer, we recieved %i bytes\n ", rx_Length);
			printf("\tThe recieved message:\n\t");
			printf("--->\t%s\t<---\n", Msg);
		} else ESP_LOGE(tag, "Did not recieve any bytes from our message buffer! Practicly impossible..");
		if( xMessageBufferReset(xMsg1Buffer) == pdFAIL)	ESP_LOGE(tag, "[User_Cmd1] Could not reset messagebuffer, is being blocked by another task while sending or reading from");
		
		memset(Msg, '\0', Max_Msg_Len+1);
		printf("Msg is now: '%s'\n", Msg);
		
		/*xMsg1Buffer = NULL;
		ESP_LOGI(tag, "deleted the messagebuffer");
		if(xMsg1Buffer == NULL)	ESP_LOGI(tag, "Successfully deleted our message buffer");
		else {
			ESP_LOGE(tag, "Deleted MessageBuffer but handle is not NULL..");
			xMsg1Buffer = NULL;
			ESP_LOGI(tag, "Buffer handle is now: %p", xMsg1Buffer);
		}*/
	
		//}
	}
	free(Msg);
	//vTaskDelete(hUser_Cmd1);
	//hUser_Cmd1 = NULL;
}

static void Monitor_serial(void *arg)
{
	ESP_LOGI(tag, "Monitor serial task succesfully created");
	char Input;
	static uint16_t Length = 0;
	static uint16_t LengthSent = 0;
	static char Cmd[Max_Msg_Len+1];

	
	for(;;) {
		if(Serial.available() && xMsg1Buffer != NULL)
		{
			Input = Serial.read();
			if(Input != 0x08 && Input != '\n' && Input != '\r' )
			{
				printf("%c", Input);
				Cmd[Length] = Input;
				Length++;
			} else if( Input == 0x08)
			{
				ESP_LOGI(tag, "recognized backspace");
				if( Length > 0)
				{
					Length--;
					Cmd[Length] = '\0';
					ESP_LOGI(tag, "removed a char from command line");
				}
				else ESP_LOGI(tag, "Nothing to remove..");
			}
			
			if(Input == '\n' || Input == '\r' || Length >= Max_Msg_Len)	// end of message
			{
				if(Input == '\n') ESP_LOGI(tag, "Cmd end was init by char: /n");
				if(Input == '\r') ESP_LOGI(tag, "Cmd end was init by char: /r");
				
				Cmd[Length+1] = '\0';
				printf("\n");
				if(Input != '\n' && Input != '\r') {
					ESP_LOGI(tag, "Cmd end was init by Full Buffer(%i)", Max_Msg_Len );
					//Length++;
				}
				printf("\n");
				ESP_LOGI(tag, "total length of the cmd: %i, Entered Command:\t%s", Length, Cmd);
					
				
				// (task completed, send notification)
				if( xMessageBufferIsEmpty(xMsg1Buffer) == pdFALSE )
				{
					// For some reason the buffer is not empty, lets clear it first
					ESP_LOGI(tag, "[Monitor_serial] Buffer was not empty, buffer is probably already in use again, this should not be able to happen");
					if( xMessageBufferReset(xMsg1Buffer) == pdFAIL )ESP_LOGE(tag, "Could not empty MessageBuffer (Monitor_serial)");
					else ESP_LOGI(tag, "Messagebuffer succesfully reset");
				}
				if( xMessageBufferSpaceAvailable(xMsg1Buffer) < Length )	ESP_LOGE(tag, "Not enough space in Message Buffer to fit our message");
				else {
					LengthSent = xMessageBufferSend(xMsg1Buffer, &Cmd, Length, 0);
					if(LengthSent != Length)
					{
						ESP_LOGE(tag, "Did not send the full message for some reason, msg length: %i, length sent: %i, buffer size: %i", Length, LengthSent, Max_Msg_Len+1);
						ESP_LOGI(tag, "The message was: '%s'; trying to send again with max delay", Cmd);
						LengthSent = xMessageBufferSend(xMsg1Buffer, &Cmd, Length, portMAX_DELAY);
						if( LengthSent != Length)	ESP_LOGE(tag, "FAIL!");
					}
					else ESP_LOGI(tag, "Successfully sent the complete Cmd to User_Cmd1 task, Msg Length: %i", Length);
				}
				//if( xMsg2Buffer != NULL) {}
				Length = 0;
				LengthSent = 0;
				memset(Cmd, '\0', sizeof(Cmd));
				
			} 
		} 
	}
	printf("Leaving infite loop for created task Monitor_serial, deleting..");
	vTaskDelete(NULL);
}

void app_main(void)
{
	esp_err_t err = nvs_flash_init();
	if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
	{
		ESP_ERROR_CHECK(nvs_flash_erase());
		err = nvs_flash_init();
	}
	if(err != ESP_OK)
	{
		ESP_LOGD(tag, "Error (%s) while initializing memory, can not proceed", esp_err_to_name(err));
	}
	err = esp_timer_early_init();
	if(err != ESP_OK) ESP_LOGE(tag, "Could not early initiate timer.h library (%i).", err);
	
	initArduino();
	Serial.begin(115200);
	while(!Serial){}
	
	BaseType_t result = pdFAIL;
	result = xTaskCreatePinnedToCore(User_Cmd1, "User_Cmd1", 4096, NULL, 10, &hUser_Cmd1, tskNO_AFFINITY);
	if(result != pdPASS)	ESP_LOGE(tag, "Failed to create the sUser_Cmd1 task");
	result = xTaskCreatePinnedToCore(Monitor_serial, "Monitor-Serial", 4096, NULL, 10, NULL, tskNO_AFFINITY);
	if(result != pdPASS)	ESP_LOGE(tag, "Failed to create the serial monitor task");
	printf("\n\nHello from app_main!\nEnter something:\n\n");
	while (true) {
      
    }
}

the logs:

Code: Select all

I (31) boot: ESP-IDF GIT-NOTFOUND 2nd stage bootloader
I (31) boot: compile time Feb  1 2025 04:22:02
I (31) boot: Multicore bootloader
I (36) boot: chip revision: v3.1
I (40) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 2MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=0cb10h ( 51984) map
I (123) esp_image: segment 1: paddr=0001cb38 vaddr=3ffb0000 size=02480h (  9344) load
I (126) esp_image: segment 2: paddr=0001efc0 vaddr=40080000 size=01058h (  4184) load
I (130) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=1fd5ch (130396) map
I (182) esp_image: segment 4: paddr=0003fd84 vaddr=40081058 size=0da94h ( 55956) load
I (211) boot: Loaded app from partition at offset 0x10000
I (211) boot: Disabling RNG early entropy source...
I (223) cpu_start: Multicore app
I (231) cpu_start: Pro cpu start user code
I (231) cpu_start: cpu freq: 160000000 Hz
I (232) app_init: Application information:
I (234) app_init: Project name:     app-template
I (240) app_init: App version:      1
I (244) app_init: Compile time:     Feb  1 2025 04:21:53
I (250) app_init: ELF file SHA256:  e228e53d7...
I (255) app_init: ESP-IDF:          HEAD-HASH-NOTFOUND
I (261) efuse_init: Min chip rev:     v0.0
I (266) efuse_init: Max chip rev:     v3.99 
I (271) efuse_init: Chip rev:         v3.1
I (276) heap_init: Initializing. RAM available for dynamic allocation:
I (283) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (289) heap_init: At 3FFB3218 len 0002CDE8 (179 KiB): DRAM
I (295) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (302) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (308) heap_init: At 4008EAEC len 00011514 (69 KiB): IRAM
I (316) spi_flash: detected chip: generic
I (319) spi_flash: flash io: dio
W (323) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (337) main_task: Started on CPU0
I (340) main_task: Calling app_main()
W (354) �ys

Creating task: User_Cmd1

I (358) Testertje: User_Cmd1 task succesfully created

                Getting ready to write some stuff..
 I (359) Testertje: Monitor serial task succesfully created
1I (10821) Testertje: Cmd end was init by char: /r


I (10821) Testertje: total length of the cmd: 1, Entered Command:       1
I (10822) Testertje: Successfully sent the complete Cmd to User_Cmd1 task, Msg Length: 1
I (10822) Testertje: User_Cmd1 successfully recieved a message from the buffer, we recieved 1 bytes
 
        The recieved message:
        --->    1       <---
Msg is now: ''
12I (18383) Testertje: Cmd end was init by char: /r


I (18383) Testertje: total length of the cmd: 2, Entered Command:       12
E (18385) Testertje: Did not send the full message for some reason, msg length: 2, length sent: 0, buffer size: 5
I (18396) Testertje: The message was: '12'; trying to send again with max delay
E (18404) Testertje: FAIL!
1I (47001) Testertje: Cmd end was init by char: /r


I (47001) Testertje: total length of the cmd: 1, Entered Command:       1
I (47003) Testertje: Successfully sent the complete Cmd to User_Cmd1 task, Msg Length: 1
I (47003) Testertje: User_Cmd1 successfully recieved a message from the buffer, we recieved 1 bytes
 
        The recieved message:
        --->    1       <---
Msg is now: ''
12I (51837) Testertje: Cmd end was init by char: /r


I (51838) Testertje: total length of the cmd: 2, Entered Command:       12
E (51839) Testertje: Did not send the full message for some reason, msg length: 2, length sent: 0, buffer size: 5
I (51850) Testertje: The message was: '12'; trying to send again with max delay
E (51858) Testertje: FAIL!
123I (55670) Testertje: Cmd end was init by char: /r


I (55671) Testertje: total length of the cmd: 3, Entered Command:       123
E (55672) Testertje: Did not send the full message for some reason, msg length: 3, length sent: 0, buffer size: 5
I (55683) Testertje: The message was: '123'; trying to send again with max delay
E (55691) Testertje: FAIL!
1234
I (64541) Testertje: Cmd end was init by Full Buffer(4)

I (64541) Testertje: total length of the cmd: 4, Entered Command:       1234
E (64544) Testertje: Did not send the full message for some reason, msg length: 4, length sent: 0, buffer size: 5
I (64555) Testertje: The message was: '1234'; trying to send again with max delay
E (64563) Testertje: FAIL!
EDIT!:
omg never mind, i just saw i overlooked the 4 bytes in the docs ( https://freertos.org/Documentation/02-K ... BufferSend -xDataLengthBytes)
Yup that did it!

Who is online

Users browsing this forum: No registered users and 90 guests