MAX5864: Abstract SPI, extract target code

This commit is contained in:
Jared Boone
2014-11-09 13:48:15 -08:00
parent 579f8212a6
commit e6c02bea62
16 changed files with 240 additions and 75 deletions

View File

@ -26,6 +26,9 @@
#include "max2837.h"
#include "max2837_spi.h"
#include "max2837_target.h"
#include "max5864.h"
#include "max5864_spi.h"
#include "max5864_target.h"
#include "rffc5071.h"
#include "rffc5071_spi.h"
#include "sgpio.h"
@ -48,6 +51,15 @@ max2837_driver_t max2837 = {
.spi = &max2837_spi,
};
spi_t max5864_spi = {
.init = max5864_spi_init,
.transfer = max5864_spi_transfer,
.transfer_gather = max5864_spi_transfer_gather,
};
max5864_driver_t max5864 = {
.spi = &max5864_spi,
};
spi_t rffc5071_spi = {
.init = rffc5071_spi_init,
@ -530,17 +542,6 @@ void cpu_clock_pll1_max_speed(void)
}
void ssp1_init(void)
{
/*
* Configure CS_AD pin to keep the MAX5864 SPI disabled while we use the
* SPI bus for the MAX2837. FIXME: this should probably be somewhere else.
*/
scu_pinmux(SCU_AD_CS, SCU_GPIO_FAST);
GPIO_SET(PORT_AD_CS) = PIN_AD_CS;
GPIO_DIR(PORT_AD_CS) |= PIN_AD_CS;
}
void ssp1_set_mode_max2837(void)
{
spi_init(max2837.spi);
@ -548,25 +549,7 @@ void ssp1_set_mode_max2837(void)
void ssp1_set_mode_max5864(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_8BITS,
SSP_FRAME_SPI,
SSP_CPOL_0_CPHA_0,
serial_clock_rate,
clock_prescale_rate,
SSP_MODE_NORMAL,
SSP_MASTER,
SSP_SLAVE_OUT_ENABLE);
spi_init(max5864.spi);
}
void pin_setup(void) {
@ -612,12 +595,6 @@ void pin_setup(void) {
rf_path_pin_setup();
/* 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));
scu_pinmux(SCU_SSP1_SSEL, (SCU_SSP_IO | SCU_CONF_FUNCTION1));
/* Configure external clock in */
scu_pinmux(SCU_PINMUX_GP_CLKIN, SCU_CLK_IN | SCU_CONF_FUNCTION1);

View File

@ -33,6 +33,7 @@ extern "C"
#include <stdbool.h>
#include "max2837.h"
#include "max5864.h"
#include "rffc5071.h"
#include "w25q80bv.h"
@ -354,13 +355,13 @@ typedef enum {
void delay(uint32_t duration);
extern max2837_driver_t max2837;
extern max5864_driver_t max5864;
extern rffc5071_driver_t rffc5072;
extern w25q80bv_driver_t spi_flash;
void cpu_clock_init(void);
void cpu_clock_pll1_low_speed(void);
void cpu_clock_pll1_max_speed(void);
void ssp1_init(void);
void ssp1_set_mode_max2837(void);
void ssp1_set_mode_max5864(void);

View File

@ -21,16 +21,20 @@
#include <stdint.h>
#include <libopencm3/lpc43xx/gpio.h>
#include <libopencm3/lpc43xx/ssp.h>
#include "hackrf_core.h"
#include "max5864.h"
#include "max5864_target.h"
void max5864_spi_write(uint_fast8_t value) {
gpio_clear(PORT_AD_CS, PIN_AD_CS);
ssp_transfer(SSP1_NUM, value);
gpio_set(PORT_AD_CS, PIN_AD_CS);
static void max5864_write(max5864_driver_t* const drv, uint8_t value) {
spi_transfer(drv->spi, &value, 1);
}
void max5864_init(max5864_driver_t* const drv) {
spi_init(drv->spi);
max5864_target_init(drv);
}
void max5864_setup(max5864_driver_t* const drv) {
max5864_init(drv);
}
/* Set MAX5864 operation mode to "Shutdown":
@ -39,9 +43,9 @@ void max5864_spi_write(uint_fast8_t value) {
* ADCs: off (bus is tri-stated)
* DACs: off (set input bus to zero or OVdd)
*/
void max5864_shutdown()
void max5864_shutdown(max5864_driver_t* const drv)
{
max5864_spi_write(0x00);
max5864_write(drv, 0x00);
}
/* Set MAX5864 operation mode to "Standby":
@ -50,9 +54,9 @@ void max5864_shutdown()
* ADCs: off (bus is tri-stated)
* DACs: off (set input bus to zero or OVdd)
*/
void max5864_standby()
void max5864_standby(max5864_driver_t* const drv)
{
max5864_spi_write(0x05);
max5864_write(drv, 0x05);
}
/* Set MAX5864 operation mode to "Idle":
@ -61,9 +65,9 @@ void max5864_standby()
* ADCs: off (bus is tri-stated)
* DACs: off (set input bus to zero or OVdd)
*/
void max5864_idle()
void max5864_idle(max5864_driver_t* const drv)
{
max5864_spi_write(0x01);
max5864_write(drv, 0x01);
}
/* Set MAX5864 operation mode to "Rx":
@ -72,9 +76,9 @@ void max5864_idle()
* ADCs: on
* DACs: off (set input bus to zero or OVdd)
*/
void max5864_rx()
void max5864_rx(max5864_driver_t* const drv)
{
max5864_spi_write(0x02);
max5864_write(drv, 0x02);
}
/* Set MAX5864 operation mode to "Tx":
@ -83,9 +87,9 @@ void max5864_rx()
* ADCs: off (bus is tri-stated)
* DACs: on
*/
void max5864_tx()
void max5864_tx(max5864_driver_t* const drv)
{
max5864_spi_write(0x03);
max5864_write(drv, 0x03);
}
/* Set MAX5864 operation mode to "Xcvr":
@ -94,7 +98,7 @@ void max5864_tx()
* ADCs: on
* DACs: on
*/
void max5864_xcvr()
void max5864_xcvr(max5864_driver_t* const drv)
{
max5864_spi_write(0x04);
max5864_write(drv, 0x04);
}

View File

@ -22,11 +22,19 @@
#ifndef __MAX5864_H
#define __MAX5864_H
void max5864_shutdown();
void max5864_standby();
void max5864_idle();
void max5864_rx();
void max5864_tx();
void max5864_xcvr();
#include "spi.h"
typedef struct max5864_driver_t {
spi_t* const spi;
} max5864_driver_t;
void max5864_setup(max5864_driver_t* const drv);
void max5864_shutdown(max5864_driver_t* const drv);
void max5864_standby(max5864_driver_t* const drv);
void max5864_idle(max5864_driver_t* const drv);
void max5864_rx(max5864_driver_t* const drv);
void max5864_tx(max5864_driver_t* const drv);
void max5864_xcvr(max5864_driver_t* const drv);
#endif // __MAX5864_H

View File

@ -0,0 +1,78 @@
/*
* 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 "max5864_spi.h"
#include <libopencm3/lpc43xx/gpio.h>
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/ssp.h>
#include "hackrf_core.h"
void max5864_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_8BITS,
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 max5864_spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfers, const size_t count) {
(void)spi;
gpio_clear(PORT_AD_CS, PIN_AD_CS);
for(size_t i=0; i<count; i++) {
const size_t data_count = transfers[i].count;
uint8_t* const data = transfers[i].data;
for(size_t j=0; j<data_count; j++) {
data[j] = ssp_transfer(SSP1_NUM, data[j]);
}
}
gpio_set(PORT_AD_CS, PIN_AD_CS);
}
void max5864_spi_transfer(spi_t* const spi, void* const data, const size_t count) {
const spi_transfer_t transfers[] = {
{ data, count },
};
max5864_spi_transfer_gather(spi, transfers, 1);
}

View File

@ -0,0 +1,33 @@
/*
* 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 __MAX5864_SPI_H__
#define __MAX5864_SPI_H__
#include <stddef.h>
#include "spi.h"
void max5864_spi_init(spi_t* const spi);
void max5864_spi_transfer(spi_t* const spi, void* const value, const size_t count);
void max5864_spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfers, const size_t count);
#endif/*__MAX5864_SPI_H__*/

View File

@ -0,0 +1,38 @@
/*
* 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 "max5864_target.h"
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/gpio.h>
#include "hackrf_core.h"
void max5864_target_init(max5864_driver_t* const drv) {
(void)drv;
/*
* Configure CS_AD pin to keep the MAX5864 SPI disabled while we use the
* SPI bus for the MAX2837. FIXME: this should probably be somewhere else.
*/
scu_pinmux(SCU_AD_CS, SCU_GPIO_FAST);
GPIO_SET(PORT_AD_CS) = PIN_AD_CS;
GPIO_DIR(PORT_AD_CS) |= PIN_AD_CS;
}

View File

@ -0,0 +1,29 @@
/*
* 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 __MAX5864_TARGET_H__
#define __MAX5864_TARGET_H__
#include "max5864.h"
void max5864_target_init(max5864_driver_t* const drv);
#endif/*__MAX5864_TARGET_H__*/

View File

@ -209,7 +209,8 @@ void rf_path_pin_setup() {
void rf_path_init(void) {
ssp1_set_mode_max5864();
max5864_shutdown();
max5864_setup(&max5864);
max5864_shutdown(&max5864);
ssp1_set_mode_max2837();
max2837_setup(&max2837);
@ -236,7 +237,7 @@ void rf_path_set_direction(const rf_path_direction_t direction) {
rffc5071_enable(&rffc5072);
}
ssp1_set_mode_max5864();
max5864_tx();
max5864_tx(&max5864);
ssp1_set_mode_max2837();
max2837_tx(&max2837);
sgpio_configure(SGPIO_DIRECTION_TX);
@ -255,7 +256,7 @@ void rf_path_set_direction(const rf_path_direction_t direction) {
rffc5071_enable(&rffc5072);
}
ssp1_set_mode_max5864();
max5864_rx();
max5864_rx(&max5864);
ssp1_set_mode_max2837();
max2837_rx(&max2837);
sgpio_configure(SGPIO_DIRECTION_RX);
@ -270,7 +271,7 @@ void rf_path_set_direction(const rf_path_direction_t direction) {
switchctrl &= ~SWITCHCTRL_TX;
rffc5071_disable(&rffc5072);
ssp1_set_mode_max5864();
max5864_standby();
max5864_standby(&max5864);
ssp1_set_mode_max2837();
max2837_set_mode(&max2837, MAX2837_MODE_STANDBY);
sgpio_configure(SGPIO_DIRECTION_RX);

View File

@ -138,6 +138,8 @@ macro(DeclareTargets)
${PATH_HACKRF_FIRMWARE_COMMON}/max2837_spi.c
${PATH_HACKRF_FIRMWARE_COMMON}/max2837_target.c
${PATH_HACKRF_FIRMWARE_COMMON}/max5864.c
${PATH_HACKRF_FIRMWARE_COMMON}/max5864_spi.c
${PATH_HACKRF_FIRMWARE_COMMON}/max5864_target.c
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071.c
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071_spi.c
${PATH_HACKRF_FIRMWARE_COMMON}/spi.c

View File

@ -208,8 +208,6 @@ int main(void) {
usb_run(&usb_device);
ssp1_init();
rf_path_init();
unsigned int phase = 0;

View File

@ -38,7 +38,6 @@ int main(void)
enable_rf_power();
#endif
cpu_clock_init();
ssp1_init();
gpio_set(PORT_LED1_3, (PIN_LED1)); /* LED1 on */

View File

@ -96,7 +96,6 @@ int main(void) {
enable_rf_power();
#endif
cpu_clock_init();
ssp1_init();
rf_path_init();
rf_path_set_direction(RF_PATH_DIRECTION_RX);

View File

@ -72,12 +72,12 @@ int main(void) {
pin_setup();
enable_1v8_power();
cpu_clock_init();
ssp1_init();
gpio_set(PORT_LED1_3, PIN_LED1);
ssp1_set_mode_max5864();
max5864_xcvr();
max5864_setup(&max5864);
max5864_xcvr(&max5864);
while (1) {

View File

@ -353,7 +353,6 @@ int main(void)
pin_setup();
enable_1v8_power();
cpu_clock_init();
ssp1_init();
gpio_set(PORT_LED1_3, PIN_LED1);
//test_sgpio_sliceA_D();

View File

@ -37,7 +37,6 @@ int main(void)
enable_rf_power();
#endif
cpu_clock_init();
ssp1_init();
gpio_set(PORT_LED1_3, (PIN_LED1)); /* LED1 on */