Open arvind55555 opened 2 months ago
Incorrect use of semaphore. After taking a semaphore, you must give it.
while (1) {
xSemaphoreTake(dataReadySemaphore, portMAX_DELAY);
spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
xSemaphoreGive(dataReadySemaphore); // Signal that data is ready
}
while (1) {
xSemaphoreTake(dataReadySemaphore, portMAX_DELAY);
send_data_to_socket((char *)currentTxBuffer, HALF_BUFFER_SIZE);
xSemaphoreGive(dataReadySemaphore);
}
Stop PinnedToCore and lower the priority.
xTaskCreate(&hello_task_core_0, "core0_task", 8192, NULL, 2, NULL):
xTaskCreate(&hello_task_core_1, "core1_task", 8192, NULL, 2, NULL);
@nopnop2002 thank for your help
@arvind55555 feel free to reopen
Hi @nopnop2002 , thank you for your help, i tried implementing what you have said, but it is showing an error that "TCP: Connection to server failed. Error code: 118". i am pretty sure my server_ip and server_port works fine. i double checked everything. but i am still not sure what seems to be the issue. i have also attached a picture of my terminal for your reference.
just for your reference i have also added how i have implemented my code below
// core_1
void hello_task_core_0(void *pvParameter) {
spi_bus_config_t buscfg = {
.mosi_io_num = GPIO_MOSI,
.miso_io_num = GPIO_MISO,
.sclk_io_num = GPIO_SCLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
spi_slave_interface_config_t slvcfg = {
.mode = 0,
.spics_io_num = GPIO_CS,
.queue_size = 3,
.flags = 0,
};
spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO);
spi_slave_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = HALF_BUFFER_SIZE;
t.rx_buffer = (void *)currentRxBuffer;
t.tx_buffer = (void *)currentTxBuffer;
while (1) {
xSemaphoreTake(dataReadySemaphore, portMAX_DELAY);
spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
xSemaphoreGive(dataReadySemaphore); // Signal that data is ready
vTaskDelay(pdMS_TO_TICKS(1));
}
}
//core_2
void hello_task_core_1(void *pvParameter) {
while (1) {
xSemaphoreTake(dataReadySemaphore, portMAX_DELAY);
send_data_to_socket((char *)currentTxBuffer, HALF_BUFFER_SIZE);
xSemaphoreGive(dataReadySemaphore);
volatile uint32_t *temp = currentTxBuffer;
currentTxBuffer = currentRxBuffer;
currentRxBuffer = temp;
}
}
@hansw123 i would like to reopen this issue
try with the steps below.
1.Test only hello_task_core_1 without using semaphores.
@arvind55555 From the log, it looks like the wifi connection is not yet complete when the tcp connection is made, so it will report a timeout 118, make sure that the tcp connection is made after the IP_EVENT_STA_GOT_IP event is complete, you can refer to the https://github.com/espressif/esp-idf/blob/master/examples/wifi/getting_started/station/main/station_example_main.c
static void event_handler(void arg, esp_event_base_t event_base, int32_t event_id, void event_data)
try with the steps below.
1.Test only hello_task_core_1 without using semaphores. 2. Test hello_task_core_0 and hello_task_core_1 without using semaphores. 3. Test hello_task_core_0 and hello_task_core_1 using semaphores.
hello @nopnop2002
i tried the way you wanted me to test and i have added screenshots for your reference.
1)
2)
3)
the first and second tests ended up giving me the same result with the Error code : 5 and when i did the test i am not able to connect to the tcp itself getting Error code : 118
@arvind55555 From the log, it looks like the wifi connection is not yet complete when the tcp connection is made, so it will report a timeout 118, make sure that the tcp connection is made after the IP_EVENT_STA_GOT_IP event is complete, you can refer to the https://github.com/espressif/esp-idf/blob/master/examples/wifi/getting_started/station/main/station_example_main.c
static void event_handler(void arg, esp_event_base_t event_base, int32_t event_id, void event_data)
hello @hansw123, i tried the way how you asked me to change by setting the tcp connection after the IP_EVENT_STA_GOT_IP event is complete. But even after that I was getting the same error
@arvind55555 from the latest log,still creat tcp connection before sta_got_ip_event and using semaphore ,you can refer https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
@arvind55555 @nopnop2002
2、nopnop2002 ‘ suggestion was to turn the binary semaphore into a mutual exclusion lock, but the original usage was not really incorrect
Answers checklist.
General issue report
Hi all, I am currently working on a SPI code that receives data from a transmitter, stores the data in a buffer, and then sends the data to a TCP socket via Wi-Fi. Additionally, I am using dual-core functionality, where one core takes care of SPI transactions and another core takes care of TCP transactions. The problem is that the code without dual-core setup works absolutely fine, but when I implement it with dual-core, I am facing the error "TCP: Failed to send data to the server." Error code: 5". Below is my code
the problem i think with this code is that
Here the sent_bytes is always < 0, but I am exactly not sure why, and I also think that due to the usage of different cores, one of the cores isn't able to access the buffer. If that were the case, I should be getting an error based on that, but I am getting a totally different error. I also think that it could be the way i have synchronized both the cores using semaphores but i am not sure how else to do it exactly. Can someone help me with this?