Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Fri Nov 14 06:05:59 2014 +0000
Revision:
31:7eaa5e881b56
Mostly working.  Wrote CAN in/out code.  Xbee still needs work.  My guess is that MODDMA breaks when used with RTOS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pspatel321 31:7eaa5e881b56 1 /*
pspatel321 31:7eaa5e881b56 2 Copyright (c) 2010 Andy Kirkham
pspatel321 31:7eaa5e881b56 3
pspatel321 31:7eaa5e881b56 4 Permission is hereby granted, free of charge, to any person obtaining a copy
pspatel321 31:7eaa5e881b56 5 of this software and associated documentation files (the "Software"), to deal
pspatel321 31:7eaa5e881b56 6 in the Software without restriction, including without limitation the rights
pspatel321 31:7eaa5e881b56 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pspatel321 31:7eaa5e881b56 8 copies of the Software, and to permit persons to whom the Software is
pspatel321 31:7eaa5e881b56 9 furnished to do so, subject to the following conditions:
pspatel321 31:7eaa5e881b56 10
pspatel321 31:7eaa5e881b56 11 The above copyright notice and this permission notice shall be included in
pspatel321 31:7eaa5e881b56 12 all copies or substantial portions of the Software.
pspatel321 31:7eaa5e881b56 13
pspatel321 31:7eaa5e881b56 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pspatel321 31:7eaa5e881b56 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pspatel321 31:7eaa5e881b56 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pspatel321 31:7eaa5e881b56 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pspatel321 31:7eaa5e881b56 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pspatel321 31:7eaa5e881b56 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pspatel321 31:7eaa5e881b56 20 THE SOFTWARE.
pspatel321 31:7eaa5e881b56 21 */
pspatel321 31:7eaa5e881b56 22
pspatel321 31:7eaa5e881b56 23 #include "MODSERIAL.h"
pspatel321 31:7eaa5e881b56 24 #include "MACROS.h"
pspatel321 31:7eaa5e881b56 25
pspatel321 31:7eaa5e881b56 26 namespace AjK {
pspatel321 31:7eaa5e881b56 27
pspatel321 31:7eaa5e881b56 28 int
pspatel321 31:7eaa5e881b56 29 MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check)
pspatel321 31:7eaa5e881b56 30 {
pspatel321 31:7eaa5e881b56 31 int rval = Ok;
pspatel321 31:7eaa5e881b56 32
pspatel321 31:7eaa5e881b56 33 // If the requested size is the same as the current size there's nothing to do,
pspatel321 31:7eaa5e881b56 34 // just continue to use the same buffer as it's fine as it is.
pspatel321 31:7eaa5e881b56 35 if (buffer_size[type] == size) return rval;
pspatel321 31:7eaa5e881b56 36
pspatel321 31:7eaa5e881b56 37 // Make sure the ISR cannot use the buffers while we are manipulating them.
pspatel321 31:7eaa5e881b56 38 disableIrq();
pspatel321 31:7eaa5e881b56 39
pspatel321 31:7eaa5e881b56 40 // If the requested buffer size is larger than the current size,
pspatel321 31:7eaa5e881b56 41 // attempt to create a new buffer and use it.
pspatel321 31:7eaa5e881b56 42 if (buffer_size[type] < size) {
pspatel321 31:7eaa5e881b56 43 rval = upSizeBuffer(size, type, memory_check);
pspatel321 31:7eaa5e881b56 44 }
pspatel321 31:7eaa5e881b56 45 else if (buffer_size[type] > size) {
pspatel321 31:7eaa5e881b56 46 rval = downSizeBuffer(size, type, memory_check);
pspatel321 31:7eaa5e881b56 47 }
pspatel321 31:7eaa5e881b56 48
pspatel321 31:7eaa5e881b56 49 // Start the ISR system again with the new buffers.
pspatel321 31:7eaa5e881b56 50 enableIrq();
pspatel321 31:7eaa5e881b56 51
pspatel321 31:7eaa5e881b56 52 return rval;
pspatel321 31:7eaa5e881b56 53 }
pspatel321 31:7eaa5e881b56 54
pspatel321 31:7eaa5e881b56 55 int
pspatel321 31:7eaa5e881b56 56 MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
pspatel321 31:7eaa5e881b56 57 {
pspatel321 31:7eaa5e881b56 58 if (size >= buffer_count[type]) {
pspatel321 31:7eaa5e881b56 59 return BufferOversize;
pspatel321 31:7eaa5e881b56 60 }
pspatel321 31:7eaa5e881b56 61
pspatel321 31:7eaa5e881b56 62 char *s = (char *)malloc(size);
pspatel321 31:7eaa5e881b56 63
pspatel321 31:7eaa5e881b56 64 if (s == (char *)NULL) {
pspatel321 31:7eaa5e881b56 65 if (memory_check) {
pspatel321 31:7eaa5e881b56 66 error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
pspatel321 31:7eaa5e881b56 67 }
pspatel321 31:7eaa5e881b56 68 return NoMemory;
pspatel321 31:7eaa5e881b56 69 }
pspatel321 31:7eaa5e881b56 70
pspatel321 31:7eaa5e881b56 71 int c, new_in = 0;
pspatel321 31:7eaa5e881b56 72
pspatel321 31:7eaa5e881b56 73 do {
pspatel321 31:7eaa5e881b56 74 c = __getc(false);
pspatel321 31:7eaa5e881b56 75 if (c != -1) s[new_in++] = (char)c;
pspatel321 31:7eaa5e881b56 76 if (new_in >= size) new_in = 0;
pspatel321 31:7eaa5e881b56 77 }
pspatel321 31:7eaa5e881b56 78 while (c != -1);
pspatel321 31:7eaa5e881b56 79
pspatel321 31:7eaa5e881b56 80 free((char *)buffer[type]);
pspatel321 31:7eaa5e881b56 81 buffer[type] = s;
pspatel321 31:7eaa5e881b56 82 buffer_in[type] = new_in;
pspatel321 31:7eaa5e881b56 83 buffer_out[type] = 0;
pspatel321 31:7eaa5e881b56 84 return Ok;
pspatel321 31:7eaa5e881b56 85 }
pspatel321 31:7eaa5e881b56 86
pspatel321 31:7eaa5e881b56 87 int
pspatel321 31:7eaa5e881b56 88 MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
pspatel321 31:7eaa5e881b56 89 {
pspatel321 31:7eaa5e881b56 90 char *s = (char *)malloc(size);
pspatel321 31:7eaa5e881b56 91
pspatel321 31:7eaa5e881b56 92 if (s == (char *)NULL) {
pspatel321 31:7eaa5e881b56 93 if (memory_check) {
pspatel321 31:7eaa5e881b56 94 error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
pspatel321 31:7eaa5e881b56 95 }
pspatel321 31:7eaa5e881b56 96 return NoMemory;
pspatel321 31:7eaa5e881b56 97 }
pspatel321 31:7eaa5e881b56 98
pspatel321 31:7eaa5e881b56 99 if (buffer_count[type] == 0) { // Current buffer empty?
pspatel321 31:7eaa5e881b56 100 free((char *)buffer[type]);
pspatel321 31:7eaa5e881b56 101 buffer[type] = s;
pspatel321 31:7eaa5e881b56 102 buffer_in[type] = 0;
pspatel321 31:7eaa5e881b56 103 buffer_out[type] = 0;
pspatel321 31:7eaa5e881b56 104 }
pspatel321 31:7eaa5e881b56 105 else { // Copy the current contents into the new buffer.
pspatel321 31:7eaa5e881b56 106 int c, new_in = 0;
pspatel321 31:7eaa5e881b56 107 do {
pspatel321 31:7eaa5e881b56 108 c = __getc(false);
pspatel321 31:7eaa5e881b56 109 if (c != -1) s[new_in++] = (char)c;
pspatel321 31:7eaa5e881b56 110 if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure.
pspatel321 31:7eaa5e881b56 111 }
pspatel321 31:7eaa5e881b56 112 while (c != -1);
pspatel321 31:7eaa5e881b56 113 free((char *)buffer[type]);
pspatel321 31:7eaa5e881b56 114 buffer[type] = s;
pspatel321 31:7eaa5e881b56 115 buffer_in[type] = new_in;
pspatel321 31:7eaa5e881b56 116 buffer_out[type] = 0;
pspatel321 31:7eaa5e881b56 117 }
pspatel321 31:7eaa5e881b56 118
pspatel321 31:7eaa5e881b56 119 buffer_size[type] = size;
pspatel321 31:7eaa5e881b56 120 return Ok;
pspatel321 31:7eaa5e881b56 121 }
pspatel321 31:7eaa5e881b56 122
pspatel321 31:7eaa5e881b56 123 }; // namespace AjK ends