Easy Embedded Programming: How to Program an I2C and SPI bus

Published By

The fourth video explains how to program an I2C and SPI bus. These are necessary to communicate with external components on the same bus.

To demonstrate the SPI, the SCB component is also used but configured as SPI this time. The L6474 datasheet gives all the specifications to configure the bus. It is said that the SDI is sampled on the rising edges of the CK and the SDO is latched on the falling edges of the CK. In SPI specification that is translated CPHA = 1 and CPOL = 1. MSB must be first and it’s a standard Motorola protocol. After each byte transmission, the chip select must be rised high that is the case.

This configuration is entered in the GUI that pops up when clicking on the SPI component. The purpose of this exercise is to read the L6474 configuration register. It has the address 0x18 and a default value of 0x2E88. To get this value, we need to send a GetParam command with the binary value bx0011 1000 = 0x38. The protocol requires to send the GatParam command followed by 2 NOP functions to receive 2 bytes of data.


 Before writing code, pin out must be configured. Following arduino rules, the SPI pins are port 0, pins 0, 1, 2 and 3. The L6474 must also be reset, and the pin is on port 0 pin 5.


Using the SPI APIs generated by the PSOC, the program is again simple. After resetting the device, the SPI block is started. A function, setUpDefaultValuesL6474 is called to send the command GetParam at address 0x18 followed by 2 NOP functions. SPIM_SendCommandPacket is called with the buffer {0x38, 0x00, 0x00} and put in an array to be transferred thanks to the function SPIM_spiUartPutArray. As the number of bytes sent is equal to the number of bytes received, once the received buffer size is to this number, we know we received all the data. This data is then extracted and the buffer is cleared.
 

A great advantage of arduino shields is the easy access to all the pins. A logic analyzer can sniff exchange packets and save a lot of time in the debug phase. As shown below, 0x38 was sent, and two packets, respectively, 0x2E and 0x88 were received. . The SPI bus works as expected and the PSOC communicated with the motor driver L6474.

 



Latest Videos

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here