/********************************** (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/control.h" #include "meshcore/packets/encrypted.h" #include "meshcore/packets/group.h" #include "task.h" #include "meshcore/packetstructs.h" #include "sx1262.h" #include "util/hexdump.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" #include "meshcore/stats.h" #include "lib/adc/temperature.h" #define TAG "MeshCore" static TIM_TypeDef *runtimeTIM = TIM2; // use TIM2 for example void vConfigureTimerForRunTimeStats (void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // Reset the timer TIM_DeInit (runtimeTIM); // Set timer for max period, upcounting TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // Assuming 72 MHz clock -> 1 MHz timer tick (1 ?s) TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Max 16-bit value TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit (runtimeTIM, &TIM_TimeBaseStructure); TIM_Cmd (runtimeTIM, ENABLE); } uint32_t ulGetRunTimeCounterValue (void) { return TIM_GetCounter (runtimeTIM); } /* Global define */ #define TASK1_TASK_PRIO 5 #define TASK1_STK_SIZE 1024 /* 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[260]; void task2_task (void *pvParameters) { MESH_LOGD (TAG, "Task2 boot0"); RTC_Init(); startupTime = RTC_GetCounter(); memset (&stats, 0, sizeof (stats)); MESH_LOGD (TAG, "Task2 boot1"); ADC_Function_Init(); MESH_LOGD (TAG, "Task2 boot2"); // ed25519_create_keypair (persistent.pubkey, persistent.privkey, "vFt0FRugSOeqnkshImMCVfgHM5vBxz4"); ed25519_create_keypair (persistent.pubkey, persistent.privkey, "vFt0FRugSOeqnkshImMCVfgHM5vBxz3"); MESH_LOGD (TAG, "Task2 boot3"); // 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(); DiscoverRequestPayload discReq; discReq.prefixOnly = 0; discReq.since = 0; discReq.tag = RTC_GetCounter(); discReq.typeFilter = 0xFF; sendDiscoverRequest (&discReq); persistent.latitude = 48190900; persistent.longitude = 17030300; persistent.altitude = 23400; char x; MESH_LOGD (TAG, "Task2 boot"); while (1) { if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE) == SET) { x = USART_ReceiveData (USART1); if (x == 'M') { MESH_LOGI (TAG, "Sending message\n"); char tempBuf[180]; snprintf (tempBuf, 180, "SySTick is %d", xTaskGetTickCount()); makeSendGroupMessage (tempBuf, 1); } if (x == '0') { MESH_LOGI (TAG, "Sending zero hop advert\n"); sendAdvert (0); } if (x == 'F') { MESH_LOGI (TAG, "Sending flood advert\n"); sendAdvert (1); } 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; MESH_LOGW (TAG, "LoraInit"); LoRaInit(); int8_t txPowerInDbm = 20; uint32_t frequencyInHz = 869554000; uint8_t spreadingFactor = 8; uint8_t bandwidth = SX126X_LORA_BW_62_5; uint8_t codingRate = SX126X_LORA_CR_4_8; uint16_t preambleLength = 16; float tcxoVoltage = 2.2; // ebyte // float tcxoVoltage = 1.8; // heltec MESH_LOGW (TAG, "Enable TCXO"); char useRegulatorLDO = 1; LoRaDebugPrint (0); MESH_LOGW (TAG, "Starting lora"); uint16_t loraBeginStat = LoRaBegin (frequencyInHz, txPowerInDbm, tcxoVoltage, useRegulatorLDO); if (loraBeginStat != 0) { MESH_LOGE (TAG, "Does not recognize the module"); while (1) { vTaskDelay (pdMS_TO_TICKS (1000)); } } char crcOn = 1; char invertIrq = 0; LoRaConfig (spreadingFactor, bandwidth, codingRate, preambleLength, 0, crcOn, invertIrq); while (1) { start_time = xTaskGetTickCount(); int8_t rssi, snr, rawsnr; FrameStruct frame; if (ReadFrame (&frame, &rssi, &snr, &rawsnr)) { hexdump ("Whole frame", frame.payload, frame.payloadLen); stats.lastSNR = rawsnr; // stats.lastSNR = snr; //TODO figure out which to use stats.lastRSSI = rssi; MESH_LOGI (TAG, "rssi=%d[dBm] snr=%d[dB] rawsnr=%d[quarter dB]", rssi, snr, rawsnr); // frame = decodeFrame (bufIn, rxLen); MESH_LOGD (TAG, "Free stack before processFrame: %u bytes", uxTaskGetStackHighWaterMark (NULL) * 4); processFrame (&frame); MESH_LOGD (TAG, "Free stack before retransmitFrame: %u bytes", uxTaskGetStackHighWaterMark (NULL) * 4); retransmitFrame (&frame); memset (&frame, 0, sizeof (FrameStruct)); // prepare for the next round } int lost = GetPacketLost(); if (lost != 0) { MESH_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) { UBaseType_t minFree = uxTaskGetStackHighWaterMark (xTask); MESH_LOGE (TAG, "Stack overflow in task '%s' (%p)! Minimum free stack: %u words (%u bytes)", pcTaskName, xTask, minFree, minFree * 4); } /********************************************************************* * @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); MESH_LOGD (TAG, "SystemClk:%d\r\n", SystemCoreClock); MESH_LOGD (TAG, "ChipID:%08x\r\n", DBGMCU_GetCHIPID()); MESH_LOGD (TAG, "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)TASK1_STK_SIZE, (void *)NULL, (UBaseType_t)TASK1_TASK_PRIO, (TaskHandle_t *)&Task2Task_Handler); vTaskStartScheduler(); while (1) { MESH_LOGE (TAG, "shouldn't run at here!!\n"); } }