diff options
| -rw-r--r-- | comm/arduino_binairy_decoder/arduino_binairy_decoder.ino (renamed from spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino) | 0 | ||||
| -rw-r--r-- | comm/deg_to_byte.py (renamed from spi_comm/deg_to_byte.py) | 0 | ||||
| -rw-r--r-- | comm/i2c/arduino_i2c_slave_1.py | 98 | ||||
| -rw-r--r-- | comm/i2c/i2c_arduino_master/i2c_arduino_master.ino | 36 | ||||
| -rw-r--r-- | comm/spi/arduino_spi_master/arduino_spi_master.ino | 34 | ||||
| -rw-r--r-- | comm/spi/arduino_spi_slave_1.py | 102 | ||||
| -rw-r--r-- | comm/uart/arduino_uart/arduino_uart.ino | 14 | ||||
| -rw-r--r-- | comm/uart/arduino_uart_1.py | 42 | ||||
| -rw-r--r-- | spi_comm/arduino_master_read/arduino_master_read.ino | 26 | ||||
| -rw-r--r-- | spi_comm/arduino_slave_read/arduino_slave_read.ino | 30 | ||||
| -rw-r--r-- | spi_comm/arduino_slave_write/arduino_slave_write.ino | 34 | ||||
| -rw-r--r-- | spi_comm/openmv_slave/spi_com_master.py | 36 |
12 files changed, 326 insertions, 126 deletions
diff --git a/spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino b/comm/arduino_binairy_decoder/arduino_binairy_decoder.ino index eb257e5..eb257e5 100644 --- a/spi_comm/arduino_binairy_decoder/arduino_binairy_decoder.ino +++ b/comm/arduino_binairy_decoder/arduino_binairy_decoder.ino diff --git a/spi_comm/deg_to_byte.py b/comm/deg_to_byte.py index 196d4a5..196d4a5 100644 --- a/spi_comm/deg_to_byte.py +++ b/comm/deg_to_byte.py diff --git a/comm/i2c/arduino_i2c_slave_1.py b/comm/i2c/arduino_i2c_slave_1.py new file mode 100644 index 0000000..5957c5b --- /dev/null +++ b/comm/i2c/arduino_i2c_slave_1.py @@ -0,0 +1,98 @@ +# This work is licensed under the MIT license. +# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. +# https://github.com/openmv/openmv/blob/master/LICENSE +# +# I2C 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 I2C Data (P5) - Arduino Uno Data (A4) +# OpenMV Cam Master I2C Clock (P4) - Arduino Uno Clock (A5) +# OpenMV Cam Ground - Arduino Ground + +import pyb +import ustruct + +text = "Hello World!\n" +data = ustruct.pack("<%ds" % len(text), text) +# Use "ustruct" to build data packets to send. +# "<" puts the data in the struct in little endian order. +# "%ds" puts a string in the data stream. E.g. "13s" for "Hello World!\n" (13 chars). +# See https://docs.python.org/3/library/struct.html + +# READ ME!!! +# +# Please understand that when your OpenMV Cam is not the I2C master it may miss responding to +# sending data as a I2C slave no matter if you call "i2c.send()" in an interrupt callback or in the +# main loop below. When this happens the Arduino will get a NAK and have to try reading from the +# OpenMV Cam again. Note that both the Arduino and OpenMV Cam I2C drivers are not good at getting +# unstuck after encountering any I2C errors. On the OpenMV Cam and Arduino you can recover by +# de-initing and then re-initing the I2C peripherals. + +# The hardware I2C bus for your OpenMV Cam is always I2C bus 2. +bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) +bus.deinit() # Fully reset I2C device... +bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) +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 I2C byte framing gets messed up, +# and etc. So, keep the Arduino in reset until the OpenMV Cam is "Waiting for Arduino...". + +while True: + try: + bus.send( + ustruct.pack("<h", len(data)), timeout=10000 + ) # Send the len first (16-bits). + try: + bus.send(data, timeout=10000) # Send the data second. + 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]". + 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]". + +################################################################################################### +# Arduino Code +################################################################################################### +# +# #include <Wire.h> +# #define BAUD_RATE 19200 +# #define CHAR_BUF 128 +# +# void setup() { +# Serial.begin(BAUD_RATE); +# Wire.begin(); +# delay(1000); // Give the OpenMV Cam time to bootup. +# } +# +# void loop() { +# int32_t temp = 0; +# char buff[CHAR_BUF] = {0}; +# +# Wire.requestFrom(0x12, 2); +# if(Wire.available() == 2) { // got length? +# +# temp = Wire.read() | (Wire.read() << 8); +# delay(1); // Give some setup time... +# +# Wire.requestFrom(0x12, temp); +# if(Wire.available() == temp) { // got full message? +# +# temp = 0; +# while(Wire.available()) buff[temp++] = Wire.read(); +# +# } else { +# while(Wire.available()) Wire.read(); // Toss garbage bytes. +# } +# } else { +# while(Wire.available()) Wire.read(); // Toss garbage bytes. +# } +# +# Serial.print(buff); +# delay(1); // Don't loop to quickly. +# } diff --git a/comm/i2c/i2c_arduino_master/i2c_arduino_master.ino b/comm/i2c/i2c_arduino_master/i2c_arduino_master.ino new file mode 100644 index 0000000..ff960ed --- /dev/null +++ b/comm/i2c/i2c_arduino_master/i2c_arduino_master.ino @@ -0,0 +1,36 @@ +#include <Wire.h> +#define BAUD_RATE 19200 +#define CHAR_BUF 128 + +void setup() { + Serial.begin(BAUD_RATE); + Wire.begin(); + delay(1000); // Give the OpenMV Cam time to bootup. +} + +void loop() { + int32_t temp = 0; + char buff[CHAR_BUF] = {0}; + + Wire.requestFrom(0x12, 2); + if(Wire.available() == 2) { // got length? + + temp = Wire.read() | (Wire.read() << 8); + delay(1); // Give some setup time... + + Wire.requestFrom(0x12, temp); + if(Wire.available() == temp) { // got full message? + + temp = 0; + while(Wire.available()) buff[temp++] = Wire.read(); + + } else { + while(Wire.available()) Wire.read(); // Toss garbage bytes. + } + } else { + while(Wire.available()) Wire.read(); // Toss garbage bytes. + } + + Serial.print(buff); + delay(1); // Don't loop to quickly. +} diff --git a/comm/spi/arduino_spi_master/arduino_spi_master.ino b/comm/spi/arduino_spi_master/arduino_spi_master.ino new file mode 100644 index 0000000..248ef0f --- /dev/null +++ b/comm/spi/arduino_spi_master/arduino_spi_master.ino @@ -0,0 +1,34 @@ +#include <SPI.h> +#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 len = 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(&len, 4); // get length + if (len) { + SPI.transfer(&buff, min(len, CHAR_BUF)); + len -= min(len, CHAR_BUF); + } + while (len--) SPI.transfer(0); // eat any remaining bytes + } + + digitalWrite(SS_PIN, HIGH); + Serial.print(buff); + delay(1); // Don't loop to quickly. +}
\ No newline at end of file diff --git a/comm/spi/arduino_spi_slave_1.py b/comm/spi/arduino_spi_slave_1.py new file mode 100644 index 0000000..e05c2f0 --- /dev/null +++ b/comm/spi/arduino_spi_slave_1.py @@ -0,0 +1,102 @@ +# This work is licensed under the MIT license. +# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. +# https://github.com/openmv/openmv/blob/master/LICENSE +# +# 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 +import ustruct +import time + +text = "Hello World!\n" +data = ustruct.pack("<bi%ds" % len(text), 85, len(text), text) # 85 is a sync char. +# Use "ustruct" to build data packets to send. +# "<" puts the data in the struct in little endian order. +# "b" puts a signed char in the data stream. +# "i" puts a signed integer in the data stream. +# "%ds" puts a string in the data stream. E.g. "13s" for "Hello World!\n" (13 chars). +# See https://docs.python.org/3/library/struct.html + +# READ ME!!! +# +# Please understand that when your OpenMV Cam is not the SPI master it may miss responding to +# sending data as a SPI slave no matter if you call "spi.send()" in an interrupt callback or in the +# main loop below. Because of this you must absolutely design your communications protocol such +# that if the slave device (the OpenMV Cam) doesn't call "spi.send()" in time that garbage data +# read from the SPI peripheral by the master (the Arduino) is tossed. To accomplish this we use a +# sync character of 85 (binary 01010101) which the Arduino will look for as the first byte read. +# If it doesn't see this then it aborts the SPI transaction and will try again. Second, in order to +# clear out the SPI peripheral state we always send a multiple of four bytes and an extra four zero +# bytes to make sure that the SPI peripheral doesn't hold any stale data which could be 85. Note +# that the OpenMV Cam will miss the "spi.send()" window randomly because it has to service +# interrupts. Interrupts will happen a lot more while connected to your PC. + +# The hardware SPI bus for your OpenMV Cam is always SPI bus 2. +# polarity = 0 -> 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) + + +# NSS callback. +def nss_callback(line): + global spi, data + try: + spi.send(data, timeout=1000) + 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]". + + +# Configure NSS/CS in IRQ mode to send data when requested by the master. +pyb.ExtInt(pyb.Pin("P3"), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, nss_callback) + +while True: + time.sleep_ms(1000) + +################################################################################################### +# Arduino Code +################################################################################################### +# +# #include <SPI.h> +# #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 len = 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(&len, 4); // get length +# if (len) { +# SPI.transfer(&buff, min(len, CHAR_BUF)); +# len -= min(len, CHAR_BUF); +# } +# while (len--) SPI.transfer(0); // eat any remaining bytes +# } +# +# digitalWrite(SS_PIN, HIGH); +# Serial.print(buff); +# delay(1); // Don't loop to quickly. +# } diff --git a/comm/uart/arduino_uart/arduino_uart.ino b/comm/uart/arduino_uart/arduino_uart.ino new file mode 100644 index 0000000..de826c9 --- /dev/null +++ b/comm/uart/arduino_uart/arduino_uart.ino @@ -0,0 +1,14 @@ +void setup() { + // put your setup code here, to run once: + Serial.begin(19200); +} + +void loop() { + // put your main code here, to run repeatedly: + if (Serial.available()) { + // Read the most recent byte + byte byteRead = Serial.read(); + // ECHO the value that was read + Serial.write(byteRead); + } +}
\ No newline at end of file diff --git a/comm/uart/arduino_uart_1.py b/comm/uart/arduino_uart_1.py new file mode 100644 index 0000000..3996cf7 --- /dev/null +++ b/comm/uart/arduino_uart_1.py @@ -0,0 +1,42 @@ +# This work is licensed under the MIT license. +# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. +# https://github.com/openmv/openmv/blob/master/LICENSE +# +# Basic UART communications between OpenMV and Arduino Uno. + +# 1) Wire up your OpenMV Cam to your Arduino Uno like this: +# +# OpenMV Cam Ground Pin ----> Arduino Ground +# OpenMV Cam UART3_TX(P4) ----> Arduino Uno UART_RX(0) +# OpenMV Cam UART3_RX(P5) ----> Arduino Uno UART_TX(1) + +# 2) Uncomment and upload the following sketch to Arduino: +# +# void setup() { +# // put your setup code here, to run once: +# Serial.begin(19200); +# } +# +# void loop() { +# // put your main code here, to run repeatedly: +# if (Serial.available()) { +# // Read the most recent byte +# byte byteRead = Serial.read(); +# // ECHO the value that was read +# Serial.write(byteRead); +# } +# } + +# 3) Run the following script in OpenMV IDE: + +import time +from pyb import UART + +# UART 3, and baudrate. +uart = UART(3, 19200, timeout_char=200) + +while True: + uart.write("Hello World!\n") + if uart.any(): + print(uart.read()) + time.sleep_ms(1000) diff --git a/spi_comm/arduino_master_read/arduino_master_read.ino b/spi_comm/arduino_master_read/arduino_master_read.ino deleted file mode 100644 index e31eac9..0000000 --- a/spi_comm/arduino_master_read/arduino_master_read.ino +++ /dev/null @@ -1,26 +0,0 @@ -#include <SPI.h> - -int x; - -void setup() { - Serial.begin(115200); - SPI.begin(); - SPI.setClockDivider(SPI_CLOCK_DIV8); - digitalWrite(SS,HIGH); - -} - -void loop() { - byte Mastersend,Mastereceive; - digitalWrite(SS, LOW); - x = 1; - Mastersend = x; - Mastereceive=SPI.transfer(Mastersend); - if(Mastereceive == 1){ - Serial.println("Master receive 1"); - } - else{ - Serial.println("Master receive 0"); - } - -} diff --git a/spi_comm/arduino_slave_read/arduino_slave_read.ino b/spi_comm/arduino_slave_read/arduino_slave_read.ino deleted file mode 100644 index 8264ae3..0000000 --- a/spi_comm/arduino_slave_read/arduino_slave_read.ino +++ /dev/null @@ -1,30 +0,0 @@ -#include<SPI.h> - -volatile boolean received; -volatile byte receivedData; - -ISR (SPI_STC_vect) //Inerrrput routine function -{ - receivedData = SPDR; // Get the received data from SPDR register - received = true; // Sets received as True -} - -void setup() -{ - Serial.begin(115200); - - pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT - SPCR |= _BV(SPE); //Turn on SPI in Slave Mode - received = false; - SPI.attachInterrupt(); //Activate SPI Interuupt -} - - -void loop() -{ - if(received) { - SPDR = receivedData; // send back the received data, this is not necessary, only for demo purpose - received = false; - Serial.print(receivedData, HEX); - } -} diff --git a/spi_comm/arduino_slave_write/arduino_slave_write.ino b/spi_comm/arduino_slave_write/arduino_slave_write.ino deleted file mode 100644 index d6f838f..0000000 --- a/spi_comm/arduino_slave_write/arduino_slave_write.ino +++ /dev/null @@ -1,34 +0,0 @@ -#include <SPI.h> - -int x; -long randNumber; - - -volatile boolean received; -volatile byte Slavereceived,Slavesend; - -void setup() { - Serial.begin(115200); - randomSeed(analogRead(0)); - - pinMode(MISO,OUTPUT); - SPCR |= _BV(SPE); - received = false; - SPI.attachInterrupt(); - -} - -ISR (SPI_STC_vect){ - Slavereceived = SPDR; - received = true; -} - -void loop() { - // put your main code here, to run repeatedly: - randNumber = random(0, 1); - Serial.println(randNumber); - Slavesend=randNumber; - SPDR = Slavesend; - delay(1000); - -} diff --git a/spi_comm/openmv_slave/spi_com_master.py b/spi_comm/openmv_slave/spi_com_master.py deleted file mode 100644 index 6e10881..0000000 --- a/spi_comm/openmv_slave/spi_com_master.py +++ /dev/null @@ -1,36 +0,0 @@ -from machine import SPI, Pin -deg = 40 -binary = format(deg, 'b') -print(binary) - -spi = SPI(0, baudrate=400000) # Create SPI peripheral 0 at frequency of 400kHz. - # Depending on the use case, extra parameters may be required - # to select the bus characteristics and/or pins to use. -cs = Pin(4, mode=Pin.OUT, value=1) # Create chip-select on pin 4. - -try: - cs(0) # Select peripheral. - spi.write(b"12345678") # Write 8 bytes, and don't care about received data. -finally: - cs(1) # Deselect peripheral. - -try: - cs(0) # Select peripheral. - rxdata = spi.read(8, 0x42) # Read 8 bytes while writing 0x42 for each byte. -finally: - cs(1) # Deselect peripheral. - -rxdata = bytearray(8) -try: - cs(0) # Select peripheral. - spi.readinto(rxdata, 0x42) # Read 8 bytes inplace while writing 0x42 for each byte. -finally: - cs(1) # Deselect peripheral. - -txdata = b"12345678" -rxdata = bytearray(len(txdata)) -try: - cs(0) # Select peripheral. - spi.write_readinto(txdata, rxdata) # Simultaneously write and read bytes. -finally: - cs(1) # Deselect peripheral. |
