clock_generator/firmware/c/main.c
finga af3790be0d Rename folder of firmware written in c
To establish a better naming convention for firmwares written in other
languages.
2021-10-25 15:26:04 +02:00

1368 lines
45 KiB
C

#include <avr/eeprom.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/twi.h>
#include <assert.h>
#include <stdbool.h>
#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_SCK PB5
#define SPI_MOSI PB3
#define SPI_SS PB2
#define LCD_CD PB0
#define LCD_RST PB1
#define ENC_A (PINB & (1 << PB6))
#define ENC_B (PINB & (1 << PB7))
#define TWI_PORT PORTC
#define TWI_SDA PC4
#define TWI_SCL PC5
#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 }
static uint8_t EEMEM eeprom_contrast = 8;
static uint8_t EEMEM eeprom_backlight = 1;
static const __flash uint8_t sacred_chao[] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80,
0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF,
0x01, 0x0F, 0x3F, 0x4F, 0x9F, 0x3F, 0x3E, 0x3C, 0x7C, 0x7C, 0x7C, 0x7C, 0x3E, 0x3E, 0x3E, 0x1F, 0x1F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x07, 0x07, 0x03, 0x81, 0x40, 0x30, 0x0E, 0x01,
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x08, 0x10, 0x20, 0x20, 0x40, 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x20, 0x20, 0x10, 0x08, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00 };
static const __flash uint8_t onders_org[] = { 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xF8, 0x00, 0x00, 0xE0, 0xA0, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00, 0xE0, 0xA0, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00, 0xE0, 0x60, 0xE0,
0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x0F};
static const __flash uint8_t sym_0[] = { 0xF8, 0xFC, 0x0C, 0xFC, 0xF8,
0x1F, 0x3F, 0x30, 0x3F, 0x1F };
static const __flash uint8_t sym_1[] = { 0x30, 0x30, 0xFC, 0xFC, 0x00,
0x30, 0x30, 0x3F, 0x3F, 0x30 };
static const __flash uint8_t sym_2[] = { 0x18, 0x1C, 0x8C, 0xFC, 0xF8,
0x38, 0x3E, 0x3F, 0x33, 0x30 };
static const __flash uint8_t sym_3[] = { 0x18, 0x9C, 0x8C, 0xFC, 0x78,
0x18, 0x39, 0x31, 0x3F, 0x1E };
static const __flash uint8_t sym_4[] = { 0x80, 0xE0, 0x78, 0xFC, 0xFC,
0x07, 0x07, 0x06, 0x3F, 0x3F };
static const __flash uint8_t sym_5[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
0x1C, 0x3D, 0x31, 0x3F, 0x1F };
static const __flash uint8_t sym_6[] = { 0xF8, 0xFC, 0x8C, 0xBC, 0x38,
0x1F, 0x3F, 0x31, 0x3F, 0x1F };
static const __flash uint8_t sym_7[] = { 0x0C, 0x0C, 0xEC, 0xFC, 0x1C,
0x00, 0x3E, 0x3F, 0x01, 0x00 };
static const __flash uint8_t sym_8[] = { 0x78, 0xFC, 0x8C, 0xFC, 0x78,
0x1E, 0x3F, 0x31, 0x3F, 0x1E };
static const __flash uint8_t sym_9[] = { 0xF8, 0xFC, 0x8C, 0xFC, 0xF8,
0x1C, 0x3D, 0x31, 0x3F, 0x1F };
static const __flash uint8_t sym_colon[] = { 0x30, 0x30,
0x0C, 0x0C };
static const __flash uint8_t sym_a[] = { 0xC0, 0xF0, 0x3C, 0x3C, 0xF0, 0xC0,
0x3F, 0x3F, 0x06, 0x06, 0x3F, 0x3F };
static const __flash uint8_t sym_b[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0x78,
0x3F, 0x3F, 0x31, 0x31, 0x3F, 0x1E };
static const __flash uint8_t sym_c[] = { 0xF8, 0xFC, 0x0C, 0x1C, 0x18,
0x1F, 0x3F, 0x30, 0x38, 0x18 };
static const __flash uint8_t sym_d[] = { 0xFC, 0xFC, 0x0C, 0x1C, 0xF8, 0xF0,
0x3F, 0x3F, 0x30, 0x38, 0x1F, 0x0F };
static const __flash uint8_t sym_e[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0x0C,
0x3F, 0x3F, 0x31, 0x31, 0x30 };
static const __flash uint8_t sym_g[] = { 0xF8, 0xFC, 0x0C, 0x0C, 0x3C, 0x38,
0x1F, 0x3F, 0x30, 0x33, 0x3F, 0x1F };
static const __flash uint8_t sym_h[] = { 0xFC, 0xFC, 0x80, 0x80, 0xFC, 0xFC,
0x3F, 0x3F, 0x01, 0x01, 0x3F, 0x3F };
static const __flash uint8_t sym_i[] = { 0x0C, 0xFC, 0xFC, 0x0C,
0x30, 0x3F, 0x3F, 0x30 };
static const __flash uint8_t sym_k[] = { 0xFC, 0xFC, 0xC0, 0xF0, 0x7C, 0x1C,
0x3F, 0x3F, 0x03, 0x0F, 0x3E, 0x38 };
static const __flash uint8_t sym_l[] = { 0xFC, 0xFC, 0x00, 0x00, 0x00,
0x3F, 0x3F, 0x30, 0x30, 0x30 };
static const __flash uint8_t sym_m[] = { 0xFC, 0xFC, 0x38, 0x70, 0xE0, 0x70, 0x38, 0xFC, 0xFC,
0x3F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x3F };
static const __flash uint8_t sym_n[] = { 0xFC, 0xFC, 0xF0, 0xC0, 0x00, 0xFC, 0xFC,
0x3F, 0x3F, 0x00, 0x03, 0x0F, 0x3F, 0x3F };
static const __flash uint8_t sym_o[] = { 0xF8, 0xFC, 0x0C, 0x0C, 0xFC, 0xF8,
0x1F, 0x3F, 0x30, 0x30, 0x3F, 0x1F };
static const __flash uint8_t sym_p[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
0x3F, 0x3F, 0x01, 0x01, 0x01, 0x00 };
static const __flash uint8_t sym_r[] = { 0xFC, 0xFC, 0x8C, 0x8C, 0xFC, 0xF8,
0x3F, 0x3F, 0x01, 0x03, 0x3F, 0x3E };
static const __flash uint8_t sym_s[] = { 0xF8, 0xFC, 0x8C, 0x8C, 0x9C, 0x18,
0x18, 0x39, 0x31, 0x31, 0x3F, 0x1F };
static const __flash uint8_t sym_t[] = { 0x0C, 0x0C, 0xFC, 0xFC, 0x0C, 0x0C,
0x00, 0x00, 0x3F, 0x3F, 0x00, 0x00 };
static const __flash uint8_t sym_u[] = { 0xFC, 0xFC, 0x00, 0x00, 0xFC, 0xFC,
0x1F, 0x3F, 0x30, 0x30, 0x3F, 0x1F };
static const __flash uint8_t sym_underscore[] = { 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x20, 0x20, 0x20, 0x20 };
static const __flash uint8_t sym_invalid[] = { 0x80, 0xE0, 0x98, 0xCC, 0x4C, 0x18, 0xE0, 0x80,
0x01, 0x07, 0x1F, 0x24, 0x25, 0x1F, 0x07, 0x01};
struct symbol {
const __flash uint8_t* data;
const uint8_t length;
};
static const struct symbol symbol_table[] = { SYM_ENTRY(sym_0),
SYM_ENTRY(sym_1),
SYM_ENTRY(sym_2),
SYM_ENTRY(sym_3),
SYM_ENTRY(sym_4),
SYM_ENTRY(sym_5),
SYM_ENTRY(sym_6),
SYM_ENTRY(sym_7),
SYM_ENTRY(sym_8),
SYM_ENTRY(sym_9),
SYM_ENTRY(sym_colon),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_a),
SYM_ENTRY(sym_b),
SYM_ENTRY(sym_c),
SYM_ENTRY(sym_d),
SYM_ENTRY(sym_e),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_g),
SYM_ENTRY(sym_h),
SYM_ENTRY(sym_i),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_k),
SYM_ENTRY(sym_l),
SYM_ENTRY(sym_m),
SYM_ENTRY(sym_n),
SYM_ENTRY(sym_o),
SYM_ENTRY(sym_p),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_r),
SYM_ENTRY(sym_s),
SYM_ENTRY(sym_t),
SYM_ENTRY(sym_u),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_invalid),
SYM_ENTRY(sym_underscore),
};
enum input { cw, ccw, click, hold };
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},
{16, 0x4F}, /* CLK0 Control: 8mA drive, Multisynth 0 as CLK0 source, Clock
not inverted, Source = PLLA, Multisynth 0 in integer mode,
clock powered up */
{17, 0x4F}, /* CLK1 Control: 8mA drive, Multisynth 1 as CLK1 source, Clock
not inverted, Source = PLLA, Multisynth 1 in integer mode,
clock powered up */
{18, 0x6F}, /* CLK2 Control: 8mA drive, Multisynth 2 as CLK2 source, Clock
not inverted, Source = PLLB, Multisynth 2 in integer mode,
clock powered up */
{19, 0x80}, /* CLK3 Control: Not used ... clock powered down */
{20, 0x80}, /* CLK4 Control: Not used ... clock powered down */
{21, 0x80}, /* CLK5 Control: Not used ... clock powered down */
{22, 0x80}, /* CLK6 Control: Not used ... clock powered down */
{23, 0x80}, /* CLK7 Control: Not used ... clock powered down */
{24, 0x00}, /* Clock disable state 0..3 (low when disabled) */
{25, 0x00}, /* Clock disable state 4..7 (low when disabled) */
// PLL_A Setup (720MHz)
// P1: 0x00C66 = 3174
// P2: 0x00002 = 2
// P3: 0x00005 = 5
{26, 0x00}, // D7 - D0: MSNA_P3 [15:8]
{27, 0x05}, // D7 - D0: MSNA_P3 [7:0]
{28, 0x00}, // D1 - D0: MSNA_P1 [17:16]
{29, 0x0C}, // D7 - D0: MSNA_P1 [15:8]
{30, 0x66}, // D7 - D0: MSNA_P1 [7:0]
{31, 0x00}, // D7 - D4: MSNA_P3 [19:16], D3 - D0: MSNA_P2 [19:16]
{32, 0x00}, // D7 - D0: MSNA_P2 [15:8]
{33, 0x02}, // D7 - D0: MSNA_P2 [7:0]
// PLL_B Setup (705MHz)
{34, 0x02},
{35, 0x71},
{36, 0x00},
{37, 0x0C},
{38, 0x1A},
{39, 0x00},
{40, 0x00},
{41, 0x86},
// Multisynth0 Setup (120MHz)
{42, 0x00},
{43, 0x01},
{44, 0x00},
{45, 0x01},
{46, 0x00},
{47, 0x00},
{48, 0x00},
{49, 0x00},
// Multisynth1 Setup (12MHz)
{50, 0x00},
{51, 0x01},
{52, 0x00},
{53, 0x1C},
{54, 0x00},
{55, 0x00},
{56, 0x00},
{57, 0x00},
// Multisynth2 Setup (13.56MHz)
{58, 0x00},
{59, 0x01},
{60, 0x00},
{61, 0x18},
{62, 0x00},
{63, 0x00},
{64, 0x00},
{65, 0x00}};
static const uint8_t m_si5351_regs_custom[51][2] = {
// Init
{3, 0xFF},
{16, 0x4F}, /* CLK0 Control: 8mA drive, Multisynth 0 as CLK0 source, Clock
not inverted, Source = PLLA, Multisynth 0 in integer mode,
clock powered up */
{17, 0x4F}, /* CLK1 Control: 8mA drive, Multisynth 1 as CLK1 source, Clock
not inverted, Source = PLLA, Multisynth 1 in integer mode,
clock powered up */
{18, 0x6F}, /* CLK2 Control: 8mA drive, Multisynth 2 as CLK2 source, Clock
not inverted, Source = PLLB, Multisynth 2 in integer mode,
clock powered up */
{19, 0x80}, /* CLK3 Control: Not used ... clock powered down */
{20, 0x80}, /* CLK4 Control: Not used ... clock powered down */
{21, 0x80}, /* CLK5 Control: Not used ... clock powered down */
{22, 0x80}, /* CLK6 Control: Not used ... clock powered down */
{23, 0x80}, /* CLK7 Control: Not used ... clock powered down */
{24, 0x00}, /* Clock disable state 0..3 (low when disabled) */
{25, 0x00}, /* Clock disable state 4..7 (low when disabled) */
// PLL_A Setup (900MHz) (36 + 0/1)
// P1: 4096 => 0x01000
// P2: 0 => 0x00000
// P3: 1 => 0x00001
{26, 0x00}, // D7 - D0: MSNA_P3 [15:8]
{27, 0x01}, // D7 - D0: MSNA_P3 [7:0]
{28, 0x00}, // D1 - D0: MSNA_P1 [17:16]
{29, 0x10}, // D7 - D0: MSNA_P1 [15:8]
{30, 0x00}, // D7 - D0: MSNA_P1 [7:0]
{31, 0x00}, // D7 - D4: MSNA_P3 [19:16], D3 - D0: MSNA_P2 [19:16]
{32, 0x00}, // D7 - D0: MSNA_P2 [15:8]
{33, 0x00}, // D7 - D0: MSNA_P2 [7:0]
// PLL_B Setup (375MHz) (15 + 0/1)
/* // P1: 1408 => 0x00580 */
/* // P2: 0 => 0x00000 */
/* // P3: 1 => 0x00001 */
{34, 0x00}, // D7 - D0: MSNB_P3 [15:8] */
{35, 0x01}, // D7 - D0: MSNB_P3 [7:0] */
{36, 0x00}, // D1 - D0: MSNB_P1 [17:16] */
{37, 0x05}, // D7 - D0: MSNB_P1 [15:8] */
{38, 0x80}, // D7 - D0: MSNB_P1 [7:0] */
{39, 0x00}, // D7 - D4: MSNB_P3 [19:16], D3 - D0: MSNA_P2 [19:16] */
{40, 0x00}, // D7 - D0: MSNB_P2 [15:8] */
{41, 0x00}, // D7 - D0: MSNB_P2 [7:0] */
// Multisynth0 Setup (120MHz)
// P1: 1
// P2: 0
// P3: 1
{42, 0x00}, // D7 - D0: MS0_P3 [15:8]
{43, 0x01}, // D7 - D0: MS0_P3 [7:0]
{44, 0x00}, // D6 - D4: R0_DIV [2:0], D3 - D2: MS0_DIVBY4 [1:0], D1 -D0: MS0_P1 [17:16]
{45, 0x01}, // D7 - D0: MS0_P1 [15:8]
{46, 0x01}, // D7 - D0: MS0_P1 [7:0]
{47, 0x00}, // D7 - D4: MS0_P3 [19:16], D3 - D0: MS0_P2 [19:16]
{48, 0x00}, // D7 - D0: MS0_P2 [15:8]
{49, 0x00}, // D7 - D0: MS0_P2 [7:0]
// Multisynth1 Setup (12MHz)
{50, 0x00},
{51, 0x01},
{52, 0x00},
{53, 0x1C},
{54, 0x00},
{55, 0x00},
{56, 0x00},
{57, 0x00},
// Multisynth2 Setup (13.56MHz)
{58, 0x00},
{59, 0x01},
{60, 0x00},
{61, 0x18},
{62, 0x00},
{63, 0x00},
{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);
SPCR = (1 << SPE) | (1 << MSTR);
SPSR = (1 << SPI2X);
}
static uint8_t spi_byte(const uint8_t data) {
SPDR = data;
while(!(SPSR & (1 << SPIF)));
return SPDR;
}
static void lcd_write(const uint8_t data) {
SPI_PORT &= ~(1 << SPI_SS);
spi_byte(data);
SPI_PORT |= (1 << SPI_SS);
}
static void lcd_init(void) {
// TODO: Optimize waiting times
SPI_DDR |= (1 << LCD_CD) | (1 << LCD_RST);
_delay_ms(1);
SPI_PORT |= (1 << LCD_RST);
_delay_ms(5);
lcd_write(0x40); // (6) Set Scroll Line: Display start line 0
lcd_write(0xA1); // (13) Set SEG direction: SEG reverse
lcd_write(0xC0); // (14) Set COM direction: Normal COM0 - COM63
lcd_write(0xA6); // (11) Set Inverse Display: Display inverse off
lcd_write(0xA2); // (17) Set LCD Bias Ratio: Set Bias 1/9 (Duty 1/65)
lcd_write(0x2F); // (5) Set Power Control: Booster, Regulator and Follower on
lcd_write(0x27); // (8) Set VLCD Resistor Ratio: Set Contrast
lcd_write(0x81); // (9) Set Electronic Volume: Set Contrast
lcd_write(value_contrast); // (9) Set Electronic Volume: Set Contrast
lcd_write(0xAF); // (12) Set Display Enable: Display on
}
static void lcd_update_contrast(void) {
SPI_PORT &= ~(1 << LCD_CD);
lcd_write(0x81); // (9) Set Electronic Volume: Set Contrast
lcd_write(value_contrast); // (9) Set Electronic Volume: Set Contrast
SPI_PORT |= (1 << LCD_CD);
}
static void lcd_update_backlight(void) {
switch (value_backlight) {
case 0:
DDRD &= (0 << PD5);
break;
case 1:
case 2:
case 3:
case 4:
case 5:
TCCR0B = 0x0C; // Prescaler = 256;
DDRD |= (1 << PD5);
OCR0B = value_backlight - 1;
break;
default:
TCCR0B = 0x0B; // Prescaler = 64;
DDRD |= (1 << PD5);
OCR0B = value_backlight - 4;
break;
}
}
static void lcd_move_cursor(const uint8_t column, const uint8_t row) {
assert(row < 16);
SPI_PORT &= ~(1 << LCD_CD);
lcd_write(0x00 + (0x0F & column));
lcd_write(0x10 + (column >> 4));
lcd_write(0xB0 + row);
SPI_PORT |= (1 << LCD_CD);
}
static void lcd_fill(const uint8_t data) {
for (uint8_t i = 0; i < 8; i++) {
lcd_move_cursor(0, i);
for (uint8_t j = 0; j < 102; j++)
lcd_write(data);
}
}
static void lcd_write_kerning(const uint8_t length,
const bool invert) {
for (uint8_t i = 0; i < length; i++)
if (invert)
lcd_write(0xFF);
else
lcd_write(0x00);
}
static void lcd_write_symbol_page(const struct symbol* symbol,
const uint8_t page,
const bool invert) {
for (uint8_t i = 0; i < symbol->length; i++)
if (invert)
lcd_write(~symbol->data[page * symbol->length + i]);
else
lcd_write(symbol->data[page * symbol->length + i]);
}
static void lcd_write_digit_page(const uint8_t digit,
const uint8_t page,
const bool invert) {
lcd_write_symbol_page(&symbol_table[digit], page, invert);
}
static void lcd_write_character_page(const char character,
const uint8_t page,
const bool invert) {
lcd_write_symbol_page(&symbol_table[character - 48], page, invert);
}
static void lcd_write_string_page(const char* string,
const uint8_t kerning,
const uint8_t page,
const bool invert) {
for (uint8_t i = 0; string[i] != 0; i++) {
lcd_write_kerning(kerning, invert);
lcd_write_character_page(string[i], page, invert);
}
}
static void lcd_write_integer_page(const uint32_t integer,
const uint8_t digits,
const uint8_t page,
const bool invert) {
if (digits != 0 || integer != 0) {
uint8_t input_digits = 0;
uint32_t comperator = 1;
// Get digits
for (; comperator <= integer; comperator *= 10, input_digits++);
// Print leading zeroes
for (int8_t i = digits - input_digits; i > 0; i--) {
lcd_write_kerning(2, invert);
lcd_write_digit_page(0, page, invert);
}
// Print number itself
for (; comperator >= 10; comperator /= 10) {
lcd_write_kerning(2, invert);
lcd_write_digit_page((integer % comperator) / (comperator / 10),
page, invert);
}
}
}
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);
else
lcd_fill(0x00);
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, i);
lcd_write_string_page("TW_STATUS:\0", 2, i, inverted);
lcd_write_kerning(2, inverted);
lcd_write_integer_page(TW_STATUS, 3, i, inverted);
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, i + 2);
lcd_write_string_page("TW_DATA:\0", 2, i, inverted);
lcd_write_kerning(16, inverted);
lcd_write_integer_page(TWDR, 3, i, inverted);
}
for (;;);
}
static void twi_start(void) {
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); // Start transmission
while (!(TWCR & (1 << TWINT))); // Wait until start is transmitted
if (TW_STATUS != TW_START) // Check status
twi_error(true);
}
static uint8_t twi_transmit(const uint8_t data) {
TWDR = data;
TWCR = (1 << TWINT) | (1 << TWEN); // Transmit data
while (!(TWCR & (1 << TWINT))); // Wait until data is transmitted
return TW_STATUS;
}
static uint8_t twi_receive(void) {
TWCR = (1 << TWINT) | (1 << TWEN); // Receive data
while (!(TWCR & (1 << TWINT))); // Wait until data is received
return TW_STATUS;
}
static void twi_stop(void) {
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
}
static uint8_t twi_read_register(const uint8_t address,
const uint8_t reg) {
twi_start();
if (twi_transmit(address << 1) != TW_MT_SLA_ACK)
twi_error(true);
if (twi_transmit(reg) != TW_MT_DATA_ACK)
twi_error(true);
twi_stop();
_delay_us(2);
twi_start();
if (twi_transmit((address << 1) + 1) != TW_MR_SLA_ACK)
twi_error(true);
if (twi_receive() != TW_MR_DATA_NACK)
twi_error(true);
twi_stop();
return TWDR;
}
static void twi_write_register(const uint8_t address,
const uint8_t reg,
const uint8_t data) {
twi_start();
if (twi_transmit(address << 1) != TW_MT_SLA_ACK)
twi_error(true);
if (twi_transmit(reg) != TW_MT_DATA_ACK)
twi_error(true);
if (twi_transmit(data) != TW_MT_DATA_ACK)
twi_error(true);
twi_stop();
}
static void draw_splash(void) {
lcd_fill(0x00);
for (uint8_t i = 0; i < 5; i++) {
lcd_move_cursor(31, 1 + i);
for (uint8_t j = 0; j < 40; j++)
lcd_write(sacred_chao[i * 40 + j]);
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(26, 6 + i);
for (uint8_t j = 0; j < 48; j++)
lcd_write(onders_org[i * 48 + j]);
}
}
static void draw_home(void) {
lcd_fill(0x00);
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) {
case ch1:
ch1_selected = true;
break;
case ch2:
ch2_selected = true;
break;
case ch3:
ch3_selected = true;
break;
case plla:
plla_selected = true;
break;
case pllb:
pllb_selected = true;
break;
default: // setup
setup_selected = true;
break;
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, i);
lcd_write_string_page("CH1\0", 2, i, ch1_selected);
lcd_write_kerning(2, ch1_selected);
lcd_move_cursor(0, 2 + i);
lcd_write_string_page("CH2\0", 2, i, ch2_selected);
lcd_write_kerning(2, ch2_selected);
lcd_move_cursor(0, 4 + i);
lcd_write_string_page("CH3\0", 2, i, ch3_selected);
lcd_write_kerning(2, ch3_selected);
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);
bool contrast_selected = false;
bool backlight_selected = false;
bool back_selected = false;
bool change_contrast_selected = false;
bool change_backlight_selected = false;
switch (setup_state) {
case contrast:
contrast_selected = true;
break;
case backlight:
backlight_selected = true;
break;
case back:
back_selected = true;
break;
case change_contrast:
change_contrast_selected = true;
break;
case change_backlight:
change_backlight_selected = true;
break;
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, i);
lcd_write_kerning(30, true);
lcd_write_string_page("SETUP\0", 2, i, true);
lcd_write_kerning(33, true);
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, 2 + i);
lcd_write_string_page("CONTRAST:\0", 2, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_kerning(16, false);
lcd_write_integer_page(value_contrast, 2, i,
change_contrast_selected);
lcd_write_kerning(2, change_contrast_selected);
}
for (uint8_t i = 0; i < 2; i++) {
lcd_move_cursor(0, 4 + i);
lcd_write_string_page("BACKLIGHT:\0", 2, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_kerning(5, false);
lcd_write_integer_page(value_backlight, 3, i,
change_backlight_selected);
lcd_write_kerning(2, change_backlight_selected);
}
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 change_state(const enum state new_state) {
switch (new_state) {
case home:
current_state = home;
draw_home();
break;
case ch1:
break;
case ch2:
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;
current_state = setup;
draw_setup();
break;
}
}
static void update_home(const enum input event) {
switch (event) {
case cw:
home_state++;
if (home_state > setup)
home_state = ch1;
draw_home();
break;
case ccw:
home_state--;
if (home_state > setup)
home_state = setup;
draw_home();
break;
case click:
change_state(home_state);
break;
case hold:
break;
}
}
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:
switch (setup_state) {
case contrast:
case backlight:
case back:
setup_state++;
if (setup_state > back)
setup_state = contrast;
break;
case change_contrast:
if (value_contrast < 63) {
value_contrast++;
lcd_update_contrast();
}
break;
case change_backlight:
if (value_backlight < 100) {
value_backlight++;
lcd_update_backlight();
}
break;
}
draw_setup();
break;
case ccw:
switch (setup_state) {
case contrast:
case backlight:
case back:
setup_state--;
if (setup_state > back)
setup_state = back;
break;
case change_contrast:
if (value_contrast > 0) {
value_contrast--;
lcd_update_contrast();
}
break;
case change_backlight:
if (value_backlight > 0) {
value_backlight--;
lcd_update_backlight();
}
break;
}
draw_setup();
break;
case click:
switch (setup_state) {
case contrast:
setup_state = change_contrast;
draw_setup();
break;
case backlight:
setup_state = change_backlight;
draw_setup();
break;
case back:
change_state(home);
break;
case change_contrast:
eeprom_update_byte(&eeprom_contrast, value_contrast);
setup_state = contrast;
draw_setup();
break;
case change_backlight:
eeprom_update_byte(&eeprom_backlight, value_backlight);
setup_state = backlight;
draw_setup();
break;
}
break;
case hold:
switch (setup_state) {
case contrast:
case backlight:
case back:
change_state(home);
break;
case change_contrast:
setup_state = contrast;
value_contrast = eeprom_read_byte(&eeprom_contrast);
lcd_update_contrast();
draw_setup();
break;
case change_backlight:
setup_state = backlight;
value_backlight = eeprom_read_byte(&eeprom_backlight);
lcd_update_backlight();
draw_setup();
break;
}
break;
}
}
static void update_state(const enum input event) {
switch (current_state) {
case home:
update_home(event);
break;
case ch1:
break;
case ch2:
break;
case ch3:
break;
case plla:
update_pll(event);
break;
case pllb:
update_pll(event);
break;
case setup:
update_setup(event);
break;
}
}
// Encoder rotation interrupt
ISR(PCINT0_vect) {
cli();
// Dechatter
_delay_ms(3);
switch (enc) {
case 0:
if (ENC_A && !ENC_B)
enc = 1;
else if (!ENC_A && ENC_B)
enc = 3;
break;
case 1:
if (ENC_A && ENC_B) {
enc = 2;
update_state(cw);
} else if (!ENC_A && !ENC_B) {
enc = 0;
update_state(ccw);
}
break;
case 2:
if (!ENC_A && ENC_B)
enc = 3;
else if (ENC_A && !ENC_B)
enc = 1;
break;
case 3:
if (!ENC_A && !ENC_B) {
enc = 0;
update_state(cw);
} else if (ENC_A && ENC_B) {
enc = 2;
update_state(ccw);
}
break;
}
sei();
}
// Encoder button interrupt
ISR(PCINT1_vect) {
cli();
// Debounce
_delay_ms(2);
if (PINC & (1 << PC0)) { // Release
if (TCCR1B & (1 << CS10)) // If release before hold
update_state(click); // Switch to selected state
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
} else { // Press
TCNT1 = 0;
if (!(TCCR1B & (1 << CS10)))
TCCR1B |= (1 << CS11) | (1 << CS10); // Enable Timer/Counter1
}
sei();
}
// Timer/Counter1 compare match A for back event
ISR(TIMER1_COMPA_vect) {
cli();
TCCR1B &= (0 << CS11) & (0 << CS10); // Disable Timer/Counter1
update_state(hold);
sei();
}
int main(void) {
// Load contrast and backlight values from EEPROM
value_contrast = eeprom_read_byte(&eeprom_contrast);
value_backlight = eeprom_read_byte(&eeprom_backlight);
// Init backlight
TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);
OCR0A = 255;
lcd_update_backlight();
// SPI and LCD init
spi_init();
lcd_init();
// Encoder setup
PORTB |= (1 << PB6) | (1 << PB7);
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT6) | (1 << PCINT7);
// Encoder switch setup
PORTC |= (1 << PC0);
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8);
// Timer/Counter1 setup to recognize held button
OCR1A = 65535;
TIMSK1 |= (1 << OCIE1A); // Enable match compare A
// Show splash screen
draw_splash();
_delay_ms(2000);
// Load the menu
change_state(current_state);
// Set TWI bit rate to 200kHz
TWBR = 12;
(void) &twi_read_register;
(void) &twi_write_register;
for (uint16_t i = 0; i < sizeof(m_si5351_regs_custom) / 2; i++)
twi_write_register(SI5351_ADDRESS, m_si5351_regs_custom[i][0],
m_si5351_regs_custom[i][1]);
twi_write_register(SI5351_ADDRESS, SI5351_REGISTER_177_PLL_RESET, 0xAC);
twi_write_register(SI5351_ADDRESS, SI5351_REGISTER_3_OUTPUT_ENABLE_CONTROL, 0x00);
// Enable interrupts
sei();
// Run...
for (;;);
}