Changeset 1170ea3c in mainline for uspace/drv/time/cmos-rtc/cmos-rtc.c


Ignore:
Timestamp:
2012-04-14T14:51:41Z (13 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cf72943
Parents:
95060d5b
Message:

Initial implementation of rtc_time_set()

  • Add rtc_tm_sanity_check() that checks if the tm structure fields are valid (not completed yet)
  • rename bcd2dec() to bcd2bin()
  • add the bin2bcd() function
  • add the rtc_register_write() function.
File:
1 edited

Legend:

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

    r95060d5b r1170ea3c  
    8585static bool rtc_update_in_progress(rtc_t *rtc);
    8686static int  rtc_register_read(rtc_t *rtc, int reg);
    87 static int  bcd2dec(int bcd);
     87static unsigned bcd2bin(unsigned bcd);
     88static unsigned bin2bcd(unsigned binary);
    8889static void rtc_default_handler(ddf_fun_t *fun,
    8990    ipc_callid_t callid, ipc_call_t *call);
    9091static int rtc_dev_remove(ddf_dev_t *dev);
     92static int rtc_tm_sanity_check(struct tm *t);
    9193
    9294
     
    233235/** Read a register from the CMOS memory
    234236 *
    235  * @param port   The I/O port assigned to the device
     237 * @param rtc    The rtc device
    236238 * @param reg    The index of the register to read
    237239 *
     
    244246        return pio_read_8(rtc->port + 1);
    245247}
     248
     249/* XXX */
     250#if 0
     251/** Write a register to the CMOS memory
     252 *
     253 * @param rtc    The rtc device
     254 * @param reg    The index of the register to write
     255 * @param data   The data to write
     256 */
     257static void
     258rtc_register_write(rtc_t *rtc, int reg, int data)
     259{
     260        pio_write_8(rtc->port, reg);
     261        pio_write_8(rtc->port + 1, data);
     262}
     263
     264#endif
     265
     266/* XXX */
    246267
    247268/** Check if an update is in progress
     
    311332
    312333        if (bcd_mode) {
    313                 t->tm_sec  = bcd2dec(t->tm_sec);
    314                 t->tm_min  = bcd2dec(t->tm_min);
    315                 t->tm_hour = bcd2dec(t->tm_hour);
    316                 t->tm_mday = bcd2dec(t->tm_mday);
    317                 t->tm_mon  = bcd2dec(t->tm_mon);
    318                 t->tm_year = bcd2dec(t->tm_year);
     334                t->tm_sec  = bcd2bin(t->tm_sec);
     335                t->tm_min  = bcd2bin(t->tm_min);
     336                t->tm_hour = bcd2bin(t->tm_hour);
     337                t->tm_mday = bcd2bin(t->tm_mday);
     338                t->tm_mon  = bcd2bin(t->tm_mon);
     339                t->tm_year = bcd2bin(t->tm_year);
    319340        }
    320341
     
    352373rtc_time_set(ddf_fun_t *fun, struct tm *t)
    353374{
     375        int rc;
     376        bool bcd_mode;
     377        rtc_t *rtc = RTC_FROM_FNODE(fun);
     378
     379        rc = rtc_tm_sanity_check(t);
     380        if (rc != EOK)
     381                return rc;
     382
     383        t->tm_mon++; /* Must start from 1, not from 0 */
     384
     385        fibril_mutex_lock(&rtc->mutex);
     386
     387        /* Check if the rtc is working in bcd mode */
     388        bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD);
     389        if (bcd_mode) {
     390                /* Convert the tm struct fields in BCD mode */
     391                t->tm_sec  = bin2bcd(t->tm_sec);
     392                t->tm_min  = bin2bcd(t->tm_min);
     393                t->tm_hour = bin2bcd(t->tm_hour);
     394                t->tm_mday = bin2bcd(t->tm_mday);
     395                t->tm_mon  = bin2bcd(t->tm_mon + 1);
     396                t->tm_year = bin2bcd(t->tm_year);
     397        }
     398
     399        /* XXX Inhibit updates */
     400
     401        fibril_mutex_unlock(&rtc->mutex);
     402
     403        return rc;
     404}
     405
     406/** Check if the tm structure contains valid values
     407 *
     408 * @param t     The tm structure to check
     409 *
     410 * @return      EOK on success or EINVAL
     411 */
     412static int
     413rtc_tm_sanity_check(struct tm *t)
     414{
     415        if (t->tm_sec < 0 || t->tm_sec > 59)
     416                return EINVAL;
     417        else if (t->tm_min < 0 || t->tm_min > 59)
     418                return EINVAL;
     419        else if (t->tm_hour < 0 || t->tm_hour > 23)
     420                return EINVAL;
     421        else if (t->tm_mday < 1 || t->tm_mday > 31)
     422                return EINVAL;
     423        else if (t->tm_mon < 0 || t->tm_mon > 11)
     424                return EINVAL;
     425        else if (t->tm_year < 0)
     426                return EINVAL;
     427
     428        /* XXX Some months have less than 31 days... */
    354429        return EOK;
    355430}
     
    527602 * @return      The converted value
    528603 */
    529 static int
    530 bcd2dec(int bcd)
     604static unsigned
     605bcd2bin(unsigned bcd)
    531606{
    532607        return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
     608}
     609
     610/** Convert from binary mode to BCD mode
     611 *
     612 * @param bcd   The number in binary mode to convert
     613 *
     614 * @return      The converted value
     615 */
     616static unsigned
     617bin2bcd(unsigned binary)
     618{
     619        return ((binary / 10) << 4) + (binary % 10);
    533620}
    534621
Note: See TracChangeset for help on using the changeset viewer.