mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-03-30 19:15:49 +00:00
Compare commits
43 Commits
room-serve
...
room-serve
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65d398fcbc | ||
|
|
436a99f088 | ||
|
|
b11f43987b | ||
|
|
1680eb29aa | ||
|
|
6dc9920be7 | ||
|
|
f38532b56d | ||
|
|
7576d45a8d | ||
|
|
1de46eae4c | ||
|
|
9f5d7a28ce | ||
|
|
3c02ac604d | ||
|
|
8007aad7a3 | ||
|
|
d2377c91ab | ||
|
|
cf1c863cc2 | ||
|
|
6c0d94aa2d | ||
|
|
74c1ff3d6d | ||
|
|
8b3d60abe7 | ||
|
|
c69657a13b | ||
|
|
e291b57a07 | ||
|
|
a56e9ef62f | ||
|
|
ed01859c12 | ||
|
|
a9b64b31b7 | ||
|
|
b035487101 | ||
|
|
805ca7b900 | ||
|
|
2ea05a5182 | ||
|
|
177dd90ca1 | ||
|
|
62a5115cc9 | ||
|
|
64b7a14a66 | ||
|
|
11b90e8876 | ||
|
|
76639e2a68 | ||
|
|
3c2781cce1 | ||
|
|
6218c1e7ae | ||
|
|
b08436eba7 | ||
|
|
dd16197eae | ||
|
|
c37622b4a0 | ||
|
|
7a83f75e60 | ||
|
|
7693274edd | ||
|
|
e88a710d0f | ||
|
|
4a15b8b0c9 | ||
|
|
35e1901d0e | ||
|
|
bce5dc9796 | ||
|
|
b92e2abe75 | ||
|
|
ae5052fec7 | ||
|
|
445179f53a |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,3 +6,5 @@
|
||||
.vscode/ipch
|
||||
out/
|
||||
.direnv/
|
||||
.DS_Store
|
||||
.vscode/settings.json
|
||||
|
||||
1
arch/stm32/Adafruit_LittleFS_stm32/README.md
Normal file
1
arch/stm32/Adafruit_LittleFS_stm32/README.md
Normal file
@@ -0,0 +1 @@
|
||||
This is LittleFS from Adafruit, stripped from things that makes it not compile with stm32 (refs to TinyUSB and free_rtos, mostly)
|
||||
10
arch/stm32/Adafruit_LittleFS_stm32/library.properties
Normal file
10
arch/stm32/Adafruit_LittleFS_stm32/library.properties
Normal file
@@ -0,0 +1,10 @@
|
||||
name=Adafruit Little File System Libraries
|
||||
version=0.11.0
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for ARM Little File System
|
||||
paragraph=Arduino library for ARM Little File System
|
||||
category=Data Storage
|
||||
url=https://github.com/adafruit/Adafruit_nRF52_Arduino
|
||||
architectures=*
|
||||
includes=Adafruit_LittleFS.h
|
||||
273
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS.cpp
Normal file
273
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <string.h>
|
||||
#include "Adafruit_LittleFS.h"
|
||||
|
||||
//#include <Adafruit_TinyUSB.h> // for Serial
|
||||
|
||||
using namespace Adafruit_LittleFS_Namespace;
|
||||
|
||||
#define memclr(buffer, size) memset(buffer, 0, size)
|
||||
#define varclr(_var) memclr(_var, sizeof(*(_var)))
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Implementation
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
Adafruit_LittleFS::Adafruit_LittleFS (void)
|
||||
: Adafruit_LittleFS(NULL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config* cfg)
|
||||
{
|
||||
varclr(&_lfs);
|
||||
_lfs_cfg = cfg;
|
||||
_mounted = false;
|
||||
// _mutex = xSemaphoreCreateMutexStatic(&this->_MutexStorageSpace);
|
||||
}
|
||||
|
||||
Adafruit_LittleFS::~Adafruit_LittleFS ()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Initialize and mount the file system
|
||||
// Return true if mounted successfully else probably corrupted.
|
||||
// User should format the disk and try again
|
||||
bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
bool ret;
|
||||
// not a loop, just an quick way to short-circuit on error
|
||||
do {
|
||||
if (_mounted) { ret = true; break; }
|
||||
if (cfg) { _lfs_cfg = cfg; }
|
||||
if (nullptr == _lfs_cfg) { ret = false; break; }
|
||||
// actually attempt to mount, and log error if one occurs
|
||||
int err = lfs_mount(&_lfs, _lfs_cfg);
|
||||
PRINT_LFS_ERR(err);
|
||||
_mounted = (err == LFS_ERR_OK);
|
||||
ret = _mounted;
|
||||
} while(0);
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Tear down and unmount file system
|
||||
void Adafruit_LittleFS::end(void)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
if (_mounted)
|
||||
{
|
||||
_mounted = false;
|
||||
int err = lfs_unmount(&_lfs);
|
||||
PRINT_LFS_ERR(err);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
_unlockFS();
|
||||
}
|
||||
|
||||
bool Adafruit_LittleFS::format (void)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
int err = LFS_ERR_OK;
|
||||
bool attemptMount = _mounted;
|
||||
// not a loop, just an quick way to short-circuit on error
|
||||
do
|
||||
{
|
||||
// if already mounted: umount first -> format -> remount
|
||||
if (_mounted)
|
||||
{
|
||||
_mounted = false;
|
||||
err = lfs_unmount(&_lfs);
|
||||
if ( LFS_ERR_OK != err) { PRINT_LFS_ERR(err); break; }
|
||||
}
|
||||
err = lfs_format(&_lfs, _lfs_cfg);
|
||||
if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR(err); break; }
|
||||
|
||||
if (attemptMount)
|
||||
{
|
||||
err = lfs_mount(&_lfs, _lfs_cfg);
|
||||
if ( LFS_ERR_OK != err ) { PRINT_LFS_ERR(err); break; }
|
||||
_mounted = true;
|
||||
}
|
||||
// success!
|
||||
} while(0);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
// Open a file or folder
|
||||
Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
|
||||
{
|
||||
// No lock is required here ... the File() object will synchronize with the mutex provided
|
||||
return Adafruit_LittleFS_Namespace::File(filepath, mode, *this);
|
||||
}
|
||||
|
||||
// Check if file or folder exists
|
||||
bool Adafruit_LittleFS::exists (char const *filepath)
|
||||
{
|
||||
struct lfs_info info;
|
||||
_lockFS();
|
||||
|
||||
bool ret = (0 == lfs_stat(&_lfs, filepath, &info));
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// Create a directory, create intermediate parent if needed
|
||||
bool Adafruit_LittleFS::mkdir (char const *filepath)
|
||||
{
|
||||
bool ret = true;
|
||||
const char* slash = filepath;
|
||||
if ( slash[0] == '/' ) slash++; // skip root '/'
|
||||
|
||||
_lockFS();
|
||||
|
||||
// make intermediate parent directory(ies)
|
||||
while ( NULL != (slash = strchr(slash, '/')) )
|
||||
{
|
||||
char parent[slash - filepath + 1] = { 0 };
|
||||
memcpy(parent, filepath, slash - filepath);
|
||||
|
||||
int rc = lfs_mkdir(&_lfs, parent);
|
||||
if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
|
||||
{
|
||||
PRINT_LFS_ERR(rc);
|
||||
ret = false;
|
||||
break;
|
||||
}
|
||||
slash++;
|
||||
}
|
||||
// make the final requested directory
|
||||
if (ret)
|
||||
{
|
||||
int rc = lfs_mkdir(&_lfs, filepath);
|
||||
if ( rc != LFS_ERR_OK && rc != LFS_ERR_EXIST )
|
||||
{
|
||||
PRINT_LFS_ERR(rc);
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Remove a file
|
||||
bool Adafruit_LittleFS::remove (char const *filepath)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
// Rename a file
|
||||
bool Adafruit_LittleFS::rename (char const *oldfilepath, char const *newfilepath)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_rename(&_lfs, oldfilepath, newfilepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
// Remove a folder
|
||||
bool Adafruit_LittleFS::rmdir (char const *filepath)
|
||||
{
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
// Remove a folder recursively
|
||||
bool Adafruit_LittleFS::rmdir_r (char const *filepath)
|
||||
{
|
||||
/* adafruit: lfs is modified to remove non-empty folder,
|
||||
According to below issue, comment these 2 line won't corrupt filesystem
|
||||
at least when using LFS v1. If moving to LFS v2, see tracked issue
|
||||
to see if issues (such as the orphans in threaded linked list) are resolved.
|
||||
https://github.com/ARMmbed/littlefs/issues/43
|
||||
*/
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
//------------- Debug -------------//
|
||||
#if CFG_DEBUG
|
||||
|
||||
const char* dbg_strerr_lfs (int32_t err)
|
||||
{
|
||||
switch ( err )
|
||||
{
|
||||
case LFS_ERR_OK : return "LFS_ERR_OK";
|
||||
case LFS_ERR_IO : return "LFS_ERR_IO";
|
||||
case LFS_ERR_CORRUPT : return "LFS_ERR_CORRUPT";
|
||||
case LFS_ERR_NOENT : return "LFS_ERR_NOENT";
|
||||
case LFS_ERR_EXIST : return "LFS_ERR_EXIST";
|
||||
case LFS_ERR_NOTDIR : return "LFS_ERR_NOTDIR";
|
||||
case LFS_ERR_ISDIR : return "LFS_ERR_ISDIR";
|
||||
case LFS_ERR_NOTEMPTY : return "LFS_ERR_NOTEMPTY";
|
||||
case LFS_ERR_BADF : return "LFS_ERR_BADF";
|
||||
case LFS_ERR_INVAL : return "LFS_ERR_INVAL";
|
||||
case LFS_ERR_NOSPC : return "LFS_ERR_NOSPC";
|
||||
case LFS_ERR_NOMEM : return "LFS_ERR_NOMEM";
|
||||
|
||||
default:
|
||||
static char errcode[10];
|
||||
sprintf(errcode, "%ld", err);
|
||||
return errcode;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
102
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS.h
Normal file
102
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ADAFRUIT_LITTLEFS_H_
|
||||
#define ADAFRUIT_LITTLEFS_H_
|
||||
|
||||
#include <Stream.h>
|
||||
|
||||
// Internal Flash uses ARM Little FileSystem
|
||||
// https://github.com/ARMmbed/littlefs
|
||||
#include "littlefs/lfs.h"
|
||||
#include "Adafruit_LittleFS_File.h"
|
||||
//#include "rtos.h" // tied to FreeRTOS for serialization
|
||||
|
||||
class Adafruit_LittleFS
|
||||
{
|
||||
public:
|
||||
Adafruit_LittleFS (void);
|
||||
Adafruit_LittleFS (struct lfs_config* cfg);
|
||||
virtual ~Adafruit_LittleFS ();
|
||||
|
||||
bool begin(struct lfs_config * cfg = NULL);
|
||||
void end(void);
|
||||
|
||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
||||
// write, etc). Returns a File object for interacting with the file.
|
||||
// Note that currently only one file can be open at a time.
|
||||
Adafruit_LittleFS_Namespace::File open (char const *filename, uint8_t mode = Adafruit_LittleFS_Namespace::FILE_O_READ);
|
||||
|
||||
// Methods to determine if the requested file path exists.
|
||||
bool exists (char const *filepath);
|
||||
|
||||
// Create the requested directory hierarchy--if intermediate directories
|
||||
// do not exist they will be created.
|
||||
bool mkdir (char const *filepath);
|
||||
|
||||
// Delete the file.
|
||||
bool remove (char const *filepath);
|
||||
|
||||
// Rename the file.
|
||||
bool rename (char const *oldfilepath, char const *newfilepath);
|
||||
|
||||
// Delete a folder (must be empty)
|
||||
bool rmdir (char const *filepath);
|
||||
|
||||
// Delete a folder (recursively)
|
||||
bool rmdir_r (char const *filepath);
|
||||
|
||||
// format file system
|
||||
bool format (void);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
lfs_t* _getFS (void) { return &_lfs; }
|
||||
void _lockFS (void) { }//xSemaphoreTake(_mutex, portMAX_DELAY); }
|
||||
void _unlockFS(void) { }//xSemaphoreGive(_mutex); }
|
||||
|
||||
protected:
|
||||
bool _mounted;
|
||||
struct lfs_config* _lfs_cfg;
|
||||
lfs_t _lfs;
|
||||
// SemaphoreHandle_t _mutex;
|
||||
|
||||
private:
|
||||
// StaticSemaphore_t _MutexStorageSpace;
|
||||
};
|
||||
|
||||
#if !CFG_DEBUG
|
||||
#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, NULL)
|
||||
#define PRINT_LFS_ERR(_err)
|
||||
#else
|
||||
#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs)
|
||||
#define PRINT_LFS_ERR(_err) do { if (_err) { VERIFY_MESS((long int)_err, dbg_strerr_lfs); } } while(0) // LFS_ERR are of type int, VERIFY_MESS expects long_int
|
||||
|
||||
const char* dbg_strerr_lfs (int32_t err);
|
||||
#endif
|
||||
|
||||
#endif /* ADAFRUIT_LITTLEFS_H_ */
|
||||
@@ -0,0 +1,420 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "Adafruit_LittleFS.h"
|
||||
#include "littlefs/lfs.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
using namespace Adafruit_LittleFS_Namespace;
|
||||
|
||||
File::File (Adafruit_LittleFS &fs)
|
||||
{
|
||||
_fs = &fs;
|
||||
_is_dir = false;
|
||||
_name[0] = 0;
|
||||
_name[LFS_NAME_MAX] = 0;
|
||||
_dir_path = NULL;
|
||||
|
||||
_dir = NULL;
|
||||
_file = NULL;
|
||||
}
|
||||
|
||||
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
|
||||
: File(fs)
|
||||
{
|
||||
// public constructor calls public API open(), which will obtain the mutex
|
||||
this->open(filename, mode);
|
||||
}
|
||||
|
||||
bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
{
|
||||
int flags = (mode == FILE_O_READ) ? LFS_O_RDONLY :
|
||||
(mode == FILE_O_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : 0;
|
||||
|
||||
if ( flags )
|
||||
{
|
||||
_file = (lfs_file_t*) malloc(sizeof(lfs_file_t));
|
||||
if (!_file) return false;
|
||||
|
||||
int rc = lfs_file_open(_fs->_getFS(), _file, filepath, flags);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
// failed to open
|
||||
PRINT_LFS_ERR(rc);
|
||||
// free memory
|
||||
free(_file);
|
||||
_file = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
// move to end of file
|
||||
if ( mode == FILE_O_WRITE ) lfs_file_seek(_fs->_getFS(), _file, 0, LFS_SEEK_END);
|
||||
|
||||
_is_dir = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::_open_dir (char const *filepath)
|
||||
{
|
||||
_dir = (lfs_dir_t*) malloc(sizeof(lfs_dir_t));
|
||||
if (!_dir) return false;
|
||||
|
||||
int rc = lfs_dir_open(_fs->_getFS(), _dir, filepath);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
// failed to open
|
||||
PRINT_LFS_ERR(rc);
|
||||
// free memory
|
||||
free(_dir);
|
||||
_dir = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
_is_dir = true;
|
||||
|
||||
_dir_path = (char*) malloc(strlen(filepath) + 1);
|
||||
strcpy(_dir_path, filepath);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::open (char const *filepath, uint8_t mode)
|
||||
{
|
||||
bool ret = false;
|
||||
_fs->_lockFS();
|
||||
|
||||
ret = this->_open(filepath, mode);
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::_open (char const *filepath, uint8_t mode)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
// close if currently opened
|
||||
if ( this->isOpen() ) _close();
|
||||
|
||||
struct lfs_info info;
|
||||
int rc = lfs_stat(_fs->_getFS(), filepath, &info);
|
||||
|
||||
if ( LFS_ERR_OK == rc )
|
||||
{
|
||||
// file existed, open file or directory accordingly
|
||||
ret = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath);
|
||||
}
|
||||
else if ( LFS_ERR_NOENT == rc )
|
||||
{
|
||||
// file not existed, only proceed with FILE_O_WRITE mode
|
||||
if ( mode == FILE_O_WRITE ) ret = _open_file(filepath, mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT_LFS_ERR(rc);
|
||||
}
|
||||
|
||||
// save bare file name
|
||||
if (ret)
|
||||
{
|
||||
char const* splash = strrchr(filepath, '/');
|
||||
strncpy(_name, splash ? (splash + 1) : filepath, LFS_NAME_MAX);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t File::write (uint8_t ch)
|
||||
{
|
||||
return write(&ch, 1);
|
||||
}
|
||||
|
||||
size_t File::write (uint8_t const *buf, size_t size)
|
||||
{
|
||||
lfs_ssize_t wrcount = 0;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
wrcount = lfs_file_write(_fs->_getFS(), _file, buf, size);
|
||||
if (wrcount < 0)
|
||||
{
|
||||
wrcount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return wrcount;
|
||||
}
|
||||
|
||||
int File::read (void)
|
||||
{
|
||||
// this thin wrapper relies on called function to synchronize
|
||||
int ret = -1;
|
||||
uint8_t ch;
|
||||
if (read(&ch, 1) > 0)
|
||||
{
|
||||
ret = static_cast<int>(ch);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int File::read (void *buf, uint16_t nbyte)
|
||||
{
|
||||
int ret = 0;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_read(_fs->_getFS(), _file, buf, nbyte);
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int File::peek (void)
|
||||
{
|
||||
int ret = -1;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
uint32_t pos = lfs_file_tell(_fs->_getFS(), _file);
|
||||
uint8_t ch = 0;
|
||||
if (lfs_file_read(_fs->_getFS(), _file, &ch, 1) > 0)
|
||||
{
|
||||
ret = static_cast<int>(ch);
|
||||
}
|
||||
(void) lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET);
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int File::available (void)
|
||||
{
|
||||
int ret = 0;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
uint32_t size = lfs_file_size(_fs->_getFS(), _file);
|
||||
uint32_t pos = lfs_file_tell(_fs->_getFS(), _file);
|
||||
ret = size - pos;
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::seek (uint32_t pos)
|
||||
{
|
||||
bool ret = false;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET) >= 0;
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t File::position (void)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_tell(_fs->_getFS(), _file);
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t File::size (void)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_size(_fs->_getFS(), _file);
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::truncate (uint32_t pos)
|
||||
{
|
||||
int32_t ret=LFS_ERR_ISDIR;
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_truncate(_fs->_getFS(), _file, pos);
|
||||
}
|
||||
_fs->_unlockFS();
|
||||
return ( ret == 0 );
|
||||
}
|
||||
|
||||
bool File::truncate (void)
|
||||
{
|
||||
int32_t ret=LFS_ERR_ISDIR;
|
||||
uint32_t pos;
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
pos = lfs_file_tell(_fs->_getFS(), _file);
|
||||
ret = lfs_file_truncate(_fs->_getFS(), _file, pos);
|
||||
}
|
||||
_fs->_unlockFS();
|
||||
return ( ret == 0 );
|
||||
}
|
||||
|
||||
void File::flush (void)
|
||||
{
|
||||
_fs->_lockFS();
|
||||
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
lfs_file_sync(_fs->_getFS(), _file);
|
||||
}
|
||||
|
||||
_fs->_unlockFS();
|
||||
return;
|
||||
}
|
||||
|
||||
void File::close (void)
|
||||
{
|
||||
_fs->_lockFS();
|
||||
this->_close();
|
||||
_fs->_unlockFS();
|
||||
}
|
||||
|
||||
void File::_close(void)
|
||||
{
|
||||
if ( this->isOpen() )
|
||||
{
|
||||
if ( this->_is_dir )
|
||||
{
|
||||
lfs_dir_close(_fs->_getFS(), _dir);
|
||||
free(_dir);
|
||||
_dir = NULL;
|
||||
|
||||
if ( this->_dir_path ) free(_dir_path);
|
||||
_dir_path = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
lfs_file_close(this->_fs->_getFS(), _file);
|
||||
free(_file);
|
||||
_file = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File::operator bool (void)
|
||||
{
|
||||
return isOpen();
|
||||
}
|
||||
|
||||
bool File::isOpen(void)
|
||||
{
|
||||
return (_file != NULL) || (_dir != NULL);
|
||||
}
|
||||
|
||||
// WARNING -- although marked as `const`, the values pointed
|
||||
// to may change. For example, if the same File
|
||||
// object has `open()` called with a different
|
||||
// file or directory name, this same pointer will
|
||||
// suddenly (unexpectedly?) have different values.
|
||||
char const* File::name (void)
|
||||
{
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
bool File::isDirectory (void)
|
||||
{
|
||||
return this->_is_dir;
|
||||
}
|
||||
|
||||
File File::openNextFile (uint8_t mode)
|
||||
{
|
||||
_fs->_lockFS();
|
||||
|
||||
File ret(*_fs);
|
||||
if (this->_is_dir)
|
||||
{
|
||||
struct lfs_info info;
|
||||
int rc;
|
||||
|
||||
// lfs_dir_read returns 0 when reaching end of directory, 1 if found an entry
|
||||
// Skip the "." and ".." entries ...
|
||||
do
|
||||
{
|
||||
rc = lfs_dir_read(_fs->_getFS(), _dir, &info);
|
||||
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
|
||||
|
||||
if ( rc == 1 )
|
||||
{
|
||||
// string cat name with current folder
|
||||
char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1]; // potential for significant stack usage
|
||||
strcpy(filepath, _dir_path);
|
||||
if ( !(_dir_path[0] == '/' && _dir_path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
|
||||
strcat(filepath, info.name);
|
||||
|
||||
(void)ret._open(filepath, mode); // return value is ignored ... caller is expected to check isOpened()
|
||||
}
|
||||
else if ( rc < 0 )
|
||||
{
|
||||
PRINT_LFS_ERR(rc);
|
||||
}
|
||||
}
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void File::rewindDirectory (void)
|
||||
{
|
||||
_fs->_lockFS();
|
||||
if (this->_is_dir)
|
||||
{
|
||||
lfs_dir_rewind(_fs->_getFS(), _dir);
|
||||
}
|
||||
_fs->_unlockFS();
|
||||
}
|
||||
|
||||
108
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS_File.h
Normal file
108
arch/stm32/Adafruit_LittleFS_stm32/src/Adafruit_LittleFS_File.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ADAFRUIT_LITTLEFS_FILE_H_
|
||||
#define ADAFRUIT_LITTLEFS_FILE_H_
|
||||
|
||||
// Forward declaration
|
||||
class Adafruit_LittleFS;
|
||||
|
||||
namespace Adafruit_LittleFS_Namespace
|
||||
{
|
||||
|
||||
// avoid conflict with other FileSystem FILE_READ/FILE_WRITE
|
||||
enum
|
||||
{
|
||||
FILE_O_READ = 0,
|
||||
FILE_O_WRITE = 1,
|
||||
};
|
||||
|
||||
class File : public Stream
|
||||
{
|
||||
public:
|
||||
File (Adafruit_LittleFS &fs);
|
||||
File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs);
|
||||
|
||||
public:
|
||||
|
||||
bool open (char const *filename, uint8_t mode);
|
||||
|
||||
//------------- Stream API -------------//
|
||||
virtual size_t write (uint8_t ch);
|
||||
virtual size_t write (uint8_t const *buf, size_t size);
|
||||
size_t write(const char *str) {
|
||||
if (str == NULL) return 0;
|
||||
return write((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
size_t write(const char *buffer, size_t size) {
|
||||
return write((const uint8_t *)buffer, size);
|
||||
}
|
||||
|
||||
virtual int read (void);
|
||||
int read (void *buf, uint16_t nbyte);
|
||||
|
||||
virtual int peek (void);
|
||||
virtual int available (void);
|
||||
virtual void flush (void);
|
||||
|
||||
bool seek (uint32_t pos);
|
||||
uint32_t position (void);
|
||||
uint32_t size (void);
|
||||
|
||||
bool truncate (uint32_t pos);
|
||||
bool truncate (void);
|
||||
|
||||
void close (void);
|
||||
|
||||
operator bool (void);
|
||||
|
||||
bool isOpen(void);
|
||||
char const* name (void);
|
||||
|
||||
bool isDirectory (void);
|
||||
File openNextFile (uint8_t mode = FILE_O_READ);
|
||||
void rewindDirectory (void);
|
||||
|
||||
private:
|
||||
Adafruit_LittleFS* _fs;
|
||||
|
||||
bool _is_dir;
|
||||
|
||||
union {
|
||||
lfs_file_t* _file;
|
||||
lfs_dir_t* _dir;
|
||||
};
|
||||
|
||||
char* _dir_path;
|
||||
char _name[LFS_NAME_MAX+1];
|
||||
|
||||
bool _open(char const *filepath, uint8_t mode);
|
||||
bool _open_file(char const *filepath, uint8_t mode);
|
||||
bool _open_dir (char const *filepath);
|
||||
void _close(void);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* ADAFRUIT_LITTLEFS_FILE_H_ */
|
||||
24
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/LICENSE.md
Normal file
24
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/LICENSE.md
Normal file
@@ -0,0 +1,24 @@
|
||||
Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice, this
|
||||
list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
- Neither the name of ARM nor the names of its contributors may be used to
|
||||
endorse or promote products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
177
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/README.md
Normal file
177
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/README.md
Normal file
@@ -0,0 +1,177 @@
|
||||
## The little filesystem
|
||||
|
||||
A little fail-safe filesystem designed for embedded systems.
|
||||
|
||||
```
|
||||
| | | .---._____
|
||||
.-----. | |
|
||||
--|o |---| littlefs |
|
||||
--| |---| |
|
||||
'-----' '----------'
|
||||
| | |
|
||||
```
|
||||
|
||||
**Bounded RAM/ROM** - The littlefs is designed to work with a limited amount
|
||||
of memory. Recursion is avoided and dynamic memory is limited to configurable
|
||||
buffers that can be provided statically.
|
||||
|
||||
**Power-loss resilient** - The littlefs is designed for systems that may have
|
||||
random power failures. The littlefs has strong copy-on-write guarantees and
|
||||
storage on disk is always kept in a valid state.
|
||||
|
||||
**Wear leveling** - Since the most common form of embedded storage is erodible
|
||||
flash memories, littlefs provides a form of dynamic wear leveling for systems
|
||||
that can not fit a full flash translation layer.
|
||||
|
||||
## Example
|
||||
|
||||
Here's a simple example that updates a file named `boot_count` every time
|
||||
main runs. The program can be interrupted at any time without losing track
|
||||
of how many times it has been booted and without corrupting the filesystem:
|
||||
|
||||
``` c
|
||||
#include "lfs.h"
|
||||
|
||||
// variables used by the filesystem
|
||||
lfs_t lfs;
|
||||
lfs_file_t file;
|
||||
|
||||
// configuration of the filesystem is provided by this struct
|
||||
const struct lfs_config cfg = {
|
||||
// block device operations
|
||||
.read = user_provided_block_device_read,
|
||||
.prog = user_provided_block_device_prog,
|
||||
.erase = user_provided_block_device_erase,
|
||||
.sync = user_provided_block_device_sync,
|
||||
|
||||
// block device configuration
|
||||
.read_size = 16,
|
||||
.prog_size = 16,
|
||||
.block_size = 4096,
|
||||
.block_count = 128,
|
||||
.lookahead = 128,
|
||||
};
|
||||
|
||||
// entry point
|
||||
int main(void) {
|
||||
// mount the filesystem
|
||||
int err = lfs_mount(&lfs, &cfg);
|
||||
|
||||
// reformat if we can't mount the filesystem
|
||||
// this should only happen on the first boot
|
||||
if (err) {
|
||||
lfs_format(&lfs, &cfg);
|
||||
lfs_mount(&lfs, &cfg);
|
||||
}
|
||||
|
||||
// read current count
|
||||
uint32_t boot_count = 0;
|
||||
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
|
||||
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
|
||||
|
||||
// update boot count
|
||||
boot_count += 1;
|
||||
lfs_file_rewind(&lfs, &file);
|
||||
lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
|
||||
|
||||
// remember the storage is not updated until the file is closed successfully
|
||||
lfs_file_close(&lfs, &file);
|
||||
|
||||
// release any resources we were using
|
||||
lfs_unmount(&lfs);
|
||||
|
||||
// print the boot count
|
||||
printf("boot_count: %d\n", boot_count);
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Detailed documentation (or at least as much detail as is currently available)
|
||||
can be found in the comments in [lfs.h](lfs.h).
|
||||
|
||||
As you may have noticed, littlefs takes in a configuration structure that
|
||||
defines how the filesystem operates. The configuration struct provides the
|
||||
filesystem with the block device operations and dimensions, tweakable
|
||||
parameters that tradeoff memory usage for performance, and optional
|
||||
static buffers if the user wants to avoid dynamic memory.
|
||||
|
||||
The state of the littlefs is stored in the `lfs_t` type which is left up
|
||||
to the user to allocate, allowing multiple filesystems to be in use
|
||||
simultaneously. With the `lfs_t` and configuration struct, a user can
|
||||
format a block device or mount the filesystem.
|
||||
|
||||
Once mounted, the littlefs provides a full set of POSIX-like file and
|
||||
directory functions, with the deviation that the allocation of filesystem
|
||||
structures must be provided by the user.
|
||||
|
||||
All POSIX operations, such as remove and rename, are atomic, even in event
|
||||
of power-loss. Additionally, no file updates are actually committed to the
|
||||
filesystem until sync or close is called on the file.
|
||||
|
||||
## Other notes
|
||||
|
||||
All littlefs have the potential to return a negative error code. The errors
|
||||
can be either one of those found in the `enum lfs_error` in [lfs.h](lfs.h),
|
||||
or an error returned by the user's block device operations.
|
||||
|
||||
In the configuration struct, the `prog` and `erase` function provided by the
|
||||
user may return a `LFS_ERR_CORRUPT` error if the implementation already can
|
||||
detect corrupt blocks. However, the wear leveling does not depend on the return
|
||||
code of these functions, instead all data is read back and checked for
|
||||
integrity.
|
||||
|
||||
If your storage caches writes, make sure that the provided `sync` function
|
||||
flushes all the data to memory and ensures that the next read fetches the data
|
||||
from memory, otherwise data integrity can not be guaranteed. If the `write`
|
||||
function does not perform caching, and therefore each `read` or `write` call
|
||||
hits the memory, the `sync` function can simply return 0.
|
||||
|
||||
## Reference material
|
||||
|
||||
[DESIGN.md](DESIGN.md) - DESIGN.md contains a fully detailed dive into how
|
||||
littlefs actually works. I would encourage you to read it since the
|
||||
solutions and tradeoffs at work here are quite interesting.
|
||||
|
||||
[SPEC.md](SPEC.md) - SPEC.md contains the on-disk specification of littlefs
|
||||
with all the nitty-gritty details. Can be useful for developing tooling.
|
||||
|
||||
## Testing
|
||||
|
||||
The littlefs comes with a test suite designed to run on a PC using the
|
||||
[emulated block device](emubd/lfs_emubd.h) found in the emubd directory.
|
||||
The tests assume a Linux environment and can be started with make:
|
||||
|
||||
``` bash
|
||||
make test
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
The littlefs is provided under the [BSD-3-Clause](https://spdx.org/licenses/BSD-3-Clause.html)
|
||||
license. See [LICENSE.md](LICENSE.md) for more information. Contributions to
|
||||
this project are accepted under the same license.
|
||||
|
||||
Individual files contain the following tag instead of the full license text.
|
||||
|
||||
SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
This enables machine processing of license information based on the SPDX
|
||||
License Identifiers that are here available: http://spdx.org/licenses/
|
||||
|
||||
## Related projects
|
||||
|
||||
[Mbed OS](https://github.com/ARMmbed/mbed-os/tree/master/features/filesystem/littlefs) -
|
||||
The easiest way to get started with littlefs is to jump into [Mbed](https://os.mbed.com/),
|
||||
which already has block device drivers for most forms of embedded storage. The
|
||||
littlefs is available in Mbed OS as the [LittleFileSystem](https://os.mbed.com/docs/latest/reference/littlefilesystem.html)
|
||||
class.
|
||||
|
||||
[littlefs-fuse](https://github.com/geky/littlefs-fuse) - A [FUSE](https://github.com/libfuse/libfuse)
|
||||
wrapper for littlefs. The project allows you to mount littlefs directly on a
|
||||
Linux machine. Can be useful for debugging littlefs if you have an SD card
|
||||
handy.
|
||||
|
||||
[littlefs-js](https://github.com/geky/littlefs-js) - A javascript wrapper for
|
||||
littlefs. I'm not sure why you would want this, but it is handy for demos.
|
||||
You can see it in action [here](http://littlefs.geky.net/demo.html).
|
||||
2585
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs.c
Normal file
2585
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs.c
Normal file
File diff suppressed because it is too large
Load Diff
501
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs.h
Normal file
501
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs.h
Normal file
@@ -0,0 +1,501 @@
|
||||
/*
|
||||
* The little filesystem
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef LFS_H
|
||||
#define LFS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
/// Version info ///
|
||||
|
||||
// Software library version
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_VERSION 0x00010007
|
||||
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
|
||||
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
|
||||
|
||||
// Version of On-disk data structures
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_DISK_VERSION 0x00010001
|
||||
#define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
|
||||
#define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
|
||||
|
||||
|
||||
/// Definitions ///
|
||||
|
||||
// Type definitions
|
||||
typedef uint32_t lfs_size_t;
|
||||
typedef uint32_t lfs_off_t;
|
||||
|
||||
typedef int32_t lfs_ssize_t;
|
||||
typedef int32_t lfs_soff_t;
|
||||
|
||||
typedef uint32_t lfs_block_t;
|
||||
|
||||
// Max name size in bytes
|
||||
#ifndef LFS_NAME_MAX
|
||||
#define LFS_NAME_MAX 255
|
||||
#endif
|
||||
|
||||
// Max file size in bytes
|
||||
#ifndef LFS_FILE_MAX
|
||||
#define LFS_FILE_MAX 2147483647
|
||||
#endif
|
||||
|
||||
// Possible error codes, these are negative to allow
|
||||
// valid positive return values
|
||||
enum lfs_error {
|
||||
LFS_ERR_OK = 0, // No error
|
||||
LFS_ERR_IO = -5, // Error during device operation
|
||||
LFS_ERR_CORRUPT = -52, // Corrupted
|
||||
LFS_ERR_NOENT = -2, // No directory entry
|
||||
LFS_ERR_EXIST = -17, // Entry already exists
|
||||
LFS_ERR_NOTDIR = -20, // Entry is not a dir
|
||||
LFS_ERR_ISDIR = -21, // Entry is a dir
|
||||
LFS_ERR_NOTEMPTY = -39, // Dir is not empty
|
||||
LFS_ERR_BADF = -9, // Bad file number
|
||||
LFS_ERR_FBIG = -27, // File too large
|
||||
LFS_ERR_INVAL = -22, // Invalid parameter
|
||||
LFS_ERR_NOSPC = -28, // No space left on device
|
||||
LFS_ERR_NOMEM = -12, // No more memory available
|
||||
};
|
||||
|
||||
// File types
|
||||
enum lfs_type {
|
||||
LFS_TYPE_REG = 0x11,
|
||||
LFS_TYPE_DIR = 0x22,
|
||||
LFS_TYPE_SUPERBLOCK = 0x2e,
|
||||
};
|
||||
|
||||
// File open flags
|
||||
enum lfs_open_flags {
|
||||
// open flags
|
||||
LFS_O_RDONLY = 1, // Open a file as read only
|
||||
LFS_O_WRONLY = 2, // Open a file as write only
|
||||
LFS_O_RDWR = 3, // Open a file as read and write
|
||||
LFS_O_CREAT = 0x0100, // Create a file if it does not exist
|
||||
LFS_O_EXCL = 0x0200, // Fail if a file already exists
|
||||
LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size
|
||||
LFS_O_APPEND = 0x0800, // Move to end of file on every write
|
||||
|
||||
// internally used flags
|
||||
LFS_F_DIRTY = 0x10000, // File does not match storage
|
||||
LFS_F_WRITING = 0x20000, // File has been written since last flush
|
||||
LFS_F_READING = 0x40000, // File has been read since last flush
|
||||
LFS_F_ERRED = 0x80000, // An error occured during write
|
||||
};
|
||||
|
||||
// File seek flags
|
||||
enum lfs_whence_flags {
|
||||
LFS_SEEK_SET = 0, // Seek relative to an absolute position
|
||||
LFS_SEEK_CUR = 1, // Seek relative to the current file position
|
||||
LFS_SEEK_END = 2, // Seek relative to the end of the file
|
||||
};
|
||||
|
||||
|
||||
// Configuration provided during initialization of the littlefs
|
||||
struct lfs_config {
|
||||
// Opaque user provided context that can be used to pass
|
||||
// information to the block device operations
|
||||
void *context;
|
||||
|
||||
// Read a region in a block. Negative error codes are propogated
|
||||
// to the user.
|
||||
int (*read)(const struct lfs_config *c, lfs_block_t block,
|
||||
lfs_off_t off, void *buffer, lfs_size_t 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 (*prog)(const struct lfs_config *c, lfs_block_t block,
|
||||
lfs_off_t off, const void *buffer, lfs_size_t 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 (*erase)(const struct lfs_config *c, lfs_block_t block);
|
||||
|
||||
// Sync the state of the underlying block device. Negative error codes
|
||||
// are propogated to the user.
|
||||
int (*sync)(const struct lfs_config *c);
|
||||
|
||||
// Minimum size of a block read. This determines the size of read buffers.
|
||||
// This may be larger than the physical read size to improve performance
|
||||
// by caching more of the block device.
|
||||
lfs_size_t read_size;
|
||||
|
||||
// Minimum size of a block program. This determines the size of program
|
||||
// buffers. This may be larger than the physical program size to improve
|
||||
// performance by caching more of the block device.
|
||||
// Must be a multiple of the read size.
|
||||
lfs_size_t prog_size;
|
||||
|
||||
// Size of an erasable block. This does not impact ram consumption and
|
||||
// may be larger than the physical erase size. However, this should be
|
||||
// kept small as each file currently takes up an entire block.
|
||||
// Must be a multiple of the program size.
|
||||
lfs_size_t block_size;
|
||||
|
||||
// Number of erasable blocks on the device.
|
||||
lfs_size_t block_count;
|
||||
|
||||
// Number of blocks to lookahead during block allocation. A larger
|
||||
// lookahead reduces the number of passes required to allocate a block.
|
||||
// The lookahead buffer requires only 1 bit per block so it can be quite
|
||||
// large with little ram impact. Should be a multiple of 32.
|
||||
lfs_size_t lookahead;
|
||||
|
||||
// Optional, statically allocated read buffer. Must be read sized.
|
||||
void *read_buffer;
|
||||
|
||||
// Optional, statically allocated program buffer. Must be program sized.
|
||||
void *prog_buffer;
|
||||
|
||||
// Optional, statically allocated lookahead buffer. Must be 1 bit per
|
||||
// lookahead block.
|
||||
void *lookahead_buffer;
|
||||
|
||||
// Optional, statically allocated buffer for files. Must be program sized.
|
||||
// If enabled, only one file may be opened at a time.
|
||||
void *file_buffer;
|
||||
};
|
||||
|
||||
// Optional configuration provided during lfs_file_opencfg
|
||||
struct lfs_file_config {
|
||||
// Optional, statically allocated buffer for files. Must be program sized.
|
||||
// If NULL, malloc will be used by default.
|
||||
void *buffer;
|
||||
};
|
||||
|
||||
// File info structure
|
||||
struct lfs_info {
|
||||
// Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
|
||||
uint8_t type;
|
||||
|
||||
// Size of the file, only valid for REG files
|
||||
lfs_size_t size;
|
||||
|
||||
// Name of the file stored as a null-terminated string
|
||||
char name[LFS_NAME_MAX+1];
|
||||
};
|
||||
|
||||
|
||||
/// littlefs data structures ///
|
||||
typedef struct lfs_entry {
|
||||
lfs_off_t off;
|
||||
|
||||
struct lfs_disk_entry {
|
||||
uint8_t type;
|
||||
uint8_t elen;
|
||||
uint8_t alen;
|
||||
uint8_t nlen;
|
||||
union {
|
||||
struct {
|
||||
lfs_block_t head;
|
||||
lfs_size_t size;
|
||||
} file;
|
||||
lfs_block_t dir[2];
|
||||
} u;
|
||||
} d;
|
||||
} lfs_entry_t;
|
||||
|
||||
typedef struct lfs_cache {
|
||||
lfs_block_t block;
|
||||
lfs_off_t off;
|
||||
uint8_t *buffer;
|
||||
} lfs_cache_t;
|
||||
|
||||
typedef struct lfs_file {
|
||||
struct lfs_file *next;
|
||||
lfs_block_t pair[2];
|
||||
lfs_off_t poff;
|
||||
|
||||
lfs_block_t head;
|
||||
lfs_size_t size;
|
||||
|
||||
const struct lfs_file_config *cfg;
|
||||
uint32_t flags;
|
||||
lfs_off_t pos;
|
||||
lfs_block_t block;
|
||||
lfs_off_t off;
|
||||
lfs_cache_t cache;
|
||||
} lfs_file_t;
|
||||
|
||||
typedef struct lfs_dir {
|
||||
struct lfs_dir *next;
|
||||
lfs_block_t pair[2];
|
||||
lfs_off_t off;
|
||||
|
||||
lfs_block_t head[2];
|
||||
lfs_off_t pos;
|
||||
|
||||
struct lfs_disk_dir {
|
||||
uint32_t rev;
|
||||
lfs_size_t size;
|
||||
lfs_block_t tail[2];
|
||||
} d;
|
||||
} lfs_dir_t;
|
||||
|
||||
typedef struct lfs_superblock {
|
||||
lfs_off_t off;
|
||||
|
||||
struct lfs_disk_superblock {
|
||||
uint8_t type;
|
||||
uint8_t elen;
|
||||
uint8_t alen;
|
||||
uint8_t nlen;
|
||||
lfs_block_t root[2];
|
||||
uint32_t block_size;
|
||||
uint32_t block_count;
|
||||
uint32_t version;
|
||||
char magic[8];
|
||||
} d;
|
||||
} lfs_superblock_t;
|
||||
|
||||
typedef struct lfs_free {
|
||||
lfs_block_t off;
|
||||
lfs_block_t size;
|
||||
lfs_block_t i;
|
||||
lfs_block_t ack;
|
||||
uint32_t *buffer;
|
||||
} lfs_free_t;
|
||||
|
||||
// The littlefs type
|
||||
typedef struct lfs {
|
||||
const struct lfs_config *cfg;
|
||||
|
||||
lfs_block_t root[2];
|
||||
lfs_file_t *files;
|
||||
lfs_dir_t *dirs;
|
||||
|
||||
lfs_cache_t rcache;
|
||||
lfs_cache_t pcache;
|
||||
|
||||
lfs_free_t free;
|
||||
bool deorphaned;
|
||||
bool moving;
|
||||
} lfs_t;
|
||||
|
||||
|
||||
/// Filesystem functions ///
|
||||
|
||||
// Format a block device with the littlefs
|
||||
//
|
||||
// Requires a littlefs object and config struct. This clobbers the littlefs
|
||||
// object, and does not leave the filesystem mounted. The config struct must
|
||||
// be zeroed for defaults and backwards compatibility.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_format(lfs_t *lfs, const struct lfs_config *config);
|
||||
|
||||
// Mounts a littlefs
|
||||
//
|
||||
// Requires a littlefs object and config struct. Multiple filesystems
|
||||
// may be mounted simultaneously with multiple littlefs objects. Both
|
||||
// lfs and config must be allocated while mounted. The config struct must
|
||||
// be zeroed for defaults and backwards compatibility.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
|
||||
|
||||
// Unmounts a littlefs
|
||||
//
|
||||
// Does nothing besides releasing any allocated resources.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_unmount(lfs_t *lfs);
|
||||
|
||||
/// General operations ///
|
||||
|
||||
// Removes a file or directory
|
||||
//
|
||||
// If removing a directory, the directory must be empty.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_remove(lfs_t *lfs, const char *path);
|
||||
|
||||
// Rename or move a file or directory
|
||||
//
|
||||
// If the destination exists, it must match the source in type.
|
||||
// If the destination is a directory, the directory must be empty.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
|
||||
|
||||
// Find info about a file or directory
|
||||
//
|
||||
// Fills out the info structure, based on the specified file or directory.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
|
||||
|
||||
|
||||
/// File operations ///
|
||||
|
||||
// Open a file
|
||||
//
|
||||
// The mode that the file is opened in is determined by the flags, which
|
||||
// are values from the enum lfs_open_flags that are bitwise-ored together.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
||||
const char *path, int flags);
|
||||
|
||||
// Open a file with extra configuration
|
||||
//
|
||||
// The mode that the file is opened in is determined by the flags, which
|
||||
// are values from the enum lfs_open_flags that are bitwise-ored together.
|
||||
//
|
||||
// The config struct provides additional config options per file as described
|
||||
// above. The config struct must be allocated while the file is open, and the
|
||||
// config struct must be zeroed for defaults and backwards compatibility.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
|
||||
const char *path, int flags,
|
||||
const struct lfs_file_config *config);
|
||||
|
||||
// Close a file
|
||||
//
|
||||
// Any pending writes are written out to storage as though
|
||||
// sync had been called and releases any allocated resources.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
|
||||
|
||||
// Synchronize a file on storage
|
||||
//
|
||||
// Any pending writes are written out to storage.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
|
||||
|
||||
// Read data from file
|
||||
//
|
||||
// Takes a buffer and size indicating where to store the read data.
|
||||
// Returns the number of bytes read, or a negative error code on failure.
|
||||
lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
||||
void *buffer, lfs_size_t size);
|
||||
|
||||
// Write data to file
|
||||
//
|
||||
// Takes a buffer and size indicating the data to write. The file will not
|
||||
// actually be updated on the storage until either sync or close is called.
|
||||
//
|
||||
// Returns the number of bytes written, or a negative error code on failure.
|
||||
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
||||
const void *buffer, lfs_size_t size);
|
||||
|
||||
// Change the position of the file
|
||||
//
|
||||
// The change in position is determined by the offset and whence flag.
|
||||
// Returns the old position of the file, or a negative error code on failure.
|
||||
lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
||||
lfs_soff_t off, int whence);
|
||||
|
||||
// Truncates the size of the file to the specified size
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
|
||||
|
||||
// Return the position of the file
|
||||
//
|
||||
// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
|
||||
// Returns the position of the file, or a negative error code on failure.
|
||||
lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
|
||||
|
||||
// Change the position of the file to the beginning of the file
|
||||
//
|
||||
// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
|
||||
|
||||
// Return the size of the file
|
||||
//
|
||||
// Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END)
|
||||
// Returns the size of the file, or a negative error code on failure.
|
||||
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
|
||||
|
||||
|
||||
/// Directory operations ///
|
||||
|
||||
// Create a directory
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_mkdir(lfs_t *lfs, const char *path);
|
||||
|
||||
// Open a directory
|
||||
//
|
||||
// Once open a directory can be used with read to iterate over files.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
|
||||
|
||||
// Close a directory
|
||||
//
|
||||
// Releases any allocated resources.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
|
||||
|
||||
// Read an entry in the directory
|
||||
//
|
||||
// Fills out the info structure, based on the specified file or directory.
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
|
||||
|
||||
// Change the position of the directory
|
||||
//
|
||||
// The new off must be a value previous returned from tell and specifies
|
||||
// an absolute offset in the directory seek.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
|
||||
|
||||
// Return the position of the directory
|
||||
//
|
||||
// The returned offset is only meant to be consumed by seek and may not make
|
||||
// sense, but does indicate the current position in the directory iteration.
|
||||
//
|
||||
// Returns the position of the directory, or a negative error code on failure.
|
||||
lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
|
||||
|
||||
// Change the position of the directory to the beginning of the directory
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
|
||||
|
||||
|
||||
/// Miscellaneous littlefs specific operations ///
|
||||
|
||||
// Traverse through all blocks in use by the filesystem
|
||||
//
|
||||
// The provided callback will be called with each block address that is
|
||||
// currently in use by the filesystem. This can be used to determine which
|
||||
// blocks are in use or how much of the storage is available.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
|
||||
|
||||
// Prunes any recoverable errors that may have occured in the filesystem
|
||||
//
|
||||
// Not needed to be called by user unless an operation is interrupted
|
||||
// but the filesystem is still mounted. This is already called on first
|
||||
// allocation.
|
||||
//
|
||||
// Returns a negative error code on failure.
|
||||
int lfs_deorphan(lfs_t *lfs);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
31
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs_util.c
Normal file
31
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs_util.c
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* lfs util functions
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include "lfs_util.h"
|
||||
|
||||
// Only compile if user does not provide custom config
|
||||
#ifndef LFS_CONFIG
|
||||
|
||||
|
||||
// Software CRC implementation with small lookup table
|
||||
void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
|
||||
static const uint32_t rtable[16] = {
|
||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
||||
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
|
||||
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
|
||||
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c,
|
||||
};
|
||||
|
||||
const uint8_t *data = buffer;
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 0)) & 0xf];
|
||||
*crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 4)) & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
197
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs_util.h
Normal file
197
arch/stm32/Adafruit_LittleFS_stm32/src/littlefs/lfs_util.h
Normal file
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* lfs utility functions
|
||||
*
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef LFS_UTIL_H
|
||||
#define LFS_UTIL_H
|
||||
|
||||
// Users can override lfs_util.h with their own configuration by defining
|
||||
// LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
|
||||
//
|
||||
// If LFS_CONFIG is used, none of the default utils will be emitted and must be
|
||||
// provided by the config file. To start I would suggest copying lfs_util.h and
|
||||
// modifying as needed.
|
||||
#ifdef LFS_CONFIG
|
||||
#define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
|
||||
#define LFS_STRINGIZE2(x) #x
|
||||
#include LFS_STRINGIZE(LFS_CONFIG)
|
||||
#else
|
||||
|
||||
// System includes
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef LFS_NO_MALLOC
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifndef LFS_NO_ASSERT
|
||||
#include <assert.h>
|
||||
#endif
|
||||
|
||||
#if !CFG_DEBUG
|
||||
#define LFS_NO_DEBUG
|
||||
#define LFS_NO_WARN
|
||||
#define LFS_NO_ERROR
|
||||
#endif
|
||||
|
||||
#if !defined(LFS_NO_DEBUG) || !defined(LFS_NO_WARN) || !defined(LFS_NO_ERROR)
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
// Macros, may be replaced by system specific wrappers. Arguments to these
|
||||
// macros must not have side-effects as the macros can be removed for a smaller
|
||||
// code footprint
|
||||
|
||||
// Logging functions
|
||||
#ifndef LFS_NO_DEBUG
|
||||
#define LFS_DEBUG(fmt, ...) \
|
||||
printf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_DEBUG(fmt, ...)
|
||||
#endif
|
||||
|
||||
#ifndef LFS_NO_WARN
|
||||
#define LFS_WARN(fmt, ...) \
|
||||
printf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_WARN(fmt, ...)
|
||||
#endif
|
||||
|
||||
#ifndef LFS_NO_ERROR
|
||||
#define LFS_ERROR(fmt, ...) \
|
||||
printf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
|
||||
#else
|
||||
#define LFS_ERROR(fmt, ...)
|
||||
#endif
|
||||
|
||||
// Runtime assertions
|
||||
#ifndef LFS_NO_ASSERT
|
||||
#define LFS_ASSERT(test) assert(test)
|
||||
#else
|
||||
#define LFS_ASSERT(test)
|
||||
#endif
|
||||
|
||||
|
||||
// Builtin functions, these may be replaced by more efficient
|
||||
// toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
|
||||
// expensive basic C implementation for debugging purposes
|
||||
|
||||
// Min/max functions for unsigned 32-bit numbers
|
||||
static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
// Find the next smallest power of 2 less than or equal to a
|
||||
static inline uint32_t lfs_npw2(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
|
||||
return 32 - __builtin_clz(a-1);
|
||||
#else
|
||||
uint32_t r = 0;
|
||||
uint32_t s;
|
||||
a -= 1;
|
||||
s = (a > 0xffff) << 4; a >>= s; r |= s;
|
||||
s = (a > 0xff ) << 3; a >>= s; r |= s;
|
||||
s = (a > 0xf ) << 2; a >>= s; r |= s;
|
||||
s = (a > 0x3 ) << 1; a >>= s; r |= s;
|
||||
return (r | (a >> 1)) + 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Count the number of trailing binary zeros in a
|
||||
// lfs_ctz(0) may be undefined
|
||||
static inline uint32_t lfs_ctz(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
|
||||
return __builtin_ctz(a);
|
||||
#else
|
||||
return lfs_npw2((a & -a) + 1) - 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Count the number of binary ones in a
|
||||
static inline uint32_t lfs_popc(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
|
||||
return __builtin_popcount(a);
|
||||
#else
|
||||
a = a - ((a >> 1) & 0x55555555);
|
||||
a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
|
||||
return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Find the sequence comparison of a and b, this is the distance
|
||||
// between a and b ignoring overflow
|
||||
static inline int lfs_scmp(uint32_t a, uint32_t b) {
|
||||
return (int)(unsigned)(a - b);
|
||||
}
|
||||
|
||||
// Convert from 32-bit little-endian to native order
|
||||
static inline uint32_t lfs_fromle32(uint32_t a) {
|
||||
#if !defined(LFS_NO_INTRINSICS) && ( \
|
||||
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
||||
return a;
|
||||
#elif !defined(LFS_NO_INTRINSICS) && ( \
|
||||
(defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
|
||||
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
|
||||
return __builtin_bswap32(a);
|
||||
#else
|
||||
return (((uint8_t*)&a)[0] << 0) |
|
||||
(((uint8_t*)&a)[1] << 8) |
|
||||
(((uint8_t*)&a)[2] << 16) |
|
||||
(((uint8_t*)&a)[3] << 24);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Convert to 32-bit little-endian from native order
|
||||
static inline uint32_t lfs_tole32(uint32_t a) {
|
||||
return lfs_fromle32(a);
|
||||
}
|
||||
|
||||
// Calculate CRC-32 with polynomial = 0x04c11db7
|
||||
void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
|
||||
|
||||
// Allocate memory, only used if buffers are not provided to littlefs
|
||||
static inline void *lfs_malloc(size_t size) {
|
||||
#ifndef LFS_NO_MALLOC
|
||||
//extern void *pvPortMalloc( size_t xWantedSize );
|
||||
//return pvPortMalloc(size);
|
||||
return malloc(size);
|
||||
#else
|
||||
(void)size;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Deallocate memory, only used if buffers are not provided to littlefs
|
||||
static inline void lfs_free(void *p) {
|
||||
#ifndef LFS_NO_MALLOC
|
||||
//extern void vPortFree( void *pv );
|
||||
//vPortFree(p);
|
||||
free(p);
|
||||
#else
|
||||
(void)p;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
10
arch/stm32/build_hex.py
Normal file
10
arch/stm32/build_hex.py
Normal file
@@ -0,0 +1,10 @@
|
||||
Import("env")
|
||||
|
||||
# Make custom HEX from ELF
|
||||
env.AddPostAction(
|
||||
"$BUILD_DIR/${PROGNAME}.elf",
|
||||
env.VerboseAction(" ".join([
|
||||
"$OBJCOPY", "-O", "ihex", "-R", ".eeprom",
|
||||
'"$BUILD_DIR/${PROGNAME}.elf"', '"$BUILD_DIR/${PROGNAME}.hex"'
|
||||
]), "Building $BUILD_DIR/${PROGNAME}.hex")
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#include <InternalFileSystem.h>
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
#include <LittleFS.h>
|
||||
@@ -102,11 +102,11 @@ static uint32_t _atoi(const char* sp) {
|
||||
#define FIRMWARE_VER_CODE 5
|
||||
|
||||
#ifndef FIRMWARE_BUILD_DATE
|
||||
#define FIRMWARE_BUILD_DATE "9 May 2025"
|
||||
#define FIRMWARE_BUILD_DATE "17 May 2025"
|
||||
#endif
|
||||
|
||||
#ifndef FIRMWARE_VERSION
|
||||
#define FIRMWARE_VERSION "v1.6.0"
|
||||
#define FIRMWARE_VERSION "v1.6.1"
|
||||
#endif
|
||||
|
||||
#define CMD_APP_START 1
|
||||
@@ -291,7 +291,7 @@ class MyMesh : public BaseChatMesh {
|
||||
}
|
||||
|
||||
void saveContacts() {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = _fs->open("/contacts3", FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -356,7 +356,7 @@ class MyMesh : public BaseChatMesh {
|
||||
}
|
||||
|
||||
void saveChannels() {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = _fs->open("/channels2", FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -413,7 +413,7 @@ class MyMesh : public BaseChatMesh {
|
||||
mesh::Utils::toHex(fname, key, key_len);
|
||||
sprintf(path, "/bl/%s", fname);
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File f = _fs->open(path, FILE_O_WRITE);
|
||||
if (f) { f.seek(0); f.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -866,7 +866,7 @@ public:
|
||||
|
||||
BaseChatMesh::begin();
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
_identity_store = new IdentityStore(fs, "");
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
_identity_store = new IdentityStore(fs, "/identity");
|
||||
@@ -933,7 +933,7 @@ public:
|
||||
}
|
||||
|
||||
void savePrefs() {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = _fs->open("/new_prefs", FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -1621,6 +1621,9 @@ public:
|
||||
#include <helpers/ArduinoSerialInterface.h>
|
||||
ArduinoSerialInterface serial_interface;
|
||||
#endif
|
||||
#elif defined(STM32_PLATFORM)
|
||||
#include <helpers/ArduinoSerialInterface.h>
|
||||
ArduinoSerialInterface serial_interface;
|
||||
#else
|
||||
#error "need to define a serial interface"
|
||||
#endif
|
||||
@@ -1654,7 +1657,7 @@ void setup() {
|
||||
|
||||
fast_rng.begin(radio_get_rng_seed());
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
InternalFS.begin();
|
||||
the_mesh.begin(InternalFS,
|
||||
#ifdef HAS_UI
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#include <InternalFileSystem.h>
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
#include <LittleFS.h>
|
||||
@@ -22,11 +22,11 @@
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
#ifndef FIRMWARE_BUILD_DATE
|
||||
#define FIRMWARE_BUILD_DATE "9 May 2025"
|
||||
#define FIRMWARE_BUILD_DATE "17 May 2025"
|
||||
#endif
|
||||
|
||||
#ifndef FIRMWARE_VERSION
|
||||
#define FIRMWARE_VERSION "v1.6.0"
|
||||
#define FIRMWARE_VERSION "v1.6.1"
|
||||
#endif
|
||||
|
||||
#ifndef LORA_FREQ
|
||||
@@ -91,7 +91,7 @@ struct RepeaterStats {
|
||||
uint32_t total_up_time_secs;
|
||||
uint32_t n_sent_flood, n_sent_direct;
|
||||
uint32_t n_recv_flood, n_recv_direct;
|
||||
uint16_t n_full_events;
|
||||
uint16_t err_events; // was 'n_full_events'
|
||||
int16_t last_snr; // x 4
|
||||
uint16_t n_direct_dups, n_flood_dups;
|
||||
};
|
||||
@@ -105,7 +105,9 @@ struct ClientInfo {
|
||||
uint8_t out_path[MAX_PATH_SIZE];
|
||||
};
|
||||
|
||||
#define MAX_CLIENTS 4
|
||||
#ifndef MAX_CLIENTS
|
||||
#define MAX_CLIENTS 32
|
||||
#endif
|
||||
|
||||
struct NeighbourInfo {
|
||||
mesh::Identity id;
|
||||
@@ -184,7 +186,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
case REQ_TYPE_GET_STATUS: { // guests can also access this now
|
||||
RepeaterStats stats;
|
||||
stats.batt_milli_volts = board.getBattMilliVolts();
|
||||
stats.curr_tx_queue_len = _mgr->getOutboundCount();
|
||||
stats.curr_tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF);
|
||||
stats.curr_free_queue_len = _mgr->getFreeCount();
|
||||
stats.last_rssi = (int16_t) radio_driver.getLastRSSI();
|
||||
stats.n_packets_recv = radio_driver.getPacketsRecv();
|
||||
@@ -195,7 +197,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
stats.n_sent_direct = getNumSentDirect();
|
||||
stats.n_recv_flood = getNumRecvFlood();
|
||||
stats.n_recv_direct = getNumRecvDirect();
|
||||
stats.n_full_events = getNumFullEvents();
|
||||
stats.err_events = _err_flags;
|
||||
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
|
||||
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
|
||||
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
|
||||
@@ -230,7 +232,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
}
|
||||
|
||||
File openAppend(const char* fname) {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
return _fs->open(fname, FILE_O_WRITE);
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
return _fs->open(fname, "a");
|
||||
@@ -597,7 +599,7 @@ public:
|
||||
}
|
||||
|
||||
bool formatFileSystem() override {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
return InternalFS.format();
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
return LittleFS.format();
|
||||
@@ -675,7 +677,8 @@ public:
|
||||
mesh::Utils::toHex(hex, neighbour->id.pub_key, 4);
|
||||
|
||||
// add next neighbour
|
||||
sprintf(dp, "%s:%d:%d", hex, neighbour->advert_timestamp, neighbour->snr);
|
||||
uint32_t secs_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp;
|
||||
sprintf(dp, "%s:%d:%d", hex, secs_ago, neighbour->snr);
|
||||
while (*dp) dp++; // find end of string
|
||||
}
|
||||
#endif
|
||||
@@ -685,7 +688,13 @@ public:
|
||||
*dp = 0; // null terminator
|
||||
}
|
||||
|
||||
const uint8_t* getSelfIdPubKey() { return self_id.pub_key; }
|
||||
const uint8_t* getSelfIdPubKey() override { return self_id.pub_key; }
|
||||
|
||||
void clearStats() override {
|
||||
radio_driver.resetStats();
|
||||
resetStats();
|
||||
((SimpleMeshTables *)getTables())->resetStats();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mesh::Mesh::loop();
|
||||
@@ -738,7 +747,7 @@ void setup() {
|
||||
fast_rng.begin(radio_get_rng_seed());
|
||||
|
||||
FILESYSTEM* fs;
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
InternalFS.begin();
|
||||
fs = &InternalFS;
|
||||
IdentityStore store(InternalFS, "");
|
||||
|
||||
@@ -22,11 +22,11 @@
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
#ifndef FIRMWARE_BUILD_DATE
|
||||
#define FIRMWARE_BUILD_DATE "9 May 2025"
|
||||
#define FIRMWARE_BUILD_DATE "17 May 2025"
|
||||
#endif
|
||||
|
||||
#ifndef FIRMWARE_VERSION
|
||||
#define FIRMWARE_VERSION "v1.6.0"
|
||||
#define FIRMWARE_VERSION "v1.6.1"
|
||||
#endif
|
||||
|
||||
#ifndef LORA_FREQ
|
||||
@@ -138,7 +138,7 @@ struct ServerStats {
|
||||
uint32_t total_up_time_secs;
|
||||
uint32_t n_sent_flood, n_sent_direct;
|
||||
uint32_t n_recv_flood, n_recv_direct;
|
||||
uint16_t n_full_events;
|
||||
uint16_t err_events; // was 'n_full_events'
|
||||
int16_t last_snr; // x 4
|
||||
uint16_t n_direct_dups, n_flood_dups;
|
||||
uint16_t n_posted, n_post_push;
|
||||
@@ -290,7 +290,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
case REQ_TYPE_GET_STATUS: {
|
||||
ServerStats stats;
|
||||
stats.batt_milli_volts = board.getBattMilliVolts();
|
||||
stats.curr_tx_queue_len = _mgr->getOutboundCount();
|
||||
stats.curr_tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF);
|
||||
stats.curr_free_queue_len = _mgr->getFreeCount();
|
||||
stats.last_rssi = (int16_t) radio_driver.getLastRSSI();
|
||||
stats.n_packets_recv = radio_driver.getPacketsRecv();
|
||||
@@ -301,7 +301,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
stats.n_sent_direct = getNumSentDirect();
|
||||
stats.n_recv_flood = getNumRecvFlood();
|
||||
stats.n_recv_direct = getNumRecvDirect();
|
||||
stats.n_full_events = getNumFullEvents();
|
||||
stats.err_events = _err_flags;
|
||||
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
|
||||
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
|
||||
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
|
||||
@@ -821,7 +821,13 @@ public:
|
||||
strcpy(reply, "not supported");
|
||||
}
|
||||
|
||||
const uint8_t* getSelfIdPubKey() { return self_id.pub_key; }
|
||||
const uint8_t* getSelfIdPubKey() override { return self_id.pub_key; }
|
||||
|
||||
void clearStats() override {
|
||||
radio_driver.resetStats();
|
||||
resetStats();
|
||||
((SimpleMeshTables *)getTables())->resetStats();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mesh::Mesh::loop();
|
||||
|
||||
@@ -69,3 +69,18 @@ lib_deps =
|
||||
extends = arduino_base
|
||||
build_flags = ${arduino_base.build_flags}
|
||||
-D RP2040_PLATFORM
|
||||
|
||||
; ----------------- STM32 ----------------------
|
||||
|
||||
[stm32_base]
|
||||
extends = arduino_base
|
||||
platform = platformio/ststm32@19.1.0
|
||||
platform_packages = platformio/framework-arduinoststm32@https://github.com/stm32duino/Arduino_Core_STM32/archive/2.10.1.zip
|
||||
extra_scripts = post:arch/stm32/build_hex.py
|
||||
build_flags = ${arduino_base.build_flags}
|
||||
-D STM32_PLATFORM
|
||||
-I src/helpers/stm32
|
||||
build_src_filter = ${arduino_base.build_src_filter}
|
||||
+<helpers/stm32>
|
||||
lib_deps = ${arduino_base.lib_deps}
|
||||
file://arch/stm32/Adafruit_LittleFS_stm32
|
||||
@@ -13,9 +13,11 @@ namespace mesh {
|
||||
void Dispatcher::begin() {
|
||||
n_sent_flood = n_sent_direct = 0;
|
||||
n_recv_flood = n_recv_direct = 0;
|
||||
n_full_events = 0;
|
||||
_err_flags = 0;
|
||||
radio_nonrx_start = _ms->getMillis();
|
||||
|
||||
_radio->begin();
|
||||
prev_isrecv_mode = _radio->isInRecvMode();
|
||||
}
|
||||
|
||||
float Dispatcher::getAirtimeBudgetFactor() const {
|
||||
@@ -34,6 +36,18 @@ uint32_t Dispatcher::getCADFailMaxDuration() const {
|
||||
}
|
||||
|
||||
void Dispatcher::loop() {
|
||||
// check for radio 'stuck' in mode other than Rx
|
||||
bool is_recv = _radio->isInRecvMode();
|
||||
if (is_recv != prev_isrecv_mode) {
|
||||
prev_isrecv_mode = is_recv;
|
||||
if (!is_recv) {
|
||||
radio_nonrx_start = _ms->getMillis();
|
||||
}
|
||||
}
|
||||
if (!is_recv && _ms->getMillis() - radio_nonrx_start > 8000) { // radio has not been in Rx mode for 8 seconds!
|
||||
_err_flags |= ERR_EVENT_STARTRX_TIMEOUT;
|
||||
}
|
||||
|
||||
if (outbound) { // waiting for outbound send to be completed
|
||||
if (_radio->isSendComplete()) {
|
||||
long t = _ms->getMillis() - outbound_start;
|
||||
@@ -191,7 +205,7 @@ void Dispatcher::processRecvPacket(Packet* pkt) {
|
||||
}
|
||||
|
||||
void Dispatcher::checkSend() {
|
||||
if (_mgr->getOutboundCount() == 0) return; // nothing waiting to send
|
||||
if (_mgr->getOutboundCount(_ms->getMillis()) == 0) return; // nothing waiting to send
|
||||
if (!millisHasNowPassed(next_tx_time)) return; // still in 'radio silence' phase (from airtime budget setting)
|
||||
if (_radio->isReceiving()) { // LBT - check if radio is currently mid-receive, or if channel activity
|
||||
if (cad_busy_start == 0) {
|
||||
@@ -199,6 +213,8 @@ void Dispatcher::checkSend() {
|
||||
}
|
||||
|
||||
if (_ms->getMillis() - cad_busy_start > getCADFailMaxDuration()) {
|
||||
_err_flags |= ERR_EVENT_CAD_TIMEOUT;
|
||||
|
||||
MESH_DEBUG_PRINTLN("%s Dispatcher::checkSend(): CAD busy max duration reached!", getLogDateTime());
|
||||
// channel activity has gone on too long... (Radio might be in a bad state)
|
||||
// force the pending transmit below...
|
||||
@@ -234,7 +250,16 @@ void Dispatcher::checkSend() {
|
||||
|
||||
uint32_t max_airtime = _radio->getEstAirtimeFor(len)*3/2;
|
||||
outbound_start = _ms->getMillis();
|
||||
_radio->startSendRaw(raw, len);
|
||||
bool success = _radio->startSendRaw(raw, len);
|
||||
if (!success) {
|
||||
MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): ERROR: send start failed!", getLogDateTime());
|
||||
|
||||
logTxFail(outbound, outbound->getRawLength());
|
||||
|
||||
releasePacket(outbound); // return to pool
|
||||
outbound = NULL;
|
||||
return;
|
||||
}
|
||||
outbound_expiry = futureMillis(max_airtime);
|
||||
|
||||
#if MESH_PACKET_LOGGING
|
||||
@@ -255,7 +280,7 @@ void Dispatcher::checkSend() {
|
||||
Packet* Dispatcher::obtainNewPacket() {
|
||||
auto pkt = _mgr->allocNew(); // TODO: zero out all fields
|
||||
if (pkt == NULL) {
|
||||
n_full_events++;
|
||||
_err_flags |= ERR_EVENT_FULL;
|
||||
} else {
|
||||
pkt->payload_len = pkt->path_len = 0;
|
||||
pkt->_snr = 0;
|
||||
|
||||
@@ -42,8 +42,9 @@ public:
|
||||
* \brief starts the raw packet send. (no wait)
|
||||
* \param bytes the raw packet data
|
||||
* \param len the length in bytes
|
||||
* \returns true if successfully started
|
||||
*/
|
||||
virtual void startSendRaw(const uint8_t* bytes, int len) = 0;
|
||||
virtual bool startSendRaw(const uint8_t* bytes, int len) = 0;
|
||||
|
||||
/**
|
||||
* \returns true if the previous 'startSendRaw()' completed successfully.
|
||||
@@ -55,6 +56,8 @@ public:
|
||||
*/
|
||||
virtual void onSendFinished() = 0;
|
||||
|
||||
virtual bool isInRecvMode() const = 0;
|
||||
|
||||
/**
|
||||
* \returns true if the radio is currently mid-receive of a packet.
|
||||
*/
|
||||
@@ -75,7 +78,7 @@ public:
|
||||
|
||||
virtual void queueOutbound(Packet* packet, uint8_t priority, uint32_t scheduled_for) = 0;
|
||||
virtual Packet* getNextOutbound(uint32_t now) = 0; // by priority
|
||||
virtual int getOutboundCount() const = 0;
|
||||
virtual int getOutboundCount(uint32_t now) const = 0;
|
||||
virtual int getFreeCount() const = 0;
|
||||
virtual Packet* getOutboundByIdx(int i) = 0;
|
||||
virtual Packet* removeOutboundByIdx(int i) = 0;
|
||||
@@ -90,6 +93,10 @@ typedef uint32_t DispatcherAction;
|
||||
#define ACTION_RETRANSMIT(pri) (((uint32_t)1 + (pri))<<24)
|
||||
#define ACTION_RETRANSMIT_DELAYED(pri, _delay) ((((uint32_t)1 + (pri))<<24) | (_delay))
|
||||
|
||||
#define ERR_EVENT_FULL (1 << 0)
|
||||
#define ERR_EVENT_CAD_TIMEOUT (1 << 1)
|
||||
#define ERR_EVENT_STARTRX_TIMEOUT (1 << 2)
|
||||
|
||||
/**
|
||||
* \brief The low-level task that manages detecting incoming Packets, and the queueing
|
||||
* and scheduling of outbound Packets.
|
||||
@@ -99,9 +106,10 @@ class Dispatcher {
|
||||
unsigned long outbound_expiry, outbound_start, total_air_time;
|
||||
unsigned long next_tx_time;
|
||||
unsigned long cad_busy_start;
|
||||
unsigned long radio_nonrx_start;
|
||||
bool prev_isrecv_mode;
|
||||
uint32_t n_sent_flood, n_sent_direct;
|
||||
uint32_t n_recv_flood, n_recv_direct;
|
||||
uint32_t n_full_events;
|
||||
|
||||
void processRecvPacket(Packet* pkt);
|
||||
|
||||
@@ -109,12 +117,16 @@ protected:
|
||||
PacketManager* _mgr;
|
||||
Radio* _radio;
|
||||
MillisecondClock* _ms;
|
||||
uint16_t _err_flags;
|
||||
|
||||
Dispatcher(Radio& radio, MillisecondClock& ms, PacketManager& mgr)
|
||||
: _radio(&radio), _ms(&ms), _mgr(&mgr)
|
||||
{
|
||||
outbound = NULL; total_air_time = 0; next_tx_time = 0;
|
||||
cad_busy_start = 0;
|
||||
_err_flags = 0;
|
||||
radio_nonrx_start = 0;
|
||||
prev_isrecv_mode = true;
|
||||
}
|
||||
|
||||
virtual DispatcherAction onRecvPacket(Packet* pkt) = 0;
|
||||
@@ -144,7 +156,10 @@ public:
|
||||
uint32_t getNumSentDirect() const { return n_sent_direct; }
|
||||
uint32_t getNumRecvFlood() const { return n_recv_flood; }
|
||||
uint32_t getNumRecvDirect() const { return n_recv_direct; }
|
||||
uint32_t getNumFullEvents() const { return n_full_events; }
|
||||
void resetStats() {
|
||||
n_sent_flood = n_sent_direct = n_recv_flood = n_recv_direct = 0;
|
||||
_err_flags = 0;
|
||||
}
|
||||
|
||||
// helper methods
|
||||
bool millisHasNowPassed(unsigned long timestamp) const;
|
||||
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
class MeshTables {
|
||||
public:
|
||||
virtual bool hasSeen(const Packet* packet) = 0;
|
||||
virtual void clear(const Packet* packet) = 0; // remove this packet hash from table
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -373,6 +373,7 @@ bool BaseChatMesh::importContact(const uint8_t src_buf[], uint8_t len) {
|
||||
if (pkt) {
|
||||
if (pkt->readFrom(src_buf, len) && pkt->getPayloadType() == PAYLOAD_TYPE_ADVERT) {
|
||||
pkt->header |= ROUTE_TYPE_FLOOD; // simulate it being received flood-mode
|
||||
getTables()->clear(pkt); // remove packet hash from table, so we can receive/process it again
|
||||
_pendingLoopback = pkt; // loop-back, as if received over radio
|
||||
return true; // success
|
||||
} else {
|
||||
|
||||
@@ -73,7 +73,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
}
|
||||
|
||||
void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = fs->open("/com_prefs", FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -169,6 +169,9 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
checkAdvertInterval();
|
||||
savePrefs();
|
||||
sprintf(reply, "password now: %s", _prefs->password); // echo back just to let admin know for sure!!
|
||||
} else if (memcmp(command, "clear stats", 11) == 0) {
|
||||
_callbacks->clearStats();
|
||||
strcpy(reply, "(OK - stats reset)");
|
||||
} else if (memcmp(command, "get ", 4) == 0) {
|
||||
const char* config = &command[4];
|
||||
if (memcmp(config, "af", 2) == 0) {
|
||||
|
||||
@@ -42,6 +42,7 @@ public:
|
||||
virtual void setTxPower(uint8_t power_dbm) = 0;
|
||||
virtual void formatNeighborsReply(char *reply) = 0;
|
||||
virtual const uint8_t* getSelfIdPubKey() = 0;
|
||||
virtual void clearStats() = 0;
|
||||
};
|
||||
|
||||
class CommonCLI {
|
||||
|
||||
17
src/helpers/CustomSTM32WLx.h
Normal file
17
src/helpers/CustomSTM32WLx.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <RadioLib.h>
|
||||
|
||||
#define SX126X_IRQ_HEADER_VALID 0b0000010000 // 4 4 valid LoRa header received
|
||||
#define SX126X_IRQ_PREAMBLE_DETECTED 0x04
|
||||
|
||||
class CustomSTM32WLx : public STM32WLx {
|
||||
public:
|
||||
CustomSTM32WLx(STM32WLx_Module *mod) : STM32WLx(mod) { }
|
||||
|
||||
bool isReceiving() {
|
||||
uint16_t irq = getIrqFlags();
|
||||
bool detected = (irq & SX126X_IRQ_HEADER_VALID) || (irq & SX126X_IRQ_PREAMBLE_DETECTED);
|
||||
return detected;
|
||||
}
|
||||
};
|
||||
30
src/helpers/CustomSTM32WLxWrapper.h
Normal file
30
src/helpers/CustomSTM32WLxWrapper.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "CustomSTM32WLx.h"
|
||||
#include "RadioLibWrappers.h"
|
||||
#include <math.h>
|
||||
|
||||
class CustomSTM32WLxWrapper : public RadioLibWrapper {
|
||||
public:
|
||||
CustomSTM32WLxWrapper(CustomSTM32WLx& radio, mesh::MainBoard& board) : RadioLibWrapper(radio, board) { }
|
||||
bool isReceiving() override {
|
||||
if (((CustomSTM32WLx *)_radio)->isReceiving()) return true;
|
||||
|
||||
idle(); // put sx126x into standby
|
||||
// do some basic CAD (blocks for ~12780 micros (on SF 10)!)
|
||||
bool activity = (((CustomSTM32WLx *)_radio)->scanChannel() == RADIOLIB_LORA_DETECTED);
|
||||
if (activity) {
|
||||
startRecv();
|
||||
} else {
|
||||
idle();
|
||||
}
|
||||
return activity;
|
||||
}
|
||||
float getLastRSSI() const override { return ((CustomSTM32WLx *)_radio)->getRSSI(); }
|
||||
float getLastSNR() const override { return ((CustomSTM32WLx *)_radio)->getSNR(); }
|
||||
|
||||
float packetScore(float snr, int packet_len) override {
|
||||
int sf = ((CustomSTM32WLx *)_radio)->spreadingFactor;
|
||||
return packetScoreInt(snr, sf, packet_len);
|
||||
}
|
||||
};
|
||||
@@ -46,7 +46,7 @@ bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id) {
|
||||
char filename[40];
|
||||
sprintf(filename, "%s/%s.id", _dir, name);
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = _fs->open(filename, FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
@@ -68,7 +68,7 @@ bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id, const
|
||||
char filename[40];
|
||||
sprintf(filename, "%s/%s.id", _dir, name);
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file = _fs->open(filename, FILE_O_WRITE);
|
||||
if (file) { file.seek(0); file.truncate(); }
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#if defined(ESP32) || defined(RP2040_PLATFORM)
|
||||
#include <FS.h>
|
||||
#define FILESYSTEM fs::FS
|
||||
#elif defined(NRF52_PLATFORM)
|
||||
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#include <Adafruit_LittleFS.h>
|
||||
#define FILESYSTEM Adafruit_LittleFS
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ void RadioLibWrapper::startRecv() {
|
||||
}
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::isInRecvMode() const {
|
||||
return (state & ~STATE_INT_READY) == STATE_RX;
|
||||
}
|
||||
|
||||
int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
|
||||
if (state & STATE_INT_READY) {
|
||||
int len = _radio->getPacketLength();
|
||||
@@ -77,13 +81,16 @@ uint32_t RadioLibWrapper::getEstAirtimeFor(int len_bytes) {
|
||||
return _radio->getTimeOnAir(len_bytes) / 1000;
|
||||
}
|
||||
|
||||
void RadioLibWrapper::startSendRaw(const uint8_t* bytes, int len) {
|
||||
state = STATE_TX_WAIT;
|
||||
bool RadioLibWrapper::startSendRaw(const uint8_t* bytes, int len) {
|
||||
_board->onBeforeTransmit();
|
||||
int err = _radio->startTransmit((uint8_t *) bytes, len);
|
||||
if (err != RADIOLIB_ERR_NONE) {
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: startTransmit(%d)", err);
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
state = STATE_TX_WAIT;
|
||||
return true;
|
||||
}
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: startTransmit(%d)", err);
|
||||
idle(); // trigger another startRecv()
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::isSendComplete() {
|
||||
|
||||
@@ -19,12 +19,15 @@ public:
|
||||
void begin() override;
|
||||
int recvRaw(uint8_t* bytes, int sz) override;
|
||||
uint32_t getEstAirtimeFor(int len_bytes) override;
|
||||
void startSendRaw(const uint8_t* bytes, int len) override;
|
||||
bool startSendRaw(const uint8_t* bytes, int len) override;
|
||||
bool isSendComplete() override;
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
|
||||
uint32_t getPacketsRecv() const { return n_recv; }
|
||||
uint32_t getPacketsSent() const { return n_sent; }
|
||||
void resetStats() { n_recv = n_sent = 0; }
|
||||
|
||||
virtual float getLastRSSI() const override;
|
||||
virtual float getLastSNR() const override;
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
#include <CayenneLPP.h>
|
||||
|
||||
#define TELEM_PERM_BASE 0x01 // 'base' permission includes battery
|
||||
#define TELEM_PERM_LOCATION 0x02
|
||||
#define TELEM_PERM_BASE 0x01 // 'base' permission includes battery
|
||||
#define TELEM_PERM_LOCATION 0x02
|
||||
#define TELEM_PERM_ENVIRONMENT 0x04 // permission to access environment sensors
|
||||
|
||||
#define TELEM_CHANNEL_SELF 1 // LPP data channel for 'self' device
|
||||
|
||||
|
||||
@@ -80,7 +80,32 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
void clear(const mesh::Packet* packet) override {
|
||||
if (packet->getPayloadType() == PAYLOAD_TYPE_ACK) {
|
||||
uint32_t ack;
|
||||
memcpy(&ack, packet->payload, 4);
|
||||
for (int i = 0; i < MAX_PACKET_ACKS; i++) {
|
||||
if (ack == _acks[i]) {
|
||||
_acks[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uint8_t hash[MAX_HASH_SIZE];
|
||||
packet->calculatePacketHash(hash);
|
||||
|
||||
uint8_t* sp = _hashes;
|
||||
for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) {
|
||||
if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) {
|
||||
memset(sp, 0, MAX_HASH_SIZE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getNumDirectDups() const { return _direct_dups; }
|
||||
uint32_t getNumFloodDups() const { return _flood_dups; }
|
||||
|
||||
void resetStats() { _direct_dups = _flood_dups = 0; }
|
||||
};
|
||||
|
||||
@@ -8,6 +8,15 @@ PacketQueue::PacketQueue(int max_entries) {
|
||||
_num = 0;
|
||||
}
|
||||
|
||||
int PacketQueue::countBefore(uint32_t now) const {
|
||||
int n = 0;
|
||||
for (int j = 0; j < _num; j++) {
|
||||
if (_schedule_table[j] > now) continue; // scheduled for future... ignore for now
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
mesh::Packet* PacketQueue::get(uint32_t now) {
|
||||
uint8_t min_pri = 0xFF;
|
||||
int best_idx = -1;
|
||||
@@ -81,8 +90,8 @@ mesh::Packet* StaticPoolPacketManager::getNextOutbound(uint32_t now) {
|
||||
return send_queue.get(now);
|
||||
}
|
||||
|
||||
int StaticPoolPacketManager::getOutboundCount() const {
|
||||
return send_queue.count();
|
||||
int StaticPoolPacketManager::getOutboundCount(uint32_t now) const {
|
||||
return send_queue.countBefore(now);
|
||||
}
|
||||
|
||||
int StaticPoolPacketManager::getFreeCount() const {
|
||||
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
mesh::Packet* get(uint32_t now);
|
||||
void add(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for);
|
||||
int count() const { return _num; }
|
||||
int countBefore(uint32_t now) const;
|
||||
mesh::Packet* itemAt(int i) const { return _table[i]; }
|
||||
mesh::Packet* removeByIdx(int i);
|
||||
};
|
||||
@@ -27,7 +28,7 @@ public:
|
||||
void free(mesh::Packet* packet) override;
|
||||
void queueOutbound(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) override;
|
||||
mesh::Packet* getNextOutbound(uint32_t now) override;
|
||||
int getOutboundCount() const override;
|
||||
int getOutboundCount(uint32_t now) const override;
|
||||
int getFreeCount() const override;
|
||||
mesh::Packet* getOutboundByIdx(int i) override;
|
||||
mesh::Packet* removeOutboundByIdx(int i) override;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#define P_BOARD_SPI_MOSI 35 //SPI for SD Card and QMI8653 (IMU)
|
||||
#define P_BOARD_SPI_MISO 37 //SPI for SD Card and QMI8653 (IMU)
|
||||
#define P_BOARD_SPI_SCK 36 //SPI for SD Card and QMI8653 (IMU)
|
||||
#define P_BPARD_SPI_CS 47 //SPI for SD Card and QMI8653 (IMU)
|
||||
#define P_BPARD_SPI_CS 47 //Pin for SD Card CS
|
||||
#define P_BOARD_IMU_CS 34 //Pin for QMI8653 (IMU) CS
|
||||
|
||||
#define P_BOARD_IMU_INT 33 //IMU Int pin
|
||||
@@ -36,7 +36,8 @@
|
||||
#define P_GPS_RX 9 //GPS RX pin
|
||||
#define P_GPS_TX 8 //GPS TX pin
|
||||
#define P_GPS_WAKE 7 //GPS Wakeup pin
|
||||
#define P_GPS_1PPS 6 //GPS 1PPS pin
|
||||
//#define P_GPS_1PPS 6 //GPS 1PPS pin - repurposed for lora tx led
|
||||
#define GPS_BAUD_RATE 9600
|
||||
|
||||
//I2C Wire addresses
|
||||
#define I2C_BME280_ADD 0x76 //BME280 sensor I2C address on Wire
|
||||
@@ -47,15 +48,20 @@
|
||||
#define I2C_RTC_ADD 0x51 //RTC I2C address on Wire1
|
||||
#define I2C_PMU_ADD 0x34 //AXP2101 I2C address on Wire1
|
||||
|
||||
|
||||
#define PMU_WIRE_PORT Wire1
|
||||
#define XPOWERS_CHIP_AXP2101
|
||||
|
||||
class TBeamS3SupremeBoard : public ESP32Board {
|
||||
|
||||
XPowersAXP2101 PMU;
|
||||
public:
|
||||
#ifdef MESH_DEBUG
|
||||
void printPMU();
|
||||
#endif
|
||||
bool power_init();
|
||||
void begin() {
|
||||
|
||||
bool power_init();
|
||||
|
||||
|
||||
power_init();
|
||||
|
||||
ESP32Board::begin();
|
||||
|
||||
esp_reset_reason_t reason = esp_reset_reason();
|
||||
@@ -94,12 +100,14 @@ public:
|
||||
}
|
||||
|
||||
uint16_t getBattMilliVolts() override {
|
||||
|
||||
return 0;
|
||||
return PMU.getBattVoltage();
|
||||
}
|
||||
|
||||
uint16_t getBattPercent();
|
||||
|
||||
uint16_t getBattPercent() {
|
||||
//Read the PMU fuel guage for battery %
|
||||
uint16_t battPercent = PMU.getBatteryPercent();
|
||||
return battPercent;
|
||||
}
|
||||
const char* getManufacturerName() const override {
|
||||
return "LilyGo T-Beam S3 Supreme SX1262";
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
static uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
static esp_now_peer_info_t peerInfo;
|
||||
static bool is_send_complete = false;
|
||||
static volatile bool is_send_complete = false;
|
||||
static esp_err_t last_send_result;
|
||||
static uint8_t rx_buf[256];
|
||||
static uint8_t last_rx_len = 0;
|
||||
@@ -44,6 +44,8 @@ void ESPNOWRadio::init() {
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
|
||||
is_send_complete = true;
|
||||
|
||||
// Add peer
|
||||
if (esp_now_add_peer(&peerInfo) == ESP_OK) {
|
||||
ESPNOW_DEBUG_PRINTLN("init success");
|
||||
@@ -67,24 +69,30 @@ uint32_t ESPNOWRadio::intID() {
|
||||
return n + m;
|
||||
}
|
||||
|
||||
void ESPNOWRadio::startSendRaw(const uint8_t* bytes, int len) {
|
||||
bool ESPNOWRadio::startSendRaw(const uint8_t* bytes, int len) {
|
||||
// Send message via ESP-NOW
|
||||
is_send_complete = false;
|
||||
esp_err_t result = esp_now_send(broadcastAddress, bytes, len);
|
||||
if (result == ESP_OK) {
|
||||
n_sent++;
|
||||
ESPNOW_DEBUG_PRINTLN("Send success");
|
||||
} else {
|
||||
last_send_result = result;
|
||||
is_send_complete = true;
|
||||
ESPNOW_DEBUG_PRINTLN("Send failed: %d", result);
|
||||
return true;
|
||||
}
|
||||
last_send_result = result;
|
||||
is_send_complete = true;
|
||||
ESPNOW_DEBUG_PRINTLN("Send failed: %d", result);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ESPNOWRadio::isSendComplete() {
|
||||
return is_send_complete;
|
||||
}
|
||||
void ESPNOWRadio::onSendFinished() {
|
||||
is_send_complete = true;
|
||||
}
|
||||
|
||||
bool ESPNOWRadio::isInRecvMode() const {
|
||||
return is_send_complete; // if NO send in progress, then we're in Rx mode
|
||||
}
|
||||
|
||||
float ESPNOWRadio::getLastRSSI() const { return 0; }
|
||||
|
||||
@@ -12,12 +12,15 @@ public:
|
||||
void init();
|
||||
int recvRaw(uint8_t* bytes, int sz) override;
|
||||
uint32_t getEstAirtimeFor(int len_bytes) override;
|
||||
void startSendRaw(const uint8_t* bytes, int len) override;
|
||||
bool startSendRaw(const uint8_t* bytes, int len) override;
|
||||
bool isSendComplete() override;
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
|
||||
uint32_t getPacketsRecv() const { return n_recv; }
|
||||
uint32_t getPacketsSent() const { return n_sent; }
|
||||
void resetStats() { n_recv = n_sent = 0; }
|
||||
|
||||
virtual float getLastRSSI() const override;
|
||||
virtual float getLastSNR() const override;
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define BLE_WRITE_MIN_INTERVAL 20
|
||||
#define BLE_WRITE_MIN_INTERVAL 60
|
||||
|
||||
bool SerialBLEInterface::isWriteBusy() const {
|
||||
return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write?
|
||||
|
||||
@@ -94,7 +94,7 @@ size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define BLE_WRITE_MIN_INTERVAL 20
|
||||
#define BLE_WRITE_MIN_INTERVAL 60
|
||||
|
||||
bool SerialBLEInterface::isWriteBusy() const {
|
||||
return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write?
|
||||
|
||||
139
src/helpers/stm32/InternalFileSystem.cpp
Normal file
139
src/helpers/stm32/InternalFileSystem.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 hathach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "InternalFileSystem.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// LFS Disk IO
|
||||
//--------------------------------------------------------------------+
|
||||
static int _internal_flash_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
|
||||
{
|
||||
if (!buffer || !size) return LFS_ERR_INVAL;
|
||||
lfs_block_t address = LFS_FLASH_ADDR_BASE + (block * FLASH_PAGE_SIZE + off);
|
||||
memcpy(buffer, (void *)address, size);
|
||||
return LFS_ERR_OK;
|
||||
}
|
||||
|
||||
// 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 _internal_flash_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
|
||||
{
|
||||
HAL_StatusTypeDef hal_rc = HAL_OK;
|
||||
lfs_block_t addr = LFS_FLASH_ADDR_BASE + (block * FLASH_PAGE_SIZE + off);
|
||||
uint64_t *bufp = (uint64_t *) buffer;
|
||||
|
||||
if (HAL_FLASH_Unlock() != HAL_OK) return LFS_ERR_IO;
|
||||
for (uint32_t i = 0; i < size/8; i++) {
|
||||
if ((addr < LFS_FLASH_ADDR_BASE) || (addr > FLASH_END_ADDR)) {
|
||||
HAL_FLASH_Lock();
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
hal_rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, *bufp);
|
||||
addr += 8;
|
||||
bufp += 1;
|
||||
}
|
||||
if (HAL_FLASH_Lock() != HAL_OK) return LFS_ERR_IO;
|
||||
|
||||
return hal_rc == HAL_OK ? LFS_ERR_OK : LFS_ERR_IO;
|
||||
}
|
||||
|
||||
// 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 _internal_flash_erase(const struct lfs_config *c, lfs_block_t block)
|
||||
{
|
||||
HAL_StatusTypeDef hal_rc;
|
||||
lfs_block_t address = LFS_FLASH_ADDR_BASE + (block * FLASH_PAGE_SIZE);
|
||||
uint32_t pageError = 0;
|
||||
FLASH_EraseInitTypeDef EraseInitStruct = {
|
||||
.TypeErase = FLASH_TYPEERASE_PAGES,
|
||||
.Page = 0,
|
||||
.NbPages = 1
|
||||
};
|
||||
|
||||
if ((address < LFS_FLASH_ADDR_BASE) || (address > FLASH_END_ADDR)) {
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
EraseInitStruct.Page = (address - FLASH_BASE) / FLASH_PAGE_SIZE;
|
||||
HAL_FLASH_Unlock();
|
||||
hal_rc = HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return hal_rc == HAL_OK ? LFS_ERR_OK : LFS_ERR_IO;
|
||||
}
|
||||
|
||||
// Sync the state of the underlying block device. Negative error codes
|
||||
// are propogated to the user.
|
||||
static int _internal_flash_sync(const struct lfs_config *c)
|
||||
{
|
||||
return LFS_ERR_OK; // don't need sync
|
||||
}
|
||||
|
||||
struct lfs_config _InternalFSConfig = {
|
||||
.context = NULL,
|
||||
.read = _internal_flash_read,
|
||||
.prog = _internal_flash_prog,
|
||||
.erase = _internal_flash_erase,
|
||||
.sync = _internal_flash_sync,
|
||||
|
||||
.read_size = LFS_BLOCK_SIZE,
|
||||
.prog_size = LFS_BLOCK_SIZE,
|
||||
.block_size = LFS_BLOCK_SIZE,
|
||||
.block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE,
|
||||
.lookahead = 128,
|
||||
|
||||
.read_buffer = NULL,
|
||||
.prog_buffer = NULL,
|
||||
.lookahead_buffer = NULL,
|
||||
.file_buffer = NULL
|
||||
};
|
||||
|
||||
InternalFileSystem InternalFS;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
//
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
InternalFileSystem::InternalFileSystem(void)
|
||||
: Adafruit_LittleFS(&_InternalFSConfig)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool InternalFileSystem::begin(void)
|
||||
{
|
||||
// failed to mount, erase all sector then format and mount again
|
||||
if ( !Adafruit_LittleFS::begin() )
|
||||
{
|
||||
// lfs format
|
||||
this->format();
|
||||
// mount again if still failed, give up
|
||||
if ( !Adafruit_LittleFS::begin() ) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
48
src/helpers/stm32/InternalFileSystem.h
Normal file
48
src/helpers/stm32/InternalFileSystem.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 hathach for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef INTERNALFILESYSTEM_H_
|
||||
#define INTERNALFILESYSTEM_H_
|
||||
|
||||
#include "Adafruit_LittleFS.h"
|
||||
|
||||
#ifndef LFS_FLASH_TOTAL_SIZE /* Flash size can be configured in platformio.ini */
|
||||
#define LFS_FLASH_TOTAL_SIZE (16 * 2048) /* defaults to 32k flash */
|
||||
#endif
|
||||
#define LFS_BLOCK_SIZE (2048)
|
||||
#define LFS_FLASH_ADDR_BASE (FLASH_END_ADDR - LFS_FLASH_TOTAL_SIZE + 1)
|
||||
|
||||
class InternalFileSystem : public Adafruit_LittleFS
|
||||
{
|
||||
public:
|
||||
InternalFileSystem(void);
|
||||
|
||||
// overwrite to also perform low level format (sector erase of whole flash region)
|
||||
bool begin(void);
|
||||
};
|
||||
|
||||
extern InternalFileSystem InternalFS;
|
||||
|
||||
#endif /* INTERNALFILESYSTEM_H_ */
|
||||
|
||||
29
src/helpers/stm32/STM32Board.h
Normal file
29
src/helpers/stm32/STM32Board.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <MeshCore.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class STM32Board : public mesh::MainBoard {
|
||||
protected:
|
||||
uint8_t startup_reason;
|
||||
|
||||
public:
|
||||
void begin() {
|
||||
startup_reason = BD_STARTUP_NORMAL;
|
||||
}
|
||||
|
||||
uint8_t getStartupReason() const override { return startup_reason; }
|
||||
|
||||
uint16_t getBattMilliVolts() override {
|
||||
return 0; // not supported
|
||||
}
|
||||
|
||||
const char* getManufacturerName() const override {
|
||||
return "Generic STM32";
|
||||
}
|
||||
|
||||
void reboot() override {
|
||||
}
|
||||
|
||||
bool startOTAUpdate(const char* id, char reply[]) override { return false; };
|
||||
};
|
||||
@@ -20,7 +20,7 @@ build_flags =
|
||||
-D PIN_BOARD_SCL=22
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/generic-e22>
|
||||
lib_deps =
|
||||
|
||||
@@ -16,7 +16,7 @@ build_flags =
|
||||
-D PIN_TFT_SDA=42 ; SDIN
|
||||
-D PIN_TFT_SCL=41 ; SCLK
|
||||
-D PIN_TFT_DC=40 ; RS (register select)
|
||||
-D PIN_TFT_RST=39 ; RES
|
||||
-D PIN_TFT_RST=39 ; RES
|
||||
-D PIN_TFT_CS=38
|
||||
-D USE_PIN_TFT=1
|
||||
-D PIN_VEXT_EN=3 ; Vext is connected to VDD which is also connected to OLED & GPS
|
||||
@@ -25,7 +25,7 @@ build_flags =
|
||||
-D PIN_GPS_TX=34
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/heltec_tracker>
|
||||
@@ -44,6 +44,7 @@ build_flags =
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456 ; HWT will use display for pin
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
@@ -58,4 +59,3 @@ lib_deps =
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
stevemarple/MicroNMEA @ ^2.0.6
|
||||
adafruit/Adafruit ST7735 and ST7789 Library @ ^1.11.0
|
||||
|
||||
@@ -5,13 +5,14 @@ build_flags =
|
||||
${esp32_base.build_flags}
|
||||
-I variants/heltec_v2
|
||||
-D HELTEC_LORA_V2
|
||||
-D RADIO_CLASS=CustomSX1276
|
||||
-D WRAPPER_CLASS=CustomSX1276Wrapper
|
||||
-D SX127X_CURRENT_LIMIT=120
|
||||
-D LORA_TX_POWER=20
|
||||
-D PIN_BOARD_SDA=4
|
||||
-D PIN_BOARD_SCL=15
|
||||
-D PIN_USER_BTN=0
|
||||
-D PIN_OLED_RESET=16
|
||||
-D RADIO_CLASS=CustomSX1276
|
||||
-D WRAPPER_CLASS=CustomSX1276Wrapper
|
||||
-D LORA_TX_POWER=20
|
||||
-D P_LORA_TX_LED=25
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/heltec_v2>
|
||||
@@ -97,6 +98,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=0
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -23,7 +23,7 @@ SensorManager sensors;
|
||||
bool radio_init() {
|
||||
fallback_clock.begin();
|
||||
rtc_clock.begin(Wire);
|
||||
|
||||
|
||||
#if defined(P_LORA_SCLK)
|
||||
spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
|
||||
#endif
|
||||
@@ -34,8 +34,12 @@ bool radio_init() {
|
||||
return false; // fail
|
||||
}
|
||||
|
||||
#ifdef SX127X_CURRENT_LIMIT
|
||||
radio.setCurrentLimit(SX127X_CURRENT_LIMIT);
|
||||
#endif
|
||||
|
||||
radio.setCRC(1);
|
||||
|
||||
|
||||
return true; // success
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ build_flags =
|
||||
-D PIN_VEXT_EN=36
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/heltec_v3>
|
||||
@@ -102,6 +102,7 @@ build_flags =
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D BLE_PIN_CODE=0 ; dynamic, random PIN
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
@@ -178,6 +179,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -21,7 +21,7 @@ build_flags =
|
||||
-D PIN_OLED_RESET=21
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
@@ -112,6 +112,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -30,6 +30,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
; -D RADIOLIB_DEBUG_BASIC=1
|
||||
|
||||
@@ -23,7 +23,7 @@ SensorManager sensors;
|
||||
bool radio_init() {
|
||||
fallback_clock.begin();
|
||||
rtc_clock.begin(Wire);
|
||||
|
||||
|
||||
#if defined(P_LORA_SCLK)
|
||||
spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
|
||||
#endif
|
||||
@@ -34,8 +34,12 @@ bool radio_init() {
|
||||
return false; // fail
|
||||
}
|
||||
|
||||
#ifdef SX127X_CURRENT_LIMIT
|
||||
radio.setCurrentLimit(SX127X_CURRENT_LIMIT);
|
||||
#endif
|
||||
|
||||
radio.setCRC(1);
|
||||
|
||||
|
||||
return true; // success
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ build_flags =
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
|
||||
@@ -5,6 +5,7 @@ build_flags =
|
||||
${esp32_base.build_flags}
|
||||
-I variants/lilygo_tbeam_supreme_SX1262
|
||||
-D LORA_TX_POWER=22
|
||||
-D P_LORA_TX_LED=6
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
;-D DISPLAY_CLASS=SSD1306Display ;Needs to be modified for SH1106
|
||||
@@ -14,9 +15,9 @@ build_src_filter = ${esp32_base.build_src_filter}
|
||||
board_build.partitions = min_spiffs.csv ; get around 4mb flash limit
|
||||
lib_deps =
|
||||
${esp32_base.lib_deps}
|
||||
lewisxhe/PCF8563_Library@^1.0.1
|
||||
lewisxhe/XPowersLib @ ^0.2.7
|
||||
;adafruit/Adafruit SSD1306 @ ^2.5.13
|
||||
stevemarple/MicroNMEA @ ^2.0.6
|
||||
|
||||
; === LILYGO T-Beam S3 Supreme with SX1262 environments ===
|
||||
[env:T_Beam_S3_Supreme_SX1262_repeater]
|
||||
@@ -58,16 +59,17 @@ extends = T_Beam_S3_Supreme_SX1262
|
||||
build_flags =
|
||||
${T_Beam_S3_Supreme_SX1262.build_flags}
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=1
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
-D MESH_PACKET_LOGGING=8
|
||||
-D MESH_DEBUG=1
|
||||
; -D MESH_PACKET_LOGGING=8
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${T_Beam_S3_Supreme_SX1262.build_src_filter}
|
||||
+<helpers/esp32/*.cpp>
|
||||
+<../examples/companion_radio>
|
||||
lib_deps =
|
||||
${T_Beam_S3_Supreme_SX1262.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
#include <Arduino.h>
|
||||
#include "target.h"
|
||||
#include <helpers/sensors/MicroNMEALocationProvider.h>
|
||||
|
||||
TBeamS3SupremeBoard board;
|
||||
|
||||
// Using PMU AXP2102
|
||||
XPowersAXP2101 PMU;
|
||||
|
||||
bool pmuIntFlag;
|
||||
|
||||
#ifndef LORA_CR
|
||||
@@ -23,111 +21,200 @@ WRAPPER_CLASS radio_driver(radio, board);
|
||||
|
||||
ESP32RTCClock fallback_clock;
|
||||
AutoDiscoverRTCClock rtc_clock(fallback_clock);
|
||||
SensorManager sensors;
|
||||
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
|
||||
TbeamSupSensorManager sensors = TbeamSupSensorManager(nmea);
|
||||
|
||||
static void setPMUIntFlag(){
|
||||
pmuIntFlag = true;
|
||||
}
|
||||
#ifdef MESH_DEBUG
|
||||
void TBeamS3SupremeBoard::printPMU()
|
||||
{
|
||||
Serial.print("isCharging:"); Serial.println(PMU.isCharging() ? "YES" : "NO");
|
||||
Serial.print("isDischarge:"); Serial.println(PMU.isDischarge() ? "YES" : "NO");
|
||||
Serial.print("isVbusIn:"); Serial.println(PMU.isVbusIn() ? "YES" : "NO");
|
||||
Serial.print("getBattVoltage:"); Serial.print(PMU.getBattVoltage()); Serial.println("mV");
|
||||
Serial.print("getVbusVoltage:"); Serial.print(PMU.getVbusVoltage()); Serial.println("mV");
|
||||
Serial.print("getSystemVoltage:"); Serial.print(PMU.getSystemVoltage()); Serial.println("mV");
|
||||
|
||||
bool power_init() {
|
||||
//Start up Wire1 with PMU address
|
||||
//Serial.println("Starting Wire1 for PMU");
|
||||
//Wire1.begin(I2C_PMU_ADD);
|
||||
//Wire1.begin(PIN_BOARD_SDA1,PIN_BOARD_SCL1);
|
||||
|
||||
//Set LED to indicate charge state
|
||||
Serial.println("Setting charge led");
|
||||
PMU.setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG);
|
||||
|
||||
//Set up PMU interrupts
|
||||
Serial.println("Setting up PMU interrupts");
|
||||
pinMode(PIN_PMU_IRQ,INPUT_PULLUP);
|
||||
attachInterrupt(PIN_PMU_IRQ,setPMUIntFlag,FALLING);
|
||||
// The battery percentage may be inaccurate at first use, the PMU will automatically
|
||||
// learn the battery curve and will automatically calibrate the battery percentage
|
||||
// after a charge and discharge cycle
|
||||
if (PMU.isBatteryConnect()) {
|
||||
Serial.print("getBatteryPercent:"); Serial.print(PMU.getBatteryPercent()); Serial.println("%");
|
||||
}
|
||||
|
||||
//GPS
|
||||
Serial.println("Setting and enabling a-ldo4 for GPS");
|
||||
PMU.setALDO4Voltage(3300);
|
||||
PMU.enableALDO4(); //disable to save power
|
||||
|
||||
//Lora
|
||||
Serial.println("Setting and enabling a-ldo3 for LoRa");
|
||||
PMU.setALDO3Voltage(3300);
|
||||
PMU.enableALDO3();
|
||||
Serial.println();
|
||||
}
|
||||
#endif
|
||||
|
||||
//To avoid SPI bus issues during power up, reset OLED, sensor, and SD card supplies
|
||||
Serial.println("Reset a-ldo1&2 and b-ldo1");
|
||||
if(ESP_SLEEP_WAKEUP_UNDEFINED == esp_sleep_get_wakeup_cause()){
|
||||
PMU.enableALDO1();
|
||||
PMU.enableALDO2();
|
||||
PMU.enableBLDO1();
|
||||
bool TBeamS3SupremeBoard::power_init()
|
||||
{
|
||||
bool result = PMU.begin(PMU_WIRE_PORT, I2C_PMU_ADD, PIN_BOARD_SDA1, PIN_BOARD_SCL1);
|
||||
if (result == false) {
|
||||
MESH_DEBUG_PRINTLN("power is not online..."); while (1)delay(50);
|
||||
}
|
||||
MESH_DEBUG_PRINTLN("Setting charge led");
|
||||
PMU.setChargingLedMode(XPOWERS_CHG_LED_CTRL_CHG);
|
||||
|
||||
// Set up PMU interrupts
|
||||
MESH_DEBUG_PRINTLN("Setting up PMU interrupts");
|
||||
pinMode(PIN_PMU_IRQ, INPUT_PULLUP);
|
||||
attachInterrupt(PIN_PMU_IRQ, setPMUIntFlag, FALLING);
|
||||
|
||||
// GPS
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling a-ldo4 for GPS");
|
||||
PMU.setALDO4Voltage(3300);
|
||||
PMU.enableALDO4(); // disable to save power
|
||||
|
||||
// Lora
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling a-ldo3 for LoRa");
|
||||
PMU.setALDO3Voltage(3300);
|
||||
PMU.enableALDO3();
|
||||
|
||||
// To avoid SPI bus issues during power up, reset OLED, sensor, and SD card supplies
|
||||
MESH_DEBUG_PRINTLN("Reset a-ldo1&2 and b-ldo1");
|
||||
if (ESP_SLEEP_WAKEUP_UNDEFINED == esp_sleep_get_wakeup_cause())
|
||||
{
|
||||
PMU.disableALDO1();
|
||||
PMU.disableALDO2();
|
||||
PMU.disableBLDO1();
|
||||
delay(250);
|
||||
}
|
||||
|
||||
//BME280 and OLED
|
||||
Serial.println("Setting and enabling a-ldo1 for oled");
|
||||
|
||||
// BME280 and OLED
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling a-ldo1 for oled");
|
||||
PMU.setALDO1Voltage(3300);
|
||||
PMU.enableALDO1();
|
||||
|
||||
//QMC6310U
|
||||
Serial.println("Setting and enabling a-ldo2 for QMC");
|
||||
// QMC6310U
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling a-ldo2 for QMC");
|
||||
PMU.setALDO2Voltage(3300);
|
||||
PMU.enableALDO2(); //disable to save power
|
||||
PMU.enableALDO2(); // disable to save power
|
||||
|
||||
//SD card
|
||||
Serial.println("Setting and enabling b-ldo2 for SD card");
|
||||
// SD card
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling b-ldo2 for SD card");
|
||||
PMU.setBLDO1Voltage(3300);
|
||||
PMU.enableBLDO1();
|
||||
|
||||
//Out to header pins
|
||||
Serial.println("Setting and enabling b-ldo2 for output to header");
|
||||
// Out to header pins
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling b-ldo2 for output to header");
|
||||
PMU.setBLDO2Voltage(3300);
|
||||
PMU.enableBLDO2();
|
||||
|
||||
Serial.println("Setting and enabling dcdc4 for output to header");
|
||||
PMU.setDC4Voltage(XPOWERS_AXP2101_DCDC4_VOL2_MAX); //1.8V
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling dcdc4 for output to header");
|
||||
PMU.setDC4Voltage(XPOWERS_AXP2101_DCDC4_VOL2_MAX); // 1.8V
|
||||
PMU.enableDC4();
|
||||
|
||||
Serial.println("Setting and enabling dcdc5 for output to header");
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling dcdc5 for output to header");
|
||||
PMU.setDC5Voltage(3300);
|
||||
PMU.enableDC5();
|
||||
|
||||
//Other power rails
|
||||
Serial.println("Setting and enabling dcdc3 for ?");
|
||||
PMU.setDC3Voltage(3300); //doesn't go anywhere in the schematic??
|
||||
// Other power rails
|
||||
MESH_DEBUG_PRINTLN("Setting and enabling dcdc3 for ?");
|
||||
PMU.setDC3Voltage(3300); // doesn't go anywhere in the schematic??
|
||||
PMU.enableDC3();
|
||||
|
||||
//Unused power rails
|
||||
Serial.println("Disabling unused supplies dcdc2, dldo1 and dldo2");
|
||||
// Unused power rails
|
||||
MESH_DEBUG_PRINTLN("Disabling unused supplies dcdc2, dldo1 and dldo2");
|
||||
PMU.disableDC2();
|
||||
PMU.disableDLDO1();
|
||||
PMU.disableDLDO2();
|
||||
PMU.disableDLDO2();
|
||||
|
||||
//Set charge current to 300mA
|
||||
Serial.println("Setting battery charge current limit and voltage");
|
||||
PMU.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_300MA);
|
||||
// Set charge current to 300mA
|
||||
MESH_DEBUG_PRINTLN("Setting battery charge current limit and voltage");
|
||||
PMU.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_500MA);
|
||||
PMU.setChargeTargetVoltage(XPOWERS_AXP2101_CHG_VOL_4V2);
|
||||
|
||||
//enable battery voltage measurement
|
||||
Serial.println("Enabling battery measurement");
|
||||
// enable battery voltage measurement
|
||||
MESH_DEBUG_PRINTLN("Enabling battery measurement");
|
||||
PMU.enableBattVoltageMeasure();
|
||||
|
||||
//Reset and re-enable PMU interrupts
|
||||
Serial.println("Re-enable interrupts");
|
||||
// Reset and re-enable PMU interrupts
|
||||
MESH_DEBUG_PRINTLN("Re-enable interrupts");
|
||||
PMU.disableIRQ(XPOWERS_AXP2101_ALL_IRQ);
|
||||
PMU.clearIrqStatus();
|
||||
PMU.enableIRQ(
|
||||
XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | //Battery interrupts
|
||||
XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | //VBUS interrupts
|
||||
XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | //Power Key interrupts
|
||||
XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ //Charging interrupts
|
||||
XPOWERS_AXP2101_BAT_INSERT_IRQ | XPOWERS_AXP2101_BAT_REMOVE_IRQ | // Battery interrupts
|
||||
XPOWERS_AXP2101_VBUS_INSERT_IRQ | XPOWERS_AXP2101_VBUS_REMOVE_IRQ | // VBUS interrupts
|
||||
XPOWERS_AXP2101_PKEY_SHORT_IRQ | XPOWERS_AXP2101_PKEY_LONG_IRQ | // Power Key interrupts
|
||||
XPOWERS_AXP2101_BAT_CHG_DONE_IRQ | XPOWERS_AXP2101_BAT_CHG_START_IRQ // Charging interrupts
|
||||
);
|
||||
#ifdef MESH_DEBUG
|
||||
printPMU();
|
||||
#endif
|
||||
|
||||
//Set the power key off press time
|
||||
// Set the power key off press time
|
||||
PMU.setPowerKeyPressOffTime(XPOWERS_POWEROFF_4S);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool readStringUntil(Stream& s, char dest[], size_t max_len, char term, unsigned int timeout_millis) {
|
||||
unsigned long timeout = millis() + timeout_millis;
|
||||
char *dp = dest;
|
||||
while (millis() < timeout && dp - dest < max_len - 1) {
|
||||
if (s.available()) {
|
||||
char c = s.read();
|
||||
if (c == term) break;
|
||||
*dp++ = c; // append to dest[]
|
||||
} else {
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
*dp = 0; // null terminator
|
||||
return millis() < timeout; // false, if timed out
|
||||
}
|
||||
|
||||
static bool l76kProbe()
|
||||
{
|
||||
bool result = false;
|
||||
uint32_t startTimeout ;
|
||||
Serial1.write("$PCAS03,0,0,0,0,0,0,0,0,0,0,,,0,0*02\r\n");
|
||||
delay(5);
|
||||
// Get version information
|
||||
startTimeout = millis() + 3000;
|
||||
MESH_DEBUG_PRINTLN("Trying to init L76K GPS");
|
||||
// Serial1.flush();
|
||||
while (Serial1.available()) {
|
||||
int c = Serial1.read();
|
||||
// Serial.write(c);
|
||||
// Serial.print(".");
|
||||
// Serial.flush();
|
||||
// Serial1.flush();
|
||||
if (millis() > startTimeout) {
|
||||
MESH_DEBUG_PRINTLN("L76K NMEA timeout!");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Serial1.flush();
|
||||
delay(200);
|
||||
|
||||
Serial1.write("$PCAS06,0*1B\r\n");
|
||||
|
||||
char ver[100];
|
||||
if (!readStringUntil(Serial1, ver, sizeof(ver), '\n', 500)) {
|
||||
MESH_DEBUG_PRINTLN("Get L76K timeout!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (memcmp(ver, "$GPTXT,01,01,02", 15) == 0) {
|
||||
MESH_DEBUG_PRINTLN("L76K GNSS init succeeded, using L76K GNSS Module\n");
|
||||
result = true;
|
||||
}
|
||||
delay(500);
|
||||
|
||||
// Initialize the L76K Chip, use GPS + GLONASS
|
||||
Serial1.write("$PCAS04,5*1C\r\n");
|
||||
delay(250);
|
||||
// only ask for RMC and GGA
|
||||
Serial1.write("$PCAS03,1,0,0,0,1,0,0,0,0,0,,,0,0*02\r\n");
|
||||
delay(250);
|
||||
// Switch to Vehicle Mode, since SoftRF enables Aviation < 2g
|
||||
Serial1.write("$PCAS11,3*1E\r\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
bool radio_init() {
|
||||
fallback_clock.begin();
|
||||
Wire1.begin(PIN_BOARD_SDA1,PIN_BOARD_SCL1);
|
||||
@@ -154,13 +241,6 @@ bool radio_init() {
|
||||
return true; // success
|
||||
}
|
||||
|
||||
uint16_t getBattPercent() {
|
||||
//Read the PMU fuel guage for battery %
|
||||
uint16_t battPercent = PMU.getBatteryPercent();
|
||||
|
||||
return battPercent;
|
||||
}
|
||||
|
||||
uint32_t radio_get_rng_seed() {
|
||||
return radio.random(0x7FFFFFFF);
|
||||
}
|
||||
@@ -176,6 +256,82 @@ void radio_set_tx_power(uint8_t dbm) {
|
||||
radio.setOutputPower(dbm);
|
||||
}
|
||||
|
||||
void TbeamSupSensorManager::start_gps()
|
||||
{
|
||||
gps_active = true;
|
||||
pinMode(P_GPS_WAKE, OUTPUT);
|
||||
digitalWrite(P_GPS_WAKE, HIGH);
|
||||
}
|
||||
|
||||
void TbeamSupSensorManager::sleep_gps() {
|
||||
gps_active = false;
|
||||
pinMode(P_GPS_WAKE, OUTPUT);
|
||||
digitalWrite(P_GPS_WAKE, LOW);
|
||||
}
|
||||
|
||||
bool TbeamSupSensorManager::begin() {
|
||||
// init GPS port
|
||||
|
||||
Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, P_GPS_RX, P_GPS_TX);
|
||||
|
||||
bool result = false;
|
||||
for ( int i = 0; i < 3; ++i) {
|
||||
result = l76kProbe();
|
||||
if (result) {
|
||||
gps_active = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool TbeamSupSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
|
||||
if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
|
||||
telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, 0.0f);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void TbeamSupSensorManager::loop() {
|
||||
static long next_gps_update = 0;
|
||||
|
||||
_nmea->loop();
|
||||
|
||||
if (millis() > next_gps_update) {
|
||||
if (_nmea->isValid()) {
|
||||
node_lat = ((double)_nmea->getLatitude())/1000000.;
|
||||
node_lon = ((double)_nmea->getLongitude())/1000000.;
|
||||
//Serial.printf("lat %f lon %f\r\n", _lat, _lon);
|
||||
}
|
||||
next_gps_update = millis() + 1000;
|
||||
}
|
||||
}
|
||||
|
||||
int TbeamSupSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
|
||||
|
||||
const char* TbeamSupSensorManager::getSettingName(int i) const {
|
||||
return i == 0 ? "gps" : NULL;
|
||||
}
|
||||
|
||||
const char* TbeamSupSensorManager::getSettingValue(int i) const {
|
||||
if (i == 0) {
|
||||
return gps_active ? "1" : "0";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool TbeamSupSensorManager::setSettingValue(const char* name, const char* value) {
|
||||
if (strcmp(name, "gps") == 0) {
|
||||
if (strcmp(value, "0") == 0) {
|
||||
sleep_gps();
|
||||
} else {
|
||||
start_gps();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false; // not supported
|
||||
}
|
||||
|
||||
mesh::LocalIdentity radio_new_identity() {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
|
||||
@@ -1,17 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#define RADIOLIB_STATIC_ONLY 1
|
||||
#include <RadioLib.h>
|
||||
#include <helpers/RadioLibWrappers.h>
|
||||
#include <helpers/TBeamS3SupremeBoard.h>
|
||||
#include <helpers/CustomSX1262Wrapper.h>
|
||||
#include <helpers/AutoDiscoverRTCClock.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
#include <helpers/sensors/LocationProvider.h>
|
||||
|
||||
class TbeamSupSensorManager: public SensorManager {
|
||||
bool gps_active = false;
|
||||
LocationProvider * _nmea;
|
||||
|
||||
void start_gps();
|
||||
void sleep_gps();
|
||||
public:
|
||||
TbeamSupSensorManager(LocationProvider &nmea): _nmea(&nmea) { }
|
||||
bool begin() override;
|
||||
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
|
||||
void loop() override;
|
||||
int getNumSettings() const override;
|
||||
const char* getSettingName(int i) const override;
|
||||
const char* getSettingValue(int i) const override;
|
||||
bool setSettingValue(const char* name, const char* value) override;
|
||||
};
|
||||
|
||||
extern TBeamS3SupremeBoard board;
|
||||
extern WRAPPER_CLASS radio_driver;
|
||||
extern AutoDiscoverRTCClock rtc_clock;
|
||||
extern SensorManager sensors;
|
||||
extern TbeamSupSensorManager sensors;
|
||||
|
||||
enum {
|
||||
POWERMANAGE_ONLINE = _BV(0),
|
||||
DISPLAY_ONLINE = _BV(1),
|
||||
RADIO_ONLINE = _BV(2),
|
||||
GPS_ONLINE = _BV(3),
|
||||
PSRAM_ONLINE = _BV(4),
|
||||
SDCARD_ONLINE = _BV(5),
|
||||
AXDL345_ONLINE = _BV(6),
|
||||
BME280_ONLINE = _BV(7),
|
||||
BMP280_ONLINE = _BV(8),
|
||||
BME680_ONLINE = _BV(9),
|
||||
QMC6310_ONLINE = _BV(10),
|
||||
QMI8658_ONLINE = _BV(11),
|
||||
PCF8563_ONLINE = _BV(12),
|
||||
OSC32768_ONLINE = _BV(13),
|
||||
};
|
||||
|
||||
bool power_init();
|
||||
bool radio_init();
|
||||
uint32_t radio_get_rng_seed();
|
||||
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
|
||||
@@ -19,10 +19,11 @@ build_flags =
|
||||
-D P_LORA_TX_LED=2 ; LED pin for TX indication
|
||||
-D PIN_VBAT_READ=35 ; Battery voltage reading (analog pin)
|
||||
-D PIN_USER_BTN=0
|
||||
-D RADIO_CLASS=CustomSX1276
|
||||
-D ARDUINO_LOOP_STACK_SIZE=16384
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D RADIO_CLASS=CustomSX1276
|
||||
-D WRAPPER_CLASS=CustomSX1276Wrapper
|
||||
-D SX127X_CURRENT_LIMIT=120
|
||||
-D LORA_TX_POWER=20
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/lilygo_tlora_v2_1>
|
||||
@@ -89,6 +90,7 @@ build_flags =
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
|
||||
@@ -19,7 +19,7 @@ SensorManager sensors;
|
||||
bool radio_init() {
|
||||
fallback_clock.begin();
|
||||
rtc_clock.begin(Wire);
|
||||
|
||||
|
||||
spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
|
||||
int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8);
|
||||
if (status != RADIOLIB_ERR_NONE) {
|
||||
@@ -28,8 +28,12 @@ bool radio_init() {
|
||||
return false; // fail
|
||||
}
|
||||
|
||||
#ifdef SX127X_CURRENT_LIMIT
|
||||
radio.setCurrentLimit(SX127X_CURRENT_LIMIT);
|
||||
#endif
|
||||
|
||||
radio.setCRC(1);
|
||||
|
||||
|
||||
return true; // success
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ build_flags = ${nrf52840_base.build_flags}
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D PIN_BOARD_SCL=7
|
||||
@@ -36,6 +36,8 @@ build_flags =
|
||||
; -D MESH_DEBUG=1
|
||||
lib_deps = ${Faketec.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:Faketec_room_server]
|
||||
extends = Faketec
|
||||
@@ -52,6 +54,8 @@ build_flags = ${Faketec.build_flags}
|
||||
; -D MESH_DEBUG=1
|
||||
lib_deps = ${Faketec.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:Faketec_terminal_chat]
|
||||
extends = Faketec
|
||||
@@ -65,6 +69,8 @@ build_src_filter = ${Faketec.build_src_filter}
|
||||
lib_deps = ${Faketec.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:Faketec_companion_radio_usb]
|
||||
extends = Faketec
|
||||
@@ -79,6 +85,8 @@ build_src_filter = ${Faketec.build_src_filter}
|
||||
lib_deps = ${Faketec.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:Faketec_companion_radio_ble]
|
||||
extends = Faketec
|
||||
@@ -89,6 +97,7 @@ build_flags = ${Faketec.build_flags}
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
-D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${Faketec.build_src_filter}
|
||||
@@ -98,6 +107,8 @@ build_src_filter = ${Faketec.build_src_filter}
|
||||
lib_deps = ${Faketec.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[ProMicroLLCC68]
|
||||
extends = nrf52840_base
|
||||
@@ -108,7 +119,7 @@ build_flags = ${nrf52840_base.build_flags}
|
||||
-D RADIO_CLASS=CustomLLCC68
|
||||
-D WRAPPER_CLASS=CustomLLCC68Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter =
|
||||
${nrf52840_base.build_src_filter}
|
||||
@@ -127,6 +138,8 @@ build_flags = ${ProMicroLLCC68.build_flags}
|
||||
; -D MESH_DEBUG=1
|
||||
lib_deps = ${ProMicroLLCC68.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:ProMicroLLCC68_room_server]
|
||||
extends = ProMicroLLCC68
|
||||
@@ -140,6 +153,8 @@ build_flags = ${ProMicroLLCC68.build_flags}
|
||||
; -D MESH_DEBUG=1
|
||||
lib_deps = ${ProMicroLLCC68.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:ProMicroLLCC68_terminal_chat]
|
||||
extends = ProMicroLLCC68
|
||||
@@ -153,6 +168,8 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
|
||||
lib_deps = ${ProMicroLLCC68.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:ProMicroLLCC68_companion_radio_usb]
|
||||
extends = ProMicroLLCC68
|
||||
@@ -166,6 +183,8 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
|
||||
lib_deps = ${ProMicroLLCC68.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
|
||||
[env:ProMicroLLCC68_companion_radio_ble]
|
||||
extends = ProMicroLLCC68
|
||||
@@ -176,6 +195,7 @@ build_flags = ${ProMicroLLCC68.build_flags}
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
-D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${ProMicroLLCC68.build_src_filter}
|
||||
@@ -184,3 +204,5 @@ build_src_filter = ${ProMicroLLCC68.build_src_filter}
|
||||
lib_deps = ${ProMicroLLCC68.lib_deps}
|
||||
adafruit/RTClib @ ^2.1.3
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
robtillaart/INA3221 @ ^0.4.1
|
||||
robtillaart/INA219 @ ^0.4.1
|
||||
@@ -10,7 +10,7 @@ WRAPPER_CLASS radio_driver(radio, board);
|
||||
|
||||
VolatileRTCClock fallback_clock;
|
||||
AutoDiscoverRTCClock rtc_clock(fallback_clock);
|
||||
SensorManager sensors;
|
||||
PromicroSensorManager sensors;
|
||||
|
||||
#ifndef LORA_CR
|
||||
#define LORA_CR 5
|
||||
@@ -74,3 +74,92 @@ mesh::LocalIdentity radio_new_identity() {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
}
|
||||
|
||||
static INA3221 INA_3221(TELEM_INA3221_ADDRESS, &Wire);
|
||||
static INA219 INA_219(TELEM_INA219_ADDRESS, &Wire);
|
||||
|
||||
bool PromicroSensorManager::begin() {
|
||||
if (INA_3221.begin()) {
|
||||
MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", INA_3221.getAddress());
|
||||
MESH_DEBUG_PRINTLN("%04X %04X %04X", INA_3221.getDieID(), INA_3221.getManufacturerID(), INA_3221.getConfiguration());
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
INA_3221.setShuntR(i, TELEM_INA3221_SHUNT_VALUE);
|
||||
}
|
||||
INA3221initialized = true;
|
||||
} else {
|
||||
INA3221initialized = false;
|
||||
MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS);
|
||||
}
|
||||
if (INA_219.begin()) {
|
||||
MESH_DEBUG_PRINTLN("Found INA219 at address: %02X", INA_219.getAddress());
|
||||
INA219_CHANNEL = INA3221initialized ? TELEM_CHANNEL_SELF + 4 : TELEM_CHANNEL_SELF + 1;
|
||||
INA_219.setMaxCurrentShunt(TELEM_INA219_MAX_CURRENT, TELEM_INA219_SHUNT_VALUE);
|
||||
INA219initialized = true;
|
||||
} else {
|
||||
INA219initialized = false;
|
||||
MESH_DEBUG_PRINTLN("INA219 was not found at I2C address %02X", TELEM_INA219_ADDRESS);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PromicroSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
|
||||
if (requester_permissions & TELEM_PERM_ENVIRONMENT) {
|
||||
if (INA3221initialized) {
|
||||
for(int i = 0; i < 3; i++) {
|
||||
// add only enabled INA3221 channels to telemetry
|
||||
if (INA3221_CHANNEL_ENABLED[i]) {
|
||||
telemetry.addVoltage(INA3221_CHANNELS[i], INA_3221.getBusVoltage(i));
|
||||
telemetry.addCurrent(INA3221_CHANNELS[i], INA_3221.getCurrent(i));
|
||||
telemetry.addPower(INA3221_CHANNELS[i], INA_3221.getPower(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
if(INA219initialized) {
|
||||
telemetry.addVoltage(INA219_CHANNEL, INA_219.getBusVoltage());
|
||||
telemetry.addCurrent(INA219_CHANNEL, INA_219.getCurrent());
|
||||
telemetry.addPower(INA219_CHANNEL, INA_219.getPower());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int PromicroSensorManager::getNumSettings() const {
|
||||
return NUM_SENSOR_SETTINGS;
|
||||
}
|
||||
|
||||
const char* PromicroSensorManager::getSettingName(int i) const {
|
||||
if (i >= 0 && i < NUM_SENSOR_SETTINGS) {
|
||||
return INA3221_CHANNEL_NAMES[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* PromicroSensorManager::getSettingValue(int i) const {
|
||||
if (i >= 0 && i < NUM_SENSOR_SETTINGS) {
|
||||
return INA3221_CHANNEL_ENABLED[i] ? "1" : "0";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool PromicroSensorManager::setSettingValue(const char* name, const char* value) {
|
||||
for (int i = 0; i < NUM_SENSOR_SETTINGS; i++) {
|
||||
if (strcmp(name, INA3221_CHANNEL_NAMES[i]) == 0) {
|
||||
int channelEnabled = INA_3221.getEnableChannel(i);
|
||||
if (strcmp(value, "1") == 0) {
|
||||
INA3221_CHANNEL_ENABLED[i] = true;
|
||||
if (!channelEnabled) {
|
||||
INA_3221.enableChannel(i);
|
||||
}
|
||||
} else {
|
||||
INA3221_CHANNEL_ENABLED[i] = false;
|
||||
if (channelEnabled) {
|
||||
INA_3221.disableChannel(i);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -8,14 +8,52 @@
|
||||
#include <helpers/CustomLLCC68Wrapper.h>
|
||||
#include <helpers/AutoDiscoverRTCClock.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
#include <INA3221.h>
|
||||
#include <INA219.h>
|
||||
|
||||
#define NUM_SENSOR_SETTINGS 3
|
||||
|
||||
extern PromicroBoard board;
|
||||
extern WRAPPER_CLASS radio_driver;
|
||||
extern AutoDiscoverRTCClock rtc_clock;
|
||||
extern SensorManager sensors;
|
||||
|
||||
|
||||
bool radio_init();
|
||||
uint32_t radio_get_rng_seed();
|
||||
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
void radio_set_tx_power(uint8_t dbm);
|
||||
mesh::LocalIdentity radio_new_identity();
|
||||
|
||||
#define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current sensor I2C address
|
||||
#define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address
|
||||
|
||||
#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts
|
||||
#define TELEM_INA3221_SETTING_CH1 "INA3221-1"
|
||||
#define TELEM_INA3221_SETTING_CH2 "INA3221-2"
|
||||
#define TELEM_INA3221_SETTING_CH3 "INA3221-3"
|
||||
|
||||
#define TELEM_INA219_SHUNT_VALUE 0.100 // shunt value in ohms (may differ between manufacturers)
|
||||
#define TELEM_INA219_MAX_CURRENT 5
|
||||
|
||||
class PromicroSensorManager: public SensorManager {
|
||||
bool INA3221initialized = false;
|
||||
bool INA219initialized = false;
|
||||
|
||||
// INA3221 channels in telemetry
|
||||
int INA3221_CHANNELS[NUM_SENSOR_SETTINGS] = {TELEM_CHANNEL_SELF + 1, TELEM_CHANNEL_SELF + 2, TELEM_CHANNEL_SELF+ 3};
|
||||
const char * INA3221_CHANNEL_NAMES[NUM_SENSOR_SETTINGS] = { TELEM_INA3221_SETTING_CH1, TELEM_INA3221_SETTING_CH2, TELEM_INA3221_SETTING_CH3};
|
||||
bool INA3221_CHANNEL_ENABLED[NUM_SENSOR_SETTINGS] = {true, true, true};
|
||||
|
||||
int INA219_CHANNEL;
|
||||
public:
|
||||
PromicroSensorManager(){};
|
||||
bool begin() override;
|
||||
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
|
||||
int getNumSettings() const override;
|
||||
const char* getSettingName(int i) const override;
|
||||
const char* getSettingValue(int i) const override;
|
||||
bool setSettingValue(const char* name, const char* value) override;
|
||||
};
|
||||
|
||||
|
||||
extern PromicroSensorManager sensors;
|
||||
@@ -13,7 +13,7 @@ build_flags = ${nrf52840_base.build_flags}
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${nrf52840_base.build_src_filter}
|
||||
+<helpers/nrf52/RAK4631Board.cpp>
|
||||
@@ -81,6 +81,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -14,7 +14,7 @@ build_flags =
|
||||
-D PIN_USER_BTN=0
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
; -D SX126X_RX_BOOSTED_GAIN=1 - DO NOT ENABLE THIS!
|
||||
; https://wiki.uniteng.com/en/meshtastic/station-g2#impact-of-lora-node-dense-areashigh-noise-environments-on-rf-performance
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
|
||||
@@ -43,6 +43,7 @@ build_flags = ${t1000-e.build_flags}
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
-D RX_BOOSTED_GAIN=true
|
||||
-D RF_SWITCH_TABLE
|
||||
-D HAS_UI
|
||||
|
||||
@@ -20,12 +20,15 @@ build_flags = ${nrf52840_t114.build_flags}
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${nrf52840_t114.build_src_filter}
|
||||
+<helpers/*.cpp>
|
||||
+<helpers/nrf52/T114Board.cpp>
|
||||
+<../variants/t114>
|
||||
lib_deps =
|
||||
${nrf52840_t114.lib_deps}
|
||||
stevemarple/MicroNMEA @ ^2.0.6
|
||||
debug_tool = jlink
|
||||
upload_protocol = nrfutil
|
||||
|
||||
@@ -68,6 +71,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include "target.h"
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#include <helpers/sensors/MicroNMEALocationProvider.h>
|
||||
|
||||
T114Board board;
|
||||
|
||||
@@ -10,7 +11,8 @@ WRAPPER_CLASS radio_driver(radio, board);
|
||||
|
||||
VolatileRTCClock fallback_clock;
|
||||
AutoDiscoverRTCClock rtc_clock(fallback_clock);
|
||||
SensorManager sensors;
|
||||
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
|
||||
T114SensorManager sensors = T114SensorManager(nmea);
|
||||
|
||||
#ifndef LORA_CR
|
||||
#define LORA_CR 5
|
||||
@@ -68,3 +70,90 @@ mesh::LocalIdentity radio_new_identity() {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
}
|
||||
|
||||
void T114SensorManager::start_gps() {
|
||||
if (!gps_active) {
|
||||
gps_active = true;
|
||||
_location->begin();
|
||||
}
|
||||
}
|
||||
|
||||
void T114SensorManager::stop_gps() {
|
||||
if (gps_active) {
|
||||
gps_active = false;
|
||||
_location->stop();
|
||||
}
|
||||
}
|
||||
|
||||
bool T114SensorManager::begin() {
|
||||
Serial1.begin(9600);
|
||||
|
||||
// Try to detect if GPS is physically connected to determine if we should expose the setting
|
||||
pinMode(GPS_EN, OUTPUT);
|
||||
digitalWrite(GPS_EN, HIGH); // Power on GPS
|
||||
|
||||
// Give GPS a moment to power up and send data
|
||||
delay(500);
|
||||
|
||||
// We'll consider GPS detected if we see any data on Serial1
|
||||
gps_detected = (Serial1.available() > 0);
|
||||
|
||||
if (gps_detected) {
|
||||
MESH_DEBUG_PRINTLN("GPS detected");
|
||||
digitalWrite(GPS_EN, LOW); // Power off GPS until the setting is changed
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN("No GPS detected");
|
||||
digitalWrite(GPS_EN, LOW);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool T114SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
|
||||
if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
|
||||
telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, 0.0f);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void T114SensorManager::loop() {
|
||||
static long next_gps_update = 0;
|
||||
|
||||
_location->loop();
|
||||
|
||||
if (millis() > next_gps_update) {
|
||||
if (_location->isValid()) {
|
||||
node_lat = ((double)_location->getLatitude())/1000000.;
|
||||
node_lon = ((double)_location->getLongitude())/1000000.;
|
||||
MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
|
||||
}
|
||||
next_gps_update = millis() + 1000;
|
||||
}
|
||||
}
|
||||
|
||||
int T114SensorManager::getNumSettings() const {
|
||||
return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
|
||||
}
|
||||
|
||||
const char* T114SensorManager::getSettingName(int i) const {
|
||||
return (gps_detected && i == 0) ? "gps" : NULL;
|
||||
}
|
||||
|
||||
const char* T114SensorManager::getSettingValue(int i) const {
|
||||
if (gps_detected && i == 0) {
|
||||
return gps_active ? "1" : "0";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool T114SensorManager::setSettingValue(const char* name, const char* value) {
|
||||
if (gps_detected && strcmp(name, "gps") == 0) {
|
||||
if (strcmp(value, "0") == 0) {
|
||||
stop_gps();
|
||||
} else {
|
||||
start_gps();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false; // not supported
|
||||
}
|
||||
|
||||
@@ -7,11 +7,30 @@
|
||||
#include <helpers/CustomSX1262Wrapper.h>
|
||||
#include <helpers/AutoDiscoverRTCClock.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
#include <helpers/sensors/LocationProvider.h>
|
||||
|
||||
class T114SensorManager : public SensorManager {
|
||||
bool gps_active = false;
|
||||
bool gps_detected = false;
|
||||
LocationProvider* _location;
|
||||
|
||||
void start_gps();
|
||||
void stop_gps();
|
||||
public:
|
||||
T114SensorManager(LocationProvider &location): _location(&location) { }
|
||||
bool begin() override;
|
||||
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
|
||||
void loop() override;
|
||||
int getNumSettings() const override;
|
||||
const char* getSettingName(int i) const override;
|
||||
const char* getSettingValue(int i) const override;
|
||||
bool setSettingValue(const char* name, const char* value) override;
|
||||
};
|
||||
|
||||
extern T114Board board;
|
||||
extern WRAPPER_CLASS radio_driver;
|
||||
extern AutoDiscoverRTCClock rtc_clock;
|
||||
extern SensorManager sensors;
|
||||
extern T114SensorManager sensors;
|
||||
|
||||
bool radio_init();
|
||||
uint32_t radio_get_rng_seed();
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// UART pin definition
|
||||
|
||||
#define PIN_SERIAL1_RX (39)
|
||||
#define PIN_SERIAL1_TX (37)
|
||||
#define PIN_SERIAL1_RX (37)
|
||||
#define PIN_SERIAL1_TX (39)
|
||||
|
||||
#define PIN_SERIAL2_RX (9)
|
||||
#define PIN_SERIAL2_TX (10)
|
||||
@@ -69,7 +69,7 @@
|
||||
#define LED_BUILTIN (35)
|
||||
#define PIN_LED LED_BUILTIN
|
||||
#define LED_RED LED_BUILTIN
|
||||
#define LED_BLUE LED_BUILTIN
|
||||
#define LED_BLUE (-1) // No blue led, prevents Bluefruit flashing the green LED during advertising
|
||||
#define LED_PIN LED_BUILTIN
|
||||
|
||||
#define LED_STATE_ON HIGH
|
||||
@@ -112,6 +112,12 @@
|
||||
#define PIN_BUZZER (46)
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPS
|
||||
|
||||
#define GPS_EN (21)
|
||||
#define GPS_RESET (38)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TFT
|
||||
#define PIN_TFT_SCL (40)
|
||||
|
||||
@@ -19,7 +19,7 @@ build_flags = ${nrf52840_techo.build_flags}
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${nrf52840_techo.build_src_filter}
|
||||
+<helpers/*.cpp>
|
||||
@@ -63,6 +63,7 @@ build_flags =
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
-D DISPLAY_CLASS=GxEPDDisplay
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
-D HAS_GxEPD
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
|
||||
@@ -19,7 +19,7 @@ build_flags = ${nrf52840_thinknode_m1.build_flags}
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${nrf52840_thinknode_m1.build_src_filter}
|
||||
+<helpers/*.cpp>
|
||||
@@ -71,6 +71,7 @@ build_flags =
|
||||
-D DISPLAY_ROTATION=4
|
||||
-D DISPLAY_CLASS=GxEPDDisplay
|
||||
-D HAS_GxEPD
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
31
variants/wio-e5/platformio.ini
Normal file
31
variants/wio-e5/platformio.ini
Normal file
@@ -0,0 +1,31 @@
|
||||
[lora_e5]
|
||||
extends = stm32_base
|
||||
board = lora_e5_dev_board
|
||||
board_upload.maximum_size = 229376 ; 32kb for FS
|
||||
build_flags = ${stm32_base.build_flags}
|
||||
-D RADIO_CLASS=CustomSTM32WLx
|
||||
-D WRAPPER_CLASS=CustomSTM32WLxWrapper
|
||||
-D SPI_INTERFACES_COUNT=0
|
||||
-I variants/wio-e5
|
||||
build_src_filter = ${stm32_base.build_src_filter}
|
||||
+<../variants/wio-e5>
|
||||
|
||||
[env:wio-e5-repeater]
|
||||
extends = lora_e5
|
||||
build_flags = ${lora_e5.build_flags}
|
||||
-D LORA_TX_POWER=20
|
||||
-D ADVERT_NAME='"WIO-E5 Repeater"'
|
||||
-D ADMIN_PASSWORD='"password"'
|
||||
build_src_filter = ${lora_e5.build_src_filter}
|
||||
+<../examples/simple_repeater/main.cpp>
|
||||
|
||||
[env:wio-e5_companion_radio_usb]
|
||||
extends = lora_e5
|
||||
build_flags = ${lora_e5.build_flags}
|
||||
-D LORA_TX_POWER=20
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
build_src_filter = ${lora_e5.build_src_filter}
|
||||
+<../examples/companion_radio/*.cpp>
|
||||
lib_deps = ${lora_e5.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
69
variants/wio-e5/target.cpp
Normal file
69
variants/wio-e5/target.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <Arduino.h>
|
||||
#include "target.h"
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
|
||||
STM32Board board;
|
||||
|
||||
RADIO_CLASS radio = new STM32WLx_Module();
|
||||
|
||||
WRAPPER_CLASS radio_driver(radio, board);
|
||||
|
||||
static const uint32_t rfswitch_pins[] = {PA4, PA5, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC};
|
||||
static const Module::RfSwitchMode_t rfswitch_table[] = {
|
||||
{STM32WLx::MODE_IDLE, {LOW, LOW}},
|
||||
{STM32WLx::MODE_RX, {HIGH, LOW}},
|
||||
{STM32WLx::MODE_TX_HP, {LOW, HIGH}}, // for LoRa-E5 mini
|
||||
// {STM32WLx::MODE_TX_LP, {HIGH, HIGH}}, // for LoRa-E5-LE mini
|
||||
END_OF_MODE_TABLE,
|
||||
};
|
||||
|
||||
VolatileRTCClock rtc_clock;
|
||||
SensorManager sensors;
|
||||
|
||||
#ifndef LORA_CR
|
||||
#define LORA_CR 5
|
||||
#endif
|
||||
|
||||
bool radio_init() {
|
||||
// rtc_clock.begin(Wire);
|
||||
|
||||
// #ifdef SX126X_DIO3_TCXO_VOLTAGE
|
||||
// float tcxo = SX126X_DIO3_TCXO_VOLTAGE;
|
||||
// #else
|
||||
// float tcxo = 1.6f;
|
||||
// #endif
|
||||
|
||||
radio.setRfSwitchTable(rfswitch_pins, rfswitch_table);
|
||||
|
||||
int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, 1.7, 0);
|
||||
|
||||
if (status != RADIOLIB_ERR_NONE) {
|
||||
Serial.print("ERROR: radio init failed: ");
|
||||
Serial.println(status);
|
||||
return false; // fail
|
||||
}
|
||||
|
||||
radio.setCRC(1);
|
||||
|
||||
return true; // success
|
||||
}
|
||||
|
||||
uint32_t radio_get_rng_seed() {
|
||||
return radio.random(0x7FFFFFFF);
|
||||
}
|
||||
|
||||
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
|
||||
radio.setFrequency(freq);
|
||||
radio.setSpreadingFactor(sf);
|
||||
radio.setBandwidth(bw);
|
||||
radio.setCodingRate(cr);
|
||||
}
|
||||
|
||||
void radio_set_tx_power(uint8_t dbm) {
|
||||
radio.setOutputPower(dbm);
|
||||
}
|
||||
|
||||
mesh::LocalIdentity radio_new_identity() {
|
||||
RadioNoiseListener rng(radio);
|
||||
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
}
|
||||
20
variants/wio-e5/target.h
Normal file
20
variants/wio-e5/target.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#define RADIOLIB_STATIC_ONLY 1
|
||||
#include <RadioLib.h>
|
||||
#include <helpers/RadioLibWrappers.h>
|
||||
#include <helpers/stm32/STM32Board.h>
|
||||
#include <helpers/CustomSTM32WLxWrapper.h>
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
|
||||
extern STM32Board board;
|
||||
extern WRAPPER_CLASS radio_driver;
|
||||
extern VolatileRTCClock rtc_clock;
|
||||
extern SensorManager sensors;
|
||||
|
||||
bool radio_init();
|
||||
uint32_t radio_get_rng_seed();
|
||||
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
void radio_set_tx_power(uint8_t dbm);
|
||||
mesh::LocalIdentity radio_new_identity();
|
||||
10
variants/wio-e5/variant.h
Normal file
10
variants/wio-e5/variant.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
// UART Definitions
|
||||
#ifndef SERIAL_UART_INSTANCE
|
||||
#define SERIAL_UART_INSTANCE 101
|
||||
#endif
|
||||
|
||||
#include <variant_LORA_E5_MINI.h>
|
||||
|
||||
#undef RNG
|
||||
@@ -15,7 +15,7 @@ build_flags =
|
||||
-D PIN_BOARD_SCL=D7
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130.0f ; for best TX power!
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
build_src_filter = ${esp32_base.build_src_filter}
|
||||
+<../variants/xiao_c3>
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ build_flags = ${nrf52840_xiao.build_flags}
|
||||
-D P_LORA_NSS=D4
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=1
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D SX126X_RX_BOOSTED_GAIN=1
|
||||
build_src_filter = ${nrf52840_xiao.build_src_filter}
|
||||
+<helpers/*.cpp>
|
||||
@@ -49,6 +49,7 @@ build_flags =
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
|
||||
@@ -17,7 +17,7 @@ build_flags = ${esp32_base.build_flags}
|
||||
-D PIN_STATUS_LED=48
|
||||
-D SX126X_DIO2_AS_RF_SWITCH=true
|
||||
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
|
||||
-D SX126X_CURRENT_LIMIT=130
|
||||
-D SX126X_CURRENT_LIMIT=140
|
||||
-D RADIO_CLASS=CustomSX1262
|
||||
-D WRAPPER_CLASS=CustomSX1262Wrapper
|
||||
-D LORA_TX_POWER=22
|
||||
@@ -81,6 +81,7 @@ build_flags =
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D DISPLAY_CLASS=SSD1306Display
|
||||
-D OFFLINE_QUEUE_SIZE=256
|
||||
; -D BLE_DEBUG_LOGGING=1
|
||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||
@@ -111,5 +112,3 @@ build_src_filter = ${Xiao_S3_WIO.build_src_filter}
|
||||
lib_deps =
|
||||
${Xiao_S3_WIO.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user