Monthly Archives: May 2014

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.