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
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-08-28 qiyu first version
*/
#include <rthw.h>
#include "drv_gpio.h"
#include "F2837xD_device.h"
#include "F28x_Project.h" // Device Headerfile and Examples Include File
#ifdef RT_USING_PIN
// the gpio pin number for each port is 32, while it is 16 for ARM
#define PIN_NUM(port, no) (((((port) & 0xFu) << 5) | ((no) & 0x1F)))
#define PIN_PORT(pin) ((rt_uint16_t)(((pin) >> 5) & 0xFu))
#define PIN_NO(pin) ((rt_uint16_t)((pin) & 0x1Fu))
#define PIN_c28x_PORT(pin) (volatile Uint32 *)&GpioDataRegs + (PIN_PORT(pin))*GPY_DATA_OFFSET
#define PIN_c28x_PIN(pin) ((rt_uint32_t)(1u << PIN_NO(pin)))
#define PIN_c28x_PORT_MAX 6 /* gpioA to GPIOF in total*/
#define PIN_IRQ_MAX 5 /* XINT1 to XINT5 in total */
static rt_err_t c28x_pin_attach_irq(struct rt_device *device, rt_base_t pin,
rt_uint8_t mode, void (*hdr)(void *args), void *args);
static rt_err_t c28x_pin_dettach_irq(struct rt_device *device, rt_base_t pin);
static rt_err_t c28x_pin_irq_enable(struct rt_device *device, rt_base_t pin,
rt_uint8_t enabled);
static rt_base_t c28x_pin_get(const char *name)
{
int hw_pin_num = 0;
int i, name_len;
name_len = rt_strlen(name);
if ((name_len < 3) || (name_len >= 7))
{
return -RT_EINVAL;
}
/*
* PX.y
*/
if ((name[0] != 'P') || (name[2] != '.'))
{
return -RT_EINVAL;
}
for (i = 3; i < name_len; i++)
{
hw_pin_num *= 10;
hw_pin_num += name[i] - '0';
}
return hw_pin_num;
}
static void c28x_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
{
volatile Uint32 *gpioDataReg;
Uint32 pinMask;
if (PIN_PORT(pin) < PIN_c28x_PORT_MAX)
{
gpioDataReg = PIN_c28x_PORT(pin);
pinMask = 1UL << (PIN_NO(pin));
if (value == 0)
{
gpioDataReg[GPYCLEAR] = pinMask;
}
else
{
gpioDataReg[GPYSET] = pinMask;
}
}
}
static rt_ssize_t c28x_pin_read(rt_device_t dev, rt_base_t pin)
{
volatile Uint32 *gpioDataReg;
rt_ssize_t value = PIN_LOW;
if (PIN_PORT(pin) < PIN_c28x_PORT_MAX)
{
gpioDataReg = PIN_c28x_PORT(pin);
value = (gpioDataReg[GPYDAT] >> PIN_NO(pin)) & 0x1;
}
return value;
}
static void c28x_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
{
volatile Uint32 *gpioBaseAddr;
volatile Uint32 *dir, *pud, *odr;
if (PIN_PORT(pin) >= PIN_c28x_PORT_MAX)
{
return;
}
rt_uint32_t pinMask;
pinMask = 1UL << PIN_NO(pin);
gpioBaseAddr = (Uint32 *)&GpioCtrlRegs + (PIN_PORT(pin))*GPY_CTRL_OFFSET;
dir = gpioBaseAddr + GPYDIR;
pud = gpioBaseAddr + GPYPUD;
odr = gpioBaseAddr + GPYODR;
EALLOW;
if (mode == PIN_MODE_OUTPUT)
{
*dir |= pinMask;
}
else if (mode == PIN_MODE_INPUT)
{
*dir &= ~pinMask;
}
else if (mode == PIN_MODE_INPUT_PULLUP)
{
*dir &= ~pinMask;
*pud &= ~pinMask;
}
else if (mode == PIN_MODE_INPUT_PULLDOWN)
{
/* input setting: pull down. */
*dir &= ~pinMask;
*pud |= pinMask;
}
else if (mode == PIN_MODE_OUTPUT_OD)
{
/* output setting: od. */
*dir |= pinMask;
*odr |= pinMask;
}
EDIS;
}
const static struct rt_pin_ops _c28x_pin_ops =
{
c28x_pin_mode,
c28x_pin_write,
c28x_pin_read,
c28x_pin_attach_irq,
c28x_pin_dettach_irq,
c28x_pin_irq_enable,
c28x_pin_get,
};
int rt_hw_pin_init(void)
{
return rt_device_pin_register("pin", &_c28x_pin_ops, RT_NULL);
}
static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
{
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
{-1, 0, RT_NULL, RT_NULL},
};
static rt_int16_t pin_irq_xint_tab[] =
{
BSP_XINT1_PIN,
BSP_XINT2_PIN,
BSP_XINT3_PIN,
BSP_XINT4_PIN,
BSP_XINT5_PIN
};
rt_inline rt_int32_t get_irq_index(rt_uint32_t pin)
{
int i;
for(i = 0 ; i < PIN_IRQ_MAX ; i++)
{
if(pin_irq_xint_tab[i] == pin)
{
return i;
}
}
return -1;
}
#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
static rt_err_t c28x_pin_attach_irq(struct rt_device *device, rt_base_t pin,
rt_uint8_t mode, void (*hdr)(void *args), void *args)
{
rt_base_t level;
rt_int32_t irqindex = -1;
if (PIN_PORT(pin) >= PIN_c28x_PORT_MAX)
{
return -RT_ENOSYS;
}
irqindex = get_irq_index(pin);
level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == pin &&
pin_irq_hdr_tab[irqindex].hdr == hdr &&
pin_irq_hdr_tab[irqindex].mode == mode &&
pin_irq_hdr_tab[irqindex].args == args)
{
rt_hw_interrupt_enable(level);
return RT_EOK;
}
if (pin_irq_hdr_tab[irqindex].pin != -1)
{
rt_hw_interrupt_enable(level);
return -RT_EBUSY;
}
pin_irq_hdr_tab[irqindex].pin = pin;
pin_irq_hdr_tab[irqindex].hdr = hdr;
pin_irq_hdr_tab[irqindex].mode = mode;
pin_irq_hdr_tab[irqindex].args = args;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
static rt_err_t c28x_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
{
rt_base_t level;
rt_int32_t irqindex = -1;
rt_uint16_t i;
if (PIN_PORT(pin) >= PIN_c28x_PORT_MAX)
{
return -RT_ENOSYS;
}
for(i = 0 ; i < PIN_IRQ_MAX ; i++)
{
if(pin_irq_hdr_tab[i].pin == pin)
{
irqindex = i;
break;
}
}
if (irqindex == -1)
{
return -RT_ENOSYS;
}
level = rt_hw_interrupt_disable();
pin_irq_hdr_tab[irqindex].pin = -1;
pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
pin_irq_hdr_tab[irqindex].mode = 0;
pin_irq_hdr_tab[irqindex].args = RT_NULL;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
static rt_err_t c28x_pin_irq_enable(struct rt_device *device, rt_base_t pin,
rt_uint8_t enabled)
{
rt_base_t level;
rt_int32_t irqindex = -1;
rt_uint16_t channel;
rt_uint16_t edge_mode,pin_mode;
if (PIN_PORT(pin) >= PIN_c28x_PORT_MAX)
{
return -RT_ENOSYS;
}
irqindex = get_irq_index(pin);
/* irqindex+1 = channel*/
if (irqindex < 0 || irqindex >= PIN_IRQ_MAX)
{
return -RT_ENOSYS;
}
if (enabled == PIN_IRQ_ENABLE)
{
level = rt_hw_interrupt_disable();
if (pin_irq_hdr_tab[irqindex].pin == -1)
{
rt_hw_interrupt_enable(level);
return -RT_ENOSYS;
}
/*
* 1. set the edge mode of interrupt triggering
* 2. set the GPIO mode
* 3. enable XINT channel and set the input source
*/
channel = irqindex+1;
switch (pin_irq_hdr_tab[irqindex].mode)
{
case PIN_IRQ_MODE_RISING:
edge_mode = 1;
pin_mode = PIN_MODE_INPUT_PULLDOWN;
break;
case PIN_IRQ_MODE_FALLING:
edge_mode = 0;
pin_mode = PIN_MODE_INPUT_PULLUP;
break;
case PIN_IRQ_MODE_RISING_FALLING:
edge_mode = 3;
pin_mode = PIN_MODE_INPUT;
break;
}
if(channel == 1)
{
XintRegs.XINT1CR.bit.ENABLE = 1; // Enable XINT1
EALLOW;
InputXbarRegs.INPUT4SELECT = pin; //Set XINT1 source to GPIO-pin
EDIS;
XintRegs.XINT1CR.bit.POLARITY = edge_mode; // Falling edge interrupt
}
else if(channel == 2)
{
XintRegs.XINT2CR.bit.ENABLE = 1; // Enable XINT2
EALLOW;
InputXbarRegs.INPUT5SELECT = pin; //Set XINT1 source to GPIO-pin
EDIS;
XintRegs.XINT2CR.bit.POLARITY = edge_mode; // Falling edge interrupt
}
else if(channel == 3)
{
XintRegs.XINT3CR.bit.ENABLE = 1; // Enable XINT2
EALLOW;
InputXbarRegs.INPUT6SELECT = pin; //Set XINT1 source to GPIO-pin
EDIS;
XintRegs.XINT3CR.bit.POLARITY = edge_mode; // Falling edge interrupt
}
else if(channel == 4)
{
XintRegs.XINT4CR.bit.ENABLE = 1; // Enable XINT2
EALLOW;
InputXbarRegs.INPUT13SELECT = pin; //Set XINT1 source to GPIO-pin
EDIS;
XintRegs.XINT4CR.bit.POLARITY = edge_mode; // Falling edge interrupt
}
else if(channel == 5)
{
XintRegs.XINT5CR.bit.ENABLE = 1; // Enable XINT2
EALLOW;
InputXbarRegs.INPUT14SELECT = pin; //Set XINT1 source to GPIO-pin
EDIS;
XintRegs.XINT5CR.bit.POLARITY = edge_mode; // Falling edge interrupt
}
c28x_pin_mode(device, pin, pin_mode);
rt_hw_interrupt_enable(level);
}
else if (enabled == PIN_IRQ_DISABLE)
{
level = rt_hw_interrupt_disable();
channel = irqindex+1;
/*
* TODO modify this simpler
*/
if(channel == 1)
{
XintRegs.XINT1CR.bit.ENABLE = 0; // Disable XINT1
}
else if(channel == 2)
{
XintRegs.XINT2CR.bit.ENABLE = 0; // Disable XINT2
}
else if(channel == 3)
{
XintRegs.XINT3CR.bit.ENABLE = 0; // Disable XINT2
}
else if(channel == 4)
{
XintRegs.XINT4CR.bit.ENABLE = 0; // Disable XINT2
}
else if(channel == 5)
{
XintRegs.XINT5CR.bit.ENABLE = 0; // Disable XINT2
}
rt_hw_interrupt_enable(level);
}
else
{
return -RT_ENOSYS;
}
return RT_EOK;
}
void GPIO_XINT_Callback(rt_int16_t XINT_number);
interrupt void XINT1_Handler(void)
{
rt_interrupt_enter();
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
GPIO_XINT_Callback(1);
rt_interrupt_leave();
}
interrupt void XINT2_Handler(void)
{
rt_interrupt_enter();
GPIO_XINT_Callback(2);
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
rt_interrupt_leave();
}
interrupt void XINT3_Handler(void)
{
rt_interrupt_enter();
GPIO_XINT_Callback(3);
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
rt_interrupt_leave();
}
interrupt void XINT4_Handler(void)
{
rt_interrupt_enter();
GPIO_XINT_Callback(4);
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
rt_interrupt_leave();
}
interrupt void XINT5_Handler(void)
{
rt_interrupt_enter();
GPIO_XINT_Callback(5);
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
rt_interrupt_leave();
}
void GPIO_XINT_Callback(rt_int16_t XINT_number)
{
rt_int32_t irqindex = XINT_number - 1;
if(pin_irq_hdr_tab[irqindex].hdr)
{
pin_irq_hdr_tab[irqindex].hdr(pin_irq_hdr_tab[irqindex].args);
}
}
#endif /* RT_USING_PIN */
|