|
| 1 | +/** |
| 2 | + SPI_1 and SPI_2 port example code |
| 3 | + |
| 4 | + Description: |
| 5 | + This sketch sends one byte with value 0x55 over the SPI_1 or SPI_2 port. |
| 6 | + The received byte (the answer from the SPI slave device) is stored to the <data> variable. |
| 7 | + |
| 8 | + The sketch as it is, works with SPI_1 port. For using the SPI_2 port, just |
| 9 | + un-comment all the nessesary code lines marked with <SPI_2> word. |
| 10 | + |
| 11 | + Created on 10 Jun 2015 by Vassilis Serasidis |
| 12 | + email: avrsite@yahoo.gr |
| 13 | + |
| 14 | + Using the first SPI port (SPI_1) |
| 15 | + SS <--> PA4 <--> BOARD_SPI1_NSS_PIN |
| 16 | + SCK <--> PA5 <--> BOARD_SPI1_SCK_PIN |
| 17 | + MISO <--> PA6 <--> BOARD_SPI1_MISO_PIN |
| 18 | + MOSI <--> PA7 <--> BOARD_SPI1_MOSI_PIN |
| 19 | + |
| 20 | + Using the second SPI port (SPI_2) |
| 21 | + SS <--> PB12 <--> BOARD_SPI2_NSS_PIN |
| 22 | + SCK <--> PB13 <--> BOARD_SPI2_SCK_PIN |
| 23 | + MISO <--> PB14 <--> BOARD_SPI2_MISO_PIN |
| 24 | + MOSI <--> PB15 <--> BOARD_SPI2_MOSI_PIN |
| 25 | +*/ |
| 26 | + |
| 27 | + |
| 28 | +#include <SPI.h> |
| 29 | + |
| 30 | +#define SPI1_NSS_PIN PA4 //SPI_1 Chip Select pin is PA4. You can change it to the STM32 pin you want. |
| 31 | +#define SPI2_NSS_PIN PB12 //SPI_2 Chip Select pin is PB12. You can change it to the STM32 pin you want. |
| 32 | + |
| 33 | +//SPIClass SPI_2(2); //un-comment this line in case you want to use the SPI_2 port. |
| 34 | +byte data; |
| 35 | + |
| 36 | +void setup() { |
| 37 | + SPI.begin(); //Initialize the SPI_1 port. |
| 38 | + //SPI_2.begin(); //Initialize the SPI_2 port. |
| 39 | + |
| 40 | + SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order |
| 41 | + //SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order |
| 42 | + |
| 43 | + SPI.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0 |
| 44 | + //SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0 |
| 45 | + |
| 46 | + SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed) |
| 47 | + //SPI_2.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_2 speed) |
| 48 | + |
| 49 | + pinMode(SPI1_NSS_PIN, OUTPUT); // note: this must be after the SPI.begin() for gpio control of CSN |
| 50 | + //pinMode(SPI2_NSS_PIN, OUTPUT); // note: this must be after the SPI_2.begin() for gpio control of CSN |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + digitalWrite(SPI1_NSS_PIN, LOW); // manually take CSN low for SPI_1 transmission |
| 55 | + //digitalWrite(SPI2_NSS_PIN, LOW); // manually take CSN low for SPI_2 transmission |
| 56 | + |
| 57 | + data = SPI.transfer(0x55); //Send the HEX data 0x55 over SPI-1 port and store the received byte to the <data> variable. |
| 58 | + //data = SPI_2.transfer(0x55); //Send the HEX data 0x55 over SPI-2 port and store the received byte to the <data> variable. |
| 59 | + |
| 60 | + digitalWrite(SPI1_NSS_PIN, HIGH); // manually take CSN high between spi transmissions |
| 61 | + //digitalWrite(SPI2_NSS_PIN, HIGH); // manually take CSN high between spi transmissions |
| 62 | + |
| 63 | + delayMicroseconds(10); //Delay 10 micro seconds. |
| 64 | +} |
0 commit comments