This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Quick check of my SPI code & a few related questions

Hi,

I've began making a SPI module for programming a PIC16. It only transmits data, so two pins.

My questions are:

1. any obvious errors?
2. does the EVENTS_ENDTX occur when the double buffer is empty? I need to ensure the cpu is a step ahead of the buffer emptying to ensure data continuity.  The 'ENDTX' suggests I could be in a race to fill the buffer again before the SPI has it's next clock. If so, best means to approach it?

inline void GLUE_Start (void) {

  NRF_SPIM0->ENABLE = 1;
}

static void wait (void) {

  while (! NRF_SPIM0->EVENTS_ENDTX) {}
  NRF_SPIM0->EVENTS_ENDTX = 0;
}

static void bulkErase (void) {

  NRF_SPI0->TXD = 0x18; // bulk erase
  nrf_delay_ms(10); // TERAB
}

static void loadAddress (uint24_t value) {

  NRF_SPI0->TXD = 0x80;
  wait();
  NRF_SPI0->TXD = value >> 15
  wait();
  NRF_SPI0->TXD = value >> 7 & 255;
  wait();
  NRF_SPI0->TXD = value & 255;
}

inline void GLUE_UpdateFirmware(void) {

  NRF_GPIO->OUTSET = 1 << CFG_PIN_GLUE_MODE; // into reset
  NRF_GPIO->OUTSET = 1 << CFG_PIN_GLUE_VBUSPOW; // enable 3.3v power

  GLUE_Start();

  NRF_SPI0->TXD = 'M';
  NRF_SPI0->TXD = 'C';
  wait();
  NRF_SPI0->TXD = 'H';
  wait();
  NRF_SPI0->TXD = 'P';
  wait();
  NRF_SPI0->TXD = 'P';
  wait();
  loadAddress(0x8000); // all areas
  bulkErase();

  GLUE_ShutDown();

  NRF_GPIO->OUTCLR = 1 << CFG_PIN_GLUE_MODE; // out of reset
  NRF_GPIO->OUTCLR = 1 << CFG_PIN_GLUE_VBUSPOW; // enable 5v power
}

Related