5
5
#include " Pins.h"
6
6
#include " Setup.h"
7
7
#include " TipThermoModel.h"
8
+ #include " USBPD.h"
8
9
#include " configuration.h"
9
10
#include " history.hpp"
10
11
#include " main.hpp"
@@ -17,7 +18,7 @@ const uint16_t powerPWM = 255;
17
18
static const uint8_t holdoffTicks = 14 ; // delay of 8 ms
18
19
static const uint8_t tempMeasureTicks = 14 ;
19
20
20
- uint16_t totalPWM; // htim2 .Init.Period, the full PWM cycle
21
+ uint16_t totalPWM; // htimADC .Init.Period, the full PWM cycle
21
22
22
23
static bool fastPWM;
23
24
static bool infastPWM;
@@ -99,20 +100,20 @@ uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
99
100
100
101
static void switchToFastPWM (void ) {
101
102
// 10Hz
102
- infastPWM = true ;
103
- totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
104
- htim2 .Instance ->ARR = totalPWM;
105
- htim2 .Instance ->CCR1 = powerPWM + holdoffTicks;
106
- htim2 .Instance ->PSC = 2690 ;
103
+ infastPWM = true ;
104
+ totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
105
+ htimADC .Instance ->ARR = totalPWM;
106
+ htimADC .Instance ->CCR1 = powerPWM + holdoffTicks;
107
+ htimADC .Instance ->PSC = 2690 ;
107
108
}
108
109
109
110
static void switchToSlowPWM (void ) {
110
111
// 5Hz
111
- infastPWM = false ;
112
- totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2 ;
113
- htim2 .Instance ->ARR = totalPWM;
114
- htim2 .Instance ->CCR1 = powerPWM + holdoffTicks / 2 ;
115
- htim2 .Instance ->PSC = 2690 * 2 ;
112
+ infastPWM = false ;
113
+ totalPWM = powerPWM + tempMeasureTicks / 2 + holdoffTicks / 2 ;
114
+ htimADC .Instance ->ARR = totalPWM;
115
+ htimADC .Instance ->CCR1 = powerPWM + holdoffTicks / 2 ;
116
+ htimADC .Instance ->PSC = 2690 * 2 ;
116
117
}
117
118
118
119
void setTipPWM (const uint8_t pulse, const bool shouldUseFastModePWM) {
@@ -126,19 +127,19 @@ void setTipPWM(const uint8_t pulse, const bool shouldUseFastModePWM) {
126
127
127
128
void HAL_TIM_PeriodElapsedCallback (TIM_HandleTypeDef *htim) {
128
129
// Period has elapsed
129
- if (htim->Instance == TIM2 ) {
130
+ if (htim->Instance == ADC_CONTROL_TIMER ) {
130
131
// we want to turn on the output again
131
132
PWMSafetyTimer--;
132
133
// We decrement this safety value so that lockups in the
133
134
// scheduler will not cause the PWM to become locked in an
134
135
// active driving state.
135
136
// While we could assume this could never happen, its a small price for
136
137
// increased safety
137
- htim2 .Instance ->CCR4 = pendingPWM;
138
- if (htim2 .Instance ->CCR4 && PWMSafetyTimer) {
139
- HAL_TIM_PWM_Start (&htim3 , TIM_CHANNEL_1);
138
+ htimADC .Instance ->CCR4 = pendingPWM;
139
+ if (htimADC .Instance ->CCR4 && PWMSafetyTimer) {
140
+ HAL_TIM_PWM_Start (&htimTip , TIM_CHANNEL_1);
140
141
} else {
141
- HAL_TIM_PWM_Stop (&htim3 , TIM_CHANNEL_1);
142
+ HAL_TIM_PWM_Stop (&htimTip , TIM_CHANNEL_1);
142
143
}
143
144
if (fastPWM != infastPWM) {
144
145
if (fastPWM) {
@@ -157,10 +158,11 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
157
158
void HAL_TIM_PWM_PulseFinishedCallback (TIM_HandleTypeDef *htim) {
158
159
// This was a when the PWM for the output has timed out
159
160
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4) {
160
- HAL_TIM_PWM_Stop (&htim3 , TIM_CHANNEL_1);
161
+ HAL_TIM_PWM_Stop (&htimTip , TIM_CHANNEL_1);
161
162
}
162
163
}
163
164
void unstick_I2C () {
165
+ #ifndef I2C_SOFT_BUS_1
164
166
GPIO_InitTypeDef GPIO_InitStruct;
165
167
int timeout = 100 ;
166
168
int timeout_cnt = 0 ;
@@ -227,6 +229,7 @@ void unstick_I2C() {
227
229
228
230
// Call initialization function.
229
231
HAL_I2C_Init (&hi2c1);
232
+ #endif
230
233
}
231
234
232
235
uint8_t getButtonA () { return HAL_GPIO_ReadPin (KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? 1 : 0 ; }
@@ -245,9 +248,36 @@ bool isTipDisconnected() {
245
248
return tipTemp > tipDisconnectedThres;
246
249
}
247
250
248
- void setStatusLED (const enum StatusLED state) {}
249
- void setBuzzer (bool on) {}
250
- uint8_t preStartChecks () { return 1 ; }
251
+ void setStatusLED (const enum StatusLED state) {}
252
+ void setBuzzer (bool on) {}
253
+ uint8_t preStartChecks () {
254
+ #ifdef HAS_SPLIT_POWER_PATH
255
+
256
+ // We want to enable the power path that has the highest voltage
257
+ // Nominally one will be ~=0 and one will be high. Unless you jamb both in, then both _may_ be high, or device may be dead
258
+ {
259
+ uint16_t dc = getRawDCVin ();
260
+ uint16_t pd = getRawPDVin ();
261
+ if (dc > pd) {
262
+ HAL_GPIO_WritePin (DC_SELECT_GPIO_Port, DC_SELECT_Pin, GPIO_PIN_SET);
263
+ HAL_GPIO_WritePin (PD_SELECT_GPIO_Port, PD_SELECT_Pin, GPIO_PIN_RESET);
264
+ } else {
265
+ HAL_GPIO_WritePin (PD_SELECT_GPIO_Port, PD_SELECT_Pin, GPIO_PIN_SET);
266
+ HAL_GPIO_WritePin (DC_SELECT_GPIO_Port, DC_SELECT_Pin, GPIO_PIN_RESET);
267
+ }
268
+ }
269
+
270
+ #endif
271
+ #ifdef POW_PD
272
+ // If we are in the middle of negotiating PD, wait until timeout
273
+ // Before turning on the heater
274
+ if (!USBPowerDelivery::negotiationComplete ()) {
275
+ return 0 ;
276
+ }
277
+
278
+ #endif
279
+ return 1 ;
280
+ }
251
281
uint64_t getDeviceID () {
252
282
//
253
283
return HAL_GetUIDw0 () | ((uint64_t )HAL_GetUIDw1 () << 32 );
0 commit comments