From ba6fff941e57dfaa0ce0206d663975b76d657251 Mon Sep 17 00:00:00 2001 From: finga Date: Fri, 22 Oct 2021 15:40:14 +0200 Subject: [PATCH] Add possibility to set PLL in gui - Add registers where PLL_A (MSNA) and PLL_B (MSNB) start. - Create screens to edit the PLLs and add their entries to the home screen. - Use a struct for keeping the PLL configs. - For editing such big numbers make the digits selctable. - Create utility function for calculating 10 to the power of `n`. --- firmware/src/main.c | 404 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 400 insertions(+), 4 deletions(-) diff --git a/firmware/src/main.c b/firmware/src/main.c index 1f5699a..40796d8 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -25,6 +25,10 @@ #define SI5351_ADDRESS 0x60 #define SI5351_REGISTER_3_OUTPUT_ENABLE_CONTROL 3 +// ... +#define SI5351_REGISTER_26_MSNA 26 +#define SI5351_REGISTER_34_MSNB 34 +// ... #define SI5351_REGISTER_177_PLL_RESET 177 #define SYM_ENTRY(SYM) { SYM, sizeof(SYM) / 2 } @@ -165,15 +169,30 @@ static const struct symbol symbol_table[] = { SYM_ENTRY(sym_0), enum input { cw, ccw, click, hold }; -static volatile enum state { home, ch1, ch2, ch3, setup } current_state = home; +static volatile enum state { home, ch1, ch2, ch3, plla, pllb, setup } current_state = home; static volatile enum state home_state = ch1; static volatile enum setup_state { contrast, backlight, back, change_contrast, change_backlight } setup_state = contrast; +static volatile enum pll_state { integer, numerator, denominator, pll_back, change_integer, change_numerator, change_denominator } pll_state = integer; static volatile uint8_t enc = 0; +static volatile uint8_t select_offset = 0; +static volatile uint8_t digit_select = 0; +static volatile uint8_t max_digits = 0; +static volatile bool digit_selected = false; static volatile uint8_t value_contrast; static volatile uint8_t value_backlight; +struct pll_config { + uint8_t reg; + uint32_t integer; + uint32_t numerator; + uint32_t denominator; +}; + +static volatile struct pll_config plla_config = { SI5351_REGISTER_26_MSNA, 0, 0, 0 }; +static volatile struct pll_config pllb_config = { SI5351_REGISTER_34_MSNB, 0, 0, 0 }; + static const uint8_t m_si5351_regs_15to92_149to170[100][2] = { // Init {3, 0xFF}, @@ -316,6 +335,15 @@ static const uint8_t m_si5351_regs_custom[51][2] = { {64, 0x00}, {65, 0x00}}; +static uint32_t ten_pow(const uint8_t exponent) { + uint32_t result = 1; + + for (uint8_t i = 0; i < exponent; i++) + result *= 10; + + return result; +} + static void spi_init(void) { SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS); SPI_PORT |= (1 << SPI_SS); @@ -469,6 +497,51 @@ static void lcd_write_integer_page(const uint32_t integer, } } +static void lcd_write_selected_integer_page(const uint32_t integer, + const uint8_t digits, + const uint8_t page, + const bool invert) { + max_digits = digits; + + if (digits != 0 || integer != 0) { + uint8_t input_digits = 0; + uint32_t comperator = 1; + + // Get digits + for (; comperator <= integer; comperator *= 10, input_digits++); + lcd_write_kerning(1, invert); + + // Print leading zeroes + for (int8_t i = digits - input_digits; i > 0; i--) { + if (input_digits + i - 1 != digit_select) { + lcd_write_kerning(1, invert); + lcd_write_digit_page(0, page, invert); + lcd_write_kerning(1, invert); + } else { + lcd_write_kerning(1, !invert); + lcd_write_digit_page(0, page, !invert); + lcd_write_kerning(1, !invert); + } + } + + // Print number itself + for (; comperator >= 10; comperator /= 10, input_digits--) { + if (input_digits - 1 != digit_select) { + lcd_write_kerning(1, invert); + lcd_write_digit_page((integer % comperator) / (comperator / 10), + page, invert); + lcd_write_kerning(1, invert); + } else { + lcd_write_kerning(1, !invert); + + lcd_write_digit_page((integer % comperator) / (comperator / 10), + page, !invert); + lcd_write_kerning(1, !invert); + } + } + } +} + static void twi_error(bool inverted) { if (inverted) lcd_fill(0xFF); @@ -587,6 +660,8 @@ static void draw_home(void) { bool ch1_selected = false; bool ch2_selected = false; bool ch3_selected = false; + bool plla_selected = false; + bool pllb_selected = false; bool setup_selected = false; switch (home_state) { @@ -599,6 +674,12 @@ static void draw_home(void) { case ch3: ch3_selected = true; break; + case plla: + plla_selected = true; + break; + case pllb: + pllb_selected = true; + break; default: // setup setup_selected = true; break; @@ -617,12 +698,144 @@ static void draw_home(void) { lcd_write_string_page("CH3\0", 2, i, ch3_selected); lcd_write_kerning(2, ch3_selected); - lcd_move_cursor(33, 6 + i); + lcd_move_cursor(0, 6 + i); + lcd_write_kerning(1, plla_selected); + lcd_write_string_page("PLL\0", 1, i, plla_selected); + + lcd_write_string_page("A\0", 1, i, plla_selected); + lcd_write_kerning(2, plla_selected); + + lcd_write_kerning(1, false); + + lcd_write_kerning(1, pllb_selected); + lcd_write_string_page("PLL\0", 1, i, pllb_selected); + + lcd_write_string_page("B\0", 1, i, pllb_selected); + lcd_write_kerning(2, pllb_selected); + + lcd_move_cursor(61, 6 + i); lcd_write_string_page("SETUP\0", 2, i, setup_selected); lcd_write_kerning(2, setup_selected); } } +static void draw_pll(void) { + lcd_fill(0x00); + + volatile struct pll_config* pll_config = &plla_config; + + switch (current_state) { + case plla: + pll_config = &plla_config; + break; + case pllb: + pll_config = &pllb_config; + break; + default: + break; + } + + bool integer_selected = false; + bool numerator_selected = false; + bool denominator_selected = false; + bool back_selected = false; + bool change_integer_selected = false; + bool change_numerator_selected = false; + bool change_denominator_selected = false; + + switch (pll_state) { + case integer: + integer_selected = true; + break; + case numerator: + numerator_selected = true; + break; + case denominator: + denominator_selected = true; + break; + case pll_back: + back_selected = true; + break; + case change_integer: + change_integer_selected = true; + break; + case change_numerator: + change_numerator_selected = true; + break; + case change_denominator: + change_denominator_selected = true; + break; + } + + for (uint8_t i = 0; i < 2; i++) { + lcd_move_cursor(0, i); + lcd_write_kerning(33, true); + lcd_write_string_page("PLL\0", 2, i, true); + lcd_write_kerning(4, true); + switch (current_state) { + case plla: + lcd_write_string_page("A\0", 2, i, true); + break; + case pllb: + lcd_write_string_page("B\0", 2, i, true); + break; + default: + break; + } + lcd_write_kerning(35, true); + } + + if (select_offset == 0) + for (uint8_t i = 0; i < 2; i++) { + lcd_move_cursor(0, 2 + i); + lcd_write_kerning(1, integer_selected); + lcd_write_string_page("INTEGER:\0", 1, i, integer_selected); + lcd_write_kerning(2, integer_selected); + lcd_write_kerning(7, false); + if (change_integer_selected) + lcd_write_selected_integer_page(pll_config->integer, 6, + i, false); + else + lcd_write_integer_page(pll_config->integer, 6, i, false); + lcd_write_kerning(2, false); + } + + for (uint8_t i = 0; i < 2; i++) { + lcd_move_cursor(0, 4 + i - select_offset * 2); + lcd_write_kerning(1, numerator_selected); + lcd_write_string_page("NUM:\0", 1, i, numerator_selected); + lcd_write_kerning(2, numerator_selected); + lcd_write_kerning(21, false); + if (change_numerator_selected) + lcd_write_selected_integer_page(pll_config->numerator, 7, + i, false); + else + lcd_write_integer_page(pll_config->numerator, 7, i, false); + lcd_write_kerning(2, false); + } + + for (uint8_t i = 0; i < 2; i++) { + lcd_move_cursor(0, 6 + i - select_offset * 2); + lcd_write_kerning(1, denominator_selected); + lcd_write_string_page("DENOM:\0", 1, i, denominator_selected); + lcd_write_kerning(2, denominator_selected); + lcd_write_kerning(8, false); + if (change_denominator_selected) + lcd_write_selected_integer_page(pll_config->denominator, 7, + i, false); + else + lcd_write_integer_page(pll_config->denominator, 7, i, false); + lcd_write_kerning(2, false); + } + + if (select_offset == 1) + for (uint8_t i = 0; i < 2; i++) { + lcd_move_cursor(33, 6 + i); + lcd_write_string_page("BACK\0", 2, i, back_selected); + lcd_write_kerning(2, back_selected); + } +} + static void draw_setup(void) { lcd_fill(0x00); @@ -687,8 +900,8 @@ static void draw_setup(void) { static void change_state(const enum state new_state) { switch (new_state) { case home: - draw_home(); current_state = home; + draw_home(); break; case ch1: break; @@ -696,10 +909,22 @@ static void change_state(const enum state new_state) { break; case ch3: break; + case plla: + select_offset = 0; + pll_state = integer; + current_state = plla; + draw_pll(); + break; + case pllb: + select_offset = 0; + pll_state = integer; + current_state = pllb; + draw_pll(); + break; case setup: setup_state = contrast; - draw_setup(); current_state = setup; + draw_setup(); break; } } @@ -726,6 +951,171 @@ static void update_home(const enum input event) { } } +static void update_pll(const enum input event) { + volatile struct pll_config* pll_config = &plla_config; + + switch (current_state) { + case plla: + pll_config = &plla_config; + break; + case pllb: + pll_config = &pllb_config; + break; + default: + break; + } + + switch (event) { + case cw: + switch (pll_state) { + case integer: + case numerator: + case denominator: + case pll_back: + pll_state++; + if (pll_state > denominator) + select_offset = 1; + if (pll_state > pll_back) { + select_offset = 0; + pll_state = integer; + } + break; + case change_integer: + if (digit_selected) + pll_config->integer += ten_pow(digit_select); + else + if (--digit_select > max_digits) + digit_select = max_digits - 1; + break; + case change_numerator: + if (digit_selected) + pll_config->numerator += ten_pow(digit_select); + else + if (--digit_select > max_digits) + digit_select = max_digits - 1; + break; + case change_denominator: + if (digit_selected) + pll_config->denominator += ten_pow(digit_select); + else + if (--digit_select > max_digits) + digit_select = max_digits - 1; + break; + } + draw_pll(); + break; + case ccw: + switch (pll_state) { + case integer: + case numerator: + case denominator: + case pll_back: + pll_state--; + if (pll_state < numerator) + select_offset = 0; + if (pll_state > pll_back) { + select_offset = 1; + pll_state = pll_back; + } + break; + case change_integer: + // TODO: Fix going back to zero + if (digit_selected) + pll_config->integer -= ten_pow(digit_select); + else + if (++digit_select >= max_digits) + digit_select = 0; + break; + case change_numerator: + // TODO: Fix going back to zero + if (digit_selected) + pll_config->numerator -= ten_pow(digit_select); + else + if (++digit_select >= max_digits) + digit_select = 0; + break; + case change_denominator: + // TODO: Fix going back to zero + if (digit_selected) + pll_config->denominator -= ten_pow(digit_select); + else + if (++digit_select >= max_digits) + digit_select = 0; + break; + } + draw_pll(); + break; + case click: + switch (pll_state) { + case integer: + pll_state = change_integer; + draw_pll(); + break; + case numerator: + pll_state = change_numerator; + draw_pll(); + break; + case denominator: + pll_state = change_denominator; + draw_pll(); + break; + case pll_back: + change_state(home); + break; + case change_integer: + if (digit_selected) { + // TODO: Save/write new value + digit_selected = false; + digit_select = 0; + } else + digit_selected = true; + break; + case change_numerator: + if (digit_selected) { + // TODO: Save/write new value + digit_selected = false; + digit_select = 0; + } else + digit_selected = true; + break; + case change_denominator: + if (digit_selected) { + // TODO: Save/write new value + digit_selected = false; + digit_select = 0; + } else + digit_selected = true; + break; + } + break; + case hold: + switch (pll_state) { + case integer: + case numerator: + case denominator: + case pll_back: + change_state(home); + break; + case change_integer: + digit_selected = false; + pll_state = integer; + draw_pll(); + break; + case change_numerator: + digit_selected = false; + pll_state = integer; + draw_pll(); + break; + case change_denominator: + digit_selected = false; + pll_state = integer; + draw_pll(); + break; + } + break; + } +} + static void update_setup(const enum input event) { switch (event) { case cw: @@ -836,6 +1226,12 @@ static void update_state(const enum input event) { break; case ch3: break; + case plla: + update_pll(event); + break; + case pllb: + update_pll(event); + break; case setup: update_setup(event); break;