From 2d517d5bf8faa6f2d93b83d4b1054a460e32b9ab Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sat, 22 May 2021 12:11:30 +0200 Subject: [PATCH] New radio driver for GDx. Renamed some entries in GDx calibration data structure --- meson.build | 9 +- openrtx/include/calibration/calibInfo_GDx.h | 10 +- platform/drivers/NVM/nvmem_GDx.c | 32 +- platform/drivers/baseband/AT1846S.h | 15 +- platform/drivers/baseband/radio_GDx.cpp | 335 ++++++++++++++++++++ 5 files changed, 368 insertions(+), 33 deletions(-) create mode 100644 platform/drivers/baseband/radio_GDx.cpp diff --git a/meson.build b/meson.build index cff39171..5c519692 100644 --- a/meson.build +++ b/meson.build @@ -107,11 +107,10 @@ gdx_src = ['platform/drivers/NVM/W25Qx.c', 'platform/drivers/NVM/nvmem_GDx.c', 'platform/drivers/ADC/ADC0_GDx.c', 'platform/drivers/backlight/backlight_GDx.c', - 'platform/drivers/baseband/radio_GDx.c', -# 'platform/drivers/baseband/AT1846S_wrapper.cpp', -# 'platform/drivers/baseband/AT1846S_GDx.cpp', -# 'platform/drivers/baseband/HR_C6000.c', -# 'platform/drivers/baseband/interfaces_GDx.c', + 'platform/drivers/baseband/radio_GDx.cpp', + 'platform/drivers/baseband/HR_Cx000.cpp', + 'platform/drivers/baseband/AT1846S_GDx.cpp', + 'platform/drivers/baseband/HR_C6000_GDx.cpp', 'platform/drivers/display/UC1701_GDx.c', 'platform/drivers/keyboard/keyboard_GDx.c', 'platform/drivers/audio/audio_GDx.c'] diff --git a/openrtx/include/calibration/calibInfo_GDx.h b/openrtx/include/calibration/calibInfo_GDx.h index ddfd2f35..ed7e0839 100644 --- a/openrtx/include/calibration/calibInfo_GDx.h +++ b/openrtx/include/calibration/calibInfo_GDx.h @@ -33,7 +33,7 @@ typedef struct uint16_t _DigitalTxGainNarrowband; // 0x002 IF Gain, TX Fine uint16_t _DigitalRxGainWideband; // 0x004 IF Gain, RX Coarse uint16_t _DigitalTxGainWideband; // 0x006 IF Gain, TX Coarse - uint16_t mod1Bias; // 0x008 + uint16_t modBias; // 0x008 uint8_t mod2Offset; // 0x00A uint8_t txLowPower[16]; // 0x00B - 0x02A uint8_t txHighPower[16]; // 0x00B - 0x02A @@ -58,7 +58,7 @@ typedef struct uint8_t mod1Amplitude[8]; // 0x055 - uint8_t dacDataRange; // 0x05D + uint8_t digAudioGain; // 0x05D uint8_t txDev_DTMF; // 0x05E uint8_t txDev_tone; // 0x05F @@ -74,8 +74,8 @@ typedef struct uint16_t mixGainWideband; // 0x068 uint16_t mixGainNarrowband; // 0x06A - uint8_t rxAudioGainWideband; // 0x06C - uint8_t rxAudioGainNarrowband; // 0x06D + uint8_t rxDacGain; // 0x06C + uint8_t rxVoiceGain; // 0x06D } bandCalData_t; @@ -84,7 +84,7 @@ typedef struct bandCalData_t data[2]; // Calibration data for VHF (index 0) and UHF (index 1) bands freq_t vhfCalPoints[8]; // VHF calibration points for both TX power and mod1Amplitude freq_t uhfPwrCalPoints[16]; // UHF calibration points for TX power - freq_t uhfMod1CalPoints[8]; // UHF calibration points for mod1Amplitude + freq_t uhfCalPoints[8]; // UHF calibration points for mod1Amplitude } gdxCalibration_t; diff --git a/platform/drivers/NVM/nvmem_GDx.c b/platform/drivers/NVM/nvmem_GDx.c index 4cc1718a..e919ef21 100644 --- a/platform/drivers/NVM/nvmem_GDx.c +++ b/platform/drivers/NVM/nvmem_GDx.c @@ -69,7 +69,7 @@ uint32_t _bcd2bin(uint32_t bcd) */ void _loadBandCalData(uint32_t baseAddr, bandCalData_t *cal) { - W25Qx_readData(baseAddr + 0x08, &(cal->mod1Bias), 2); + W25Qx_readData(baseAddr + 0x08, &(cal->modBias), 2); W25Qx_readData(baseAddr + 0x0A, &(cal->mod2Offset), 1); W25Qx_readData(baseAddr + 0x3F, cal->analogSqlThresh, 8); W25Qx_readData(baseAddr + 0x47, &(cal->noise1_HighTsh_Wb), 1); @@ -87,7 +87,7 @@ void _loadBandCalData(uint32_t baseAddr, bandCalData_t *cal) W25Qx_readData(baseAddr + 0x53, &(cal->RSSILowerThreshold), 1); W25Qx_readData(baseAddr + 0x54, &(cal->RSSIUpperThreshold), 1); W25Qx_readData(baseAddr + 0x55, cal->mod1Amplitude, 8); - W25Qx_readData(baseAddr + 0x5D, &(cal->dacDataRange), 1); + W25Qx_readData(baseAddr + 0x5D, &(cal->digAudioGain), 1); W25Qx_readData(baseAddr + 0x5E, &(cal->txDev_DTMF), 1); W25Qx_readData(baseAddr + 0x5F, &(cal->txDev_tone), 1); W25Qx_readData(baseAddr + 0x60, &(cal->txDev_CTCSS_wb), 1); @@ -100,8 +100,8 @@ void _loadBandCalData(uint32_t baseAddr, bandCalData_t *cal) W25Qx_readData(baseAddr + 0x67, &(cal->rxAGCgain), 1); W25Qx_readData(baseAddr + 0x68, &(cal->mixGainWideband), 2); W25Qx_readData(baseAddr + 0x6A, &(cal->mixGainNarrowband), 2); - W25Qx_readData(baseAddr + 0x6C, &(cal->rxAudioGainWideband), 1); - W25Qx_readData(baseAddr + 0x6D, &(cal->rxAudioGainNarrowband), 1); + W25Qx_readData(baseAddr + 0x6C, &(cal->rxDacGain), 1); + W25Qx_readData(baseAddr + 0x6D, &(cal->rxVoiceGain), 1); uint8_t txPwr[32] = {0}; W25Qx_readData(baseAddr + 0x0B, txPwr, 32); @@ -161,7 +161,7 @@ void nvm_readCalibData(void *buf) for(uint8_t i = 0; i < 16; i++) { uint8_t ii = i/2; - calib->uhfMod1CalPoints[ii] = 405000000 + (5000000 * ii); + calib->uhfCalPoints[ii] = 405000000 + (5000000 * ii); calib->uhfPwrCalPoints[i] = 400000000 + (5000000 * i); } @@ -183,9 +183,9 @@ void nvm_loadHwInfo(hwInfo_t *info) int nvm_readVFOChannelData(channel_t *channel) { gdxChannel_t chData; - + AT24Cx_readData(vfoChannelBaseAddr, ((uint8_t *) &chData), sizeof(gdxChannel_t)); - + // Copy data to OpenRTX channel_t channel->mode = chData.channel_mode; channel->bandwidth = chData.bandwidth; @@ -256,7 +256,7 @@ int nvm_readChannelData(channel_t *channel, uint16_t pos) { if((pos <= 0) || (pos > maxNumChannels)) return -1; - + // Channels are organized in 128-channel banks uint8_t bank_num = (pos - 1) / 128; // Note: pos is 1-based because an empty slot in a zone contains index 0 @@ -374,15 +374,15 @@ int nvm_readChannelData(channel_t *channel, uint16_t pos) int nvm_readZoneData(zone_t *zone, uint16_t pos) { - if((pos <= 0) || (pos > maxNumZones)) return -1; - + if((pos <= 0) || (pos > maxNumZones)) return -1; + // zone number is 1-based to be consistent with channels // Convert to 0-based index to fetch data from flash uint16_t index = pos - 1; // ### Read zone bank bitmap ### uint8_t bitmap[32]; AT24Cx_readData(zoneBaseAddr, ((uint8_t *) &bitmap), sizeof(bitmap)); - + uint8_t bitmap_byte = index / 8; uint8_t bitmap_bit = index % 8; // The zone is marked not valid in the bitmap @@ -393,16 +393,16 @@ int nvm_readZoneData(zone_t *zone, uint16_t pos) AT24Cx_readData(zoneAddr, ((uint8_t *) &zoneData), sizeof(gdxZone_t)); // Check if zone is empty - if(wcslen((wchar_t *) zoneData.name) == 0) return -1; - + if(wcslen((wchar_t *) zoneData.name) == 0) return -1; + memcpy(zone->name, zoneData.name, sizeof(zoneData.name)); // Terminate string with 0x00 instead of 0xFF _addStringTerminator(zone->name, sizeof(zoneData.name)); // Copy zone channel indexes for(uint16_t i = 0; i < 16; i++) - { + { zone->member[i] = zoneData.member[i]; - } + } return 0; } @@ -421,7 +421,7 @@ int nvm_readContactData(contact_t *contact, uint16_t pos) // Check if contact is empty if(wcslen((wchar_t *) contactData.name) == 0) return -1; - + // Copy contact name memcpy(contact->name, contactData.name, sizeof(contactData.name)); // Terminate string with 0x00 instead of 0xFF diff --git a/platform/drivers/baseband/AT1846S.h b/platform/drivers/baseband/AT1846S.h index 782c8ab6..ae374b70 100644 --- a/platform/drivers/baseband/AT1846S.h +++ b/platform/drivers/baseband/AT1846S.h @@ -185,7 +185,7 @@ public: */ inline void setMicGain(const uint8_t gain) { - maskSetRegister(0x41, 0x007F, ((uint16_t) gain)); + maskSetRegister(0x41, 0x007F, static_cast< uint16_t >(gain)); } /** @@ -210,14 +210,15 @@ public: /** * Set audio gain for recepion. - * @param gainWb: gain for wideband Rx (25kHz). - * @param gainNb: gain for narrowband Rx (12.5kHz). + * @param analogDacGain: "analog DAC gain" in AT1846S manual. + * @param digitalGain: "digital voice gain" in AT1846S manual. */ - inline void setRxAudioGain(const uint8_t gainWb, const uint8_t gainNb) + inline void setRxAudioGain(const uint8_t analogDacGain, + const uint8_t digitalGain) { - uint16_t value = (gainWb & 0x0F) << 4; + uint16_t value = (analogDacGain & 0x0F) << 4; maskSetRegister(0x44, 0x00F0, value); - maskSetRegister(0x44, 0x000F, ((uint16_t) gainNb)); + maskSetRegister(0x44, 0x000F, static_cast< uint16_t >(digitalGain)); } /** @@ -269,7 +270,7 @@ public: */ inline void setAnalogSqlThresh(const uint8_t thresh) { - i2c_writeReg16(0x49, ((uint16_t) thresh)); + i2c_writeReg16(0x49, static_cast< uint16_t >(thresh)); } private: diff --git a/platform/drivers/baseband/radio_GDx.cpp b/platform/drivers/baseband/radio_GDx.cpp new file mode 100644 index 00000000..eed9b7d3 --- /dev/null +++ b/platform/drivers/baseband/radio_GDx.cpp @@ -0,0 +1,335 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 3 of the License, 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; if not, see * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include "radioUtils.h" +#include "HR_C6000.h" +#include "AT1846S.h" + +const gdxCalibration_t *calData; // Pointer to calibration data +const rtxStatus_t *config; // Pointer to data structure with radio configuration + +Band currRxBand = BND_NONE; // Current band for RX +Band currTxBand = BND_NONE; // Current band for TX +uint16_t apcVoltage = 0; // APC voltage for TX output power control + +enum opstatus radioStatus; // Current operating status + +HR_C6000& C6000 = HR_C6000::instance(); // HR_C5000 driver +AT1846S& at1846s = AT1846S::instance(); // AT1846S driver + +void radio_init(const rtxStatus_t *rtxState) +{ + /* + * Load calibration data + */ + calData = reinterpret_cast< const gdxCalibration_t * >(platform_getCalibrationData()); + + config = rtxState; + radioStatus = OFF; + + /* + * Configure RTX GPIOs + */ + gpio_setMode(VHF_LNA_EN, OUTPUT); + gpio_setMode(UHF_LNA_EN, OUTPUT); + gpio_setMode(VHF_PA_EN, OUTPUT); + gpio_setMode(UHF_PA_EN, OUTPUT); + gpio_setMode(RX_AUDIO_MUX, OUTPUT); + gpio_setMode(TX_AUDIO_MUX, OUTPUT); + + + gpio_clearPin(VHF_LNA_EN); // Turn VHF LNA off + gpio_clearPin(UHF_LNA_EN); // Turn UHF LNA off + gpio_clearPin(VHF_PA_EN); // Turn VHF PA off + gpio_clearPin(UHF_PA_EN); // Turn UHF PA off + gpio_clearPin(RX_AUDIO_MUX); // Audio out to HR_C6000 + gpio_clearPin(TX_AUDIO_MUX); // Audio in to microphone + + /* + * Enable and configure DAC for PA drive control + */ + SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK; + DAC0->DAT[0].DATL = 0; + DAC0->DAT[0].DATH = 0; + DAC0->C0 |= DAC_C0_DACRFS_MASK // Reference voltage is Vref2 + | DAC_C0_DACEN_MASK; // Enable DAC + + /* + * Enable and configure both AT1846S and HR_C6000 + */ + at1846s.init(); + C6000.init(); +} + +void radio_terminate() +{ + radioStatus = OFF; + radio_disableRtx(); + C6000.terminate(); + + DAC0->DAT[0].DATL = 0; + DAC0->DAT[0].DATH = 0; + SIM->SCGC6 &= ~SIM_SCGC6_DAC0_MASK; +} + +void radio_setOpmode(const enum opmode mode) +{ + switch(mode) + { + case FM: + gpio_setPin(RX_AUDIO_MUX); // Audio out to amplifier + gpio_clearPin(TX_AUDIO_MUX); // Audio in to microphone + at1846s.setOpMode(AT1846S_OpMode::FM); + break; + + case DMR: + gpio_clearPin(RX_AUDIO_MUX); // Audio out to HR_C6000 + gpio_setPin(TX_AUDIO_MUX); // Audio in from HR_C6000 + at1846s.setOpMode(AT1846S_OpMode::DMR); + break; + + default: + break; + } +} + +bool radio_checkRxDigitalSquelch() +{ + return true; +} + +void radio_enableRx() +{ + gpio_clearPin(VHF_LNA_EN); + gpio_clearPin(UHF_LNA_EN); + gpio_clearPin(VHF_PA_EN); + gpio_clearPin(UHF_PA_EN); + DAC0->DAT[0].DATH = 0; + DAC0->DAT[0].DATL = 0; + + if(currRxBand == BND_NONE) return; + + // Adjust reference oscillator bias and offset. + C6000.writeCfgRegister(0x04, calData->data[currRxBand].mod2Offset); + C6000.setModOffset(calData->data[currRxBand].modBias); + + // Set frequency and enable AT1846S RX + at1846s.setFrequency(config->rxFrequency); + at1846s.setFuncMode(AT1846S_FuncMode::RX); + + // Enable RX LNA + if(currRxBand == BND_VHF) + { + gpio_setPin(VHF_LNA_EN); + } + else + { + gpio_setPin(UHF_LNA_EN); + } + + radioStatus = RX; +} + +void radio_enableTx() +{ + if(config->txDisable == 1) return; + + gpio_clearPin(VHF_LNA_EN); + gpio_clearPin(UHF_LNA_EN); + gpio_clearPin(VHF_PA_EN); + gpio_clearPin(UHF_PA_EN); + + if(currTxBand == BND_NONE) return; + + // Adjust reference oscillator bias and offset. + C6000.writeCfgRegister(0x04, calData->data[currTxBand].mod2Offset); + C6000.setModOffset(calData->data[currTxBand].modBias); + + // Set frequency and enable AT1846S TX + at1846s.setFrequency(config->txFrequency); + at1846s.setFuncMode(AT1846S_FuncMode::TX); + + // Set APC voltage + DAC0->DAT[0].DATH = (apcVoltage >> 8) & 0xFF; + DAC0->DAT[0].DATL = apcVoltage & 0xFF; + + // Enable TX PA + if(currTxBand == BND_VHF) + { + gpio_setPin(VHF_PA_EN); + } + else + { + gpio_setPin(UHF_PA_EN); + } + + if(config->txToneEn) + { + at1846s.enableTxCtcss(config->txTone); + } + + radioStatus = TX; +} + +void radio_disableRtx() +{ + gpio_clearPin(VHF_LNA_EN); + gpio_clearPin(UHF_LNA_EN); + gpio_clearPin(VHF_PA_EN); + gpio_clearPin(UHF_PA_EN); + + if(radioStatus == TX) + { + at1846s.disableCtcss(); + DAC0->DAT[0].DATH = 0; + DAC0->DAT[0].DATL = 0; + } + + at1846s.setFuncMode(AT1846S_FuncMode::OFF); + radioStatus = OFF; +} + +void radio_updateConfiguration() +{ + currRxBand = getBandFromFrequency(config->rxFrequency); + currTxBand = getBandFromFrequency(config->txFrequency); + + if((currRxBand == BND_NONE) || (currTxBand == BND_NONE)) return; + + /* + * Parameters dependent on RX frequency only + */ + const bandCalData_t *cal = &(calData->data[currRxBand]); + + at1846s.setRxAudioGain(cal->rxDacGain, cal->rxVoiceGain); + + if(config->bandwidth == BW_12_5) + { + at1846s.setNoise1Thresholds(cal->noise1_HighTsh_Nb, cal->noise1_LowTsh_Nb); + at1846s.setNoise2Thresholds(cal->noise2_HighTsh_Nb, cal->noise2_LowTsh_Nb); + at1846s.setRssiThresholds(cal->rssi_HighTsh_Nb, cal->rssi_LowTsh_Nb); + } + else + { + at1846s.setNoise1Thresholds(cal->noise1_HighTsh_Wb, cal->noise1_LowTsh_Wb); + at1846s.setNoise2Thresholds(cal->noise2_HighTsh_Wb, cal->noise2_LowTsh_Wb); + at1846s.setRssiThresholds(cal->rssi_HighTsh_Wb, cal->rssi_LowTsh_Wb); + } + + C6000.writeCfgRegister(0x37, cal->digAudioGain); // DACDATA gain + + uint8_t sqlTresh = 0; + if(currRxBand == BND_VHF) + { + sqlTresh = interpCalParameter(config->rxFrequency, calData->vhfCalPoints, + cal->analogSqlThresh, 8); + } + else + { + sqlTresh = interpCalParameter(config->rxFrequency, calData->uhfCalPoints, + cal->analogSqlThresh, 8); + } + + at1846s.setAnalogSqlThresh(sqlTresh); + + /* + * Parameters dependent on TX frequency only + */ + at1846s.setPgaGain(calData->data[currTxBand].PGA_gain); + at1846s.setMicGain(calData->data[currTxBand].analogMicGain); + at1846s.setAgcGain(calData->data[currTxBand].rxAGCgain); + at1846s.setPaDrive(calData->data[currTxBand].PA_drv); + + uint8_t mod1Amp = 0; + uint8_t txpwr_lo = 0; + uint8_t txpwr_hi = 0; + + if(currTxBand == BND_VHF) + { + /* VHF band */ + txpwr_lo = interpCalParameter(config->txFrequency, calData->vhfCalPoints, + calData->data[currTxBand].txLowPower, 8); + + txpwr_hi = interpCalParameter(config->txFrequency, calData->vhfCalPoints, + calData->data[currTxBand].txHighPower, 8); + + mod1Amp = interpCalParameter(config->txFrequency, calData->vhfCalPoints, + cal->mod1Amplitude, 8); + } + else + { + /* UHF band */ + txpwr_lo = interpCalParameter(config->txFrequency, calData->uhfPwrCalPoints, + calData->data[currTxBand].txLowPower, 16); + + txpwr_hi = interpCalParameter(config->txFrequency, calData->uhfPwrCalPoints, + calData->data[currTxBand].txHighPower, 16); + + mod1Amp = interpCalParameter(config->txFrequency, calData->uhfCalPoints, + cal->mod1Amplitude, 8); + } + + C6000.setModAmplitude(0, mod1Amp); + + // Calculate APC voltage, constraining output power between 1W and 5W. + float power = std::max(std::min(config->txPower, 5.0f), 1.0f); + float pwrHi = static_cast< float >(txpwr_hi); + float pwrLo = static_cast< float >(txpwr_lo); + float apc = pwrLo + (pwrHi - pwrLo)/4.0f*(power - 1.0f); + apcVoltage = static_cast< uint16_t >(apc) * 16; + + // Set bandwidth and TX deviation, force 12.5kHz for DMR mode + if((config->bandwidth == BW_12_5) || (config->opMode == DMR)) + { + at1846s.setBandwidth(AT1846S_BW::_12P5); + at1846s.setTxDeviation(calData->data[currTxBand].mixGainNarrowband); + } + else + { + at1846s.setBandwidth(AT1846S_BW::_25); + at1846s.setTxDeviation(calData->data[currTxBand].mixGainWideband); + } + + /* + * Update VCO frequency and tuning parameters if current operating status + * is different from OFF. + * This is done by calling again the corresponding functions, which is safe + * to do and avoids code duplication. + */ + if(radioStatus == RX) radio_enableRx(); + if(radioStatus == TX) radio_enableTx(); +} + +float radio_getRssi() +{ + return static_cast< float >(at1846s.readRSSI()); +} + +enum opstatus radio_getStatus() +{ + return radioStatus; +}