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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
|
/*****************************************************************************
*
* \file
*
* \brief GPIO software driver interface for AVR UC3.
*
* Copyright (c) 2010-2018 Microchip Technology Inc. and its subsidiaries.
*
* \asf_license_start
*
* \page License
*
* Subject to your compliance with these terms, you may use Microchip
* software and any derivatives exclusively with Microchip products.
* It is your responsibility to comply with third party license terms applicable
* to your use of third party software (including open source software) that
* may accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
* INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
* AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
* LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
* LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
* SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
* POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
* ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
* RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* \asf_license_stop
*
*****************************************************************************/
/*
* Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
*/
#include "gpio.h"
/** \name Peripheral Bus Interface
*
* @{
*/
/** \brief Enables specific module modes for a set of pins.
*
* \param gpiomap The pin map.
* \param size The number of pins in \a gpiomap.
*
* \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
*/
uint32_t gpio_enable_module(const gpio_map_t gpiomap, uint32_t size)
{
uint32_t status = GPIO_SUCCESS;
uint32_t i;
for (i = 0; i < size; i++) {
status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function);
gpiomap++;
}
return status;
}
/** \brief Enables a specific module mode for a pin.
*
* \note A pin and pin function index can be found in the device part header
* file. The \c AVR32_*_PIN constants map a GPIO number from the device
* datasheet to the appropriate pin function, while the corresponding
* \c AVR32_*_FUNCTION macro contains the appropriate function index.
* \n\n
* For example, the constants \c AVR32_PWM_3_PIN and
* \c AVR32_PWM_3_FUNCTION contain the pin and function index of the PWM
* module, channel 3, for the current device (if available).
*
* \param pin The pin number.
* \param function The pin function.
*
* \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
*/
uint32_t gpio_enable_module_pin(uint32_t pin, uint32_t function)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Enable the correct function. */
switch (function) {
case 0: /* A function. */
gpio_port->pmr0c = 1 << (pin & 0x1F);
gpio_port->pmr1c = 1 << (pin & 0x1F);
#if (AVR32_GPIO_H_VERSION >= 210)
gpio_port->pmr2c = 1 << (pin & 0x1F);
#endif
break;
case 1: /* B function. */
gpio_port->pmr0s = 1 << (pin & 0x1F);
gpio_port->pmr1c = 1 << (pin & 0x1F);
#if (AVR32_GPIO_H_VERSION >= 210)
gpio_port->pmr2c = 1 << (pin & 0x1F);
#endif
break;
case 2: /* C function. */
gpio_port->pmr0c = 1 << (pin & 0x1F);
gpio_port->pmr1s = 1 << (pin & 0x1F);
#if (AVR32_GPIO_H_VERSION >= 210)
gpio_port->pmr2c = 1 << (pin & 0x1F);
#endif
break;
case 3: /* D function. */
gpio_port->pmr0s = 1 << (pin & 0x1F);
gpio_port->pmr1s = 1 << (pin & 0x1F);
#if (AVR32_GPIO_H_VERSION >= 210)
gpio_port->pmr2c = 1 << (pin & 0x1F);
#endif
break;
#if (AVR32_GPIO_H_VERSION >= 210)
case 4: /* E function. */
gpio_port->pmr0c = 1 << (pin & 0x1F);
gpio_port->pmr1c = 1 << (pin & 0x1F);
gpio_port->pmr2s = 1 << (pin & 0x1F);
break;
case 5: /* F function. */
gpio_port->pmr0s = 1 << (pin & 0x1F);
gpio_port->pmr1c = 1 << (pin & 0x1F);
gpio_port->pmr2s = 1 << (pin & 0x1F);
break;
case 6: /* G function. */
gpio_port->pmr0c = 1 << (pin & 0x1F);
gpio_port->pmr1s = 1 << (pin & 0x1F);
gpio_port->pmr2s = 1 << (pin & 0x1F);
break;
case 7: /* H function. */
gpio_port->pmr0s = 1 << (pin & 0x1F);
gpio_port->pmr1s = 1 << (pin & 0x1F);
gpio_port->pmr2s = 1 << (pin & 0x1F);
break;
#endif
default:
return GPIO_INVALID_ARGUMENT;
}
/* Disable GPIO control. */
gpio_port->gperc = 1 << (pin & 0x1F);
return GPIO_SUCCESS;
}
/** \brief Enables the GPIO mode of a set of pins.
*
* \param gpiomap The pin map.
* \param size The number of pins in \a gpiomap.
*/
void gpio_enable_gpio(const gpio_map_t gpiomap, uint32_t size)
{
uint32_t i;
for (i = 0; i < size; i++) {
gpio_enable_gpio_pin(gpiomap->pin);
gpiomap++;
}
}
/** \brief Enables the GPIO mode of a pin.
*
* \param pin The pin number.\n
* Refer to the product header file `uc3x.h' (where x is the part
* number; e.g. x = a0512) for pin definitions. E.g., to enable the
* GPIO mode of PX21, AVR32_PIN_PX21 can be used. Module pins such as
* AVR32_PWM_3_PIN for PWM channel 3 can also be used to release
* module pins for GPIO.
*/
void gpio_enable_gpio_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->oderc = 1 << (pin & 0x1F);
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Enables the pull-up resistor of a pin.
*
* \param pin The pin number.
*/
void gpio_enable_pin_pull_up(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->puers = 1 << (pin & 0x1F);
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
gpio_port->pderc = 1 << (pin & 0x1F);
#endif
}
/** \brief Disables the pull-up resistor of a pin.
*
* \param pin The pin number.
*/
void gpio_disable_pin_pull_up(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->puerc = 1 << (pin & 0x1F);
}
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
/* Added support of Pull-up Resistor, Pull-down Resistor and Buskeeper Control. */
/** \brief Enables the pull-down resistor of a pin.
*
* \param pin The pin number.
*/
void gpio_enable_pin_pull_down(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->puerc = 1 << (pin & 0x1F);
gpio_port->pders = 1 << (pin & 0x1F);
}
/** \brief Disables the pull-down resistor of a pin.
*
* \param pin The pin number.
*/
void gpio_disable_pin_pull_down(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->pderc = 1 << (pin & 0x1F);
}
/** \brief Enables the buskeeper functionality on a pin.
*
* \param pin The pin number.
*/
void gpio_enable_pin_buskeeper(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->puers = 1 << (pin & 0x1F);
gpio_port->pders = 1 << (pin & 0x1F);
}
/** \brief Disables the buskeeper functionality on a pin.
*
* \param pin The pin number.
*/
void gpio_disable_pin_buskeeper(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->puerc = 1 << (pin & 0x1F);
gpio_port->pderc = 1 << (pin & 0x1F);
}
#endif
/** \brief Configuration functionality on a pin.
*
* \param pin The pin number.
* \param flags The configuration.
*/
void gpio_configure_pin(uint32_t pin, uint32_t flags)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Both pull-up and pull-down set means buskeeper */
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
if (flags & GPIO_PULL_DOWN) {
gpio_port->pders = 1 << (pin & 0x1F);
} else {
gpio_port->pderc = 1 << (pin & 0x1F);
}
#endif
if (flags & GPIO_PULL_UP) {
gpio_port->puers = 1 << (pin & 0x1F);
} else {
gpio_port->puerc = 1 << (pin & 0x1F);
}
/* Enable open-drain mode if requested */
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
if (flags & GPIO_OPEN_DRAIN) {
gpio_port->odmers = 1 << (pin & 0x1F);
} else {
gpio_port->odmerc = 1 << (pin & 0x1F);
}
#endif
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
/* Select drive strength */
if (flags & GPIO_DRIVE_LOW) {
gpio_port->odcr0s = 1 << (pin & 0x1F);
} else {
gpio_port->odcr0c = 1 << (pin & 0x1F);
}
if (flags & GPIO_DRIVE_HIGH) {
gpio_port->odcr1s = 1 << (pin & 0x1F);
} else {
gpio_port->odcr1c = 1 << (pin & 0x1F);
}
#endif
/* Select interrupt level for group */
if (flags & GPIO_INTERRUPT) {
if (flags & GPIO_BOTHEDGES) {
gpio_port->imr0c = 1 << (pin & 0x1F);
gpio_port->imr1c = 1 << (pin & 0x1F);
} else if (flags & GPIO_RISING) {
gpio_port->imr0s = 1 << (pin & 0x1F);
gpio_port->imr1c = 1 << (pin & 0x1F);
} else if (flags & GPIO_FALLING) {
gpio_port->imr0c = 1 << (pin & 0x1F);
gpio_port->imr1s = 1 << (pin & 0x1F);
}
}
/* Select direction and initial pin state */
if (flags & GPIO_DIR_OUTPUT) {
if (flags & GPIO_INIT_HIGH) {
gpio_port->ovrs = 1 << (pin & 0x1F);
} else {
gpio_port->ovrc = 1 << (pin & 0x1F);
}
gpio_port->oders = 1 << (pin & 0x1F);
} else {
gpio_port->oderc = 1 << (pin & 0x1F);
}
/* Enable GPIO */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Configuration functionality on a port.
*
* \param port The port number.
* \param mask The mask.
* \param flags The configuration.
*/
void gpio_configure_group(uint32_t port, uint32_t mask, uint32_t flags)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
/* Both pull-up and pull-down set means buskeeper */
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
if (flags & GPIO_PULL_DOWN) {
gpio_port->pders = mask;
} else {
gpio_port->pderc = mask;
}
#endif
if (flags & GPIO_PULL_UP) {
gpio_port->puers = mask;
} else {
gpio_port->puerc = mask;
}
/* Enable open-drain mode if requested */
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
if (flags & GPIO_OPEN_DRAIN) {
gpio_port->odmers = mask;
} else {
gpio_port->odmerc = mask;
}
if (flags & GPIO_OPEN_DRAIN) {
gpio_port->pders = mask;
} else {
gpio_port->pderc = mask;
}
#endif
#if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || \
defined(AVR32_GPIO_212_H_INCLUDED)
/* Select drive strength */
if (flags & GPIO_DRIVE_LOW) {
gpio_port->odcr0s = mask;
} else {
gpio_port->odcr0c = mask;
}
if (flags & GPIO_DRIVE_HIGH) {
gpio_port->odcr1s = mask;
} else {
gpio_port->odcr1c = mask;
}
#endif
/* Select interrupt level for group */
if (flags & GPIO_INTERRUPT) {
if (flags & GPIO_BOTHEDGES) {
gpio_port->imr0c = mask;
gpio_port->imr1c = mask;
} else if (flags & GPIO_RISING) {
gpio_port->imr0s = mask;
gpio_port->imr1c = mask;
} else if (flags & GPIO_FALLING) {
gpio_port->imr0c = mask;
gpio_port->imr1s = mask;
}
}
/* Select direction and initial pin state */
if (flags & GPIO_DIR_OUTPUT) {
if (flags & GPIO_INIT_HIGH) {
gpio_port->ovrs = mask;
} else {
gpio_port->ovrc = mask;
}
gpio_port->oders = mask;
} else {
gpio_port->oderc = mask;
}
/* Enable GPIO */
gpio_port->gpers = mask;
}
/** \brief Returns the value of a pin.
*
* \param pin The pin number.
*
* \return The pin value.
*/
bool gpio_get_pin_value(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
return (gpio_port->pvr >> (pin & 0x1F)) & 1;
}
/** \brief Returns the output value set for a GPIO pin.
*
* \param pin The pin number.
*
* \return The pin output value.
*
* \note This function must be used in conjunction with \ref gpio_set_gpio_pin,
* \ref gpio_clr_gpio_pin and \ref gpio_tgl_gpio_pin.
*/
bool gpio_get_gpio_pin_output_value(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
return (gpio_port->ovr >> (pin & 0x1F)) & 1;
}
/** \brief Returns the output value set for a GPIO pin using open drain.
*
* \param pin The pin number.
*
* \return The pin output value.
*
* \note This function must be used in conjunction with
* \ref gpio_set_gpio_open_drain_pin, \ref gpio_clr_gpio_open_drain_pin
* and \ref gpio_tgl_gpio_open_drain_pin.
*/
bool gpio_get_gpio_open_drain_pin_output_value(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1;
}
/** \brief Drives a GPIO pin to 1.
*
* \param pin The pin number.
*/
void gpio_set_gpio_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line: 1. */
gpio_port->ovrs = 1 << (pin & 0x1F);
/* The GPIO output driver is enabled for that pin. */
gpio_port->oders = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Drives a GPIO pin to 1.
*
* \param pin The pin number.
*
* \note The function \ref gpio_configure_pin must be called before.
*/
void gpio_set_pin_high(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line: 1. */
gpio_port->ovrs = 1 << (pin & 0x1F);
}
/** \brief Drives a GPIO port to 1.
*
* \param port The port number.
* \param mask The mask.
*/
void gpio_set_group_high(uint32_t port, uint32_t mask)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
/* Value to be driven on the I/O group: 1. */
gpio_port->ovrs = mask;
}
/** \brief Drives a GPIO pin to 0.
*
* \param pin The pin number.
*
* \note The function \ref gpio_configure_pin must be called before.
*/
void gpio_set_pin_low(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line: 0. */
gpio_port->ovrc = 1 << (pin & 0x1F);
}
/** \brief Drives a GPIO pin to 0.
*
* \param pin The pin number.
*/
void gpio_clr_gpio_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line: 0. */
gpio_port->ovrc = 1 << (pin & 0x1F);
/* The GPIO output driver is enabled for that pin. */
gpio_port->oders = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Drives a GPIO port to 0.
*
* \param port The port number.
* \param mask The mask.
*/
void gpio_set_group_low(uint32_t port, uint32_t mask)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
/* Value to be driven on the I/O group: 0. */
gpio_port->ovrc = mask;
}
/** \brief Toggles a GPIO pin.
*
* \param pin The pin number.
*/
void gpio_tgl_gpio_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Toggle the I/O line. */
gpio_port->ovrt = 1 << (pin & 0x1F);
/* The GPIO output driver is enabled for that pin. */
gpio_port->oders = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Toggles a GPIO pin.
*
* \param pin The pin number.
*
* \note The function \ref gpio_configure_pin must be called before.
*/
void gpio_toggle_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Toggle the I/O line. */
gpio_port->ovrt = 1 << (pin & 0x1F);
}
/** \brief Toggles a GPIO group.
*
* \param port The port number.
* \param mask The mask.
*/
void gpio_toggle_group(uint32_t port, uint32_t mask)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[port];
/* Toggle the I/O port. */
gpio_port->ovrt = mask;
}
/** \brief Drives a GPIO pin to 1 using open drain.
*
* \param pin The pin number.
*/
void gpio_set_gpio_open_drain_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* The GPIO output driver is disabled for that pin. */
gpio_port->oderc = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Drives a GPIO pin to 0 using open drain.
*
* \param pin The pin number.
*/
void gpio_clr_gpio_open_drain_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line: 0. */
gpio_port->ovrc = 1 << (pin & 0x1F);
/* The GPIO output driver is enabled for that pin. */
gpio_port->oders = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Toggles a GPIO pin using open drain.
*
* \param pin The pin number.
*/
void gpio_tgl_gpio_open_drain_pin(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Value to be driven on the I/O line if the GPIO output driver is
* enabled: 0. */
gpio_port->ovrc = 1 << (pin & 0x1F);
/* The GPIO output driver is toggled for that pin. */
gpio_port->odert = 1 << (pin & 0x1F);
/* The GPIO module controls that pin. */
gpio_port->gpers = 1 << (pin & 0x1F);
}
/** \brief Enables the glitch filter of a pin.
*
* When the glitch filter is enabled, a glitch with duration of less than 1
* clock cycle is automatically rejected, while a pulse with duration of 2 clock
* cycles or more is accepted. For pulse durations between 1 clock cycle and 2
* clock cycles, the pulse may or may not be taken into account, depending on
* the precise timing of its occurrence. Thus for a pulse to be guaranteed
* visible it must exceed 2 clock cycles, whereas for a glitch to be reliably
* filtered out, its duration must not exceed 1 clock cycle. The filter
* introduces 2 clock cycles latency.
*
* \param pin The pin number.
*/
void gpio_enable_pin_glitch_filter(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->gfers = 1 << (pin & 0x1F);
}
/** \brief Disables the glitch filter of a pin.
*
* \param pin The pin number.
*/
void gpio_disable_pin_glitch_filter(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->gferc = 1 << (pin & 0x1F);
}
/** \brief Configure the edge detector of an input pin
*
* \param pin The pin number.
* \param mode The edge detection mode (\ref GPIO_PIN_CHANGE,
* \ref GPIO_RISING_EDGE or \ref GPIO_FALLING_EDGE).
*
* \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
*/
static uint32_t gpio_configure_edge_detector(uint32_t pin, uint32_t mode)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Configure the edge detector. */
switch (mode) {
case GPIO_PIN_CHANGE:
gpio_port->imr0c = 1 << (pin & 0x1F);
gpio_port->imr1c = 1 << (pin & 0x1F);
break;
case GPIO_RISING_EDGE:
gpio_port->imr0s = 1 << (pin & 0x1F);
gpio_port->imr1c = 1 << (pin & 0x1F);
break;
case GPIO_FALLING_EDGE:
gpio_port->imr0c = 1 << (pin & 0x1F);
gpio_port->imr1s = 1 << (pin & 0x1F);
break;
default:
return GPIO_INVALID_ARGUMENT;
}
return GPIO_SUCCESS;
}
/** \brief Enables the interrupt of a pin with the specified settings.
*
* \param pin The pin number.
* \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
* \ref GPIO_FALLING_EDGE).
*
* \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
*/
uint32_t gpio_enable_pin_interrupt(uint32_t pin, uint32_t mode)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
/* Enable the glitch filter. */
gpio_port->gfers = 1 << (pin & 0x1F);
/* Configure the edge detector. */
if (GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode)) {
return(GPIO_INVALID_ARGUMENT);
}
/* Enable interrupt. */
gpio_port->iers = 1 << (pin & 0x1F);
return GPIO_SUCCESS;
}
/** \brief Disables the interrupt of a pin.
*
* \param pin The pin number.
*/
void gpio_disable_pin_interrupt(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
gpio_port->ierc = 1 << (pin & 0x1F);
}
/** \brief Gets the interrupt flag of a pin.
*
* \param pin The pin number.
*
* \return The pin interrupt flag.
*/
bool gpio_get_pin_interrupt_flag(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
return (gpio_port->ifr >> (pin & 0x1F)) & 1;
}
/** \brief Clears the interrupt flag of a pin.
*
* \param pin The pin number.
*/
void gpio_clear_pin_interrupt_flag(uint32_t pin)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
#if (AVR32_GPIO_H_VERSION == 211)
/* GPIO erratum - Writing a one to the GPIO.IFRC register */
/* to clear an interrupt will be ignored if interrupt is enabled for the */
/* corresponding port. */
/* Work around for the erratum - Disable the interrupt, clear it by
* writing */
/* a one to GPIO.IFRC, then enable the interrupt. */
/* Save interrupt enable register. */
uint32_t const gpio_ier = gpio_port->ier;
/* Disable interrupt. */
gpio_port->ierc = gpio_ier;
/* Clear pin interrupt. */
gpio_port->ifrc = 1 << (pin & 0x1F);
/* Restore interrupt enable register. */
gpio_port->ier = gpio_ier;
#else
gpio_port->ifrc = 1 << (pin & 0x1F);
#endif
}
#if UC3L
/** \brief Configure the peripheral event trigger mode of a pin
*
* \param pin The pin number.
* \param mode The trigger mode (\ref GPIO_PIN_CHANGE, \ref GPIO_RISING_EDGE or
* \ref GPIO_FALLING_EDGE).
* \param use_igf use the Input Glitch Filter (true) or not (false).
*
* \return \ref GPIO_SUCCESS or \ref GPIO_INVALID_ARGUMENT.
*/
uint32_t gpio_configure_pin_periph_event_mode(uint32_t pin, uint32_t mode,
uint32_t use_igf)
{
volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[pin >> 5];
if (true == use_igf) {
/* Enable the glitch filter. */
gpio_port->gfers = 1 << (pin & 0x1F);
} else {
/* Disable the glitch filter. */
gpio_port->gferc = 1 << (pin & 0x1F);
}
/* Configure the edge detector. */
return gpio_configure_edge_detector(pin, mode);
}
#endif
/** @} */
|