dcs-bios-arduino-library icon indicating copy to clipboard operation
dcs-bios-arduino-library copied to clipboard

ATmega32U4 supported as a RS485 Master/Slave

Open kbastronomics opened this issue 2 years ago • 10 comments

Is it possible to get the ATmega32U4 supported as a RS485 Master/Slave? working on a project we we're using the Sparkfun Pro Micro with HID but would the rest of our pit we'd like to also use it as RS485 Master/Slave.

It looks like the issue is with the UART
added my log file to show the errors

RS485.log.txt

My Sample test code

#include <Arduino.h>

/*
  The following #define tells DCS-BIOS that this is a RS-485 slave device.
  It also sets the address of this slave device. The slave address should be
  between 1 and 126 and must be unique among all devices on the same bus.
*/

#define DCSBIOS_DEFAULT_SERIAL
#define DCSBIOS_RS485_SLAVE 1

/*
  The Arduino pin that is connected to the
  /RE and DE pins on the RS-485 transceiver.
*/
#define TXENABLE_PIN 2

#include "DcsBios.h"

/* paste code snippets from the reference documentation here */

void setup() {
  DcsBios::setup();
}

void loop() {
  DcsBios::loop();
}

kbastronomics avatar Nov 30 '22 04:11 kbastronomics

Note that I have tried this with VSCode+PlatformIO and Arduino 1.8.6 both give same errors

kbastronomics avatar Dec 27 '22 15:12 kbastronomics

If you're comfortable with it, please try giving the "32U4_Support" branch of my fork a try. If that works I will submit the pull request for this repo. You will also need to add a #define UART1_SELECT to your script and remove the #define DCS_BIOS_DEFAULT_SERIAL when you are using RS485. I don't have the hardware to test this out on a live RS485 network, but it does appear to compile correctly now. For reference: the root of this issue appears to be that the 32U4 indexes its UART port off of 1 instead of zero. UART0 does not exist, UART1 does. I don't have a deep enough understanding of the RS485 code to comment on any further effects that this might have outside of RS485, but my current implementation essentially just tells BIOS to pretend that it is an ATmega2560 instead of a 32U4 for the purposes of communication. @talbotmcinnis may have a clearer knowledge of what that portion of the code is actually doing.

AerialElectron avatar Dec 28 '22 02:12 AerialElectron

Happy to try. It did compile the slave code see attcahed for one message in it

C:\Users\smgvb\Documents\Arduino\libraries\dcs-bios-arduino-library-32U4_Support\src/internal/DcsBiosNgRS485Slave.cpp.inc: In member function 'void DcsBios::RS485Slave::rxISR()':
C:\Users\smgvb\Documents\Arduino\libraries\dcs-bios-arduino-library-32U4_Support\src/internal/DcsBiosNgRS485Slave.cpp.inc:79:12: warning: this statement may fall through [-Wimplicit-fallthrough=]
      state = RX_WAIT_ADDRESS;
      ~~~~~~^~~~~~~~~~~~~~~~~
C:\Users\smgvb\Documents\Arduino\libraries\dcs-bios-arduino-library-32U4_Support\src/internal/DcsBiosNgRS485Slave.cpp.inc:83:4: note: here
    case RX_WAIT_ADDRESS:
    ^~~~

log.txt

It does not compile the RS485 Master code though for me (not sure if you address master or not) here's the log from that compile.

RS485Master_log.txt

I really appreciate your help on this

kbastronomics avatar Dec 28 '22 03:12 kbastronomics

I looked at the code around that point and it seems to be expected to fall thru, refering to this

C:\Users\smgvb\Documents\Arduino\libraries\dcs-bios-arduino-library-32U4_Support\src/internal/DcsBiosNgRS485Slave.cpp.inc:79:12: warning: this statement may fall through [-Wimplicit-fallthrough=]
      state = RX_WAIT_ADDRESS;

kbastronomics avatar Dec 28 '22 16:12 kbastronomics

Correct, that fall through is intentional. That particular message is only a warning, so it shouldn't be an issue, but I've corrected it now. As for the master version: I'll look into it. Theoretically, it should be possible, but I'm learning this codebase on the fly.

AerialElectron avatar Dec 28 '22 19:12 AerialElectron

Been awhile on this issue so checking in to see if there's been any progress on this?

kbastronomics avatar Feb 03 '23 18:02 kbastronomics

Kind of gave up on this ask, I've gotten it compiling now on the Pro-Micro in both Master/Slave modes, really does come down to the 32U4 has no UART0 that's reserved for the CDC USB port, so UART1 is the first UART available. my current code is still untested and have to check the UART BAUD settings. I'll eventually do a PR once fully tested for it to incorporate if desired.

oh and also believe I got IRQ working, also to be tested

kbastronomics avatar Aug 27 '23 23:08 kbastronomics

@kbastronomics Did you ever get this working?

I made the mistake of buying ATMEGA32U4 Pro Micro's instead of Nano's, and rather than let them go to waste I'm hoping you've solved this issue.

ltshifty avatar Feb 04 '24 13:02 ltshifty

Partially, Slave seems to be working. master is a little more work.

kbastronomics avatar Feb 04 '24 14:02 kbastronomics