The fourth video explains how to program an I2C and SPI bus. These are necessary to communicate with external components on the same bus.
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.