Subversion Repositories FlightCtrl

Rev

Rev 1926 | Rev 2025 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <util/delay.h>
#include <avr/eeprom.h>
#include <stddef.h>
#include "configuration.h"
#include "eeprom.h"
#include "uart0.h"

int16_t variables[8] = {0,0,0,0,0,0,0,0};
dynamicParam_t dynamicParams;// = {48,251,16,58,64,8,150,150,2,10,{0,0,0,0,0,0,0,0},100,70,90,65,64,100,0,0,0};
uint8_t CPUType = ATMEGA644;
uint8_t BoardRelease = 13;

/************************************************************************
 * Map the parameter to pot values                                    
 * Replacing this code by the code below saved almost 1 kbyte.
 ************************************************************************/


void configuration_staticToDynamic(void) {
  uint8_t i;
#define SET_POT_MM(b,a,min,max) {if (a<255) {if (a>=251) b=variables[a-251]; else b=a;} if(b<=min) b=min; else if(b>=max) b=max;}
#define SET_POT(b,a) { if (a<255) {if (a>=251) b=variables[a-251]; else b=a;}}
  SET_POT(dynamicParams.MaxHeight,staticParams.MaxHeight);
  SET_POT_MM(dynamicParams.HeightD,staticParams.HeightD,0,100);
  SET_POT_MM(dynamicParams.HeightP,staticParams.HeightP,0,100);
    SET_POT(dynamicParams.CompassYawEffect,staticParams.CompassYawEffect);

  SET_POT(dynamicParams.GyroPitchP,staticParams.GyroPitchP);
  SET_POT(dynamicParams.GyroRollP,staticParams.GyroRollP);
  SET_POT(dynamicParams.GyroYawP,staticParams.GyroYawP);

  SET_POT(dynamicParams.GyroPitchD,staticParams.GyroPitchD);
  SET_POT(dynamicParams.GyroRollD,staticParams.GyroRollD);
  SET_POT(dynamicParams.GyroYawD,staticParams.GyroYawD);

  for (i=0; i<sizeof(staticParams.UserParams); i++) {
    SET_POT(dynamicParams.UserParams[i],staticParams.UserParams[i]);
  }
   
  SET_POT_MM(dynamicParams.J16Timing,staticParams.J16Timing,1,255);
  SET_POT_MM(dynamicParams.J17Timing,staticParams.J17Timing,1,255);

  SET_POT(dynamicParams.ExternalControl,staticParams.ExternalControl);
}

uint8_t getCPUType(void) {   // works only after reset or power on when the registers have default values
  uint8_t CPUType = ATMEGA644;
  //if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P;  // initial Values for 644P after reset
  return CPUType;
}

/*
 * Automatic detection of hardware components is not supported in this development-oriented
 * FC firmware. It would go against the point of it: To enable alternative hardware
 * configurations with otherwise unsupported components. Instead, one should write
 * custom code + adjust constants for the new hardware, and include the relevant code
 * from the makefile.
 * However - we still do detect the board release. Reason: Otherwise it would be too
 * tedious to have to modify the code for how to turn on and off LEDs when deploying
 * on different HW version....
 */


uint8_t getBoardRelease(void) {
  uint8_t BoardRelease = 13;
  // the board release is coded via the pull up or down the 2 status LED

  PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
  DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input

  _delay_loop_2(1000); // make some delay

  switch( PINB & ((1<<PINB1)|(1<<PINB0))) {
    case 0x00:
      BoardRelease = 10; // 1.0
      break;
    case 0x01:
      BoardRelease = 11; // 1.1 or 1.2
      break;
    case 0x02:
      BoardRelease = 20; // 2.0
      break;
    case 0x03:
      BoardRelease = 13; // 1.3
      break;
    default:
      break;
    }
  // set LED ports as output
  DDRB |= (1<<DDB1)|(1<<DDB0);
  RED_ON;
  GRN_OFF;
  return BoardRelease;
}