really still wip

This commit is contained in:
2025-12-28 12:23:11 +01:00
parent a48ef9d5e0
commit 31dda62474
33 changed files with 1721 additions and 589 deletions

View File

@@ -18,24 +18,52 @@
#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 2048
#define TASK1_STK_SIZE 1024
/* Global Variable */
TaskHandle_t Task1Task_Handler;
@@ -51,13 +79,21 @@ TaskHandle_t Task2Task_Handler;
* @return none
*/
uint8_t bufIn[256];
// uint8_t bufIn[260];
void task2_task (void *pvParameters) {
MESH_LOGD (TAG, "Task2 boot0");
RTC_Init();
//ed25519_create_keypair (persistent.pubkey, persistent.privkey, "vFt0FRugSOeqnkshImMCVfgHM5vBxz4");
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");
// persistent.nodeType = NODE_TYPE_CHAT_NODE;
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");
@@ -75,21 +111,38 @@ void task2_task (void *pvParameters) {
memcpy (persistent.aesKeys, aesKeysDefault, sizeof (persistent.aesKeys));
sendAdvert();
// 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') {
printf ("Sending message\n");
MESH_LOGI (TAG, "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 == '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();
@@ -99,9 +152,9 @@ void task2_task (void *pvParameters) {
plainTextMessage.timestamp = RTC_GetCounter();
plainTextMessage.textType = 0;
plainTextMessage.attempt = 0;
snprintf(plainTextMessage.message, sizeof(plainTextMessage.message), "Sending message at SySTick is %d", xTaskGetTickCount());
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);
sendEncryptedTextMessage (&(persistent.contacts[0]), &plainTextMessage);
}
}
vTaskDelay (pdMS_TO_TICKS (2000));
@@ -113,30 +166,31 @@ void task1_task (void *pvParameters) {
const int64_t interval_ms = 10; // 10 ms
int64_t start_time, end_time, elapsed;
ESP_LOGW (TAG, "LoraInit");
MESH_LOGW (TAG, "LoraInit");
LoRaInit();
int8_t txPowerInDbm = 20;
uint32_t frequencyInHz = 869554000;
ESP_LOGW (TAG, "Enable TCXO");
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);
ESP_LOGW (TAG, "Starting lora");
MESH_LOGW (TAG, "Starting lora");
uint16_t loraBeginStat = LoRaBegin (frequencyInHz, txPowerInDbm, tcxoVoltage, useRegulatorLDO);
if (loraBeginStat != 0) {
ESP_LOGE (TAG, "Does not recognize the module");
MESH_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;
@@ -146,22 +200,25 @@ void task1_task (void *pvParameters) {
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);
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) {
ESP_LOGW (TAG, "%d packets lost", lost);
MESH_LOGW (TAG, "%d packets lost", lost);
}
end_time = xTaskGetTickCount();
@@ -174,7 +231,9 @@ void task1_task (void *pvParameters) {
}
void vApplicationStackOverflowHook (TaskHandle_t xTask, char *pcTaskName) {
printf ("stackoverflow");
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);
}
/*********************************************************************
@@ -218,9 +277,9 @@ int main (void) {
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);
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",
@@ -231,7 +290,7 @@ int main (void) {
xTaskCreate ((TaskFunction_t)task2_task,
(const char *)"task2",
(uint16_t)1024,
(uint16_t)TASK1_STK_SIZE,
(void *)NULL,
(UBaseType_t)TASK1_TASK_PRIO,
(TaskHandle_t *)&Task2Task_Handler);
@@ -240,6 +299,6 @@ int main (void) {
vTaskStartScheduler();
while (1) {
printf ("shouldn't run at here!!\n");
MESH_LOGE (TAG, "shouldn't run at here!!\n");
}
}