adapter/adapter.c File Reference

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/fuse.h>
#include <string.h>

Go to the source code of this file.

Defines

#define STATIC_ASSERT(expr)   extern char static_assert[ (!!(expr))*2 - 1]
#define elements_of(array)   (sizeof(array) / sizeof(array[0]))
#define EGO_RED_LED_IS_ON()   (!(PINB & _BV(PB5)))
#define EGO_GREEN_LED_IS_ON()   (!(PINA & _BV(PA7)))
#define EGO_MUTE_ON()   (!(PINB & _BV(PB3)))
#define EGO_X012(x)   PORTA = (PORTA & ~EGO_X012_INACTIVE) | ((x) & EGO_X012_INACTIVE)
#define EGO_INIT()   DDRA &= ~(_BV(PA7));DDRA |= EGO_X012_INACTIVE; PORTA |= _BV(PA7); DDRB &= ~(_BV(PB5) | _BV(PB3)); PORTB |= _BV(PB5);
#define BTN1_LED_GREEN_ON()   PORTA |= _BV(PA2)
#define BTN1_LED_GREEN_OFF()   PORTA &= ~_BV(PA2)
#define BTN1_LED_GREEN_INIT()   DDRA |= _BV(PA2);BTN1_LED_GREEN_OFF()
#define BTN2_LED_RED_ON()   PORTA |= _BV(PA0)
#define BTN2_LED_RED_OFF()   PORTA &= ~_BV(PA0)
#define BTN2_LED_RED_INIT()   DDRA |= _BV(PA0);BTN2_LED_RED_OFF()
#define BTN_GREEN_PRESSED()   (!(PINA & _BV(PA3)))
#define BTN_RED_PRESSED()   (!(PINA & _BV(PA1)))
#define BTN_INIT()   DDRA &= ~(_BV(PA3) | _BV(PA1));PORTA |= (_BV(PA3) | _BV(PA1))
#define VOLTAGE_TO_ADC_VALUE(v)   (uint16_t)(v * ((1UL<<16)-1) / (2.56 * 2))
 2.56 is ref voltage and /2 because of the external voltage divider
#define debug_voltage(v)
#define T1_OVERFLOW_TIME_MS   ((4UL*256UL*1000UL) / F_CPU)
#define MS_TO_TICKS(t)   (t/T1_OVERFLOW_TIME_MS)
#define VOLUME_ACTIVATE_TIMEOUT   MS_TO_TICKS(300)

Enumerations

enum  EGO_X012 {
  EGO_X012_INACTIVE = 1 * _BV(PA6) | 1 * _BV(PA5) | 1 * _BV(PA4), EGO_X012_PLAY_UNHOOK = 0 * _BV(PA6) | 0 * _BV(PA5) | 0 * _BV(PA4), EGO_X012_STOP_ONHOOK = 1 * _BV(PA6) | 0 * _BV(PA5) | 0 * _BV(PA4), EGO_X012_VOL_UP = 0 * _BV(PA6) | 0 * _BV(PA5) | 1 * _BV(PA4),
  EGO_X012_VOL_DOWN = 1 * _BV(PA6) | 0 * _BV(PA5) | 1 * _BV(PA4), EGO_X012_NEXT = 0 * _BV(PA6) | 1 * _BV(PA5) | 0 * _BV(PA4), EGO_X012_PREV = 1 * _BV(PA6) | 1 * _BV(PA5) | 0 * _BV(PA4)
}
 

The EGO multiplexes the buttons to a 3bit value, those signals are connected to PA6, PA5, PA4.

More...
enum  { STRW_UP_INPUT = 7, STRW_DOWN_INPUT = 9 }
 

ADC channels for the different steering wheel button resistor arrays.

More...
enum  { VOLTAGE1 = VOLTAGE_TO_ADC_VALUE(0.5), VOLTAGE2 = VOLTAGE_TO_ADC_VALUE(1.5), VOLTAGE3 = VOLTAGE_TO_ADC_VALUE(3.0), MUTE_ON_THRES = VOLTAGE_TO_ADC_VALUE(0.6) }
 

Different voltage zones for the steering wheel buttons.

More...
enum  btn_t { STRW_INACTIVE = 0, STRW_HIGH = 1, STRW_LOW = 2, STRW_GND = 3 }
 

Activation level on a steering wheel channel.

More...
enum  wheel_control_t {
  STRW_NONE = (STRW_INACTIVE << 2) + STRW_INACTIVE, STRW_UP = (STRW_LOW << 2) + STRW_INACTIVE, STRW_DOWN = (STRW_INACTIVE << 2) + STRW_LOW, STRW_VOL_UP = (STRW_HIGH << 2) + STRW_INACTIVE,
  STRW_VOL_DOWN = (STRW_INACTIVE << 2) + STRW_HIGH, STRW_MODE = (STRW_GND << 2) + STRW_INACTIVE, STRW_PWR = (STRW_INACTIVE << 2) + STRW_GND, STRW_DOWN_MODE = (STRW_GND << 2) + STRW_LOW
}
 

Buttons activated on the steering wheel.

More...

Functions

 STATIC_ASSERT (F_CPU > 0)
 STATIC_ASSERT (VOLUME_ACTIVATE_TIMEOUT > 1)
 ISR (SIG_OVERFLOW1)
 ISR (SIG_OUTPUT_COMPARE1A)
 ISR (SIG_OUTPUT_COMPARE1B)
int main (void)
 Here it all begins.

Variables

 FUSES
volatile uint16_t voltages [] = {VOLTAGE1,VOLTAGE2,VOLTAGE3}

Define Documentation

 
#define BTN1_LED_GREEN_INIT (  )     DDRA |= _BV(PA2);BTN1_LED_GREEN_OFF()

Definition at line 141 of file adapter.c.

Referenced by main().

 
#define BTN1_LED_GREEN_OFF (  )     PORTA &= ~_BV(PA2)

Definition at line 140 of file adapter.c.

Referenced by ISR().

 
#define BTN1_LED_GREEN_ON (  )     PORTA |= _BV(PA2)

Definition at line 139 of file adapter.c.

Referenced by ISR().

 
#define BTN2_LED_RED_INIT (  )     DDRA |= _BV(PA0);BTN2_LED_RED_OFF()

Definition at line 145 of file adapter.c.

Referenced by main().

 
#define BTN2_LED_RED_OFF (  )     PORTA &= ~_BV(PA0)

Definition at line 144 of file adapter.c.

Referenced by ISR().

 
#define BTN2_LED_RED_ON (  )     PORTA |= _BV(PA0)

Definition at line 143 of file adapter.c.

Referenced by ISR().

 
#define BTN_GREEN_PRESSED (  )     (!(PINA & _BV(PA3)))

Definition at line 147 of file adapter.c.

Referenced by main().

 
#define BTN_INIT (  )     DDRA &= ~(_BV(PA3) | _BV(PA1));PORTA |= (_BV(PA3) | _BV(PA1))

Definition at line 149 of file adapter.c.

Referenced by main().

 
#define BTN_RED_PRESSED (  )     (!(PINA & _BV(PA1)))

Definition at line 148 of file adapter.c.

Referenced by main().

#define debug_voltage (  ) 
Value:
{ \
    red_led = v & 0x1;\
    green_led = v & 0x2;\
}

Definition at line 235 of file adapter.c.

 
#define EGO_GREEN_LED_IS_ON (  )     (!(PINA & _BV(PA7)))

Definition at line 131 of file adapter.c.

Referenced by main().

 
#define EGO_INIT (  )     DDRA &= ~(_BV(PA7));DDRA |= EGO_X012_INACTIVE; PORTA |= _BV(PA7); DDRB &= ~(_BV(PB5) | _BV(PB3)); PORTB |= _BV(PB5);

Definition at line 134 of file adapter.c.

Referenced by main().

 
#define EGO_MUTE_ON (  )     (!(PINB & _BV(PB3)))

Definition at line 132 of file adapter.c.

Referenced by main().

 
#define EGO_RED_LED_IS_ON (  )     (!(PINB & _BV(PB5)))

Definition at line 130 of file adapter.c.

Referenced by main().

#define EGO_X012 (  )     PORTA = (PORTA & ~EGO_X012_INACTIVE) | ((x) & EGO_X012_INACTIVE)

Definition at line 133 of file adapter.c.

Referenced by main().

#define elements_of ( array   )     (sizeof(array) / sizeof(array[0]))

Definition at line 97 of file adapter.c.

#define MS_TO_TICKS (  )     (t/T1_OVERFLOW_TIME_MS)

Definition at line 259 of file adapter.c.

#define STATIC_ASSERT ( expr   )     extern char static_assert[ (!!(expr))*2 - 1]

Definition at line 96 of file adapter.c.

#define T1_OVERFLOW_TIME_MS   ((4UL*256UL*1000UL) / F_CPU)

Definition at line 258 of file adapter.c.

#define VOLTAGE_TO_ADC_VALUE (  )     (uint16_t)(v * ((1UL<<16)-1) / (2.56 * 2))

2.56 is ref voltage and /2 because of the external voltage divider

Definition at line 160 of file adapter.c.

#define VOLUME_ACTIVATE_TIMEOUT   MS_TO_TICKS(300)

Definition at line 260 of file adapter.c.

Referenced by main().


Enumeration Type Documentation

anonymous enum

ADC channels for the different steering wheel button resistor arrays.

Enumerator:
STRW_UP_INPUT 
STRW_DOWN_INPUT 

Definition at line 154 of file adapter.c.

00154      {
00155     STRW_UP_INPUT  = 7,
00156     STRW_DOWN_INPUT = 9
00157 };

anonymous enum

Different voltage zones for the steering wheel buttons.

Enumerator:
VOLTAGE1 
VOLTAGE2 
VOLTAGE3 
MUTE_ON_THRES 

Definition at line 170 of file adapter.c.

00170      {
00171     VOLTAGE1 = VOLTAGE_TO_ADC_VALUE(0.5),
00172     VOLTAGE2 = VOLTAGE_TO_ADC_VALUE(1.5),
00173     VOLTAGE3 = VOLTAGE_TO_ADC_VALUE(3.0),
00174     MUTE_ON_THRES =  VOLTAGE_TO_ADC_VALUE(0.6)
00175 };

enum btn_t

Activation level on a steering wheel channel.

Enumerator:
STRW_INACTIVE 
STRW_HIGH 
STRW_LOW 
STRW_GND 

Definition at line 180 of file adapter.c.

00180              {
00181     STRW_INACTIVE = 0,
00182     STRW_HIGH = 1,
00183     STRW_LOW = 2,
00184     STRW_GND = 3
00185 } btn_t;

enum EGO_X012

The EGO multiplexes the buttons to a 3bit value, those signals are connected to PA6, PA5, PA4.

Enumerator:
EGO_X012_INACTIVE 
EGO_X012_PLAY_UNHOOK 
EGO_X012_STOP_ONHOOK 
EGO_X012_VOL_UP 
EGO_X012_VOL_DOWN 
EGO_X012_NEXT 
EGO_X012_PREV 

Definition at line 120 of file adapter.c.

00120               {
00121     EGO_X012_INACTIVE    = 1 * _BV(PA6) | 1 * _BV(PA5) | 1 * _BV(PA4),
00122     EGO_X012_PLAY_UNHOOK = 0 * _BV(PA6) | 0 * _BV(PA5) | 0 * _BV(PA4),
00123     EGO_X012_STOP_ONHOOK = 1 * _BV(PA6) | 0 * _BV(PA5) | 0 * _BV(PA4),
00124     EGO_X012_VOL_UP      = 0 * _BV(PA6) | 0 * _BV(PA5) | 1 * _BV(PA4),
00125     EGO_X012_VOL_DOWN    = 1 * _BV(PA6) | 0 * _BV(PA5) | 1 * _BV(PA4),
00126     EGO_X012_NEXT        = 0 * _BV(PA6) | 1 * _BV(PA5) | 0 * _BV(PA4),
00127     EGO_X012_PREV        = 1 * _BV(PA6) | 1 * _BV(PA5) | 0 * _BV(PA4),
00128 };

Buttons activated on the steering wheel.

Enumerator:
STRW_NONE 
STRW_UP 
STRW_DOWN 
STRW_VOL_UP 
STRW_VOL_DOWN 
STRW_MODE 
STRW_PWR 
STRW_DOWN_MODE 

Definition at line 188 of file adapter.c.

00188              {
00189     STRW_NONE       = (STRW_INACTIVE << 2) + STRW_INACTIVE,
00190     STRW_UP         = (STRW_LOW      << 2) + STRW_INACTIVE,
00191     STRW_DOWN       = (STRW_INACTIVE << 2) + STRW_LOW,
00192     STRW_VOL_UP     = (STRW_HIGH     << 2) + STRW_INACTIVE,
00193     STRW_VOL_DOWN   = (STRW_INACTIVE << 2) + STRW_HIGH,
00194     STRW_MODE       = (STRW_GND      << 2) + STRW_INACTIVE,
00195     STRW_PWR        = (STRW_INACTIVE << 2) + STRW_GND,
00196     STRW_DOWN_MODE  = (STRW_GND      << 2) + STRW_LOW,
00197 } wheel_control_t;


Function Documentation

ISR ( SIG_OUTPUT_COMPARE1B   ) 

Definition at line 301 of file adapter.c.

00302 {
00303     if (!red_led) {
00304         BTN2_LED_RED_OFF();
00305     }
00306 }

ISR ( SIG_OUTPUT_COMPARE1A   ) 

Definition at line 293 of file adapter.c.

00294 {
00295     if (!green_led) {
00296         BTN1_LED_GREEN_OFF();
00297     }
00298 }

ISR ( SIG_OVERFLOW1   ) 

Definition at line 285 of file adapter.c.

00286 {
00287     if (!green_led_1) BTN1_LED_GREEN_ON(); else BTN1_LED_GREEN_OFF();
00288     if (!red_led_1) BTN2_LED_RED_ON(); else BTN2_LED_RED_OFF();
00289     if (volume_activated_timer > 0) volume_activated_timer--;
00290     timer1ovfl++;
00291 }

int main ( void   ) 

Here it all begins.

Definition at line 309 of file adapter.c.

00310 {
00311     wheel_control_t prev = STRW_NONE;
00312     uint8_t volume_activated = 0;
00313 
00314     //wdt_enable(WDTO_2S);
00315     adc_init();
00316     BTN2_LED_RED_INIT();
00317     BTN1_LED_GREEN_INIT();
00318     BTN_INIT();
00319     EGO_INIT();
00320     EGO_X012(EGO_X012_INACTIVE);
00321 
00322     /* configure timer 1 for a rate of 1M/(4 * 256) = () */
00323     PLLCSR = 0;
00324     TCCR1A = 0;
00325     TCCR1B = 3;
00326     TCCR1D = 0;
00327     TCCR1E = 0;
00328     TIMSK |= _BV(TOIE1) | _BV(OCIE1A) | _BV(OCIE1B);
00329     OCR1A = 2 * 256/100; //5% backlight on red
00330     OCR1B = 3 * 256/100; //10% backlight on green
00331 
00332     sei();
00333 
00334     // main event loop
00335     for (;;)
00336     {
00337         //Watchdog
00338         wdt_reset();
00339         if (timer1ovfl > 20)
00340         {
00341             wheel_control_t  ctrl = 0;
00342             timer1ovfl = 0;
00343             ctrl = get_wheel_control();
00344 
00345             //Active the leds
00346             red_led = EGO_RED_LED_IS_ON();
00347             green_led = EGO_GREEN_LED_IS_ON();
00348 
00349             if (volume_activated_timer == 0) {
00350                 prev = STRW_NONE;
00351             }
00352             if (BTN_RED_PRESSED()) red_led_1 = 1; else red_led_1 = 0;
00353             if (BTN_GREEN_PRESSED()) green_led_1 = 1; else green_led_1 = 0;
00354 
00355             if (0) {
00356                 default_strwh_inactive();
00357             }
00358             else {
00359                 if (EGO_MUTE_ON() && EGO_RED_LED_IS_ON()) {
00360                     switch (ctrl){
00361                         default:
00362                         case STRW_NONE:      default_strwh_inactive();break;
00363                         case STRW_VOL_UP:    EGO_X012(EGO_X012_VOL_UP);break;
00364                         case STRW_VOL_DOWN:  EGO_X012(EGO_X012_VOL_DOWN);break;
00365                         case STRW_UP:        EGO_X012(EGO_X012_PLAY_UNHOOK);break;
00366                         case STRW_DOWN:      EGO_X012(EGO_X012_STOP_ONHOOK);break;
00367                         case STRW_DOWN_MODE: EGO_X012(EGO_X012_STOP_ONHOOK); break;
00368                     }
00369                 }
00370                 else {
00371                     if (EGO_GREEN_LED_IS_ON() && !EGO_RED_LED_IS_ON()) {
00372 
00373                         switch (ctrl){
00374                             default:
00375                             case STRW_NONE:
00376                                 default_strwh_inactive();
00377                                 break;
00378                             case STRW_VOL_UP:
00379                                 {
00380                                     volume_activated_timer = VOLUME_ACTIVATE_TIMEOUT;
00381                                     if (prev == STRW_VOL_DOWN) {
00382                                         volume_activated = 1;
00383                                     }
00384                                     if (volume_activated) {
00385                                         EGO_X012(EGO_X012_VOL_UP);
00386                                     }
00387                                 }
00388                                 break;
00389                             case STRW_VOL_DOWN:
00390                                 {
00391                                     volume_activated_timer = VOLUME_ACTIVATE_TIMEOUT;
00392                                     if (prev == STRW_VOL_UP) {
00393                                         volume_activated = 1;
00394                                     }
00395                                     if (volume_activated) {
00396                                         EGO_X012(EGO_X012_VOL_DOWN);
00397                                     }
00398                                 }
00399                                 break;
00400 
00401                             case STRW_UP:
00402                                 EGO_X012(EGO_X012_NEXT);
00403                                 break;
00404                             case STRW_DOWN:
00405                                 EGO_X012(EGO_X012_PREV);
00406                                 break;
00407                             case STRW_DOWN_MODE:
00408                                 EGO_X012(EGO_X012_STOP_ONHOOK);
00409                                 break;
00410                         }
00411                     }
00412                     else {
00413                         switch (ctrl){
00414                             default:
00415                             case STRW_NONE:
00416                                 default_strwh_inactive();
00417                                 break;
00418                             case STRW_DOWN_MODE: EGO_X012(EGO_X012_STOP_ONHOOK);
00419                                 break;
00420                         }
00421                     }
00422                 }
00423             }
00424 
00425             if (ctrl != STRW_NONE) {
00426                 prev = ctrl;
00427             }
00428 
00429             if (ctrl != STRW_VOL_DOWN || ctrl != STRW_VOL_UP) {
00430                 volume_activated = 0;
00431             }
00432         }
00433     }
00434     return 0;
00435 }

STATIC_ASSERT ( VOLUME_ACTIVATE_TIMEOUT  ,
 
)
STATIC_ASSERT ( F_CPU  ,
 
)

Variable Documentation

Initial value:
{
  .low = LFUSE_DEFAULT, 
  .high = (FUSE_EESAVE & FUSE_SPIEN),
}

Definition at line 100 of file adapter.c.

volatile uint16_t voltages[] = {VOLTAGE1,VOLTAGE2,VOLTAGE3}

Definition at line 177 of file adapter.c.

Generated on Fri Jun 4 14:16:06 2010 for Audio and steering wheel adapter EGO Talk <=> Infiniti FX 35 by  doxygen 1.6.3