Skip to content

Commit

Permalink
Remove "known at compile time" checks
Browse files Browse the repository at this point in the history
In my opinion it makes the code more difficult to maintain because the compiler is so strict when it comes to which functions that are allowed to check pins at compile time.
One solution may be to inline all functions that checks pin state at compile time, but this goes at the cost of compiled code size
  • Loading branch information
MCUdude committed Dec 9, 2020
1 parent 4bd5416 commit bcfb8a4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 78 deletions.
32 changes: 0 additions & 32 deletions avr/cores/microcore/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,6 @@ extern "C"{
void loop(void);
void yield(void) __attribute__ ((weak, alias("__empty")));
static void __empty() { /* Empty*/ }

// Constant checks error handler
void badArg(const char*) __attribute__((error("")));
#define ASSERT_CONST(pin) \
if(!__builtin_constant_p(pin)) badArg("Pin must be a constant")

// Check for valid PWM pin (0 .. 5)
__attribute__((always_inline))
inline void check_valid_digital_pin(uint8_t pin)
{
if(__builtin_constant_p(pin))
{
if(pin >= NUM_DIGITAL_PINS)
badArg("Digtial pin out of range");
}
else
badArg("Digital pin must be a constant");
}

// Check for valid PWM pin (0 .. 1)
__attribute__((always_inline))
inline void check_valid_pwm_pin(uint8_t pin)
{
if(__builtin_constant_p(pin))
{
if(pin > 1)
badArg("PWM pin out of range");
}
else
badArg("PWM pin must be a constant");
}

#ifdef __cplusplus
} // extern "C"

Expand Down
13 changes: 3 additions & 10 deletions avr/cores/microcore/wiring_digital.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*** MicroCore - wiring_digital.c ***
An Arduino core designed for ATtiny13
Based on the work done by "smeezekitty"
Based on the work done by "smeezekitty"
Modified and maintained by MCUdude
https://github.com/MCUdude/MicroCore
This file contains digital related
This file contains digital related
functions such as pinMode(),
digitalWrite() and digitalRead().
*/
Expand All @@ -13,11 +13,9 @@ digitalWrite() and digitalRead().

void pinMode(uint8_t pin, uint8_t mode)
{
check_valid_digital_pin(pin);

if(mode == OUTPUT) // Pin as output
DDRB |= _BV(pin);

else // Pin as input or input pullup
{
DDRB &= ~_BV(pin); // Set pin as input
Expand All @@ -26,11 +24,8 @@ void pinMode(uint8_t pin, uint8_t mode)
}
}


void digitalWrite(uint8_t pin, uint8_t val)
{
check_valid_digital_pin(pin);

if(val)
PORTB |= _BV(pin); // Set pin high
else
Expand All @@ -40,7 +35,5 @@ void digitalWrite(uint8_t pin, uint8_t val)

uint8_t digitalRead(uint8_t pin)
{
check_valid_digital_pin(pin);

return !!(PINB & _BV(pin));
}
7 changes: 0 additions & 7 deletions avr/cores/microcore/wiring_pulse.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ dozen microseconds before the start of the pulse.
// However it will only work properly with LTO enabled.
uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
{
check_valid_digital_pin(pin);

#if defined(SAFEMODE)
if(pin < 2)
turnOffPWM(c); // If it's a PWM pin, make sure PWM is off
#endif

// Convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
uint32_t numloops = 0;
Expand Down
8 changes: 2 additions & 6 deletions avr/cores/microcore/wiring_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ analogWrite().

void turnOffPWM(uint8_t pin)
{
check_valid_pwm_pin(pin);

if(pin == 0)
TCCR0A &= ~_BV(COM0A1);
else
TCCR0A &= ~_BV(COM0B1);
}


void analogWrite(uint8_t pin, uint8_t val)
{
check_valid_pwm_pin(pin);

// Set Timer0 prescaler
#if !defined(ENABLE_MICROS)
#if defined(PWM_PRESCALER_NONE) // PWM frequency = (F_CPU/256) / 1
Expand All @@ -47,7 +42,8 @@ void analogWrite(uint8_t pin, uint8_t val)
#endif
#endif

pinMode(pin, OUTPUT);
// Set pin to output
DDRB |= _BV(pin);

// Handle off condition
if(val == 0)
Expand Down
26 changes: 3 additions & 23 deletions avr/cores/microcore/wiring_shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,23 @@ functions shiftIn() and shiftOut().

uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
{
check_valid_digital_pin(dataPin);
check_valid_digital_pin(clockPin);

#if defined(SAFEMODE)
if(clockPin < 2)
turnOffPWM(clockPin); // If it's a PWM pin, make sure PWM is off
if(dataPin < 2)
turnOffPWM(dataPin); // If it's a PWM pin, make sure PWM is off
#endif

uint8_t value = 0;
uint8_t i = 8;
do
{
digitalWrite(clockPin, HIGH);
PORTB |= _BV(clockPin);
if(bitOrder == MSBFIRST)
value <<= 1;
else
value >>= 1;
if(digitalRead(dataPin))
if(PINB & _BV(dataPin))
{
if(bitOrder == MSBFIRST)
value |= 0x01;
else
value |= 0x80;
}
digitalWrite(clockPin, LOW);
PORTB &= ~_BV(clockPin);
}
while(--i);

Expand All @@ -49,21 +39,11 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
{
check_valid_digital_pin(dataPin);
check_valid_digital_pin(clockPin);

const uint8_t datapinMask = _BV(dataPin);
const uint8_t clkpinMask = _BV(clockPin);
uint8_t i = 8;
uint8_t portbits = (PORTB &= ~(datapinMask | clkpinMask));

#if defined(SAFEMODE)
if(clockPin < 2)
turnOffPWM(clockPin); // If it's a PWM pin, make sure PWM is off
if(dataPin < 2)
turnOffPWM(dataPin); // If it's a PWM pin, make sure PWM is off
#endif

do
{
if(bitOrder == MSBFIRST)
Expand Down

0 comments on commit bcfb8a4

Please sign in to comment.