• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #pragma once
19 
20 #include "common/compiler.h"
21 #include "common/types.h"
22 
23 /** @addtogroup  TELINK_COMMON_MODULE TELINK Common Module
24  *  @{
25  */
26 
27 /** @addtogroup  EV_BUFFER EV Buffer
28  *  @{
29  */
30 
31 /** @defgroup EV_BUFFER_CONSTANT EV Buffer Constants
32  *  @{
33  */
34 
35 /** @addtogroup ev_buffer_groups EV Buffer Groups
36  * Definition the length of each buffer group
37  * @{
38  */
39 #define BUFFER_GROUP_0  24
40 #define BUFFER_GROUP_1  60
41 #define BUFFER_GROUP_2  150
42 #define MAX_BUFFER_SIZE BUFFER_GROUP_2
43 
44 /** @} end of group ev_buffer_groups */
45 
46 /**
47  * @brief Default buffer number in each group
48  */
49 #define BUFFER_NUM_IN_GROUP0 8
50 #define BUFFER_NUM_IN_GROUP1 20
51 #define BUFFER_NUM_IN_GROUP2 5
52 
53 /** @addtogroup ev_buffer_typical_size EV Buffer Typical Application Size
54  * Definition default buffer size for different typical usage
55  * @{
56  */
57 #define SMALL_BUFFER 48
58 #define LARGE_BUFFER 142
59 
60 #define EV_BUFFER_DEBUG 0
61 
62 /** @} end of group ev_buffer_typical_size */
63 
64 /** @} end of group EV_BUFFER_CONSTANT */
65 
66 /** @defgroup EV_BUFFER_TYPE EV Buffer Types
67  *  @{
68  */
69 
70 /**
71  *  @brief Definition of a buffer item, it is internal used.
72  */
73 typedef struct ev_bufItem {
74     struct ev_bufItem *next;
75 #if EV_BUFFER_DEBUG
76     u8 groupIndex;
77     u8 flag;
78     u16 line;
79 #else
80     u32 groupIndex;
81 #endif
82     u8 data[1];
83 } ev_bufItem_t;
84 
85 /**
86  *  @brief Definiton error code of EV buffer operation
87  */
88 typedef enum buf_sts_e {
89     // SUCCESS always be ZERO
90     BUFFER_SUCC,
91     BUFFER_INVALID_PARAMETER = 1,  // !< Invalid parameter passed to the buffer API
92     BUFFER_DUPLICATE_FREE          // !< The same buffer is freed more than once
93 } buf_sts_t;
94 
95 /**  @} end of group EV_BUFFER_TYPE */
96 
97 /** @defgroup EV_BUFFER_FUNCTIONS EV Buffer API
98  *  @brief Function declaration of EV Buffer module
99  *  @{
100  */
101 
102 /**
103   * @brief       Reset the EV Buffer module
104   *
105   * @param       None
106   *
107   * @return      None
108   */
109 void ev_buf_reset(void);
110 
111 /**
112   * @brief       Initialize the EV Buffer module
113   *
114   * @param       None
115   *
116   * @return      None
117   */
118 void ev_buf_init(void);
119 
120 /**
121   * @brief       Allocate an available buffer according to the requested size
122   *              The allocated buffer will have only three kind of size, defined
123   *              in @ref EV_BUFFER_CONSTANT
124   *
125   * @param       size - The requested size
126   *
127   * @return      Pointer to an allocated buffer.
128   *              NULL means the there is no available buffer.
129   */
130 
131 #if EV_BUFFER_DEBUG
132 #define ev_buf_allocate(size) my_ev_buf_allocate(size, __LINE__)
133 #else
134 u8 *ev_buf_allocate(u16 size);
135 #endif
136 
137 /**
138   * @brief       Free the specified buffer
139   *
140   * @param       pBuf - the pointer to the specified buffer to free.
141   *
142   * @return      Status.
143   */
144 
145 #if EV_BUFFER_DEBUG
146 #define ev_buf_free(pBuf) my_ev_buf_free(pBuf, __LINE__)
147 #else
148 buf_sts_t ev_buf_free(u8 *pBuf);
149 #endif
150 
151 /**
152   * @brief       Get the header of a buffer item
153   *
154   * @param       pd - the pointer of a data, which is previously allocated
155   *
156   * @return      Pointer of bufferItem
157   */
158 ev_bufItem_t *ev_buf_getHead(u8 *pd);
159 
160 /**
161  * @brief       judge if the buffer is ev buffer
162  *
163  * @param       the buffer adrress
164  *
165  * @return      1: if ev buffer; 0: others
166  */
167 u8 is_ev_buf(void *arg);
168 
169 u16 ev_buf_getFreeMaxSize(void);
170 /**  @} end of group EV_BUFFER_FUNCTIONS */
171 
172 /**  @} end of group EV_BUFFER */
173 
174 /**  @} end of group TELINK_COMMON_MODULE */
175