Exemple #1
0
static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
{
	static char err_readtime[] __initdata =
		KERN_ERR "PM: can't read %s time, err %d\n";
	static char err_wakealarm [] __initdata =
		KERN_ERR "PM: can't set %s wakealarm, err %d\n";
	static char err_suspend[] __initdata =
		KERN_ERR "PM: suspend test failed, error %d\n";
	static char info_test[] __initdata =
		KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";

	unsigned long		now;
	struct rtc_wkalrm	alm;
	int			status;

	/* this may fail if the RTC hasn't been initialized */
	status = rtc_read_time(rtc, &alm.time);
	if (status < 0) {
		printk(err_readtime, dev_name(&rtc->dev), status);
		return;
	}
	rtc_tm_to_time(&alm.time, &now);

	memset(&alm, 0, sizeof alm);
	rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
	alm.enabled = true;

	status = rtc_set_alarm(rtc, &alm);
	if (status < 0) {
		printk(err_wakealarm, dev_name(&rtc->dev), status);
		return;
	}

	if (state == PM_SUSPEND_MEM) {
		printk(info_test, pm_states[state]);
		status = pm_suspend(state);
		if (status == -ENODEV)
			state = PM_SUSPEND_STANDBY;
	}
	if (state == PM_SUSPEND_STANDBY) {
		printk(info_test, pm_states[state]);
		status = pm_suspend(state);
	}
	if (status < 0)
		printk(err_suspend, status);

	/* Some platforms can't detect that the alarm triggered the
	 * wakeup, or (accordingly) disable it after it afterwards.
	 * It's supposed to give oneshot behavior; cope.
	 */
	alm.enabled = false;
	rtc_set_alarm(rtc, &alm);
}
Exemple #2
0
static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
{
	static char err_readtime[] __initdata =
		KERN_ERR "PM: can't read %s time, err %d\n";
	static char err_wakealarm [] __initdata =
		KERN_ERR "PM: can't set %s wakealarm, err %d\n";
	static char err_suspend[] __initdata =
		KERN_ERR "PM: suspend test failed, error %d\n";
	static char info_test[] __initdata =
		KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";

	unsigned long		now;
	struct rtc_wkalrm	alm;
	int			status;

	
	status = rtc_read_time(rtc, &alm.time);
	if (status < 0) {
		printk(err_readtime, dev_name(&rtc->dev), status);
		return;
	}
	rtc_tm_to_time(&alm.time, &now);

	memset(&alm, 0, sizeof alm);
	rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
	alm.enabled = true;

	status = rtc_set_alarm(rtc, &alm);
	if (status < 0) {
		printk(err_wakealarm, dev_name(&rtc->dev), status);
		return;
	}

	if (state == PM_SUSPEND_MEM) {
		printk(info_test, pm_states[state]);
		status = pm_suspend(state);
		if (status == -ENODEV)
			state = PM_SUSPEND_STANDBY;
	}
	if (state == PM_SUSPEND_STANDBY) {
		printk(info_test, pm_states[state]);
		status = pm_suspend(state);
	}
	if (status < 0)
		printk(err_suspend, status);

	
	alm.enabled = false;
	rtc_set_alarm(rtc, &alm);
}
static int bq2419x_wakealarm(struct bq2419x_chip *bq2419x, int time_sec)
{
	int ret;
	unsigned long now;
	struct rtc_wkalrm alm;
	int alarm_time = time_sec;

	if (!alarm_time)
		return 0;

	alm.enabled = true;
	ret = rtc_read_time(bq2419x->rtc, &alm.time);
	if (ret < 0) {
		dev_err(bq2419x->dev, "RTC read time failed %d\n", ret);
		return ret;
	}
	rtc_tm_to_time(&alm.time, &now);

	rtc_time_to_tm(now + alarm_time, &alm.time);
	ret = rtc_set_alarm(bq2419x->rtc, &alm);
	if (ret < 0) {
		dev_err(bq2419x->dev, "RTC set alarm failed %d\n", ret);
		alm.enabled = false;
		return ret;
	}
	alm.enabled = false;
	return 0;
}
/**
 * \brief Initialize timeout
 *
 * Initializes timeout counter for desired tick rate and starts it. The device
 * interrupt controller should be initialized prior to calling this function,
 * and global interrupts must be enabled.
 *
 * \note If the service is configured to use the asynchronous RTC32 module,
 *       there are restrictions on the timeout period that can be used - see
 *       to \ref rtc32_min_alarm_time for details.
 */
void timeout_init(void)
{
	rtc_init();
	rtc_set_callback(tick_handler);
	rtc_set_time(0);
	rtc_set_alarm(TIMEOUT_COMP);
}
void alarm_set_power_on(struct timespec new_pwron_time, bool logo)
{
	unsigned long pwron_time;
	struct rtc_wkalrm alm;
	struct rtc_device *alarm_rtc_dev;

	alarm_dbg(INFO, "alarm set power on\n");

#ifdef RTC_PWRON_SEC
	/* round down the second */
	new_pwron_time.tv_sec = (new_pwron_time.tv_sec / 60) * 60;
#endif
	if (new_pwron_time.tv_sec > 0) {
		pwron_time = new_pwron_time.tv_sec;
#ifdef RTC_PWRON_SEC
		pwron_time += RTC_PWRON_SEC;
#endif
		alm.enabled = (logo ? 3 : 2);
	} else {
		pwron_time = 0;
		alm.enabled = 4;
	}
	alarm_rtc_dev = alarmtimer_get_rtcdev();
	rtc_time_to_tm(pwron_time, &alm.time);
	rtc_set_alarm(alarm_rtc_dev, &alm);
	rtc_set_alarm_poweron(alarm_rtc_dev, &alm);
}
void set_power_on_alarm(long secs, bool enable)
{
	int rc;
	struct timespec wall_time;
	long rtc_secs, alarm_time, alarm_delta;
	struct rtc_time rtc_time;
	struct rtc_wkalrm alarm;

	rc = mutex_lock_interruptible(&power_on_alarm_lock);
	if (rc != 0)
		return;

	if (enable) {
			power_on_alarm = secs;
	} else {
		if (power_on_alarm == secs)
			power_on_alarm = 0;
		else
			goto exit;
	}

	if (!power_on_alarm)
		goto disable_alarm;

	rtc_read_time(rtcdev, &rtc_time);
	getnstimeofday(&wall_time);
	rtc_tm_to_time(&rtc_time, &rtc_secs);
	alarm_delta = wall_time.tv_sec - rtc_secs;
	alarm_time = power_on_alarm - alarm_delta;

	/*
	 *Substract ALARM_DELTA from actual alarm time
	 *to power up the device before actual alarm
	 *expiration
	 */
#ifdef CONFIG_ZTEMT_PON_ALARM_DELTA
       if ((alarm_time - ALARM_DELTA) > rtc_secs)
               alarm_time -= ALARM_DELTA;
       else
               goto disable_alarm;
#else
	if (alarm_time <= rtc_secs)
		goto disable_alarm;
#endif

	rtc_time_to_tm(alarm_time, &alarm.time);
	alarm.enabled = 1;
	rc = rtc_set_alarm(rtcdev, &alarm);
	if (rc)
		goto disable_alarm;

	mutex_unlock(&power_on_alarm_lock);
	return;

disable_alarm:
	power_on_alarm = 0;
	rtc_alarm_irq_enable(rtcdev, 0);
exit:
	mutex_unlock(&power_on_alarm_lock);
}
Exemple #7
0
PROCESS_THREAD(rtctest_process, ev, data)
{
	static struct etimer et;
	PROCESS_BEGIN();
	printf("RTC init\r\n");
	rtc_init();
	//Alarm counter

	rtc_set_alarm(200);


	while(1) {
		etimer_set(&et, CLOCK_SECOND);
		PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));


		if (rtc_read() > 100 && rtc_read() < 150) {
			leds_off(LEDS_ALL);
			pm_stop_mode(0);
		}
		if (rtc_read() > 200) {
			leds_toggle(LEDS_ALL);
		}

		rtc_sync();
		printf("1s tick.. %d rtc alarm flag %d\r\n", rtc_read(), 				(RTC->CRL & RTC_CRL_ALRF));
		
	}
	
  PROCESS_END();
}
Exemple #8
0
/*
 * Test command to set RTC Alarm time, and start RTC: rtc alarm <rtc_alarm_time>
 *
 * @param[in]   argc        Number of arguments in the Test Command (including group and name)
 * @param[in]   argv        Table of null-terminated buffers containing the arguments
 * @param[in]   ctx         The context to pass back to responses
 */
void rtc_alarm_tcmd(int argc, char *argv[], struct tcmd_handler_ctx *ctx)
{
	struct rtc_config config = { 0 };
	struct device *rtc_dev;
	uint32_t keys = 0;

	if (argc == RTC_ARG_NO && isdigit(argv[RTC_TIME_IDX][0])) {
		keys = irq_lock();
		tcmd_user_alarm_rtc_val =
			(uint32_t)strtoul(argv[RTC_TIME_IDX], NULL,
					  10);
		test_rtc = 0;
		config.alarm_val = tcmd_user_alarm_rtc_val;
		config.alarm_enable = true;
		config.cb_fn = test_rtc_interrupt_fn;
		alarm_pending = true;
		irq_unlock(keys);

		rtc_dev = device_get_binding(RTC_DRV_NAME);
		assert(rtc_dev != NULL);
		rtc_dev->driver_data = (void *)ctx;
		rtc_enable(rtc_dev);
		rtc_set_config(rtc_dev, &config);
		rtc_set_alarm(rtc_dev, config.alarm_val);
	} else {
		TCMD_RSP_ERROR(ctx, "Usage: rtc alarm <alarm_time>");
	}
}
Exemple #9
0
void test_rtc_interrupt_fn(struct device *rtc_dev)
{
	u32_t now = rtc_read(rtc_dev);

	printk("Alarm\n");
	rtc_set_alarm(rtc_dev, now + ALARM);
}
static int suspend_autotest_suspend(struct device *dev)
{
	unsigned long now;
	struct rtc_wkalrm alm;
	int status;
	int need_test = 0;
	unsigned long alarm_time;

	/* if test isn't enabled, no need to proceed below code */
	if ((!test_enable) || (test_mode != SUSPEND))
		return 0;

	/* read alarm time */
	status = rtc_read_alarm(rtc, &alm);
	if (status) {
		pr_info("%s: rtc_read_alarm fail\n", __func__);
		return status;
	}

	rtc_tm_to_time(&alm.time, &alarm_time);
	/* if alarm is set already, alarm time should be compared.
	 * if no alarm is set, test alarm can be done.
	 */
	if (alm.enabled)
		need_test = 0;
	else
		need_test = 1;

	status = rtc_read_time(rtc, &alm.time);
	if (status < 0) {
		pr_info("%s: rtc_read_time fail\n", __func__);
		return status;
	}
	rtc_tm_to_time(&alm.time, &now);

	/* if alarm will be expired in TEST_SUSPEND_SECONDS,
	 * don't set test alarm time
	 */
	if (!need_test && alarm_time < (now + TEST_SUSPEND_SECONDS)) {
		pr_info("%s: no setting of test alarm\n", __func__);
		return 0;
	}

	memset(&alm, 0, sizeof alm);
	rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
	alm.enabled = true;

	status = rtc_set_alarm(rtc, &alm);
	if (status < 0) {
		pr_info("%s: rtc_set_alarm fail\n",__func__);
		return status;
	}

	test_alarm_set = 1;
	pr_info("%s: test alarm will be envoked after about %d sec.\n",
			__func__, TEST_SUSPEND_SECONDS);

	return 0;
}
static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct rtc_time *tm = &alrm->time;
	void __iomem *base = s3c_rtc_base;
	unsigned int alrm_en;
#ifdef CONFIG_RTC_DRV_S5M
    struct rtc_device *rtc1;
#endif
	//pr_debug
	printk("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
		 alrm->enabled,
		 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
		 tm->tm_hour, tm->tm_min, tm->tm_sec);

	alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
	writeb(0x00, base + S3C2410_RTCALM);

	if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
		alrm_en |= S3C2410_RTCALM_SECEN;
		writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
	}

	if (tm->tm_min < 60 && tm->tm_min >= 0) {
		alrm_en |= S3C2410_RTCALM_MINEN;
		writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
	}

	if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
		alrm_en |= S3C2410_RTCALM_HOUREN;
		writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
	}

	pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);

	writeb(alrm_en, base + S3C2410_RTCALM);

	s3c_rtc_setaie(dev, alrm->enabled);
	
    /*Cellon add start, Devin Yuan, 2012/10/11, for off-mode alarm*/ 
#ifdef S5M8767_OFF_ALARM
#ifdef CONFIG_RTC_DRV_S5M
	rtc1 = rtc_class_open("rtc1");
	if(rtc1 == NULL)
	{
	    printk("!!!!!! %s: unable to open rtc1 device!!!!!\n",
	            __FILE__);
	}
	else
	{
	    rtc_set_alarm(rtc1, alrm);
	    rtc_class_close(rtc1);
	}
#endif
#endif
     /*Cellon add end, Devin Yuan, 2012/10/11, for off-mode alarm*/

	return 0;
}
Exemple #12
0
int exec_alarm(void) {
	leds_toggle(LED_RED);
	alarm.date = rtc_time() + 5;
	alarm.exec_cb = exec_alarm;
	
	rtc_set_alarm(&alarm);
	
	return 0;
}
Exemple #13
0
static void _prepare_next_alarm(void)
{
    struct tm time;
    rtc_get_time(&time);
    /* set initial alarm */
    time.tm_sec += PERIOD;
    mktime(&time);
    rtc_set_alarm(&time, rtc_cb, NULL);
}
/**
 * Initialize the system controller. In particular:
 *   - Load settings
 *   - Setup the log interval alarm.
 */
Controller::Controller() {

  m_sleeping=false;
  m_powerup=false;
  //m_log_interval_seconds = 60*30;
  m_log_interval_seconds = 0;  // default set to zero to save power
  rtc_clear_alarmed();
  rtc_enable_alarm(RTC);
  m_interval_stored = rtc_get_time(RTC);
  m_counts_stored = 0;
  m_alarm_log = false;
  system_controller = this;
  m_last_switch_state = true;
  m_never_dim = false;

  bool sstate = switch_state();
  m_last_switch_state = sstate;

  m_warning_raised = false;
  m_dim_off = false;

  m_cpm_cps_switch = false;
  m_current_units = 2;
  m_cpm_cps_threshold = 1100.0;
  m_cps_cpm_threshold = 1000.0;

  // Get warning cpm from flash
  m_warncpm = 0;
  const char *swarncpm = flashstorage_keyval_get("WARNCPM");
  if(swarncpm != 0) {
    int32_t c;
    sscanf(swarncpm, "%"PRIu32"", &c);
    m_warncpm = c;
  }

  // Get never dim from flash
  m_warncpm = -1;
  const char *sneverdim = flashstorage_keyval_get("NEVERDIM");
  if(sneverdim != 0) {
    if(strcmp(sneverdim,"true") == 0) m_never_dim=true;
                                 else m_never_dim=false;
  } else m_never_dim=false;

  if(m_never_dim) tick_item("Never Dim" ,true);

  // Get logging interval from flash
  const char *sloginter = flashstorage_keyval_get("LOGINTERVAL");
  if(sloginter != 0) {
    int32_t c;
    sscanf(sloginter, "%"PRIu32"", &c);
    m_log_interval_seconds = c;
  } else {
   // m_log_interval_seconds = 30*60;
    m_log_interval_seconds = 0;  // default set to zero to save power
  }
  rtc_set_alarm(RTC,rtc_get_time(RTC)+m_log_interval_seconds);
}
Exemple #15
0
static void _rtc_setalarm(char **argv)
{
    struct tm now;

    if (_parse_time(argv, &now) == 0) {
        if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
            puts("rtc: error setting alarm");
        }
    }
}
Exemple #16
0
static ssize_t
rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	ssize_t retval;
	unsigned long now, alarm;
	struct rtc_wkalrm alm;
	struct rtc_device *rtc = to_rtc_device(dev);
	char *buf_ptr;
	int adjust = 0;
	 	printk("%s \n", __func__);
	/* Only request alarms that trigger in the future.  Disable them
	 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
	 */
	retval = rtc_read_time(rtc, &alm.time);
	sys_out("retval is %d \n", retval);
	if (retval < 0)
		return retval;
	rtc_tm_to_time(&alm.time, &now);

	buf_ptr = (char *)buf;
	if (*buf_ptr == '+') {
		buf_ptr++;
		adjust = 1;
	}
	alarm = simple_strtoul(buf_ptr, NULL, 0);
	if (adjust) {
		alarm += now;
	}
	if (alarm > now) {
		/* Avoid accidentally clobbering active alarms; we can't
		 * entirely prevent that here, without even the minimal
		 * locking from the /dev/rtcN api.
		 */
		retval = rtc_read_alarm(rtc, &alm);
		if (retval < 0)
			return retval;
		if (alm.enabled)
			return -EBUSY;

		alm.enabled = 1;
	} else {
		alm.enabled = 0;

		/* Provide a valid future alarm time.  Linux isn't EFI,
		 * this time won't be ignored when disabling the alarm.
		 */
		alarm = now + 300;
	}
	rtc_time_to_tm(alarm, &alm.time);

	retval = rtc_set_alarm(rtc, &alm);
	printk("set_alarm retval: %d\n",retval);
	return (retval < 0) ? retval : n;
}
Exemple #17
0
static void enable_wake_event(void)
{
	u32_t now = rtc_read(rtc_dev);
	u32_t alarm;

	alarm = (rtc_read(rtc_dev) + ALARM);
	rtc_set_alarm(rtc_dev, alarm);

	/* Wait a few ticks to ensure the alarm value gets loaded */
	while (rtc_read(rtc_dev) < now + 5)
		;
}
Exemple #18
0
void main(void)
{
	struct device *rtc_dev;
	struct rtc_config config;
	u32_t now;

	printk("LMT: Quark SE PM Multicore Demo\n");

	k_fifo_init(&fifo);

	build_suspend_device_list();

	ipm = device_get_binding("alarm_notification");
	if (!ipm) {
		printk("Error: Failed to get IPM device\n");
		return;
	}

	rtc_dev = device_get_binding("RTC_0");
	if (!rtc_dev) {
		printk("Error: Failed to get RTC device\n");
		return;
	}

	rtc_enable(rtc_dev);

	/* In QMSI, in order to save the alarm callback we must set
	 * 'alarm_enable = 1' during configuration. However, this
	 * automatically triggers the alarm underneath. So, to avoid
	 * the alarm being fired any time soon, we set the 'init_val'
	 * to 1 and the 'alarm_val' to 0.
	 */
	config.init_val = 1;
	config.alarm_val = 0;
	config.alarm_enable = 1;
	config.cb_fn = alarm_handler;
	rtc_set_config(rtc_dev, &config);

	while (1) {
		/* Simulate some task handling by busy waiting. */
		printk("LMT: busy\n");
		k_busy_wait(TASK_TIME_IN_SEC * 1000 * 1000);

		now = rtc_read(rtc_dev);
		rtc_set_alarm(rtc_dev,
			      now + (RTC_ALARM_SECOND * IDLE_TIME_IN_SEC));

		printk("LMT: idle\n");
		k_fifo_get(&fifo, K_FOREVER);
	}
}
Exemple #19
0
static void set_rtc_alarm(void)
{
	u32_t now = rtc_read(rtc_dev);
	u32_t alarm = now + (RTC_ALARM_SECOND * (TIMEOUT - 1));

	rtc_set_alarm(rtc_dev, alarm);

	/* Wait a few ticks to ensure the 'Counter Match Register' was loaded
	 * with the 'alarm' value.
	 * Refer to the documentation in qm_rtc.h for more details.
	 */
	while (rtc_read(rtc_dev) < now + 5)
		;
}
Exemple #20
0
static int restore_wakealarm(void)
{
	struct rtc_wkalrm	alm;
	struct rtc_device	*rtc = NULL;

	rtc = rtc_class_open(CONFIG_WAKEALARM_RTC);
	if (!rtc) {
		return -1;
	}
	alm.enabled = false;
	rtc_set_alarm(rtc, &alm);
	rtc_class_close(rtc);
	return 0;
}
/**
 * Append a new entry to the log if necessary. Checks the
 * state of m_alarm_log to determine if we should append a log
 * entry.
 */
void Controller::do_logging() {
  if(rtc_alarmed()) {
    m_alarm_log = true;
    m_last_alarm_time = rtc_get_time(RTC);
    #ifndef DISABLE_ACCEL
    accel_read_state(&m_accel_x_stored,&m_accel_y_stored,&m_accel_z_stored);
    #endif
    //m_magsensor_stored = gpio_read_bit(PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit);


    // set new alarm for log_interval_seconds from now.
    rtc_clear_alarmed();
  }

  if(m_alarm_log == true) {
    if(system_geiger->is_cpm30_valid()) {

      log_data_t data;
      #ifndef DISABLE_ACCEL
      accel_read_state(&data.accel_x_end,&data.accel_y_end,&data.accel_z_end);
      #endif
      //data.magsensor_end = gpio_read_bit(PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit);

      data.time  = rtc_get_time(RTC);
      data.cpm   = system_geiger->get_cpm30();

      data.accel_x_start = m_accel_x_stored;
      data.accel_y_start = m_accel_y_stored;
      data.accel_z_start = m_accel_z_stored;
      //data.magsensor_start = m_magsensor_stored;
      data.log_type      = UINT_MAX;

      flashstorage_log_pushback((uint8_t *) &data,sizeof(log_data_t));

      bool full = flashstorage_log_isfull();
      if((full == true) && (!m_sleeping)) {
        m_gui->show_dialog("Flash Log","is full",0,0,0,43,44,255,255);
      }

      m_alarm_log = false;

      rtc_set_alarm(RTC,m_last_alarm_time+m_log_interval_seconds);
      rtc_enable_alarm(RTC);
      if(m_sleeping) {
        power_standby();
      }
    }
  }
}
int alarm_set_rtc_ring(struct timespec alarm_time)
{
    struct rtc_wkalrm   rtc_alarm;
    unsigned long       rtc_alarm_time;

    if (pwr_rtc_dev != NULL) {
        rtc_alarm_time = alarm_time.tv_sec;
        pr_alarm(INT, "%s, alarm time: %lu\n",
                 __func__, rtc_alarm_time);
        rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
        rtc_alarm.enabled = 1;
        rtc_set_alarm(pwr_rtc_dev, &rtc_alarm);
    }
    return 0;
}
Exemple #23
0
static int set_wakealarm(void)
{
	int retval = 0;
	unsigned long now, alarm;
	struct rtc_wkalrm alm;
	struct rtc_device *rtc;

	rtc = rtc_class_open(CONFIG_WAKEALARM_RTC);
	if (!rtc) {
		return -1;
	}

	/* Only request alarms that trigger in the future.  Disable them
	 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
	 */
	retval = rtc_read_time(rtc, &alm.time);
	if (retval < 0)
		goto close_rtc;
	rtc_tm_to_time(&alm.time, &now);

	alarm = now + sleep_time;
	
	if (alarm > now) {
		/* Avoid accidentally clobbering active alarms; we can't
		 * entirely prevent that here, without even the minimal
		 * locking from the /dev/rtcN api.
		 */
		retval = rtc_read_alarm(rtc, &alm);
		if (retval < 0)
			goto close_rtc;

		alm.enabled = 1;
	} else {
		alm.enabled = 0;

		/* Provide a valid future alarm time.  Linux isn't EFI,
		 * this time won't be ignored when disabling the alarm.
		 */
		alarm = now + 300;
	}

	rtc_time_to_tm(alarm, &alm.time);
	retval = rtc_set_alarm(rtc, &alm);

close_rtc:
	rtc_class_close(rtc);
	return retval;
}
Exemple #24
0
static ssize_t
rtc_sysfs_set_wakealarm(struct device *dev, struct device_attribute *attr,
                        const char *buf, size_t n)
{
    ssize_t retval;
    unsigned long now, alarm;
    struct rtc_wkalrm alm;
    struct rtc_device *rtc = to_rtc_device(dev);
    char *buf_ptr;
    int adjust = 0;


    retval = rtc_read_time(rtc, &alm.time);
    if (retval < 0)
        return retval;
    rtc_tm_to_time(&alm.time, &now);

    buf_ptr = (char *)buf;
    if (*buf_ptr == '+') {
        buf_ptr++;
        adjust = 1;
    }
    alarm = simple_strtoul(buf_ptr, NULL, 0);
    if (adjust) {
        alarm += now;
    }
    if (alarm > now) {

        retval = rtc_read_alarm(rtc, &alm);
        if (retval < 0)
            return retval;
        if (alm.enabled)
            return -EBUSY;

        alm.enabled = 1;
    } else {
        alm.enabled = 0;


        alarm = now + 300;
    }
    rtc_time_to_tm(alarm, &alm.time);

    retval = rtc_set_alarm(rtc, &alm);
    return (retval < 0) ? retval : n;
}
Exemple #25
0
static ssize_t
rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
{
	ssize_t retval;
	unsigned long now, alarm;
	struct rtc_wkalrm alm;

	/* Only request alarms that trigger in the future.  Disable them
	 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
	 */
	retval = rtc_read_time(dev, &alm.time);
	if (retval < 0)
		return retval;
	rtc_tm_to_time(&alm.time, &now);

	alarm = simple_strtoul(buf, NULL, 0);
	if (alarm > now) {
		/* Avoid accidentally clobbering active alarms; we can't
		 * entirely prevent that here, without even the minimal
		 * locking from the /dev/rtcN api.
		 */
		retval = rtc_read_alarm(dev, &alm);
		if (retval < 0)
			return retval;
		if (alm.enabled)
			return -EBUSY;

		alm.enabled = 1;
	} else {
		alm.enabled = 0;

		/* Provide a valid future alarm time.  Linux isn't EFI,
		 * this time won't be ignored when disabling the alarm.
		 */
		alarm = now + 300;
	}
	rtc_time_to_tm(alarm, &alm.time);

	retval = rtc_set_alarm(dev, &alm);
	return (retval < 0) ? retval : n;
}
void Controller::save_loginterval() {
  int l1 = m_gui->get_item_state_uint8("LOGINTER1");
  int l2 = m_gui->get_item_state_uint8("LOGINTER2");
  int l3 = m_gui->get_item_state_uint8("LOGINTER3");
  int32_t log_interval_mins = (l1*100) + (l2*10) + l3;
  m_log_interval_seconds = log_interval_mins*60;

  char sloginterval[50];
  sprintf(sloginterval,"%"PRIu32"",m_log_interval_seconds);
  flashstorage_keyval_set("LOGINTERVAL",sloginterval);
  uint32_t current_time = realtime_get_unixtime();

if(m_log_interval_seconds > 0){
  rtc_set_alarm(RTC,current_time+m_log_interval_seconds);
}else{
     rtc_disable_alarm(RTC);
}

  m_gui->jump_to_screen(0);

}
/**
 * \brief Callback function for RTC compare interrupt handler
 *
 * The function executes when the RTC compare interrupt occurs and loop
 * through all timeout channels. The timeout_array[channel_index] which
 * contains the remaining ticks before timeout is decremented and the timeout
 * active/expired masks are updated.
 */
static void tick_handler(uint32_t time)
{
	uint8_t i;

	/* Loop through all timeout channels */
	for (i = 0; i < TIMEOUT_COUNT; i++) {
		/* Skip processing on current channel if not active */
		if (!(timeout_active & (1 << i))) {
			continue;
		}

		/* Decrement current channel with one tick */
		timeout_array[i].count--;

		/* Skip further processing on current channel if not expired */
		if (timeout_array[i].count) {
			continue;
		} else {
			/* Update expired bit mask with current channel */
			timeout_expired |= 1 << i;

			/* If Periodic timer, reset timeout counter to period
			 * time */
			if (timeout_array[i].period) {
				timeout_array[i].count
					= timeout_array[i].period;
			}
			/* If not periodic timeout, set current channel to
			 * in-active */
			else {
				timeout_active &= ~(1 << i);
			}
		}
	}
	/* Reset RTC before next tick */
	rtc_set_time(0);
	rtc_set_alarm(TIMEOUT_COMP);
}
void Controller::save_date() {
  int d1 = m_gui->get_item_state_uint8("DATEDAY1");
  int d2 = m_gui->get_item_state_uint8("DATEDAY2");
  int m1 = m_gui->get_item_state_uint8("DATEMON1");
  int m2 = m_gui->get_item_state_uint8("DATEMON2");
  int y1 = m_gui->get_item_state_uint8("DATEYEAR1");
  int y2 = m_gui->get_item_state_uint8("DATEYEAR2");

  int new_day  = d2 + (d1*10);
  int new_mon  = m2 + (m1*10);
  int new_year = y2 + (y1*10);

  uint8_t hours,min,sec,day,month;
  uint16_t year;
  realtime_getdate(hours,min,sec,day,month,year);
  day   = new_day;
  month = new_mon-1;
  year  = (2000+new_year)-1900;
  realtime_setdate(hours,min,sec,day,month,year);
  rtc_set_alarm(RTC,rtc_get_time(RTC)+m_log_interval_seconds);

  flashstorage_log_userchange();
  m_gui->jump_to_screen(0);
}
void Controller::save_time() {
  int h1 = m_gui->get_item_state_uint8("TIMEHOUR1");
  int h2 = m_gui->get_item_state_uint8("TIMEHOUR2");
  int m1 = m_gui->get_item_state_uint8("TIMEMIN1");
  int m2 = m_gui->get_item_state_uint8("TIMEMIN2");
  int s1 = m_gui->get_item_state_uint8("TIMESEC1");
  int s2 = m_gui->get_item_state_uint8("TIMESEC2");

  int new_hours = h2 + (h1*10);
  int new_min   = m2 + (m1*10);
  int new_sec   = s2 + (s1*10);

  uint8_t hours,min,sec,day,month;
  uint16_t year;
  realtime_getdate(hours,min,sec,day,month,year);
  hours = new_hours;
  min   = new_min;
  sec   = new_sec;
  realtime_setdate(hours,min,sec,day,month,year);
  rtc_set_alarm(RTC,rtc_get_time(RTC)+m_log_interval_seconds);

  flashstorage_log_userchange();
  m_gui->jump_to_screen(0);
}
void serial_process_command(char *line) {

  if(in_displayparams) {
    serial_displayparams_run(line);
    in_displayparams = false;
  }

  if(in_setdevicetag) {
    serial_setdevicetag_run(line);
    in_setdevicetag = false;
  }

  if(in_setrtc) {
    serial_setrtc_run(line);
    in_setrtc = false;
  }

  if(in_setkeyval) {
    serial_setkeyval_run(line);
    in_setkeyval = false;
  }

  serial_write_string("\r\n");
  if(strcmp(line,"HELLO") == 0) {
   serial_write_string("GREETINGS PROFESSOR FALKEN.\r\n");
  } else 
  if(strcmp(line,"LIST GAMES") == 0) {
    serial_write_string("I'M KIND OF BORED OF GAMES, TURNS OUT THE ONLY WAY TO WIN IS NOT TO PLAY...\r\n");
  } else 
  if(strcmp(line,"LOGXFER") == 0) {
    serial_sendlog();
  } else 
  if(strcmp(line,"DISPLAYPARAMS") == 0) {
    serial_displayparams();
  } else
  if(strcmp(line,"HELP") == 0) {
    serial_write_string("Available commands: HELP, LOGXFER, DISPLAYTEST, HELLO");
  } else 
  if(strcmp(line,"DISPLAYTEST") == 0) {
    display_test();
  } else
  if(strcmp(line,"LOGTEST") == 0) {
    char stemp[100];

    sprintf(stemp,"Raw log data\r\n");
    serial_write_string(stemp);

    uint8_t *flash_log = flashstorage_log_get();
    for(int n=0;n<1024;n++) {
      sprintf(stemp,"%u ",flash_log[n]);
      serial_write_string(stemp);
      if(n%64 == 0) serial_write_string("\r\n");
    }
    serial_write_string("\r\n");

    log_data_t data;
    data.time = 0;
    data.cpm  = 1;
    data.accel_x_start = 2;
    data.accel_y_start = 3;
    data.accel_z_start = 4;
    data.accel_x_end   = 5;
    data.accel_y_end   = 6;
    data.accel_z_end   = 7;
    data.log_type      = UINT_MAX;
    sprintf(stemp,"Log size: %u\r\n",flashstorage_log_size());
    serial_write_string(stemp);

    sprintf(stemp,"Writing test log entry of size: %u\r\n",sizeof(log_data_t));
    serial_write_string(stemp);

    flashstorage_log_pushback((uint8 *) &data,sizeof(log_data_t));

    sprintf(stemp,"Log size: %u\r\n",flashstorage_log_size());
    serial_write_string(stemp);
  } else 
  if(strcmp(line,"VERSION") == 0) {
    char stemp[50];
    sprintf(stemp,"Version: %s\r\n",OS100VERSION);
    serial_write_string(stemp);
  } else 
  if(strcmp(line,"GETDEVICETAG") == 0) {
    const char *devicetag = flashstorage_keyval_get("DEVICETAG");
    if(devicetag != 0) {
      char stemp[100];
      sprintf(stemp,"Devicetag: %s\r\n",devicetag);
      serial_write_string(stemp);
    } else {
      serial_write_string("No device tag set");
    }
  } else
  if(strcmp(line,"SETDEVICETAG") == 0) {
    serial_setdevicetag();
  } else 
  if(strcmp(line,"READPRIVATEKEY") == 0) {
   // serial_readprivatekey();  // removed for production
  } else
  if(strcmp(line,"WRITEPRIVATEKEY") == 0) {
   // serial_writeprivatekey(); // maybe this should be removed for production?
  } else 
  if(strcmp(line,"MAGREAD") == 0) {
    gpio_set_mode (PIN_MAP[41].gpio_device,PIN_MAP[41].gpio_bit, GPIO_OUTPUT_PP); // MAGPOWER
    gpio_set_mode (PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit, GPIO_INPUT_PU);  // MAGSENSE

    // Power up magsense
    gpio_write_bit(PIN_MAP[41].gpio_device,PIN_MAP[41].gpio_bit,1);

    // wait...
    delay_us(1000);
    
    // Read magsense
    int magsense = gpio_read_bit(PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit);

    char magsenses[50];
    sprintf(magsenses,"%u\r\n",magsense);
    serial_write_string(magsenses);
  } else
  if(strcmp(line,"WRITEDAC") == 0) {
    dac_init(DAC,DAC_CH2);

    int8_t idelta=1;
    uint8_t i=0;
    for(int n=0;n<1000000;n++) {
      
      if(i == 254) idelta = -1;
      if(i == 0  ) idelta =  1;

      i += idelta;

      dac_write_channel(DAC,2,i);
    }
    serial_write_string("WRITEDACFIN");
  } else
  if(strcmp(line,"TESTHP") == 0) {
    gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_OUTPUT_PP);  // HP_COMBINED
    for(int n=0;n<100000;n++) {
      gpio_write_bit(PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit,1);
      delay_us(100);
      gpio_write_bit(PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit,0);
      delay_us(100);
    }
  } else 
  if(strcmp(line,"READADC") == 0) {

    adc_init(PIN_MAP[12].adc_device); // all on ADC1

    adc_set_extsel(PIN_MAP[12].adc_device, ADC_SWSTART);
    adc_set_exttrig(PIN_MAP[12].adc_device, true);

    adc_enable(PIN_MAP[12].adc_device);
    adc_calibrate(PIN_MAP[12].adc_device);
    adc_set_sample_rate(PIN_MAP[12].adc_device, ADC_SMPR_55_5);

    gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_INPUT_ANALOG);
    gpio_set_mode (PIN_MAP[19].gpio_device,PIN_MAP[19].gpio_bit, GPIO_INPUT_ANALOG);
    gpio_set_mode (PIN_MAP[20].gpio_device,PIN_MAP[20].gpio_bit, GPIO_INPUT_ANALOG);

    int n=0;
    uint16 value1 = adc_read(PIN_MAP[12].adc_device,PIN_MAP[12].adc_channel);
    uint16 value2 = adc_read(PIN_MAP[19].adc_device,PIN_MAP[19].adc_channel);
    uint16 value3 = adc_read(PIN_MAP[20].adc_device,PIN_MAP[20].adc_channel);
    char values[50];
    sprintf(values,"PA6 ADC Read: %u\r\n",value1);
    serial_write_string(values);
    sprintf(values,"PC4 ADC Read: %u\r\n",value2);
    serial_write_string(values);
    sprintf(values,"PC5 ADC Read: %u\r\n",value3);
    serial_write_string(values);
  } else
  if(strcmp(line,"SETMICREVERSE") == 0) {
    gpio_set_mode (PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit, GPIO_OUTPUT_PP);  // MICREVERSE
    gpio_set_mode (PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit, GPIO_OUTPUT_PP);  // MICIPHONE
    gpio_write_bit(PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit,1); // MICREVERSE
    gpio_write_bit(PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit,0); // MICIPHONE
    serial_write_string("Set MICREVERSE to 1, MICIPHONE to 0\r\n");
  } else 
  if(strcmp(line,"SETMICIPHONE") == 0) {
    gpio_set_mode (PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit, GPIO_OUTPUT_PP);  // MICREVERSE
    gpio_set_mode (PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit, GPIO_OUTPUT_PP);  // MICIPHONE
    gpio_write_bit(PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit,0); // MICREVERSE
    gpio_write_bit(PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit,1); // MICIPHONE
    serial_write_string("Set MICREVERSE to 0, MICIPHONE to 1\r\n");
  } else
  if(strcmp(line,"TESTSIGN") == 0) {
    serial_signing_test();
  } else 
  if(strcmp(line,"PUBKEY") == 0) {
    signing_printPubKey();
    serial_write_string("\n\r");
  } else 
  if(strcmp(line,"GUID") == 0) {
    signing_printGUID();
    serial_write_string("\n\r");
  } else 
  if(strcmp(line,"KEYVALID") == 0) {
    if( signing_isKeyValid() == 1 )
      serial_write_string("uu_valid VALID KEY\r\n");
    else
      serial_write_string("uu_valid IMPROPER OR UNINITIALIZED KEY\r\n");
  } else 
  if(strcmp(line,"LOGSIG") == 0) {
    signing_hashLog();
    serial_write_string("\n\r");
  } else
  if(strcmp(line,"LOGPAUSE") == 0) {
    flashstorage_log_pause();
  } else 
  if(strcmp(line,"LOGRESUME") == 0) {
    flashstorage_log_resume();
  } else
  if(strcmp(line,"LOGCLEAR") == 0) {
    serial_write_string("Clearing flash log\r\n");
    flashstorage_log_clear();
    serial_write_string("Cleared\r\n");
  } else
  if(strcmp(line,"KEYVALDUMP") == 0) {
    serial_keyvaldump();
  } else
  if(strcmp(line,"KEYVALSET") == 0) {
    serial_setkeyval();
  } else
  if(strcmp(line,"SETRTC") == 0) {
    serial_setrtc();
  } else
  if(strcmp(line,"RTCALARM") == 0) {
    serial_write_string("Alarm triggered for 10s\r\n");
    rtc_set_alarm(RTC,rtc_get_time(RTC)+10);
  }

  serial_write_string("\r\n>");
}