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:45] – [Raspberry] 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) |
| + | // 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]; | ||
| void setup() | void setup() | ||
| { | { | ||
| + | // by default all pins are inputs | ||
| + | for (int i=0; i<11; i++) | ||
| + | { | ||
| + | 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 102: | Ligne 151: | ||
| SPCR |= _BV(SPE); | SPCR |= _BV(SPE); | ||
| } | } | ||
| - | |||
| - | // The following code is then used to receive a byte and then set the data register to return a byte to the master controller device. | ||
| 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 111: | Ligne 158: | ||
| { | { | ||
| in_byte = SPDR; | in_byte = SPDR; | ||
| - | // Handle the input code here | + | // if no action bit sent then return same |
| - | // Set return_val | + | if (in_byte & 0x80) SPDR = in_byte; |
| - | SPDR = return_val; | + | // if write set |
| + | else if (in_byte & 0x20) | ||
| + | { | ||
| + | // set digital pin output - use mask to extract pin and output | ||
| + | if (set_digital_pin (in_byte & 0x0F, in_byte & 0x40) != 0xFF) SPDR = in_byte; | ||
| + | } | ||
| + | // If value is only pin numbers then digital read | ||
| + | | ||
| + | { | ||
| + | byte return_val = read_digital_pin (in_byte); | ||
| + | | ||
| + | } | ||
| + | else if ((in_byte & 0x10) && !(in_byte & 0x20)) | ||
| + | { | ||
| + | byte return_val = read_analog_pin (in_byte & 0x0F); | ||
| + | SPDR = return_val; | ||
| + | } | ||
| + | else // Otherwise an error - return 0xff | ||
| + | { | ||
| + | SPDR = 0xFF; | ||
| + | }}} | ||
| + | |||
| + | byte set_digital_pin (byte address, bool high_value) | ||
| + | { | ||
| + | if (address < 0 || address > 10) return 0xFF; | ||
| + | // Is it currently set as output - if not then set to output | ||
| + | if (!pin_output[address]) | ||
| + | { | ||
| + | pinMode(address, | ||
| + | pin_output[address] = true; | ||
| } | } | ||
| + | // set to low if 0, otherwise 1 | ||
| + | if (high_value == 0) digitalWrite(address, | ||
| + | else digitalWrite (address, HIGH); | ||
| + | } | ||
| + | |||
| + | // Returns 1 for high and 0 for low | ||
| + | // If invalid then returns 0xff | ||
| + | byte read_digital_pin (byte address) | ||
| + | { | ||
| + | if (address < 0 || address > 10) return 0xFF; | ||
| + | // if currently output then change to input | ||
| + | if (pin_output[address]) | ||
| + | { | ||
| + | pinMode(address, | ||
| + | pin_output[address] = false; | ||
| + | } | ||
| + | return digitalRead (address); | ||
| + | } | ||
| + | // As full byte is used for value there is no invalid value | ||
| + | // If invalid then returns 0x00 | ||
| + | byte read_analog_pin (byte address) | ||
| + | { | ||
| + | if (address < 0 || address > 5) return 0x00; | ||
| + | int analog_val = analogRead (address); | ||
| + | // Analog value is between 0 and 1024 - div by 4 for byte | ||
| + | return (analog_val / 4); | ||
| + | } | ||
| </ | </ | ||
| {{tag> communication sb spi }} | {{tag> communication sb spi }} | ||
spi_documentation_2.1653918321.txt.gz · Dernière modification : de serge
