1 /* 2 * Arm SCP/MCP Software 3 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #include <stdbool.h> 9 #include <stdint.h> 10 11 #include <cli_config.h> 12 #include <cli_fifo.h> 13 fifo_init(fifo_st * fifo,char * buf,uint32_t buf_size)14uint32_t fifo_init(fifo_st *fifo, char *buf, uint32_t buf_size) 15 { 16 if (fifo == 0 || buf == 0 || buf_size == 0) 17 return FWK_E_PARAM; 18 19 fifo->get_ptr = 0; 20 fifo->put_ptr = 0; 21 fifo->count = 0; 22 fifo->high_water = 0; 23 fifo->reset_high_water = false; 24 fifo->buf_size = buf_size; 25 fifo->buf = buf; 26 return FWK_SUCCESS; 27 } 28 fifo_get(fifo_st * fifo,char * val)29uint32_t fifo_get(fifo_st *fifo, char *val) 30 { 31 /* Checking parameters. */ 32 if (fifo == 0 || val == 0) 33 return FWK_E_PARAM; 34 35 /* Making sure FIFO isn't empty. */ 36 if (fifo->get_ptr != fifo->put_ptr) { 37 *val = fifo->buf[fifo->get_ptr]; 38 fifo->get_ptr = (fifo->get_ptr + 1) % fifo->buf_size; 39 fifo->count = fifo->count - 1; 40 /* Tracking FIFO high-water mark. */ 41 if ((fifo->reset_high_water == true) && (fifo->count == 0)) { 42 fifo->high_water = 0; 43 fifo->reset_high_water = false; 44 } 45 return FWK_SUCCESS; 46 } 47 48 return FWK_E_DATA; 49 } 50 fifo_put(fifo_st * fifo,char * val)51uint32_t fifo_put(fifo_st *fifo, char *val) 52 { 53 /* Checking parameters. */ 54 if (fifo == 0 || val == 0) 55 return FWK_E_PARAM; 56 57 uint32_t newPutPtr = (fifo->put_ptr + 1) % fifo->buf_size; 58 59 /* Making sure FIFO isn't full. */ 60 if (newPutPtr != fifo->get_ptr) { 61 fifo->buf[fifo->put_ptr] = *val; 62 fifo->put_ptr = newPutPtr; 63 fifo->count = fifo->count + 1; 64 /* Tracking FIFO high-water mark. */ 65 if (fifo->count > fifo->high_water && fifo->reset_high_water == false) 66 fifo->high_water = fifo->count; 67 68 return FWK_SUCCESS; 69 } 70 71 return FWK_E_NOMEM; 72 } 73 fifo_free_space(fifo_st * fifo)74uint32_t fifo_free_space(fifo_st *fifo) 75 { 76 /* Checking parameters. */ 77 if (fifo == 0) 78 return FWK_E_PARAM; 79 80 return fifo->buf_size - fifo->count - 1; 81 } 82 fifo_count(fifo_st * fifo)83uint32_t fifo_count(fifo_st *fifo) 84 { 85 /* Checking parameters. */ 86 if (fifo == 0) 87 return FWK_E_PARAM; 88 89 return fifo->count; 90 } 91 fifo_capacity(fifo_st * fifo)92uint32_t fifo_capacity(fifo_st *fifo) 93 { 94 /* Checking parameters. */ 95 if (fifo == 0) 96 return FWK_E_PARAM; 97 98 return fifo->buf_size - 1; 99 } 100 fifo_high_water(fifo_st * fifo)101uint32_t fifo_high_water(fifo_st *fifo) 102 { 103 /* Checking parameters. */ 104 if (fifo == 0) 105 return FWK_E_PARAM; 106 107 return fifo->high_water; 108 } 109 fifo_high_water_reset(fifo_st * fifo)110uint32_t fifo_high_water_reset(fifo_st *fifo) 111 { 112 /* Checking parameters. */ 113 if (fifo == 0) 114 return FWK_E_PARAM; 115 116 fifo->reset_high_water = true; 117 return FWK_SUCCESS; 118 } 119 fifo_empty(fifo_st * fifo)120uint32_t fifo_empty(fifo_st *fifo) 121 { 122 /* Check for valid pointer. */ 123 if (fifo == 0) 124 return FWK_E_PARAM; 125 126 fifo->get_ptr = 0; 127 fifo->put_ptr = 0; 128 fifo->count = 0; 129 fifo->high_water = 0; 130 fifo->reset_high_water = false; 131 132 return FWK_SUCCESS; 133 } 134