Firmware enhancements for HSP_RPC_GUI 3.0.1

Dependencies:   USBDevice

Fork of HSP_RPC_GUI by Maxim Integrated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RpcFifo.cpp Source File

RpcFifo.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 #include "mbed.h"
00034 #include "RpcFifo.h"
00035 
00036 /****************************************************************************/
00037 void fifo_init(fifo_t *fifo, void *mem, unsigned int length) {
00038   // atomic FIFO access
00039   __disable_irq();
00040 
00041   fifo->rindex = 0;
00042   fifo->windex = 0;
00043   fifo->data = mem;
00044   fifo->length = length;
00045 
00046   __enable_irq();
00047 }
00048 
00049 /****************************************************************************/
00050 int fifo_put8(fifo_t *fifo, uint8_t element) {
00051   // Check if FIFO is full
00052   if ((fifo->windex == (fifo->rindex - 1)) ||
00053       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) {
00054     return -1;
00055   }
00056 
00057   // atomic FIFO access
00058   __disable_irq();
00059 
00060   // Put data into FIFO
00061   ((uint8_t *)(fifo->data))[fifo->windex] = element;
00062 
00063   // Increment pointer
00064   fifo->windex++;
00065   if (fifo->windex == fifo->length) {
00066     fifo->windex = 0;
00067   }
00068 
00069   __enable_irq();
00070 
00071   return 0;
00072 }
00073 
00074 /****************************************************************************/
00075 int fifo_get8(fifo_t *fifo, uint8_t *element) {
00076   // Check if FIFO is empty
00077   if (fifo->rindex == fifo->windex)
00078     return -1;
00079 
00080   // atomic FIFO access
00081   __disable_irq();
00082 
00083   // Get data from FIFO
00084   *element = ((uint8_t *)(fifo->data))[fifo->rindex];
00085 
00086   // Increment pointer
00087   fifo->rindex++;
00088   if (fifo->rindex == fifo->length) {
00089     fifo->rindex = 0;
00090   }
00091 
00092   __enable_irq();
00093 
00094   return 0;
00095 }
00096 
00097 /****************************************************************************/
00098 int fifo_put16(fifo_t *fifo, uint16_t element) {
00099   // Check if FIFO is full
00100   if ((fifo->windex == (fifo->rindex - 1)) ||
00101       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) {
00102     return -1;
00103   }
00104 
00105   // atomic FIFO access
00106   __disable_irq();
00107 
00108   // Put data into FIFO
00109   ((uint16_t *)(fifo->data))[fifo->windex] = element;
00110 
00111   // Increment pointer
00112   fifo->windex++;
00113   if (fifo->windex == fifo->length) {
00114     fifo->windex = 0;
00115   }
00116 
00117   __enable_irq();
00118 
00119   return 0;
00120 }
00121 
00122 /****************************************************************************/
00123 int fifo_get16(fifo_t *fifo, uint16_t *element) {
00124   // Check if FIFO is empty
00125   if (fifo->rindex == fifo->windex)
00126     return -1;
00127 
00128   // atomic FIFO access
00129   __disable_irq();
00130 
00131   // Get data from FIFO
00132   *element = ((uint16_t *)(fifo->data))[fifo->rindex];
00133 
00134   // Increment pointer
00135   fifo->rindex++;
00136   if (fifo->rindex == fifo->length) {
00137     fifo->rindex = 0;
00138   }
00139 
00140   __enable_irq();
00141 
00142   return 0;
00143 }
00144 
00145 /****************************************************************************/
00146 int fifo_put32(fifo_t *fifo, uint32_t element) {
00147   // Check if FIFO is full
00148   if ((fifo->windex == (fifo->rindex - 1)) ||
00149       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) {
00150     return -1;
00151   }
00152 
00153   // atomic FIFO access
00154   __disable_irq();
00155 
00156   // Put data into FIFO
00157   ((uint32_t *)(fifo->data))[fifo->windex] = element;
00158 
00159   // Increment pointer
00160   fifo->windex++;
00161   if (fifo->windex == fifo->length) {
00162     fifo->windex = 0;
00163   }
00164 
00165   __enable_irq();
00166 
00167   return 0;
00168 }
00169 
00170 /****************************************************************************/
00171 int fifo_get32(fifo_t *fifo, uint32_t *element) {
00172   // Check if FIFO is empty
00173   if (fifo->rindex == fifo->windex)
00174     return -1;
00175 
00176   // atomic FIFO access
00177   __disable_irq();
00178 
00179   // Get data from FIFO
00180   *element = ((uint32_t *)(fifo->data))[fifo->rindex];
00181 
00182   // Increment pointer
00183   fifo->rindex++;
00184   if (fifo->rindex == fifo->length) {
00185     fifo->rindex = 0;
00186   }
00187 
00188   __enable_irq();
00189 
00190   return 0;
00191 }
00192 /****************************************************************************/
00193 void fifo_clear(fifo_t *fifo) {
00194   // atomic FIFO access
00195   __disable_irq();
00196 
00197   fifo->rindex = 0;
00198   fifo->windex = 0;
00199 
00200   __enable_irq();
00201 }
00202 
00203 /****************************************************************************/
00204 int fifo_empty(fifo_t *fifo) { return (fifo->rindex == fifo->windex); }
00205 
00206 /****************************************************************************/
00207 int fifo_full(fifo_t *fifo) {
00208   int retval;
00209 
00210   // atomic FIFO access
00211   __disable_irq();
00212   retval = ((fifo->windex == (fifo->rindex - 1)) ||
00213             ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1))));
00214   __enable_irq();
00215 
00216   return retval;
00217 }
00218 
00219 /****************************************************************************/
00220 unsigned int fifo_level(fifo_t *fifo) {
00221   uint16_t value;
00222 
00223   // atomic FIFO access
00224   __disable_irq();
00225 
00226   if (fifo->windex >= fifo->rindex) {
00227     value = fifo->windex - fifo->rindex;
00228   } else {
00229     value = fifo->length - fifo->rindex + fifo->windex;
00230   }
00231 
00232   __enable_irq();
00233 
00234   return value;
00235 }
00236 
00237 /****************************************************************************/
00238 unsigned int fifo_remaining(fifo_t *fifo) {
00239   uint16_t value;
00240 
00241   // atomic FIFO access
00242   __disable_irq();
00243 
00244   if (fifo->rindex > fifo->windex) {
00245     value = fifo->rindex - fifo->windex - 1;
00246   } else {
00247     value = fifo->length - fifo->windex + fifo->rindex - 1;
00248   }
00249 
00250   __enable_irq();
00251 
00252   return value;
00253 }