Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:2c9f4d24d521, committed 2021-11-02
- Comitter:
- ruslanbredun
- Date:
- Tue Nov 02 11:31:02 2021 +0000
- Parent:
- 0:858059db6068
- Commit message:
- Clear lib;
Changed in this revision
diff -r 858059db6068 -r 2c9f4d24d521 BufferedSerial/Buffer/MyBuffer.cpp --- a/BufferedSerial/Buffer/MyBuffer.cpp Tue Nov 02 11:28:01 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ - -/** - * @file Buffer.cpp - * @brief Software Buffer - Templated Ring Buffer for most data types - * @author sam grove - * @version 1.0 - * @see - * - * Copyright (c) 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "MyBuffer.h" - -template <class T> -MyBuffer<T>::MyBuffer(uint32_t size) -{ - _buf = new T [size]; - _size = size; - clear(); - - return; -} - -template <class T> -MyBuffer<T>::~MyBuffer() -{ - delete [] _buf; - - return; -} - -template <class T> -uint32_t MyBuffer<T>::getSize() -{ - return this->_size; -} - -template <class T> -void MyBuffer<T>::clear(void) -{ - _wloc = 0; - _rloc = 0; - memset(_buf, 0, _size); - - return; -} - -template <class T> -uint32_t MyBuffer<T>::peek(char c) -{ - return 1; -} - -// make the linker aware of some possible types -template class MyBuffer<uint8_t>; -template class MyBuffer<int8_t>; -template class MyBuffer<uint16_t>; -template class MyBuffer<int16_t>; -template class MyBuffer<uint32_t>; -template class MyBuffer<int32_t>; -template class MyBuffer<uint64_t>; -template class MyBuffer<int64_t>; -template class MyBuffer<char>; -template class MyBuffer<wchar_t>;
diff -r 858059db6068 -r 2c9f4d24d521 BufferedSerial/Buffer/MyBuffer.h --- a/BufferedSerial/Buffer/MyBuffer.h Tue Nov 02 11:28:01 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,163 +0,0 @@ - -/** - * @file Buffer.h - * @brief Software Buffer - Templated Ring Buffer for most data types - * @author sam grove - * @version 1.0 - * @see - * - * Copyright (c) 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MYBUFFER_H -#define MYBUFFER_H - -#include <stdint.h> -#include <string.h> - -/** A templated software ring buffer - * - * Example: - * @code - * #include "mbed.h" - * #include "MyBuffer.h" - * - * MyBuffer <char> buf; - * - * int main() - * { - * buf = 'a'; - * buf.put('b'); - * char *head = buf.head(); - * puts(head); - * - * char whats_in_there[2] = {0}; - * int pos = 0; - * - * while(buf.available()) - * { - * whats_in_there[pos++] = buf; - * } - * printf("%c %c\n", whats_in_there[0], whats_in_there[1]); - * buf.clear(); - * error("done\n\n\n"); - * } - * @endcode - */ - -template <typename T> -class MyBuffer -{ -private: - T *_buf; - volatile uint32_t _wloc; - volatile uint32_t _rloc; - uint32_t _size; - -public: - /** Create a Buffer and allocate memory for it - * @param size The size of the buffer - */ - MyBuffer(uint32_t size = 0x100); - - /** Get the size of the ring buffer - * @return the size of the ring buffer - */ - uint32_t getSize(); - - /** Destry a Buffer and release it's allocated memory - */ - ~MyBuffer(); - - /** Add a data element into the buffer - * @param data Something to add to the buffer - */ - void put(T data); - - /** Remove a data element from the buffer - * @return Pull the oldest element from the buffer - */ - T get(void); - - /** Get the address to the head of the buffer - * @return The address of element 0 in the buffer - */ - T *head(void); - - /** Reset the buffer to 0. Useful if using head() to parse packeted data - */ - void clear(void); - - /** Determine if anything is readable in the buffer - * @return 1 if something can be read, 0 otherwise - */ - uint32_t available(void); - - /** Overloaded operator for writing to the buffer - * @param data Something to put in the buffer - * @return - */ - MyBuffer &operator= (T data) - { - put(data); - return *this; - } - - /** Overloaded operator for reading from the buffer - * @return Pull the oldest element from the buffer - */ - operator int(void) - { - return get(); - } - - uint32_t peek(char c); - -}; - -template <class T> -inline void MyBuffer<T>::put(T data) -{ - _buf[_wloc++] = data; - _wloc %= (_size-1); - - return; -} - -template <class T> -inline T MyBuffer<T>::get(void) -{ - T data_pos = _buf[_rloc++]; - _rloc %= (_size-1); - - return data_pos; -} - -template <class T> -inline T *MyBuffer<T>::head(void) -{ - T *data_pos = &_buf[0]; - - return data_pos; -} - -template <class T> -inline uint32_t MyBuffer<T>::available(void) -{ - return (_wloc == _rloc) ? 0 : 1; -} - -#endif -
diff -r 858059db6068 -r 2c9f4d24d521 BufferedSerial/BufferedSerial.cpp --- a/BufferedSerial/BufferedSerial.cpp Tue Nov 02 11:28:01 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,158 +0,0 @@ -/** - * @file BufferedSerial.cpp - * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX - * @author sam grove - * @version 1.0 - * @see - * - * Copyright (c) 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "BufferedSerial.h" -#include <stdarg.h> - -BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name) - : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size)) -{ - RawSerial::attach(callback(this, &BufferedSerial::rxIrq), Serial::RxIrq); - this->_buf_size = buf_size; - this->_tx_multiple = tx_multiple; - return; -} - -BufferedSerial::~BufferedSerial(void) -{ - RawSerial::attach(NULL, RawSerial::RxIrq); - RawSerial::attach(NULL, RawSerial::TxIrq); - - return; -} - -int BufferedSerial::readable(void) -{ - return _rxbuf.available(); // note: look if things are in the buffer -} - -int BufferedSerial::writeable(void) -{ - return 1; // buffer allows overwriting by design, always true -} - -int BufferedSerial::getc(void) -{ - return _rxbuf; -} - -int BufferedSerial::putc(int c) -{ - _txbuf = (char)c; - BufferedSerial::prime(); - - return c; -} - -int BufferedSerial::puts(const char *s) -{ - if (s != NULL) { - const char* ptr = s; - - while(*(ptr) != 0) { - _txbuf = *(ptr++); - } - _txbuf = '\n'; // done per puts definition - BufferedSerial::prime(); - - return (ptr - s) + 1; - } - return 0; -} - -int BufferedSerial::printf(const char* format, ...) -{ - char buffer[this->_buf_size]; - memset(buffer,0,this->_buf_size); - int r = 0; - - va_list arg; - va_start(arg, format); - r = vsprintf(buffer, format, arg); - // this may not hit the heap but should alert the user anyways - if(r > this->_buf_size) { - error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r); - va_end(arg); - return 0; - } - va_end(arg); - r = BufferedSerial::write(buffer, r); - - return r; -} - -ssize_t BufferedSerial::write(const void *s, size_t length) -{ - if (s != NULL && length > 0) { - const char* ptr = (const char*)s; - const char* end = ptr + length; - - while (ptr != end) { - _txbuf = *(ptr++); - } - BufferedSerial::prime(); - - return ptr - (const char*)s; - } - return 0; -} - - -void BufferedSerial::rxIrq(void) -{ - // read from the peripheral and make sure something is available - if(serial_readable(&_serial)) { - _rxbuf = serial_getc(&_serial); // if so load them into a buffer - } - - return; -} - -void BufferedSerial::txIrq(void) -{ - // see if there is room in the hardware fifo and if something is in the software fifo - while(serial_writable(&_serial)) { - if(_txbuf.available()) { - serial_putc(&_serial, (int)_txbuf.get()); - } else { - // disable the TX interrupt when there is nothing left to send - RawSerial::attach(NULL, RawSerial::TxIrq); - break; - } - } - - return; -} - -void BufferedSerial::prime(void) -{ - // if already busy then the irq will pick this up - if(serial_writable(&_serial)) { - RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq - BufferedSerial::txIrq(); // only write to hardware in one place - RawSerial::attach(callback(this, &BufferedSerial::txIrq), RawSerial::TxIrq); - } - - return; -} - -
diff -r 858059db6068 -r 2c9f4d24d521 BufferedSerial/BufferedSerial.h --- a/BufferedSerial/BufferedSerial.h Tue Nov 02 11:28:01 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,146 +0,0 @@ - -/** - * @file BufferedSerial.h - * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX - * @author sam grove - * @version 1.0 - * @see - * - * Copyright (c) 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BUFFEREDSERIAL_H -#define BUFFEREDSERIAL_H - -#include "mbed.h" -#include "MyBuffer.h" - -#if (MBED_MAJOR_VERSION == 5) && (MBED_MINOR_VERSION >= 2) -#elif (MBED_MAJOR_VERSION == 2) && (MBED_PATCH_VERSION > 130) -#else -#error "BufferedSerial version 13 and newer requires use of Mbed OS 5.2.0 and newer or Mbed 2 version 130 and newer. Use BufferedSerial version 12 and older or upgrade the Mbed version. -#endif - -/** A serial port (UART) for communication with other serial devices - * - * Can be used for Full Duplex communication, or Simplex by specifying - * one pin as NC (Not Connected) - * - * Example: - * @code - * #include "mbed.h" - * #include "BufferedSerial.h" - * - * BufferedSerial pc(USBTX, USBRX); - * - * int main() - * { - * while(1) - * { - * Timer s; - * - * s.start(); - * pc.printf("Hello World - buffered\n"); - * int buffered_time = s.read_us(); - * wait(0.1f); // give time for the buffer to empty - * - * s.reset(); - * printf("Hello World - blocking\n"); - * int polled_time = s.read_us(); - * s.stop(); - * wait(0.1f); // give time for the buffer to empty - * - * pc.printf("printf buffered took %d us\n", buffered_time); - * pc.printf("printf blocking took %d us\n", polled_time); - * wait(0.5f); - * } - * } - * @endcode - */ - -/** - * @class BufferedSerial - * @brief Software buffers and interrupt driven tx and rx for Serial - */ -class BufferedSerial : public RawSerial -{ -private: - MyBuffer <char> _rxbuf; - MyBuffer <char> _txbuf; - uint32_t _buf_size; - uint32_t _tx_multiple; - - void rxIrq(void); - void txIrq(void); - void prime(void); - -public: - /** Create a BufferedSerial port, connected to the specified transmit and receive pins - * @param tx Transmit pin - * @param rx Receive pin - * @param buf_size printf() buffer size - * @param tx_multiple amount of max printf() present in the internal ring buffer at one time - * @param name optional name - * @note Either tx or rx may be specified as NC if unused - */ - BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); - - /** Destroy a BufferedSerial port - */ - virtual ~BufferedSerial(void); - - /** Check on how many bytes are in the rx buffer - * @return 1 if something exists, 0 otherwise - */ - virtual int readable(void); - - /** Check to see if the tx buffer has room - * @return 1 always has room and can overwrite previous content if too small / slow - */ - virtual int writeable(void); - - /** Get a single byte from the BufferedSerial Port. - * Should check readable() before calling this. - * @return A byte that came in on the Serial Port - */ - virtual int getc(void); - - /** Write a single byte to the BufferedSerial Port. - * @param c The byte to write to the Serial Port - * @return The byte that was written to the Serial Port Buffer - */ - virtual int putc(int c); - - /** Write a string to the BufferedSerial Port. Must be NULL terminated - * @param s The string to write to the Serial Port - * @return The number of bytes written to the Serial Port Buffer - */ - virtual int puts(const char *s); - - /** Write a formatted string to the BufferedSerial Port. - * @param format The string + format specifiers to write to the Serial Port - * @return The number of bytes written to the Serial Port Buffer - */ - virtual int printf(const char* format, ...); - - /** Write data to the Buffered Serial Port - * @param s A pointer to data to send - * @param length The amount of data being pointed to - * @return The number of bytes written to the Serial Port Buffer - */ - virtual ssize_t write(const void *s, std::size_t length); -}; - -#endif