Skip to content

Commit

Permalink
make wiring header only for better inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Sommerlad committed Apr 6, 2020
1 parent 33cface commit b196bf3
Show file tree
Hide file tree
Showing 7 changed files with 830 additions and 139 deletions.
4 changes: 2 additions & 2 deletions Cpp17AVR-package-0.0.2.tar.gz
Git LFS file not shown
202 changes: 202 additions & 0 deletions createAnalogWriteTemplateSpecializations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#!/bin/bash

# create template code for timers to set pwm on AVRmegaxxx fro Arduino

cat <<!EOF0
// generated from createAnalogWriteTemplateSpecializations.sh
#ifndef _SFR_BYTE
#include <avr/io.h>
#endif
template <typename PTR>
inline
__attribute__((always_inline))
void cbi(PTR sfr, uint8_t const bit)
{
(_SFR_BYTE(sfr) &= ~_BV(bit));
}
template <typename PTR>
inline
__attribute__((always_inline))
void sbi(PTR sfr, uint8_t const bit)
{
(_SFR_BYTE(sfr) |= _BV(bit));
}
template <uint8_t pin, uint8_t val>
inline
__attribute__((always_inline))
void digitalWrite();
template<timer_values t>
inline
__attribute__((always_inline))
void setPWMValue(int val);
// special cases taken from avr
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
template <> inline
__attribute__((always_inline))
void setPWMValue<TIMER0A>(int val)
{
sbi(TCCR0, COM00);
OCR0 = val;
}
#endif
#if defined(TCCR2) && defined(COM21)
template <> inline
__attribute__((always_inline))
void setPWMValue<TIMER2>(int val)
{
sbi(TCCR2, COM21);
OCR2 = val;
}
#endif
#if defined(TCCR4A)
template <> inline
__attribute__((always_inline))
void setPWMValue<TIMER4A>(int val)
{
sbi(TCCR4A, COM4A1);
#if defined(COM4A0)
cbi(TCCR4A, COM4A0);
#endif
OCR4A = val;
}
#endif
#if defined(TCCR4C) && defined(COM4D1)
template <> inline
__attribute__((always_inline))
void setPWMValue<TIMER4D>(int val)
{
sbi(TCCR4C, COM4D1);
#if defined(COM4D0)
cbi(TCCR4C, COM4D0);
#endif
OCR4D = val;
}
#endif
!EOF0

function timers() {
case "$1" in
0) echo A B ;;
1) echo A B C ;;
2) echo A B ;;
3) echo A B C ;;
4) echo B C ;; # 4A and 4 D special cases
5) echo A B C ;;
esac
}

for num in 0 1 2 3 4 5
do
for c in `timers $num `
do
# some wrong 0 4A $D, some missing some superfluous
cat <<!EOF
#if defined(TCCR${num}A) && defined(COM${num}${c}1)
template <> inline
__attribute__((always_inline))
void setPWMValue<TIMER${num}${c}>(int val)
{
sbi(TCCR${num}A, COM${num}${c}1);
OCR${num}${c} = val;
}
#endif
!EOF
done
done


cat <<!EOF2
template<PinType pin>
void setPWMValuePin(int val){
if constexpr (digitalPinToTimer(pin) != NOT_ON_TIMER)
setPWMValue<digitalPinToTimer(pin)>(val);
else {
if (val < 128) {
digitalWrite<pin, LOW>();
} else {
digitalWrite<pin, HIGH>();
}
}
}
// to be defined in pins_arduino.h afterwards, since it depends on available timers
inline void analog_timer_turnoff(timer_values const theTimer) ;
// make it a compile error if called with NOT_ON_TIMER
template<timer_values t> inline
__attribute__((always_inline))
void analog_timer_turnoff() ;
template<PinType pin> inline
__attribute__((always_inline))
void analog_pin_to_timer_turnoff()
{
if constexpr(digital_pin_to_timer_PS(pin) != NOT_ON_TIMER)
analog_timer_turnoff<digital_pin_to_timer_PS(pin)>();
}
#if defined(TCCR2) && defined(COM21)
template <> inline
__attribute__((always_inline))
void analog_timer_turnoff<TIMER2>()
{
cbi(TCCR2, COM21);
}
#endif
#if defined(TCCR4C) && defined(COM4D1)
template <> inline
__attribute__((always_inline))
void analog_timer_turnoff<TIMER4D>()
{
cbi(TCCR4C, COM4D1);
}
#endif
!EOF2

function timers_to_turn_off() {
case "$1" in
0) echo A B ;;
1) echo A B C ;;
2) echo A B ;; # 2 nothing is special case
3) echo A B C ;;
4) echo A B C ;; # 4 D special case
5) echo A B C ;;
esac
}


for num in 0 1 2 3 4 5
do
for c in `timers_to_turn_off $num `
do
# some wrong 0 4A $D, some missing some superfluous
cat <<!EOF
#if defined(TCCR${num}A) && defined(COM${num}${c}1)
template <> inline
__attribute__((always_inline))
void analog_timer_turnoff<TIMER${num}${c}>()
{
cbi(TCCR${num}A, COM${num}${c}1);
}
#endif
!EOF
done
done



24 changes: 24 additions & 0 deletions create_analog_timer_turnoff_cases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
function timers_to_turn_off() {
case "$1" in
0) echo A B ;;
1) echo A B C ;;
2) echo A B ;;
3) echo A B C ;;
4) echo A B C ;;
5) echo A B C ;;
esac
}


for num in 0 1 2 3 4 5
do
for c in `timers_to_turn_off $num `
do
# some wrong 0 4A $D, some missing some superfluous
cat <<!EOF
case TIMER${num}${c}: analog_timer_turnoff<TIMER${num}${c}>(); break;
!EOF

done
done
4 changes: 2 additions & 2 deletions package_Cpp17AVR_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@
}
],
"category": "Contributed",
"checksum": "SHA-256:4ec1a3995f531c90f9b442df94d1c5fefe653a47d4035f0d303527702f24b647",
"checksum": "SHA-256:7157245d16864645bd1a4116cdb923dece865092d5104cfe40d066e22e0b3a24",
"help": {
"online": "http://www.arduino.cc/en/Reference/HomePage"
},
"name": "Cpp17 Arduino AVR Boards",
"size": "22401056",
"size": "22451895",
"toolsDependencies": [
{
"name": "avr-gcc",
Expand Down
40 changes: 10 additions & 30 deletions wsAVR/arduinoavr_dummy/.project
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@
<type>2</type>
<locationURI>virtual:/virtual</locationURI>
</link>
<link>
<name>microp</name>
<type>2</type>
<locationURI>virtual:/virtual</locationURI>
</link>
<link>
<name>robot_control</name>
<type>2</type>
<locationURI>virtual:/virtual</locationURI>
</link>
<link>
<name>robot_motor</name>
<type>2</type>
<locationURI>virtual:/virtual</locationURI>
</link>
<link>
<name>standard</name>
<type>2</type>
Expand Down Expand Up @@ -130,21 +115,6 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/variants/micro/pins_arduino.h</locationURI>
</link>
<link>
<name>microp/pins_arduino.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/variants/microp/pins_arduino.h</locationURI>
</link>
<link>
<name>robot_control/pins_arduino.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/variants/robot_control/pins_arduino.h</locationURI>
</link>
<link>
<name>robot_motor/pins_arduino.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/variants/robot_motor/pins_arduino.h</locationURI>
</link>
<link>
<name>standard/pins_arduino.h</name>
<type>1</type>
Expand Down Expand Up @@ -340,6 +310,11 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/cores/arduino/new.h</locationURI>
</link>
<link>
<name>cores/arduino/pwm_timer_handling.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/cores/arduino/pwm_timer_handling.h</locationURI>
</link>
<link>
<name>cores/arduino/wiring.c</name>
<type>1</type>
Expand All @@ -355,6 +330,11 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/cores/arduino/wiring_digital.c</locationURI>
</link>
<link>
<name>cores/arduino/wiring_inline.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/ArduinoCore-avr/cores/arduino/wiring_inline.h</locationURI>
</link>
<link>
<name>cores/arduino/wiring_private.h</name>
<type>1</type>
Expand Down
Loading

0 comments on commit b196bf3

Please sign in to comment.