<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nrf52840 3 wire spi using sdk</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/105329/nrf52840-3-wire-spi-using-sdk</link><description>Is it possible to use SDK of nrf52840 to configure a 3wire SPI? 
 I saw in some posts that i can not use directly the MISO pin and not assing it in the sdk&amp;#39;s nrf function, but is the best way to proceed?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 03 Nov 2023 10:42:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/105329/nrf52840-3-wire-spi-using-sdk" /><item><title>RE: nrf52840 3 wire spi using sdk</title><link>https://devzone.nordicsemi.com/thread/453935?ContentTypeID=1</link><pubDate>Fri, 03 Nov 2023 10:42:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a77b931f-1edc-4962-b53e-531564132cfb</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;I agree with Hugh, there is no reason you shouldn&amp;#39;t be able to set either the MISO or the MOSI pin to unassigned (0xFFFFFFFF) in order to run the SPI interface in 3-wire mode.&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf52840 3 wire spi using sdk</title><link>https://devzone.nordicsemi.com/thread/453847?ContentTypeID=1</link><pubDate>Thu, 02 Nov 2023 20:41:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5a3decb7-101a-4bdc-9797-86b943229abf</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;By SDK hopefully you mean nRF52 SDK, but in any case here&amp;#39;s a guide using bare-metal programming which you can use to alter other examples. Tested with LIS2DH12 which starts as 4-wire SPI but can be set to 3-wire SPI. (This code assumes the SPI slave is always 3-wire so the 4-wire to 3-wire slave command is not shown). Sequence is send a 1-byte command, no rx, reverse the pin then send a dummy command and receive whatever number of rx bytes are required, in this example a single byte &amp;quot;I am ..&amp;quot; response but can be longer&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define CSPIN    11  // spi chip select
#define SCKPIN   12  // spi clock
#define MOSIPIN  13  // spi mosi
//#define MISOPIN  14  // spi miso - not used in 3-wire mode

#define LIS2DH12_WHO_AM_I  0x0F // Read as 0x33

uint8_t spi3wTXBuf[] = {0x80|LIS2DH12_WHO_AM_I, 0xFF};
uint8_t spi3wRXBuf[sizeof(spi3wTXBuf)] = {0,0};
static const uint8_t m3wRxTxBufLength = sizeof(spi3wTXBuf);

volatile uint32_t mSpiInterruptCounter = 0UL;

void notSPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void)
{
  if (NRF_SPIM0-&amp;gt;EVENTS_END) NRF_SPIM0-&amp;gt;EVENTS_END = 0;
  mSpiInterruptCounter++;
}

static void Test3WireSPI(void)
{
  NRF_GPIO-&amp;gt;PIN_CNF[CSPIN]   = 0x301;  // output, high drive high and low H0H1
  NRF_GPIO-&amp;gt;PIN_CNF[SCKPIN]  = 0x301;  // output, high drive high and low H0H1
  NRF_GPIO-&amp;gt;PIN_CNF[MOSIPIN] = 1;      // output, standard drive S0S1
//NRF_GPIO-&amp;gt;PIN_CNF[MISOPIN] = 0;      // input pin, input buffer connected, no pull, S0S1, sense disabled
  NRF_GPIO-&amp;gt;OUTSET = 1 &amp;lt;&amp;lt; CSPIN;       // deactivate by setting chip select high
  NRF_SPIM0-&amp;gt;PSEL.SCK = SCKPIN;
  NRF_SPIM0-&amp;gt;PSEL.MOSI = MOSIPIN;
  NRF_SPIM0-&amp;gt;PSEL.MISO = 0xFFFFFFFFUL; // MISOPIN;
  NRF_SPIM0-&amp;gt;CONFIG = 0;               // CPOL 0 -- clock polarity active high, CPHA 1 -- sample on trailing clock edge, send Msb first
  NRF_SPIM0-&amp;gt;FREQUENCY = 0x80000000UL; // 8 Mbps
  NRF_SPIM0-&amp;gt;ORC =0;                   // Unused Tx bytes, set all low

  // Configure registers for 3-wire mode
  NRF_SPIM0-&amp;gt;PSEL.MOSI = MOSIPIN;
  NRF_SPIM0-&amp;gt;PSEL.MISO = 0xFFFFFFFFUL; // MISOPIN;
  NRF_GPIO-&amp;gt;PIN_CNF[MOSIPIN] = 1;       // output
  NRF_SPIM0-&amp;gt;EVENTS_ENDTX = 0;
  //NRF_SPIM0-&amp;gt;EVENTS_ENDRX = 0;
  // Disable all interrupts
  NRF_SPIM0-&amp;gt;INTENCLR = 0xFFFFFFFFUL;
  // Enable selected interrupts
  NRF_SPIM0-&amp;gt;INTENSET = 0x40;   // END
  // Set interrupt priority and enable interrupt
  NVIC_SetPriority(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, 6);
  NVIC_ClearPendingIRQ(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
  NVIC_EnableIRQ(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
  NRF_SPIM0-&amp;gt;ENABLE = 7;               // enable SPI
  // Transfer registers to set 3-wire SPI mode on target
  // - not required if slave only has or is already in 3-wire mode

  // Send 3-wire whoami command byte (1 byte only)
  // Connect output drive, connect 1-wire rx/tx output to MOSI
  NRF_SPIM0-&amp;gt;PSEL.MISO = 0xFFFFFFFF; // MISOPIN;
  NRF_SPIM0-&amp;gt;PSEL.MOSI = MOSIPIN;
  NRF_GPIO-&amp;gt;PIN_CNF[MOSIPIN] = 1;    // output
  NRF_SPIM0-&amp;gt;TXD.PTR = (uint32_t)spi3wTXBuf;
  NRF_SPIM0-&amp;gt;TXD.MAXCNT = 1;
  NRF_SPIM0-&amp;gt;RXD.PTR = NULL;
  NRF_SPIM0-&amp;gt;RXD.MAXCNT = 0;
  NRF_SPIM0-&amp;gt;EVENTS_ENDTX = 0;
  NRF_GPIO-&amp;gt;OUTCLR = 1 &amp;lt;&amp;lt; CSPIN;       // drive cs low to initiate spi comm
  NRF_SPIM0-&amp;gt;TASKS_START = 1;
  while(!NRF_SPIM0-&amp;gt;EVENTS_ENDTX);     // last byte transmitted

  // Manual says disable SPI before changing pins, but actually doesn&amp;#39;t matter
  NRF_SPIM0-&amp;gt;ENABLE = 0;               // disable SPI
  // Disconnect output drive, connect 1-wire rx/tx input to MISO
  NRF_SPIM0-&amp;gt;PSEL.MOSI = 0xFFFFFFFFUL; // MOSIPIN;
  NRF_GPIO-&amp;gt;PIN_CNF[MOSIPIN] = 0;      // input pin, input buffer connected, no pull, S0S1, sense disabled
  NRF_SPIM0-&amp;gt;PSEL.MISO = MOSIPIN;      // MISOPIN;
  NRF_SPIM0-&amp;gt;ENABLE = 7;               // enable SPI
  spi3wRXBuf[0] = 99; spi3wRXBuf[1] = 98;
  NRF_SPIM0-&amp;gt;TXD.PTR = NULL;
  NRF_SPIM0-&amp;gt;TXD.MAXCNT = 0;
  NRF_SPIM0-&amp;gt;RXD.PTR = (uint32_t)spi3wRXBuf;
  NRF_SPIM0-&amp;gt;RXD.MAXCNT = 1;
  NRF_SPIM0-&amp;gt;EVENTS_ENDRX = 0;
  NRF_SPIM0-&amp;gt;TASKS_START = 1;
  while(!NRF_SPIM0-&amp;gt;EVENTS_ENDRX);    // last byte received

  NRF_SPIM0-&amp;gt;TASKS_STOP = 1;
  while(!NRF_SPIM0-&amp;gt;EVENTS_STOPPED);
  NRF_GPIO-&amp;gt;OUTSET = 1 &amp;lt;&amp;lt; CSPIN;
  while(1);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Bunch of other links via this post:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/102394/nrf52840-3-wire-spi-implementation"&gt;nrf52840-3-wire-spi-implementation&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>