Rev 1479 | Rev 1487 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1479 | Rev 1483 | ||
|---|---|---|---|
| Line 15... | Line 15... | ||
| 15 | #include <drivers/mcu/gpio.h> |
15 | #include <drivers/mcu/gpio.h> |
| 16 | #include <drivers/ir/transceiver/irtransceiverMulti.h> |
16 | #include <drivers/ir/transceiver/irtransceiverMulti.h> |
| 17 | #include <drivers/ir/protocols.h> |
17 | #include <drivers/ir/protocols.h> |
| 18 | #include <avr/io.h> |
18 | #include <avr/io.h> |
| 19 | #include <avr/interrupt.h> |
19 | #include <avr/interrupt.h> |
| 20 | - | ||
| 21 | /*----------------------------------------------------------------------------- |
- | |
| 22 | * Globals |
- | |
| 23 | *---------------------------------------------------------------------------*/ |
- | |
| 24 | #if IR_RX_ENABLE==1 |
- | |
| 25 | struct { |
- | |
| 26 | uint8_t enable; |
- | |
| 27 | uint16_t timeout; |
- | |
| 28 | uint8_t timeoutEnable; |
- | |
| 29 | uint16_t *rxbuf; |
- | |
| 30 | uint8_t rxlen; |
- | |
| 31 | uint8_t storeEnable; |
- | |
| 32 | irCallback_t callback; |
- | |
| 33 | } drvIrRxChannel[3]; |
- | |
| 34 | - | ||
| 35 | - | ||
| 36 | #endif |
- | |
| 37 | #if IR_TX_ENABLE==1 |
- | |
| 38 | uint16_t *txbuf; |
- | |
| 39 | uint8_t txlen; |
- | |
| 40 | static uint8_t bufIndex; //selects the pulse width in buffer to send |
- | |
| 41 | volatile uint8_t data_transmitted; |
- | |
| 42 | #endif |
- | |
| 43 | - | ||
| 44 | - | ||
| 45 | 20 | ||
| 46 | /*----------------------------------------------------------------------------- |
21 | /*----------------------------------------------------------------------------- |
| 47 | * Prerequisites |
22 | * Prerequisites |
| 48 | *---------------------------------------------------------------------------*/ |
23 | *---------------------------------------------------------------------------*/ |
| - | 24 | //TODO: Use this define, might fix the bug |
|
| 49 | #ifndef IR_RX_ACTIVE_LOW |
25 | #ifndef IR_RX_ACTIVE_LOW |
| 50 | #define IR_RX_ACTIVE_LOW 1 |
26 | #define IR_RX_ACTIVE_LOW 1 |
| 51 | #endif |
27 | #endif |
| 52 | 28 | ||
| 53 | #ifndef IR_TX_ACTIVE_LOW |
29 | #ifndef IR_TX_ACTIVE_LOW |
| 54 | #define IR_TX_ACTIVE_LOW 0 |
30 | #define IR_TX_ACTIVE_LOW 0 |
| 55 | #endif |
31 | #endif |
| 56 | 32 | ||
| 57 | #ifndef IR_TX_ENABLE |
33 | #ifndef IR_TX_ENABLE |
| 58 | #define IR_TX_ENABLE 0 |
34 | #define IR_TX_ENABLE 0 |
| 59 | #endif |
35 | #endif |
| 60 | 36 | ||
| 61 | #ifndef IR_RX_ENABLE |
37 | #ifndef IR_RX_ENABLE |
| 62 | #define IR_RX_ENABLE 0 |
38 | #define IR_RX_ENABLE 0 |
| 63 | #endif |
39 | #endif |
| 64 | 40 | ||
| 65 | 41 | ||
| 66 | #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) |
42 | #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) |
| 67 | #define TIMSK1 TIMSK |
43 | #define TIMSK1 TIMSK |
| 68 | #define TIFR1 TIFR |
44 | #define TIFR1 TIFR |
| 69 | #define ICIE1 TICIE1 |
45 | #define ICIE1 TICIE1 |
| 70 | #endif |
46 | #endif |
| 71 | 47 | ||
| 72 | #define IR_COMPARE_VECTOR TIMER1_COMPB_vect |
48 | #define IR_COMPARE_VECTOR TIMER1_COMPB_vect |
| 73 | #define IR_TIMEOUT_VECTOR TIMER1_COMPA_vect |
49 | #define IR_TIMEOUT_VECTOR TIMER1_COMPA_vect |
| 74 | #define IR_CAPTURE_VECTOR TIMER1_CAPT_vect |
50 | #define IR_CAPTURE_VECTOR TIMER1_CAPT_vect |
| 75 | #define IR_TIMEOUT_REG OCR1A |
51 | #define IR_TIMEOUT_REG OCR1A |
| Line 77... | Line 53... | ||
| 77 | #define IR_CAPTURE_REG ICR1 |
53 | #define IR_CAPTURE_REG ICR1 |
| 78 | #define IR_COUNT_REG TCNT1 |
54 | #define IR_COUNT_REG TCNT1 |
| 79 | #define IR_MODULATION_REG OCR0A |
55 | #define IR_MODULATION_REG OCR0A |
| 80 | /*#define IR_TIMER_INIT() TCCR1A = 0; \ |
56 | /*#define IR_TIMER_INIT() TCCR1A = 0; \ |
| 81 | TCCR1B = (1<<ICNC1)|(2<<CS10); |
57 | TCCR1B = (1<<ICNC1)|(2<<CS10); |
| 82 | */ |
58 | */ |
| 83 | #define IR_TIMER_INIT() TCCR1A = 0; \ |
59 | #define IR_TIMER_INIT() TCCR1A = 0; \ |
| 84 | TCCR1B = (2<<CS10); |
60 | TCCR1B = (2<<CS10); |
| 85 | #define IR_MASK_COMPARE() TIMSK1 &= ~(1<<OCIE1B); |
61 | #define IR_MASK_COMPARE() TIMSK1 &= ~(1<<OCIE1B); |
| 86 | #define IR_UNMASK_COMPARE() TIFR1 = (1<<OCF1B); TIMSK1 |= (1<<OCIE1B); |
62 | #define IR_UNMASK_COMPARE() TIFR1 = (1<<OCF1B); TIMSK1 |= (1<<OCIE1B); |
| 87 | #define IR_MASK_TIMEOUT() TIMSK1 &= ~(1<<OCIE1A); |
63 | #define IR_MASK_TIMEOUT() TIMSK1 &= ~(1<<OCIE1A); |
| Line 91... | Line 67... | ||
| 91 | #define IR_CAPTURE_FALLING() TCCR1B &= ~(1<<ICES1); \ |
67 | #define IR_CAPTURE_FALLING() TCCR1B &= ~(1<<ICES1); \ |
| 92 | TIFR1 = (1<<ICF1); |
68 | TIFR1 = (1<<ICF1); |
| 93 | #define IR_CAPTURE_RISING() TCCR1B |= (1<<ICES1); \ |
69 | #define IR_CAPTURE_RISING() TCCR1B |= (1<<ICES1); \ |
| 94 | TIFR1 = (1<<ICF1); |
70 | TIFR1 = (1<<ICF1); |
| 95 | 71 | ||
| 96 |
|
72 | //#define IR_OUTP_HIGH() TCCR0A |= (1<<COM0A0); |
| 97 |
|
73 | //#define IR_OUTP_LOW() TCCR0A &= ~(1<<COM0A0); |
| - | 74 | #if IR_TX_ACTIVE_LOW==1 |
|
| - | 75 | #define IR_OUTP_HIGH() *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask |
|
| - | 76 | #define IR_OUTP_LOW() *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask |
|
| - | 77 | #else |
|
| - | 78 | #define IR_OUTP_HIGH() *drvIrTxChannel[channel].port |= drvIrTxChannel[channel].pinmask |
|
| - | 79 | #define IR_OUTP_LOW() *drvIrTxChannel[channel].port &= ~drvIrTxChannel[channel].pinmask |
|
| - | 80 | #endif |
|
| - | 81 | ||
| - | 82 | ||
| - | 83 | /*----------------------------------------------------------------------------- |
|
| - | 84 | * Globals |
|
| - | 85 | *---------------------------------------------------------------------------*/ |
|
| - | 86 | #if IR_RX_ENABLE==1 |
|
| - | 87 | struct { |
|
| - | 88 | uint8_t enable; |
|
| - | 89 | uint16_t timeout; |
|
| - | 90 | uint8_t timeoutEnable; |
|
| - | 91 | uint16_t *rxbuf; |
|
| - | 92 | uint8_t rxlen; |
|
| - | 93 | uint8_t storeEnable; |
|
| - | 94 | irRxCallback_t callback; |
|
| - | 95 | } drvIrRxChannel[3]; |
|
| - | 96 | #endif |
|
| - | 97 | ||
| - | 98 | #if IR_TX_ENABLE==1 |
|
| - | 99 | struct { |
|
| - | 100 | uint8_t enable; |
|
| - | 101 | uint16_t timeout; |
|
| - | 102 | uint8_t timeoutEnable; |
|
| - | 103 | uint16_t *txbuf; |
|
| - | 104 | uint8_t txlen; |
|
| - | 105 | uint8_t txindex; |
|
| - | 106 | irTxCallback_t callback; |
|
| - | 107 | volatile uint8_t *port; |
|
| - | 108 | uint8_t pinmask; |
|
| - | 109 | } drvIrTxChannel[3]; |
|
| - | 110 | #endif |
|
| 98 | 111 | ||
| 99 | 112 | ||
| 100 | /*----------------------------------------------------------------------------- |
113 | /*----------------------------------------------------------------------------- |
| 101 | * Interrupt Handlers |
114 | * Interrupt Handlers |
| 102 | *---------------------------------------------------------------------------*/ |
115 | *---------------------------------------------------------------------------*/ |
| Line 108... | Line 121... | ||
| 108 | */ |
121 | */ |
| 109 | 122 | ||
| 110 | #if IR_TX_ENABLE==1 |
123 | #if IR_TX_ENABLE==1 |
| 111 | ISR(IR_COMPARE_VECTOR) |
124 | ISR(IR_COMPARE_VECTOR) |
| 112 | { |
125 | { |
| - | 126 | ||
| - | 127 | /* TODO find which channel */ |
|
| - | 128 | uint8_t channel = 0; |
|
| - | 129 | ||
| - | 130 | ||
| 113 | if (( |
131 | if ((drvIrTxChannel[channel].txindex&1) == 1) { /* if odd, ir-pause */ |
| 114 | IR_OUTP_LOW(); |
132 | IR_OUTP_LOW(); |
| 115 | } else { |
133 | } else { |
| 116 | IR_OUTP_HIGH(); |
134 | IR_OUTP_HIGH(); |
| 117 | } |
135 | } |
| 118 | 136 | ||
| 119 |
|
137 | if (drvIrTxChannel[channel].txindex < drvIrTxChannel[channel].txlen) |
| 120 | /* bufIndex++; |
- | |
| 121 | while (txbuf[bufIndex] == 0){ |
- | |
| 122 | bufIndex++; |
- | |
| 123 | }*/ |
- | |
| 124 | 138 | { |
|
| 125 | if (bufIndex < txlen) |
- | |
| 126 | { |
- | |
| 127 | IR_COMPARE_REG += txbuf[bufIndex++]; |
- | |
| 128 |
|
139 | drvIrTxChannel[channel].timeout = IR_COMPARE_REG + drvIrTxChannel[channel].txbuf[drvIrTxChannel[channel].txindex++]; |
| 129 | } |
140 | } |
| 130 | else |
141 | else |
| 131 | { |
142 | { |
| 132 | IR_MASK_COMPARE(); |
143 | IR_MASK_COMPARE(); |
| - | 144 | ||
| - | 145 | /* Notify the application that a pulse train has been sent. */ |
|
| 133 |
|
146 | drvIrTxChannel[channel].callback(channel); |
| 134 | } |
147 | } |
| - | 148 | ||
| - | 149 | /* TODO: find channel with lowest time left */ |
|
| - | 150 | IR_COMPARE_REG = drvIrTxChannel[channel].timeout; |
|
| 135 | } |
151 | } |
| 136 | #endif |
152 | #endif |
| 137 | 153 | ||
| 138 | #if IR_RX_ENABLE==1 |
154 | #if IR_RX_ENABLE==1 |
| 139 | //TODO: hur hantera detta i multi receiver |
155 | //TODO: hur hantera detta i multi receiver |
| Line 167... | Line 183... | ||
| 167 | 183 | ||
| 168 | IR_MASK_TIMEOUT(); |
184 | IR_MASK_TIMEOUT(); |
| 169 | 185 | ||
| 170 | //TODO: reset the timeout for next enabled channel |
186 | //TODO: reset the timeout for next enabled channel |
| 171 | /* |
187 | /* |
| 172 | uint16_t nextTimeout = drvIrRxChannel[channel].timeout; |
188 | uint16_t nextTimeout = drvIrRxChannel[channel].timeout; |
| 173 | for (uint8_t i=0; i < 3; i++) |
189 | for (uint8_t i=0; i < 3; i++) |
| 174 | { |
190 | { |
| 175 | //TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt) |
191 | //TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt) |
| 176 | //kanske inte är så noga, i värsta fall tar det 65ms extra |
192 | //kanske inte är så noga, i värsta fall tar det 65ms extra |
| 177 | if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout)) |
193 | if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout)) |
| 178 | { |
194 | { |
| 179 | nextTimeout = drvIrRxChannel[i].timeout; |
195 | nextTimeout = drvIrRxChannel[i].timeout; |
| 180 | } |
196 | } |
| 181 | } |
197 | } |
| 182 | IR_UNMASK_TIMEOUT(); |
198 | IR_UNMASK_TIMEOUT(); |
| 183 | IR_TIMEOUT_REG = nextTimeout; |
199 | IR_TIMEOUT_REG = nextTimeout; |
| 184 | */ |
200 | */ |
| 185 | } |
201 | } |
| 186 | #endif |
202 | #endif |
| 187 | 203 | ||
| 188 | 204 | ||
| 189 | /*----------------------------------------------------------------------------- |
205 | /*----------------------------------------------------------------------------- |
| 190 | * Private Functions |
206 | * Private Functions |
| 191 | *---------------------------------------------------------------------------*/ |
207 | *---------------------------------------------------------------------------*/ |
| 192 | 208 | ||
| 193 | #if IR_RX_ENABLE==1 |
209 | #if IR_RX_ENABLE==1 |
| 194 | 210 | ||
| 195 | /* these wrapper functions are called from the pcint driver (callback) */ |
211 | /* these wrapper functions are called from the pcint driver (callback) */ |
| 196 | void IrTransceiver_Store_ch0(uint8_t id, uint8_t status) |
212 | void IrTransceiver_Store_ch0(uint8_t id, uint8_t status) |
| 197 | { |
213 | { |
| 198 | IrTransceiver_Store(0); |
214 | IrTransceiver_Store(0); |
| 199 | } |
215 | } |
| 200 | void IrTransceiver_Store_ch1(uint8_t id, uint8_t status) |
216 | void IrTransceiver_Store_ch1(uint8_t id, uint8_t status) |
| 201 | { |
217 | { |
| 202 | IrTransceiver_Store(1); |
218 | IrTransceiver_Store(1); |
| 203 | } |
219 | } |
| 204 | void IrTransceiver_Store_ch2(uint8_t id, uint8_t status) |
220 | void IrTransceiver_Store_ch2(uint8_t id, uint8_t status) |
| 205 | { |
221 | { |
| 206 | IrTransceiver_Store(2); |
222 | IrTransceiver_Store(2); |
| 207 | } |
223 | } |
| 208 | 224 | ||
| Line 215... | Line 231... | ||
| 215 | uint16_t time = IR_COUNT_REG; |
231 | uint16_t time = IR_COUNT_REG; |
| 216 | 232 | ||
| 217 | /* Subtract the current measurement from the previous to get the pulse width. */ |
233 | /* Subtract the current measurement from the previous to get the pulse width. */ |
| 218 | pulsewidth = time - prev_time[channel]; |
234 | pulsewidth = time - prev_time[channel]; |
| 219 | prev_time[channel] = time; |
235 | prev_time[channel] = time; |
| 220 | 236 | ||
| 221 | if (drvIrRxChannel[channel].storeEnable) |
237 | if (drvIrRxChannel[channel].storeEnable) |
| 222 | { |
238 | { |
| 223 | /* Store the measurement. */ |
239 | /* Store the measurement. */ |
| 224 | drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth; |
240 | drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth; |
| 225 | 241 | ||
| Line 264... | Line 280... | ||
| 264 | /*----------------------------------------------------------------------------- |
280 | /*----------------------------------------------------------------------------- |
| 265 | * Public Functions |
281 | * Public Functions |
| 266 | *---------------------------------------------------------------------------*/ |
282 | *---------------------------------------------------------------------------*/ |
| 267 | 283 | ||
| 268 | /* call this function like this: |
284 | /* call this function like this: |
| 269 | |
285 | IrTransceiver_Init_RX_Channel(channelnumber, buffer, callback, pcint-id, GPIO_D6); |
| 270 | buffer is a memory location |
286 | buffer is a memory location |
| 271 | callback is a function to call when a receive/transmitt is complete |
287 | callback is a function to call when a receive/transmitt is complete |
| 272 | also call with IO port to use |
288 | also call with IO port to use |
| 273 | */ |
289 | */ |
| 274 | 290 | ||
| 275 | #if IR_RX_ENABLE==1 |
291 | #if IR_RX_ENABLE==1 |
| 276 | void |
292 | void IrTransceiver_InitRxChannel(uint8_t channel, uint16_t *buffer, irRxCallback_t callback, uint8_t pcint_id, volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) |
| 277 | { |
293 | { |
| 278 | if (channel == 0) |
294 | if (channel == 0) |
| 279 | { |
295 | { |
| 280 | Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch0); |
296 | Pcint_SetCallback(pcint_id, pcint, &IrTransceiver_Store_ch0); |
| 281 | } |
297 | } |
| Line 301... | Line 317... | ||
| 301 | drvIrRxChannel[channel].enable = TRUE; |
317 | drvIrRxChannel[channel].enable = TRUE; |
| 302 | } |
318 | } |
| 303 | } |
319 | } |
| 304 | 320 | ||
| 305 | /* call this function like this: |
321 | /* call this function like this: |
| 306 | |
322 | IrTransceiver_DeInit_RX_Channel(channelnumber, pcint-id, GPIO_D6); |
| 307 | */ |
323 | */ |
| 308 | 324 | ||
| 309 | void |
325 | void IrTransceiver_DeInitRxChannel(uint8_t channel, uint8_t pcint_id, volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) |
| 310 | { |
326 | { |
| 311 | /* Deactivate interrupt */ |
327 | /* Deactivate interrupt */ |
| 312 | Pcint_SetCallback(pcint_id, pcint, 0); |
328 | Pcint_SetCallback(pcint_id, pcint, 0); |
| 313 | 329 | ||
| 314 | if (channel < 3) |
330 | if (channel < 3) |
| Line 320... | Line 336... | ||
| 320 | drvIrRxChannel[channel].timeoutEnable = FALSE; |
336 | drvIrRxChannel[channel].timeoutEnable = FALSE; |
| 321 | drvIrRxChannel[channel].callback = 0; |
337 | drvIrRxChannel[channel].callback = 0; |
| 322 | } |
338 | } |
| 323 | } |
339 | } |
| 324 | 340 | ||
| 325 | uint8_t |
341 | uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel) |
| 326 | { |
342 | { |
| 327 | return drvIrRxChannel[channel].storeEnable; |
343 | return drvIrRxChannel[channel].storeEnable; |
| 328 | } |
344 | } |
| 329 | 345 | ||
| 330 | void |
346 | void IrTransceiver_DisableRx(uint8_t channel) |
| 331 | { |
347 | { |
| 332 | if (channel < 3) |
348 | if (channel < 3) |
| 333 | { |
349 | { |
| 334 | /* Soft disable channel */ |
350 | /* Soft disable channel */ |
| 335 | drvIrRxChannel[channel].enable = FALSE; |
351 | drvIrRxChannel[channel].enable = FALSE; |
| 336 | } |
352 | } |
| 337 | } |
353 | } |
| 338 | void |
354 | void IrTransceiver_EnableRx(uint8_t channel) |
| 339 | { |
355 | { |
| 340 | if (channel < 3) |
356 | if (channel < 3) |
| 341 | { |
357 | { |
| 342 | /* Soft enable channel */ |
358 | /* Soft enable channel */ |
| 343 | drvIrRxChannel[channel].enable = TRUE; |
359 | drvIrRxChannel[channel].enable = TRUE; |
| 344 | } |
360 | } |
| 345 | } |
361 | } |
| 346 | void |
362 | void IrTransceiver_ResetRx(uint8_t channel) |
| 347 | { |
363 | { |
| 348 | if (channel < 3) |
364 | if (channel < 3) |
| 349 | { |
365 | { |
| 350 | /* Reset channel */ |
366 | /* Reset channel */ |
| 351 | drvIrRxChannel[channel].rxlen = 0; |
367 | drvIrRxChannel[channel].rxlen = 0; |
| Line 354... | Line 370... | ||
| 354 | } |
370 | } |
| 355 | } |
371 | } |
| 356 | 372 | ||
| 357 | #endif |
373 | #endif |
| 358 | 374 | ||
| 359 | /* |
- | |
| 360 | void IrTransceiver_Init_RX_Channel(uint8_t channel, uint16_t *buffer, irCallback_t callback, volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) |
- | |
| 361 | { |
- | |
| 362 | |
- | |
| 363 | } |
- | |
| 364 | */ |
- | |
| 365 | 375 | ||
| 366 | void IrTransceiver_Init(void) |
376 | void IrTransceiver_Init(void) |
| 367 | { |
377 | { |
| - | 378 | IR_TIMER_INIT(); |
|
| - | 379 | ||
| - | 380 | #if IR_RX_ENABLE==1 |
|
| 368 | for (uint8_t i = 0; i < 3; i++) |
381 | for (uint8_t i = 0; i < 3; i++) |
| 369 | { |
382 | { |
| 370 | drvIrRxChannel[i].storeEnable = FALSE; |
383 | drvIrRxChannel[i].storeEnable = FALSE; |
| 371 | drvIrRxChannel[i].timeoutEnable = FALSE; |
384 | drvIrRxChannel[i].timeoutEnable = FALSE; |
| 372 | drvIrRxChannel[i].rxlen = 0; |
385 | drvIrRxChannel[i].rxlen = 0; |
| 373 | } |
386 | } |
| 374 | 387 | #endif |
|
| 375 | IR_TIMER_INIT(); |
- | |
| 376 | 388 | ||
| 377 | #if IR_TX_ENABLE==1 |
389 | #if IR_TX_ENABLE==1 |
| 378 | /* Set up transmitter */ |
390 | /* Set up transmitter */ |
| 379 | IR_T_DDR |= (1<<IR_T_BIT); |
- | |
| 380 | #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) |
391 | #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) |
| 381 | TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00); |
392 | TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00); |
| 382 | TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02); |
393 | TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02); |
| 383 | #endif |
- | |
| 384 | - | ||
| 385 | #if IR_TX_ACTIVE_LOW==1 |
- | |
| 386 | /* when pwm is disconnected from port (during low) the port should output 5V */ |
- | |
| 387 | IR_T_PORT |= (1<<IR_T_BIT); |
- | |
| 388 | #else |
- | |
| 389 | /* when pwm is disconnected from port (during low) the port should output 0V */ |
- | |
| 390 | IR_T_PORT &= ~(1<<IR_T_BIT); |
- | |
| 391 | #endif |
- | |
| 392 | - | ||
| 393 | #endif |
- | |
| 394 | - | ||
| 395 | } |
- | |
| 396 | 394 | ||
| 397 |
|
395 | /* set up modulation frequency to 38kHz */ |
| 398 |
|
396 | IR_MODULATION_REG = (((F_CPU/2000)/38) -1); |
| - | 397 | ||
| 399 |
|
398 | /* start pwm generator */ |
| 400 |
|
399 | TCCR0A |= (1<<COM0A0); |
| 401 |
|
400 | #endif |
| 402 | return IR_OK; |
- | |
| 403 | } |
401 | |
| 404 | #else |
- | |
| 405 | return 0; |
- | |
| 406 | #endif |
402 | #endif |
| - | 403 | ||
| - | 404 | } |
|
| - | 405 | ||
| - | 406 | #if IR_TX_ENABLE==1 |
|
| - | 407 | void IrTransceiver_InitTxChannel(uint8_t channel, irTxCallback_t callback, volatile uint8_t *port, volatile uint8_t *pin, volatile uint8_t *ddr,uint8_t nr, uint8_t pcint) |
|
| - | 408 | { |
|
| - | 409 | if (channel < 3) |
|
| - | 410 | { |
|
| - | 411 | drvIrTxChannel[channel].port=port; |
|
| - | 412 | drvIrTxChannel[channel].pinmask=(1<<*pin); |
|
| - | 413 | ||
| - | 414 | /* set up port as output */ |
|
| - | 415 | *ddr |= (1<<*pin); |
|
| - | 416 | ||
| - | 417 | IR_OUTP_LOW(); |
|
| - | 418 | ||
| - | 419 | drvIrTxChannel[channel].callback = callback; |
|
| - | 420 | drvIrTxChannel[channel].enable = TRUE; |
|
| - | 421 | drvIrTxChannel[channel].timeoutEnable = FALSE; |
|
| - | 422 | } |
|
| 407 | } |
423 | } |
| 408 | 424 | ||
| 409 |
|
425 | void IrTransceiver_Transmit(uint8_t channel, uint16_t *buffer, uint8_t length) |
| 410 | { |
426 | { |
| 411 | #if IR_TX_ENABLE==1 |
- | |
| 412 |
|
427 | if (length > 0) |
| 413 | 428 | { |
|
| 414 | if |
429 | if (drvIrTxChannel[channel].timeoutEnable == FALSE) |
| 415 | 430 | { |
|
| - | 431 | drvIrTxChannel[channel].timeoutEnable = TRUE; |
|
| 416 | txbuf = buffer; |
432 | drvIrTxChannel[channel].txbuf = buffer; |
| 417 | txlen = length; |
433 | drvIrTxChannel[channel].txlen = length; |
| - | 434 | /* first value will be loaded directly to timer below */ |
|
| 418 |
|
435 | drvIrTxChannel[channel].txindex = 1; |
| 419 | 436 | ||
| 420 |
|
437 | drvIrTxChannel[channel].timeout = IR_COUNT_REG + drvIrTxChannel[channel].txbuf[0]; |
| 421 | - | ||
| - | 438 | /* TODO: only modify timer reg if this channel has the lowest time left */ |
|
| 422 | IR_COMPARE_REG = |
439 | IR_COMPARE_REG = drvIrTxChannel[channel].timeout; |
| 423 | 440 | ||
| 424 | IR_OUTP_HIGH(); |
441 | IR_OUTP_HIGH(); |
| 425 | 442 | ||
| 426 | IR_UNMASK_COMPARE(); |
443 | IR_UNMASK_COMPARE(); |
| 427 | 444 | } |
|
| 428 | //while (!data_transmitted); |
- | |
| 429 | 445 | } |
|
| 430 | return IR_OK; |
- | |
| 431 | #else |
- | |
| 432 | return 0; |
- | |
| 433 | #endif |
- | |
| 434 | } |
446 | } |
| - | 447 | #endif |
|
| - | 448 | ||