Rev 1542 | Rev 1545 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1542 | Rev 1543 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | 1 | ||
| 2 | #include "sns_inputAnalog.h" |
2 | #include "sns_inputAnalog.h" |
| 3 | //TODO: pullups, references |
- | |
| 4 | 3 | ||
| 5 | struct { |
4 | struct { |
| 6 | uint8_t Status; //Used for digital input, high or low |
5 | uint8_t Status; //Used for digital input, high or low |
| 7 | uint16_t PeriodCnt; //Counter for periodicity |
6 | uint16_t PeriodCnt; //Counter for periodicity |
| 8 | uint16_t LastSentAdVal; //Remember the last sent AD value |
7 | uint16_t LastSentAdVal; //Remember the last sent AD value |
| Line 63... | Line 62... | ||
| 63 | }, |
62 | }, |
| 64 | 0 // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts |
63 | 0 // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts |
| 65 | }; |
64 | }; |
| 66 | #endif |
65 | #endif |
| 67 | 66 | ||
| - | 67 | /* Will set up pullup enable outputs according config */ |
|
| 68 | void setPullups(void) |
68 | void setPullups(void) |
| 69 | { |
69 | { |
| - | 70 | if (sns_inputAnalog_Config[0].PullupEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_PULLUP_ENABLE) |
|
| - | 71 | { |
|
| - | 72 | #if sns_inputAnalog0PullupPCA95xx==0 |
|
| - | 73 | gpio_set_pin(sns_inputAnalog0Pullup); |
|
| - | 74 | gpio_set_out(sns_inputAnalog0Pullup); |
|
| - | 75 | #else |
|
| - | 76 | Pca95xx_set_pin(sns_inputAnalog0Pullup); |
|
| - | 77 | Pca95xx_set_out(sns_inputAnalog0Pullup); |
|
| - | 78 | #endif |
|
| - | 79 | } |
|
| - | 80 | else |
|
| - | 81 | { |
|
| - | 82 | #if sns_inputAnalog0PullupPCA95xx==0 |
|
| - | 83 | gpio_clr_pin(sns_inputAnalog0Pullup); |
|
| - | 84 | gpio_set_out(sns_inputAnalog0Pullup); |
|
| - | 85 | #else |
|
| - | 86 | Pca95xx_clr_pin(sns_inputAnalog0Pullup); |
|
| - | 87 | Pca95xx_set_out(sns_inputAnalog0Pullup); |
|
| - | 88 | #endif |
|
| - | 89 | } |
|
| 70 | 90 | ||
| - | 91 | #if sns_inputAnalog_NUM_SUPPORTED>=2 |
|
| - | 92 | if (sns_inputAnalog_Config[1].PullupEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_PULLUP_ENABLE) |
|
| - | 93 | { |
|
| - | 94 | #if sns_inputAnalog1PullupPCA95xx==0 |
|
| - | 95 | gpio_set_pin(sns_inputAnalog1Pullup); |
|
| - | 96 | gpio_set_out(sns_inputAnalog1Pullup); |
|
| - | 97 | #else |
|
| - | 98 | Pca95xx_set_pin(sns_inputAnalog1Pullup); |
|
| - | 99 | Pca95xx_set_out(sns_inputAnalog1Pullup); |
|
| - | 100 | #endif |
|
| - | 101 | } |
|
| - | 102 | else |
|
| - | 103 | { |
|
| - | 104 | #if sns_inputAnalog1PullupPCA95xx==0 |
|
| - | 105 | gpio_clr_pin(sns_inputAnalog1Pullup); |
|
| - | 106 | gpio_set_out(sns_inputAnalog1Pullup); |
|
| - | 107 | #else |
|
| - | 108 | Pca95xx_clr_pin(sns_inputAnalog1Pullup); |
|
| - | 109 | Pca95xx_set_out(sns_inputAnalog1Pullup); |
|
| - | 110 | #endif |
|
| - | 111 | } |
|
| - | 112 | #endif |
|
| - | 113 | ||
| - | 114 | #if sns_inputAnalog_NUM_SUPPORTED>=3 |
|
| - | 115 | if (sns_inputAnalog_Config[2].PullupEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_PULLUP_ENABLE) |
|
| - | 116 | { |
|
| - | 117 | #if sns_inputAnalog2PullupPCA95xx==0 |
|
| - | 118 | gpio_set_pin(sns_inputAnalog2Pullup); |
|
| - | 119 | gpio_set_out(sns_inputAnalog2Pullup); |
|
| - | 120 | #else |
|
| - | 121 | Pca95xx_set_pin(sns_inputAnalog2Pullup); |
|
| - | 122 | Pca95xx_set_out(sns_inputAnalog2Pullup); |
|
| - | 123 | #endif |
|
| - | 124 | } |
|
| - | 125 | else |
|
| - | 126 | { |
|
| - | 127 | #if sns_inputAnalog2PullupPCA95xx==0 |
|
| - | 128 | gpio_clr_pin(sns_inputAnalog2Pullup); |
|
| - | 129 | gpio_set_out(sns_inputAnalog2Pullup); |
|
| - | 130 | #else |
|
| - | 131 | Pca95xx_clr_pin(sns_inputAnalog2Pullup); |
|
| - | 132 | Pca95xx_set_out(sns_inputAnalog2Pullup); |
|
| - | 133 | #endif |
|
| - | 134 | } |
|
| - | 135 | #endif |
|
| - | 136 | ||
| - | 137 | #if sns_inputAnalog_NUM_SUPPORTED>=4 |
|
| - | 138 | if (sns_inputAnalog_Config[3].PullupEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_PULLUP_ENABLE) |
|
| - | 139 | { |
|
| - | 140 | #if sns_inputAnalog3PullupPCA95xx==0 |
|
| - | 141 | gpio_set_pin(sns_inputAnalog3Pullup); |
|
| - | 142 | gpio_set_out(sns_inputAnalog3Pullup); |
|
| - | 143 | #else |
|
| - | 144 | Pca95xx_set_pin(sns_inputAnalog3Pullup); |
|
| - | 145 | Pca95xx_set_out(sns_inputAnalog3Pullup); |
|
| - | 146 | #endif |
|
| - | 147 | } |
|
| - | 148 | else |
|
| - | 149 | { |
|
| - | 150 | #if sns_inputAnalog3PullupPCA95xx==0 |
|
| - | 151 | gpio_clr_pin(sns_inputAnalog3Pullup); |
|
| - | 152 | gpio_set_out(sns_inputAnalog3Pullup); |
|
| - | 153 | #else |
|
| - | 154 | Pca95xx_clr_pin(sns_inputAnalog3Pullup); |
|
| - | 155 | Pca95xx_set_out(sns_inputAnalog3Pullup); |
|
| - | 156 | #endif |
|
| - | 157 | } |
|
| - | 158 | #endif |
|
| 71 | } |
159 | } |
| 72 | 160 | ||
| - | 161 | /* Will set up reference enable outputs according config */ |
|
| 73 | void setReferences(void) |
162 | void setReferences(void) |
| 74 | { |
163 | { |
| - | 164 | if (sns_inputAnalog_Config[0].RefEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_REFERENCE_ENABLE) |
|
| - | 165 | { |
|
| - | 166 | #if sns_inputAnalog0RefPCA95xx==0 |
|
| - | 167 | gpio_set_pin(sns_inputAnalog0Ref); |
|
| - | 168 | gpio_set_out(sns_inputAnalog0Ref); |
|
| - | 169 | #else |
|
| - | 170 | Pca95xx_set_pin(sns_inputAnalog0Ref); |
|
| - | 171 | Pca95xx_set_out(sns_inputAnalog0Ref); |
|
| - | 172 | #endif |
|
| - | 173 | } |
|
| - | 174 | else |
|
| - | 175 | { |
|
| - | 176 | #if sns_inputAnalog0RefPCA95xx==0 |
|
| - | 177 | gpio_clr_pin(sns_inputAnalog0Ref); |
|
| - | 178 | gpio_set_out(sns_inputAnalog0Ref); |
|
| - | 179 | #else |
|
| - | 180 | Pca95xx_clr_pin(sns_inputAnalog0Ref); |
|
| - | 181 | Pca95xx_set_out(sns_inputAnalog0Ref); |
|
| - | 182 | #endif |
|
| - | 183 | } |
|
| 75 | 184 | ||
| - | 185 | #if sns_inputAnalog_NUM_SUPPORTED>=2 |
|
| - | 186 | if (sns_inputAnalog_Config[1].RefEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_REFERENCE_ENABLE) |
|
| - | 187 | { |
|
| - | 188 | #if sns_inputAnalog1RefPCA95xx==0 |
|
| - | 189 | gpio_set_pin(sns_inputAnalog1Ref); |
|
| - | 190 | gpio_set_out(sns_inputAnalog1Ref); |
|
| - | 191 | #else |
|
| - | 192 | Pca95xx_set_pin(sns_inputAnalog1Ref); |
|
| - | 193 | Pca95xx_set_out(sns_inputAnalog1Ref); |
|
| - | 194 | #endif |
|
| - | 195 | } |
|
| - | 196 | else |
|
| - | 197 | { |
|
| - | 198 | #if sns_inputAnalog1RefPCA95xx==0 |
|
| - | 199 | gpio_clr_pin(sns_inputAnalog1Ref); |
|
| - | 200 | gpio_set_out(sns_inputAnalog1Ref); |
|
| - | 201 | #else |
|
| - | 202 | Pca95xx_clr_pin(sns_inputAnalog1Ref); |
|
| - | 203 | Pca95xx_set_out(sns_inputAnalog1Ref); |
|
| - | 204 | #endif |
|
| - | 205 | } |
|
| - | 206 | #endif |
|
| - | 207 | ||
| - | 208 | #if sns_inputAnalog_NUM_SUPPORTED>=3 |
|
| - | 209 | if (sns_inputAnalog_Config[2].RefEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_REFERENCE_ENABLE) |
|
| - | 210 | { |
|
| - | 211 | #if sns_inputAnalog2RefPCA95xx==0 |
|
| - | 212 | gpio_set_pin(sns_inputAnalog2Ref); |
|
| - | 213 | gpio_set_out(sns_inputAnalog2Ref); |
|
| - | 214 | #else |
|
| - | 215 | Pca95xx_set_pin(sns_inputAnalog2Ref); |
|
| - | 216 | Pca95xx_set_out(sns_inputAnalog2Ref); |
|
| - | 217 | #endif |
|
| - | 218 | } |
|
| - | 219 | else |
|
| - | 220 | { |
|
| - | 221 | #if sns_inputAnalog2RefPCA95xx==0 |
|
| - | 222 | gpio_clr_pin(sns_inputAnalog2Ref); |
|
| - | 223 | gpio_set_out(sns_inputAnalog2Ref); |
|
| - | 224 | #else |
|
| - | 225 | Pca95xx_clr_pin(sns_inputAnalog2Ref); |
|
| - | 226 | Pca95xx_set_out(sns_inputAnalog2Ref); |
|
| - | 227 | #endif |
|
| - | 228 | } |
|
| - | 229 | #endif |
|
| - | 230 | ||
| - | 231 | #if sns_inputAnalog_NUM_SUPPORTED>=4 |
|
| - | 232 | if (sns_inputAnalog_Config[3].RefEnable==CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_REFERENCE_ENABLE) |
|
| - | 233 | { |
|
| - | 234 | #if sns_inputAnalog3RefPCA95xx==0 |
|
| - | 235 | gpio_set_pin(sns_inputAnalog3Ref); |
|
| - | 236 | gpio_set_out(sns_inputAnalog3Ref); |
|
| - | 237 | #else |
|
| - | 238 | Pca95xx_set_pin(sns_inputAnalog3Ref); |
|
| - | 239 | Pca95xx_set_out(sns_inputAnalog3Ref); |
|
| - | 240 | #endif |
|
| - | 241 | } |
|
| - | 242 | else |
|
| - | 243 | { |
|
| - | 244 | #if sns_inputAnalog3RefPCA95xx==0 |
|
| - | 245 | gpio_clr_pin(sns_inputAnalog3Ref); |
|
| - | 246 | gpio_set_out(sns_inputAnalog3Ref); |
|
| - | 247 | #else |
|
| - | 248 | Pca95xx_clr_pin(sns_inputAnalog3Ref); |
|
| - | 249 | Pca95xx_set_out(sns_inputAnalog3Ref); |
|
| - | 250 | #endif |
|
| - | 251 | } |
|
| - | 252 | #endif |
|
| 76 | } |
253 | } |
| 77 | 254 | ||
| 78 | void sns_inputAnalog_Init(void) |
255 | void sns_inputAnalog_Init(void) |
| 79 | { |
256 | { |
| 80 | #ifdef sns_inputAnalog_USEEEPROM |
257 | #ifdef sns_inputAnalog_USEEEPROM |
| Line 86... | Line 263... | ||
| 86 | //eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog, sizeof(sns_inputAnalog_Config)*i ); |
263 | //eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog, sizeof(sns_inputAnalog_Config)*i ); |
| 87 | eeprom_read_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
264 | eeprom_read_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
| 88 | } |
265 | } |
| 89 | } |
266 | } |
| 90 | else |
267 | else |
| 91 | { |
268 | { |
| 92 | /* The CRC of the EEPROM is not correct, store default values and update CRC */ |
269 | /* The CRC of the EEPROM is not correct, store default values and update CRC */ |
| 93 | for (uint8_t i=0; i<sns_inputAnalog_NUM_SUPPORTED; i++) |
270 | for (uint8_t i=0; i<sns_inputAnalog_NUM_SUPPORTED; i++) |
| 94 | { |
271 | { |
| 95 | sns_inputAnalog_Config[i].LowTh=50; //Config, low level threshold voltage |
272 | sns_inputAnalog_Config[i].LowTh=50; //Config, low level threshold voltage |
| 96 | sns_inputAnalog_Config[i].HighTh=100; //Config, high level threshold voltage |
273 | sns_inputAnalog_Config[i].HighTh=100; //Config, high level threshold voltage |
| Line 102... | Line 279... | ||
| 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) |
279 | #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) |
| 103 | eeprom_update_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
280 | eeprom_update_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
| 104 | #else |
281 | #else |
| 105 | eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
282 | eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); |
| 106 | #warning Using old version of AVRlibc, does not support eeprom_update-functions |
283 | #warning Using old version of AVRlibc, does not support eeprom_update-functions |
| 107 | #endif |
284 | #endif |
| 108 | } |
285 | } |
| 109 | EEDATA_UPDATE_CRC; |
286 | EEDATA_UPDATE_CRC; |
| 110 | } |
287 | } |
| 111 | #endif |
288 | #endif |
| 112 | 289 | ||
| - | 290 | #if sns_inputAnalog_ENABLE_PCA95xx==1 |
|
| - | 291 | Pca95xx_Init(0); |
|
| - | 292 | #endif |
|
| 113 | /* Initiate ADC */ |
293 | /* Initiate ADC */ |
| 114 | ADC_Init(); |
294 | ADC_Init(); |
| 115 | /* Start timer for reading inputs */ |
295 | /* Start timer for reading inputs */ |
| 116 | Timer_SetTimeout(sns_inputAnalog_TIMER, sns_inputAnalog_POLL_PERIOD_MS , TimerTypeFreeRunning, 0); |
296 | Timer_SetTimeout(sns_inputAnalog_TIMER, sns_inputAnalog_POLL_PERIOD_MS , TimerTypeFreeRunning, 0); |
| 117 | /* Set pullups according config */ |
297 | /* Set pullups according config */ |