UP Task,Argpase

main
JiXieShi 2024-05-20 21:53:19 +08:00
parent be1d31e732
commit 8c05281f26
20 changed files with 207 additions and 60 deletions

View File

@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.27) cmake_minimum_required(VERSION 3.27)
project(HW_Lib C) project(HW_Lib CXX C)
set(CMAKE_C_STANDARD 23) 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( include_directories(
lib/inc lib/inc
) )

View File

@ -41,11 +41,15 @@ void Test_iic() {
uint32_t len = 64; uint32_t len = 64;
uint8_t writeData[len]; uint8_t writeData[len];
uint8_t readData[len]; uint8_t readData[len];
for (int i = 0; i < len; ++i) { for
range(i, len)
{
writeData[i] = rand() % 200; writeData[i] = rand() % 200;
} }
PRINT_ARRAY(writeData, "%3d", 16);
SW_IIC_WL(dev, internalAddress, writeData, len); 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); SW_IIC_RL(dev, internalAddress, readData, len, 1);
BufPrint("<IIC> RX", readData, 8, len, 16); BufPrint("<IIC> RX", readData, TYPE_T(readData), len, 16);
} }

View File

@ -50,13 +50,13 @@ void Test_spi() {
tbuf[i] = rand() % 10; tbuf[i] = rand() % 10;
} }
SW_SPI_RWL(ltl, rbuf, tbuf, 64); SW_SPI_RWL(ltl, rbuf, tbuf, 64);
BufPrint("<SPI> TX", tbuf, 8, 64, 8); BufPrint("<SPI> TX", tbuf, TYPE_T(tbuf), 64, 8);
BufPrint("<SPI> RX", rbuf, 8, 64, 8); BufPrint("<SPI> RX", rbuf, TYPE_T(rbuf), 64, 8);
uint16_t rbuf16[64], tbuf16[64]; uint16_t rbuf16[64], tbuf16[64];
for (int i = 0; i < 64; ++i) { for (int i = 0; i < 64; ++i) {
tbuf16[i] = rand() % 1000; tbuf16[i] = rand() % 1000;
} }
SW_SPI_RWL16(ltl, rbuf16, tbuf16, 64); SW_SPI_RWL16(ltl, rbuf16, tbuf16, 64);
BufPrint("<SPI> TX[16]", tbuf16, 16, 64, 16); BufPrint("<SPI> TX[16]", tbuf16, TYPE_T(tbuf16), 64, 16);
BufPrint("<SPI> RX[16]", rbuf16, 16, 64, 16); BufPrint("<SPI> RX[16]", rbuf16, TYPE_T(rbuf16), 64, 16);
} }

0
lib/CMakeLists.txt Normal file
View File

13
lib/HW_Lib.h Normal file
View File

@ -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

8
lib/HW_Lib_conf.h Normal file
View File

@ -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

View File

@ -1,6 +1,8 @@
#ifndef ARGPASE_H #ifndef ARGPASE_H
#define ARGPASE_H #define ARGPASE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef char OptId; typedef char OptId;
typedef struct Option { 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[]); size_t Str_To_Args(char *str, char *argv[]);
#ifdef __cplusplus
}
#endif
#endif //ARGPASE_H #endif //ARGPASE_H

View File

@ -1,6 +1,8 @@
#ifndef SW_LIB_IIC_H #ifndef SW_LIB_IIC_H
#define SW_LIB_IIC_H #define SW_LIB_IIC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h> #include <stdint.h>
#define HIGH 1 #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 * @return void
**/ **/
void SW_IIC_RL(SW_Dev_IIC Dev, uint8_t InternalAddress, uint8_t *Read_Data, uint32_t Len, uint8_t Ack); 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 #endif //SW_LIB_IIC_H

View File

@ -1,6 +1,8 @@
#ifndef SW_LIB_SPI_H #ifndef SW_LIB_SPI_H
#define SW_LIB_SPI_H #define SW_LIB_SPI_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h> #include <stdint.h>
#define HIGH 1 #define HIGH 1
@ -111,5 +113,7 @@ void SW_SPI_WL16(SW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len);
* @return void * @return void
**/ **/
void SW_SPI_RWL16(SW_Dev_Spi Dev, uint16_t *Read_Date, 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);
#ifdef __cplusplus
}
#endif
#endif //SW_LIB_SPI_H #endif //SW_LIB_SPI_H

View File

@ -4,10 +4,12 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAL
void RetargetInit(UART_HandleTypeDef *huart); void RetargetInit(UART_HandleTypeDef *huart);
int _write(int fd, char *ptr, int len); int _write(int fd, char *ptr, int len);
int _read(int fd, char *ptr, int len); int _read(int fd, char *ptr, int len);
#endif
#endif //#ifndef RETARGET_H #endif //#ifndef RETARGET_H

View File

@ -1,6 +1,9 @@
#ifndef TASK_H #ifndef TASK_H
#define TASK_H #define TASK_H
#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h" #include "stdint.h"
#define true 0 #define true 0
@ -56,5 +59,7 @@ TaskStatus_t TaskSetTime(Task_t *task, TaskTime_t time);
//任务调度 //任务调度
TaskStatus_t TaskRun(void); TaskStatus_t TaskRun(void);
#ifdef __cplusplus
}
#endif
#endif //TASK_H #endif //TASK_H

View File

@ -1,13 +1,14 @@
#ifndef HW_LIB_TOOL_H #ifndef HW_LIB_TOOL_H
#define HW_LIB_TOOL_H #define HW_LIB_TOOL_H
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned int u32; typedef unsigned int u32;
typedef unsigned short u16; typedef unsigned short u16;
typedef unsigned char u8; typedef unsigned char u8;
typedef int s32; typedef int s32;
typedef short int s16; typedef short int s16;
typedef char s8; typedef char s8;
typedef union { typedef union {
int i; int i;
char c[4]; char c[4];
@ -18,7 +19,7 @@ typedef union {
u8 u8[4]; u8 u8[4];
u16 u16[2]; u16 u16[2];
u32 u32; u32 u32;
} Data32; } Data32_t;
typedef union { typedef union {
char c[2]; char c[2];
@ -26,7 +27,52 @@ typedef union {
s16 s16; s16 s16;
u8 u8[2]; u8 u8[2];
u16 u16; 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宏遍历数组ae为当前元素下标
#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: [] 0a-1
* @param b: [] ab-1
* @param c: [] ab-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 * @Name Mapping
@ -41,16 +87,46 @@ typedef union {
* ADC * ADC
**/ **/
float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max); float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max);
/** /**
* @brief HEX * @brief HEX
* @param name: [] * @param name: []
* @param buf: [] * @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 len: []
* @param frame: [] * @param frame: []
* @return void * @return void
* @example BufPrint("TX", buf, 8, 64, 16); //将长64的8位buf以16个数据断帧打印 * @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 #endif //HW_LIB_TOOL_H

View File

@ -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) { uint16_t SW_SPI_RW16(SW_Dev_Spi Dev, uint16_t Write_Date) {
Data16 wdate16, rdate16; Data16_t wdate16, rdate16;
wdate16.u16 = Write_Date; wdate16.u16 = Write_Date;
CS_ENABLE(); CS_ENABLE();
if (Dev.ENDIAN) { 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) { void SW_SPI_RL16(SW_Dev_Spi Dev, uint16_t *Read_Date, uint32_t Len) {
Data16 rdate16; Data16_t rdate16;
CS_ENABLE(); CS_ENABLE();
switch (Dev.ENDIAN) { switch (Dev.ENDIAN) {
case LTL: 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) { void SW_SPI_WL16(SW_Dev_Spi Dev, uint16_t *Write_Date, uint32_t Len) {
Data16 wdate16; Data16_t wdate16;
CS_ENABLE(); CS_ENABLE();
switch (Dev.ENDIAN) { switch (Dev.ENDIAN) {
case LTL: 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) { 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(); CS_ENABLE();
switch (Dev.ENDIAN) { switch (Dev.ENDIAN) {
case LTL: case LTL:

View File

@ -8,7 +8,7 @@
#define STDIN_FILENO 0 #define STDIN_FILENO 0
#define STDOUT_FILENO 1 #define STDOUT_FILENO 1
#define STDERR_FILENO 2 #define STDERR_FILENO 2
#ifdef HAL
UART_HandleTypeDef *gHuart; UART_HandleTypeDef *gHuart;
void RetargetInit(UART_HandleTypeDef *huart) { void RetargetInit(UART_HandleTypeDef *huart) {
@ -45,5 +45,5 @@ int _read(int fd, char *ptr, int len) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
#endif
#endif //#if !defined(OS_USE_SEMIHOSTING) #endif //#if !defined(OS_USE_SEMIHOSTING)

View File

@ -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");
}

45
lib/src/tool.cpp Normal file
View File

@ -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
View File

@ -5,10 +5,22 @@
#include "t_iic.h" #include "t_iic.h"
#include "t_task.h" #include "t_task.h"
#include "t_arg.h" #include "t_arg.h"
#include <time.h>
void Test(char *name, void (*pFunction)()) { void Test(char *name, void (*pFunction)()) {
clock_t start, end;
double cpu_time_used;
printf("\n------< %s TEST >------\n", name); printf("\n------< %s TEST >------\n", name);
start = clock();
pFunction(); 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); printf("\n------< %s END >------\n", name);
} }