diff --git a/arch/apollo3/apollo3.ini b/arch/apollo3/apollo3.ini index edde6058c..db2d36e0c 100644 --- a/arch/apollo3/apollo3.ini +++ b/arch/apollo3/apollo3.ini @@ -3,9 +3,11 @@ extends = arduino_base platform = https://github.com/nigelb/platform-apollo3blue.git#2e8a9895cf82f2836c483885e6f89b3f83d3ade4 platform_packages=framework-arduinoapollo3@https://github.com/sparkfun/Arduino_Apollo3#v2.2.2rc2 build_type = debug -build_flags = +build_flags = ${arduino_base.build_flags} -Isrc/platform/apollo3 -g + -I"${platformio.packages_dir}/framework-arduinoapollo3/libraries/SPI/src" + -DRADIOLIB_EEPROM_UNSUPPORTED build_src_filter = ${arduino_base.build_src_filter} - @@ -19,14 +21,12 @@ build_src_filter = - - - - - - - - - - lib_deps = ${env.lib_deps} - jgromes/RadioLib@^6.3.0 charlesbaynham/OSFS@^1.2.3 + rweather/Crypto lib_ignore = mathertel/OneButton diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index c1b576fee..c58b16df5 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -24,6 +24,35 @@ SPIClass SPI1(HSPI); #endif // HAS_SDCARD +#if defined(ARCH_APOLLO3) +// Apollo series 2 Kbytes (8 rows of 256 bytes) + +uint16_t OSFS::startOfEEPROM = 1; +uint16_t OSFS::endOfEEPROM = 2048; + +// Useful consts +const OSFS::result noerr = OSFS::result::NO_ERROR; +const OSFS::result notfound = OSFS::result::FILE_NOT_FOUND; + +// 3) How do I read from the medium? +void OSFS::readNBytes(uint16_t address, unsigned int num, byte *output) +{ + for (uint16_t i = address; i < address + num; i++) { + *output = EEPROM.read(i); + output++; + } +} + +// 4) How to I write to the medium? +void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input) +{ + for (uint16_t i = address; i < address + num; i++) { + EEPROM.update(i, *input); + input++; + } +} +#endif + /** * @brief Copies a file from one location to another. * diff --git a/src/FSCommon.h b/src/FSCommon.h index a99fea131..8b1f59f70 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -25,30 +25,18 @@ using namespace LittleFS_Namespace; #include #include -uint16_t OSFS::startOfEEPROM = 1; -uint16_t OSFS::endOfEEPROM = 2048; +extern uint16_t OSFS::startOfEEPROM; +extern uint16_t OSFS::endOfEEPROM; // Useful consts -const OSFS::result noerr = OSFS::result::NO_ERROR; -const OSFS::result notfound = OSFS::result::FILE_NOT_FOUND; +extern const OSFS::result noerr; +extern const OSFS::result notfound; // 3) How do I read from the medium? -void OSFS::readNBytes(uint16_t address, unsigned int num, byte *output) -{ - for (uint16_t i = address; i < address + num; i++) { - *output = EEPROM.read(i); - output++; - } -} +void OSFS::readNBytes(uint16_t address, unsigned int num, byte *output); // 4) How to I write to the medium? -void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input) -{ - for (uint16_t i = address; i < address + num; i++) { - EEPROM.update(i, *input); - input++; - } -} +void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input); #endif #if defined(ARCH_RP2040) diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 930b3483e..1d2086adb 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -213,4 +213,6 @@ extern uint32_t error_address; #define Module_Config_size \ (ModuleConfig_CannedMessageConfig_size + ModuleConfig_ExternalNotificationConfig_size + ModuleConfig_MQTTConfig_size + \ ModuleConfig_RangeTestConfig_size + ModuleConfig_SerialConfig_size + ModuleConfig_StoreForwardConfig_size + \ - ModuleConfig_TelemetryConfig_size + ModuleConfig_size) \ No newline at end of file + ModuleConfig_TelemetryConfig_size + ModuleConfig_size) + +// Please do not remove this comment, it makes trunk and compiler happy at the same time. diff --git a/src/platform/apollo3/Apollo3CryptoEngine.cpp b/src/platform/apollo3/Apollo3CryptoEngine.cpp new file mode 100644 index 000000000..da63dc3ec --- /dev/null +++ b/src/platform/apollo3/Apollo3CryptoEngine.cpp @@ -0,0 +1,66 @@ +#include "AES.h" +#include "CTR.h" +#include "CryptoEngine.h" +#include "configuration.h" + +class Apollo3CryptoEngine : public CryptoEngine +{ + + CTRCommon *ctr = NULL; + + public: + Apollo3CryptoEngine() {} + + ~Apollo3CryptoEngine() {} + + virtual void setKey(const CryptoKey &k) override + { + CryptoEngine::setKey(k); + LOG_DEBUG("Installing AES%d key!\n", key.length * 8); + if (ctr) { + delete ctr; + ctr = NULL; + } + if (key.length != 0) { + if (key.length == 16) + ctr = new CTR(); + else + ctr = new CTR(); + + ctr->setKey(key.bytes, key.length); + } + } + /** + * Encrypt a packet + * + * @param bytes is updated in place + */ + virtual void encrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override + { + if (key.length > 0) { + initNonce(fromNode, packetId); + if (numBytes <= MAX_BLOCKSIZE) { + static uint8_t scratch[MAX_BLOCKSIZE]; + memcpy(scratch, bytes, numBytes); + memset(scratch + numBytes, 0, + sizeof(scratch) - numBytes); // Fill rest of buffer with zero (in case cypher looks at it) + + ctr->setIV(nonce, sizeof(nonce)); + ctr->setCounterSize(4); + ctr->encrypt(bytes, scratch, numBytes); + } else { + LOG_ERROR("Packet too large for crypto engine: %d. noop encryption!\n", numBytes); + } + } + } + + virtual void decrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override + { + // For CTR, the implementation is the same + encrypt(fromNode, packetId, numBytes, bytes); + } + + private: +}; + +CryptoEngine *crypto = new Apollo3CryptoEngine(); diff --git a/src/platform/apollo3/main-apollo3.cpp b/src/platform/apollo3/main-apollo3.cpp index 897c4a259..89398454f 100644 --- a/src/platform/apollo3/main-apollo3.cpp +++ b/src/platform/apollo3/main-apollo3.cpp @@ -13,7 +13,9 @@ void getMacAddr(uint8_t *dmac) dmac[i] = i; } -void cpuDeepSleep(uint64_t msecToWake) {} +void cpuDeepSleep(uint32_t msecToWake) {} + +void initVariant() {} /* pacify libc_nano */ extern "C" { diff --git a/variants/rak11720/mbed/.ld-flags b/variants/rak11720/mbed/.ld-flags new file mode 100644 index 000000000..6341b37b7 --- /dev/null +++ b/variants/rak11720/mbed/.ld-flags @@ -0,0 +1 @@ +-DMBED_BOOT_STACK_SIZE=1024 -DXIP_ENABLE=0 -Wl,--gc-sections -Wl,--wrap,_calloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,atexit -Wl,--wrap,exit -Wl,--wrap,fprintf -Wl,--wrap,main -Wl,--wrap,printf -Wl,--wrap,snprintf -Wl,--wrap,sprintf -Wl,--wrap,vfprintf -Wl,--wrap,vprintf -Wl,--wrap,vsnprintf -Wl,--wrap,vsprintf -Wl,-n -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb \ No newline at end of file diff --git a/variants/rak11720/mbed/.ld-macros b/variants/rak11720/mbed/.ld-macros new file mode 100644 index 000000000..2d6e1ff86 --- /dev/null +++ b/variants/rak11720/mbed/.ld-macros @@ -0,0 +1 @@ +ARDUINO_BLE_FIX \ No newline at end of file diff --git a/variants/rak11720/mbed/.ld-symbols b/variants/rak11720/mbed/.ld-symbols new file mode 100644 index 000000000..7ce8ec6c6 --- /dev/null +++ b/variants/rak11720/mbed/.ld-symbols @@ -0,0 +1 @@ +-DAM_CUSTOM_BDADDR -DAM_PACKAGE_BGA -DARDUINO_BLE_FIX -DARM_MATH_CM4 -DCOMPONENT_FLASHIAP=1 -DCORDIO_ZERO_COPY_HCI -DDEVICE_FLASH=1 -DDEVICE_I2C=1 -DDEVICE_INTERRUPTIN=1 -DDEVICE_LPTICKER=1 -DDEVICE_MPU=1 -DDEVICE_SERIAL=1 -DDEVICE_SPI=1 -DDEVICE_STDIO_MESSAGES=1 -DDEVICE_USTICKER=1 -DFEATURE_BLE=1 -DMBED_BUILD_TIMESTAMP=1701297259.6592317 -DTARGET_AMA3B1KK -DTARGET_Ambiq_Micro -DTARGET_Apollo3 -DTARGET_CORDIO -DTARGET_CORTEX -DTARGET_CORTEX_M -DTARGET_FAMILY_Apollo3 -DTARGET_LIKE_CORTEX_M4 -DTARGET_LIKE_MBED -DTARGET_M4 -DTARGET_NAME=SFE_ARTEMIS -DTARGET_RELEASE -DTARGET_RTOS_M4_M7 -DTARGET_SFE_ARTEMIS -DTOOLCHAIN_GCC -DTOOLCHAIN_GCC_ARM -D__CMSIS_RTOS -D__CORTEX_M4 -D__FPU_PRESENT=1 -D__MBED_CMSIS_RTOS_CM -D__MBED__=1 \ No newline at end of file diff --git a/variants/rak11720/mbed/libmbed-os.a b/variants/rak11720/mbed/libmbed-os.a new file mode 100644 index 000000000..8889401e6 Binary files /dev/null and b/variants/rak11720/mbed/libmbed-os.a differ diff --git a/variants/rak11720/platformio.ini b/variants/rak11720/platformio.ini index 311461133..0bfdbc393 100644 --- a/variants/rak11720/platformio.ini +++ b/variants/rak11720/platformio.ini @@ -2,4 +2,4 @@ extends = apollo3_base board = wiscore_rak11720 board_level = extra -build_flags = ${apollo3_base.build_flags} -Ivariants/rak11720 +build_flags = ${apollo3_base.build_flags} -Ivariants/rak11720 -DDEVICE_LOCALFILESYSTEM diff --git a/variants/rak11720/variant.cpp b/variants/rak11720/variant.cpp new file mode 100644 index 000000000..823c475ab --- /dev/null +++ b/variants/rak11720/variant.cpp @@ -0,0 +1,17 @@ +#include "variant.h" + +const pin_size_t variantPinCount = 22; + +PinState variantPinStates[variantPinCount] = { + {D0, 0, NULL, /*NULL, NULL, NULL,*/ NULL}, {D1, 1, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D2, 2, NULL, /*NULL, NULL, NULL,*/ NULL}, {D3, 3, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D4, 4, NULL, /*NULL, NULL, NULL,*/ NULL}, {D5, 5, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D6, 6, NULL, /*NULL, NULL, NULL,*/ NULL}, {D7, 7, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D8, 8, NULL, /*NULL, NULL, NULL,*/ NULL}, {D9, 9, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D10, 10, NULL, /*NULL, NULL, NULL,*/ NULL}, {D11, 11, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D12, 12, NULL, /*NULL, NULL, NULL,*/ NULL}, {D13, 13, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D14, 14, NULL, /*NULL, NULL, NULL,*/ NULL}, {D15, 15, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D16, 16, NULL, /*NULL, NULL, NULL,*/ NULL}, {D17, 17, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D18, 18, NULL, /*NULL, NULL, NULL,*/ NULL}, {D19, 19, NULL, /*NULL, NULL, NULL,*/ NULL}, + {D20, 20, NULL, /*NULL, NULL, NULL,*/ NULL}, {D21, 21, NULL, /*NULL, NULL, NULL,*/ NULL}, +}; \ No newline at end of file diff --git a/variants/rak11720/variant.h b/variants/rak11720/variant.h index 30751235e..44b4ce7c5 100644 --- a/variants/rak11720/variant.h +++ b/variants/rak11720/variant.h @@ -162,6 +162,10 @@ static const uint8_t AREF = PIN_AREF; #define PIN_SPI_MOSI WB_SPI_MOSI #define PIN_SPI_SCK WB_SPI_CLK +#define VARIANT_SPI_SDI PIN_SPI_MISO +#define VARIANT_SPI_SDO PIN_SPI_MOSI +#define VARIANT_SPI_CLK PIN_SPI_SCK + static const uint8_t SS = PIN_SPI_CS; static const uint8_t MOSI = PIN_SPI_MOSI; static const uint8_t MISO = PIN_SPI_MISO;