Commit 21c4e090b5b1a2a46e4ff18d8b38d5bd33ca14c8
Committed by
GitHub
1 parent
dae7d0e9
Delete qqqDali_ATMega328.h
Showing
1 changed file
with
0 additions
and
145 deletions
qqqDali_ATMega328.h deleted
| 1 | -/*########################################################################### | |
| 2 | - qqqDali_ATMega328.h - copyright qqqlab.com / github.com/qqqlab | |
| 3 | - | |
| 4 | - This program is free software: you can redistribute it and/or modify | |
| 5 | - it under the terms of the GNU General Public License as published by | |
| 6 | - the Free Software Foundation, either version 3 of the License, or | |
| 7 | - (at your option) any later version. | |
| 8 | - | |
| 9 | - This program is distributed in the hope that it will be useful, | |
| 10 | - but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | - GNU General Public License for more details. | |
| 13 | - | |
| 14 | - You should have received a copy of the GNU General Public License | |
| 15 | - along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | - | |
| 17 | ----------------------------------------------------------------------------- | |
| 18 | -Changelog: | |
| 19 | -2020-11-10 Created & tested on ATMega328 @ 8Mhz | |
| 20 | -###########################################################################*/ | |
| 21 | -#include "arduino.h" | |
| 22 | -#include "qqqDali.h" | |
| 23 | - | |
| 24 | -#define DALI_HOOK_COUNT 3 | |
| 25 | - | |
| 26 | -class Dali_ATMega328 : public Dali { | |
| 27 | -public: | |
| 28 | - void begin(int8_t tx_pin, int8_t rx_pin); | |
| 29 | - virtual void HAL_set_bus_low() const override; | |
| 30 | - virtual void HAL_set_bus_high() const override; | |
| 31 | - virtual uint8_t HAL_is_bus_low() const override; | |
| 32 | - virtual uint32_t HAL_micros() const override; | |
| 33 | - | |
| 34 | -private: | |
| 35 | - uint8_t tx_pin; //transmitter pin | |
| 36 | - uint8_t rx_pin; //receiver pin | |
| 37 | -}; | |
| 38 | - | |
| 39 | -//----------------------------------------- | |
| 40 | -//Hardware Abstraction Layer | |
| 41 | -void Dali_ATMega328::HAL_set_bus_low() const { | |
| 42 | - digitalWrite(this->tx_pin,LOW); | |
| 43 | -} | |
| 44 | - | |
| 45 | -void Dali_ATMega328::HAL_set_bus_high() const { | |
| 46 | - digitalWrite(this->tx_pin,HIGH); | |
| 47 | -} | |
| 48 | - | |
| 49 | -uint8_t Dali_ATMega328::HAL_is_bus_low() const { | |
| 50 | - return (digitalRead(this->rx_pin) == LOW); | |
| 51 | -} | |
| 52 | - | |
| 53 | -uint32_t Dali_ATMega328::HAL_micros() const { | |
| 54 | - return micros(); | |
| 55 | -} | |
| 56 | - | |
| 57 | -//----------------------------------------- | |
| 58 | -// Transmitter ISR | |
| 59 | -static Dali *IsrTimerHooks[DALI_HOOK_COUNT+1]; | |
| 60 | - | |
| 61 | -// timer compare interrupt service routine | |
| 62 | -ISR(TIMER1_COMPA_vect) { | |
| 63 | - | |
| 64 | - for(uint8_t i=0;i<DALI_HOOK_COUNT;i++) { | |
| 65 | - if(IsrTimerHooks[i]==NULL) {return;} | |
| 66 | - IsrTimerHooks[i]->ISR_timer(); | |
| 67 | - } | |
| 68 | -} | |
| 69 | - | |
| 70 | -//----------------------------------------- | |
| 71 | -// Receiver ISR | |
| 72 | -//pin PCINT | |
| 73 | -//0-7 PCINT2_vect PCINT16-23 | |
| 74 | -//8-13 PCINT0_vect PCINT0-5 | |
| 75 | -//14-19 PCINT1_vect PCINT8-13 | |
| 76 | -static Dali *IsrPCINT0Hook; | |
| 77 | -static Dali *IsrPCINT1Hook; | |
| 78 | -static Dali *IsrPCINT2Hook; | |
| 79 | - | |
| 80 | -ISR(PCINT0_vect) { | |
| 81 | - if(IsrPCINT0Hook!=NULL) IsrPCINT0Hook->ISR_pinchange(); | |
| 82 | -} | |
| 83 | -ISR(PCINT1_vect) { | |
| 84 | - if(IsrPCINT1Hook!=NULL) IsrPCINT1Hook->ISR_pinchange(); | |
| 85 | -} | |
| 86 | -ISR(PCINT2_vect) { | |
| 87 | - if(IsrPCINT2Hook!=NULL) IsrPCINT2Hook->ISR_pinchange(); | |
| 88 | -} | |
| 89 | - | |
| 90 | - | |
| 91 | -//----------------------------------------- | |
| 92 | -// begin | |
| 93 | -void Dali_ATMega328::begin(int8_t tx_pin, int8_t rx_pin) { | |
| 94 | - this->tx_pin = tx_pin; | |
| 95 | - this->rx_pin = rx_pin; | |
| 96 | - | |
| 97 | - //setup tx | |
| 98 | - if(this->tx_pin>=0) { | |
| 99 | - //setup tx pin | |
| 100 | - pinMode(this->tx_pin, OUTPUT); | |
| 101 | - this->HAL_set_bus_high(); | |
| 102 | - this->tx_bus_low=0; | |
| 103 | - | |
| 104 | - //setup tx timer interrupt | |
| 105 | - TCCR1A = 0; | |
| 106 | - TCCR1B = 0; | |
| 107 | - TCNT1 = 0; | |
| 108 | - OCR1A = (F_CPU+(DALI_BAUD))/(2*(DALI_BAUD)); // compare match register at baud rate * 2 | |
| 109 | - TCCR1B |= (1 << WGM12); // CTC mode | |
| 110 | - TCCR1B |= (1 << CS10); // 1:1 prescaler | |
| 111 | - TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt | |
| 112 | - | |
| 113 | - //setup timer interrupt hooks | |
| 114 | - for(uint8_t i=0;i<DALI_HOOK_COUNT;i++) { | |
| 115 | - if(IsrTimerHooks[i] == NULL) { | |
| 116 | - IsrTimerHooks[i] = this; | |
| 117 | - break; | |
| 118 | - } | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | - //setup rx | |
| 123 | - if(this->rx_pin>=0) { | |
| 124 | - //setup rx pin | |
| 125 | - pinMode(this->rx_pin, INPUT); | |
| 126 | - | |
| 127 | - //setup rx pinchange interrupt | |
| 128 | - // 0- 7 PCINT2_vect PCINT16-23 | |
| 129 | - // 8-13 PCINT0_vect PCINT0-5 | |
| 130 | - //14-19 PCINT1_vect PCINT8-13 | |
| 131 | - if(this->rx_pin <= 7){ | |
| 132 | - PCICR |= (1 << PCIE2); | |
| 133 | - PCMSK2 |= (1 << (this->rx_pin)); | |
| 134 | - IsrPCINT2Hook = this; //setup pinchange interrupt hook | |
| 135 | - }else if(this->rx_pin <= 13) { | |
| 136 | - PCICR |= (1 << PCIE0); | |
| 137 | - PCMSK0 |= (1 << (this->rx_pin - 8)); | |
| 138 | - IsrPCINT0Hook = this; //setup pinchange interrupt hook | |
| 139 | - }else if(this->rx_pin <= 19) { | |
| 140 | - PCICR |= (1 << PCIE1); | |
| 141 | - PCMSK1 |= (1 << (this->rx_pin - 14)); | |
| 142 | - IsrPCINT1Hook = this; //setup pinchange interrupt hook | |
| 143 | - } | |
| 144 | - } | |
| 145 | -} |