1
+ /*
2
+ ap3_shift taken from wiring_shift.c by David Mellis.
3
+ Original copyright notice below.
4
+
5
+ wiring_shift.c - shiftOut() function
6
+ Part of Arduino - http://www.arduino.cc/
7
+
8
+ Copyright (c) 2005-2006 David A. Mellis
9
+
10
+ This library is free software; you can redistribute it and/or
11
+ modify it under the terms of the GNU Lesser General Public
12
+ License as published by the Free Software Foundation; either
13
+ version 2.1 of the License, or (at your option) any later version.
14
+
15
+ This library is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ Lesser General Public License for more details.
19
+
20
+ You should have received a copy of the GNU Lesser General
21
+ Public License along with this library; if not, write to the
22
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23
+ Boston, MA 02111-1307 USA
24
+ */
25
+
26
+ #include " ap3_shift.h"
27
+
28
+ uint8_t shiftIn (uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
29
+ {
30
+ uint8_t value = 0 ;
31
+
32
+ for (uint8_t i = 0 ; i < 8 ; ++i)
33
+ {
34
+ digitalWrite (clockPin, HIGH);
35
+ if (bitOrder == LSBFIRST)
36
+ value |= digitalRead (dataPin) << i;
37
+ else
38
+ value |= digitalRead (dataPin) << (7 - i);
39
+ digitalWrite (clockPin, LOW);
40
+ }
41
+ return value;
42
+ }
43
+
44
+ void shiftOut (uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
45
+ {
46
+ for (uint8_t i = 0 ; i < 8 ; i++)
47
+ {
48
+ if (bitOrder == LSBFIRST)
49
+ digitalWrite (dataPin, !!(val & (1 << i)));
50
+ else
51
+ digitalWrite (dataPin, !!(val & (1 << (7 - i))));
52
+
53
+ digitalWrite (clockPin, HIGH);
54
+ digitalWrite (clockPin, LOW);
55
+ }
56
+ }
57
+
58
+ // Configure a pin to be used in fast mode
59
+ void enableFastShift (uint8_t dataPin, uint8_t clockPin)
60
+ {
61
+ uint8_t dataPadNumber = ap3_gpio_pin2pad (dataPin);
62
+ uint8_t clockPadNumber = ap3_gpio_pin2pad (clockPin);
63
+
64
+ am_hal_gpio_fastgpio_disable (dataPadNumber);
65
+ am_hal_gpio_fastgpio_clr (dataPadNumber);
66
+
67
+ am_hal_gpio_fastgpio_disable (clockPadNumber);
68
+ am_hal_gpio_fastgpio_clr (clockPadNumber);
69
+
70
+ am_hal_gpio_fast_pinconfig ((uint64_t )0x1 << dataPadNumber |
71
+ (uint64_t )0x1 << clockPadNumber,
72
+ g_AM_HAL_GPIO_OUTPUT_12, 0 );
73
+ }
74
+
75
+ void fastShiftOut (uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
76
+ {
77
+ uint8_t dataPadNumber = ap3_gpio_pin2pad (dataPin);
78
+ uint8_t clockPadNumber = ap3_gpio_pin2pad (clockPin);
79
+
80
+ for (uint8_t i = 0 ; i < 8 ; i++)
81
+ {
82
+ if (bitOrder == LSBFIRST)
83
+ {
84
+ if (val & (1 << i))
85
+ am_hal_gpio_fastgpio_set (dataPadNumber);
86
+ else
87
+ am_hal_gpio_fastgpio_clr (dataPadNumber);
88
+ }
89
+ else
90
+ {
91
+ if (val & (1 << (7 - i)))
92
+ am_hal_gpio_fastgpio_set (dataPadNumber);
93
+ else
94
+ am_hal_gpio_fastgpio_clr (dataPadNumber);
95
+ }
96
+
97
+ am_hal_gpio_fastgpio_set (clockPadNumber);
98
+ am_hal_gpio_fastgpio_clr (clockPadNumber);
99
+ }
100
+ }
101
+
102
+ uint8_t fastShiftIn (uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
103
+ {
104
+ uint8_t dataPadNumber = ap3_gpio_pin2pad (dataPin);
105
+ uint8_t clockPadNumber = ap3_gpio_pin2pad (clockPin);
106
+
107
+ uint8_t value = 0 ;
108
+
109
+ for (uint8_t i = 0 ; i < 8 ; ++i)
110
+ {
111
+ am_hal_gpio_fastgpio_set (clockPadNumber);
112
+ if (bitOrder == LSBFIRST)
113
+ value |= am_hal_gpio_fastgpio_read (dataPadNumber) << i;
114
+ else
115
+ value |= am_hal_gpio_fastgpio_read (dataPadNumber) << (7 - i);
116
+ am_hal_gpio_fastgpio_clr (clockPadNumber);
117
+ }
118
+ return value;
119
+ }
0 commit comments