sparkfun / Arduino_Apollo3

Arduino core to support the Apollo3 microcontroller from Ambiq Micro
83 stars 38 forks source link

[library issue]: using the Adafruit FRAM library with the core v1: compilation aborts, likely due to Adafruit_BusIO #390

Closed jerabaul29 closed 2 years ago

jerabaul29 commented 3 years ago

I want to use the Adafruit_FRAM_SPI library together with the Artemis core v1. I cannot use the core v2. The Adafruit_FRAM_SPI relies on the Adafruit_BusIO library. I am hitting a number of compilation errors trying to run the examples of the FRAM library:

Compiling library "Adafruit_BusIO"
/home/jrlab/.arduino15/packages/SparkFun/tools/arm-none-eabi-gcc/8-2018-q4-major/bin/arm-none-eabi-g++ -DPART_apollo3 -DAM_PACKAGE_BGA -DAM_PART_APOLLO3 -c -g -MMD -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -fdata-sections -Os -ffunction-sections -Wall -Wextra -std=gnu++11 -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DF_CPU=48000000L -DARDUINO=10607 -DARDUINO_AM_AP3_SFE_BB_ARTEMIS -DARDUINO_ARCH_APOLLO3 -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/variants/redboard_artemis/config -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/variants/redboard_artemis/bsp -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ard_supers -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/variants/redboard_artemis -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/libraries/Wire/src -I/home/jrlab/Arduino/libraries/Adafruit_FRAM_SPI -I/home/jrlab/Arduino/libraries/Adafruit_BusIO -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/libraries/SPI/src -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/mcu/apollo3 -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/mcu/apollo3/hal -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/mcu/apollo3/regs -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/utils -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/devices -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/CMSIS/AmbiqMicro/Include -I/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/am_sdk_ap3/CMSIS/ARM/Include /home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp -o /tmp/arduino-sketch-73C19A3BE4B42F268D58308828CBB3D3/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp.o
Using previously compiled file: /tmp/arduino-sketch-73C19A3BE4B42F268D58308828CBB3D3/libraries/Adafruit_BusIO/Adafruit_BusIO_Register.cpp.o
Using previously compiled file: /tmp/arduino-sketch-73C19A3BE4B42F268D58308828CBB3D3/libraries/Adafruit_BusIO/Adafruit_I2CDevice.cpp.o
In file included from /home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/Arduino.h:78,
                 from /home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/libraries/SPI/src/SPI.h:23,
                 from /home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.h:1,
                 from /home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:1:
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h: In function 'bool ap3_gpio_is_valid(ap3_gpio_pad_t)':
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h:54:38: warning: comparison is always true due to limited range of data type [-Wtype-limits]
 #define AP3_GPIO_IS_VALID(pad) ((pad >= 0) && (pad < AP3_GPIO_MAX_PADS))
                                      ^
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h:63:60: note: in expansion of macro 'AP3_GPIO_IS_VALID'
 inline bool ap3_gpio_is_valid(ap3_gpio_pad_t pad) { return AP3_GPIO_IS_VALID(pad); }
                                                            ^~~~~~~~~~~~~~~~~
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h: In function 'bool ap3_gpio_has_gpio(ap3_gpio_pad_t)':
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h:54:38: warning: comparison is always true due to limited range of data type [-Wtype-limits]
 #define AP3_GPIO_IS_VALID(pad) ((pad >= 0) && (pad < AP3_GPIO_MAX_PADS))
                                      ^
/home/jrlab/.arduino15/packages/SparkFun/hardware/apollo3/1.2.1/cores/arduino/ard_sup/ap3_gpio.h:64:60: note: in expansion of macro 'AP3_GPIO_IS_VALID'
 inline bool ap3_gpio_has_gpio(ap3_gpio_pad_t pad) { return AP3_GPIO_IS_VALID(pad); }
                                                            ^~~~~~~~~~~~~~~~~
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp: In constructor 'Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t, int8_t, int8_t, int8_t, uint32_t, BitOrder, uint8_t)':
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:51:48: error: 'digitalPinToPort' was not declared in this scope
   csPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(cspin));
                                                ^~~~~~~~~~~~~~~~
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:51:48: note: suggested alternative: 'digitalPinToInterrupt'
   csPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(cspin));
                                                ^~~~~~~~~~~~~~~~
                                                digitalPinToInterrupt
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:51:29: error: 'portOutputRegister' was not declared in this scope
   csPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(cspin));
                             ^~~~~~~~~~~~~~~~~~
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:52:15: error: 'digitalPinToBitMask' was not declared in this scope
   csPinMask = digitalPinToBitMask(cspin);
               ^~~~~~~~~~~~~~~~~~~
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:52:15: note: suggested alternative: 'digitalPinToInterrupt'
   csPinMask = digitalPinToBitMask(cspin);
               ^~~~~~~~~~~~~~~~~~~
               digitalPinToInterrupt
/home/jrlab/Arduino/libraries/Adafruit_BusIO/Adafruit_SPIDevice.cpp:58:33: error: 'portInputRegister' was not declared in this scope
     misoPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(misopin));
                                 ^~~~~~~~~~~~~~~~~

These errors result in a failure of the compilation, as could be expected.

I suppose this is due to the custom annoying Adafruit library that is not portable to non adafruit boards at the moment, right? (this is really, really annoying to me, feels like "pseudo open source" where non trivial dependency problems are introduced if not everything is bought from adafruit...). This would be possibly similar to: https://github.com/sparkfun/Arduino_Apollo3/issues/270 . I cannot use the fix recommended there to comment out the SPI part of the code, as these FRAM modules are only SPI...

jerabaul29 commented 3 years ago

Note: this is discussed here too: https://forum.arduino.cc/t/adafruit-ssd1306-library-compile-problem/674044 . There it looks like these functions / macros are actually quite standard for AVR Arduino boards. Could something similar be made available for Artemis, so that we can use some extra libraries more "transparently"? :) .

jerabaul29 commented 3 years ago

PS: if there is an alternative Sparkfun product providing equivalent functionality (i.e. 4 or more Mb of non volatile RAM), I will happily buy it instead, but I cannot find anything like this so far on the Sparkfun website.

jerabaul29 commented 3 years ago

Just for the records, the list of missing definitions / declarations (hope I did not forget some of them):

digitalPinToPort
portOutputRegister
digitalPinToBitMask
portInputRegister

I do not have enough knowledge of the low level stuff inside this core to know where / how to implement these, but looking at the Arduino AVR codes that support it, it does not look super complex; wondering if it would be a nice addition to put these in the core v1 and v2 to help users utilize a wider range of libraries out of the box :) .

jerabaul29 commented 3 years ago

Looking a bit at their code, I could maybe get some form of quick and dirty fix to get things working with the Adafruit library, but would be much cleaner to get these pin mappings defined for each board variant.

jerabaul29 commented 3 years ago

Opening a specific issue about these mappings, as this is bigger than just this library not working.

jerabaul29 commented 3 years ago

A small update: a simple solution to get this library to work (and similar for other libraries that rely on busIO from Adafruit at the moment) is to disable the constructors that rely on passing all the SPI pins (these need the mapping to be defined to work), and to instead only use the constructor where the CS and SPI pointer are passed. This is easy to disable, see for example:

Using these I am able to compile the example sketches from the FRAM library. Still needs to check if it works, will report ASAP :) .

jerabaul29 commented 3 years ago

I confirm that it is working fine using these small changes :) See the recipe here, with the corresponding readmes:

https://github.com/jerabaul29/Artemis_MbedOS_recipes/tree/main/recipes/recipe_adafruit_fram

jerabaul29 commented 3 years ago

Feel free to close. I still think that issue #391 would help improve compatibility with a wider range of pre-existing arduino libraries.