{"id":1453,"date":"2023-09-18T14:33:51","date_gmt":"2023-09-18T13:33:51","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=1453"},"modified":"2023-09-21T10:42:49","modified_gmt":"2023-09-21T09:42:49","slug":"spi-master","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic32\/xc32\/spi\/spi-master","title":{"rendered":"SPI Master"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">SPI Mode<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Mode  CPOL  CPHA\n0     0     0     Clock idle low, data is clocked in on rising edge, output data (change) on falling edge\n1     0     1     Clock idle low, data is clocked in on falling edge, output data (change) on rising edge\n2     1     0     Clock idle high, data is clocked in on falling edge, output data (change) on rising edge\n3     1     1     Clock idle high, data is clocked in on rising edge, output data (change) on falling edge<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using SPI bus master<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/IN YOUR INITIALSE CODE\n\t\/\/----- SETUP SPI 1 -----\n\t\/\/Used for: \n\t\/\/Setup SPI using MCC:\n\t\/\/\tSetup SPI:\n\t\/\/\t\t(The following configures port for SPI Master Mode 3)\n\t\/\/\t\tMaster Mode\n\t\/\/\t\tClock Polarity Select Bit: Idle state is high, active state is low\n\t\/\/\t\tSPI Clock Edge Select Bit: Serial output data changesa on transition from Idle clock state to active clock state\n\t\/\/\t\tSPI Data Input Sample Phase Bit: Input data sampled at middle of data output time\n\t\/\/\t\tMaster Mode Slave Select Enable Bit: Slave select SPI support is disabled\n\t\/\/\t\tData Width: 8-bit\n\t\/\/\t\tMaster Clock Enalbe Bit: PBCLK2 is used by the Baud Rate Generator\n\t\/\/\t\tBaud Rate in Hz: 1,000,000\n\t\/\/\t\tDummy Data: 0xFF\n\t\/\/\tSetup interrupts:\n\t\/\/\t\tEnable SPI1 Fault Interrupt = Can be left set to Off\n\t\/\/\t\tEnable SPI1 Receive Done Interrupt = Will already be set to Enabled\n\t\/\/\t\tEnable SPI1 Transmit Done Interrupt: Will already be set to Enabled\n\t\/\/\t\tPriority (set for all): 1\t\/\/&lt;&lt;&lt;Set as required for your application\n\t\/\/\t\tSubpriority (set for all): 0\t\/\/&lt;&lt;&lt;Set as required for your application\n\n\t\/\/SPI1_CallbackRegister(SPI1_MyEventHandler,(uintptr_t)NULL);\t\t\/\/Register my callback function to be called when the SPI operation completes (optional)\n\n\n\/\/HOW TO PERFORM A WRITE READ OPERATION\n\tuint8_t RxData&#91;3];\t\t\t\t\t\t\t\t\t\/\/Can be uint8_t, uint16_t or uint32_t depending on your SPI port Data Width setting\n\tuint8_t TxData&#91;3] = {0xff, 0xff, 0xff};\t\t\t\t\/\/Can be uint8_t, uint16_t or uint32_t depending on your SPI port Data Width setting\n\tif (SPI1_WriteRead(&amp;TxData&#91;0], 3, &amp;RxData&#91;0], 3))\t\/\/&lt;&lt;&lt; RxData or TxData can be set as NULL and length to 0 if you don't care about the rx or tx side of the data transfer\n\t{\n\t\t\/\/WRITE READ STARTED\n\n\t\twhile (SPI1_IsBusy())\n\t\t\t;\n\n\t\t\/\/WRITE READ COMPLETED\n\n\n\t}\n\n\n\/\/*************************************************\n\/\/*************************************************\n\/\/********** SPI1 OPERATION COMPLETE IRQ **********\n\/\/*************************************************\n\/\/*************************************************\n\/*\nvoid SPI1_MyEventHandler (uintptr_t context)\n{\n\t\n \n}\n*\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SPI Mode Using SPI bus master<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[],"class_list":["post-1453","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=1453"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1453\/revisions"}],"predecessor-version":[{"id":1472,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1453\/revisions\/1472"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=1453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=1453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=1453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}