title: How we can read 4 Bytes with microcontroller and SPI communication
body: <p>I want to transfer and get data from my microcontroller to my SPI memory chip with 4 Bytes (256 octets), i developped my code below, i want to write some Bytes then read it from Read function, but i get different value in 4 Bytes ( exemple i send 0x10011111 i get A100A1F0) </p>
  
  <p>In the datasheet of the Memory chip, there are no information about Page transfer (256 bits) and the transfer of data is always in double Byte ( 1 word = 16 bits) </p>
  
  <p><strong>this is my Code for write 4 Bytes:</strong> </p>
  
  <pre><code>  void ecrire4OctetsFRAM(uint32_t const addressRegister, uint32_t const fourBytes, uint8_t const slaAddress)
  {
      if (fourBytes != lire4OctetsFRAM(addressRegister, slaAddress))
      {
          if(addressRegister%4)
          {
              ecrire1OctetFRAM(addressRegister+3,(uint32_t) (fourBytes&gt;&gt; 24),FRAM);
              ecrire1OctetFRAM(addressRegister+2,(uint32_t) (fourBytes&gt;&gt; 16),FRAM);
              ecrire1OctetFRAM(addressRegister+1,(uint32_t) (fourBytes&gt;&gt; 8),FRAM);
              ecrire1OctetFRAM(addressRegister,  (uint32_t) (fourBytes)    ,FRAM);
  
  
  
  
          }
          else
          {
              uint8_t tries(0);
              do 
              {
                  error = 0;
                  positionnerPointeur(addressRegister, slaAddress);
                  if (error) continue;
                  transfer((uint8_t) (fourBytes));
                  if (error) continue;
                  transfer((uint8_t) (fourBytes&gt;&gt; 8));
                  if (error) continue;
                  transfer((uint8_t) (fourBytes&gt;&gt;16));
                  if (error) continue;
                  transfer((uint8_t) (fourBytes&gt;&gt;24));
                  release();
              } while (error &amp;&amp; ++tries &lt; MAX_TRIES);
              error = (tries &gt;= MAX_TRIES)? TW_MAX_TRIES: error;
          }
      }
  }
  </code></pre>
  
  <p><strong>and this is for read</strong> </p>
  
  <pre><code>uint32_t    lire4OctetsFRAM  (uint32_t const addressregister, uint8_t const slaveadress ){
  
  
  uint32_t fourBytes(0);
  uint8_t tries(0);
  //**
  do
      {   error = 0;
      positionnerPointeur(addressregister, slaveadress);
      select();
      transfer(READ);
  
      fourBytes = transfer((uint32_t) ((addressregister &gt;&gt;  24) &amp; 0xFFFF));
      fourBytes += transfer((uint32_t) ((addressregister &gt;&gt; 16) &amp; 0xFF));
      fourBytes += transfer((uint32_t) ((addressregister &gt;&gt;   8) &amp; 0xFF));
  
      fourBytes +=transfer((uint32_t) ( addressregister &amp; 0xFF));
      transfer(0);
      fourBytes = SPDR; 
      fourBytes += (fourBytes &lt;&lt; 24) ;
      fourBytes += (fourBytes &lt;&lt; 16);
      fourBytes = (fourBytes &lt;&lt;  8);
  transfer(0);
      fourBytes += SPDR; 
      release();
  }
  while (error &amp;&amp; ++tries &lt; MAX_TRIES);
  error = (tries &gt;= MAX_TRIES)? TW_MAX_TRIES: error;
  return fourBytes ; 
  }
  </code></pre>
  
  <p>Positionnerpointeur is a function to point a bit address ( pointer). </p>