<?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>Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/51565/problem-with-spi-communication-read-write-for-ms5540c</link><description>Hello to all, 
 I am using nRF52840 DK with SDK15.0 and SES. To communicate with MS5540C I have used SPI master sample example but getting some incorrect results. 
 Almost 4 days I have spend to analyse this issue but still get same problem. I want to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 09 Sep 2019 10:55:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/51565/problem-with-spi-communication-read-write-for-ms5540c" /><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/208636?ContentTypeID=1</link><pubDate>Mon, 09 Sep 2019 10:55:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f0dc169-d7aa-4fd9-a510-004e52a1cc62</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi Vishal&lt;/p&gt;
&lt;p&gt;The wait_for_low_DOUT(); and wait_for_falling_edge_on_DOUT Hugh is describing should be very similar to your wait_for_spi_event(); I believe. Instead of the while loop waiting for the SPI transfer to be done, you should set it to wait for D OUT to go low. You should probably set the delay to a bit more than 35ms as well. Could you try with a 40-50 ms delay to see if that&amp;#39;s successful?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/208487?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2019 13:49:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d04a6700-4215-4393-a2cc-a5c93d43bcbd</guid><dc:creator>vishal</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If I change delay 200ms to 35ms then sensor is not getting correct value. If I add 200ms delay then it should fine and working and got corrected value of D1 and D2.&lt;/p&gt;
&lt;p&gt;What I need to do in&amp;nbsp;wait_for_low_DOUT(); and&amp;nbsp;wait_for_falling_edge_on_DOUT(); functions.&lt;/p&gt;
&lt;p&gt;Currently my program is working with added 200ms delay but is this good way.&lt;/p&gt;
&lt;p&gt;Thanks....&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/207243?ContentTypeID=1</link><pubDate>Sun, 01 Sep 2019 00:41:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:043e4658-0891-459d-9547-8268a48521b9</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;One last thing, the more I look at the timing diagrams (not as clear as I would like) the more I see CPHA 1 and CPOL 0 to match the sensor timing. In the Nordic encoding this is &lt;em&gt;NRF_DRV_SPI_MODE_1, ie.&amp;nbsp;&lt;/em&gt;not&lt;em&gt; Mode_0&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;  spi_config.mode = NRF_DRV_SPI_MODE_1; // CPOL = 0, CPHA = 1&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/207205?ContentTypeID=1</link><pubDate>Fri, 30 Aug 2019 17:02:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:743d2393-81b1-4c5f-b0f4-829218fc0b5e</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;First I would not wait for the spi event, or use such a long delay, since the 33mSec conversion starts while the spi transfer is finishing up.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;Change:
  nrf_delay_ms(200);
  wait_for_spi_event();
To:
  nrf_delay_ms(33 and a bit more); // 33 mSec conversion time
  wait_for_low_DOUT();             // DOUT goes low when data ready
or just:
  wait_for_falling_edge_on_DOUT(); // DOUT goes low when data ready&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Next the SPI transfer is already in progress, so I would suggest the first 2 bytes of received data are the bytes making up the 16-bit value in D1 (say), not the 2nd and 3rd bytes. For there to be no issues with Tx presenting unwanted &amp;#39;1&amp;#39;s while reading out Rx, which could cause erroneous operation, I would be inclined to make the transfer specifically send &amp;#39;0&amp;#39; on Tx while reading in the 2 Rx bytes&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Ensure trailing Tx bits are all &amp;#39;0&amp;#39;
spi_config.orc = 0x00;&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206994?ContentTypeID=1</link><pubDate>Fri, 30 Aug 2019 06:56:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80e7c772-dc33-4a02-b877-455356f61adb</guid><dc:creator>vishal</dc:creator><description>&lt;p&gt;Thanks for your great explanation. Now I have modified my program snippet here is below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;unsigned int read_cal_word(const uint8_t *address, const uint8_t add_len) {
  word = 0;
  memset(m_rx_buf, 0, m_length);
  reset_sensor(); /*Before read calibration word need to reset sensor module*/
  spi_xfer_done = false;
  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, address, add_len, m_rx_buf, sizeof(m_rx_buf)));
  wait_for_spi_event();
  NRF_LOG_FLUSH();
  NRF_LOG_INFO(&amp;quot;RX Buff_byte1:%X byte2:%X&amp;quot;, m_rx_buf[2], m_rx_buf[3]);
  word = (m_rx_buf[2] &amp;lt;&amp;lt; 8) | m_rx_buf[3]; /*Combine received 2 bytes from sensor*/
//  NRF_LOG_DEBUG(&amp;quot;Return Calibration word = %u&amp;quot;, word);
  return word;
}

\\ Below function use for read D1 and D2
unsigned int read_sensor_value(const bool check_add) {
  sensor_val = 0;
  static uint8_t       add_pressure[] = { 0x0F, 0x40 }; // D1 command
  static uint8_t       add_temp[] = { 0x0F, 0x20 }; // D2 command       
  uint8_t dummy_byte = 0x00;
  memset(m_rx_buf, 0, m_length);
  reset_sensor(); /*Before read calibration word need to reset sensor module*/
  spi_xfer_done = false;
  if (check_add == true) { /*Check here address of read sensor value*/
  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, add_pressure, sizeof(add_pressure), NULL, 0));
  } else {
  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, add_temp, sizeof(add_temp), NULL, 0));
  }
  nrf_delay_ms(200);
  wait_for_spi_event();
  NRF_LOG_FLUSH();
  spi_xfer_done = false;
  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, &amp;amp;dummy_byte, sizeof(dummy_byte), m_rx_buf, sizeof(m_rx_buf)));
  nrf_delay_ms(200);
  wait_for_spi_event();
  NRF_LOG_FLUSH();
  sensor_val = (m_rx_buf[1] &amp;lt;&amp;lt; 8) | m_rx_buf[2];
  NRF_LOG_DEBUG(&amp;quot;Sensor value MSB:%X LSB:%X return:[%u]&amp;quot;, m_rx_buf[1], m_rx_buf[2], sensor_val);
  return sensor_val;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;But problem is the value of D1= 58048 and D2= 63552 which is out of range of max value of sensor.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/500x240/__key/communityserver-discussions-components-files/4/max-values.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;Here is my Logs:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;lt;info&amp;gt; app: Level Detector started.
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  FE                     |.       
&amp;lt;info&amp;gt; app: RX Buff_byte1:D7 byte2:9C
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  FE 01 9F 8C            |....    
&amp;lt;info&amp;gt; app: RX Buff_byte1:9F byte2:8C
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  FE                     |.       
&amp;lt;info&amp;gt; app: RX Buff_byte1:C6 byte2:2E
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  FE 01 DA 5D            |...]    
&amp;lt;info&amp;gt; app: RX Buff_byte1:DA byte2:5D
&amp;lt;info&amp;gt; app: coefficients- C1: 27598 C2:2973 C3:873 C4:792 C5:638 C6:12
&amp;lt;info&amp;gt; app: Wake up sensor
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  21 E3 40               |!.@     
&amp;lt;info&amp;gt; app: Sensor value MSB:E3 LSB:40 return:[58176]
&amp;lt;info&amp;gt; app: compensated pressure value [D1]: 58176
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  3D F9 40               |=.@     
&amp;lt;info&amp;gt; app: Sensor value MSB:F9 LSB:40 return:[63808]
&amp;lt;info&amp;gt; app: compensated temperature value [D2]: 63808
&amp;lt;info&amp;gt; app: Compensated pressure in mbar: -669
&amp;lt;info&amp;gt; app: Water level: 0cm
&amp;lt;info&amp;gt; app: Real Temperature in C: 208
&amp;lt;info&amp;gt; app: Wake up sensor
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  21 E2 C0               |!..     
&amp;lt;info&amp;gt; app: Sensor value MSB:E2 LSB:C0 return:[58048]
&amp;lt;info&amp;gt; app: compensated pressure value [D1]: 58048
&amp;lt;info&amp;gt; app: Reset Sensor
&amp;lt;info&amp;gt; app:  Received:
&amp;lt;info&amp;gt; app:  3D F8 40               |=.@     
&amp;lt;info&amp;gt; app: Sensor value MSB:F8 LSB:40 return:[63552]
&amp;lt;info&amp;gt; app: compensated temperature value [D2]: 63552
&amp;lt;info&amp;gt; app: Compensated pressure in mbar: -1069
&amp;lt;info&amp;gt; app: Water level: 0cm
&amp;lt;info&amp;gt; app: Real Temperature in C: 207&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;How I know the measured value is corrected, because the temperature is not getting correct.&lt;/p&gt;
&lt;p&gt;Thanks....&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206941?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 16:09:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80112562-7bb4-4383-9e48-7b1e1356f8fb</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;A few suggestions, if I may. First the expected command byte 0x1D is 0001&amp;#39;1101 in binary, which corresponds with the data sheet description of &amp;quot;D1&amp;quot; backwards. However, confusingly the actual binary code in the example for &amp;quot;D1&amp;quot; is 10-bit sequence 111&amp;#39;1010&amp;#39;000. Since you are using MSB first transfers, that requires a 10-bit sequence, say 0xF4&amp;#39;0, or the 16-bit sequence 000000&amp;#39;&lt;span&gt;111&amp;#39;1010&amp;#39;000 which is 0x03, 0xD0 although the extra received data would require re-aligning. Using postfix of unwanted clocks rather than prefix would give&amp;nbsp;111&amp;#39;1010&amp;#39;000&amp;#39;000000 or 0xF4, 0x00. Extra 0x00 bytes would read out the data.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;D2 would then be&amp;nbsp;the 16-bit sequence 000000&amp;#39;111&amp;#39;1001&amp;#39;000 which is 0x03, 0xC8, or postfix&amp;nbsp;111&amp;#39;1001&amp;#39;000&amp;#39;000000 which is 0xF2, 0x00.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The 21-bit Reset sequence would be 101010101010101000000&amp;#39;000, which with MSB First is the 3-byte sequence 0xAA, 0xAA, 0x00. This could perhaps better be preceded with the 3 unwanted &amp;#39;0&amp;#39; bits, giving 000&amp;#39;&lt;span&gt;101010101010101000000 or 0x15, 0x55, 0x40. This is what you are using I see.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I think my starting tests would therefore use the following:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint8_t reset_seq[]     = { 0x15, 0x55, 0x40 };       // 000&amp;#39;101010101010101000000 or 0x15, 0x55, 0x40
static uint8_t address_word1[] = { 0xF4, 0x00, 0x00, 0x00 }; // 111&amp;#39;1010&amp;#39;000&amp;#39;000000 or 0xF4, 0x00.
static uint8_t address_word2[] = { 0xF2, 0x00, 0x00, 0x00 }; // 111&amp;#39;1001&amp;#39;000&amp;#39;000000 or 0xF2, 0x00
or
static uint8_t reset_seq[]     = { 0x15, 0x55, 0x40 };       // 000&amp;#39;101010101010101000000 or 0x15, 0x55, 0x40
static uint8_t address_word1[] = { 0x0F, 0x40, 0x00, 0x00 }; // 0000&amp;#39;111&amp;#39;1010&amp;#39;000&amp;#39;00
static uint8_t address_word2[] = { 0x0F, 0x20, 0x00, 0x00 }; // 0000&amp;#39;111&amp;#39;1001&amp;#39;000&amp;#39;00

So not the same as your code
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;There is one other issue worth noting, which will break the code if anything other than 4-byte data arrays are used. Have a look at these tests, which pass:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint8_t address_word1[] = { 0x1D, 0x50, 0x00, 0x00 };       /**&amp;lt; TX buffer. */
const uint8_t *address_p = address_word1;
STATIC_ASSERT(sizeof(address_p) == 4, &amp;quot;Misunderstanding on meaning of sizeofaddress_p)&amp;quot;);
STATIC_ASSERT(sizeof(address_p) == 4, &amp;quot;Misunderstanding on meaning of sizeof(address_p)&amp;quot;);
STATIC_ASSERT(sizeof(address_word1) == 4, &amp;quot;Error in sizeof(address_word1)&amp;quot;);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now try using something other than a 4-byte command:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint8_t address_word5[] = { 0x1D, 0xA0, 0x00, 0x00, 0x99 };       /**&amp;lt; TX buffer. */
const uint8_t *address_p5 = address_word5;
STATIC_ASSERT(sizeof(address_p5) == 4, &amp;quot;Misunderstanding on meaning of sizeofaddress_p)&amp;quot;);
STATIC_ASSERT(sizeof(address_p5) == 4, &amp;quot;Misunderstanding on meaning of sizeof(address_p)&amp;quot;);
STATIC_ASSERT(sizeof(address_word5) == 5, &amp;quot;Error in  sizeof(address_word5)&amp;quot;);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;These tests also pass .. but in the code there is a bug:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;unsigned int read_cal_word(const uint8_t *address) {
  spi_xfer_done = false;
//blah-blah
  APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, address, sizeof(address), m_rx_buf, sizeof(m_rx_buf)));&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;em&gt;sizeof(address)&lt;/em&gt; is always 4 even if the passed array is (say) 5.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;(edited to correct typos)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206916?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 14:13:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:027bf57f-1fa3-48f4-952b-addf911aa034</guid><dc:creator>vishal</dc:creator><description>[quote userid="75734" url="~/f/nordic-q-a/51565/problem-with-spi-communication-read-write-for-ms5540c/206909"]Can you point to where in the sensor&amp;#39;s datasheet that is stated?[/quote]
&lt;p&gt;I have attached screen shot below:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/500x240/__key/communityserver-discussions-components-files/4/0042.datasheet.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;Also the sensor does not have SS_pin&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/500x240/__key/communityserver-discussions-components-files/4/sensor_5F00_block.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;Thanks......&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206909?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 13:58:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c321e5af-73b3-4995-8700-3907b928892a</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Can you point to where in the sensor&amp;#39;s datasheet that is stated? I&amp;#39;m not able to find it. In any case, I think you should try using the SS_Pin to see if it helps, the SS_Pin usually has to be handled somehow AFAIK.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206874?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 12:20:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d06d3f59-6d9c-42a9-8746-b203fb9e38e4</guid><dc:creator>vishal</dc:creator><description>&lt;p&gt;Okay please&amp;nbsp;set it in public mode to get fast solution.&lt;/p&gt;
&lt;p&gt;The sensor datasheet said no need to handle or use SS_Pin so that&amp;#39;s why I have comment it.&lt;/p&gt;
&lt;p&gt;Thanks for your quick response..!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem with SPI communication read/write for MS5540C.</title><link>https://devzone.nordicsemi.com/thread/206873?ContentTypeID=1</link><pubDate>Thu, 29 Aug 2019 11:51:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:182dc5b8-1b2a-4c16-b24b-ca5bd6221465</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi Vishal&lt;/p&gt;
&lt;p&gt;First of all, I think this case would be better to post publically, as there are multiple people in the community with experience with different sensors and such. So if you want, I can set it to public mode for you.&lt;/p&gt;
&lt;p&gt;I see that you&amp;#39;ve commented out the SS_PIN in your configuration, but I can&amp;#39;t see where you handle it manually, please indulge me.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>