SP8EBC-ParaTNC/src/main.c

539 wiersze
15 KiB
C
Czysty Zwykły widok Historia

2019-01-26 22:18:25 +00:00
#include <delay.h>
#include <LedConfig.h>
2017-08-20 12:41:17 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x.h>
2017-08-20 12:41:17 +00:00
#include "main.h"
2019-01-27 13:23:20 +00:00
#include "packet_tx_handler.h"
2017-08-20 12:41:17 +00:00
#include "station_config.h"
#include "diag/Trace.h"
#include "antilib_adc.h"
#include "afsk_pr.h"
#include "drivers/serial.h"
#include "TimerConfig.h"
#include "PathConfig.h"
2019-01-27 13:23:20 +00:00
#include "it_handlers.h"
2017-08-20 12:41:17 +00:00
#include "aprs/digi.h"
#include "aprs/telemetry.h"
#include "aprs/dac.h"
#include "aprs/beacon.h"
2019-04-03 09:41:37 +00:00
#ifdef _VICTRON
#include "ve_direct_protocol/parser.h"
#endif
2019-01-26 22:18:25 +00:00
#include "rte_wx.h"
2019-04-03 09:41:37 +00:00
#include "rte_pv.h"
#include "rte_main.h"
2017-08-20 12:41:17 +00:00
#ifdef _METEO
2019-01-26 22:18:25 +00:00
#include <wx_handler.h>
2017-08-20 12:41:17 +00:00
#include "drivers/dallas.h"
#include "drivers/ms5611.h"
#include "drivers/i2c.h"
#include "drivers/tx20.h"
#include "drivers/analog_anemometer.h"
2017-08-20 12:41:17 +00:00
#include "aprs/wx.h"
#include "drivers/gpio_conf.h"
#ifdef _UMB_MASTER
#include "umb_master/umb_master.h"
#include "umb_master/umb_channel_pool.h"
#include "umb_master/umb_0x26_status.h"
#endif
2017-08-20 12:41:17 +00:00
#endif
#ifdef _DALLAS_AS_TELEM
#include "drivers/dallas.h"
#endif
2017-08-20 12:41:17 +00:00
#include "KissCommunication.h"
#define SOH 0x01
2020-03-18 20:56:02 +00:00
//#define SERIAL_TX_TEST_MODE
2017-08-20 12:41:17 +00:00
// Niebieska dioda -> DCD
// Zielona dioda -> anemometr albo TX
// backup registers
// 2 -> 4bit hard-faults | 4bit boot-counter
// 3 -> hard fault PC LSB
// 4 -> hard fault PC MSB
// 5 -> hard fault LR LSB
// 6 -> hard fault LR MSB
2017-08-20 12:41:17 +00:00
// ----- main() ---------------------------------------------------------------
// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"
#pragma GCC diagnostic ignored "-Wempty-body"
// global variable incremented by the SysTick handler to measure time in miliseconds
uint32_t master_time = 0;
2019-01-26 17:02:19 +00:00
// global variable used as a timer to trigger meteo sensors mesurements
2019-01-27 13:23:20 +00:00
int32_t main_wx_sensors_pool_timer = 65500;
2019-01-26 22:18:25 +00:00
// global variable used as a timer to trigger packet sending
2019-01-27 13:23:20 +00:00
int32_t main_packet_tx_pool_timer = 60000;
2019-01-26 17:02:19 +00:00
// one second pool interval
int32_t main_one_second_pool_timer = 1000;
// two second pool interval
int32_t main_two_second_pool_timer = 2000;
// ten second pool interval
int32_t main_ten_second_pool_timer = 10000;
// global variables represending the AX25/APRS stack
2019-01-26 22:18:25 +00:00
AX25Ctx main_ax25;
Afsk main_afsk;
2017-08-20 12:41:17 +00:00
2019-01-13 14:57:59 +00:00
2019-01-27 11:34:43 +00:00
AX25Call main_own_path[3];
uint8_t main_own_path_ln = 0;
uint8_t main_own_aprs_msg_len;
2019-01-26 22:18:25 +00:00
char main_own_aprs_msg[160];
2017-08-20 12:41:17 +00:00
// global variable used to store return value from various functions
volatile uint8_t retval = 100;
2019-04-03 09:41:37 +00:00
uint16_t buffer_len = 0;
#ifdef _VICTRON
#endif
#ifdef _UMB_MASTER
// return value from UMB related functions
umb_retval_t main_umb_retval = UMB_UNINITIALIZED;
#endif
2017-08-20 12:41:17 +00:00
char after_tx_lock;
unsigned short rx10m = 0, tx10m = 0, digi10m = 0, kiss10m = 0;
2017-08-20 12:41:17 +00:00
2017-08-20 12:41:17 +00:00
static void message_callback(struct AX25Msg *msg) {
}
int
main(int argc, char* argv[])
{
// Send a greeting to the trace device (skipped on Release).
// trace_puts("Hello ARM World!");
2019-01-13 14:57:59 +00:00
int32_t ln = 0;
2017-08-20 12:41:17 +00:00
2019-01-27 13:23:20 +00:00
uint8_t button_inhibit = 0;
2017-08-20 12:41:17 +00:00
RCC->APB1ENR |= (RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM7EN | RCC_APB1ENR_TIM4EN);
RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_TIM1EN);
2019-01-26 22:18:25 +00:00
memset(main_own_aprs_msg, 0x00, 128);
2017-08-20 12:41:17 +00:00
2019-01-27 11:34:43 +00:00
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
// choosing the signal source for the SysTick timer.
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
// Configuring the SysTick timer to generate interrupt 100x per second (one interrupt = 10ms)
2019-01-27 11:34:43 +00:00
SysTick_Config(SystemCoreClock / SYSTICK_TICKS_PER_SECONDS);
// setting an Systick interrupt priority
NVIC_SetPriority(SysTick_IRQn, 5);
// enable access to BKP registers
RCC->APB1ENR |= (RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN);
PWR->CR |= PWR_CR_DBP;
rte_main_reboot_req = 0;
// read current number of boot cycles
rte_main_boot_cycles = (uint8_t)(BKP->DR2 & 0xFF);
// read current number of hard faults
rte_main_hard_faults = (uint8_t)((BKP->DR2 & 0xFF00) >> 8);
// increase boot cycles count
rte_main_boot_cycles++;
// erasing old value from backup registers
BKP->DR2 &= (0xFFFF ^ 0xFF);
// storing increased value
BKP->DR2 |= rte_main_boot_cycles;
rte_main_hardfault_pc = (BKP->DR3 | (BKP->DR4 << 16));
rte_main_hardfault_lr = (BKP->DR5 | (BKP->DR6 << 16));
BKP->DR3 = 0;
BKP->DR4 = 0;
BKP->DR5 = 0;
BKP->DR6 = 0;
// disabling access to BKP registers
RCC->APB1ENR &= (0xFFFFFFFF ^ (RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN));
PWR->CR &= (0xFFFFFFFF ^ PWR_CR_DBP);
2019-01-26 17:02:19 +00:00
#if defined _RANDOM_DELAY
// configuring a default delay value
delay_set(_DELAY_BASE, 1);
#elif !defined _RANDOM_DELAY
delay_set(_DELAY_BASE, 0);
#endif
// configuring an APRS path used to transmit own packets (telemetry, wx, beacons)
2019-01-27 11:34:43 +00:00
main_own_path_ln = ConfigPath(main_own_path);
2017-08-20 12:41:17 +00:00
#ifdef _METEO
// initialize i2c controller
2017-08-20 12:41:17 +00:00
i2cConfigure();
#endif
// initialize GPIO pins leds are connecting to
led_init();
// initialize AX25 & APRS stuff
2019-01-26 22:18:25 +00:00
AFSK_Init(&main_afsk);
ax25_init(&main_ax25, &main_afsk, 0, 0x00);
2017-08-20 12:41:17 +00:00
DA_Init();
// initialize Watchdog output
Configure_GPIO(GPIOA,12,GPPP_OUTPUT_50MHZ);
// initialize variables & arrays in rte_wx
rte_wx_init();
2017-08-20 12:41:17 +00:00
#ifdef _METEO
// initialize sensor power control and switch off supply voltage
wx_pwr_init();
// call periodic handle to wait for 1 second and then switch on voltage
wx_pwr_periodic_handle();
// initialize humidity sensor
dht22_init();
#ifndef _DALLAS_SPLIT_PIN
dallas_init(GPIOC, GPIO_Pin_6, GPIO_PinSource6, &rte_wx_dallas_average);
#else
dallas_init(GPIOC, GPIO_Pin_11, GPIO_PinSource11, &rte_wx_dallas_average);
#endif
#if defined(_UMB_MASTER)
// UMB client cannot be used in the same time with TX20 or analogue anemometer
#undef _ANEMOMETER_TX20
#undef _ANEMOMETER_ANALOGUE
// client initialization
2020-03-18 20:56:02 +00:00
umb_master_init(&rte_wx_umb_context);
#endif
#ifdef _ANEMOMETER_TX20
tx20_init();
#endif
#ifdef _ANEMOMETER_ANALOGUE
analog_anemometer_init(10, 10, 100, 1);
#endif
#endif
#ifdef _DALLAS_AS_TELEM
#ifndef _DALLAS_SPLIT_PIN
dallas_init(GPIOC, GPIO_Pin_6, GPIO_PinSource6, &rte_wx_dallas_average);
#else
dallas_init(GPIOC, GPIO_Pin_11, GPIO_PinSource11, &rte_wx_dallas_average);
#endif
2017-08-20 12:41:17 +00:00
#endif
2019-01-26 22:18:25 +00:00
// initializing UART drvier
srl_init();
2017-08-20 12:41:17 +00:00
// configuring interrupt priorities
it_handlers_set_priorities();
2017-08-20 12:41:17 +00:00
#ifdef _METEO
2019-01-26 17:02:19 +00:00
ms5611_reset(&rte_wx_ms5611_qf);
ms5611_read_calibration(SensorCalData, &rte_wx_ms5611_qf);
ms5611_trigger_measure(0, 0);
2017-08-20 12:41:17 +00:00
#endif
2019-01-12 13:44:16 +00:00
// preparing initial beacon which will be sent to host PC using KISS protocol via UART
2019-01-27 11:34:43 +00:00
main_own_aprs_msg_len = sprintf(main_own_aprs_msg, "=%07.2f%c%c%08.2f%c%c %s", (float)_LAT, _LATNS, _SYMBOL_F, (float)_LON, _LONWE, _SYMBOL_S, _COMMENT);
2019-01-12 13:44:16 +00:00
// terminating the aprs message
2019-01-27 11:34:43 +00:00
main_own_aprs_msg[main_own_aprs_msg_len] = 0;
2019-01-12 13:44:16 +00:00
// 'sending' the message which will only encapsulate it inside AX25 protocol (ax25_starttx is not called here)
//ax25_sendVia(&main_ax25, main_own_path, (sizeof(main_own_path) / sizeof(*(main_own_path))), main_own_aprs_msg, main_own_aprs_msg_len);
ln = ax25_sendVia_toBuffer(main_own_path, (sizeof(main_own_path) / sizeof(*(main_own_path))), main_own_aprs_msg, main_own_aprs_msg_len, srl_tx_buffer, TX_BUFFER_LN);
2019-01-12 13:44:16 +00:00
// SendKISSToHost function cleares the output buffer hence routine need to wait till the UART will be ready for next transmission.
// Here this could be omitted because UART isn't used before but general idea
2019-01-13 14:57:59 +00:00
while(srl_tx_state != SRL_TX_IDLE && srl_tx_state != SRL_TX_ERROR);
2019-01-12 13:44:16 +00:00
// converting AX25 with beacon to KISS format
//ln = SendKISSToHost(main_afsk.tx_buf + 1, main_afsk.tx_fifo.tail - main_afsk.tx_fifo.head - 4, srl_tx_buffer, TX_BUFFER_LN);
2019-01-12 13:44:16 +00:00
// checking if KISS-framing was done correctly
if (ln != KISS_TOO_LONG_FRM) {
#ifdef SERIAL_TX_TEST_MODE
// infinite loop for testing UART transmission
for (;;) {
2019-01-13 14:57:59 +00:00
retval = srl_receive_data(100, FEND, FEND, 0, 0, 0);
#endif
retval = srl_start_tx(ln);
2019-01-12 13:44:16 +00:00
#ifdef SERIAL_TX_TEST_MODE
2019-01-13 14:57:59 +00:00
while(srl_tx_state != SRL_TX_IDLE);
2020-03-17 21:52:10 +00:00
// while(srl_rx_state != SRL_RX_DONE);
2019-01-13 14:57:59 +00:00
2019-03-13 18:54:01 +00:00
GPIOC->ODR = (GPIOC->ODR ^ GPIO_Pin_9);
2019-01-13 14:57:59 +00:00
if (srl_rx_state == SRL_RX_DONE) {
2019-03-13 18:54:01 +00:00
GPIOC->ODR = (GPIOC->ODR ^ GPIO_Pin_8);
2019-01-13 14:57:59 +00:00
retval = 200;
}
2019-01-12 13:44:16 +00:00
}
#endif
}
// reinitializing AFSK and AX25 driver
2019-01-26 22:18:25 +00:00
AFSK_Init(&main_afsk);
2017-08-20 12:41:17 +00:00
ADCStartConfig();
DACStartConfig();
2019-01-26 22:18:25 +00:00
AFSK_Init(&main_afsk);
ax25_init(&main_ax25, &main_afsk, 0, message_callback);
2017-08-20 12:41:17 +00:00
2019-01-26 17:02:19 +00:00
// getting all meteo measuremenets to be sure that WX frames want be sent with zeros
wx_get_all_measurements();
2017-08-20 12:41:17 +00:00
#if defined _VICTRON && !defined _UMB_MASTER
2019-04-03 09:41:37 +00:00
// initializing protocol parser
ve_direct_parser_init(&rte_pv_struct, &rte_pv_average);
// enabling timeout handling for serial port. This is required because VE protocol frame may vary in lenght
// and serial port driver could finish reception only either on stop character or when declared number of bytes
// has been received.
srl_switch_timeout(1, 100);
// switching UART to receive mode to be ready for data from charging controller
srl_receive_data(VE_DIRECT_MAX_FRAME_LN, 0x0D, 0, 0, 0, 0);
#elif !defined _VICTRON && defined _UMB_MASTER
2020-03-18 20:56:02 +00:00
// srl_receive_data(8, SOH, 0x00, 0, 6, 12);
#elif ! defined _VICTRON && !defined _UMB_MASTER
2019-01-26 22:18:25 +00:00
// switching UART to receive mode to be ready for KISS frames from host
2019-01-26 17:02:19 +00:00
srl_receive_data(100, FEND, FEND, 0, 0, 0);
2019-04-03 09:41:37 +00:00
#endif
2017-08-20 12:41:17 +00:00
GPIO_ResetBits(GPIOC, GPIO_Pin_8 | GPIO_Pin_9);
2019-01-26 22:18:25 +00:00
// configuting system timers
2018-04-20 21:23:50 +00:00
TimerConfig();
2017-08-20 12:41:17 +00:00
#ifdef _BCN_ON_STARTUP
SendStartup();
2017-08-20 12:41:17 +00:00
#endif
#if defined(_UMB_MASTER)
2020-03-17 21:52:10 +00:00
umb_0x26_status_request(&rte_wx_umb, &rte_wx_umb_context);
#endif
2020-03-17 21:52:10 +00:00
2017-08-20 12:41:17 +00:00
// Infinite loop
while (1)
{
if (rte_main_reboot_req == 1)
NVIC_SystemReset();
2017-08-20 12:41:17 +00:00
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
2019-01-27 13:23:20 +00:00
if (main_afsk.sending == false && button_inhibit == 0) {
while(main_ax25.dcd == true);
#ifndef _METEO
2019-01-27 13:23:20 +00:00
//telemetry_send_values(rx10m, tx10m, digi10m, kiss10m, rte_wx_temperature_dallas_valid, rte_wx_dallas_qf, rte_wx_ms5611_qf, rte_wx_dht.qf);
SendOwnBeacon();
#else
SendWXFrame(rte_wx_average_windspeed, rte_wx_max_windspeed, rte_wx_average_winddirection, rte_wx_temperature_dallas_valid, rte_wx_pressure_valid);
#endif // #ifndef _METEO
2019-01-27 13:23:20 +00:00
}
button_inhibit = 1;
2017-08-20 12:41:17 +00:00
}
else {
2019-01-27 13:23:20 +00:00
button_inhibit = 0;
2017-08-20 12:41:17 +00:00
}
2019-01-26 22:18:25 +00:00
// if new packet has been received from radio channel
if(ax25_new_msg_rx_flag == 1) {
memset(srl_tx_buffer, 0x00, sizeof(srl_tx_buffer));
2019-01-26 22:18:25 +00:00
// convert message to kiss format and send it to host
srl_start_tx(SendKISSToHost(ax25_rxed_frame.raw_data, (ax25_rxed_frame.raw_msg_len - 2), srl_tx_buffer, TX_BUFFER_LN));
2018-04-20 21:23:50 +00:00
2019-01-26 22:18:25 +00:00
main_ax25.dcd = false;
2017-08-20 12:41:17 +00:00
#ifdef _DBG_TRACE
trace_printf("APRS-RF:RadioPacketFrom=%.6s-%d,FirstPathEl=%.6s-%d\r\n", ax25_rxed_frame.src.call, ax25_rxed_frame.src.ssid, ax25_rxed_frame.rpt_lst[0].call, ax25_rxed_frame.rpt_lst[0].ssid);
2017-08-20 12:41:17 +00:00
#endif
#ifdef _DIGI
2019-01-26 22:18:25 +00:00
// check if this packet needs to be repeated (digipeated) and do it if it is neccessary
Digi(&ax25_rxed_frame);
2017-08-20 12:41:17 +00:00
#endif
ax25_new_msg_rx_flag = 0;
2017-08-20 12:41:17 +00:00
rx10m++;
}
#if defined _VICTRON
2019-01-26 22:18:25 +00:00
// if new KISS message has been received from the host
2019-04-03 09:41:37 +00:00
if (srl_rx_state == SRL_RX_DONE || srl_rx_state == SRL_RX_ERROR) {
2019-01-26 22:18:25 +00:00
2019-04-03 09:41:37 +00:00
// cutting received string to Checksum, everything after will be skipped
ve_direct_cut_to_checksum(srl_get_rx_buffer(), RX_BUFFER_LN, &buffer_len);
2019-04-03 09:41:37 +00:00
// checking if this frame is ok
ve_direct_validate_checksum(srl_get_rx_buffer(), buffer_len, &retval);
if (retval == 1) {
// parsing data from input serial buffer to
retval = ve_direct_parse_to_raw_struct(srl_get_rx_buffer(), buffer_len, &rte_pv_struct);
if (retval == 0) {
ve_direct_add_to_average(&rte_pv_struct, &rte_pv_average);
ve_direct_get_averages(&rte_pv_average, &rte_pv_battery_current, &rte_pv_battery_voltage, &rte_pv_cell_voltage, &rte_pv_load_current);
ve_direct_set_sys_voltage(&rte_pv_struct, &rte_pv_sys_voltage);
2019-04-04 17:57:21 +00:00
ve_direct_store_errors(&rte_pv_struct, &rte_pv_last_error);
2019-04-03 09:41:37 +00:00
}
}
srl_receive_data(VE_DIRECT_MAX_FRAME_LN, 0x0D, 0, 0, 0, 0);
}
#elif defined _UMB_MASTER
// if some UMB data have been received
if (srl_rx_state == SRL_RX_DONE) {
2020-03-29 07:12:23 +00:00
umb_pooling_handler(&rte_wx_umb_context, REASON_RECEIVE_IDLE, master_time);
}
// if there were an error during receiving frame from host, restart rxing once again
if (srl_rx_state == SRL_RX_ERROR) {
2020-03-29 07:12:23 +00:00
umb_pooling_handler(&rte_wx_umb_context, REASON_RECEIVE_ERROR, master_time);
2020-03-07 18:13:24 +00:00
}
2020-03-17 21:52:10 +00:00
if (srl_tx_state == SRL_TX_IDLE) {
2020-03-29 07:12:23 +00:00
umb_pooling_handler(&rte_wx_umb_context, REASON_TRANSMIT_IDLE, master_time);
}
2019-04-03 09:41:37 +00:00
#else
// if new KISS message has been received from the host
if (srl_rx_state == SRL_RX_DONE) {
2019-01-26 22:18:25 +00:00
// parse incoming data and then transmit on radio freq
short res = ParseReceivedKISS(srl_get_rx_buffer(), srl_get_num_bytes_rxed(), &main_ax25, &main_afsk);
if (res == 0)
2019-01-26 22:18:25 +00:00
kiss10m++; // increase kiss messages counter
2019-01-26 22:18:25 +00:00
// restart KISS receiving to be ready for next frame
srl_receive_data(120, FEND, FEND, 0, 0, 0);
2017-08-20 12:41:17 +00:00
}
2019-01-26 22:18:25 +00:00
// if there were an error during receiving frame from host, restart rxing once again
if (srl_rx_state == SRL_RX_ERROR) {
srl_receive_data(120, FEND, FEND, 0, 0, 0);
}
2019-04-03 09:41:37 +00:00
#endif
2019-01-26 22:18:25 +00:00
// get all meteo measuremenets each 65 seconds. some values may not be
// downloaded from sensors if _METEO and/or _DALLAS_AS_TELEM aren't defined
if (main_wx_sensors_pool_timer < 10) {
2019-01-12 13:44:16 +00:00
2019-01-26 17:02:19 +00:00
wx_get_all_measurements();
2019-01-26 22:18:25 +00:00
main_wx_sensors_pool_timer = 65500;
2018-04-18 23:00:44 +00:00
}
2019-01-26 17:02:19 +00:00
2019-01-27 13:23:20 +00:00
if (main_packet_tx_pool_timer < 10) {
packet_tx_handler();
main_packet_tx_pool_timer = 60000;
}
if (main_one_second_pool_timer < 10) {
#ifdef _ANEMOMETER_ANALOGUE
analog_anemometer_direction_handler();
#endif
main_one_second_pool_timer = 1000;
}
else if (main_one_second_pool_timer < -10) {
#ifdef _ANEMOMETER_ANALOGUE
analog_anemometer_direction_reset();
#endif
main_one_second_pool_timer = 1000;
}
if (main_two_second_pool_timer < 10) {
wx_pwr_periodic_handle();
main_two_second_pool_timer = 2000;
}
if (main_ten_second_pool_timer < 10) {
#if defined(_UMB_MASTER)
umb_channel_pool(&rte_wx_umb, &rte_wx_umb_context);
rte_wx_umb_qf = umb_get_current_qf(&rte_wx_umb_context, master_time);
#endif
wx_pool_anemometer();
main_ten_second_pool_timer = 10000;
GPIOA->ODR = (GPIOA->ODR ^ GPIO_Pin_12);
}
2019-01-26 22:18:25 +00:00
#ifdef _METEO
2019-01-26 17:02:19 +00:00
// dht22 sensor communication pooling
wx_pool_dht22();
2019-01-12 13:44:16 +00:00
#endif
2017-08-20 12:41:17 +00:00
}
// Infinite loop, never return.
}
uint16_t main_get_adc_sample(void) {
return (uint16_t) ADC1->DR;
}
2019-01-27 13:23:20 +00:00
2017-08-20 12:41:17 +00:00
#pragma GCC diagnostic pop
// ----------------------------------------------------------------------------