refactor: code style (if-else in one line)

remove: old hanning window
feat: FFT fixed length
This commit is contained in:
liuyihui 2023-04-24 01:05:42 +08:00
parent 384369ca62
commit f1348dd584
8 changed files with 48 additions and 219 deletions

View File

@ -19,7 +19,7 @@ AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None AlwaysBreakAfterReturnType: None

View File

@ -1178,10 +1178,8 @@ __attribute__((always_inline)) __STATIC_INLINE int32_t __QSUB(int32_t op1, int32
#define __PKHTB(ARG1, ARG2, ARG3) \ #define __PKHTB(ARG1, ARG2, ARG3) \
({ \ ({ \
uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \ uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \
if (ARG3 == 0) \ if (ARG3 == 0) __ASM("pkhtb %0, %1, %2" : "=r"(__RES) : "r"(__ARG1), "r"(__ARG2)); \
__ASM("pkhtb %0, %1, %2" : "=r"(__RES) : "r"(__ARG1), "r"(__ARG2)); \ else __ASM("pkhtb %0, %1, %2, asr %3" : "=r"(__RES) : "r"(__ARG1), "r"(__ARG2), "I"(ARG3)); \
else \
__ASM("pkhtb %0, %1, %2, asr %3" : "=r"(__RES) : "r"(__ARG1), "r"(__ARG2), "I"(ARG3)); \
__RES; \ __RES; \
}) })

View File

@ -5,7 +5,7 @@
#define q15_t int16_t #define q15_t int16_t
#define q31_t int32_t #define q31_t int32_t
#define FFT_BIN(num, fs, size) (uint16_t)((float)(num) * (float)fs / (float)size) #define FFT_BIN(num, re) (uint16_t)((float)(num + 0.5) * re)
#define FFT_INDEX(freq, fs, size) (uint16_t)((float)freq / ((float)fs / (float)size)) #define FFT_INDEX(freq, re) (uint16_t)((float)freq / re)
int FFT(q15_t *data, uint16_t len); int FFT(q15_t *data);

View File

@ -2,10 +2,7 @@
#include "fft.h" #include "fft.h"
extern const q15_t hanning_128[]; extern const q15_t hanning_160[];
extern const q15_t hanning_256[];
extern const q15_t hanning_512[];
extern const q15_t hanning_1024[];
extern const uint16_t bitrevTable[]; extern const uint16_t bitrevTable[];
extern const q15_t rotCoef[]; extern const q15_t rotCoef[];

View File

@ -20,8 +20,7 @@ static inline void uart_init(UART_Type *UART, unsigned long baud) {
PORTE->PCR[23] = PORT_PCR_MUX(0x3); PORTE->PCR[23] = PORT_PCR_MUX(0x3);
PORTE->PCR[22] = PORT_PCR_MUX(0x3); PORTE->PCR[22] = PORT_PCR_MUX(0x3);
} else } else return;
return;
// Make sure that the transmitter and receiver are disabled while we change settings. // Make sure that the transmitter and receiver are disabled while we change settings.
UART->C2 &= (uint8_t)(~(UART_C2_TE_MASK | UART_C2_RE_MASK)); UART->C2 &= (uint8_t)(~(UART_C2_TE_MASK | UART_C2_RE_MASK));
@ -43,10 +42,8 @@ static inline void uart_init(UART_Type *UART, unsigned long baud) {
static inline void uart_rie_enable(UART_Type *UART) { static inline void uart_rie_enable(UART_Type *UART) {
// Enable UART interrupt // Enable UART interrupt
if (UART == UART1) if (UART == UART1) NVIC_EnableIRQ(UART1_IRQn);
NVIC_EnableIRQ(UART1_IRQn); else if (UART == UART2) NVIC_EnableIRQ(UART2_IRQn);
else if (UART == UART2)
NVIC_EnableIRQ(UART2_IRQn);
UART->C2 |= UART_C2_RIE_MASK; // Receiver interrupt enable UART->C2 |= UART_C2_RIE_MASK; // Receiver interrupt enable
} }

View File

@ -4,13 +4,15 @@
#include "math.h" #include "math.h"
#include "stdlib.h" #include "stdlib.h"
#define N 1024
#define Re(x) 2 * x #define Re(x) 2 * x
#define Im(x) 2 * x + 1 #define Im(x) 2 * x + 1
static inline void bitreversal(q31_t *data, uint16_t len, uint16_t *bitrev, uint16_t step) { static inline void bitreversal(q31_t *data) {
q31_t tmp; q31_t tmp;
uint16_t n1 = len >> 1; uint16_t n1 = N >> 1;
uint16_t n2 = n1 + 1; uint16_t n2 = n1 + 1;
uint16_t *bitrev = (uint16_t *)&bitrevTable[0];
for (uint16_t i = 0, j = 0; i <= (n1 - 2); i += 2) { for (uint16_t i = 0, j = 0; i <= (n1 - 2); i += 2) {
if (i < j) { if (i < j) {
@ -27,14 +29,15 @@ static inline void bitreversal(q31_t *data, uint16_t len, uint16_t *bitrev, uint
data[j + n1] = tmp; data[j + n1] = tmp;
j = *bitrev; j = *bitrev;
bitrev += step; bitrev++;
} }
} }
static inline void butterfly(q15_t *data, uint16_t len, uint16_t step) { static inline void butterfly(q15_t *data) {
uint16_t step = 1;
q15_t xt, yt, cosv, sinv; q15_t xt, yt, cosv, sinv;
for (uint16_t m = len; m > 1; m = m >> 1) { for (uint16_t m = N; m > 1; m = m >> 1) {
uint16_t n1 = m; uint16_t n1 = m;
uint16_t n2 = m >> 1; uint16_t n2 = m >> 1;
@ -43,9 +46,9 @@ static inline void butterfly(q15_t *data, uint16_t len, uint16_t step) {
sinv = rotCoef[ia * 2 + 1]; sinv = rotCoef[ia * 2 + 1];
ia = ia + step; ia = ia + step;
for (uint16_t j = i, k; j < len; j += n1) { for (uint16_t j = i, k; j < N; j += n1) {
k = j + n2; k = j + n2;
if (m == len) { if (m == N) {
xt = (data[Re(j)] >> 2u) - (data[Re(k)] >> 2u); xt = (data[Re(j)] >> 2u) - (data[Re(k)] >> 2u);
yt = (data[Im(j)] >> 2u) - (data[Im(k)] >> 2u); yt = (data[Im(j)] >> 2u) - (data[Im(k)] >> 2u);
@ -79,57 +82,17 @@ static inline void butterfly(q15_t *data, uint16_t len, uint16_t step) {
} }
} }
static inline void applyWindow(q15_t *src, const q15_t *window, uint16_t len) { int FFT(q15_t *data) {
while (len--) { q15_t out[N * 2];
*src = (q15_t)(((q31_t)(*src) * (*window)) >> 15);
src++;
window++;
}
}
int FFT(q15_t *data, uint16_t len) { for (uint16_t i = 0; i < N; i++) {
uint16_t step;
uint16_t *bitrev;
q15_t out[len * 2];
switch (len) {
case 1024u:
step = 1u;
bitrev = (uint16_t *)&bitrevTable[0];
applyWindow(data, hanning_1024, 1024);
break;
case 512u:
step = 2u;
bitrev = (uint16_t *)&bitrevTable[1];
applyWindow(data, hanning_512, 512);
break;
case 256u:
step = 4u;
bitrev = (uint16_t *)&bitrevTable[3];
applyWindow(data, hanning_256, 256);
break;
case 128u:
step = 8u;
bitrev = (uint16_t *)&bitrevTable[7];
applyWindow(data, hanning_128, 128);
break;
default:
return -1;
break;
}
for (uint16_t i = 0; i < len; i++) {
out[Re(i)] = data[i]; // real out[Re(i)] = data[i]; // real
out[Im(i)] = 0; // imaginary out[Im(i)] = 0; // imaginary
} }
butterfly(out, len, step); butterfly(out);
bitreversal((q31_t *)out, len, bitrev, step); bitreversal((q31_t *)out);
for (uint16_t i = 0; i < len; i++) data[i] = (q15_t)abs(out[2 * i]); for (uint16_t i = 0; i < N; i++) data[i] = (q15_t)abs(out[2 * i]);
return 0; return 0;
} }

View File

@ -1,135 +1,16 @@
#include "fft/table.h" #include "fft/table.h"
const q15_t hanning_128[] = { const q15_t hanning_160[] = {
0, 20, 80, 180, 319, 498, 716, 972, 1266, 1597, 1964, 2366, 2803, 3273, 3775, 4308, 0, 13, 51, 115, 204, 319, 458, 623, 812, 1025, 1263, 1524, 1808, 2115, 2444, 2795,
4870, 5461, 6078, 6720, 7386, 8075, 8783, 9510, 10254, 11013, 11785, 12568, 13361, 14161, 14966, 15775, 3167, 3560, 3973, 4405, 4856, 5325, 5811, 6314, 6832, 7366, 7913, 8474, 9047, 9631, 10226, 10831,
16586, 17396, 18203, 19006, 19803, 20591, 21369, 22134, 22886, 23622, 24340, 25038, 25716, 26370, 27000, 27604, 11444, 12065, 12693, 13327, 13965, 14607, 15252, 15898, 16546, 17193, 17839, 18482, 19123, 19759, 20390, 21014,
28181, 28729, 29246, 29732, 30186, 30605, 30990, 31339, 31652, 31927, 32164, 32362, 32521, 32641, 32721, 32761, 21631, 22240, 22840, 23430, 24009, 24576, 25130, 25671, 26197, 26707, 27202, 27680, 28140, 28581, 29004, 29407,
32761, 32721, 32641, 32521, 32362, 32164, 31927, 31652, 31339, 30990, 30605, 30186, 29732, 29246, 28729, 28181, 29789, 30151, 30491, 30809, 31105, 31378, 31627, 31852, 32054, 32230, 32383, 32510, 32612, 32688, 32739, 32765,
27604, 27000, 26370, 25716, 25038, 24340, 23622, 22886, 22134, 21369, 20591, 19803, 19006, 18203, 17396, 16586, 32765, 32739, 32688, 32612, 32510, 32383, 32230, 32054, 31852, 31627, 31378, 31105, 30809, 30491, 30151, 29789,
15775, 14966, 14161, 13361, 12568, 11785, 11013, 10254, 9510, 8783, 8075, 7386, 6720, 6078, 5461, 4870, 29407, 29004, 28581, 28140, 27680, 27202, 26707, 26197, 25671, 25130, 24576, 24009, 23430, 22840, 22240, 21631,
4308, 3775, 3273, 2803, 2366, 1964, 1597, 1266, 972, 716, 498, 319, 180, 80, 20, 0, 21014, 20390, 19759, 19123, 18482, 17839, 17193, 16546, 15898, 15252, 14607, 13965, 13327, 12693, 12065, 11444,
}; 10831, 10226, 9631, 9047, 8474, 7913, 7366, 6832, 6314, 5811, 5325, 4856, 4405, 3973, 3560, 3167,
2795, 2444, 2115, 1808, 1524, 1263, 1025, 812, 623, 458, 319, 204, 115, 51, 13, 0,
const q15_t hanning_256[] = {
0, 4, 19, 44, 79, 124, 178, 243, 317, 401, 494, 598, 710, 833, 965, 1106,
1256, 1416, 1585, 1762, 1949, 2144, 2348, 2561, 2782, 3011, 3248, 3493, 3746, 4007, 4275, 4551,
4834, 5124, 5420, 5724, 6033, 6349, 6672, 7000, 7333, 7673, 8017, 8367, 8721, 9080, 9444, 9812,
10183, 10559, 10938, 11320, 11706, 12094, 12485, 12878, 13273, 13671, 14070, 14470, 14871, 15274, 15677, 16080,
16484, 16888, 17291, 17694, 18096, 18496, 18896, 19294, 19691, 20085, 20477, 20867, 21253, 21637, 22018, 22395,
22769, 23139, 23505, 23866, 24223, 24575, 24922, 25264, 25600, 25931, 26256, 26575, 26888, 27195, 27495, 27788,
28074, 28354, 28626, 28890, 29147, 29396, 29638, 29871, 30096, 30313, 30521, 30720, 30911, 31094, 31267, 31431,
31586, 31732, 31868, 31996, 32113, 32221, 32320, 32408, 32488, 32557, 32616, 32666, 32706, 32735, 32755, 32765,
32765, 32755, 32735, 32706, 32666, 32616, 32557, 32488, 32408, 32320, 32221, 32113, 31996, 31868, 31732, 31586,
31431, 31267, 31094, 30911, 30720, 30521, 30313, 30096, 29871, 29638, 29396, 29147, 28890, 28626, 28354, 28074,
27788, 27495, 27195, 26888, 26575, 26256, 25931, 25600, 25264, 24922, 24575, 24223, 23866, 23505, 23139, 22769,
22395, 22018, 21637, 21253, 20867, 20477, 20085, 19691, 19294, 18896, 18496, 18096, 17694, 17291, 16888, 16484,
16080, 15677, 15274, 14871, 14470, 14070, 13671, 13273, 12878, 12485, 12094, 11706, 11320, 10938, 10559, 10183,
9812, 9444, 9080, 8721, 8367, 8017, 7673, 7333, 7000, 6672, 6349, 6033, 5724, 5420, 5124, 4834,
4551, 4275, 4007, 3746, 3493, 3248, 3011, 2782, 2561, 2348, 2144, 1949, 1762, 1585, 1416, 1256,
1106, 965, 833, 710, 598, 494, 401, 317, 243, 178, 124, 79, 44, 19, 4, 0,
};
const q15_t hanning_512[] = {
0, 1, 4, 11, 19, 30, 44, 60, 79, 100, 123, 149, 178, 208, 242, 277,
316, 356, 399, 445, 492, 543, 595, 650, 708, 767, 830, 894, 961, 1030, 1102, 1175,
1251, 1330, 1410, 1493, 1579, 1666, 1756, 1847, 1941, 2038, 2136, 2237, 2339, 2444, 2551, 2660,
2771, 2884, 2999, 3117, 3236, 3357, 3480, 3605, 3732, 3861, 3992, 4125, 4259, 4396, 4534, 4674,
4816, 4959, 5105, 5252, 5400, 5551, 5703, 5856, 6011, 6168, 6326, 6486, 6647, 6810, 6974, 7140,
7307, 7475, 7645, 7816, 7989, 8162, 8337, 8513, 8690, 8869, 9048, 9229, 9411, 9594, 9778, 9962,
10148, 10335, 10523, 10711, 10901, 11091, 11282, 11474, 11666, 11860, 12054, 12248, 12443, 12639, 12836, 13033,
13230, 13428, 13626, 13825, 14024, 14224, 14424, 14624, 14824, 15025, 15226, 15427, 15628, 15829, 16030, 16232,
16433, 16635, 16836, 17038, 17239, 17440, 17641, 17842, 18042, 18242, 18442, 18642, 18841, 19040, 19239, 19437,
19635, 19832, 20029, 20225, 20420, 20615, 20810, 21003, 21196, 21388, 21580, 21770, 21960, 22149, 22337, 22525,
22711, 22896, 23080, 23264, 23446, 23627, 23807, 23986, 24164, 24341, 24517, 24691, 24864, 25035, 25206, 25375,
25543, 25709, 25874, 26037, 26199, 26360, 26519, 26677, 26832, 26987, 27140, 27291, 27440, 27588, 27734, 27879,
28021, 28162, 28301, 28439, 28574, 28708, 28839, 28969, 29097, 29223, 29348, 29470, 29590, 29708, 29824, 29939,
30051, 30161, 30269, 30375, 30478, 30580, 30679, 30777, 30872, 30965, 31056, 31144, 31230, 31314, 31396, 31476,
31553, 31628, 31700, 31771, 31839, 31904, 31968, 32029, 32087, 32144, 32197, 32249, 32298, 32344, 32389, 32430,
32470, 32507, 32541, 32573, 32603, 32630, 32655, 32677, 32697, 32714, 32729, 32741, 32751, 32759, 32764, 32766,
32766, 32764, 32759, 32751, 32741, 32729, 32714, 32697, 32677, 32655, 32630, 32603, 32573, 32541, 32507, 32470,
32430, 32389, 32344, 32298, 32249, 32197, 32144, 32087, 32029, 31968, 31904, 31839, 31771, 31700, 31628, 31553,
31476, 31396, 31314, 31230, 31144, 31056, 30965, 30872, 30777, 30679, 30580, 30478, 30375, 30269, 30161, 30051,
29939, 29824, 29708, 29590, 29470, 29348, 29223, 29097, 28969, 28839, 28708, 28574, 28439, 28301, 28162, 28021,
27879, 27734, 27588, 27440, 27291, 27140, 26987, 26832, 26677, 26519, 26360, 26199, 26037, 25874, 25709, 25543,
25375, 25206, 25035, 24864, 24691, 24517, 24341, 24164, 23986, 23807, 23627, 23446, 23264, 23080, 22896, 22711,
22525, 22337, 22149, 21960, 21770, 21580, 21388, 21196, 21003, 20810, 20615, 20420, 20225, 20029, 19832, 19635,
19437, 19239, 19040, 18841, 18642, 18442, 18242, 18042, 17842, 17641, 17440, 17239, 17038, 16836, 16635, 16433,
16232, 16030, 15829, 15628, 15427, 15226, 15025, 14824, 14624, 14424, 14224, 14024, 13825, 13626, 13428, 13230,
13033, 12836, 12639, 12443, 12248, 12054, 11860, 11666, 11474, 11282, 11091, 10901, 10711, 10523, 10335, 10148,
9962, 9778, 9594, 9411, 9229, 9048, 8869, 8690, 8513, 8337, 8162, 7989, 7816, 7645, 7475, 7307,
7140, 6974, 6810, 6647, 6486, 6326, 6168, 6011, 5856, 5703, 5551, 5400, 5252, 5105, 4959, 4816,
4674, 4534, 4396, 4259, 4125, 3992, 3861, 3732, 3605, 3480, 3357, 3236, 3117, 2999, 2884, 2771,
2660, 2551, 2444, 2339, 2237, 2136, 2038, 1941, 1847, 1756, 1666, 1579, 1493, 1410, 1330, 1251,
1175, 1102, 1030, 961, 894, 830, 767, 708, 650, 595, 543, 492, 445, 399, 356, 316,
277, 242, 208, 178, 149, 123, 100, 79, 60, 44, 30, 19, 11, 4, 1, 0,
};
const q15_t hanning_1024[] = {
0, 0, 1, 2, 4, 7, 11, 15, 19, 25, 30, 37, 44, 52, 60, 69,
79, 89, 100, 111, 123, 136, 149, 163, 177, 192, 208, 224, 241, 259, 277, 296,
315, 335, 355, 377, 398, 421, 444, 467, 491, 516, 542, 568, 594, 621, 649, 677,
706, 736, 766, 797, 828, 860, 892, 925, 959, 993, 1028, 1063, 1099, 1136, 1173, 1211,
1249, 1288, 1327, 1367, 1408, 1449, 1491, 1533, 1576, 1619, 1663, 1707, 1752, 1798, 1844, 1891,
1938, 1986, 2034, 2083, 2132, 2182, 2232, 2283, 2335, 2387, 2439, 2493, 2546, 2600, 2655, 2710,
2766, 2822, 2879, 2936, 2994, 3052, 3111, 3170, 3230, 3290, 3351, 3412, 3474, 3536, 3599, 3662,
3725, 3789, 3854, 3919, 3985, 4051, 4117, 4184, 4252, 4319, 4388, 4456, 4526, 4595, 4665, 4736,
4807, 4878, 4950, 5023, 5095, 5168, 5242, 5316, 5390, 5465, 5540, 5616, 5692, 5769, 5845, 5923,
6000, 6078, 6157, 6236, 6315, 6394, 6474, 6555, 6635, 6716, 6798, 6880, 6962, 7044, 7127, 7210,
7294, 7378, 7462, 7547, 7631, 7717, 7802, 7888, 7974, 8061, 8148, 8235, 8322, 8410, 8498, 8586,
8675, 8764, 8853, 8943, 9033, 9123, 9213, 9304, 9395, 9486, 9577, 9669, 9761, 9853, 9945, 10038,
10131, 10224, 10317, 10411, 10505, 10599, 10693, 10787, 10882, 10977, 11072, 11167, 11263, 11359, 11454, 11550,
11647, 11743, 11840, 11936, 12033, 12130, 12228, 12325, 12423, 12520, 12618, 12716, 12814, 12913, 13011, 13110,
13208, 13307, 13406, 13505, 13604, 13703, 13803, 13902, 14002, 14101, 14201, 14301, 14401, 14500, 14600, 14700,
14801, 14901, 15001, 15101, 15202, 15302, 15402, 15503, 15603, 15704, 15805, 15905, 16006, 16106, 16207, 16308,
16408, 16509, 16609, 16710, 16811, 16911, 17012, 17112, 17213, 17313, 17414, 17514, 17615, 17715, 17815, 17915,
18015, 18116, 18216, 18316, 18415, 18515, 18615, 18715, 18814, 18914, 19013, 19112, 19211, 19311, 19409, 19508,
19607, 19706, 19804, 19902, 20001, 20099, 20197, 20294, 20392, 20490, 20587, 20684, 20781, 20878, 20975, 21071,
21167, 21264, 21360, 21455, 21551, 21646, 21742, 21837, 21931, 22026, 22120, 22214, 22308, 22402, 22495, 22589,
22682, 22775, 22867, 22959, 23051, 23143, 23235, 23326, 23417, 23508, 23598, 23688, 23778, 23868, 23957, 24046,
24135, 24224, 24312, 24400, 24487, 24575, 24662, 24748, 24835, 24921, 25007, 25092, 25177, 25262, 25346, 25430,
25514, 25597, 25680, 25763, 25845, 25927, 26009, 26090, 26171, 26252, 26332, 26411, 26491, 26570, 26648, 26727,
26805, 26882, 26959, 27036, 27112, 27188, 27263, 27338, 27413, 27487, 27561, 27634, 27707, 27780, 27852, 27923,
27995, 28065, 28136, 28206, 28275, 28344, 28412, 28481, 28548, 28615, 28682, 28748, 28814, 28879, 28944, 29009,
29073, 29136, 29199, 29261, 29323, 29385, 29446, 29506, 29566, 29626, 29685, 29743, 29801, 29859, 29916, 29972,
30028, 30083, 30138, 30193, 30247, 30300, 30353, 30405, 30457, 30508, 30559, 30609, 30659, 30708, 30756, 30804,
30852, 30899, 30945, 30991, 31036, 31081, 31125, 31169, 31212, 31254, 31296, 31338, 31379, 31419, 31459, 31498,
31536, 31574, 31612, 31648, 31685, 31720, 31755, 31790, 31824, 31857, 31890, 31922, 31954, 31985, 32015, 32045,
32074, 32103, 32131, 32158, 32185, 32211, 32237, 32262, 32287, 32311, 32334, 32357, 32379, 32400, 32421, 32441,
32461, 32480, 32498, 32516, 32533, 32550, 32566, 32581, 32596, 32610, 32624, 32637, 32649, 32661, 32672, 32682,
32692, 32702, 32710, 32718, 32726, 32732, 32739, 32744, 32749, 32753, 32757, 32760, 32763, 32765, 32766, 32766,
32766, 32766, 32765, 32763, 32760, 32757, 32753, 32749, 32744, 32739, 32732, 32726, 32718, 32710, 32702, 32692,
32682, 32672, 32661, 32649, 32637, 32624, 32610, 32596, 32581, 32566, 32550, 32533, 32516, 32498, 32480, 32461,
32441, 32421, 32400, 32379, 32357, 32334, 32311, 32287, 32262, 32237, 32211, 32185, 32158, 32131, 32103, 32074,
32045, 32015, 31985, 31954, 31922, 31890, 31857, 31824, 31790, 31755, 31720, 31685, 31648, 31612, 31574, 31536,
31498, 31459, 31419, 31379, 31338, 31296, 31254, 31212, 31169, 31125, 31081, 31036, 30991, 30945, 30899, 30852,
30804, 30756, 30708, 30659, 30609, 30559, 30508, 30457, 30405, 30353, 30300, 30247, 30193, 30138, 30083, 30028,
29972, 29916, 29859, 29801, 29743, 29685, 29626, 29566, 29506, 29446, 29385, 29323, 29261, 29199, 29136, 29073,
29009, 28944, 28879, 28814, 28748, 28682, 28615, 28548, 28481, 28412, 28344, 28275, 28206, 28136, 28065, 27995,
27923, 27852, 27780, 27707, 27634, 27561, 27487, 27413, 27338, 27263, 27188, 27112, 27036, 26959, 26882, 26805,
26727, 26648, 26570, 26491, 26411, 26332, 26252, 26171, 26090, 26009, 25927, 25845, 25763, 25680, 25597, 25514,
25430, 25346, 25262, 25177, 25092, 25007, 24921, 24835, 24748, 24662, 24575, 24487, 24400, 24312, 24224, 24135,
24046, 23957, 23868, 23778, 23688, 23598, 23508, 23417, 23326, 23235, 23143, 23051, 22959, 22867, 22775, 22682,
22589, 22495, 22402, 22308, 22214, 22120, 22026, 21931, 21837, 21742, 21646, 21551, 21455, 21360, 21264, 21167,
21071, 20975, 20878, 20781, 20684, 20587, 20490, 20392, 20294, 20197, 20099, 20001, 19902, 19804, 19706, 19607,
19508, 19409, 19311, 19211, 19112, 19013, 18914, 18814, 18715, 18615, 18515, 18415, 18316, 18216, 18116, 18015,
17915, 17815, 17715, 17615, 17514, 17414, 17313, 17213, 17112, 17012, 16911, 16811, 16710, 16609, 16509, 16408,
16308, 16207, 16106, 16006, 15905, 15805, 15704, 15603, 15503, 15402, 15302, 15202, 15101, 15001, 14901, 14801,
14700, 14600, 14500, 14401, 14301, 14201, 14101, 14002, 13902, 13803, 13703, 13604, 13505, 13406, 13307, 13208,
13110, 13011, 12913, 12814, 12716, 12618, 12520, 12423, 12325, 12228, 12130, 12033, 11936, 11840, 11743, 11647,
11550, 11454, 11359, 11263, 11167, 11072, 10977, 10882, 10787, 10693, 10599, 10505, 10411, 10317, 10224, 10131,
10038, 9945, 9853, 9761, 9669, 9577, 9486, 9395, 9304, 9213, 9123, 9033, 8943, 8853, 8764, 8675,
8586, 8498, 8410, 8322, 8235, 8148, 8061, 7974, 7888, 7802, 7717, 7631, 7547, 7462, 7378, 7294,
7210, 7127, 7044, 6962, 6880, 6798, 6716, 6635, 6555, 6474, 6394, 6315, 6236, 6157, 6078, 6000,
5923, 5845, 5769, 5692, 5616, 5540, 5465, 5390, 5316, 5242, 5168, 5095, 5023, 4950, 4878, 4807,
4736, 4665, 4595, 4526, 4456, 4388, 4319, 4252, 4184, 4117, 4051, 3985, 3919, 3854, 3789, 3725,
3662, 3599, 3536, 3474, 3412, 3351, 3290, 3230, 3170, 3111, 3052, 2994, 2936, 2879, 2822, 2766,
2710, 2655, 2600, 2546, 2493, 2439, 2387, 2335, 2283, 2232, 2182, 2132, 2083, 2034, 1986, 1938,
1891, 1844, 1798, 1752, 1707, 1663, 1619, 1576, 1533, 1491, 1449, 1408, 1367, 1327, 1288, 1249,
1211, 1173, 1136, 1099, 1063, 1028, 993, 959, 925, 892, 860, 828, 797, 766, 736, 706,
677, 649, 621, 594, 568, 542, 516, 491, 467, 444, 421, 398, 377, 355, 335, 315,
296, 277, 259, 241, 224, 208, 192, 177, 163, 149, 136, 123, 111, 100, 89, 79,
69, 60, 52, 44, 37, 30, 25, 19, 15, 11, 7, 4, 2, 1, 0, 0,
}; };
/* /*

View File

@ -53,12 +53,10 @@ static void FLL_clock(void) {
Divider = (uint16_t)(32LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT)); Divider = (uint16_t)(32LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
break; break;
} }
else else Divider = (uint16_t)(1LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
Divider = (uint16_t)(1LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
MCGOUTClock = (CPU_XTAL_CLK_HZ / Divider); MCGOUTClock = (CPU_XTAL_CLK_HZ / Divider);
} else } else MCGOUTClock = CPU_INT_SLOW_CLK_HZ;
MCGOUTClock = CPU_INT_SLOW_CLK_HZ;
/* /*
DMX32: DCO Maximum Frequency with 32.768 kHz Reference, bit 7 of MCG_C4 DMX32: DCO Maximum Frequency with 32.768 kHz Reference, bit 7 of MCG_C4
@ -117,8 +115,7 @@ static void inter_clock(void) {
FCRDIV: Fast Clock Internal Reference Divider, bits 1-3 of MCG_SC FCRDIV: Fast Clock Internal Reference Divider, bits 1-3 of MCG_SC
0b000 - 0b111: Divide Factor is 2^FCRDIV 0b000 - 0b111: Divide Factor is 2^FCRDIV
*/ */
if ((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U) if ((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U) MCGOUTClock = CPU_INT_SLOW_CLK_HZ;
MCGOUTClock = CPU_INT_SLOW_CLK_HZ;
else { else {
Divider = (uint16_t)(0x01LU << ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT)); Divider = (uint16_t)(0x01LU << ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT));
MCGOUTClock = (uint32_t)(CPU_INT_FAST_CLK_HZ / Divider); MCGOUTClock = (uint32_t)(CPU_INT_FAST_CLK_HZ / Divider);
@ -145,14 +142,10 @@ static void clock_init(void) {
value from 0b000 to 0b111, divide factor from 1 to 8, default 0b001 (2) value from 0b000 to 0b111, divide factor from 1 to 8, default 0b001 (2)
*/ */
if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x00U) { if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x00U) {
if ((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U) if ((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U) FLL_clock();
FLL_clock(); else PLL_clock();
else } else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x40U) inter_clock();
PLL_clock(); else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U) exter_clock();
} else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x40U)
inter_clock();
else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U)
exter_clock();
CORCLK = (MCGOUTClock / (0x01U + ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT))); CORCLK = (MCGOUTClock / (0x01U + ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT)));
CORCLK = (uint32_t)(CORCLK / 1000U) * 1000U; CORCLK = (uint32_t)(CORCLK / 1000U) * 1000U;