MAX2837: Refactoring toward abstracted SPI.

Conflicts:
	firmware/common/hackrf_core.c
	firmware/common/hackrf_core.h
	firmware/common/tuning.c
This commit is contained in:
Jared Boone
2014-11-09 12:08:21 -08:00
parent f034bc82ca
commit 58e7ef4171
10 changed files with 167 additions and 83 deletions

View File

@ -24,6 +24,8 @@
#include "hackrf_core.h"
#include "si5351c.h"
#include "max2837.h"
#include "max2837_spi.h"
#include "max2837_target.h"
#include "rffc5071.h"
#include "rffc5071_spi.h"
#include "sgpio.h"
@ -36,7 +38,10 @@
#define WAIT_CPU_CLOCK_INIT_DELAY (10000)
max2837_driver_t max2837;
max2837_driver_t max2837 = {
.spi = NULL, /* TODO */
};
spi_t rffc5071_spi = {
.init = rffc5071_spi_init,
@ -528,38 +533,11 @@ void ssp1_init(void)
scu_pinmux(SCU_AD_CS, SCU_GPIO_FAST);
GPIO_SET(PORT_AD_CS) = PIN_AD_CS;
GPIO_DIR(PORT_AD_CS) |= PIN_AD_CS;
scu_pinmux(SCU_XCVR_CS, SCU_GPIO_FAST);
GPIO_SET(PORT_XCVR_CS) = PIN_XCVR_CS;
GPIO_DIR(PORT_XCVR_CS) |= PIN_XCVR_CS;
/* Configure SSP1 Peripheral (to be moved later in SSP driver) */
scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
}
void ssp1_set_mode_max2837(void)
{
/* FIXME speed up once everything is working reliably */
/*
// Freq About 0.0498MHz / 49.8KHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=204MHz
const uint8_t serial_clock_rate = 32;
const uint8_t clock_prescale_rate = 128;
*/
// Freq About 4.857MHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=204MHz
const uint8_t serial_clock_rate = 21;
const uint8_t clock_prescale_rate = 2;
ssp_init(SSP1_NUM,
SSP_DATA_16BITS,
SSP_FRAME_SPI,
SSP_CPOL_0_CPHA_0,
serial_clock_rate,
clock_prescale_rate,
SSP_MODE_NORMAL,
SSP_MASTER,
SSP_SLAVE_OUT_ENABLE);
max2837_spi_init(max2837.spi);
}
void ssp1_set_mode_max5864(void)

View File

@ -32,7 +32,7 @@ extern "C"
#include <stdint.h>
#include <stdbool.h>
#include "max2837_drv.h"
#include "max2837.h"
#include "rffc5071.h"
#include "w25q80bv.h"

View File

@ -31,7 +31,8 @@
#include <stdint.h>
#include <string.h>
#include "max2837.h"
#include "max2837_drv.h"
#include "max2837_spi.h"
#include "max2837_target.h"
#include "max2837_regs.def" // private register def macros
#include "hackrf_core.h"
@ -77,8 +78,12 @@ static const uint16_t max2837_regs_default[MAX2837_NUM_REGS] = {
0x000 }; /* 31 */
/* Set up all registers according to defaults specified in docs. */
void max2837_init(max2837_driver_t* const drv)
static void max2837_init(max2837_driver_t* const drv)
{
max2837_spi_init(drv->spi);
max2837_mode_shutdown(drv);
max2837_target_init(drv);
memcpy(drv->regs, max2837_regs_default, sizeof(drv->regs));
drv->regs_dirty = 0xffffffff;
@ -92,10 +97,8 @@ void max2837_init(max2837_driver_t* const drv)
*/
void max2837_setup(max2837_driver_t* const drv)
{
max2837_pin_config(drv);
max2837_init(drv);
/* Use SPI control instead of B1-B7 pins for gain settings. */
set_MAX2837_TXVGA_GAIN_SPI_EN(drv, 1);
set_MAX2837_TXVGA_GAIN_MSB_SPI_EN(drv, 1);
@ -121,10 +124,21 @@ void max2837_setup(max2837_driver_t* const drv)
max2837_regs_commit(drv);
}
static uint16_t max2837_read(max2837_driver_t* const drv, uint8_t r) {
uint16_t value = (1 << 15) | (r << 10);
max2837_spi_transfer(drv->spi, &value, 1);
return value & 0x3ff;
}
static void max2837_write(max2837_driver_t* const drv, uint8_t r, uint16_t v) {
uint16_t value = (r << 10) | (v & 0x3ff);
max2837_spi_transfer(drv->spi, &value, 1);
}
uint16_t max2837_reg_read(max2837_driver_t* const drv, uint8_t r)
{
if ((drv->regs_dirty >> r) & 0x1) {
drv->regs[r] = max2837_spi_read(drv, r);
drv->regs[r] = max2837_read(drv, r);
};
return drv->regs[r];
}
@ -132,7 +146,7 @@ uint16_t max2837_reg_read(max2837_driver_t* const drv, uint8_t r)
void max2837_reg_write(max2837_driver_t* const drv, uint8_t r, uint16_t v)
{
drv->regs[r] = v;
max2837_spi_write(drv, r, v);
max2837_write(drv, r, v);
MAX2837_REG_SET_CLEAN(drv, r);
}

View File

@ -26,10 +26,26 @@
#include <stdint.h>
#include <stdbool.h>
#include "max2837_drv.h"
#include "spi.h"
/* 32 registers, each containing 10 bits of data. */
#define MAX2837_NUM_REGS 32
#define MAX2837_DATA_REGS_MAX_VALUE 1024
typedef enum {
MAX2837_MODE_SHUTDOWN,
MAX2837_MODE_STANDBY,
MAX2837_MODE_TX,
MAX2837_MODE_RX
} max2837_mode_t;
typedef struct {
spi_t* const spi;
uint16_t regs[MAX2837_NUM_REGS];
uint32_t regs_dirty;
} max2837_driver_t;
/* Initialize chip. */
extern void max2837_init(max2837_driver_t* const drv);
extern void max2837_setup(max2837_driver_t* const drv);
/* Read a register via SPI. Save a copy to memory and return

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012 Will Code? (TODO: Proper attribution)
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* 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 "max2837_spi.h"
#include <libopencm3/lpc43xx/gpio.h>
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/ssp.h>
#include "hackrf_core.h"
void max2837_spi_init(spi_t* const spi) {
(void)spi;
/* FIXME speed up once everything is working reliably */
/*
// Freq About 0.0498MHz / 49.8KHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=204MHz
const uint8_t serial_clock_rate = 32;
const uint8_t clock_prescale_rate = 128;
*/
// Freq About 4.857MHz => Freq = PCLK / (CPSDVSR * [SCR+1]) with PCLK=PLL1=204MHz
const uint8_t serial_clock_rate = 21;
const uint8_t clock_prescale_rate = 2;
ssp_init(SSP1_NUM,
SSP_DATA_16BITS,
SSP_FRAME_SPI,
SSP_CPOL_0_CPHA_0,
serial_clock_rate,
clock_prescale_rate,
SSP_MODE_NORMAL,
SSP_MASTER,
SSP_SLAVE_OUT_ENABLE);
/* Configure SSP1 Peripheral (to be moved later in SSP driver) */
scu_pinmux(SCU_SSP1_MISO, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
scu_pinmux(SCU_SSP1_MOSI, (SCU_SSP_IO | SCU_CONF_FUNCTION5));
scu_pinmux(SCU_SSP1_SCK, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
}
void max2837_spi_transfer(spi_t* const spi, void* const _data, const size_t count) {
(void)spi;
uint16_t* const data = _data;
gpio_clear(PORT_XCVR_CS, PIN_XCVR_CS);
for(size_t i=0; i<count; i++) {
data[i] = ssp_transfer(SSP1_NUM, data[i]);
}
gpio_set(PORT_XCVR_CS, PIN_XCVR_CS);
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2012 Will Code? (TODO: Proper attribution)
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* 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 __MAX2837_SPI_H__
#define __MAX2837_SPI_H__
#include "stdint.h"
#include "stddef.h"
#include "spi.h"
void max2837_spi_init(spi_t* const spi);
void max2837_spi_transfer(spi_t* const spi, void* const data, const size_t count);
#endif/*__MAX2837_SPI_H__*/

View File

@ -20,14 +20,19 @@
* Boston, MA 02110-1301, USA.
*/
#include "max2837_drv.h"
#include "max2837_target.h"
#include <libopencm3/lpc43xx/ssp.h>
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/gpio.h>
#include "hackrf_core.h"
void max2837_pin_config(max2837_driver_t* const drv) {
void max2837_target_init(max2837_driver_t* const drv) {
(void)drv;
scu_pinmux(SCU_XCVR_CS, SCU_GPIO_FAST);
GPIO_SET(PORT_XCVR_CS) = PIN_XCVR_CS;
GPIO_DIR(PORT_XCVR_CS) |= PIN_XCVR_CS;
/* Configure XCVR_CTL GPIO pins. */
#ifdef JELLYBEAN
scu_pinmux(SCU_XCVR_RXHP, SCU_GPIO_FAST);
@ -58,7 +63,6 @@ void max2837_pin_config(max2837_driver_t* const drv) {
;
#endif
max2837_mode_shutdown(drv);
#ifdef JELLYBEAN
gpio_set(PORT_XCVR_RXHP, PIN_XCVR_RXHP);
gpio_set(PORT_XCVR_B,
@ -131,20 +135,3 @@ max2837_mode_t max2837_mode(max2837_driver_t* const drv) {
return MAX2837_MODE_SHUTDOWN;
}
}
/* SPI register read. */
uint16_t max2837_spi_read(max2837_driver_t* const drv, uint8_t r) {
(void)drv;
gpio_clear(PORT_XCVR_CS, PIN_XCVR_CS);
const uint16_t value = ssp_transfer(SSP1_NUM, (uint16_t)((1 << 15) | (r << 10)));
gpio_set(PORT_XCVR_CS, PIN_XCVR_CS);
return value & 0x3ff;
}
/* SPI register write */
void max2837_spi_write(max2837_driver_t* const drv, uint8_t r, uint16_t v) {
(void)drv;
gpio_clear(PORT_XCVR_CS, PIN_XCVR_CS);
ssp_transfer(SSP1_NUM, (uint16_t)((r << 10) | (v & 0x3ff)));
gpio_set(PORT_XCVR_CS, PIN_XCVR_CS);
}

View File

@ -20,35 +20,18 @@
* Boston, MA 02110-1301, USA.
*/
#ifndef __MAX2837_DRV_H
#define __MAX2837_DRV_H
#ifndef __MAX2837_TARGET_H
#define __MAX2837_TARGET_H
#include <stdint.h>
/* 32 registers, each containing 10 bits of data. */
#define MAX2837_NUM_REGS 32
#define MAX2837_DATA_REGS_MAX_VALUE 1024
#include "max2837.h"
typedef enum {
MAX2837_MODE_SHUTDOWN,
MAX2837_MODE_STANDBY,
MAX2837_MODE_TX,
MAX2837_MODE_RX
} max2837_mode_t;
typedef struct {
uint16_t regs[MAX2837_NUM_REGS];
uint32_t regs_dirty;
} max2837_driver_t;
void max2837_pin_config(max2837_driver_t* const drv);
void max2837_target_init(max2837_driver_t* const drv);
void max2837_mode_shutdown(max2837_driver_t* const drv);
void max2837_mode_standby(max2837_driver_t* const drv);
void max2837_mode_tx(max2837_driver_t* const drv);
void max2837_mode_rx(max2837_driver_t* const drv);
max2837_mode_t max2837_mode(max2837_driver_t* const drv);
uint16_t max2837_spi_read(max2837_driver_t* const drv, uint8_t r);
void max2837_spi_write(max2837_driver_t* const drv, uint8_t r, uint16_t v);
#endif // __MAX2837_DRV_H
#endif // __MAX2837_TARGET_H

View File

@ -26,6 +26,7 @@
#include <rffc5071.h>
#include <max2837.h>
#include <sgpio.h>
#include "max2837_target.h"
#define FREQ_ONE_MHZ (1000*1000)

View File

@ -135,7 +135,8 @@ macro(DeclareTargets)
${PATH_HACKRF_FIRMWARE_COMMON}/rf_path.c
${PATH_HACKRF_FIRMWARE_COMMON}/si5351c.c
${PATH_HACKRF_FIRMWARE_COMMON}/max2837.c
${PATH_HACKRF_FIRMWARE_COMMON}/max2837_drv.c
${PATH_HACKRF_FIRMWARE_COMMON}/max2837_spi.c
${PATH_HACKRF_FIRMWARE_COMMON}/max2837_target.c
${PATH_HACKRF_FIRMWARE_COMMON}/max5864.c
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071.c
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071_spi.c