forked from vannheath/TMC-EvalSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
218 lines (177 loc) · 7.41 KB
/
main.c
File metadata and controls
218 lines (177 loc) · 7.41 KB
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
/*******************************************************************************
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
* (now owned by Analog Devices, Inc.),
*
* Copyright © 2023 Analog Devices, Inc.
*******************************************************************************/
#include "ProjectConfig.h"
#include "boards/Board.h"
#include "hal/derivative.h"
#include "hal/HAL.h"
#include "tmc/IdDetection.h"
#include "tmc/StepDir.h"
#include "tmc/TMCL.h"
#include "tmc/VitalSignsMonitor.h"
#include "tmc/BoardAssignment.h"
#include "tmc/RAMDebug.h"
// Module ID and Version of the firmware shown in the TMCL-IDE
const char VersionString[8] = {
// 3-digit module ID
'0' + (MODULE_ID / 1000) % 10,
'0' + (MODULE_ID / 100) % 10,
'0' + (MODULE_ID / 10) % 10,
'0' + (MODULE_ID / 1) % 10,
'V',
// 1-digit major firmware version
'0' + (VERSION_MAJOR) % 10,
// 2-digit minor firmware version
'0' + (VERSION_MINOR / 10) % 10,
'0' + (VERSION_MINOR / 1) % 10,
};
EvalboardsTypeDef Evalboards;
// Forward declaration
void enterBootloader();
/* Keep as is! This lines are important for the update functionality. */
#if defined(Landungsbruecke) || defined(LandungsbrueckeSmall)
const uint8_t Protection[] __attribute__ ((section(".cfmconfig")))=
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //Backdoor key
0xFF, 0xFF, 0xFF, 0xFF, //Flash protection (FPPROT)
0x7E, //Flash security (FSEC) => nach Image-Generierung manuell auf 0x40 setzen im Image
0xF9, //Flash option (FOPT) (NMI ausgeschaltet, EzPort ausgeschaltet, Normal power)
0xFF, //reserved
0xFF //reserved
};
#endif
#if defined(Landungsbruecke) || defined(LandungsbrueckeSmall) || defined(LandungsbrueckeV3)
// This struct gets placed at a specific address by the linker
struct BootloaderConfig __attribute__ ((section(".bldata"))) BLConfig;
#endif
/* Check if jumping into bootloader is forced */
/* */
/* In order to jump to bootloader e.g. because of an accidental infinite loop */
/* in a modified firmware you may short ID_CLK and ID_CH0 pins on start up. */
/* This will force the entrance into bootloader mode and allow to replace bad firmware. */
void shallForceBoot()
{
// toggle each pin and see if you can read the state on the other
// leave if not, because this means that the pins are not tied together
HAL.IOs->config->toOutput(&HAL.IOs->pins->ID_CLK);
HAL.IOs->config->toInput(&HAL.IOs->pins->ID_CH0);
HAL.IOs->config->setHigh(&HAL.IOs->pins->ID_CLK);
if(!HAL.IOs->config->isHigh(&HAL.IOs->pins->ID_CH0))
return;
HAL.IOs->config->setLow(&HAL.IOs->pins->ID_CLK);
if(HAL.IOs->config->isHigh(&HAL.IOs->pins->ID_CH0))
return;
HAL.IOs->config->toOutput(&HAL.IOs->pins->ID_CH0);
HAL.IOs->config->toInput(&HAL.IOs->pins->ID_CLK);
HAL.IOs->config->setHigh(&HAL.IOs->pins->ID_CH0);
if(!HAL.IOs->config->isHigh(&HAL.IOs->pins->ID_CLK))
return;
HAL.IOs->config->setLow(&HAL.IOs->pins->ID_CH0);
if(HAL.IOs->config->isHigh(&HAL.IOs->pins->ID_CLK))
return;
// not returned, this means pins are tied together
enterBootloader();
}
/* Call all standard initialization routines. */
static void init()
{
HAL.init(); // Initialize Hardware Abstraction Layer
tmcdriver_init(); // Initialize dummy driver board --> preset EvalBoards.ch2
tmcmotioncontroller_init(); // Initialize dummy motion controller board --> preset EvalBoards.ch1
#if defined(LandungsbrueckeV3)
// Check for the button-based return-to-bootloader request
if (HAL.IOs->config->isHigh(&HAL.IOs->pins->BUTTON))
{
enterBootloader();
}
#endif
IDDetection_init(); // Initialize board detection
tmcl_init(); // Initialize TMCL communication
VitalSignsMonitor.busy = 1; // Put state to busy
Evalboards.driverEnable = DRIVER_ENABLE;
Evalboards.ch1.id = 0; // preset id for driver board to 0 --> error/not found
Evalboards.ch2.id = 0; // preset id for driver board to 0 --> error/not found
// We disable the drivers before configurating anything
HAL.IOs->config->toOutput(&HAL.IOs->pins->DIO0);
HAL.IOs->config->setHigh(&HAL.IOs->pins->DIO0);
IdAssignmentTypeDef ids = { 0 };
IDDetection_initialScan(&ids); // start initial board detection
IDDetection_initialScan(&ids); // start second time, first time not 100% reliable, not sure why - too fast after startup?
if(!ids.ch1.id && !ids.ch2.id)
{
shallForceBoot(); // only checking to force jump into bootloader if there are no boards attached
// todo CHECK 2: Workaround: shallForceBoot() changes pin settings - change them again here, since otherwise IDDetection partially breaks (LH)
HAL.IOs->config->toOutput(&HAL.IOs->pins->ID_CLK);
HAL.IOs->config->toInput(&HAL.IOs->pins->ID_CH0);
}
if (ID_CH1_DEFAULT && (!ids.ch1.id || ID_CH1_OVERRIDE))
{
ids.ch1.id = ID_CH1_DEFAULT;
ids.ch1.state = ID_STATE_DONE;
}
if (ID_CH2_DEFAULT && (!ids.ch2.id || ID_CH2_OVERRIDE))
{
ids.ch2.id = ID_CH2_DEFAULT;
ids.ch2.state = ID_STATE_DONE;
}
Board_assign(&ids); // assign boards with detected id
VitalSignsMonitor.busy = 0; // not busy any more!
}
/* main function */
int main(void)
{
// Start all initialization routines
init();
// Main loop
while(1)
{
// Check all parameters and life signs and mark errors
vitalsignsmonitor_checkVitalSigns();
// handle RAMDebug
debug_process();
// Perodic jobs of Motion controller/Driver boards
Evalboards.ch1.periodicJob(systick_getTick());
Evalboards.ch2.periodicJob(systick_getTick());
// Process TMCL communication
tmcl_process();
}
return 0;
}
void enterBootloader()
{
#if defined(Landungsbruecke) || defined(LandungsbrueckeSmall) || defined(LandungsbrueckeV3)
if(Evalboards.ch1.id == ID_TMC4671)
{
// Driver Enable has to be set low by the bootloader for these ICs
BLConfig.drvEnableResetValue = 0;
}
else
{
// Default: Driver Enable is set to high
BLConfig.drvEnableResetValue = 1;
}
#endif
Evalboards.driverEnable = DRIVER_DISABLE;
Evalboards.ch1.enableDriver(DRIVER_DISABLE); // todo CHECK 2: the ch1/2 deInit() calls should already disable the drivers - keep this driver disabling to be sure or remove it and leave the disabling to deInit? (LH)
Evalboards.ch2.enableDriver(DRIVER_DISABLE);
Evalboards.ch1.deInit();
Evalboards.ch2.deInit();
HAL.USB->deInit();
wait(500);
HAL.Timer->deInit();
HAL.RS232->deInit();
HAL.WLAN->deInit();
HAL.ADCs->deInit();
// todo: CHECK 2: Muss api_deInit hier dazu? (ED)
StepDir_deInit();
IDDetection_deInit();
HAL.NVIC_DeInit();
#if defined(Landungsbruecke) || defined(LandungsbrueckeSmall) || defined(LandungsbrueckeV3)
bool isBLNew = (BLConfig.BLMagic == BL_MAGIC_VALUE_BL_NEW);
BLConfig.BLMagic = isBLNew ? BL_MAGIC_VALUE_APP_NEW : BL_MAGIC_VALUE_OLD;
HAL.reset(true);
#endif
}