-
Notifications
You must be signed in to change notification settings - Fork 3
/
install
executable file
·316 lines (284 loc) · 11.6 KB
/
install
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
# Check whether CubeMX/CubeIDE correctly generate the directory.
# If there is no or multpile main.c / main.h in the project,
# Display an error message and abort.
# IF OK, Create variables to point the source directory and include directory.
# Count the include directories
INCDIRNUM=$(dirname `find .. -name main.h` | wc -l )
if [ $INCDIRNUM -lt "1" ] ; then
echo "The main.h is not found. Process aborting"
exit
fi
if [ $INCDIRNUM -gt "1" ] ; then
echo "Too many main.h are found. Process aborting"
exit
fi
# Count the source directories
SRCDIRNUM=$(dirname `find .. -name main.c` | wc -l )
if [ $SRCDIRNUM -lt "1" ] ; then
echo "The main.c is not found. Process aborting"
exit
fi
if [ $SRCDIRNUM -gt "1" ] ; then
echo "Too many main.c are found. Process aborting"
exit
fi
# Count the startup directories.
SUPDIRNUM=$(dirname `find .. -name startup_stm32*.s` | wc -l )
if [ $SUPDIRNUM -lt "1" ] ; then
echo "The startup_stm32*.s is not found. Process aborting"
exit
fi
if [ $SUPDIRNUM -gt "1" ] ; then
echo "Too many startup_stm32*.s are found. Process aborting"
exit
fi
# Variables to point the source, include and startup directories.
SRCDIR=$(dirname `find .. -name main.c`)
INCDIR=$(dirname `find .. -name main.h`)
SUPDIR=$(dirname `find .. -name startup_stm32*.s`)
echo Found $SRCDIR as the source directory.
echo Found $INCDIR as the include directory.
echo Found $SUPDIR as the startup directory.
# Copy the template files. Deleting ".tmp" extention by basename command.
if [ ! -f ${SRCDIR}/murasaki_platform.cpp ]; then
for filename in template/*.hpp.tmp; do
cp $filename ${INCDIR}/`basename $filename .tmp`
done
for filename in template/*.cpp.tmp; do
cp $filename ${SRCDIR}/`basename $filename .tmp`
done
echo template files copied
else
echo template files exist. No change.
fi
# check whether string $2 is found in file $1
# if exist, do nothing.
# if not exist, quit script.
function check_target_string() {
grep "$2" $1 > /dev/null
if [ $? -ne 0 ] ; then
echo \"$2\" not found in $1
exit 1
fi
}
# Modify the Src/main.c like following
# 1. Insert include file.
# Find the line which has "USER CODE BEGIN Includes", and then
# add the following line after that one.
# #include "murasaki_platform.hpp"
# 2. Find the line which has "USER CODE END 6" and then,
# add the following line before that one.
# CustomAssertFailed(file, line);
#
TARGET_FILE=${SRCDIR}/main.c
check_target_string ${TARGET_FILE} "USER CODE BEGIN Includes"
check_target_string ${TARGET_FILE} "USER CODE END 6"
# Is murasaki already isntalled in main.c file?
grep "CustomAssertFailed" ${TARGET_FILE} > /dev/null
if [ $? -ne 0 ]; then
cat ${TARGET_FILE} | awk -f <(cat - <<- 'EDO'
/USER CODE END 6/{print " CustomAssertFailed(file, line);"}
{print $0}
/USER CODE BEGIN Includes/{print "#include \"murasaki_platform.hpp\""}
EDO
) > foo.txt
mv foo.txt ${TARGET_FILE}
echo ${TARGET_FILE} : Added CustomAssertFailed\(\) calling and include file.
else
echo ${TARGET_FILE} have been already modified for CustomAssertFailed\(\). No change.
fi
# Modify StartDefaultTask() like following
# 1. Find out the file which has StartDefaultTas()
# 2. Insert the platform code.
# Find the line which has "USER CODE BEGIN 5" and then,
# add the following lines after that one.
# InitPlatform();
# ExecPlatform();
# 3. Insert the platform code.
# Find the line which has "USER CODE BEGIN defaultTask" and then,
# add the following lines after that one.
# InitPlatform();
# ExecPlatform();
#
# For the old product STM32F446, CubeIDE generate the StartDEfaultTask()
# inside main.c with the tag "USER CODE BEGIN 5".
# For the new product STM32H506, CubeIDE generate the StartDEfaultTask()
# inside app_freertos.c with the tag "USER CODE BEGIN defaultTask".
#
# Above algorithm works fine for both cases.
# Count how many files have "StartDefaullTask"
FREE_RTOS_STUB_HOOK="StartDefaultTask"
FREE_RTOS_STUB_NUM=$(grep ${FREE_RTOS_STUB_HOOK} ${SRCDIR}/*.c -l | wc -l)
# 0 => error
if [ ${FREE_RTOS_STUB_NUM} == 0 ]; then
echo \"${FREE_RTOS_STUB_HOOK}\" not found in ${SRCDIR} directory.
exit 1
fi
# >1 => error
if [ ${FREE_RTOS_STUB_NUM} -gt 1 ]; then
echo Too many \"${FREE_RTOS_STUB_HOOK}\" found in ${SRCDIR} directory.
exit 1
fi
# Obtain the filename which contains "USER CODE BEGIN 5"
TARGET_FILE=$(grep ${FREE_RTOS_STUB_HOOK} ${SRCDIR}/*.c -l )
# Is murasaki already isntalled in FreeRTOS defoult startup function?
grep "InitPlatform" ${TARGET_FILE} > /dev/null
if [ $? -ne 0 ]; then
cat ${TARGET_FILE} | awk -f <(cat - <<- 'EDO'
{print $0}
/USER CODE BEGIN Includes/{print "#include \"murasaki_platform.hpp\""}
/USER CODE BEGIN 5/ {print " InitPlatform();"; print " ExecPlatform();"}
/USER CODE BEGIN defaultTask/{print " InitPlatform();"; print " ExecPlatform();"}
EDO
) > foo.txt
mv foo.txt ${TARGET_FILE}
echo ${TARGET_FILE} : Added InitPlatform\(\)/ExecPlatform\(\) calling code.
else
echo ${TARGET_FILE} have been already modified for StartDefaultTask\(\). No change.
fi
# Modify the startup/startup_stm32*.s like following.
# Find the line which has "Deafult_Handler:" and then,
# Substitute that line with following lines :
# .global CustomDefaultHandler
# Default_Handler:
# #if (__ARM_ARCH == 6 )
# ldr r0, = CustomDefaultHandler
# bx r0
# #else
# b.w CustomDefaultHandler
# #endif
#
# Note : Because of the restriction of the b instruction,
# ARM v6 architecture destroy the R0 register.
# Check whether appropriate file and string exist
TARGET_FILE=`ls ${SUPDIR}/startup_stm32*.s`
check_target_string ${TARGET_FILE} "Default_Handler\:"
# Is murasaki already isntalled in this file?
grep "CustomDefaultHandler" ${TARGET_FILE} > /dev/null
if [ $? -ne 0 ]; then # if not, install
cat ${TARGET_FILE} | awk -f <(cat - <<- 'EDO'
/Default_Handler\:/{print " .global CustomDefaultHandler"}
{print $0}
/Default_Handler\:/{print "#if (__ARM_ARCH == 6 )"; print " ldr r0, = CustomDefaultHandler"; print " bx r0"; print "#else"; print " b.w CustomDefaultHandler"; print "#endif" }
EDO
) > foo.txt
mv foo.txt ${TARGET_FILE}
echo ${TARGET_FILE} : Added jump code to CustomDefaultHandler.
else
echo ${TARGET_FILE} have been already modified. No change.
fi
# Modify the Src/stm32*_it.c like following.
# 1. Insert include file.
# Find the line which has "USER CODE BEGIN Includes", and then
# add the following line after that one.
# #include "murasaki_platform.hpp"
# 2. Insert the HardFault code
# Find the line which has "USER CODE BEGIN HardFault_IRQn 0" and then,
# add the following lines after that one.
# CustomDefaultHandler();
# Check whether appropriate file and string exist
TARGET_FILE=`ls ${SRCDIR}/stm32*_it.c`
check_target_string ${TARGET_FILE} "USER CODE BEGIN Includes"
check_target_string ${TARGET_FILE} "USER CODE BEGIN HardFault_IRQn 0"
# Is murasaki already isntalled in this file?
grep "CustomDefaultHandler" ${TARGET_FILE} > /dev/null
if [ $? -ne 0 ]; then # if not, install
cat `ls ${SRCDIR}/stm32*_it.c` | awk -f <(cat - <<- 'EDO'
{print $0}
/USER CODE BEGIN Includes/{print "#include \"murasaki_platform.hpp\""}
/USER CODE BEGIN HardFault_IRQn 0/{print " CustomDefaultHandler();"}
EDO
) > foo.txt
mv foo.txt `ls ${SRCDIR}/stm32*_it.c`
echo ${TARGET_FILE} : Added jump code to CustomDefaultHandler.
else
echo ${TARGET_FILE} have been already modified. No change.
fi
# Make an include stub for murasaki like following.
# 1. Detect the interrupt include file
# CubeMX generate a ${INCDIR}/stm32*_it.h file as interrupt conrol header file.
# Pick up this file by ls command.
# Now, the string in pipe is ${INCDIR}/stm32?????_it.h
# 2. Remove the path
# xargs command applies this string the pipe, to the basename command as parameter.
# basename command takes the path out.
# Now, the string in pipe is stm32?????_it.h
# 3. Change the file name.
# substitute it with hal.
# Now, the string pipe is stm32????_hal.h
# 4. Construct the include statement in C language.
# Surround the given string by the #include statement.
# Now, the string is #include<stm32?????_hal.h>
# This is the include statement to include the appropriate HAL file.
cat > ${INCDIR}/murasaki_include_stub.h <<EOF
/**
* @file murasaki_include_stub.h
* @brief Stub to include the HAL headers.
* @details
* The CubeIDE add the STM32 microprocessor product name as pre-defined macro when a file is compiled.
* For example, following is the macro definition for STM32F446 processor at the compiler command line.
* @code
* -DSTM32F446xx
* @endcode
*
* On the other hand, this is not enough to determine the appropriate include file inside murasaki_defs.hpp.
* As a result, there are difficulties to include the appropriate file.
*
* One of the naive approach is to emulate all possible pre-defined macro to determine the filename as following.
*
* @code
* #elif defined (STM32F405xx) || defined (STM32F415xx) || defined (STM32F407xx) || defined (STM32F417xx) || \
* defined (STM32F427xx) || defined (STM32F437xx) || defined (STM32F429xx) || defined (STM32F439xx) || \
* defined (STM32F401xC) || defined (STM32F401xE) || defined (STM32F410Tx) || defined (STM32F410Cx) || \
* defined (STM32F410Rx) || defined (STM32F411xE) || defined (STM32F446xx) || defined (STM32F469xx) || \
* defined (STM32F479xx) || defined (STM32F412Cx) || defined (STM32F412Rx) || defined (STM32F412Vx) || \
* defined (STM32F412Zx) || defined (STM32F413xx) || defined (STM32F423xx)
* #include "stm32f4xx_hal.h"
* @endcode
*
* This is easy to understand. But boring to maintain.
*
* This stub is alternate way. @ref murasaki_defs.hpp is including this file (murasaki_include_stub.h).
* And this stub file include the appropriate HAL header file.
* This stub file is generated by murasaki/install script.
* Thus, user doesn't need to maintain this file.
*
*/
EOF
ls ${INCDIR}/stm32*it.h | xargs basename | sed s/it/hal/ - | awk '{print("#include<" $0 ">")}' >> ${INCDIR}/murasaki_include_stub.h
# In addition stm32?????_hal.h, the cmsis_os[?].h is target dependent.
# Some targets use cmsis_os.h, some targets use cmsis_os2.h.
# To solve this problem, we will check main.c and investigate which cmsis_os.h or cmsis_os2.h is used.
# And include it in murasaki_include_stub.h.
cat >> ${INCDIR}/murasaki_include_stub.h <<EOF
/**
* @brief Stub to include cmsis_os header file.
* @details
* The CubeIDE was using cmsis_os.h as the header file fo the CMSIS OS. Recently,
* x-CUBE-FREERTOS started to use the cmsis_os2.h as header, for STM32H5 series.
*
* Murasaki is not using the functionality of the CMSIS OS. But it is referring the
* definition of the task priority. For example, we refer osPriorityAboveNormal.
* This is essential because of the wired relation
* between the CORTEX-M architecture and FREERTOS task priority.
*
* On the other hand, there is no way to determine which cmsis_os.h or cmsis_os2.h is
* used.
*
* This is alternate solution. We check which cmsis_os.h or cmsis_os2.h is included in
* main.c, and then, place same file as include file in the murasaki_include_stub.h.
*/
EOF
# Is "cmsis_os.h" in the main.c?
if grep "cmsis_os.h" ${SRCDIR}/main.c > /dev/null ; then
echo "#include <cmsis_os.h>" >> ${INCDIR}/murasaki_include_stub.h
# Is "cmsis_os2.h" in the main.c?
elif grep "cmsis_os2.h" ${SRCDIR}/main.c > /dev/null ; then
echo "#include <cmsis_os2.h>" >> ${INCDIR}/murasaki_include_stub.h
else
echo Neither "cmsis_os.h" nor "cmsis_os2.h" were found.
echo ${INCDIR}/murasaki_include_stub.h is incmplete.
exit 1
fi
echo ${INCDIR}/murasaki_include_stub.h created.