-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
213 lines (180 loc) · 4.35 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
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
/*
* Licensed under the GNU General Public License version 2 with exceptions. See
* LICENSE file in the project root for full license information
*/
#include "ecat_slv.h"
#include "esc_foe.h"
#include "utypes.h"
#include "xmc_gpio.h"
#include <string.h>
#include <stdio.h>
#ifdef XMC4800_F144x2048
#define P_LED P5_8
#define P_BTN P15_12
#endif
#ifdef XMC4300_F100x256
#define A_LED P4_0
#define P_LED P4_1
#define P_BTN P3_4
#endif
#define FLASH_WRITE_BLOCK_SIZE 8192
#define DUMMY_FLASH_SIZE FLASH_WRITE_BLOCK_SIZE
extern void ESC_eep_handler(void);
void foe_init(void);
uint32_t foe_write_to_test_bin(foe_file_cfg_t * self, uint8_t * data, size_t length);
/* Application variables */
uint32_t volatile msTicks;
_Rbuffer Rb;
_Wbuffer Wb;
_Cbuffer Cb;
uint8_t * rxpdo = (uint8_t *)&Wb.LED;
uint8_t * txpdo = (uint8_t *)&Rb.button;
uint32_t encoder_scale;
uint32_t encoder_scale_mirror;
bool throw_emergency = false;
static const uint32_t test_bin_dummy_flash = 0;
static const XMC_GPIO_CONFIG_t gpio_config_btn = {
.mode = XMC_GPIO_MODE_INPUT_INVERTED_PULL_UP,
.output_level = 0,
.output_strength = 0
};
static const XMC_GPIO_CONFIG_t gpio_config_led = {
.mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL,
.output_level = XMC_GPIO_OUTPUT_LEVEL_LOW,
.output_strength = XMC_GPIO_OUTPUT_STRENGTH_STRONG_SOFT_EDGE
};
void cb_get_inputs (void)
{
Rb.button = XMC_GPIO_GetInput(P_BTN);
Cb.reset_counter++;
Rb.encoder = ESCvar.Time;
}
void cb_set_outputs (void)
{
if (Wb.LED)
{
XMC_GPIO_SetOutputHigh(P_LED);
}
else
{
XMC_GPIO_SetOutputLow(P_LED);
}
}
uint32_t post_object_download_hook (uint16_t index, uint8_t subindex,
uint16_t flags)
{
switch(index)
{
case 0x7100:
{
switch (subindex)
{
case 0x01:
{
encoder_scale_mirror = encoder_scale;
break;
}
}
break;
}
case 0x8001:
{
switch (subindex)
{
case 0x01:
{
Cb.reset_counter = 0;
throw_emergency = true;
break;
}
}
break;
}
}
return 0;
}
void SysTick_Handler (void) {
msTicks++;
}
void soes (void * arg)
{
SysTick_Config(SystemCoreClock/1000);
/* Setup config hooks */
static esc_cfg_t config =
{
.user_arg = NULL,
.use_interrupt = 0,
.watchdog_cnt = 50000,
.set_defaults_hook = NULL,
.pre_state_change_hook = NULL,
.post_state_change_hook = NULL,
.application_hook = NULL,
.safeoutput_override = NULL,
.pre_object_download_hook = NULL,
.post_object_download_hook = post_object_download_hook,
.rxpdo_override = NULL,
.txpdo_override = NULL,
.esc_hw_interrupt_enable = NULL,
.esc_hw_interrupt_disable = NULL,
.esc_hw_eep_handler = ESC_eep_handler
};
DPRINT ("SOES (Simple Open EtherCAT Slave)\n");
// configure I/O
XMC_GPIO_Init(P_BTN, &gpio_config_btn);
XMC_GPIO_Init(A_LED, &gpio_config_led);
XMC_GPIO_Init(P_LED, &gpio_config_led);
ecat_slv_init(&config);
foe_init();
while (1)
{
ecat_slv();
if (throw_emergency)
{
uint8_t data[5] = {0xAA, 0x50, 0x51, 0x52, 0x53};
if (!COE_throwEmergency(0xFFFE, 0x00, data))
{
throw_emergency = false;
}
}
if ((msTicks & 1023) < 100)
{
XMC_GPIO_SetOutputHigh(A_LED);
}
else
{
XMC_GPIO_SetOutputLow(A_LED);
}
}
}
int main (void)
{
soes (NULL);
return 0;
}
void foe_init(void)
{
static foe_file_cfg_t files[] =
{
{
.name = "test.bin",
.max_data = DUMMY_FLASH_SIZE,
.dest_start_address = (uint32_t)test_bin_dummy_flash,
.address_offset = 0,
.filepass = 0,
.write_function = foe_write_to_test_bin /* NULL if not used */
}
};
static uint8_t fbuf[FLASH_WRITE_BLOCK_SIZE];
static foe_cfg_t config =
{
.buffer_size = FLASH_WRITE_BLOCK_SIZE, /* Buffer size before we flush to destination */
.fbuffer = (uint8_t *)&fbuf,
.n_files = 1,
.files = files
};
FOE_config(&config);
}
uint32_t foe_write_to_test_bin(foe_file_cfg_t * self, uint8_t * data, size_t length)
{
return 0;
}