From b4b768c76f49aeeeeb669e68e67368a06269d714 Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Sun, 7 Jun 2015 13:58:46 +0200 Subject: [PATCH] feat(max2871.c): Set the default values after reset --- firmware/common/hackrf_core.h | 24 +++--- firmware/common/max2871.c | 128 ++++++++++++++++++++++++++++++- firmware/common/mixer.h | 2 + firmware/common/rf_path.c | 4 + firmware/hackrf_usb/hackrf_usb.c | 2 +- 5 files changed, 143 insertions(+), 17 deletions(-) diff --git a/firmware/common/hackrf_core.h b/firmware/common/hackrf_core.h index 1c7fdca4..9325cb87 100644 --- a/firmware/common/hackrf_core.h +++ b/firmware/common/hackrf_core.h @@ -160,10 +160,10 @@ extern "C" #define SCU_MIXER_RESETX (P5_5) /* GPIO2[14] on P5_5 */ #endif #ifdef RAD10 -#define SCU_MIXER_ENX (P5_4) /* GPIO2[13] on P5_4 */ -#define SCU_MIXER_SCLK (P2_6) /* GPIO5[6] on P2_6 */ -#define SCU_MIXER_SDATA (P6_4) /* GPIO3[3] on P6_4 */ -#define SCU_MIXER_LE (P5_5) /* GPIO2[14] on P5_5 */ +#define SCU_VCO_CE (P5_4) /* GPIO2[13] on P5_4 */ +#define SCU_VCO_SCLK (P2_6) /* GPIO5[6] on P2_6 */ +#define SCU_VCO_SDATA (P6_4) /* GPIO3[3] on P6_4 */ +#define SCU_VCO_LE (P5_5) /* GPIO2[14] on P5_5 */ #define SCU_MIXER_EN (P6_8) /* GPIO5[16] on P6_8 */ #endif @@ -315,14 +315,14 @@ extern "C" #define PORT_MIXER_RESETX (GPIO2) #endif #ifdef RAD10 -#define PIN_MIXER_ENX (BIT13) /* GPIO2[13] on P5_4 */ -#define PORT_MIXER_ENX (GPIO2) -#define PIN_MIXER_SCLK (BIT6) /* GPIO5[6] on P2_6 */ -#define PORT_MIXER_SCLK (GPIO5) -#define PIN_MIXER_SDATA (BIT3) /* GPIO3[3] on P6_4 */ -#define PORT_MIXER_SDATA (GPIO3) -#define PIN_MIXER_LE (BIT14) /* GPIO2[14] on P5_5 */ -#define PORT_MIXER_LE (GPIO2) +#define PIN_VCO_CE (BIT13) /* GPIO2[13] on P5_4 */ +#define PORT_VCO_CE (GPIO2) +#define PIN_VCO_SCLK (BIT6) /* GPIO5[6] on P2_6 */ +#define PORT_VCO_SCLK (GPIO5) +#define PIN_VCO_SDATA (BIT3) /* GPIO3[3] on P6_4 */ +#define PORT_VCO_SDATA (GPIO3) +#define PIN_VCO_LE (BIT14) /* GPIO2[14] on P5_5 */ +#define PORT_VCO_LE (GPIO2) #define PIN_MIXER_EN (BIT16) /* GPIO5[16] on P6_8 */ #define PORT_MIXER_EN (GPIO5) #endif diff --git a/firmware/common/max2871.c b/firmware/common/max2871.c index 3e454978..5b960e8a 100644 --- a/firmware/common/max2871.c +++ b/firmware/common/max2871.c @@ -1,5 +1,25 @@ -#include +#include "mixer.h" +//#include "max2871.h" +//#include "mac2871_regs.def" // private register def macros +#if (defined DEBUG) +#include +#define LOG printf +#else +#define LOG(x,...) +#include +#include +#include +#include "hackrf_core.h" +#endif + +#include +#include + +static void max2871_spi_write(uint8_t r, uint32_t v); +static void delay_ms(int ms); + +static uint32_t registers[6]; /* * - The input is fixed to 50 MHz * f_REF = 50 MHz @@ -116,10 +136,110 @@ * */ -void mixer_init(void) -{} void mixer_setup(void) -{} +{ + /* Configure GPIO pins. */ + scu_pinmux(SCU_VCO_CE, SCU_GPIO_FAST); + //scu_pinmux(SCU_VCO_SCLK, SCU_GPIO_FAST | SCU_CONF_FUNCTION4); + scu_pinmux(SCU_VCO_SCLK, SCU_GPIO_FAST); + scu_pinmux(SCU_VCO_SDATA, SCU_GPIO_FAST); + scu_pinmux(SCU_VCO_LE, SCU_GPIO_FAST); + + /* Set GPIO pins as outputs. */ + GPIO_DIR(PORT_VCO_CE) |= PIN_VCO_CE; + GPIO_DIR(PORT_VCO_SCLK) |= PIN_VCO_SCLK; + GPIO_DIR(PORT_VCO_SDATA) |= PIN_VCO_SDATA; + GPIO_DIR(PORT_VCO_LE) |= PIN_VCO_LE; + + /* set to known state */ + gpio_set(PORT_VCO_CE, PIN_VCO_CE); /* active high */ + gpio_clear(PORT_VCO_SCLK, PIN_VCO_SCLK); + gpio_clear(PORT_VCO_SDATA, PIN_VCO_SDATA); + gpio_set(PORT_VCO_LE, PIN_VCO_LE); /* active low */ + + registers[0] = 0x007D0000; + registers[1] = 0x2000FFF9; + registers[2] = 0x00004042; + registers[3] = 0x0000000B; + registers[4] = 0x6180B23C; + registers[5] = 0x00400005; + + int i; + for(i = 5; i >= 0; i--) { + max2871_spi_write(i, registers[i]); + delay_ms(20); + } +} + +static void delay_ms(int ms) +{ + uint32_t i; + while(ms--) { + for (i = 0; i < 20000; i++) { + __asm__("nop"); + } + } +} + + +static void serial_delay(void) +{ + uint32_t i; + + for (i = 0; i < 2; i++) + __asm__("nop"); +} + + +/* SPI register write + * + * Send 32 bits: + * First 29 bits are data + * Last 3 bits are register number + */ +static void max2871_spi_write(uint8_t r, uint32_t v) { + +#if DEBUG + LOG("0x%04x -> reg%d\n", v, r); +#else + + uint32_t bits = 32; + uint32_t msb = 1 << (bits -1); + uint32_t data = v | r; + + /* make sure everything is starting in the correct state */ + gpio_set(PORT_VCO_LE, PIN_VCO_LE); + gpio_clear(PORT_VCO_SCLK, PIN_VCO_SCLK); + gpio_clear(PORT_VCO_SDATA, PIN_VCO_SDATA); + + /* start transaction by bringing LE low */ + gpio_clear(PORT_VCO_LE, PIN_VCO_LE); + + while (bits--) { + if (data & msb) + gpio_set(PORT_VCO_SDATA, PIN_VCO_SDATA); + else + gpio_clear(PORT_VCO_SDATA, PIN_VCO_SDATA); + data <<= 1; + + serial_delay(); + gpio_set(PORT_VCO_SCLK, PIN_VCO_SCLK); + + serial_delay(); + gpio_clear(PORT_VCO_SCLK, PIN_VCO_SCLK); + } + + gpio_set(PORT_VCO_LE, PIN_VCO_LE); +#endif +} + +void max2871_write_registers(void) +{ + int i; + for(i = 5; i >= 0; i--) { + max2871_spi_write(i, registers[i]); + } +} /* Set frequency (MHz). */ uint64_t mixer_set_frequency(uint16_t mhz) diff --git a/firmware/common/mixer.h b/firmware/common/mixer.h index 1ee30057..d48e748b 100644 --- a/firmware/common/mixer.h +++ b/firmware/common/mixer.h @@ -23,6 +23,8 @@ #ifndef __MIXER_H #define __MIXER_H +#include + /* Initialize chip. Call _setup() externally, as it calls _init(). */ extern void mixer_init(void); extern void mixer_setup(void); diff --git a/firmware/common/rf_path.c b/firmware/common/rf_path.c index ba2cc12e..906e613b 100644 --- a/firmware/common/rf_path.c +++ b/firmware/common/rf_path.c @@ -99,9 +99,11 @@ static void switchctrl_set_rad10(uint8_t ctrl) { if (ctrl & SWITCHCTRL_MIX_BYPASS) { gpio_clear(PORT_BY_MIX, PIN_BY_MIX); gpio_set(PORT_BY_MIX_N, PIN_BY_MIX_N); + gpio_clear(PORT_MIXER_EN, PIN_MIXER_EN); } else { gpio_set(PORT_BY_MIX, PIN_BY_MIX); gpio_clear(PORT_BY_MIX_N, PIN_BY_MIX_N); + gpio_set(PORT_MIXER_EN, PIN_MIXER_EN); } if (ctrl & SWITCHCTRL_HP) { @@ -301,6 +303,7 @@ void rf_path_pin_setup() { scu_pinmux(SCU_LOW_HIGH_FILT_N,SCU_GPIO_FAST | SCU_CONF_FUNCTION0); scu_pinmux(SCU_TX_AMP, SCU_GPIO_FAST | SCU_CONF_FUNCTION0); scu_pinmux(SCU_RX_LNA, SCU_GPIO_FAST | SCU_CONF_FUNCTION4); + scu_pinmux(SCU_MIXER_EN, SCU_GPIO_FAST | SCU_CONF_FUNCTION4); /* Configure RF power supply (VAA) switch */ scu_pinmux(SCU_VAA_ENABLE, SCU_GPIO_FAST | SCU_CONF_FUNCTION0); @@ -310,6 +313,7 @@ void rf_path_pin_setup() { GPIO1_DIR |= PIN_BY_AMP | PIN_TX_RX_N | PIN_BY_MIX; GPIO2_DIR |= PIN_BY_MIX_N | PIN_LOW_HIGH_FILT | PIN_LOW_HIGH_FILT_N | PIN_TX_AMP; GPIO5_DIR |= PIN_BY_AMP_N | PIN_RX_LNA; + GPIO_DIR(PORT_MIXER_EN) |= PIN_MIXER_EN; /* * Safe (initial) switch settings turn off both amplifiers and antenna port diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 53f41505..ca20293e 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -219,7 +219,7 @@ void usb_set_descriptor_by_serial_number(void) int main(void) { pin_setup(); enable_1v8_power(); -#ifdef HACKRF_ONE +#if (defined HACKRF_ONE || defined RAD10) enable_rf_power(); #endif cpu_clock_init();