MODSERIAL with support for more devices

Dependents:   1D-Pong BMT-K9_encoder BMT-K9-Regelaar programma_filter ... more

Check the cookbook page for more information: https://mbed.org/cookbook/MODSERIAL

Did you add a device? Please send a pull request so we can keep everything in one library instead of many copies. In that case also send a PM, since currently mbed does not inform of new pull requests. I will then also add you to the developers of this library so you can do other changes directly.

Committer:
riaancillie
Date:
Sun May 05 14:57:11 2019 +0000
Revision:
46:d2a5e26fd658
Parent:
38:a04506a9a55b
Added support for target NUCLEO_F103RB

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)
BlazeX 38:a04506a9a55b 30 {
AjK 0:eb2522b41db8 31 // Make sure the ISR cannot use the buffers while we are manipulating them.
Sissors 27:9c93ce7cb9d8 32 NVIC_DisableIRQ(_IRQ);
AjK 0:eb2522b41db8 33
BlazeX 38:a04506a9a55b 34 // If the requested size is the same as the current size there's nothing to do,
BlazeX 38:a04506a9a55b 35 // just continue to use the same buffer as it's fine as it is.
BlazeX 38:a04506a9a55b 36 if (buffer_size[type] == size)
BlazeX 38:a04506a9a55b 37 {
BlazeX 38:a04506a9a55b 38 NVIC_EnableIRQ(_IRQ);
BlazeX 38:a04506a9a55b 39 return Ok;
AjK 0:eb2522b41db8 40 }
AjK 0:eb2522b41db8 41
BlazeX 37:31db07ebcb68 42 // is new buffer is big enough?
BlazeX 37:31db07ebcb68 43 if (size <= buffer_count[type])
BlazeX 38:a04506a9a55b 44 {
BlazeX 38:a04506a9a55b 45 NVIC_EnableIRQ(_IRQ);
AjK 0:eb2522b41db8 46 return BufferOversize;
BlazeX 38:a04506a9a55b 47 }
BlazeX 37:31db07ebcb68 48
BlazeX 37:31db07ebcb68 49 // allocate new buffer
BlazeX 37:31db07ebcb68 50 char * newBuffer = (char*)malloc(size);
AjK 0:eb2522b41db8 51
BlazeX 37:31db07ebcb68 52 // allocation failed?
BlazeX 37:31db07ebcb68 53 if (newBuffer == (char*)NULL)
BlazeX 37:31db07ebcb68 54 {
BlazeX 37:31db07ebcb68 55 if (memory_check)
AjK 0:eb2522b41db8 56 error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
BlazeX 37:31db07ebcb68 57
AjK 0:eb2522b41db8 58 return NoMemory;
AjK 0:eb2522b41db8 59 }
AjK 0:eb2522b41db8 60
BlazeX 37:31db07ebcb68 61 // copy old buffer content to new one
BlazeX 37:31db07ebcb68 62 moveRingBuffer(newBuffer, type);
AjK 0:eb2522b41db8 63
BlazeX 37:31db07ebcb68 64 // free old buffer and reset ring buffer cursor
BlazeX 37:31db07ebcb68 65 free((char*)buffer[type]);
BlazeX 37:31db07ebcb68 66
BlazeX 37:31db07ebcb68 67 buffer[type] = newBuffer;
BlazeX 37:31db07ebcb68 68 buffer_size[type] = size;
BlazeX 37:31db07ebcb68 69 buffer_in[type] = buffer_count[type];
BlazeX 38:a04506a9a55b 70 buffer_out[type] = 0;
AjK 0:eb2522b41db8 71
BlazeX 38:a04506a9a55b 72 // Start the ISR system again with the new buffers.
BlazeX 38:a04506a9a55b 73 NVIC_EnableIRQ(_IRQ);
AjK 0:eb2522b41db8 74 return Ok;
AjK 0:eb2522b41db8 75 }
AjK 0:eb2522b41db8 76
BlazeX 37:31db07ebcb68 77 void MODSERIAL::moveRingBuffer(char * newBuffer, IrqType type)
BlazeX 38:a04506a9a55b 78 {
BlazeX 37:31db07ebcb68 79 // copy old buffer content to new one
BlazeX 37:31db07ebcb68 80 if(buffer_in[type] > buffer_out[type])
BlazeX 37:31db07ebcb68 81 { // content in the middle of the ring buffer
BlazeX 38:a04506a9a55b 82 memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], buffer_count[type]);
BlazeX 37:31db07ebcb68 83 }
BlazeX 37:31db07ebcb68 84 else if(buffer_in[type] < buffer_out[type])
BlazeX 37:31db07ebcb68 85 { // content split, free space in the middle
BlazeX 37:31db07ebcb68 86 // copy last part of the old buffer
BlazeX 37:31db07ebcb68 87 int end_count= buffer_size[type] - buffer_out[type];
BlazeX 37:31db07ebcb68 88 memcpy(&newBuffer[0], (char*)&buffer[type][buffer_out[type]], end_count);
BlazeX 37:31db07ebcb68 89
BlazeX 37:31db07ebcb68 90 // copy first part of old buffer
BlazeX 37:31db07ebcb68 91 memcpy(&newBuffer[end_count], (char*)buffer[type], buffer_in[type]);
BlazeX 37:31db07ebcb68 92 }
BlazeX 37:31db07ebcb68 93 // else old buffer empty
BlazeX 37:31db07ebcb68 94 }
BlazeX 37:31db07ebcb68 95
AjK 0:eb2522b41db8 96 }; // namespace AjK ends