Code: Select all
void spi_slave_task(void *pvParameters)
{
esp_err_t ret;
spi_slave_transaction_t t;
memset(&t, 0, sizeof(t));
uint8_t *recvbuf = (uint8_t *)malloc(SPI_RX_MAX * sizeof(uint8_t));
if(recvbuf == NULL)
{
ESP_LOGE(SPI_SLAVE, "Receive buffer memory allocation failed!");
return;
}
while(1) {
memset(recvbuf, 0x00, SPI_RX_MAX);
//Set up a transaction of 128 bytes to send/receive
t.length=SPI_RX_MAX*8;
t.rx_buffer=recvbuf;
ret=spi_slave_transmit(RCV_HOST, &t, portMAX_DELAY);
UBaseType_t res = xRingbufferSend(buf_handle, recvbuf, t.trans_len/8, pdMS_TO_TICKS(10));
if (res != pdTRUE) {
ESP_LOGE(SPI_SLAVE, "Failed to send item\n");
}
}
}
void wifi_send_task(void *pvParameters)
{
while(1) {
size_t item_size;
char *item = (char *)xRingbufferReceiveUpTo(buf_handle, &item_size, pdMS_TO_TICKS(10), SPI_RX_MAX*2);
if (item != NULL) {
// 如果网络连接,通过网络发送数
if(tcp_is_connect == true) {
int written = send(tcp_sock, item, item_size, 0);
if (written < 0) {
ESP_LOGE(SPI_SLAVE, "Error occurred during sending: errno %d", errno);
}
}
vRingbufferReturnItem(buf_handle, (void *)item);
}
}
}
void spi_slave_task_init(void)
{
buf_handle = xRingbufferCreate(64*1024, RINGBUF_TYPE_BYTEBUF);
if (buf_handle == NULL) {
ESP_LOGE(SPI_SLAVE, "Failed to create ring buffer\n");
}
spi_slave_init();
xTaskCreate(spi_slave_task, "spi_slave", 2048, NULL, 5, NULL);
xTaskCreate(wifi_send_task, "wifi_send", 2048, NULL, 6, NULL);
}