max32630fthr quad spi , unexpected spi behavior
FTHR_USB_serial/USBDevice/USBSerial/CircBuffer.h@0:a35c40f49345, 2018-03-13 (annotated)
- Committer:
- boonshen
- Date:
- Tue Mar 13 21:12:00 2018 +0000
- Revision:
- 0:a35c40f49345
MAX32630FTHR QuadSPI test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boonshen | 0:a35c40f49345 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
boonshen | 0:a35c40f49345 | 2 | * |
boonshen | 0:a35c40f49345 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
boonshen | 0:a35c40f49345 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
boonshen | 0:a35c40f49345 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
boonshen | 0:a35c40f49345 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
boonshen | 0:a35c40f49345 | 7 | * Software is furnished to do so, subject to the following conditions: |
boonshen | 0:a35c40f49345 | 8 | * |
boonshen | 0:a35c40f49345 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
boonshen | 0:a35c40f49345 | 10 | * substantial portions of the Software. |
boonshen | 0:a35c40f49345 | 11 | * |
boonshen | 0:a35c40f49345 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
boonshen | 0:a35c40f49345 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
boonshen | 0:a35c40f49345 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
boonshen | 0:a35c40f49345 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
boonshen | 0:a35c40f49345 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
boonshen | 0:a35c40f49345 | 17 | */ |
boonshen | 0:a35c40f49345 | 18 | |
boonshen | 0:a35c40f49345 | 19 | #ifndef CIRCBUFFER_H |
boonshen | 0:a35c40f49345 | 20 | #define CIRCBUFFER_H |
boonshen | 0:a35c40f49345 | 21 | |
boonshen | 0:a35c40f49345 | 22 | template <class T, int Size> |
boonshen | 0:a35c40f49345 | 23 | class CircBuffer { |
boonshen | 0:a35c40f49345 | 24 | public: |
boonshen | 0:a35c40f49345 | 25 | CircBuffer():write(0), read(0){} |
boonshen | 0:a35c40f49345 | 26 | bool isFull() { |
boonshen | 0:a35c40f49345 | 27 | return ((write + 1) % size == read); |
boonshen | 0:a35c40f49345 | 28 | }; |
boonshen | 0:a35c40f49345 | 29 | |
boonshen | 0:a35c40f49345 | 30 | bool isEmpty() { |
boonshen | 0:a35c40f49345 | 31 | return (read == write); |
boonshen | 0:a35c40f49345 | 32 | }; |
boonshen | 0:a35c40f49345 | 33 | |
boonshen | 0:a35c40f49345 | 34 | void queue(T k) { |
boonshen | 0:a35c40f49345 | 35 | if (isFull()) { |
boonshen | 0:a35c40f49345 | 36 | read++; |
boonshen | 0:a35c40f49345 | 37 | read %= size; |
boonshen | 0:a35c40f49345 | 38 | } |
boonshen | 0:a35c40f49345 | 39 | buf[write++] = k; |
boonshen | 0:a35c40f49345 | 40 | write %= size; |
boonshen | 0:a35c40f49345 | 41 | } |
boonshen | 0:a35c40f49345 | 42 | |
boonshen | 0:a35c40f49345 | 43 | uint16_t available() { |
boonshen | 0:a35c40f49345 | 44 | return (write >= read) ? write - read : size - read + write; |
boonshen | 0:a35c40f49345 | 45 | }; |
boonshen | 0:a35c40f49345 | 46 | |
boonshen | 0:a35c40f49345 | 47 | bool dequeue(T * c) { |
boonshen | 0:a35c40f49345 | 48 | bool empty = isEmpty(); |
boonshen | 0:a35c40f49345 | 49 | if (!empty) { |
boonshen | 0:a35c40f49345 | 50 | *c = buf[read++]; |
boonshen | 0:a35c40f49345 | 51 | read %= size; |
boonshen | 0:a35c40f49345 | 52 | } |
boonshen | 0:a35c40f49345 | 53 | return(!empty); |
boonshen | 0:a35c40f49345 | 54 | }; |
boonshen | 0:a35c40f49345 | 55 | |
boonshen | 0:a35c40f49345 | 56 | private: |
boonshen | 0:a35c40f49345 | 57 | volatile uint16_t write; |
boonshen | 0:a35c40f49345 | 58 | volatile uint16_t read; |
boonshen | 0:a35c40f49345 | 59 | static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram. |
boonshen | 0:a35c40f49345 | 60 | T buf[Size+1]; |
boonshen | 0:a35c40f49345 | 61 | }; |
boonshen | 0:a35c40f49345 | 62 | |
boonshen | 0:a35c40f49345 | 63 | #endif |