So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Shogun
#89951 I have my Arduino IDE setup for ESP8266 and have compiled another program recently with it. I am starting another project and was starting with some Arduino code. I have a few ESP8266's left over from another project and thought this would be a perfect project for them. The code compiles fine when I select an Arduino processor like an Uno, but won't compile on any ESP8266 I choose. I am not that familiar with these functions so I can't seem to determine where this is being defined...

Code: Select all#include <Arduino.h>
#include <wiring_private.h> // cbi/sbi definition

#if defined(UCSRB) && defined(UBRRL)
  volatile byte * const _ucsrb = &UCSRB;
#else
  volatile byte * const _ucsrb = &UCSR0B;
#endif

// Definitions for the hardware Serial port, used for Nexstar auxBus
#define AUXBUS_RX_PIN       0
#define AUXBUS_TX_PIN       1
#define AUXBUS_BUSY_PIN     4  // The RTS (aka. "Busy") line for Nexstar auxBus
#define auxBus              Serial
static inline void          auxBus_enable_tx()  { sbi(*_ucsrb, TXEN0); }
static inline void          auxBus_disable_tx() { cbi(*_ucsrb, TXEN0); }

#define MINUS_PIN           9
#define PLUS_PIN            8

#define LED_PIN             13


Again, without any changes it compiles on Arduino processors but fails on ESP8266 processors with the following error:

Code: Select allArduino: 1.8.13 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

celestron_motor_focuser_hand_controller:7:35: error: 'UCSR0B' was not declared in this scope
   volatile byte * const _ucsrb = &UCSR0B;

<deleted>\celestron_motor_focuser_hand_controller\celestron_motor_focuser_hand_controller.ino: In function 'void auxBus_enable_tx()':

celestron_motor_focuser_hand_controller:16:64: error: 'TXEN0' was not declared in this scope
 static inline void          auxBus_enable_tx()  { sbi(*_ucsrb, TXEN0); }
                                                                     ^

celestron_motor_focuser_hand_controller:16:69: error: 'sbi' was not declared in this scope
 static inline void          auxBus_enable_tx()  { sbi(*_ucsrb, TXEN0); }
                                                                     ^
<deleted>\celestron_motor_focuser_hand_controller\celestron_motor_focuser_hand_controller.ino: In function 'void auxBus_disable_tx()':
celestron_motor_focuser_hand_controller:17:64: error: 'TXEN0' was not declared in this scope
 static inline void          auxBus_disable_tx() { cbi(*_ucsrb, TXEN0); }
                                                                ^

celestron_motor_focuser_hand_controller:17:69: error: 'cbi' was not declared in this scope
 static inline void          auxBus_disable_tx() { cbi(*_ucsrb, TXEN0); }
                                                                     ^

exit status 1
'UCSR0B' was not declared in this scope


Any help or suggestions would be appreciated!
User avatar
By GetOffMyHack
#89963 I am not an ESP8266 expert, but what I can tell you is that your Arduino code is using low-level registers which are generally µC specific, in this case for the ATmega328 in an UNO, and will not be found on any other µC (outside of AVRs). And to add to that, the registers in your code (UCSRB, TXEN0, etc.) are UART specific on the AVR.

Any reason not to use the Arduino Serial object, as that abstracts the low-level registers needed to config / use the serial port?
User avatar
By QuickFix
#89968 An Arduino (the PCB or module with an ATMEL chip) is not the same as an ESP8266 (in any shape or form).
Yes you can program them both with the Arduino IDE (the development environment), but they're not created equal

You could (somewhat) compare it with an Apple Mac and a Windows PC: they both can do similar things, but you can't (directly) use each other's software: they're simply not compatible.

Most (ATMEL) Arduino sketches can be rewritten (within reason) to work on ESP8266 or ESP32 hardware, but some changes need to be made.
It depends on the type of firmware you're trying to compile, but you probably have to remap/redefine some I/O-ports, rewrite (or optimize) loops since tight loops will trip the ESP watchdog and include other (ESP-compatible) libraries.

9 times out 10 times it's not too difficult (or impossible) to do, but most of the time some rewriting is inevitable.