From 395cb4489c79f83cee02c1a578aebfa2c62058c7 Mon Sep 17 00:00:00 2001 From: JiXieShi Date: Mon, 20 May 2024 21:53:19 +0800 Subject: [PATCH] UP --- CMakeLists.txt | 5 +- demo/iic/test.c | 10 ++- demo/spi/test.c | 8 +-- lib/CMakeLists.txt | 0 lib/HW_Lib.h | 13 ++++ lib/HW_Lib_conf.h | 8 +++ lib/inc/arg/argpase.h | 8 ++- lib/inc/iic/iic.h | 8 ++- lib/inc/spi/spi.h | 8 ++- lib/inc/target.h | 4 +- lib/inc/task/task.h | 7 +- lib/inc/tool.h | 88 ++++++++++++++++++++++++-- lib/src/arg/{argpase.c => argpase.cpp} | 0 lib/src/iic/{iic.c => iic.cpp} | 0 lib/src/spi/{spi.c => spi.cpp} | 8 +-- lib/src/target.c | 4 +- lib/src/task/{task.c => task.cpp} | 0 lib/src/tool.c | 31 --------- lib/src/tool.cpp | 45 +++++++++++++ main.c | 12 ++++ 20 files changed, 207 insertions(+), 60 deletions(-) create mode 100644 lib/CMakeLists.txt create mode 100644 lib/HW_Lib.h create mode 100644 lib/HW_Lib_conf.h rename lib/src/arg/{argpase.c => argpase.cpp} (100%) rename lib/src/iic/{iic.c => iic.cpp} (100%) rename lib/src/spi/{spi.c => spi.cpp} (98%) rename lib/src/task/{task.c => task.cpp} (100%) delete mode 100644 lib/src/tool.c create mode 100644 lib/src/tool.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 03956f7..838de4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,9 @@ cmake_minimum_required(VERSION 3.27) -project(HW_Lib C) +project(HW_Lib CXX C) set(CMAKE_C_STANDARD 23) - +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") #设置c++的编译选项 +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") #设置c的编译选项 include_directories( lib/inc ) diff --git a/demo/iic/test.c b/demo/iic/test.c index 3e7ed05..0110bf5 100644 --- a/demo/iic/test.c +++ b/demo/iic/test.c @@ -41,11 +41,15 @@ void Test_iic() { uint32_t len = 64; uint8_t writeData[len]; uint8_t readData[len]; - for (int i = 0; i < len; ++i) { + for + range(i, len) + { writeData[i] = rand() % 200; } + PRINT_ARRAY(writeData, "%3d", 16); SW_IIC_WL(dev, internalAddress, writeData, len); - BufPrint(" TX", writeData, 8, len, 16); +// BufPrint(" TX", writeData, TYPE_T(writeData), len, 16); + Buf_Print(" TX", writeData, len, 16); SW_IIC_RL(dev, internalAddress, readData, len, 1); - BufPrint(" RX", readData, 8, len, 16); + BufPrint(" RX", readData, TYPE_T(readData), len, 16); } \ No newline at end of file diff --git a/demo/spi/test.c b/demo/spi/test.c index 86cccae..e94b217 100644 --- a/demo/spi/test.c +++ b/demo/spi/test.c @@ -50,13 +50,13 @@ void Test_spi() { tbuf[i] = rand() % 10; } SW_SPI_RWL(ltl, rbuf, tbuf, 64); - BufPrint(" TX", tbuf, 8, 64, 8); - BufPrint(" RX", rbuf, 8, 64, 8); + BufPrint(" TX", tbuf, TYPE_T(tbuf), 64, 8); + BufPrint(" RX", rbuf, TYPE_T(rbuf), 64, 8); uint16_t rbuf16[64], tbuf16[64]; for (int i = 0; i < 64; ++i) { tbuf16[i] = rand() % 1000; } SW_SPI_RWL16(ltl, rbuf16, tbuf16, 64); - BufPrint(" TX[16]", tbuf16, 16, 64, 16); - BufPrint(" RX[16]", rbuf16, 16, 64, 16); + BufPrint(" TX[16]", tbuf16, TYPE_T(tbuf16), 64, 16); + BufPrint(" RX[16]", rbuf16, TYPE_T(rbuf16), 64, 16); } \ No newline at end of file diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/lib/HW_Lib.h b/lib/HW_Lib.h new file mode 100644 index 0000000..489093e --- /dev/null +++ b/lib/HW_Lib.h @@ -0,0 +1,13 @@ +// +// Created by lydxh on 2024/5/20. +// + +#ifndef HW_LIB_HW_LIB_H +#define HW_LIB_HW_LIB_H + +#ifdef SW_SPI +#include "" +#endif + + +#endif //HW_LIB_HW_LIB_H diff --git a/lib/HW_Lib_conf.h b/lib/HW_Lib_conf.h new file mode 100644 index 0000000..a559eb1 --- /dev/null +++ b/lib/HW_Lib_conf.h @@ -0,0 +1,8 @@ +// +// Created by lydxh on 2024/5/20. +// + +#ifndef HW_LIB_CONF_H +#define HW_LIB_CONF_H + +#endif //HW_LIB_CONF_H diff --git a/lib/inc/arg/argpase.h b/lib/inc/arg/argpase.h index 05d1d06..3c37fb1 100644 --- a/lib/inc/arg/argpase.h +++ b/lib/inc/arg/argpase.h @@ -1,6 +1,8 @@ #ifndef ARGPASE_H #define ARGPASE_H - +#ifdef __cplusplus +extern "C" { +#endif typedef char OptId; typedef struct Option { @@ -37,5 +39,7 @@ OptId Options_Load(OptList *opts, char *argv[], size_t argc); //字符串转参数数组 size_t Str_To_Args(char *str, char *argv[]); - +#ifdef __cplusplus +} +#endif #endif //ARGPASE_H diff --git a/lib/inc/iic/iic.h b/lib/inc/iic/iic.h index 07609f3..6c67f49 100644 --- a/lib/inc/iic/iic.h +++ b/lib/inc/iic/iic.h @@ -1,6 +1,8 @@ #ifndef SW_LIB_IIC_H #define SW_LIB_IIC_H - +#ifdef __cplusplus +extern "C" { +#endif #include #define HIGH 1 @@ -41,5 +43,7 @@ void SW_IIC_WL(SW_Dev_IIC Dev, uint8_t InternalAddress, uint8_t *Write_Data, uin * @return void **/ void SW_IIC_RL(SW_Dev_IIC Dev, uint8_t InternalAddress, uint8_t *Read_Data, uint32_t Len, uint8_t Ack); - +#ifdef __cplusplus +} +#endif #endif //SW_LIB_IIC_H diff --git a/lib/inc/spi/spi.h b/lib/inc/spi/spi.h index 3ed1287..ae24f84 100644 --- a/lib/inc/spi/spi.h +++ b/lib/inc/spi/spi.h @@ -1,6 +1,8 @@ #ifndef SW_LIB_SPI_H #define SW_LIB_SPI_H - +#ifdef __cplusplus +extern "C" { +#endif #include #define HIGH 1 @@ -111,5 +113,7 @@ void SW_SPI_WL16(SW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len); * @return void **/ void SW_SPI_RWL16(SW_Dev_Spi Dev, uint16_t *Read_Date, uint16_t *Write_Date, uint32_t Len); - +#ifdef __cplusplus +} +#endif #endif //SW_LIB_SPI_H diff --git a/lib/inc/target.h b/lib/inc/target.h index d3a40fd..c5bbadc 100644 --- a/lib/inc/target.h +++ b/lib/inc/target.h @@ -4,10 +4,12 @@ #include #include + +#ifdef HAL void RetargetInit(UART_HandleTypeDef *huart); int _write(int fd, char *ptr, int len); int _read(int fd, char *ptr, int len); - +#endif #endif //#ifndef RETARGET_H \ No newline at end of file diff --git a/lib/inc/task/task.h b/lib/inc/task/task.h index d714aba..e0fadcc 100644 --- a/lib/inc/task/task.h +++ b/lib/inc/task/task.h @@ -1,6 +1,9 @@ #ifndef TASK_H #define TASK_H +#ifdef __cplusplus +extern "C" { +#endif #include "stdint.h" #define true 0 @@ -56,5 +59,7 @@ TaskStatus_t TaskSetTime(Task_t *task, TaskTime_t time); //任务调度 TaskStatus_t TaskRun(void); - +#ifdef __cplusplus +} +#endif #endif //TASK_H diff --git a/lib/inc/tool.h b/lib/inc/tool.h index c731b5a..561e036 100644 --- a/lib/inc/tool.h +++ b/lib/inc/tool.h @@ -1,13 +1,14 @@ #ifndef HW_LIB_TOOL_H #define HW_LIB_TOOL_H - +#ifdef __cplusplus +extern "C" { +#endif typedef unsigned int u32; typedef unsigned short u16; typedef unsigned char u8; typedef int s32; typedef short int s16; typedef char s8; - typedef union { int i; char c[4]; @@ -18,7 +19,7 @@ typedef union { u8 u8[4]; u16 u16[2]; u32 u32; -} Data32; +} Data32_t; typedef union { char c[2]; @@ -26,7 +27,52 @@ typedef union { s16 s16; u8 u8[2]; u16 u16; -} Data16; +} Data16_t; +typedef enum { // 定义枚举类型Type_t,包含不同数据类型 + U8, // 无符号8位整数 + U16, // 无符号16位整数 + U32, // 无符号32位整数 + + CHAR, // 字符 + SHORT, // 短整数 + INT, // 整数 + + FLOAT, // 浮点数 + DOUBLE, // 双精度浮点数 +} Type_t; + +#define TYPE_T(v) _Generic((v), \ + u8 *:0,u16 *:1,u32 *:2, \ + char *:3,short *:4,int *:5, \ + float *:6,double *:10, \ + default: ((void)0)) + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) // 计算数组的元素个数 +#define in , // 定义逗号为in +#define _foreach(e, a) for(size_t e = 0; e < ARRAY_SIZE(a); e++) // 实现foreach宏,遍历数组a,e为当前元素下标 +#define foreach(exp) _foreach(exp) // 定义foreach宏,用于遍历数组 + +#define _VA_ARGS_N(_9, _8, _7, _6, _5, _4, _3, _2, _1, _0, N, ...) N +#define VA_ARGS_N(...) _VA_ARGS_N(0 __VA_OPT__(,) __VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define _range_1(var, n) (size_t var = 0; var < n; var++) // 定义范围循环,从0到n-1 +#define _range_2(var, a, b) (size_t var = a; var < b; var++) // 定义范围循环,从a到b-1 +#define _range_3(var, start, stop, step) (size_t var = start; var < stop; var += step) // 定义范围循环,从start开始,每次增加step,直到stop +#define _range_n_cat(a, b) a ## b +#define _range_n(n) _range_n_cat(_range_, n) +#define _range(...) _range_n(VA_ARGS_N(__VA_ARGS__)) // 根据传入参数个数选择对应的范围循环宏 +/** + * @Name 范围循环 + * @brief 根据传入参数个数生成不同范围的遍历 + * @param val: [输入] 循环变量 + * @param a: [输入] 0到a-1 + * @param b: [输入] a到b-1 + * @param c: [输入] a到b-1,布进c + * @retval 需求处理值 + * @example range(i, 10) → (size_t i = 0; i < 10; i++) + * @example range(c, 1, 10) → (size_t c = 1; c < 10; c++) + * @example range(n, 10, 32, 3) → (size_t n = 10; n < 32; n += 3) + **/ +#define range(var, ...) _range(__VA_ARGS__)(var, __VA_ARGS__) // 定义范围循环,传入循环变量和参数 /** * @Name Mapping @@ -41,16 +87,46 @@ typedef union { * ADC采集引脚电压 **/ float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max); + /** * @brief 数组内容HEX打印 * @param name: [输入] 标识名 * @param buf: [输入] 数组 - * @param size: [输入] 数组类型大小 [8,16,32] + * @param len: [输入] 数组长度 + * @param frame: [输入] 断帧大小 + * @return void + * @example BufPrint("TX", buf, 64, 16); //将长64的buf以16个数据断帧打印 + **/ +#define Buf_Print(n, b, l, f) BufPrint(n,b,TYPE_T(b),l,f) + +/** + * @brief 数组内容HEX打印 + * @param name: [输入] 标识名 + * @param buf: [输入] 数组 + * @param size: [输入] 数组类型 Type_t[U8,U16,U32,CHAR,SHORT,INT...] * @param len: [输入] 数组长度 * @param frame: [输入] 断帧大小 * @return void * @example BufPrint("TX", buf, 8, 64, 16); //将长64的8位buf以16个数据断帧打印 **/ -void BufPrint(char *name,void *buf,unsigned char size,unsigned int len,unsigned char frame); +void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned char frame); +/** + * @brief 数组内容打印 + * @param arr: [输入] 标识名数组 + * @param fmt: [输入] 格式化字段 + * @param frame: [输入] 断帧大小 + * @return void + * @example BufPrint("TX", buf, 8, 64, 16); //将长64的8位buf以16个数据断帧打印 + **/ +#define PRINT_ARRAY(arr, fmt, frame) do { \ + printf("\n"#arr ":\n"); \ + foreach(i in arr) {\ + if(i%frame==0&&i!=0) printf("\n");\ + printf(fmt " ", arr[i]); }\ + printf("\n"); } while (0) + +#ifdef __cplusplus +} +#endif #endif //HW_LIB_TOOL_H diff --git a/lib/src/arg/argpase.c b/lib/src/arg/argpase.cpp similarity index 100% rename from lib/src/arg/argpase.c rename to lib/src/arg/argpase.cpp diff --git a/lib/src/iic/iic.c b/lib/src/iic/iic.cpp similarity index 100% rename from lib/src/iic/iic.c rename to lib/src/iic/iic.cpp diff --git a/lib/src/spi/spi.c b/lib/src/spi/spi.cpp similarity index 98% rename from lib/src/spi/spi.c rename to lib/src/spi/spi.cpp index 43ba2c3..fadf985 100644 --- a/lib/src/spi/spi.c +++ b/lib/src/spi/spi.cpp @@ -97,7 +97,7 @@ void SW_SPI_RWL(SW_Dev_Spi Dev, uint8_t *Read_Date, uint8_t *Write_Date, uint32_ } uint16_t SW_SPI_RW16(SW_Dev_Spi Dev, uint16_t Write_Date) { - Data16 wdate16, rdate16; + Data16_t wdate16, rdate16; wdate16.u16 = Write_Date; CS_ENABLE(); if (Dev.ENDIAN) { @@ -112,7 +112,7 @@ uint16_t SW_SPI_RW16(SW_Dev_Spi Dev, uint16_t Write_Date) { } void SW_SPI_RL16(SW_Dev_Spi Dev, uint16_t *Read_Date, uint32_t Len) { - Data16 rdate16; + Data16_t rdate16; CS_ENABLE(); switch (Dev.ENDIAN) { case LTL: @@ -148,7 +148,7 @@ void SW_SPI_RL16(SW_Dev_Spi Dev, uint16_t *Read_Date, uint32_t Len) { } void SW_SPI_WL16(SW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len) { - Data16 wdate16; + Data16_t wdate16; CS_ENABLE(); switch (Dev.ENDIAN) { case LTL: @@ -184,7 +184,7 @@ void SW_SPI_WL16(SW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len) { } void SW_SPI_RWL16(SW_Dev_Spi Dev, uint16_t *Read_Date, uint16_t *Write_Date, uint32_t Len) { - Data16 wdate16, rdate16; + Data16_t wdate16, rdate16; CS_ENABLE(); switch (Dev.ENDIAN) { case LTL: diff --git a/lib/src/target.c b/lib/src/target.c index 5fc0299..08aba4f 100644 --- a/lib/src/target.c +++ b/lib/src/target.c @@ -8,7 +8,7 @@ #define STDIN_FILENO 0 #define STDOUT_FILENO 1 #define STDERR_FILENO 2 - +#ifdef HAL UART_HandleTypeDef *gHuart; void RetargetInit(UART_HandleTypeDef *huart) { @@ -45,5 +45,5 @@ int _read(int fd, char *ptr, int len) { errno = EBADF; return -1; } - +#endif #endif //#if !defined(OS_USE_SEMIHOSTING) \ No newline at end of file diff --git a/lib/src/task/task.c b/lib/src/task/task.cpp similarity index 100% rename from lib/src/task/task.c rename to lib/src/task/task.cpp diff --git a/lib/src/tool.c b/lib/src/tool.c deleted file mode 100644 index 6dc5d5b..0000000 --- a/lib/src/tool.c +++ /dev/null @@ -1,31 +0,0 @@ -// -// Created by lydxh on 2024/5/9. -// -#include -#include "tool.h" - -float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max) -{ - return(((val-I_Min)*((O_Max-O_Min)/(I_Max-I_Min)))+O_Min); -} - -void BufPrint(char *name,void *buf,unsigned char size,unsigned int len,unsigned char frame){ - printf("\n%s:\n",name); - for (int i = 0; i < len; ++i) { - if(i%frame==0&&i!=0) printf("\n"); - switch (size) { - case 8: - printf("%02x ",*((unsigned char*)buf + i)); - break; - case 16: - printf("%04x ",*((unsigned short *)buf + i)); - break; - case 32: - printf("%08x ",*((unsigned int *)buf + i)); - break; - default: - printf("未指定类型大小"); - } - } - printf("\n"); -} \ No newline at end of file diff --git a/lib/src/tool.cpp b/lib/src/tool.cpp new file mode 100644 index 0000000..0b4023f --- /dev/null +++ b/lib/src/tool.cpp @@ -0,0 +1,45 @@ +// +// Created by lydxh on 2024/5/9. +// +#include +#include "tool.h" + +float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max) { + return (((val - I_Min) * ((O_Max - O_Min) / (I_Max - I_Min))) + O_Min); +} + +void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned char frame) { + printf("\n%s:\n", name); + for (int i = 0; i < len; ++i) { + if (i % frame == 0 && i != 0) printf("\n"); + switch (type) { + case U8: + printf("%02X ", *((unsigned char *) buf + i)); + break; + case U16: + printf("%04X ", *((unsigned short *) buf + i)); + break; + case U32: + printf("%08X ", *((unsigned int *) buf + i)); + break; + case CHAR: + printf("%c ", *((char *) buf + i)); + break; + case SHORT: + printf("%d ", *((short *) buf + i)); + break; + case INT: + printf("%d ", *((int *) buf + i)); + break; + case FLOAT: + printf("%0.2f ", *((float *) buf + i)); + break; + case DOUBLE: + printf("%0.2f ", *((double *) buf + i)); + break; + default: + printf("未指定类型大小:%p", buf); + } + } + printf("\n"); +} \ No newline at end of file diff --git a/main.c b/main.c index 2700d0d..9d6bc92 100644 --- a/main.c +++ b/main.c @@ -5,10 +5,22 @@ #include "t_iic.h" #include "t_task.h" #include "t_arg.h" +#include void Test(char *name, void (*pFunction)()) { + clock_t start, end; + double cpu_time_used; + printf("\n------< %s TEST >------\n", name); + + start = clock(); pFunction(); + end = clock(); + + cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; + + printf("\nTime taken by %s: %f seconds\n", name, cpu_time_used); + printf("\n------< %s END >------\n", name); }