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.
Fork of MODSERIAL by
RESIZE.cpp@44:a04506a9a55b, 2014-12-12 (annotated)
- Committer:
- BlazeX
- Date:
- Fri Dec 12 08:49:27 2014 +0000
- Revision:
- 44:a04506a9a55b
- Parent:
- 43:31db07ebcb68
Fixed txBufferSetSize and rxBufferSetSize. Now working with buffers in use.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| AjK | 0:eb2522b41db8 | 1 | /* |
| AjK | 0:eb2522b41db8 | 2 | Copyright (c) 2010 Andy Kirkham |
| AjK | 0:eb2522b41db8 | 3 | |
| AjK | 0:eb2522b41db8 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| AjK | 0:eb2522b41db8 | 5 | of this software and associated documentation files (the "Software"), to deal |
| AjK | 0:eb2522b41db8 | 6 | in the Software without restriction, including without limitation the rights |
| AjK | 0:eb2522b41db8 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| AjK | 0:eb2522b41db8 | 8 | copies of the Software, and to permit persons to whom the Software is |
| AjK | 0:eb2522b41db8 | 9 | furnished to do so, subject to the following conditions: |
| AjK | 0:eb2522b41db8 | 10 | |
| AjK | 0:eb2522b41db8 | 11 | The above copyright notice and this permission notice shall be included in |
| AjK | 0:eb2522b41db8 | 12 | all copies or substantial portions of the Software. |
| AjK | 0:eb2522b41db8 | 13 | |
| AjK | 0:eb2522b41db8 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| AjK | 0:eb2522b41db8 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| AjK | 0:eb2522b41db8 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| AjK | 0:eb2522b41db8 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| AjK | 0:eb2522b41db8 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| AjK | 0:eb2522b41db8 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| AjK | 0:eb2522b41db8 | 20 | THE SOFTWARE. |
| AjK | 0:eb2522b41db8 | 21 | */ |
| AjK | 0:eb2522b41db8 | 22 | |
| AjK | 0:eb2522b41db8 | 23 | #include "MODSERIAL.h" |
| AjK | 0:eb2522b41db8 | 24 | #include "MACROS.h" |
| AjK | 0:eb2522b41db8 | 25 | |
| AjK | 0:eb2522b41db8 | 26 | namespace AjK { |
| AjK | 0:eb2522b41db8 | 27 | |
| AjK | 0:eb2522b41db8 | 28 | int |
| AjK | 0:eb2522b41db8 | 29 | MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check) |
| BlazeX | 44:a04506a9a55b | 30 | { |
| AjK | 0:eb2522b41db8 | 31 | // Make sure the ISR cannot use the buffers while we are manipulating them. |
| Sissors | 33:9c93ce7cb9d8 | 32 | NVIC_DisableIRQ(_IRQ); |
| AjK | 0:eb2522b41db8 | 33 | |
| BlazeX | 44:a04506a9a55b | 34 | // If the requested size is the same as the current size there's nothing to do, |
| BlazeX | 44:a04506a9a55b | 35 | // just continue to use the same buffer as it's fine as it is. |
| BlazeX | 44:a04506a9a55b | 36 | if (buffer_size[type] == size) |
| BlazeX | 44:a04506a9a55b | 37 | { |
| BlazeX | 44:a04506a9a55b | 38 | NVIC_EnableIRQ(_IRQ); |
| BlazeX | 44:a04506a9a55b | 39 | return Ok; |
| AjK | 0:eb2522b41db8 | 40 | } |
| AjK | 0:eb2522b41db8 | 41 | |
| BlazeX | 43:31db07ebcb68 | 42 | // is new buffer is big enough? |
| BlazeX | 43:31db07ebcb68 | 43 | if (size <= buffer_count[type]) |
| BlazeX | 44:a04506a9a55b | 44 | { |
| BlazeX | 44:a04506a9a55b | 45 | NVIC_EnableIRQ(_IRQ); |
| AjK | 0:eb2522b41db8 | 46 | return BufferOversize; |
| BlazeX | 44:a04506a9a55b | 47 | } |
| BlazeX | 43:31db07ebcb68 | 48 | |
| BlazeX | 43:31db07ebcb68 | 49 | // allocate new buffer |
| BlazeX | 43:31db07ebcb68 | 50 | char * newBuffer = (char*)malloc(size); |
| AjK | 0:eb2522b41db8 | 51 | |
| BlazeX | 43:31db07ebcb68 | 52 | // allocation failed? |
| BlazeX | 43:31db07ebcb68 | 53 | if (newBuffer == (char*)NULL) |
| BlazeX | 43:31db07ebcb68 | 54 | { |
| BlazeX | 43:31db07ebcb68 | 55 | if (memory_check) |
| AjK | 0:eb2522b41db8 | 56 | error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX"); |
| BlazeX | 43:31db07ebcb68 | 57 | |
| AjK | 0:eb2522b41db8 | 58 | return NoMemory; |
| AjK | 0:eb2522b41db8 | 59 | } |
| AjK | 0:eb2522b41db8 | 60 | |
| BlazeX | 43:31db07ebcb68 | 61 | // copy old buffer content to new one |
| BlazeX | 43:31db07ebcb68 | 62 | moveRingBuffer(newBuffer, type); |
| AjK | 0:eb2522b41db8 | 63 | |
| BlazeX | 43:31db07ebcb68 | 64 | // free old buffer and reset ring buffer cursor |
| BlazeX | 43:31db07ebcb68 | 65 | free((char*)buffer[type]); |
| BlazeX | 43:31db07ebcb68 | 66 | |
| BlazeX | 43:31db07ebcb68 | 67 | buffer[type] = newBuffer; |
| BlazeX | 43:31db07ebcb68 | 68 | buffer_size[type] = size; |
| BlazeX | 43:31db07ebcb68 | 69 | buffer_in[type] = buffer_count[type]; |
| BlazeX | 44:a04506a9a55b | 70 | buffer_out[type] = 0; |
| AjK | 0:eb2522b41db8 | 71 | |
| BlazeX | 44:a04506a9a55b | 72 | // Start the ISR system again with the new buffers. |
| BlazeX | 44:a04506a9a55b | 73 | NVIC_EnableIRQ(_IRQ); |
| AjK | 0:eb2522b41db8 | 74 | return Ok; |
| AjK | 0:eb2522b41db8 | 75 | } |
| AjK | 0:eb2522b41db8 | 76 | |
| BlazeX | 43:31db07ebcb68 | 77 | void MODSERIAL::moveRingBuffer(char * newBuffer, IrqType type) |
| BlazeX | 44:a04506a9a55b | 78 | { |
| BlazeX | 43:31db07ebcb68 | 79 | // copy old buffer content to new one |
| BlazeX | 43:31db07ebcb68 | 80 | if(buffer_in[type] > buffer_out[type]) |
| BlazeX | 43:31db07ebcb68 | 81 | { // content in the middle of the ring buffer |
| BlazeX | 44:a04506a9a55b | 82 | memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], buffer_count[type]); |
| BlazeX | 43:31db07ebcb68 | 83 | } |
| BlazeX | 43:31db07ebcb68 | 84 | else if(buffer_in[type] < buffer_out[type]) |
| BlazeX | 43:31db07ebcb68 | 85 | { // content split, free space in the middle |
| BlazeX | 43:31db07ebcb68 | 86 | // copy last part of the old buffer |
| BlazeX | 43:31db07ebcb68 | 87 | int end_count= buffer_size[type] - buffer_out[type]; |
| BlazeX | 43:31db07ebcb68 | 88 | memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], end_count); |
| BlazeX | 43:31db07ebcb68 | 89 | |
| BlazeX | 43:31db07ebcb68 | 90 | // copy first part of old buffer |
| BlazeX | 43:31db07ebcb68 | 91 | memcpy(&newBuffer[end_count], (char*)buffer[type], buffer_in[type]); |
| BlazeX | 43:31db07ebcb68 | 92 | } |
| BlazeX | 43:31db07ebcb68 | 93 | // else old buffer empty |
| BlazeX | 43:31db07ebcb68 | 94 | } |
| BlazeX | 43:31db07ebcb68 | 95 | |
| AjK | 0:eb2522b41db8 | 96 | }; // namespace AjK ends |
