{"id":3467,"date":"2016-08-18T07:43:26","date_gmt":"2016-08-18T07:43:26","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=3467"},"modified":"2016-08-18T07:43:26","modified_gmt":"2016-08-18T07:43:26","slug":"esp8266-spi-duplex-transfers","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2016\/08\/esp8266-spi-duplex-transfers\/","title":{"rendered":"esp8266 SPI duplex transfers"},"content":{"rendered":"<p>I&#8217;ve been working with the esp8266 spi driver by MetalPhreak located <a href=\"https:\/\/github.com\/MetalPhreak\/ESP8266_SPI_Driver\">here<\/a>. One of the issues I&#8217;ve found is that duplex transfers (where you want to both send and receive at the same time) don&#8217;t work correctly. If you attempt to call spi_transaction specifying 8 dout_bits and 8 din_bits 16 clocks will be sent (IIRC a receive followed by a transmit).<\/p>\n<p>The misunderstanding is likely down to the fact that the esp8266 has very little documentation. And the SPI registers aren&#8217;t documented at all. However the Arduino guys seem to have figured it out and it works correctly in the Arduino IDE board pack for the esp8266.<\/p>\n<p>The trick is to set the SPI_USER register just so. There are 3 bits which control the direction of transfer SPI_USR_MOSI, SPI_USR_MISO and SPI_DOUTDIN (bits 27,28 and 0). In order to send and receive you need to set SPI_DOUTDIN and SPI_USR_MOSI but NOT SPI_USR_MISO. At least that&#8217;s what&#8217;s worked for me.<\/p>\n<p>I made the following modification to spi_transaction to fix this. Change the line reading:<\/p>\n<pre>\r\nif(din_bits) {SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MISO);}\r\n<\/pre>\n<p>to<\/p>\n<pre>\r\n  if((din_bits) && (dout_bits)) {\r\n    SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_DOUTDIN);\r\n  } else {\r\n    if(din_bits) {SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MISO);}\r\n  }\r\n<\/pre>\n<p>I&#8217;ll probably submit a pull request once I&#8217;ve made all the changes required for my project. But in this meantime this should work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working with the esp8266 spi driver by MetalPhreak located here. One of the issues I&#8217;ve found is that duplex transfers (where you want to both send and receive at the same time) don&#8217;t work correctly. If you attempt to call spi_transaction specifying 8 dout_bits and 8 din_bits 16 clocks will be sent (IIRC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-3467","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-TV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/3467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=3467"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/3467\/revisions"}],"predecessor-version":[{"id":3468,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/3467\/revisions\/3468"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=3467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=3467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=3467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}