Changeset 1170ea3c in mainline for uspace/drv/time/cmos-rtc/cmos-rtc.c
- Timestamp:
- 2012-04-14T14:51:41Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- cf72943
- Parents:
- 95060d5b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/time/cmos-rtc/cmos-rtc.c
r95060d5b r1170ea3c 85 85 static bool rtc_update_in_progress(rtc_t *rtc); 86 86 static int rtc_register_read(rtc_t *rtc, int reg); 87 static int bcd2dec(int bcd); 87 static unsigned bcd2bin(unsigned bcd); 88 static unsigned bin2bcd(unsigned binary); 88 89 static void rtc_default_handler(ddf_fun_t *fun, 89 90 ipc_callid_t callid, ipc_call_t *call); 90 91 static int rtc_dev_remove(ddf_dev_t *dev); 92 static int rtc_tm_sanity_check(struct tm *t); 91 93 92 94 … … 233 235 /** Read a register from the CMOS memory 234 236 * 235 * @param port The I/O port assigned to thedevice237 * @param rtc The rtc device 236 238 * @param reg The index of the register to read 237 239 * … … 244 246 return pio_read_8(rtc->port + 1); 245 247 } 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 */ 257 static void 258 rtc_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 */ 246 267 247 268 /** Check if an update is in progress … … 311 332 312 333 if (bcd_mode) { 313 t->tm_sec = bcd2 dec(t->tm_sec);314 t->tm_min = bcd2 dec(t->tm_min);315 t->tm_hour = bcd2 dec(t->tm_hour);316 t->tm_mday = bcd2 dec(t->tm_mday);317 t->tm_mon = bcd2 dec(t->tm_mon);318 t->tm_year = bcd2 dec(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); 319 340 } 320 341 … … 352 373 rtc_time_set(ddf_fun_t *fun, struct tm *t) 353 374 { 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 */ 412 static int 413 rtc_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... */ 354 429 return EOK; 355 430 } … … 527 602 * @return The converted value 528 603 */ 529 static int530 bcd2 dec(intbcd)604 static unsigned 605 bcd2bin(unsigned bcd) 531 606 { 532 607 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 */ 616 static unsigned 617 bin2bcd(unsigned binary) 618 { 619 return ((binary / 10) << 4) + (binary % 10); 533 620 } 534 621
Note:
See TracChangeset
for help on using the changeset viewer.