#define CSPIN 11  //spi chip select
#define SCKPIN 12  //spi clock
#define MOSIPIN 13  //spi mosi
#define MISOPIN 14  //spi miso

int main()
{
  NRF_GPIO->PIN_CNF[CSPIN] = 1;  //output
  NRF_GPIO->PIN_CNF[SCKPIN] = 1;  //output
  NRF_GPIO->PIN_CNF[MOSIPIN] = 1;  //output
  NRF_GPIO->PIN_CNF[MISOPIN] = 0;  //input pin, input buffer connected, no pull, S0S1, sense disabled
  NRF_GPIO->OUTSET |= 1 << CSPIN;  //deactivate by setting chip select high
  NRF_SPIM0->PSEL.SCK = SCKPIN;
  NRF_SPIM0->PSEL.MOSI = MOSIPIN;
  NRF_SPIM0->PSEL.MISO = MISOPIN;
  NRF_SPIM0->CONFIG = 0b010;  //CPOL 0 -- clock polarity active high, CPHA 1 -- sample on trailing clock edge, send Msb first
  NRF_SPIM0->FREQUENCY = 0x20000000;  //2 Mbps
  uint8_t spiTXBuf[2];  //2 byte tx buffer
  uint8_t spiRXBuf[2];  //2 byte rx buffer
  spiTXBuf[0] = 0b10101010;  //example byte
  spiTXBuf[1] = 0b10101010;  //example byte
  NRF_SPIM0->TXD.PTR = (uint32_t)((uint8_t *)spiTXBuf);
  NRF_SPIM0->TXD.MAXCNT = 2;
  NRF_SPIM0->RXD.PTR = (uint32_t)((uint8_t *)spiRXBuf);
  NRF_SPIM0->RXD.MAXCNT = 2;
  NRF_SPIM0->ENABLE = 7;  //enabled
  NRF_SPIM0->EVENTS_ENDTX = 0;
  NRF_SPIM0->EVENTS_ENDRX = 0;
  NRF_GPIO->OUTCLR |= 1 << CSPIN;  //drive cs low to initiate spi comm
  for(uint8_t i = 0; i < 21; i++) __asm__("nop\n\t");  //low for 21 machine instructions
  NRF_SPIM0->TASKS_START = 1;
  while(!NRF_SPIM0->EVENTS_ENDTX);  //last byte transmitted
  while(!NRF_SPIM0->EVENTS_ENDRX);  //last byte received
  NRF_SPIM0->TASKS_STOP = 1;
  NRF_GPIO->OUTSET |= 1 << CSPIN;
  while(1);
}