From 12bb516cc01b4ac6bdd6a6f72444eac2e9f9c7e0 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 18 Mar 2019 15:09:11 -0700 Subject: [PATCH 1/7] PortaPack: Extract non-UI code into separate portapack.[ch] module. I'll be adding some non-UI API functions to the top-level PortaPack structure. --- firmware/common/hackrf-ui.c | 11 +-- firmware/common/portapack.c | 109 +++++++++++++++++++++++++++++ firmware/common/portapack.h | 33 +++++++++ firmware/common/ui_portapack.c | 78 +-------------------- firmware/common/ui_portapack.h | 2 - firmware/hackrf_usb/CMakeLists.txt | 1 + 6 files changed, 151 insertions(+), 83 deletions(-) create mode 100644 firmware/common/portapack.c create mode 100644 firmware/common/portapack.h diff --git a/firmware/common/hackrf-ui.c b/firmware/common/hackrf-ui.c index 8dd8715a..23974135 100644 --- a/firmware/common/hackrf-ui.c +++ b/firmware/common/hackrf-ui.c @@ -21,7 +21,7 @@ #include "hackrf-ui.h" -#include "ui_portapack.h" +#include "portapack.h" #include "ui_rad1o.h" #include @@ -60,7 +60,7 @@ static const hackrf_ui_t hackrf_ui_null = { &hackrf_ui_set_antenna_bias_null, }; -const hackrf_ui_t* portapack_detect(void) __attribute__((weak)); +const portapack_t* portapack_init(void) __attribute__((weak)); const hackrf_ui_t* rad1o_ui_setup(void) __attribute__((weak)); static const hackrf_ui_t* ui = NULL; @@ -69,8 +69,11 @@ const hackrf_ui_t* hackrf_ui(void) { /* Detect on first use. If no UI hardware is detected, use a stub function table. */ if( ui == NULL ) { #ifdef HACKRF_ONE - if( portapack_detect ) { - ui = portapack_detect(); + if( portapack_init ) { + const portapack_t* const portapack = portapack_init(); + if( portapack != NULL ) { + ui = portapack->hackrf_ui; + } } #endif #ifdef RAD1O diff --git a/firmware/common/portapack.c b/firmware/common/portapack.c new file mode 100644 index 00000000..4c4f3a9b --- /dev/null +++ b/firmware/common/portapack.c @@ -0,0 +1,109 @@ +/* + * Copyright 2018 Jared Boone + * + * This file is part of HackRF. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "portapack.h" +#include "hackrf_core.h" + +static bool jtag_pp_tck(const bool tms_value) { + gpio_write(jtag_cpld.gpio->gpio_pp_tms, tms_value); + + // 8 ns TMS/TDI to TCK setup + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + + gpio_set(jtag_cpld.gpio->gpio_tck); + + // 15 ns TCK to TMS/TDI hold time + // 20 ns TCK high time + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + + gpio_clear(jtag_cpld.gpio->gpio_tck); + + // 20 ns TCK low time + // 25 ns TCK falling edge to TDO valid + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + + return gpio_read(jtag_cpld.gpio->gpio_pp_tdo); +} + +static uint32_t jtag_pp_shift(const uint32_t tms_bits, const size_t count) { + uint32_t result = 0; + size_t bit_in_index = count - 1; + size_t bit_out_index = 0; + while(bit_out_index < count) { + const uint32_t tdo = jtag_pp_tck((tms_bits >> bit_in_index) & 1) & 1; + result |= (tdo << bit_out_index); + + bit_in_index--; + bit_out_index++; + } + + return result; +} + +static uint32_t jtag_pp_idcode(void) { + cpld_jtag_take(&jtag_cpld); + + /* TODO: Check if PortaPack TMS is floating or driven by an external device. */ + gpio_output(jtag_cpld.gpio->gpio_pp_tms); + + /* Test-Logic/Reset -> Run-Test/Idle -> Select-DR/Scan -> Capture-DR */ + jtag_pp_shift(0b11111010, 8); + + /* Shift-DR */ + const uint32_t idcode = jtag_pp_shift(0, 32); + + /* Exit1-DR -> Update-DR -> Run-Test/Idle -> ... -> Test-Logic/Reset */ + jtag_pp_shift(0b11011111, 8); + + cpld_jtag_release(&jtag_cpld); + + return idcode; +} + +static bool portapack_detect(void) { + return jtag_pp_idcode() == 0x020A50DD; +} + +extern const hackrf_ui_t portapack_hackrf_ui; + +const portapack_t portapack = { + &portapack_hackrf_ui, +}; + +const portapack_t* portapack_init(void) { + if( portapack_detect() ) { + return &portapack; + } else { + return NULL; + } +} \ No newline at end of file diff --git a/firmware/common/portapack.h b/firmware/common/portapack.h new file mode 100644 index 00000000..cd0a8f7a --- /dev/null +++ b/firmware/common/portapack.h @@ -0,0 +1,33 @@ +/* + * Copyright 2018 Jared Boone + * + * This file is part of HackRF. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __PORTAPACK_H__ +#define __PORTAPACK_H__ + +#include "hackrf-ui.h" + +typedef struct { + const hackrf_ui_t* const hackrf_ui; +} portapack_t; + +const portapack_t* portapack_init(void); + +#endif/*__PORTAPACK_H__*/ diff --git a/firmware/common/ui_portapack.c b/firmware/common/ui_portapack.c index e8430af1..018a8cdb 100644 --- a/firmware/common/ui_portapack.c +++ b/firmware/common/ui_portapack.c @@ -455,74 +455,6 @@ static void portapack_lcd_wake() { portapack_lcd_display_on(); } -static bool jtag_pp_tck(const bool tms_value) { - gpio_write(jtag_cpld.gpio->gpio_pp_tms, tms_value); - - // 8 ns TMS/TDI to TCK setup - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - - gpio_set(jtag_cpld.gpio->gpio_tck); - - // 15 ns TCK to TMS/TDI hold time - // 20 ns TCK high time - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - - gpio_clear(jtag_cpld.gpio->gpio_tck); - - // 20 ns TCK low time - // 25 ns TCK falling edge to TDO valid - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - - return gpio_read(jtag_cpld.gpio->gpio_pp_tdo); -} - -static uint32_t jtag_pp_shift(const uint32_t tms_bits, const size_t count) { - uint32_t result = 0; - size_t bit_in_index = count - 1; - size_t bit_out_index = 0; - while(bit_out_index < count) { - const uint32_t tdo = jtag_pp_tck((tms_bits >> bit_in_index) & 1) & 1; - result |= (tdo << bit_out_index); - - bit_in_index--; - bit_out_index++; - } - - return result; -} - -static uint32_t jtag_pp_idcode() { - cpld_jtag_take(&jtag_cpld); - - /* TODO: Check if PortaPack TMS is floating or driven by an external device. */ - gpio_output(jtag_cpld.gpio->gpio_pp_tms); - - /* Test-Logic/Reset -> Run-Test/Idle -> Select-DR/Scan -> Capture-DR */ - jtag_pp_shift(0b11111010, 8); - - /* Shift-DR */ - const uint32_t idcode = jtag_pp_shift(0, 32); - - /* Exit1-DR -> Update-DR -> Run-Test/Idle -> ... -> Test-Logic/Reset */ - jtag_pp_shift(0b11011111, 8); - - cpld_jtag_release(&jtag_cpld); - - return idcode; -} - static void portapack_if_init() { portapack_data_mask_set(); portapack_data_write_high(0); @@ -1019,7 +951,7 @@ static void portapack_ui_set_antenna_bias(bool antenna_bias) { (void)antenna_bias; } -static const hackrf_ui_t portapack_ui = { +const hackrf_ui_t portapack_hackrf_ui = { &portapack_ui_init, &portapack_ui_set_frequency, &portapack_ui_set_sample_rate, @@ -1033,11 +965,3 @@ static const hackrf_ui_t portapack_ui = { &portapack_ui_set_filter, &portapack_ui_set_antenna_bias, }; - -const hackrf_ui_t* portapack_detect(void) { - if( jtag_pp_idcode() == 0x020A50DD ) { - return &portapack_ui; - } else { - return NULL; - } -} diff --git a/firmware/common/ui_portapack.h b/firmware/common/ui_portapack.h index 0d2fc58e..d4ddda18 100644 --- a/firmware/common/ui_portapack.h +++ b/firmware/common/ui_portapack.h @@ -56,6 +56,4 @@ typedef struct ui_font_t { size_t data_stride; } ui_font_t; -const hackrf_ui_t* portapack_detect(void); - #endif/*__UI_PORTAPACK_H__*/ diff --git a/firmware/hackrf_usb/CMakeLists.txt b/firmware/hackrf_usb/CMakeLists.txt index 271bf434..7e124b1f 100644 --- a/firmware/hackrf_usb/CMakeLists.txt +++ b/firmware/hackrf_usb/CMakeLists.txt @@ -66,6 +66,7 @@ set(SRC_M4 if(BOARD STREQUAL "HACKRF_ONE") SET(SRC_M4 ${SRC_M4} + "${PATH_HACKRF_FIRMWARE_COMMON}/portapack.c" "${PATH_HACKRF_FIRMWARE_COMMON}/ui_portapack.c" ) endif() From 5df5ec109454c7ed6e34c6882f6aefb255b2991d Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 18 Mar 2019 15:11:34 -0700 Subject: [PATCH 2/7] PortaPack: Hide UI types that don't need to be exposed. --- firmware/common/ui_portapack.c | 32 ++++++++++++++++++++++++++++++++ firmware/common/ui_portapack.h | 34 ---------------------------------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/firmware/common/ui_portapack.c b/firmware/common/ui_portapack.c index 018a8cdb..aa29a088 100644 --- a/firmware/common/ui_portapack.c +++ b/firmware/common/ui_portapack.c @@ -30,6 +30,38 @@ #include +typedef struct ui_color_t { + uint16_t v; +} ui_color_t; + +typedef struct ui_point_t { + int16_t x; + int16_t y; +} ui_point_t; + +typedef struct ui_size_t { + int16_t width; + int16_t height; +} ui_size_t; + +typedef struct ui_rect_t { + ui_point_t point; + ui_size_t size; +} ui_rect_t; + +typedef struct ui_bitmap_t { + ui_size_t size; + const uint8_t* const data; +} ui_bitmap_t; + +typedef struct ui_font_t { + const ui_size_t glyph_size; + const uint8_t* const data; + char c_start; + size_t c_count; + size_t data_stride; +} ui_font_t; + /* Pixel data within a font or bitmap byte is "reversed": LSB is left-most pixel. */ static const uint8_t font_fixed_8x16_glyph_data[] = { diff --git a/firmware/common/ui_portapack.h b/firmware/common/ui_portapack.h index d4ddda18..69754c7e 100644 --- a/firmware/common/ui_portapack.h +++ b/firmware/common/ui_portapack.h @@ -22,38 +22,4 @@ #ifndef __UI_PORTAPACK_H__ #define __UI_PORTAPACK_H__ -#include - -typedef struct ui_color_t { - uint16_t v; -} ui_color_t; - -typedef struct ui_point_t { - int16_t x; - int16_t y; -} ui_point_t; - -typedef struct ui_size_t { - int16_t width; - int16_t height; -} ui_size_t; - -typedef struct ui_rect_t { - ui_point_t point; - ui_size_t size; -} ui_rect_t; - -typedef struct ui_bitmap_t { - ui_size_t size; - const uint8_t* const data; -} ui_bitmap_t; - -typedef struct ui_font_t { - const ui_size_t glyph_size; - const uint8_t* const data; - char c_start; - size_t c_count; - size_t data_stride; -} ui_font_t; - #endif/*__UI_PORTAPACK_H__*/ From c5e9b55b14d7214b8f0aedea6b1cc136283a766f Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 18 Mar 2019 16:22:14 -0700 Subject: [PATCH 3/7] PortaPack: Refactor general UI functions into separate PortaPack API. --- firmware/common/portapack.c | 459 +++++++++++++++++++++++++++++ firmware/common/portapack.h | 57 ++++ firmware/common/ui_portapack.c | 507 +-------------------------------- 3 files changed, 521 insertions(+), 502 deletions(-) diff --git a/firmware/common/portapack.c b/firmware/common/portapack.c index 4c4f3a9b..ff383fbb 100644 --- a/firmware/common/portapack.c +++ b/firmware/common/portapack.c @@ -20,7 +20,463 @@ */ #include "portapack.h" + #include "hackrf_core.h" +#include "gpio_lpc.h" + +#include + +static void portapack_sleep_milliseconds(const uint32_t milliseconds) { + /* NOTE: Naively assumes 204 MHz instruction cycle clock and five instructions per count */ + delay(milliseconds * 40800); +} + +static struct gpio_t gpio_io_stbx = GPIO(5, 0); /* P2_0 */ +static struct gpio_t gpio_addr = GPIO(5, 1); /* P2_1 */ +__attribute__((unused)) static struct gpio_t gpio_lcd_te = GPIO(5, 3); /* P2_3 */ +__attribute__((unused)) static struct gpio_t gpio_unused = GPIO(5, 7); /* P2_8 */ +static struct gpio_t gpio_lcd_rdx = GPIO(5, 4); /* P2_4 */ +static struct gpio_t gpio_lcd_wrx = GPIO(1, 10); /* P2_9 */ +static struct gpio_t gpio_dir = GPIO(1, 13); /* P2_13 */ + +typedef struct portapack_if_t { + gpio_t gpio_dir; + gpio_t gpio_lcd_rdx; + gpio_t gpio_lcd_wrx; + gpio_t gpio_io_stbx; + gpio_t gpio_addr; + gpio_port_t* const gpio_port_data; + uint8_t io_reg; +} portapack_if_t; + +static portapack_if_t portapack_if = { + .gpio_dir = &gpio_dir, + .gpio_lcd_rdx = &gpio_lcd_rdx, + .gpio_lcd_wrx = &gpio_lcd_wrx, + .gpio_io_stbx = &gpio_io_stbx, + .gpio_addr = &gpio_addr, + .gpio_port_data = GPIO_LPC_PORT(3), + .io_reg = 0x03, +}; + +/* NOTE: Code below assumes the shift value is "8". */ +#define GPIO_DATA_SHIFT (8) +static const uint32_t gpio_data_mask = 0xFFU << GPIO_DATA_SHIFT; + +static void portapack_data_mask_set() { + portapack_if.gpio_port_data->mask = ~gpio_data_mask; +} + +static void portapack_data_write_low(const uint32_t value) { + portapack_if.gpio_port_data->mpin = (value << GPIO_DATA_SHIFT); +} + +static void portapack_data_write_high(const uint32_t value) { + /* NOTE: Assumes no other bits in the port are masked. */ + /* NOTE: Assumes that bits 15 through 8 are masked. */ + portapack_if.gpio_port_data->mpin = value; +} + +static void portapack_dir_read() { + portapack_if.gpio_port_data->dir &= ~gpio_data_mask; + gpio_set(portapack_if.gpio_dir); +} + +static void portapack_dir_write() { + gpio_clear(portapack_if.gpio_dir); + portapack_if.gpio_port_data->dir |= gpio_data_mask; + /* TODO: Manipulating DIR[3] makes me queasy. The RFFC5072 DATA pin + * is also on port 3, and switches direction periodically... + * Time to resort to bit-banding to enforce atomicity? But then, how + * to change direction on eight bits efficiently? Or do I care, since + * the PortaPack data bus shouldn't change direction too frequently? + */ +} + +__attribute__((unused)) static void portapack_lcd_rd_assert() { + gpio_clear(portapack_if.gpio_lcd_rdx); +} + +static void portapack_lcd_rd_deassert() { + gpio_set(portapack_if.gpio_lcd_rdx); +} + +static void portapack_lcd_wr_assert() { + gpio_clear(portapack_if.gpio_lcd_wrx); +} + +static void portapack_lcd_wr_deassert() { + gpio_set(portapack_if.gpio_lcd_wrx); +} + +static void portapack_io_stb_assert() { + gpio_clear(portapack_if.gpio_io_stbx); +} + +static void portapack_io_stb_deassert() { + gpio_set(portapack_if.gpio_io_stbx); +} + +static void portapack_addr(const bool value) { + gpio_write(portapack_if.gpio_addr, value); +} + +static void portapack_lcd_command(const uint32_t value) { + portapack_data_write_high(0); /* Drive high byte (with zero -- don't care) */ + portapack_dir_write(); /* Turn around data bus, MCU->CPLD */ + portapack_addr(0); /* Indicate command */ + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + portapack_lcd_wr_assert(); /* Latch high byte */ + + portapack_data_write_low(value); /* Drive low byte (pass-through) */ + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + portapack_lcd_wr_deassert(); /* Complete write operation */ + + portapack_addr(1); /* Set up for data phase (most likely after a command) */ +} + +static void portapack_lcd_write_data(const uint32_t value) { + // NOTE: Assumes and DIR=0 and ADDR=1 from command phase. + portapack_data_write_high(value); /* Drive high byte */ + __asm__("nop"); + portapack_lcd_wr_assert(); /* Latch high byte */ + + portapack_data_write_low(value); /* Drive low byte (pass-through) */ + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + portapack_lcd_wr_deassert(); /* Complete write operation */ +} + +static void portapack_io_write(const bool address, const uint_fast16_t value) { + portapack_data_write_low(value); + portapack_dir_write(); + portapack_addr(address); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + portapack_io_stb_assert(); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + portapack_io_stb_deassert(); +} + +static void portapack_if_init() { + portapack_data_mask_set(); + portapack_data_write_high(0); + + portapack_dir_read(); + portapack_lcd_rd_deassert(); + portapack_lcd_wr_deassert(); + portapack_io_stb_deassert(); + portapack_addr(0); + + gpio_output(portapack_if.gpio_dir); + gpio_output(portapack_if.gpio_lcd_rdx); + gpio_output(portapack_if.gpio_lcd_wrx); + gpio_output(portapack_if.gpio_io_stbx); + gpio_output(portapack_if.gpio_addr); + /* gpio_input(portapack_if.gpio_rot_a); */ + /* gpio_input(portapack_if.gpio_rot_b); */ + + scu_pinmux(SCU_PINMUX_PP_D0, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D1, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D2, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D3, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D4, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D5, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D6, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + scu_pinmux(SCU_PINMUX_PP_D7, SCU_CONF_FUNCTION0 | SCU_GPIO_PDN); + + scu_pinmux(SCU_PINMUX_PP_DIR, SCU_CONF_FUNCTION0 | SCU_GPIO_NOPULL); + scu_pinmux(SCU_PINMUX_PP_LCD_RDX, SCU_CONF_FUNCTION4 | SCU_GPIO_NOPULL); + scu_pinmux(SCU_PINMUX_PP_LCD_WRX, SCU_CONF_FUNCTION0 | SCU_GPIO_NOPULL); + scu_pinmux(SCU_PINMUX_PP_IO_STBX, SCU_CONF_FUNCTION4 | SCU_GPIO_NOPULL); + scu_pinmux(SCU_PINMUX_PP_ADDR, SCU_CONF_FUNCTION4 | SCU_GPIO_NOPULL); + /* scu_pinmux(SCU_PINMUX_PP_LCD_TE, SCU_CONF_FUNCTION4 | SCU_GPIO_NOPULL); */ + /* scu_pinmux(SCU_PINMUX_PP_UNUSED, SCU_CONF_FUNCTION4 | SCU_GPIO_NOPULL); */ +} + +static void portapack_lcd_reset_state(const bool active) { + portapack_if.io_reg = (portapack_if.io_reg & 0xfe) | (active ? (1 << 0) : 0); + portapack_io_write(1, portapack_if.io_reg); +} + +static void portapack_lcd_data_write_command_and_data( + const uint_fast8_t command, + const uint8_t* data, + const size_t data_count +) { + portapack_lcd_command(command); + for(size_t i=0; i> 8), (start & 0xff), + (end >> 8), (end & 0xff) + }; + portapack_lcd_data_write_command_and_data(command, data, ARRAY_SIZEOF(data)); +} + +static void portapack_lcd_caset(const uint_fast16_t start_column, const uint_fast16_t end_column) { + portapack_lcd_set(0x2a, start_column, end_column); +} + +static void portapack_lcd_paset(const uint_fast16_t start_page, const uint_fast16_t end_page) { + portapack_lcd_set(0x2b, start_page, end_page); +} + +static void portapack_lcd_start_ram_write( + const ui_rect_t rect +) { + portapack_lcd_caset(rect.point.x, rect.point.x + rect.size.width - 1); + portapack_lcd_paset(rect.point.y, rect.point.y + rect.size.height - 1); + portapack_lcd_ramwr_start(); +} + +static void portapack_lcd_write_pixel(const ui_color_t pixel) { + portapack_lcd_write_data(pixel.v); +} + +static void portapack_lcd_write_pixels_color(const ui_color_t c, size_t n) { + while(n--) { + portapack_lcd_write_data(c.v); + } +} + +static void portapack_lcd_wake() { + portapack_lcd_sleep_out(); + portapack_lcd_display_on(); +} + +static void portapack_lcd_reset() { + portapack_lcd_reset_state(false); + portapack_sleep_milliseconds(1); + portapack_lcd_reset_state(true); + portapack_sleep_milliseconds(10); + portapack_lcd_reset_state(false); + portapack_sleep_milliseconds(120); +} + +static void portapack_lcd_init() { + // LCDs are configured for IM[2:0] = 001 + // 8080-I system, 16-bit parallel bus + + // + // 0x3a: DBI[2:0] = 101 + // MDT[1:0] = XX (if not in 18-bit mode, right?) + + // Power control B + // 0 + // PCEQ=1, DRV_ena=0, Power control=3 + const uint8_t cmd_cf[] = { 0x00, 0xD9, 0x30 }; + portapack_lcd_data_write_command_and_data(0xCF, cmd_cf, ARRAY_SIZEOF(cmd_cf)); + + // Power on sequence control + const uint8_t cmd_ed[] = { 0x64, 0x03, 0x12, 0x81 }; + portapack_lcd_data_write_command_and_data(0xED, cmd_ed, ARRAY_SIZEOF(cmd_ed)); + + // Driver timing control A + const uint8_t cmd_e8[] = { 0x85, 0x10, 0x78 }; + portapack_lcd_data_write_command_and_data(0xE8, cmd_e8, ARRAY_SIZEOF(cmd_e8)); + + // Power control A + const uint8_t cmd_cb[] = { 0x39, 0x2C, 0x00, 0x34, 0x02 }; + portapack_lcd_data_write_command_and_data(0xCB, cmd_cb, ARRAY_SIZEOF(cmd_cb)); + + // Pump ratio control + const uint8_t cmd_f7[] = { 0x20 }; + portapack_lcd_data_write_command_and_data(0xF7, cmd_f7, ARRAY_SIZEOF(cmd_f7)); + + // Driver timing control B + const uint8_t cmd_ea[] = { 0x00, 0x00 }; + portapack_lcd_data_write_command_and_data(0xEA, cmd_ea, ARRAY_SIZEOF(cmd_ea)); + + const uint8_t cmd_b1[] = { 0x00, 0x1B }; + portapack_lcd_data_write_command_and_data(0xB1, cmd_b1, ARRAY_SIZEOF(cmd_b1)); + + // Blanking Porch Control + // VFP = 0b0000010 = 2 (number of HSYNC of vertical front porch) + // VBP = 0b0000010 = 2 (number of HSYNC of vertical back porch) + // HFP = 0b0001010 = 10 (number of DOTCLOCK of horizontal front porch) + // HBP = 0b0010100 = 20 (number of DOTCLOCK of horizontal back porch) + const uint8_t cmd_b5[] = { 0x02, 0x02, 0x0a, 0x14 }; + portapack_lcd_data_write_command_and_data(0xB5, cmd_b5, ARRAY_SIZEOF(cmd_b5)); + + // Display Function Control + // PT[1:0] = 0b10 + // PTG[1:0] = 0b10 + // ISC[3:0] = 0b0010 (scan cycle interval of gate driver: 5 frames) + // SM = 0 (gate driver pin arrangement in combination with GS) + // SS = 1 (source output scan direction S720 -> S1) + // GS = 0 (gate output scan direction G1 -> G320) + // REV = 1 (normally white) + // NL = 0b100111 (default) + // PCDIV = 0b000000 (default?) + const uint8_t cmd_b6[] = { 0x0A, 0xA2, 0x27, 0x00 }; + portapack_lcd_data_write_command_and_data(0xB6, cmd_b6, ARRAY_SIZEOF(cmd_b6)); + + // Power Control 1 + //VRH[5:0] + const uint8_t cmd_c0[] = { 0x1B }; + portapack_lcd_data_write_command_and_data(0xC0, cmd_c0, ARRAY_SIZEOF(cmd_c0)); + + // Power Control 2 + //SAP[2:0];BT[3:0] + const uint8_t cmd_c1[] = { 0x12 }; + portapack_lcd_data_write_command_and_data(0xC1, cmd_c1, ARRAY_SIZEOF(cmd_c1)); + + // VCOM Control 1 + const uint8_t cmd_c5[] = { 0x32, 0x3C }; + portapack_lcd_data_write_command_and_data(0xC5, cmd_c5, ARRAY_SIZEOF(cmd_c5)); + + // VCOM Control 2 + const uint8_t cmd_c7[] = { 0x9B }; + portapack_lcd_data_write_command_and_data(0xC7, cmd_c7, ARRAY_SIZEOF(cmd_c7)); + + // Memory Access Control + // Invert X and Y memory access order, so upper-left of + // screen is (0,0) when writing to display. + const uint8_t cmd_36[] = { + (1 << 7) | // MY=1 + (1 << 6) | // MX=1 + (0 << 5) | // MV=0 + (1 << 4) | // ML=1: reverse vertical refresh to simplify scrolling logic + (1 << 3) // BGR=1: For Kingtech LCD, BGR filter. + }; + portapack_lcd_data_write_command_and_data(0x36, cmd_36, ARRAY_SIZEOF(cmd_36)); + + // COLMOD: Pixel Format Set + // DPI=101 (16 bits/pixel), DBI=101 (16 bits/pixel) + const uint8_t cmd_3a[] = { 0x55 }; + portapack_lcd_data_write_command_and_data(0x3A, cmd_3a, ARRAY_SIZEOF(cmd_3a)); + + //portapack_lcd_data_write_command_and_data(0xF6, { 0x01, 0x30 }); + // WEMODE=1 (reset column and page number on overflow) + // MDT[1:0] + // EPF[1:0]=00 (use channel MSB for LSB) + // RIM=0 (If COLMOD[6:4]=101 (65k color), 16-bit RGB interface (1 transfer/pixel)) + // RM=0 (system interface/VSYNC interface) + // DM[1:0]=00 (internal clock operation) + // ENDIAN=0 (doesn't matter with 16-bit interface) + const uint8_t cmd_f6[] = { 0x01, 0x30, 0x00 }; + portapack_lcd_data_write_command_and_data(0xF6, cmd_f6, ARRAY_SIZEOF(cmd_f6)); + + // 3Gamma Function Disable + const uint8_t cmd_f2[] = { 0x00 }; + portapack_lcd_data_write_command_and_data(0xF2, cmd_f2, ARRAY_SIZEOF(cmd_f2)); + + // Gamma curve selected + const uint8_t cmd_26[] = { 0x01 }; + portapack_lcd_data_write_command_and_data(0x26, cmd_26, ARRAY_SIZEOF(cmd_26)); + + // Set Gamma + const uint8_t cmd_e0[] = { + 0x0F, 0x1D, 0x19, 0x0E, 0x10, 0x07, 0x4C, 0x63, + 0x3F, 0x03, 0x0D, 0x00, 0x26, 0x24, 0x04 + }; + portapack_lcd_data_write_command_and_data(0xE0, cmd_e0, ARRAY_SIZEOF(cmd_e0)); + + // Set Gamma + const uint8_t cmd_e1[] = { + 0x00, 0x1C, 0x1F, 0x02, 0x0F, 0x03, 0x35, 0x25, + 0x47, 0x04, 0x0C, 0x0B, 0x29, 0x2F, 0x05 + }; + portapack_lcd_data_write_command_and_data(0xE1, cmd_e1, ARRAY_SIZEOF(cmd_e1)); + + portapack_lcd_wake(); + + // Turn on Tearing Effect Line (TE) output signal. + const uint8_t cmd_35[] = { 0b00000000 }; + portapack_lcd_data_write_command_and_data(0x35, cmd_35, ARRAY_SIZEOF(cmd_35)); +} + +void portapack_backlight(const bool on) { + portapack_if.io_reg = (portapack_if.io_reg & 0x7f) | (on ? (1 << 7) : 0); + portapack_io_write(1, portapack_if.io_reg); +} + +void portapack_fill_rectangle( + const ui_rect_t rect, + const ui_color_t color +) { + portapack_lcd_start_ram_write(rect); + portapack_lcd_write_pixels_color(color, rect.size.width * rect.size.height); +} + +void portapack_clear_display(const ui_color_t color) { + const ui_rect_t rect_screen = { { 0, 0 }, { 240, 320 } }; + portapack_fill_rectangle(rect_screen, color); +} + +void portapack_draw_bitmap( + const ui_point_t point, + const ui_bitmap_t bitmap, + const ui_color_t foreground, + const ui_color_t background +) { + const ui_rect_t rect = { + .point = point, + .size = bitmap.size + }; + + portapack_lcd_start_ram_write(rect); + + const size_t count = bitmap.size.width * bitmap.size.height; + for(size_t i=0; i> 3] & (1U << (i & 0x7)); + portapack_lcd_write_pixel(pixel ? foreground : background); + } +} + +ui_bitmap_t portapack_font_glyph( + const ui_font_t* const font, + const char c +) { + if( c >= font->c_start ) { + const uint_fast8_t index = c - font->c_start; + if( index < font->c_count ) { + const ui_bitmap_t bitmap = { + .size = font->glyph_size, + .data = &font->data[index * font->data_stride] + }; + return bitmap; + } + } + + const ui_bitmap_t bitmap = { + .size = font->glyph_size, + .data = font->data, + }; + return bitmap; +} static bool jtag_pp_tck(const bool tms_value) { gpio_write(jtag_cpld.gpio->gpio_pp_tms, tms_value); @@ -102,6 +558,9 @@ const portapack_t portapack = { const portapack_t* portapack_init(void) { if( portapack_detect() ) { + portapack_if_init(); + portapack_lcd_reset(); + portapack_lcd_init(); return &portapack; } else { return NULL; diff --git a/firmware/common/portapack.h b/firmware/common/portapack.h index cd0a8f7a..0157b39c 100644 --- a/firmware/common/portapack.h +++ b/firmware/common/portapack.h @@ -22,12 +22,69 @@ #ifndef __PORTAPACK_H__ #define __PORTAPACK_H__ +#include + #include "hackrf-ui.h" +#define ARRAY_SIZEOF(x) (sizeof(x) / sizeof(x[0])) + +typedef struct ui_color_t { + uint16_t v; +} ui_color_t; + +typedef struct ui_point_t { + int16_t x; + int16_t y; +} ui_point_t; + +typedef struct ui_size_t { + int16_t width; + int16_t height; +} ui_size_t; + +typedef struct ui_rect_t { + ui_point_t point; + ui_size_t size; +} ui_rect_t; + +typedef struct ui_bitmap_t { + ui_size_t size; + const uint8_t* const data; +} ui_bitmap_t; + +typedef struct ui_font_t { + const ui_size_t glyph_size; + const uint8_t* const data; + char c_start; + size_t c_count; + size_t data_stride; +} ui_font_t; + typedef struct { const hackrf_ui_t* const hackrf_ui; } portapack_t; const portapack_t* portapack_init(void); +void portapack_backlight(const bool on); + +void portapack_fill_rectangle( + const ui_rect_t rect, + const ui_color_t color +); + +void portapack_clear_display(const ui_color_t color); + +void portapack_draw_bitmap( + const ui_point_t point, + const ui_bitmap_t bitmap, + const ui_color_t foreground, + const ui_color_t background +); + +ui_bitmap_t portapack_font_glyph( + const ui_font_t* const font, + const char c +); + #endif/*__PORTAPACK_H__*/ diff --git a/firmware/common/ui_portapack.c b/firmware/common/ui_portapack.c index aa29a088..f80c1c6c 100644 --- a/firmware/common/ui_portapack.c +++ b/firmware/common/ui_portapack.c @@ -19,48 +19,9 @@ * Boston, MA 02110-1301, USA. */ -#include "hackrf-ui.h" - #include "ui_portapack.h" -#include "hackrf_core.h" -#include "gpio_lpc.h" - -#include - -#include - -typedef struct ui_color_t { - uint16_t v; -} ui_color_t; - -typedef struct ui_point_t { - int16_t x; - int16_t y; -} ui_point_t; - -typedef struct ui_size_t { - int16_t width; - int16_t height; -} ui_size_t; - -typedef struct ui_rect_t { - ui_point_t point; - ui_size_t size; -} ui_rect_t; - -typedef struct ui_bitmap_t { - ui_size_t size; - const uint8_t* const data; -} ui_bitmap_t; - -typedef struct ui_font_t { - const ui_size_t glyph_size; - const uint8_t* const data; - char c_start; - size_t c_count; - size_t data_stride; -} ui_font_t; +#include "portapack.h" /* Pixel data within a font or bitmap byte is "reversed": LSB is left-most pixel. */ @@ -295,456 +256,6 @@ __attribute__((unused)) static ui_color_t portapack_color_rgb( static const ui_color_t color_background = { 0x001f }; static const ui_color_t color_foreground = { 0xffff }; -#define ARRAY_SIZEOF(x) (sizeof(x) / sizeof(x[0])) - -static void portapack_sleep_milliseconds(const uint32_t milliseconds) { - /* NOTE: Naively assumes 204 MHz instruction cycle clock and five instructions per count */ - delay(milliseconds * 40800); -} - -static struct gpio_t gpio_io_stbx = GPIO(5, 0); /* P2_0 */ -static struct gpio_t gpio_addr = GPIO(5, 1); /* P2_1 */ -__attribute__((unused)) static struct gpio_t gpio_lcd_te = GPIO(5, 3); /* P2_3 */ -__attribute__((unused)) static struct gpio_t gpio_unused = GPIO(5, 7); /* P2_8 */ -static struct gpio_t gpio_lcd_rdx = GPIO(5, 4); /* P2_4 */ -static struct gpio_t gpio_lcd_wrx = GPIO(1, 10); /* P2_9 */ -static struct gpio_t gpio_dir = GPIO(1, 13); /* P2_13 */ - -typedef struct portapack_t { - gpio_t gpio_dir; - gpio_t gpio_lcd_rdx; - gpio_t gpio_lcd_wrx; - gpio_t gpio_io_stbx; - gpio_t gpio_addr; - gpio_port_t* const gpio_port_data; - uint8_t io_reg; -} portapack_t; - -static portapack_t portapack = { - .gpio_dir = &gpio_dir, - .gpio_lcd_rdx = &gpio_lcd_rdx, - .gpio_lcd_wrx = &gpio_lcd_wrx, - .gpio_io_stbx = &gpio_io_stbx, - .gpio_addr = &gpio_addr, - .gpio_port_data = GPIO_LPC_PORT(3), - .io_reg = 0x03, -}; - -/* NOTE: Code below assumes the shift value is "8". */ -#define GPIO_DATA_SHIFT (8) -static const uint32_t gpio_data_mask = 0xFFU << GPIO_DATA_SHIFT; - -static void portapack_data_mask_set() { - portapack.gpio_port_data->mask = ~gpio_data_mask; -} - -static void portapack_data_write_low(const uint32_t value) { - portapack.gpio_port_data->mpin = (value << GPIO_DATA_SHIFT); -} - -static void portapack_data_write_high(const uint32_t value) { - /* NOTE: Assumes no other bits in the port are masked. */ - /* NOTE: Assumes that bits 15 through 8 are masked. */ - portapack.gpio_port_data->mpin = value; -} - -static void portapack_dir_read() { - portapack.gpio_port_data->dir &= ~gpio_data_mask; - gpio_set(portapack.gpio_dir); -} - -static void portapack_dir_write() { - gpio_clear(portapack.gpio_dir); - portapack.gpio_port_data->dir |= gpio_data_mask; - /* TODO: Manipulating DIR[3] makes me queasy. The RFFC5072 DATA pin - * is also on port 3, and switches direction periodically... - * Time to resort to bit-banding to enforce atomicity? But then, how - * to change direction on eight bits efficiently? Or do I care, since - * the PortaPack data bus shouldn't change direction too frequently? - */ -} - -__attribute__((unused)) static void portapack_lcd_rd_assert() { - gpio_clear(portapack.gpio_lcd_rdx); -} - -static void portapack_lcd_rd_deassert() { - gpio_set(portapack.gpio_lcd_rdx); -} - -static void portapack_lcd_wr_assert() { - gpio_clear(portapack.gpio_lcd_wrx); -} - -static void portapack_lcd_wr_deassert() { - gpio_set(portapack.gpio_lcd_wrx); -} - -static void portapack_io_stb_assert() { - gpio_clear(portapack.gpio_io_stbx); -} - -static void portapack_io_stb_deassert() { - gpio_set(portapack.gpio_io_stbx); -} - -static void portapack_addr(const bool value) { - gpio_write(portapack.gpio_addr, value); -} - -static void portapack_lcd_command(const uint32_t value) { - portapack_data_write_high(0); /* Drive high byte (with zero -- don't care) */ - portapack_dir_write(); /* Turn around data bus, MCU->CPLD */ - portapack_addr(0); /* Indicate command */ - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - portapack_lcd_wr_assert(); /* Latch high byte */ - - portapack_data_write_low(value); /* Drive low byte (pass-through) */ - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - portapack_lcd_wr_deassert(); /* Complete write operation */ - - portapack_addr(1); /* Set up for data phase (most likely after a command) */ -} - -static void portapack_lcd_write_data(const uint32_t value) { - // NOTE: Assumes and DIR=0 and ADDR=1 from command phase. - portapack_data_write_high(value); /* Drive high byte */ - __asm__("nop"); - portapack_lcd_wr_assert(); /* Latch high byte */ - - portapack_data_write_low(value); /* Drive low byte (pass-through) */ - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - portapack_lcd_wr_deassert(); /* Complete write operation */ -} - -static void portapack_io_write(const bool address, const uint_fast16_t value) { - portapack_data_write_low(value); - portapack_dir_write(); - portapack_addr(address); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - portapack_io_stb_assert(); - __asm__("nop"); - __asm__("nop"); - __asm__("nop"); - portapack_io_stb_deassert(); -} - -static void portapack_lcd_data_write_command_and_data( - const uint_fast8_t command, - const uint8_t* data, - const size_t data_count -) { - portapack_lcd_command(command); - for(size_t i=0; i S1) - // GS = 0 (gate output scan direction G1 -> G320) - // REV = 1 (normally white) - // NL = 0b100111 (default) - // PCDIV = 0b000000 (default?) - const uint8_t cmd_b6[] = { 0x0A, 0xA2, 0x27, 0x00 }; - portapack_lcd_data_write_command_and_data(0xB6, cmd_b6, ARRAY_SIZEOF(cmd_b6)); - - // Power Control 1 - //VRH[5:0] - const uint8_t cmd_c0[] = { 0x1B }; - portapack_lcd_data_write_command_and_data(0xC0, cmd_c0, ARRAY_SIZEOF(cmd_c0)); - - // Power Control 2 - //SAP[2:0];BT[3:0] - const uint8_t cmd_c1[] = { 0x12 }; - portapack_lcd_data_write_command_and_data(0xC1, cmd_c1, ARRAY_SIZEOF(cmd_c1)); - - // VCOM Control 1 - const uint8_t cmd_c5[] = { 0x32, 0x3C }; - portapack_lcd_data_write_command_and_data(0xC5, cmd_c5, ARRAY_SIZEOF(cmd_c5)); - - // VCOM Control 2 - const uint8_t cmd_c7[] = { 0x9B }; - portapack_lcd_data_write_command_and_data(0xC7, cmd_c7, ARRAY_SIZEOF(cmd_c7)); - - // Memory Access Control - // Invert X and Y memory access order, so upper-left of - // screen is (0,0) when writing to display. - const uint8_t cmd_36[] = { - (1 << 7) | // MY=1 - (1 << 6) | // MX=1 - (0 << 5) | // MV=0 - (1 << 4) | // ML=1: reverse vertical refresh to simplify scrolling logic - (1 << 3) // BGR=1: For Kingtech LCD, BGR filter. - }; - portapack_lcd_data_write_command_and_data(0x36, cmd_36, ARRAY_SIZEOF(cmd_36)); - - // COLMOD: Pixel Format Set - // DPI=101 (16 bits/pixel), DBI=101 (16 bits/pixel) - const uint8_t cmd_3a[] = { 0x55 }; - portapack_lcd_data_write_command_and_data(0x3A, cmd_3a, ARRAY_SIZEOF(cmd_3a)); - - //portapack_lcd_data_write_command_and_data(0xF6, { 0x01, 0x30 }); - // WEMODE=1 (reset column and page number on overflow) - // MDT[1:0] - // EPF[1:0]=00 (use channel MSB for LSB) - // RIM=0 (If COLMOD[6:4]=101 (65k color), 16-bit RGB interface (1 transfer/pixel)) - // RM=0 (system interface/VSYNC interface) - // DM[1:0]=00 (internal clock operation) - // ENDIAN=0 (doesn't matter with 16-bit interface) - const uint8_t cmd_f6[] = { 0x01, 0x30, 0x00 }; - portapack_lcd_data_write_command_and_data(0xF6, cmd_f6, ARRAY_SIZEOF(cmd_f6)); - - // 3Gamma Function Disable - const uint8_t cmd_f2[] = { 0x00 }; - portapack_lcd_data_write_command_and_data(0xF2, cmd_f2, ARRAY_SIZEOF(cmd_f2)); - - // Gamma curve selected - const uint8_t cmd_26[] = { 0x01 }; - portapack_lcd_data_write_command_and_data(0x26, cmd_26, ARRAY_SIZEOF(cmd_26)); - - // Set Gamma - const uint8_t cmd_e0[] = { - 0x0F, 0x1D, 0x19, 0x0E, 0x10, 0x07, 0x4C, 0x63, - 0x3F, 0x03, 0x0D, 0x00, 0x26, 0x24, 0x04 - }; - portapack_lcd_data_write_command_and_data(0xE0, cmd_e0, ARRAY_SIZEOF(cmd_e0)); - - // Set Gamma - const uint8_t cmd_e1[] = { - 0x00, 0x1C, 0x1F, 0x02, 0x0F, 0x03, 0x35, 0x25, - 0x47, 0x04, 0x0C, 0x0B, 0x29, 0x2F, 0x05 - }; - portapack_lcd_data_write_command_and_data(0xE1, cmd_e1, ARRAY_SIZEOF(cmd_e1)); - - portapack_lcd_wake(); - - // Turn on Tearing Effect Line (TE) output signal. - const uint8_t cmd_35[] = { 0b00000000 }; - portapack_lcd_data_write_command_and_data(0x35, cmd_35, ARRAY_SIZEOF(cmd_35)); -} - -static void portapack_lcd_ramwr_start() { - const uint8_t cmd_2c[] = {}; - portapack_lcd_data_write_command_and_data(0x2c, cmd_2c, ARRAY_SIZEOF(cmd_2c)); -} - -static void portapack_lcd_set(const uint_fast8_t command, const uint_fast16_t start, const uint_fast16_t end) { - const uint8_t data[] = { - (start >> 8), (start & 0xff), - (end >> 8), (end & 0xff) - }; - portapack_lcd_data_write_command_and_data(command, data, ARRAY_SIZEOF(data)); -} - -static void portapack_lcd_caset(const uint_fast16_t start_column, uint_fast16_t end_column) { - portapack_lcd_set(0x2a, start_column, end_column); -} - -static void portapack_lcd_paset(const uint_fast16_t start_page, const uint_fast16_t end_page) { - portapack_lcd_set(0x2b, start_page, end_page); -} - - -static void portapack_lcd_start_ram_write( - ui_rect_t rect -) { - portapack_lcd_caset(rect.point.x, rect.point.x + rect.size.width - 1); - portapack_lcd_paset(rect.point.y, rect.point.y + rect.size.height - 1); - portapack_lcd_ramwr_start(); -} - -static void portapack_lcd_fill_rectangle( - ui_rect_t rect, - ui_color_t color -) { - portapack_lcd_start_ram_write(rect); - portapack_lcd_write_pixels_color(color, rect.size.width * rect.size.height); -} - -static void portapack_draw_bitmap( - const ui_point_t point, - const ui_bitmap_t bitmap, - const ui_color_t foreground, - const ui_color_t background -) { - const ui_rect_t rect = { - .point = point, - .size = bitmap.size - }; - - portapack_lcd_start_ram_write(rect); - - const size_t count = bitmap.size.width * bitmap.size.height; - for(size_t i=0; i> 3] & (1U << (i & 0x7)); - portapack_lcd_write_pixel(pixel ? foreground : background); - } -} - -static ui_bitmap_t portapack_font_glyph( - const ui_font_t* const font, - const char c -) { - if( c >= font->c_start ) { - const uint_fast8_t index = c - font->c_start; - if( index < font->c_count ) { - const ui_bitmap_t bitmap = { - .size = font->glyph_size, - .data = &font->data[index * font->data_stride] - }; - return bitmap; - } - } - - const ui_bitmap_t bitmap = { - .size = font->glyph_size, - .data = font->data, - }; - return bitmap; -} - static ui_point_t portapack_lcd_draw_int(const ui_point_t point, uint64_t value, size_t field_width) { const ui_point_t point_done = { .x = point.x + font_fixed_8x16.glyph_size.width * field_width, @@ -775,11 +286,6 @@ static ui_point_t portapack_lcd_draw_string(ui_point_t point, const char* s) { return point; } -static void portapack_lcd_clear() { - const ui_rect_t rect_screen = { { 0, 0 }, { 240, 320 } }; - portapack_lcd_fill_rectangle(rect_screen, color_background); -} - typedef struct draw_list_t { const ui_bitmap_t* bitmap; const ui_point_t point; @@ -852,12 +358,9 @@ static void portapack_radio_path_redraw() { portapack_draw_radio_path(radio_draw_list, ARRAY_SIZEOF(radio_draw_list)); } -static void portapack_ui_init() { - portapack_if_init(); - portapack_lcd_reset(); - portapack_lcd_init(); - portapack_lcd_clear(); - portapack_backlight(1); +static void portapack_ui_init(void) { + portapack_clear_display(color_background); + portapack_backlight(true); } static void portapack_ui_set_frequency(uint64_t frequency) { @@ -878,7 +381,7 @@ static void portapack_ui_set_frequency(uint64_t frequency) { if( (i>=6) && (value == 0) ) { /* Blank out leading zeros. */ const ui_rect_t rect = { point, glyph.size }; - portapack_lcd_fill_rectangle(rect, color_background); + portapack_fill_rectangle(rect, color_background); } else { portapack_draw_bitmap(point, glyph, color_foreground, color_background); } From af66a7c0762f0809303227cd165bee4459ec2794 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 19 Mar 2019 10:04:08 -0700 Subject: [PATCH 4/7] PortaPack: Add reference oscillator control function. --- firmware/common/portapack.c | 6 ++++++ firmware/common/portapack.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/firmware/common/portapack.c b/firmware/common/portapack.c index ff383fbb..4bbb2312 100644 --- a/firmware/common/portapack.c +++ b/firmware/common/portapack.c @@ -423,6 +423,12 @@ void portapack_backlight(const bool on) { portapack_io_write(1, portapack_if.io_reg); } +void portapack_reference_oscillator(const bool on) { + const uint8_t mask = 1 << 6; + portapack_if.io_reg = (portapack_if.io_reg & ~mask) | (on ? mask : 0); + portapack_io_write(1, portapack_if.io_reg); +} + void portapack_fill_rectangle( const ui_rect_t rect, const ui_color_t color diff --git a/firmware/common/portapack.h b/firmware/common/portapack.h index 0157b39c..9febdbbd 100644 --- a/firmware/common/portapack.h +++ b/firmware/common/portapack.h @@ -68,6 +68,8 @@ const portapack_t* portapack_init(void); void portapack_backlight(const bool on); +void portapack_reference_oscillator(const bool on); + void portapack_fill_rectangle( const ui_rect_t rect, const ui_color_t color From b4d8ee678e64a75d9386a1ee08ae0d58aabe89d6 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 20 Mar 2019 10:44:56 -0700 Subject: [PATCH 5/7] PortaPack: Lots of clean-up Perform PortaPack initialization separately from and earlier than UI initialization. Track if PortaPack was detected, provide (mostly useless) pointer if so. Put "weak" declarations into respective headers. Moving #includes around. --- firmware/common/hackrf-ui.c | 12 +++--------- firmware/common/portapack.c | 17 ++++++++++------- firmware/common/portapack.h | 13 ++++++++----- firmware/common/ui_portapack.c | 8 ++++++++ firmware/common/ui_portapack.h | 4 ++++ firmware/common/ui_rad1o.c | 1 - firmware/common/ui_rad1o.h | 4 +++- firmware/hackrf_usb/hackrf_usb.c | 5 +++++ 8 files changed, 41 insertions(+), 23 deletions(-) diff --git a/firmware/common/hackrf-ui.c b/firmware/common/hackrf-ui.c index 23974135..bca2e86a 100644 --- a/firmware/common/hackrf-ui.c +++ b/firmware/common/hackrf-ui.c @@ -21,7 +21,7 @@ #include "hackrf-ui.h" -#include "portapack.h" +#include "ui_portapack.h" #include "ui_rad1o.h" #include @@ -60,20 +60,14 @@ static const hackrf_ui_t hackrf_ui_null = { &hackrf_ui_set_antenna_bias_null, }; -const portapack_t* portapack_init(void) __attribute__((weak)); -const hackrf_ui_t* rad1o_ui_setup(void) __attribute__((weak)); - static const hackrf_ui_t* ui = NULL; const hackrf_ui_t* hackrf_ui(void) { /* Detect on first use. If no UI hardware is detected, use a stub function table. */ if( ui == NULL ) { #ifdef HACKRF_ONE - if( portapack_init ) { - const portapack_t* const portapack = portapack_init(); - if( portapack != NULL ) { - ui = portapack->hackrf_ui; - } + if( portapack_hackrf_ui_init ) { + ui = portapack_hackrf_ui_init(); } #endif #ifdef RAD1O diff --git a/firmware/common/portapack.c b/firmware/common/portapack.c index 4bbb2312..2931ff80 100644 --- a/firmware/common/portapack.c +++ b/firmware/common/portapack.c @@ -556,19 +556,22 @@ static bool portapack_detect(void) { return jtag_pp_idcode() == 0x020A50DD; } -extern const hackrf_ui_t portapack_hackrf_ui; - -const portapack_t portapack = { - &portapack_hackrf_ui, +static const portapack_t portapack_instance = { }; -const portapack_t* portapack_init(void) { +static const portapack_t* portapack_pointer = NULL; + +const portapack_t* portapack(void) { + return portapack_pointer; +} + +void portapack_init(void) { if( portapack_detect() ) { portapack_if_init(); portapack_lcd_reset(); portapack_lcd_init(); - return &portapack; + portapack_pointer = &portapack_instance; } else { - return NULL; + portapack_pointer = NULL; } } \ No newline at end of file diff --git a/firmware/common/portapack.h b/firmware/common/portapack.h index 9febdbbd..9c537b0b 100644 --- a/firmware/common/portapack.h +++ b/firmware/common/portapack.h @@ -22,9 +22,9 @@ #ifndef __PORTAPACK_H__ #define __PORTAPACK_H__ +#include #include - -#include "hackrf-ui.h" +#include #define ARRAY_SIZEOF(x) (sizeof(x) / sizeof(x[0])) @@ -60,11 +60,14 @@ typedef struct ui_font_t { size_t data_stride; } ui_font_t; -typedef struct { - const hackrf_ui_t* const hackrf_ui; +typedef struct portapack_t { } portapack_t; -const portapack_t* portapack_init(void); +void portapack_init(void); + +/* If the "portapack" symbol is defined, PortaPack support is compiled in */ +/* If the portapack() call returns non-NULL, a PortaPack was detected and is initialized. */ +const portapack_t* portapack(void); void portapack_backlight(const bool on); diff --git a/firmware/common/ui_portapack.c b/firmware/common/ui_portapack.c index f80c1c6c..a61c3b5a 100644 --- a/firmware/common/ui_portapack.c +++ b/firmware/common/ui_portapack.c @@ -500,3 +500,11 @@ const hackrf_ui_t portapack_hackrf_ui = { &portapack_ui_set_filter, &portapack_ui_set_antenna_bias, }; + +const hackrf_ui_t* portapack_hackrf_ui_init() { + if( portapack() ) { + return &portapack_hackrf_ui; + } else { + return NULL; + } +} diff --git a/firmware/common/ui_portapack.h b/firmware/common/ui_portapack.h index 69754c7e..22d27d9c 100644 --- a/firmware/common/ui_portapack.h +++ b/firmware/common/ui_portapack.h @@ -22,4 +22,8 @@ #ifndef __UI_PORTAPACK_H__ #define __UI_PORTAPACK_H__ +#include "hackrf-ui.h" + +const hackrf_ui_t* portapack_hackrf_ui_init() __attribute__((weak)); + #endif/*__UI_PORTAPACK_H__*/ diff --git a/firmware/common/ui_rad1o.c b/firmware/common/ui_rad1o.c index 4d3b1254..aac72369 100644 --- a/firmware/common/ui_rad1o.c +++ b/firmware/common/ui_rad1o.c @@ -19,7 +19,6 @@ * Boston, MA 02110-1301, USA. */ -#include "hackrf-ui.h" #include "ui_rad1o.h" /* Weak functions from rad1o app */ diff --git a/firmware/common/ui_rad1o.h b/firmware/common/ui_rad1o.h index 6e658e5b..7c14a79a 100644 --- a/firmware/common/ui_rad1o.h +++ b/firmware/common/ui_rad1o.h @@ -22,6 +22,8 @@ #ifndef __UI_RAD1O_H__ #define __UI_RAD1O_H__ -const hackrf_ui_t* rad1o_ui_setup(void); +#include "hackrf-ui.h" + +const hackrf_ui_t* rad1o_ui_setup(void) __attribute__((weak)); #endif/*__UI_RAD1O_H__*/ diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 4714dbfe..92ab9aa2 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -46,6 +46,7 @@ #include "usb_api_transceiver.h" #include "usb_bulk_buffer.h" #include "cpld_xc2c.h" +#include "portapack.h" #include "hackrf-ui.h" @@ -235,6 +236,10 @@ int main(void) { halt_and_flash(6000000); } +#ifdef HACKRF_ONE + portapack_init(); +#endif + #ifndef DFU_MODE usb_set_descriptor_by_serial_number(); #endif From 46fd11af5bbd62d60420d0e4070c6a31e9ba4df0 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 20 Mar 2019 11:16:44 -0700 Subject: [PATCH 6/7] Si5351C: Extract best block source function into hackrf_core. It's not an Si5351C driver thing, but a HackRF thing. Also added a driver function to check if CLKIN signal is valid, and made use of it, instead of opaque register read code. --- firmware/common/hackrf_core.c | 9 ++++++++ firmware/common/hackrf_core.h | 2 ++ firmware/common/si5351c.c | 25 +++++++---------------- firmware/common/si5351c.h | 4 +++- firmware/hackrf_usb/usb_api_transceiver.c | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c index 64254c5d..a42a06ae 100644 --- a/firmware/common/hackrf_core.c +++ b/firmware/common/hackrf_core.c @@ -712,6 +712,15 @@ void cpu_clock_init(void) #endif } +void activate_best_clock_source(void) +{ + if (si5351c_clkin_signal_valid(&clock_gen)) { + si5351c_set_clock_source(&clock_gen, PLL_SOURCE_CLKIN); + } else { + si5351c_set_clock_source(&clock_gen, PLL_SOURCE_XTAL); + } +} + void ssp1_set_mode_max2837(void) { spi_bus_start(max2837.bus, &ssp_config_max2837); diff --git a/firmware/common/hackrf_core.h b/firmware/common/hackrf_core.h index 3b2f7982..e6e4e811 100644 --- a/firmware/common/hackrf_core.h +++ b/firmware/common/hackrf_core.h @@ -294,6 +294,8 @@ bool sample_rate_frac_set(uint32_t rate_num, uint32_t rate_denom); bool sample_rate_set(const uint32_t sampling_rate_hz); bool baseband_filter_bandwidth_set(const uint32_t bandwidth_hz); +void activate_best_clock_source(void); + #if (defined HACKRF_ONE || defined RAD1O) void enable_rf_power(void); void disable_rf_power(void); diff --git a/firmware/common/si5351c.c b/firmware/common/si5351c.c index 64a3dda1..a7364241 100644 --- a/firmware/common/si5351c.c +++ b/firmware/common/si5351c.c @@ -22,7 +22,7 @@ #include "si5351c.h" -enum pll_sources active_clock_source; +enum pll_sources active_clock_source = PLL_SOURCE_UNINITIALIZED; /* write to single register */ void si5351c_write_single(si5351c_driver_t* const drv, uint8_t reg, uint8_t val) @@ -239,25 +239,14 @@ void si5351c_set_int_mode(si5351c_driver_t* const drv, const uint_fast8_t ms_num void si5351c_set_clock_source(si5351c_driver_t* const drv, const enum pll_sources source) { - si5351c_configure_clock_control(drv, source); - active_clock_source = source; + if( source != active_clock_source ) { + si5351c_configure_clock_control(drv, source); + active_clock_source = source; + } } -void si5351c_activate_best_clock_source(si5351c_driver_t* const drv) -{ - uint8_t device_status = si5351c_read_single(drv, 0); - - if (device_status & SI5351C_LOS) { - /* CLKIN not detected */ - if (active_clock_source == PLL_SOURCE_CLKIN) { - si5351c_set_clock_source(drv, PLL_SOURCE_XTAL); - } - } else { - /* CLKIN detected */ - if (active_clock_source == PLL_SOURCE_XTAL) { - si5351c_set_clock_source(drv, PLL_SOURCE_CLKIN); - } - } +bool si5351c_clkin_signal_valid(si5351c_driver_t* const drv) { + return (si5351c_read_single(drv, 0) & SI5351C_LOS) == 0; } void si5351c_clkout_enable(si5351c_driver_t* const drv, uint8_t enable) diff --git a/firmware/common/si5351c.h b/firmware/common/si5351c.h index 533c5830..03e82260 100644 --- a/firmware/common/si5351c.h +++ b/firmware/common/si5351c.h @@ -29,6 +29,7 @@ extern "C" #endif #include +#include #include "i2c_bus.h" @@ -59,6 +60,7 @@ extern "C" #define SI5351C_LOS (1<<4) enum pll_sources { + PLL_SOURCE_UNINITIALIZED = -1, PLL_SOURCE_XTAL = 0, PLL_SOURCE_CLKIN = 1, }; @@ -84,7 +86,7 @@ void si5351c_configure_clock_control(si5351c_driver_t* const drv, const enum pll void si5351c_enable_clock_outputs(si5351c_driver_t* const drv); void si5351c_set_int_mode(si5351c_driver_t* const drv, const uint_fast8_t ms_number, const uint_fast8_t on); void si5351c_set_clock_source(si5351c_driver_t* const drv, const enum pll_sources source); -void si5351c_activate_best_clock_source(si5351c_driver_t* const drv); +bool si5351c_clkin_signal_valid(si5351c_driver_t* const drv); void si5351c_write_single(si5351c_driver_t* const drv, uint8_t reg, uint8_t val); uint8_t si5351c_read_single(si5351c_driver_t* const drv, uint8_t reg); diff --git a/firmware/hackrf_usb/usb_api_transceiver.c b/firmware/hackrf_usb/usb_api_transceiver.c index fda9b6a9..3d64d7f9 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.c +++ b/firmware/hackrf_usb/usb_api_transceiver.c @@ -273,7 +273,7 @@ void set_transceiver_mode(const transceiver_mode_t new_transceiver_mode) { if( _transceiver_mode != TRANSCEIVER_MODE_OFF ) { - si5351c_activate_best_clock_source(&clock_gen); + activate_best_clock_source(); hw_sync_enable(_hw_sync_mode); From dccb7482166b3faa03d44646291fde63006ffe15 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 20 Mar 2019 13:27:20 -0700 Subject: [PATCH 7/7] PortaPack: Add check for PortaPack clock reference, use if present. Wow, it takes a lot of ugly code to keep blinky happy... --- firmware/common/hackrf_core.c | 26 ++++++++++++++++++++++++++ firmware/common/portapack.h | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c index a42a06ae..1ee7e3e6 100644 --- a/firmware/common/hackrf_core.c +++ b/firmware/common/hackrf_core.c @@ -39,6 +39,10 @@ #include #include +#ifdef HACKRF_ONE +#include "portapack.h" +#endif + #include "gpio_lpc.h" #define WAIT_CPU_CLOCK_INIT_DELAY (10000) @@ -714,9 +718,31 @@ void cpu_clock_init(void) void activate_best_clock_source(void) { +#ifdef HACKRF_ONE + /* Ensure PortaPack reference oscillator is off while checking for external clock input. */ + if( portapack_reference_oscillator && portapack()) { + portapack_reference_oscillator(false); + } +#endif + + /* Check for external clock input. */ if (si5351c_clkin_signal_valid(&clock_gen)) { si5351c_set_clock_source(&clock_gen, PLL_SOURCE_CLKIN); } else { +#ifdef HACKRF_ONE + /* Enable PortaPack reference oscillator (if present), and check for valid clock. */ + if( portapack_reference_oscillator && portapack() ) { + portapack_reference_oscillator(true); + delay(510000); /* loop iterations @ 204MHz for >10ms for oscillator to enable. */ + if (si5351c_clkin_signal_valid(&clock_gen)) { + si5351c_set_clock_source(&clock_gen, PLL_SOURCE_CLKIN); + return; + } else { + portapack_reference_oscillator(false); + } + } +#endif + /* No external or PortaPack clock was found. Use HackRF Si5351C crystal. */ si5351c_set_clock_source(&clock_gen, PLL_SOURCE_XTAL); } } diff --git a/firmware/common/portapack.h b/firmware/common/portapack.h index 9c537b0b..98f682f2 100644 --- a/firmware/common/portapack.h +++ b/firmware/common/portapack.h @@ -67,11 +67,11 @@ void portapack_init(void); /* If the "portapack" symbol is defined, PortaPack support is compiled in */ /* If the portapack() call returns non-NULL, a PortaPack was detected and is initialized. */ -const portapack_t* portapack(void); +const portapack_t* portapack(void) __attribute__((weak)); void portapack_backlight(const bool on); -void portapack_reference_oscillator(const bool on); +void portapack_reference_oscillator(const bool on) __attribute__((weak)); void portapack_fill_rectangle( const ui_rect_t rect,