2018-07-28 16:49:13 -04:00
|
|
|
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is for gatt server. It can send adv data, be connected by clent.
|
|
|
|
* Run the gatt_client demo, the client demo will automatically connect to the gatt_server demo.
|
|
|
|
* Client demo will enable gatt_server's notify after connection. Then two devices will exchange
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp32-lora.h"
|
|
|
|
#include "esp32-uart-cmdr.h"
|
|
|
|
|
|
|
|
#define TAG "LORA32"
|
|
|
|
#define BUF_SIZE (1024)
|
|
|
|
#define RD_BUF_SIZE (BUF_SIZE)
|
|
|
|
#define EX_UART_NUM UART_NUM_0
|
|
|
|
|
|
|
|
uint8_t data_available = 0;
|
|
|
|
static lora32_cfg_t lora;
|
|
|
|
static QueueHandle_t uart0_queue;
|
|
|
|
|
|
|
|
static void uart_event_task(void *pvParameters) {
|
|
|
|
uart_event_t event;
|
|
|
|
uint8_t *dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
|
|
|
|
uint8_t size = 0;
|
|
|
|
|
|
|
|
bzero(dtmp, RD_BUF_SIZE);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
//Waiting for UART event.
|
|
|
|
if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
|
|
|
switch(event.type) {
|
|
|
|
case UART_DATA:
|
|
|
|
uart_read_bytes(UART_NUM_0, dtmp + size, event.size, portMAX_DELAY);
|
|
|
|
//uart_write_bytes(UART_NUM_0, (const char*) dtmp + size, event.size);
|
|
|
|
|
|
|
|
size += event.size;
|
|
|
|
|
|
|
|
if(dtmp[size - 1] == '\n') {
|
|
|
|
uart_write_bytes(UART_NUM_0, (const char*) dtmp, size);
|
|
|
|
|
|
|
|
struct Cmd *handler;
|
|
|
|
uint8_t match = ucmdr_match((char *)dtmp, &handler);
|
|
|
|
|
2018-07-29 03:49:23 -04:00
|
|
|
ESP_LOGD(TAG, "command size: %d", strlen(handler->cmd));
|
|
|
|
|
2018-07-28 16:49:13 -04:00
|
|
|
if(match) {
|
2018-07-29 03:49:23 -04:00
|
|
|
handler->callback(dtmp + strlen(handler->cmd));
|
2018-07-28 16:49:13 -04:00
|
|
|
} else {
|
|
|
|
printf("command not found\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(dtmp, RD_BUF_SIZE);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UART_BREAK:
|
|
|
|
case UART_BUFFER_FULL:
|
|
|
|
case UART_FIFO_OVF:
|
|
|
|
case UART_FRAME_ERR:
|
|
|
|
case UART_PARITY_ERR:
|
|
|
|
case UART_DATA_BREAK:
|
|
|
|
case UART_PATTERN_DET:
|
|
|
|
case UART_EVENT_MAX:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_lora_receive(uint8_t *data, uint8_t size) {
|
2019-06-28 22:57:57 -04:00
|
|
|
uint8_t j;
|
|
|
|
|
2020-01-01 23:32:53 -05:00
|
|
|
ESP_LOGI(TAG, "received: " LOG_RESET_COLOR "%s", data);
|
|
|
|
|
2019-06-28 22:57:57 -04:00
|
|
|
for(uint8_t i = 0; i < size; i += 16) {
|
|
|
|
for(j = 0; j < 16; j++) {
|
|
|
|
printf("%02X ", data[i + j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
2018-07-28 16:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void *p) {
|
|
|
|
ESP_LOGI(TAG, "starting main loop");
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_mem() {
|
|
|
|
lora32_dump_regs(&lora);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_mode(void *args) {
|
|
|
|
int mode = atoi(args);
|
|
|
|
ESP_LOGI(TAG, "mode: %d", mode);
|
|
|
|
|
|
|
|
if(mode == 1) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(void *args) {
|
2018-08-10 13:10:27 -04:00
|
|
|
ESP_LOGI(TAG, "sending: %s", (char*)args);
|
2018-07-29 17:00:59 -04:00
|
|
|
|
2020-01-01 23:32:53 -05:00
|
|
|
lora32_send(&lora, (uint8_t *)args, strlen(args));
|
2018-08-10 13:10:27 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "done");
|
2018-07-28 16:49:13 -04:00
|
|
|
};
|
|
|
|
|
2018-07-29 03:49:42 -04:00
|
|
|
void set_spreadfactor(void *args) {
|
|
|
|
uint32_t sf = atoi(args);
|
|
|
|
|
2018-07-29 17:00:59 -04:00
|
|
|
printf("spreadfactor: %d\n", sf);
|
2018-07-29 03:49:42 -04:00
|
|
|
|
|
|
|
lora32_set_spreadfactor(&lora, sf);
|
|
|
|
}
|
|
|
|
|
2018-07-29 17:00:59 -04:00
|
|
|
void set_coderate(void *args) {
|
|
|
|
uint8_t cr = atoi(args);
|
|
|
|
|
|
|
|
printf("coding rate: %d\n", cr);
|
|
|
|
|
|
|
|
lora32_set_coding_rate(&lora, cr);
|
|
|
|
}
|
|
|
|
|
2018-07-28 16:49:13 -04:00
|
|
|
void app_main() {
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
// Initialize NVS.
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
xTaskCreate(&loop, "loop", 2048, NULL, 6, NULL);
|
|
|
|
|
|
|
|
lora = lora32_create();
|
|
|
|
lora.frequency = frequencies[F915];
|
|
|
|
lora.receive = &handle_lora_receive;
|
|
|
|
//lora.poll_rx = true;
|
|
|
|
|
|
|
|
lora32_init(&lora);
|
|
|
|
//lora32_set_spreadfactor(&lora, 12);
|
|
|
|
|
2020-01-01 23:32:53 -05:00
|
|
|
lora32_dump_regs(&lora);
|
|
|
|
|
2018-07-28 16:49:13 -04:00
|
|
|
ucmdr_init();
|
|
|
|
ucmdr_install("send", send);
|
|
|
|
ucmdr_install("dump", dump_mem);
|
2018-07-29 03:49:42 -04:00
|
|
|
ucmdr_install("sf", set_spreadfactor);
|
2018-07-29 17:00:59 -04:00
|
|
|
ucmdr_install("cr", set_coderate);
|
2018-07-28 16:49:13 -04:00
|
|
|
|
|
|
|
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_param_config(EX_UART_NUM, &uart_config);
|
|
|
|
|
|
|
|
esp_log_level_set(TAG, ESP_LOG_INFO);
|
|
|
|
uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
|
|
|
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
|
|
|
|
|
|
|
|
//Create a task to handler UART event from ISR
|
|
|
|
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
|
|
|
|
};
|