1 /**
2 ********************************************************************************************************
3 Copyright (c) 2015, Realtek Semiconductor Corporation. All rights reserved.
4 *********************************************************************************************************
5 * @file cycle_queue.c
6 * @brief Cycle Queue functions Implementation.
7 * @details
8 *
9 * @author lory_xu
10 * @date 2015-07-13
11 * @version v0.1
12 */
13
14 #include <string.h>
15 #include "os_sync.h"
16 #include "cycle_queue.h"
17 #include "trace_app.h"
18 #include "os_mem.h"
19
20
21 /** @detailed cyc_buffer stores all the printing information which will be print in the PrintTask */
22 uint8_t *cyc_buffer = NULL;
23 volatile uint16_t pRead = 0;
24 volatile uint16_t pWrite = 0;
25
26 bool MallocCycQueue(void);
27 /**
28 * @brief check if write queue empty
29 *
30 * @param
31 *
32 * @return
33 */
IsCycQueueEmpty()34 uint8_t IsCycQueueEmpty()
35 {
36 uint16_t tmpRead = pRead;
37 uint16_t tmpWrite = pWrite;
38 return (tmpRead == tmpWrite);
39 }
40
41 /**
42 * @brief check if write queue full
43 *
44 * @param
45 *
46 * @return
47 */
IsCycQueueFull()48 uint8_t IsCycQueueFull()
49 {
50 uint16_t tmpRead = pRead;
51 uint16_t tmpWrite = pWrite;
52 return ((tmpWrite + 1) & (MAX_BUFFER_SIZE - 1)) == tmpRead;
53 }
54
55 /**
56 * @brief get data size in cycle queue
57 *
58 * @param
59 *
60 * @return
61 */
CycQueueSize()62 uint16_t CycQueueSize()
63 {
64 uint16_t tmpRead = pRead;
65 uint16_t tmpWrite = pWrite;
66 return (tmpWrite - tmpRead + MAX_BUFFER_SIZE) & (MAX_BUFFER_SIZE - 1);
67 }
68
69 /**
70 * @brief remain cycle queue size
71 *
72 * @param
73 *
74 * @return
75 */
CycQueueRemainSize()76 uint16_t CycQueueRemainSize()
77 {
78 uint16_t tmpRead = pRead;
79 uint16_t tmpWrite = pWrite;
80 return (MAX_BUFFER_SIZE - tmpWrite + tmpRead - 1) & (MAX_BUFFER_SIZE - 1);
81 }
82
83 /**
84 * @brief write data to cycle queue
85 *
86 * @param pWriteBuf: data buf
87 * @param length: data length
88 *
89 * @return
90 */
CycQueueWrite(uint8_t * pWriteBuf,uint16_t length)91 bool CycQueueWrite(uint8_t *pWriteBuf, uint16_t length)
92 {
93 bool ret = true;
94 uint32_t s;
95
96 if (cyc_buffer == NULL)
97 {
98 // DiagPutChar("cyc_buffer is init\r\n");
99 MallocCycQueue();
100 if(cyc_buffer == NULL)
101 {
102 APP_PRINT_ERROR0("cyc_buffer is NULL, malloc fail");
103 return false;
104 }
105 }
106
107 s = os_lock();
108
109 if (CycQueueRemainSize() >= length)
110 {
111 if ((pWrite + length) <= MAX_BUFFER_SIZE)
112 {
113 memcpy(cyc_buffer + pWrite, pWriteBuf, length);
114 pWrite = (pWrite + length) & (MAX_BUFFER_SIZE - 1);
115 }
116 else
117 {
118 uint16_t part_length = MAX_BUFFER_SIZE - pWrite;
119 memcpy(cyc_buffer + pWrite, pWriteBuf, part_length);
120 memcpy(cyc_buffer, (pWriteBuf + part_length), (length - part_length));
121 pWrite = length - part_length;
122 }
123 }
124 else
125 {
126 ret = false;
127 }
128
129 os_unlock(s);
130
131 return ret;
132 }
133
134 /**
135 * @brief upate read ponit of cycle queue
136 *
137 * @param SendSize
138 *
139 * @return
140 */
UpdateQueueRead(uint16_t SendSize)141 void UpdateQueueRead(uint16_t SendSize)
142 {
143 uint32_t s;
144
145 s = os_lock();
146 pRead = (pRead + SendSize) & (MAX_BUFFER_SIZE - 1);
147 os_unlock(s);
148 }
149
150
151 /**
152 * @brief malloc the buffer
153 *
154 * @param malloc the cycle
155 *
156 * @return
157 */
MallocCycQueue()158 bool MallocCycQueue()
159 {
160 if(cyc_buffer != NULL)
161 {
162 APP_PRINT_ERROR0("cyc_buffer is not free");
163 FreeCycQueue();
164 }
165 cyc_buffer = os_mem_zalloc(RAM_TYPE_DATA_ON, MAX_BUFFER_SIZE);
166 pRead = 0;
167 pWrite = 0;
168 return true;
169 }
170
171 /**
172 * @brief free the buffer
173 *
174 * @param free the buffer
175 *
176 * @return
177 */
FreeCycQueue()178 void FreeCycQueue()
179 {
180 if (cyc_buffer != NULL) {
181 os_mem_free(cyc_buffer);
182 }
183 pRead = 0;
184 pWrite = 0;
185 cyc_buffer = NULL;
186 }
187
188