TTS fixes

This commit is contained in:
romolo.manfredini@gmail.com
2012-11-15 02:10:48 +00:00
parent b3b670b55f
commit 6150cac0f5
5 changed files with 246 additions and 95 deletions
+185 -44
View File
@@ -20,7 +20,7 @@
#define NOINLINE __attribute__ ((noinline))
// Version number, sent in first byte of response
#define VERSION 0x03
#define VERSION 0x04
/*****************************************************************************/
// USI_TWI write states.
@@ -44,8 +44,19 @@
#define TSOFFSET 5
#define TSGAIN 7
// EEPROM offsets
#define EE_SECOND 1
#define EE_MINUTE 2
#define EE_HOUR 3
#define EE_DATE 4
#define EE_MONTH 5
#define EE_YEARL 6
#define EE_YEARH 7
// Bits in AdcControl
#define ADC_RUNNING 0x80
#define ADC_CALC 0x40
#define ADC_COUNT_MASK 0x0F
uint8_t AdcControl ;
@@ -54,6 +65,12 @@ int8_t T_offset ;
uint8_t T_gain ;
int16_t T_mult ;
uint8_t StoreTime ; // Flag set to cause date/time put into eeprom
uint8_t StoreState ; // Flag set to cause date/time put into eeprom
uint8_t I2CstartTime ;
uint8_t I2Crunning ;
// USI_TWI state values.
static uint8_t USI_TWI_SLAVE_Write_State;
static uint8_t USI_TWI_SLAVE_Overflow_State;
@@ -95,6 +112,7 @@ static void Disable_WatchDogTimer(void) ;
static void Init_WatchDogTimer(void) ;
void enableAdc( void ) ;
void disableAdc( void ) ;
void storeTime( void ) ;
char not_leap(void) //check for leap year
@@ -118,6 +136,10 @@ void updateTime(uint8_t amount)
if (++p->minute >= 60)
{
p->minute=0;
if ( p->hour & 1 )
{
StoreTime = 1 ; // Store date/time every 2 hours
}
if (++p->hour >= 24)
{
p->hour=0;
@@ -205,6 +227,10 @@ void checkPowerSave()
sleep_disable();
cli() ;
wdt_reset() ;
if ( StoreTime )
{
storeTime() ;
}
}
PRR = 0x39 ; // USI re-powered
Init_WatchDogTimer() ;
@@ -297,8 +323,9 @@ static void USI_TWI_SLAVE_ReadAndProcessPacket ()
CLKPR = 0x80 ; // Do inline for quick response
CLKPR = 0 ; // Full speed 8.0MHz
// Process the USI TWI start condition.
USI_TWI_SLAVE_Process_Start_Condition();
USI_TWI_SLAVE_Process_Start_Condition() ;
I2Crunning = 1 ;
I2CstartTime = TCNT0 ;
}
// Check for USI_TWI_SLAVE overflow condition.
@@ -336,6 +363,8 @@ static void USI_TWI_SLAVE_ReadAndProcessPacket ()
{
// Clear the stop condition flag.
USISR = (1<<USIPF);
I2Crunning = 0 ;
// Check for the special address to exit the bootloader.
if ( Run_boot )
{
@@ -370,8 +399,8 @@ static void USI_TWI_SLAVE_ReadAndProcessPacket ()
*q++ = p->year ;
*q++ = p->year >> 8 ;
*q++ = Temperature ;
*q++ = T_offset ;
*q++ = T_gain ;
// *q++ = T_offset ;
// *q++ = T_gain ;
// *q++ = MCUSR ;
if ( ActivityFlag < 255 )
@@ -380,26 +409,11 @@ static void USI_TWI_SLAVE_ReadAndProcessPacket ()
}
set_clock( 5 ) ; // Slow to 0.25MHz
}
checkPowerSave() ;
if ( ( AdcControl & ADC_RUNNING ) == 0 )
else // If time done, don't do ADC, minimise loop latency
{
ADMUX = TEMPERATURE ; // Internal 1.1V REF and ADCx
ADCSRA |= (1<<ADSC); // do single conversion
AdcControl |= ADC_RUNNING ;
}
else
{
if ( ( ADCSRA & (1<<ADSC) ) == 0 )
if ( ( AdcControl & ADC_RUNNING ) == 0 )
{
AdcControl &= ~ADC_RUNNING ;
AdcControl += 1 ;
if ( ( AdcControl & ADC_COUNT_MASK ) > 1 )
{ // Skip first conversion
AdcTotal += ADCW ;
}
if ( ( AdcControl & ADC_COUNT_MASK ) >= 9 )
if ( AdcControl & ADC_CALC )
{
AdcTotal += 4 ;
AdcTotal >>= 3 ;
@@ -416,32 +430,59 @@ static void USI_TWI_SLAVE_ReadAndProcessPacket ()
}
AdcControl = 0 ;
AdcTotal = 0 ;
// if (AdcTotal < 0x00f6)
// {
// Temperature=-40;
// }
// else if (AdcTotal < 0x0144)
// {
// Temperature=((AdcTotal*78)/65)-40;
// }
// else
// {
// Temperature=((((AdcTotal-324)*600)/74)+250)/10;
// }
}
else
{
ADMUX = TEMPERATURE ; // Internal 1.1V REF and ADCx
ADCSRA |= (1<<ADSC); // do single conversion
AdcControl |= ADC_RUNNING ;
}
}
else
{
if ( ( ADCSRA & (1<<ADSC) ) == 0 )
{
AdcControl &= ~ADC_RUNNING ;
AdcControl += 1 ;
if ( ( AdcControl & ADC_COUNT_MASK ) > 1 )
{ // Skip first conversion
AdcTotal += ADCW ;
}
if ( ( AdcControl & ADC_COUNT_MASK ) >= 9 )
{
AdcControl = ADC_CALC ;
}
}
}
}
if ( StoreTime )
{
storeTime() ;
}
else
{
checkPowerSave() ;
}
if ( I2Crunning )
{
if ( ( TCNT0 - I2CstartTime ) > 12 ) // 50mS
{
// Timeout I2C bus, force release of lines.
USI_TWI_SLAVE_Init() ;
}
}
} // while
// Disable_WatchDogTimer(); // After Reset the WDT state does not change
// void (*FuncPtr) (void) = (void (*)(void)) (0x0080); // Set up function pointer to address 0x0080
// FuncPtr ();
ADCSRA = 0 ;
TCCR0B = 0 ;
USICR = 0 ;
USISR = 0 ;
ASSR = 0 ;
PRR = 0 ;
CLKPR = 0x80 ; // Do inline for quick response
cli() ;
CLKPR = 0x80 ;
CLKPR = 0 ; // Full speed 8.0MHz
((void (*)(void)) (0))() ;
}
@@ -701,6 +742,7 @@ void USI_TWI_SLAVE_Process_Overflow_Condition(void)
p->date= DataBuffer[4] ;
p->month = DataBuffer[5] ;
p->year = DataBuffer[6] + ( DataBuffer[7] << 8 ) ;
StoreTime = 1 ; // Store date/time in eeprom
}
Value = 9 ;
bufferPtr = &DataBuffer[DATA_SIZE-1] ;
@@ -869,6 +911,92 @@ NOINLINE void set_clock( uint8_t speed )
*ptr = speed ;
}
int8_t eeprom_write_byte_cmp (uint8_t dat, uint16_t pointer_eeprom)
{
if ( EECR & (1<<EEPE) ) /* make sure EEPROM is ready */
{
return -1 ;
}
EEAR = pointer_eeprom;
EECR = 1<<EERE;
if(dat == EEDR)
{
return 0 ;
}
EEDR = dat;
uint8_t flags=SREG;
cli();
EECR |= 1<<EEMPE;
EECR |= 1<<EEPE;
SREG = flags;
return 0 ;
}
int16_t eepromReadByte( uint16_t pointer_eeprom )
{
if ( EECR & (1<<EEPE) ) /* make sure EEPROM is ready */
{
return -1 ;
}
EEAR = pointer_eeprom;
EECR |= 1<<EERE;
return EEDR ;
}
void storeTime()
{
switch ( StoreState )
{
case 0 :
if ( eeprom_write_byte_cmp( Time.second, EE_SECOND ) == 0 )
{
StoreState = 1 ;
}
break ;
case 1 :
if ( eeprom_write_byte_cmp( Time.minute, EE_MINUTE ) == 0 )
{
StoreState = 2 ;
}
break ;
case 2 :
if ( eeprom_write_byte_cmp( Time.hour, EE_HOUR ) == 0 )
{
StoreState = 3 ;
}
break ;
case 3 :
if ( eeprom_write_byte_cmp( Time.date, EE_DATE ) == 0 )
{
StoreState = 4 ;
}
break ;
case 4 :
if ( eeprom_write_byte_cmp( Time.month, EE_MONTH ) == 0 )
{
StoreState = 5 ;
}
break ;
case 5 :
if ( eeprom_write_byte_cmp( Time.year, EE_YEARL ) == 0 )
{
StoreState = 6 ;
}
break ;
case 6 :
if ( eeprom_write_byte_cmp( Time.year >> 8, EE_YEARH ) == 0 )
{
StoreState = 0 ;
StoreTime = 0 ;
}
break ;
}
}
/***********************************************************************/
// Main Starts from here
void main (void)
@@ -892,12 +1020,23 @@ void main (void)
t_time *p = &Time ;
FORCE_INDIRECT(p) ;
p->second = 5 ;
p->minute = 6 ;
p->hour = 16 ;
p->date= 23 ;
p->month = 9 ;
p->year = 2012 ;
p->second = eepromReadByte( EE_SECOND ) ;
p->minute = eepromReadByte( EE_MINUTE ) ;
p->hour = eepromReadByte( EE_HOUR ) ;
p->date = eepromReadByte( EE_DATE ) ;
p->month = eepromReadByte( EE_MONTH ) ;
p->year = eepromReadByte( EE_YEARL ) + ( eepromReadByte( EE_YEARH ) << 8 ) ;
if ( p->year == 0xFFFF )
{
p->second = 0 ;
p->minute = 0 ;
p->hour = 19 ;
p->date = 7 ;
p->month = 10 ;
p->year = 2012 ;
StoreTime = 1 ; // Flag set to cause date/time put into eeprom
}
bufferPtr = DataBuffer ;
@@ -927,3 +1066,5 @@ void disableAdc()
sbi( PRR, PRADC ) ; // Power Down the ADC
}