233 lines
8.0 KiB
C
233 lines
8.0 KiB
C
//
|
||
// Created by cfif on 14.04.23.
|
||
//
|
||
#include "Us.h"
|
||
//#include "i2c_smbus.h"
|
||
#include "CmsisRtosThreadUtils.h"
|
||
#include "stdlib.h"
|
||
#include "ModemSend.h"
|
||
#include "ModemGonecFunc.h"
|
||
|
||
#define LOGGER env->logger
|
||
#define LOG_SIGN "GONEC"
|
||
|
||
void Us_Init(tUs *env, tGpios *gpios, tFs *fs, tRtcIO *rtcIo, tDeviceStorageIni *storage, tModemMain *ModemMain) {
|
||
|
||
env->gpios = gpios;
|
||
env->fs = fs;
|
||
env->rtcIo = rtcIo;
|
||
env->storage = storage;
|
||
env->ModemMain = ModemMain;
|
||
|
||
env->storage->runtime.almaError = true;
|
||
|
||
InitThreadAtrStatic(&env->thread.attr, "Us", env->thread.controlBlock, env->thread.stack, osPriorityNormal);
|
||
env->thread.id = 0;
|
||
}
|
||
|
||
|
||
static _Noreturn void Us_Thread(tUs *env) {
|
||
/*
|
||
bool startStuckGetMs = false;
|
||
uint32_t timeStuckGetMs = 0;
|
||
|
||
|
||
GpioPinSet(&env->gpios->Power.tx_pwr_en, false);
|
||
|
||
// Флаг проверки включения предусилиеля
|
||
bool FlagOnUs = true;
|
||
// Флаг проверки выключения предусилиеля
|
||
bool FlagOffUs = false;
|
||
*/
|
||
for (;;) {
|
||
|
||
if (env->ModemMain->location.isOneDateTime) {
|
||
SystemDelayMs(100);
|
||
continue;
|
||
}
|
||
/*
|
||
// Считывание состояния GPIO3 - Пин управления предусилителем
|
||
// Сообщает, когда включить или выключить предусилитель
|
||
env->pwrStatus = GpioPinGet(&env->gpios->Power.gonec_pwr_amp);
|
||
|
||
if ((env->pwrStatus) && (FlagOnUs)) {
|
||
// Фиксация уже включенного предусилителя
|
||
FlagOnUs = false;
|
||
FlagOffUs = true;
|
||
|
||
// Включение TX_PWR_En
|
||
GpioPinSet(&env->gpios->Power.tx_pwr_en, true);
|
||
|
||
// Ожидание инициализации устройства управления питанием
|
||
SystemDelayMs(80);
|
||
// Настройка источников питания, если процесс ВДРУГ завершился с ошибкой,
|
||
// в следующем цикле настройка питания повторяется
|
||
if (sendI2c()) {
|
||
LoggerInfoStatic(LOGGER, LOG_SIGN, "Включение предусилителя - успех");
|
||
} else {
|
||
LoggerInfoStatic(LOGGER, LOG_SIGN, "Включение предусилителя - ошибка");
|
||
FlagOnUs = true;
|
||
FlagOffUs = false;
|
||
}
|
||
|
||
// Запуск контроля включения предусилителя
|
||
timeStuckGetMs = SystemGetMs();
|
||
startStuckGetMs = true;
|
||
|
||
}
|
||
|
||
if ((env->pwrStatus == false) && (FlagOffUs)) {
|
||
|
||
LoggerInfoStatic(LOGGER, LOG_SIGN, "Отключение предусилителя");
|
||
|
||
// Отключение TX_PWR_En
|
||
GpioPinSet(&env->gpios->Power.tx_pwr_en, false);
|
||
|
||
// Фиксация уже выключенного предусилителя
|
||
FlagOnUs = true;
|
||
FlagOffUs = false;
|
||
|
||
// Останов контроля включения предусилителя
|
||
startStuckGetMs = false;
|
||
}
|
||
|
||
// Проверка включенного предусилителя
|
||
if (startStuckGetMs) {
|
||
|
||
uint32_t dlStuckGetMs = SystemGetMs() - timeStuckGetMs;
|
||
|
||
// Если предусилитель включен больше 2 минут
|
||
if (dlStuckGetMs > 120000) {
|
||
|
||
char bufText[128];
|
||
bufText[0] = '\0';
|
||
strcat(bufText, "Контроль выключения предусилителя (");
|
||
utoa(env->pwrStatus, &bufText[strlen(bufText)], 10);
|
||
strcat(bufText, ",");
|
||
utoa(dlStuckGetMs, &bufText[strlen(bufText)], 10);
|
||
strcat(bufText, ",");
|
||
utoa(timeStuckGetMs, &bufText[strlen(bufText)], 10);
|
||
strcat(bufText, ")");
|
||
|
||
LoggerInfo(LOGGER, LOG_SIGN, bufText, strlen(bufText));
|
||
startStuckGetMs = false;
|
||
// Отключение TX_PWR_En
|
||
GpioPinSet(&env->gpios->Power.tx_pwr_en, false);
|
||
}
|
||
}
|
||
*/
|
||
|
||
bool isSos = getIsSlotsSos(env->ModemMain);
|
||
|
||
if ((env->storage->nvm.routing) || (isSos)) {
|
||
|
||
time_t now;
|
||
env->rtcIo->get(env->rtcIo, &now);
|
||
now += env->storage->nvm.Settings_General.GMTcorr_v * 3600;
|
||
|
||
bool almanacIsValid = GonetsAlmanac_IsValid(env->ModemMain->alms, now);
|
||
|
||
if ((env->ModemMain->countAlmanac == 0) || (almanacIsValid == false)) {
|
||
env->storage->runtime.almaError = true;
|
||
env->storage->nvm.routing = false;
|
||
|
||
env->storage->runtime.BannedSAT_ex[0] = '\0';
|
||
strcat(env->storage->runtime.BannedSAT_ex, env->storage->runtime.Settings_General.BannedSAT_v);
|
||
|
||
SetRoutingRestrictSc(env->ModemMain, env->storage->nvm.Settings_General.BannedSAT_v);
|
||
|
||
} else {
|
||
env->storage->runtime.almaError = false;
|
||
}
|
||
|
||
if (env->storage->runtime.almaError == false) {
|
||
|
||
uint8_t inRange[99]; // массив со спутниками внутри конуса
|
||
size_t countRange = 0;
|
||
|
||
uint32_t timeZoneInSat = env->storage->nvm.timeZoneInSat;
|
||
if (isSos)
|
||
timeZoneInSat = env->storage->nvm.Settings_Msd.SatTimeInZone;
|
||
|
||
uint32_t dist = env->storage->nvm.dist;
|
||
if (isSos)
|
||
dist = (uint32_t)GonetsAlmanac_GetDistanceByAngleDeg((float)env->storage->nvm.Settings_Msd.AlmSatAngle);
|
||
|
||
vector2 selfPosition;
|
||
selfPosition.x = (float) env->ModemMain->location.latitude;
|
||
selfPosition.y = (float) env->ModemMain->location.longitude;
|
||
//vector3 selfPosition = GeodesicToSpaceA(vc, 1500);
|
||
|
||
//time_t interval_begin = now - timeZoneInSat * 60;
|
||
time_t interval_begin = now;
|
||
time_t interval_end = now + timeZoneInSat * 60;
|
||
|
||
|
||
volatile uint32_t t1 = SystemGetMs();
|
||
|
||
countRange = GonetsAlmanac_GetInRangeForInterval(
|
||
env->ModemMain->alms,
|
||
(size_t) env->ModemMain->countAlmanac,
|
||
|
||
selfPosition,
|
||
(float) dist,
|
||
|
||
interval_begin,
|
||
interval_end,
|
||
|
||
inRange
|
||
);
|
||
|
||
volatile uint32_t t2 = SystemGetMs();
|
||
volatile uint32_t t3 = t2 - t1;
|
||
|
||
env->storage->runtime.BannedSAT_ex[0] = '\0';
|
||
strcat(env->storage->runtime.BannedSAT_ex, env->storage->runtime.Settings_General.BannedSAT_v);
|
||
|
||
for (int i = 0; i < env->ModemMain->countAlmanac; ++i) {
|
||
|
||
bool isFindKA = false;
|
||
|
||
for (size_t j = 0; j < countRange; ++j) {
|
||
if (env->ModemMain->alms[i].NKA == inRange[j]) {
|
||
isFindKA = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (isFindKA == false) {
|
||
|
||
if (strlen(env->storage->runtime.BannedSAT_ex) > 0)
|
||
strcat(env->storage->runtime.BannedSAT_ex, ",");
|
||
|
||
char buf[12];
|
||
utoa(env->ModemMain->alms[i].NKA, buf, 10);
|
||
strcat(env->storage->runtime.BannedSAT_ex, buf);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
SystemDelayMs(30000);
|
||
|
||
// 10 мин
|
||
if (SystemGetMs() > osWaitForever - 600000) {
|
||
NVIC_SystemReset();
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
void Us_StartThread(tUs *env) {
|
||
if (!env->thread.id) {
|
||
env->thread.id = osThreadNew((osThreadFunc_t) (Us_Thread), (void *) (env), &env->thread.attr);
|
||
} else {
|
||
osThreadResume(env->thread.id);
|
||
}
|
||
}
|