Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Serial Peripheral Interface
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Bit-banging the protocol=== The [[pseudocode]] below outlines a software implementation ("[[bit-banging]]") of SPI's protocol as a master with simultaneous output and input. This pseudocode is for CPHA=0 and CPOL=0, thus SCLK is pulled low before {{Overline|SS}} is activated and bits are inputted on SCLK's rising edge while bits are outputted on SCLK's falling edge. * Initialize SCLK as low and {{Overline|SS}} as high * Pull {{Overline|SS}} low to select the slave * Loop for however many number of bytes to transfer:{{NoteTag|Peripherals may allow or require a particular number (or any number) of transfer bytes while selected, as specified in their datasheet.}} ** Initialize{{Code|byte_out|C}}with the next output byte to transmit ** Loop 8 times: *** [[Left-shift operator|Left-Shift]]{{NoteTag|Left-shifts are used because SPI normally transmits the most-significant bit first. Right-shifts could instead be used to transfer least-significant bit first.}} the next output bit from{{Code|byte_out|C}}to MOSI *** [[NOP (code)|NOP]] for the slave's [[setup time]] *** Pull SCLK high *** Left-Shift the next input bit from MISO into{{Code|byte_in|C}} *** NOP for the slave's hold time *** Pull SCLK low ** {{Code|byte_in|C}}now contains that recently-received byte and can be used as desired * Pull {{Overline|SS}} high to unselect the slave Bit-banging a slave's protocol is similar but different from above. An implementation might involve [[busy waiting]] for {{Overline|SS}} to fall or triggering an [[Interrupt handler|interrupt routine]] when {{Overline|SS}} falls, and then shifting in and out bits when the received SCLK changes appropriately for however long the transfer size is.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)