Subversion Repositories HomeAutomation

Rev

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

Rev 1875 Rev 1931
Line 97... Line 97...
97
 
97
 
98
 
98
 
99
/*-----------------------------------------------------------------------------
99
/*-----------------------------------------------------------------------------
100
 * Interrupt Handlers
100
 * Interrupt Handlers
101
 *---------------------------------------------------------------------------*/
101
 *---------------------------------------------------------------------------*/
-
 
102
 
-
 
103
 /* TRANSMIT */
102
#if IR_TX_ENABLE==1
104
#if IR_TX_ENABLE==1
103
ISR(IR_COMPARE_VECTOR)
105
ISR(IR_COMPARE_VECTOR)
104
{
106
{
105
    /* find which channel */
107
    /* find which channel */
106
    uint16_t time = IR_COMPARE_REG;
108
    uint16_t time = IR_COMPARE_REG;
Line 141... Line 143...
141
                    /* notify the application that a pulse train has been sent. */
143
                    /* notify the application that a pulse train has been sent. */
142
                    drvIrTxChannel[channel].callback(channel);
144
                    drvIrTxChannel[channel].callback(channel);
143
                }
145
                }
144
            }
146
            }
145
        }
147
        }
146
    }
148
    }
147
   
149
   
148
    channel = 0;
150
    channel = 0;
149
    diff = 0xffff;
151
    diff = 0xffff;
150
    /* go through all channels and find the one that have the next overflow */
152
    /* go through all channels and find the one that have the next overflow */
151
    for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
153
    for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
152
    {
154
    {
153
        /* if channel is waiting for an overflow */
155
        /* if channel is waiting for an overflow */
154
        if (drvIrTxChannel[i].timeoutEnable==TRUE)
156
        if (drvIrTxChannel[i].timeoutEnable==TRUE)
155
        {
157
        {
156
            if (drvIrTxChannel[i].timeout - time < diff)
158
            if (drvIrTxChannel[i].timeout - time < diff)
157
            {
159
            {
158
                diff = drvIrTxChannel[i].timeout - time;
160
                diff = drvIrTxChannel[i].timeout - time;
159
                channel = i;
161
                channel = i;
160
            }
162
            }
161
            /* enable overflow interrupt */
163
            /* enable overflow interrupt */
162
            IR_UNMASK_COMPARE();
164
            IR_UNMASK_COMPARE();
163
        }      
165
        }      
164
    }  
166
    }  
165
 
167
 
166
    /* set compare value to the channel that have the next overflow */
168
    /* set compare value to the channel that have the next overflow */
167
    IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
169
    IR_COMPARE_REG = drvIrTxChannel[channel].timeout;
168
}
170
}
169
#endif
171
#endif
170
 
172
 
171
 
173
 
172
#if IR_RX_ENABLE==1
174
#if IR_RX_ENABLE==1
-
 
175
/* RECEIVE */
173
/* When this timeout occurs a pulsetrain is complete */
176
/* When this timeout occurs a pulsetrain is complete */
174
ISR(IR_TIMEOUT_VECTOR)
177
ISR(IR_TIMEOUT_VECTOR)
175
{
178
{
-
 
179
#if IR_RX_CONTINUOUS_MODE==0
176
    for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
180
    for (uint8_t i=0; i < IR_SUPPORTED_NUM_CHANNELS; i++)
177
    {
181
    {
178
        /* If more than 2 edges were recevied */
182
        /* If more than 2 edges were recevied */
179
        if (drvIrRxChannel[i].storeEnable == TRUE && drvIrRxChannel[i].rxlen > 2)
183
        if (drvIrRxChannel[i].storeEnable == TRUE && drvIrRxChannel[i].rxlen > 2)
180
        {
184
        {
181
            /* Notify the application that a pulse train has been received. */
185
            /* Notify the application that a pulse train has been received. */
182
            drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
186
            drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
183
        }
187
        }
184
 
188
 
185
        drvIrRxChannel[i].storeEnable = FALSE;
189
        drvIrRxChannel[i].storeEnable = FALSE;
186
    }
190
    }
187
 
191
 
188
    /* Disable ISR */
192
    /* Disable ISR */
189
    IR_MASK_TIMEOUT();
193
    IR_MASK_TIMEOUT();
190
   
194
#endif
191
//  gpio_toggle_pin(EXP_J);
195
//  gpio_toggle_pin(EXP_J);
192
}
196
}
193
#endif
197
#endif
194
 
198
 
195
 
199
 
196
/*-----------------------------------------------------------------------------
200
/*-----------------------------------------------------------------------------
197
 * Private Functions
201
 * Private Functions
198
 *---------------------------------------------------------------------------*/
202
 *---------------------------------------------------------------------------*/
199
 
203
 
200
#if IR_RX_ENABLE==1
204
#if IR_RX_ENABLE==1
201
 
205
 
202
/* these wrapper functions are called from the pcint driver (callback) */
206
/* these wrapper functions are called from the pcint driver (callback) */
203
void IrTransceiver_Store_ch0(uint8_t id, uint8_t status)
207
void IrTransceiver_Store_ch0(uint8_t id, uint8_t status)
204
{
208
{
205
    IrTransceiver_Store(0);
209
    IrTransceiver_Store(0);
206
}
210
}
207
void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
211
void IrTransceiver_Store_ch1(uint8_t id, uint8_t status)
208
{
212
{
209
    IrTransceiver_Store(1);
213
    IrTransceiver_Store(1);
210
}
214
}
211
void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
215
void IrTransceiver_Store_ch2(uint8_t id, uint8_t status)
212
{
216
{
213
    IrTransceiver_Store(2);
217
    IrTransceiver_Store(2);
214
}
218
}
215
 
219
 
216
void IrTransceiver_Store(uint8_t channel)
220
void IrTransceiver_Store(uint8_t channel)
217
{
221
{
218
    static uint16_t prev_time[3];
222
    static uint16_t prev_time[3];
219
    uint16_t pulsewidth;
223
    uint16_t pulsewidth;
220
 
224
 
221
    /* Read the timer counter register to get the current "time". */
225
    /* Read the timer counter register to get the current "time". */
222
    uint16_t time = IR_COUNT_REG;
226
    uint16_t time = IR_COUNT_REG;
223
   
227
   
224
    /* Subtract the current measurement from the previous to get the pulse width. */
228
    /* Subtract the current measurement from the previous to get the pulse width. */
225
    pulsewidth = time - prev_time[channel];
229
    pulsewidth = time - prev_time[channel];
226
    prev_time[channel] = time;
230
    prev_time[channel] = time;
227
 
231
 
228
#if IR_MIN_STARTPULSE_WIDTH>0
232
#if IR_MIN_STARTPULSE_WIDTH>0
229
    if ((pulsewidth <= IR_MIN_STARTPULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE) && (drvIrRxChannel[channel].rxlen == 0))
233
    if ((pulsewidth <= IR_MIN_STARTPULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE) && (drvIrRxChannel[channel].rxlen == 0))
230
    {
234
    {
231
        IR_MASK_TIMEOUT();
235
        IR_MASK_TIMEOUT();
232
        return;
236
        return;
233
    }
237
    }
-
 
238
    else
-
 
239
    {
-
 
240
        IR_UNMASK_TIMEOUT();
-
 
241
    }
-
 
242
#endif
-
 
243
//gpio_toggle_pin(EXP_K);
-
 
244
 
-
 
245
#if IR_RX_CONTINUOUS_MODE==0
-
 
246
/* in continuous mode when short pulse arrives received len should be set to zero */
-
 
247
//TODO
-
 
248
    if ((pulsewidth <= IR_MIN_PULSE_WIDTH) && (drvIrRxChannel[channel].storeEnable == TRUE))
-
 
249
    {
-
 
250
        drvIrRxChannel[channel].rxlen = 0;
-
 
251
        //IR_MASK_TIMEOUT();
-
 
252
        return;
-
 
253
    }
234
    else
254
    else
235
    {
255
    {
236
        IR_UNMASK_TIMEOUT();
256
        //IR_UNMASK_TIMEOUT();
237
    }
257
    }
238
#endif
258
#endif
239
//gpio_toggle_pin(EXP_K);
-
 
240
 
259
 
241
 
260
 
242
    if (drvIrRxChannel[channel].storeEnable)
261
    if (drvIrRxChannel[channel].storeEnable)
243
    {
262
    {
244
        /* Store the measurement. */
263
        /* Store the measurement. */
Line 250... Line 269...
250
        {
269
        {
251
            drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
270
            drvIrRxChannel[channel].rxlen = MAX_NR_TIMES-1;
252
        }
271
        }
253
        else
272
        else
254
        {
273
        {
-
 
274
#if IR_RX_CONTINUOUS_MODE==0
255
            /* Set the timeout for detection of the end of the pulse train. */
275
            /* Set the timeout for detection of the end of the pulse train. */
256
            drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
276
            drvIrRxChannel[channel].timeout = time + IR_MAX_PULSE_WIDTH;
257
            IR_TIMEOUT_REG = drvIrRxChannel[channel].timeout;
277
            IR_TIMEOUT_REG = drvIrRxChannel[channel].timeout;
-
 
278
#else           
-
 
279
            /* Notify the application that a pulse has been received. */
-
 
280
            drvIrRxChannel[i].callback(i, drvIrRxChannel[i].rxbuf, drvIrRxChannel[i].rxlen);
-
 
281
#endif
-
 
282
 
-
 
283
        }
258
        }
284
    }
259
    }
-
 
260
    else if (drvIrRxChannel[channel].enable == TRUE)
285
    else if (drvIrRxChannel[channel].enable == TRUE)
261
    {
286
    {
262
        /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
287
        /* The first edge of the pulse train has been detected. Enable the storage of the following pulsewidths. */
263
        drvIrRxChannel[channel].storeEnable = TRUE;
288
        drvIrRxChannel[channel].storeEnable = TRUE;
264
        drvIrRxChannel[channel].rxlen = 0;
289
        drvIrRxChannel[channel].rxlen = 0;
265
 
290
 
-
 
291
#if IR_RX_CONTINUOUS_MODE==0
266
        /* Enable timeout interrupt for detection of the end of the pulse train. */
292
        /* Enable timeout interrupt for detection of the end of the pulse train. */
267
        IR_TIMEOUT_REG = time + IR_MAX_PULSE_WIDTH;
293
        IR_TIMEOUT_REG = time + IR_MAX_PULSE_WIDTH;
268
        IR_UNMASK_TIMEOUT();
294
        IR_UNMASK_TIMEOUT();
-
 
295
#endif
269
    }
296
    }
270
}
297
}
271
#endif
298
#endif
272
 
299
 
273
 
300