fifo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("Autochips Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to Autochips Inc. and/or its licensors.
  6. * Without the prior written permission of Autochips inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of Autochips Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. *
  10. * Copyright(C) 2016, Autochips ( All rights reserved. )
  11. *
  12. * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
  14. * RECEIVED FROM Autochips AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  15. * AN "AS-IS" BASIS ONLY. Autochips EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES Autochips PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE Autochips SOFTWARE, AND BUYER AGREES TO LO0 ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. Autochips SHALL ALSO
  22. * NOT BE RESPONSIBLE FOR ANY Autochips SOFTWARE RELEASES MADE TO BUYER'S
  23. * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  24. *
  25. * BUYER'S SOLE AND EXCLUSIVE REMEDY AND Autochips'S ENTIRE AND CUMULATIVE
  26. * LIABILITY WITH RESPECT TO THE Autochips SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT Autochips'S OPTION, TO REVISE OR REPLACE THE Autochips SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  29. *
  30. * Autochips FOR SUCH Autochips SOFTWARE AT ISSUE.
  31. *
  32. * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  33. * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  34. * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  35. * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  36. * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  37. *****************************************************************************/
  38. /* Includes ------------------------------------------------------------------*/
  39. #include <stdlib.h>
  40. #include "fifo.h"
  41. #include <string.h>
  42. #define minimum(x, y) (((x) < (y))? (x) : (y))
  43. /************************************************************************
  44. Function :
  45. Description : Init the fifo structure.
  46. _fifo : reference to _fifo struct.
  47. size : size of the fifo.
  48. retval 1 if memory allocation fails or 0.
  49. Change history:
  50. Note:
  51. Author:
  52. Date: Jan-10-2013
  53. ************************************************************************/
  54. uint8_t fifo_Init(fifo_TypeDef * _fifo, uint8_t* data, uint32_t size)
  55. {
  56. /* check for a valid pointer */
  57. if (_fifo == NULL)
  58. {
  59. return 1;
  60. }
  61. /* if fifo size is null nothing to do */
  62. if ( size == 0)
  63. {
  64. memset((uint8_t *)_fifo, 0x00, sizeof(fifo_TypeDef));
  65. return 1;
  66. }
  67. else
  68. {
  69. /* allocate fifo space. */
  70. _fifo->baseAddr = data;
  71. if (_fifo->baseAddr == NULL)
  72. {
  73. /* memory allocation failure. */
  74. #ifdef SZ_LOG
  75. putLine();
  76. putString("----> Malloc Fail!");
  77. #endif
  78. return 1;
  79. }
  80. else
  81. {
  82. memset((uint8_t *)_fifo->baseAddr, 0x00, sizeof(uint8_t) * size);
  83. /* tail = head --> empty fifo */
  84. _fifo->head = 0;
  85. _fifo->tail = 0;
  86. _fifo->size = size;
  87. return 0;
  88. }
  89. }
  90. }
  91. /************************************************************************
  92. Function :
  93. Description : DeInit the fifo structure.
  94. _fifo : reference to _fifo struct.
  95. Change history:
  96. Note:
  97. Author:
  98. Date: Jan-10-2013
  99. ************************************************************************/
  100. uint8_t fifo_DeInit(fifo_TypeDef * _fifo)
  101. {
  102. uint8_t status = 1;
  103. if (_fifo == NULL)
  104. {
  105. status = 1;
  106. }
  107. else
  108. {
  109. if (_fifo->baseAddr != NULL)
  110. {
  111. /* optional (clear memory region) */
  112. memset((uint8_t *)_fifo->baseAddr, 0x00, _fifo->size);
  113. memset((uint8_t *)_fifo, 0x00, sizeof(fifo_TypeDef));
  114. status = 0;
  115. }
  116. else
  117. {
  118. /* FiFo invalid base address. */
  119. status = 1;
  120. }
  121. }
  122. return status;
  123. }
  124. /************************************************************************
  125. Function :
  126. Description : Reset the fifo structure.
  127. _fifo : reference to _fifo struct.
  128. Change history:
  129. Note:
  130. Author:
  131. Date: Jan-10-2013
  132. ************************************************************************/
  133. uint8_t fifo_Reset(fifo_TypeDef * _fifo)
  134. {
  135. if (_fifo == NULL)
  136. {
  137. return 1;
  138. }
  139. else
  140. {
  141. if (_fifo->baseAddr != NULL)
  142. {
  143. /* optional (clear memory region) */
  144. memset((uint8_t *)_fifo->baseAddr, 0x00, _fifo->size);
  145. _fifo->head = 0;
  146. _fifo->tail = 0;
  147. return 0;
  148. }
  149. else
  150. {
  151. /* FiFo invalid base address. */
  152. return 1;
  153. }
  154. }
  155. }
  156. /************************************************************************
  157. Function :
  158. Description : returns how much data is stored in the fifo.
  159. _fifo : reference to _fifo struct.
  160. Change history:
  161. Note:
  162. Author:
  163. Date: Jan-10-2013
  164. ************************************************************************/
  165. uint32_t fifo_GetLen(fifo_TypeDef * _fifo)
  166. {
  167. if (_fifo == NULL)
  168. {
  169. return 0;
  170. }
  171. else
  172. {
  173. return (_fifo->tail - _fifo->head);
  174. }
  175. }
  176. /************************************************************************
  177. Function :
  178. Description : returns how much free sapce in the fifo.
  179. _fifo : reference to _fifo struct.
  180. Change history:
  181. Note:
  182. Author:
  183. Date: Jan-10-2013
  184. ************************************************************************/
  185. uint32_t fifo_GetFreeSpace(fifo_TypeDef * _fifo)
  186. {
  187. if (_fifo == NULL)
  188. {
  189. return 0;
  190. }
  191. else
  192. {
  193. return (_fifo->size - (_fifo->tail - _fifo->head));
  194. }
  195. }
  196. /************************************************************************
  197. Function :
  198. Description : returns SET if the FIFO is full.
  199. _fifo : reference to _fifo struct.
  200. Change history:
  201. Note:
  202. Author:
  203. Date: Jan-10-2013
  204. ************************************************************************/
  205. uint8_t fifo_NotFull(fifo_TypeDef * _fifo)
  206. {
  207. if (_fifo == NULL)
  208. {
  209. return 0;
  210. }
  211. else
  212. {
  213. return ((_fifo->tail - _fifo->head) != _fifo->size) ? 1 : 0;
  214. }
  215. }
  216. /************************************************************************
  217. Function :
  218. Description : Insert data to the fifo.
  219. _fifo : reference to _fifo struct.
  220. data : reference to data buffer.
  221. len : data buffer length.
  222. Change history:
  223. Note:
  224. Author:
  225. Date: Jan-10-2013
  226. ************************************************************************/
  227. uint32_t fifo_insert(fifo_TypeDef * _fifo, uint8_t *data, uint32_t len)
  228. {
  229. uint32_t real_len;
  230. uint32_t offset;
  231. uint32_t end;
  232. offset = _fifo->size - (_fifo->tail - _fifo->head);
  233. /*Actual size of data we can write*/
  234. real_len = minimum(len, offset);
  235. /*Offset of write pointer*/
  236. offset = _fifo->tail % _fifo->size;
  237. /*End position before buffer being warpped*/
  238. end = minimum(real_len, _fifo->size - offset);
  239. /*Copy data to buffer before warp*/
  240. memcpy(_fifo->baseAddr + offset, data, end);
  241. /*Copy data to buffer after warp*/
  242. memcpy(_fifo->baseAddr, data + end, real_len - end);
  243. _fifo->tail += real_len;
  244. return real_len;
  245. }
  246. /************************************************************************
  247. Function :
  248. Description : Retrieve data from the fifo.
  249. _fifo : reference to _fifo struct.
  250. data : reference to data buffer.
  251. len : data buffer length.
  252. Change history:
  253. Note:
  254. Author:
  255. Date: Jan-10-2013
  256. ************************************************************************/
  257. uint32_t fifo_retrieve(fifo_TypeDef * _fifo, uint8_t *data, uint32_t len)
  258. {
  259. uint32_t real_len;
  260. uint32_t offset;
  261. uint32_t end;
  262. offset = _fifo->tail- _fifo->head;
  263. real_len = minimum(len, offset);
  264. //得到偏移量
  265. offset = _fifo->head % _fifo->size;
  266. end = minimum(real_len, _fifo->size - offset);
  267. memcpy(data, _fifo->baseAddr + offset, end);
  268. memcpy(data + end, _fifo->baseAddr, real_len - end);
  269. _fifo->head += real_len;
  270. return real_len;
  271. }
  272. /*****************************************************************************
  273. * 函数名: 串口通道帧尾获取函数
  274. * 函数功能: 获取当前帧帧尾所在地址
  275. * 输入参数: _fifo - fifo结构体
  276. * 输入参数: data-帧尾结束符
  277. * 返回数据: fifo中接收到的数据到字节data第一次出现的长度
  278. *****************************************************************************/
  279. uint32_t Fifo_strchr(fifo_TypeDef * _fifo, uint8_t data)
  280. {
  281. uint32_t remaining = 0;
  282. uint32_t len = 0;
  283. uint32_t head;
  284. head = _fifo->head;
  285. remaining = _fifo->tail- head;
  286. head = head % _fifo->size;
  287. while(remaining)
  288. {
  289. len++;
  290. if(data == *((_fifo->baseAddr + head)))
  291. {
  292. return len;
  293. }
  294. head++;
  295. remaining--;
  296. head %= _fifo->size;
  297. }
  298. return 0;
  299. }