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);
}
Page 335 full program from fragment
#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 = 1000;
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);
char RecData[2000];
int nr = uart_read_bytes(UART_NUM_2, RecData,
sizeof(RecData), 300 / portTICK_PERIOD_MS);
RecData[nr + 1] = 0;
printf("%s\n", &RecData[1]);
printf("%d %d\n", nr, sizeof(SendData));
}
Page 335-336
#include "freertos/FreeRTOS.h"
#include <string.h>
#include <unistd.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 delay_us(int t)
{
usleep(t);
}
void app_main(void)
{
uart_driver_install(UART_NUM_2, 512, 1024 * 2, 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 = 1000;
char SendData[n];
for (int i = 0; i < n; i++)
{
SendData[i] = 'A' + i / 40;
}
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);
delay_us(2000 * 1000);
char RecData[2000];
int nr = uart_read_bytes(UART_NUM_2, RecData, sizeof(RecData),
300 / portTICK_PERIOD_MS);
RecData[nr + 1] = 0;
printf("%s\n", &RecData[1]);
printf("%d %d\n", nr, sizeof(SendData));
uart_driver_delete(UART_NUM_2);
}
Page 341
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include <unistd.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 delay_us(int t)
{
usleep(t);
}
void app_main(void)
{
uart_driver_install(UART_NUM_2, 256, 2000, 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_CTS_RTS,
.rx_flow_ctrl_thresh = 50,
.source_clk = UART_SCLK_DEFAULT};
uart_param_config(UART_NUM_2, &uart_config);
uart_set_pin(UART_NUM_2, 1, 2, 4, 5);
int n = 1000;
char SendData[n];
for (int i = 0; i < n; i++)
{
SendData[i] = 'A' + i / 40;
}
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);
fflush(stdout);
delay_us(1000 * 1000);
char RecData[2000];
int nr = 0;
int nt = 0;
do
{
nt = uart_read_bytes(UART_NUM_2, &RecData[nr],
sizeof(RecData) - nr, 50 / portTICK_PERIOD_MS);
nr = nr + nt;
printf("%d %d\n", nt, nr);
} while (nt != 0);
RecData[nr + 1] = 0;
printf("%s\n", &RecData[1]);
printf("%d %d\n", nr, sizeof(SendData));
uart_driver_delete(UART_NUM_2);
}
Page 347
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include <unistd.h>
#include "driver/uart.h"
void delay_us(int t)
{
usleep(t);
}
char uart_read(uart_port_t uart_num)
{
size_t num;
do
{
uart_get_buffered_data_len(uart_num, &num);
} while (num == 0);
char byte;
uart_read_bytes(UART_NUM_2, &byte, 1, 0);
return byte;
}
int uart_read_chars(uart_port_t uart_num, char *buf, int length, int timeout, int timeout_char)
{
int nr = uart_read_bytes(uart_num, buf, 1, timeout / portTICK_PERIOD_MS);
if (nr == 0)
return 0;
nr = uart_read_bytes(uart_num, &buf[1], length - 1, timeout_char / portTICK_PERIOD_MS);
return nr;
}
int presence(uart_port_t uart_num)
{
uart_set_baudrate(uart_num, 9600);
char byte = 0xF0;
uart_write_bytes(uart_num, &byte, 1);
delay_us(1000);
uart_read_bytes(uart_num, &byte, 1, 30 / portTICK_PERIOD_MS);
uart_set_baudrate(uart_num, 115200);
if (byte == 0xF0)
return -1;
return 0;
}
void writeByte(uart_port_t uart_num, int byte)
{
char buf[8];
for (int i = 0; i < 8; i++)
{
if ((byte & 1) == 1)
{
buf[i] = 0xFF;
}
else
{
buf[i] = 0x00;
}
byte = byte >> 1;
};
uart_write_bytes(uart_num, buf, 8);
delay_us(1000);
uart_read_bytes(uart_num, buf, 8, 30 / portTICK_PERIOD_MS);
}
char readByte(uart_port_t uart_num)
{
char buf[8];
for (int i = 0; i < 8; i++)
{
buf[i] = 0xFF;
}
uart_write_bytes(uart_num, buf, 8);
delay_us(1000);
uart_read_bytes(uart_num, buf, 8, 30 / portTICK_PERIOD_MS);
char result = 0;
for (int i = 0; i < 8; i++)
{
result = result >> 1;
if (buf[i] == 0xFF)
result = result | 0x80;
}
return result;
}
void app_main(void)
{
uart_driver_install(UART_NUM_2, 256, 2000, 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,
.rx_flow_ctrl_thresh = 50,
.source_clk = UART_SCLK_DEFAULT};
uart_param_config(UART_NUM_2, &uart_config);
uart_set_pin(UART_NUM_2, 1, 2, -1, -1);
uart_flush(UART_NUM_2);
printf("%d\n", presence(UART_NUM_2));
presence(UART_NUM_2);
writeByte(UART_NUM_2, 0xCC);
writeByte(UART_NUM_2, 0x44);
delay_us(1000 * 1000);
presence(UART_NUM_2);
writeByte(UART_NUM_2, 0xCC);
writeByte(UART_NUM_2, 0xBE);
char data[9];
for (int i = 0; i < 9; i++)
{
data[i] = readByte(UART_NUM_2);
}
int t1 = data[0];
int t2 = data[1];
int temp1 = (t2 << 8 | t1);
if (t2 & 0x80)
temp1 = temp1 | 0xFFFF0000;
float temp = temp1 / 16.0;
printf("temp =%f\n", temp);
uart_driver_delete(UART_NUM_2);
}