TEST

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Committer:
gmehmet
Date:
Wed Apr 10 14:56:25 2019 +0300
Revision:
1:f60eafbf009a
Child:
3:2fe2ff1ca0dc
upload from local

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gmehmet 1:f60eafbf009a 1 /*******************************************************************************
gmehmet 1:f60eafbf009a 2 * Author: Ismail Kose, Ismail.Kose@maximintegrated.com
gmehmet 1:f60eafbf009a 3 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
gmehmet 1:f60eafbf009a 4 *
gmehmet 1:f60eafbf009a 5 * Permission is hereby granted, free of charge, to any person obtaining a
gmehmet 1:f60eafbf009a 6 * copy of this software and associated documentation files (the "Software"),
gmehmet 1:f60eafbf009a 7 * to deal in the Software without restriction, including without limitation
gmehmet 1:f60eafbf009a 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
gmehmet 1:f60eafbf009a 9 * and/or sell copies of the Software, and to permit persons to whom the
gmehmet 1:f60eafbf009a 10 * Software is furnished to do so, subject to the following conditions:
gmehmet 1:f60eafbf009a 11 *
gmehmet 1:f60eafbf009a 12 * The above copyright notice and this permission notice shall be included
gmehmet 1:f60eafbf009a 13 * in all copies or substantial portions of the Software.
gmehmet 1:f60eafbf009a 14 *
gmehmet 1:f60eafbf009a 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
gmehmet 1:f60eafbf009a 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
gmehmet 1:f60eafbf009a 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
gmehmet 1:f60eafbf009a 18 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
gmehmet 1:f60eafbf009a 19 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
gmehmet 1:f60eafbf009a 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
gmehmet 1:f60eafbf009a 21 * OTHER DEALINGS IN THE SOFTWARE.
gmehmet 1:f60eafbf009a 22 *
gmehmet 1:f60eafbf009a 23 * Except as contained in this notice, the name of Maxim Integrated
gmehmet 1:f60eafbf009a 24 * Products, Inc. shall not be used except as stated in the Maxim Integrated
gmehmet 1:f60eafbf009a 25 * Products, Inc. Branding Policy.
gmehmet 1:f60eafbf009a 26 *
gmehmet 1:f60eafbf009a 27 * The mere transfer of this software does not imply any licenses
gmehmet 1:f60eafbf009a 28 * of trade secrets, proprietary technology, copyrights, patents,
gmehmet 1:f60eafbf009a 29 * trademarks, maskwork rights, or any other form of intellectual
gmehmet 1:f60eafbf009a 30 * property whatsoever. Maxim Integrated Products, Inc. retains all
gmehmet 1:f60eafbf009a 31 * ownership rights.
gmehmet 1:f60eafbf009a 32 *******************************************************************************
gmehmet 1:f60eafbf009a 33 */
gmehmet 1:f60eafbf009a 34
gmehmet 1:f60eafbf009a 35 #ifndef _QUEUE_H_
gmehmet 1:f60eafbf009a 36 #define _QUEUE_H_
gmehmet 1:f60eafbf009a 37 #include <stdio.h>
gmehmet 1:f60eafbf009a 38 #include <string.h>
gmehmet 1:f60eafbf009a 39 #include <stdint.h>
gmehmet 1:f60eafbf009a 40 #include <stdlib.h>
gmehmet 1:f60eafbf009a 41 #include <stdbool.h>
gmehmet 1:f60eafbf009a 42 #include <errno.h>
gmehmet 1:f60eafbf009a 43
gmehmet 1:f60eafbf009a 44 #ifndef UNIT_TEST
gmehmet 1:f60eafbf009a 45 #include "platform.h"
gmehmet 1:f60eafbf009a 46 #else
gmehmet 1:f60eafbf009a 47 #ifndef enter_critical_section
gmehmet 1:f60eafbf009a 48 #define enter_critical_section()
gmehmet 1:f60eafbf009a 49 #endif
gmehmet 1:f60eafbf009a 50
gmehmet 1:f60eafbf009a 51 #ifndef exit_critical_section
gmehmet 1:f60eafbf009a 52 #define exit_critical_section()
gmehmet 1:f60eafbf009a 53 #endif
gmehmet 1:f60eafbf009a 54
gmehmet 1:f60eafbf009a 55 #ifndef pr_info
gmehmet 1:f60eafbf009a 56 #define pr_info(...) printf(__VA_ARGS__)
gmehmet 1:f60eafbf009a 57 #endif
gmehmet 1:f60eafbf009a 58 #endif
gmehmet 1:f60eafbf009a 59
gmehmet 1:f60eafbf009a 60 #define QUEUE_USAGE_STATS
gmehmet 1:f60eafbf009a 61
gmehmet 1:f60eafbf009a 62 struct queue_t {
gmehmet 1:f60eafbf009a 63 char *wr; // write pointer
gmehmet 1:f60eafbf009a 64 char *rd; // read pointer
gmehmet 1:f60eafbf009a 65 char *base; // buffer base pointer
gmehmet 1:f60eafbf009a 66 int num_item; // number of data item
gmehmet 1:f60eafbf009a 67 int ovf_item; // Number of overflowed data
gmehmet 1:f60eafbf009a 68 int buffer_size; // buffer size in bytes
gmehmet 1:f60eafbf009a 69 int max_buffer_size; // buffer size in bytes
gmehmet 1:f60eafbf009a 70 int item_size; // data size
gmehmet 1:f60eafbf009a 71 char *name;
gmehmet 1:f60eafbf009a 72 #ifdef QUEUE_USAGE_STATS
gmehmet 1:f60eafbf009a 73 int pop_cnt;
gmehmet 1:f60eafbf009a 74 int push_cnt;
gmehmet 1:f60eafbf009a 75 int stats_period_cnt;
gmehmet 1:f60eafbf009a 76 #endif
gmehmet 1:f60eafbf009a 77 };
gmehmet 1:f60eafbf009a 78
gmehmet 1:f60eafbf009a 79 #ifdef __cplusplus
gmehmet 1:f60eafbf009a 80 extern "C" {
gmehmet 1:f60eafbf009a 81 #endif
gmehmet 1:f60eafbf009a 82
gmehmet 1:f60eafbf009a 83 /**
gmehmet 1:f60eafbf009a 84 * @brief Queue initialization.
gmehmet 1:f60eafbf009a 85 * @param[in] *q Points to the queue handle
gmehmet 1:f60eafbf009a 86 * @param[in] *buf Points to external queue buffer
gmehmet 1:f60eafbf009a 87 * @param[in] item_size Data size
gmehmet 1:f60eafbf009a 88 * @param[in] buffer_size Total buffer size in bytes
gmehmet 1:f60eafbf009a 89 * @param[out] *pDst points to output matrix structure
gmehmet 1:f60eafbf009a 90 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 91 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 92 * -2: Queue buffer is full, no more space
gmehmet 1:f60eafbf009a 93 **/
gmehmet 1:f60eafbf009a 94 int queue_init(struct queue_t *q, void *buf, int item_size, int buffer_size);
gmehmet 1:f60eafbf009a 95
gmehmet 1:f60eafbf009a 96
gmehmet 1:f60eafbf009a 97
gmehmet 1:f60eafbf009a 98 /**
gmehmet 1:f60eafbf009a 99 * @brief Queue initialization by name.
gmehmet 1:f60eafbf009a 100 * @param[in] *q Points to the queue handle
gmehmet 1:f60eafbf009a 101 * @param[in] *buf Points to external queue buffer
gmehmet 1:f60eafbf009a 102 * @param[in] item_size Data size
gmehmet 1:f60eafbf009a 103 * @param[in] buffer_size Total buffer size in bytes
gmehmet 1:f60eafbf009a 104 * @param[out] *pDst points to output matrix structure
gmehmet 1:f60eafbf009a 105 * @param[in] Set queue name for debugging
gmehmet 1:f60eafbf009a 106 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 107 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 108 * -2: Queue buffer is full, no more space
gmehmet 1:f60eafbf009a 109 **/
gmehmet 1:f60eafbf009a 110 int queue_init_by_name(struct queue_t *q, void *buf, int item_size, int buffer_size, const char *name);
gmehmet 1:f60eafbf009a 111
gmehmet 1:f60eafbf009a 112
gmehmet 1:f60eafbf009a 113
gmehmet 1:f60eafbf009a 114 /**
gmehmet 1:f60eafbf009a 115 * @brief Reset queue
gmehmet 1:f60eafbf009a 116 * @param[in] *q Points to the queue handle
gmehmet 1:f60eafbf009a 117 * @param[in] *data Points to any type of data to put FIFO
gmehmet 1:f60eafbf009a 118 * @param[out] *pDst Points to output matrix structure
gmehmet 1:f60eafbf009a 119 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 120 * -EINVAL (-22): Invalid Pointer
gmehmet 1:f60eafbf009a 121 *
gmehmet 1:f60eafbf009a 122 **/
gmehmet 1:f60eafbf009a 123 int queue_reset(struct queue_t *q);
gmehmet 1:f60eafbf009a 124
gmehmet 1:f60eafbf009a 125
gmehmet 1:f60eafbf009a 126
gmehmet 1:f60eafbf009a 127 /**
gmehmet 1:f60eafbf009a 128 * @brief Update item size and reset queue. Usign of this command requires exta cauion.
gmehmet 1:f60eafbf009a 129 * @param[in] *q Points to the queue handle
gmehmet 1:f60eafbf009a 130 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 131 * -EINVAL (-22): Invalid Pointer
gmehmet 1:f60eafbf009a 132 *
gmehmet 1:f60eafbf009a 133 **/
gmehmet 1:f60eafbf009a 134 int queue_update_items_size(struct queue_t *q, int item_size);
gmehmet 1:f60eafbf009a 135
gmehmet 1:f60eafbf009a 136
gmehmet 1:f60eafbf009a 137
gmehmet 1:f60eafbf009a 138 /**
gmehmet 1:f60eafbf009a 139 * @brief Data enqueue.
gmehmet 1:f60eafbf009a 140 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 141 * @param[in] *data points to any type of data to put FIFO
gmehmet 1:f60eafbf009a 142 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 143 * -EINVAL (-22): Invalid Pointer
gmehmet 1:f60eafbf009a 144 * -2: Queue buffer is full, no more space
gmehmet 1:f60eafbf009a 145 **/
gmehmet 1:f60eafbf009a 146 int enqueue(struct queue_t *q, void *data);
gmehmet 1:f60eafbf009a 147
gmehmet 1:f60eafbf009a 148
gmehmet 1:f60eafbf009a 149
gmehmet 1:f60eafbf009a 150 /**
gmehmet 1:f60eafbf009a 151 * @brief Data dequeue.
gmehmet 1:f60eafbf009a 152 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 153 * @param[in] *data points to any type of data to put FIFO
gmehmet 1:f60eafbf009a 154 * @param[out] *data pop data from Queue
gmehmet 1:f60eafbf009a 155 * @return The function returns 0: success
gmehmet 1:f60eafbf009a 156 * -EINVAL (-22): Invalid Pointer or data
gmehmet 1:f60eafbf009a 157 * -2: Queue buffer is empty
gmehmet 1:f60eafbf009a 158 **/
gmehmet 1:f60eafbf009a 159 int dequeue(struct queue_t *q, void *data);
gmehmet 1:f60eafbf009a 160
gmehmet 1:f60eafbf009a 161
gmehmet 1:f60eafbf009a 162
gmehmet 1:f60eafbf009a 163 /**
gmehmet 1:f60eafbf009a 164 * @brief Queue Destroy
gmehmet 1:f60eafbf009a 165 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 166 **/
gmehmet 1:f60eafbf009a 167 void queue_destroy(struct queue_t *q);
gmehmet 1:f60eafbf009a 168
gmehmet 1:f60eafbf009a 169
gmehmet 1:f60eafbf009a 170
gmehmet 1:f60eafbf009a 171 /**
gmehmet 1:f60eafbf009a 172 * @brief Number of elements in Queue
gmehmet 1:f60eafbf009a 173 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 174 * @return number of elements
gmehmet 1:f60eafbf009a 175 **/
gmehmet 1:f60eafbf009a 176 int queue_len(struct queue_t *q);
gmehmet 1:f60eafbf009a 177
gmehmet 1:f60eafbf009a 178
gmehmet 1:f60eafbf009a 179
gmehmet 1:f60eafbf009a 180 /**
gmehmet 1:f60eafbf009a 181 * @brief Copies an item from the front of queue to data, but does not remove it from queue
gmehmet 1:f60eafbf009a 182 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 183 * @param[out] Copy of item from front of the queue
gmehmet 1:f60eafbf009a 184 * @return if value is greater than 0, return value is number of elements.
gmehmet 1:f60eafbf009a 185 If value is less than 0, returns -EINVAL (-22)
gmehmet 1:f60eafbf009a 186 **/
gmehmet 1:f60eafbf009a 187 int queue_front(struct queue_t *q, void *data);
gmehmet 1:f60eafbf009a 188
gmehmet 1:f60eafbf009a 189
gmehmet 1:f60eafbf009a 190
gmehmet 1:f60eafbf009a 191 /**
gmehmet 1:f60eafbf009a 192 * @brief Copies n items from the front of the queue, but does not remove them
gmehmet 1:f60eafbf009a 193 * @param[in] *q - points to the queue handle
gmehmet 1:f60eafbf009a 194 * @param[out] *data - The buffer to hold copied data
gmehmet 1:f60eafbf009a 195 * @param[in] n - the number of items to remove
gmehmet 1:f60eafbf009a 196 * @param[in] buf_sz - input *data buffer size
gmehmet 1:f60eafbf009a 197 * @return 0: success
gmehmet 1:f60eafbf009a 198 * -EINVAL (-22): Invalid pointer
gmehmet 1:f60eafbf009a 199 * -2: Queue contains less than n items
gmehmet 1:f60eafbf009a 200 */
gmehmet 1:f60eafbf009a 201 int queue_front_n(struct queue_t *q, void *data, int n, int buf_sz);
gmehmet 1:f60eafbf009a 202
gmehmet 1:f60eafbf009a 203
gmehmet 1:f60eafbf009a 204
gmehmet 1:f60eafbf009a 205 /**
gmehmet 1:f60eafbf009a 206 * @brief Removes an item from front of queue
gmehmet 1:f60eafbf009a 207 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 208 * @return status, success or fail
gmehmet 1:f60eafbf009a 209 **/
gmehmet 1:f60eafbf009a 210 int queue_pop(struct queue_t *q);
gmehmet 1:f60eafbf009a 211
gmehmet 1:f60eafbf009a 212 /**
gmehmet 1:f60eafbf009a 213 * @brief Removes n items from the front of the queue
gmehmet 1:f60eafbf009a 214 * @param[in] *q - points to the queue handle
gmehmet 1:f60eafbf009a 215 * @param[in] n - the number of items to remove
gmehmet 1:f60eafbf009a 216 * @return 0: success
gmehmet 1:f60eafbf009a 217 * -EINVAL (-22): Invalid pointer
gmehmet 1:f60eafbf009a 218 * -2: Queue contains less than n items
gmehmet 1:f60eafbf009a 219 */
gmehmet 1:f60eafbf009a 220 int queue_pop_n(struct queue_t *q, int n);
gmehmet 1:f60eafbf009a 221
gmehmet 1:f60eafbf009a 222
gmehmet 1:f60eafbf009a 223
gmehmet 1:f60eafbf009a 224 /**
gmehmet 1:f60eafbf009a 225 * @brief Checks if queue is fill
gmehmet 1:f60eafbf009a 226 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 227 * @return true (full), false (not full)
gmehmet 1:f60eafbf009a 228 *
gmehmet 1:f60eafbf009a 229 **/
gmehmet 1:f60eafbf009a 230 bool queue_is_full(struct queue_t *q);
gmehmet 1:f60eafbf009a 231
gmehmet 1:f60eafbf009a 232
gmehmet 1:f60eafbf009a 233
gmehmet 1:f60eafbf009a 234 /**
gmehmet 1:f60eafbf009a 235 * @brief returns fifo usage info
gmehmet 1:f60eafbf009a 236 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 237 * @param[out] *total returns total FIFO size in number of elements
gmehmet 1:f60eafbf009a 238 * @param[out] *nm_item returns number of elements in FIFO
gmehmet 1:f60eafbf009a 239 * @return status, success or fail
gmehmet 1:f60eafbf009a 240 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 241 **/
gmehmet 1:f60eafbf009a 242 int queue_usage(struct queue_t *q, int *total, int *nm_item);
gmehmet 1:f60eafbf009a 243
gmehmet 1:f60eafbf009a 244
gmehmet 1:f60eafbf009a 245
gmehmet 1:f60eafbf009a 246 /**
gmehmet 1:f60eafbf009a 247 * @brief Pops out delimiter terminated string
gmehmet 1:f60eafbf009a 248 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 249 * @param[out] *buf output char array to write
gmehmet 1:f60eafbf009a 250 * @param[in] buffer_size Maximum buffer size to write the output char array
gmehmet 1:f60eafbf009a 251 * @return status, string length if positive or fail if negative
gmehmet 1:f60eafbf009a 252 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 253 **/
gmehmet 1:f60eafbf009a 254 int dequeue_string(struct queue_t *q, char *buf, int buffer_size);
gmehmet 1:f60eafbf009a 255
gmehmet 1:f60eafbf009a 256
gmehmet 1:f60eafbf009a 257
gmehmet 1:f60eafbf009a 258 /**
gmehmet 1:f60eafbf009a 259 * @brief Pushes null terminated string (char array)
gmehmet 1:f60eafbf009a 260 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 261 * @param[in] *data string(char array) to add it to the circullar buffer
gmehmet 1:f60eafbf009a 262 * @param[in] sz 'data' length
gmehmet 1:f60eafbf009a 263 * @return status, success or fail
gmehmet 1:f60eafbf009a 264 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 265 **/
gmehmet 1:f60eafbf009a 266 int enqueue_string(struct queue_t *q, char *data, int sz);
gmehmet 1:f60eafbf009a 267
gmehmet 1:f60eafbf009a 268
gmehmet 1:f60eafbf009a 269 /**
gmehmet 1:f60eafbf009a 270 * @brief Counts length of string in queue
gmehmet 1:f60eafbf009a 271 * @param[in] *q points to the queue handle
gmehmet 1:f60eafbf009a 272 * @return status, success or fail
gmehmet 1:f60eafbf009a 273 * -EINVAL (-22): Invalid Pointer, data or parameters
gmehmet 1:f60eafbf009a 274 * if ret >= 0, string length
gmehmet 1:f60eafbf009a 275 **/
gmehmet 1:f60eafbf009a 276 int queue_str_len(struct queue_t *q);
gmehmet 1:f60eafbf009a 277
gmehmet 1:f60eafbf009a 278
gmehmet 1:f60eafbf009a 279
gmehmet 1:f60eafbf009a 280 void queue_n_test(void);
gmehmet 1:f60eafbf009a 281
gmehmet 1:f60eafbf009a 282 int enqueue_test(struct queue_t *q, void *data);
gmehmet 1:f60eafbf009a 283 int dequeue_test(struct queue_t *q, void *data);
gmehmet 1:f60eafbf009a 284
gmehmet 1:f60eafbf009a 285 #ifdef __cplusplus
gmehmet 1:f60eafbf009a 286 }
gmehmet 1:f60eafbf009a 287 #endif
gmehmet 1:f60eafbf009a 288 #endif //_QUEUE_H_