Skip to content

Commit

Permalink
Less RTC cal code size
Browse files Browse the repository at this point in the history
Use float values in UI for set/display RTC cal value
  • Loading branch information
DiSlord committed Apr 7, 2024
1 parent d7358eb commit 8b066c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
27 changes: 6 additions & 21 deletions NANOVNA_STM32_F072/rtc_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,35 +161,20 @@ void rtc_init(void){
RTC->ISR &= ~RTC_ISR_RSF;
}

bool rtc_clock_output_enabled(void) {
return (RTC->CR & RTC_CR_COE) != 0;
}

void rtc_clock_output_enable(bool en) {
if (en)
RTC->CR |= RTC_CR_COE;
else
RTC->CR &= ~RTC_CR_COE;
}

bool rtc_set_cal(int16_t ppm) {
int32_t cal = (ppm << 20) / 1000000;
void rtc_set_cal(float ppm) {
ppm*= (1<<20) / 1000000.0f;
int32_t cal = ppm > 0 ? ppm + 0.5f : ppm - 0.5f;
if ((RTC->ISR & RTC_ISR_RECALPF) != 0 || cal < -511 || cal > 512)
return false;
return;
if (cal > 0)
RTC->CALR = RTC_CALR_CALP | (512 - cal);
else
RTC->CALR = -cal;
return true;
}

int16_t rtc_get_cal(void) {
float rtc_get_cal(void) {
int32_t cal = -(RTC->CALR & RTC_CALR_CALM);
if (RTC->CALR & RTC_CALR_CALP)
cal += 512;
return (cal * 1000000) >> 20;
}

bool rtc_cal_pending(void) {
return (RTC->ISR & RTC_ISR_RECALPF) != 0;
return cal * (1000000.0f / (1<<20));
}
27 changes: 6 additions & 21 deletions NANOVNA_STM32_F303/rtc_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,35 +161,20 @@ void rtc_init(void){
RTC->ISR &= ~RTC_ISR_RSF;
}

bool rtc_clock_output_enabled(void) {
return (RTC->CR & RTC_CR_COE) != 0;
}

void rtc_clock_output_enable(bool en) {
if (en)
RTC->CR |= RTC_CR_COE;
else
RTC->CR &= ~RTC_CR_COE;
}

bool rtc_set_cal(int16_t ppm) {
int32_t cal = (ppm << 20) / 1000000;
void rtc_set_cal(float ppm) {
ppm*= (1<<20) / 1000000.0f;
int32_t cal = ppm > 0 ? ppm + 0.5f : ppm - 0.5f;
if ((RTC->ISR & RTC_ISR_RECALPF) != 0 || cal < -511 || cal > 512)
return false;
return;
if (cal > 0)
RTC->CALR = RTC_CALR_CALP | (512 - cal);
else
RTC->CALR = -cal;
return true;
}

int16_t rtc_get_cal(void) {
float rtc_get_cal(void) {
int32_t cal = -(RTC->CALR & RTC_CALR_CALM);
if (RTC->CALR & RTC_CALR_CALP)
cal += 512;
return (cal * 1000000) >> 20;
}

bool rtc_cal_pending(void) {
return (RTC->ISR & RTC_ISR_RECALPF) != 0;
return cal * (1000000.0f / (1<<20));
}
13 changes: 8 additions & 5 deletions hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ uint32_t rtc_get_dr_bin(void);
uint32_t rtc_get_FAT(void);
// Write date and time (need in bcd format!!!)
void rtc_set_time(uint32_t dr, uint32_t tr);
bool rtc_clock_output_enabled(void);
void rtc_clock_output_enable(bool en);
bool rtc_set_cal(int16_t ppm);
int16_t rtc_get_cal(void);
bool rtc_cal_pending(void);
// Toggle RTC clock output
#define rtc_clock_output_toggle() RTC->CR^= RTC_CR_COE
// Check RTC clock output
#define rtc_clock_output_enabled() (RTC->CR & RTC_CR_COE)
// Set RTC calibration value in ppm (value rounded by (1<<20)/1e6
void rtc_set_cal(float ppm);
// Get RTC calibration value in ppm
float rtc_get_cal(void);
#endif

/*
Expand Down
8 changes: 4 additions & 4 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2302,11 +2302,11 @@ static UI_FUNCTION_ADV_CALLBACK(menu_rtc_out_acb)
b->p1.text = enabled ? "ON" : "OFF";
return;
}
rtc_clock_output_enable(!enabled);
rtc_clock_output_toggle();
}

const menuitem_t menu_rtc_cal[] = {
{ MT_ADV_CALLBACK, KM_RTC_CAL, "RTC CAL\n " R_LINK_COLOR "%d" S_PPM, menu_keyboard_acb },
{ MT_ADV_CALLBACK, KM_RTC_CAL, "RTC CAL\n " R_LINK_COLOR "%b.4f " S_PPM, menu_keyboard_acb },
{ MT_ADV_CALLBACK, 0, "RTC OUT\n%s" , menu_rtc_out_acb },
{ MT_NEXT, 0, NULL, menu_back } // next-> menu_back
};
Expand Down Expand Up @@ -2869,10 +2869,10 @@ UI_KEYBOARD_CALLBACK(input_date_time) {
UI_KEYBOARD_CALLBACK(input_rtc_cal) {
(void)data;
if (b) {
b->p1.i = rtc_get_cal();
b->p1.f = rtc_get_cal();
return;
}
rtc_set_cal(keyboard_get_int());
rtc_set_cal(keyboard_get_float());
}
#endif

Expand Down

0 comments on commit 8b066c3

Please sign in to comment.