Subversion Repositories HomeAutomation

Rev

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

Rev 1541 Rev 1542
Line 1... Line 1...
1
 
1
 
2
#include "sns_inputAnalog.h"
2
#include "sns_inputAnalog.h"
3
//TODO: Config over CAN, save to eeprom, pullups, references
3
//TODO: pullups, references
4
 
4
 
5
struct {
5
struct {
6
    uint8_t             Status;         //Used for digital input, high or low
6
    uint8_t             Status;         //Used for digital input, high or low
7
    uint16_t            PeriodCnt;      //Counter for periodicity
7
    uint16_t            PeriodCnt;      //Counter for periodicity
8
    uint16_t            LastSentAdVal;  //Remember the last sent AD value
8
    uint16_t            LastSentAdVal;  //Remember the last sent AD value
Line 62... Line 62...
62
#endif
62
#endif
63
    },
63
    },
64
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
64
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
65
};
65
};
66
#endif
66
#endif
-
 
67
 
-
 
68
void setPullups(void)
-
 
69
{
-
 
70
   
-
 
71
}
-
 
72
 
-
 
73
void setReferences(void)
-
 
74
{
-
 
75
   
-
 
76
}
67
 
77
 
68
void sns_inputAnalog_Init(void)
78
void sns_inputAnalog_Init(void)
69
{
79
{
70
#ifdef sns_inputAnalog_USEEEPROM
80
#ifdef sns_inputAnalog_USEEEPROM
71
    if (EEDATA_OK)
81
    if (EEDATA_OK)
Line 91... Line 101...
91
           
101
           
92
#if ((__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >= 7)||(__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1)
102
#if ((__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >= 7)||(__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1)
93
            eeprom_update_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) );
103
            eeprom_update_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) );
94
#else
104
#else
95
            eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) );
105
            eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) );
96
            #warning Using old version of AVRlibc
106
            #warning Using old version of AVRlibc, does not support eeprom_update-functions
97
#endif
107
#endif
98
        }
108
        }
99
        EEDATA_UPDATE_CRC;
109
        EEDATA_UPDATE_CRC;
100
    }
110
    }
101
#endif  
111
#endif  
102
 
112
 
-
 
113
    /* Initiate ADC */
103
    ADC_Init();
114
    ADC_Init();
-
 
115
    /* Start timer for reading inputs */
104
    Timer_SetTimeout(sns_inputAnalog_TIMER, sns_inputAnalog_POLL_PERIOD_MS , TimerTypeFreeRunning, 0);
116
    Timer_SetTimeout(sns_inputAnalog_TIMER, sns_inputAnalog_POLL_PERIOD_MS , TimerTypeFreeRunning, 0);
-
 
117
    /* Set pullups according config */
-
 
118
    setPullups();
-
 
119
    /* Set gnd references according config */
-
 
120
    setReferences();
105
}
121
}
106
 
122
 
107
void sns_inputAnalog_Process(void)
123
void sns_inputAnalog_Process(void)
108
{
124
{
109
    /* When the timer has overflowed the AD channels shall be read */
125
    /* When the timer has overflowed the AD channels shall be read */
Line 144... Line 160...
144
                /* If periodicity overflowed or AD value changed more than threshold since last sent value */
160
                /* If periodicity overflowed or AD value changed more than threshold since last sent value */
145
                if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity ||
161
                if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity ||
146
                    MAX(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal)-MIN(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh)
162
                    MAX(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal)-MIN(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh)
147
                //if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity ||
163
                //if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity ||
148
                //  abs(AdValue-sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh)
164
                //  abs(AdValue-sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh)
149
                {
165
                {
150
                    /* Reset periodicity counter */
166
                    /* Reset periodicity counter */
151
                    sns_inputAnalog_Sensor[i].PeriodCnt = 0;
167
                    sns_inputAnalog_Sensor[i].PeriodCnt = 0;
152
                    /* Store value as last sent */
168
                    /* Store value as last sent */
153
                    sns_inputAnalog_Sensor[i].LastSentAdVal = AdValue;
169
                    sns_inputAnalog_Sensor[i].LastSentAdVal = AdValue;
154
                   
170
                   
Line 213... Line 229...
213
                    /* send status on CAN with command CAN_MODULE_CMD_PHYSICAL_PINSTATUS */
229
                    /* send status on CAN with command CAN_MODULE_CMD_PHYSICAL_PINSTATUS */
214
                    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
230
                    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
215
                }
231
                }
216
            }
232
            }
217
        }
233
        }
218
 
-
 
219
    }
234
    }
220
}
235
}
221
 
236
 
222
void sns_inputAnalog_HandleMessage(StdCan_Msg_t *rxMsg)
237
void sns_inputAnalog_HandleMessage(StdCan_Msg_t *rxMsg)
223
{
238
{
224
/*  if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
239
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
225
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
240
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
226
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_INPUTANALOG &&
241
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_INPUTANALOG &&
227
        rxMsg->Header.ModuleId == sns_inputAnalog_ID)
242
        rxMsg->Header.ModuleId == sns_inputAnalog_ID)
228
    {
243
    {
-
 
244
        uint8_t index=0;
229
        switch (rxMsg->Header.Command)
245
        switch (rxMsg->Header.Command)
230
        {
246
        {
231
        case CAN_CMD_MODULE_DUMMY:
247
        case CAN_MODULE_CMD_INPUTANALOG_ANALOGCONFIG:
-
 
248
            /* Find sensor id from incoming data */
-
 
249
            index=rxMsg->Data[0]&0x0f;
-
 
250
            /* Check if sensor id is in range */
-
 
251
            if (index < sns_inputAnalog_NUM_SUPPORTED)
-
 
252
            {
-
 
253
                /* Save all config values */
-
 
254
                sns_inputAnalog_Config[index].Type=(rxMsg->Data[0]>>4)&0x03;
-
 
255
                sns_inputAnalog_Config[index].PullupEnable=(rxMsg->Data[0]>>6)&0x01;
-
 
256
                sns_inputAnalog_Config[index].RefEnable=(rxMsg->Data[0]>>7)&0x01;
-
 
257
                sns_inputAnalog_Config[index].LowTh=(rxMsg->Data[2])|(rxMsg->Data[1]<<8);
-
 
258
                sns_inputAnalog_Config[index].HighTh=(rxMsg->Data[4])|(rxMsg->Data[3]<<8);
-
 
259
                sns_inputAnalog_Config[index].Periodicity=(rxMsg->Data[6])|(rxMsg->Data[5]<<8);
-
 
260
               
-
 
261
                /* Convert voltage to AD value (0-1023) acording static config parameters */
-
 
262
                switch (index)
-
 
263
                {
-
 
264
                    case 0:
-
 
265
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh<<(sns_inputAnalog0Scale-8);
-
 
266
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh / sns_inputAnalog0Factor;
-
 
267
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh<<(sns_inputAnalog0Scale-8);
-
 
268
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh / sns_inputAnalog0Factor;
-
 
269
                        break;
-
 
270
                    case 1:
-
 
271
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh<<(sns_inputAnalog1Scale-8);
-
 
272
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh / sns_inputAnalog1Factor;
-
 
273
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh<<(sns_inputAnalog1Scale-8);
-
 
274
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh / sns_inputAnalog1Factor;
-
 
275
                        break;
-
 
276
                    case 2:
-
 
277
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh<<(sns_inputAnalog2Scale-8);
-
 
278
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh / sns_inputAnalog2Factor;
-
 
279
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh<<(sns_inputAnalog2Scale-8);
-
 
280
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh / sns_inputAnalog2Factor;
-
 
281
                        break;
-
 
282
                    case 3:
-
 
283
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh<<(sns_inputAnalog3Scale-8);
-
 
284
                        sns_inputAnalog_Config[index].LowTh = sns_inputAnalog_Config[index].LowTh / sns_inputAnalog3Factor;
-
 
285
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh<<(sns_inputAnalog3Scale-8);
-
 
286
                        sns_inputAnalog_Config[index].HighTh = sns_inputAnalog_Config[index].HighTh / sns_inputAnalog3Factor;
-
 
287
                        break;
-
 
288
                }
-
 
289
               
232
        ///TODO: Do something dummy
290
                /* Store config to eeprom */
-
 
291
#if ((__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >= 7)||(__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1)
-
 
292
                eeprom_update_block( &sns_inputAnalog_Config[index], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*index, sizeof(sns_inputAnalog_Config) );
-
 
293
#else
-
 
294
                eeprom_write_block( &sns_inputAnalog_Config[index], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*index, sizeof(sns_inputAnalog_Config) );
-
 
295
                #warning Using old version of AVRlibc, does not support eeprom_update-functions
-
 
296
#endif
-
 
297
            }
233
        break;
298
            break;
234
        }
299
        }
-
 
300
        /* Set pullups according config */
-
 
301
        setPullups();
-
 
302
        /* Set gnd references according config */
-
 
303
        setReferences();
235
    }*/
304
    }
236
}
305
}
237
 
306
 
238
void sns_inputAnalog_List(uint8_t ModuleSequenceNumber)
307
void sns_inputAnalog_List(uint8_t ModuleSequenceNumber)
239
{
308
{
240
    StdCan_Msg_t txMsg;
309
    StdCan_Msg_t txMsg;