forked from TonyKerman/stm32_workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_preProcess.py
executable file
·235 lines (184 loc) · 4.8 KB
/
C_preProcess.py
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
retarget_c='''
#include <_ansi.h>
#include <_syslist.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/times.h>
#include "retarget.h"
#include <stdint.h>
#if !defined(OS_USE_SEMIHOSTING)
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
UART_HandleTypeDef *gHuart;
void RetargetInit(UART_HandleTypeDef *huart)
{
gHuart = huart;
/* Disable I/O buffering for STDOUT stream, so that
* chars are sent out as soon as they are printed. */
setvbuf(stdout, NULL, _IONBF, 0);
}
int _isatty(int fd)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 1;
errno = EBADF;
return 0;
}
int _write(int fd, char *ptr, int len)
{
HAL_StatusTypeDef hstatus;
if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return len;
else
return EIO;
}
errno = EBADF;
return -1;
}
int _close(int fd)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 0;
errno = EBADF;
return -1;
}
int _lseek(int fd, int ptr, int dir)
{
(void) fd;
(void) ptr;
(void) dir;
errno = EBADF;
return -1;
}
int _read(int fd, char *ptr, int len)
{
HAL_StatusTypeDef hstatus;
if (fd == STDIN_FILENO)
{
hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY);
if (hstatus == HAL_OK)
return 1;
else
return EIO;
}
errno = EBADF;
return -1;
}
int _fstat(int fd, struct stat *st)
{
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
{
st->st_mode = S_IFCHR;
return 0;
}
errno = EBADF;
return 0;
}
#endif //#if !defined(OS_USE_SEMIHOSTING)'''
retarget_h = '''
#ifndef __RETARGET_H
#define __RETARGET_H
#include "stm32f1xx_hal.h"
#include <sys/stat.h>
#include <stdio.h>
void RetargetInit(UART_HandleTypeDef *huart);
int _isatty(int fd);
int _write(int fd, char *ptr, int len);
int _close(int fd);
int _lseek(int fd, int ptr, int dir);
int _read(int fd, char *ptr, int len);
int _fstat(int fd, struct stat *st);
#endif //FRESHMANPROJECT_RETARGET_H
'''
usrmain_c ='''
#include "main.h"
#include "USER_main.h"
#include "retarget.h"
void USER_main(void)
{
printf("hello world\\n");
while (1)
{
}
}
'''
usrmain_h = '''
#ifndef __USER_MAIN_H
#define __USER_MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
void USER_main(void);
#ifdef __cplusplus
}
#endif
#endif
'''
text = []
print("change main.c")
with open("./Core/Src/main.c","r") as f:
for line in f.readlines():
if '#include"retarget.h"' in line \
or 'RetargetInit(&huart1);' in line\
or 'HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);' in line\
or 'HAL_Delay(300);' in line\
or 'USER_main();' in line\
or '#include"USER_main.h"' in line :
continue
text.append(line)
if "/* USER CODE BEGIN Includes */" in line:
text.append('#include"retarget.h"\n')
text.append('#include"USER_main.h"\n')
if "/* USER CODE BEGIN 2 */" in line:
text.append(' RetargetInit(&huart1);\n')
text.append(' HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);\nHAL_Delay(300);\n'*3)
text.append(' USER_main();\n')
with open("./Core/Src/main.c","w+") as f:
f.writelines(text)
try:
print("add sretarget.c")
with open("./Core/Src/retarget.c","x") as f:
f.write(retarget_c)
print("add retarget.h")
with open("./Core/Inc/retarget.h","x") as f:
f.write(retarget_h)
except FileExistsError as e:
print("error!! file is existed")
try:
print("add USER_main.c")
with open("./Core/Src/USER_main.c","x") as f:
f.write(usrmain_c)
print("add USER_main.h")
with open("./Core/Inc/USER_main.h","x") as f:
f.write(usrmain_h)
except FileExistsError as e:
print("error!! file is existed")
delet_flag=0;
text=[]
print("change syscalls.c")
with open("./Core/Src/syscalls.c","r") as f:
for line in f.readlines():
if 'int _close(int file)' in line:
delet_flag=1
if 'int _wait(int *status)'in line:
delet_flag=0
if delet_flag==1:
continue
text.append(line)
with open("./Core/Src/syscalls.c","w+") as f:
f.writelines(text)
text=[]
print("change CmakeList.txt")
with open("./CMakeLists.txt","r") as f:
for line in f.readlines():
if 'set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float")' in line:
continue
text.append(line)
if 'set(LINKER_SCRIPT $' in line:
text.append('set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float")\n')
with open("./CMakeLists.txt","w+") as f:
f.writelines(text)