forked from stancecoke/BMSBattery_S_controllers_firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adc.c
executable file
·165 lines (134 loc) · 4.6 KB
/
adc.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
/*
* EGG OpenSource EBike firmware
*
* Copyright (C) Casainho, 2015, 2106, 2017.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include <stdio.h>
#include "stm8s.h"
#include "gpio.h"
#include "stm8s_adc1.h"
#include "adc.h"
//#include "update_setpoint.h" // FIXME, not needed any more?
#include "timers.h"
#include "ACAcontrollerState.h"
void adc_init(void) {
uint8_t ui8_i;
//init GPIO for the used ADC pins
GPIO_Init(GPIOB,
(THROTTLE__PIN || CURRENT_PHASE_B__PIN || CURRENT_MOTOR_TOTAL__PIN || REGEN_THROTTLE__PIN),
GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOE,
(CURRENT_MOTOR_TOTAL_FILTERED__PIN),
GPIO_MODE_IN_FL_NO_IT);
//de-Init ADC peripheral
ADC1_DeInit();
//init ADC1 peripheral
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,
ADC1_CHANNEL_9,
ADC1_PRESSEL_FCPU_D2,
ADC1_EXTTRIG_TIM,
DISABLE,
ADC1_ALIGN_LEFT,
(ADC1_SCHMITTTRIG_CHANNEL4 || ADC1_SCHMITTTRIG_CHANNEL5 || ADC1_SCHMITTTRIG_CHANNEL6 || ADC1_SCHMITTTRIG_CHANNEL7 || ADC1_SCHMITTTRIG_CHANNEL8),
DISABLE);
ADC1_ScanModeCmd(ENABLE);
ADC1_Cmd(ENABLE);
//********************************************************************************
// next code is for "calibrating" the offset value of ADC motor total current
// read and discard few samples of ADC, to make sure the next samples are ok
for (ui8_i = 0; ui8_i < 8; ui8_i++) {
//ui16_counter = TIM2_GetCounter () + 1000;
// while (TIM2_GetCounter () < ui16_counter) ; // delay
delay_halfms(200);
adc_trigger();
delay_halfms(200);
ui16_current_cal_b = ui16_adc_read_motor_total_current();
ui16_x4_cal_b = ui16_adc_read_x4_value();
ui16_throttle_cal_b = ui8_adc_read_throttle();
}
// is there a deeper meaning behind assigning the value 8 times above?
// compiler optimization?
// i don't dare to cleanup this code until I'm sure its just bad style :)
ui16_current_cal_b = 0;
ui16_x4_cal_b = 0;
ui16_throttle_cal_b = 0;
// read and average a few values of ADC
for (ui8_i = 0; ui8_i < 16; ui8_i++) {
delay_halfms(30);
adc_trigger();
delay_halfms(30);
ui16_current_cal_b += ui16_adc_read_motor_total_current();
ui16_x4_cal_b += ui16_adc_read_x4_value();
ui16_throttle_cal_b += ui8_adc_read_throttle();
}
ui16_current_cal_b >>= 4;
ui16_current_cal_b -= 1;
ui16_x4_cal_b >>= 4;
ui16_x4_cal_b -= 1;
ui16_throttle_cal_b >>= 4;
ui16_throttle_cal_b -= 1;
#ifdef DIAGNOSTICS
printf("ui16_current_cal_b = %d\r\n", ui16_current_cal_b);
#endif
//ui8_motor_total_current_offset = ui16_motor_total_current_offset_10b >> 2;
}
inline void adc_trigger(void) //inline ?!
{
// start ADC all channels, scan conversion (buffered)
ADC1->CSR &= 0x09; // clear EOC flag first (selected also channel 9)
ADC1_StartConversion();
}
uint8_t ui8_adc_read_phase_B_current(void) {
// /* Read LSB first */
// templ = *(uint8_t*)(uint16_t)((uint16_t)ADC1_BaseAddress + (uint8_t)(Buffer << 1) + 1);
// /* Then read MSB */
// temph = *(uint8_t*)(uint16_t)((uint16_t)ADC1_BaseAddress + (uint8_t)(Buffer << 1));
//#define ADC1_BaseAddress 0x53E0
//phase_B_current --> ADC_AIN5
// 0x53E0 + 2*5 = 0x53EA
return *(uint8_t*) (0x53EA);
}
uint16_t ui16_adc_read_phase_B_current(void) {
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*) (0x53EB);
temph = *(uint8_t*) (0x53EA);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint8_t ui8_adc_read_throttle(void) {
// 0x53E0 + 2*4 = 0x53E8
// return *(uint8_t*)(0x53E8);
return *(uint8_t*) (0x53E8);
}
uint16_t ui16_adc_read_x4_value(void) {
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*) (0x53EF);
temph = *(uint8_t*) (0x53EE);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint8_t ui8_adc_read_motor_total_current(void) {
// 0x53E0 + 2*8 = 0x53F0
return *(uint8_t*) (0x53F0);
}
uint16_t ui16_adc_read_motor_total_current(void) {
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*) (0x53F1);
temph = *(uint8_t*) (0x53F0);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}
uint8_t ui8_adc_read_battery_voltage(void) {
// 0x53E0 + 2*9 = 0x53F2
return *(uint8_t*) (0x53F2);
}
uint16_t ui16_adc_read_battery_voltage(void) {
uint16_t temph;
uint8_t templ;
templ = *(uint8_t*) (0x53F3);
temph = *(uint8_t*) (0x53F2);
return ((uint16_t) temph) << 2 | ((uint16_t) templ);
}