From d6869d1ec4bd24cd2c3eafa534f0849b25ec5607 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 28 Feb 2019 17:04:22 -0500 Subject: added basic code --- arduino/cores/nRF5/utility/AdaCallback.c | 93 ++++++ arduino/cores/nRF5/utility/AdaCallback.cpp | 93 ++++++ arduino/cores/nRF5/utility/AdaCallback.h | 112 +++++++ arduino/cores/nRF5/utility/SoftwareTimer.h | 73 +++++ arduino/cores/nRF5/utility/TimeoutTimer.h | 56 ++++ arduino/cores/nRF5/utility/adafruit_fifo.cpp | 271 ++++++++++++++++ arduino/cores/nRF5/utility/adafruit_fifo.h | 87 ++++++ arduino/cores/nRF5/utility/debug.cpp | 444 +++++++++++++++++++++++++++ arduino/cores/nRF5/utility/debug.h | 71 +++++ arduino/cores/nRF5/utility/utilities.c | 102 ++++++ arduino/cores/nRF5/utility/utilities.h | 66 ++++ 11 files changed, 1468 insertions(+) create mode 100755 arduino/cores/nRF5/utility/AdaCallback.c create mode 100755 arduino/cores/nRF5/utility/AdaCallback.cpp create mode 100755 arduino/cores/nRF5/utility/AdaCallback.h create mode 100755 arduino/cores/nRF5/utility/SoftwareTimer.h create mode 100755 arduino/cores/nRF5/utility/TimeoutTimer.h create mode 100755 arduino/cores/nRF5/utility/adafruit_fifo.cpp create mode 100755 arduino/cores/nRF5/utility/adafruit_fifo.h create mode 100755 arduino/cores/nRF5/utility/debug.cpp create mode 100755 arduino/cores/nRF5/utility/debug.h create mode 100755 arduino/cores/nRF5/utility/utilities.c create mode 100755 arduino/cores/nRF5/utility/utilities.h (limited to 'arduino/cores/nRF5/utility') diff --git a/arduino/cores/nRF5/utility/AdaCallback.c b/arduino/cores/nRF5/utility/AdaCallback.c new file mode 100755 index 0000000..0066bc2 --- /dev/null +++ b/arduino/cores/nRF5/utility/AdaCallback.c @@ -0,0 +1,93 @@ +/**************************************************************************/ +/*! + @file AdaCallback.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#include "Arduino.h" + +static QueueHandle_t _cb_queue = NULL; + +void adafruit_callback_task(void* arg) +{ + (void) arg; + + while(1) + { + ada_callback_t* cb_data; + if ( xQueueReceive(_cb_queue, (void*) &cb_data, portMAX_DELAY) ) + { +// PRINT_HEX(cb_data); +// PRINT_HEX(cb_data->malloced_data); + + void* func = cb_data->callback_func; + uint32_t* args = cb_data->arguments; + + switch (cb_data->arg_count) + { + case 0: ((adacb_0arg_t) func)(); break; + case 1: ((adacb_1arg_t) func)(args[0]); break; + case 2: ((adacb_2arg_t) func)(args[0], args[1]); break; + case 3: ((adacb_3arg_t) func)(args[0], args[1], args[2]); break; + case 4: ((adacb_4arg_t) func)(args[0], args[1], args[2], args[3]); break; + case 5: ((adacb_5arg_t) func)(args[0], args[1], args[2], args[3], args[4]); break; + + default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM, dbg_err_str); break; + } + + // free up resource + if (cb_data->malloced_data) rtos_free(cb_data->malloced_data); + rtos_free(cb_data); + } + } +} + +void ada_callback_queue(ada_callback_t* cb_data, bool from_isr) +{ + if ( from_isr ) + { + xQueueSendFromISR(_cb_queue, (void*) &cb_data, NULL); + }else + { + xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT); + } +} + +void ada_callback_init(void) +{ + // queue to hold "Pointer to callback data" + _cb_queue = xQueueCreate(CFG_CALLBACK_QUEUE_LENGTH, sizeof(ada_callback_t*)); + + TaskHandle_t callback_task_hdl; + xTaskCreate( adafruit_callback_task, "Callback", CFG_CALLBACK_TASK_STACKSIZE, NULL, TASK_PRIO_NORMAL, &callback_task_hdl); +} diff --git a/arduino/cores/nRF5/utility/AdaCallback.cpp b/arduino/cores/nRF5/utility/AdaCallback.cpp new file mode 100755 index 0000000..0066bc2 --- /dev/null +++ b/arduino/cores/nRF5/utility/AdaCallback.cpp @@ -0,0 +1,93 @@ +/**************************************************************************/ +/*! + @file AdaCallback.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#include "Arduino.h" + +static QueueHandle_t _cb_queue = NULL; + +void adafruit_callback_task(void* arg) +{ + (void) arg; + + while(1) + { + ada_callback_t* cb_data; + if ( xQueueReceive(_cb_queue, (void*) &cb_data, portMAX_DELAY) ) + { +// PRINT_HEX(cb_data); +// PRINT_HEX(cb_data->malloced_data); + + void* func = cb_data->callback_func; + uint32_t* args = cb_data->arguments; + + switch (cb_data->arg_count) + { + case 0: ((adacb_0arg_t) func)(); break; + case 1: ((adacb_1arg_t) func)(args[0]); break; + case 2: ((adacb_2arg_t) func)(args[0], args[1]); break; + case 3: ((adacb_3arg_t) func)(args[0], args[1], args[2]); break; + case 4: ((adacb_4arg_t) func)(args[0], args[1], args[2], args[3]); break; + case 5: ((adacb_5arg_t) func)(args[0], args[1], args[2], args[3], args[4]); break; + + default: VERIFY_MESS(NRF_ERROR_INVALID_PARAM, dbg_err_str); break; + } + + // free up resource + if (cb_data->malloced_data) rtos_free(cb_data->malloced_data); + rtos_free(cb_data); + } + } +} + +void ada_callback_queue(ada_callback_t* cb_data, bool from_isr) +{ + if ( from_isr ) + { + xQueueSendFromISR(_cb_queue, (void*) &cb_data, NULL); + }else + { + xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT); + } +} + +void ada_callback_init(void) +{ + // queue to hold "Pointer to callback data" + _cb_queue = xQueueCreate(CFG_CALLBACK_QUEUE_LENGTH, sizeof(ada_callback_t*)); + + TaskHandle_t callback_task_hdl; + xTaskCreate( adafruit_callback_task, "Callback", CFG_CALLBACK_TASK_STACKSIZE, NULL, TASK_PRIO_NORMAL, &callback_task_hdl); +} diff --git a/arduino/cores/nRF5/utility/AdaCallback.h b/arduino/cores/nRF5/utility/AdaCallback.h new file mode 100755 index 0000000..b6a2708 --- /dev/null +++ b/arduino/cores/nRF5/utility/AdaCallback.h @@ -0,0 +1,112 @@ +/**************************************************************************/ +/*! + @file AdaCallback.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ +#ifndef ADACALLBACK_H_ +#define ADACALLBACK_H_ + +#include "common_inc.h" + +#define CFG_CALLBACK_TASK_STACKSIZE (512*2) +#define CFG_CALLBACK_QUEUE_LENGTH 20 +#define CFG_CALLBACK_TIMEOUT 100 + +//#ifdef __cplusplus +//extern "C"{ +//#endif + +typedef struct +{ + void* malloced_data; + void* callback_func; + + uint8_t arg_count; + bool from_isr; +// uint8_t callback_type; +// uint8_t _reserved[2]; + + uint32_t arguments[1]; // flexible array holder +}ada_callback_t; + +VERIFY_STATIC( sizeof(ada_callback_t) == 16 ); + +/*------------- Defer callback type, determined by number of arguments -------------*/ +typedef void (*adacb_0arg_t) (void); +typedef void (*adacb_1arg_t) (uint32_t); +typedef void (*adacb_2arg_t) (uint32_t, uint32_t); +typedef void (*adacb_3arg_t) (uint32_t, uint32_t, uint32_t); +typedef void (*adacb_4arg_t) (uint32_t, uint32_t, uint32_t, uint32_t); +typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); + +#ifdef __cplusplus +template +inline void _cb_setup(bool _from_isr, void *_malloced, Func _func, Args... args) +{ + uint8_t const _count = sizeof...(args); + ada_callback_t* cb_data = (ada_callback_t*) rtos_malloc( sizeof(ada_callback_t) + (_count ? (_count-1)*4 : 0) ); + cb_data->malloced_data = _malloced; + cb_data->callback_func = (void*)_func; + cb_data->arg_count = _count; + if ( _count ) { + uint32_t arguments[] = { ((uint32_t)args)... }; + memcpy(cb_data->arguments, arguments, 4*_count); + } + + extern void ada_callback_queue(ada_callback_t* cb_data, bool from_isr); + ada_callback_queue(cb_data, _from_isr); +} +#endif // __cplusplus + +/** + * Schedule an function and parameters to be invoked in Ada Callback Task + * Macro can take at least 2 and at max 7 arguments + * - 1st arg : pointer data that need to be freed with free(pointer) after function is invoked + * - 2nd arg : function to be invoked + * - 3rd-7th arg : function argument, will be cast to uint32_t + */ +#define ada_callback(... ) _cb_setup(false, __VA_ARGS__) + +/** + * Similar to ada_callback() but invoke in ISR-context + */ +#define ada_callback_fromISR(... ) _cb_setup(true , __VA_ARGS__) + +void ada_callback_init(void); +void ada_callback_queue(ada_callback_t* cb_data, bool from_isr); + +//#ifdef __cplusplus +//} +//#endif + +#endif /* ADACALLBACK_H_ */ diff --git a/arduino/cores/nRF5/utility/SoftwareTimer.h b/arduino/cores/nRF5/utility/SoftwareTimer.h new file mode 100755 index 0000000..ac72443 --- /dev/null +++ b/arduino/cores/nRF5/utility/SoftwareTimer.h @@ -0,0 +1,73 @@ +/**************************************************************************/ +/*! + @file SoftwareTimer.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ +#ifndef SOFTWARETIMER_H_ +#define SOFTWARETIMER_H_ + +#include "Arduino.h" + +class SoftwareTimer +{ + private: + TimerHandle_t _handle; + + public: + SoftwareTimer() { _handle = NULL; } + virtual ~SoftwareTimer() { if(_handle != NULL) xTimerDelete(_handle, 0); } + + void begin(uint32_t ms, TimerCallbackFunction_t callback) + { + _handle = xTimerCreate(NULL, ms2tick(ms), true, NULL, callback); + } + + TimerHandle_t getHandle(void) + { + return _handle; + } + + void start(void) { xTimerStart(_handle, 0); } + void stop (void) { xTimerStop (_handle, 0); } + + void setPeriod(uint32_t ms) + { + BaseType_t active = xTimerIsTimerActive(_handle); + xTimerChangePeriod(_handle, ms2tick(ms), 0); + + // Change period of inactive timer will also start it !! + if ( !active ) xTimerStop(_handle, 0); + } +}; + +#endif /* SOFTWARETIMER_H_ */ diff --git a/arduino/cores/nRF5/utility/TimeoutTimer.h b/arduino/cores/nRF5/utility/TimeoutTimer.h new file mode 100755 index 0000000..d0a8d68 --- /dev/null +++ b/arduino/cores/nRF5/utility/TimeoutTimer.h @@ -0,0 +1,56 @@ +/**************************************************************************/ +/*! + @file TimeoutTimer.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#ifndef _TIMEOUT_TIMER_H_ +#define _TIMEOUT_TIMER_H_ + +class TimeoutTimer +{ + private: + uint32_t start; + uint32_t interval; + + public: + TimeoutTimer() { start = millis(); interval = 0; } + TimeoutTimer(uint32_t msec) { set(msec); } + + void set(uint32_t msec) { start = millis(); interval = msec; } + bool expired(void) const { return (millis() - start) >= interval; } + void restart(void) { start = millis(); } + void reset(void) { start += interval; } // used for periodic invoke to prevent drift +}; + +#endif /* _TIMEOUT_TIMER_H_ */ diff --git a/arduino/cores/nRF5/utility/adafruit_fifo.cpp b/arduino/cores/nRF5/utility/adafruit_fifo.cpp new file mode 100755 index 0000000..e20ac1e --- /dev/null +++ b/arduino/cores/nRF5/utility/adafruit_fifo.cpp @@ -0,0 +1,271 @@ +/**************************************************************************/ +/*! + @file adafruit_fifo.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#include "adafruit_fifo.h" +#include + +/******************************************************************************/ +/*! + @brief Constructor + + @param[in] depth + Maximum number of items can be hold in buffer + @param[in] item_size + Number of bytes of each item +*/ +/******************************************************************************/ +Adafruit_FIFO::Adafruit_FIFO(uint8_t item_size, uint16_t depth) + : _item_size(item_size) +{ + _buffer = NULL; + _mutex = NULL; + _depth = depth; + _overwritable = false; + + _count = _wr_idx = _rd_idx = 0; +} + +void Adafruit_FIFO::begin(void) +{ + _buffer = (uint8_t*) malloc(_item_size*_depth); + _mutex = xSemaphoreCreateMutex(); +} + +void Adafruit_FIFO::begin(uint16_t depth) +{ + _depth = depth; + begin(); +} + +void Adafruit_FIFO::overwriteIfFull(bool enable) +{ + _overwritable = enable; +} + + +/** + * Destructor + * @return + */ +Adafruit_FIFO::~Adafruit_FIFO() +{ + if (_mutex) vSemaphoreDelete(_mutex); + if (_buffer) rtos_free(_buffer); +} + +bool Adafruit_FIFO::_mutex_lock(bool isr) +{ + (void) isr; + return xSemaphoreTake(_mutex, portMAX_DELAY); +} + +bool Adafruit_FIFO::_mutex_unlock(bool isr) +{ + (void) isr; + return xSemaphoreGive(_mutex); +} + + +/******************************************************************************/ +/*! + @brief Clear the FIFO +*/ +/******************************************************************************/ +void Adafruit_FIFO::clear(void) +{ + _mutex_lock(false); + _rd_idx = _wr_idx = _count = 0; + _mutex_unlock(false); +} + +/******************************************************************************/ +/*! + @brief Write an item to the FIFO + + @param[in] item + Memory address of the item +*/ +/******************************************************************************/ +uint16_t Adafruit_FIFO::write(void const* item) +{ + if ( full() && !_overwritable ) return 0; + + _mutex_lock(false); + + memcpy( _buffer + (_wr_idx * _item_size), + item, + _item_size); + + _wr_idx = (_wr_idx + 1) % _depth; + + if ( full() ) + { + _rd_idx = _wr_idx; // keep the full state (rd == wr && len = size) + } + else + { + _count++; + } + + _mutex_unlock(false); + + return 1; +} + +/******************************************************************************/ +/*! + @brief Write array of items to the FIFO + + @param[in] data + Memory address of the item's array + @param[in] n + Number of items to write + + @return Number of written items +*/ +/******************************************************************************/ +uint16_t Adafruit_FIFO::write(void const * data, uint16_t n) +{ + if ( n == 0 ) return 0; + + uint8_t* buf = (uint8_t*) data; + + uint16_t len = 0; + while( (len < n) && write(buf) ) + { + len++; + buf += _item_size; + } + + return len; +} + +/******************************************************************************/ +/*! + @brief Read an item from FIFO + + @param[in] buffer + Memory address to store item +*/ +/******************************************************************************/ +uint16_t Adafruit_FIFO::read(void* buffer) +{ + if( empty() ) return 0; + + _mutex_lock(false); + + memcpy(buffer, + _buffer + (_rd_idx * _item_size), + _item_size); + _rd_idx = (_rd_idx + 1) % _depth; + _count--; + + _mutex_unlock(false); + + return 1; +} + +/******************************************************************************/ +/*! + @brief Read multiple items to an array + + @param[in] buffer + Memory address of the item's array + @param[in] n + Number of items to read + + @return Number of read items +*/ +/******************************************************************************/ +uint16_t Adafruit_FIFO::read(void * buffer, uint16_t n) +{ + if( n == 0 ) return 0; + + uint8_t* buf = (uint8_t*) buffer; + + uint16_t len = 0; + while( (len < n) && read(buf) ) + { + len++; + buf += _item_size; + } + + return len; +} + +/******************************************************************************/ +/*! + @brief Read an item without removing it from the FIFO + + @param[in] buffer + Memory address to store item +*/ +/******************************************************************************/ +bool Adafruit_FIFO::peek(void* buffer) +{ + if( empty() ) return false; + + memcpy(buffer, + _buffer + (_rd_idx * _item_size), + _item_size); + + return true; +} + + +/******************************************************************************/ +/*! + @brief Read an item without removing it from the FIFO at the specific index + + @param[in] position + Position to read from in the FIFO buffer + + @param[in] buffer + Memory address to store item +*/ +/******************************************************************************/ +bool Adafruit_FIFO::peekAt(uint16_t position, void * p_buffer) +{ + if( empty() || (position >= _count) ) return false; + + uint16_t index = (_rd_idx + position) % _depth; // rd_idx is position=0 + memcpy(p_buffer, + _buffer + (index * _item_size), + _item_size); + + return true; +} + diff --git a/arduino/cores/nRF5/utility/adafruit_fifo.h b/arduino/cores/nRF5/utility/adafruit_fifo.h new file mode 100755 index 0000000..bfeb333 --- /dev/null +++ b/arduino/cores/nRF5/utility/adafruit_fifo.h @@ -0,0 +1,87 @@ +/**************************************************************************/ +/*! + @file adafruit_fifo.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#ifndef _Adafruit_FIFO_H_ +#define _Adafruit_FIFO_H_ + +#include +#include +#include + +class Adafruit_FIFO +{ + private: + uint8_t* _buffer ; ///< buffer pointer + uint16_t _depth ; ///< max items + const uint8_t _item_size ; ///< size of each item + bool _overwritable ; ///< Overwrite when full + volatile uint16_t _count ; ///< number of items in queue + volatile uint16_t _wr_idx ; ///< write pointer + volatile uint16_t _rd_idx ; ///< read pointer + + SemaphoreHandle_t _mutex; + + bool _mutex_lock(bool isr); + bool _mutex_unlock(bool isr); + + public: + // Constructor + Adafruit_FIFO(uint8_t item_size, uint16_t depth = 0); + + virtual ~Adafruit_FIFO(); + + void begin(void); + void begin(uint16_t depth); + + void clear(void); + void overwriteIfFull(bool enable); + + uint16_t write(void const* item); + uint16_t write(void const * data, uint16_t n); + + uint16_t read(void* buffer); + uint16_t read(void * buffer, uint16_t n); + + bool peek(void* buffer); + bool peekAt(uint16_t position, void * p_buffer); + + inline bool empty(void) { return _count == 0; } + inline bool full(void) { return _count == _depth; } + inline uint16_t count(void) { return _count; } + inline uint16_t remaining(void) { return _depth - _count; } +}; + +#endif /* _Adafruit_FIFO_H_ */ diff --git a/arduino/cores/nRF5/utility/debug.cpp b/arduino/cores/nRF5/utility/debug.cpp new file mode 100755 index 0000000..d786202 --- /dev/null +++ b/arduino/cores/nRF5/utility/debug.cpp @@ -0,0 +1,444 @@ +/**************************************************************************/ +/*! + @file debug.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ + +#include +#include +#include +#include +#include + +// defined in linker script +extern uint32_t __data_start__[]; +extern uint32_t __data_end__[]; + +extern uint32_t __bss_start__[]; +extern uint32_t __bss_end__[]; + +extern unsigned char __HeapBase[]; +extern unsigned char __HeapLimit[]; + +extern uint32_t __StackTop[]; +extern uint32_t __StackLimit[]; + +extern "C" +{ + +void HardFault_Handler(void) +{ + // reset on hardfault + NVIC_SystemReset(); +} + +int dbgHeapTotal(void) +{ + return ((uint32_t) __HeapLimit) - ((uint32_t) __HeapBase); +} + +int dbgHeapUsed(void) +{ + return (mallinfo()).uordblks; +} + +int dbgStackTotal(void) +{ + return ((uint32_t) __StackTop) - ((uint32_t) __StackLimit); +} + +int dbgStackUsed(void) +{ + enum { STACK_PATTERN = 0xADADADAD }; + + uint32_t * p_start = (uint32_t*) &__StackLimit; + uint32_t * p_end = (uint32_t*) &__StackTop; + + uint32_t * p_buf = p_start; + while( *p_buf == STACK_PATTERN && p_buf != p_end) + { + p_buf++; + } + + if (p_buf == p_end) return (-1); + + return ((uint32_t) p_end) - ((uint32_t) p_buf); +} + +static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used) +{ + char buffer[30]; + if ( used ) + { + sprintf(buffer, "%5lu / %5lu (%02lu%%)", used, top-bottom, (used*100)/ (top-bottom)); + }else + { + sprintf(buffer, "%lu", top-bottom); + } + + printf("| %-5s| 0x%04X - 0x%04X | %-19s |\n", name, (uint16_t) bottom, (uint16_t) (top-1), buffer); +} + +void dbgMemInfo(void) +{ + printf(" ______________________________________________\n"); + printf("| Name | Addr 0x2000xxxx | Usage |\n"); + printf("| ---------------------------------------------|\n"); + + // Pritn SRAM used for Stack executed by S132 and ISR + printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), dbgStackUsed() ); + + // Print Heap usage overall (including memory malloced to tasks) + printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed() ); + + // DATA + BSS + printMemRegion("Bss", ((uint32_t) __bss_end__), ((uint32_t) __data_start__), 0); + + // Print SRAM Used by SoftDevice + printMemRegion("S132", (uint32_t) __data_start__, 0x20000000, 0); + + printf("|______________________________________________|\n"); + printf("\n"); + + // Print Task list + uint32_t tasknum = uxTaskGetNumberOfTasks(); + char* buf = (char*) rtos_malloc(tasknum*40); // 40 bytes per task + + vTaskList(buf); + + printf("Task State Prio StackLeft Num\n"); + printf("-----------------------------------\n"); + printf(buf); + printf("\n"); + rtos_free(buf); +} + +void dbgPrintVersion(void) +{ + printf("\n"); + printf("BSP Library : " ARDUINO_BSP_VERSION "\n"); + printf("Bootloader : %s\n", getBootloaderVersion()); + printf("Serial No : %s\n", getMcuUniqueID()); + printf("\n"); +} + +/******************************************************************************/ +/*! + @brief Helper function to display memory contents in a friendly format +*/ +/******************************************************************************/ +static void dump_str_line(uint8_t const* buf, uint16_t count) +{ + // each line is 16 bytes + for(int i=0; i +#include "utilities.h" +#include +#include + +#include "nrf_sdm.h" +#include "nrf52/nrf_mbr.h" + + +/******************************************************************************/ +/*! + @brief Find the corresponding data from the key + @param +*/ +/******************************************************************************/ +void const * lookup_find(lookup_table_t const* p_table, uint32_t key) +{ + for(uint16_t i=0; icount; i++) + { + if (p_table->items[i].key == key) return p_table->items[i].data; + } + + return NULL; +} +/** + * Format: SDname SDverion, bootloader version + * e.g + * "s132 2.0.1, 0.5.0" + * "s132 5.0.0, 5.0.0 single bank" + * "s132 6.1.1 r0" + * @return + */ +const char* getBootloaderVersion(void) +{ + static char fw_str[30+1] = { 0 }; + + // Skip if already created + if ( fw_str[0] == 0 ) + { + uint32_t const sd_id = SD_ID_GET(MBR_SIZE); + uint32_t const sd_version = SD_VERSION_GET(MBR_SIZE); + + uint32_t const ver1 = sd_version / 1000000; + uint32_t const ver2 = (sd_version % 1000000)/1000; + uint32_t const ver3 = sd_version % 1000; + + sprintf(fw_str, "s%d %d.%d.%d r%d", sd_id, + ver1, ver2, ver3, U32_BYTE4(bootloaderVersion) ); + } + + return fw_str; +} + +const char* getMcuUniqueID(void) +{ + static char serial_str[16+1] = { 0 }; + + // Skip if already created + if ( serial_str[0] == 0 ) + { + sprintf(serial_str, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]); + } + + return serial_str; +} + diff --git a/arduino/cores/nRF5/utility/utilities.h b/arduino/cores/nRF5/utility/utilities.h new file mode 100755 index 0000000..3359587 --- /dev/null +++ b/arduino/cores/nRF5/utility/utilities.h @@ -0,0 +1,66 @@ +/**************************************************************************/ +/*! + @file utilities.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. 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. + 3. Neither the name of the copyright holders 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 ''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 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. +*/ +/**************************************************************************/ +#ifndef UTILITIES_H_ +#define UTILITIES_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct +{ + uint32_t key; + void const * data; +}lookup_entry_t; + +typedef struct +{ + uint16_t count; + lookup_entry_t const* items; +} lookup_table_t; + +void const * lookup_find(lookup_table_t const* p_table, uint32_t key); + +const char* getBootloaderVersion(void); +const char* getMcuUniqueID(void); + +#ifdef __cplusplus +} +#endif + +#endif /* UTILITIES_H_ */ -- cgit v1.2.3