mattairtech / ArduinoCore-samd

This is a fork from arduino/ArduinoCore-samd on GitHub. This will be used to maintain Arduino support for SAM D|L|C (M0+ and M4F) boards including the MattairTech Xeno Mini and the MT-D21E (see https://www.mattairtech.com/). It adds support for new devices like the D51, L21, C21, and D11. It also adds new clock sources, like a high speed crystal or internal oscillator.
104 stars 43 forks source link

Dataflash library compatibility error #9

Closed perigalacticon closed 6 years ago

perigalacticon commented 7 years ago

I would like to use a library for interfacing with Dataflash, D series SPI Flash memory with the SAMD11. I am successfully using the "arduino-dataflash" library (by Mooz from https://github.com/BlockoS/arduino-dataflash ) with ATTINY841 boards but for the SAMD11 the compiler gives the following error:

C:\Program Files (x86)\Arduino\libraries\DataFlash\DataFlash.cpp: In member function 'void DataFlash::setup(int8_t, int8_t, int8_t)':

C:\Program Files (x86)\Arduino\libraries\DataFlash\DataFlash.cpp:152:15: error: 'SPSR' was not declared in this scope

 m_SPSR = (SPSR & SPI_2XCLOCK_MASK);

           ^

I am using the Generic D11C14A board option from the Beta Core in the Arduino IDE.

Do you understand this error and how I might make this library work with the SAMD11 with your core?

perigalacticon commented 7 years ago

The relevant code lines from DataFlash.cpp are:

    /* Save SPI registers with settings for DataFlash for fast restore
     * in .begin(). */
    m_SPSR = (SPSR & SPI_2XCLOCK_MASK);
    m_SPCR = SPCR;

    /* Clear pending SPI interrupts */
#ifdef __AVR__
    uint8_t clr;
    clr = SPSR;
    clr = SPDR;
    (void) clr; // Prevent variable set but unused warning. No code generated.
#endif`
perigalacticon commented 7 years ago

I think the problem may be with the definition of "SPI_2XCLOCK_MASK", I found it in Arduino file "SPI.h": https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/SPI/src/SPI.h `

define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR

`

mattairtech commented 7 years ago

It looks like the library is compatible with the AVR core, but not the SAM or SAMD cores. While there is an "#ifdef AVR" in at least one place, there are other places in the code that reference the SPSR and SPCR registers, which do not exist with SAMD. It looks like the dataflash library mostly uses the SPI library, but it directly accesses these registers (looks like only once), breaking compatibility for non-AVR. This should be pretty easy to fix, but you will have to contact the author. It looks like a performance enhancement that should not be needed for the SAMD.