spi_documentation_2
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| spi_documentation_2 [2022/05/30 13:46] – [Arduino] serge | spi_documentation_2 [2022/05/31 07:45] (Version actuelle) – serge | ||
|---|---|---|---|
| Ligne 4: | Ligne 4: | ||
| </ | </ | ||
| - | **Cette page est inspirée de [[http:// | + | =====Une ESP32 n'est pas une carte Arduino===== |
| + | **Les sketchs ci-dessous ne fonctionnent pas avec une ESP32** | ||
| + | =====Exemple 1===== | ||
| + | https:// | ||
| + | Raspberry Pi (master) Arduino Uno (slave) SPI communication with WiringPi | ||
| - | =====Raspberry===== | + | <code csharp> |
| - | <code python | + | #include < |
| + | void setup() { | ||
| + | // have to send on master in, *slave out* | ||
| + | pinMode(MISO, | ||
| + | // turn on SPI in slave mode | ||
| + | SPCR |= _BV(SPE); | ||
| + | // turn on interrupts | ||
| + | SPI.attachInterrupt(); | ||
| + | } | ||
| + | // SPI interrupt routine | ||
| + | ISR (SPI_STC_vect) | ||
| + | { | ||
| + | byte c = SPDR; | ||
| + | SPDR = c+10; | ||
| + | } // end of interrupt service routine (ISR) for SPI | ||
| + | |||
| + | void loop () { } | ||
| + | </ | ||
| + | |||
| + | **Raspberry Pi avec wiringPi** | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #define SPI_CHANNEL 0 | ||
| + | #define SPI_CLOCK_SPEED 1000000 | ||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | int fd = wiringPiSPISetupMode(SPI_CHANNEL, | ||
| + | if (fd == -1) { | ||
| + | std::cout << " | ||
| + | return -1; | ||
| + | } | ||
| + | std::cout << "SPI communication successfully setup.\n"; | ||
| + | |||
| + | unsigned char buf[2] = { 23, 0 }; | ||
| + | wiringPiSPIDataRW(SPI_CHANNEL, | ||
| + | std::cout << "Data returned: " << +buf[1] << " | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | Voir le lien pour la compilation de wiringPi et de ce script | ||
| + | |||
| + | =====Exemple 2===== | ||
| + | Tiré de [[http:// | ||
| + | Arduino: SPCR et SPDR pas défini ! | ||
| + | ====Raspberry==== | ||
| + | <code python> | ||
| import spidev | import spidev | ||
| import time | import time | ||
| - | |||
| spi_bus = 0 | spi_bus = 0 | ||
| spi_device = 0 | spi_device = 0 | ||
| Ligne 18: | Ligne 67: | ||
| spi.open(spi_bus, | spi.open(spi_bus, | ||
| spi.max_speed_hz = 1000000 | spi.max_speed_hz = 1000000 | ||
| - | |||
| # Send a null byte to check for value | # Send a null byte to check for value | ||
| send_byte = 0x80 | send_byte = 0x80 | ||
| Ligne 28: | Ligne 76: | ||
| print (" | print (" | ||
| quit() | quit() | ||
| - | |||
| - | |||
| # Data is sent as single bytearray | # Data is sent as single bytearray | ||
| # lower 4 bits = pin number | # lower 4 bits = pin number | ||
| Ligne 44: | Ligne 90: | ||
| pin += 0x20 | pin += 0x20 | ||
| return pin | return pin | ||
| - | |||
| # read analog pin | # read analog pin | ||
| def req_read_analog (pin): | def req_read_analog (pin): | ||
| Ligne 50: | Ligne 95: | ||
| return 0xff | return 0xff | ||
| return pin + 0x10 | return pin + 0x10 | ||
| - | |||
| # read digital pin | # read digital pin | ||
| def req_read_digital (pin): | def req_read_digital (pin): | ||
| Ligne 56: | Ligne 100: | ||
| return 0xff | return 0xff | ||
| return pin | return pin | ||
| - | |||
| while True: | while True: | ||
| # Turn LEDs on | # Turn LEDs on | ||
| Ligne 89: | Ligne 132: | ||
| </ | </ | ||
| - | + | ====Arduino==== | |
| - | =====Arduino===== | + | <code csharp> |
| - | <code csharp | + | |
| #include < | #include < | ||
| - | |||
| // track if digital pins are set as input or output (saves reading registers) | // track if digital pins are set as input or output (saves reading registers) | ||
| // Pins 0 to 9 can be used, pin 10 can be used for SS (but can be used if that's not required) | // Pins 0 to 9 can be used, pin 10 can be used for SS (but can be used if that's not required) | ||
| bool pin_output[11]; | bool pin_output[11]; | ||
| - | |||
| void setup() | void setup() | ||
| { | { | ||
| Ligne 105: | Ligne 145: | ||
| pin_output[i] = false; | pin_output[i] = false; | ||
| } | } | ||
| - | | ||
| // Set the Main in Secondary Out as an output | // Set the Main in Secondary Out as an output | ||
| pinMode(MISO, | pinMode(MISO, | ||
| Ligne 112: | Ligne 151: | ||
| SPCR |= _BV(SPE); | SPCR |= _BV(SPE); | ||
| } | } | ||
| - | |||
| - | |||
| void loop () | void loop () | ||
| - | { | + | { char print_text [50]; |
| - | | + | |
| byte in_byte; | byte in_byte; | ||
| // SPIF indicates transmission complete (byte received) | // SPIF indicates transmission complete (byte received) | ||
| Ligne 144: | Ligne 180: | ||
| { | { | ||
| SPDR = 0xFF; | SPDR = 0xFF; | ||
| - | } | + | }}} |
| - | + | ||
| - | | + | |
| - | } | + | |
| byte set_digital_pin (byte address, bool high_value) | byte set_digital_pin (byte address, bool high_value) | ||
| Ligne 174: | Ligne 207: | ||
| pin_output[address] = false; | pin_output[address] = false; | ||
| } | } | ||
| - | | ||
| return digitalRead (address); | return digitalRead (address); | ||
| } | } | ||
| - | |||
| // As full byte is used for value there is no invalid value | // As full byte is used for value there is no invalid value | ||
| // If invalid then returns 0x00 | // If invalid then returns 0x00 | ||
spi_documentation_2.1653918361.txt.gz · Dernière modification : de serge
