108 lines
2.6 KiB
C++
Raw Normal View History

2015-07-08 08:39:24 -07:00
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* 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 "rffc507x_spi.hpp"
#include "utility.hpp"
#include "hackrf_gpio.hpp"
using namespace hackrf::one;
namespace rffc507x {
namespace spi {
void SPI::init() {
gpio_rffc5072_select.set();
gpio_rffc5072_clock.clear();
2015-07-08 08:39:24 -07:00
gpio_rffc5072_select.output();
gpio_rffc5072_clock.output();
gpio_rffc5072_data.input();
2015-07-08 08:39:24 -07:00
gpio_rffc5072_data.clear();
2015-07-08 08:39:24 -07:00
}
inline void SPI::select(const bool active) {
gpio_rffc5072_select.write(!active);
2015-07-08 08:39:24 -07:00
}
inline void SPI::direction_out() {
gpio_rffc5072_data.output();
2015-07-08 08:39:24 -07:00
}
inline void SPI::direction_in() {
gpio_rffc5072_data.input();
2015-07-08 08:39:24 -07:00
}
inline void SPI::write_bit(const bit_t value) {
gpio_rffc5072_data.write(value);
2015-07-08 08:39:24 -07:00
}
inline bit_t SPI::read_bit() {
return gpio_rffc5072_data.read() & 1;
2015-07-08 08:39:24 -07:00
}
inline bit_t SPI::transfer_bit(const bit_t bit_out) {
gpio_rffc5072_clock.clear();
write_bit(bit_out);
const bit_t bit_in = read_bit();
gpio_rffc5072_clock.set();
return bit_in;
2015-07-08 08:39:24 -07:00
}
data_t SPI::transfer_bits(const data_t data_out, const size_t count) {
data_t data_in = 0;
for (size_t i = 0; i < count; i++) {
data_in = (data_in << 1) | transfer_bit((data_out >> (count - i - 1)) & 1);
}
return data_in;
2015-07-08 08:39:24 -07:00
}
data_t SPI::transfer_word(const Direction direction, const address_t address, const data_t data_out) {
select(true);
2015-07-08 08:39:24 -07:00
const data_t address_word =
((direction == Direction::Read) ? (1 << 7) : 0) | (address & 0x7f);
2015-07-08 08:39:24 -07:00
direction_out();
transfer_bits(address_word, 9);
2015-07-08 08:39:24 -07:00
if (direction == Direction::Read) {
direction_in();
transfer_bits(0, 2);
}
2015-07-08 08:39:24 -07:00
const data_t data_in = transfer_bits(data_out, 16);
2015-07-08 08:39:24 -07:00
if (direction == Direction::Write) {
direction_in();
}
2015-07-08 08:39:24 -07:00
select(false);
2015-07-08 08:39:24 -07:00
transfer_bits(0, 2);
2015-07-08 08:39:24 -07:00
return data_in;
2015-07-08 08:39:24 -07:00
}
} // namespace spi
} // namespace rffc507x