Thomas Morris
/
PROJ324_Final
Proj 324 Final
Fork of ELEC351_Group_T by
SPI.cpp@57:aba1296e51b1, 2018-08-15 (annotated)
- Committer:
- thomasmorris
- Date:
- Wed Aug 15 21:34:59 2018 +0000
- Revision:
- 57:aba1296e51b1
- Parent:
- 56:bc5345bc6650
Final Version
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 | 57:aba1296e51b1 | 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 | 57:aba1296e51b1 | 43 | /* |
thomasmorris | 56:bc5345bc6650 | 44 | //pc.printf("SPI Test \n"); |
thomasmorris | 56:bc5345bc6650 | 45 | Thread::wait(1000); |
thomasmorris | 56:bc5345bc6650 | 46 | cs= 0; |
thomasmorris | 56:bc5345bc6650 | 47 | SPI_RX_DATA = spi.write(0xF0); |
thomasmorris | 56:bc5345bc6650 | 48 | wait_us(3); |
thomasmorris | 56:bc5345bc6650 | 49 | cs= 1; |
thomasmorris | 56:bc5345bc6650 | 50 | colour_data = SPI_RX_DATA; |
thomasmorris | 56:bc5345bc6650 | 51 | if(Log_Value == 1){pc.printf("Received data = %d\n", SPI_RX_DATA);} |
thomasmorris | 57:aba1296e51b1 | 52 | pc.printf("Received data = %d\n", SPI_RX_DATA); |
thomasmorris | 56:bc5345bc6650 | 53 | Thread::wait(1000); |
thomasmorris | 57:aba1296e51b1 | 54 | */ |
thomasmorris | 53:71f59e195f06 | 55 | } |