Skip to content

Commit

Permalink
clean up main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
ligenxxxx committed Oct 24, 2023
1 parent 500999a commit eca31c7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 121 deletions.
118 changes: 118 additions & 0 deletions src/hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sfr_ext.h"
#include "smartaudio_protocol.h"
#include "spi.h"
#include "tramp_protocol.h"
#include "uart.h"

uint8_t KEYBOARD_ON = 0; // avoid conflict between keyboard and cam_control
Expand Down Expand Up @@ -1513,4 +1514,121 @@ void vtx_paralized(void) {
LED_Flip();
WAIT(50);
}
}

void timer_task() {
static uint16_t cur_ms10x_1sd16 = 0, last_ms10x_1sd16 = 0;
cur_ms10x_1sd16 = timer_ms10x;

if (((cur_ms10x_1sd16 - last_ms10x_1sd16) >= TIMER0_1SD16) || (cur_ms10x_1sd16 < last_ms10x_1sd16)) {
last_ms10x_1sd16 = cur_ms10x_1sd16;
timer_cnt++;
timer_cnt &= 15;
if (timer_cnt == 15) { // every second, 1Hz
btn1_tflg = 1;
pwr_tflg = 1;
cfg_tflg = 1;
seconds++;
pwr_sflg = 1;
}

timer_2hz = ((timer_cnt & 7) == 7);
timer_4hz = ((timer_cnt & 3) == 3);
timer_8hz = ((timer_cnt & 1) == 1);
timer_16hz = 1;
} else {
timer_2hz = 0;
timer_4hz = 0;
timer_8hz = 0;
timer_16hz = 0;
}
}

void RF_Delay_Init() {
static uint8_t SA_saved = 0;

#ifdef _RF_CALIB
return;
#endif

if (tramp_lock)
return;

if (SA_saved == 0) {
if (seconds >= WAIT_SA_CONFIG) {
I2C_Write8(ADDR_EEPROM, EEP_ADDR_SA_LOCK, SA_lock);
SA_saved = 1;
#ifdef _DEBUG_MODE
debugf("\r\nSave SA_lock(%x) to EEPROM", (uint16_t)SA_lock);
#endif
}
}

// init_rf
if (seconds < WAIT_SA_CONFIG) { // wait for SA config vtx
if (seconds < WAIT_SA_LOCK)
return;
else if (SA_lock)
return;
else
seconds = WAIT_SA_CONFIG;
} else if (rf_delay_init_done)
return;
else if (dm6300_init_done)
return;
else
rf_delay_init_done = 1;

if (last_SA_lock) {
#ifdef _DEBUG_MODE
debugf("\r\nRF_Delay_Init: SA");
#endif
pwr_lmt_sec = PWR_LMT_SEC;
if (SA_lock) {
if (pwr_init == POWER_MAX + 2) { // 0mW
RF_POWER = POWER_MAX + 2;
cur_pwr = POWER_MAX + 2;
} else if (PIT_MODE) {
Init_6300RF(ch_init, POWER_MAX + 1);
#ifdef _DEBUG_MODE
debugf("\r\n ch%x, pwr%x", (uint16_t)ch_init, (uint16_t)cur_pwr);
#endif
} else {
Init_6300RF(ch_init, pwr_init);
#ifdef _DEBUG_MODE
debugf("\r\n ch%x, pwr%x", (uint16_t)ch_init, (uint16_t)cur_pwr);
#endif
}
DM6300_AUXADC_Calib();
}
} else if (!mspVtxLock) {
#ifdef _DEBUG_MODE
debugf("\r\nRF_Delay_Init: None");
#endif
if (TEAM_RACE == 0x01)
vtx_paralized();
#if (0)
if (PIT_MODE == PIT_0MW) {
pwr_lmt_done = 1;
RF_POWER = POWER_MAX + 2;
cur_pwr = POWER_MAX + 2;
vtx_pit = PIT_0MW;
} else if (PIT_MODE == PIT_P1MW) {
#else
if (PIT_MODE != PIT_OFF) {
#endif
Init_6300RF(RF_FREQ, POWER_MAX + 1);
vtx_pit = PIT_P1MW;
} else {
WriteReg(0, 0x8F, 0x00);
WriteReg(0, 0x8F, 0x01);
DM6300_Init(RF_FREQ, RF_BW);
DM6300_SetChannel(RF_FREQ);
DM6300_SetPower(0, RF_FREQ, 0);
cur_pwr = RF_POWER;
WriteReg(0, 0x8F, 0x11);
rf_delay_init_done = 1;
}
DM6300_AUXADC_Calib();
}
}
1 change: 1 addition & 0 deletions src/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ uint8_t temperature_level(void);
void vtx_paralized(void);

void timer_task();
void RF_Delay_Init();

#if defined HDZERO_FREESTYLE || HDZERO_FREESTYLE_V2
extern uint8_t powerLock;
Expand Down
121 changes: 0 additions & 121 deletions src/mcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
uint8_t UNUSED = 0;
uint8_t rf_delay_init_done = 0;

void timer_task();
void RF_Delay_Init();

BIT_TYPE int0_req = 0;
BIT_TYPE int1_req = 0;

Expand Down Expand Up @@ -173,124 +170,6 @@ void main(void) {
uart_baudrate_detect();
runcam_shutter_fix(seconds);
}

RF_Delay_Init();
}
}

void timer_task() {
static uint16_t cur_ms10x_1sd16 = 0, last_ms10x_1sd16 = 0;
cur_ms10x_1sd16 = timer_ms10x;

if (((cur_ms10x_1sd16 - last_ms10x_1sd16) >= TIMER0_1SD16) || (cur_ms10x_1sd16 < last_ms10x_1sd16)) {
last_ms10x_1sd16 = cur_ms10x_1sd16;
timer_cnt++;
timer_cnt &= 15;
if (timer_cnt == 15) { // every second, 1Hz
btn1_tflg = 1;
pwr_tflg = 1;
cfg_tflg = 1;
seconds++;
pwr_sflg = 1;
}

timer_2hz = ((timer_cnt & 7) == 7);
timer_4hz = ((timer_cnt & 3) == 3);
timer_8hz = ((timer_cnt & 1) == 1);
timer_16hz = 1;
} else {
timer_2hz = 0;
timer_4hz = 0;
timer_8hz = 0;
timer_16hz = 0;
}
}

void RF_Delay_Init() {
static uint8_t SA_saved = 0;

#ifdef _RF_CALIB
return;
#endif

if (tramp_lock)
return;

if (SA_saved == 0) {
if (seconds >= WAIT_SA_CONFIG) {
I2C_Write8(ADDR_EEPROM, EEP_ADDR_SA_LOCK, SA_lock);
SA_saved = 1;
#ifdef _DEBUG_MODE
debugf("\r\nSave SA_lock(%x) to EEPROM", (uint16_t)SA_lock);
#endif
}
}

// init_rf
if (seconds < WAIT_SA_CONFIG) { // wait for SA config vtx
if (seconds < WAIT_SA_LOCK)
return;
else if (SA_lock)
return;
else
seconds = WAIT_SA_CONFIG;
} else if (rf_delay_init_done)
return;
else if (dm6300_init_done)
return;
else
rf_delay_init_done = 1;

if (last_SA_lock) {
#ifdef _DEBUG_MODE
debugf("\r\nRF_Delay_Init: SA");
#endif
pwr_lmt_sec = PWR_LMT_SEC;
if (SA_lock) {
if (pwr_init == POWER_MAX + 2) { // 0mW
RF_POWER = POWER_MAX + 2;
cur_pwr = POWER_MAX + 2;
} else if (PIT_MODE) {
Init_6300RF(ch_init, POWER_MAX + 1);
#ifdef _DEBUG_MODE
debugf("\r\n ch%x, pwr%x", (uint16_t)ch_init, (uint16_t)cur_pwr);
#endif
} else {
Init_6300RF(ch_init, pwr_init);
#ifdef _DEBUG_MODE
debugf("\r\n ch%x, pwr%x", (uint16_t)ch_init, (uint16_t)cur_pwr);
#endif
}
DM6300_AUXADC_Calib();
}
} else if (!mspVtxLock) {
#ifdef _DEBUG_MODE
debugf("\r\nRF_Delay_Init: None");
#endif
if (TEAM_RACE == 0x01)
vtx_paralized();
#if (0)
if (PIT_MODE == PIT_0MW) {
pwr_lmt_done = 1;
RF_POWER = POWER_MAX + 2;
cur_pwr = POWER_MAX + 2;
vtx_pit = PIT_0MW;
} else if (PIT_MODE == PIT_P1MW) {
#else
if (PIT_MODE != PIT_OFF) {
#endif
Init_6300RF(RF_FREQ, POWER_MAX + 1);
vtx_pit = PIT_P1MW;
} else {
WriteReg(0, 0x8F, 0x00);
WriteReg(0, 0x8F, 0x01);
DM6300_Init(RF_FREQ, RF_BW);
DM6300_SetChannel(RF_FREQ);
DM6300_SetPower(0, RF_FREQ, 0);
cur_pwr = RF_POWER;
WriteReg(0, 0x8F, 0x11);
rf_delay_init_done = 1;
}
DM6300_AUXADC_Calib();
}
}

0 comments on commit eca31c7

Please sign in to comment.