diff --git a/boards.txt b/boards.txt index 179dad5e..55b1f502 100644 --- a/boards.txt +++ b/boards.txt @@ -35,10 +35,10 @@ feather52832.upload.native_usb=false # Build feather52832.build.mcu=cortex-m4 feather52832.build.f_cpu=64000000 -feather52832.build.board=NRF52_FEATHER +feather52832.build.board=NRF52832_FEATHER feather52832.build.core=nRF5 feather52832.build.variant=feather_nrf52832 -feather52832.build.extra_flags=-DNRF52832_XXAA -DNRF52 +feather52832.build.extra_flags=-DNRF52832_XXAA -DNRF52 -DARDUINO_NRF52_FEATHER # SofDevice Menu # Ram & ROM size varies depending on SoftDevice (check linker script) @@ -98,7 +98,7 @@ feather52840.build.f_cpu=64000000 feather52840.build.board=NRF52840_FEATHER feather52840.build.core=nRF5 feather52840.build.variant=feather_nrf52840_express -feather52840.build.extra_flags=-DNRF52840_XXAA +feather52840.build.extra_flags=-DNRF52840_XXAA -DARDUINO_NRF52_FEATHER # SofDevice Menu # Ram & ROM size varies depending on SoftDevice (check linker script) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 244b1c3e..39e56e07 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -86,7 +86,7 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) break; default: - return; + return 0; } for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 4f0918db..459a20de 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -114,8 +114,9 @@ int _write (int fd, const void *buf, size_t count) if ( Serial ) { - Serial.write( (const uint8_t *) buf, count); + return Serial.write( (const uint8_t *) buf, count); } + return 0; } } diff --git a/cores/nRF5/utility/AdaCallback.h b/cores/nRF5/utility/AdaCallback.h index 429a31b9..cf3b75b9 100644 --- a/cores/nRF5/utility/AdaCallback.h +++ b/cores/nRF5/utility/AdaCallback.h @@ -38,9 +38,18 @@ #include "common_inc.h" +#ifndef CFG_CALLBACK_TASK_STACKSIZE #define CFG_CALLBACK_TASK_STACKSIZE (512*2) +#endif + +#ifndef CFG_CALLBACK_QUEUE_LENGTH #define CFG_CALLBACK_QUEUE_LENGTH 20 +#endif + +#ifndef CFG_CALLBACK_TIMEOUT #define CFG_CALLBACK_TIMEOUT 100 +#endif + #ifdef __cplusplus extern "C"{ diff --git a/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino b/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino index 0daf7167..df86e0bc 100644 --- a/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino @@ -24,6 +24,7 @@ const int baudrate = 115200; void setup() { Serial.begin (baudrate); + while ( !Serial ) delay(10); // for nrf52840 with native usb Serial.println("Serial Echo demo"); Serial.print("Badurate : "); diff --git a/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino b/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino index 464e31b7..1e59bdbb 100644 --- a/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino @@ -23,6 +23,7 @@ int interruptPin = A0; void setup() { Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb pinMode(interruptPin, INPUT_PULLUP); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino index 04d18438..5b6e0e43 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino @@ -19,6 +19,7 @@ #include +BLEDis bledis; BLEHidAdafruit blehid; // Array of pins and its keycode @@ -56,6 +57,11 @@ void setup() Bluefruit.setTxPower(4); Bluefruit.setName("Bluefruit52"); + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.begin(); + // set up pin as input for (uint8_t i=0; iformat(false); + } else { + _mounted = true; } return true; @@ -219,17 +173,17 @@ bool LittleFS::begin (void) bool LittleFS::format (bool eraseall) { - if ( eraseall ) - { - for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE ) - { - flash_nrf5x_erase(addr); - } + if ( eraseall ) { + _flash_erase_all(); + } + if(_mounted) { + VERIFY_LFS(lfs_unmount(&_lfs), false); } - VERIFY_LFS(lfs_format(&_lfs, &_lfs_cfg), false); VERIFY_LFS(lfs_mount(&_lfs, &_lfs_cfg), false); + _mounted = true; + return true; } @@ -458,3 +412,89 @@ void LittleFS::_f_rewindDirectory (void* fhdl) { VERIFY_LFS(lfs_dir_rewind(&_lfs, (lfs_dir_t* ) fhdl),); } + + +int LittleFS::_flash_read (lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) +{ + uint32_t addr = lba2addr(block) + off; + flash_nrf5x_read(buffer, addr, size); + + return 0; +} + +// Program a region in a block. The block must have previously +// been erased. Negative error codes are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_flash_prog (lfs_block_t block, lfs_off_t off, const void *buffer, + lfs_size_t size) +{ + uint32_t addr = lba2addr(block) + off; + flash_nrf5x_write(addr, buffer, size); + + return 0; +} + +// Erase a block. A block must be erased before being programmed. +// The state of an erased block is undefined. Negative error codes +// are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_flash_erase (lfs_block_t block) +{ + uint32_t addr = lba2addr(block); + + // implement as write 0xff to whole block address + for(int i=0; i context)->_flash_read(block, off, buffer, size); +} + +// Program a region in a block. The block must have previously +// been erased. Negative error codes are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, + lfs_size_t size) +{ + return ((LittleFS *)c->context)->_flash_prog(block, off, buffer, size); +} + +// Erase a block. A block must be erased before being programmed. +// The state of an erased block is undefined. Negative error codes +// are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_iflash_erase (const struct lfs_config *c, lfs_block_t block) +{ + return ((LittleFS *)c->context)->_flash_erase(block); +} + +// Sync the state of the underlying block device. Negative error codes +// are propogated to the user. +int LittleFS::_iflash_sync (const struct lfs_config *c) +{ + return ((LittleFS *)c->context)->_flash_sync(); +} diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 153bb423..03086487 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -58,6 +58,12 @@ class LittleFS: public BluefruitFS::FileSystemClass bool rmdir_r (char const *filepath); bool format (bool eraseall); + protected: + bool _begun; + bool _mounted; + + LittleFS (lfs_size_t read_size, lfs_size_t prog_size, lfs_size_t block_size, lfs_size_t block_count, lfs_size_t lookahead); + // Internal API: shouldn't be used by Arduino sketch virtual size_t _f_write (void* fhdl, uint8_t const *buf, size_t size); virtual int _f_read (void* fhdl, void *buf, uint16_t nbyte); @@ -67,16 +73,26 @@ class LittleFS: public BluefruitFS::FileSystemClass virtual uint32_t _f_position (void* fhdl); virtual uint32_t _f_size (void* fhdl); + // Raw flash opperations, override to use an external flash chip + virtual int _flash_read (lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); + virtual int _flash_prog (lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); + virtual int _flash_erase (lfs_block_t block); + virtual void _flash_erase_all(); + virtual int _flash_sync (); + virtual BluefruitFS::File _f_openNextFile (void* fhdl, char const* cwd, uint8_t mode); virtual void _f_rewindDirectory (void* fhdl); - private: struct lfs_config _lfs_cfg; lfs_t _lfs; - bool _begun; BluefruitFS::File _open_file (char const *filepath, uint8_t mode); BluefruitFS::File _open_dir (char const *filepath); + + static int _iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); + static int _iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); + static int _iflash_erase (const struct lfs_config *c, lfs_block_t block); + static int _iflash_sync (const struct lfs_config *c); }; extern LittleFS InternalFS; diff --git a/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino b/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino index 8104fcbc..31712934 100755 --- a/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino +++ b/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino @@ -39,6 +39,7 @@ const int chipSelectPin = 7; void setup() { Serial.begin(9600); + while ( !Serial ) delay(10); // for nrf52840 with native usb // start the SPI library: SPI.begin(); diff --git a/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino index bf852bf2..4ecbff3f 100644 --- a/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino +++ b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino @@ -9,6 +9,9 @@ * TX is digital pin A1 (connect to RX of other device) This example code is in the public domain. + + Software Serial will not work when Bluetooth Radio is active because the softdevice + interrupts the library. */ diff --git a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino index 88c4284a..b1e2ac98 100644 --- a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino +++ b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino @@ -18,6 +18,9 @@ This example code is in the public domain. + Software Serial will not work when Bluetooth Radio is active because the softdevice + interrupts the library. + */ #include diff --git a/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino b/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino index 9c41c18f..f41511c1 100755 --- a/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino +++ b/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino @@ -16,6 +16,7 @@ void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial communication at 9600bps + while ( !Serial ) delay(10); // for nrf52840 with native usb } int reading = 0; diff --git a/libraries/Wire/examples/master_reader/master_reader.ino b/libraries/Wire/examples/master_reader/master_reader.ino index 4124d7d6..ee59610e 100755 --- a/libraries/Wire/examples/master_reader/master_reader.ino +++ b/libraries/Wire/examples/master_reader/master_reader.ino @@ -16,6 +16,7 @@ void setup() { Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial for output + while ( !Serial ) delay(10); // for nrf52840 with native usb } void loop() diff --git a/libraries/Wire/examples/slave_receiver/slave_receiver.ino b/libraries/Wire/examples/slave_receiver/slave_receiver.ino index 60dd4bdd..c0c26286 100755 --- a/libraries/Wire/examples/slave_receiver/slave_receiver.ino +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -17,6 +17,7 @@ void setup() Wire.begin(4); // join i2c bus with address #4 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output + while ( !Serial ) delay(10); // for nrf52840 with native usb } void loop() diff --git a/tools/uf2conv/uf2conv.py b/tools/uf2conv/uf2conv.py index 2418d8e4..4a1455e0 100644 --- a/tools/uf2conv/uf2conv.py +++ b/tools/uf2conv/uf2conv.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python3 import sys import struct import subprocess @@ -8,6 +7,7 @@ import os import os.path import argparse + UF2_MAGIC_START0 = 0x0A324655 # "UF2\n" UF2_MAGIC_START1 = 0x9E5D5157 # Randomly selected UF2_MAGIC_END = 0x0AB16F30 # Ditto @@ -26,27 +26,31 @@ INFO_FILE = "/INFO_UF2.TXT" appstartaddr = 0x2000 familyid = 0x0 -def isUF2(buf): + +def is_uf2(buf): w = struct.unpack(" 0: padding -= 4 - outp += "\x00\x00\x00\x00" + outp += b"\x00\x00\x00\x00" outp += block[32 : 32 + datalen] curraddr = newaddr + datalen return outp -def convertToUF2(fileContent): +def convert_to_carray(file_content): + outp = "const unsigned char bindata[] __attribute__((aligned(16))) = {" + for i in range(len(file_content)): + if i % 16 == 0: + outp += "\n" + outp += "0x%02x, " % ord(file_content[i]) + outp += "\n};\n" + return outp + +def convert_to_uf2(file_content): global familyid - datapadding = "" + datapadding = b"" while len(datapadding) < 512 - 256 - 32 - 4: - datapadding += "\x00\x00\x00\x00" - numblocks = (len(fileContent) + 255) / 256 - outp = "" - for blockno in range(0, numblocks): + datapadding += b"\x00\x00\x00\x00" + numblocks = (len(file_content) + 255) // 256 + outp = b"" + for blockno in range(numblocks): ptr = 256 * blockno - chunk = fileContent[ptr:ptr + 256] + chunk = file_content[ptr:ptr + 256] flags = 0x0 if familyid: flags |= 0x2000 - hd = struct.pack("= 3 and words[1] == "2" and words[2] == "FAT": @@ -176,36 +187,41 @@ def getdrives(): rootpath = tmp for d in os.listdir(rootpath): drives.append(os.path.join(rootpath, d)) - - def hasInfo(d): + + + def has_info(d): try: return os.path.isfile(d + INFO_FILE) except: return False - - return filter(hasInfo, drives) -def boardID(path): + return list(filter(has_info, drives)) + + +def board_id(path): with open(path + INFO_FILE, mode='r') as file: - fileContent = file.read() - return re.search("Board-ID: ([^\r\n]*)", fileContent).group(1) - -def listdrives(): - for d in getdrives(): - print d, boardID(d) + file_content = file.read() + return re.search("Board-ID: ([^\r\n]*)", file_content).group(1) -def writeFile(name, buf): + +def list_drives(): + for d in get_drives(): + print(d, board_id(d)) + + +def write_file(name, buf): with open(name, "wb") as f: f.write(buf) - print "Wrote %d bytes to %s." % (len(buf), name) + print("Wrote %d bytes to %s." % (len(buf), name)) + def main(): global appstartaddr, familyid def error(msg): - print msg + print(msg) sys.exit(1) parser = argparse.ArgumentParser(description='Convert to UF2 or flash directly.') - parser.add_argument('input', metavar='INPUT', type=str, nargs='?', + parser.add_argument('input', metavar='INPUT', type=str, nargs='?', help='input file (HEX, BIN or UF2)') parser.add_argument('-b' , '--base', dest='base', type=str, default="0x2000", @@ -221,6 +237,8 @@ def main(): parser.add_argument('-f' , '--family', dest='family', type=str, default="0x0", help='specify familyID - number or name (default: 0x0)') + parser.add_argument('-C' , '--carray', action='store_true', + help='convert binary file to a C array, not UF2') args = parser.parse_args() appstartaddr = int(args.base, 0) @@ -233,38 +251,42 @@ def main(): error("Family ID needs to be a number or one of: " + ", ".join(families.keys())) if args.list: - listdrives() + list_drives() else: if not args.input: error("Need input file") - with open(args.input, mode='rb') as file: - inpbuf = file.read() - fromUF2 = isUF2(inpbuf) + with open(args.input, mode='rb') as f: + inpbuf = f.read() + from_uf2 = is_uf2(inpbuf) ext = "uf2" - if fromUF2: - outbuf = convertFromUF2(inpbuf) + if from_uf2: + outbuf = convert_from_uf2(inpbuf) ext = "bin" - elif isHEX(inpbuf): - outbuf = convertFromHexToUF2(inpbuf) + elif is_hex(inpbuf): + outbuf = convert_from_hex_to_uf2(inpbuf.decode("utf-8")) + elif args.carray: + outbuf = convert_to_carray(inpbuf) + ext = "h" else: - outbuf = convertToUF2(inpbuf) - print "Converting to %s, output size: %d, start address: 0x%x" % (ext, len(outbuf), appstartaddr) - + outbuf = convert_to_uf2(inpbuf) + print("Converting to %s, output size: %d, start address: 0x%x" % + (ext, len(outbuf), appstartaddr)) if args.convert: drives = [] if args.output == None: args.output = "flash." + ext else: - drives = getdrives() - + drives = get_drives() + if args.output: - writeFile(args.output, outbuf) + write_file(args.output, outbuf) else: if len(drives) == 0: error("No drive to deploy.") for d in drives: - print "Flashing %s (%s)" % (d, boardID(d)) - writeFile(outbuf, d + "/NEW.UF2") + print("Flashing %s (%s)" % (d, board_id(d))) + write_file(d + "/NEW.UF2", outbuf) + if __name__ == "__main__": - main() + main() \ No newline at end of file