Skip to content

Commit

Permalink
Merge pull request #30 from tmytek/Update-TLKCore
Browse files Browse the repository at this point in the history
Support usrp_set_mode() in lib_usrp_spi
  • Loading branch information
alinyo98 authored Jul 31, 2023
2 parents 8bacbd9 + 39cbcab commit 02be12a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion example_Linux/C_C++/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This example directory contains two sub directories, please configure to your ow
* **include/tlkcore_lib.hpp** -> ../../lib_tlkcore_cpp/include/tlkcore_lib.hpp
* **libusrp_fbs.so** -> ../lib_usrp_spi/libusrp_fbs.so
* **include/usrp_fbs.hpp** -> ../../lib_usrp_spi/include/usrp_fbs.hpp
4. After libraries built, according to your Python environment, copy the extracted **lib/** from **TLKCore_release/** to **example/lib/**, and we already placed libs for Python3.6 as default.
4. After libraries built, according to your Python environment, copy the extracted **lib/** from **TLKCore_release/** to **example/lib/**, and we already placed libs for Python3.8 as default.

# Building TLKCore+USRP Applications using CMake
After above process, to try it out, run these commands:
Expand Down
17 changes: 15 additions & 2 deletions example_Linux/C_C++/examples/tlkcore_fbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ int update_beam_config(tlkcore_lib::tlkcore_ptr service)
service->get_fast_parallel_mode(sn, fpga_mode);
printf("[Main] get_fast_parallel_mode: %d\r\n", fpga_mode);

#if 0
/* A sample to set beam directly */
rf_mode_t mode = MODE_TX;
float gain_db = 4;
int theta = 0;
int phi = 0;
service->set_beam_angle(sn, 28.0, mode, gain_db, theta, phi);

#else
/* Set all beam configs via csv file */
service->apply_beam_patterns(sn, 28.0);
#endif
}
return 0;
}
Expand All @@ -49,8 +51,9 @@ int fpga_conftrol(tlkcore_lib::tlkcore_ptr service)
std::string usrp_addr = "";//"addr=192.168.100.10";
usrp_spi_setup(usrp_addr);

// Control UHD to switch beam id
char buf[64];
#if 1
// Case1: control UHD to switch beam id by typing beam id
int beam_id = 0;
do {
memset(buf, 0, sizeof(buf));
Expand All @@ -67,6 +70,16 @@ int fpga_conftrol(tlkcore_lib::tlkcore_ptr service)
continue;
}
} while (1);
#else
// Case2: setup batch beams, please DO NOT print any msg after running
printf("Please press enter to start:");
fgets(buf, sizeof(buf), stdin);
int beams[] = {1, 2, 3, 1, 4, 64};
int length = sizeof(beams)/sizeof(beams[0]);
for (int i=0; i<length; i++) {
usrp_select_beam_id(MODE_TX, beams[i]);
}
#endif

// Setup BBox back from fast parallel mode
fpga_mode = false;
Expand Down
4 changes: 2 additions & 2 deletions example_Linux/C_C++/lib_tlkcore_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ set(GCC_MIN_VERSION "6.3.0")
set(CLANG_MIN_VERSION "6.0.0")
# This Python version gets used for Python API (if requested) as well as
# all the build-time Python scripts
set(PYTHON_MIN_VERSION "3.6")
set(PYTHON_MIN_VERSION "3.8")
#set(PYTHON_VERSION 3.6)
set(PYBIND11_PYTHON_VERSION 3.6)
set(PYBIND11_PYTHON_VERSION 3.8)

cmake_minimum_required(VERSION 3.4...3.18)
set(CMAKE_CXX_STANDARD 11)
Expand Down
2 changes: 1 addition & 1 deletion example_Linux/C_C++/lib_tlkcore_cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ To try it out, run these commands:
`options` might be(modify to your Python version here):

-Dpybind11_DIR=~/.local/share/cmake/pybind11
-DPYBIND11_PYTHON_VERSION=3.6
-DPYBIND11_PYTHON_VERSION=3.8

See the CMakeLists.txt file to figure out how to set up a build system.
Binary file modified example_Linux/C_C++/lib_tlkcore_cpp/libtlkcore_lib.so
Binary file not shown.
1 change: 1 addition & 0 deletions example_Linux/C_C++/lib_usrp_spi/include/usrp_fbs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

int usrp_spi_setup(std::string addr);

void usrp_set_mode(int mode);
int usrp_select_beam_id(int mode, int id);

void usrp_free(void);
Binary file modified example_Linux/C_C++/lib_usrp_spi/libusrp_fbs.so
Binary file not shown.
57 changes: 40 additions & 17 deletions example_Linux/C_C++/lib_usrp_spi/usrp_fbs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Example for SPI testing for BBox of TMYTEK.
// Example for SPI control beam for BBox of TMYTEK.
//
// This example shows how to work with SPI (for TMYTEK BBox procotol)
// which is based on the GPIO interface of the X410.
Expand Down Expand Up @@ -56,6 +56,8 @@ uhd::spi_iface::sptr spi_ref;
// The spi_config_t holds items like the clock divider and the SDI and SDO edges
uhd::spi_config_t spi_config;

static const bool debug = false;
static int rf_mode = 0; //0:TX
std::string to_bit_string(uint32_t val, const size_t num_bits)
{
std::string out;
Expand Down Expand Up @@ -106,6 +108,22 @@ void output_reg_values(const std::string& bank,
}
}

/*
* Switch TX/RX mode via gpio operation for BBox of TMYTEK
*/
void usrp_set_mode(int mode)
{
uint32_t tx_pin = (mode == 0 ? GPIO_BIT(GPIO_DEFAULT_TX_EN_PIN) : 0);
uint32_t rx_pin = (mode == 0 ? 0 : GPIO_BIT(GPIO_DEFAULT_RX_EN_PIN));
if (debug) {
std::string msg = (mode == 0 ? "[USRP] Tx mode": "[USRP] Rx mode");
std::cout << msg << std::endl;
}
usrp->set_gpio_attr(gpio_bank, "OUT", tx_pin, GPIO_BIT(GPIO_DEFAULT_TX_EN_PIN));
usrp->set_gpio_attr(gpio_bank, "OUT", rx_pin, GPIO_BIT(GPIO_DEFAULT_RX_EN_PIN));
rf_mode = mode;
}

/*
* Setup SPI & gpio config for BBox of TMYTEK
*/
Expand Down Expand Up @@ -184,6 +202,9 @@ int usrp_spi_setup(std::string addr)
usrp->set_gpio_attr(gpio_bank, "OUT", GPIO_BIT(SPI_DEFAULT_SDI_PIN), GPIO_BIT(SPI_DEFAULT_SDI_PIN));
usrp->set_gpio_attr(gpio_bank, "OUT", GPIO_BIT(GPIO_DEFAULT_LDB_PIN), GPIO_BIT(GPIO_DEFAULT_LDB_PIN));

// Set TX as default
usrp_set_mode(0);

std::cout << "[USRP] Configured GPIO values:" << std::endl;
bool has_src_api = true;
output_reg_values(gpio_bank, port, usrp, num_bits, has_src_api);
Expand All @@ -208,38 +229,40 @@ int usrp_select_beam_id(int mode, int id)
}

// Switch Tx/Rx mode
if (mode == 0) {
std::cout << "[USRP] Tx mode" << std::endl;
usrp->set_gpio_attr(gpio_bank, "OUT", GPIO_BIT(GPIO_DEFAULT_TX_EN_PIN), GPIO_BIT(GPIO_DEFAULT_TX_EN_PIN));
usrp->set_gpio_attr(gpio_bank, "OUT", 0, GPIO_BIT(GPIO_DEFAULT_RX_EN_PIN));
} else {
std::cout << "[USRP] Rx mode" << std::endl;
usrp->set_gpio_attr(gpio_bank, "OUT", 0, GPIO_BIT(GPIO_DEFAULT_TX_EN_PIN));
usrp->set_gpio_attr(gpio_bank, "OUT", GPIO_BIT(GPIO_DEFAULT_RX_EN_PIN), GPIO_BIT(GPIO_DEFAULT_RX_EN_PIN));
if (rf_mode != mode) {
usrp_set_mode(mode);
}

/* Transform payload to the format of BF IC, and index starts from 0
/*
* Transform payload to the format of BF IC, and index starts from 0
* [Example]:
* 1. TX beam_id=1 => Gain:0x1600 & Phase:0x1e00
* 2. RX beam_id=20 => Gain:0x1513 & Phase:0x1c13
*/
uint32_t payload_id = ((id-1) & 0x3F);
if (debug) {
for (int i=0; i<2; i++) {
payload = (reg_address[mode][i] << 6) | payload_id;
std::cout << "[USRP] Writing payload: 0x" << std::hex << payload << " with length "
<< std::dec << payload_length << " bits" << std::endl;
}
}

for (int i=0; i<2; i++) {
payload = (reg_address[mode][i] << 6) | ((id-1) & 0x3F);
std::cout << "[USRP] Writing payload: 0x" << std::hex << payload << " with length "
<< std::dec << payload_length << " bits" << std::endl;
payload = (reg_address[mode][i] << 6) | payload_id;

// Do the SPI transaction. There are write() and read() methods available, too.
std::cout << "[USRP] Performing SPI transaction..." << std::endl;
// uint32_t read_data = spi_ref->transact_spi(0, spi_config, payload, payload_length, true);
// std::cout << "Data read: 0x" << std::hex << read_data << std::endl;
// std::cout << "[USRP] Performing SPI transaction..." << std::endl;
spi_ref->write_spi(0, spi_config, payload, payload_length);

// TMY LDB: low pulse
usrp->set_gpio_attr(gpio_bank, "OUT", 0, GPIO_BIT(GPIO_DEFAULT_LDB_PIN));
usrp->set_gpio_attr(gpio_bank, "OUT", GPIO_BIT(GPIO_DEFAULT_LDB_PIN), GPIO_BIT(GPIO_DEFAULT_LDB_PIN));
}

std::cout << "[USRP] BeamID:" << id << " selected" << std::endl;
if (debug) {
std::cout << "[USRP] BeamID:" << id << " selected" << std::endl;
}
return EXIT_SUCCESS;
}

Expand Down

0 comments on commit 02be12a

Please sign in to comment.