analog.c 13.7 KB
Newer Older
springob's avatar
springob committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*  BSD-License:

Copyright (c) 2016 by Nils Springob, nicai-systems, Germany

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  * Neither the name nicai-systems nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*! @file    analog.c
 *  @brief   Zuordnung der physikalischen Pins zu symbolischen Namen
 *  @author  Nils Springob (nils.springob@nicai-systems.com)
 *  @date    2015-02-25
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/atomic.h>

#include "iodefs.h"
#include "analog.h"


#ifdef __cplusplus
extern "C" {
#endif

extern uint8_t _bob3_revision;
springob's avatar
springob committed
52
extern uint8_t _arm_mode;
springob's avatar
springob committed
53 54 55 56

uint16_t analog_samples[2 * ANALOG_CNT];

uint16_t analog_random_seed;
springob's avatar
springob committed
57 58
//uint8_t analog_pos;
//uint8_t analog_flags;
springob's avatar
springob committed
59 60 61
volatile uint8_t analog_sample_id;
uint16_t analog_sum;

springob's avatar
springob committed
62 63 64 65 66
// Interrupt
uint8_t analog_ui_ch;
uint8_t analog_ui_cnt;
uint8_t analog_first = 1;

springob's avatar
springob committed
67 68 69 70 71 72 73 74
#define AREF_1V1(x) (_BV(REFS1)|_BV(REFS0)|((x)))
#define AREF_AVCC(x) (_BV(REFS0)|((x)))
#define AREF_EXT(x)  ((x))
#define AMUX_TEMP 0x08
#define AMUX_1V1  0x0e
#define AMUX_0V   0x0f

void analog_init() {
springob's avatar
springob committed
75 76
  analog_first = 1;
  analog_ui_ch = 3;
springob's avatar
springob committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#if defined(DIDR0)
//  DIDR0 = 0xff;
#endif

  ADCSRA = _BV(ADPS2)  // prescale faktor = 64 ADC laeuft
         //| _BV(ADPS1)  // mit 8 MHz / 64 = 125 kHz
	 | _BV(ADPS0)  // mit 8 MHz / 32 = 250 kHz
         | _BV(ADEN)   // ADC an
//         | _BV(ADATE)  // auto trigger
         | _BV(ADIE)   // enable interrupt
         | _BV(ADSC);  // Beginne mit der Konvertierung

  DDRC |= _BV(2) | _BV(3) | _BV(4) | _BV(5);
  DDRB |= _BV(0);

  PORTC |= _BV(0) | _BV(1); // PullUp!
  
  activate_output_bit(IO_ARM_1R);
  activate_output_bit(IO_ARM_1C);
  activate_output_bit(IO_ARM_2R);
  activate_output_bit(IO_ARM_2C);
  activate_output_bit(IO_EN_IR);
  
  if (_bob3_revision==103) {
    deactivate_output_bit(IO_HEAD_S);
  }
  
  if (SREG&_BV(7)) {
    // measure all once!
    analog_wait_update();
  } else {
    // precharge Key and Voltage input
    analog_samples[ANALOG_VOLT] = analog_samples[ANALOG_VOLT+ANALOG_CNT] = (ADC_BANDGAP_CHANNEL_VOLTAGE*1024)/5;
  }
}

uint8_t analog_enable(uint8_t enable) {
  uint8_t result = (ADCSRA & _BV(ADIE))?0xff:0x00;
  if (enable) {
springob's avatar
springob committed
116 117 118 119 120
    ADCSRA = _BV(ADPS2)  // prescale faktor = 64 ADC laeuft
           | _BV(ADPS0)  // mit 8 MHz / 32 = 250 kHz
           | _BV(ADEN)   // ADC an
           | _BV(ADIE)   // enable interrupt
           | _BV(ADSC);  // Beginne mit der Konvertierung
springob's avatar
springob committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  } else {
    ADCSRA &= ~_BV(ADIE);
    ADCSRA &= ~_BV(ADEN);
  }
  return result;
}  

uint16_t analog_getValue(uint8_t idx) {
  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  uint16_t retval = 0;
  {
    uint8_t bak = SREG;
    cli();
    retval = analog_samples[idx];
    SREG = bak;
  }
  return retval;
}


uint16_t analog_getValueExt(uint8_t idx, uint8_t active) {
  if (active==0) {
    //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    uint16_t retval;
    {
      uint8_t bak = SREG;
      cli();
      retval = analog_samples[idx];
      SREG = bak;
    }
    return retval;

  } else if (active==1) {
    idx += ANALOG_CNT;
    //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    uint16_t retval;
    {
      uint8_t bak = SREG;
      cli();
      retval = analog_samples[idx];
      SREG = bak;
    }
    return retval;

  } else if (active==2) {
    uint8_t idx2 = idx+ANALOG_CNT;
    int16_t val = 0;
    //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    if (idx<8) {
      // inverted signal: 1023 == no touch
      uint8_t bak = SREG;
      cli();
      val = analog_samples[idx];
      val -= analog_samples[idx2];
      SREG = bak;
    } else {
      uint8_t bak = SREG;
      cli();
      val = analog_samples[idx2];
      val -= analog_samples[idx];
      SREG = bak;
    }
    return (val<0)?0:val;
  }
  return 0;
}

uint16_t analog_getRandomSeed() {
  //random_r(unsigned long *  __ctx);
  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  uint16_t retval;
  {
    uint8_t bak = SREG;
    cli();
    retval = analog_random_seed;
    SREG = bak;
  }
  return retval;
}


void analog_wait_update() {
  uint8_t id = analog_sample_id;
  while (id == analog_sample_id) {
  }
}


uint8_t analog_has_update() {
  static uint8_t id = 0;
  if (id == analog_sample_id) {
    return 0;
  }
  id = analog_sample_id;
  return 1;
}

#define KEY_THRESHOLD 50
#define KEY_COUNT 5

static uint8_t updateKey (uint8_t state, uint8_t value) {
  if (value>KEY_THRESHOLD) {
    if (state&0x01) {
      return 1;
      //return (state>1)?(state-2):1;
    } else {
      return (state>=(2*KEY_COUNT-2))?1:(state+2);
    }
  } else {
    if (state&0x01) {
      return (state>(2*KEY_COUNT-2))?0:(state+2);
    } else {
      return 0;
      //return (state>1)?(state-2):0;
    }    
  }
}



void analog_irq_hook() __attribute__((weak));
void analog_irq_hook() {
}

void _key_update_state() __attribute__((weak));
void _key_update_state() {
}

void _key_update_event() __attribute__((weak));
void _key_update_event() {
}

#if F_CPU==8000000
#define STD0 0
#define STD2 2
#define STD3 3
#define STD4 4
#define STD5 5
#define STD6 6

#elif F_CPU==15000000
#define STD0 0
#define STD2 2
#define STD3 3
#define STD4 4
#define STD5 5
#define STD6 6

#elif F_CPU==20000000
#define STD0 0
#define STD2 3
#define STD3 4
#define STD4 5
#define STD5 7
#define STD6 8

#else
#error "unsupported clock speed!"
#endif


springob's avatar
springob committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
// **** IR Sensor ****
static inline void analog_ai_ir(uint16_t value) {
  if (analog_ui_cnt==0) {
    ADMUX = AREF_1V1(IOG_ANALOG_BIT_SENS_IR);
  } else if (analog_ui_cnt==6) {
    ADMUX = AREF_1V1(IOG_ANALOG_BIT_SENS_IR);
    PORTC &= ~_BV(IOG_ANALOG_BIT_SENS_IR);
    DDRC |= _BV(IOG_ANALOG_BIT_SENS_IR);
    DDRC &= ~_BV(IOG_ANALOG_BIT_SENS_IR);
    PORTC |= _BV(IOG_ANALOG_BIT_SENS_IR);
  } else if (analog_ui_cnt==8) {
    analog_sum = value;
  } else if (analog_ui_cnt==9) {
    analog_sum += value;
  } else if (analog_ui_cnt==10) {
    analog_sum += value;
  } else if (analog_ui_cnt==11) {
    analog_sum += value;
    analog_samples[ANALOG_IR] = analog_sum/4;
    activate_output_bit(IO_EN_IR);
    set_output_bit(IO_EN_IR);
  } else if (analog_ui_cnt==14) {
    analog_sum = value;
  } else if (analog_ui_cnt==15) {
    analog_sum += value;
  } else if (analog_ui_cnt==16) {
    analog_sum += value;
  } else if (analog_ui_cnt==17) {
    analog_sum += value;
    analog_samples[ANALOG_IR+ANALOG_CNT] = analog_sum/4;
    clear_output_bit(IO_EN_IR);
    analog_ui_cnt = 0;
    return;
  }
  analog_ui_cnt++;
}


// **** Temperature Sensor ****
static inline void analog_ai_temp(uint16_t value) {
  if (analog_ui_cnt==0) {
    ADMUX = AREF_1V1(AMUX_TEMP);
  } else if (analog_ui_cnt==4) {
    analog_sum = value;
  } else if (analog_ui_cnt==5) {
    analog_sum += value;
  } else if (analog_ui_cnt==6) {
    analog_sum += value;
  } else if (analog_ui_cnt==7) {
    analog_sum += value;
    value = analog_sum;
    analog_samples[ANALOG_TEMP] = value;
    value += 7*analog_samples[ANALOG_CNT+ANALOG_TEMP] + 4;
    value /= 8;
    analog_samples[ANALOG_CNT+ANALOG_TEMP] = value;
    analog_ui_cnt = 0;
    return;
  }
  analog_ui_cnt++;
}


// **** Supply Voltage Sensor ****
static inline void analog_ai_volt(uint16_t value) {
  if (analog_ui_cnt==0) {
    ADMUX = AREF_AVCC(AMUX_1V1);
  } else if (analog_ui_cnt==2) {
    analog_samples[ANALOG_VOLT] = value;
    analog_ui_cnt = 0;
    return;
  }
  analog_ui_cnt++;
}

springob's avatar
springob committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

      
#define CASE_ADC_KEY(CASE, ANALOG_CH, IO_EN_BIT, AREF, APORT) \
    case CASE+STD0: \
      ADMUX = AREF(APORT); \
      PORTC &= ~_BV(APORT);\
      DDRC |= _BV(APORT);\
      DDRC &= ~_BV(APORT);\
      PORTC |= _BV(APORT);\
      break; \
    case CASE+STD2: \
      analog_sum = value; \
      set_output_bit(IO_EN_BIT); \
      break; \
    case CASE+STD5: \
      analog_sum = (analog_sum>value)?(analog_sum-value):0; \
      analog_samples[ANALOG_CH+ANALOG_CNT]=analog_sum; \
      clear_output_bit(IO_EN_BIT); \
      analog_samples[ANALOG_CH]=updateKey(analog_samples[ANALOG_CH], analog_sum/4); \
      break;

springob's avatar
springob committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
   
// **** Supply Voltage Sensor ****
static inline void analog_ai_key_v102(uint16_t value) {
  switch (analog_ui_cnt) {
        CASE_ADC_KEY(0*STD6, ANALOG_L0,  IO_ARM_1R, AREF_AVCC, IOG_ANALOG_BIT_ARM_U)
        CASE_ADC_KEY(1*STD6, ANALOG_L1,  IO_ARM_1C, AREF_AVCC, IOG_ANALOG_BIT_ARM_U)
        CASE_ADC_KEY(2*STD6, ANALOG_L2,  IO_ARM_1C, AREF_AVCC, IOG_ANALOG_BIT_ARM_L)
        CASE_ADC_KEY(3*STD6, ANALOG_L3,  IO_ARM_1R, AREF_AVCC, IOG_ANALOG_BIT_ARM_L)
        CASE_ADC_KEY(4*STD6, ANALOG_R0,  IO_ARM_2R, AREF_AVCC, IOG_ANALOG_BIT_ARM_U)
        CASE_ADC_KEY(5*STD6, ANALOG_R1,  IO_ARM_2C, AREF_AVCC, IOG_ANALOG_BIT_ARM_U)
        CASE_ADC_KEY(6*STD6, ANALOG_R2,  IO_ARM_2C, AREF_AVCC, IOG_ANALOG_BIT_ARM_L)
        CASE_ADC_KEY(7*STD6, ANALOG_R3,  IO_ARM_2R, AREF_AVCC, IOG_ANALOG_BIT_ARM_L)
  }    
  analog_ui_cnt++;
  if (analog_ui_cnt >= 8*STD6) {
    analog_ui_cnt = 0;
  }
}      

// **** Supply Voltage Sensor ****
static inline void analog_ai_key_v103(uint16_t value) {
  switch (analog_ui_cnt) {
        CASE_ADC_KEY(0*STD6, ANALOG_L0,  IO_ARM2_TA, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S1)
        CASE_ADC_KEY(1*STD6, ANALOG_L1,  IO_ARM2_TB, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S1)
        CASE_ADC_KEY(2*STD6, ANALOG_L2,  IO_ARM2_TB, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S1)
        CASE_ADC_KEY(3*STD6, ANALOG_L3,  IO_ARM2_TC, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S1)
        CASE_ADC_KEY(4*STD6, ANALOG_R0,  IO_ARM2_TA, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S2)
        CASE_ADC_KEY(5*STD6, ANALOG_R1,  IO_ARM2_TB, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S2)
        CASE_ADC_KEY(6*STD6, ANALOG_R2,  IO_ARM2_TB, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S2)
        CASE_ADC_KEY(7*STD6, ANALOG_R3,  IO_ARM2_TC, AREF_AVCC, IOG_ANALOG_BIT_ARM2_S2)
  }  
  analog_ui_cnt++;
  if (analog_ui_cnt >= 8*STD6) {
    analog_ui_cnt = 0;
  }
}      

// **** Supply Voltage Sensor ****
static inline void analog_ai_key_detect(uint16_t value) {
  switch (analog_ui_cnt) {
        case 0:
          // set all arm output pins to vcc level
          set_output_bit(IO_ARM2_TA);
          set_output_bit(IO_ARM2_TB);
          set_output_bit(IO_ARM2_TC);
          activate_output_bit(IO_ARM2_TA);
          activate_output_bit(IO_ARM2_TB);
          activate_output_bit(IO_ARM2_TC);
          // enable pull-ups on arm input pins
          deactivate_output_bit(IO_ARM2_S1);
          deactivate_output_bit(IO_ARM2_S2);
          set_output_bit(IO_ARM2_S1);
          set_output_bit(IO_ARM2_S2);
          ADMUX = AREF_AVCC(IOG_ANALOG_BIT_ARM2_S1);
          break;

        case 2*STD6:
          analog_sum = value;
          break;
        case 2*STD6+1:
          analog_sum += value;
          break;
        case 2*STD6+2:
          analog_sum += value;
          break;
        case 2*STD6+3:
          analog_sum += value;
          break;
        case 2*STD6+4:
          analog_sum /= 2;
          analog_sum += 15*analog_samples[ANALOG_L0]+7;
          analog_sum /= 16;
          break;
        case 2*STD6+5:
          analog_samples[ANALOG_L0] = analog_sum;
          ADMUX = AREF_AVCC(IOG_ANALOG_BIT_ARM2_S2);
          break;

        case 4*STD6:
          analog_sum = value;
          break;
        case 4*STD6+1:  
          analog_sum += value;
          break;
        case 4*STD6+2:
          analog_sum += value;
          break;
        case 4*STD6+3:
          analog_sum += value;
          break;
        case 4*STD6+4:
          analog_sum /= 2;
          analog_sum += 15*analog_samples[ANALOG_R0]+7;
          analog_sum /= 16;
          break;
        case 4*STD6+5:
          analog_samples[ANALOG_R0] = analog_sum;
          break;
  }  
  analog_ui_cnt++;
  if (analog_ui_cnt >= 8*STD6) {
    analog_ui_cnt = 0;
  }
}

springob's avatar
springob committed
482 483 484 485 486


ISR(ADC_vect) {
  uint16_t value = ADC;
  sei();
springob's avatar
springob committed
487 488 489 490 491 492 493 494 495 496 497 498 499
  switch (analog_ui_ch) {
    // Vcc Voltage Ref
    case 0: analog_ai_key_v102(value); break;
    case 1: analog_ai_key_v103(value); break;
    case 2: analog_ai_key_detect(value); break;
    
    // 1.1V Voltage Ref
    case 3: analog_ai_ir(value); break;
    case 4: analog_ai_temp(value); break;
    
    // Vcc Voltage Ref
    case 5: analog_ai_volt(value); break;
  }
springob's avatar
springob committed
500
  
springob's avatar
springob committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  if (analog_ui_cnt==0) {
    if (analog_ui_ch<3) analog_ui_ch=3;
    else if (analog_ui_ch<5) analog_ui_ch++;
    else if (_bob3_revision==102) analog_ui_ch=0;
    else if (_arm_mode==1) analog_ui_ch=1;
    else if (_arm_mode==2) analog_ui_ch=2;
    else analog_ui_ch=0;
    
    if (analog_ui_ch<3) {
      analog_irq_hook();
      analog_sample_id++;
      analog_random_seed<<=1;
      if (analog_first) {
        analog_first = 0;
        analog_samples[ANALOG_TEMP+ANALOG_CNT] = analog_samples[ANALOG_TEMP];
      }
springob's avatar
springob committed
517 518
    }
  }
springob's avatar
springob committed
519
  
springob's avatar
springob committed
520 521 522 523 524 525 526 527 528 529
  analog_random_seed += value;
  ADCSRA |= _BV(ADSC);
}


#ifdef __cplusplus
} // extern "C"
#endif