-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.h
142 lines (131 loc) · 3.86 KB
/
utils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef _UTILS_H
#define _UTILS_H
/**
* various utility functions for ch32v003fun
*/
#include <stdint.h>
typedef int (*puts_ft)(char* s, int len, void* buf);
int mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...);
int _write(int fd, const char *buf, int size);
// ----------------------------------------------------------
static inline int32_t abs_i32(int32_t x) __attribute__((always_inline,unused));
static inline int32_t abs_i32(int32_t x)
{
return (x < 0)? -x : x;
}
static inline int32_t clamp_i(int32_t Value, int32_t Min, int32_t Max) __attribute__((always_inline,unused));
static inline int32_t clamp_i(int32_t Value, int32_t Min, int32_t Max)
{
return (Value < Min)? Min : (Value > Max)? Max : Value;
}
// ----------------------------------------------------------
/**
* @brief Fast multiply for ch32v003,
* author: CNLohr
* source: https://github.com/cnlohr/ch32v003fun/tree/master/examples/ws2812bdemo
*/
static inline uint32_t FastMultiply( uint32_t big_num, uint32_t small_num ) __attribute__((section(".data")));
static inline uint32_t FastMultiply( uint32_t big_num, uint32_t small_num )
{
uint32_t ret = 0;
uint32_t multiplicand = small_num;
uint32_t mutliplicant = big_num;
do
{
if( multiplicand & 1 )
ret += mutliplicant;
mutliplicant<<=1;
multiplicand>>=1;
} while( multiplicand );
return ret;
}
#if defined (UTILS_IMPLEMETATION)
// ----------------------------------------------------------
/**
* @brief print a binary representation of max 32bit value
*
* @param v input value
* @param num_places how many places, ie. 8 for an 8bit input
* @param puts_cb callback for bulk write function, if NULL
* internal _write (debug printf of uart printf)
* is used.
* Example: printBin(0x5A5A, 16, NULL);
*/
void printBin(uint32_t v, uint8_t num_places, puts_ft puts_cb)
{
uint32_t mask=0, n;
for (n=1; n<=num_places; n++)
{
mask = (mask << 1) | 0x0001UL;
}
v = v & mask; // truncate v to specified number of places
while(num_places)
{
if (v & (0x0001 << (num_places-1)))
{
if (puts_cb) puts_cb("1", 2, NULL);
else _write(0,"1", 2);
}
else
{
if (puts_cb) puts_cb("0", 2, NULL);
else _write(0,"0", 2);
}
--num_places;
if(((num_places%4) == 0) && (num_places != 0))
{
if (puts_cb) puts_cb("_", 2, NULL);
else _write(0,"_", 2);
}
}
if (puts_cb) puts_cb("\r\n", 3, NULL);
else _write(0,"\r\n", 3);
}
// ----------------------------------------------------------
const uint32_t i32_pow10[10] = {1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
/**
* @brief simple itoa not using division or modulo
*
* @param inVal input value
* @param dstBf pointer to destination buffer
* @return int number of written bytes
*/
int i32toa(int32_t inVal, uint8_t *dstBf)
{
int8_t digit[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int8_t idx = 0;
uint8_t valStarted = 0;
uint8_t neg = inVal < 0;
uint8_t *p = dstBf;
inVal = abs_i32(inVal);
while( idx < (sizeof(i32_pow10)/sizeof(int32_t)) )
{
while (inVal >= 0)
{
inVal -= i32_pow10[idx];
digit[idx]++;
}
inVal += i32_pow10[idx];
if ((digit[idx] != 0 && !valStarted) || idx == 9)
{
valStarted = 1;
if (neg) *p++ = '-';
}
if (valStarted) *p++ = '0' + digit[idx];
idx++;
}
*p++ = '\0';
return (p - dstBf);
}
// ----------------------------------------------------------
static inline uint32_t getRegBits(uint32_t reg, uint32_t mask);
static inline uint32_t getRegBits(uint32_t reg, uint32_t mask)
{
uint8_t i = 0;
while(!(mask & (1<<i))) i++;
return ((reg & mask)>>i);
}
// ----------------------------------------------------------
// ----------------------------------------------------------
#endif // UTILS_IMPLEMETATION
#endif // _UTILS_H