Merge branch 'master' into develop
This commit is contained in:
commit
c88bd0092f
21 files changed
+273
-153
No files matched your search
+3
-3
@@ -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)
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
+2
-1
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"{
|
||||
|
||||
@@ -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 : ");
|
||||
|
||||
+1
@@ -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);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <bluefruit.h>
|
||||
|
||||
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; i<pincount; i++)
|
||||
{
|
||||
|
||||
@@ -596,7 +596,7 @@ uint16_t BLECharacteristic::read16(void)
|
||||
|
||||
uint32_t BLECharacteristic::read32(void)
|
||||
{
|
||||
uint16_t num;
|
||||
uint32_t num;
|
||||
return read(&num, sizeof(num)) ? num : 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,11 +42,23 @@
|
||||
#include "usb/usb.h"
|
||||
#endif
|
||||
|
||||
#ifndef CFG_BLE_TX_POWER_LEVEL
|
||||
#define CFG_BLE_TX_POWER_LEVEL 0
|
||||
#define CFG_DEFAULT_NAME "Bluefruit52"
|
||||
#endif
|
||||
|
||||
#ifndef CFG_DEFAULT_NAME
|
||||
#define CFG_DEFAULT_NAME "Bluefruit52"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef CFG_BLE_TASK_STACKSIZE
|
||||
#define CFG_BLE_TASK_STACKSIZE (512*3)
|
||||
#endif
|
||||
|
||||
#ifndef CFG_SOC_TASK_STACKSIZE
|
||||
#define CFG_SOC_TASK_STACKSIZE (200)
|
||||
#endif
|
||||
|
||||
|
||||
AdafruitBluefruit Bluefruit;
|
||||
|
||||
|
||||
@@ -81,6 +81,8 @@ bool BLEClientHidAdafruit::begin(void)
|
||||
// set notify callback
|
||||
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
|
||||
_mse_boot_input.setNotifyCallback(mse_client_notify_cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::setKeyboardReportCallback(kbd_callback_t fp)
|
||||
@@ -140,12 +142,12 @@ bool BLEClientHidAdafruit::keyboardPresent(void)
|
||||
|
||||
bool BLEClientHidAdafruit::enableKeyboard(void)
|
||||
{
|
||||
_kbd_boot_input.enableNotify();
|
||||
return _kbd_boot_input.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::disableKeyboard(void)
|
||||
{
|
||||
_kbd_boot_input.disableNotify();
|
||||
return _kbd_boot_input.disableNotify();
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::_handle_kbd_input(uint8_t* data, uint16_t len)
|
||||
@@ -171,12 +173,12 @@ bool BLEClientHidAdafruit::mousePresent(void)
|
||||
|
||||
bool BLEClientHidAdafruit::enableMouse(void)
|
||||
{
|
||||
_mse_boot_input.enableNotify();
|
||||
return _mse_boot_input.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::disableMouse(void)
|
||||
{
|
||||
_mse_boot_input.disableNotify();
|
||||
return _mse_boot_input.disableNotify();
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::_handle_mse_input(uint8_t* data, uint16_t len)
|
||||
|
||||
@@ -81,7 +81,7 @@ EddyStoneUrl::EddyStoneUrl(int8_t rssiAt0m, const char* url)
|
||||
_url = url;
|
||||
}
|
||||
|
||||
bool EddyStoneUrl::setUrl(const char* url)
|
||||
void EddyStoneUrl::setUrl(const char* url)
|
||||
{
|
||||
_url = url;
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ class EddyStoneUrl
|
||||
EddyStoneUrl(void);
|
||||
EddyStoneUrl(int8_t rssiAt0m, const char* url = NULL);
|
||||
|
||||
bool setUrl(const char* url);
|
||||
void setUrl(const char* url);
|
||||
void setRssi(int8_t rssiAt0m);
|
||||
|
||||
bool start(void);
|
||||
|
||||
@@ -118,81 +118,33 @@ static inline uint32_t lba2addr(uint32_t block)
|
||||
return ((uint32_t) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
static int _iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
|
||||
{
|
||||
(void) c;
|
||||
|
||||
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.
|
||||
static int _iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
|
||||
lfs_size_t size)
|
||||
{
|
||||
(void) c;
|
||||
|
||||
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.
|
||||
static int _iflash_erase (const struct lfs_config *c, lfs_block_t block)
|
||||
{
|
||||
(void) c;
|
||||
|
||||
uint32_t addr = lba2addr(block);
|
||||
|
||||
// implement as write 0xff to whole block address
|
||||
for(int i=0; i <LFS_BLOCK_SIZE; i++)
|
||||
{
|
||||
flash_nrf5x_write8(addr + i, 0xFF);
|
||||
}
|
||||
|
||||
// flash_nrf5x_flush();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Sync the state of the underlying block device. Negative error codes
|
||||
// are propogated to the user.
|
||||
static int _iflash_sync (const struct lfs_config *c)
|
||||
{
|
||||
(void) c;
|
||||
flash_nrf5x_flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Implementation
|
||||
//--------------------------------------------------------------------+
|
||||
LittleFS InternalFS;
|
||||
|
||||
LittleFS::LittleFS (void)
|
||||
LittleFS::LittleFS (void) :
|
||||
LittleFS( LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE, 128)
|
||||
{
|
||||
}
|
||||
|
||||
LittleFS::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)
|
||||
{
|
||||
varclr(&_lfs_cfg);
|
||||
_lfs_cfg.context = NULL;
|
||||
_lfs_cfg.context = this;
|
||||
_lfs_cfg.read = _iflash_read;
|
||||
_lfs_cfg.prog = _iflash_prog;
|
||||
_lfs_cfg.erase = _iflash_erase;
|
||||
_lfs_cfg.sync = _iflash_sync;
|
||||
|
||||
_lfs_cfg.read_size = LFS_BLOCK_SIZE;
|
||||
_lfs_cfg.prog_size = LFS_BLOCK_SIZE;
|
||||
_lfs_cfg.block_size = LFS_BLOCK_SIZE;
|
||||
_lfs_cfg.block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE;
|
||||
_lfs_cfg.lookahead = 128;
|
||||
_lfs_cfg.read_size = read_size;
|
||||
_lfs_cfg.prog_size = prog_size;
|
||||
_lfs_cfg.block_size = block_size;
|
||||
_lfs_cfg.block_count = block_count;
|
||||
_lfs_cfg.lookahead = lookahead;
|
||||
|
||||
_begun = false;
|
||||
_mounted = false;
|
||||
}
|
||||
|
||||
LittleFS::~LittleFS ()
|
||||
@@ -212,6 +164,8 @@ bool LittleFS::begin (void)
|
||||
{
|
||||
LOG_LV1("IFLASH", "Format internal file system");
|
||||
this->format(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 <LFS_BLOCK_SIZE; i++)
|
||||
{
|
||||
flash_nrf5x_write8(addr + i, 0xFF);
|
||||
}
|
||||
|
||||
// flash_nrf5x_flush();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LittleFS::_flash_erase_all()
|
||||
{
|
||||
for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
|
||||
{
|
||||
flash_nrf5x_erase(addr);
|
||||
}
|
||||
}
|
||||
|
||||
// Sync the state of the underlying block device. Negative error codes
|
||||
// are propogated to the user.
|
||||
int LittleFS::_flash_sync ()
|
||||
{
|
||||
flash_nrf5x_flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LittleFS::_iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
|
||||
{
|
||||
return ((LittleFS *)c->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();
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@@ -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 <SoftwareSerial.h>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
+92
-70
@@ -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("<II", buf[0:8])
|
||||
return w[0] == UF2_MAGIC_START0 and w[1] == UF2_MAGIC_START1
|
||||
|
||||
def isHEX(buf):
|
||||
w = buf[0:30]
|
||||
if w[0] == ':' and re.match("^[:0-9a-fA-F\r\n]+$", buf):
|
||||
def is_hex(buf):
|
||||
try:
|
||||
w = buf[0:30].decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return False
|
||||
if w[0] == ':' and re.match(b"^[:0-9a-fA-F\r\n]+$", buf):
|
||||
return True
|
||||
return False
|
||||
|
||||
def convertFromUF2(buf):
|
||||
def convert_from_uf2(buf):
|
||||
global appstartaddr
|
||||
numblocks = len(buf) / 512
|
||||
numblocks = len(buf) // 512
|
||||
curraddr = None
|
||||
outp = ""
|
||||
for blockno in range(0, numblocks):
|
||||
outp = b""
|
||||
for blockno in range(numblocks):
|
||||
ptr = blockno * 512
|
||||
block = buf[ptr:ptr + 512]
|
||||
hd = struct.unpack("<IIIIIIII", block[0:32])
|
||||
hd = struct.unpack(b"<IIIIIIII", block[0:32])
|
||||
if hd[0] != UF2_MAGIC_START0 or hd[1] != UF2_MAGIC_START1:
|
||||
print "Skipping block at " + ptr + "; bad magic"
|
||||
print("Skipping block at " + ptr + "; bad magic")
|
||||
continue
|
||||
if hd[2] & 1:
|
||||
# NO-flash flag set; skip block
|
||||
@@ -67,30 +71,39 @@ def convertFromUF2(buf):
|
||||
assert False, "Non-word padding size at " + ptr
|
||||
while padding > 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("<IIIIIIII",
|
||||
UF2_MAGIC_START0, UF2_MAGIC_START1,
|
||||
hd = struct.pack(b"<IIIIIIII",
|
||||
UF2_MAGIC_START0, UF2_MAGIC_START1,
|
||||
flags, ptr + appstartaddr, 256, blockno, numblocks, familyid)
|
||||
while len(chunk) < 256:
|
||||
chunk += "\x00"
|
||||
block = hd + chunk + datapadding + struct.pack("<I", UF2_MAGIC_END)
|
||||
chunk += b"\x00"
|
||||
block = hd + chunk + datapadding + struct.pack(b"<I", UF2_MAGIC_END)
|
||||
assert len(block) == 512
|
||||
outp += block
|
||||
return outp
|
||||
@@ -98,26 +111,23 @@ def convertToUF2(fileContent):
|
||||
class Block:
|
||||
def __init__(self, addr):
|
||||
self.addr = addr
|
||||
self.bytes = []
|
||||
for i in range(0, 256):
|
||||
self.bytes.append(0)
|
||||
self.bytes = bytearray(256)
|
||||
|
||||
def encode(self, blockno, numblocks):
|
||||
global familyid
|
||||
flags = 0x0
|
||||
if familyid:
|
||||
flags |= 0x2000
|
||||
hd = struct.pack("<IIIIIIII",
|
||||
UF2_MAGIC_START0, UF2_MAGIC_START1,
|
||||
hd = struct.pack("<IIIIIIII",
|
||||
UF2_MAGIC_START0, UF2_MAGIC_START1,
|
||||
flags, self.addr, 256, blockno, numblocks, familyid)
|
||||
for i in range(0, 256):
|
||||
hd += chr(self.bytes[i])
|
||||
hd += self.bytes[0:256]
|
||||
while len(hd) < 512 - 4:
|
||||
hd += "\x00"
|
||||
hd += b"\x00"
|
||||
hd += struct.pack("<I", UF2_MAGIC_END)
|
||||
return hd
|
||||
|
||||
def convertFromHexToUF2(buf):
|
||||
|
||||
def convert_from_hex_to_uf2(buf):
|
||||
global appstartaddr
|
||||
appstartaddr = None
|
||||
upper = 0
|
||||
@@ -152,16 +162,17 @@ def convertFromHexToUF2(buf):
|
||||
addr += 1
|
||||
i += 1
|
||||
numblocks = len(blocks)
|
||||
resfile = ""
|
||||
resfile = b""
|
||||
for i in range(0, numblocks):
|
||||
resfile += blocks[i].encode(i, numblocks)
|
||||
return resfile
|
||||
|
||||
|
||||
def getdrives():
|
||||
def get_drives():
|
||||
drives = []
|
||||
if sys.platform == "win32":
|
||||
r = subprocess.check_output(["wmic", "PATH", "Win32_LogicalDisk", "get", "DeviceID,", "VolumeName,", "FileSystem,", "DriveType"])
|
||||
r = subprocess.check_output(["wmic", "PATH", "Win32_LogicalDisk",
|
||||
"get", "DeviceID,", "VolumeName,",
|
||||
"FileSystem,", "DriveType"])
|
||||
for line in r.split('\n'):
|
||||
words = re.split('\s+', line)
|
||||
if len(words) >= 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()
|
||||
Reference in new issue
Block a user