Datalog with MegaPirateNG 3.0.1 R4

As new MPNG 3.0.1 R4 have completely overhauled structure there is needed also do code changes in different way. Here is how to make it work. Sample uses pin 33 (Arduino notation) which is PortC , Pin 4 in ATMega notation.

What you need to update :

Lib\AP_HAL_MPNG\SPIDevice_SPI0.cpp

#define SPI0_MISO_PIN MISO
#define SPI0_MOSI_PIN MOSI
#define SPI0_SCK_PIN SCK
#define SPI0_SS_PIN SS  //add this

AVRSemaphore AVRSPI0DeviceDriver::_semaphore;

static volatile bool spi0_transferflag = false;

void AVRSPI0DeviceDriver::init() {
hal.gpio->pinMode(SPI0_MISO_PIN, GPIO_INPUT);
hal.gpio->pinMode(SPI0_MOSI_PIN, GPIO_OUTPUT);
hal.gpio->pinMode(SPI0_SCK_PIN, GPIO_OUTPUT);
hal.gpio->pinMode(SPI0_SS_PIN, GPIO_OUTPUT); //and add this

_cs_pin->mode(GPIO_OUTPUT);
_cs_pin->write(1);

Default SS pin have to be defined, set on output even if you are using different pin for slave CS. Reason is simple – if SS is not on output, SPI in AtMega resetting Master mode during operation and communication doesn’t work.

libraries\AP_HAL_MPNG\SPIDeviceManager_MPNG.cpp

/* dataflow cs is on arduino pin 53, PORTB0 */
AVRDigitalSource* df_cs = new AVRDigitalSource(_BV(4), PC);  //change this
/* dataflash: divide clock by 2 to 8Mhz, set SPI_MODE_3

Pin have to be changed. Use “PC”/”PB” convention instead of “PORTC”. Maybe there is not difference but PORTC doesn’t worked for me.

If anybody can confirm it to me, it will be great :-) After couple of tests I am little bit out and not sure if I covered everything.

7 thoughts on “Datalog with MegaPirateNG 3.0.1 R4

  1. I can confirm that this modification for 3.0.1 R4 and way to modify code works great.
    Thank You DevFor8 for Your hard work.

    Happy Datalogging, greets.

  2. Greetings to Pilsen,

    I tried to build the external memory mod and could not get it to work.

    I am running a HK AIO V2 (Crius V1 clone) with MPNG 3.0.1R4 and wired the memory according to the primary post onto the SPI connector. CS ist connected to pin 33 on the board. I checked all wiring for three times. It’ all good.

    So when it boots it blinks twice and locks up (stuck on SPI I guess). The O-scope shows CS pin going high once – so that seems to work.
    I can see boot message on terminal but it never proceeds to CLI.

    The code changes up here seem to be slightly different to mine.
    The SPIDevice_SPI0.cpp is looking different.

    Here is what I changed:

    /libraries/AP_HAL_MPNG/SPIDeviceManager_MPNG.cpp

    void MPNGSPIDeviceManager::init(void* machtnichts) {
    /* dataflow cs is on arduino pin 53, PORTB0 */
    AVRDigitalSource* df_cs = new AVRDigitalSource(_BV(4), PC);
    /* dataflash: divide clock by 2 to 8Mhz, set SPI_MODE_3
    * spcr gets 0x0C to set SPI_MODE_3
    * spsr gets bit SPI2X for clock divider */
    _dataflash = new AVRSPI0DeviceDriver(df_cs, 0x0C, _BV(SPI2X));
    _dataflash->init();
    }

    /libraries/AP_HAL_MPNG/SPIDevice_SPI0.cpp

    #define SPI0_MISO_PIN 50
    #define SPI0_MOSI_PIN 51
    #define SPI0_SCK_PIN 52
    #define SPI0_SS_PIN 33

    AVRSemaphore AVRSPI0DeviceDriver::_semaphore;

    static volatile bool spi0_transferflag = false;

    void AVRSPI0DeviceDriver::init() {
    hal.gpio->pinMode(SPI0_MISO_PIN, GPIO_INPUT);
    hal.gpio->pinMode(SPI0_MOSI_PIN, GPIO_OUTPUT);
    hal.gpio->pinMode(SPI0_SCK_PIN, GPIO_OUTPUT);
    hal.gpio->pinMode(SPI0_SS_PIN, GPIO_OUTPUT);

    Any suggestions?

    Greetz

    Moscito

    1. Hi

      you are almost ok :-) But there is misunderstanding in functionality of SS pin and CS pin. It is common because default SS pin is often used as default CS pin. We can’t access pin 53 on board so we need to play with two pins instead of one. SS pin must be set to OUTPUT to make ATMega work in Master SPI mode. This is what you did wrong in your code.

      Look on my and your code :

      #define SPI0_SS_PIN SS //here MUST BE “SS” even if you are using CS on different pin than 53 (as you do) because in 3.0.1 code is this not used for CS pin but we must use it for Master switch

      This should be all what you need to do and then it should work. Just for sure check also this :

      hal.gpio->pinMode(SPI0_SS_PIN, GPIO_OUTPUT); //and add this
      _cs_pin->mode(GPIO_OUTPUT);
      _cs_pin->write(1);

      Here you can see switch of system “SS” pin to OUTPUT (and with this is ATMega switched to SPI master) and also you can see that _cs pin is set to output and 1 later. So both SS and CS pins are output, real CS (33) is also on “1″.

  3. Hi,

    thank you for the quick answer.
    You were absolutely right. The “SS” did the trick.

    test] logging
    Testing dataflash logging
    Manufacturer: 0x1f Device: 0×2600
    NumPages: 4096 PageSize: 528

    Now that you explained it I also understood.

    Excellent work – thanks for sharing this.

    Greets

    Moscito

  4. Hi.
    If I understand, SPI protocol is used for SD cards, so it’s possible to use an SD card instead of AT45DB161D?
    Because old SD cards may be more accesible than SMD chip for many users and.. may be possible to read directly fom PC?

    1. SD as dataflash – yes, it should be possible but single memory chip is smaller and less problematic solution than SDcard in socket. Chip is available everywhere, ebay with free shipping include so I don’t see it as problem. But it is open source so enjoy and use it :-D For example, now I am using PixHawk board and it have micro SD card for log

      SD readable in PC – this is more bigger problem because it is different when you just store data and when you store data in FAT format. FAT writing take lot of space in processor memory/flash and is not easy implementable in mega 2560 with megapirate (no space)

Leave a Reply to Tomek Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>