/
main.c
154 lines (139 loc) · 3.39 KB
/
main.c
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
143
144
145
146
147
148
149
150
151
152
153
154
#include "user/expander_board.h"
#include "user/display.h"
#include "user/defines.h"
#include "user/analog.h"
#include "user/clocks.h"
#include "user/debug.h"
#include "user/main.h"
// Do we skip data analysis after updating display?
//#define SKIP_ADC_DATA_AFTER_DISPLAY
// After how many FFTs do we update display
#define FFT_CNT_DISP 10
// Define to enable current transient test
//#define CUR_TRANSIENT_TEST
/*! \fn main_user(void)
* \brief User main
*/
void main_user(void)
{
BOOL output_debug_enabled = FALSE;
BOOL sd_card_initialized = FALSE;
BOOL remove_low_freqs = FALSE;
uint16_t last_fft_return = 0;
uint16_t fft_nb_counter = 0;
uint16_t idle_anim_st = 0;
#ifdef CUR_TRANSIENT_TEST
uint16_t temp_counter = 0;
#endif
/* Clock init */
clocks_init();
/* Debug init */
debug_init();
/* Say hello! */
debug_printf("CDM324 fw v%d.%d, compiled %s %s\r\n", FW_MAJOR, FW_MINOR, __DATE__, __TIME__);
/* Display init */
if (display_init() == FALSE)
{
debug_print_string("Display not found!\r\n");
while(1);
}
/* Analog input init */
analog_init();
/* IO expander init */
if (expander_init() != FALSE)
{
sd_card_initialized = TRUE;
}
/* Trigger analog conversions */
analog_trigger_conversion();
while (1)
{
/* Sequence of ADC measurements complete? */
if(analog_get_and_clear_adc_measurement_done() != FALSE)
{
/* Every few ms we display the speed and depending on define not do anything with the data has the current draw is enough to have an impact on the +5V PSU */
if (fft_nb_counter++ == FFT_CNT_DISP)
{
/* Reset counter */
fft_nb_counter = 0;
}
#ifdef SKIP_ADC_DATA_AFTER_DISPLAY
else
#endif
{
/* Should we display the result? */
if (fft_nb_counter == FFT_CNT_DISP)
{
#ifndef CUR_TRANSIENT_TEST
/* Valid return? */
if (last_fft_return == 0)
{
/* Idle animation */
display_animation_step(idle_anim_st++);
if (idle_anim_st == 12)
{
idle_anim_st = 0;
}
}
else
{
/* Convert to mph or kph depending on user selection */
if (expander_is_kph_selected() != FALSE)
{
display_speed((uint16_t)(last_fft_return * 0.2262295));
}
else
{
display_speed((uint16_t)(last_fft_return * 0.1449275));
}
}
#else
/* Code to test current transient */
if ((temp_counter++ & 0x0001) == 0)
display_text("", FALSE);
else
display_speed(8888);
#endif
}
/* Debug ADC output buffer ? */
if(output_debug_enabled != FALSE)
{
analog_output_conversion_buffer_to_uart();
}
/* Compute FFT */
last_fft_return = analog_compute_fft_on_cplted_sequence(remove_low_freqs);
/* Debug FFT output buffer ? */
if(output_debug_enabled != FALSE)
{
analog_output_current_fft_to_uart(150);
}
/* Commands from UART */
char uart_input = debug_get_char_from_uart();
if(uart_input == 'a')
{
output_debug_enabled = TRUE;
}
else if(uart_input == 's')
{
output_debug_enabled = FALSE;
}
else if(uart_input == 'h')
{
remove_low_freqs = TRUE;
}
else if(uart_input == 'l')
{
remove_low_freqs = FALSE;
}
else if(uart_input == 'k')
{
debug_printf("%d\r\n", (uint16_t)(last_fft_return * 0.2262295));
}
else if(uart_input == 'm')
{
debug_printf("%d\r\n", (uint16_t)(last_fft_return * 0.1449275));
}
}
}
}
}