Skip to content

Commit 4ffbe20

Browse files
committed
check for valid pins in shift functions
1 parent a167a5b commit 4ffbe20

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

avr/cores/picocore/Arduino.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ extern inline void delay(uint16_t ms)
5858
_delay_us(ms * 1000);
5959
}
6060

61-
// todo: add check_valid_digital_pin
62-
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value);
63-
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
61+
void shiftOut(uint8_t dataPin, uint8_t clockPin, _bitOrder bitOrder, uint8_t value);
62+
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, _bitOrder bitOrder);
6463

6564
extern inline void pinMode(uint8_t pin, _pin_mode mode)
6665
{

avr/cores/picocore/constants.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// global constants
22
#include <stdint.h>
33

4-
static const uint8_t LOW = 0;
5-
static const uint8_t HIGH = 1;
6-
static const uint8_t LED_BUILTIN = 2;
7-
static const uint8_t NUM_DIGITAL_PINS = 6;
4+
const uint8_t LOW = 0;
5+
const uint8_t HIGH = 1;
6+
const uint8_t LED_BUILTIN = 2;
7+
const uint8_t NUM_DIGITAL_PINS = 6;
88

9-
enum _bitOrder {MSBFIRST = 0, LSBFIRST};
9+
typedef enum {MSBFIRST = 0, LSBFIRST} _bitOrder;
1010

11-
enum _pin_mode {
11+
typedef enum {
1212
INPUT = 0, OUTPUT, INPUT_PULLUP
13-
};
13+
} _pin_mode;
1414

15-
enum _analog_ref { DEFAULT = 0, INTERNAL };
15+
typedef enum { DEFAULT = 0, INTERNAL } _analog_ref;
1616

17-
enum _analog_pin { A0 = 0, A1, A2, A3, BAD_ANALOG_PIN };
17+
typedef enum { A0 = 0, A1, A2, A3, BAD_ANALOG_PIN } _analog_pin;
1818

avr/cores/picocore/wiring.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdint.h>
22
#include <avr/io.h>
33
#include <util/delay.h>
4-
#include "constants.h"
4+
#include "Arduino.h"
55

66
void delay_16ms(uint16_t count)
77
{
@@ -11,38 +11,42 @@ void delay_16ms(uint16_t count)
1111
}
1212

1313
// shift data in after rising edge of clock, 9 cycles/bit
14-
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
14+
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, _bitOrder bitOrder)
1515
{
1616
uint8_t value;
1717

1818
if (bitOrder == MSBFIRST) value = 0x01;
1919
else value = 0x80;
2020

21-
{
21+
{ // do
2222
// use inline asm to access the carry bit (not part of C/C++)
2323
morebits:
24-
PORTB |= 1 << clockPin;
24+
//PORTB |= 1 << clockPin;
25+
digitalWrite(clockPin, HIGH);
2526
if (bitOrder == MSBFIRST) value <<= 1;
2627
else value >>= 1;
2728

28-
if (bit_is_set(PORTB, dataPin))
29+
//if (bit_is_set(PORTB, dataPin))
30+
if (digialRead(dataPin))
2931
{
3032
if (bitOrder == MSBFIRST) value |= 0x01;
3133
else value |= 0x80;
3234
}
33-
PORTB &= ~(1 << clockPin);
35+
//PORTB &= ~(1 << clockPin);
36+
digitalWrite(clockPin, LOW);
3437

3538
asm goto ("brcc %l[morebits]" :::: morebits);
36-
}
39+
} // while (value)
3740

3841
return value;
3942
}
4043

41-
4244
// clock data out, 50% duty cycle, 8 cycles/bit
4345
// clock line left high after shiftOut
44-
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
46+
void shiftOut(uint8_t dataPin, uint8_t clockPin, _bitOrder bitOrder, uint8_t value)
4547
{
48+
check_valid_digital_pin(dataPin);
49+
check_valid_digital_pin(clockPin);
4650
const uint8_t dataMask = 1 << dataPin;
4751
const uint8_t clkMask = 1 << clockPin;
4852
uint8_t portbits = (PORTB & ~(dataMask | clkMask));
@@ -57,7 +61,7 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value
5761
value |= 0x80;
5862
}
5963

60-
{
64+
{ // do
6165
// use inline asm to access the carry bit (not part of C/C++)
6266
morebits:
6367
PORTB = portbits; // Clock and data pin low
@@ -74,6 +78,6 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value
7478
else value >>= 1;
7579
// loop until no more bits
7680
asm goto ("brne %l[morebits]" :: "r"(value) :: morebits);
77-
}
81+
} // while (value)
7882
}
7983

0 commit comments

Comments
 (0)