230 lines
6.5 KiB
C
230 lines
6.5 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
|
* File Name : main.c
|
|
* Author : WCH
|
|
* Version : V1.0.0
|
|
* Date : 2021/06/06
|
|
* Description : Main program body.
|
|
*********************************************************************************
|
|
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
|
* Attention: This software (modified or not) and binary are used for
|
|
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
|
*******************************************************************************/
|
|
|
|
/*
|
|
*@Note
|
|
*task1 and task2 alternate printing
|
|
*/
|
|
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
|
|
#include "meshcore/packetstructs.h"
|
|
#include "sx1262.h"
|
|
#include "util/log.h"
|
|
#include "string.h"
|
|
#include "meshcore/meshcore.h"
|
|
#include "lib/base64.h"
|
|
#include "lib/config.h"
|
|
#include "lib/rtc/rtc.h"
|
|
|
|
#define TAG "MeshCore"
|
|
|
|
/* Global define */
|
|
#define TASK1_TASK_PRIO 5
|
|
#define TASK1_STK_SIZE 2048
|
|
|
|
uint8_t publKey[32] = {0x73, 0x78, 0x46, 0x76, 0x87, 0x3c, 0x9f, 0xeb, 0x00, 0x95, 0x05, 0xba, 0xdd, 0x3a, 0x4b, 0x33, 0xc8, 0xf5, 0x88, 0xa3, 0x8f, 0xaa, 0x30, 0x85, 0x3b, 0x91, 0xe6, 0xde, 0x97, 0x8c, 0xf1, 0xb2};
|
|
uint8_t privKey[32] = {0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20, 0x8a, 0x27, 0xca, 0x33, 0x85, 0xa5, 0x3b, 0x7d, 0x88, 0xce, 0x92, 0x23, 0xc4, 0x40, 0xc3, 0xac};
|
|
|
|
/* Global Variable */
|
|
TaskHandle_t Task1Task_Handler;
|
|
TaskHandle_t Task2Task_Handler;
|
|
|
|
/*********************************************************************
|
|
* @fn task1_task
|
|
*
|
|
* @brief task1 program.
|
|
*
|
|
* @param *pvParameters - Parameters point of task1
|
|
*
|
|
* @return none
|
|
*/
|
|
|
|
uint8_t bufIn[256];
|
|
|
|
void task2_task (void *pvParameters) {
|
|
RTC_Init();
|
|
while (1) {
|
|
// char tempBuf[180];
|
|
// vTaskDelay (pdMS_TO_TICKS (10000));
|
|
// snprintf (tempBuf, 180, "BRN RISCV: SySTick is %d", xTaskGetTickCount());
|
|
// makeSendGroupMessage (tempBuf, 1);
|
|
vTaskDelay (pdMS_TO_TICKS (10000));
|
|
memcpy (persistent.privkey, privKey, 32);
|
|
memcpy (persistent.pubkey, publKey, 32);
|
|
strcpy (persistent.nodeName, "BRN RiscV");
|
|
sendAdvert();
|
|
vTaskDelay (pdMS_TO_TICKS (20000));
|
|
}
|
|
}
|
|
|
|
void task1_task (void *pvParameters) {
|
|
//loadConfig();
|
|
const int64_t interval_ms = 10; // 10 ms
|
|
int64_t start_time, end_time, elapsed;
|
|
|
|
ESP_LOGW (TAG, "LoraInit");
|
|
LoRaInit();
|
|
int8_t txPowerInDbm = 20;
|
|
uint32_t frequencyInHz = 869618000;
|
|
|
|
ESP_LOGW (TAG, "Enable TCXO");
|
|
float tcxoVoltage = 2.2; // ebyte
|
|
// float tcxoVoltage = 1.8; // heltec
|
|
char useRegulatorLDO = 1;
|
|
|
|
LoRaDebugPrint (0);
|
|
ESP_LOGW (TAG, "Starting lora");
|
|
uint16_t loraBeginStat = LoRaBegin (frequencyInHz, txPowerInDbm, tcxoVoltage, useRegulatorLDO);
|
|
if (loraBeginStat != 0) {
|
|
ESP_LOGE (TAG, "Does not recognize the module");
|
|
while (1) {
|
|
vTaskDelay (pdMS_TO_TICKS (1000));
|
|
}
|
|
}
|
|
|
|
uint8_t spreadingFactor = 8;
|
|
uint8_t bandwidth = SX126X_LORA_BW_62_5;
|
|
uint8_t codingRate = SX126X_LORA_CR_4_8;
|
|
uint16_t preambleLength = 16;
|
|
char crcOn = 1;
|
|
char invertIrq = 0;
|
|
|
|
LoRaConfig (spreadingFactor, bandwidth, codingRate, preambleLength, 0, crcOn,
|
|
invertIrq);
|
|
|
|
|
|
while (1) {
|
|
start_time = xTaskGetTickCount();
|
|
|
|
uint8_t rxLen = LoRaReceive (bufIn, sizeof (bufIn));
|
|
if (rxLen > 0) {
|
|
// ESP_LOGI (TAG, "%d byte packet received", rxLen);
|
|
|
|
int8_t rssi, snr;
|
|
GetPacketStatus (&rssi, &snr);
|
|
ESP_LOGI (TAG, "rssi=%d[dBm] snr=%d[dB]", rssi, snr);
|
|
|
|
FrameStruct frame = decodeFrame (bufIn, rxLen);
|
|
|
|
printFrameHeader (frame);
|
|
|
|
unsigned char checkSumBuf[10];
|
|
AdvertisementPayload advert;
|
|
GroupTextMessage msg;
|
|
|
|
switch (frame.header & PAYLOAD_TYPE_MASK) {
|
|
case PAYLOAD_TYPE_REQ:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_RESPONSE:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_TXT_MSG:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_ACK:
|
|
memset (checkSumBuf, 0, sizeof (checkSumBuf));
|
|
base64_encode (frame.payload, 4, checkSumBuf);
|
|
printf ("Checksum: %s\n", checkSumBuf);
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_ADVERT:
|
|
advert = decodeAdvertisement (frame);
|
|
printAdvertisement (advert);
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_GRP_TXT:
|
|
msg = decodeGroupMessage (frame);
|
|
printGroupMessage (msg);
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_GRP_DATA:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_ANON_REQ:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_PATH:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_TRACE:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_MULTIPART:
|
|
break;
|
|
|
|
case PAYLOAD_TYPE_RAW_CUSTOM:
|
|
break;
|
|
}
|
|
}
|
|
|
|
int lost = GetPacketLost();
|
|
if (lost != 0) {
|
|
ESP_LOGW (TAG, "%d packets lost", lost);
|
|
}
|
|
|
|
end_time = xTaskGetTickCount();
|
|
elapsed = end_time - start_time;
|
|
|
|
if (elapsed < (interval_ms / 2)) {
|
|
vTaskDelay (pdMS_TO_TICKS ((interval_ms - elapsed)));
|
|
}
|
|
}
|
|
}
|
|
|
|
void vApplicationStackOverflowHook (TaskHandle_t xTask, char *pcTaskName) {
|
|
printf ("stackoverflow");
|
|
}
|
|
|
|
/*********************************************************************
|
|
* @fn main
|
|
*
|
|
* @brief ; program.
|
|
*
|
|
* @return none
|
|
*/
|
|
|
|
int main (void) {
|
|
|
|
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
|
|
SystemCoreClockUpdate();
|
|
Delay_Init();
|
|
USART_Printf_Init (115200);
|
|
|
|
printf ("SystemClk:%d\r\n", SystemCoreClock);
|
|
printf ("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
|
|
printf ("FreeRTOS Kernel Version:%s\r\n", tskKERNEL_VERSION_NUMBER);
|
|
|
|
xTaskCreate ((TaskFunction_t)task1_task,
|
|
(const char *)"task1",
|
|
(uint16_t)TASK1_STK_SIZE,
|
|
(void *)NULL,
|
|
(UBaseType_t)TASK1_TASK_PRIO,
|
|
(TaskHandle_t *)&Task1Task_Handler);
|
|
|
|
xTaskCreate ((TaskFunction_t)task2_task,
|
|
(const char *)"task2",
|
|
(uint16_t)1024,
|
|
(void *)NULL,
|
|
(UBaseType_t)TASK1_TASK_PRIO,
|
|
(TaskHandle_t *)&Task2Task_Handler);
|
|
|
|
|
|
vTaskStartScheduler();
|
|
|
|
while (1) {
|
|
printf ("shouldn't run at here!!\n");
|
|
}
|
|
}
|