From 4971e86d05c714ff3630ada8851938614128b412 Mon Sep 17 00:00:00 2001 From: Abel Tim Date: Sat, 22 Jun 2024 09:17:50 +0200 Subject: update repo structure update poster add code flowchart --- .../spi/arduino_spi_master/arduino_spi_master.ino | 34 +++++++ debug/comm/spi/arduino_spi_slave_1.py | 102 +++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 debug/comm/spi/arduino_spi_master/arduino_spi_master.ino create mode 100644 debug/comm/spi/arduino_spi_slave_1.py (limited to 'debug/comm/spi') diff --git a/debug/comm/spi/arduino_spi_master/arduino_spi_master.ino b/debug/comm/spi/arduino_spi_master/arduino_spi_master.ino new file mode 100644 index 0000000..248ef0f --- /dev/null +++ b/debug/comm/spi/arduino_spi_master/arduino_spi_master.ino @@ -0,0 +1,34 @@ +#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 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/debug/comm/spi/arduino_spi_slave_1.py b/debug/comm/spi/arduino_spi_slave_1.py new file mode 100644 index 0000000..e05c2f0 --- /dev/null +++ b/debug/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(" 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 +# #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. +# } -- cgit v1.2.3