esp32 && 8266 freertos Task与timer,ringbuffer入门练习

  1. 自定义一一个 timer 和 task,timer 每秒向 ringbuf 写 入字符串
    “abcdefg\r\n”, task 从 ringbuf 取出数据,并打印。(ringbuf 采用用其中一一个即可)
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include "rom/ets_sys.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include <assert.h>
#include "esp_partition.h"
#include "esp_log.h"
#include "freertos/ringbuf.h"
RingbufHandle_t buf_handle;
#define mainAUTO_RELOAD_TIMER_PERIOD  pdMS_TO_TICKS( 1000 )
static void prvAutoReloadTimerCallback(TimerHandle_t xTimer)
{
  printf("prvAutoReloadTimerCallback\n");
  static char tx_item[] = "abcdefg/r/n";
  //Send an item
  UBaseType_t res =  xRingbufferSend(buf_handle, tx_item, sizeof(tx_item), pdMS_TO_TICKS(1000));
  if (res != pdTRUE) {
      printf("Failed to send item\n");
  }
}

void vTask1( void *pvParameters )
{
  for(;;){
    size_t item_size;
    printf("\r\n=%p\r\n",buf_handle);
    char *item = (char *)xRingbufferReceive(buf_handle, &item_size, pdMS_TO_TICKS(1000));
    //printf("===%p",item);
    //printf("===%08x",item);
    //Check received item
    if (item != NULL) {
        //Print item
        for (int i = 0; i < item_size; i++) {
            printf("%c", item[i]);
        }
        printf("\n");
        //Return Item
        vRingbufferReturnItem(buf_handle, (void *)item);
    } else {
        //Failed to receive item
          printf("Failed to receive item\n");
    }
  }
      vTaskDelete(NULL);
}
void app_main()
{
  printf("app_main\r\n");
    TimerHandle_t xAutoReloadTimer;
    BaseType_t xTimerStarted;
    buf_handle = xRingbufferCreate(1028, RINGBUF_TYPE_NOSPLIT);
    // printf("\r\n===0x%08x\r\n",buf_handle);
    //printf("*value=%p\r\n", value);
    if (buf_handle == NULL) {
      printf("Failed to create ring buffer\n");
    }
    printf("\r\n===0x%p\r\n",buf_handle);
    xTaskCreate(vTask1,"Task1",4096, NULL,5,NULL);
    xAutoReloadTimer=xTimerCreate("AutoReload",mainAUTO_RELOAD_TIMER_PERIOD,pdTRUE,0,prvAutoReloadTimerCallback);
    if(  ( xAutoReloadTimer != NULL ) ){
      xTimerStarted = xTimerStart( xAutoReloadTimer, 0 );
      }
}

你可能感兴趣的:(嵌入式)