Page 330

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include <unistd.h>
#include "driver/uart.h"

void delay_us(int t)
{
    usleep(t);
}

void app_main(void)
{
    uart_driver_install(UART_NUM_2, 1024, 1024, 0, NULL, 0);
    uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT};
    uart_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, 1, 2, -1, -1);

    char SendData[] = "Hello World";
    uart_flush(UART_NUM_2);
    uart_write_bytes(UART_NUM_2, SendData, sizeof(SendData));
    char RecData[100];
    int n = uart_read_bytes(UART_NUM_2, RecData,
                            sizeof(SendData), 3);
    RecData[n + 1] = 0;
    printf("%s\n", RecData);
    printf("%d    %d\n", n, sizeof(SendData));
}

Page 333

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/uart.h"

#include "driver/gptimer.h"

gptimer_handle_t tick_us_start(void)
{
    gptimer_config_t timer_config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,
        .direction = GPTIMER_COUNT_UP,
        .resolution_hz = 1000000,
    };
    gptimer_handle_t gptimer_us = NULL;
    gptimer_new_timer(&timer_config, &gptimer_us);
    gptimer_enable(gptimer_us);
    gptimer_start(gptimer_us);
    return gptimer_us;
}

int64_t tick_us(gptimer_handle_t gptimer_us, int64_t offset)
{
    uint64_t count;
    gptimer_get_raw_count(gptimer_us, &count);
    return count + offset;
}

void app_main(void)
{
    uart_driver_install(UART_NUM_2, 1024, 256, 0, NULL, 0);
    uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT};
    uart_param_config(UART_NUM_2, &uart_config);

    uart_set_pin(UART_NUM_2, 1, 2, -1, -1);

    int n = 252;
    char SendData[n];
    for (int i = 0; i < n; i++)
    {
        SendData[i] = 'A';
    }
    SendData[n - 1] = 0;

    gptimer_handle_t th = tick_us_start();
    int64_t t = tick_us(th, 0);
    uart_flush(UART_NUM_2);
    uart_write_bytes(UART_NUM_2, SendData, sizeof(SendData));
    printf("t= %f\n", (tick_us(th, 0) - t) / 1000.0);
}