BoardController.c

Go to the documentation of this file.
00001 /*
00002 *  C Implementation: BoardController
00003 *
00004 * Description: Implementation of the board controller (LED's, Switches ...)
00005 *
00006 *
00007 * Author: Michel Tokic <michel@tokic.com>, (C) 2009
00008 *
00009 * Copyright: See COPYING file that comes with this distribution
00010 * 
00011 */
00012 
00013 #include "BoardController.h"
00014 
00016 void BoardController::initPorts() {
00017 
00018      // initialize switch-pins as inputs (PA0-7)
00019      DDRA = 0x00;
00020      // enable internal pull-ups for switch-pins (PA0-7)
00021      PORTA |= (1<<PA0) | (1<<PA1) | (1<<PA2) | (1<<PA3) | (1<<PA4) | (1<<PA5) | (1<<PA6) | (1<<PA7);
00022 
00023 
00024      // initialize LED pins as outputs (PB0-3)
00025      DDRB = (1<<PB0) | (1<<PB1) | (1<<PB2) | (1<<PB3); // LED1..4
00026 
00027      // turn on power led
00028      powerOnLED(LED_POWER, true);
00029      powerOnLED(LED_WALK, true);
00030      powerOnLED(LED_LEARN, true);
00031      powerOnLED(LED_EXPLORATION, true);
00032 
00033      msDelay(800);
00034 
00035      // LED test (turn all LEDs off)
00036      powerOnLED(LED_POWER, true);
00037      powerOnLED(LED_WALK, false);
00038      powerOnLED(LED_LEARN, false);
00039      powerOnLED(LED_EXPLORATION, false);
00040 
00041 
00042 
00043      // define serial interface control pins as outputs ... 
00044      DDRC |= (1<<EN_TXD_BIOLOID) | (1<<EN_RXD_BIOLOID) | (1<<EN_AIR_V24) | (1<<RTS);
00045 
00046      // disable serial port
00047      UCSRB &= ~((1<<TXEN) | (1<<RXEN));   // disable TX and RX
00048 
00049      // define RTS as input
00050      DDRC &= ~(1<<CTS);
00051      // indicate Host that serial line is not ready for reception
00052      PORTC |= (1<<RTS);
00053 
00054      // USART pins
00055      DDRD |= (1<<PD1);
00056      DDRD &= ~(1<<PD0);
00057 }
00058 
00060 unsigned char BoardController::getSwitchState (unsigned char _switch) {
00061 
00062      bool status;
00063 
00064      if (_switch == SWITCH_WALK) {
00065           status = (PINA & (1<<PA0)); // Bioloid
00066           
00067      } else if (_switch == SWITCH_RANDOM_STATE) {
00068           status = (PINA & (1<<PA1)); // Bioloid
00069           
00070      } else if (_switch == SWITCH_INV_FB_0) {
00071           status = (PINA & (1<<PA2)); // Bioloid
00072      
00073      } else if (_switch == SWITCH_ENABLE_EXPLORATION) {
00074           status = (PINA & (1<<PA3)); // Bioloid
00075           
00076      } else if (_switch == SWITCH_FULL_EXP_AT_START) {
00077           status = (PINA & (1<<PA4)); // PCBv2 and Bioloid
00078           
00079      } else if (_switch == SWITCH_V24) {
00080           status = (PINA & (1<<PA5)); // PCBv2 and Bioloid 
00081                               
00082      } else if (_switch == SWITCH_VDBE_EGREEDY) {
00083           status = (PINA & (1<<PA6)); // PCBv2 and Bioloid
00084 
00085      } else if (_switch == SWITCH_GAMMA) {
00086           status = (PINA & (1<<PA7)); // PCBv2 and Bioloid
00087 
00088      } else if (_switch == SWITCH_RAM) {
00089           status = SWITCH_RAM_VALUE; // PCBv2
00090 
00091      // blink if _switch is unknown
00092      } else {
00093           
00094           register char i;
00095           
00096           for (i=0; i<100; i++) {
00097           
00098                powerOnLED(LED_POWER, 1);
00099                powerOnLED(LED_WALK, 0);
00100                powerOnLED(LED_LEARN, 0);
00101                powerOnLED(LED_EXPLORATION, 1);
00102                
00103                msDelay(250);
00104                
00105                powerOnLED(LED_POWER, 0);
00106                powerOnLED(LED_WALK, 1);
00107                powerOnLED(LED_LEARN, 1);
00108                powerOnLED(LED_EXPLORATION, 0);
00109                
00110                msDelay(250);
00111           }
00112           
00113           return 0;
00114      }
00115      
00116      return !status;
00117 }
00118 
00120 void BoardController::enableV24Interrupt()  { 
00121 
00122      disableV24Interrupt();
00123      if (getSwitchState(SWITCH_V24)) {
00124           //UCSRB |=  (1 << RXCIE); 
00125           //sei();
00126      }
00127      //PORTC &= ~(1<<PC3);
00128      //UCSRB |= (1<<RXEN);
00129 }
00130 
00132 void BoardController::disableV24Interrupt() { 
00133      if (getSwitchState(SWITCH_V24)) {
00134           //UCSRB &= ~(1 << RXCIE); 
00135           //cli();
00136      }
00137      //PORTC  |= (1<<PC3);
00138      //PORTC |= (1<<PC3);
00139      //UCSRB &= ~(1<<RXEN);
00140 }
00141 
00142 
00144 void BoardController::msDelay (int n) {
00145 
00146      //double maxDelay = (262.14 / (double)((11059200UL / 1000000)));
00147      double maxDelay = (262.14 / (double)((16000000UL / 1000000)));
00148 
00149      for (int i=0; i< (n/maxDelay); i++) {
00150           _delay_ms (maxDelay);
00151      }
00152 }
00153 
00154 
00156 void BoardController::powerOnLED(unsigned char _ledID, bool _status) {
00157 
00158      if (_status == true) {
00159           PORTB &= ~(1<<_ledID);
00160      } else {
00161           PORTB |= (1<<_ledID);
00162      }
00163 }
00164 
00165 
00166 void BoardController::blinkLED (unsigned char led_id, unsigned int count, unsigned int delay) {
00167 
00168      unsigned char i;
00169 //   DDRB = 0x0F;
00170 //   PORTB |= (1<<PA4) | (1<<PA5) | (1<<PA6) | (1<<PA7);
00171 
00172      for (i=0; i<count; i++) {
00173 
00174           if (led_id & (1<<LED_WALK)) powerOnLED ( LED_WALK, true);
00175           if (led_id & (1<<LED_LEARN)) powerOnLED ( LED_LEARN, true);
00176           if (led_id & (1<<LED_EXPLORATION)) powerOnLED ( LED_EXPLORATION, true);
00177           if (led_id & (1<<LED_POWER)) powerOnLED ( LED_POWER, true);
00178 
00179           msDelay (delay);
00180 
00181           if (led_id & (1<<LED_LEARN)) powerOnLED ( LED_LEARN, false);
00182           if (led_id & (1<<LED_WALK)) powerOnLED ( LED_WALK, false);
00183           if (led_id & (1<<LED_EXPLORATION)) powerOnLED ( LED_EXPLORATION, false);
00184           if (led_id & (1<<LED_POWER)) powerOnLED ( LED_POWER, true);
00185 
00186           msDelay (delay);
00187      }
00188 }
00189 
00190 
00192 void BoardController::enableSerialBioloidInterface() {
00193 
00194      // indicate Host that serial line is not ready for reception
00195      PORTC  |= (1<<RTS);
00196 
00197      // disable serial wires
00198      PORTC  &= ~((1<<EN_TXD_BIOLOID) | (1<<EN_RXD_BIOLOID) | (1<<EN_AIR_V24));
00199 
00200      // flushing the buffer
00201      unsigned char dummy;
00202      while (UCSRA & (1<<RXC)) dummy = UDR;
00203 
00204      // ** Serial init **
00205      // 1) USART
00206      //UCSRB &= ~((1<<TXEN) | (1<<RXEN));   // disable TX and RX
00207 
00208      // set baudrate 
00209      UBRRH = (unsigned char) (BIOLOID_UBRR >> 8);  // load the upper bits
00210      UBRRL = (unsigned char) BIOLOID_UBRR; 
00211 
00212      UCSRA |= (1<<U2X);
00213      UCSRB |= ((1<<TXEN) | (1<<RXEN));   // enable transmit and receive.enable TX and RX
00214      //UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0); // Frame Format: 8data, 1stop bit, noParity 
00215 
00216      // enable bioloid transmission pin
00217      PORTC  |= (1<<EN_TXD_BIOLOID);
00218      PORTC  &= ~((1<<EN_RXD_BIOLOID) | (1<<EN_AIR_V24));
00219 
00220 }
00221 
00222 void BoardController::enableSerialV24Interface() {
00223 
00224      BoardController::powerOnLED(LED_EXPLORATION, true);
00225 
00226      // disable serial wires
00227      PORTC  &= ~((1<<EN_TXD_BIOLOID) | (1<<EN_RXD_BIOLOID) | (1<<EN_AIR_V24));
00228 
00229      // flushing the buffer
00230      unsigned char dummy;
00231      while (UCSRA & (1<<RXC)) dummy = UDR;
00232 
00233      // ** Serial init **
00234      // 1) USART
00235      //UCSRB &= ~((1<<TXEN) | (1<<RXEN));   // disable TX and RX
00236 
00237      // set baudrate 
00238      UBRRH = (unsigned char) (V24_UBRR >> 8);  // load the upper bits
00239      UBRRL = (unsigned char) V24_UBRR; 
00240 
00241      UCSRA |= (1<<U2X);
00242      UCSRB = ((1<<TXEN) | (1<<RXEN));   // enable transmit and receive.enable TX and RX
00243 
00244      // disable bioloid bus and enable V24
00245      PORTC  |= (1<<EN_AIR_V24);
00246      PORTC  &= ~((1<<EN_TXD_BIOLOID) | (1<<EN_RXD_BIOLOID));
00247 
00248      // ... ready to receive
00249      PORTC  &= ~(1<<RTS);
00250 
00251      BoardController::msDelay(50);
00252      BoardController::powerOnLED(LED_EXPLORATION, false);
00253 }
00254 
00255 void BoardController::disableSerialInterfaces() {
00256 
00257      //PORTC  &= ~(1<<CTS);
00258 
00259      // disable serial wires
00260      PORTC  &= ~((1<<EN_TXD_BIOLOID) | (1<<EN_RXD_BIOLOID) | (1<<EN_AIR_V24));
00261      UCSRB &= ~((1<<TXEN) | (1<<RXEN));   // disable TX and RX
00262 
00263      if (getSwitchState(SWITCH_V24)) {
00264           enableSerialV24Interface();
00265      }
00266 }
00267 
00268 

Generated on Wed Nov 25 11:27:41 2009 for Crawling Robot Microcontroller Software by  doxygen 1.5.5