123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /* Copyright Statement:
- *
- * This software/firmware and related documentation ("Autochips Software") are
- * protected under relevant copyright laws. The information contained herein
- * is confidential and proprietary to Autochips Inc. and/or its licensors.
- * Without the prior written permission of Autochips inc. and/or its licensors,
- * any reproduction, modification, use or disclosure of Autochips Software,
- * and information contained herein, in whole or in part, shall be strictly prohibited.
- *
- * Copyright(C) 2016, Autochips ( All rights reserved. )
- *
- * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
- * RECEIVED FROM Autochips AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
- * AN "AS-IS" BASIS ONLY. Autochips EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
- * NEITHER DOES Autochips PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
- * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
- * SUPPLIED WITH THE Autochips SOFTWARE, AND BUYER AGREES TO LO0 ONLY TO SUCH
- * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. Autochips SHALL ALSO
- * NOT BE RESPONSIBLE FOR ANY Autochips SOFTWARE RELEASES MADE TO BUYER'S
- * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
- *
- * BUYER'S SOLE AND EXCLUSIVE REMEDY AND Autochips'S ENTIRE AND CUMULATIVE
- * LIABILITY WITH RESPECT TO THE Autochips SOFTWARE RELEASED HEREUNDER WILL BE,
- * AT Autochips'S OPTION, TO REVISE OR REPLACE THE Autochips SOFTWARE AT ISSUE,
- * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
- *
- * Autochips FOR SUCH Autochips SOFTWARE AT ISSUE.
- *
- * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
- * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
- * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
- * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
- * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
- *****************************************************************************/
-
- /* Includes ------------------------------------------------------------------*/
- #include <stdlib.h>
- #include "fifo.h"
- #include <string.h>
- #define minimum(x, y) (((x) < (y))? (x) : (y))
- /************************************************************************
- Function :
- Description : Init the fifo structure.
- _fifo : reference to _fifo struct.
- size : size of the fifo.
- retval 1 if memory allocation fails or 0.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint8_t fifo_Init(fifo_TypeDef * _fifo, uint8_t* data, uint32_t size)
- {
- /* check for a valid pointer */
- if (_fifo == NULL)
- {
- return 1;
- }
-
- /* if fifo size is null nothing to do */
- if ( size == 0)
- {
- memset((uint8_t *)_fifo, 0x00, sizeof(fifo_TypeDef));
- return 1;
- }
- else
- {
- /* allocate fifo space. */
- _fifo->baseAddr = data;
- if (_fifo->baseAddr == NULL)
- {
- /* memory allocation failure. */
- #ifdef SZ_LOG
- putLine();
- putString("----> Malloc Fail!");
- #endif
-
- return 1;
- }
- else
- {
- memset((uint8_t *)_fifo->baseAddr, 0x00, sizeof(uint8_t) * size);
- /* tail = head --> empty fifo */
- _fifo->head = 0;
- _fifo->tail = 0;
- _fifo->size = size;
- return 0;
- }
- }
- }
- /************************************************************************
- Function :
- Description : DeInit the fifo structure.
- _fifo : reference to _fifo struct.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint8_t fifo_DeInit(fifo_TypeDef * _fifo)
- {
- uint8_t status = 1;
-
- if (_fifo == NULL)
- {
- status = 1;
- }
- else
- {
- if (_fifo->baseAddr != NULL)
- {
- /* optional (clear memory region) */
- memset((uint8_t *)_fifo->baseAddr, 0x00, _fifo->size);
- memset((uint8_t *)_fifo, 0x00, sizeof(fifo_TypeDef));
- status = 0;
- }
- else
- {
- /* FiFo invalid base address. */
- status = 1;
- }
- }
- return status;
- }
- /************************************************************************
- Function :
- Description : Reset the fifo structure.
- _fifo : reference to _fifo struct.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint8_t fifo_Reset(fifo_TypeDef * _fifo)
- {
- if (_fifo == NULL)
- {
- return 1;
- }
- else
- {
- if (_fifo->baseAddr != NULL)
- {
- /* optional (clear memory region) */
- memset((uint8_t *)_fifo->baseAddr, 0x00, _fifo->size);
-
- _fifo->head = 0;
- _fifo->tail = 0;
- return 0;
- }
- else
- {
- /* FiFo invalid base address. */
- return 1;
- }
- }
- }
- /************************************************************************
- Function :
- Description : returns how much data is stored in the fifo.
- _fifo : reference to _fifo struct.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint32_t fifo_GetLen(fifo_TypeDef * _fifo)
- {
- if (_fifo == NULL)
- {
- return 0;
- }
- else
- {
- return (_fifo->tail - _fifo->head);
- }
- }
- /************************************************************************
- Function :
- Description : returns how much free sapce in the fifo.
- _fifo : reference to _fifo struct.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint32_t fifo_GetFreeSpace(fifo_TypeDef * _fifo)
- {
- if (_fifo == NULL)
- {
- return 0;
- }
- else
- {
- return (_fifo->size - (_fifo->tail - _fifo->head));
- }
- }
- /************************************************************************
- Function :
- Description : returns SET if the FIFO is full.
- _fifo : reference to _fifo struct.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint8_t fifo_NotFull(fifo_TypeDef * _fifo)
- {
- if (_fifo == NULL)
- {
- return 0;
- }
- else
- {
- return ((_fifo->tail - _fifo->head) != _fifo->size) ? 1 : 0;
- }
- }
- /************************************************************************
- Function :
- Description : Insert data to the fifo.
- _fifo : reference to _fifo struct.
- data : reference to data buffer.
- len : data buffer length.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint32_t fifo_insert(fifo_TypeDef * _fifo, uint8_t *data, uint32_t len)
- {
- uint32_t real_len;
- uint32_t offset;
- uint32_t end;
- offset = _fifo->size - (_fifo->tail - _fifo->head);
- /*Actual size of data we can write*/
- real_len = minimum(len, offset);
- /*Offset of write pointer*/
- offset = _fifo->tail % _fifo->size;
- /*End position before buffer being warpped*/
- end = minimum(real_len, _fifo->size - offset);
-
- /*Copy data to buffer before warp*/
- memcpy(_fifo->baseAddr + offset, data, end);
-
- /*Copy data to buffer after warp*/
- memcpy(_fifo->baseAddr, data + end, real_len - end);
-
- _fifo->tail += real_len;
-
- return real_len;
- }
- /************************************************************************
- Function :
- Description : Retrieve data from the fifo.
- _fifo : reference to _fifo struct.
- data : reference to data buffer.
- len : data buffer length.
- Change history:
- Note:
- Author:
- Date: Jan-10-2013
- ************************************************************************/
- uint32_t fifo_retrieve(fifo_TypeDef * _fifo, uint8_t *data, uint32_t len)
- {
- uint32_t real_len;
- uint32_t offset;
- uint32_t end;
-
-
- offset = _fifo->tail- _fifo->head;
- real_len = minimum(len, offset);
-
- //得到偏移量
- offset = _fifo->head % _fifo->size;
-
- end = minimum(real_len, _fifo->size - offset);
-
- memcpy(data, _fifo->baseAddr + offset, end);
-
- memcpy(data + end, _fifo->baseAddr, real_len - end);
-
- _fifo->head += real_len;
-
- return real_len;
- }
- /*****************************************************************************
- * 函数名: 串口通道帧尾获取函数
- * 函数功能: 获取当前帧帧尾所在地址
- * 输入参数: _fifo - fifo结构体
- * 输入参数: data-帧尾结束符
- * 返回数据: fifo中接收到的数据到字节data第一次出现的长度
- *****************************************************************************/
- uint32_t Fifo_strchr(fifo_TypeDef * _fifo, uint8_t data)
- {
- uint32_t remaining = 0;
- uint32_t len = 0;
- uint32_t head;
-
- head = _fifo->head;
- remaining = _fifo->tail- head;
- head = head % _fifo->size;
-
-
- while(remaining)
- {
- len++;
- if(data == *((_fifo->baseAddr + head)))
- {
- return len;
- }
- head++;
- remaining--;
- head %= _fifo->size;
- }
- return 0;
- }
|