espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
12.57k stars 7.02k forks source link

my buffer is not filling up (IDFGH-12691) #13681

Open arvind55555 opened 3 weeks ago

arvind55555 commented 3 weeks ago

Answers checklist.

General issue report

hi all, i am currently working on a ESP-IDF reciever SPI code, where it receives data from transmitter, stores in a buffer and sends it to a external TCP socket via Wi-Fi. everything in this code works fine but, the problem i am facing is that for some reason my buffer is never filling up and i want to know why. below is my full code:

#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"

#include "lwip/dns.h"
#include "lwip/igmp.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"

#include "driver/gpio.h"
#include "driver/ledc.h"
#include "driver/spi_slave.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "soc/rtc_periph.h"
#include "spi_flash_mmap.h"

// WiFi configuration
const char *ssid = "**";
const char *pass = "**";

// Server configuration (replace with your server details)
#define SERVER_IP "**"
#define SERVER_PORT ***

// Pins in use
#define GPIO_MOSI 23
#define GPIO_MISO 19
#define GPIO_SCLK 18
#define GPIO_CS 5

// Buffer size
#define BUFFER_SIZE 16384 // 16K buffer size
#define HALF_BUFFER_SIZE (BUFFER_SIZE / 2)

// Socket variable
int sock = -1;

// Buffers for double buffering
uint32_t buffer1[BUFFER_SIZE];
uint32_t buffer2[BUFFER_SIZE];

// Pointers to the current buffers being filled and transmitted
volatile uint32_t *currentTxBuffer;
volatile uint32_t *currentRxBuffer;

// Function to initialize the socket
void initialize_socket() {
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    ESP_LOGE("TCP", "Unable to create socket");
    return;
  }

  struct sockaddr_in server_address;
  memset(&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(SERVER_PORT);
  inet_pton(AF_INET, SERVER_IP, &(server_address.sin_addr));

  if (connect(sock, (struct sockaddr *)&server_address,
              sizeof(server_address)) < 0) {
    ESP_LOGE("TCP", "Connection to server failed. Error code: %d", errno);
    close(sock);
    sock = -1;
    return;
  }

  ESP_LOGI("TCP", "Socket initialized successfully");
}

// Function to send data to the socket
void send_data_to_socket(const char *data, size_t data_length) {
  if (sock < 0) {
    // Socket not initialized, attempt to initialize
    ESP_LOGI("TCP", "Socket not initialized, attempting to initialize...");
    initialize_socket();
    if (sock < 0) {
      ESP_LOGE("TCP", "Socket initialization failed, cannot send data");
      return;
    }
  }

  // Send data to the server as binary
  int sent_bytes = send(sock, data, data_length, 0);
  if (sent_bytes < 0) {
    ESP_LOGE("TCP", "Failed to send data to server. Error code: %d", errno);
  } else {
    ESP_LOGI("TCP", "Sent %d bytes to server", sent_bytes);
  }
}

// Function to close the socket
void close_socket() {
  if (sock >= 0) {
    close(sock);
    sock = -1;
    ESP_LOGI("TCP", "Socket closed");
  }
}

// WiFi event handler
static void wifi_event_handler(void *event_handler_arg,
                               esp_event_base_t event_base, int32_t event_id,
                               void *event_data) {
  if (event_id == WIFI_EVENT_STA_START) {

  } else if (event_id == WIFI_EVENT_STA_CONNECTED) {

  } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {

    esp_wifi_connect();
  } else if (event_id == IP_EVENT_STA_GOT_IP) {
  }
}

// WiFi connection setup
void wifi_connection() {
  esp_netif_init();
  esp_event_loop_create_default();
  esp_netif_create_default_wifi_sta();
  wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT();
  esp_wifi_init(&wifi_initiation);
  esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler,
                             NULL);
  esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler,
                             NULL);
  wifi_config_t wifi_configuration = {.sta = {
                                          .ssid = "**",
                                          .password = "**",
                                      }};
  strcpy((char *)wifi_configuration.sta.ssid, ssid);
  strcpy((char *)wifi_configuration.sta.password, pass);
  esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration);
  esp_wifi_start();
  esp_wifi_set_mode(WIFI_MODE_STA);
  esp_wifi_connect();
  printf("wifi_init_softap finished. SSID:%s  password:%s", ssid, pass);
}

// Function to print the contents of a buffer
void print_buffer(const char *label, const uint32_t *buffer,
                  size_t buffer_length) {
  printf("%s: ", label);
  for (size_t i = 0; i < buffer_length; i++) {
    printf("%ld ", buffer[i]);
  }
  printf("\n");
}

void app_main(void) {

  // Set current buffers to buffer1
  currentTxBuffer = buffer1;
  currentRxBuffer = buffer2;

  nvs_flash_init();
  wifi_connection();
  initialize_socket(); // Initialize the socket

  // Configuration for the SPI bus
  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,
  };

  // Configuration for the SPI slave interface
  spi_slave_interface_config_t slvcfg = {
      .mode = 0,
      .spics_io_num = GPIO_CS,
      .queue_size = 3,
      .flags = 0,
  };

  // Initialize SPI slave interface
  spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO);

  spi_slave_transaction_t t; // Transaction descriptor

  // Prepare the transaction descriptor
  memset(&t, 0, sizeof(t));
  t.length =
      HALF_BUFFER_SIZE; // Set the length of data to be received/transmitted
  t.rx_buffer = (void *)currentRxBuffer; // Set the receive buffer
  t.tx_buffer = (void *)currentTxBuffer; // Set the transmit buffer

  while (1) {
    // Perform the SPI transaction
    spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);

    // Print the contents of the currentTxBuffer
    print_buffer("Tx Buffer", currentTxBuffer, HALF_BUFFER_SIZE);

    // Send data from the filled buffer to the TCP server
    send_data_to_socket((char *)currentTxBuffer, HALF_BUFFER_SIZE);

    // Swap the buffers for next transaction
    volatile uint32_t *temp = currentTxBuffer;
    currentTxBuffer = currentRxBuffer;
    currentRxBuffer = temp;

    // Print the contents of the currentTxBuffer after swapping
    print_buffer("Rx Buffer", currentRxBuffer, HALF_BUFFER_SIZE);

    // Add a delay or yield to prevent stack overflow
    // vTaskDelay(pdMS_TO_TICKS(1)); // Delay for 1 millisecond
  }
  // Close the socket before exiting the loop
  close_socket();
}

below you can see the printed outputs that i am receivinng from the transmitter. i am transmitted 8 kilobyte worth of data through SPI but for some reason the buffer only fills 1/3rd and rest all goes to zero, like shown below (note that i am printing it as a signed and also only copied the printed output upto to a certain limit. i actually got lot of zeros till my buffer got full)

-1082525832 1953966715 -1988321407 -1451638638 -75825772 930755735 -1535554153 -1434758249 -495218282 -1434695785 343561623 -2139496554 -2005199209 -142904170 712785302 -1468414570 1048286613 897331095 -1988539753 1434148503 545036950 2038037142 1736130198 1149023126 -1585863018 -1568977770 1249694101 -2139527273 1903910550 561780887 679072662 326849687 -1602367110 762795387 1903940993 -75762286 -713332331 561777303 695849622 -377872746 -1569074027 1752909206 276616598 578379671 343638422 -1854205289 -1250222441 561831830 1098487444 695970965 -1334100586 276617622 142172054 -931465579 -1434880618 1434131862 -92537963 1769778327 1434056086 1115385751 -293901929 1501335703 1920589206 1115371927 1518721665 -175440243 1351608468 -1349473386 -611374442 1301401238 512831127 -275714154 1603291030 1536218774 1569752470 1804664726 1435530391 1083199383 143665303 -896577641 -644830827 -812626537 -812613737 -1483669610 -1047493738 -1903113066 1586552726 1469079447 1536196501 1167084950 714115478 546365590 395321239 596702102 378558359 361824150 -445131657 -1955020932 -1770498689 1685693570 -897950319 41567382 1803115926 1971041686 -92545643 -1786999401 -1082556521 -931422313 -1535327081 -344334441 -1283731305 981262231 1920540053 -1233409642 -1870898539 -176614761 92053141 897184406 1350294166 830241430 -1451671403 -1971609706 1350309270 -1267096170 -696542314 -2005122922 -1468430954 1652290965 -175471222 1452024464 -342963308 -1517239146 -544243562 1217464471 -7278698 -191856745 -1466894186 2005964695 -1886339177 1871769494 1922086807 1435564439 445697943 345030039 59810709 -1081102442 1334914967 798051222 395419541 730944406 -175073643 2140155031 -1752138091 1603297174 1167120022 1418737303 1284538006 647030422 -141589098 1100072854 -1350522753 1602879357 1334776962 -409970798 630232980 1418769558 -1886326890 -1886326891 1116762006 1334855831 -1701819241 -661619305 764538263 1603361431 1435525015 2140160151 1150309524 -1785703018 -1617898091 43081623 361862038 747746454 1066476951 1754354582 -1533946730 -1483647081 -1752082538 -1148067947 -544121449 613598871 378606743 898708887 645320823 1500964732 360363391 1970920834 -8721519 1132115350 -2005277290 1467698582 -1602528363 293355671 1752784791 -1451527017 1098678679 1887006614 -1418020714 -226929001 1534799253 1501351318 -1585881705 1853595542 695941014 -629410667 1434240406 -1871052906 -1434763113 1434228630 1903775382 -2139375466 897319831 -2072380778 -1250196841 930902679 797821835 -762571375 1569670549 -1617888618 -544219242 663859095 328311703 -359617642 -2104449643 -1919947370 -560967786 -963575914 -2087697515 395320725 294658964 -896536683 546377108 294769044 -1081024619 2140176279 -1383119721 43079575 -2054135913 1116766102 1100017045 160459671 -930133865 9535383 -1064297833 1989165719 730912662 -712041065 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

i am pretty sure it is the way i am using the two buffer but i am not sure how exactly to implement it, can anyone help me in this?

wanckl commented 3 weeks ago

@arvind55555

Please note t.length is in BIT not BYTE

arvind55555 commented 2 weeks ago

@arvind55555

Please note t.length is in BIT not BYTE

Yeah I'm receiving 2048 Kilobits data. So if that's the case why am I seeing zeros?

kayzen9 commented 10 hours ago

@arvind55555 Try to multiply length by 8 - convert to bytes... that shud fix the problem