Subversion Repositories HomeAutomation

Rev

Rev 1730 | Rev 1753 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1730 Rev 1744
Line 67... Line 67...
67
 *---------------------------------------------------------------------------*/
67
 *---------------------------------------------------------------------------*/
68
#if IR_RX_ENABLE==1
68
#if IR_RX_ENABLE==1
69
struct {
69
struct {
70
    uint8_t     enable;
70
    uint8_t     enable;
71
    uint16_t    timeout;
71
    uint16_t    timeout;
72
    uint8_t     timeoutEnable;
-
 
73
    uint16_t    *rxbuf;
72
    uint16_t    *rxbuf;
74
    uint8_t     rxlen;
73
    uint8_t     rxlen;
75
    uint8_t     storeEnable;
74
    uint8_t     storeEnable;
76
    irRxCallback_t callback;
75
    irRxCallback_t callback;
77
} drvIrRxChannel[3];
76
} drvIrRxChannel[3];
78
#endif
77
#endif
79
 
78
 
80
#if IR_TX_ENABLE==1
79
#if IR_TX_ENABLE==1
81
struct {
80
struct {
82
    uint8_t     enable;
81
    uint8_t     enable;
83
    uint16_t    timeout;
82
    uint16_t    timeout;
84
    uint8_t     timeoutEnable;
83
    uint8_t     timeoutEnable;
Line 87... Line 86...
87
    uint8_t     txindex;
86
    uint8_t     txindex;
88
    irTxCallback_t callback;
87
    irTxCallback_t callback;
89
    volatile uint8_t    *port;
88
    volatile uint8_t    *port;
90
    uint8_t     pinmask;
89
    uint8_t     pinmask;
91
} drvIrTxChannel[3];
90
} drvIrTxChannel[3];
92
#endif
91
#endif
93
 
92
 
94
 
93
 
95
/*-----------------------------------------------------------------------------
94
/*-----------------------------------------------------------------------------
96
 * Interrupt Handlers
95
 * Interrupt Handlers
97
 *---------------------------------------------------------------------------*/
96
 *---------------------------------------------------------------------------*/
98
#if IR_TX_ENABLE==1
97
#if IR_TX_ENABLE==1
99
ISR(IR_COMPARE_VECTOR)
98
ISR(IR_COMPARE_VECTOR)
100
{
99
{
101
    /* find which channel */
100
    /* find which channel */
102
    uint16_t time = IR_COMPARE_REG;
101
    uint16_t time = IR_COMPARE_REG;
103
    uint16_t diff;
102
    uint16_t diff;
104
    uint8_t channel;
103
    uint8_t channel;
105
 
104
 
106
    /* go through all channels and check which of thems have overflowed */
105
    /* go through all channels and check which of thems have overflowed */
107
    for (channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
106
    for (channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
108
    {
107
    {
109
        /* if channel is waiting for an overflow */
108
        /* if channel is waiting for an overflow */
110
        if (drvIrTxChannel[channel].timeoutEnable==TRUE)
109
        if (drvIrTxChannel[channel].timeoutEnable==TRUE)
111
        {
110
        {
112
            /* check if channel have overflowed or is about to overflow */
111
            /* check if channel have overflowed or is about to overflow */
113
            diff = (time+50)-drvIrTxChannel[channel].timeout;
112
            diff = (time+50)-drvIrTxChannel[channel].timeout;
114
            if (diff < IR_MAX_PULSE_WIDTH)
113
            if (diff < IR_MAX_PULSE_WIDTH)
115
            {
114
            {
116
                /* check what to output next */
115
                /* check what to output next */
117
                if ((drvIrTxChannel[channel].txindex&1) == 1) {     /* if odd, ir-pause */
116
                if ((drvIrTxChannel[channel].txindex&1) == 1) {     /* if odd, ir-pause */
118
                    IR_OUTP_LOW();
117
                    IR_OUTP_LOW();
119
                } else {
118
                } else {
120
                    IR_OUTP_HIGH();
119
                    IR_OUTP_HIGH();
Line 162... Line 161...
162
    /* set compare value to the channel that have the next overflow */
161
    /* set compare value to the channel that have the next overflow */
163
    IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
162
    IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
164
}
163
}
165
#endif
164
#endif
166
 
165
 
167
#if IR_RX_ENABLE==1
-
 
168
//TODO: hur hantera detta i multi receiver
-
 
169
//TODO: ha en array med tider och nån enableflagga för varje kanal
-
 
170
//TODO: mot slutet av ISRen laddas nästa tid in... likadant som för TX, se ovan ISR
-
 
171
 
166
 
-
 
167
#if IR_RX_ENABLE==1
172
/* When this timeout occurs a pulsetrain is complete */
168
/* When this timeout occurs a pulsetrain is complete */
173
ISR(IR_TIMEOUT_VECTOR)
169
ISR(IR_TIMEOUT_VECTOR)
174
{
170
{
175
    /* Find which channel timed out */
171
gpio_toggle_pin(GPIO_B0); /* Toggle debug output PB0 */
176
    uint16_t timeDiff = 0xffff;
-
 
177
    uint8_t channel = 0;
-
 
178
/*  for (uint8_t i=0; i < 3; i++)
172
    for (uint8_t i=0; i < 3; i++)
179
    {
173
    {
180
        if ((drvIrRxChannel[channel].timeoutEnable==TRUE) && (IR_TIMEOUT_REG-drvIrRxChannel[i].timeout < timeDiff))
-
 
181
        {
-
 
182
            timeDiff = IR_TIMEOUT_REG-drvIrRxChannel[i].timeout;
-
 
183
            channel = i;
-
 
184
        }
-
 
185
    }
-
 
186
*/
-
 
187
    /* If more than 3 flanks was recevied */
174
        /* If more than 3 flanks was recevied */
188
    if (drvIrRxChannel[channel].rxlen > 3)
175
        if (drvIrRxChannel[i].storeEnable == TRUE && drvIrRxChannel[i].rxlen > 3)
189
    {
176
        {
190
        /* Notify the application that a pulse train has been received. */
177
            /* Notify the application that a pulse train has been received. */
191
        drvIrRxChannel[channel].callback(channel, drvIrRxChannel[channel].rxbuf, drvIrRxChannel[channel].rxlen);
178
            drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
192
    }
179
        }
193
 
180
 
194
    drvIrRxChannel[channel].storeEnable = FALSE;
181
        drvIrRxChannel[i].storeEnable = FALSE;
195
    drvIrRxChannel[channel].timeoutEnable = FALSE;
-
 
-
 
182
    }
196
 
183
 
-
 
184
    /* Disable ISR */
197
    IR_MASK_TIMEOUT();
185
    IR_MASK_TIMEOUT();
-
 
186
}
-
 
187
#endif
-
 
188
 
198
   
189
 
199
//TODO: reset the timeout for next enabled channel
-
 
200
/*
-
 
201
    uint16_t nextTimeout = drvIrRxChannel[channel].timeout;
-
 
202
    for (uint8_t i=0; i < 3; i++)
-
 
203
    {
-
 
204
//TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt)
-
 
205
//kanske inte är så noga, i värsta fall tar det 65ms extra
-
 
206
        if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout))
-
 
207
        {
-
 
208
            nextTimeout = drvIrRxChannel[i].timeout;
-
 
209
        }
-
 
210
    }
-
 
211
    IR_UNMASK_TIMEOUT();
-
 
212
    IR_TIMEOUT_REG = nextTimeout;
-
 
213
*/
-
 
214
}
-
 
215
#endif
-
 
216
 
-
 
217
 
-
 
218
/*-----------------------------------------------------------------------------
190
/*-----------------------------------------------------------------------------
219
 * Private Functions
191
 * Private Functions
220
 *---------------------------------------------------------------------------*/
192
 *---------------------------------------------------------------------------*/
221
 
193
 
222
#if IR_RX_ENABLE==1
194
#if IR_RX_ENABLE==1
Line 227... Line 199...
227
    IrTransceiver_Store(0);
199
    IrTransceiver_Store(0);
228
}
200
}
229
void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
201
void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
230
{
202
{
231
    IrTransceiver_Store(1);
203
    IrTransceiver_Store(1);
232
}
204
}
233
void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
205
void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
234
{
206
{
235
    IrTransceiver_Store(2);
207
    IrTransceiver_Store(2);
236
}
208
}
237
 
209
 
238
void IrTransceiver_Store(uint8_t channel)
210
void IrTransceiver_Store(uint8_t channel)
239
{
211
{
-
 
212
gpio_toggle_pin(GPIO_B7); /* Toggle debug output PB7 */
-
 
213
//gpio_toggle_pin(GPIO_D7); /* Toggle debug output PD7 */
-
 
214
 
240
    static uint16_t prev_time[3];
215
    static uint16_t prev_time[3];
241
    uint16_t pulsewidth;
216
    uint16_t pulsewidth;
242
 
217
 
243
    /* Read the timer counter register to get the current "time". */
218
    /* Read the timer counter register to get the current "time". */
244
    uint16_t time = IR_COUNT_REG;
219
    uint16_t time = IR_COUNT_REG;
Line 251... Line 226...
251
    {
226
    {
252
        /* Store the measurement. */
227
        /* Store the measurement. */
253
        drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth;
228
        drvIrRxChannel[channel].rxbuf[drvIrRxChannel[channel].rxlen++] = pulsewidth;
254
 
229
 
255
        /* Disable future measurements if we've filled the buffer. */
230
        /* Disable future measurements if we've filled the buffer. */
256
        //TODO: What to do in multi recevier?
-
 
257
        //TODO: Report overflow to application
231
        //TODO: Report overflow to application
258
        if (drvIrRxChannel[channel].rxlen == MAX_NR_TIMES)
232
        if (drvIrRxChannel[channel].rxlen == MAX_NR_TIMES)
259
        {
233
        {
260
            drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
234
            drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
261
        }
235
        }
-
 
236
        else
-
 
237
        {
-
 
238
            /* Set the timeout for detection of the end of the pulse train. */
-
 
239
            drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
-
 
240
            IR_TIMEOUT_REG = drvIrRxChannel[channel].timeout;
-
 
241
        }
262
    }
242
    }
263
    else if (drvIrRxChannel[channel].enable == TRUE)
243
    else if (drvIrRxChannel[channel].enable == TRUE)
264
    {
244
    {
265
        /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
245
        /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
266
        drvIrRxChannel[channel].storeEnable = TRUE;
246
        drvIrRxChannel[channel].storeEnable = TRUE;
267
        drvIrRxChannel[channel].rxlen = 0;
247
        drvIrRxChannel[channel].rxlen = 0;
268
        drvIrRxChannel[channel].timeoutEnable = TRUE;
-
 
269
 
-
 
270
        /* Set the timeout for detection of the end of the pulse train. */
-
 
271
        drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
-
 
272
        uint16_t nextTimeout = drvIrRxChannel[channel].timeout;
-
 
273
        for (uint8_t i=0; i < 3; i++)
-
 
274
        {
-
 
275
    //TODO: hitta den med timeout härnäst (svårt med tanke på att den tiden kan ha passerat eftersom vi är under interrupt)
-
 
276
    //kanske inte är så noga, i värsta fall tar det 65ms extra
-
 
277
            if ((drvIrRxChannel[channel].timeoutEnable) && (drvIrRxChannel[i].timeout < nextTimeout))
-
 
278
            {
-
 
279
                nextTimeout = drvIrRxChannel[i].timeout;
-
 
280
            }
-
 
281
        }
-
 
282
        IR_TIMEOUT_REG = nextTimeout;
-
 
283
 
248
 
284
        /* Enable timeout interrupt for detection of the end of the pulse train. */
249
        /* Enable timeout interrupt for detection of the end of the pulse train. */
-
 
250
        IR_TIMEOUT_REG = time + IR_MAX_PULSE_WIDTH;
285
        IR_UNMASK_TIMEOUT();
251
        IR_UNMASK_TIMEOUT();
286
 
-
 
287
        //TODO: if buffer resource coordinator is implemented this is where a buffer should be assinged to this channel
-
 
288
    }
252
    }
289
}
253
}
290
#endif
254
#endif
291
 
255
 
292
 
256
 
Line 319... Line 283...
319
        *ddr &= ~(1 << nr);
283
        *ddr &= ~(1 << nr);
320
       
284
       
321
        drvIrRxChannel[channel].rxbuf = buffer;
285
        drvIrRxChannel[channel].rxbuf = buffer;
322
        drvIrRxChannel[channel].rxlen = 0;
286
        drvIrRxChannel[channel].rxlen = 0;
323
        drvIrRxChannel[channel].storeEnable = FALSE;
287
        drvIrRxChannel[channel].storeEnable = FALSE;
324
        drvIrRxChannel[channel].timeoutEnable = FALSE;
-
 
325
        drvIrRxChannel[channel].callback = callback;
288
        drvIrRxChannel[channel].callback = callback;
326
        drvIrRxChannel[channel].enable = TRUE;
289
        drvIrRxChannel[channel].enable = TRUE;
327
    }
290
    }
328
}
291
}
329
 
292
 
Line 340... Line 303...
340
    {
303
    {
341
        /* Deactivate channel */
304
        /* Deactivate channel */
342
        drvIrRxChannel[channel].enable = FALSE;
305
        drvIrRxChannel[channel].enable = FALSE;
343
        drvIrRxChannel[channel].rxlen = 0;
306
        drvIrRxChannel[channel].rxlen = 0;
344
        drvIrRxChannel[channel].storeEnable = FALSE;
307
        drvIrRxChannel[channel].storeEnable = FALSE;
345
        drvIrRxChannel[channel].timeoutEnable = FALSE;
-
 
346
        drvIrRxChannel[channel].callback = 0;
308
        drvIrRxChannel[channel].callback = 0;
347
    }
309
    }
348
}
310
}
349
 
311
 
350
uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel)
312
uint8_t IrTransceiver_GetStoreEnableRx(uint8_t channel)
Line 375... Line 337...
375
    if (channel < 3)
337
    if (channel < 3)
376
    {
338
    {
377
        /* Reset channel */
339
        /* Reset channel */
378
        drvIrRxChannel[channel].rxlen = 0;
340
        drvIrRxChannel[channel].rxlen = 0;
379
        drvIrRxChannel[channel].storeEnable = FALSE;
341
        drvIrRxChannel[channel].storeEnable = FALSE;
380
        drvIrRxChannel[channel].timeoutEnable = FALSE;
-
 
381
    }
342
    }
382
}
343
}
383
#endif
344
#endif
384
 
345
 
385
 
346
 
Line 389... Line 350...
389
 
350
 
390
#if IR_RX_ENABLE==1
351
#if IR_RX_ENABLE==1
391
    for (uint8_t i = 0; i < 3; i++)
352
    for (uint8_t i = 0; i < 3; i++)
392
    {
353
    {
393
        drvIrRxChannel[i].storeEnable = FALSE;
354
        drvIrRxChannel[i].storeEnable = FALSE;
394
        drvIrRxChannel[i].timeoutEnable = FALSE;
-
 
395
        drvIrRxChannel[i].rxlen = 0;
355
        drvIrRxChannel[i].rxlen = 0;
396
    }
356
    }
397
#endif
357
#endif
398
   
358
   
399
#if IR_TX_ENABLE==1
359
#if IR_TX_ENABLE==1