Main Site Documentation

Using SPI 1 to interface with RTC


I want to give some background and my question/issue will be in the issue section


Got this project when the developer working on the project moved to another company (A good opportunity he couldn’t pass up)

Uses the GHI G400D SoM module

Existing PCB layout and has been populated and deployed (No option to change)

Needed external real-time clock (RTC) for more accurate time over years of operation. (could be running for 5+ years)

Not using the Gadgeteer library

NETMF v4.3

The needed external RTC is interfaced with SPI 1 and the documentation specifically recommends to use SPI 2 but I can’t change the hardware because the PCB is already populated and shipped. I was trying to use the Microsoft.SPOT.Hardware API to set up and talk to the RTC but it freezes the current screen and will not change after attempting to communicate with the RTC. Since I am debugging the system I see responses that imply the module is still running and hasn’t crashed but I can’t seem to solve the issue.

Has anyone been successful with using SPI 1 to communicate with any devices?


SPI1 is shared internally with the flash memory so you have to use CS for every additional device connected to this bus


Thanks for the quick reply!

I do have one connected for CS and configured but is there one that is designated as the CS for SPI1?

Edit: From the schematic PA10 is connected as CS but I wasn’t sure if I missed a configuration for that pin to be the CS for the SPI bus.


There is not a dedicated gpio pin for CS, you can use any GPIO pin that is not already used for another function.


Would using PA10 be ok as long as I am not using COM1 or CAN1?


Yes, PA10, along with any other GPIO pin, will work fine as long as you aren’t using it for anything else.


So I just wanted to drop the code snippets on the tread to show what I am doing for initialization. Either I am missing something or I am using something incorrectly.

I initialize the spi config and the blank SPI.

static SPI.Configuration config = new SPI.Configuration(GHI.Pins.G400D.Gpio.PA10, false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
static SPI SPI1;

Then I assign the SPI to a new SPI using the configuration, create the send and receive bytes, then try to writeread those bytes.

SPI1 = new SPI(config);
byte[] address = new byte[1];
address[0] = 0x00;
byte[] value = new byte[1];
value[0] = 0x00;


If I comment out the SPI.WriteRead(address,value) line everything works fine but I don’t poll the external RTC.


Was this ever working? Is the RTC a totally new device being added to the system?


This is a new device that is being added.

I have tested the real-time clock using an Arduino Genuino/Uno to verify that the PCB layout for the RTC was correct and function.

Side note in case it helps: We are using RLP for a screen but looking at the pins I didn’t think they could interfere. They do use the LCD RGB pins, SPI 2 MISO and MOSI, and GPIO PD3, PD5, and PD6


How did you physically connect the RTC to the existing PCB?

Also, do you have an oscilloscope (make things easier).


It is connected through a header that was put on the board to allow for external SPI bus devices.

I am working on that right now. The project is bulky and tough to find space for.


Have you tried looping back the SPI connector to verify that what you send is what you’re getting back?


Just to clarify for my sake, you mean connecting mosi and miso correct?


That is Correct.


So looking into it, the scope images verify that it is correct (not posted) and everything is sending correctly BUT I did find out that my SPI MISO pin goes low and then my chip freezes. After I power cycle the device it all of a sudden goes into boot loader mode and I have to power cycle the unit again.

Looking from the documentation, to update the bootloader you have to pull that pin low. So I’m assuming it would be fair to say that I cant use this port?


If your RTC chip is driving MISO low on startup while not selected, then you are correct, you can’t use it. Since on power up you haven’t initialized the chip select pin in NETMF yet, it is floating by default so your device may think it is spuriously selected and drive MISO. To get around that, you should make sure to have a proper pull up resistor on the CS line.