UP Task,Argpase
parent
be1d31e732
commit
8c05281f26
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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("<IIC> TX", writeData, 8, len, 16);
|
||||
// BufPrint("<IIC> TX", writeData, TYPE_T(writeData), len, 16);
|
||||
Buf_Print("<IIC> TX", writeData, len, 16);
|
||||
SW_IIC_RL(dev, internalAddress, readData, len, 1);
|
||||
BufPrint("<IIC> RX", readData, 8, len, 16);
|
||||
BufPrint("<IIC> RX", readData, TYPE_T(readData), len, 16);
|
||||
}
|
|
@ -50,13 +50,13 @@ void Test_spi() {
|
|||
tbuf[i] = rand() % 10;
|
||||
}
|
||||
SW_SPI_RWL(ltl, rbuf, tbuf, 64);
|
||||
BufPrint("<SPI> TX", tbuf, 8, 64, 8);
|
||||
BufPrint("<SPI> RX", rbuf, 8, 64, 8);
|
||||
BufPrint("<SPI> TX", tbuf, TYPE_T(tbuf), 64, 8);
|
||||
BufPrint("<SPI> 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("<SPI> TX[16]", tbuf16, 16, 64, 16);
|
||||
BufPrint("<SPI> RX[16]", rbuf16, 16, 64, 16);
|
||||
BufPrint("<SPI> TX[16]", tbuf16, TYPE_T(tbuf16), 64, 16);
|
||||
BufPrint("<SPI> RX[16]", rbuf16, TYPE_T(rbuf16), 64, 16);
|
||||
}
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef SW_LIB_IIC_H
|
||||
#define SW_LIB_IIC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef SW_LIB_SPI_H
|
||||
#define SW_LIB_SPI_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
|
|
@ -4,10 +4,12 @@
|
|||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
|
@ -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)
|
|
@ -1,31 +0,0 @@
|
|||
//
|
||||
// Created by lydxh on 2024/5/9.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#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");
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// Created by lydxh on 2024/5/9.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#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");
|
||||
}
|
12
main.c
12
main.c
|
@ -5,10 +5,22 @@
|
|||
#include "t_iic.h"
|
||||
#include "t_task.h"
|
||||
#include "t_arg.h"
|
||||
#include <time.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue