-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio_pin.c
More file actions
531 lines (471 loc) · 12.4 KB
/
gpio_pin.c
File metadata and controls
531 lines (471 loc) · 12.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*********************************************************************
* Filename: gpio_pin.c
*
* Description:
*
* Author: wangzw <wangzw@yuettak.com>
* Created at: 2013-04-22
*
* Modify:
*
* 2013-04-25 Bright Pan <loststriker@gmail.com>
* 2013-04-27 Bright Pan <loststriker@gmail.com>
*
* Copyright (C) 2013 Yuettak Co.,Ltd
********************************************************************/
#include "gpio_pin.h"
struct gpio_pin_user_data
{
const char name[RT_NAME_MAX];
GPIO_TypeDef* gpiox;//port
rt_uint16_t gpio_pinx;//pin
GPIOMode_TypeDef gpio_mode;//mode
GPIOSpeed_TypeDef gpio_speed;//speed
rt_uint32_t gpio_clock;//clock
rt_uint8_t gpio_default_output;
};
/*
* gpio pin ops configure
*/
rt_err_t gpio_pin_configure(gpio_device *gpio)
{
GPIO_InitTypeDef gpio_init_structure;
struct gpio_pin_user_data *user = (struct gpio_pin_user_data*)gpio->parent.user_data;
GPIO_StructInit(&gpio_init_structure);
RCC_APB2PeriphClockCmd(user->gpio_clock,ENABLE);
gpio_init_structure.GPIO_Mode = user->gpio_mode;
gpio_init_structure.GPIO_Pin = user->gpio_pinx;
gpio_init_structure.GPIO_Speed = user->gpio_speed;
if (user->gpio_default_output)
{
GPIO_SetBits(user->gpiox,user->gpio_pinx);
}
else
{
GPIO_ResetBits(user->gpiox,user->gpio_pinx);
}
GPIO_Init(user->gpiox,&gpio_init_structure);
return RT_EOK;
}
rt_err_t gpio_pin_control(gpio_device *gpio, rt_uint8_t cmd, void *arg)
{
GPIO_InitTypeDef gpio_struct;
GPIOMode_TypeDef gpio_mode = *(GPIOMode_TypeDef*)arg;
struct gpio_pin_user_data *user = (struct gpio_pin_user_data*)gpio->parent.user_data;
switch(cmd)
{
case GPIO_CMD_INIT_CONFIG://config gpio pin
{
gpio_struct.GPIO_Mode = gpio_mode;
gpio_struct.GPIO_Pin = user->gpio_pinx;
gpio_struct.GPIO_Speed = user->gpio_speed;
GPIO_Init(user->gpiox,&gpio_struct);
/* if gpio is output mode */
if((gpio_mode != GPIO_Mode_IN_FLOATING)&&
(gpio_mode != GPIO_Mode_IPD)&&
(gpio_mode != GPIO_Mode_IPU))
{
gpio->parent.flag &= ~RT_DEVICE_FLAG_RDONLY;
gpio->parent.flag |= RT_DEVICE_FLAG_WRONLY;
}
else
{
gpio->parent.flag &= ~RT_DEVICE_FLAG_WRONLY;
gpio->parent.flag |= RT_DEVICE_FLAG_RDONLY;
}
break;
}
default:
{
break;
}
}
return RT_EOK;
}
void gpio_pin_out(gpio_device *gpio, rt_uint8_t data)
{
struct gpio_pin_user_data *user = (struct gpio_pin_user_data*)gpio->parent.user_data;
if (gpio->parent.flag & RT_DEVICE_FLAG_WRONLY)
{
if (data)
{
GPIO_SetBits(user->gpiox,user->gpio_pinx);
}
else
{
GPIO_ResetBits(user->gpiox,user->gpio_pinx);
}
}
else
{
#ifdef RT_USING_FINSH
rt_kprintf("gpio pin device <%s> is can`t write! please check device flag RT_DEVICE_FLAG_WRONLY\n", user->name);
#endif
}
}
rt_uint8_t gpio_pin_intput(gpio_device *gpio)
{
struct gpio_pin_user_data* user = (struct gpio_pin_user_data *)gpio->parent.user_data;
if (gpio->parent.flag & RT_DEVICE_FLAG_RDONLY)
{
return GPIO_ReadInputDataBit(user->gpiox,user->gpio_pinx);
}
else
{
#ifdef RT_USING_FINSH
rt_kprintf("gpio pin device <%s> is can`t read! please check device flag RT_DEVICE_FLAG_RDONLY\n", user->name);
#endif
return 0;
}
}
struct rt_gpio_ops gpio_pin_user_ops=
{
gpio_pin_configure,
gpio_pin_control,
gpio_pin_out,
gpio_pin_intput
};
/**************************************************
* gpio pin device register
*
**************************************************/
/* RFID power device register */
struct gpio_pin_user_data rfid_power_user_data =
{
DEVICE_NAME_RFID_POWER,
GPIOE,
GPIO_Pin_11,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
0,//default value
};
gpio_device rfid_power_device;
void rt_hw_rfid_power_register(void)
{
gpio_device *gpio_device = &rfid_power_device;
struct gpio_pin_user_data *gpio_user_data = &rfid_power_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* camera led device */
struct gpio_pin_user_data camera_led_user_data =
{
DEVICE_NAME_CAMERA_LED,
GPIOE,
GPIO_Pin_9,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
0,
};
gpio_device camera_led_device;
void rt_hw_camera_led_register(void)
{
gpio_device *gpio_device = &camera_led_device;
struct gpio_pin_user_data *gpio_user_data = &camera_led_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* camera power device */
struct gpio_pin_user_data camera_power_user_data =
{
DEVICE_NAME_CAMERA_POWER,
GPIOE,
GPIO_Pin_12,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
1,
};
gpio_device camera_power_device;
void rt_hw_camera_power_register(void)
{
gpio_device *gpio_device = &camera_power_device;
struct gpio_pin_user_data *gpio_user_data = &camera_power_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* logo led device */
struct gpio_pin_user_data logo_led_user_data =
{
DEVICE_NAME_LOGO_LED,
GPIOE,
GPIO_Pin_10,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
0,
};
gpio_device logo_led_device;
void rt_hw_logo_led_register(void)
{
gpio_device *gpio_device = &logo_led_device;
struct gpio_pin_user_data *gpio_user_data = &logo_led_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* gsm led device */
struct gpio_pin_user_data gsm_led_user_data =
{
DEVICE_NAME_GSM_LED,
GPIOE,
GPIO_Pin_13,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
0,
};
gpio_device gsm_led_device;
void rt_hw_gsm_led_register(void)
{
gpio_device *gpio_device = &gsm_led_device;
struct gpio_pin_user_data *gpio_user_data = &gsm_led_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* gsm power device */
struct gpio_pin_user_data gsm_power_user_data =
{
DEVICE_NAME_GSM_POWER,
GPIOD,
GPIO_Pin_0,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOD,
0,
};
gpio_device gsm_power_device;
void rt_hw_gsm_power_register(void)
{
gpio_device *gpio_device = &gsm_power_device;
struct gpio_pin_user_data *gpio_user_data = &gsm_power_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* gsm dtr device */
struct gpio_pin_user_data gsm_dtr_user_data =
{
DEVICE_NAME_GSM_DTR,
GPIOC,
GPIO_Pin_1,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOC,
1,
};
gpio_device gsm_dtr_device;
void rt_hw_gsm_dtr_register(void)
{
gpio_device *gpio_device = &gsm_dtr_device;
struct gpio_pin_user_data *gpio_user_data = &gsm_dtr_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* gsm status device */
struct gpio_pin_user_data gsm_status_user_data =
{
DEVICE_NAME_GSM_STATUS,
GPIOD,
GPIO_Pin_7,
GPIO_Mode_IN_FLOATING,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOE,
0,
};
gpio_device gsm_status_device;
void rt_hw_gsm_status_register(void)
{
gpio_device *gpio_device = &gsm_status_device;
struct gpio_pin_user_data *gpio_user_data = &gsm_status_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* voice reset device
struct gpio_pin_user_data voice_reset_user_data =
{
DEVICE_NAME_VOICE_RESET,
GPIOA,
GPIO_Pin_12,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOA,
0,
};
gpio_device voice_reset_device;
void rt_hw_voice_reset_register(void)
{
gpio_device *gpio_device = &voice_reset_device;
struct gpio_pin_user_data *gpio_user_data = &voice_reset_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
*/
/* voice switch device */
struct gpio_pin_user_data voice_switch_user_data =
{
DEVICE_NAME_VOICE_SWITCH,
GPIOC,
GPIO_Pin_7,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOC,
1,
};
gpio_device voice_switch_device;
void rt_hw_voice_switch_register(void)
{
gpio_device *gpio_device = &voice_switch_device;
struct gpio_pin_user_data *gpio_user_data = &voice_switch_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* voice amp device */
struct gpio_pin_user_data voice_amp_user_data =
{
DEVICE_NAME_VOICE_AMP,
GPIOC,
GPIO_Pin_6,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOC,
0,
};
gpio_device voice_amp_device;
void rt_hw_voice_amp_register(void)
{
gpio_device *gpio_device = &voice_amp_device;
struct gpio_pin_user_data *gpio_user_data = &voice_amp_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* gsm led device */
struct gpio_pin_user_data test_user_data =
{
"test",
GPIOC,
GPIO_Pin_8,
GPIO_Mode_Out_PP,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOC,
1,
};
gpio_device test_device;
void rt_hw_test_register(void)
{
gpio_device *gpio_device = &test_device;
struct gpio_pin_user_data *gpio_user_data = &test_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* motor status device */
gpio_device motor_status_device;
struct gpio_pin_user_data motor_status_user_data =
{
DEVICE_NAME_MOTOR_STATUS,
GPIOD,
GPIO_Pin_8,
GPIO_Mode_IPD,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOD |RCC_APB2Periph_AFIO,
1
};
void rt_hw_motor_status_register(void)
{
gpio_device *gpio_device = &motor_status_device;
struct gpio_pin_user_data *gpio_user_data = &motor_status_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* camera photosensor device */
gpio_device camera_photosensor_device;
struct gpio_pin_user_data camera_photosensor_user_data =
{
DEVICE_NAME_CAMERA_PHOTOSENSOR,
GPIOB,
GPIO_Pin_0,
GPIO_Mode_IPD,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,
1
};
void rt_hw_camera_photosensor_register(void)
{
gpio_device *gpio_device = &camera_photosensor_device;
struct gpio_pin_user_data *gpio_user_data = &camera_photosensor_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
/* camera usart tx pin input */
gpio_device camera_TX_device;
struct gpio_pin_user_data camera_TX_user_data =
{
DEVICE_NAME_CAMERA_USART_TX,
GPIOC,
GPIO_Pin_12,
GPIO_Mode_IN_FLOATING,
GPIO_Speed_50MHz,
RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,
0
};
void rt_hw_camera_usart_tx(void)
{
gpio_device *gpio_device = &camera_TX_device;
struct gpio_pin_user_data *gpio_user_data = &camera_TX_user_data;
gpio_device->ops = &gpio_pin_user_ops;
rt_hw_gpio_register(gpio_device, gpio_user_data->name, (RT_DEVICE_FLAG_RDWR), gpio_user_data);
}
void gpio_pin_output(char *str, const rt_uint8_t dat)
{
rt_device_t device = RT_NULL;
device = rt_device_find(str);
if (device != RT_NULL)
{
rt_device_write(device,0,&dat,0);
}
else
{
#ifdef RT_USING_FINSH
rt_kprintf("the gpio device %s is not found!\n", str);
#endif
}
}
uint8_t gpio_pin_input(char *str)
{
rt_device_t device = RT_NULL;
rt_uint8_t dat;
device = rt_device_find(str);
if (device != RT_NULL)
{
rt_device_read(device,0,&dat,0);
rt_kprintf("the gpio pin device <%s> value is %d\n", str, dat);
}
else
{
#ifdef RT_USING_FINSH
rt_kprintf("the gpio device %s is not found!\n", str);
#endif
}
return dat;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void led(const char *str, const rt_uint8_t dat)
{
rt_device_t led = RT_NULL;
led = rt_device_find(str);
if (led != RT_NULL)
{
rt_device_write(led,0,&dat,0);
}
else
{
#ifdef RT_USING_FINSH
rt_kprintf("the led device <%s>is not found!\n", str);
#endif
}
}
FINSH_FUNCTION_EXPORT(led, led[device_name 0/1])
FINSH_FUNCTION_EXPORT(gpio_pin_output, [device_name <0 1>])
FINSH_FUNCTION_EXPORT(gpio_pin_input, [device_name result])
#endif