Example #1
0
static int
ds1307_settime(device_t dev, struct timespec *ts)
{
	int error;
	struct clocktime ct;
	struct ds1307_softc *sc;
	uint8_t data[8];

	sc = device_get_softc(dev);
	/* Accuracy is only one second. */
	if (ts->tv_nsec >= 500000000)
		ts->tv_sec++;
	ts->tv_nsec = 0;
	clock_ts_to_ct(ts, &ct);
	memset(data, 0, sizeof(data));
	data[0] = DS1307_SECS;
	data[DS1307_SECS + 1] = TOBCD(ct.sec);
	data[DS1307_MINS + 1] = TOBCD(ct.min);
	data[DS1307_HOUR + 1] = TOBCD(ct.hour);
	data[DS1307_DATE + 1] = TOBCD(ct.day);
	data[DS1307_WEEKDAY + 1] = ct.dow;
	data[DS1307_MONTH + 1] = TOBCD(ct.mon);
	data[DS1307_YEAR + 1] = TOBCD(ct.year % 100);
	/* Write the time back to RTC. */
	error = ds1307_write(dev, sc->sc_addr, data, sizeof(data));
	if (error != 0)
		device_printf(dev, "cannot write to RTC.\n");

	return (error);
}
Example #2
0
static int
ds1307_osc_enable(struct ds1307_softc *sc)
{
	int error;
	uint8_t data[2], secs;

	secs = 0;
	error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_SECS,
	    &secs, sizeof(secs));
	if (error) {
		device_printf(sc->sc_dev, "cannot read from RTC.\n");
		return (error);
	}
	/* Check if the oscillator is disabled. */
	if ((secs & DS1307_SECS_CH) == 0)
		return (0);
	device_printf(sc->sc_dev, "clock was halted, check the battery.\n");
	data[0] = DS1307_SECS;
	data[1] = secs & DS1307_SECS_MASK;
	error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
	if (error != 0)
		device_printf(sc->sc_dev, "cannot write to RTC.\n");

	return (error);
}
Example #3
0
void rtc_set(uint32_t new_time) {
#ifdef RTC_STUB
  time.time = new_time;
#else
  time_union_t tut = (time_union_t) new_time;

  ds1307_regs.ss = tut.bytes[0];
  ds1307_regs.mm = tut.bytes[1];
  ds1307_regs.hh = tut.bytes[2];

  ds1307_write();
#endif
}
Example #4
0
static int
ds1307_ctrl_write(struct ds1307_softc *sc)
{
	int error;
	uint8_t data[2];

	data[0] = DS1307_CONTROL;
	data[1] = sc->sc_ctrl & DS1307_CTRL_MASK;
	error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
	if (error != 0)
		device_printf(sc->sc_dev, "cannot write to RTC.\n");

	return (error);
}
Example #5
0
void terminal_set_weekday()
{
	// check argument format
	if(usart_command[8] <= '0' || usart_command[8] >= '8') {
		usart_transmit_async("Wrong weekday format\r\n");
		return;
	}
	
	ds1307_data.weekday = (usart_command[8] - '0');
	
	ds1307_write();

	//
	usart_transmit_async("OK\r\n");
	
}
Example #6
0
void terminal_set_date()
{
	// check argument format
	for(uint8_t i = 5; i < 11; i++) {
		if(usart_command[i] < '0' || usart_command[i] > '9') {
			usart_transmit_async("Wrong date format\r\n");
			return;
		}
	}
	
	ds1307_data.day = (usart_command[5] - '0') * 10 + (usart_command[6] - '0');
	ds1307_data.month = (usart_command[7] - '0') * 10 + (usart_command[8] - '0');
	ds1307_data.year = (usart_command[9] - '0') * 10 + (usart_command[10] - '0');
	
	ds1307_write();
	
	//
	usart_transmit_async("OK\r\n");
}
Example #7
0
static int
ds1307_set_24hrs_mode(struct ds1307_softc *sc)
{
	int error;
	uint8_t data[2], hour;

	hour = 0;
	error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_HOUR,
	    &hour, sizeof(hour));
	if (error) {
		device_printf(sc->sc_dev, "cannot read from RTC.\n");
		return (error);
	}
	data[0] = DS1307_HOUR;
	data[1] = hour & DS1307_HOUR_MASK;
	error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
	if (error != 0)
		device_printf(sc->sc_dev, "cannot write to RTC.\n");

	return (error);
}
void rtc_init(unsigned char rs,unsigned char sqwe,unsigned char out)    {

        unsigned char ctrl_reg = (unsigned char) ( out<<7 | sqwe<<4 | rs );  
        ds1307_write(0x07,ctrl_reg);
        
}