From f9a15dac57651d5a4f7ea8f16f19e402379761d2 Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Wed, 22 Jul 2020 00:42:45 -0700 Subject: [PATCH 01/10] WiFiUDP compile errors fixed and multicast functional - mostly for my convenience at this point to be put here --- jcw --- libraries/WiFi/src/WiFiUdp.cpp | 96 ++++++++++++++++++++++++++-------- libraries/WiFi/src/WiFiUdp.h | 19 +++---- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/libraries/WiFi/src/WiFiUdp.cpp b/libraries/WiFi/src/WiFiUdp.cpp index 24be84bf6..3461fabf2 100644 --- a/libraries/WiFi/src/WiFiUdp.cpp +++ b/libraries/WiFi/src/WiFiUdp.cpp @@ -7,10 +7,14 @@ extern WiFiClass WiFi; #endif arduino::WiFiUDP::WiFiUDP() { - _packet_buffer = (uint8_t*)malloc(WIFI_UDP_BUFFER_SIZE); + _packet_buffer = new uint8_t[WIFI_UDP_BUFFER_SIZE]; _current_packet = NULL; _current_packet_size = 0; - // if this malloc fails then ::begin will fail + // if this allocation fails then ::begin will fail +} + +arduino::WiFiUDP::~WiFiUDP() { + delete[] _packet_buffer; } uint8_t arduino::WiFiUDP::begin(uint16_t port) { @@ -21,6 +25,10 @@ uint8_t arduino::WiFiUDP::begin(uint16_t port) { return 0; } + if (_socket.bind(port) < 0) { + return 0; //Failed to bind UDP Socket to port + } + if (!_packet_buffer) { return 0; } @@ -31,56 +39,79 @@ uint8_t arduino::WiFiUDP::begin(uint16_t port) { return 1; } -void arduino::WiFiUDP::stop() { - _socket.close(); -} +uint8_t arduino::WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) { + // success = 1, fail = 0 + if(begin(port) != 1){ + return 0; + } -// we should get the octets out of the IPAddress... there's nothing for it right now -// int arduino::WiFiUDP::beginPacket(IPAddress ip, uint16_t port) { -// } + nsapi_addr_t multicastGroup = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}}; -int arduino::WiFiUDP::beginPacket(const char *host, uint16_t port) { - _host = host; - _port = port; + if (_socket.join_multicast_group(SocketAddress(multicastGroup)) != NSAPI_ERROR_OK) { + printf("Error joining the multicast group\n"); + return 0; + } return 1; } +void arduino::WiFiUDP::stop() { + _socket.close(); +} + +int arduino::WiFiUDP::beginPacket(IPAddress ip, uint16_t port) { + nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}}; + _host = SocketAddress(convertedIP, port); + //If IP is null and port is 0 the initialization failed + return (_host.get_ip_address() == nullptr && _host.get_port() == 0) ? 0 : 1; +} + +int arduino::WiFiUDP::beginPacket(const char *host, uint16_t port) { + _host = SocketAddress(host, port); + //If IP is null and port is 0 the initialization failed + return (_host.get_ip_address() == nullptr && _host.get_port() == 0) ? 0 : 1; +} + int arduino::WiFiUDP::endPacket() { return 1; } // Write a single byte into the packet size_t arduino::WiFiUDP::write(uint8_t byte) { - uint8_t buffer[1] = { byte }; - SocketAddress addr(_host, _port); - return _socket.sendto(addr, buffer, 1); + uint8_t buffer[1] = { byte }; + return _socket.sendto(_host, buffer, 1); } // Write size bytes from buffer into the packet -size_t arduino::WiFiUDP::write(const uint8_t *buffer, size_t size) { - SocketAddress addr(_host, _port); - return _socket.sendto(addr, buffer, size); +size_t arduino::WiFiUDP::write(const uint8_t *buffer, size_t size) { + return _socket.sendto(_host, buffer, size); } int arduino::WiFiUDP::parsePacket() { - nsapi_size_or_error_t ret = _socket.recvfrom(NULL, _packet_buffer, WIFI_UDP_BUFFER_SIZE); + nsapi_size_or_error_t ret = _socket.recvfrom(&_remoteHost, _packet_buffer, WIFI_UDP_BUFFER_SIZE); if (ret == NSAPI_ERROR_WOULD_BLOCK) { // no data return 0; + } else if(ret == NSAPI_ERROR_NO_SOCKET){ + // socket was not created correctly. + return -1; } // error codes below zero are errors else if (ret <= 0) { // something else went wrong, need some tracing info... - return 0; + return -1; } // set current packet states _current_packet = _packet_buffer; _current_packet_size = ret; - return 1; + return _current_packet_size; +} + +int arduino::WiFiUDP::available() { + return _current_packet_size; } // Read a single byte from the current packet @@ -96,7 +127,7 @@ int arduino::WiFiUDP::read() { // check for overflow if (_current_packet > _packet_buffer + _current_packet_size) { // try reading the next packet... - if (parsePacket() == 1) { + if (parsePacket() > 0) { // if so, read first byte of next packet; return read(); } @@ -131,7 +162,7 @@ int arduino::WiFiUDP::read(unsigned char* buffer, size_t len) { // at the end of the packet? if (max_bytes == 0) { // try read next packet... - if (parsePacket() == 1) { + if (parsePacket() > 0) { return read(buffer, len); } else { @@ -147,4 +178,25 @@ int arduino::WiFiUDP::read(unsigned char* buffer, size_t len) { _current_packet += len; return len; +} + +IPAddress arduino::WiFiUDP::remoteIP() { + nsapi_addr_t address = _remoteHost.get_addr(); + return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]); +} + +uint16_t arduino::WiFiUDP::remotePort() { + return _remoteHost.get_port(); +} + +void arduino::WiFiUDP::flush(){ + // TODO: a real check to ensure transmission has been completed +} + +int arduino::WiFiUDP::peek(){ + if (_current_packet_size < 1){ + return -1; + } + + return _current_packet[0]; } \ No newline at end of file diff --git a/libraries/WiFi/src/WiFiUdp.h b/libraries/WiFi/src/WiFiUdp.h index 362e6ef8b..3796c5f9b 100644 --- a/libraries/WiFi/src/WiFiUdp.h +++ b/libraries/WiFi/src/WiFiUdp.h @@ -33,8 +33,8 @@ namespace arduino { class WiFiUDP : public UDP { private: UDPSocket _socket; // Mbed OS socket - const char *_host; // Host to be used to send data (todo: switch to SocketAddress) - uint16_t _port; // Port to be used to send data (^) + SocketAddress _host; // Host to be used to send data + SocketAddress _remoteHost; // Remote host that sent incoming packets uint8_t* _packet_buffer; // Raw packet buffer (contains data we got from the UDPSocket) @@ -45,15 +45,16 @@ class WiFiUDP : public UDP { public: WiFiUDP(); // Constructor + ~WiFiUDP(); virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use - // virtual uint8_t beginMulticast(IPAddress, uint16_t); // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t beginMulticast(IPAddress, uint16_t); // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 if there are no sockets available to use virtual void stop(); // Finish with the UDP socket // Sending UDP packets // Start building up a packet to send to the remote host specific in ip and port // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port - // virtual int beginPacket(IPAddress ip, uint16_t port); + virtual int beginPacket(IPAddress ip, uint16_t port); // Start building up a packet to send to the remote host specific in host and port // Returns 1 if successful, 0 if there was a problem resolving the hostname or port virtual int beginPacket(const char *host, uint16_t port); @@ -71,7 +72,7 @@ class WiFiUDP : public UDP { // Returns the size of the packet in bytes, or 0 if no packets are available virtual int parsePacket(); // Number of bytes remaining in the current packet - // virtual int available(); + virtual int available(); // Read a single byte from the current packet virtual int read(); // Read up to len bytes from the current packet and place them into buffer @@ -81,13 +82,13 @@ class WiFiUDP : public UDP { // Returns the number of characters read, or 0 if none are available virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); }; // Return the next byte from the current packet without moving on to the next byte - // virtual int peek(); - // virtual void flush(); // Finish reading the current packet + virtual int peek(); + virtual void flush(); // Finish reading the current packet // Return the IP address of the host who sent the current incoming packet - // virtual IPAddress remoteIP(); + virtual IPAddress remoteIP(); // // Return the port of the host who sent the current incoming packet - // virtual uint16_t remotePort(); + virtual uint16_t remotePort(); }; } From 664f83a633bf462be164f79c20ba502959f37952 Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Sat, 25 Jul 2020 16:20:52 -0700 Subject: [PATCH 02/10] For Pull request to add Segger Jlink to programmers - note this is WIP! Proceed with the appropriate caution --- jcw --- boards.txt | 13 +++++++++++++ platform.txt | 12 +++++++++--- programmers.txt | 9 ++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/boards.txt b/boards.txt index 030ed454e..795072a13 100644 --- a/boards.txt +++ b/boards.txt @@ -40,6 +40,19 @@ envie_m7.upload.native_usb=true envie_m7.upload.maximum_size=786432 envie_m7.upload.maximum_data_size=523624 +### WIP! Note, this may not be able to coexist - proceed accordingly! --- jcw + +## for jlink - REM: Must use WINUSB w/jlink (Zadig good utility for this under win10.) + +envie_m7.debug.tool=gdb +envie_m7.bootloader.tool=openocd +envie_m7.bootloader.config=-f target/stm32h7x_dual_bank.cfg +envie_m7.bootloader.programmer=-f interface/jlink.cfg +envie_m7.bootloader.extra_action.preflash=stm32h7x option_write 0 0x01c 0xb86aaf0 +envie_m7.bootloader.file=PORTENTA_H7/portentah7_bootloader_mbed_hs.elf + +## for stlink + envie_m7.debug.tool=gdb envie_m7.bootloader.tool=openocd envie_m7.bootloader.config=-f target/stm32h7x_dual_bank.cfg diff --git a/platform.txt b/platform.txt index 339c18371..e62bc5e14 100644 --- a/platform.txt +++ b/platform.txt @@ -52,6 +52,10 @@ compiler.ar.extra_flags= compiler.objcopy.eep.extra_flags= compiler.elf2hex.extra_flags= +# {build.library_discovery_phase} is set to 1 by the builder during library discovery. +# (this is available since arduino-builder>=1.5.5, keeping the default here for backward compatiblity) +build.library_discovery_phase=0 +build.library_discovery_phase_flag=-DARDUINO_LIBRARY_DISCOVERY_PHASE={build.library_discovery_phase} # USB Flags # --------- @@ -67,10 +71,10 @@ build.zip.pattern={recipe.size.pattern} # ----------------------- ## Compile c files -recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {build.extra_flags} {compiler.c.extra_flags} "-I{build.core.path}/api/deprecated" {includes} "-iprefix{build.core.path}" "@{compiler.mbed.includes}" -o "{object_file}" "{source_file}" +recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {build.library_discovery_phase_flag} {build.extra_flags} {compiler.c.extra_flags} "-I{build.core.path}/api/deprecated" {includes} "-iprefix{build.core.path}" "@{compiler.mbed.includes}" -o "{object_file}" "{source_file}" ## Compile c++ files -recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {includes} {build.extra_flags} {compiler.cpp.extra_flags} "-I{build.core.path}/api/deprecated" "-iprefix{build.core.path}" "@{compiler.mbed.includes}" "{source_file}" -o "{object_file}" +recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {build.library_discovery_phase_flag} {includes} {build.extra_flags} {compiler.cpp.extra_flags} "-I{build.core.path}/api/deprecated" "-iprefix{build.core.path}" "@{compiler.mbed.includes}" "{source_file}" -o "{object_file}" ## Compile asm files recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {includes} {build.extra_flags} {compiler.cpp.extra_flags} "-I{build.core.path}/api/deprecated" "-iprefix{build.core.path}" "@{compiler.mbed.includes}" "{source_file}" -o "{object_file}" @@ -118,9 +122,11 @@ tools.openocd.erase.params.verbose=-d2 tools.openocd.erase.params.quiet=-d0 tools.openocd.erase.pattern= +### WIP!::: this needs to be double-checked to make sure all is OK with the jlink addition: + tools.openocd.bootloader.params.verbose=-d2 tools.openocd.bootloader.params.quiet=-d0 -tools.openocd.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -s "{path}/share/openocd/scripts/" {bootloader.programmer} {bootloader.config} -c "telnet_port disabled; init; reset init; halt; adapter speed 10000; {bootloader.extra_action.preflash}; program {{runtime.platform.path}/bootloaders/{bootloader.file}}; reset run; shutdown" +tools.openocd.bootloader.pattern="{path}/{cmd}" {bootloader.verbose} -s "{path}/share/openocd/scripts/" {bootloader.programmer} {bootloader.config} -c "telnet_port disabled; init; reset init; halt; adapter speed 10000; transport select swd; {bootloader.extra_action.preflash}; program {{runtime.platform.path}/bootloaders/{bootloader.file}}; reset run; shutdown" # # BOSSA diff --git a/programmers.txt b/programmers.txt index 967f468c2..fab890ae4 100644 --- a/programmers.txt +++ b/programmers.txt @@ -10,4 +10,11 @@ stlink.communication=USB stlink.protocol= stlink.program.protocol= stlink.program.tool=openocd -stlink.program.extra_params= \ No newline at end of file +stlink.program.extra_params= + +jlink.name=SEGGER JLINK +jlink.communication=USB +jlink.protocol= +jlink.program.protocol=swd +jlink.program.tool=openocd +jlink.program.extra_params= From 46d3418de7ff1ecf387034bc4045bae936992013 Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 03:07:28 -0700 Subject: [PATCH 03/10] For PR to add pinPeripheral() --- jcw --- cores/arduino/wiring_constants.h | 64 +++++++++++++++++ cores/arduino/wiring_private.c | 119 +++++++++++++++++++++++++++++++ cores/arduino/wiring_private.h | 9 ++- 3 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 cores/arduino/wiring_constants.h create mode 100644 cores/arduino/wiring_private.c diff --git a/cores/arduino/wiring_constants.h b/cores/arduino/wiring_constants.h new file mode 100644 index 000000000..72bc019c1 --- /dev/null +++ b/cores/arduino/wiring_constants.h @@ -0,0 +1,64 @@ +//** just starting suggestion at this point --- jcw - 9/10/20 +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _WIRING_CONSTANTS_ +#define _WIRING_CONSTANTS_ + +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus + +#define LOW (0x0) +#define HIGH (0x1) + +#define INPUT (0x0) +#define OUTPUT (0x1) +#define INPUT_PULLUP (0x2) +#define INPUT_PULLDOWN (0x3) + +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 +#define EULER 2.718281828459045235360287471352 + +#define SERIAL 0x0 +#define DISPLAY 0x1 + +enum BitOrder { + LSBFIRST = 0, + MSBFIRST = 1 +}; + +// moved to WInterrupts.h +//// LOW 0 +//// HIGH 1 +//#define CHANGE 2 +//#define FALLING 3 +//#define RISING 4 +// +//#define DEFAULT 1 +//#define EXTERNAL 0 + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* _WIRING_CONSTANTS_ */ diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c new file mode 100644 index 000000000..4af42faf2 --- /dev/null +++ b/cores/arduino/wiring_private.c @@ -0,0 +1,119 @@ +//** just starting suggestion at this point --- jcw - 9/10/20 +/* + Copyright (c) 2015 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Arduino.h" +#include "wiring_private.h" + +int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) +{ + // Handle the case the pin isn't usable as PIO + if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) + { + return -1 ; + } + + switch ( ulPeripheral ) + { + case PIO_DIGITAL: + case PIO_INPUT: + case PIO_INPUT_PULLUP: + case PIO_OUTPUT: + // Disable peripheral muxing, done in pinMode +// PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PMUXEN = 0 ; + + // Configure pin mode, if requested + if ( ulPeripheral == PIO_INPUT ) + { + pinMode( ulPin, INPUT ) ; + } + else + { + if ( ulPeripheral == PIO_INPUT_PULLUP ) + { + pinMode( ulPin, INPUT_PULLUP ) ; + } + else + { + if ( ulPeripheral == PIO_OUTPUT ) + { + pinMode( ulPin, OUTPUT ) ; + } + else + { + // PIO_DIGITAL, do we have to do something as all cases are covered? + } + } + } + break ; + + case PIO_ANALOG: + case PIO_SERCOM: + case PIO_SERCOM_ALT: + case PIO_TIMER: + case PIO_TIMER_ALT: + case PIO_EXTINT: + case PIO_COM: + case PIO_AC_CLK: +#if 0 + // Is the pio pin in the lower 16 ones? + // The WRCONFIG register allows update of only 16 pin max out of 32 + if ( g_APinDescription[ulPin].ulPin < 16 ) + { + PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) | + PORT_WRCONFIG_WRPINCFG | + PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin ) ; + } + else + { + PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | + PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) | + PORT_WRCONFIG_WRPINCFG | + PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ; + } +#else + if ( g_APinDescription[ulPin].ulPin & 1 ) // is pin odd? + { + uint32_t temp ; + + // Get whole current setup for both odd and even pins and remove odd one + temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ; + // Set new muxing + PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXO( ulPeripheral ) ; + // Enable port mux + PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; + } + else // even pin + { + uint32_t temp ; + + temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXO( 0xF ) ; + PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ; + PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux + } +#endif + break ; + + case PIO_NOT_A_PIN: + return -1l ; + break ; + } + + return 0l ; +} + diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h index 349db493d..4ec015998 100644 --- a/cores/arduino/wiring_private.h +++ b/cores/arduino/wiring_private.h @@ -1,3 +1,4 @@ +//** just starting suggestion at this point --- jcw - 9/10/20 /* wiring_private.h Part of Arduino - http://www.arduino.cc/ @@ -23,6 +24,7 @@ #ifndef WiringPrivate_h #define WiringPrivate_h +#include #include #include @@ -31,11 +33,14 @@ #ifdef __cplusplus extern "C"{ #endif - +#include "wiring_constants.h" typedef void (*voidFuncPtr)(void); + +int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ); + #ifdef __cplusplus } // extern "C" #endif - +// #include "HardwareSerial.h" #endif From bbab0643a4665a97767849d758ce59f30e2b640e Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 04:08:39 -0700 Subject: [PATCH 04/10] For PR to add pinPeripheral() function - note wiring_private.c stubbed right now, will be fixed to make compatible with H7 --- jcw --- cores/arduino/WVariant.h | 64 ++++++++++++++++++++++++++++++++ cores/arduino/wiring_constants.h | 8 ++-- cores/arduino/wiring_private.c | 23 ++++++++---- cores/arduino/wiring_private.h | 4 +- 4 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 cores/arduino/WVariant.h diff --git a/cores/arduino/WVariant.h b/cores/arduino/WVariant.h new file mode 100644 index 000000000..310c03539 --- /dev/null +++ b/cores/arduino/WVariant.h @@ -0,0 +1,64 @@ +//** just starting suggestion at this point --- jcw - 9/10/20 +/* +Copyright (c) 2015 Arduino LLC. All right reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum _EPioType +{ + PIO_NOT_A_PIN=-1, /* Not under control of a peripheral. */ + PIO_EXTINT=0, /* The pin is controlled by the associated signal of peripheral A. */ + PIO_ANALOG, /* The pin is controlled by the associated signal of peripheral B. */ + PIO_SERCOM, /* The pin is controlled by the associated signal of peripheral C. */ + PIO_SERCOM_ALT, /* The pin is controlled by the associated signal of peripheral D. */ + PIO_TIMER, /* The pin is controlled by the associated signal of peripheral E. */ + PIO_TIMER_ALT, /* The pin is controlled by the associated signal of peripheral F. */ + PIO_COM, /* The pin is controlled by the associated signal of peripheral G. */ + PIO_AC_CLK, /* The pin is controlled by the associated signal of peripheral H. */ + PIO_DIGITAL, /* The pin is controlled by PORT. */ + PIO_INPUT, /* The pin is controlled by PORT and is an input. */ + PIO_INPUT_PULLUP, /* The pin is controlled by PORT and is an input with internal pull-up resistor enabled. */ + PIO_OUTPUT, /* The pin is controlled by PORT and is an output. */ + + PIO_PWM=PIO_TIMER, + PIO_PWM_ALT=PIO_TIMER_ALT, +} EPioType ; + +/** + * Pin Attributes to be OR-ed + */ +#define PIN_ATTR_NONE (0UL<<0) +#define PIN_ATTR_COMBO (1UL<<0) +#define PIN_ATTR_ANALOG (1UL<<1) +#define PIN_ATTR_DIGITAL (1UL<<2) +#define PIN_ATTR_PWM (1UL<<3) +#define PIN_ATTR_TIMER (1UL<<4) +#define PIN_ATTR_TIMER_ALT (1UL<<5) +#define PIN_ATTR_EXTINT (1UL<<6) + + +#ifdef __cplusplus +} // extern "C" +#endif + diff --git a/cores/arduino/wiring_constants.h b/cores/arduino/wiring_constants.h index 72bc019c1..b5260b627 100644 --- a/cores/arduino/wiring_constants.h +++ b/cores/arduino/wiring_constants.h @@ -42,10 +42,10 @@ extern "C"{ #define SERIAL 0x0 #define DISPLAY 0x1 -enum BitOrder { - LSBFIRST = 0, - MSBFIRST = 1 -}; +// enum BitOrder { +// LSBFIRST = 0, +// MSBFIRST = 1 +// }; // moved to WInterrupts.h //// LOW 0 diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c index 4af42faf2..b38d3115e 100644 --- a/cores/arduino/wiring_private.c +++ b/cores/arduino/wiring_private.c @@ -1,4 +1,10 @@ //** just starting suggestion at this point --- jcw - 9/10/20 + +// This file is stubbed - will fix incompatibilies with Portenta +// pinMode() calls need to be fixed for a start... +///**XXX** - marks what needs to be fixed + + /* Copyright (c) 2015 Arduino LLC. All right reserved. @@ -19,14 +25,15 @@ #include "Arduino.h" #include "wiring_private.h" +#include "WVariant.h" int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) { // Handle the case the pin isn't usable as PIO - if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) - { - return -1 ; - } +//**XXX*** STIB UNTIL FIXED --- if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) +/// { +/// return -1 ; +/// } switch ( ulPeripheral ) { @@ -40,19 +47,19 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) // Configure pin mode, if requested if ( ulPeripheral == PIO_INPUT ) { - pinMode( ulPin, INPUT ) ; +///***XXX^^^ stub until fixed pinMode( ulPin, INPUT ) ; } else { if ( ulPeripheral == PIO_INPUT_PULLUP ) { - pinMode( ulPin, INPUT_PULLUP ) ; +///***XXX^^^ stub until fixed pinMode( ulPin, INPUT_PULLUP ) ; } else { if ( ulPeripheral == PIO_OUTPUT ) { - pinMode( ulPin, OUTPUT ) ; +///***XXX^^^ stub until fixed pinMode( ulPin, OUTPUT ) ; } else { @@ -87,6 +94,7 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ; } #else +#if 0 ///***XXX^^^ stub until fixed - should be #if 1 here <- if ( g_APinDescription[ulPin].ulPin & 1 ) // is pin odd? { uint32_t temp ; @@ -106,6 +114,7 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ; PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux } +#endif #endif break ; diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h index 4ec015998..28d7a4883 100644 --- a/cores/arduino/wiring_private.h +++ b/cores/arduino/wiring_private.h @@ -29,7 +29,7 @@ #include #include "Arduino.h" - +#include "WVariant.h" #ifdef __cplusplus extern "C"{ #endif @@ -42,5 +42,5 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ); #ifdef __cplusplus } // extern "C" #endif -// #include "HardwareSerial.h" + #endif From 3b78c7534c7384e6322b0b4d73e909545065cc9f Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 04:59:37 -0700 Subject: [PATCH 05/10] For PR to add pinPeripheral() function - note wiring_private.c stubbed right now, will be fixed to make compatible with H7 --- jcw --- cores/arduino/api | 1 - cores/arduino/wiring_private.c | 28 ++++++++++++---------------- cores/arduino/wiring_private.h | 3 ++- 3 files changed, 14 insertions(+), 18 deletions(-) delete mode 120000 cores/arduino/api diff --git a/cores/arduino/api b/cores/arduino/api deleted file mode 120000 index d615d8846..000000000 --- a/cores/arduino/api +++ /dev/null @@ -1 +0,0 @@ -../../../ArduinoCore-API/api/ \ No newline at end of file diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c index b38d3115e..1af763392 100644 --- a/cores/arduino/wiring_private.c +++ b/cores/arduino/wiring_private.c @@ -1,10 +1,4 @@ //** just starting suggestion at this point --- jcw - 9/10/20 - -// This file is stubbed - will fix incompatibilies with Portenta -// pinMode() calls need to be fixed for a start... -///**XXX** - marks what needs to be fixed - - /* Copyright (c) 2015 Arduino LLC. All right reserved. @@ -24,16 +18,18 @@ */ #include "Arduino.h" +#include "api/Common.h" #include "wiring_private.h" #include "WVariant.h" + int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) { // Handle the case the pin isn't usable as PIO -//**XXX*** STIB UNTIL FIXED --- if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) -/// { -/// return -1 ; -/// } +// if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) +// { +// return -1 ; +// } switch ( ulPeripheral ) { @@ -42,24 +38,24 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) case PIO_INPUT_PULLUP: case PIO_OUTPUT: // Disable peripheral muxing, done in pinMode -// PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PMUXEN = 0 ; +// PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PMUXEN = 0 ; // Configure pin mode, if requested if ( ulPeripheral == PIO_INPUT ) { -///***XXX^^^ stub until fixed pinMode( ulPin, INPUT ) ; + pin_mode( ulPin, INPUT ) ; // pinMode( ulPin, INPUT ) ; } else { if ( ulPeripheral == PIO_INPUT_PULLUP ) { -///***XXX^^^ stub until fixed pinMode( ulPin, INPUT_PULLUP ) ; + pin_mode( ulPin, INPUT_PULLUP ) ; } else { if ( ulPeripheral == PIO_OUTPUT ) { -///***XXX^^^ stub until fixed pinMode( ulPin, OUTPUT ) ; + pin_mode( ulPin, OUTPUT ) ; } else { @@ -94,7 +90,7 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ; } #else -#if 0 ///***XXX^^^ stub until fixed - should be #if 1 here <- +#if 0 if ( g_APinDescription[ulPin].ulPin & 1 ) // is pin odd? { uint32_t temp ; @@ -114,7 +110,7 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ; PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux } -#endif +#endif #endif break ; diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h index 28d7a4883..58ef190ce 100644 --- a/cores/arduino/wiring_private.h +++ b/cores/arduino/wiring_private.h @@ -33,7 +33,8 @@ #ifdef __cplusplus extern "C"{ #endif -#include "wiring_constants.h" +// #include "wiring_constants.h" +#include "api/Common.h" // H7 equivalent typedef void (*voidFuncPtr)(void); From eff6cb4908d563b327f5e88f9145de774e786094 Mon Sep 17 00:00:00 2001 From: JOHN C WESTMORELAND Date: Thu, 10 Sep 2020 05:17:29 -0700 Subject: [PATCH 06/10] Oops - edited common.h Oops - edited common.h - restoring api 'link' --- jcw --- cores/arduino/api | 1 + 1 file changed, 1 insertion(+) create mode 100644 cores/arduino/api diff --git a/cores/arduino/api b/cores/arduino/api new file mode 100644 index 000000000..d615d8846 --- /dev/null +++ b/cores/arduino/api @@ -0,0 +1 @@ +../../../ArduinoCore-API/api/ \ No newline at end of file From eb424d9f08f649e75f7ab78a72b19b3f66920f1c Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 05:28:56 -0700 Subject: [PATCH 07/10] Oops - edited common.h - restoring api 'link', plus the issue with github complaining about stashing changes --- jcw --- cores/arduino/wiring_private.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c index 1af763392..140234474 100644 --- a/cores/arduino/wiring_private.c +++ b/cores/arduino/wiring_private.c @@ -49,13 +49,13 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) { if ( ulPeripheral == PIO_INPUT_PULLUP ) { - pin_mode( ulPin, INPUT_PULLUP ) ; + pin_mode( ulPin, INPUT_PULLUP ) ; // pinMode( ulPin, INPUT_PULLUP ) ; } else { if ( ulPeripheral == PIO_OUTPUT ) { - pin_mode( ulPin, OUTPUT ) ; + pin_mode( ulPin, OUTPUT ) ; // pinMode( ulPin, OUTPUT ) ; } else { From bd2b1d9e4bcf6177d266800eec0b76196f1e7bba Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 05:56:30 -0700 Subject: [PATCH 08/10] github complaining since I added the api dir - trying to fix --- jcw --- cores/arduino/wiring_private.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c index 140234474..32b893b3f 100644 --- a/cores/arduino/wiring_private.c +++ b/cores/arduino/wiring_private.c @@ -1,4 +1,5 @@ //** just starting suggestion at this point --- jcw - 9/10/20 + /* Copyright (c) 2015 Arduino LLC. All right reserved. From 8f623f9148b228e0912b36f2b7c6ec8e1f02aa86 Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Thu, 10 Sep 2020 06:12:57 -0700 Subject: [PATCH 09/10] Trying to fix the goof --- jcw --- cores/arduino/api | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 120000 cores/arduino/api diff --git a/cores/arduino/api b/cores/arduino/api deleted file mode 100644 index d615d8846..000000000 --- a/cores/arduino/api +++ /dev/null @@ -1 +0,0 @@ -../../../ArduinoCore-API/api/ \ No newline at end of file diff --git a/cores/arduino/api b/cores/arduino/api new file mode 120000 index 000000000..d615d8846 --- /dev/null +++ b/cores/arduino/api @@ -0,0 +1 @@ +../../../ArduinoCore-API/api/ \ No newline at end of file From 955a927fd3f71fe455acbed38645fc1ec634c9b8 Mon Sep 17 00:00:00 2001 From: "John C. Westmoreland" Date: Fri, 23 Oct 2020 06:53:25 -0700 Subject: [PATCH 10/10] Wiring related files need to be updated for the MKR-RGB Shield to work --- jcw --- cores/arduino/WVariant.h | 1 - cores/arduino/wiring_private.c | 98 ++++++++++++++++++++++++++++++---- cores/arduino/wiring_private.h | 19 +++++-- 3 files changed, 105 insertions(+), 13 deletions(-) diff --git a/cores/arduino/WVariant.h b/cores/arduino/WVariant.h index 310c03539..75e8f6f0d 100644 --- a/cores/arduino/WVariant.h +++ b/cores/arduino/WVariant.h @@ -1,4 +1,3 @@ -//** just starting suggestion at this point --- jcw - 9/10/20 /* Copyright (c) 2015 Arduino LLC. All right reserved. diff --git a/cores/arduino/wiring_private.c b/cores/arduino/wiring_private.c index 32b893b3f..ac11712a7 100644 --- a/cores/arduino/wiring_private.c +++ b/cores/arduino/wiring_private.c @@ -1,5 +1,4 @@ //** just starting suggestion at this point --- jcw - 9/10/20 - /* Copyright (c) 2015 Arduino LLC. All right reserved. @@ -18,16 +17,75 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "Arduino.h" -#include "api/Common.h" -#include "wiring_private.h" -#include "WVariant.h" +// #include +// #include "gpio_object.h" +// #include "Wire.h" +// #include "api/Common.h" +#include +// #include "device.h" +// #include "pinmap.h" +// #include "WVariant.h" +// #include "pinmode_arduino.h" +#include "pins_arduino.h" +#include "mbed.h" + + +static uint8_t pin = 4; +// static gpio_t gpio; +// gpio_t gpio; + +// extern PinDescription g_APinDescription[]; +#if 0 +PinDescription g_APinDescription[] = { + // D0 - D7 + { PH_15, NULL, NULL, NULL }, // D0 + { PK_1, NULL, NULL, NULL }, // D1 + { PJ_11, NULL, NULL, NULL }, // D2 + { PG_7, NULL, NULL, NULL }, // D3 + { PC_7, NULL, NULL, NULL }, // D4 + { PC_6, NULL, NULL, NULL }, // D5 + { PA_8, NULL, NULL, NULL }, // D6 + { PI_0, NULL, NULL, NULL }, // D7 + + // D8 - D14 + { PC_3, NULL, NULL, NULL }, // D8 + { PI_1, NULL, NULL, NULL }, // D9 + { PC_2, NULL, NULL, NULL }, // D10 + { PH_8, NULL, NULL, NULL }, // D11 + { PH_7, NULL, NULL, NULL }, // D12 + { PA_10, NULL, NULL, NULL }, // D13 + { PA_9, NULL, NULL, NULL }, // D14 + + // A0 - A6 + { PA_0C, NULL, NULL, NULL }, // A0 ADC2_INP0 + { PA_1C, NULL, NULL, NULL }, // A1 ADC2_INP1 + { PC_2C, NULL, NULL, NULL }, // A2 ADC3_INP0 + { PC_3C, NULL, NULL, NULL }, // A3 ADC3_INP1 + { PC_2_ALT0, NULL, NULL, NULL }, // A4 ADC1_INP12 + { PC_3_ALT0, NULL, NULL, NULL }, // A5 ADC1_INP13 + { PA_4, NULL, NULL, NULL }, // A6 ADC1_INP18 + + // LEDS + { PK_5, NULL, NULL, NULL }, // LEDR + { PK_6, NULL, NULL, NULL }, // LEDG + { PK_7, NULL, NULL, NULL }, // LEDB +}; +#endif + +extern struct PinDescription g_APinDescription; +// extern void pinMode(pin_size_t pinNumber, PinMode pinMode); int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) { + + + // Handle the case the pin isn't usable as PIO -// if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) +// if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) +// if ( g_APinDescription[ulPin].gpio == PIO_NOT_A_PIN ) +// if ( g_APinDescription[pin].gpio == PIO_NOT_A_PIN ) +// if ( g_APinDescription[pin] == PIO_NOT_A_PIN ) // { // return -1 ; // } @@ -44,19 +102,24 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) // Configure pin mode, if requested if ( ulPeripheral == PIO_INPUT ) { - pin_mode( ulPin, INPUT ) ; // pinMode( ulPin, INPUT ) ; + pin_mode( ulPin, INPUT ) ; // pinMode( ulPin, INPUT ) ; + +// pinMode( ulPin, INPUT ) ; // pinMode( ulPin, INPUT ) ; } else { if ( ulPeripheral == PIO_INPUT_PULLUP ) { - pin_mode( ulPin, INPUT_PULLUP ) ; // pinMode( ulPin, INPUT_PULLUP ) ; + pin_mode( ulPin, INPUT_PULLUP ) ; // pinMode( ulPin, INPUT_PULLUP ) ; +// pinMode( ulPin, INPUT_PULLUP ) ; // pinMode( ulPin, INPUT_PULLUP ) ; } else { if ( ulPeripheral == PIO_OUTPUT ) { - pin_mode( ulPin, OUTPUT ) ; // pinMode( ulPin, OUTPUT ) ; + pin_mode( ulPin, OUTPUT ) ; // pinMode( ulPin, OUTPUT ) ; +// gpio_init_out(&gpio, ulPin); +// pinMode( ulPin, OUTPUT ) ; // pinMode( ulPin, OUTPUT ) ; } else { @@ -123,3 +186,20 @@ int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ) return 0l ; } + + +void shiftOutMatrix(pin_size_t dataPin, uint8_t clockPin, BitOrder bitOrder, uint32_t val) +{ + uint32_t i; + + for (i = 0; i < 32; i++) { + if (bitOrder == LSBFIRST) + digitalWrite(dataPin, !!(val & (1 << i)) ? HIGH : LOW); + else + digitalWrite(dataPin, !!(val & (1 << (31 - i))) ? HIGH : LOW); + + digitalWrite(clockPin, HIGH); + digitalWrite(clockPin, LOW); + } +} + diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h index 58ef190ce..89d606d87 100644 --- a/cores/arduino/wiring_private.h +++ b/cores/arduino/wiring_private.h @@ -28,18 +28,31 @@ #include #include +// #include "gpio_object.h" + #include "Arduino.h" #include "WVariant.h" #ifdef __cplusplus extern "C"{ #endif // #include "wiring_constants.h" -#include "api/Common.h" // H7 equivalent +/// #include "api/Common.h" // H7 equivalent typedef void (*voidFuncPtr)(void); - +#if 0 +typedef struct { + uint32_t mask; + __IO uint32_t *reg_in; + __IO uint32_t *reg_set; + __IO uint32_t *reg_clr; + PinName pin; + GPIO_TypeDef *gpio; + uint32_t ll_pin; +} gpio_t; +#endif +// gpio_t gpio; int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral ); - +void shiftOutMatrix(pin_size_t dataPin, uint8_t clockPin, BitOrder bitOrder, uint32_t val); #ifdef __cplusplus } // extern "C" #endif