MODSERIAL with support for more devices

Fork of MODSERIAL by Erik -

Committer:
BlazeX
Date:
Thu Dec 11 20:24:15 2014 +0000
Revision:
37:31db07ebcb68
Parent:
36:a2ceec937db7
Child:
38:a04506a9a55b
Tried to fix the BufferResize issue, but still not working with buffers in use.

Who changed what in which revision?

UserRevisionLine numberNew 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)
AjK 0:eb2522b41db8 30 {
AjK 0:eb2522b41db8 31 int rval = Ok;
AjK 0:eb2522b41db8 32
AjK 0:eb2522b41db8 33 // If the requested size is the same as the current size there's nothing to do,
AjK 0:eb2522b41db8 34 // just continue to use the same buffer as it's fine as it is.
AjK 0:eb2522b41db8 35 if (buffer_size[type] == size) return rval;
AjK 0:eb2522b41db8 36
AjK 0:eb2522b41db8 37 // Make sure the ISR cannot use the buffers while we are manipulating them.
Sissors 27:9c93ce7cb9d8 38 NVIC_DisableIRQ(_IRQ);
AjK 0:eb2522b41db8 39
AjK 0:eb2522b41db8 40 // If the requested buffer size is larger than the current size,
AjK 0:eb2522b41db8 41 // attempt to create a new buffer and use it.
AjK 0:eb2522b41db8 42 if (buffer_size[type] < size) {
AjK 0:eb2522b41db8 43 rval = upSizeBuffer(size, type, memory_check);
AjK 0:eb2522b41db8 44 }
AjK 0:eb2522b41db8 45 else if (buffer_size[type] > size) {
AjK 0:eb2522b41db8 46 rval = downSizeBuffer(size, type, memory_check);
AjK 0:eb2522b41db8 47 }
AjK 0:eb2522b41db8 48
AjK 0:eb2522b41db8 49 // Start the ISR system again with the new buffers.
Sissors 27:9c93ce7cb9d8 50 NVIC_EnableIRQ(_IRQ);
AjK 0:eb2522b41db8 51
AjK 0:eb2522b41db8 52 return rval;
AjK 0:eb2522b41db8 53 }
AjK 0:eb2522b41db8 54
AjK 0:eb2522b41db8 55 int
AjK 0:eb2522b41db8 56 MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
AjK 0:eb2522b41db8 57 {
BlazeX 37:31db07ebcb68 58 // is new buffer is big enough?
BlazeX 37:31db07ebcb68 59 if (size <= buffer_count[type])
AjK 0:eb2522b41db8 60 return BufferOversize;
BlazeX 37:31db07ebcb68 61
BlazeX 37:31db07ebcb68 62 // allocate new buffer
BlazeX 37:31db07ebcb68 63 char * newBuffer = (char*)malloc(size);
AjK 0:eb2522b41db8 64
BlazeX 37:31db07ebcb68 65 // allocation failed?
BlazeX 37:31db07ebcb68 66 if (newBuffer == (char*)NULL)
BlazeX 37:31db07ebcb68 67 {
BlazeX 37:31db07ebcb68 68 if (memory_check)
AjK 0:eb2522b41db8 69 error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
BlazeX 37:31db07ebcb68 70
AjK 0:eb2522b41db8 71 return NoMemory;
AjK 0:eb2522b41db8 72 }
AjK 0:eb2522b41db8 73
BlazeX 37:31db07ebcb68 74 // copy old buffer content to new one
BlazeX 37:31db07ebcb68 75 moveRingBuffer(newBuffer, type);
AjK 0:eb2522b41db8 76
BlazeX 37:31db07ebcb68 77 // free old buffer and reset ring buffer cursor
BlazeX 37:31db07ebcb68 78 free((char*)buffer[type]);
BlazeX 37:31db07ebcb68 79
BlazeX 37:31db07ebcb68 80 buffer[type] = newBuffer;
BlazeX 37:31db07ebcb68 81 buffer_size[type] = size;
BlazeX 37:31db07ebcb68 82 buffer_in[type] = buffer_count[type];
BlazeX 37:31db07ebcb68 83 buffer_out[type] = 0;
AjK 0:eb2522b41db8 84
BlazeX 37:31db07ebcb68 85 return Ok;
AjK 0:eb2522b41db8 86 }
AjK 0:eb2522b41db8 87
AjK 0:eb2522b41db8 88 int
AjK 0:eb2522b41db8 89 MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
AjK 0:eb2522b41db8 90 {
BlazeX 37:31db07ebcb68 91 // allocate new buffer
BlazeX 37:31db07ebcb68 92 char * newBuffer = (char*)malloc(size);
AjK 0:eb2522b41db8 93
BlazeX 37:31db07ebcb68 94 // allocation failed?
BlazeX 37:31db07ebcb68 95 if (newBuffer == (char*)NULL)
BlazeX 37:31db07ebcb68 96 {
BlazeX 37:31db07ebcb68 97 if (memory_check)
AjK 0:eb2522b41db8 98 error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
BlazeX 37:31db07ebcb68 99
AjK 0:eb2522b41db8 100 return NoMemory;
AjK 0:eb2522b41db8 101 }
AjK 0:eb2522b41db8 102
BlazeX 37:31db07ebcb68 103 // copy old buffer content to new one
BlazeX 37:31db07ebcb68 104 moveRingBuffer(newBuffer, type);
AjK 0:eb2522b41db8 105
BlazeX 37:31db07ebcb68 106 // free old buffer and reset ring buffer cursor
BlazeX 37:31db07ebcb68 107 free((char*)buffer[type]);
BlazeX 37:31db07ebcb68 108
BlazeX 37:31db07ebcb68 109 buffer[type] = newBuffer;
AjK 0:eb2522b41db8 110 buffer_size[type] = size;
BlazeX 37:31db07ebcb68 111 buffer_in[type] = buffer_count[type];
BlazeX 37:31db07ebcb68 112 buffer_out[type] = 0;
BlazeX 37:31db07ebcb68 113
AjK 0:eb2522b41db8 114 return Ok;
AjK 0:eb2522b41db8 115 }
AjK 0:eb2522b41db8 116
BlazeX 37:31db07ebcb68 117 void MODSERIAL::moveRingBuffer(char * newBuffer, IrqType type)
BlazeX 37:31db07ebcb68 118 {
BlazeX 37:31db07ebcb68 119 // copy old buffer content to new one
BlazeX 37:31db07ebcb68 120 if(buffer_in[type] > buffer_out[type])
BlazeX 37:31db07ebcb68 121 { // content in the middle of the ring buffer
BlazeX 37:31db07ebcb68 122 memcpy(&newBuffer[0], (char*)buffer[type], buffer_count[type]);
BlazeX 37:31db07ebcb68 123 }
BlazeX 37:31db07ebcb68 124 else if(buffer_in[type] < buffer_out[type])
BlazeX 37:31db07ebcb68 125 { // content split, free space in the middle
BlazeX 37:31db07ebcb68 126 // copy last part of the old buffer
BlazeX 37:31db07ebcb68 127 int end_count= buffer_size[type] - buffer_out[type];
BlazeX 37:31db07ebcb68 128 memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], end_count);
BlazeX 37:31db07ebcb68 129
BlazeX 37:31db07ebcb68 130 // copy first part of old buffer
BlazeX 37:31db07ebcb68 131 memcpy(&newBuffer[end_count], (char*)buffer[type], buffer_in[type]);
BlazeX 37:31db07ebcb68 132 }
BlazeX 37:31db07ebcb68 133 // else old buffer empty
BlazeX 37:31db07ebcb68 134 }
BlazeX 37:31db07ebcb68 135
AjK 0:eb2522b41db8 136 }; // namespace AjK ends