-
Notifications
You must be signed in to change notification settings - Fork 28
/
DEV_Config.c
376 lines (331 loc) · 9.18 KB
/
DEV_Config.c
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
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
*----------------
* | This version: V2.0
* | Date : 2019-07-08
* | Info : Basic version
*
******************************************************************************/
#include "lidarbot_base/DEV_Config.h"
#include <unistd.h>
#include <fcntl.h>
uint32_t fd;
int INT_PIN;
/******************************************************************************
function: Equipment Testing
parameter:
Info: Only supports Jetson Nano and Raspberry Pi
******************************************************************************/
static int DEV_Equipment_Testing(void)
{
int i;
int fd;
char value_str[20];
fd = open("/etc/issue", O_RDONLY);
printf("Current environment: ");
while(1) {
if (fd < 0) {
return -1;
}
for(i=0;; i++) {
if (read(fd, &value_str[i], 1) < 0) {
return -1;
}
if(value_str[i] ==32) {
printf("\r\n");
break;
}
printf("%c",value_str[i]);
}
break;
}
if(i<5) {
printf("Unrecognizable\r\n");
return -1;
} else {
char RPI_System[10] = {"Raspbian"};
for(i=0; i<6; i++) {
if(RPI_System[i] != value_str[i]) {
#if USE_DEV_LIB
return 'J';
#endif
return -1;
}
}
return 'R';
}
return -1;
}
/******************************************************************************
function: GPIO Function initialization and transfer
parameter:
Info:
******************************************************************************/
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
{
/*
0: INPT
1: OUTP
*/
#ifdef USE_BCM2835_LIB
if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT){
bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
}else {
bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
}
#elif USE_WIRINGPI_LIB
if(Mode == 0 || Mode == INPUT){
pinMode(Pin, INPUT);
pullUpDnControl(Pin, PUD_UP);
}else{
pinMode(Pin, OUTPUT);
}
#elif USE_DEV_LIB
SYSFS_GPIO_Export(Pin);
if(Mode == 0 || Mode == SYSFS_GPIO_IN){
SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_IN);
}else{
SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_OUT);
}
#endif
}
void DEV_Digital_Write(UWORD Pin, UBYTE Value)
{
#ifdef USE_BCM2835_LIB
bcm2835_gpio_write(Pin, Value);
#elif USE_WIRINGPI_LIB
digitalWrite(Pin, Value);
#elif USE_DEV_LIB
SYSFS_GPIO_Write(Pin, Value);
#endif
}
UBYTE DEV_Digital_Read(UWORD Pin)
{
UBYTE Read_value = 0;
#ifdef USE_BCM2835_LIB
Read_value = bcm2835_gpio_lev(Pin);
#elif USE_WIRINGPI_LIB
Read_value = digitalRead(Pin);
#elif USE_DEV_LIB
Read_value = SYSFS_GPIO_Read(Pin);
#endif
return Read_value;
}
/**
* delay x ms
**/
void DEV_Delay_ms(UDOUBLE xms)
{
#ifdef USE_BCM2835_LIB
bcm2835_delay(xms);
#elif USE_WIRINGPI_LIB
delay(xms);
#elif USE_DEV_LIB
UDOUBLE i;
for(i=0; i < xms; i++){
usleep(1000);
}
#endif
}
void GPIO_Config(void)
{
int Equipment = DEV_Equipment_Testing();
if(Equipment=='R'){
INT_PIN = 4;
}else if(Equipment=='J'){
#if USE_DEV_LIB
INT_PIN = GPIO4;
#endif
}else{
printf("Device read failed or unrecognized!!!\r\n");
while(1);
}
DEV_GPIO_Mode(INT_PIN, 0);
}
/******************************************************************************
function: SPI Function initialization and transfer
parameter:
Info:
******************************************************************************/
void DEV_SPI_Init()
{
#if DEV_SPI
#ifdef USE_BCM2835_LIB
printf("BCM2835 SPI Device\r\n");
bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //spi mode 0
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); //Frequency
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
#elif USE_WIRINGPI_LIB
printf("WIRINGPI SPI Device\r\n");
//wiringPiSPISetup(0,9000000);
wiringPiSPISetupMode(0, 9000000, 0);
#elif USE_DEV_LIB
printf("DEV SPI Device\r\n");
DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
#endif
#endif
}
void DEV_SPI_WriteByte(uint8_t Value)
{
#if DEV_SPI
#ifdef USE_BCM2835_LIB
bcm2835_spi_transfer(Value);
#elif USE_WIRINGPI_LIB
wiringPiSPIDataRW(0,&Value,1);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_TransferByte(Value);
#endif
#endif
}
void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
{
#if DEV_SPI
#ifdef USE_BCM2835_LIB
uint8_t rData[Len];
bcm2835_spi_transfernb(pData,rData,Len);
#elif USE_WIRINGPI_LIB
wiringPiSPIDataRW(0, pData, Len);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_Transfer(pData, Len);
#endif
#endif
}
/******************************************************************************
function: I2C Function initialization and transfer
parameter:
Info:
******************************************************************************/
void DEV_I2C_Init(uint8_t Add)
{
#if DEV_I2C
#ifdef USE_BCM2835_LIB
printf("BCM2835 I2C Device\r\n");
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(Add);
#elif USE_WIRINGPI_LIB
printf("WIRINGPI I2C Device\r\n");
fd = wiringPiI2CSetup(Add);
#elif USE_DEV_LIB
// printf("DEV I2C Device\r\n");
DEV_HARDWARE_I2C_begin("/dev/i2c-1");
DEV_HARDWARE_I2C_setSlaveAddress(Add);
#endif
#endif
}
void I2C_Write_Byte(uint8_t Cmd, uint8_t value)
{
int ref;
#if DEV_I2C
#ifdef USE_BCM2835_LIB
char wbuf[2]={Cmd, value};
bcm2835_i2c_write(wbuf, 2);
#elif USE_WIRINGPI_LIB
//wiringPiI2CWrite(fd,Cmd);
ref = wiringPiI2CWriteReg8(fd, (int)Cmd, (int)value);
while(ref != 0) {
ref = wiringPiI2CWriteReg8 (fd, (int)Cmd, (int)value);
if(ref == 0)
break;
}
#elif USE_DEV_LIB
char wbuf[2]={Cmd, value};
DEV_HARDWARE_I2C_write(wbuf, 2);
#endif
#endif
}
int I2C_Read_Byte(uint8_t Cmd)
{
int ref;
#if DEV_I2C
#ifdef USE_BCM2835_LIB
char rbuf[2]={0};
bcm2835_i2c_read_register_rs(&Cmd, rbuf, 1);
ref = rbuf[0];
#elif USE_WIRINGPI_LIB
ref = wiringPiI2CReadReg8 (fd, (int)Cmd);
#elif USE_DEV_LIB
char rbuf[2]={0};
DEV_HARDWARE_I2C_read(Cmd, rbuf, 1);
ref = rbuf[0];
#endif
#endif
return ref;
}
int I2C_Read_Word(uint8_t Cmd)
{
int ref;
#if DEV_I2C
#ifdef USE_BCM2835_LIB
char rbuf[2] = {0};
bcm2835_i2c_read_register_rs(&Cmd, rbuf, 2);
ref = rbuf[1]<<8 | rbuf[0];
#elif USE_WIRINGPI_LIB
ref = wiringPiI2CReadReg16 (fd, (int)Cmd);
#elif USE_DEV_LIB
char rbuf[2] = {0};
DEV_HARDWARE_I2C_read(Cmd, rbuf, 2);
ref = rbuf[1]<<8 | rbuf[0];
#endif
#endif
return ref;
}
/******************************************************************************
function: Module Initialize, the library and initialize the pins, SPI protocol
parameter:
Info:
******************************************************************************/
UBYTE DEV_ModuleInit(void)
{
#ifdef USE_BCM2835_LIB
if(!bcm2835_init()) {
printf("bcm2835 init failed !!! \r\n");
return 1;
} else {
printf("bcm2835 init success !!! \r\n");
}
#elif USE_WIRINGPI_LIB
//if(wiringPiSetup() < 0)//use wiringpi Pin number table
if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
printf("set wiringPi lib failed !!! \r\n");
return 1;
} else {
printf("set wiringPi lib success !!! \r\n");
}
#elif USE_DEV_LIB
printf("USE_DEV_LIB \r\n");
#endif
GPIO_Config();
DEV_I2C_Init(0x29);
return 0;
}
/******************************************************************************
function: Module exits, closes SPI and BCM2835 library
parameter:
Info:
******************************************************************************/
void DEV_ModuleExit(void)
{
#ifdef USE_BCM2835_LIB
#if DEV_I2C
bcm2835_i2c_end();
#endif
#if DEV_SPI
bcm2835_spi_end();
#endif
bcm2835_close();
#elif USE_WIRINGPI_LIB
#elif USE_DEV_LIB
#if DEV_I2C
DEV_HARDWARE_I2C_end();
#endif
#if DEV_SPI
DEV_HARDWARE_SPI_end();
#endif
#endif
}