Friday, July 19, 2024

Homebrew sBitx - RTC and Power Meter - ATTINY85 Tale of Woe

 

sBitx RTC and SWR/PWR Meter

I'm getting down to the end of the sBitx build.  The last two functional modules are the RTC and the SWR/PWR meter.  Both are attached to the sBitx bitbang I2C bus.  What is a bitbang bus and why are we using it?

In Farhan, VU2ESE's original build of the sBitx he found that the WM8731 codec chip and the SI5351 clock generator did not play well with each other on the same I2C bus.  (I2C is a ubiquitous communications bus for microcontrollers and peripherals - its how the Raspberry Pi controls thes other modules in the sBitx).  Farhan decided to implement a second I2C bus using alternate GPIO pins on the Pi - and for that he needed a software implementaion of the I2C protrocol - that is the I2C bitbang.  He left the codec on the dedicated hardware bus and put the SI5351 and later the Real Time Clock and SWR/PWR meter modules on the bitbang bus.

The RTC is an Adafruit module that is a battery backed up clock for the Pi.  So if you take your RTC-enabled sBitx to the field with no internet you can be confident that the time will still be correct and your FT8 QSOs will work.  The SWR/PWR meter consists of a Stockton Bridge directional coupler and an ATTINY85 microcontroller to sample the forward and reflected power from the bridge and send it on the the Pi for processing. 

The picture at the top of the post is the board with the RTC and ATTINY95 sitting next to the directional coupler - just prior to wiring it all up.   The RTC meter worked perfectly on first power up  -so far, so good.  

Here is how the PWR and SWR meter sampling works.  On transmit, the main sbitx program running on the Pi sends an I2C command to the ATTINY85 telling it to sample the forward and reflected power pads on the coupler and send the results back to the Pi.  The ADC on the ATTINY85 should return values from 0-1023 which linearly represent 0 - 3.3V DC.  

TALE OF WOE BEGINS HERE

Directional Coupler - PWR/SWR Bridge 

ATTINY85 PWR/
SWR Sensor


To test, I put the sBitx in CW mode at max power on 40 meters which should be approximately 20 watts into a dummy load so the SWR should be 1:1.   I keyed down and the power meter on the sBitx read 0 and the SWR read 1:1.  Not good.  I also had an external analog power meter in the circuit and it dutifully swung up to 20 watts.  So power out is ok, but the sBitx power sensor is not working.

First question was - is the directional coupler working?   The Stockton bridge samples the forward and reflected power and rectifies to a DC voltage from 0 to about 3 volts.  I put a DC power meter on the FWD pad and keyed down - the result was 1.5V DC.  Check!  For good measure the REF pad measured about 120 millivolts - so pretty close to 1:1 as expected.

Next I add print statements to the sBitx code to print out the values received from the ATTINY85.  This would tell  me two things.  First - was the ATTINY85 sending back anything - and what was it sending back.  I keyed down while observing the console window - yes the ATTINY85 was sending back data on transmit, but instead of being 0-1023 the value was always -1 (all 1s binary).  

Next question - is the problem in the ATTINY85 ADC or the I2C exchange.  So to do that I made changes to the code in the ATTINY85 to send hard-coded values.  That takes the ADC out of the equation.

On receipt of a an I2C command from the Pi an interrupt fires and the ATTINY85 samples the FWD and REF pads of the coupler and sends the data back to the Pi.  The code is brief enough that I can include the whole sketch right here:  

#include <Wire.h>

int16_t fwd, ref;
byte message[4];

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  fwd = analogRead(A2);
  ref = analogRead(A3);

  message[0] = fwd & 0xff;
  message[1] = fwd >> 8;
  message[2] = ref & 0xff;
  message[3] = ref >> 8;
  Wire.write(message, 4); // 4 bytes message with fwd and ref
}

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
}

So for my next test I hard-coded vfw and vref, programmed a new ATTINY85 and tried again.  No change - still receiving only -1.

Next step - is the problem on the ATTINY85 side or the Raspberry Pi side.  To figure this out I wanted to look at the I2C packets as they traversed the bus.  Fortuitously my Rigol Oscillocpe will decode digital signals including I2C.  I hooked probe channel one up to the Clock line (SCL) and channel two up to the Data line (SDA) and after some fiddling and a couple of Youtube videos I managed to capture a single message going from the ATTINY85 to the Pi.

I2C Decode on Rigol DS1202 Scope

Just an aside. This is super cool! I can see the whole protocol, the ones and zeros and acks and nacks. And the Rigol even decodes it for me.  Its such a pleasure to work with good test tools - and at such reasonable prices.  Just a few years ago protocol analyzers ran into the thousands of dollars. Moderns digital scopes and tools like the NanoVNA and TinySA open up a whole new world to homebrew radio enthusiasts.

So hardware probe results confirm the software testg - the ATTINY85 is sending back on all 1s is the data frame.  

So that is where the tale of woe stops at the point. I am certain at this time that this is a software problem in one of the ATTINY85 Ardunio libraries,  I've done some internet sleuthing and found that others have had this identical problem going back almost 10 years.   I suspect the issue is with either the board manager I loaded for the ATTINY85 or a bad version of the WIRE (I2C) protocol code for the ATTINY85.  Like much in the open-source Arduino world there are several competing libraries with either identical or very nearly identical names and there is rarely a definitive version. There are multiple board definitions for the ATTINY85 and multiple versions of the WIRE protocol.  Its not clear which one Farhan used in the original build (he's checking), but I am pretty sure I have the wrong one.  

Away from the bench for a few days - but in the meantime if you have any suggestions, leave them in the comments below.

73 from St. Michaels, MD on the Eastern Shore of the Chesapeake Bay,
Dean
KK4DAS

St. Michaels Harbor


No comments:

Post a Comment