Skip to content

Commit

Permalink
initial work to get I2C digipot control working (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
W3AXL committed Jun 16, 2023
1 parent c728dab commit c83a409
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 122 deletions.
92 changes: 92 additions & 0 deletions DWT_delay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Digital Voice Modem - DSP Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DSP Firmware
*
*/
//
// Based on code from Mark Carter (https://mcturra2000.wordpress.com/2022/03/10/sending-data-over-i2c-using-bit-banging/)
// And also https://github.com/PascalPolygon/stm32_bitbang_i2c/
// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0)
//
/*
* Copyright (C) 2023 by Patrick McDonnell, W3AXL
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/

#include "DWT_delay.h"

RCC_ClocksTypeDef RCC_Clocks;
uint32_t HCLK_Freq;

/**
* @brief Initializes DWT_Clock_Cycle_Count for DWT_Delay_us function
* @return Error DWT counter
* 1: clock cycle counter not started
* 0: clock cycle counter works
*/
uint32_t DWT_Delay_Init(void) {
// Get HCLK for calculation (done here so we don't have to do it every time)
RCC_GetClocksFreq(&RCC_Clocks);
HCLK_Freq = RCC_Clocks.HCLK_Frequency;
/* Disable TRC */
CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk; // ~0x01000000;
/* Enable TRC */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // 0x01000000;

/* Disable clock cycle counter */
DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk; //~0x00000001;
/* Enable clock cycle counter */
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; //0x00000001;

/* Reset the clock cycle counter value */
DWT->CYCCNT = 0;
/* 3 NO OPERATION instructions */
__ASM volatile ("NOP");
__ASM volatile ("NOP");
__ASM volatile ("NOP");

/* Check if clock cycle counter has started */
if(DWT->CYCCNT)
{
return 0; /*clock cycle counter started*/
}
else
{
return 1; /*clock cycle counter not started*/
}
}

/**
* @brief This function provides a delay (in microseconds)
* @param microseconds: delay in microseconds
*/
void DWT_Delay_us(volatile uint32_t microseconds)
{
uint32_t clk_cycle_start = DWT->CYCCNT;

/* Go to number of cycles for system */
microseconds *= (HCLK_Freq / 1000000);

/* Delay till end */
while ((DWT->CYCCNT - clk_cycle_start) < microseconds);
}

/* Use DWT_Delay_Init (); and DWT_Delay_us (microseconds) in the main */
42 changes: 42 additions & 0 deletions DWT_delay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Digital Voice Modem - DSP Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DSP Firmware
*
*/
//
// Based on code from Mark Carter (https://mcturra2000.wordpress.com/2022/03/10/sending-data-over-i2c-using-bit-banging/)
// And also https://github.com/PascalPolygon/stm32_bitbang_i2c/
// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0)
//
/*
* Copyright (C) 2023 by Patrick McDonnell, W3AXL
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/

#ifndef __DWT_DELAY_H__
#define __DWT_DELAY_H__

#include "stm32f4xx_rcc.h"

uint32_t DWT_Delay_Init(void);
void DWT_Delay_us(volatile uint32_t microseconds);

#endif
38 changes: 19 additions & 19 deletions Digipot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
Digipot::Digipot() :
m_RxFine(127U),
m_RxCoarse(127U),
m_TxFine(127U),
m_TxCoarse(127U),
m_RssiFine(127U),
m_RssiCoarse(127U)
{
/* stub */
Expand All @@ -48,47 +46,49 @@ void Digipot::initialize() {
// reset all values to middle
m_RxFine = 127U;
m_RxCoarse = 127U;
m_TxFine = 127U;
m_TxCoarse = 127U;
m_RssiFine = 127U;
m_RssiCoarse = 127U;

// Set the pot's ACR registers to non-volatile, non-shutdown mode
setRegister(TxPotAddr, TPL0102_REG_ACR, 0x40);
setRegister(RxPotAddr, TPL0102_REG_ACR, 0x40);

// push the values to the pots
setPotVal(RxPotAddr, 0, m_RxFine);
setPotVal(RxPotAddr, 1, m_RxCoarse);
setPotVal(TxPotAddr, 0, m_TxFine);
setPotVal(TxPotAddr, 1, m_TxCoarse);
setPotVal(RssiPotAddr, 0, m_RssiFine);
setPotVal(RssiPotAddr, 1, m_RssiCoarse);
setRxFine(m_RxFine);
setRxCoarse(m_RxCoarse);
setTxCoarse(m_TxCoarse);
setRssiCoarse(m_RssiCoarse);
}

void Digipot::setPotVal(uint8_t addr, uint8_t reg, uint8_t value) {
uint8_t cmd[2U];
io.I2C_Write(addr, cmd, 2U);
void Digipot::setRegister(uint8_t i2c_addr, uint8_t reg_addr, uint8_t reg_value) {
uint8_t cmd[2] = {reg_addr, reg_value};
io.I2C_Write(i2c_addr, cmd, 2U);
}

void Digipot::setRxFine(uint8_t val) {
setPotVal(RxPotAddr, 1, val);
setRegister(RxPotAddr, TPL0102_REG_WRB, val);
}

void Digipot::setRxCoarse(uint8_t val) {
setPotVal(RxPotAddr, 0, val);
setRegister(RxPotAddr, TPL0102_REG_WRA, val);
}

void Digipot::setTxFine(uint8_t val) {
setPotVal(TxPotAddr, 1, val);
//there is no TX fine pot so ignore this
return;
}

void Digipot::setTxCoarse(uint8_t val) {
setPotVal(TxPotAddr, 0, val);
setRegister(TxPotAddr, TPL0102_REG_WRA, val);
}

void Digipot::setRssiFine(uint8_t val) {
setPotVal(RssiPotAddr, 1, val);
//there is no RSSI fine pot so ignore this
return;
}

void Digipot::setRssiCoarse(uint8_t val) {
setPotVal(RssiPotAddr, 0, val);
setRegister(TxPotAddr, TPL0102_REG_WRB, val);
}

#endif // DIGIPOT_ENABLED
21 changes: 10 additions & 11 deletions Digipot.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0)
//
/*
* Copyright (C) 2022 by Natalie Moore
* Copyright (C) 2023 by Natalie Moore, Patrick McDonnell
*
* 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
Expand All @@ -25,6 +25,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#if !defined(__DIGIPOT_H__)
#define __DIGIPOT_H__

Expand All @@ -33,13 +34,13 @@
#include "Defines.h"
#include <stm32f4xx_i2c.h>

#define RxPotAddr 0x2C
#define TxPotAddr 0x2E
#define RssiPotAddr 0x2F
#define TxPotAddr 0xA2 // 7-Bit address 1010001 (0x51) becomes 0xA2 after << 1
#define RxPotAddr 0xA4 // 7-Bit address 1010010 (0x52) becomes 0xA4 after << 1
//#define RssiPotAddr 0x2E // RSSI pot is part of TX pot IC

#define AD5242_CMD_SET_RDAC1 0x00
#define AD5242_CMD_SET_RDAC2 0x80
#define AD5242_CMD_RESET 0x40
#define TPL0102_REG_WRA 0x00
#define TPL0102_REG_WRB 0x01
#define TPL0102_REG_ACR 0x10

// ---------------------------------------------------------------------------
// Class Declaration
Expand Down Expand Up @@ -75,12 +76,10 @@ class DSP_FW_API Digipot {
private:
uint8_t m_RxFine;
uint8_t m_RxCoarse;
uint8_t m_TxFine;
uint8_t m_TxCoarse;
uint8_t m_RssiFine;
uint8_t m_RssiCoarse;
// helper to set softpot value at given address and register
void setPotVal(uint8_t addr, uint8_t reg, uint8_t value);
// helper to set registers
void setRegister(uint8_t i2c_addr, uint8_t reg_addr, uint8_t reg_value);
};

#endif // DIGIPOT_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion FirmwareMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void setup()
{
serial.start();
#if defined(DIGIPOT_ENABLED)
digitpot.initialize();
digipot.initialize();
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extern CWIdTX cwIdTX;

#if defined(DIGIPOT_ENABLED)
/** Digipot */
extern Digipot digitpot;
extern Digipot digipot;
#endif

#if defined(NATIVE_SDR)
Expand Down
Loading

0 comments on commit c83a409

Please sign in to comment.