# SPI with the Arduino as the master device and the OpenMV Cam as the slave. # # Please wire up your OpenMV Cam to your Arduino like this: # # OpenMV Cam Master Out Slave In (P0) - Arduino Uno MOSI (11) # OpenMV Cam Master In Slave Out (P1) - Arduino Uno MISO (12) # OpenMV Cam Serial Clock (P2) - Arduino Uno SCK (13) # OpenMV Cam Slave Select (P3) - Arduino Uno SS (10) # OpenMV Cam Ground - Arduino Ground import pyb, ustruct text = "Hello World!\n" data = ustruct.pack(" clock is idle low. # phase = 0 -> sample data on rising clock edge, output data on falling clock edge. spi = pyb.SPI(2, pyb.SPI.SLAVE, polarity=0, phase=0) pin = pyb.Pin("P3", pyb.Pin.IN, pull=pyb.Pin.PULL_UP) print("Waiting for Arduino...") # Note that for sync up to work correctly the OpenMV Cam must be running this script before the # Arduino starts to poll the OpenMV Cam for data. Otherwise the SPI byte framing gets messed up, # and etc. So, keep the Arduino in reset until the OpenMV Cam is "Waiting for Arduino...". while(True): while(pin.value()): pass try: spi.send(data, timeout=1000) # If we failed to sync up the first time we'll sync up the next time. print("Sent Data!") # Only reached on no error. except OSError as err: pass # Don't care about errors - so pass. # Note that there are 3 possible errors. A timeout error, a general purpose error, or # a busy error. The error codes are 116, 5, 16 respectively for "err.arg[0]". while(not pin.value()): pass ################################################################################################### # Arduino Code ################################################################################################### # # #include # #define SS_PIN 10 # #define BAUD_RATE 19200 # #define CHAR_BUF 128 # # void setup() { # pinMode(SS_PIN, OUTPUT); # Serial.begin(BAUD_RATE); # SPI.begin(); # SPI.setBitOrder(MSBFIRST); # SPI.setClockDivider(SPI_CLOCK_DIV16); # SPI.setDataMode(SPI_MODE0); # delay(1000); // Give the OpenMV Cam time to bootup. # } # # void loop() { # int32_t temp = 0; # char buff[CHAR_BUF] = {0}; # digitalWrite(SS_PIN, LOW); # delay(1); // Give the OpenMV Cam some time to setup to send data. # # if(SPI.transfer(1) == 85) { // saw sync char? # SPI.transfer(&temp, 4); // get length # int zero_legnth = 4 + ((temp + 1) % 4); # if (temp) { # SPI.transfer(&buff, min(temp, CHAR_BUF)); # temp -= min(temp, CHAR_BUF); # } # while (temp--) SPI.transfer(0); // eat any remaining bytes # while (zero_legnth--) SPI.transfer(0); // eat zeros. # } # # digitalWrite(SS_PIN, HIGH); # Serial.print(buff); # delay(1); // Don't loop to quickly. # }