hdl: nios: clean up devices.c, wrap fns in platform ifdefs

This commit is contained in:
Rey Tucker 2018-11-27 14:33:05 -05:00 • committed by Rey Tucker
commit e3fdf7d7bb

View file

@ -24,13 +24,13 @@
#ifndef BLADERF_NIOS_PC_SIMULATION
#include "devices.h"
#include "debug.h"
#include "devices_inline.h"
#include "fpga_version.h"
#include <alt_types.h>
#include <stdbool.h>
#include <stdint.h>
#include <alt_types.h>
#include "devices.h"
#include "devices_inline.h"
#include "debug.h"
#include "fpga_version.h"
/* Define a global variable containing the current VCTCXO DAC setting.
* This is a 'cached' value of what is written to the DAC and is used
@ -42,40 +42,45 @@ uint16_t vctcxo_trim_dac_value = 0x7FFF;
/* Define a cached version of the VCTCXO tamer control register */
uint8_t vctcxo_tamer_ctrl_reg = 0x00;
#ifdef BOARD_BLADERF_MICRO
/* Cached version of ADF400x registers */
uint32_t adf400x_reg[4] = {0};
uint32_t adf400x_reg[4] = { 0 };
/* Define the fast lock profile storage arrays */
fastlock_profile fastlocks_rx[NUM_BBP_FASTLOCK_PROFILES];
fastlock_profile fastlocks_tx[NUM_BBP_FASTLOCK_PROFILES];
#endif // BOARD_BLADERF_MICRO
static void command_uart_enable_isr(bool enable) {
uint32_t val = enable ? 1 : 0 ;
IOWR_32DIRECT(COMMAND_UART_BASE, 16, val) ;
return ;
static void command_uart_enable_isr(bool enable)
{
uint32_t val = enable ? 1 : 0;
IOWR_32DIRECT(COMMAND_UART_BASE, 16, val);
return;
}
static void command_uart_isr(void *context) {
struct pkt_buf *pkt = (struct pkt_buf *)context ;
static void command_uart_isr(void *context)
{
struct pkt_buf *pkt = (struct pkt_buf *)context;
/* Reading the request should clear the interrupt */
command_uart_read_request((uint8_t *)pkt->req) ;
command_uart_read_request((uint8_t *)pkt->req);
/* Tell the main loop that there is a request pending */
pkt->ready = true ;
pkt->ready = true;
return ;
return;
}
static void vctcxo_tamer_isr(void *context) {
static void vctcxo_tamer_isr(void *context)
{
struct vctcxo_tamer_pkt_buf *pkt = (struct vctcxo_tamer_pkt_buf *)context;
uint8_t error_status = 0x00;
uint8_t error_status = 0x00;
/* Disable interrupts */
vctcxo_tamer_enable_isr( false );
vctcxo_tamer_enable_isr(false);
/* Reset (stop) the counters */
vctcxo_tamer_reset_counters( true );
vctcxo_tamer_reset_counters(true);
/* Read the current count values */
pkt->pps_1s_error = vctcxo_tamer_read_count(VT_ERR_1S_ADDR);
@ -86,8 +91,8 @@ static void vctcxo_tamer_isr(void *context) {
error_status = vctcxo_tamer_read(VT_STAT_ADDR);
/* Set the appropriate flags in the packet buffer */
pkt->pps_1s_error_flag = (error_status & VT_STAT_ERR_1S) ? true : false;
pkt->pps_10s_error_flag = (error_status & VT_STAT_ERR_10S) ? true : false;
pkt->pps_1s_error_flag = (error_status & VT_STAT_ERR_1S) ? true : false;
pkt->pps_10s_error_flag = (error_status & VT_STAT_ERR_10S) ? true : false;
pkt->pps_100s_error_flag = (error_status & VT_STAT_ERR_100S) ? true : false;
/* Clear interrupt */
@ -99,8 +104,9 @@ static void vctcxo_tamer_isr(void *context) {
return;
}
void vctcxo_tamer_enable_isr(bool enable) {
if( enable ) {
void vctcxo_tamer_enable_isr(bool enable)
{
if (enable) {
vctcxo_tamer_ctrl_reg |= VT_CTRL_IRQ_EN;
} else {
vctcxo_tamer_ctrl_reg &= ~VT_CTRL_IRQ_EN;
@ -110,13 +116,15 @@ void vctcxo_tamer_enable_isr(bool enable) {
return;
}
void vctcxo_tamer_clear_isr() {
void vctcxo_tamer_clear_isr()
{
vctcxo_tamer_write(VT_CTRL_ADDR, vctcxo_tamer_ctrl_reg | VT_CTRL_IRQ_CLR);
return;
}
void vctcxo_tamer_reset_counters(bool reset) {
if( reset ) {
void vctcxo_tamer_reset_counters(bool reset)
{
if (reset) {
vctcxo_tamer_ctrl_reg |= VT_CTRL_RESET;
} else {
vctcxo_tamer_ctrl_reg &= ~VT_CTRL_RESET;
@ -126,8 +134,8 @@ void vctcxo_tamer_reset_counters(bool reset) {
return;
}
void vctcxo_tamer_set_tune_mode(bladerf_vctcxo_tamer_mode mode) {
void vctcxo_tamer_set_tune_mode(bladerf_vctcxo_tamer_mode mode)
{
switch (mode) {
case BLADERF_VCTCXO_TAMER_DISABLED:
case BLADERF_VCTCXO_TAMER_1_PPS:
@ -142,15 +150,15 @@ void vctcxo_tamer_set_tune_mode(bladerf_vctcxo_tamer_mode mode) {
/* Set tuning mode */
vctcxo_tamer_ctrl_reg &= ~VT_CTRL_TUNE_MODE;
vctcxo_tamer_ctrl_reg |= (((uint8_t) mode) << 6);
vctcxo_tamer_ctrl_reg |= (((uint8_t)mode) << 6);
vctcxo_tamer_write(VT_CTRL_ADDR, vctcxo_tamer_ctrl_reg);
/* Reset the counters */
vctcxo_tamer_reset_counters( true );
vctcxo_tamer_reset_counters(true);
/* Take counters out of reset if tuning mode is not DISABLED */
if( mode != 0x00 ) {
vctcxo_tamer_reset_counters( false );
if (mode != 0x00) {
vctcxo_tamer_reset_counters(false);
}
switch (mode) {
@ -170,61 +178,66 @@ void vctcxo_tamer_set_tune_mode(bladerf_vctcxo_tamer_mode mode) {
bladerf_vctcxo_tamer_mode vctcxo_tamer_get_tune_mode()
{
uint8_t tmp = vctcxo_tamer_read(VT_CTRL_ADDR);
tmp = (tmp & VT_CTRL_TUNE_MODE) >> 6;
tmp = (tmp & VT_CTRL_TUNE_MODE) >> 6;
switch (tmp) {
case BLADERF_VCTCXO_TAMER_DISABLED:
case BLADERF_VCTCXO_TAMER_1_PPS:
case BLADERF_VCTCXO_TAMER_10_MHZ:
return (bladerf_vctcxo_tamer_mode) tmp;
return (bladerf_vctcxo_tamer_mode)tmp;
default:
return BLADERF_VCTCXO_TAMER_INVALID;
}
}
int32_t vctcxo_tamer_read_count(uint8_t addr) {
uint32_t base = VCTCXO_TAMER_0_BASE;
int32_t vctcxo_tamer_read_count(uint8_t addr)
{
uint32_t base = VCTCXO_TAMER_0_BASE;
uint8_t offset = addr;
int32_t value = 0;
int32_t value = 0;
value = IORD_8DIRECT(base, offset++);
value |= ((int32_t) IORD_8DIRECT(base, offset++)) << 8;
value |= ((int32_t) IORD_8DIRECT(base, offset++)) << 16;
value |= ((int32_t) IORD_8DIRECT(base, offset++)) << 24;
value = IORD_8DIRECT(base, offset++);
value |= ((int32_t)IORD_8DIRECT(base, offset++)) << 8;
value |= ((int32_t)IORD_8DIRECT(base, offset++)) << 16;
value |= ((int32_t)IORD_8DIRECT(base, offset++)) << 24;
return value;
}
uint8_t vctcxo_tamer_read(uint8_t addr) {
uint8_t vctcxo_tamer_read(uint8_t addr)
{
return (uint8_t)IORD_8DIRECT(VCTCXO_TAMER_0_BASE, addr);
}
void vctcxo_tamer_write(uint8_t addr, uint8_t data) {
void vctcxo_tamer_write(uint8_t addr, uint8_t data)
{
IOWR_8DIRECT(VCTCXO_TAMER_0_BASE, addr, data);
}
void tamer_schedule(bladerf_module m, uint64_t time) {
uint32_t base = (m == BLADERF_MODULE_RX) ? RX_TAMER_BASE : TX_TAMER_BASE ;
void tamer_schedule(bladerf_module m, uint64_t time)
{
uint32_t base = (m == BLADERF_MODULE_RX) ? RX_TAMER_BASE : TX_TAMER_BASE;
/* Set the holding time */
IOWR_8DIRECT(base, 0, (time>> 0)&0xff) ;
IOWR_8DIRECT(base, 1, (time>> 8)&0xff) ;
IOWR_8DIRECT(base, 2, (time>>16)&0xff) ;
IOWR_8DIRECT(base, 3, (time>>24)&0xff) ;
IOWR_8DIRECT(base, 4, (time>>32)&0xff) ;
IOWR_8DIRECT(base, 5, (time>>40)&0xff) ;
IOWR_8DIRECT(base, 6, (time>>48)&0xff) ;
IOWR_8DIRECT(base, 7, (time>>54)&0xff) ;
IOWR_8DIRECT(base, 0, (time >> 0) & 0xff);
IOWR_8DIRECT(base, 1, (time >> 8) & 0xff);
IOWR_8DIRECT(base, 2, (time >> 16) & 0xff);
IOWR_8DIRECT(base, 3, (time >> 24) & 0xff);
IOWR_8DIRECT(base, 4, (time >> 32) & 0xff);
IOWR_8DIRECT(base, 5, (time >> 40) & 0xff);
IOWR_8DIRECT(base, 6, (time >> 48) & 0xff);
IOWR_8DIRECT(base, 7, (time >> 54) & 0xff);
/* Commit it and arm the comparison */
IOWR_8DIRECT(base, 8, 0) ;
IOWR_8DIRECT(base, 8, 0);
return ;
return;
}
void bladerf_nios_init(struct pkt_buf *pkt, struct vctcxo_tamer_pkt_buf *vctcxo_tamer_pkt) {
void bladerf_nios_init(struct pkt_buf *pkt,
struct vctcxo_tamer_pkt_buf *vctcxo_tamer_pkt)
{
/* Set the prescaler for 400kHz with an 80MHz clock:
* (prescaler = clock / (5*desired) - 1)
*/
@ -244,41 +257,37 @@ void bladerf_nios_init(struct pkt_buf *pkt, struct vctcxo_tamer_pkt_buf *vctcxo_
IOWR_ALTERA_AVALON_PIO_DATA(RX_TRIGGER_CTL_BASE, 0x00);
/* Register Command UART ISR */
alt_ic_isr_register(
COMMAND_UART_IRQ_INTERRUPT_CONTROLLER_ID,
COMMAND_UART_IRQ,
command_uart_isr,
pkt,
NULL
) ;
alt_ic_isr_register(COMMAND_UART_IRQ_INTERRUPT_CONTROLLER_ID,
COMMAND_UART_IRQ, command_uart_isr, pkt, NULL);
/* Register the VCTCXO Tamer ISR */
alt_ic_isr_register(
VCTCXO_TAMER_0_IRQ_INTERRUPT_CONTROLLER_ID,
VCTCXO_TAMER_0_IRQ,
vctcxo_tamer_isr,
vctcxo_tamer_pkt,
NULL
);
alt_ic_isr_register(VCTCXO_TAMER_0_IRQ_INTERRUPT_CONTROLLER_ID,
VCTCXO_TAMER_0_IRQ, vctcxo_tamer_isr, vctcxo_tamer_pkt,
NULL);
/* Default VCTCXO Tamer and its interrupts to be disabled. */
vctcxo_tamer_set_tune_mode(BLADERF_VCTCXO_TAMER_DISABLED);
/* Enable interrupts */
command_uart_enable_isr(true) ;
alt_ic_irq_enable(COMMAND_UART_IRQ_INTERRUPT_CONTROLLER_ID, COMMAND_UART_IRQ);
alt_ic_irq_enable(VCTCXO_TAMER_0_IRQ_INTERRUPT_CONTROLLER_ID, VCTCXO_TAMER_0_IRQ);
command_uart_enable_isr(true);
alt_ic_irq_enable(COMMAND_UART_IRQ_INTERRUPT_CONTROLLER_ID,
COMMAND_UART_IRQ);
alt_ic_irq_enable(VCTCXO_TAMER_0_IRQ_INTERRUPT_CONTROLLER_ID,
VCTCXO_TAMER_0_IRQ);
}
static void i2c_complete_transfer(uint8_t check_rxack)
{
if ((IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_TIP) == 0) {
while ( (IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_TIP) == 0);
while ((IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_TIP) == 0)
;
}
while (IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_TIP);
while (IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_TIP)
;
while (check_rxack && (IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_RXACK));
while (check_rxack && (IORD_8DIRECT(I2C, OC_I2C_CMD_STATUS) & OC_I2C_RXACK))
;
}
void spi_arbiter_lock()
@ -291,8 +300,8 @@ void spi_arbiter_lock()
do {
data = IORD_8DIRECT(ARBITER_0_BASE, 1);
} while((data & 1) == 0);
#endif // ARBITER_0_BASE
} while ((data & 1) == 0);
#endif // ARBITER_0_BASE
}
void spi_arbiter_unlock()
@ -300,7 +309,7 @@ void spi_arbiter_unlock()
// Applies only to bladeRF1
#ifdef ARBITER_0_BASE
IOWR_32DIRECT(ARBITER_0_BASE, 0, 2);
#endif // ARBITER_0_BASE
#endif // ARBITER_0_BASE
}
uint8_t lms6_read(uint8_t addr)
@ -321,12 +330,13 @@ void lms6_write(uint8_t addr, uint8_t data)
spi_arbiter_unlock();
}
#ifdef BOARD_BLADERF_MICRO
uint64_t adi_spi_read(uint16_t addr)
{
alt_u8 addr8[2];
alt_u8 data8[8];
alt_u8 bytes;
uint8_t i;
alt_u8 addr8[2];
alt_u8 data8[8];
alt_u8 bytes;
uint8_t i;
uint64_t rv;
// The alt_avalon_spi_command expects parameters to be arrays of bytes
@ -343,18 +353,20 @@ uint64_t adi_spi_read(uint16_t addr)
// Build the uint64_t return value
rv = UINT64_C(0x0);
for( i = 0; i < 8; i++ ) {
rv |= (((uint64_t)data8[i]) << 8*(7-i));
for (i = 0; i < 8; i++) {
rv |= (((uint64_t)data8[i]) << 8 * (7 - i));
}
return rv;
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_spi_write(uint16_t addr, uint64_t data)
{
alt_u8 data8[10];
alt_u8 bytes;
uint8_t i;
alt_u8 data8[10];
alt_u8 bytes;
uint8_t i;
// The alt_avalon_spi_command expects parameters to be arrays of bytes
@ -363,8 +375,8 @@ void adi_spi_write(uint16_t addr, uint64_t data)
data8[1] = (addr & 0xff);
// Convert the uint64_t data into an array of 8 uint8_t
for( i = 0; i < 8; i++ ) {
data8[i+2] = (data >> 8*(7-i)) & 0xff;
for (i = 0; i < 8; i++) {
data8[i + 2] = (data >> 8 * (7 - i)) & 0xff;
}
// Calculate number of command and data bytes in this command
@ -373,32 +385,37 @@ void adi_spi_write(uint16_t addr, uint64_t data)
// Send down the command and the data
alt_avalon_spi_command(RFFE_SPI_BASE, 0, bytes, &data8[0], 0, 0, 0);
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
uint32_t adi_axi_read(uint16_t addr)
{
uint32_t data = 0;
#ifdef AXI_AD9361_0_BASE // Temporary hack for bladeRF1 compat
#ifdef AXI_AD9361_0_BASE // Temporary hack for bladeRF1 compat
data = IORD_32DIRECT(AXI_AD9361_0_BASE, addr);
#endif
return data;
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_axi_write(uint16_t addr, uint32_t data)
{
#ifdef AXI_AD9361_0_BASE // Temporary hack for bladeRF1 compat
#ifdef AXI_AD9361_0_BASE // Temporary hack for bladeRF1 compat
IOWR_32DIRECT(AXI_AD9361_0_BASE, addr, data);
#endif
}
#endif // BOARD_BLADERF_MICRO
void adi_fastlock_save(bool is_tx, uint8_t rffe_profile,
uint16_t nios_profile)
#ifdef BOARD_BLADERF_MICRO
void adi_fastlock_save(bool is_tx, uint8_t rffe_profile, uint16_t nios_profile)
{
uint16_t fl_prog_addr_reg;
uint16_t fl_prog_rddata_reg;
uint16_t addr;
uint64_t data;
uint32_t i;
fastlock_profile* fastlocks;
fastlock_profile *fastlocks;
if (is_tx) {
fl_prog_addr_reg = 0x29c;
@ -411,28 +428,29 @@ void adi_fastlock_save(bool is_tx, uint8_t rffe_profile,
}
/* Read out the profile data and save to Nios memory */
for( i = 0; i < 16; i++ ) {
for (i = 0; i < 16; i++) {
addr = (0x1 << 15) | (0x0 << 12) | (fl_prog_addr_reg & 0x3ff);
data = (uint64_t)(((rffe_profile & 0x7) << 4) | (i & 0xf)) << 8*7;
adi_spi_write( addr, data );
data = (uint64_t)(((rffe_profile & 0x7) << 4) | (i & 0xf)) << 8 * 7;
adi_spi_write(addr, data);
addr = (0x0 << 15) | (0x0 << 12) | (fl_prog_rddata_reg & 0x3ff);
fastlocks[nios_profile].profile_data[i] = adi_spi_read(addr) >> 56;
}
/* Kick out any other profile stored in the Nios that was in this slot */
for( i = 0; i < NUM_BBP_FASTLOCK_PROFILES; i++ ) {
if( (fastlocks[i].profile_num == rffe_profile) &&
(fastlocks[i].state == FASTLOCK_STATE_BBP_RFFE) ) {
for (i = 0; i < NUM_BBP_FASTLOCK_PROFILES; i++) {
if ((fastlocks[i].profile_num == rffe_profile) &&
(fastlocks[i].state == FASTLOCK_STATE_BBP_RFFE)) {
fastlocks[i].state = FASTLOCK_STATE_BBP;
}
}
/* Update profile state */
fastlocks[nios_profile].state = FASTLOCK_STATE_BBP_RFFE;
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_fastlock_load(bladerf_module m, fastlock_profile *p)
{
static const uint8_t fl_prog_write = 1 << 1;
@ -443,7 +461,7 @@ void adi_fastlock_load(bladerf_module m, fastlock_profile *p)
uint32_t i;
uint16_t addr;
uint64_t data;
fastlock_profile* fastlocks;
fastlock_profile *fastlocks;
if (BLADERF_CHANNEL_IS_TX(m)) {
fl_prog_data_reg = 0x29d;
@ -461,7 +479,7 @@ void adi_fastlock_load(bladerf_module m, fastlock_profile *p)
return;
} else {
/* Kick out any other loaded profile that's in this slot */
for( i = 0; i < NUM_BBP_FASTLOCK_PROFILES; i++ ) {
for (i = 0; i < NUM_BBP_FASTLOCK_PROFILES; i++) {
if ((fastlocks[i].profile_num == p->profile_num) &&
(fastlocks[i].state == FASTLOCK_STATE_BBP_RFFE)) {
fastlocks[i].state = FASTLOCK_STATE_BBP;
@ -469,59 +487,63 @@ void adi_fastlock_load(bladerf_module m, fastlock_profile *p)
}
/* Write 2 bytes to fast lock program data register */
addr = (0x1 << 15) | (0x1 << 12) | (fl_prog_data_reg & 0x3ff);
data = (uint64_t)(p->profile_data[0]) << 8*7;
data |= (uint64_t)(((p->profile_num & 0x7) << 4) | (i & 0xf)) << 8*6;
adi_spi_write( addr, data );
addr = (0x1 << 15) | (0x1 << 12) | (fl_prog_data_reg & 0x3ff);
data = (uint64_t)(p->profile_data[0]) << 8 * 7;
data |= (uint64_t)(((p->profile_num & 0x7) << 4) | (i & 0xf)) << 8 * 6;
adi_spi_write(addr, data);
for( i = 1; i < fl_prog_bytes; i++ ) {
for (i = 1; i < fl_prog_bytes; i++) {
/* Write 4 bytes to fast lock program control register */
addr = (0x1 << 15) | (0x3 << 12) | (fl_prog_ctrl_reg & 0x3ff);
data = (uint64_t)(fl_prog_write | fl_prog_clken) << 8*7;
data |= UINT64_C(0x0) << 8*6;
data |= (uint64_t)(p->profile_data[i]) << 8*5;
addr = (0x1 << 15) | (0x3 << 12) | (fl_prog_ctrl_reg & 0x3ff);
data = (uint64_t)(fl_prog_write | fl_prog_clken) << 8 * 7;
data |= UINT64_C(0x0) << 8 * 6;
data |= (uint64_t)(p->profile_data[i]) << 8 * 5;
data |= ((uint64_t)(((p->profile_num & 0x7) << 4) | (i & 0xf))
<< 8*4);
adi_spi_write( addr, data );
<< 8 * 4);
adi_spi_write(addr, data);
}
/* Write 1 byte to fast lock program control register */
addr = (0x1 << 15) | (0x0 << 12) | (fl_prog_ctrl_reg & 0x3ff);
data = (uint64_t)(fl_prog_write | fl_prog_clken) << 8*7;
adi_spi_write( addr, data );
addr = (0x1 << 15) | (0x0 << 12) | (fl_prog_ctrl_reg & 0x3ff);
data = (uint64_t)(fl_prog_write | fl_prog_clken) << 8 * 7;
adi_spi_write(addr, data);
/* Write 1 byte to fast lock program control register */
addr = (0x1 << 15) | (0x0 << 12) | (fl_prog_ctrl_reg & 0x3ff);
adi_spi_write( addr, 0 );
addr = (0x1 << 15) | (0x0 << 12) | (fl_prog_ctrl_reg & 0x3ff);
adi_spi_write(addr, 0);
/* Update profile state */
p->state = FASTLOCK_STATE_BBP_RFFE;
}
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_fastlock_recall(bladerf_module m, fastlock_profile *p)
{
uint16_t fl_setup_reg = BLADERF_CHANNEL_IS_TX(m) ? 0x29a : 0x25a;
uint16_t addr;
uint64_t data = 0;
addr = (0x1 << 15) | (0x0 << 12) | (fl_setup_reg & 0x3ff);
data = (uint64_t)(((p->profile_num & 0x7) << 5) | (0x1)) << 8*7;
addr = (0x1 << 15) | (0x0 << 12) | (fl_setup_reg & 0x3ff);
data = (uint64_t)(((p->profile_num & 0x7) << 5) | (0x1)) << 8 * 7;
adi_spi_write( addr, data );
adi_spi_write(addr, data);
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_rfport_select(fastlock_profile *p)
{
static const uint16_t input_sel_reg = 0x4;
static const uint8_t rx_port_mask = 0x3f;
static const uint8_t tx_port_mask = 0x40;
static const uint8_t rx_port_mask = 0x3f;
static const uint8_t tx_port_mask = 0x40;
uint16_t addr;
uint64_t data;
/* Get current port selection */
addr = (0x0 << 15) | (0x0 << 12) | (input_sel_reg & 0x3ff);
data = adi_spi_read(addr) >> 56;
addr = (0x0 << 15) | (0x0 << 12) | (input_sel_reg & 0x3ff);
data = adi_spi_read(addr) >> 56;
if (p->port >> 7) {
/* RX bit is set, only modify RX port selection */
@ -532,11 +554,13 @@ void adi_rfport_select(fastlock_profile *p)
}
/* Write the new port selection to AD9361 */
addr = (0x1 << 15) | (0x0 << 12) | (input_sel_reg & 0x3ff);
data = data << 56;
adi_spi_write( addr, data );
addr = (0x1 << 15) | (0x0 << 12) | (input_sel_reg & 0x3ff);
data = data << 56;
adi_spi_write(addr, data);
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adi_rfspdt_select(bladerf_module m, fastlock_profile *p)
{
static const uint32_t tx_spdt_mask = 0x7800;
@ -554,6 +578,7 @@ void adi_rfspdt_select(bladerf_module m, fastlock_profile *p)
rffe_csr_write(rffe_gpio);
}
#endif // BOARD_BLADERF_MICRO
uint8_t si5338_read(uint8_t addr)
{
@ -566,12 +591,12 @@ uint8_t si5338_read(uint8_t addr)
IOWR_8DIRECT(I2C, OC_I2C_DATA, addr);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_WR | OC_I2C_STO);
i2c_complete_transfer(1) ;
i2c_complete_transfer(1);
/* Next transfer is a read operation, so '1' in the read/write bit */
IOWR_8DIRECT(I2C, OC_I2C_DATA, SI5338_I2C | 1);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_STA | OC_I2C_WR);
i2c_complete_transfer(1) ;
i2c_complete_transfer(1);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_RD | OC_I2C_NACK | OC_I2C_STO);
i2c_complete_transfer(0);
@ -583,7 +608,7 @@ uint8_t si5338_read(uint8_t addr)
void si5338_write(uint8_t addr, uint8_t data)
{
/* Set the address to the Si5338 */
IOWR_8DIRECT(I2C, OC_I2C_DATA, SI5338_I2C) ;
IOWR_8DIRECT(I2C, OC_I2C_DATA, SI5338_I2C);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_STA | OC_I2C_WR);
i2c_complete_transfer(1);
@ -596,6 +621,7 @@ void si5338_write(uint8_t addr, uint8_t data)
i2c_complete_transfer(0);
}
#ifdef BOARD_BLADERF_MICRO
uint16_t ina219_read(uint8_t addr)
{
uint16_t data;
@ -607,14 +633,14 @@ uint16_t ina219_read(uint8_t addr)
IOWR_8DIRECT(I2C, OC_I2C_DATA, addr);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_WR | OC_I2C_STO);
i2c_complete_transfer(1) ;
i2c_complete_transfer(1);
/* Next transfer is a read operation, so '1' in the read/write bit */
IOWR_8DIRECT(I2C, OC_I2C_DATA, INA219_I2C | 1);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_STA | OC_I2C_WR);
i2c_complete_transfer(1) ;
i2c_complete_transfer(1);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_RD );
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_RD);
i2c_complete_transfer(1);
data = IORD_8DIRECT(I2C, OC_I2C_DATA) << 8;
@ -624,11 +650,13 @@ uint16_t ina219_read(uint8_t addr)
data |= IORD_8DIRECT(I2C, OC_I2C_DATA);
return data;
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void ina219_write(uint8_t addr, uint16_t data)
{
/* Set the address to the INA219 */
IOWR_8DIRECT(I2C, OC_I2C_DATA, INA219_I2C) ;
IOWR_8DIRECT(I2C, OC_I2C_DATA, INA219_I2C);
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_STA | OC_I2C_WR);
i2c_complete_transfer(1);
@ -644,6 +672,7 @@ void ina219_write(uint8_t addr, uint16_t data)
IOWR_8DIRECT(I2C, OC_I2C_CMD_STATUS, OC_I2C_WR | OC_I2C_STO);
i2c_complete_transfer(0);
}
#endif // BOARD_BLADERF_MICRO
void vctcxo_trim_dac_write(uint8_t cmd, uint16_t val)
{
@ -656,19 +685,21 @@ void vctcxo_trim_dac_write(uint8_t cmd, uint16_t val)
/* Update cached value of trim DAC setting */
vctcxo_trim_dac_value = val;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 3, data, 0, 0, 0) ;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 3, data, 0, 0, 0);
}
void vctcxo_trim_dac_read(uint8_t cmd, uint16_t *val)
{
alt_u8 data[2];
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 1, &cmd, 0, 0, ALT_AVALON_SPI_COMMAND_MERGE);
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 1, &cmd, 0, 0,
ALT_AVALON_SPI_COMMAND_MERGE);
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 0, 0, 2, &data[0], 0);
*val = ((uint8_t)data[0] << 8) | (uint8_t)data[1];
}
#ifdef BOARD_BLADERF_MICRO
void ad56x1_vctcxo_trim_dac_write(uint16_t val)
{
uint8_t data[2] = {
@ -679,30 +710,33 @@ void ad56x1_vctcxo_trim_dac_write(uint16_t val)
/* Update cached value of trim DAC setting */
vctcxo_trim_dac_value = val;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 2, data, 0, 0, 0) ;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 0, 2, data, 0, 0, 0);
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void ad56x1_vctcxo_trim_dac_read(uint16_t *val)
{
/* The AD56x1 DAC does not have readback functionality.
* Return the cached DAC value */
*val = vctcxo_trim_dac_value;
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
void adf400x_spi_write(uint32_t val)
{
uint8_t data[3] = {
(val >> 16) & 0xff,
(val >> 8) & 0xff,
(val >> 0) & 0xff
};
uint8_t data[3] = { (val >> 16) & 0xff, (val >> 8) & 0xff,
(val >> 0) & 0xff };
/* Update cached value of ADF400x setting */
adf400x_reg[val & 0x3] = val;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 3, data, 0, 0, 0) ;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 3, data, 0, 0, 0);
}
#endif // BOARD_BLADERF_MICRO
#ifdef BOARD_BLADERF_MICRO
uint32_t adf400x_spi_read(uint8_t addr)
{
/* This assumes the AD400x MUXOUT is set to SDO/MISO,
@ -715,11 +749,13 @@ uint32_t adf400x_spi_read(uint8_t addr)
}; */
/* Perform the SPI operation so we can take a look on SignalTap */
//alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 3, sdi_data, 3, sdo_data, 0);
// alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 3, sdi_data, 3, sdo_data,
// 0);
/* Just return the cached value */
return adf400x_reg[addr & 0x3];
}
#endif // BOARD_BLADERF_MICRO
void adf4351_write(uint32_t val)
{
@ -731,15 +767,16 @@ void adf4351_write(uint32_t val)
uint8_t t;
sval.val = val;
t = sval.byte[0];
t = sval.byte[0];
sval.byte[0] = sval.byte[3];
sval.byte[3] = t;
t = sval.byte[1];
t = sval.byte[1];
sval.byte[1] = sval.byte[2];
sval.byte[2] = t;
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 4, (uint8_t*)&sval.val, 0, 0, 0);
alt_avalon_spi_command(PERIPHERAL_SPI_BASE, 1, 4, (uint8_t *)&sval.val, 0,
0, 0);
}
// Temporary for bladeRF2 compat
@ -756,15 +793,15 @@ static inline uint32_t module_to_iqcorr_base(bladerf_module m)
uint16_t iqbal_get_gain(bladerf_module m)
{
const uint32_t base = module_to_iqcorr_base(m);
const uint32_t base = module_to_iqcorr_base(m);
const uint32_t regval = IORD_ALTERA_AVALON_PIO_DATA(base);
return (uint16_t) regval;
return (uint16_t)regval;
}
void iqbal_set_gain(bladerf_module m, uint16_t value)
{
const uint32_t base = module_to_iqcorr_base(m);
uint32_t regval = IORD_ALTERA_AVALON_PIO_DATA(base);
uint32_t regval = IORD_ALTERA_AVALON_PIO_DATA(base);
regval &= 0xffff0000;
regval |= value;
@ -782,46 +819,52 @@ uint16_t iqbal_get_phase(bladerf_module m)
regval = IORD_ALTERA_AVALON_PIO_DATA(IQ_CORR_TX_PHASE_GAIN_BASE);
}
return (uint16_t) (regval >> 16);
return (uint16_t)(regval >> 16);
}
void iqbal_set_phase(bladerf_module m, uint16_t value)
{
const uint32_t base = module_to_iqcorr_base(m);
uint32_t regval = IORD_ALTERA_AVALON_PIO_DATA(base);
uint32_t regval = IORD_ALTERA_AVALON_PIO_DATA(base);
regval &= 0x0000ffff;
regval |= ((uint32_t) value) << 16;
regval |= ((uint32_t)value) << 16;
IOWR_ALTERA_AVALON_PIO_DATA(base, regval);
}
#endif
#else
uint16_t iqbal_get_gain(bladerf_module m) { return 0; }
void iqbal_set_gain(bladerf_module m, uint16_t value) { }
uint16_t iqbal_get_phase(bladerf_module m) { return 0; }
void iqbal_set_phase(bladerf_module m, uint16_t value) { }
uint16_t iqbal_get_gain(bladerf_module m)
{
return 0;
}
void iqbal_set_gain(bladerf_module m, uint16_t value) {}
uint16_t iqbal_get_phase(bladerf_module m)
{
return 0;
}
void iqbal_set_phase(bladerf_module m, uint16_t value) {}
#endif
void tx_trigger_ctl_write(uint8_t data)
{
IOWR_ALTERA_AVALON_PIO_DATA(TX_TRIGGER_CTL_BASE, data);
IOWR_ALTERA_AVALON_PIO_DATA(TX_TRIGGER_CTL_BASE, data);
}
uint8_t tx_trigger_ctl_read(void)
{
return IORD_ALTERA_AVALON_PIO_DATA(TX_TRIGGER_CTL_BASE);
return IORD_ALTERA_AVALON_PIO_DATA(TX_TRIGGER_CTL_BASE);
}
void rx_trigger_ctl_write(uint8_t data)
{
IOWR_ALTERA_AVALON_PIO_DATA(RX_TRIGGER_CTL_BASE, data);
IOWR_ALTERA_AVALON_PIO_DATA(RX_TRIGGER_CTL_BASE, data);
}
uint8_t rx_trigger_ctl_read(void)
{
return IORD_ALTERA_AVALON_PIO_DATA(RX_TRIGGER_CTL_BASE);
return IORD_ALTERA_AVALON_PIO_DATA(RX_TRIGGER_CTL_BASE);
}
void agc_dc_corr_write(uint16_t addr, uint16_t value)
@ -840,23 +883,23 @@ void agc_dc_corr_write(uint16_t addr, uint16_t value)
IOWR_ALTERA_AVALON_PIO_DATA(AGC_DC_Q_MIN_BASE, value);
else if (addr == NIOS_PKT_8x16_ADDR_AGC_DC_I_MIN)
IOWR_ALTERA_AVALON_PIO_DATA(AGC_DC_I_MIN_BASE, value);
#endif // AGC_DC_I_MIN_BASE
#endif // AGC_DC_I_MIN_BASE
}
uint64_t time_tamer_read(bladerf_module m)
{
uint32_t base = (m == BLADERF_MODULE_RX) ? RX_TAMER_BASE : TX_TAMER_BASE ;
uint32_t base = (m == BLADERF_MODULE_RX) ? RX_TAMER_BASE : TX_TAMER_BASE;
uint8_t offset = 0;
uint64_t value = 0;
value = IORD_8DIRECT(base, offset++);
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 8;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 16;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 24;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 32;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 40;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 48;
value |= ((uint64_t) IORD_8DIRECT(base, offset++)) << 56;
value = IORD_8DIRECT(base, offset++);
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 8;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 16;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 24;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 32;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 40;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 48;
value |= ((uint64_t)IORD_8DIRECT(base, offset++)) << 56;
return value;
}