int rtc_test(void)
{			

	uart_putchars("RTC Test\r\n", 10);
	
	DDRB = 0;
	// AVR-PIN	Signal						RTC-PIN
	// PB0		CS		Chipselect			3
	// PB3		MOSI	Master Output		9
	// PB4		MISO	Master Input		5
	// PB5		SCK		Serial Clock		4
	
	Set_bits(DDRB,  (1 << PB0) | (1 << PB3) | (1 << PB5) | (1 << PB2));
	
	CS_OFF();
	
	SPCR = (1<<SPE) | (1 << MSTR) | (0 << SPI2X) | (0 << SPR1) | (0 << SPR0);
	
	// RTC Software Reset
	CS_ON();
	spi_transfer(RTC_CTRL_RESET | RTC_WRITE);
	spi_transfer(RTC_RESET);
	CS_OFF();
	
	//debug_rtc();
	_delay_ms(100);
	
	// RTC Status
	CS_ON();
	uint8_t ctrl_Status = spi_transfer(RTC_CTRL_STATUS);
	CS_OFF();
	
    if(ctrl_Status & RTC_PON) {
		//clear POWER-ON bit
		ctrl_Status &= ~RTC_PON;
        
		CS_ON();
		spi_transfer(RTC_CTRL_STATUS | RTC_WRITE);
		spi_transfer(ctrl_Status);
		CS_OFF();
    }
	  
    if(ctrl_Status & RTC_SR) {
		//clear SELF-RECOVERY bit
		ctrl_Status &= ~RTC_SR;
        
		CS_ON();
		spi_transfer(RTC_CTRL_STATUS | RTC_WRITE);
		spi_transfer(ctrl_Status);
		CS_OFF();
    }
	
	CS_ON();
	spi_transfer(RTC_CTRL_INT_FLAG | RTC_WRITE);
	spi_transfer(0);
	CS_OFF();
	
	CS_ON();
	spi_transfer(RTC_CTRL_INT | RTC_WRITE);
	spi_transfer(1);
	CS_OFF();
	
	CS_ON();
	spi_transfer(RTC_EPROM_CTRL | RTC_WRITE);
	spi_transfer(RTC_THE);
	CS_OFF();
	
	
	// set datetime	
	now.year = 16;
	now.month = 5;
	now.day = 12;
	
	now.hour = 12;
	now.minute = 00;
	now.second = 00;	
	rtc_set_datetime(&now);
	
	
	// set alarm
	TDateTime alarm = { 0 };	
	alarm.second = 5 | RTC_AE_S;
	//alarm.minute = 0 | RTC_AE_S;
	rtc_set_alarm(&alarm);
	
	
	rtc_debug();
	
	
	uint8_t s = 0;
	
	for (;;)
	{
		rtc_get_datetime(&now);
		
		int temperature = rtc_get_temperature();						
				
		if(now.second != s) {
			s = now.second;
			
			char buf[50] = { 0 };		
			sprintf(buf, "%d.%d.%d - %d:%d:%d  Temp:%d\r\n", now.day, now.month, now.year, now.hour, now.minute, now.second, temperature);
		
			uart_putchars(buf, 50);
			
			if(s == (alarm.second + 1))
			{
				rtc_clear_alarm_int();		
			}
		}
	}

}
Exemplo n.º 2
0
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
  static uint8_t success = 1;

  uint8_t bmRequestType = USB_ControlRequest.bmRequestType;
  uint8_t bRequest      = USB_ControlRequest.bRequest;
  //uint8_t wValue        = USB_ControlRequest.wValue;
  char data[51];

  HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);

  if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    char lock;
    uint16_t wLength = USB_ControlRequest.wLength;
    char pw[32];
    success = 1; /* default */

    eeprom_read(ADDR_LOCK, &lock, 1);
    if(lock == 0 || lock == 255)
      lock = 0;
    else
    {
      if(bRequest != OPENKUBUS_GET_NONCE && bRequest != OPENKUBUS_SET_TIMESTAMP && bRequest != OPENKUBUS_RESET)
      {
        success = 0;
        return;
      }
    }

    // read data
    if(wLength)
    {
      Endpoint_ClearSETUP();
      Endpoint_Read_Control_Stream_LE(data, MIN(sizeof(data), wLength));
      Endpoint_ClearIN();
    }

    switch(bRequest)
    {
      case OPENKUBUS_SET_LOCK:
        lock = 1;
        eeprom_write(ADDR_LOCK, &lock, 1);
        break;

      
      case OPENKUBUS_SET_OWNER:
        if(wLength)
          eeprom_write(ADDR_OWNER, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_COMPANY:
        if(wLength)
          eeprom_write(ADDR_COMPANY, data, wLength);
        else
          success = 0;
        
        break;
      

      case OPENKUBUS_SET_DESCRIPTION:
        if(wLength)
          eeprom_write(ADDR_DESCRIPTION, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_ID:
        if(wLength == 4)
          eeprom_write(ADDR_ID, data, wLength);
        else
          success = 0;
        
        break;


      case OPENKUBUS_SET_TIMESTAMP:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          eeprom_read(ADDR_SEED, pw, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            set_timestamp(array2int((uint8_t *)&data[12]));
            update_nonce();
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_RESET:
        if(wLength == 16)
        {
          aes256_ctx_t ctx;

          memset(&ctx, 0, sizeof(aes256_ctx_t));
          memcpy_P(pw, MASTER_PASSWORD, sizeof(pw));

          aes256_init(pw, &ctx);
          aes256_dec(data, &ctx);

          if(strncmp(nonce, data, sizeof(nonce)) == 0)
          {
            clear_eeprom();
            wdt_enable(WDTO_15MS);
            while(1);
          }
          else
            success = 0;
        }
        else
          success = 0;

        break;


      case OPENKUBUS_SET_SEED:
        if(wLength == LEN_SEED)
          eeprom_write(ADDR_SEED, data, LEN_SEED);
        else
          success = 0;

        break;


      case OPENKUBUS_SET_COUNTER:
        if(wLength == LEN_COUNTER)
          eeprom_write(ADDR_COUNTER, data, LEN_COUNTER);
        else
          success = 0;

        break;


      default:
        success = 0;
        break;
    }
  }
  else if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE))
  {
    uint8_t length = 0;
    uint8_t i;
    uint32_t temp32 = 0;
    char c;

    switch(bRequest)
    {
      case OPENKUBUS_GET_SUCCESS:
        data[length++] = success;
        break;


      case OPENKUBUS_GET_NONCE:
        for(i = 0; i < sizeof(nonce); i++)
          data[length++] = nonce[i];

        break;


      case OPENKUBUS_GET_TEMPERATURE:
        #ifdef RTC
          data[length++] = rtc_get_temperature();
        #else
          data[length++] = 0xFF;
        #endif
        break;


      case OPENKUBUS_GET_ID:
        for(i = 0; i < LEN_ID; i++)
        {
          eeprom_read(ADDR_ID+i, &c, 1); 
          data[length++] = c;
        }

        break;


      case OPENKUBUS_GET_TIME:
        temp32 = get_timestamp();

        data[length++] = temp32 >> 24;
        data[length++] = temp32 >> 16;
        data[length++] = temp32 >> 8;
        data[length++] = temp32;
        break;


      case OPENKUBUS_GET_SERIAL:
        for(i = 0x0e; i <= 0x18; i++)
          data[length++] = boot_signature_byte_get(i);
        break;


      case OPENKUBUS_GET_DESCRIPTION:
        for(i = 0; i < LEN_DESCRIPTION; i++)
        {
          eeprom_read(ADDR_DESCRIPTION+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_COMPANY:
        for(i = 0; i < LEN_COMPANY; i++)
        {
          eeprom_read(ADDR_COMPANY+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;


      case OPENKUBUS_GET_OWNER:
        for(i = 0; i < LEN_OWNER; i++)
        {
          eeprom_read(ADDR_OWNER+i, &c, 1); 
          data[length++] = c;
          if(c == 0)
            break;
        }

        break;

      default:
        data[length++] = 0;
    }

    // send data
    Endpoint_ClearSETUP();
    Endpoint_Write_Control_Stream_LE(data, length);
    Endpoint_ClearOUT();
  }