Stefan Roese | 5a44c60 | 2007-10-21 08:16:12 +0200 | [diff] [blame] | 1 | /* Permission is hereby granted to copy, modify and redistribute this code |
| 2 | * in terms of the GNU Library General Public License, Version 2 or later, |
| 3 | * at your option. |
| 4 | */ |
| 5 | |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 6 | /* inline functions to translate to/from binary and binary-coded decimal |
| 7 | * (frequently found in RTC chips). |
Stefan Roese | 5a44c60 | 2007-10-21 08:16:12 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef _BCD_H |
| 11 | #define _BCD_H |
| 12 | |
Simon Glass | 0c1d7a6 | 2015-04-20 12:37:20 -0600 | [diff] [blame] | 13 | static inline unsigned int bcd2bin(unsigned int val) |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 14 | { |
Simon Glass | 0c1d7a6 | 2015-04-20 12:37:20 -0600 | [diff] [blame] | 15 | return ((val) & 0x0f) + ((val & 0xff) >> 4) * 10; |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 16 | } |
Stefan Roese | 5a44c60 | 2007-10-21 08:16:12 +0200 | [diff] [blame] | 17 | |
Simon Glass | 0c1d7a6 | 2015-04-20 12:37:20 -0600 | [diff] [blame] | 18 | static inline unsigned int bin2bcd(unsigned int val) |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 19 | { |
| 20 | return (((val / 10) << 4) | (val % 10)); |
| 21 | } |
Stefan Roese | 5a44c60 | 2007-10-21 08:16:12 +0200 | [diff] [blame] | 22 | |
| 23 | #endif /* _BCD_H */ |