-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm_inlines_bcd.h
81 lines (65 loc) · 1.49 KB
/
arm_inlines_bcd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!\file arm_inlines_bcd.h
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief BCD code conversion inlines
*/
/****************************************************************/
#ifndef ARM_INLINES_BCD_H_
#define ARM_INLINES_BCD_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
/****************************************************************/
/*!\brief Converts hexadecimal value to BCD
** \note Returns 0xFFFFFFFF if Hex value can't be represented on a BCD BYTE
** \param[in] hex - Hexadecimal value to convert
** \return BCD value
**/
__INLINE DWORD HexToBCD(const DWORD hex)
{
DWORD res = 0;
if (hex > 99999999U)
{
res = (DWORD) -1;
}
else
{
div_t tmp;
tmp.quot = hex;
for (uintCPU_t i = 0 ; i < 8U ; i++)
{
tmp = div(tmp.quot, 10);
res |= LSHIFT(tmp.rem, (4U * i));
}
}
return res;
}
/*!\brief Converts BCD value to hexadecimal
** \note Returns 0xFFFFFFFF if BCD value is inconsistent
** \param[in] bcd - BCD value to convert
** \return Hexadecimal value
**/
__INLINE DWORD BCDToHex(const DWORD bcd)
{
DWORD res = 0;
DWORD mult = 1U;
for (uintCPU_t i = 0 ; i < 8U ; i++)
{
const BYTE single = (BYTE) (RSHIFT(bcd, (4U * i)) & 0x0FU);
if (single > 9U)
{
res = (DWORD) -1;
break;
}
res += single * mult;
mult *= 10;
}
return res;
}
/****************************************************************/
#ifdef __cplusplus
}
#endif
#endif /* ARM_INLINES_BCD_H_ */
/****************************************************************/