-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuart_task.c
295 lines (276 loc) · 8.03 KB
/
uart_task.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* uart_task.c
*
* Created: 11.01.2017 22:02:14
* Author: Hubert
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/eeprom.h>
#include "CAN-MFA.h"
#include "uart.h"
#include "dog_display.h"
extern uint8_t EEMEM cal_voltage; // 171
extern uint8_t EEMEM cal_speed; // 169
extern uint8_t EEMEM cal_oil_temperature;
extern uint8_t EEMEM cal_manifold;
extern uint8_t EEMEM cal_consumption;
extern uint8_t EEMEM cal_gearbox_temperature;
extern uint8_t EEMEM cal_ambient_temperature;
extern uint8_t EEMEM cal_k15_delay;
extern uint8_t EEMEM cal_k58b_off_val;
extern uint8_t EEMEM cal_k58b_on_val;
extern uint8_t EEMEM cal_can_mode;
extern uint8_t EEMEM cal_startstop_enabled;
extern uint16_t t3cnt;
void uart_bootloader_init(uint32_t baudrate){
uart1_init( UART_BAUD_SELECT(baudrate,F_CPU) );
sei();
uart1_puts_P("\n\rHier ist die CAN MFA...!\n\r");
uart1_puts("\n\rOptionen: ");
uart1_puts("\n\rb: Springe zum Bootloader");
uart1_puts("\n\rc: Calibrierung");
}
//*
int uart_get_int(void){
char val[4] = {0,};
char exit_now = 0;
uint8_t loopcnt = 0;
int c = ' ';
do{
c = uart1_getc();
if(!(c & UART_NO_DATA))
{
if((unsigned char) c >= '0' && (unsigned char) c <= '9'){
val[loopcnt++] = (unsigned char) c;
uart1_putc((unsigned char) c);
}else if(c == 13){
exit_now = 1;
uart1_putc('\n');
uart1_putc('\r');
}
}
}while(!exit_now);
return atoi((const char*) val);
}
//*/
void uart_print_cal_menu(void){
uart1_puts("\n\rCalibrierung: Waehle die Variable ");
uart1_puts("\n\rv\tcal_voltage ");
uart1_puts("\n\rs\tcal_speed ");
uart1_puts("\n\ra\tcal_ambient_temperature");
uart1_puts("\n\rp\tcal_manifold_pressure");
uart1_puts("\n\rg\tcal_gearbox_temperature");
uart1_puts("\n\ro\tcal_oil_temperature ");
uart1_puts("\n\rc\tcal_consumption ");
uart1_puts("\n\rb\tcal_k58b ");
uart1_puts("\n\rd\tcal_k15_delay ");
uart1_puts("\n\rm\tcal_can_mode ");
uart1_puts("\n\rS\tcal_startstop ");
uart1_puts("\n\ri\tcal_i2c ");
uart1_puts("\n\rE\tcal_engine_type ");
uart1_puts("\n\re\tEnde ");
}
void uart_calibrate(void){
void (*reset)( void ) = 0x0000;
uint16_t timeout = t3cnt;
unsigned int c;
uart_print_cal_menu();
do{
uint16_t diff = (timeout>t3cnt)?timeout-t3cnt:t3cnt-timeout;
if(diff>300) return;
c = uart1_getc();
if(!(c & UART_NO_DATA))
{
diff = 0;
switch( (unsigned char)c)
{
case 'a':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_ambient_temperature: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_ambient_temperature));
uart1_puts(val);
eeprom_write_byte(&cal_ambient_temperature, uart_get_int());
uart_print_cal_menu();
break;
}
case 'm':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_can_mode: (0: CAN | 1: NO_CAN ) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_can_mode));
uart1_puts(val);
eeprom_write_byte(&cal_can_mode, uart_get_int());
uart_print_cal_menu();
break;
}
case 'i':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_i2c: (1: I2C | 0: NO_I2C ) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_i2c_mode));
uart1_puts(val);
eeprom_write_byte(&cal_i2c_mode, uart_get_int());
uart_print_cal_menu();
break;
}
case 'S':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_startstop: (0: Aus | 1: Ein ) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_startstop_enabled));
uart1_puts(val);
eeprom_write_byte(&cal_startstop_enabled, uart_get_int());
uart_print_cal_menu();
break;
}
case 'g':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_gearbox_temperature: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_gearbox_temperature));
uart1_puts(val);
eeprom_write_byte(&cal_gearbox_temperature, uart_get_int());
uart_print_cal_menu();
break;
}
case 'p':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_manifold: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_manifold));
uart1_puts(val);
eeprom_write_byte(&cal_manifold, uart_get_int());
uart_print_cal_menu();
break;
}
case 'v':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_voltage1: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_voltage1));
uart1_puts(val);
eeprom_write_byte(&cal_voltage1, uart_get_int());
uart1_puts("\n\rWert cal_voltage2: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_voltage2));
uart1_puts(val);
eeprom_write_byte(&cal_voltage2, uart_get_int());
uart1_puts("\n\rWert cal_voltage3: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_voltage3));
uart1_puts(val);
eeprom_write_byte(&cal_voltage3, uart_get_int());
uart1_puts("\n\rWert cal_voltage4: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_voltage4));
uart1_puts(val);
eeprom_write_byte(&cal_voltage4, uart_get_int());
uart_print_cal_menu();
break;
}
case 's':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_speed: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_speed));
uart1_puts(val);
eeprom_write_byte(&cal_speed, uart_get_int());
uart_print_cal_menu();
break;
}
case 'o':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_oil_temperature: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_oil_temperature));
uart1_puts(val);
eeprom_write_byte(&cal_oil_temperature, uart_get_int());
uart_print_cal_menu();
break;
}
case 'c':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_consumption: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_consumption));
uart1_puts(val);
eeprom_write_byte(&cal_consumption, uart_get_int());
uart_print_cal_menu();
break;
}
case 'b':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_k58b_on_val: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_k58b_on_val));
uart1_puts(val);
eeprom_write_byte(&cal_k58b_on_val, uart_get_int());
uart1_puts("\n\rWert cal_k58b_off_val: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_k58b_off_val));
uart1_puts(val);
eeprom_write_byte(&cal_k58b_off_val, uart_get_int());
uart_print_cal_menu();
break;
}
case 'd':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_k15_delay: (0-255) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_k15_delay));
uart1_puts(val);
eeprom_write_byte(&cal_k15_delay, uart_get_int());
uart_print_cal_menu();
break;
}
case 'E':{
char val[5] = {0,};
uart1_puts("\n\rWert cal_engine_type:\n\r\t0 = PETROL,\n\r\t1 = TDI_CAN,\n\r\t2 = TDI_NOCAN) ");
sprintf(val, "%i\n\r", eeprom_read_byte(&cal_engine_type));
uart1_puts(val);
eeprom_write_byte(&cal_engine_type, uart_get_int());
uart_print_cal_menu();
break;
}
//*
case 'e':{
reset();
break;
}
//*/
default:{
break;
}
}
}
_delay_ms(200);
if(read_mfa_switch(MFA_SWITCH_RES)){
reset();
}
}while( (unsigned char) c != 13);
return;
}
void uart_bootloader_task(void){
unsigned int c;
void (*bootloader)( void ) = 0xF000; // Achtung Falle: Hier Word-Adresse
enable_mfa_switch();
c = uart1_getc();
if(!(c & UART_NO_DATA))
{
switch( (unsigned char)c)
{
case 'b':{
dog_clear_lcd(); // 0123456789012345
dog_write_mid_string(NEW_POSITION(4,4), " *BOOTLOADER* ");
uart1_puts("\n\rSpringe zum Bootloader...\n\r");
LED_PORT |= (1<<LED);
LED_DDR |= (1<<LED);
_delay_ms(1000);
bootloader();
break;
}
case 'c':{
dog_clear_lcd(); // 0123456789012345
dog_write_mid_string(NEW_POSITION(4,0), " *CALIBRATION* ");
uart1_puts("\n\rCalibrierung...");
uart_calibrate();
break;
}
default:{
uart1_puts("\n\rCAN-MFA USB tool Optionen: ");
uart1_puts("\n\rb: Springe zum Bootloader");
uart1_puts("\n\rc: Calibrierung");
}
}
}
disable_mfa_switch();
}