第一次提交

This commit is contained in:
2026-04-09 10:14:20 +08:00
commit e325a77b42
584 changed files with 279711 additions and 0 deletions

3191
Middlewares/cJSON.c Normal file

File diff suppressed because it is too large Load Diff

306
Middlewares/cJSON.h Normal file
View File

@@ -0,0 +1,306 @@
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
For *nix builds that support visibility attribute, you can define similar behavior by
setting default visibility to hidden by adding
-fvisibility=hidden (for gcc)
or
-xldscope=hidden (for sun cc)
to CFLAGS
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
*/
#define CJSON_CDECL __cdecl
#define CJSON_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_EXPORT_SYMBOLS
#endif
#if defined(CJSON_HIDE_SYMBOLS)
#define CJSON_PUBLIC(type) type CJSON_STDCALL
#elif defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#elif defined(CJSON_IMPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#endif
#else /* !__WINDOWS__ */
#define CJSON_CDECL
#define CJSON_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define CJSON_PUBLIC(type) type
#endif
#endif
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 18
#include <stddef.h>
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
void *(CJSON_CDECL *malloc_fn)(size_t sz);
void (CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_NESTING_LIMIT
#define CJSON_NESTING_LIMIT 1000
#endif
/* Limits the length of circular references can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_CIRCULAR_LIMIT
#define CJSON_CIRCULAR_LIMIT 10000
#endif
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
/* Check item type and return its value */
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* Create a string where valuestring references a string so
* it will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
/* Create an object/array that only references it's elements so
* they will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
/* These utilities create an Array of count items.
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detach items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
* The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
* The input pointer json cannot point to a read-only address area, such as a string constant,
* but should point to a readable and writable address area. */
CJSON_PUBLIC(void) cJSON_Minify(char *json);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
#define cJSON_SetBoolValue(object, boolValue) ( \
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
cJSON_Invalid\
)
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,284 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "circular_buffer.h"
#include "priv_malloc.h"
/**
* @brief Check if Num is power of 2
*
* @param[in] Num the number to check
*
* @return 1 if Num is power of 2
*/
unsigned long long IsPowerOf2(unsigned long long Num) {
return (Num > 0 && !(Num & (Num - 1)));
}
/**
* @brief calculate the minimum number that round up to the next power of 2
*
* @param[in] Num the number to calculate
*
* @return the number that round up to the next power of 2 (0x100 if Num is 0xf0, 0x81, 0xa3 ... )
*/
unsigned long RoundUp_PowerOf2(unsigned long Num) {
unsigned long result = 1;
if (IsPowerOf2(Num) || Num == 0)
return Num;
else if (Num > LONG_MAX)
return (LONG_MAX ^ ULONG_MAX); // WARN: if Num biger than (LONG_MAX+1) then result will equals to (LONG_MAX+1)
while (Num) {
Num >>= 1;
result <<= 1;
}
return result;
}
/**
* @brief calculate the minimum number that round down to the next power of 2
*
* @param[] Num the number to check
*
* @return the number that round up to the last power of 2 (4 if Num is 5,6,7, 8 if Num is 9,10,11 ... )
*/
unsigned long RoundDown_PowerOf2(unsigned long Num) {
unsigned long result = 1;
if (IsPowerOf2(Num) || Num == 0)
return Num;
else if (Num > LONG_MAX)
return (LONG_MAX ^ ULONG_MAX); // WARN: if Num biger than (LONG_MAX+1) then result will equals to (LONG_MAX+1)
while (Num) {
Num >>= 1;
result <<= 1;
}
return result >> 1;
}
/**
* @brief Init the Circular buffer with a array
*
* @param[in] CBuf The circular buffer to initial
* @param[in] Buff the buffer for circular buffer to store data
* @param[in] Size the size of buffer
*
* @return the Round Down(Power Of 2) size that the circular buffer to be used
*/
int CircBuf_Init(CircBuf_t *CBuf, unsigned char *Buff, unsigned int Size) {
CBuf->Buffer = Buff;
if(!IsPowerOf2(Size)) {
if (Size > INT_MAX)
Size = (INT_MAX ^ UINT_MAX);
else
Size = (int) RoundDown_PowerOf2(Size);
}
CBuf->Size = Size;
CBuf->Tailer = 0;
CBuf->Header = 0;
return Size;
}
/**
* @brief Circular Buffer initialization
*
* @param[in] CBuf the circular buffer to initialization
* @param[in] Size size of the circular buffer
*
* @return 1 if memery allocation success
* 0 if fail
*/
int CircBuf_Alloc(CircBuf_t *CBuf, unsigned int Size) {
int result = 0;
if(!IsPowerOf2(Size)) {
if(Size > INT_MAX)
Size = (INT_MAX ^ UINT_MAX);
else
Size = (int)RoundUp_PowerOf2(Size);
}
CBuf->Buffer = (unsigned char *) board_calloc(Size); // Buffer will set to 0
CBuf->Tailer = 0;
CBuf->Header = 0;
if(CBuf->Buffer != NULL) {
CBuf->Size = Size;
result = 1;
}
return result;
}
/**
* @brief delete circular buffer and release the memery
*
* @param[in] CBuf the circular buffer to delete
*/
void CircBuf_Free(CircBuf_t *CBuf) {
free(CBuf->Buffer);
CBuf = NULL;
}
/**
* @brief put data into the circular buffer
*
* @param[in] CBuf the circular buffer that will store the data
* @param[in] data the data to store into circular buffer
* @param[in] LenToPush the length of data to store into circular buffer
*
* @return the actual size stored into circular buffer
*/
unsigned int CircBuf_Push(CircBuf_t *CBuf, unsigned char *data, unsigned int LenToPush) {
unsigned int len;
LenToPush = MIN(LenToPush, (CBuf->Size - (CBuf->Header - CBuf->Tailer)));
len = MIN(LenToPush, CBuf->Size - (CBuf->Header & (CBuf->Size - 1)));
memcpy(CBuf->Buffer + (CBuf->Header & CBuf->Size - 1), data, len);
memcpy(CBuf->Buffer, data + len, LenToPush - len);
CBuf->Header += LenToPush;
return LenToPush;
}
/**
* @brief get data from circular buffer
*
* @param[in] CBuf the circular buffer that stored data
* @param[in] data target buffer that will store the data that from circular buffer
* @param[in] LenToPop the length that wan't to get from circular buffer
*
* @return actual length that get from circular buffer
*/
unsigned int CircBuf_Pop(CircBuf_t *CBuf, unsigned char *data, unsigned int LenToPop) {
unsigned int len;
LenToPop = MIN(LenToPop, CBuf->Header - CBuf->Tailer);
len = MIN(LenToPop, CBuf->Size - (CBuf->Tailer & (CBuf->Size - 1)));
memcpy(data, CBuf->Buffer + (CBuf->Tailer & (CBuf->Size - 1)), len);
memcpy(data + len, CBuf->Buffer, LenToPop - len);
CBuf->Tailer += LenToPop;
return LenToPop;
}
/**
* @brief get one char from circular buffer
*
* @param[in] CBuf the circular buffer that stored data
* @param[n] data target buffer that will store the data that from circular buffer
*
* @return actual length that get from circular buffer
*/
unsigned int CircBuf_PopOneChar(CircBuf_t *CBuf, unsigned char *data) {
return CircBuf_Pop(CBuf, data, 1);
}
/**
* @brief for access data at Tailer + offset
*
* @param[in] CBuf the circular buffer that stored data
* @param[in] offset the offset of Tailer
*
* @return the data at Buffer[Tailer + offset]
*/
unsigned char CircBuf_At(CircBuf_t *CBuf, unsigned int offset) {
unsigned int index = (CBuf->Tailer + offset) & (CBuf->Size - 1);
return CBuf->Buffer[index];
}
/**
* @brief get data from circular buffer
*
* @param[in] CBuf the circular buffer that stored data
* @param[in] data target buffer that will store the data that from circular buffer
* @param[in] LenToRead the length that wan't to get from circular buffer
*
* @return actual length that get from circular buffer
*/
unsigned int CircBuf_Read(CircBuf_t *CBuf, unsigned char *data, unsigned int LenToRead) {
unsigned int len;
LenToRead = MIN(LenToRead, CBuf->Header - CBuf->Tailer);
len = MIN(LenToRead, CBuf->Size - (CBuf->Tailer & (CBuf->Size - 1)));
memcpy(data, CBuf->Buffer + (CBuf->Tailer & (CBuf->Size - 1)), len);
memcpy(data + len, CBuf->Buffer, LenToRead - len);
return LenToRead;
}
/**
* @brief drop the the size of data at tailer
*
* @param[in] CBuf the circular buffer that stored data
* @param[in] LenToDrop the size of data at tailer of circular_buffer to drop
*/
void CircBuf_Drop(CircBuf_t *CBuf, unsigned int LenToDrop) {
if((CBuf->Tailer + LenToDrop) <= CBuf->Header )
CBuf->Tailer += LenToDrop;
else
CBuf->Tailer = CBuf->Header;
}
/**
* @brief get the Available memery size of circular buffer
*
* @param[in] CBuf the circular buffer to get size
*
* @return Available size of the circular buffer
*/
unsigned int CircBuf_GetAvalaibleSize(CircBuf_t *CBuf) {
return ((CBuf->Size > 0) ? (CBuf->Size - (CBuf->Header - CBuf->Tailer)) : 0);
}
/**
* @brief get the used memery size of circular buffer
*
* @param[in] CBuf the circular buffer to get size
*
* @return used size of the circular buffer
*/
unsigned int CircBuf_GetUsedSize(CircBuf_t *CBuf) {
return (CBuf->Header - CBuf->Tailer);
}
/**
* @brief check if the circular buffer is empty
*
* @param[in] CBuf the circular buffer to check
*
* @return 1 if no data stored in the circular buffer
* 0 if the size of circular buffer equals to 0
* or some data stored in the circular buffer
*/
unsigned int CircBuf_IsEmpty(CircBuf_t *CBuf) {
return ((CBuf->Size > 0) && (CBuf->Header == CBuf->Tailer));
}
/**
* @brief check if the circular buffer is full
*
* @param[in] CBuf the circular buffer to check
*
* @return 1 if the size of circular buffer equals to 0
* or no Available space of circular buffer
*/
unsigned int CircBuf_IsFull(CircBuf_t *CBuf) {
return ((CBuf->Size == 0) || (CBuf->Size == (CBuf->Header - CBuf->Tailer)));
}

View File

@@ -0,0 +1,41 @@
#ifndef __CIRC_BUF__
#define __CIRC_BUF__
#ifdef __cplusplus
extern "C" {
#endif
#define MIN(a, b) (((a) > (b)) ? (b) : (a))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
typedef struct CIRC_BUF {
unsigned int Size;
unsigned int Header;
unsigned int Tailer;
unsigned char *Buffer;
} CircBuf_t;
unsigned long long IsPowerOf2 (unsigned long long Num);
unsigned long RoundUp_PowerOf2 (unsigned long Num);
unsigned long RoundDown_PowerOf2(unsigned long Num);
int CircBuf_Init(CircBuf_t *CBuf, unsigned char *Buff, unsigned int Size);
int CircBuf_Alloc (CircBuf_t *CBuf, unsigned int Size);
void CircBuf_Free (CircBuf_t *CBuf);
unsigned int CircBuf_Push (CircBuf_t *CBuf, unsigned char *data, unsigned int LenToPush);
unsigned int CircBuf_Pop (CircBuf_t *CBuf, unsigned char *data, unsigned int LenToPop);
unsigned int CircBuf_PopOneChar (CircBuf_t *CBuf, unsigned char *data);
unsigned char CircBuf_At(CircBuf_t *CBuf, unsigned int offset);
unsigned int CircBuf_Read(CircBuf_t *CBuf, unsigned char *data, unsigned int LenToRead);
void CircBuf_Drop(CircBuf_t *CBuf, unsigned int LenToDrop);
unsigned int CircBuf_GetAvalaibleSize (CircBuf_t *CBuf);
unsigned int CircBuf_GetUsedSize (CircBuf_t *CBuf);
unsigned int CircBuf_IsEmpty (CircBuf_t *CBuf);
unsigned int CircBuf_IsFull (CircBuf_t *CBuf);
#ifdef __cplusplus
}
#endif
#endif

161
Middlewares/mx_frame_core.c Normal file
View File

@@ -0,0 +1,161 @@
#include "mx_frame_core.h"
#include "string.h"
static uint16_t CalChecksum(uint8_t * data, uint16_t len)
{
uint16_t sum = 0;
for(int i = 0; i < len; ++i)
{
sum += data[i];
}
return sum;
}
/*
<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>֡<EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rev_buff--<2D><><EFBFBD>պ<EFBFBD>һ֡<D2BB><D6A1><EFBFBD>ݵ<EFBFBD>buff<66><66>ַ,rev_buff_len--<2D><><EFBFBD>պ<EFBFBD>һ֡<D2BB><D6A1><EFBFBD>ݵij<DDB5><C4B3>ȣ<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>֡<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t mx_frame_rev(uint8_t* rev_buff, uint16_t rev_buff_len, mx_frame_struct *mx_out_frame)
{
uint8_t scan_max_number = 30;
uint8_t state = 0;
uint16_t index = 0;
uint16_t start_index = 0;
uint16_t surplus_len = 0;
uint16_t frame_sof = 0;
uint8_t frame_trantype = 0;
uint16_t frame_datalen = 0;
uint8_t *frame_data = NULL;
uint8_t frame_type = 0;
uint16_t frame_checksum = 0;
if(rev_buff_len < 8)
{
return FRAME_LEN_LITTLE;
}
while(1)
{
switch(state)
{
case SOF_GET:
{
frame_sof = (rev_buff[index]<<8) + rev_buff[index + 1];
index++;
surplus_len = rev_buff_len - index;
if(frame_sof == FRAME_HEAD)
{
start_index = index - 1;
state = TRANTYPE_GET;
index++;
}
else if(index == scan_max_number || surplus_len < 8)
{
return SOF_ERR;
}
}
break;
case TRANTYPE_GET:
{
frame_trantype = rev_buff[index];
index++;
state = DATALEN_GET;
}
break;
case DATALEN_GET:
{
frame_datalen = rev_buff[index] + (rev_buff[index + 1]<<8);
index = index + 2;
surplus_len = rev_buff_len - index;
if(frame_datalen - 3 <= surplus_len)
{
state = TYPE_GET;
}
else
{
return DATA_LEN_OVER;
}
}
break;
case TYPE_GET:
{
frame_type = rev_buff[index];
index++;
state = DATA_GET;
}
break;
case DATA_GET:
{
frame_data = &rev_buff[index];
index = index + frame_datalen - 6;
state = CHECKSUM_GET;
}
break;
case CHECKSUM_GET:
{
frame_checksum = (rev_buff[index + 1]<<8) + rev_buff[index];
if(frame_checksum == CalChecksum((uint8_t*)&rev_buff[start_index], frame_datalen))
{
mx_out_frame->type = frame_type;
mx_out_frame->trantype = frame_trantype;
mx_out_frame->sof = frame_sof;
mx_out_frame->datalen = frame_datalen;
mx_out_frame->data = frame_data;
//memcpy(mx_out_frame->data,frame_data,frame_datalen-6);//<2F><>bug
mx_out_frame->checksum = frame_checksum;
return REV_SUCCESSFUL;
}
else
{
return CHECK_SUM_ERR;
}
}
}
}
}
uint16_t create_frame(uint8_t *out_frame_buff, uint16_t out_fram_buff_len, mx_frame_struct *mx_input_frame)
{
uint16_t num = 0;
uint16_t loop = 0;
uint16_t checksum = 0;
uint8_t *tx_buff = out_frame_buff;
if(tx_buff == NULL)
{
return 0;
}
tx_buff[num++] = (uint8_t)SERIAL_HEAD;
tx_buff[num++] = SERIAL_HEAD>>8;
tx_buff[num++] = mx_input_frame->trantype;
tx_buff[num++] = (mx_input_frame->datalen + 6) & 0xff;
tx_buff[num++] = (mx_input_frame->datalen + 6) >> 8;
tx_buff[num++] = mx_input_frame->type;
for(loop = 0; loop < mx_input_frame->datalen; loop++)
{
tx_buff[num++] = mx_input_frame->data[loop];
}
checksum = CalChecksum(tx_buff, num);
tx_buff[num++] = checksum & 0xff;
tx_buff[num] = checksum >> 8;
return num + 1;
}

View File

@@ -0,0 +1,43 @@
#ifndef __MX_SERIAL_CORE_H
#define __MX_SERIAL_CORE_H
#include "at32a423.h"
#define SERIAL_HEAD 0x5AA5
#define FRAME_HEAD 0XA55A
typedef enum
{
DATA_SRC_ERR,
SOF_ERR,
DATA_LEN_OVER,
FRAME_LEN_LITTLE,
CHECK_SUM_ERR,
REV_SUCCESSFUL,
}mx_frame_rev_err;
typedef enum
{
SOF_GET = 0,
TRANTYPE_GET,
DATALEN_GET,
TYPE_GET,
DATA_GET,
CHECKSUM_GET,
}mx_frame_rev_enum;
#pragma pack (1)
typedef struct
{
uint16_t sof;
uint8_t trantype;
uint16_t datalen;
uint8_t type;
uint8_t *data;
uint16_t checksum;
}mx_frame_struct;
#pragma pack ()
uint8_t mx_frame_rev(uint8_t* rev_buff, uint16_t rev_buff_len, mx_frame_struct *mx_out_frame);
uint16_t create_frame(uint8_t *out_frame_buff, uint16_t out_fram_buff_len, mx_frame_struct *mx_input_frame);
#endif

28
Middlewares/mx_serial.c Normal file
View File

@@ -0,0 +1,28 @@
#include "mx_serial.h"
#include "bsp_uart.h"
#include "mx_frame_core.h"
#include <string.h>
uint8_t mx_serial_buff[MX_SERIAL_BUFF_SIZE];
uint16_t mx_serial_rx_parser(USART_COM_ID_T com_id,mx_frame_struct *mx_rx_frame)
{
int32_t len = 0;
len = usart_rx_get_rx_data_count(com_id);
memset(mx_serial_buff, 0, sizeof(mx_serial_buff)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
usart_rx_recv(com_id,(uint8_t*)mx_serial_buff, len);
return mx_frame_rev(mx_serial_buff,sizeof(mx_serial_buff),mx_rx_frame);
}
uint16_t mx_serial_tx_frame(USART_COM_ID_T com_id, mx_frame_struct *mx_tx_frame)
{
uint16_t len = 0;
len = create_frame(mx_serial_buff,sizeof(mx_serial_buff),mx_tx_frame);
if(len)
{
len = usart_tx_push(com_id, mx_serial_buff,len);
}
return len;
}

8
Middlewares/mx_serial.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _MX_SERIAL_H
#define _MX_SERIAL_H
#define MX_SERIAL_BUFF_SIZE 1024
#include "bsp_uart.h"
#include "mx_frame_core.h"
uint16_t mx_serial_rx_parser(USART_COM_ID_T com_id,mx_frame_struct *mx_rx_frame);
uint16_t mx_serial_tx_frame(USART_COM_ID_T com_id, mx_frame_struct *mx_tx_frame);
#endif

84
Middlewares/mx_spi.c Normal file
View File

@@ -0,0 +1,84 @@
#include "mx_spi.h"
#include "bsp_spi.h"
#include "string.h"
mx_spi_frame_struct mx_spi_frame = {0};
static uint16_t CalChecksum(uint8_t * data, uint16_t len)
{
uint16_t sum = 0;
for(int i = 0; i < len; ++i)
{
sum += data[i];
}
return sum;
}
uint8_t creat_mx_spi_frame(uint8_t device_id, uint8_t message_type, uint8_t error_flag, uint8_t *data, uint16_t data_len, mx_spi_frame_struct * mx_spi_out_frame)
{
if(data_len != sizeof(mx_spi_out_frame->data))
{
return 0;
}
else
{
mx_spi_out_frame->device_id = device_id;
mx_spi_out_frame->message_type = message_type;
mx_spi_out_frame->error_flag = error_flag;
memcpy(mx_spi_out_frame->data,data,data_len);
mx_spi_out_frame->check_sum = CalChecksum((uint8_t *)mx_spi_out_frame,sizeof(mx_spi_frame_struct)-1);
return 1;
}
}
void mx_spi_init(void)
{
spi1_init((uint8_t *)&mx_spi_frame, sizeof(mx_spi_frame));
}
void update_spi_data(uint8_t *sensor_data,uint16_t sensor_data_len)
{
static mx_spi_frame_struct mx_spi_frame_temp = {0};
creat_mx_spi_frame(0x01,0x01,0x00,sensor_data,sensor_data_len,&mx_spi_frame_temp);
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == RESET)
{
if(SPI_SLAVE_CS_READ() == SET)
{
memcpy((uint8_t *)&mx_spi_frame,(uint8_t *)&mx_spi_frame_temp,sizeof(mx_spi_frame_struct));
}
}
}
//ˢ<>±<EFBFBD>־
void updata_spi_dma(void)
{
static uint8_t busy = 0;
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == RESET && SPI_SLAVE_CS_READ() == SET)
{
if(busy)
{
//mx_spi_init();
busy = 0;
dma_channel_enable(DMA2_CHANNEL1, FALSE);
spi_enable(SPI1, FALSE);
spi_enable(SPI1, TRUE);
dma_data_number_set(DMA2_CHANNEL1, sizeof(mx_spi_frame));
dma_channel_enable(DMA2_CHANNEL1, TRUE);
}
}
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == SET && SPI_SLAVE_CS_READ() == RESET)
{
busy = 1;
}
}

19
Middlewares/mx_spi.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef _MX_SPI_H
#define _MX_SPI_H
#include "at32a423.h"
#include "main.h"
typedef struct
{
uint8_t : 2;
uint8_t message_type : 2;
uint8_t device_id : 4;
uint8_t data[AX_NUM*AY_NUM];
uint8_t error_flag;
uint8_t check_sum;
}mx_spi_frame_struct;
void mx_spi_init(void);
void update_spi_data(uint8_t *sensor_data,uint16_t sensor_data_len);
void updata_spi_dma(void);
#endif

192
Middlewares/priv_malloc.c Normal file
View File

@@ -0,0 +1,192 @@
#include "priv_malloc.h"
//<2F>ڴ<EFBFBD><DAB4><EFBFBD>(32<33>ֽڶ<D6BD><DAB6><EFBFBD>)
__attribute__((aligned (32))) uint8_t mem1base[MEM1_MAX_SIZE]; //<2F>ڲ<EFBFBD>SRAM<41>ڴ<EFBFBD><DAB4><EFBFBD>
__attribute__((aligned (32))) uint8_t mem2base[MEM2_MAX_SIZE]; //<2F>ڲ<EFBFBD>SRAM<41>ڴ<EFBFBD><DAB4><EFBFBD>
__attribute__((aligned (32))) uint16_t mem1mapbase[MEM1_ALLOC_TABLE_SIZE];
__attribute__((aligned (32))) uint16_t mem2mapbase[MEM2_ALLOC_TABLE_SIZE];
//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
const uint32_t memtblsize[SRAMBANK] = {MEM1_ALLOC_TABLE_SIZE, MEM2_ALLOC_TABLE_SIZE}; //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>С
const uint32_t memblksize[SRAMBANK] = {MEM1_BLOCK_SIZE, MEM2_BLOCK_SIZE}; //<2F>ڴ<EFBFBD><DAB4>ֿ<EFBFBD><D6BF><EFBFBD>С
const uint32_t memsize[SRAMBANK]={MEM1_MAX_SIZE, MEM2_MAX_SIZE}; //<2F>ڴ<EFBFBD><DAB4>ܴ<EFBFBD>С
//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct _m_mallco_dev mallco_dev=
{
my_mem_init, //<2F>ڴ<EFBFBD><DAB4><EFBFBD>ʼ<EFBFBD><CABC>
my_mem_perused, //<2F>ڴ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>
mem1base, //<2F>ڴ<EFBFBD><DAB4><EFBFBD>
mem2base, //<2F>ڴ<EFBFBD><DAB4><EFBFBD>
mem1mapbase,
mem2mapbase,
0,0 //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>
};
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
//*des:Ŀ<>ĵ<EFBFBD>ַ
//*src:Դ<><D4B4>ַ
//n:<3A><>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶ<EFBFBD><C6B5>ڴ泤<DAB4><E6B3A4>(<28>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA>λ)
void mymemcpy(void *des,void *src,uint32_t n)
{
uint8_t *xdes=des;
uint8_t *xsrc=src;
while(n--)*xdes++=*xsrc++;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
//*s:<3A>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ
//c :Ҫ<><D2AA><EFBFBD>õ<EFBFBD>ֵ
//count:<3A><>Ҫ<EFBFBD><D2AA><EFBFBD>õ<EFBFBD><C3B5>ڴ<EFBFBD><DAB4><EFBFBD>С(<28>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA>λ)
void mymemset(void *s,uint8_t c,uint32_t count)
{
uint8_t *xs = s;
while(count--)*xs++=c;
}
//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
void my_mem_init(uint8_t memx)
{
mymemset(mallco_dev.memmap[memx], 0, memtblsize[memx] * 2);//<2F>ڴ<EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mymemset(mallco_dev.membase[memx], 0, memsize[memx]); //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mallco_dev.memrdy[memx]=1; //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>OK
}
//<2F><>ȡ<EFBFBD>ڴ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ֵ:ʹ<><CAB9><EFBFBD><EFBFBD>(0~100)
uint8_t my_mem_perused(uint8_t memx)
{
uint32_t used=0;
uint32_t i;
for(i = 0; i < memtblsize[memx]; i ++)
{
if(mallco_dev.memmap[memx][i])used++;
}
return (used * 100) / (memtblsize[memx]);
}
//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>(<28>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>)
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//size:Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>С(<28>ֽ<EFBFBD>)
//<2F><><EFBFBD><EFBFBD>ֵ:0XFFFFFFFF,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B><><EFBFBD><EFBFBD>,<2C>ڴ<EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ
uint32_t my_mem_malloc(uint8_t memx,uint32_t size)
{
signed long offset=0;
uint32_t nmemb; //<2F><>Ҫ<EFBFBD><D2AA><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
uint32_t cmemb = 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
uint32_t i;
if(!mallco_dev.memrdy[memx])mallco_dev.init(memx);//δ<><CEB4>ʼ<EFBFBD><CABC>,<2C><>ִ<EFBFBD>г<EFBFBD>ʼ<EFBFBD><CABC>
if(size == 0) return 0XFFFFFFFF;//<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
nmemb = size/memblksize[memx]; //<2F><>ȡ<EFBFBD><C8A1>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
if(size%memblksize[memx]) nmemb++;
for(offset = memtblsize[memx] - 1; offset >= 0; offset --)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(!mallco_dev.memmap[memx][offset]) cmemb++;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else cmemb = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(cmemb == nmemb) //<2F>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nmemb<6D><62><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
{
for(i = 0; i < nmemb; i ++) //<2F><>ע<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ǿ<EFBFBD>
{
mallco_dev.memmap[memx][offset + i] = nmemb;
}
return (offset * memblksize[memx]);//<2F><><EFBFBD><EFBFBD>ƫ<EFBFBD>Ƶ<EFBFBD>ַ
}
}
return 0XFFFFFFFF;//δ<>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
}
//<2F>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>(<28>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>)
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//offset:<3A>ڴ<EFBFBD><DAB4><EFBFBD>ַƫ<D6B7><C6AB>
//<2F><><EFBFBD><EFBFBD>ֵ:0,<2C>ͷųɹ<C5B3>;1,<2C>ͷ<EFBFBD>ʧ<EFBFBD><CAA7>;
uint8_t my_mem_free(uint8_t memx,uint32_t offset)
{
int i;
if(!mallco_dev.memrdy[memx])//δ<><CEB4>ʼ<EFBFBD><CABC>,<2C><>ִ<EFBFBD>г<EFBFBD>ʼ<EFBFBD><CABC>
{
mallco_dev.init(memx);
return 1;//δ<><CEB4>ʼ<EFBFBD><CABC>
}
if(offset<memsize[memx])//ƫ<><C6AB><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>.
{
int index = offset / memblksize[memx]; //ƫ<><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int nmemb=mallco_dev.memmap[memx][index]; //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i = 0; i < nmemb; i ++) //<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
mallco_dev.memmap[memx][index+i]=0;
}
return 0;
}else return 2;//ƫ<>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>.
}
//<2F>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>(<28>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>)
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//ptr:<3A>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ
void myfree(uint8_t memx,void *ptr)
{
uint32_t offset;
if(ptr == NULL) return;//<2F><>ַΪ0.
offset=(uint32_t)ptr-(uint32_t)mallco_dev.membase[memx];
my_mem_free(memx,offset); //<2F>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>(<28>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>)
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//size:<3A>ڴ<EFBFBD><DAB4><EFBFBD>С(<28>ֽ<EFBFBD>)
//<2F><><EFBFBD><EFBFBD>ֵ:<3A><><EFBFBD><EFBFBD><E4B5BD><EFBFBD>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ.
void *mymalloc(uint8_t memx,uint32_t size)
{
uint32_t offset;
offset = my_mem_malloc(memx,size);
if(offset==0XFFFFFFFF) return NULL;
else return (void*)((uint32_t)mallco_dev.membase[memx]+offset);
}
//<2F><><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ڴ<EFBFBD>(<28>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>)
//memx:<3A><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
//*ptr:<3A><><EFBFBD>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ
//size:Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>С(<28>ֽ<EFBFBD>)
//<2F><><EFBFBD><EFBFBD>ֵ:<3A>·<EFBFBD><C2B7><EFBFBD><E4B5BD><EFBFBD>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ.
void *myrealloc(uint8_t memx,void *ptr,uint32_t size)
{
uint32_t offset;
offset = my_mem_malloc(memx,size);
if(offset==0XFFFFFFFF)return NULL;
else
{
mymemcpy((void*)((uint32_t)mallco_dev.membase[memx]+offset), ptr, size); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD>ڴ<EFBFBD>
myfree(memx,ptr); //<2F>ͷž<CDB7><C5BE>ڴ<EFBFBD>
return (void*)((uint32_t)mallco_dev.membase[memx]+offset); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>׵<EFBFBD>ַ
}
}
void *board_calloc(uint32_t size)
{
void *ptr = NULL;
ptr = mymalloc(SRAM_1, size);
if (ptr != NULL)
mymemset(ptr, 0, size);
return ptr;
}
void *board_malloc(uint32_t size)
{
return mymalloc(SRAM_1, size);
}
void *board_realloc(void *ptr, uint32_t size)
{
return myrealloc(SRAM_1, ptr, size);
}
void board_free(void *ptr)
{
myfree(SRAM_1, ptr);
}

50
Middlewares/priv_malloc.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef __PRIV_MALLOC_H__
#define __PRIV_MALLOC_H__
#include "stdint.h"
#ifndef NULL
#define NULL 0
#endif
#define SRAM_1 0
#define SRAM_2 1
#define SRAMBANK 2
#define MEM1_BLOCK_SIZE 512
#define MEM1_MAX_SIZE 10 * 1024
#define MEM1_ALLOC_TABLE_SIZE MEM1_MAX_SIZE / MEM1_BLOCK_SIZE
#define MEM2_BLOCK_SIZE 32
#define MEM2_MAX_SIZE 1 * 64
#define MEM2_ALLOC_TABLE_SIZE MEM2_MAX_SIZE / MEM2_BLOCK_SIZE
struct _m_mallco_dev
{
void (*init)(uint8_t);
uint8_t (*perused)(uint8_t);
uint8_t *membase[SRAMBANK];
uint16_t *memmap[SRAMBANK];
uint8_t memrdy[SRAMBANK];
};
extern struct _m_mallco_dev mallco_dev;
void mymemset(void *s,uint8_t c,uint32_t count);
void mymemcpy(void *des,void *src,uint32_t n);
void my_mem_init(uint8_t memx);
uint32_t my_mem_malloc(uint8_t memx,uint32_t size);
uint8_t my_mem_free(uint8_t memx,uint32_t offset);
uint8_t my_mem_perused(uint8_t memx);
void myfree(uint8_t memx,void *ptr);
void *mymalloc(uint8_t memx,uint32_t size);
void *myrealloc(uint8_t memx,void *ptr,uint32_t size);
void *board_malloc(uint32_t size);
void board_free(void *ptr);
void *board_realloc(void *ptr, uint32_t size);
void *board_calloc(uint32_t size);
#endif