Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset db8d552 in mainline


Ignore:
Timestamp:
2012-04-03T10:23:48Z (10 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master
Children:
05ed9d7
Parents:
d9c11eed
Message:

rtc: read the time from the rtc:

  • Perform the read in the rtc_time_get() function
  • add a function to convert from BCD to binary mode
  • change the rtc_register_read() prototype, pass the rtc_t structure instead of the ioport8_t, it helps the code readability.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/time/cmos-rtc/cmos-rtc.c

    rd9c11eed rdb8d552  
    8080static void rtc_close(ddf_fun_t *fun);
    8181static bool rtc_update_in_progress(rtc_t *rtc);
    82 static int  rtc_register_read(ioport8_t *port, int reg);
     82static int  rtc_register_read(rtc_t *rtc, int reg);
     83static int  bcd2dec(int bcd);
    8384
    8485
     
    231232 */
    232233static int
    233 rtc_register_read(ioport8_t *port, int reg)
    234 {
    235         pio_write_8(port, reg);
    236         return pio_read_8(port + 1);
     234rtc_register_read(rtc_t *rtc, int reg)
     235{
     236        pio_write_8(rtc->port, reg);
     237        return pio_read_8(rtc->port + 1);
    237238}
    238239
     
    246247rtc_update_in_progress(rtc_t *rtc)
    247248{
    248         return rtc_register_read(rtc->port, RTC_UPDATE) & RTC_MASK_UPDATE;
     249        return rtc_register_read(rtc, RTC_UPDATE) & RTC_MASK_UPDATE;
    249250}
    250251
     
    259260rtc_time_get(ddf_fun_t *fun, struct tm *t)
    260261{
     262        bool bcd_mode;
    261263        rtc_t *rtc = RTC_FROM_FNODE(fun);
    262264
    263265        fibril_mutex_lock(&rtc->mutex);
    264266
    265         while (rtc_update_in_progress(rtc));
     267        /* now read the registers */
     268        do {
     269                /* Suspend until the update process has finished */
     270                while (rtc_update_in_progress(rtc));
     271
     272                t->tm_sec  = rtc_register_read(rtc, RTC_SEC);
     273                t->tm_min  = rtc_register_read(rtc, RTC_MIN);
     274                t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
     275                t->tm_mday = rtc_register_read(rtc, RTC_DAY);
     276                t->tm_mon  = rtc_register_read(rtc, RTC_MON);
     277                t->tm_year = rtc_register_read(rtc, RTC_YEAR);
     278
     279                /* Now check if it is stable */
     280        } while( t->tm_sec  != rtc_register_read(rtc, RTC_SEC) ||
     281                 t->tm_min  != rtc_register_read(rtc, RTC_MIN) ||
     282                 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
     283                 t->tm_mon  != rtc_register_read(rtc, RTC_MON) ||
     284                 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
     285
     286        /* Check if the RTC is working in BCD mode */
     287        bcd_mode = rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD;
     288
     289        if (bcd_mode) {
     290                t->tm_sec  = bcd2dec(t->tm_sec);
     291                t->tm_min  = bcd2dec(t->tm_min);
     292                t->tm_hour = bcd2dec(t->tm_hour);
     293                t->tm_mday = bcd2dec(t->tm_mday);
     294                t->tm_mon  = bcd2dec(t->tm_mon);
     295                t->tm_year = bcd2dec(t->tm_year);
     296        }
    266297
    267298        fibril_mutex_unlock(&rtc->mutex);
     
    392423}
    393424
     425/** Convert from BCD mode to binary mode
     426 *
     427 * @param bcd   The number in BCD format to convert
     428 *
     429 * @return      The converted value
     430 */
     431static int
     432bcd2dec(int bcd)
     433{
     434        return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
     435}
     436
    394437int
    395438main(int argc, char **argv)
Note: See TracChangeset for help on using the changeset viewer.