![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
FINAL PROJECT isn't it
Fork of ELEC351 by
SPI.cpp@56:bc5345bc6650, 2018-07-16 (annotated)
- Committer:
- thomasmorris
- Date:
- Mon Jul 16 10:51:47 2018 +0000
- Revision:
- 56:bc5345bc6650
- Parent:
- 53:71f59e195f06
- Child:
- 57:aba1296e51b1
Moves added to serial;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 53:71f59e195f06 | 1 | #include "SPI.hpp" |
thomasmorris | 53:71f59e195f06 | 2 | //File for SPI communication |
thomasmorris | 53:71f59e195f06 | 3 | |
thomasmorris | 56:bc5345bc6650 | 4 | int SPI_RX_DATA = 0; |
thomasmorris | 53:71f59e195f06 | 5 | |
thomasmorris | 53:71f59e195f06 | 6 | void SPI_INIT() |
thomasmorris | 53:71f59e195f06 | 7 | { |
thomasmorris | 53:71f59e195f06 | 8 | |
thomasmorris | 53:71f59e195f06 | 9 | |
thomasmorris | 53:71f59e195f06 | 10 | /* |
thomasmorris | 53:71f59e195f06 | 11 | MOSI = Blue |
thomasmorris | 53:71f59e195f06 | 12 | MISO = White |
thomasmorris | 53:71f59e195f06 | 13 | SCLK = Yellow |
thomasmorris | 53:71f59e195f06 | 14 | Slave Select = Green |
thomasmorris | 53:71f59e195f06 | 15 | |
thomasmorris | 53:71f59e195f06 | 16 | */ |
thomasmorris | 53:71f59e195f06 | 17 | |
thomasmorris | 53:71f59e195f06 | 18 | |
thomasmorris | 53:71f59e195f06 | 19 | // Chip must be deselected |
thomasmorris | 53:71f59e195f06 | 20 | cs = 1; //Active Low |
thomasmorris | 53:71f59e195f06 | 21 | |
thomasmorris | 53:71f59e195f06 | 22 | // Setup the spi for 8 bit data, high steady state clock, |
thomasmorris | 53:71f59e195f06 | 23 | // second edge capture, with a 1MHz clock rate |
thomasmorris | 53:71f59e195f06 | 24 | spi.format(16,0); // 8 Data bits phase 0 polarity 0 |
thomasmorris | 53:71f59e195f06 | 25 | spi.frequency(1000000);//Output clock frequency 1Mhz |
thomasmorris | 53:71f59e195f06 | 26 | |
thomasmorris | 53:71f59e195f06 | 27 | // Select the device by seting chip select low |
thomasmorris | 53:71f59e195f06 | 28 | //cs = 0;//This is active after spi is set up |
thomasmorris | 53:71f59e195f06 | 29 | |
thomasmorris | 53:71f59e195f06 | 30 | // Send 0x8f, the command to read the WHOAMI register |
thomasmorris | 53:71f59e195f06 | 31 | //spi.write(0x8F); |
thomasmorris | 53:71f59e195f06 | 32 | |
thomasmorris | 53:71f59e195f06 | 33 | // Send a dummy byte to receive the contents of the WHOAMI register |
thomasmorris | 53:71f59e195f06 | 34 | //int whoami = spi.write(0x00); |
thomasmorris | 53:71f59e195f06 | 35 | //printf("WHOAMI register = 0x%X\n", whoami); |
thomasmorris | 53:71f59e195f06 | 36 | // Deselect the device |
thomasmorris | 53:71f59e195f06 | 37 | //cs = 1; |
thomasmorris | 56:bc5345bc6650 | 38 | } |
thomasmorris | 56:bc5345bc6650 | 39 | |
thomasmorris | 56:bc5345bc6650 | 40 | |
thomasmorris | 56:bc5345bc6650 | 41 | void SPI_INTERFACE_SERIAL() |
thomasmorris | 56:bc5345bc6650 | 42 | { |
thomasmorris | 56:bc5345bc6650 | 43 | //pc.printf("SPI Test \n"); |
thomasmorris | 56:bc5345bc6650 | 44 | Thread::wait(1000); |
thomasmorris | 56:bc5345bc6650 | 45 | cs= 0; |
thomasmorris | 56:bc5345bc6650 | 46 | SPI_RX_DATA = spi.write(0xF0); |
thomasmorris | 56:bc5345bc6650 | 47 | wait_us(3); |
thomasmorris | 56:bc5345bc6650 | 48 | cs= 1; |
thomasmorris | 56:bc5345bc6650 | 49 | colour_data = SPI_RX_DATA; |
thomasmorris | 56:bc5345bc6650 | 50 | if(Log_Value == 1){pc.printf("Received data = %d\n", SPI_RX_DATA);} |
thomasmorris | 56:bc5345bc6650 | 51 | Thread::wait(1000); |
thomasmorris | 53:71f59e195f06 | 52 | } |