Skip to content

Commit 471d824

Browse files
author
Nathan Seidle
committed
Add shiftIn/shiftOut as well as fastShiftIn fastShiftOut
1 parent 878635e commit 471d824

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

cores/arduino/ard_sup/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extern "C"
7777
#include "ap3_uart.h"
7878
#include "ap3_analog.h"
7979
#include "ap3_clock_sources.h"
80+
#include "ap3_shift.h"
8081
#include "WMath.h"
8182
#include "WCharacter.h"
8283

cores/arduino/ard_sup/ap3_shift.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (c) 2019 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _AP3_SHIFT_H_
24+
#define _AP3_SHIFT_H_
25+
26+
#include "Arduino.h"
27+
28+
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
29+
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
30+
31+
void enableFastShift(uint8_t dataPin, uint8_t clockPin);
32+
void fastShiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
33+
uint8_t fastShiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
34+
35+
#endif // _AP3_SHIFT_H_
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)