246 lines
7.5 KiB
C
246 lines
7.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 "meshcore/meshframing.h"
|
|
#include "meshcore/packets/advert.h"
|
|
#include "meshcore/packets/encrypted.h"
|
|
#include "meshcore/packets/group.h"
|
|
#include "task.h"
|
|
|
|
#include "meshcore/packetstructs.h"
|
|
#include "sx1262.h"
|
|
#include "util/log.h"
|
|
#include "string.h"
|
|
#include "meshcore/meshcore.h"
|
|
#include "lib/config.h"
|
|
#include "lib/rtc/rtc.h"
|
|
#include "lib/ed25519/ed_25519.h"
|
|
|
|
#define TAG "MeshCore"
|
|
|
|
/* Global define */
|
|
#define TASK1_TASK_PRIO 5
|
|
#define TASK1_STK_SIZE 2048
|
|
|
|
/* 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();
|
|
//ed25519_create_keypair (persistent.pubkey, persistent.privkey, "vFt0FRugSOeqnkshImMCVfgHM5vBxz4");
|
|
ed25519_create_keypair (persistent.pubkey, persistent.privkey, "vFt0FRugSOeqnkshImMCVfgHM5vBxz3");
|
|
// persistent.nodeType = NODE_TYPE_CHAT_NODE;
|
|
persistent.nodeType = NODE_TYPE_REPEATER;
|
|
memset (persistent.password, 0, sizeof (persistent.password));
|
|
strcpy (persistent.password, "hesielko");
|
|
strcpy (persistent.nodeName, "BRN RiscVpeater");
|
|
|
|
const uint8_t aesKeysDefault[AESKeyCount][17] = {
|
|
{0x11, 0x8b, 0x33, 0x87, 0xe9, 0xc5,
|
|
0xcd, 0xea, 0x6a, 0xc9, 0xe5, 0xed,
|
|
0xba, 0xa1, 0x15, 0xcd, 0x72},
|
|
|
|
{0x0a, 0x44, 0x81, 0xda, 0x0e, 0x4e,
|
|
0x03, 0xc4, 0x9e, 0x84, 0x77, 0x25,
|
|
0xd8, 0x3a, 0x93, 0xbf, 0x80}
|
|
};
|
|
|
|
memcpy (persistent.aesKeys, aesKeysDefault, sizeof (persistent.aesKeys));
|
|
|
|
sendAdvert();
|
|
|
|
char x;
|
|
while (1) {
|
|
if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE) == SET) {
|
|
x = USART_ReceiveData (USART1);
|
|
if (x == 'M') {
|
|
printf ("Sending message\n");
|
|
char tempBuf[180];
|
|
snprintf (tempBuf, 180, "SySTick is %d", xTaskGetTickCount());
|
|
makeSendGroupMessage (tempBuf, 1);
|
|
}
|
|
if (x == 'A') {
|
|
printf ("Sending advert\n");
|
|
sendAdvert();
|
|
}
|
|
if (x == 'N') {
|
|
printNodeDB();
|
|
}
|
|
if (x == 'D') {
|
|
PlainTextMessagePayload plainTextMessage;
|
|
plainTextMessage.timestamp = RTC_GetCounter();
|
|
plainTextMessage.textType = 0;
|
|
plainTextMessage.attempt = 0;
|
|
snprintf(plainTextMessage.message, sizeof(plainTextMessage.message), "Sending message at SySTick is %d", xTaskGetTickCount());
|
|
printf ("Sending a direct message to the first node\n");
|
|
sendEncryptedTextMessage(&(persistent.contacts[0]), &plainTextMessage);
|
|
}
|
|
}
|
|
vTaskDelay (pdMS_TO_TICKS (2000));
|
|
}
|
|
}
|
|
|
|
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 = 869554000;
|
|
|
|
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;
|
|
frame = decodeFrame (bufIn, rxLen);
|
|
processFrame (frame);
|
|
}
|
|
|
|
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();
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init (GPIOA, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init (GPIOA, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.USART_BaudRate = 115200;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
|
|
|
USART_Init (USART1, &USART_InitStructure);
|
|
USART_Cmd (USART1, ENABLE);
|
|
|
|
|
|
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");
|
|
}
|
|
}
|