Added UART and PingPong function
This commit is contained in:
parent
e618b2510a
commit
c4ae453af7
@ -1,3 +1,4 @@
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_crc.h"
|
||||
#include "esp_event.h"
|
||||
@ -14,6 +15,7 @@
|
||||
#include "portmacro.h"
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -35,9 +37,7 @@ static uint8_t s_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF};
|
||||
static uint16_t s_espnow_seq[2] = {0, 0};
|
||||
|
||||
void setIsMaster(bool status) {
|
||||
isMaster = status;
|
||||
}
|
||||
void setIsMaster(bool status) { isMaster = status; }
|
||||
|
||||
void wifi_init(void) {
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
@ -84,14 +84,13 @@ void espnow_data_prepare(espnow_send_param_t *send_param) {
|
||||
buf->seq_num = 0; // s_espnow_seq[buf->type]++;
|
||||
buf->crc = 0;
|
||||
buf->magic = send_param->magic;
|
||||
buf->unionPage = UNION_STATUS;
|
||||
buf->realPayload.status.isMaster = isMaster;
|
||||
buf->unionPage = UNION_PING_PONG;
|
||||
buf->realPayload.pingPongData.startCounter = xTaskGetTickCount();
|
||||
|
||||
buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
|
||||
}
|
||||
|
||||
void espnow_send_cb(const uint8_t *mac_addr,
|
||||
esp_now_send_status_t status) {
|
||||
void espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
||||
espnow_event_t evt;
|
||||
espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
|
||||
|
||||
@ -108,8 +107,8 @@ void espnow_send_cb(const uint8_t *mac_addr,
|
||||
}
|
||||
}
|
||||
|
||||
void espnow_recv_cb(const esp_now_recv_info_t *recv_info,
|
||||
const uint8_t *data, int len) {
|
||||
void espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data,
|
||||
int len) {
|
||||
espnow_event_t evt;
|
||||
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
|
||||
uint8_t *mac_addr = recv_info->src_addr;
|
||||
@ -146,7 +145,8 @@ void espnow_recv_cb(const esp_now_recv_info_t *recv_info,
|
||||
}
|
||||
|
||||
int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
|
||||
uint16_t *seq, uint32_t *magic) {
|
||||
uint16_t *seq, uint32_t *magic,
|
||||
espnow_event_recv_cb_t *recv_cb) {
|
||||
espnow_data_t *buf = (espnow_data_t *)data;
|
||||
uint16_t crc, crc_cal = 0;
|
||||
|
||||
@ -173,6 +173,27 @@ int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
|
||||
case UNION_SENSORDATA:
|
||||
ESP_LOGI(tag, "Yeah Daten %d", buf->realPayload.sensorData.dataPoint);
|
||||
break;
|
||||
case UNION_PING_PONG:
|
||||
if (isMaster) {
|
||||
uint endCounter = xTaskGetTickCount();
|
||||
uint diffCounter =
|
||||
endCounter - buf->realPayload.pingPongData.startCounter;
|
||||
ESP_LOGI(tag, "Start: %d, End: %d, Diff: %d, Ping: %d",
|
||||
buf->realPayload.pingPongData.startCounter, endCounter,
|
||||
diffCounter, diffCounter / configTICK_RATE_HZ);
|
||||
} else {
|
||||
ESP_LOGI(tag, "Got Ping request sending back startCounter %d",
|
||||
buf->realPayload.pingPongData.startCounter);
|
||||
if (esp_now_send(recv_cb->mac_addr, recv_cb->data, recv_cb->data_len) !=
|
||||
ESP_OK) {
|
||||
ESP_LOGE(tag, "Send error PingPong");
|
||||
}
|
||||
// todo send back data
|
||||
}
|
||||
break;
|
||||
case UNION_TEST:
|
||||
ESP_LOGI(tag, "Unit Testing!!!");
|
||||
break;
|
||||
}
|
||||
|
||||
if (crc_cal == crc) {
|
||||
@ -315,7 +336,7 @@ void espnow_task(void *pvParameter) {
|
||||
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
|
||||
|
||||
ret = espnow_data_parse(recv_cb->data, recv_cb->data_len, &recv_state,
|
||||
&recv_seq, &recv_magic);
|
||||
&recv_seq, &recv_magic, recv_cb);
|
||||
free(recv_cb->data);
|
||||
if (ret == EXAMPLE_ESPNOW_DATA_BROADCAST) {
|
||||
ESP_LOGI(tag, "Receive %dth broadcast data from: " MACSTR ", len: %d",
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
#define ESPNOW_HANDLER_H
|
||||
|
||||
#include "shared.h"
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
static bool isMaster;
|
||||
|
||||
#define MAX_PAYLOAD_SIZE 250
|
||||
@ -44,6 +46,8 @@ enum {
|
||||
enum {
|
||||
UNION_STATUS,
|
||||
UNION_SENSORDATA,
|
||||
UNION_PING_PONG,
|
||||
UNION_TEST,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
@ -54,9 +58,19 @@ typedef struct {
|
||||
uint8_t dataPoint;
|
||||
} __attribute__((packed)) payloadSensorData;
|
||||
|
||||
typedef struct {
|
||||
uint startCounter;
|
||||
} __attribute__((packed)) payloadPingPong;
|
||||
|
||||
typedef struct {
|
||||
uint testNumber;
|
||||
} __attribute__((packed)) payloadTesting;
|
||||
|
||||
union realPayload {
|
||||
payloadStatus status;
|
||||
payloadSensorData sensorData;
|
||||
payloadPingPong pingPongData;
|
||||
payloadTesting testingData;
|
||||
};
|
||||
|
||||
/* User defined field of ESPNOW data in this example. */
|
||||
@ -96,7 +110,7 @@ void espnow_send_cb(const uint8_t *mac_addr,
|
||||
void espnow_recv_cb(const esp_now_recv_info_t *recv_info,
|
||||
const uint8_t *data, int len);
|
||||
int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
|
||||
uint16_t *seq, uint32_t *magic);
|
||||
uint16_t *seq, uint32_t *magic, espnow_event_recv_cb_t* recv_cb);
|
||||
esp_err_t espnow_init(void);
|
||||
void espnow_task(void *pvParameter);
|
||||
|
||||
|
||||
27
main/main.c
27
main/main.c
@ -1,4 +1,5 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/uart.h"
|
||||
#include "esp_crc.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
@ -10,6 +11,7 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "hal/gpio_types.h"
|
||||
#include "hal/uart_types.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "portmacro.h"
|
||||
#include <assert.h>
|
||||
@ -17,8 +19,12 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "espnow_handler.h"
|
||||
#include "main.h"
|
||||
|
||||
#define BUF_SIZE (1024)
|
||||
#define TXD_PIN (GPIO_NUM_17)
|
||||
#define RXD_PIN (GPIO_NUM_16)
|
||||
|
||||
void app_main(void) {
|
||||
// Master Slave Detection, default pin is pull up so ground it and check state
|
||||
@ -30,6 +36,25 @@ void app_main(void) {
|
||||
setIsMaster(true);
|
||||
}
|
||||
|
||||
uart_config_t uart_config = {.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
||||
|
||||
uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
uart_param_config(UART_NUM_2, &uart_config);
|
||||
uart_set_pin(UART_NUM_2, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
||||
UART_PIN_NO_CHANGE);
|
||||
|
||||
char *test_str = "Ok\n";
|
||||
|
||||
/*while (1) {*/
|
||||
/* uart_write_bytes(UART_NUM_2, (const char *)test_str, strlen(test_str));*/
|
||||
/* ESP_LOGI(tag, "Sending UART\n");*/
|
||||
/* vTaskDelay(100 / portTICK_PERIOD_MS);*/
|
||||
/*}*/
|
||||
|
||||
ESP_LOGI(tag, "ESP MASTER State %d\n", isMaster);
|
||||
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user