/* Set the time. Get the time_in_secs which is the number of seconds since Jan 1970 and set the RTC registers * based on this value. */ int rtc_set(struct rtc_time *tmp) { unsigned long remain, days, hrs, mins, secs; pr_stamp(); if (tmp == NULL) { puts("Error setting the date/time\n"); return -1; } rtc_init(); wait_for_complete(); /* Calculate number of seconds this incoming time represents */ remain = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); /* Figure out how many days since epoch */ days = remain / NUM_SECS_IN_DAY; /* From the remaining secs, compute the hrs(0-23), mins(0-59) and secs(0-59) */ remain = remain % NUM_SECS_IN_DAY; hrs = remain / NUM_SECS_IN_HR; remain = remain % NUM_SECS_IN_HR; mins = remain / NUM_SECS_IN_MIN; secs = remain % NUM_SECS_IN_MIN; /* Encode these time values into our RTC_STAT register */ bfin_write_RTC_STAT(SET_ALARM(days, hrs, mins, secs)); return 0; }
/* returns 0 or errno on error */ int pbuf_flush( pbuf* buf, int block, /* bool_t */ unsigned int timeo, /* N.B. Not a struct timeval */ const char* const id) /* destination identifier */ { size_t len = (size_t)(buf->ptr - buf->base); int changed = 0; int nwrote = 0; int status = ENOERR; /* success */ int tmpErrno; time_t start; time_t duration; udebug(" pbuf_flush fd %d %6d %s", buf->pfd, len, block ? "block" : "" ); if(len == 0) return 0; /* nothing to do */ /* else */ (void)time(&start); if(block) changed = clr_fd_nonblock(buf->pfd); if(block && timeo != 0) /* (timeo == 0) => don't set alarm */ SET_ALARM(timeo, flush_timeo); nwrote = (int) write(buf->pfd, buf->base, len); tmpErrno = errno; /* CLR_ALRM() can change "errno" */ if(block && timeo != 0) CLR_ALRM(); if(nwrote == -1) { if((tmpErrno == EAGAIN) && (!block)) { udebug(" pbuf_flush: EAGAIN on %d bytes", len); nwrote = 0; } else { status = tmpErrno; serror(NULL == id ? "pbuf_flush(): fd=%d" : "pbuf_flush(): fd=%d, cmd=(%s)", buf->pfd, id); } } else if(nwrote == len) { /* wrote the whole buffer */ udebug(" pbuf_flush: wrote %d bytes", nwrote); buf->ptr = buf->base; len = 0; } else if(nwrote > 0) { /* partial write, just shift the buffer by the amount written */ udebug(" pbuf_flush: partial write %d of %d bytes", nwrote, len); len -= nwrote; /* could be an overlapping copy */ memmove(buf->base, buf->base + nwrote, len); buf->ptr = buf->base +len; } if(changed) set_fd_nonblock(buf->pfd); duration = time(NULL) - start; if(duration > 5) uwarn(id == NULL ? "pbuf_flush(): write(%d,,%d) to decoder took %lu s" : "pbuf_flush(): write(%d,,%d) to decoder took %lu s: %s", buf->pfd, nwrote, (unsigned long)duration, id); return status; flush_timeo: if(changed) set_fd_nonblock(buf->pfd); uerror(id == NULL ? "pbuf_flush(): write(%d,,%lu) to decoder timed-out (%lu s)" : "pbuf_flush(): write(%d,,%lu) to decoder timed-out (%lu s): %s", buf->pfd, (unsigned long)len, (unsigned long)(time(NULL) - start), id); return EAGAIN; }
void read_probes(void) { struct storage s; int32_t s_hi,s_lo; int32_t a_hi,a_lo; int32_t j_hi,j_lo; uint8_t valve; t0_temp=read_probe(PSTR("t0")); t1_temp=read_probe(PSTR("t1")); t2_temp=read_probe(PSTR("t2")); t3_temp=read_probe(PSTR("t3")); /* Don't be a thermostat if we don't have a reading */ if (t0_temp==BAD_TEMP) { SET_ALARM(ALARM_NO_TEMPERATURE); return; } UNSET_ALARM(ALARM_NO_TEMPERATURE); /* Read necessary registers from eeprom */ s=reg_storage(&set_hi); eeprom_read_block(&s_hi,(void *)s.loc.eeprom.start,4); s=reg_storage(&set_lo); eeprom_read_block(&s_lo,(void *)s.loc.eeprom.start,4); s=reg_storage(&alarm_hi); eeprom_read_block(&a_hi,(void *)s.loc.eeprom.start,4); s=reg_storage(&alarm_lo); eeprom_read_block(&a_lo,(void *)s.loc.eeprom.start,4); s=reg_storage(&jog_hi); eeprom_read_block(&j_hi,(void *)s.loc.eeprom.start,4); s=reg_storage(&jog_lo); eeprom_read_block(&j_lo,(void *)s.loc.eeprom.start,4); s=reg_storage(&vtype); valve=eeprom_read_byte((void *)s.loc.eeprom.start); /* Check alarm temperatures */ if (t0_temp>a_hi) { SET_ALARM(ALARM_TEMPERATURE_HIGH); } else { UNSET_ALARM(ALARM_TEMPERATURE_HIGH); } if (t0_temp<a_lo) { SET_ALARM(ALARM_TEMPERATURE_LOW); } else { UNSET_ALARM(ALARM_TEMPERATURE_LOW); } /* Be a thermostat, with valve opened to provide chilling */ if (t0_temp>s_hi) { desired_v0_state=1; } if (t0_temp<s_lo) { desired_v0_state=0; } v0_state=desired_v0_state; /* Check "jog" temperatures. If temperature is out of bounds, we assume that our valve may be stuck and jiggle it to try to free it. We invert v0_state for jog/flip and then wait jog/wait before trying again. We have one timer for this, jog_timer, which counts down to zero and then stays there until we reset it. */ if (t0_temp<j_lo || t0_temp>j_hi) { SET_ALARM(ALARM_VALVE_STUCK); if (jog_timer==0) { jiggling=!jiggling; if (jiggling) { trigger_jog_timer(&jog_flip); } else { trigger_jog_timer(&jog_wait); } } if (jiggling) v0_state=!v0_state; } else { /* Temperature is within bounds. If the flip timer expires, start the wait timer. */ UNSET_ALARM(ALARM_VALVE_STUCK); if (jiggling && jog_timer==0) { jiggling=0; trigger_jog_timer(&jog_wait); } } switch (valve) { case 0: case 0xff: /* Spring-return valve on VALVE1, nothing on VALVE2 */ if (v0_state && !v1_output_on) { trigger_relay(VALVE1_SET); v1_output_on=1; } if (!v0_state && v1_output_on) { trigger_relay(VALVE1_RESET); v1_output_on=0; } break; case 1: /* Ball valve: open on VALVE1, close on VALVE2, no limit sensors */ case 2: /* As case 1, but with limit sensors */ if (v0_state) { /* VALVE1 should be energised, VALVE2 should be de-energised */ if (v2_output_on) { trigger_relay(VALVE2_RESET); v2_output_on=0; } if (!v1_output_on) { trigger_relay(VALVE1_SET); v1_output_on=1; } } else { /* VALVE1 should be de-energised, VALVE2 should be energised */ if (v1_output_on) { trigger_relay(VALVE1_RESET); v1_output_on=0; } if (!v2_output_on) { trigger_relay(VALVE2_SET); v2_output_on=1; } } break; } }