Repository navigation
Expand file tree
/
Copy pathplatform_dev.h
More file actions
206 lines (175 loc) · 7.4 KB
/
Copy pathplatform_dev.h
File metadata and controls
206 lines (175 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @file platform_dev.h
* @author Luyi365 (luyi.hub@foxmail.com)
* @brief 模仿 Linux 的简化版驱动框架
* @date 2026-10-21
*
* @copyright Copyright (c) 2026
*
*/
#ifndef _PLATFORM_DEV_H_
#define _PLATFORM_DEV_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/****************************************设计说明****************************************
* 资源限制使用简化版驱动框架,设计中不使用动态内存分配。因为平台驱动都是固定的,所以记录都保存在flash里对应的section里。
* 框架说明:应用->芯片驱动->平台总线驱动
*********************************************************************************************/
extern uint32_t __start_platform_init;
extern uint32_t __stop_platform_init;
extern uint32_t __start_device_init;
extern uint32_t __stop_device_init;
#define DEV_INVAILD_OP 0xffff
/****************************************函数返回值定义****************************************/
typedef enum{
DEV_OK = 0,
DEV_ERROR,
DEV_TIMEOUT,
DEV_NOT_FOUND,
}dev_ret_code_t;
/****************************************外设事件定义****************************************/
typedef enum{
EVENT_INIT,
EVENT_TX_BUF_RDY,
EVENT_RX_BUF_RDY,
EVENT_TX_DATA_DONE,
EVENT_RX_DATA_DONE,
EVENT_HW_ERROR,
EVENT_CLOSE,
}platform_dev_event;
/****************************************通用设备操作函数****************************************/
typedef struct{
int ( *open ) (void *param);
int ( *close ) (void *param);
int ( *suspend ) (void);
int ( *resume ) (void);
int ( *write ) (uint32_t offset,const void *buffer, uint32_t size);
int ( *read ) (uint32_t offset,void *buffer, uint32_t size);
int ( *ioctrl ) (int cmd, void *param1, void *param2);
} dev_general_ops_t;
/****************************************i2c设备操作函数****************************************/
typedef struct{
int ( *open ) (void *param);
int ( *close ) (void *param);
int ( *suspend ) (void);
int ( *resume ) (void);
int ( *write ) (uint16_t dev_addr, uint16_t reg_addr,
const uint8_t *buf,uint16_t size);
int ( *read ) (uint16_t dev_addr, uint16_t reg_addr,
uint8_t *buf,uint16_t size);
} dev_i2c_ops_t;
/****************************************spi设备操作函数****************************************/
typedef struct{
int ( *open ) (void *param);
int ( *close ) (void *param);
int ( *suspend ) (void);
int ( *resume ) (void);
int ( *write ) (const void *buffer, uint32_t size);
int ( *read ) (void *buffer, uint32_t size);
int ( *write_read) (const void *wbuffer, uint32_t wsize,void *rbuffer, uint32_t rsize);
} dev_spi_ops_t;
/****************************************uart设备操作函数****************************************/
typedef int (*uart_event_handler)(platform_dev_event event);
typedef struct{
int ( *open ) (void *param);
int ( *close ) (void *param);
int ( *suspend ) (void);
int ( *resume ) (void);
int ( *write ) (const void *buffer, uint32_t size);
int ( *read ) (void *buffer, uint32_t size);
int ( *set_tx_buf) (void *buffer, uint32_t size);
int ( *set_rx_buf ) (void *buffer, uint32_t size);
int ( *set_event_handler)(uart_event_handler handler);
} dev_uart_ops_t;
/****************************************adc设备操作函数****************************************/
typedef struct{
int ( *open ) (void *param);
int ( *close ) (void *param);
int ( *suspend ) (void);
int ( *resume ) (void);
int ( *set_sample_frequency ) (uint8_t channel,uint32_t fq);
int ( *read ) (uint8_t channel,uint32_t *value);
} dev_adc_ops_t;
typedef union{
dev_general_ops_t *general_ops;
dev_i2c_ops_t *i2c_ops;
dev_spi_ops_t *spi_ops;
dev_uart_ops_t *uart_ops;
dev_adc_ops_t *adc_ops;
} platform_dev_ops_t;
typedef struct{
const char* name;
const platform_dev_ops_t ops;
}platform_init_desc;
typedef int (*device_fn_t)();
typedef void (*device_pm_fn_t)(void);
typedef struct
{
const char* name;
const device_fn_t open;
const device_fn_t close;
const device_pm_fn_t suspend;
const device_pm_fn_t resume;
}device_desc;
typedef struct
{
void *param1;
uint32_t param2;
}user_param_t;
dev_ret_code_t platform_dev_find(const char *dev_name,uint16_t *op_index);
dev_ret_code_t platform_dev_open(const char *dev_name,void *param);
dev_ret_code_t platform_dev_close(const char *dev_name,void *param);
dev_ret_code_t platform_dev_suspend(uint16_t op_index);
dev_ret_code_t platform_dev_resume(uint16_t op_index);
dev_ret_code_t platform_dev_general_write(uint16_t op_index,uint32_t offset,const void *buffer, uint32_t size);
dev_ret_code_t platform_dev_general_read(uint16_t op_index,uint32_t offset,void *buffer, uint32_t size);
dev_ret_code_t platform_dev_general_ioctrl(uint16_t op_index,int cmd, void *param1, void *param2);
dev_ret_code_t platform_dev_i2c_write(uint16_t op_index,uint16_t dev_addr, uint16_t reg_addr,
const uint8_t *buf,uint16_t size);
dev_ret_code_t platform_dev_i2c_read(uint16_t op_index,uint16_t dev_addr, uint16_t reg_addr,
uint8_t *buf,uint16_t size);
dev_ret_code_t platform_dev_spi_write(uint16_t op_index,const void *buffer, uint32_t size);
dev_ret_code_t platform_dev_spi_read(uint16_t op_index,void *buffer, uint32_t size);
dev_ret_code_t platform_dev_spi_write_read(uint16_t op_index,const void *wbuffer, uint32_t wsize,void *rbuffer, uint32_t rsize);
dev_ret_code_t platform_dev_uart_write(uint16_t op_index,const void *buffer, uint32_t size);
dev_ret_code_t platform_dev_uart_read(uint16_t op_index,void *buffer, uint32_t size);
dev_ret_code_t platform_dev_uart_set_tx_buf(uint16_t op_index,void *buffer, uint32_t size);
dev_ret_code_t platform_dev_uart_set_rx_buf(uint16_t op_index,void *buffer, uint32_t size);
dev_ret_code_t platform_dev_uart_set_evnet_handler(uint16_t op_index,uart_event_handler handler);
dev_ret_code_t platform_dev_adc_set_sample_frequency(uint16_t op_index,uint8_t channel,uint32_t fq);
dev_ret_code_t platform_dev_adc_read(uint16_t op_index,uint8_t channel,uint32_t *value);
dev_ret_code_t device_open(const char *dev_name);
dev_ret_code_t device_close(const char *dev_name);
dev_ret_code_t device_init_all(void);
void device_suspend(const char *dev_name);
void device_resume(const char *dev_name);
void device_suspend_all(void);
void device_resume_all(void);
#define DECLARE_PLATFORM_DEV(var_name) static uint16_t var_name = DEV_INVAILD_OP
#define PLATFORM_DEV_HANDLER_GET(dev_name,handler_addr) \
do{ \
if(*((uint16_t *)(handler_addr)) == DEV_INVAILD_OP){ \
platform_dev_find(dev_name,(handler_addr)); \
} \
}while(0)
#define PLATFORM_ITEM_REGISTER(var_name,dev_name,ops_fn) \
const platform_init_desc var_name __attribute__ ((section(".platform_init"))) __attribute__((used)) = \
{ \
.name = dev_name, \
.ops.general_ops = (dev_general_ops_t*)ops_fn, \
}
#define DEVICE_ITEM_REGISTER(var_name,dev_name,_open,_close,_suspend,_resume) \
const device_desc var_name __attribute__ ((section(".device_init"))) __attribute__((used)) = \
{ \
.name = dev_name, \
.open = _open, \
.close = _close, \
.suspend = _suspend, \
.resume = _resume, \
}
#ifdef __cplusplus
}
#endif
#endif